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