Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/dccp/options.c |
| 3 | * |
| 4 | * An implementation of the DCCP protocol |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 5 | * Copyright (c) 2005 Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org> |
| 6 | * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net> |
| 7 | * Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz> |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version |
| 12 | * 2 of the License, or (at your option) any later version. |
| 13 | */ |
| 14 | #include <linux/config.h> |
| 15 | #include <linux/dccp.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/skbuff.h> |
| 20 | |
Arnaldo Carvalho de Melo | ae31c33 | 2005-09-18 00:17:51 -0700 | [diff] [blame^] | 21 | #include "ackvec.h" |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 22 | #include "ccid.h" |
| 23 | #include "dccp.h" |
| 24 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 25 | /* stores the default values for new connection. may be changed with sysctl */ |
| 26 | static const struct dccp_options dccpo_default_values = { |
| 27 | .dccpo_sequence_window = DCCPF_INITIAL_SEQUENCE_WINDOW, |
| 28 | .dccpo_ccid = DCCPF_INITIAL_CCID, |
| 29 | .dccpo_send_ack_vector = DCCPF_INITIAL_SEND_ACK_VECTOR, |
| 30 | .dccpo_send_ndp_count = DCCPF_INITIAL_SEND_NDP_COUNT, |
| 31 | }; |
| 32 | |
| 33 | void dccp_options_init(struct dccp_options *dccpo) |
| 34 | { |
| 35 | memcpy(dccpo, &dccpo_default_values, sizeof(*dccpo)); |
| 36 | } |
| 37 | |
| 38 | static u32 dccp_decode_value_var(const unsigned char *bf, const u8 len) |
| 39 | { |
| 40 | u32 value = 0; |
| 41 | |
| 42 | if (len > 3) |
| 43 | value += *bf++ << 24; |
| 44 | if (len > 2) |
| 45 | value += *bf++ << 16; |
| 46 | if (len > 1) |
| 47 | value += *bf++ << 8; |
| 48 | if (len > 0) |
| 49 | value += *bf; |
| 50 | |
| 51 | return value; |
| 52 | } |
| 53 | |
| 54 | int dccp_parse_options(struct sock *sk, struct sk_buff *skb) |
| 55 | { |
| 56 | struct dccp_sock *dp = dccp_sk(sk); |
Arnaldo Carvalho de Melo | 725ba8e | 2005-08-13 20:35:39 -0300 | [diff] [blame] | 57 | #ifdef CONFIG_IP_DCCP_DEBUG |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 58 | const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ? |
| 59 | "CLIENT rx opt: " : "server rx opt: "; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 60 | #endif |
| 61 | const struct dccp_hdr *dh = dccp_hdr(skb); |
| 62 | const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type; |
| 63 | unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb); |
| 64 | unsigned char *opt_ptr = options; |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 65 | const unsigned char *opt_end = (unsigned char *)dh + |
| 66 | (dh->dccph_doff * 4); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 67 | struct dccp_options_received *opt_recv = &dp->dccps_options_received; |
| 68 | unsigned char opt, len; |
| 69 | unsigned char *value; |
Arnaldo Carvalho de Melo | 1c14ac0 | 2005-09-09 02:32:01 -0300 | [diff] [blame] | 70 | u32 elapsed_time; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 71 | |
| 72 | memset(opt_recv, 0, sizeof(*opt_recv)); |
| 73 | |
| 74 | while (opt_ptr != opt_end) { |
| 75 | opt = *opt_ptr++; |
| 76 | len = 0; |
| 77 | value = NULL; |
| 78 | |
| 79 | /* Check if this isn't a single byte option */ |
| 80 | if (opt > DCCPO_MAX_RESERVED) { |
| 81 | if (opt_ptr == opt_end) |
| 82 | goto out_invalid_option; |
| 83 | |
| 84 | len = *opt_ptr++; |
| 85 | if (len < 3) |
| 86 | goto out_invalid_option; |
| 87 | /* |
| 88 | * Remove the type and len fields, leaving |
| 89 | * just the value size |
| 90 | */ |
| 91 | len -= 2; |
| 92 | value = opt_ptr; |
| 93 | opt_ptr += len; |
| 94 | |
| 95 | if (opt_ptr > opt_end) |
| 96 | goto out_invalid_option; |
| 97 | } |
| 98 | |
| 99 | switch (opt) { |
| 100 | case DCCPO_PADDING: |
| 101 | break; |
| 102 | case DCCPO_NDP_COUNT: |
| 103 | if (len > 3) |
| 104 | goto out_invalid_option; |
| 105 | |
| 106 | opt_recv->dccpor_ndp = dccp_decode_value_var(value, len); |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 107 | dccp_pr_debug("%sNDP count=%d\n", debug_prefix, |
| 108 | opt_recv->dccpor_ndp); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 109 | break; |
| 110 | case DCCPO_ACK_VECTOR_0: |
Arnaldo Carvalho de Melo | ae31c33 | 2005-09-18 00:17:51 -0700 | [diff] [blame^] | 111 | case DCCPO_ACK_VECTOR_1: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 112 | if (pkt_type == DCCP_PKT_DATA) |
| 113 | continue; |
| 114 | |
Arnaldo Carvalho de Melo | ae31c33 | 2005-09-18 00:17:51 -0700 | [diff] [blame^] | 115 | if (dp->dccps_options.dccpo_send_ack_vector && |
| 116 | dccp_ackvec_parse(sk, skb, opt, value, len)) |
| 117 | goto out_invalid_option; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 118 | break; |
| 119 | case DCCPO_TIMESTAMP: |
| 120 | if (len != 4) |
| 121 | goto out_invalid_option; |
| 122 | |
| 123 | opt_recv->dccpor_timestamp = ntohl(*(u32 *)value); |
| 124 | |
| 125 | dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp; |
Arnaldo Carvalho de Melo | b0e5678 | 2005-09-09 02:38:35 -0300 | [diff] [blame] | 126 | dccp_timestamp(sk, &dp->dccps_timestamp_time); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 127 | |
| 128 | dccp_pr_debug("%sTIMESTAMP=%u, ackno=%llu\n", |
| 129 | debug_prefix, opt_recv->dccpor_timestamp, |
David S. Miller | f6ccf55 | 2005-08-09 20:27:14 -0700 | [diff] [blame] | 130 | (unsigned long long) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 131 | DCCP_SKB_CB(skb)->dccpd_ack_seq); |
| 132 | break; |
| 133 | case DCCPO_TIMESTAMP_ECHO: |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 134 | if (len != 4 && len != 6 && len != 8) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 135 | goto out_invalid_option; |
| 136 | |
| 137 | opt_recv->dccpor_timestamp_echo = ntohl(*(u32 *)value); |
| 138 | |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 139 | dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, ", |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 140 | debug_prefix, |
| 141 | opt_recv->dccpor_timestamp_echo, |
David S. Miller | f6ccf55 | 2005-08-09 20:27:14 -0700 | [diff] [blame] | 142 | len + 2, |
| 143 | (unsigned long long) |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 144 | DCCP_SKB_CB(skb)->dccpd_ack_seq); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 145 | |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 146 | |
Arnaldo Carvalho de Melo | 1c14ac0 | 2005-09-09 02:32:01 -0300 | [diff] [blame] | 147 | if (len == 4) |
| 148 | break; |
| 149 | |
| 150 | if (len == 6) |
| 151 | elapsed_time = ntohs(*(u16 *)(value + 4)); |
| 152 | else |
| 153 | elapsed_time = ntohl(*(u32 *)(value + 4)); |
| 154 | |
| 155 | /* Give precedence to the biggest ELAPSED_TIME */ |
| 156 | if (elapsed_time > opt_recv->dccpor_elapsed_time) |
| 157 | opt_recv->dccpor_elapsed_time = elapsed_time; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 158 | break; |
| 159 | case DCCPO_ELAPSED_TIME: |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 160 | if (len != 2 && len != 4) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 161 | goto out_invalid_option; |
| 162 | |
| 163 | if (pkt_type == DCCP_PKT_DATA) |
| 164 | continue; |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 165 | |
| 166 | if (len == 2) |
Arnaldo Carvalho de Melo | 1c14ac0 | 2005-09-09 02:32:01 -0300 | [diff] [blame] | 167 | elapsed_time = ntohs(*(u16 *)value); |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 168 | else |
Arnaldo Carvalho de Melo | 1c14ac0 | 2005-09-09 02:32:01 -0300 | [diff] [blame] | 169 | elapsed_time = ntohl(*(u32 *)value); |
| 170 | |
| 171 | if (elapsed_time > opt_recv->dccpor_elapsed_time) |
| 172 | opt_recv->dccpor_elapsed_time = elapsed_time; |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 173 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 174 | dccp_pr_debug("%sELAPSED_TIME=%d\n", debug_prefix, |
Arnaldo Carvalho de Melo | 1c14ac0 | 2005-09-09 02:32:01 -0300 | [diff] [blame] | 175 | elapsed_time); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 176 | break; |
| 177 | /* |
| 178 | * From draft-ietf-dccp-spec-11.txt: |
| 179 | * |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 180 | * Option numbers 128 through 191 are for |
| 181 | * options sent from the HC-Sender to the |
| 182 | * HC-Receiver; option numbers 192 through 255 |
| 183 | * are for options sent from the HC-Receiver to |
| 184 | * the HC-Sender. |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 185 | */ |
| 186 | case 128 ... 191: { |
| 187 | const u16 idx = value - options; |
| 188 | |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 189 | if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk, |
| 190 | opt, len, idx, |
| 191 | value) != 0) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 192 | goto out_invalid_option; |
| 193 | } |
| 194 | break; |
| 195 | case 192 ... 255: { |
| 196 | const u16 idx = value - options; |
| 197 | |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 198 | if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk, |
| 199 | opt, len, idx, |
| 200 | value) != 0) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 201 | goto out_invalid_option; |
| 202 | } |
| 203 | break; |
| 204 | default: |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 205 | pr_info("DCCP(%p): option %d(len=%d) not " |
| 206 | "implemented, ignoring\n", |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 207 | sk, opt, len); |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | |
| 214 | out_invalid_option: |
| 215 | DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT); |
| 216 | DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR; |
| 217 | pr_info("DCCP(%p): invalid option %d, len=%d\n", sk, opt, len); |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | static void dccp_encode_value_var(const u32 value, unsigned char *to, |
| 222 | const unsigned int len) |
| 223 | { |
| 224 | if (len > 3) |
| 225 | *to++ = (value & 0xFF000000) >> 24; |
| 226 | if (len > 2) |
| 227 | *to++ = (value & 0xFF0000) >> 16; |
| 228 | if (len > 1) |
| 229 | *to++ = (value & 0xFF00) >> 8; |
| 230 | if (len > 0) |
| 231 | *to++ = (value & 0xFF); |
| 232 | } |
| 233 | |
| 234 | static inline int dccp_ndp_len(const int ndp) |
| 235 | { |
| 236 | return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3; |
| 237 | } |
| 238 | |
| 239 | void dccp_insert_option(struct sock *sk, struct sk_buff *skb, |
| 240 | const unsigned char option, |
| 241 | const void *value, const unsigned char len) |
| 242 | { |
| 243 | unsigned char *to; |
| 244 | |
| 245 | if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN) { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 246 | LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert " |
| 247 | "%d option!\n", option); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 248 | return; |
| 249 | } |
| 250 | |
| 251 | DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2; |
| 252 | |
| 253 | to = skb_push(skb, len + 2); |
| 254 | *to++ = option; |
| 255 | *to++ = len + 2; |
| 256 | |
| 257 | memcpy(to, value, len); |
| 258 | } |
| 259 | |
| 260 | EXPORT_SYMBOL_GPL(dccp_insert_option); |
| 261 | |
| 262 | static void dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb) |
| 263 | { |
| 264 | struct dccp_sock *dp = dccp_sk(sk); |
| 265 | int ndp = dp->dccps_ndp_count; |
| 266 | |
| 267 | if (dccp_non_data_packet(skb)) |
| 268 | ++dp->dccps_ndp_count; |
| 269 | else |
| 270 | dp->dccps_ndp_count = 0; |
| 271 | |
| 272 | if (ndp > 0) { |
| 273 | unsigned char *ptr; |
| 274 | const int ndp_len = dccp_ndp_len(ndp); |
| 275 | const int len = ndp_len + 2; |
| 276 | |
| 277 | if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) |
| 278 | return; |
| 279 | |
| 280 | DCCP_SKB_CB(skb)->dccpd_opt_len += len; |
| 281 | |
| 282 | ptr = skb_push(skb, len); |
| 283 | *ptr++ = DCCPO_NDP_COUNT; |
| 284 | *ptr++ = len; |
| 285 | dccp_encode_value_var(ndp, ptr, ndp_len); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | static inline int dccp_elapsed_time_len(const u32 elapsed_time) |
| 290 | { |
Ian McDonald | b1c9fe7 | 2005-08-18 20:45:29 -0300 | [diff] [blame] | 291 | return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void dccp_insert_option_elapsed_time(struct sock *sk, |
| 295 | struct sk_buff *skb, |
| 296 | u32 elapsed_time) |
| 297 | { |
Arnaldo Carvalho de Melo | 725ba8e | 2005-08-13 20:35:39 -0300 | [diff] [blame] | 298 | #ifdef CONFIG_IP_DCCP_DEBUG |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 299 | struct dccp_sock *dp = dccp_sk(sk); |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 300 | const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ? |
| 301 | "CLIENT TX opt: " : "server TX opt: "; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 302 | #endif |
| 303 | const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time); |
| 304 | const int len = 2 + elapsed_time_len; |
| 305 | unsigned char *to; |
| 306 | |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 307 | if (elapsed_time_len == 0) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 308 | return; |
| 309 | |
| 310 | if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 311 | LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to " |
| 312 | "insert elapsed time!\n"); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
| 316 | DCCP_SKB_CB(skb)->dccpd_opt_len += len; |
| 317 | |
| 318 | to = skb_push(skb, len); |
| 319 | *to++ = DCCPO_ELAPSED_TIME; |
| 320 | *to++ = len; |
| 321 | |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 322 | if (elapsed_time_len == 2) { |
| 323 | const u16 var16 = htons((u16)elapsed_time); |
| 324 | memcpy(to, &var16, 2); |
| 325 | } else { |
| 326 | const u32 var32 = htonl(elapsed_time); |
| 327 | memcpy(to, &var32, 4); |
| 328 | } |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 329 | |
| 330 | dccp_pr_debug("%sELAPSED_TIME=%u, len=%d, seqno=%llu\n", |
| 331 | debug_prefix, elapsed_time, |
David S. Miller | f6ccf55 | 2005-08-09 20:27:14 -0700 | [diff] [blame] | 332 | len, |
| 333 | (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Arnaldo Carvalho de Melo | d4b81ff | 2005-08-23 21:51:36 -0700 | [diff] [blame] | 336 | EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 337 | |
Arnaldo Carvalho de Melo | b0e5678 | 2005-09-09 02:38:35 -0300 | [diff] [blame] | 338 | void dccp_timestamp(const struct sock *sk, struct timeval *tv) |
| 339 | { |
| 340 | const struct dccp_sock *dp = dccp_sk(sk); |
| 341 | |
| 342 | do_gettimeofday(tv); |
| 343 | tv->tv_sec -= dp->dccps_epoch.tv_sec; |
| 344 | tv->tv_usec -= dp->dccps_epoch.tv_usec; |
| 345 | |
| 346 | while (tv->tv_usec < 0) { |
| 347 | tv->tv_sec--; |
| 348 | tv->tv_usec += USEC_PER_SEC; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | EXPORT_SYMBOL_GPL(dccp_timestamp); |
| 353 | |
Arnaldo Carvalho de Melo | d4b81ff | 2005-08-23 21:51:36 -0700 | [diff] [blame] | 354 | void dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 355 | { |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 356 | struct timeval tv; |
| 357 | u32 now; |
| 358 | |
Arnaldo Carvalho de Melo | b0e5678 | 2005-09-09 02:38:35 -0300 | [diff] [blame] | 359 | dccp_timestamp(sk, &tv); |
| 360 | now = timeval_usecs(&tv) / 10; |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 361 | /* yes this will overflow but that is the point as we want a |
| 362 | * 10 usec 32 bit timer which mean it wraps every 11.9 hours */ |
| 363 | |
| 364 | now = htonl(now); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 365 | dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now)); |
| 366 | } |
| 367 | |
Arnaldo Carvalho de Melo | d4b81ff | 2005-08-23 21:51:36 -0700 | [diff] [blame] | 368 | EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp); |
| 369 | |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 370 | static void dccp_insert_option_timestamp_echo(struct sock *sk, |
| 371 | struct sk_buff *skb) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 372 | { |
| 373 | struct dccp_sock *dp = dccp_sk(sk); |
Arnaldo Carvalho de Melo | 725ba8e | 2005-08-13 20:35:39 -0300 | [diff] [blame] | 374 | #ifdef CONFIG_IP_DCCP_DEBUG |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 375 | const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ? |
| 376 | "CLIENT TX opt: " : "server TX opt: "; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 377 | #endif |
Arnaldo Carvalho de Melo | b0e5678 | 2005-09-09 02:38:35 -0300 | [diff] [blame] | 378 | struct timeval now; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 379 | u32 tstamp_echo; |
Arnaldo Carvalho de Melo | b0e5678 | 2005-09-09 02:38:35 -0300 | [diff] [blame] | 380 | u32 elapsed_time; |
| 381 | int len, elapsed_time_len; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 382 | unsigned char *to; |
| 383 | |
Arnaldo Carvalho de Melo | b0e5678 | 2005-09-09 02:38:35 -0300 | [diff] [blame] | 384 | dccp_timestamp(sk, &now); |
| 385 | elapsed_time = timeval_delta(&now, &dp->dccps_timestamp_time) / 10; |
| 386 | elapsed_time_len = dccp_elapsed_time_len(elapsed_time); |
| 387 | len = 6 + elapsed_time_len; |
| 388 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 389 | if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 390 | LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert " |
| 391 | "timestamp echo!\n"); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | |
| 395 | DCCP_SKB_CB(skb)->dccpd_opt_len += len; |
| 396 | |
| 397 | to = skb_push(skb, len); |
| 398 | *to++ = DCCPO_TIMESTAMP_ECHO; |
| 399 | *to++ = len; |
| 400 | |
| 401 | tstamp_echo = htonl(dp->dccps_timestamp_echo); |
| 402 | memcpy(to, &tstamp_echo, 4); |
| 403 | to += 4; |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 404 | |
| 405 | if (elapsed_time_len == 2) { |
| 406 | const u16 var16 = htons((u16)elapsed_time); |
| 407 | memcpy(to, &var16, 2); |
| 408 | } else if (elapsed_time_len == 4) { |
| 409 | const u32 var32 = htonl(elapsed_time); |
| 410 | memcpy(to, &var32, 4); |
| 411 | } |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 412 | |
| 413 | dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, seqno=%llu\n", |
| 414 | debug_prefix, dp->dccps_timestamp_echo, |
David S. Miller | f6ccf55 | 2005-08-09 20:27:14 -0700 | [diff] [blame] | 415 | len, |
| 416 | (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 417 | |
| 418 | dp->dccps_timestamp_echo = 0; |
Ian McDonald | 1bc0986 | 2005-08-20 00:23:43 -0300 | [diff] [blame] | 419 | dp->dccps_timestamp_time.tv_sec = 0; |
| 420 | dp->dccps_timestamp_time.tv_usec = 0; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void dccp_insert_options(struct sock *sk, struct sk_buff *skb) |
| 424 | { |
| 425 | struct dccp_sock *dp = dccp_sk(sk); |
| 426 | |
| 427 | DCCP_SKB_CB(skb)->dccpd_opt_len = 0; |
| 428 | |
| 429 | if (dp->dccps_options.dccpo_send_ndp_count) |
| 430 | dccp_insert_option_ndp(sk, skb); |
| 431 | |
| 432 | if (!dccp_packet_without_ack(skb)) { |
| 433 | if (dp->dccps_options.dccpo_send_ack_vector && |
Arnaldo Carvalho de Melo | ae31c33 | 2005-09-18 00:17:51 -0700 | [diff] [blame^] | 434 | dccp_ackvec_pending(dp->dccps_hc_rx_ackvec)) |
| 435 | dccp_insert_option_ackvec(sk, skb); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 436 | if (dp->dccps_timestamp_echo != 0) |
| 437 | dccp_insert_option_timestamp_echo(sk, skb); |
| 438 | } |
| 439 | |
Arnaldo Carvalho de Melo | 507d37c | 2005-09-09 02:30:07 -0300 | [diff] [blame] | 440 | if (dp->dccps_hc_rx_insert_options) { |
| 441 | ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb); |
| 442 | dp->dccps_hc_rx_insert_options = 0; |
| 443 | } |
| 444 | if (dp->dccps_hc_tx_insert_options) { |
| 445 | ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb); |
| 446 | dp->dccps_hc_tx_insert_options = 0; |
| 447 | } |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 448 | |
| 449 | /* XXX: insert other options when appropriate */ |
| 450 | |
| 451 | if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) { |
| 452 | /* The length of all options has to be a multiple of 4 */ |
| 453 | int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4; |
| 454 | |
| 455 | if (padding != 0) { |
| 456 | padding = 4 - padding; |
| 457 | memset(skb_push(skb, padding), 0, padding); |
| 458 | DCCP_SKB_CB(skb)->dccpd_opt_len += padding; |
| 459 | } |
| 460 | } |
| 461 | } |