[DCCP]: Simplified conditions due to use of enum:8 states
[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 <ian.mcdonald@jandi.co.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/dccp.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/skbuff.h>
19
20 #include "ackvec.h"
21 #include "ccid.h"
22 #include "dccp.h"
23 #include "feat.h"
24
25 int sysctl_dccp_feat_sequence_window = DCCPF_INITIAL_SEQUENCE_WINDOW;
26 int sysctl_dccp_feat_rx_ccid          = DCCPF_INITIAL_CCID;
27 int sysctl_dccp_feat_tx_ccid          = DCCPF_INITIAL_CCID;
28 int sysctl_dccp_feat_ack_ratio        = DCCPF_INITIAL_ACK_RATIO;
29 int sysctl_dccp_feat_send_ack_vector = DCCPF_INITIAL_SEND_ACK_VECTOR;
30 int sysctl_dccp_feat_send_ndp_count  = DCCPF_INITIAL_SEND_NDP_COUNT;
31
32 EXPORT_SYMBOL_GPL(sysctl_dccp_feat_sequence_window);
33
34 void dccp_minisock_init(struct dccp_minisock *dmsk)
35 {
36         dmsk->dccpms_sequence_window = sysctl_dccp_feat_sequence_window;
37         dmsk->dccpms_rx_ccid         = sysctl_dccp_feat_rx_ccid;
38         dmsk->dccpms_tx_ccid         = sysctl_dccp_feat_tx_ccid;
39         dmsk->dccpms_ack_ratio       = sysctl_dccp_feat_ack_ratio;
40         dmsk->dccpms_send_ack_vector = sysctl_dccp_feat_send_ack_vector;
41         dmsk->dccpms_send_ndp_count  = sysctl_dccp_feat_send_ndp_count;
42 }
43
44 static u32 dccp_decode_value_var(const unsigned char *bf, const u8 len)
45 {
46         u32 value = 0;
47
48         if (len > 3)
49                 value += *bf++ << 24;
50         if (len > 2)
51                 value += *bf++ << 16;
52         if (len > 1)
53                 value += *bf++ << 8;
54         if (len > 0)
55                 value += *bf;
56
57         return value;
58 }
59
60 int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
61 {
62         struct dccp_sock *dp = dccp_sk(sk);
63         const struct dccp_hdr *dh = dccp_hdr(skb);
64         const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type;
65         unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
66         unsigned char *opt_ptr = options;
67         const unsigned char *opt_end = (unsigned char *)dh +
68                                         (dh->dccph_doff * 4);
69         struct dccp_options_received *opt_recv = &dp->dccps_options_received;
70         unsigned char opt, len;
71         unsigned char *value;
72         u32 elapsed_time;
73         int rc;
74         int mandatory = 0;
75
76         memset(opt_recv, 0, sizeof(*opt_recv));
77
78         opt = len = 0;
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_MANDATORY:
108                         if (mandatory)
109                                 goto out_invalid_option;
110                         if (pkt_type != DCCP_PKT_DATA)
111                                 mandatory = 1;
112                         break;
113                 case DCCPO_NDP_COUNT:
114                         if (len > 3)
115                                 goto out_invalid_option;
116
117                         opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
118                         dccp_pr_debug("%s rx opt: NDP count=%d\n", dccp_role(sk),
119                                       opt_recv->dccpor_ndp);
120                         break;
121                 case DCCPO_CHANGE_L:
122                         /* fall through */
123                 case DCCPO_CHANGE_R:
124                         if (len < 2)
125                                 goto out_invalid_option;
126                         rc = dccp_feat_change_recv(sk, opt, *value, value + 1,
127                                                    len - 1);
128                         /*
129                          * When there is a change error, change_recv is
130                          * responsible for dealing with it.  i.e. reply with an
131                          * empty confirm.
132                          * If the change was mandatory, then we need to die.
133                          */
134                         if (rc && mandatory)
135                                 goto out_invalid_option;
136                         break;
137                 case DCCPO_CONFIRM_L:
138                         /* fall through */
139                 case DCCPO_CONFIRM_R:
140                         if (len < 2)
141                                 goto out_invalid_option;
142                         if (dccp_feat_confirm_recv(sk, opt, *value,
143                                                    value + 1, len - 1))
144                                 goto out_invalid_option;
145                         break;
146                 case DCCPO_ACK_VECTOR_0:
147                 case DCCPO_ACK_VECTOR_1:
148                         if (pkt_type == DCCP_PKT_DATA)
149                                 break;
150
151                         if (dccp_msk(sk)->dccpms_send_ack_vector &&
152                             dccp_ackvec_parse(sk, skb, opt, value, len))
153                                 goto out_invalid_option;
154                         break;
155                 case DCCPO_TIMESTAMP:
156                         if (len != 4)
157                                 goto out_invalid_option;
158
159                         opt_recv->dccpor_timestamp = ntohl(*(__be32 *)value);
160
161                         dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp;
162                         dccp_timestamp(sk, &dp->dccps_timestamp_time);
163
164                         dccp_pr_debug("%s rx opt: TIMESTAMP=%u, ackno=%llu\n",
165                                       dccp_role(sk), opt_recv->dccpor_timestamp,
166                                       (unsigned long long)
167                                       DCCP_SKB_CB(skb)->dccpd_ack_seq);
168                         break;
169                 case DCCPO_TIMESTAMP_ECHO:
170                         if (len != 4 && len != 6 && len != 8)
171                                 goto out_invalid_option;
172
173                         opt_recv->dccpor_timestamp_echo = ntohl(*(__be32 *)value);
174
175                         dccp_pr_debug("%s rx opt: TIMESTAMP_ECHO=%u, len=%d, "
176                                       "ackno=%llu, ",  dccp_role(sk),
177                                       opt_recv->dccpor_timestamp_echo,
178                                       len + 2,
179                                       (unsigned long long)
180                                       DCCP_SKB_CB(skb)->dccpd_ack_seq);
181
182
183                         if (len == 4)
184                                 break;
185
186                         if (len == 6)
187                                 elapsed_time = ntohs(*(__be16 *)(value + 4));
188                         else
189                                 elapsed_time = ntohl(*(__be32 *)(value + 4));
190
191                         /* Give precedence to the biggest ELAPSED_TIME */
192                         if (elapsed_time > opt_recv->dccpor_elapsed_time)
193                                 opt_recv->dccpor_elapsed_time = elapsed_time;
194                         break;
195                 case DCCPO_ELAPSED_TIME:
196                         if (len != 2 && len != 4)
197                                 goto out_invalid_option;
198
199                         if (pkt_type == DCCP_PKT_DATA)
200                                 continue;
201
202                         if (len == 2)
203                                 elapsed_time = ntohs(*(__be16 *)value);
204                         else
205                                 elapsed_time = ntohl(*(__be32 *)value);
206
207                         if (elapsed_time > opt_recv->dccpor_elapsed_time)
208                                 opt_recv->dccpor_elapsed_time = elapsed_time;
209
210                         dccp_pr_debug("%s rx opt: ELAPSED_TIME=%d\n",
211                                       dccp_role(sk), elapsed_time);
212                         break;
213                         /*
214                          * From RFC 4340, sec. 10.3:
215                          *
216                          *      Option numbers 128 through 191 are for
217                          *      options sent from the HC-Sender to the
218                          *      HC-Receiver; option numbers 192 through 255
219                          *      are for options sent from the HC-Receiver to
220                          *      the HC-Sender.
221                          */
222                 case 128 ... 191: {
223                         const u16 idx = value - options;
224
225                         if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
226                                                      opt, len, idx,
227                                                      value) != 0)
228                                 goto out_invalid_option;
229                 }
230                         break;
231                 case 192 ... 255: {
232                         const u16 idx = value - options;
233
234                         if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
235                                                      opt, len, idx,
236                                                      value) != 0)
237                                 goto out_invalid_option;
238                 }
239                         break;
240                 default:
241                         DCCP_CRIT("DCCP(%p): option %d(len=%d) not "
242                                   "implemented, ignoring", sk, opt, len);
243                         break;
244                 }
245
246                 if (opt != DCCPO_MANDATORY)
247                         mandatory = 0;
248         }
249
250         /* mandatory was the last byte in option list -> reset connection */
251         if (mandatory)
252                 goto out_invalid_option;
253
254         return 0;
255
256 out_invalid_option:
257         DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
258         DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR;
259         DCCP_WARN("DCCP(%p): invalid option %d, len=%d", sk, opt, len);
260         return -1;
261 }
262
263 EXPORT_SYMBOL_GPL(dccp_parse_options);
264
265 static void dccp_encode_value_var(const u32 value, unsigned char *to,
266                                   const unsigned int len)
267 {
268         if (len > 3)
269                 *to++ = (value & 0xFF000000) >> 24;
270         if (len > 2)
271                 *to++ = (value & 0xFF0000) >> 16;
272         if (len > 1)
273                 *to++ = (value & 0xFF00) >> 8;
274         if (len > 0)
275                 *to++ = (value & 0xFF);
276 }
277
278 static inline int dccp_ndp_len(const int ndp)
279 {
280         return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3;
281 }
282
283 int dccp_insert_option(struct sock *sk, struct sk_buff *skb,
284                         const unsigned char option,
285                         const void *value, const unsigned char len)
286 {
287         unsigned char *to;
288
289         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN)
290                 return -1;
291
292         DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2;
293
294         to    = skb_push(skb, len + 2);
295         *to++ = option;
296         *to++ = len + 2;
297
298         memcpy(to, value, len);
299         return 0;
300 }
301
302 EXPORT_SYMBOL_GPL(dccp_insert_option);
303
304 static int dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb)
305 {
306         struct dccp_sock *dp = dccp_sk(sk);
307         int ndp = dp->dccps_ndp_count;
308
309         if (dccp_non_data_packet(skb))
310                 ++dp->dccps_ndp_count;
311         else
312                 dp->dccps_ndp_count = 0;
313
314         if (ndp > 0) {
315                 unsigned char *ptr;
316                 const int ndp_len = dccp_ndp_len(ndp);
317                 const int len = ndp_len + 2;
318
319                 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
320                         return -1;
321
322                 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
323
324                 ptr = skb_push(skb, len);
325                 *ptr++ = DCCPO_NDP_COUNT;
326                 *ptr++ = len;
327                 dccp_encode_value_var(ndp, ptr, ndp_len);
328         }
329
330         return 0;
331 }
332
333 static inline int dccp_elapsed_time_len(const u32 elapsed_time)
334 {
335         return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4;
336 }
337
338 int dccp_insert_option_elapsed_time(struct sock *sk, struct sk_buff *skb,
339                                     u32 elapsed_time)
340 {
341         const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
342         const int len = 2 + elapsed_time_len;
343         unsigned char *to;
344
345         if (elapsed_time_len == 0)
346                 return 0;
347
348         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
349                 return -1;
350
351         DCCP_SKB_CB(skb)->dccpd_opt_len += len;
352
353         to    = skb_push(skb, len);
354         *to++ = DCCPO_ELAPSED_TIME;
355         *to++ = len;
356
357         if (elapsed_time_len == 2) {
358                 const __be16 var16 = htons((u16)elapsed_time);
359                 memcpy(to, &var16, 2);
360         } else {
361                 const __be32 var32 = htonl(elapsed_time);
362                 memcpy(to, &var32, 4);
363         }
364
365         return 0;
366 }
367
368 EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time);
369
370 void dccp_timestamp(const struct sock *sk, struct timeval *tv)
371 {
372         const struct dccp_sock *dp = dccp_sk(sk);
373
374         do_gettimeofday(tv);
375         tv->tv_sec  -= dp->dccps_epoch.tv_sec;
376         tv->tv_usec -= dp->dccps_epoch.tv_usec;
377
378         while (tv->tv_usec < 0) {
379                 tv->tv_sec--;
380                 tv->tv_usec += USEC_PER_SEC;
381         }
382 }
383
384 EXPORT_SYMBOL_GPL(dccp_timestamp);
385
386 int dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb)
387 {
388         struct timeval tv;
389         __be32 now;
390
391         dccp_timestamp(sk, &tv);
392         now = htonl(timeval_usecs(&tv) / 10);
393         /* yes this will overflow but that is the point as we want a
394          * 10 usec 32 bit timer which mean it wraps every 11.9 hours */
395
396         return dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
397 }
398
399 EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp);
400
401 static int dccp_insert_option_timestamp_echo(struct sock *sk,
402                                              struct sk_buff *skb)
403 {
404         struct dccp_sock *dp = dccp_sk(sk);
405         struct timeval now;
406         __be32 tstamp_echo;
407         u32 elapsed_time;
408         int len, elapsed_time_len;
409         unsigned char *to;
410
411         dccp_timestamp(sk, &now);
412         elapsed_time = timeval_delta(&now, &dp->dccps_timestamp_time) / 10;
413         elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
414         len = 6 + elapsed_time_len;
415
416         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
417                 return -1;
418
419         DCCP_SKB_CB(skb)->dccpd_opt_len += len;
420
421         to    = skb_push(skb, len);
422         *to++ = DCCPO_TIMESTAMP_ECHO;
423         *to++ = len;
424
425         tstamp_echo = htonl(dp->dccps_timestamp_echo);
426         memcpy(to, &tstamp_echo, 4);
427         to += 4;
428
429         if (elapsed_time_len == 2) {
430                 const __be16 var16 = htons((u16)elapsed_time);
431                 memcpy(to, &var16, 2);
432         } else if (elapsed_time_len == 4) {
433                 const __be32 var32 = htonl(elapsed_time);
434                 memcpy(to, &var32, 4);
435         }
436
437         dp->dccps_timestamp_echo = 0;
438         dp->dccps_timestamp_time.tv_sec = 0;
439         dp->dccps_timestamp_time.tv_usec = 0;
440         return 0;
441 }
442
443 static int dccp_insert_feat_opt(struct sk_buff *skb, u8 type, u8 feat,
444                                 u8 *val, u8 len)
445 {
446         u8 *to;
447
448         if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 3 > DCCP_MAX_OPT_LEN) {
449                 DCCP_WARN("packet too small for feature %d option!\n", feat);
450                 return -1;
451         }
452
453         DCCP_SKB_CB(skb)->dccpd_opt_len += len + 3;
454
455         to    = skb_push(skb, len + 3);
456         *to++ = type;
457         *to++ = len + 3;
458         *to++ = feat;
459
460         if (len)
461                 memcpy(to, val, len);
462
463         dccp_pr_debug("%s(%s (%d), ...), length %d\n",
464                       dccp_feat_typename(type),
465                       dccp_feat_name(feat), feat, len);
466         return 0;
467 }
468
469 static int dccp_insert_options_feat(struct sock *sk, struct sk_buff *skb)
470 {
471         struct dccp_sock *dp = dccp_sk(sk);
472         struct dccp_minisock *dmsk = dccp_msk(sk);
473         struct dccp_opt_pend *opt, *next;
474         int change = 0;
475
476         /* confirm any options [NN opts] */
477         list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
478                 dccp_insert_feat_opt(skb, opt->dccpop_type,
479                                      opt->dccpop_feat, opt->dccpop_val,
480                                      opt->dccpop_len);
481                 /* fear empty confirms */
482                 if (opt->dccpop_val)
483                         kfree(opt->dccpop_val);
484                 kfree(opt);
485         }
486         INIT_LIST_HEAD(&dmsk->dccpms_conf);
487
488         /* see which features we need to send */
489         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
490                 /* see if we need to send any confirm */
491                 if (opt->dccpop_sc) {
492                         dccp_insert_feat_opt(skb, opt->dccpop_type + 1,
493                                              opt->dccpop_feat,
494                                              opt->dccpop_sc->dccpoc_val,
495                                              opt->dccpop_sc->dccpoc_len);
496
497                         BUG_ON(!opt->dccpop_sc->dccpoc_val);
498                         kfree(opt->dccpop_sc->dccpoc_val);
499                         kfree(opt->dccpop_sc);
500                         opt->dccpop_sc = NULL;
501                 }
502
503                 /* any option not confirmed, re-send it */
504                 if (!opt->dccpop_conf) {
505                         dccp_insert_feat_opt(skb, opt->dccpop_type,
506                                              opt->dccpop_feat, opt->dccpop_val,
507                                              opt->dccpop_len);
508                         change++;
509                 }
510         }
511
512         /* Retransmit timer.
513          * If this is the master listening sock, we don't set a timer on it.  It
514          * should be fine because if the dude doesn't receive our RESPONSE
515          * [which will contain the CHANGE] he will send another REQUEST which
516          * will "retrnasmit" the change.
517          */
518         if (change && dp->dccps_role != DCCP_ROLE_LISTEN) {
519                 dccp_pr_debug("reset feat negotiation timer %p\n", sk);
520
521                 /* XXX don't reset the timer on re-transmissions.  I.e. reset it
522                  * only when sending new stuff i guess.  Currently the timer
523                  * never backs off because on re-transmission it just resets it!
524                  */
525                 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
526                                           inet_csk(sk)->icsk_rto, DCCP_RTO_MAX);
527         }
528
529         return 0;
530 }
531
532 int dccp_insert_options(struct sock *sk, struct sk_buff *skb)
533 {
534         struct dccp_sock *dp = dccp_sk(sk);
535         struct dccp_minisock *dmsk = dccp_msk(sk);
536
537         DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
538
539         if (dmsk->dccpms_send_ndp_count &&
540             dccp_insert_option_ndp(sk, skb))
541                 return -1;
542
543         if (!dccp_packet_without_ack(skb)) {
544                 if (dmsk->dccpms_send_ack_vector &&
545                     dccp_ackvec_pending(dp->dccps_hc_rx_ackvec) &&
546                     dccp_insert_option_ackvec(sk, skb))
547                         return -1;
548
549                 if (dp->dccps_timestamp_echo != 0 &&
550                     dccp_insert_option_timestamp_echo(sk, skb))
551                         return -1;
552         }
553
554         if (dp->dccps_hc_rx_insert_options) {
555                 if (ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb))
556                         return -1;
557                 dp->dccps_hc_rx_insert_options = 0;
558         }
559         if (dp->dccps_hc_tx_insert_options) {
560                 if (ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb))
561                         return -1;
562                 dp->dccps_hc_tx_insert_options = 0;
563         }
564
565         /* Feature negotiation */
566         /* Data packets can't do feat negotiation */
567         if (DCCP_SKB_CB(skb)->dccpd_type != DCCP_PKT_DATA &&
568             DCCP_SKB_CB(skb)->dccpd_type != DCCP_PKT_DATAACK &&
569             dccp_insert_options_feat(sk, skb))
570                 return -1;
571
572         /* XXX: insert other options when appropriate */
573
574         if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) {
575                 /* The length of all options has to be a multiple of 4 */
576                 int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4;
577
578                 if (padding != 0) {
579                         padding = 4 - padding;
580                         memset(skb_push(skb, padding), 0, padding);
581                         DCCP_SKB_CB(skb)->dccpd_opt_len += padding;
582                 }
583         }
584
585         return 0;
586 }