[PATCH] USB Serial: move old changelog comments out of source code
[linux/fpc-iii.git] / net / dccp / options.c
blob0a76426c9aeab322f6eae5dd8e8e2309e1454d98
1 /*
2 * net/dccp/options.c
4 * An implementation of the DCCP protocol
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>
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.
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>
21 #include "ackvec.h"
22 #include "ccid.h"
23 #include "dccp.h"
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_rx_ccid = DCCPF_INITIAL_CCID,
29 .dccpo_tx_ccid = DCCPF_INITIAL_CCID,
30 .dccpo_send_ack_vector = DCCPF_INITIAL_SEND_ACK_VECTOR,
31 .dccpo_send_ndp_count = DCCPF_INITIAL_SEND_NDP_COUNT,
34 void dccp_options_init(struct dccp_options *dccpo)
36 memcpy(dccpo, &dccpo_default_values, sizeof(*dccpo));
39 static u32 dccp_decode_value_var(const unsigned char *bf, const u8 len)
41 u32 value = 0;
43 if (len > 3)
44 value += *bf++ << 24;
45 if (len > 2)
46 value += *bf++ << 16;
47 if (len > 1)
48 value += *bf++ << 8;
49 if (len > 0)
50 value += *bf;
52 return value;
55 int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
57 struct dccp_sock *dp = dccp_sk(sk);
58 #ifdef CONFIG_IP_DCCP_DEBUG
59 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
60 "CLIENT rx opt: " : "server rx opt: ";
61 #endif
62 const struct dccp_hdr *dh = dccp_hdr(skb);
63 const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type;
64 unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
65 unsigned char *opt_ptr = options;
66 const unsigned char *opt_end = (unsigned char *)dh +
67 (dh->dccph_doff * 4);
68 struct dccp_options_received *opt_recv = &dp->dccps_options_received;
69 unsigned char opt, len;
70 unsigned char *value;
71 u32 elapsed_time;
73 memset(opt_recv, 0, sizeof(*opt_recv));
75 while (opt_ptr != opt_end) {
76 opt = *opt_ptr++;
77 len = 0;
78 value = NULL;
80 /* Check if this isn't a single byte option */
81 if (opt > DCCPO_MAX_RESERVED) {
82 if (opt_ptr == opt_end)
83 goto out_invalid_option;
85 len = *opt_ptr++;
86 if (len < 3)
87 goto out_invalid_option;
89 * Remove the type and len fields, leaving
90 * just the value size
92 len -= 2;
93 value = opt_ptr;
94 opt_ptr += len;
96 if (opt_ptr > opt_end)
97 goto out_invalid_option;
100 switch (opt) {
101 case DCCPO_PADDING:
102 break;
103 case DCCPO_NDP_COUNT:
104 if (len > 3)
105 goto out_invalid_option;
107 opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
108 dccp_pr_debug("%sNDP count=%d\n", debug_prefix,
109 opt_recv->dccpor_ndp);
110 break;
111 case DCCPO_ACK_VECTOR_0:
112 case DCCPO_ACK_VECTOR_1:
113 if (pkt_type == DCCP_PKT_DATA)
114 continue;
116 if (dp->dccps_options.dccpo_send_ack_vector &&
117 dccp_ackvec_parse(sk, skb, opt, value, len))
118 goto out_invalid_option;
119 break;
120 case DCCPO_TIMESTAMP:
121 if (len != 4)
122 goto out_invalid_option;
124 opt_recv->dccpor_timestamp = ntohl(*(u32 *)value);
126 dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp;
127 dccp_timestamp(sk, &dp->dccps_timestamp_time);
129 dccp_pr_debug("%sTIMESTAMP=%u, ackno=%llu\n",
130 debug_prefix, opt_recv->dccpor_timestamp,
131 (unsigned long long)
132 DCCP_SKB_CB(skb)->dccpd_ack_seq);
133 break;
134 case DCCPO_TIMESTAMP_ECHO:
135 if (len != 4 && len != 6 && len != 8)
136 goto out_invalid_option;
138 opt_recv->dccpor_timestamp_echo = ntohl(*(u32 *)value);
140 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, ",
141 debug_prefix,
142 opt_recv->dccpor_timestamp_echo,
143 len + 2,
144 (unsigned long long)
145 DCCP_SKB_CB(skb)->dccpd_ack_seq);
148 if (len == 4)
149 break;
151 if (len == 6)
152 elapsed_time = ntohs(*(u16 *)(value + 4));
153 else
154 elapsed_time = ntohl(*(u32 *)(value + 4));
156 /* Give precedence to the biggest ELAPSED_TIME */
157 if (elapsed_time > opt_recv->dccpor_elapsed_time)
158 opt_recv->dccpor_elapsed_time = elapsed_time;
159 break;
160 case DCCPO_ELAPSED_TIME:
161 if (len != 2 && len != 4)
162 goto out_invalid_option;
164 if (pkt_type == DCCP_PKT_DATA)
165 continue;
167 if (len == 2)
168 elapsed_time = ntohs(*(u16 *)value);
169 else
170 elapsed_time = ntohl(*(u32 *)value);
172 if (elapsed_time > opt_recv->dccpor_elapsed_time)
173 opt_recv->dccpor_elapsed_time = elapsed_time;
175 dccp_pr_debug("%sELAPSED_TIME=%d\n", debug_prefix,
176 elapsed_time);
177 break;
179 * From draft-ietf-dccp-spec-11.txt:
181 * Option numbers 128 through 191 are for
182 * options sent from the HC-Sender to the
183 * HC-Receiver; option numbers 192 through 255
184 * are for options sent from the HC-Receiver to
185 * the HC-Sender.
187 case 128 ... 191: {
188 const u16 idx = value - options;
190 if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
191 opt, len, idx,
192 value) != 0)
193 goto out_invalid_option;
195 break;
196 case 192 ... 255: {
197 const u16 idx = value - options;
199 if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
200 opt, len, idx,
201 value) != 0)
202 goto out_invalid_option;
204 break;
205 default:
206 pr_info("DCCP(%p): option %d(len=%d) not "
207 "implemented, ignoring\n",
208 sk, opt, len);
209 break;
213 return 0;
215 out_invalid_option:
216 DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
217 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR;
218 pr_info("DCCP(%p): invalid option %d, len=%d\n", sk, opt, len);
219 return -1;
222 static void dccp_encode_value_var(const u32 value, unsigned char *to,
223 const unsigned int len)
225 if (len > 3)
226 *to++ = (value & 0xFF000000) >> 24;
227 if (len > 2)
228 *to++ = (value & 0xFF0000) >> 16;
229 if (len > 1)
230 *to++ = (value & 0xFF00) >> 8;
231 if (len > 0)
232 *to++ = (value & 0xFF);
235 static inline int dccp_ndp_len(const int ndp)
237 return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3;
240 void dccp_insert_option(struct sock *sk, struct sk_buff *skb,
241 const unsigned char option,
242 const void *value, const unsigned char len)
244 unsigned char *to;
246 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN) {
247 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
248 "%d option!\n", option);
249 return;
252 DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2;
254 to = skb_push(skb, len + 2);
255 *to++ = option;
256 *to++ = len + 2;
258 memcpy(to, value, len);
261 EXPORT_SYMBOL_GPL(dccp_insert_option);
263 static void dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb)
265 struct dccp_sock *dp = dccp_sk(sk);
266 int ndp = dp->dccps_ndp_count;
268 if (dccp_non_data_packet(skb))
269 ++dp->dccps_ndp_count;
270 else
271 dp->dccps_ndp_count = 0;
273 if (ndp > 0) {
274 unsigned char *ptr;
275 const int ndp_len = dccp_ndp_len(ndp);
276 const int len = ndp_len + 2;
278 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
279 return;
281 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
283 ptr = skb_push(skb, len);
284 *ptr++ = DCCPO_NDP_COUNT;
285 *ptr++ = len;
286 dccp_encode_value_var(ndp, ptr, ndp_len);
290 static inline int dccp_elapsed_time_len(const u32 elapsed_time)
292 return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4;
295 void dccp_insert_option_elapsed_time(struct sock *sk,
296 struct sk_buff *skb,
297 u32 elapsed_time)
299 #ifdef CONFIG_IP_DCCP_DEBUG
300 struct dccp_sock *dp = dccp_sk(sk);
301 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
302 "CLIENT TX opt: " : "server TX opt: ";
303 #endif
304 const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
305 const int len = 2 + elapsed_time_len;
306 unsigned char *to;
308 if (elapsed_time_len == 0)
309 return;
311 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
312 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
313 "insert elapsed time!\n");
314 return;
317 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
319 to = skb_push(skb, len);
320 *to++ = DCCPO_ELAPSED_TIME;
321 *to++ = len;
323 if (elapsed_time_len == 2) {
324 const u16 var16 = htons((u16)elapsed_time);
325 memcpy(to, &var16, 2);
326 } else {
327 const u32 var32 = htonl(elapsed_time);
328 memcpy(to, &var32, 4);
331 dccp_pr_debug("%sELAPSED_TIME=%u, len=%d, seqno=%llu\n",
332 debug_prefix, elapsed_time,
333 len,
334 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
337 EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time);
339 void dccp_timestamp(const struct sock *sk, struct timeval *tv)
341 const struct dccp_sock *dp = dccp_sk(sk);
343 do_gettimeofday(tv);
344 tv->tv_sec -= dp->dccps_epoch.tv_sec;
345 tv->tv_usec -= dp->dccps_epoch.tv_usec;
347 while (tv->tv_usec < 0) {
348 tv->tv_sec--;
349 tv->tv_usec += USEC_PER_SEC;
353 EXPORT_SYMBOL_GPL(dccp_timestamp);
355 void dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb)
357 struct timeval tv;
358 u32 now;
360 dccp_timestamp(sk, &tv);
361 now = timeval_usecs(&tv) / 10;
362 /* yes this will overflow but that is the point as we want a
363 * 10 usec 32 bit timer which mean it wraps every 11.9 hours */
365 now = htonl(now);
366 dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
369 EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp);
371 static void dccp_insert_option_timestamp_echo(struct sock *sk,
372 struct sk_buff *skb)
374 struct dccp_sock *dp = dccp_sk(sk);
375 #ifdef CONFIG_IP_DCCP_DEBUG
376 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
377 "CLIENT TX opt: " : "server TX opt: ";
378 #endif
379 struct timeval now;
380 u32 tstamp_echo;
381 u32 elapsed_time;
382 int len, elapsed_time_len;
383 unsigned char *to;
385 dccp_timestamp(sk, &now);
386 elapsed_time = timeval_delta(&now, &dp->dccps_timestamp_time) / 10;
387 elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
388 len = 6 + elapsed_time_len;
390 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
391 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
392 "timestamp echo!\n");
393 return;
396 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
398 to = skb_push(skb, len);
399 *to++ = DCCPO_TIMESTAMP_ECHO;
400 *to++ = len;
402 tstamp_echo = htonl(dp->dccps_timestamp_echo);
403 memcpy(to, &tstamp_echo, 4);
404 to += 4;
406 if (elapsed_time_len == 2) {
407 const u16 var16 = htons((u16)elapsed_time);
408 memcpy(to, &var16, 2);
409 } else if (elapsed_time_len == 4) {
410 const u32 var32 = htonl(elapsed_time);
411 memcpy(to, &var32, 4);
414 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, seqno=%llu\n",
415 debug_prefix, dp->dccps_timestamp_echo,
416 len,
417 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
419 dp->dccps_timestamp_echo = 0;
420 dp->dccps_timestamp_time.tv_sec = 0;
421 dp->dccps_timestamp_time.tv_usec = 0;
424 void dccp_insert_options(struct sock *sk, struct sk_buff *skb)
426 struct dccp_sock *dp = dccp_sk(sk);
428 DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
430 if (dp->dccps_options.dccpo_send_ndp_count)
431 dccp_insert_option_ndp(sk, skb);
433 if (!dccp_packet_without_ack(skb)) {
434 if (dp->dccps_options.dccpo_send_ack_vector &&
435 dccp_ackvec_pending(dp->dccps_hc_rx_ackvec))
436 dccp_insert_option_ackvec(sk, skb);
437 if (dp->dccps_timestamp_echo != 0)
438 dccp_insert_option_timestamp_echo(sk, skb);
441 if (dp->dccps_hc_rx_insert_options) {
442 ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb);
443 dp->dccps_hc_rx_insert_options = 0;
445 if (dp->dccps_hc_tx_insert_options) {
446 ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb);
447 dp->dccps_hc_tx_insert_options = 0;
450 /* XXX: insert other options when appropriate */
452 if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) {
453 /* The length of all options has to be a multiple of 4 */
454 int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4;
456 if (padding != 0) {
457 padding = 4 - padding;
458 memset(skb_push(skb, padding), 0, padding);
459 DCCP_SKB_CB(skb)->dccpd_opt_len += padding;