aboutsummaryrefslogtreecommitdiffstats
path: root/net/dccp/options.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/dccp/options.c')
-rw-r--r--net/dccp/options.c855
1 files changed, 855 insertions, 0 deletions
diff --git a/net/dccp/options.c b/net/dccp/options.c
new file mode 100644
index 000000000000..382c5894acb2
--- /dev/null
+++ b/net/dccp/options.c
@@ -0,0 +1,855 @@
1/*
2 * net/dccp/options.c
3 *
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>
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
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);
62#ifdef CONFIG_IP_DCCP_DEBUG
63 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
64 "CLIENT rx opt: " : "server rx opt: ";
65#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;
70 const unsigned char *opt_end = (unsigned char *)dh +
71 (dh->dccph_doff * 4);
72 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);
111 dccp_pr_debug("%sNDP count=%d\n", debug_prefix,
112 opt_recv->dccpor_ndp);
113 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",
125 debug_prefix, len,
126 (unsigned long long)
127 DCCP_SKB_CB(skb)->dccpd_ack_seq);
128 dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq,
129 value, len);
130 dccp_ackpkts_check_rcv_ackvector(dp->dccps_hc_rx_ackpkts,
131 sk,
132 DCCP_SKB_CB(skb)->dccpd_ack_seq,
133 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;
142 do_gettimeofday(&dp->dccps_timestamp_time);
143
144 dccp_pr_debug("%sTIMESTAMP=%u, ackno=%llu\n",
145 debug_prefix, opt_recv->dccpor_timestamp,
146 (unsigned long long)
147 DCCP_SKB_CB(skb)->dccpd_ack_seq);
148 break;
149 case DCCPO_TIMESTAMP_ECHO:
150 if (len != 4 && len != 6 && len != 8)
151 goto out_invalid_option;
152
153 opt_recv->dccpor_timestamp_echo = ntohl(*(u32 *)value);
154
155 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, ",
156 debug_prefix,
157 opt_recv->dccpor_timestamp_echo,
158 len + 2,
159 (unsigned long long)
160 DCCP_SKB_CB(skb)->dccpd_ack_seq);
161
162 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",
171 debug_prefix,
172 opt_recv->dccpor_elapsed_time);
173 }
174 break;
175 case DCCPO_ELAPSED_TIME:
176 if (len != 2 && len != 4)
177 goto out_invalid_option;
178
179 if (pkt_type == DCCP_PKT_DATA)
180 continue;
181
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
189 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 *
195 * 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.
200 */
201 case 128 ... 191: {
202 const u16 idx = value - options;
203
204 if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
205 opt, len, idx,
206 value) != 0)
207 goto out_invalid_option;
208 }
209 break;
210 case 192 ... 255: {
211 const u16 idx = value - options;
212
213 if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
214 opt, len, idx,
215 value) != 0)
216 goto out_invalid_option;
217 }
218 break;
219 default:
220 pr_info("DCCP(%p): option %d(len=%d) not "
221 "implemented, ignoring\n",
222 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) {
261 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
262 "%d option!\n", option);
263 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{
306 return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4;
307}
308
309void dccp_insert_option_elapsed_time(struct sock *sk,
310 struct sk_buff *skb,
311 u32 elapsed_time)
312{
313#ifdef CONFIG_IP_DCCP_DEBUG
314 struct dccp_sock *dp = dccp_sk(sk);
315 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
316 "CLIENT TX opt: " : "server TX opt: ";
317#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
322 if (elapsed_time_len == 0)
323 return;
324
325 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
326 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
327 "insert elapsed time!\n");
328 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
337 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 }
344
345 dccp_pr_debug("%sELAPSED_TIME=%u, len=%d, seqno=%llu\n",
346 debug_prefix, elapsed_time,
347 len,
348 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
349}
350
351EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time);
352
353static void dccp_insert_option_ack_vector(struct sock *sk, struct sk_buff *skb)
354{
355 struct dccp_sock *dp = dccp_sk(sk);
356#ifdef CONFIG_IP_DCCP_DEBUG
357 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
358 "CLIENT TX opt: " : "server TX opt: ";
359#endif
360 struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts;
361 int len = ap->dccpap_buf_vector_len + 2;
362 const u32 elapsed_time = timeval_now_delta(&ap->dccpap_time) / 10;
363 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) {
369 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
370 "insert ACK Vector!\n");
371 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) {
395 const unsigned int tailsize = (ap->dccpap_buf_len -
396 ap->dccpap_buf_head);
397
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
411 * buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
412 * equal buf_nonce.
413 *
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
422 dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
423 "ack_ackno=%llu\n",
424 debug_prefix, ap->dccpap_ack_vector_len,
425 (unsigned long long) ap->dccpap_ack_seqno,
426 (unsigned long long) ap->dccpap_ack_ackno);
427}
428
429void dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb)
430{
431 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);
440 dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
441}
442
443EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp);
444
445static void dccp_insert_option_timestamp_echo(struct sock *sk,
446 struct sk_buff *skb)
447{
448 struct dccp_sock *dp = dccp_sk(sk);
449#ifdef CONFIG_IP_DCCP_DEBUG
450 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
451 "CLIENT TX opt: " : "server TX opt: ";
452#endif
453 u32 tstamp_echo;
454 const u32 elapsed_time =
455 timeval_now_delta(&dp->dccps_timestamp_time) / 10;
456 const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
457 const int len = 6 + elapsed_time_len;
458 unsigned char *to;
459
460 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
461 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
462 "timestamp echo!\n");
463 return;
464 }
465
466 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
467
468 to = skb_push(skb, len);
469 *to++ = DCCPO_TIMESTAMP_ECHO;
470 *to++ = len;
471
472 tstamp_echo = htonl(dp->dccps_timestamp_echo);
473 memcpy(to, &tstamp_echo, 4);
474 to += 4;
475
476 if (elapsed_time_len == 2) {
477 const u16 var16 = htons((u16)elapsed_time);
478 memcpy(to, &var16, 2);
479 } else if (elapsed_time_len == 4) {
480 const u32 var32 = htonl(elapsed_time);
481 memcpy(to, &var32, 4);
482 }
483
484 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, seqno=%llu\n",
485 debug_prefix, dp->dccps_timestamp_echo,
486 len,
487 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
488
489 dp->dccps_timestamp_echo = 0;
490 dp->dccps_timestamp_time.tv_sec = 0;
491 dp->dccps_timestamp_time.tv_usec = 0;
492}
493
494void dccp_insert_options(struct sock *sk, struct sk_buff *skb)
495{
496 struct dccp_sock *dp = dccp_sk(sk);
497
498 DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
499
500 if (dp->dccps_options.dccpo_send_ndp_count)
501 dccp_insert_option_ndp(sk, skb);
502
503 if (!dccp_packet_without_ack(skb)) {
504 if (dp->dccps_options.dccpo_send_ack_vector &&
505 (dp->dccps_hc_rx_ackpkts->dccpap_buf_ackno !=
506 DCCP_MAX_SEQNO + 1))
507 dccp_insert_option_ack_vector(sk, skb);
508
509 if (dp->dccps_timestamp_echo != 0)
510 dccp_insert_option_timestamp_echo(sk, skb);
511 }
512
513 ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb);
514 ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb);
515
516 /* XXX: insert other options when appropriate */
517
518 if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) {
519 /* The length of all options has to be a multiple of 4 */
520 int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4;
521
522 if (padding != 0) {
523 padding = 4 - padding;
524 memset(skb_push(skb, padding), 0, padding);
525 DCCP_SKB_CB(skb)->dccpd_opt_len += padding;
526 }
527 }
528}
529
530struct dccp_ackpkts *dccp_ackpkts_alloc(const unsigned int len,
531 const unsigned int __nocast priority)
532{
533 struct dccp_ackpkts *ap = kmalloc(sizeof(*ap) + len, priority);
534
535 if (ap != NULL) {
536#ifdef CONFIG_IP_DCCP_DEBUG
537 memset(ap->dccpap_buf, 0xFF, len);
538#endif
539 ap->dccpap_buf_len = len;
540 ap->dccpap_buf_head =
541 ap->dccpap_buf_tail =
542 ap->dccpap_buf_len - 1;
543 ap->dccpap_buf_ackno =
544 ap->dccpap_ack_ackno =
545 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
546 ap->dccpap_buf_nonce = ap->dccpap_buf_nonce = 0;
547 ap->dccpap_ack_ptr = 0;
548 ap->dccpap_time.tv_sec = 0;
549 ap->dccpap_time.tv_usec = 0;
550 ap->dccpap_buf_vector_len = ap->dccpap_ack_vector_len = 0;
551 }
552
553 return ap;
554}
555
556void dccp_ackpkts_free(struct dccp_ackpkts *ap)
557{
558 if (ap != NULL) {
559#ifdef CONFIG_IP_DCCP_DEBUG
560 memset(ap, 0xFF, sizeof(*ap) + ap->dccpap_buf_len);
561#endif
562 kfree(ap);
563 }
564}
565
566static inline u8 dccp_ackpkts_state(const struct dccp_ackpkts *ap,
567 const unsigned int index)
568{
569 return ap->dccpap_buf[index] & DCCP_ACKPKTS_STATE_MASK;
570}
571
572static inline u8 dccp_ackpkts_len(const struct dccp_ackpkts *ap,
573 const unsigned int index)
574{
575 return ap->dccpap_buf[index] & DCCP_ACKPKTS_LEN_MASK;
576}
577
578/*
579 * If several packets are missing, the HC-Receiver may prefer to enter multiple
580 * bytes with run length 0, rather than a single byte with a larger run length;
581 * this simplifies table updates if one of the missing packets arrives.
582 */
583static inline int dccp_ackpkts_set_buf_head_state(struct dccp_ackpkts *ap,
584 const unsigned int packets,
585 const unsigned char state)
586{
587 unsigned int gap;
588 signed long new_head;
589
590 if (ap->dccpap_buf_vector_len + packets > ap->dccpap_buf_len)
591 return -ENOBUFS;
592
593 gap = packets - 1;
594 new_head = ap->dccpap_buf_head - packets;
595
596 if (new_head < 0) {
597 if (gap > 0) {
598 memset(ap->dccpap_buf, DCCP_ACKPKTS_STATE_NOT_RECEIVED,
599 gap + new_head + 1);
600 gap = -new_head;
601 }
602 new_head += ap->dccpap_buf_len;
603 }
604
605 ap->dccpap_buf_head = new_head;
606
607 if (gap > 0)
608 memset(ap->dccpap_buf + ap->dccpap_buf_head + 1,
609 DCCP_ACKPKTS_STATE_NOT_RECEIVED, gap);
610
611 ap->dccpap_buf[ap->dccpap_buf_head] = state;
612 ap->dccpap_buf_vector_len += packets;
613 return 0;
614}
615
616/*
617 * Implements the draft-ietf-dccp-spec-11.txt Appendix A
618 */
619int dccp_ackpkts_add(struct dccp_ackpkts *ap, u64 ackno, u8 state)
620{
621 /*
622 * Check at the right places if the buffer is full, if it is, tell the
623 * caller to start dropping packets till the HC-Sender acks our ACK
624 * vectors, when we will free up space in dccpap_buf.
625 *
626 * We may well decide to do buffer compression, etc, but for now lets
627 * just drop.
628 *
629 * From Appendix A:
630 *
631 * Of course, the circular buffer may overflow, either when the
632 * HC-Sender is sending data at a very high rate, when the
633 * HC-Receiver's acknowledgements are not reaching the HC-Sender,
634 * or when the HC-Sender is forgetting to acknowledge those acks
635 * (so the HC-Receiver is unable to clean up old state). In this
636 * case, the HC-Receiver should either compress the buffer (by
637 * increasing run lengths when possible), transfer its state to
638 * a larger buffer, or, as a last resort, drop all received
639 * packets, without processing them whatsoever, until its buffer
640 * shrinks again.
641 */
642
643 /* See if this is the first ackno being inserted */
644 if (ap->dccpap_buf_vector_len == 0) {
645 ap->dccpap_buf[ap->dccpap_buf_head] = state;
646 ap->dccpap_buf_vector_len = 1;
647 } else if (after48(ackno, ap->dccpap_buf_ackno)) {
648 const u64 delta = dccp_delta_seqno(ap->dccpap_buf_ackno,
649 ackno);
650
651 /*
652 * Look if the state of this packet is the same as the
653 * previous ackno and if so if we can bump the head len.
654 */
655 if (delta == 1 &&
656 dccp_ackpkts_state(ap, ap->dccpap_buf_head) == state &&
657 (dccp_ackpkts_len(ap, ap->dccpap_buf_head) <
658 DCCP_ACKPKTS_LEN_MASK))
659 ap->dccpap_buf[ap->dccpap_buf_head]++;
660 else if (dccp_ackpkts_set_buf_head_state(ap, delta, state))
661 return -ENOBUFS;
662 } else {
663 /*
664 * A.1.2. Old Packets
665 *
666 * When a packet with Sequence Number S arrives, and
667 * S <= buf_ackno, the HC-Receiver will scan the table
668 * for the byte corresponding to S. (Indexing structures
669 * could reduce the complexity of this scan.)
670 */
671 u64 delta = dccp_delta_seqno(ackno, ap->dccpap_buf_ackno);
672 unsigned int index = ap->dccpap_buf_head;
673
674 while (1) {
675 const u8 len = dccp_ackpkts_len(ap, index);
676 const u8 state = dccp_ackpkts_state(ap, index);
677 /*
678 * valid packets not yet in dccpap_buf have a reserved
679 * entry, with a len equal to 0.
680 */
681 if (state == DCCP_ACKPKTS_STATE_NOT_RECEIVED &&
682 len == 0 && delta == 0) { /* Found our
683 reserved seat! */
684 dccp_pr_debug("Found %llu reserved seat!\n",
685 (unsigned long long) ackno);
686 ap->dccpap_buf[index] = state;
687 goto out;
688 }
689 /* len == 0 means one packet */
690 if (delta < len + 1)
691 goto out_duplicate;
692
693 delta -= len + 1;
694 if (++index == ap->dccpap_buf_len)
695 index = 0;
696 }
697 }
698
699 ap->dccpap_buf_ackno = ackno;
700 do_gettimeofday(&ap->dccpap_time);
701out:
702 dccp_pr_debug("");
703 dccp_ackpkts_print(ap);
704 return 0;
705
706out_duplicate:
707 /* Duplicate packet */
708 dccp_pr_debug("Received a dup or already considered lost "
709 "packet: %llu\n", (unsigned long long) ackno);
710 return -EILSEQ;
711}
712
713#ifdef CONFIG_IP_DCCP_DEBUG
714void dccp_ackvector_print(const u64 ackno, const unsigned char *vector,
715 int len)
716{
717 if (!dccp_debug)
718 return;
719
720 printk("ACK vector len=%d, ackno=%llu |", len,
721 (unsigned long long) ackno);
722
723 while (len--) {
724 const u8 state = (*vector & DCCP_ACKPKTS_STATE_MASK) >> 6;
725 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
726
727 printk("%d,%d|", state, rl);
728 ++vector;
729 }
730
731 printk("\n");
732}
733
734void dccp_ackpkts_print(const struct dccp_ackpkts *ap)
735{
736 dccp_ackvector_print(ap->dccpap_buf_ackno,
737 ap->dccpap_buf + ap->dccpap_buf_head,
738 ap->dccpap_buf_vector_len);
739}
740#endif
741
742static void dccp_ackpkts_trow_away_ack_record(struct dccp_ackpkts *ap)
743{
744 /*
745 * As we're keeping track of the ack vector size
746 * (dccpap_buf_vector_len) and the sent ack vector size
747 * (dccpap_ack_vector_len) we don't need dccpap_buf_tail at all, but
748 * keep this code here as in the future we'll implement a vector of
749 * ack records, as suggested in draft-ietf-dccp-spec-11.txt
750 * Appendix A. -acme
751 */
752#if 0
753 ap->dccpap_buf_tail = ap->dccpap_ack_ptr + 1;
754 if (ap->dccpap_buf_tail >= ap->dccpap_buf_len)
755 ap->dccpap_buf_tail -= ap->dccpap_buf_len;
756#endif
757 ap->dccpap_buf_vector_len -= ap->dccpap_ack_vector_len;
758}
759
760void dccp_ackpkts_check_rcv_ackno(struct dccp_ackpkts *ap, struct sock *sk,
761 u64 ackno)
762{
763 /* Check if we actually sent an ACK vector */
764 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
765 return;
766
767 if (ackno == ap->dccpap_ack_seqno) {
768#ifdef CONFIG_IP_DCCP_DEBUG
769 struct dccp_sock *dp = dccp_sk(sk);
770 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
771 "CLIENT rx ack: " : "server rx ack: ";
772#endif
773 dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
774 "ack_ackno=%llu, ACKED!\n",
775 debug_prefix, 1,
776 (unsigned long long) ap->dccpap_ack_seqno,
777 (unsigned long long) ap->dccpap_ack_ackno);
778 dccp_ackpkts_trow_away_ack_record(ap);
779 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
780 }
781}
782
783static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap,
784 struct sock *sk, u64 ackno,
785 const unsigned char len,
786 const unsigned char *vector)
787{
788 unsigned char i;
789
790 /* Check if we actually sent an ACK vector */
791 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
792 return;
793 /*
794 * We're in the receiver half connection, so if the received an ACK
795 * vector ackno (e.g. 50) before dccpap_ack_seqno (e.g. 52), we're
796 * not interested.
797 *
798 * Extra explanation with example:
799 *
800 * if we received an ACK vector with ackno 50, it can only be acking
801 * 50, 49, 48, etc, not 52 (the seqno for the ACK vector we sent).
802 */
803 /* dccp_pr_debug("is %llu < %llu? ", ackno, ap->dccpap_ack_seqno); */
804 if (before48(ackno, ap->dccpap_ack_seqno)) {
805 /* dccp_pr_debug_cat("yes\n"); */
806 return;
807 }
808 /* dccp_pr_debug_cat("no\n"); */
809
810 i = len;
811 while (i--) {
812 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
813 u64 ackno_end_rl;
814
815 dccp_set_seqno(&ackno_end_rl, ackno - rl);
816
817 /*
818 * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl,
819 * ap->dccpap_ack_seqno, ackno);
820 */
821 if (between48(ap->dccpap_ack_seqno, ackno_end_rl, ackno)) {
822 const u8 state = (*vector &
823 DCCP_ACKPKTS_STATE_MASK) >> 6;
824 /* dccp_pr_debug_cat("yes\n"); */
825
826 if (state != DCCP_ACKPKTS_STATE_NOT_RECEIVED) {
827#ifdef CONFIG_IP_DCCP_DEBUG
828 struct dccp_sock *dp = dccp_sk(sk);
829 const char *debug_prefix =
830 dp->dccps_role == DCCP_ROLE_CLIENT ?
831 "CLIENT rx ack: " : "server rx ack: ";
832#endif
833 dccp_pr_debug("%sACK vector 0, len=%d, "
834 "ack_seqno=%llu, ack_ackno=%llu, "
835 "ACKED!\n",
836 debug_prefix, len,
837 (unsigned long long)
838 ap->dccpap_ack_seqno,
839 (unsigned long long)
840 ap->dccpap_ack_ackno);
841 dccp_ackpkts_trow_away_ack_record(ap);
842 }
843 /*
844 * If dccpap_ack_seqno was not received, no problem
845 * we'll send another ACK vector.
846 */
847 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
848 break;
849 }
850 /* dccp_pr_debug_cat("no\n"); */
851
852 dccp_set_seqno(&ackno, ackno_end_rl - 1);
853 ++vector;
854 }
855}