]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/dccp/feat.c
faade82856fe36f931d1114eb84ac7a27354b41f
[linux-2.6-omap-h63xx.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 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 /* generate @to as full clone of @from - @to must not contain any nodes */
283 int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
284 {
285         struct dccp_feat_entry *entry, *new;
286
287         INIT_LIST_HEAD(to);
288         list_for_each_entry(entry, from, node) {
289                 new = dccp_feat_clone_entry(entry);
290                 if (new == NULL)
291                         goto cloning_failed;
292                 list_add_tail(&new->node, to);
293         }
294         return 0;
295
296 cloning_failed:
297         dccp_feat_list_purge(to);
298         return -ENOMEM;
299 }
300
301 int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
302                      u8 *val, u8 len, gfp_t gfp)
303 {
304         struct dccp_opt_pend *opt;
305
306         dccp_feat_debug(type, feature, *val);
307
308         if (len > 3) {
309                 DCCP_WARN("invalid length %d\n", len);
310                 return -EINVAL;
311         }
312         /* XXX add further sanity checks */
313
314         /* check if that feature is already being negotiated */
315         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
316                 /* ok we found a negotiation for this option already */
317                 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
318                         dccp_pr_debug("Replacing old\n");
319                         /* replace */
320                         BUG_ON(opt->dccpop_val == NULL);
321                         kfree(opt->dccpop_val);
322                         opt->dccpop_val  = val;
323                         opt->dccpop_len  = len;
324                         opt->dccpop_conf = 0;
325                         return 0;
326                 }
327         }
328
329         /* negotiation for a new feature */
330         opt = kmalloc(sizeof(*opt), gfp);
331         if (opt == NULL)
332                 return -ENOMEM;
333
334         opt->dccpop_type = type;
335         opt->dccpop_feat = feature;
336         opt->dccpop_len  = len;
337         opt->dccpop_val  = val;
338         opt->dccpop_conf = 0;
339         opt->dccpop_sc   = NULL;
340
341         BUG_ON(opt->dccpop_val == NULL);
342
343         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
344         return 0;
345 }
346
347 EXPORT_SYMBOL_GPL(dccp_feat_change);
348
349 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
350 {
351         struct dccp_sock *dp = dccp_sk(sk);
352         struct dccp_minisock *dmsk = dccp_msk(sk);
353         /* figure out if we are changing our CCID or the peer's */
354         const int rx = type == DCCPO_CHANGE_R;
355         const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
356         struct ccid *new_ccid;
357
358         /* Check if nothing is being changed. */
359         if (ccid_nr == new_ccid_nr)
360                 return 0;
361
362         new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
363         if (new_ccid == NULL)
364                 return -ENOMEM;
365
366         if (rx) {
367                 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
368                 dp->dccps_hc_rx_ccid = new_ccid;
369                 dmsk->dccpms_rx_ccid = new_ccid_nr;
370         } else {
371                 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
372                 dp->dccps_hc_tx_ccid = new_ccid;
373                 dmsk->dccpms_tx_ccid = new_ccid_nr;
374         }
375
376         return 0;
377 }
378
379 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
380 {
381         dccp_feat_debug(type, feat, val);
382
383         switch (feat) {
384         case DCCPF_CCID:
385                 return dccp_feat_update_ccid(sk, type, val);
386         default:
387                 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
388                               dccp_feat_typename(type), feat);
389                 break;
390         }
391         return 0;
392 }
393
394 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
395                                u8 *rpref, u8 rlen)
396 {
397         struct dccp_sock *dp = dccp_sk(sk);
398         u8 *spref, slen, *res = NULL;
399         int i, j, rc, agree = 1;
400
401         BUG_ON(rpref == NULL);
402
403         /* check if we are the black sheep */
404         if (dp->dccps_role == DCCP_ROLE_CLIENT) {
405                 spref = rpref;
406                 slen  = rlen;
407                 rpref = opt->dccpop_val;
408                 rlen  = opt->dccpop_len;
409         } else {
410                 spref = opt->dccpop_val;
411                 slen  = opt->dccpop_len;
412         }
413         /*
414          * Now we have server preference list in spref and client preference in
415          * rpref
416          */
417         BUG_ON(spref == NULL);
418         BUG_ON(rpref == NULL);
419
420         /* FIXME sanity check vals */
421
422         /* Are values in any order?  XXX Lame "algorithm" here */
423         for (i = 0; i < slen; i++) {
424                 for (j = 0; j < rlen; j++) {
425                         if (spref[i] == rpref[j]) {
426                                 res = &spref[i];
427                                 break;
428                         }
429                 }
430                 if (res)
431                         break;
432         }
433
434         /* we didn't agree on anything */
435         if (res == NULL) {
436                 /* confirm previous value */
437                 switch (opt->dccpop_feat) {
438                 case DCCPF_CCID:
439                         /* XXX did i get this right? =P */
440                         if (opt->dccpop_type == DCCPO_CHANGE_L)
441                                 res = &dccp_msk(sk)->dccpms_tx_ccid;
442                         else
443                                 res = &dccp_msk(sk)->dccpms_rx_ccid;
444                         break;
445
446                 default:
447                         DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
448                         /* XXX implement res */
449                         return -EFAULT;
450                 }
451
452                 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
453                 agree = 0; /* this is used for mandatory options... */
454         }
455
456         /* need to put result and our preference list */
457         rlen = 1 + opt->dccpop_len;
458         rpref = kmalloc(rlen, GFP_ATOMIC);
459         if (rpref == NULL)
460                 return -ENOMEM;
461
462         *rpref = *res;
463         memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
464
465         /* put it in the "confirm queue" */
466         if (opt->dccpop_sc == NULL) {
467                 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
468                 if (opt->dccpop_sc == NULL) {
469                         kfree(rpref);
470                         return -ENOMEM;
471                 }
472         } else {
473                 /* recycle the confirm slot */
474                 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
475                 kfree(opt->dccpop_sc->dccpoc_val);
476                 dccp_pr_debug("recycling confirm slot\n");
477         }
478         memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
479
480         opt->dccpop_sc->dccpoc_val = rpref;
481         opt->dccpop_sc->dccpoc_len = rlen;
482
483         /* update the option on our side [we are about to send the confirm] */
484         rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
485         if (rc) {
486                 kfree(opt->dccpop_sc->dccpoc_val);
487                 kfree(opt->dccpop_sc);
488                 opt->dccpop_sc = NULL;
489                 return rc;
490         }
491
492         dccp_pr_debug("Will confirm %d\n", *rpref);
493
494         /* say we want to change to X but we just got a confirm X, suppress our
495          * change
496          */
497         if (!opt->dccpop_conf) {
498                 if (*opt->dccpop_val == *res)
499                         opt->dccpop_conf = 1;
500                 dccp_pr_debug("won't ask for change of same feature\n");
501         }
502
503         return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
504 }
505
506 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
507 {
508         struct dccp_minisock *dmsk = dccp_msk(sk);
509         struct dccp_opt_pend *opt;
510         int rc = 1;
511         u8 t;
512
513         /*
514          * We received a CHANGE.  We gotta match it against our own preference
515          * list.  If we got a CHANGE_R it means it's a change for us, so we need
516          * to compare our CHANGE_L list.
517          */
518         if (type == DCCPO_CHANGE_L)
519                 t = DCCPO_CHANGE_R;
520         else
521                 t = DCCPO_CHANGE_L;
522
523         /* find our preference list for this feature */
524         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
525                 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
526                         continue;
527
528                 /* find the winner from the two preference lists */
529                 rc = dccp_feat_reconcile(sk, opt, val, len);
530                 break;
531         }
532
533         /* We didn't deal with the change.  This can happen if we have no
534          * preference list for the feature.  In fact, it just shouldn't
535          * happen---if we understand a feature, we should have a preference list
536          * with at least the default value.
537          */
538         BUG_ON(rc == 1);
539
540         return rc;
541 }
542
543 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
544 {
545         struct dccp_opt_pend *opt;
546         struct dccp_minisock *dmsk = dccp_msk(sk);
547         u8 *copy;
548         int rc;
549
550         /* NN features must be Change L (sec. 6.3.2) */
551         if (type != DCCPO_CHANGE_L) {
552                 dccp_pr_debug("received %s for NN feature %d\n",
553                                 dccp_feat_typename(type), feature);
554                 return -EFAULT;
555         }
556
557         /* XXX sanity check opt val */
558
559         /* copy option so we can confirm it */
560         opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
561         if (opt == NULL)
562                 return -ENOMEM;
563
564         copy = kmemdup(val, len, GFP_ATOMIC);
565         if (copy == NULL) {
566                 kfree(opt);
567                 return -ENOMEM;
568         }
569
570         opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
571         opt->dccpop_feat = feature;
572         opt->dccpop_val  = copy;
573         opt->dccpop_len  = len;
574
575         /* change feature */
576         rc = dccp_feat_update(sk, type, feature, *val);
577         if (rc) {
578                 kfree(opt->dccpop_val);
579                 kfree(opt);
580                 return rc;
581         }
582
583         dccp_feat_debug(type, feature, *copy);
584
585         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
586
587         return 0;
588 }
589
590 static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
591                                     u8 type, u8 feature)
592 {
593         /* XXX check if other confirms for that are queued and recycle slot */
594         struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
595
596         if (opt == NULL) {
597                 /* XXX what do we do?  Ignoring should be fine.  It's a change
598                  * after all =P
599                  */
600                 return;
601         }
602
603         switch (type) {
604         case DCCPO_CHANGE_L:
605                 opt->dccpop_type = DCCPO_CONFIRM_R;
606                 break;
607         case DCCPO_CHANGE_R:
608                 opt->dccpop_type = DCCPO_CONFIRM_L;
609                 break;
610         default:
611                 DCCP_WARN("invalid type %d\n", type);
612                 kfree(opt);
613                 return;
614         }
615         opt->dccpop_feat = feature;
616         opt->dccpop_val  = NULL;
617         opt->dccpop_len  = 0;
618
619         /* change feature */
620         dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
621
622         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
623 }
624
625 static void dccp_feat_flush_confirm(struct sock *sk)
626 {
627         struct dccp_minisock *dmsk = dccp_msk(sk);
628         /* Check if there is anything to confirm in the first place */
629         int yes = !list_empty(&dmsk->dccpms_conf);
630
631         if (!yes) {
632                 struct dccp_opt_pend *opt;
633
634                 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
635                         if (opt->dccpop_conf) {
636                                 yes = 1;
637                                 break;
638                         }
639                 }
640         }
641
642         if (!yes)
643                 return;
644
645         /* OK there is something to confirm... */
646         /* XXX check if packet is in flight?  Send delayed ack?? */
647         if (sk->sk_state == DCCP_OPEN)
648                 dccp_send_ack(sk);
649 }
650
651 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
652 {
653         int rc;
654
655         dccp_feat_debug(type, feature, *val);
656
657         /* figure out if it's SP or NN feature */
658         switch (feature) {
659         /* deal with SP features */
660         case DCCPF_CCID:
661                 rc = dccp_feat_sp(sk, type, feature, val, len);
662                 break;
663
664         /* deal with NN features */
665         case DCCPF_ACK_RATIO:
666                 rc = dccp_feat_nn(sk, type, feature, val, len);
667                 break;
668
669         /* XXX implement other features */
670         default:
671                 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
672                               dccp_feat_typename(type), feature);
673                 rc = -EFAULT;
674                 break;
675         }
676
677         /* check if there were problems changing features */
678         if (rc) {
679                 /* If we don't agree on SP, we sent a confirm for old value.
680                  * However we propagate rc to caller in case option was
681                  * mandatory
682                  */
683                 if (rc != DCCP_FEAT_SP_NOAGREE)
684                         dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
685         }
686
687         /* generate the confirm [if required] */
688         dccp_feat_flush_confirm(sk);
689
690         return rc;
691 }
692
693 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
694
695 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
696                            u8 *val, u8 len)
697 {
698         u8 t;
699         struct dccp_opt_pend *opt;
700         struct dccp_minisock *dmsk = dccp_msk(sk);
701         int found = 0;
702         int all_confirmed = 1;
703
704         dccp_feat_debug(type, feature, *val);
705
706         /* locate our change request */
707         switch (type) {
708         case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
709         case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
710         default:              DCCP_WARN("invalid type %d\n", type);
711                               return 1;
712
713         }
714         /* XXX sanity check feature value */
715
716         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
717                 if (!opt->dccpop_conf && opt->dccpop_type == t &&
718                     opt->dccpop_feat == feature) {
719                         found = 1;
720                         dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
721
722                         /* XXX do sanity check */
723
724                         opt->dccpop_conf = 1;
725
726                         /* We got a confirmation---change the option */
727                         dccp_feat_update(sk, opt->dccpop_type,
728                                          opt->dccpop_feat, *val);
729
730                         /* XXX check the return value of dccp_feat_update */
731                         break;
732                 }
733
734                 if (!opt->dccpop_conf)
735                         all_confirmed = 0;
736         }
737
738         /* fix re-transmit timer */
739         /* XXX gotta make sure that no option negotiation occurs during
740          * connection shutdown.  Consider that the CLOSEREQ is sent and timer is
741          * on.  if all options are confirmed it might kill timer which should
742          * remain alive until close is received.
743          */
744         if (all_confirmed) {
745                 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
746                 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
747         }
748
749         if (!found)
750                 dccp_pr_debug("%s(%d, ...) never requested\n",
751                               dccp_feat_typename(type), feature);
752         return 0;
753 }
754
755 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
756
757 void dccp_feat_clean(struct dccp_minisock *dmsk)
758 {
759         struct dccp_opt_pend *opt, *next;
760
761         list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
762                                  dccpop_node) {
763                 BUG_ON(opt->dccpop_val == NULL);
764                 kfree(opt->dccpop_val);
765
766                 if (opt->dccpop_sc != NULL) {
767                         BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
768                         kfree(opt->dccpop_sc->dccpoc_val);
769                         kfree(opt->dccpop_sc);
770                 }
771
772                 kfree(opt);
773         }
774         INIT_LIST_HEAD(&dmsk->dccpms_pending);
775
776         list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
777                 BUG_ON(opt == NULL);
778                 if (opt->dccpop_val != NULL)
779                         kfree(opt->dccpop_val);
780                 kfree(opt);
781         }
782         INIT_LIST_HEAD(&dmsk->dccpms_conf);
783 }
784
785 EXPORT_SYMBOL_GPL(dccp_feat_clean);
786
787 /* this is to be called only when a listening sock creates its child.  It is
788  * assumed by the function---the confirm is not duplicated, but rather it is
789  * "passed on".
790  */
791 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
792 {
793         struct dccp_minisock *olddmsk = dccp_msk(oldsk);
794         struct dccp_minisock *newdmsk = dccp_msk(newsk);
795         struct dccp_opt_pend *opt;
796         int rc = 0;
797
798         INIT_LIST_HEAD(&newdmsk->dccpms_pending);
799         INIT_LIST_HEAD(&newdmsk->dccpms_conf);
800
801         list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
802                 struct dccp_opt_pend *newopt;
803                 /* copy the value of the option */
804                 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
805
806                 if (val == NULL)
807                         goto out_clean;
808
809                 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
810                 if (newopt == NULL) {
811                         kfree(val);
812                         goto out_clean;
813                 }
814
815                 /* insert the option */
816                 newopt->dccpop_val = val;
817                 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
818
819                 /* XXX what happens with backlogs and multiple connections at
820                  * once...
821                  */
822                 /* the master socket no longer needs to worry about confirms */
823                 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
824
825                 /* reset state for a new socket */
826                 opt->dccpop_conf = 0;
827         }
828
829         /* XXX not doing anything about the conf queue */
830
831 out:
832         return rc;
833
834 out_clean:
835         dccp_feat_clean(newdmsk);
836         rc = -ENOMEM;
837         goto out;
838 }
839
840 EXPORT_SYMBOL_GPL(dccp_feat_clone);
841
842 static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
843                             u8 *val, u8 len)
844 {
845         int rc = -ENOMEM;
846         u8 *copy = kmemdup(val, len, GFP_KERNEL);
847
848         if (copy != NULL) {
849                 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
850                 if (rc)
851                         kfree(copy);
852         }
853         return rc;
854 }
855
856 int dccp_feat_init(struct dccp_minisock *dmsk)
857 {
858         int rc;
859
860         INIT_LIST_HEAD(&dmsk->dccpms_pending);
861         INIT_LIST_HEAD(&dmsk->dccpms_conf);
862
863         /* CCID L */
864         rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
865                               &dmsk->dccpms_tx_ccid, 1);
866         if (rc)
867                 goto out;
868
869         /* CCID R */
870         rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
871                               &dmsk->dccpms_rx_ccid, 1);
872         if (rc)
873                 goto out;
874
875         /* Ack ratio */
876         rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
877                               &dmsk->dccpms_ack_ratio, 1);
878 out:
879         return rc;
880 }
881
882 EXPORT_SYMBOL_GPL(dccp_feat_init);
883
884 #ifdef CONFIG_IP_DCCP_DEBUG
885 const char *dccp_feat_typename(const u8 type)
886 {
887         switch(type) {
888         case DCCPO_CHANGE_L:  return("ChangeL");
889         case DCCPO_CONFIRM_L: return("ConfirmL");
890         case DCCPO_CHANGE_R:  return("ChangeR");
891         case DCCPO_CONFIRM_R: return("ConfirmR");
892         /* the following case must not appear in feature negotation  */
893         default:              dccp_pr_debug("unknown type %d [BUG!]\n", type);
894         }
895         return NULL;
896 }
897
898 EXPORT_SYMBOL_GPL(dccp_feat_typename);
899
900 const char *dccp_feat_name(const u8 feat)
901 {
902         static const char *feature_names[] = {
903                 [DCCPF_RESERVED]        = "Reserved",
904                 [DCCPF_CCID]            = "CCID",
905                 [DCCPF_SHORT_SEQNOS]    = "Allow Short Seqnos",
906                 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
907                 [DCCPF_ECN_INCAPABLE]   = "ECN Incapable",
908                 [DCCPF_ACK_RATIO]       = "Ack Ratio",
909                 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
910                 [DCCPF_SEND_NDP_COUNT]  = "Send NDP Count",
911                 [DCCPF_MIN_CSUM_COVER]  = "Min. Csum Coverage",
912                 [DCCPF_DATA_CHECKSUM]   = "Send Data Checksum",
913         };
914         if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
915                 return feature_names[DCCPF_RESERVED];
916
917         if (feat ==  DCCPF_SEND_LEV_RATE)
918                 return "Send Loss Event Rate";
919         if (feat >= DCCPF_MIN_CCID_SPECIFIC)
920                 return "CCID-specific";
921
922         return feature_names[feat];
923 }
924
925 EXPORT_SYMBOL_GPL(dccp_feat_name);
926 #endif /* CONFIG_IP_DCCP_DEBUG */