blob: eabcc8f1c625719e2c3d65b1fcf2c3e7c8adb347 [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
2 * net/dccp/options.c
3 *
4 * An implementation of the DCCP protocol
Ian McDonald1bc09862005-08-20 00:23:43 -03005 * 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 Melo7c657872005-08-09 20:14:34 -07008 *
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
21#include "ccid.h"
22#include "dccp.h"
23
24static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap,
25 struct sock *sk,
26 const u64 ackno,
27 const unsigned char len,
28 const unsigned char *vector);
29
30/* stores the default values for new connection. may be changed with sysctl */
31static const struct dccp_options dccpo_default_values = {
32 .dccpo_sequence_window = DCCPF_INITIAL_SEQUENCE_WINDOW,
33 .dccpo_ccid = DCCPF_INITIAL_CCID,
34 .dccpo_send_ack_vector = DCCPF_INITIAL_SEND_ACK_VECTOR,
35 .dccpo_send_ndp_count = DCCPF_INITIAL_SEND_NDP_COUNT,
36};
37
38void dccp_options_init(struct dccp_options *dccpo)
39{
40 memcpy(dccpo, &dccpo_default_values, sizeof(*dccpo));
41}
42
43static u32 dccp_decode_value_var(const unsigned char *bf, const u8 len)
44{
45 u32 value = 0;
46
47 if (len > 3)
48 value += *bf++ << 24;
49 if (len > 2)
50 value += *bf++ << 16;
51 if (len > 1)
52 value += *bf++ << 8;
53 if (len > 0)
54 value += *bf;
55
56 return value;
57}
58
59int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
60{
61 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -030062#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030063 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
64 "CLIENT rx opt: " : "server rx opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070065#endif
66 const struct dccp_hdr *dh = dccp_hdr(skb);
67 const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type;
68 unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
69 unsigned char *opt_ptr = options;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030070 const unsigned char *opt_end = (unsigned char *)dh +
71 (dh->dccph_doff * 4);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070072 struct dccp_options_received *opt_recv = &dp->dccps_options_received;
73 unsigned char opt, len;
74 unsigned char *value;
75
76 memset(opt_recv, 0, sizeof(*opt_recv));
77
78 while (opt_ptr != opt_end) {
79 opt = *opt_ptr++;
80 len = 0;
81 value = NULL;
82
83 /* Check if this isn't a single byte option */
84 if (opt > DCCPO_MAX_RESERVED) {
85 if (opt_ptr == opt_end)
86 goto out_invalid_option;
87
88 len = *opt_ptr++;
89 if (len < 3)
90 goto out_invalid_option;
91 /*
92 * Remove the type and len fields, leaving
93 * just the value size
94 */
95 len -= 2;
96 value = opt_ptr;
97 opt_ptr += len;
98
99 if (opt_ptr > opt_end)
100 goto out_invalid_option;
101 }
102
103 switch (opt) {
104 case DCCPO_PADDING:
105 break;
106 case DCCPO_NDP_COUNT:
107 if (len > 3)
108 goto out_invalid_option;
109
110 opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300111 dccp_pr_debug("%sNDP count=%d\n", debug_prefix,
112 opt_recv->dccpor_ndp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700113 break;
114 case DCCPO_ACK_VECTOR_0:
115 if (len > DCCP_MAX_ACK_VECTOR_LEN)
116 goto out_invalid_option;
117
118 if (pkt_type == DCCP_PKT_DATA)
119 continue;
120
121 opt_recv->dccpor_ack_vector_len = len;
122 opt_recv->dccpor_ack_vector_idx = value - options;
123
124 dccp_pr_debug("%sACK vector 0, len=%d, ack_ackno=%llu\n",
David S. Millerf6ccf552005-08-09 20:27:14 -0700125 debug_prefix, len,
126 (unsigned long long)
127 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700128 dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq,
129 value, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300130 dccp_ackpkts_check_rcv_ackvector(dp->dccps_hc_rx_ackpkts,
131 sk,
132 DCCP_SKB_CB(skb)->dccpd_ack_seq,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700133 len, value);
134 break;
135 case DCCPO_TIMESTAMP:
136 if (len != 4)
137 goto out_invalid_option;
138
139 opt_recv->dccpor_timestamp = ntohl(*(u32 *)value);
140
141 dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp;
Ian McDonald1bc09862005-08-20 00:23:43 -0300142 do_gettimeofday(&dp->dccps_timestamp_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700143
144 dccp_pr_debug("%sTIMESTAMP=%u, ackno=%llu\n",
145 debug_prefix, opt_recv->dccpor_timestamp,
David S. Millerf6ccf552005-08-09 20:27:14 -0700146 (unsigned long long)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700147 DCCP_SKB_CB(skb)->dccpd_ack_seq);
148 break;
149 case DCCPO_TIMESTAMP_ECHO:
Ian McDonald1bc09862005-08-20 00:23:43 -0300150 if (len != 4 && len != 6 && len != 8)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700151 goto out_invalid_option;
152
153 opt_recv->dccpor_timestamp_echo = ntohl(*(u32 *)value);
154
Ian McDonald1bc09862005-08-20 00:23:43 -0300155 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, ",
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300156 debug_prefix,
157 opt_recv->dccpor_timestamp_echo,
David S. Millerf6ccf552005-08-09 20:27:14 -0700158 len + 2,
159 (unsigned long long)
Ian McDonald1bc09862005-08-20 00:23:43 -0300160 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700161
Ian McDonald1bc09862005-08-20 00:23:43 -0300162 if (len > 4) {
163 if (len == 6)
164 opt_recv->dccpor_elapsed_time =
165 ntohs(*(u16 *)(value + 4));
166 else
167 opt_recv->dccpor_elapsed_time =
168 ntohl(*(u32 *)(value + 4));
169
170 dccp_pr_debug("%sTIMESTAMP_ECHO ELAPSED_TIME=%d\n",
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300171 debug_prefix,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700172 opt_recv->dccpor_elapsed_time);
Ian McDonald1bc09862005-08-20 00:23:43 -0300173 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700174 break;
175 case DCCPO_ELAPSED_TIME:
Ian McDonald1bc09862005-08-20 00:23:43 -0300176 if (len != 2 && len != 4)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700177 goto out_invalid_option;
178
179 if (pkt_type == DCCP_PKT_DATA)
180 continue;
Ian McDonald1bc09862005-08-20 00:23:43 -0300181
182 if (len == 2)
183 opt_recv->dccpor_elapsed_time =
184 ntohs(*(u16 *)value);
185 else
186 opt_recv->dccpor_elapsed_time =
187 ntohl(*(u32 *)value);
188
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700189 dccp_pr_debug("%sELAPSED_TIME=%d\n", debug_prefix,
190 opt_recv->dccpor_elapsed_time);
191 break;
192 /*
193 * From draft-ietf-dccp-spec-11.txt:
194 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300195 * Option numbers 128 through 191 are for
196 * options sent from the HC-Sender to the
197 * HC-Receiver; option numbers 192 through 255
198 * are for options sent from the HC-Receiver to
199 * the HC-Sender.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700200 */
201 case 128 ... 191: {
202 const u16 idx = value - options;
203
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300204 if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
205 opt, len, idx,
206 value) != 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700207 goto out_invalid_option;
208 }
209 break;
210 case 192 ... 255: {
211 const u16 idx = value - options;
212
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300213 if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
214 opt, len, idx,
215 value) != 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700216 goto out_invalid_option;
217 }
218 break;
219 default:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300220 pr_info("DCCP(%p): option %d(len=%d) not "
221 "implemented, ignoring\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700222 sk, opt, len);
223 break;
224 }
225 }
226
227 return 0;
228
229out_invalid_option:
230 DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
231 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR;
232 pr_info("DCCP(%p): invalid option %d, len=%d\n", sk, opt, len);
233 return -1;
234}
235
236static void dccp_encode_value_var(const u32 value, unsigned char *to,
237 const unsigned int len)
238{
239 if (len > 3)
240 *to++ = (value & 0xFF000000) >> 24;
241 if (len > 2)
242 *to++ = (value & 0xFF0000) >> 16;
243 if (len > 1)
244 *to++ = (value & 0xFF00) >> 8;
245 if (len > 0)
246 *to++ = (value & 0xFF);
247}
248
249static inline int dccp_ndp_len(const int ndp)
250{
251 return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3;
252}
253
254void dccp_insert_option(struct sock *sk, struct sk_buff *skb,
255 const unsigned char option,
256 const void *value, const unsigned char len)
257{
258 unsigned char *to;
259
260 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300261 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
262 "%d option!\n", option);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700263 return;
264 }
265
266 DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2;
267
268 to = skb_push(skb, len + 2);
269 *to++ = option;
270 *to++ = len + 2;
271
272 memcpy(to, value, len);
273}
274
275EXPORT_SYMBOL_GPL(dccp_insert_option);
276
277static void dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb)
278{
279 struct dccp_sock *dp = dccp_sk(sk);
280 int ndp = dp->dccps_ndp_count;
281
282 if (dccp_non_data_packet(skb))
283 ++dp->dccps_ndp_count;
284 else
285 dp->dccps_ndp_count = 0;
286
287 if (ndp > 0) {
288 unsigned char *ptr;
289 const int ndp_len = dccp_ndp_len(ndp);
290 const int len = ndp_len + 2;
291
292 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
293 return;
294
295 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
296
297 ptr = skb_push(skb, len);
298 *ptr++ = DCCPO_NDP_COUNT;
299 *ptr++ = len;
300 dccp_encode_value_var(ndp, ptr, ndp_len);
301 }
302}
303
304static inline int dccp_elapsed_time_len(const u32 elapsed_time)
305{
Ian McDonaldb1c9fe72005-08-18 20:45:29 -0300306 return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700307}
308
309void dccp_insert_option_elapsed_time(struct sock *sk,
310 struct sk_buff *skb,
311 u32 elapsed_time)
312{
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300313#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700314 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300315 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
316 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700317#endif
318 const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
319 const int len = 2 + elapsed_time_len;
320 unsigned char *to;
321
Ian McDonald1bc09862005-08-20 00:23:43 -0300322 if (elapsed_time_len == 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700323 return;
324
325 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300326 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
327 "insert elapsed time!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700328 return;
329 }
330
331 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
332
333 to = skb_push(skb, len);
334 *to++ = DCCPO_ELAPSED_TIME;
335 *to++ = len;
336
Ian McDonald1bc09862005-08-20 00:23:43 -0300337 if (elapsed_time_len == 2) {
338 const u16 var16 = htons((u16)elapsed_time);
339 memcpy(to, &var16, 2);
340 } else {
341 const u32 var32 = htonl(elapsed_time);
342 memcpy(to, &var32, 4);
343 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700344
345 dccp_pr_debug("%sELAPSED_TIME=%u, len=%d, seqno=%llu\n",
346 debug_prefix, elapsed_time,
David S. Millerf6ccf552005-08-09 20:27:14 -0700347 len,
348 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700349}
350
Arnaldo Carvalho de Melod4b81ff2005-08-23 21:51:36 -0700351EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700352
353static void dccp_insert_option_ack_vector(struct sock *sk, struct sk_buff *skb)
354{
355 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300356#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300357 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
358 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700359#endif
360 struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts;
361 int len = ap->dccpap_buf_vector_len + 2;
Ian McDonald1bc09862005-08-20 00:23:43 -0300362 const u32 elapsed_time = now_delta(ap->dccpap_time) / 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700363 unsigned char *to, *from;
364
365 if (elapsed_time != 0)
366 dccp_insert_option_elapsed_time(sk, skb, elapsed_time);
367
368 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300369 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
370 "insert ACK Vector!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700371 return;
372 }
373
374 /*
375 * XXX: now we have just one ack vector sent record, so
376 * we have to wait for it to be cleared.
377 *
378 * Of course this is not acceptable, but this is just for
379 * basic testing now.
380 */
381 if (ap->dccpap_ack_seqno != DCCP_MAX_SEQNO + 1)
382 return;
383
384 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
385
386 to = skb_push(skb, len);
387 *to++ = DCCPO_ACK_VECTOR_0;
388 *to++ = len;
389
390 len = ap->dccpap_buf_vector_len;
391 from = ap->dccpap_buf + ap->dccpap_buf_head;
392
393 /* Check if buf_head wraps */
394 if (ap->dccpap_buf_head + len > ap->dccpap_buf_len) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300395 const unsigned int tailsize = (ap->dccpap_buf_len -
396 ap->dccpap_buf_head);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700397
398 memcpy(to, from, tailsize);
399 to += tailsize;
400 len -= tailsize;
401 from = ap->dccpap_buf;
402 }
403
404 memcpy(to, from, len);
405 /*
406 * From draft-ietf-dccp-spec-11.txt:
407 *
408 * For each acknowledgement it sends, the HC-Receiver will add an
409 * acknowledgement record. ack_seqno will equal the HC-Receiver
410 * sequence number it used for the ack packet; ack_ptr will equal
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300411 * buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
412 * equal buf_nonce.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700413 *
414 * This implemention uses just one ack record for now.
415 */
416 ap->dccpap_ack_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
417 ap->dccpap_ack_ptr = ap->dccpap_buf_head;
418 ap->dccpap_ack_ackno = ap->dccpap_buf_ackno;
419 ap->dccpap_ack_nonce = ap->dccpap_buf_nonce;
420 ap->dccpap_ack_vector_len = ap->dccpap_buf_vector_len;
421
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300422 dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
423 "ack_ackno=%llu\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700424 debug_prefix, ap->dccpap_ack_vector_len,
David S. Millerf6ccf552005-08-09 20:27:14 -0700425 (unsigned long long) ap->dccpap_ack_seqno,
426 (unsigned long long) ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700427}
428
Arnaldo Carvalho de Melod4b81ff2005-08-23 21:51:36 -0700429void dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700430{
Ian McDonald1bc09862005-08-20 00:23:43 -0300431 struct timeval tv;
432 u32 now;
433
434 do_gettimeofday(&tv);
435 now = (tv.tv_sec * USEC_PER_SEC + tv.tv_usec) / 10;
436 /* yes this will overflow but that is the point as we want a
437 * 10 usec 32 bit timer which mean it wraps every 11.9 hours */
438
439 now = htonl(now);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700440 dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
441}
442
Arnaldo Carvalho de Melod4b81ff2005-08-23 21:51:36 -0700443EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp);
444
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300445static void dccp_insert_option_timestamp_echo(struct sock *sk,
446 struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700447{
448 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300449#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300450 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
451 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700452#endif
453 u32 tstamp_echo;
Ian McDonald1bc09862005-08-20 00:23:43 -0300454 const u32 elapsed_time = now_delta(dp->dccps_timestamp_time) / 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700455 const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
456 const int len = 6 + elapsed_time_len;
457 unsigned char *to;
458
459 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300460 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
461 "timestamp echo!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700462 return;
463 }
464
465 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
466
467 to = skb_push(skb, len);
468 *to++ = DCCPO_TIMESTAMP_ECHO;
469 *to++ = len;
470
471 tstamp_echo = htonl(dp->dccps_timestamp_echo);
472 memcpy(to, &tstamp_echo, 4);
473 to += 4;
Ian McDonald1bc09862005-08-20 00:23:43 -0300474
475 if (elapsed_time_len == 2) {
476 const u16 var16 = htons((u16)elapsed_time);
477 memcpy(to, &var16, 2);
478 } else if (elapsed_time_len == 4) {
479 const u32 var32 = htonl(elapsed_time);
480 memcpy(to, &var32, 4);
481 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700482
483 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, seqno=%llu\n",
484 debug_prefix, dp->dccps_timestamp_echo,
David S. Millerf6ccf552005-08-09 20:27:14 -0700485 len,
486 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700487
488 dp->dccps_timestamp_echo = 0;
Ian McDonald1bc09862005-08-20 00:23:43 -0300489 dp->dccps_timestamp_time.tv_sec = 0;
490 dp->dccps_timestamp_time.tv_usec = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700491}
492
493void dccp_insert_options(struct sock *sk, struct sk_buff *skb)
494{
495 struct dccp_sock *dp = dccp_sk(sk);
496
497 DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
498
499 if (dp->dccps_options.dccpo_send_ndp_count)
500 dccp_insert_option_ndp(sk, skb);
501
502 if (!dccp_packet_without_ack(skb)) {
503 if (dp->dccps_options.dccpo_send_ack_vector &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300504 (dp->dccps_hc_rx_ackpkts->dccpap_buf_ackno !=
505 DCCP_MAX_SEQNO + 1))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700506 dccp_insert_option_ack_vector(sk, skb);
507
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700508 if (dp->dccps_timestamp_echo != 0)
509 dccp_insert_option_timestamp_echo(sk, skb);
510 }
511
512 ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb);
513 ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb);
514
515 /* XXX: insert other options when appropriate */
516
517 if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) {
518 /* The length of all options has to be a multiple of 4 */
519 int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4;
520
521 if (padding != 0) {
522 padding = 4 - padding;
523 memset(skb_push(skb, padding), 0, padding);
524 DCCP_SKB_CB(skb)->dccpd_opt_len += padding;
525 }
526 }
527}
528
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300529struct dccp_ackpkts *dccp_ackpkts_alloc(const unsigned int len,
530 const unsigned int __nocast priority)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700531{
532 struct dccp_ackpkts *ap = kmalloc(sizeof(*ap) + len, priority);
533
534 if (ap != NULL) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300535#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700536 memset(ap->dccpap_buf, 0xFF, len);
537#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300538 ap->dccpap_buf_len = len;
539 ap->dccpap_buf_head =
540 ap->dccpap_buf_tail =
541 ap->dccpap_buf_len - 1;
542 ap->dccpap_buf_ackno =
543 ap->dccpap_ack_ackno =
544 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
545 ap->dccpap_buf_nonce = ap->dccpap_buf_nonce = 0;
546 ap->dccpap_ack_ptr = 0;
Ian McDonald1bc09862005-08-20 00:23:43 -0300547 ap->dccpap_time.tv_sec = 0;
548 ap->dccpap_time.tv_usec = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700549 ap->dccpap_buf_vector_len = ap->dccpap_ack_vector_len = 0;
550 }
551
552 return ap;
553}
554
555void dccp_ackpkts_free(struct dccp_ackpkts *ap)
556{
557 if (ap != NULL) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300558#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700559 memset(ap, 0xFF, sizeof(*ap) + ap->dccpap_buf_len);
560#endif
561 kfree(ap);
562 }
563}
564
565static inline u8 dccp_ackpkts_state(const struct dccp_ackpkts *ap,
566 const unsigned int index)
567{
568 return ap->dccpap_buf[index] & DCCP_ACKPKTS_STATE_MASK;
569}
570
571static inline u8 dccp_ackpkts_len(const struct dccp_ackpkts *ap,
572 const unsigned int index)
573{
574 return ap->dccpap_buf[index] & DCCP_ACKPKTS_LEN_MASK;
575}
576
577/*
578 * If several packets are missing, the HC-Receiver may prefer to enter multiple
579 * bytes with run length 0, rather than a single byte with a larger run length;
580 * this simplifies table updates if one of the missing packets arrives.
581 */
582static inline int dccp_ackpkts_set_buf_head_state(struct dccp_ackpkts *ap,
583 const unsigned int packets,
584 const unsigned char state)
585{
586 unsigned int gap;
587 signed long new_head;
588
589 if (ap->dccpap_buf_vector_len + packets > ap->dccpap_buf_len)
590 return -ENOBUFS;
591
592 gap = packets - 1;
593 new_head = ap->dccpap_buf_head - packets;
594
595 if (new_head < 0) {
596 if (gap > 0) {
597 memset(ap->dccpap_buf, DCCP_ACKPKTS_STATE_NOT_RECEIVED,
598 gap + new_head + 1);
599 gap = -new_head;
600 }
601 new_head += ap->dccpap_buf_len;
602 }
603
604 ap->dccpap_buf_head = new_head;
605
606 if (gap > 0)
607 memset(ap->dccpap_buf + ap->dccpap_buf_head + 1,
608 DCCP_ACKPKTS_STATE_NOT_RECEIVED, gap);
609
610 ap->dccpap_buf[ap->dccpap_buf_head] = state;
611 ap->dccpap_buf_vector_len += packets;
612 return 0;
613}
614
615/*
616 * Implements the draft-ietf-dccp-spec-11.txt Appendix A
617 */
618int dccp_ackpkts_add(struct dccp_ackpkts *ap, u64 ackno, u8 state)
619{
620 /*
621 * Check at the right places if the buffer is full, if it is, tell the
622 * caller to start dropping packets till the HC-Sender acks our ACK
623 * vectors, when we will free up space in dccpap_buf.
624 *
625 * We may well decide to do buffer compression, etc, but for now lets
626 * just drop.
627 *
628 * From Appendix A:
629 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300630 * Of course, the circular buffer may overflow, either when the
631 * HC-Sender is sending data at a very high rate, when the
632 * HC-Receiver's acknowledgements are not reaching the HC-Sender,
633 * or when the HC-Sender is forgetting to acknowledge those acks
634 * (so the HC-Receiver is unable to clean up old state). In this
635 * case, the HC-Receiver should either compress the buffer (by
636 * increasing run lengths when possible), transfer its state to
637 * a larger buffer, or, as a last resort, drop all received
638 * packets, without processing them whatsoever, until its buffer
639 * shrinks again.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700640 */
641
642 /* See if this is the first ackno being inserted */
643 if (ap->dccpap_buf_vector_len == 0) {
644 ap->dccpap_buf[ap->dccpap_buf_head] = state;
645 ap->dccpap_buf_vector_len = 1;
646 } else if (after48(ackno, ap->dccpap_buf_ackno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300647 const u64 delta = dccp_delta_seqno(ap->dccpap_buf_ackno,
648 ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700649
650 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300651 * Look if the state of this packet is the same as the
652 * previous ackno and if so if we can bump the head len.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700653 */
654 if (delta == 1 &&
655 dccp_ackpkts_state(ap, ap->dccpap_buf_head) == state &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300656 (dccp_ackpkts_len(ap, ap->dccpap_buf_head) <
657 DCCP_ACKPKTS_LEN_MASK))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700658 ap->dccpap_buf[ap->dccpap_buf_head]++;
659 else if (dccp_ackpkts_set_buf_head_state(ap, delta, state))
660 return -ENOBUFS;
661 } else {
662 /*
663 * A.1.2. Old Packets
664 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300665 * When a packet with Sequence Number S arrives, and
666 * S <= buf_ackno, the HC-Receiver will scan the table
667 * for the byte corresponding to S. (Indexing structures
668 * could reduce the complexity of this scan.)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700669 */
670 u64 delta = dccp_delta_seqno(ackno, ap->dccpap_buf_ackno);
671 unsigned int index = ap->dccpap_buf_head;
672
673 while (1) {
674 const u8 len = dccp_ackpkts_len(ap, index);
675 const u8 state = dccp_ackpkts_state(ap, index);
676 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300677 * valid packets not yet in dccpap_buf have a reserved
678 * entry, with a len equal to 0.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700679 */
680 if (state == DCCP_ACKPKTS_STATE_NOT_RECEIVED &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300681 len == 0 && delta == 0) { /* Found our
682 reserved seat! */
David S. Millerf6ccf552005-08-09 20:27:14 -0700683 dccp_pr_debug("Found %llu reserved seat!\n",
684 (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700685 ap->dccpap_buf[index] = state;
686 goto out;
687 }
688 /* len == 0 means one packet */
689 if (delta < len + 1)
690 goto out_duplicate;
691
692 delta -= len + 1;
693 if (++index == ap->dccpap_buf_len)
694 index = 0;
695 }
696 }
697
698 ap->dccpap_buf_ackno = ackno;
Ian McDonald1bc09862005-08-20 00:23:43 -0300699 do_gettimeofday(&ap->dccpap_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700700out:
701 dccp_pr_debug("");
702 dccp_ackpkts_print(ap);
703 return 0;
704
705out_duplicate:
706 /* Duplicate packet */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300707 dccp_pr_debug("Received a dup or already considered lost "
708 "packet: %llu\n", (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700709 return -EILSEQ;
710}
711
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300712#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300713void dccp_ackvector_print(const u64 ackno, const unsigned char *vector,
714 int len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700715{
716 if (!dccp_debug)
717 return;
718
David S. Millerf6ccf552005-08-09 20:27:14 -0700719 printk("ACK vector len=%d, ackno=%llu |", len,
720 (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700721
722 while (len--) {
723 const u8 state = (*vector & DCCP_ACKPKTS_STATE_MASK) >> 6;
724 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
725
726 printk("%d,%d|", state, rl);
727 ++vector;
728 }
729
730 printk("\n");
731}
732
733void dccp_ackpkts_print(const struct dccp_ackpkts *ap)
734{
735 dccp_ackvector_print(ap->dccpap_buf_ackno,
736 ap->dccpap_buf + ap->dccpap_buf_head,
737 ap->dccpap_buf_vector_len);
738}
739#endif
740
741static void dccp_ackpkts_trow_away_ack_record(struct dccp_ackpkts *ap)
742{
743 /*
744 * As we're keeping track of the ack vector size
745 * (dccpap_buf_vector_len) and the sent ack vector size
746 * (dccpap_ack_vector_len) we don't need dccpap_buf_tail at all, but
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300747 * keep this code here as in the future we'll implement a vector of
748 * ack records, as suggested in draft-ietf-dccp-spec-11.txt
749 * Appendix A. -acme
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700750 */
751#if 0
752 ap->dccpap_buf_tail = ap->dccpap_ack_ptr + 1;
753 if (ap->dccpap_buf_tail >= ap->dccpap_buf_len)
754 ap->dccpap_buf_tail -= ap->dccpap_buf_len;
755#endif
756 ap->dccpap_buf_vector_len -= ap->dccpap_ack_vector_len;
757}
758
759void dccp_ackpkts_check_rcv_ackno(struct dccp_ackpkts *ap, struct sock *sk,
760 u64 ackno)
761{
762 /* Check if we actually sent an ACK vector */
763 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
764 return;
765
766 if (ackno == ap->dccpap_ack_seqno) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300767#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700768 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300769 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
770 "CLIENT rx ack: " : "server rx ack: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700771#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300772 dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
773 "ack_ackno=%llu, ACKED!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700774 debug_prefix, 1,
David S. Millerf6ccf552005-08-09 20:27:14 -0700775 (unsigned long long) ap->dccpap_ack_seqno,
776 (unsigned long long) ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700777 dccp_ackpkts_trow_away_ack_record(ap);
778 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
779 }
780}
781
782static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap,
783 struct sock *sk, u64 ackno,
784 const unsigned char len,
785 const unsigned char *vector)
786{
787 unsigned char i;
788
789 /* Check if we actually sent an ACK vector */
790 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
791 return;
792 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300793 * We're in the receiver half connection, so if the received an ACK
794 * vector ackno (e.g. 50) before dccpap_ack_seqno (e.g. 52), we're
795 * not interested.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700796 *
797 * Extra explanation with example:
798 *
799 * if we received an ACK vector with ackno 50, it can only be acking
800 * 50, 49, 48, etc, not 52 (the seqno for the ACK vector we sent).
801 */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300802 /* dccp_pr_debug("is %llu < %llu? ", ackno, ap->dccpap_ack_seqno); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700803 if (before48(ackno, ap->dccpap_ack_seqno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300804 /* dccp_pr_debug_cat("yes\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700805 return;
806 }
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300807 /* dccp_pr_debug_cat("no\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700808
809 i = len;
810 while (i--) {
811 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
812 u64 ackno_end_rl;
813
814 dccp_set_seqno(&ackno_end_rl, ackno - rl);
815
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300816 /*
817 * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl,
818 * ap->dccpap_ack_seqno, ackno);
819 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700820 if (between48(ap->dccpap_ack_seqno, ackno_end_rl, ackno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300821 const u8 state = (*vector &
822 DCCP_ACKPKTS_STATE_MASK) >> 6;
823 /* dccp_pr_debug_cat("yes\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700824
825 if (state != DCCP_ACKPKTS_STATE_NOT_RECEIVED) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300826#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700827 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300828 const char *debug_prefix =
829 dp->dccps_role == DCCP_ROLE_CLIENT ?
830 "CLIENT rx ack: " : "server rx ack: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700831#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300832 dccp_pr_debug("%sACK vector 0, len=%d, "
833 "ack_seqno=%llu, ack_ackno=%llu, "
834 "ACKED!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700835 debug_prefix, len,
David S. Millerf6ccf552005-08-09 20:27:14 -0700836 (unsigned long long)
837 ap->dccpap_ack_seqno,
838 (unsigned long long)
839 ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700840 dccp_ackpkts_trow_away_ack_record(ap);
841 }
842 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300843 * If dccpap_ack_seqno was not received, no problem
844 * we'll send another ACK vector.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700845 */
846 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
847 break;
848 }
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300849 /* dccp_pr_debug_cat("no\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700850
851 dccp_set_seqno(&ackno, ackno_end_rl - 1);
852 ++vector;
853 }
854}