2ec2cd1176990cde94ce8b383bf8051a5190fd06
[safe/jmp/linux-2.6] / 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 static const struct {
27         u8                      feat_num;               /* DCCPF_xxx */
28         enum dccp_feat_type     rxtx;                   /* RX or TX  */
29         enum dccp_feat_type     reconciliation;         /* SP or NN  */
30         u8                      default_value;          /* as in 6.4 */
31 /*
32  *    Lookup table for location and type of features (from RFC 4340/4342)
33  *  +--------------------------+----+-----+----+----+---------+-----------+
34  *  | Feature                  | Location | Reconc. | Initial |  Section  |
35  *  |                          | RX | TX  | SP | NN |  Value  | Reference |
36  *  +--------------------------+----+-----+----+----+---------+-----------+
37  *  | DCCPF_CCID               |    |  X  | X  |    |   2     | 10        |
38  *  | DCCPF_SHORT_SEQNOS       |    |  X  | X  |    |   0     |  7.6.1    |
39  *  | DCCPF_SEQUENCE_WINDOW    |    |  X  |    | X  | 100     |  7.5.2    |
40  *  | DCCPF_ECN_INCAPABLE      | X  |     | X  |    |   0     | 12.1      |
41  *  | DCCPF_ACK_RATIO          |    |  X  |    | X  |   2     | 11.3      |
42  *  | DCCPF_SEND_ACK_VECTOR    | X  |     | X  |    |   0     | 11.5      |
43  *  | DCCPF_SEND_NDP_COUNT     |    |  X  | X  |    |   0     |  7.7.2    |
44  *  | DCCPF_MIN_CSUM_COVER     | X  |     | X  |    |   0     |  9.2.1    |
45  *  | DCCPF_DATA_CHECKSUM      | X  |     | X  |    |   0     |  9.3.1    |
46  *  | DCCPF_SEND_LEV_RATE      | X  |     | X  |    |   0     | 4342/8.4  |
47  *  +--------------------------+----+-----+----+----+---------+-----------+
48  */
49 } dccp_feat_table[] = {
50         { DCCPF_CCID,            FEAT_AT_TX, FEAT_SP, 2 },
51         { DCCPF_SHORT_SEQNOS,    FEAT_AT_TX, FEAT_SP, 0 },
52         { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100 },
53         { DCCPF_ECN_INCAPABLE,   FEAT_AT_RX, FEAT_SP, 0 },
54         { DCCPF_ACK_RATIO,       FEAT_AT_TX, FEAT_NN, 2 },
55         { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0 },
56         { DCCPF_SEND_NDP_COUNT,  FEAT_AT_TX, FEAT_SP, 0 },
57         { DCCPF_MIN_CSUM_COVER,  FEAT_AT_RX, FEAT_SP, 0 },
58         { DCCPF_DATA_CHECKSUM,   FEAT_AT_RX, FEAT_SP, 0 },
59         { DCCPF_SEND_LEV_RATE,   FEAT_AT_RX, FEAT_SP, 0 },
60 };
61 #define DCCP_FEAT_SUPPORTED_MAX         ARRAY_SIZE(dccp_feat_table)
62
63 /**
64  * dccp_feat_index  -  Hash function to map feature number into array position
65  * Returns consecutive array index or -1 if the feature is not understood.
66  */
67 static int dccp_feat_index(u8 feat_num)
68 {
69         /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
70         if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
71                 return feat_num - 1;
72
73         /*
74          * Other features: add cases for new feature types here after adding
75          * them to the above table.
76          */
77         switch (feat_num) {
78         case DCCPF_SEND_LEV_RATE:
79                         return DCCP_FEAT_SUPPORTED_MAX - 1;
80         }
81         return -1;
82 }
83
84 static u8 dccp_feat_type(u8 feat_num)
85 {
86         int idx = dccp_feat_index(feat_num);
87
88         if (idx < 0)
89                 return FEAT_UNKNOWN;
90         return dccp_feat_table[idx].reconciliation;
91 }
92
93 static int dccp_feat_default_value(u8 feat_num)
94 {
95         int idx = dccp_feat_index(feat_num);
96
97         return idx < 0 ? : dccp_feat_table[idx].default_value;
98 }
99
100 /* copy constructor, fval must not already contain allocated memory */
101 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
102 {
103         fval->sp.len = len;
104         if (fval->sp.len > 0) {
105                 fval->sp.vec = kmemdup(val, len, gfp_any());
106                 if (fval->sp.vec == NULL) {
107                         fval->sp.len = 0;
108                         return -ENOBUFS;
109                 }
110         }
111         return 0;
112 }
113
114 static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
115 {
116         if (unlikely(val == NULL))
117                 return;
118         if (dccp_feat_type(feat_num) == FEAT_SP)
119                 kfree(val->sp.vec);
120         memset(val, 0, sizeof(*val));
121 }
122
123 static struct dccp_feat_entry *
124               dccp_feat_clone_entry(struct dccp_feat_entry const *original)
125 {
126         struct dccp_feat_entry *new;
127         u8 type = dccp_feat_type(original->feat_num);
128
129         if (type == FEAT_UNKNOWN)
130                 return NULL;
131
132         new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
133         if (new == NULL)
134                 return NULL;
135
136         if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
137                                                       original->val.sp.vec,
138                                                       original->val.sp.len)) {
139                 kfree(new);
140                 return NULL;
141         }
142         return new;
143 }
144
145 static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
146 {
147         if (entry != NULL) {
148                 dccp_feat_val_destructor(entry->feat_num, &entry->val);
149                 kfree(entry);
150         }
151 }
152
153 /*
154  * List management functions
155  *
156  * Feature negotiation lists rely on and maintain the following invariants:
157  * - each feat_num in the list is known, i.e. we know its type and default value
158  * - each feat_num/is_local combination is unique (old entries are overwritten)
159  * - SP values are always freshly allocated
160  * - list is sorted in increasing order of feature number (faster lookup)
161  */
162 static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
163                                                      u8 feat_num, bool is_local)
164 {
165         struct dccp_feat_entry *entry;
166
167         list_for_each_entry(entry, fn_list, node)
168                 if (entry->feat_num == feat_num && entry->is_local == is_local)
169                         return entry;
170                 else if (entry->feat_num > feat_num)
171                         break;
172         return NULL;
173 }
174
175 /**
176  * dccp_feat_entry_new  -  Central list update routine (called by all others)
177  * @head:  list to add to
178  * @feat:  feature number
179  * @local: whether the local (1) or remote feature with number @feat is meant
180  * This is the only constructor and serves to ensure the above invariants.
181  */
182 static struct dccp_feat_entry *
183               dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
184 {
185         struct dccp_feat_entry *entry;
186
187         list_for_each_entry(entry, head, node)
188                 if (entry->feat_num == feat && entry->is_local == local) {
189                         dccp_feat_val_destructor(entry->feat_num, &entry->val);
190                         return entry;
191                 } else if (entry->feat_num > feat) {
192                         head = &entry->node;
193                         break;
194                 }
195
196         entry = kmalloc(sizeof(*entry), gfp_any());
197         if (entry != NULL) {
198                 entry->feat_num = feat;
199                 entry->is_local = local;
200                 list_add_tail(&entry->node, head);
201         }
202         return entry;
203 }
204
205 /**
206  * dccp_feat_push_change  -  Add/overwrite a Change option in the list
207  * @fn_list: feature-negotiation list to update
208  * @feat: one of %dccp_feature_numbers
209  * @local: whether local (1) or remote (0) @feat_num is meant
210  * @needs_mandatory: whether to use Mandatory feature negotiation options
211  * @fval: pointer to NN/SP value to be inserted (will be copied)
212  */
213 static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
214                                  u8 mandatory, dccp_feat_val *fval)
215 {
216         struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
217
218         if (new == NULL)
219                 return -ENOMEM;
220
221         new->feat_num        = feat;
222         new->is_local        = local;
223         new->state           = FEAT_INITIALISING;
224         new->needs_confirm   = 0;
225         new->empty_confirm   = 0;
226         new->val             = *fval;
227         new->needs_mandatory = mandatory;
228
229         return 0;
230 }
231
232 /**
233  * dccp_feat_push_confirm  -  Add a Confirm entry to the FN list
234  * @fn_list: feature-negotiation list to add to
235  * @feat: one of %dccp_feature_numbers
236  * @local: whether local (1) or remote (0) @feat_num is being confirmed
237  * @fval: pointer to NN/SP value to be inserted or NULL
238  * Returns 0 on success, a Reset code for further processing otherwise.
239  */
240 static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
241                                   dccp_feat_val *fval)
242 {
243         struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
244
245         if (new == NULL)
246                 return DCCP_RESET_CODE_TOO_BUSY;
247
248         new->feat_num        = feat;
249         new->is_local        = local;
250         new->state           = FEAT_STABLE;     /* transition in 6.6.2 */
251         new->needs_confirm   = 1;
252         new->empty_confirm   = (fval == NULL);
253         new->val.nn          = 0;               /* zeroes the whole structure */
254         if (!new->empty_confirm)
255                 new->val     = *fval;
256         new->needs_mandatory = 0;
257
258         return 0;
259 }
260
261 static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
262 {
263         return dccp_feat_push_confirm(fn_list, feat, local, NULL);
264 }
265
266 static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
267 {
268         list_del(&entry->node);
269         dccp_feat_entry_destructor(entry);
270 }
271
272 void dccp_feat_list_purge(struct list_head *fn_list)
273 {
274         struct dccp_feat_entry *entry, *next;
275
276         list_for_each_entry_safe(entry, next, fn_list, node)
277                 dccp_feat_entry_destructor(entry);
278         INIT_LIST_HEAD(fn_list);
279 }
280 EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
281
282 int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
283                      u8 *val, u8 len, gfp_t gfp)
284 {
285         struct dccp_opt_pend *opt;
286
287         dccp_feat_debug(type, feature, *val);
288
289         if (len > 3) {
290                 DCCP_WARN("invalid length %d\n", len);
291                 return -EINVAL;
292         }
293         /* XXX add further sanity checks */
294
295         /* check if that feature is already being negotiated */
296         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
297                 /* ok we found a negotiation for this option already */
298                 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
299                         dccp_pr_debug("Replacing old\n");
300                         /* replace */
301                         BUG_ON(opt->dccpop_val == NULL);
302                         kfree(opt->dccpop_val);
303                         opt->dccpop_val  = val;
304                         opt->dccpop_len  = len;
305                         opt->dccpop_conf = 0;
306                         return 0;
307                 }
308         }
309
310         /* negotiation for a new feature */
311         opt = kmalloc(sizeof(*opt), gfp);
312         if (opt == NULL)
313                 return -ENOMEM;
314
315         opt->dccpop_type = type;
316         opt->dccpop_feat = feature;
317         opt->dccpop_len  = len;
318         opt->dccpop_val  = val;
319         opt->dccpop_conf = 0;
320         opt->dccpop_sc   = NULL;
321
322         BUG_ON(opt->dccpop_val == NULL);
323
324         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
325         return 0;
326 }
327
328 EXPORT_SYMBOL_GPL(dccp_feat_change);
329
330 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
331 {
332         struct dccp_sock *dp = dccp_sk(sk);
333         struct dccp_minisock *dmsk = dccp_msk(sk);
334         /* figure out if we are changing our CCID or the peer's */
335         const int rx = type == DCCPO_CHANGE_R;
336         const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
337         struct ccid *new_ccid;
338
339         /* Check if nothing is being changed. */
340         if (ccid_nr == new_ccid_nr)
341                 return 0;
342
343         new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
344         if (new_ccid == NULL)
345                 return -ENOMEM;
346
347         if (rx) {
348                 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
349                 dp->dccps_hc_rx_ccid = new_ccid;
350                 dmsk->dccpms_rx_ccid = new_ccid_nr;
351         } else {
352                 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
353                 dp->dccps_hc_tx_ccid = new_ccid;
354                 dmsk->dccpms_tx_ccid = new_ccid_nr;
355         }
356
357         return 0;
358 }
359
360 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
361 {
362         dccp_feat_debug(type, feat, val);
363
364         switch (feat) {
365         case DCCPF_CCID:
366                 return dccp_feat_update_ccid(sk, type, val);
367         default:
368                 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
369                               dccp_feat_typename(type), feat);
370                 break;
371         }
372         return 0;
373 }
374
375 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
376                                u8 *rpref, u8 rlen)
377 {
378         struct dccp_sock *dp = dccp_sk(sk);
379         u8 *spref, slen, *res = NULL;
380         int i, j, rc, agree = 1;
381
382         BUG_ON(rpref == NULL);
383
384         /* check if we are the black sheep */
385         if (dp->dccps_role == DCCP_ROLE_CLIENT) {
386                 spref = rpref;
387                 slen  = rlen;
388                 rpref = opt->dccpop_val;
389                 rlen  = opt->dccpop_len;
390         } else {
391                 spref = opt->dccpop_val;
392                 slen  = opt->dccpop_len;
393         }
394         /*
395          * Now we have server preference list in spref and client preference in
396          * rpref
397          */
398         BUG_ON(spref == NULL);
399         BUG_ON(rpref == NULL);
400
401         /* FIXME sanity check vals */
402
403         /* Are values in any order?  XXX Lame "algorithm" here */
404         for (i = 0; i < slen; i++) {
405                 for (j = 0; j < rlen; j++) {
406                         if (spref[i] == rpref[j]) {
407                                 res = &spref[i];
408                                 break;
409                         }
410                 }
411                 if (res)
412                         break;
413         }
414
415         /* we didn't agree on anything */
416         if (res == NULL) {
417                 /* confirm previous value */
418                 switch (opt->dccpop_feat) {
419                 case DCCPF_CCID:
420                         /* XXX did i get this right? =P */
421                         if (opt->dccpop_type == DCCPO_CHANGE_L)
422                                 res = &dccp_msk(sk)->dccpms_tx_ccid;
423                         else
424                                 res = &dccp_msk(sk)->dccpms_rx_ccid;
425                         break;
426
427                 default:
428                         DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
429                         /* XXX implement res */
430                         return -EFAULT;
431                 }
432
433                 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
434                 agree = 0; /* this is used for mandatory options... */
435         }
436
437         /* need to put result and our preference list */
438         rlen = 1 + opt->dccpop_len;
439         rpref = kmalloc(rlen, GFP_ATOMIC);
440         if (rpref == NULL)
441                 return -ENOMEM;
442
443         *rpref = *res;
444         memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
445
446         /* put it in the "confirm queue" */
447         if (opt->dccpop_sc == NULL) {
448                 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
449                 if (opt->dccpop_sc == NULL) {
450                         kfree(rpref);
451                         return -ENOMEM;
452                 }
453         } else {
454                 /* recycle the confirm slot */
455                 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
456                 kfree(opt->dccpop_sc->dccpoc_val);
457                 dccp_pr_debug("recycling confirm slot\n");
458         }
459         memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
460
461         opt->dccpop_sc->dccpoc_val = rpref;
462         opt->dccpop_sc->dccpoc_len = rlen;
463
464         /* update the option on our side [we are about to send the confirm] */
465         rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
466         if (rc) {
467                 kfree(opt->dccpop_sc->dccpoc_val);
468                 kfree(opt->dccpop_sc);
469                 opt->dccpop_sc = NULL;
470                 return rc;
471         }
472
473         dccp_pr_debug("Will confirm %d\n", *rpref);
474
475         /* say we want to change to X but we just got a confirm X, suppress our
476          * change
477          */
478         if (!opt->dccpop_conf) {
479                 if (*opt->dccpop_val == *res)
480                         opt->dccpop_conf = 1;
481                 dccp_pr_debug("won't ask for change of same feature\n");
482         }
483
484         return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
485 }
486
487 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
488 {
489         struct dccp_minisock *dmsk = dccp_msk(sk);
490         struct dccp_opt_pend *opt;
491         int rc = 1;
492         u8 t;
493
494         /*
495          * We received a CHANGE.  We gotta match it against our own preference
496          * list.  If we got a CHANGE_R it means it's a change for us, so we need
497          * to compare our CHANGE_L list.
498          */
499         if (type == DCCPO_CHANGE_L)
500                 t = DCCPO_CHANGE_R;
501         else
502                 t = DCCPO_CHANGE_L;
503
504         /* find our preference list for this feature */
505         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
506                 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
507                         continue;
508
509                 /* find the winner from the two preference lists */
510                 rc = dccp_feat_reconcile(sk, opt, val, len);
511                 break;
512         }
513
514         /* We didn't deal with the change.  This can happen if we have no
515          * preference list for the feature.  In fact, it just shouldn't
516          * happen---if we understand a feature, we should have a preference list
517          * with at least the default value.
518          */
519         BUG_ON(rc == 1);
520
521         return rc;
522 }
523
524 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
525 {
526         struct dccp_opt_pend *opt;
527         struct dccp_minisock *dmsk = dccp_msk(sk);
528         u8 *copy;
529         int rc;
530
531         /* NN features must be Change L (sec. 6.3.2) */
532         if (type != DCCPO_CHANGE_L) {
533                 dccp_pr_debug("received %s for NN feature %d\n",
534                                 dccp_feat_typename(type), feature);
535                 return -EFAULT;
536         }
537
538         /* XXX sanity check opt val */
539
540         /* copy option so we can confirm it */
541         opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
542         if (opt == NULL)
543                 return -ENOMEM;
544
545         copy = kmemdup(val, len, GFP_ATOMIC);
546         if (copy == NULL) {
547                 kfree(opt);
548                 return -ENOMEM;
549         }
550
551         opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
552         opt->dccpop_feat = feature;
553         opt->dccpop_val  = copy;
554         opt->dccpop_len  = len;
555
556         /* change feature */
557         rc = dccp_feat_update(sk, type, feature, *val);
558         if (rc) {
559                 kfree(opt->dccpop_val);
560                 kfree(opt);
561                 return rc;
562         }
563
564         dccp_feat_debug(type, feature, *copy);
565
566         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
567
568         return 0;
569 }
570
571 static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
572                                     u8 type, u8 feature)
573 {
574         /* XXX check if other confirms for that are queued and recycle slot */
575         struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
576
577         if (opt == NULL) {
578                 /* XXX what do we do?  Ignoring should be fine.  It's a change
579                  * after all =P
580                  */
581                 return;
582         }
583
584         switch (type) {
585         case DCCPO_CHANGE_L:
586                 opt->dccpop_type = DCCPO_CONFIRM_R;
587                 break;
588         case DCCPO_CHANGE_R:
589                 opt->dccpop_type = DCCPO_CONFIRM_L;
590                 break;
591         default:
592                 DCCP_WARN("invalid type %d\n", type);
593                 kfree(opt);
594                 return;
595         }
596         opt->dccpop_feat = feature;
597         opt->dccpop_val  = NULL;
598         opt->dccpop_len  = 0;
599
600         /* change feature */
601         dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
602
603         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
604 }
605
606 static void dccp_feat_flush_confirm(struct sock *sk)
607 {
608         struct dccp_minisock *dmsk = dccp_msk(sk);
609         /* Check if there is anything to confirm in the first place */
610         int yes = !list_empty(&dmsk->dccpms_conf);
611
612         if (!yes) {
613                 struct dccp_opt_pend *opt;
614
615                 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
616                         if (opt->dccpop_conf) {
617                                 yes = 1;
618                                 break;
619                         }
620                 }
621         }
622
623         if (!yes)
624                 return;
625
626         /* OK there is something to confirm... */
627         /* XXX check if packet is in flight?  Send delayed ack?? */
628         if (sk->sk_state == DCCP_OPEN)
629                 dccp_send_ack(sk);
630 }
631
632 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
633 {
634         int rc;
635
636         dccp_feat_debug(type, feature, *val);
637
638         /* figure out if it's SP or NN feature */
639         switch (feature) {
640         /* deal with SP features */
641         case DCCPF_CCID:
642                 rc = dccp_feat_sp(sk, type, feature, val, len);
643                 break;
644
645         /* deal with NN features */
646         case DCCPF_ACK_RATIO:
647                 rc = dccp_feat_nn(sk, type, feature, val, len);
648                 break;
649
650         /* XXX implement other features */
651         default:
652                 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
653                               dccp_feat_typename(type), feature);
654                 rc = -EFAULT;
655                 break;
656         }
657
658         /* check if there were problems changing features */
659         if (rc) {
660                 /* If we don't agree on SP, we sent a confirm for old value.
661                  * However we propagate rc to caller in case option was
662                  * mandatory
663                  */
664                 if (rc != DCCP_FEAT_SP_NOAGREE)
665                         dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
666         }
667
668         /* generate the confirm [if required] */
669         dccp_feat_flush_confirm(sk);
670
671         return rc;
672 }
673
674 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
675
676 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
677                            u8 *val, u8 len)
678 {
679         u8 t;
680         struct dccp_opt_pend *opt;
681         struct dccp_minisock *dmsk = dccp_msk(sk);
682         int found = 0;
683         int all_confirmed = 1;
684
685         dccp_feat_debug(type, feature, *val);
686
687         /* locate our change request */
688         switch (type) {
689         case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
690         case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
691         default:              DCCP_WARN("invalid type %d\n", type);
692                               return 1;
693
694         }
695         /* XXX sanity check feature value */
696
697         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
698                 if (!opt->dccpop_conf && opt->dccpop_type == t &&
699                     opt->dccpop_feat == feature) {
700                         found = 1;
701                         dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
702
703                         /* XXX do sanity check */
704
705                         opt->dccpop_conf = 1;
706
707                         /* We got a confirmation---change the option */
708                         dccp_feat_update(sk, opt->dccpop_type,
709                                          opt->dccpop_feat, *val);
710
711                         /* XXX check the return value of dccp_feat_update */
712                         break;
713                 }
714
715                 if (!opt->dccpop_conf)
716                         all_confirmed = 0;
717         }
718
719         /* fix re-transmit timer */
720         /* XXX gotta make sure that no option negotiation occurs during
721          * connection shutdown.  Consider that the CLOSEREQ is sent and timer is
722          * on.  if all options are confirmed it might kill timer which should
723          * remain alive until close is received.
724          */
725         if (all_confirmed) {
726                 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
727                 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
728         }
729
730         if (!found)
731                 dccp_pr_debug("%s(%d, ...) never requested\n",
732                               dccp_feat_typename(type), feature);
733         return 0;
734 }
735
736 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
737
738 void dccp_feat_clean(struct dccp_minisock *dmsk)
739 {
740         struct dccp_opt_pend *opt, *next;
741
742         list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
743                                  dccpop_node) {
744                 BUG_ON(opt->dccpop_val == NULL);
745                 kfree(opt->dccpop_val);
746
747                 if (opt->dccpop_sc != NULL) {
748                         BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
749                         kfree(opt->dccpop_sc->dccpoc_val);
750                         kfree(opt->dccpop_sc);
751                 }
752
753                 kfree(opt);
754         }
755         INIT_LIST_HEAD(&dmsk->dccpms_pending);
756
757         list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
758                 BUG_ON(opt == NULL);
759                 if (opt->dccpop_val != NULL)
760                         kfree(opt->dccpop_val);
761                 kfree(opt);
762         }
763         INIT_LIST_HEAD(&dmsk->dccpms_conf);
764 }
765
766 EXPORT_SYMBOL_GPL(dccp_feat_clean);
767
768 /* this is to be called only when a listening sock creates its child.  It is
769  * assumed by the function---the confirm is not duplicated, but rather it is
770  * "passed on".
771  */
772 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
773 {
774         struct dccp_minisock *olddmsk = dccp_msk(oldsk);
775         struct dccp_minisock *newdmsk = dccp_msk(newsk);
776         struct dccp_opt_pend *opt;
777         int rc = 0;
778
779         INIT_LIST_HEAD(&newdmsk->dccpms_pending);
780         INIT_LIST_HEAD(&newdmsk->dccpms_conf);
781
782         list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
783                 struct dccp_opt_pend *newopt;
784                 /* copy the value of the option */
785                 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
786
787                 if (val == NULL)
788                         goto out_clean;
789
790                 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
791                 if (newopt == NULL) {
792                         kfree(val);
793                         goto out_clean;
794                 }
795
796                 /* insert the option */
797                 newopt->dccpop_val = val;
798                 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
799
800                 /* XXX what happens with backlogs and multiple connections at
801                  * once...
802                  */
803                 /* the master socket no longer needs to worry about confirms */
804                 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
805
806                 /* reset state for a new socket */
807                 opt->dccpop_conf = 0;
808         }
809
810         /* XXX not doing anything about the conf queue */
811
812 out:
813         return rc;
814
815 out_clean:
816         dccp_feat_clean(newdmsk);
817         rc = -ENOMEM;
818         goto out;
819 }
820
821 EXPORT_SYMBOL_GPL(dccp_feat_clone);
822
823 static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
824                             u8 *val, u8 len)
825 {
826         int rc = -ENOMEM;
827         u8 *copy = kmemdup(val, len, GFP_KERNEL);
828
829         if (copy != NULL) {
830                 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
831                 if (rc)
832                         kfree(copy);
833         }
834         return rc;
835 }
836
837 int dccp_feat_init(struct dccp_minisock *dmsk)
838 {
839         int rc;
840
841         INIT_LIST_HEAD(&dmsk->dccpms_pending);
842         INIT_LIST_HEAD(&dmsk->dccpms_conf);
843
844         /* CCID L */
845         rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
846                               &dmsk->dccpms_tx_ccid, 1);
847         if (rc)
848                 goto out;
849
850         /* CCID R */
851         rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
852                               &dmsk->dccpms_rx_ccid, 1);
853         if (rc)
854                 goto out;
855
856         /* Ack ratio */
857         rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
858                               &dmsk->dccpms_ack_ratio, 1);
859 out:
860         return rc;
861 }
862
863 EXPORT_SYMBOL_GPL(dccp_feat_init);
864
865 #ifdef CONFIG_IP_DCCP_DEBUG
866 const char *dccp_feat_typename(const u8 type)
867 {
868         switch(type) {
869         case DCCPO_CHANGE_L:  return("ChangeL");
870         case DCCPO_CONFIRM_L: return("ConfirmL");
871         case DCCPO_CHANGE_R:  return("ChangeR");
872         case DCCPO_CONFIRM_R: return("ConfirmR");
873         /* the following case must not appear in feature negotation  */
874         default:              dccp_pr_debug("unknown type %d [BUG!]\n", type);
875         }
876         return NULL;
877 }
878
879 EXPORT_SYMBOL_GPL(dccp_feat_typename);
880
881 const char *dccp_feat_name(const u8 feat)
882 {
883         static const char *feature_names[] = {
884                 [DCCPF_RESERVED]        = "Reserved",
885                 [DCCPF_CCID]            = "CCID",
886                 [DCCPF_SHORT_SEQNOS]    = "Allow Short Seqnos",
887                 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
888                 [DCCPF_ECN_INCAPABLE]   = "ECN Incapable",
889                 [DCCPF_ACK_RATIO]       = "Ack Ratio",
890                 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
891                 [DCCPF_SEND_NDP_COUNT]  = "Send NDP Count",
892                 [DCCPF_MIN_CSUM_COVER]  = "Min. Csum Coverage",
893                 [DCCPF_DATA_CHECKSUM]   = "Send Data Checksum",
894         };
895         if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
896                 return feature_names[DCCPF_RESERVED];
897
898         if (feat ==  DCCPF_SEND_LEV_RATE)
899                 return "Send Loss Event Rate";
900         if (feat >= DCCPF_MIN_CCID_SPECIFIC)
901                 return "CCID-specific";
902
903         return feature_names[feat];
904 }
905
906 EXPORT_SYMBOL_GPL(dccp_feat_name);
907 #endif /* CONFIG_IP_DCCP_DEBUG */