[DCCP]: Remove unused and redundant validation functions
[pandora-kernel.git] / net / dccp / feat.c
1 /*
2  *  net/dccp/feat.c
3  *
4  *  An implementation of the DCCP protocol
5  *  Andrea Bittau <a.bittau@cs.ucl.ac.uk>
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/module.h>
14
15 #include "ccid.h"
16 #include "feat.h"
17
18 #define DCCP_FEAT_SP_NOAGREE (-123)
19
20 int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
21                      u8 *val, u8 len, gfp_t gfp)
22 {
23         struct dccp_opt_pend *opt;
24
25         dccp_feat_debug(type, feature, *val);
26
27         if (len > 3) {
28                 DCCP_WARN("invalid length %d\n", len);
29                 return 1;
30         }
31         /* XXX add further sanity checks */
32
33         /* check if that feature is already being negotiated */
34         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
35                 /* ok we found a negotiation for this option already */
36                 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
37                         dccp_pr_debug("Replacing old\n");
38                         /* replace */
39                         BUG_ON(opt->dccpop_val == NULL);
40                         kfree(opt->dccpop_val);
41                         opt->dccpop_val  = val;
42                         opt->dccpop_len  = len;
43                         opt->dccpop_conf = 0;
44                         return 0;
45                 }
46         }
47
48         /* negotiation for a new feature */
49         opt = kmalloc(sizeof(*opt), gfp);
50         if (opt == NULL)
51                 return -ENOMEM;
52
53         opt->dccpop_type = type;
54         opt->dccpop_feat = feature;
55         opt->dccpop_len  = len;
56         opt->dccpop_val  = val;
57         opt->dccpop_conf = 0;
58         opt->dccpop_sc   = NULL;
59
60         BUG_ON(opt->dccpop_val == NULL);
61
62         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
63         return 0;
64 }
65
66 EXPORT_SYMBOL_GPL(dccp_feat_change);
67
68 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
69 {
70         struct dccp_sock *dp = dccp_sk(sk);
71         struct dccp_minisock *dmsk = dccp_msk(sk);
72         /* figure out if we are changing our CCID or the peer's */
73         const int rx = type == DCCPO_CHANGE_R;
74         const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
75         struct ccid *new_ccid;
76
77         /* Check if nothing is being changed. */
78         if (ccid_nr == new_ccid_nr)
79                 return 0;
80
81         new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
82         if (new_ccid == NULL)
83                 return -ENOMEM;
84
85         if (rx) {
86                 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
87                 dp->dccps_hc_rx_ccid = new_ccid;
88                 dmsk->dccpms_rx_ccid = new_ccid_nr;
89         } else {
90                 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
91                 dp->dccps_hc_tx_ccid = new_ccid;
92                 dmsk->dccpms_tx_ccid = new_ccid_nr;
93         }
94
95         return 0;
96 }
97
98 /* XXX taking only u8 vals */
99 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
100 {
101         dccp_feat_debug(type, feat, val);
102
103         switch (feat) {
104         case DCCPF_CCID:
105                 return dccp_feat_update_ccid(sk, type, val);
106         default:
107                 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
108                               dccp_feat_typename(type), feat);
109                 break;
110         }
111         return 0;
112 }
113
114 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
115                                u8 *rpref, u8 rlen)
116 {
117         struct dccp_sock *dp = dccp_sk(sk);
118         u8 *spref, slen, *res = NULL;
119         int i, j, rc, agree = 1;
120
121         BUG_ON(rpref == NULL);
122
123         /* check if we are the black sheep */
124         if (dp->dccps_role == DCCP_ROLE_CLIENT) {
125                 spref = rpref;
126                 slen  = rlen;
127                 rpref = opt->dccpop_val;
128                 rlen  = opt->dccpop_len;
129         } else {
130                 spref = opt->dccpop_val;
131                 slen  = opt->dccpop_len;
132         }
133         /*
134          * Now we have server preference list in spref and client preference in
135          * rpref
136          */
137         BUG_ON(spref == NULL);
138         BUG_ON(rpref == NULL);
139
140         /* FIXME sanity check vals */
141
142         /* Are values in any order?  XXX Lame "algorithm" here */
143         /* XXX assume values are 1 byte */
144         for (i = 0; i < slen; i++) {
145                 for (j = 0; j < rlen; j++) {
146                         if (spref[i] == rpref[j]) {
147                                 res = &spref[i];
148                                 break;
149                         }
150                 }
151                 if (res)
152                         break;
153         }
154
155         /* we didn't agree on anything */
156         if (res == NULL) {
157                 /* confirm previous value */
158                 switch (opt->dccpop_feat) {
159                 case DCCPF_CCID:
160                         /* XXX did i get this right? =P */
161                         if (opt->dccpop_type == DCCPO_CHANGE_L)
162                                 res = &dccp_msk(sk)->dccpms_tx_ccid;
163                         else
164                                 res = &dccp_msk(sk)->dccpms_rx_ccid;
165                         break;
166
167                 default:
168                         DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
169                         /* XXX implement res */
170                         return -EFAULT;
171                 }
172
173                 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
174                 agree = 0; /* this is used for mandatory options... */
175         }
176
177         /* need to put result and our preference list */
178         /* XXX assume 1 byte vals */
179         rlen = 1 + opt->dccpop_len;
180         rpref = kmalloc(rlen, GFP_ATOMIC);
181         if (rpref == NULL)
182                 return -ENOMEM;
183
184         *rpref = *res;
185         memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
186
187         /* put it in the "confirm queue" */
188         if (opt->dccpop_sc == NULL) {
189                 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
190                 if (opt->dccpop_sc == NULL) {
191                         kfree(rpref);
192                         return -ENOMEM;
193                 }
194         } else {
195                 /* recycle the confirm slot */
196                 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
197                 kfree(opt->dccpop_sc->dccpoc_val);
198                 dccp_pr_debug("recycling confirm slot\n");
199         }
200         memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
201
202         opt->dccpop_sc->dccpoc_val = rpref;
203         opt->dccpop_sc->dccpoc_len = rlen;
204
205         /* update the option on our side [we are about to send the confirm] */
206         rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
207         if (rc) {
208                 kfree(opt->dccpop_sc->dccpoc_val);
209                 kfree(opt->dccpop_sc);
210                 opt->dccpop_sc = NULL;
211                 return rc;
212         }
213
214         dccp_pr_debug("Will confirm %d\n", *rpref);
215
216         /* say we want to change to X but we just got a confirm X, suppress our
217          * change
218          */
219         if (!opt->dccpop_conf) {
220                 if (*opt->dccpop_val == *res)
221                         opt->dccpop_conf = 1;
222                 dccp_pr_debug("won't ask for change of same feature\n");
223         }
224
225         return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
226 }
227
228 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
229 {
230         struct dccp_minisock *dmsk = dccp_msk(sk);
231         struct dccp_opt_pend *opt;
232         int rc = 1;
233         u8 t;
234
235         /*
236          * We received a CHANGE.  We gotta match it against our own preference
237          * list.  If we got a CHANGE_R it means it's a change for us, so we need
238          * to compare our CHANGE_L list.
239          */
240         if (type == DCCPO_CHANGE_L)
241                 t = DCCPO_CHANGE_R;
242         else
243                 t = DCCPO_CHANGE_L;
244
245         /* find our preference list for this feature */
246         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
247                 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
248                         continue;
249
250                 /* find the winner from the two preference lists */
251                 rc = dccp_feat_reconcile(sk, opt, val, len);
252                 break;
253         }
254
255         /* We didn't deal with the change.  This can happen if we have no
256          * preference list for the feature.  In fact, it just shouldn't
257          * happen---if we understand a feature, we should have a preference list
258          * with at least the default value.
259          */
260         BUG_ON(rc == 1);
261
262         return rc;
263 }
264
265 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
266 {
267         struct dccp_opt_pend *opt;
268         struct dccp_minisock *dmsk = dccp_msk(sk);
269         u8 *copy;
270         int rc;
271
272         /* NN features must be Change L (sec. 6.3.2) */
273         if (type != DCCPO_CHANGE_L) {
274                 dccp_pr_debug("received %s for NN feature %d\n",
275                                 dccp_feat_typename(type), feature);
276                 return -EFAULT;
277         }
278
279         /* XXX sanity check opt val */
280
281         /* copy option so we can confirm it */
282         opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
283         if (opt == NULL)
284                 return -ENOMEM;
285
286         copy = kmemdup(val, len, GFP_ATOMIC);
287         if (copy == NULL) {
288                 kfree(opt);
289                 return -ENOMEM;
290         }
291
292         opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
293         opt->dccpop_feat = feature;
294         opt->dccpop_val  = copy;
295         opt->dccpop_len  = len;
296
297         /* change feature */
298         rc = dccp_feat_update(sk, type, feature, *val);
299         if (rc) {
300                 kfree(opt->dccpop_val);
301                 kfree(opt);
302                 return rc;
303         }
304
305         dccp_feat_debug(type, feature, *copy);
306
307         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
308
309         return 0;
310 }
311
312 static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
313                                     u8 type, u8 feature)
314 {
315         /* XXX check if other confirms for that are queued and recycle slot */
316         struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
317
318         if (opt == NULL) {
319                 /* XXX what do we do?  Ignoring should be fine.  It's a change
320                  * after all =P
321                  */
322                 return;
323         }
324
325         switch (type) {
326         case DCCPO_CHANGE_L:
327                 opt->dccpop_type = DCCPO_CONFIRM_R;
328                 break;
329         case DCCPO_CHANGE_R:
330                 opt->dccpop_type = DCCPO_CONFIRM_L;
331                 break;
332         default:
333                 DCCP_WARN("invalid type %d\n", type);
334                 kfree(opt);
335                 return;
336         }
337         opt->dccpop_feat = feature;
338         opt->dccpop_val  = NULL;
339         opt->dccpop_len  = 0;
340
341         /* change feature */
342         dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
343
344         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
345 }
346
347 static void dccp_feat_flush_confirm(struct sock *sk)
348 {
349         struct dccp_minisock *dmsk = dccp_msk(sk);
350         /* Check if there is anything to confirm in the first place */
351         int yes = !list_empty(&dmsk->dccpms_conf);
352
353         if (!yes) {
354                 struct dccp_opt_pend *opt;
355
356                 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
357                         if (opt->dccpop_conf) {
358                                 yes = 1;
359                                 break;
360                         }
361                 }
362         }
363
364         if (!yes)
365                 return;
366
367         /* OK there is something to confirm... */
368         /* XXX check if packet is in flight?  Send delayed ack?? */
369         if (sk->sk_state == DCCP_OPEN)
370                 dccp_send_ack(sk);
371 }
372
373 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
374 {
375         int rc;
376
377         dccp_feat_debug(type, feature, *val);
378
379         /* figure out if it's SP or NN feature */
380         switch (feature) {
381         /* deal with SP features */
382         case DCCPF_CCID:
383                 rc = dccp_feat_sp(sk, type, feature, val, len);
384                 break;
385
386         /* deal with NN features */
387         case DCCPF_ACK_RATIO:
388                 rc = dccp_feat_nn(sk, type, feature, val, len);
389                 break;
390
391         /* XXX implement other features */
392         default:
393                 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
394                               dccp_feat_typename(type), feature);
395                 rc = -EFAULT;
396                 break;
397         }
398
399         /* check if there were problems changing features */
400         if (rc) {
401                 /* If we don't agree on SP, we sent a confirm for old value.
402                  * However we propagate rc to caller in case option was
403                  * mandatory
404                  */
405                 if (rc != DCCP_FEAT_SP_NOAGREE)
406                         dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
407         }
408
409         /* generate the confirm [if required] */
410         dccp_feat_flush_confirm(sk);
411
412         return rc;
413 }
414
415 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
416
417 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
418                            u8 *val, u8 len)
419 {
420         u8 t;
421         struct dccp_opt_pend *opt;
422         struct dccp_minisock *dmsk = dccp_msk(sk);
423         int found = 0;
424         int all_confirmed = 1;
425
426         dccp_feat_debug(type, feature, *val);
427
428         /* locate our change request */
429         switch (type) {
430         case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
431         case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
432         default:              DCCP_WARN("invalid type %d\n", type);
433                               return 1;
434
435         }
436         /* XXX sanity check feature value */
437
438         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
439                 if (!opt->dccpop_conf && opt->dccpop_type == t &&
440                     opt->dccpop_feat == feature) {
441                         found = 1;
442                         dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
443
444                         /* XXX do sanity check */
445
446                         opt->dccpop_conf = 1;
447
448                         /* We got a confirmation---change the option */
449                         dccp_feat_update(sk, opt->dccpop_type,
450                                          opt->dccpop_feat, *val);
451
452                         /* XXX check the return value of dccp_feat_update */
453                         break;
454                 }
455
456                 if (!opt->dccpop_conf)
457                         all_confirmed = 0;
458         }
459
460         /* fix re-transmit timer */
461         /* XXX gotta make sure that no option negotiation occurs during
462          * connection shutdown.  Consider that the CLOSEREQ is sent and timer is
463          * on.  if all options are confirmed it might kill timer which should
464          * remain alive until close is received.
465          */
466         if (all_confirmed) {
467                 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
468                 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
469         }
470
471         if (!found)
472                 dccp_pr_debug("%s(%d, ...) never requested\n",
473                               dccp_feat_typename(type), feature);
474         return 0;
475 }
476
477 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
478
479 void dccp_feat_clean(struct dccp_minisock *dmsk)
480 {
481         struct dccp_opt_pend *opt, *next;
482
483         list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
484                                  dccpop_node) {
485                 BUG_ON(opt->dccpop_val == NULL);
486                 kfree(opt->dccpop_val);
487
488                 if (opt->dccpop_sc != NULL) {
489                         BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
490                         kfree(opt->dccpop_sc->dccpoc_val);
491                         kfree(opt->dccpop_sc);
492                 }
493
494                 kfree(opt);
495         }
496         INIT_LIST_HEAD(&dmsk->dccpms_pending);
497
498         list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
499                 BUG_ON(opt == NULL);
500                 if (opt->dccpop_val != NULL)
501                         kfree(opt->dccpop_val);
502                 kfree(opt);
503         }
504         INIT_LIST_HEAD(&dmsk->dccpms_conf);
505 }
506
507 EXPORT_SYMBOL_GPL(dccp_feat_clean);
508
509 /* this is to be called only when a listening sock creates its child.  It is
510  * assumed by the function---the confirm is not duplicated, but rather it is
511  * "passed on".
512  */
513 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
514 {
515         struct dccp_minisock *olddmsk = dccp_msk(oldsk);
516         struct dccp_minisock *newdmsk = dccp_msk(newsk);
517         struct dccp_opt_pend *opt;
518         int rc = 0;
519
520         INIT_LIST_HEAD(&newdmsk->dccpms_pending);
521         INIT_LIST_HEAD(&newdmsk->dccpms_conf);
522
523         list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
524                 struct dccp_opt_pend *newopt;
525                 /* copy the value of the option */
526                 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
527
528                 if (val == NULL)
529                         goto out_clean;
530
531                 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
532                 if (newopt == NULL) {
533                         kfree(val);
534                         goto out_clean;
535                 }
536
537                 /* insert the option */
538                 newopt->dccpop_val = val;
539                 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
540
541                 /* XXX what happens with backlogs and multiple connections at
542                  * once...
543                  */
544                 /* the master socket no longer needs to worry about confirms */
545                 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
546
547                 /* reset state for a new socket */
548                 opt->dccpop_conf = 0;
549         }
550
551         /* XXX not doing anything about the conf queue */
552
553 out:
554         return rc;
555
556 out_clean:
557         dccp_feat_clean(newdmsk);
558         rc = -ENOMEM;
559         goto out;
560 }
561
562 EXPORT_SYMBOL_GPL(dccp_feat_clone);
563
564 static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
565                             u8 *val, u8 len)
566 {
567         int rc = -ENOMEM;
568         u8 *copy = kmemdup(val, len, GFP_KERNEL);
569
570         if (copy != NULL) {
571                 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
572                 if (rc)
573                         kfree(copy);
574         }
575         return rc;
576 }
577
578 int dccp_feat_init(struct dccp_minisock *dmsk)
579 {
580         int rc;
581
582         INIT_LIST_HEAD(&dmsk->dccpms_pending);
583         INIT_LIST_HEAD(&dmsk->dccpms_conf);
584
585         /* CCID L */
586         rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
587                               &dmsk->dccpms_tx_ccid, 1);
588         if (rc)
589                 goto out;
590
591         /* CCID R */
592         rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
593                               &dmsk->dccpms_rx_ccid, 1);
594         if (rc)
595                 goto out;
596
597         /* Ack ratio */
598         rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
599                               &dmsk->dccpms_ack_ratio, 1);
600 out:
601         return rc;
602 }
603
604 EXPORT_SYMBOL_GPL(dccp_feat_init);
605
606 #ifdef CONFIG_IP_DCCP_DEBUG
607 const char *dccp_feat_typename(const u8 type)
608 {
609         switch(type) {
610         case DCCPO_CHANGE_L:  return("ChangeL");
611         case DCCPO_CONFIRM_L: return("ConfirmL");
612         case DCCPO_CHANGE_R:  return("ChangeR");
613         case DCCPO_CONFIRM_R: return("ConfirmR");
614         /* the following case must not appear in feature negotation  */
615         default:              dccp_pr_debug("unknown type %d [BUG!]\n", type);
616         }
617         return NULL;
618 }
619
620 EXPORT_SYMBOL_GPL(dccp_feat_typename);
621
622 const char *dccp_feat_name(const u8 feat)
623 {
624         static const char *feature_names[] = {
625                 [DCCPF_RESERVED]        = "Reserved",
626                 [DCCPF_CCID]            = "CCID",
627                 [DCCPF_SHORT_SEQNOS]    = "Allow Short Seqnos",
628                 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
629                 [DCCPF_ECN_INCAPABLE]   = "ECN Incapable",
630                 [DCCPF_ACK_RATIO]       = "Ack Ratio",
631                 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
632                 [DCCPF_SEND_NDP_COUNT]  = "Send NDP Count",
633                 [DCCPF_MIN_CSUM_COVER]  = "Min. Csum Coverage",
634                 [DCCPF_DATA_CHECKSUM]   = "Send Data Checksum",
635         };
636         if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
637                 return feature_names[DCCPF_RESERVED];
638
639         if (feat >= DCCPF_MIN_CCID_SPECIFIC)
640                 return "CCID-specific";
641
642         return feature_names[feat];
643 }
644
645 EXPORT_SYMBOL_GPL(dccp_feat_name);
646 #endif /* CONFIG_IP_DCCP_DEBUG */