Merge master.kernel.org:/pub/scm/linux/kernel/git/acme/net-2.6
[sfrench/cifs-2.6.git] / net / dccp / options.c
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
24 static 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 */
31 static 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
38 void dccp_options_init(struct dccp_options *dccpo)
39 {
40         memcpy(dccpo, &dccpo_default_values, sizeof(*dccpo));
41 }
42
43 static 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
59 int 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         u32 elapsed_time;
76
77         memset(opt_recv, 0, sizeof(*opt_recv));
78
79         while (opt_ptr != opt_end) {
80                 opt   = *opt_ptr++;
81                 len   = 0;
82                 value = NULL;
83
84                 /* Check if this isn't a single byte option */
85                 if (opt > DCCPO_MAX_RESERVED) {
86                         if (opt_ptr == opt_end)
87                                 goto out_invalid_option;
88
89                         len = *opt_ptr++;
90                         if (len < 3)
91                                 goto out_invalid_option;
92                         /*
93                          * Remove the type and len fields, leaving
94                          * just the value size
95                          */
96                         len     -= 2;
97                         value   = opt_ptr;
98                         opt_ptr += len;
99
100                         if (opt_ptr > opt_end)
101                                 goto out_invalid_option;
102                 }
103
104                 switch (opt) {
105                 case DCCPO_PADDING:
106                         break;
107                 case DCCPO_NDP_COUNT:
108                         if (len > 3)
109                                 goto out_invalid_option;
110
111                         opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
112                         dccp_pr_debug("%sNDP count=%d\n", debug_prefix,
113                                       opt_recv->dccpor_ndp);
114                         break;
115                 case DCCPO_ACK_VECTOR_0:
116                         if (len > DCCP_MAX_ACK_VECTOR_LEN)
117                                 goto out_invalid_option;
118
119                         if (pkt_type == DCCP_PKT_DATA)
120                                 continue;
121
122                         opt_recv->dccpor_ack_vector_len = len;
123                         opt_recv->dccpor_ack_vector_idx = value - options;
124
125                         dccp_pr_debug("%sACK vector 0, len=%d, ack_ackno=%llu\n",
126                                       debug_prefix, len,
127                                       (unsigned long long)
128                                       DCCP_SKB_CB(skb)->dccpd_ack_seq);
129                         dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq,
130                                              value, len);
131                         dccp_ackpkts_check_rcv_ackvector(dp->dccps_hc_rx_ackpkts,
132                                                          sk,
133                                                  DCCP_SKB_CB(skb)->dccpd_ack_seq,
134                                                          len, value);
135                         break;
136                 case DCCPO_TIMESTAMP:
137                         if (len != 4)
138                                 goto out_invalid_option;
139
140                         opt_recv->dccpor_timestamp = ntohl(*(u32 *)value);
141
142                         dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp;
143                         dccp_timestamp(sk, &dp->dccps_timestamp_time);
144
145                         dccp_pr_debug("%sTIMESTAMP=%u, ackno=%llu\n",
146                                       debug_prefix, opt_recv->dccpor_timestamp,
147                                       (unsigned long long)
148                                       DCCP_SKB_CB(skb)->dccpd_ack_seq);
149                         break;
150                 case DCCPO_TIMESTAMP_ECHO:
151                         if (len != 4 && len != 6 && len != 8)
152                                 goto out_invalid_option;
153
154                         opt_recv->dccpor_timestamp_echo = ntohl(*(u32 *)value);
155
156                         dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, ",
157                                       debug_prefix,
158                                       opt_recv->dccpor_timestamp_echo,
159                                       len + 2,
160                                       (unsigned long long)
161                                       DCCP_SKB_CB(skb)->dccpd_ack_seq);
162
163
164                         if (len == 4)
165                                 break;
166
167                         if (len == 6)
168                                 elapsed_time = ntohs(*(u16 *)(value + 4));
169                         else
170                                 elapsed_time = ntohl(*(u32 *)(value + 4));
171
172                         /* Give precedence to the biggest ELAPSED_TIME */
173                         if (elapsed_time > opt_recv->dccpor_elapsed_time)
174                                 opt_recv->dccpor_elapsed_time = elapsed_time;
175                         break;
176                 case DCCPO_ELAPSED_TIME:
177                         if (len != 2 && len != 4)
178                                 goto out_invalid_option;
179
180                         if (pkt_type == DCCP_PKT_DATA)
181                                 continue;
182
183                         if (len == 2)
184                                 elapsed_time = ntohs(*(u16 *)value);
185                         else
186                                 elapsed_time = ntohl(*(u32 *)value);
187
188                         if (elapsed_time > opt_recv->dccpor_elapsed_time)
189                                 opt_recv->dccpor_elapsed_time = elapsed_time;
190
191                         dccp_pr_debug("%sELAPSED_TIME=%d\n", debug_prefix,
192                                       elapsed_time);
193                         break;
194                         /*
195                          * From draft-ietf-dccp-spec-11.txt:
196                          *
197                          *      Option numbers 128 through 191 are for
198                          *      options sent from the HC-Sender to the
199                          *      HC-Receiver; option numbers 192 through 255
200                          *      are for options sent from the HC-Receiver to
201                          *      the HC-Sender.
202                          */
203                 case 128 ... 191: {
204                         const u16 idx = value - options;
205
206                         if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
207                                                      opt, len, idx,
208                                                      value) != 0)
209                                 goto out_invalid_option;
210                 }
211                         break;
212                 case 192 ... 255: {
213                         const u16 idx = value - options;
214
215                         if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
216                                                      opt, len, idx,
217                                                      value) != 0)
218                                 goto out_invalid_option;
219                 }
220                         break;
221                 default:
222                         pr_info("DCCP(%p): option %d(len=%d) not "
223                                 "implemented, ignoring\n",
224                                 sk, opt, len);
225                         break;
226                 }
227         }
228
229         return 0;
230
231 out_invalid_option:
232         DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
233         DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR;
234         pr_info("DCCP(%p): invalid option %d, len=%d\n", sk, opt, len);
235         return -1;
236 }
237
238 static void dccp_encode_value_var(const u32 value, unsigned char *to,
239                                   const unsigned int len)
240 {
241         if (len > 3)
242                 *to++ = (value & 0xFF000000) >> 24;
243         if (len > 2)
244                 *to++ = (value & 0xFF0000) >> 16;
245         if (len > 1)
246                 *to++ = (value & 0xFF00) >> 8;
247         if (len > 0)
248                 *to++ = (value & 0xFF);
249 }
250
251 static inline int dccp_ndp_len(const int ndp)
252 {
253         return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3;
254 }
255
256 void dccp_insert_option(struct sock *sk, struct sk_buff *skb,
257                         const unsigned char option,
258                         const void *value, const unsigned char len)
259 {
260         unsigned char *to;
261
262         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN) {
263                 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
264                                "%d option!\n", option);
265                 return;
266         }
267
268         DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2;
269
270         to    = skb_push(skb, len + 2);
271         *to++ = option;
272         *to++ = len + 2;
273
274         memcpy(to, value, len);
275 }
276
277 EXPORT_SYMBOL_GPL(dccp_insert_option);
278
279 static void dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb)
280 {
281         struct dccp_sock *dp = dccp_sk(sk);
282         int ndp = dp->dccps_ndp_count;
283
284         if (dccp_non_data_packet(skb))
285                 ++dp->dccps_ndp_count;
286         else
287                 dp->dccps_ndp_count = 0;
288
289         if (ndp > 0) {
290                 unsigned char *ptr;
291                 const int ndp_len = dccp_ndp_len(ndp);
292                 const int len = ndp_len + 2;
293
294                 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
295                         return;
296
297                 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
298
299                 ptr = skb_push(skb, len);
300                 *ptr++ = DCCPO_NDP_COUNT;
301                 *ptr++ = len;
302                 dccp_encode_value_var(ndp, ptr, ndp_len);
303         }
304 }
305
306 static inline int dccp_elapsed_time_len(const u32 elapsed_time)
307 {
308         return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4;
309 }
310
311 void dccp_insert_option_elapsed_time(struct sock *sk,
312                                      struct sk_buff *skb,
313                                      u32 elapsed_time)
314 {
315 #ifdef CONFIG_IP_DCCP_DEBUG
316         struct dccp_sock *dp = dccp_sk(sk);
317         const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
318                                         "CLIENT TX opt: " : "server TX opt: ";
319 #endif
320         const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
321         const int len = 2 + elapsed_time_len;
322         unsigned char *to;
323
324         if (elapsed_time_len == 0)
325                 return;
326
327         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
328                 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
329                                          "insert elapsed time!\n");
330                 return;
331         }
332
333         DCCP_SKB_CB(skb)->dccpd_opt_len += len;
334
335         to    = skb_push(skb, len);
336         *to++ = DCCPO_ELAPSED_TIME;
337         *to++ = len;
338
339         if (elapsed_time_len == 2) {
340                 const u16 var16 = htons((u16)elapsed_time);
341                 memcpy(to, &var16, 2);
342         } else {
343                 const u32 var32 = htonl(elapsed_time);
344                 memcpy(to, &var32, 4);
345         }
346
347         dccp_pr_debug("%sELAPSED_TIME=%u, len=%d, seqno=%llu\n",
348                       debug_prefix, elapsed_time,
349                       len,
350                       (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
351 }
352
353 EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time);
354
355 static void dccp_insert_option_ack_vector(struct sock *sk, struct sk_buff *skb)
356 {
357         struct dccp_sock *dp = dccp_sk(sk);
358 #ifdef CONFIG_IP_DCCP_DEBUG
359         const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
360                                         "CLIENT TX opt: " : "server TX opt: ";
361 #endif
362         struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts;
363         int len = ap->dccpap_buf_vector_len + 2;
364         struct timeval now;
365         u32 elapsed_time;
366         unsigned char *to, *from;
367
368         dccp_timestamp(sk, &now);
369         elapsed_time = timeval_delta(&now, &ap->dccpap_time) / 10;
370
371         if (elapsed_time != 0)
372                 dccp_insert_option_elapsed_time(sk, skb, elapsed_time);
373
374         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
375                 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
376                                          "insert ACK Vector!\n");
377                 return;
378         }
379
380         /*
381          * XXX: now we have just one ack vector sent record, so
382          * we have to wait for it to be cleared.
383          *
384          * Of course this is not acceptable, but this is just for
385          * basic testing now.
386          */
387         if (ap->dccpap_ack_seqno != DCCP_MAX_SEQNO + 1)
388                 return;
389
390         DCCP_SKB_CB(skb)->dccpd_opt_len += len;
391
392         to    = skb_push(skb, len);
393         *to++ = DCCPO_ACK_VECTOR_0;
394         *to++ = len;
395
396         len  = ap->dccpap_buf_vector_len;
397         from = ap->dccpap_buf + ap->dccpap_buf_head;
398
399         /* Check if buf_head wraps */
400         if (ap->dccpap_buf_head + len > ap->dccpap_buf_len) {
401                 const unsigned int tailsize = (ap->dccpap_buf_len -
402                                                ap->dccpap_buf_head);
403
404                 memcpy(to, from, tailsize);
405                 to   += tailsize;
406                 len  -= tailsize;
407                 from = ap->dccpap_buf;
408         }
409
410         memcpy(to, from, len);
411         /*
412          *      From draft-ietf-dccp-spec-11.txt:
413          *
414          *      For each acknowledgement it sends, the HC-Receiver will add an
415          *      acknowledgement record.  ack_seqno will equal the HC-Receiver
416          *      sequence number it used for the ack packet; ack_ptr will equal
417          *      buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
418          *      equal buf_nonce.
419          *
420          * This implemention uses just one ack record for now.
421          */
422         ap->dccpap_ack_seqno      = DCCP_SKB_CB(skb)->dccpd_seq;
423         ap->dccpap_ack_ptr        = ap->dccpap_buf_head;
424         ap->dccpap_ack_ackno      = ap->dccpap_buf_ackno;
425         ap->dccpap_ack_nonce      = ap->dccpap_buf_nonce;
426         ap->dccpap_ack_vector_len = ap->dccpap_buf_vector_len;
427
428         dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
429                       "ack_ackno=%llu\n",
430                       debug_prefix, ap->dccpap_ack_vector_len,
431                       (unsigned long long) ap->dccpap_ack_seqno,
432                       (unsigned long long) ap->dccpap_ack_ackno);
433 }
434
435 void dccp_timestamp(const struct sock *sk, struct timeval *tv)
436 {
437         const struct dccp_sock *dp = dccp_sk(sk);
438
439         do_gettimeofday(tv);
440         tv->tv_sec  -= dp->dccps_epoch.tv_sec;
441         tv->tv_usec -= dp->dccps_epoch.tv_usec;
442
443         while (tv->tv_usec < 0) {
444                 tv->tv_sec--;
445                 tv->tv_usec += USEC_PER_SEC;
446         }
447 }
448
449 EXPORT_SYMBOL_GPL(dccp_timestamp);
450
451 void dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb)
452 {
453         struct timeval tv;
454         u32 now;
455         
456         dccp_timestamp(sk, &tv);
457         now = timeval_usecs(&tv) / 10;
458         /* yes this will overflow but that is the point as we want a
459          * 10 usec 32 bit timer which mean it wraps every 11.9 hours */
460
461         now = htonl(now);
462         dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
463 }
464
465 EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp);
466
467 static void dccp_insert_option_timestamp_echo(struct sock *sk,
468                                               struct sk_buff *skb)
469 {
470         struct dccp_sock *dp = dccp_sk(sk);
471 #ifdef CONFIG_IP_DCCP_DEBUG
472         const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
473                                         "CLIENT TX opt: " : "server TX opt: ";
474 #endif
475         struct timeval now;
476         u32 tstamp_echo;
477         u32 elapsed_time;
478         int len, elapsed_time_len;
479         unsigned char *to;
480
481         dccp_timestamp(sk, &now);
482         elapsed_time = timeval_delta(&now, &dp->dccps_timestamp_time) / 10;
483         elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
484         len = 6 + elapsed_time_len;
485
486         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
487                 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
488                                          "timestamp echo!\n");
489                 return;
490         }
491
492         DCCP_SKB_CB(skb)->dccpd_opt_len += len;
493
494         to    = skb_push(skb, len);
495         *to++ = DCCPO_TIMESTAMP_ECHO;
496         *to++ = len;
497
498         tstamp_echo = htonl(dp->dccps_timestamp_echo);
499         memcpy(to, &tstamp_echo, 4);
500         to += 4;
501         
502         if (elapsed_time_len == 2) {
503                 const u16 var16 = htons((u16)elapsed_time);
504                 memcpy(to, &var16, 2);
505         } else if (elapsed_time_len == 4) {
506                 const u32 var32 = htonl(elapsed_time);
507                 memcpy(to, &var32, 4);
508         }
509
510         dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, seqno=%llu\n",
511                       debug_prefix, dp->dccps_timestamp_echo,
512                       len,
513                       (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
514
515         dp->dccps_timestamp_echo = 0;
516         dp->dccps_timestamp_time.tv_sec = 0;
517         dp->dccps_timestamp_time.tv_usec = 0;
518 }
519
520 void dccp_insert_options(struct sock *sk, struct sk_buff *skb)
521 {
522         struct dccp_sock *dp = dccp_sk(sk);
523
524         DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
525
526         if (dp->dccps_options.dccpo_send_ndp_count)
527                 dccp_insert_option_ndp(sk, skb);
528
529         if (!dccp_packet_without_ack(skb)) {
530                 if (dp->dccps_options.dccpo_send_ack_vector &&
531                     (dp->dccps_hc_rx_ackpkts->dccpap_buf_ackno !=
532                      DCCP_MAX_SEQNO + 1))
533                         dccp_insert_option_ack_vector(sk, skb);
534                 if (dp->dccps_timestamp_echo != 0)
535                         dccp_insert_option_timestamp_echo(sk, skb);
536         }
537
538         if (dp->dccps_hc_rx_insert_options) {
539                 ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb);
540                 dp->dccps_hc_rx_insert_options = 0;
541         }
542         if (dp->dccps_hc_tx_insert_options) {
543                 ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb);
544                 dp->dccps_hc_tx_insert_options = 0;
545         }
546
547         /* XXX: insert other options when appropriate */
548
549         if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) {
550                 /* The length of all options has to be a multiple of 4 */
551                 int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4;
552
553                 if (padding != 0) {
554                         padding = 4 - padding;
555                         memset(skb_push(skb, padding), 0, padding);
556                         DCCP_SKB_CB(skb)->dccpd_opt_len += padding;
557                 }
558         }
559 }
560
561 struct dccp_ackpkts *dccp_ackpkts_alloc(const unsigned int len,
562                                         const unsigned int __nocast priority)
563 {
564         struct dccp_ackpkts *ap = kmalloc(sizeof(*ap) + len, priority);
565
566         if (ap != NULL) {
567 #ifdef CONFIG_IP_DCCP_DEBUG
568                 memset(ap->dccpap_buf, 0xFF, len);
569 #endif
570                 ap->dccpap_buf_len   = len;
571                 ap->dccpap_buf_head  =
572                         ap->dccpap_buf_tail =
573                                 ap->dccpap_buf_len - 1;
574                 ap->dccpap_buf_ackno =
575                         ap->dccpap_ack_ackno =
576                                 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
577                 ap->dccpap_buf_nonce = ap->dccpap_buf_nonce = 0;
578                 ap->dccpap_ack_ptr   = 0;
579                 ap->dccpap_time.tv_sec = 0;
580                 ap->dccpap_time.tv_usec = 0;
581                 ap->dccpap_buf_vector_len = ap->dccpap_ack_vector_len = 0;
582         }
583
584         return ap;
585 }
586
587 void dccp_ackpkts_free(struct dccp_ackpkts *ap)
588 {
589         if (ap != NULL) {
590 #ifdef CONFIG_IP_DCCP_DEBUG
591                 memset(ap, 0xFF, sizeof(*ap) + ap->dccpap_buf_len);
592 #endif
593                 kfree(ap);
594         }
595 }
596
597 static inline u8 dccp_ackpkts_state(const struct dccp_ackpkts *ap,
598                                     const unsigned int index)
599 {
600         return ap->dccpap_buf[index] & DCCP_ACKPKTS_STATE_MASK;
601 }
602
603 static inline u8 dccp_ackpkts_len(const struct dccp_ackpkts *ap,
604                                   const unsigned int index)
605 {
606         return ap->dccpap_buf[index] & DCCP_ACKPKTS_LEN_MASK;
607 }
608
609 /*
610  * If several packets are missing, the HC-Receiver may prefer to enter multiple
611  * bytes with run length 0, rather than a single byte with a larger run length;
612  * this simplifies table updates if one of the missing packets arrives.
613  */
614 static inline int dccp_ackpkts_set_buf_head_state(struct dccp_ackpkts *ap,
615                                                   const unsigned int packets,
616                                                   const unsigned char state)
617 {
618         unsigned int gap;
619         signed long new_head;
620
621         if (ap->dccpap_buf_vector_len + packets > ap->dccpap_buf_len)
622                 return -ENOBUFS;
623
624         gap      = packets - 1;
625         new_head = ap->dccpap_buf_head - packets;
626
627         if (new_head < 0) {
628                 if (gap > 0) {
629                         memset(ap->dccpap_buf, DCCP_ACKPKTS_STATE_NOT_RECEIVED,
630                                gap + new_head + 1);
631                         gap = -new_head;
632                 }
633                 new_head += ap->dccpap_buf_len;
634         } 
635
636         ap->dccpap_buf_head = new_head;
637
638         if (gap > 0)
639                 memset(ap->dccpap_buf + ap->dccpap_buf_head + 1,
640                        DCCP_ACKPKTS_STATE_NOT_RECEIVED, gap);
641
642         ap->dccpap_buf[ap->dccpap_buf_head] = state;
643         ap->dccpap_buf_vector_len += packets;
644         return 0;
645 }
646
647 /*
648  * Implements the draft-ietf-dccp-spec-11.txt Appendix A
649  */
650 int dccp_ackpkts_add(struct dccp_ackpkts *ap, const struct sock *sk,
651                      u64 ackno, u8 state)
652 {
653         /*
654          * Check at the right places if the buffer is full, if it is, tell the
655          * caller to start dropping packets till the HC-Sender acks our ACK
656          * vectors, when we will free up space in dccpap_buf.
657          *
658          * We may well decide to do buffer compression, etc, but for now lets
659          * just drop.
660          *
661          * From Appendix A:
662          *
663          *      Of course, the circular buffer may overflow, either when the
664          *      HC-Sender is sending data at a very high rate, when the
665          *      HC-Receiver's acknowledgements are not reaching the HC-Sender,
666          *      or when the HC-Sender is forgetting to acknowledge those acks
667          *      (so the HC-Receiver is unable to clean up old state). In this
668          *      case, the HC-Receiver should either compress the buffer (by
669          *      increasing run lengths when possible), transfer its state to
670          *      a larger buffer, or, as a last resort, drop all received
671          *      packets, without processing them whatsoever, until its buffer
672          *      shrinks again.
673          */
674
675         /* See if this is the first ackno being inserted */
676         if (ap->dccpap_buf_vector_len == 0) {
677                 ap->dccpap_buf[ap->dccpap_buf_head] = state;
678                 ap->dccpap_buf_vector_len = 1;
679         } else if (after48(ackno, ap->dccpap_buf_ackno)) {
680                 const u64 delta = dccp_delta_seqno(ap->dccpap_buf_ackno,
681                                                    ackno);
682
683                 /*
684                  * Look if the state of this packet is the same as the
685                  * previous ackno and if so if we can bump the head len.
686                  */
687                 if (delta == 1 &&
688                     dccp_ackpkts_state(ap, ap->dccpap_buf_head) == state &&
689                     (dccp_ackpkts_len(ap, ap->dccpap_buf_head) <
690                      DCCP_ACKPKTS_LEN_MASK))
691                         ap->dccpap_buf[ap->dccpap_buf_head]++;
692                 else if (dccp_ackpkts_set_buf_head_state(ap, delta, state))
693                         return -ENOBUFS;
694         } else {
695                 /*
696                  * A.1.2.  Old Packets
697                  *
698                  *      When a packet with Sequence Number S arrives, and
699                  *      S <= buf_ackno, the HC-Receiver will scan the table
700                  *      for the byte corresponding to S. (Indexing structures
701                  *      could reduce the complexity of this scan.)
702                  */
703                 u64 delta = dccp_delta_seqno(ackno, ap->dccpap_buf_ackno);
704                 unsigned int index = ap->dccpap_buf_head;
705
706                 while (1) {
707                         const u8 len = dccp_ackpkts_len(ap, index);
708                         const u8 state = dccp_ackpkts_state(ap, index);
709                         /*
710                          * valid packets not yet in dccpap_buf have a reserved
711                          * entry, with a len equal to 0.
712                          */
713                         if (state == DCCP_ACKPKTS_STATE_NOT_RECEIVED &&
714                             len == 0 && delta == 0) { /* Found our
715                                                          reserved seat! */
716                                 dccp_pr_debug("Found %llu reserved seat!\n",
717                                               (unsigned long long) ackno);
718                                 ap->dccpap_buf[index] = state;
719                                 goto out;
720                         }
721                         /* len == 0 means one packet */
722                         if (delta < len + 1)
723                                 goto out_duplicate;
724
725                         delta -= len + 1;
726                         if (++index == ap->dccpap_buf_len)
727                                 index = 0;
728                 }
729         }
730
731         ap->dccpap_buf_ackno = ackno;
732         dccp_timestamp(sk, &ap->dccpap_time);
733 out:
734         dccp_pr_debug("");
735         dccp_ackpkts_print(ap);
736         return 0;
737
738 out_duplicate:
739         /* Duplicate packet */
740         dccp_pr_debug("Received a dup or already considered lost "
741                       "packet: %llu\n", (unsigned long long) ackno);
742         return -EILSEQ;
743 }
744
745 #ifdef CONFIG_IP_DCCP_DEBUG
746 void dccp_ackvector_print(const u64 ackno, const unsigned char *vector,
747                           int len)
748 {
749         if (!dccp_debug)
750                 return;
751
752         printk("ACK vector len=%d, ackno=%llu |", len,
753                (unsigned long long) ackno);
754
755         while (len--) {
756                 const u8 state = (*vector & DCCP_ACKPKTS_STATE_MASK) >> 6;
757                 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
758
759                 printk("%d,%d|", state, rl);
760                 ++vector;
761         }
762
763         printk("\n");
764 }
765
766 void dccp_ackpkts_print(const struct dccp_ackpkts *ap)
767 {
768         dccp_ackvector_print(ap->dccpap_buf_ackno,
769                              ap->dccpap_buf + ap->dccpap_buf_head,
770                              ap->dccpap_buf_vector_len);
771 }
772 #endif
773
774 static void dccp_ackpkts_trow_away_ack_record(struct dccp_ackpkts *ap)
775 {
776         /*
777          * As we're keeping track of the ack vector size
778          * (dccpap_buf_vector_len) and the sent ack vector size
779          * (dccpap_ack_vector_len) we don't need dccpap_buf_tail at all, but
780          * keep this code here as in the future we'll implement a vector of
781          * ack records, as suggested in draft-ietf-dccp-spec-11.txt
782          * Appendix A. -acme
783          */
784 #if 0
785         ap->dccpap_buf_tail = ap->dccpap_ack_ptr + 1;
786         if (ap->dccpap_buf_tail >= ap->dccpap_buf_len)
787                 ap->dccpap_buf_tail -= ap->dccpap_buf_len;
788 #endif
789         ap->dccpap_buf_vector_len -= ap->dccpap_ack_vector_len;
790 }
791
792 void dccp_ackpkts_check_rcv_ackno(struct dccp_ackpkts *ap, struct sock *sk,
793                                  u64 ackno)
794 {
795         /* Check if we actually sent an ACK vector */
796         if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
797                 return;
798
799         if (ackno == ap->dccpap_ack_seqno) {
800 #ifdef CONFIG_IP_DCCP_DEBUG
801                 struct dccp_sock *dp = dccp_sk(sk);
802                 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
803                                         "CLIENT rx ack: " : "server rx ack: ";
804 #endif
805                 dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
806                               "ack_ackno=%llu, ACKED!\n",
807                               debug_prefix, 1,
808                               (unsigned long long) ap->dccpap_ack_seqno,
809                               (unsigned long long) ap->dccpap_ack_ackno);
810                 dccp_ackpkts_trow_away_ack_record(ap);
811                 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
812         }
813 }
814
815 static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap,
816                                              struct sock *sk, u64 ackno,
817                                              const unsigned char len,
818                                              const unsigned char *vector)
819 {
820         unsigned char i;
821
822         /* Check if we actually sent an ACK vector */
823         if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
824                 return;
825         /*
826          * We're in the receiver half connection, so if the received an ACK
827          * vector ackno (e.g. 50) before dccpap_ack_seqno (e.g. 52), we're
828          * not interested.
829          *
830          * Extra explanation with example:
831          * 
832          * if we received an ACK vector with ackno 50, it can only be acking
833          * 50, 49, 48, etc, not 52 (the seqno for the ACK vector we sent).
834          */
835         /* dccp_pr_debug("is %llu < %llu? ", ackno, ap->dccpap_ack_seqno); */
836         if (before48(ackno, ap->dccpap_ack_seqno)) {
837                 /* dccp_pr_debug_cat("yes\n"); */
838                 return;
839         }
840         /* dccp_pr_debug_cat("no\n"); */
841
842         i = len;
843         while (i--) {
844                 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
845                 u64 ackno_end_rl;
846
847                 dccp_set_seqno(&ackno_end_rl, ackno - rl);
848
849                 /*
850                  * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl,
851                  * ap->dccpap_ack_seqno, ackno);
852                  */
853                 if (between48(ap->dccpap_ack_seqno, ackno_end_rl, ackno)) {
854                         const u8 state = (*vector &
855                                           DCCP_ACKPKTS_STATE_MASK) >> 6;
856                         /* dccp_pr_debug_cat("yes\n"); */
857
858                         if (state != DCCP_ACKPKTS_STATE_NOT_RECEIVED) {
859 #ifdef CONFIG_IP_DCCP_DEBUG
860                                 struct dccp_sock *dp = dccp_sk(sk);
861                                 const char *debug_prefix =
862                                         dp->dccps_role == DCCP_ROLE_CLIENT ?
863                                         "CLIENT rx ack: " : "server rx ack: ";
864 #endif
865                                 dccp_pr_debug("%sACK vector 0, len=%d, "
866                                               "ack_seqno=%llu, ack_ackno=%llu, "
867                                               "ACKED!\n",
868                                               debug_prefix, len,
869                                               (unsigned long long)
870                                               ap->dccpap_ack_seqno,
871                                               (unsigned long long)
872                                               ap->dccpap_ack_ackno);
873                                 dccp_ackpkts_trow_away_ack_record(ap);
874                         }
875                         /*
876                          * If dccpap_ack_seqno was not received, no problem
877                          * we'll send another ACK vector.
878                          */
879                         ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
880                         break;
881                 }
882                 /* dccp_pr_debug_cat("no\n"); */
883
884                 dccp_set_seqno(&ackno, ackno_end_rl - 1);
885                 ++vector;
886         }
887 }