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>
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)
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: ";
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 +
68 struct dccp_options_received *opt_recv = &dp->dccps_options_received;
69 unsigned char opt, len;
73 memset(opt_recv, 0, sizeof(*opt_recv));
75 while (opt_ptr != opt_end) {
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;
87 goto out_invalid_option;
89 * Remove the type and len fields, leaving
96 if (opt_ptr > opt_end)
97 goto out_invalid_option;
103 case DCCPO_NDP_COUNT:
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);
111 case DCCPO_ACK_VECTOR_0:
112 case DCCPO_ACK_VECTOR_1:
113 if (pkt_type == DCCP_PKT_DATA)
116 if (dp->dccps_options.dccpo_send_ack_vector &&
117 dccp_ackvec_parse(sk, skb, opt, value, len))
118 goto out_invalid_option;
120 case DCCPO_TIMESTAMP:
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,
132 DCCP_SKB_CB(skb)->dccpd_ack_seq);
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, ",
142 opt_recv->dccpor_timestamp_echo,
145 DCCP_SKB_CB(skb)->dccpd_ack_seq);
152 elapsed_time = ntohs(*(u16 *)(value + 4));
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;
160 case DCCPO_ELAPSED_TIME:
161 if (len != 2 && len != 4)
162 goto out_invalid_option;
164 if (pkt_type == DCCP_PKT_DATA)
168 elapsed_time = ntohs(*(u16 *)value);
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,
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
188 const u16 idx = value - options;
190 if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
193 goto out_invalid_option;
197 const u16 idx = value - options;
199 if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
202 goto out_invalid_option;
206 pr_info("DCCP(%p): option %d(len=%d) not "
207 "implemented, ignoring\n",
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);
222 static void dccp_encode_value_var(const u32 value, unsigned char *to,
223 const unsigned int len)
226 *to++ = (value & 0xFF000000) >> 24;
228 *to++ = (value & 0xFF0000) >> 16;
230 *to++ = (value & 0xFF00) >> 8;
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)
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);
252 DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2;
254 to = skb_push(skb, 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;
271 dp->dccps_ndp_count = 0;
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)
281 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
283 ptr = skb_push(skb, len);
284 *ptr++ = DCCPO_NDP_COUNT;
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,
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: ";
304 const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
305 const int len = 2 + elapsed_time_len;
308 if (elapsed_time_len == 0)
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");
317 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
319 to = skb_push(skb, len);
320 *to++ = DCCPO_ELAPSED_TIME;
323 if (elapsed_time_len == 2) {
324 const u16 var16 = htons((u16)elapsed_time);
325 memcpy(to, &var16, 2);
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,
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);
344 tv->tv_sec -= dp->dccps_epoch.tv_sec;
345 tv->tv_usec -= dp->dccps_epoch.tv_usec;
347 while (tv->tv_usec < 0) {
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)
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 */
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,
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: ";
382 int len, elapsed_time_len;
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");
396 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
398 to = skb_push(skb, len);
399 *to++ = DCCPO_TIMESTAMP_ECHO;
402 tstamp_echo = htonl(dp->dccps_timestamp_echo);
403 memcpy(to, &tstamp_echo, 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,
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;
457 padding = 4 - padding;
458 memset(skb_push(skb, padding), 0, padding);
459 DCCP_SKB_CB(skb)->dccpd_opt_len += padding;