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