]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/dccp/feat.c
dccp: Clean up old feature-negotiation infrastructure
[net-next-2.6.git] / net / dccp / feat.c
1 /*
2  *  net/dccp/feat.c
3  *
4  *  Feature negotiation for the DCCP protocol (RFC 4340, section 6)
5  *
6  *  Copyright (c) 2008 The University of Aberdeen, Scotland, UK
7  *  Copyright (c) 2008 Gerrit Renker <gerrit@erg.abdn.ac.uk>
8  *  Rewrote from scratch, some bits from earlier code by
9  *  Copyright (c) 2005 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
10  *
11  *
12  *  ASSUMPTIONS
13  *  -----------
14  *  o Feature negotiation is coordinated with connection setup (as in TCP), wild
15  *    changes of parameters of an established connection are not supported.
16  *  o All currently known SP features have 1-byte quantities. If in the future
17  *    extensions of RFCs 4340..42 define features with item lengths larger than
18  *    one byte, a feature-specific extension of the code will be required.
19  *
20  *  This program is free software; you can redistribute it and/or
21  *  modify it under the terms of the GNU General Public License
22  *  as published by the Free Software Foundation; either version
23  *  2 of the License, or (at your option) any later version.
24  */
25 #include <linux/module.h>
26 #include "ccid.h"
27 #include "feat.h"
28
29 /*
30  * Feature activation handlers.
31  *
32  * These all use an u64 argument, to provide enough room for NN/SP features. At
33  * this stage the negotiated values have been checked to be within their range.
34  */
35 static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
36 {
37         struct dccp_sock *dp = dccp_sk(sk);
38         struct ccid *new_ccid = ccid_new(ccid, sk, rx, gfp_any());
39
40         if (new_ccid == NULL)
41                 return -ENOMEM;
42
43         if (rx) {
44                 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
45                 dp->dccps_hc_rx_ccid = new_ccid;
46         } else {
47                 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
48                 dp->dccps_hc_tx_ccid = new_ccid;
49         }
50         return 0;
51 }
52
53 static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
54 {
55         if (!rx)
56                 dccp_msk(sk)->dccpms_sequence_window = seq_win;
57         return 0;
58 }
59
60 static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
61 {
62         if (rx)
63                 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
64         else
65                 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
66         return 0;
67 }
68
69 static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
70 {
71         struct dccp_sock *dp = dccp_sk(sk);
72
73         if (rx) {
74                 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
75                         dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
76                         if (dp->dccps_hc_rx_ackvec == NULL)
77                                 return -ENOMEM;
78                 } else if (!enable) {
79                         dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
80                         dp->dccps_hc_rx_ackvec = NULL;
81                 }
82         }
83         return 0;
84 }
85
86 static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
87 {
88         if (!rx)
89                 dccp_msk(sk)->dccpms_send_ndp_count = (enable > 0);
90         return 0;
91 }
92
93 /*
94  * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
95  * `rx' holds when the sending peer informs about his partial coverage via a
96  * ChangeR() option. In the other case, we are the sender and the receiver
97  * announces its coverage via ChangeL() options. The policy here is to honour
98  * such communication by enabling the corresponding partial coverage - but only
99  * if it has not been set manually before; the warning here means that all
100  * packets will be dropped.
101  */
102 static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
103 {
104         struct dccp_sock *dp = dccp_sk(sk);
105
106         if (rx)
107                 dp->dccps_pcrlen = cscov;
108         else {
109                 if (dp->dccps_pcslen == 0)
110                         dp->dccps_pcslen = cscov;
111                 else if (cscov > dp->dccps_pcslen)
112                         DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
113                                   dp->dccps_pcslen, (u8)cscov);
114         }
115         return 0;
116 }
117
118 static const struct {
119         u8                      feat_num;               /* DCCPF_xxx */
120         enum dccp_feat_type     rxtx;                   /* RX or TX  */
121         enum dccp_feat_type     reconciliation;         /* SP or NN  */
122         u8                      default_value;          /* as in 6.4 */
123         int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
124 /*
125  *    Lookup table for location and type of features (from RFC 4340/4342)
126  *  +--------------------------+----+-----+----+----+---------+-----------+
127  *  | Feature                  | Location | Reconc. | Initial |  Section  |
128  *  |                          | RX | TX  | SP | NN |  Value  | Reference |
129  *  +--------------------------+----+-----+----+----+---------+-----------+
130  *  | DCCPF_CCID               |    |  X  | X  |    |   2     | 10        |
131  *  | DCCPF_SHORT_SEQNOS       |    |  X  | X  |    |   0     |  7.6.1    |
132  *  | DCCPF_SEQUENCE_WINDOW    |    |  X  |    | X  | 100     |  7.5.2    |
133  *  | DCCPF_ECN_INCAPABLE      | X  |     | X  |    |   0     | 12.1      |
134  *  | DCCPF_ACK_RATIO          |    |  X  |    | X  |   2     | 11.3      |
135  *  | DCCPF_SEND_ACK_VECTOR    | X  |     | X  |    |   0     | 11.5      |
136  *  | DCCPF_SEND_NDP_COUNT     |    |  X  | X  |    |   0     |  7.7.2    |
137  *  | DCCPF_MIN_CSUM_COVER     | X  |     | X  |    |   0     |  9.2.1    |
138  *  | DCCPF_DATA_CHECKSUM      | X  |     | X  |    |   0     |  9.3.1    |
139  *  | DCCPF_SEND_LEV_RATE      | X  |     | X  |    |   0     | 4342/8.4  |
140  *  +--------------------------+----+-----+----+----+---------+-----------+
141  */
142 } dccp_feat_table[] = {
143         { DCCPF_CCID,            FEAT_AT_TX, FEAT_SP, 2,   dccp_hdlr_ccid     },
144         { DCCPF_SHORT_SEQNOS,    FEAT_AT_TX, FEAT_SP, 0,   NULL },
145         { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win  },
146         { DCCPF_ECN_INCAPABLE,   FEAT_AT_RX, FEAT_SP, 0,   NULL },
147         { DCCPF_ACK_RATIO,       FEAT_AT_TX, FEAT_NN, 2,   dccp_hdlr_ack_ratio},
148         { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0,   dccp_hdlr_ackvec   },
149         { DCCPF_SEND_NDP_COUNT,  FEAT_AT_TX, FEAT_SP, 0,   dccp_hdlr_ndp      },
150         { DCCPF_MIN_CSUM_COVER,  FEAT_AT_RX, FEAT_SP, 0,   dccp_hdlr_min_cscov},
151         { DCCPF_DATA_CHECKSUM,   FEAT_AT_RX, FEAT_SP, 0,   NULL },
152         { DCCPF_SEND_LEV_RATE,   FEAT_AT_RX, FEAT_SP, 0,   NULL },
153 };
154 #define DCCP_FEAT_SUPPORTED_MAX         ARRAY_SIZE(dccp_feat_table)
155
156 /**
157  * dccp_feat_index  -  Hash function to map feature number into array position
158  * Returns consecutive array index or -1 if the feature is not understood.
159  */
160 static int dccp_feat_index(u8 feat_num)
161 {
162         /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
163         if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
164                 return feat_num - 1;
165
166         /*
167          * Other features: add cases for new feature types here after adding
168          * them to the above table.
169          */
170         switch (feat_num) {
171         case DCCPF_SEND_LEV_RATE:
172                         return DCCP_FEAT_SUPPORTED_MAX - 1;
173         }
174         return -1;
175 }
176
177 static u8 dccp_feat_type(u8 feat_num)
178 {
179         int idx = dccp_feat_index(feat_num);
180
181         if (idx < 0)
182                 return FEAT_UNKNOWN;
183         return dccp_feat_table[idx].reconciliation;
184 }
185
186 static int dccp_feat_default_value(u8 feat_num)
187 {
188         int idx = dccp_feat_index(feat_num);
189
190         return idx < 0 ? : dccp_feat_table[idx].default_value;
191 }
192
193 static int __dccp_feat_activate(struct sock *sk, const int idx,
194                                 const bool is_local, dccp_feat_val const *fval)
195 {
196         bool rx;
197         u64 val;
198
199         if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
200                 return -1;
201         if (dccp_feat_table[idx].activation_hdlr == NULL)
202                 return 0;
203
204         if (fval == NULL) {
205                 val = dccp_feat_table[idx].default_value;
206         } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
207                 if (fval->sp.vec == NULL) {
208                         /*
209                          * This can happen when an empty Confirm is sent
210                          * for an SP (i.e. known) feature. In this case
211                          * we would be using the default anyway.
212                          */
213                         DCCP_CRIT("Feature #%d undefined: using default", idx);
214                         val = dccp_feat_table[idx].default_value;
215                 } else {
216                         val = fval->sp.vec[0];
217                 }
218         } else {
219                 val = fval->nn;
220         }
221
222         /* Location is RX if this is a local-RX or remote-TX feature */
223         rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
224
225         return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
226 }
227
228 /* Test for "Req'd" feature (RFC 4340, 6.4) */
229 static inline int dccp_feat_must_be_understood(u8 feat_num)
230 {
231         return  feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
232                 feat_num == DCCPF_SEQUENCE_WINDOW;
233 }
234
235 /* copy constructor, fval must not already contain allocated memory */
236 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
237 {
238         fval->sp.len = len;
239         if (fval->sp.len > 0) {
240                 fval->sp.vec = kmemdup(val, len, gfp_any());
241                 if (fval->sp.vec == NULL) {
242                         fval->sp.len = 0;
243                         return -ENOBUFS;
244                 }
245         }
246         return 0;
247 }
248
249 static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
250 {
251         if (unlikely(val == NULL))
252                 return;
253         if (dccp_feat_type(feat_num) == FEAT_SP)
254                 kfree(val->sp.vec);
255         memset(val, 0, sizeof(*val));
256 }
257
258 static struct dccp_feat_entry *
259               dccp_feat_clone_entry(struct dccp_feat_entry const *original)
260 {
261         struct dccp_feat_entry *new;
262         u8 type = dccp_feat_type(original->feat_num);
263
264         if (type == FEAT_UNKNOWN)
265                 return NULL;
266
267         new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
268         if (new == NULL)
269                 return NULL;
270
271         if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
272                                                       original->val.sp.vec,
273                                                       original->val.sp.len)) {
274                 kfree(new);
275                 return NULL;
276         }
277         return new;
278 }
279
280 static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
281 {
282         if (entry != NULL) {
283                 dccp_feat_val_destructor(entry->feat_num, &entry->val);
284                 kfree(entry);
285         }
286 }
287
288 /*
289  * List management functions
290  *
291  * Feature negotiation lists rely on and maintain the following invariants:
292  * - each feat_num in the list is known, i.e. we know its type and default value
293  * - each feat_num/is_local combination is unique (old entries are overwritten)
294  * - SP values are always freshly allocated
295  * - list is sorted in increasing order of feature number (faster lookup)
296  */
297 static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
298                                                      u8 feat_num, bool is_local)
299 {
300         struct dccp_feat_entry *entry;
301
302         list_for_each_entry(entry, fn_list, node)
303                 if (entry->feat_num == feat_num && entry->is_local == is_local)
304                         return entry;
305                 else if (entry->feat_num > feat_num)
306                         break;
307         return NULL;
308 }
309
310 /**
311  * dccp_feat_entry_new  -  Central list update routine (called by all others)
312  * @head:  list to add to
313  * @feat:  feature number
314  * @local: whether the local (1) or remote feature with number @feat is meant
315  * This is the only constructor and serves to ensure the above invariants.
316  */
317 static struct dccp_feat_entry *
318               dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
319 {
320         struct dccp_feat_entry *entry;
321
322         list_for_each_entry(entry, head, node)
323                 if (entry->feat_num == feat && entry->is_local == local) {
324                         dccp_feat_val_destructor(entry->feat_num, &entry->val);
325                         return entry;
326                 } else if (entry->feat_num > feat) {
327                         head = &entry->node;
328                         break;
329                 }
330
331         entry = kmalloc(sizeof(*entry), gfp_any());
332         if (entry != NULL) {
333                 entry->feat_num = feat;
334                 entry->is_local = local;
335                 list_add_tail(&entry->node, head);
336         }
337         return entry;
338 }
339
340 /**
341  * dccp_feat_push_change  -  Add/overwrite a Change option in the list
342  * @fn_list: feature-negotiation list to update
343  * @feat: one of %dccp_feature_numbers
344  * @local: whether local (1) or remote (0) @feat_num is meant
345  * @needs_mandatory: whether to use Mandatory feature negotiation options
346  * @fval: pointer to NN/SP value to be inserted (will be copied)
347  */
348 static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
349                                  u8 mandatory, dccp_feat_val *fval)
350 {
351         struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
352
353         if (new == NULL)
354                 return -ENOMEM;
355
356         new->feat_num        = feat;
357         new->is_local        = local;
358         new->state           = FEAT_INITIALISING;
359         new->needs_confirm   = 0;
360         new->empty_confirm   = 0;
361         new->val             = *fval;
362         new->needs_mandatory = mandatory;
363
364         return 0;
365 }
366
367 /**
368  * dccp_feat_push_confirm  -  Add a Confirm entry to the FN list
369  * @fn_list: feature-negotiation list to add to
370  * @feat: one of %dccp_feature_numbers
371  * @local: whether local (1) or remote (0) @feat_num is being confirmed
372  * @fval: pointer to NN/SP value to be inserted or NULL
373  * Returns 0 on success, a Reset code for further processing otherwise.
374  */
375 static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
376                                   dccp_feat_val *fval)
377 {
378         struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
379
380         if (new == NULL)
381                 return DCCP_RESET_CODE_TOO_BUSY;
382
383         new->feat_num        = feat;
384         new->is_local        = local;
385         new->state           = FEAT_STABLE;     /* transition in 6.6.2 */
386         new->needs_confirm   = 1;
387         new->empty_confirm   = (fval == NULL);
388         new->val.nn          = 0;               /* zeroes the whole structure */
389         if (!new->empty_confirm)
390                 new->val     = *fval;
391         new->needs_mandatory = 0;
392
393         return 0;
394 }
395
396 static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
397 {
398         return dccp_feat_push_confirm(fn_list, feat, local, NULL);
399 }
400
401 static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
402 {
403         list_del(&entry->node);
404         dccp_feat_entry_destructor(entry);
405 }
406
407 void dccp_feat_list_purge(struct list_head *fn_list)
408 {
409         struct dccp_feat_entry *entry, *next;
410
411         list_for_each_entry_safe(entry, next, fn_list, node)
412                 dccp_feat_entry_destructor(entry);
413         INIT_LIST_HEAD(fn_list);
414 }
415 EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
416
417 /* generate @to as full clone of @from - @to must not contain any nodes */
418 int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
419 {
420         struct dccp_feat_entry *entry, *new;
421
422         INIT_LIST_HEAD(to);
423         list_for_each_entry(entry, from, node) {
424                 new = dccp_feat_clone_entry(entry);
425                 if (new == NULL)
426                         goto cloning_failed;
427                 list_add_tail(&new->node, to);
428         }
429         return 0;
430
431 cloning_failed:
432         dccp_feat_list_purge(to);
433         return -ENOMEM;
434 }
435
436 /**
437  * dccp_feat_valid_nn_length  -  Enforce length constraints on NN options
438  * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
439  * incoming options are accepted as long as their values are valid.
440  */
441 static u8 dccp_feat_valid_nn_length(u8 feat_num)
442 {
443         if (feat_num == DCCPF_ACK_RATIO)        /* RFC 4340, 11.3 and 6.6.8 */
444                 return 2;
445         if (feat_num == DCCPF_SEQUENCE_WINDOW)  /* RFC 4340, 7.5.2 and 6.5  */
446                 return 6;
447         return 0;
448 }
449
450 static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
451 {
452         switch (feat_num) {
453         case DCCPF_ACK_RATIO:
454                 return val <= DCCPF_ACK_RATIO_MAX;
455         case DCCPF_SEQUENCE_WINDOW:
456                 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
457         }
458         return 0;       /* feature unknown - so we can't tell */
459 }
460
461 /* check that SP values are within the ranges defined in RFC 4340 */
462 static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
463 {
464         switch (feat_num) {
465         case DCCPF_CCID:
466                 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
467         /* Type-check Boolean feature values: */
468         case DCCPF_SHORT_SEQNOS:
469         case DCCPF_ECN_INCAPABLE:
470         case DCCPF_SEND_ACK_VECTOR:
471         case DCCPF_SEND_NDP_COUNT:
472         case DCCPF_DATA_CHECKSUM:
473         case DCCPF_SEND_LEV_RATE:
474                 return val < 2;
475         case DCCPF_MIN_CSUM_COVER:
476                 return val < 16;
477         }
478         return 0;                       /* feature unknown */
479 }
480
481 static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
482 {
483         if (sp_list == NULL || sp_len < 1)
484                 return 0;
485         while (sp_len--)
486                 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
487                         return 0;
488         return 1;
489 }
490
491 /**
492  * dccp_feat_insert_opts  -  Generate FN options from current list state
493  * @skb: next sk_buff to be sent to the peer
494  * @dp: for client during handshake and general negotiation
495  * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
496  */
497 int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
498                           struct sk_buff *skb)
499 {
500         struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
501         struct dccp_feat_entry *pos, *next;
502         u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
503         bool rpt;
504
505         /* put entries into @skb in the order they appear in the list */
506         list_for_each_entry_safe_reverse(pos, next, fn, node) {
507                 opt  = dccp_feat_genopt(pos);
508                 type = dccp_feat_type(pos->feat_num);
509                 rpt  = false;
510
511                 if (pos->empty_confirm) {
512                         len = 0;
513                         ptr = NULL;
514                 } else {
515                         if (type == FEAT_SP) {
516                                 len = pos->val.sp.len;
517                                 ptr = pos->val.sp.vec;
518                                 rpt = pos->needs_confirm;
519                         } else if (type == FEAT_NN) {
520                                 len = dccp_feat_valid_nn_length(pos->feat_num);
521                                 ptr = nn_in_nbo;
522                                 dccp_encode_value_var(pos->val.nn, ptr, len);
523                         } else {
524                                 DCCP_BUG("unknown feature %u", pos->feat_num);
525                                 return -1;
526                         }
527                 }
528
529                 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
530                         return -1;
531                 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
532                         return -1;
533                 /*
534                  * Enter CHANGING after transmitting the Change option (6.6.2).
535                  */
536                 if (pos->state == FEAT_INITIALISING)
537                         pos->state = FEAT_CHANGING;
538         }
539         return 0;
540 }
541
542 /**
543  * __feat_register_nn  -  Register new NN value on socket
544  * @fn: feature-negotiation list to register with
545  * @feat: an NN feature from %dccp_feature_numbers
546  * @mandatory: use Mandatory option if 1
547  * @nn_val: value to register (restricted to 4 bytes)
548  * Note that NN features are local by definition (RFC 4340, 6.3.2).
549  */
550 static int __feat_register_nn(struct list_head *fn, u8 feat,
551                               u8 mandatory, u64 nn_val)
552 {
553         dccp_feat_val fval = { .nn = nn_val };
554
555         if (dccp_feat_type(feat) != FEAT_NN ||
556             !dccp_feat_is_valid_nn_val(feat, nn_val))
557                 return -EINVAL;
558
559         /* Don't bother with default values, they will be activated anyway. */
560         if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
561                 return 0;
562
563         return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
564 }
565
566 /**
567  * __feat_register_sp  -  Register new SP value/list on socket
568  * @fn: feature-negotiation list to register with
569  * @feat: an SP feature from %dccp_feature_numbers
570  * @is_local: whether the local (1) or the remote (0) @feat is meant
571  * @mandatory: use Mandatory option if 1
572  * @sp_val: SP value followed by optional preference list
573  * @sp_len: length of @sp_val in bytes
574  */
575 static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
576                               u8 mandatory, u8 const *sp_val, u8 sp_len)
577 {
578         dccp_feat_val fval;
579
580         if (dccp_feat_type(feat) != FEAT_SP ||
581             !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
582                 return -EINVAL;
583
584         /* Avoid negotiating alien CCIDs by only advertising supported ones */
585         if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
586                 return -EOPNOTSUPP;
587
588         if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
589                 return -ENOMEM;
590
591         return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
592 }
593
594 /**
595  * dccp_feat_register_sp  -  Register requests to change SP feature values
596  * @sk: client or listening socket
597  * @feat: one of %dccp_feature_numbers
598  * @is_local: whether the local (1) or remote (0) @feat is meant
599  * @list: array of preferred values, in descending order of preference
600  * @len: length of @list in bytes
601  */
602 int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
603                           u8 const *list, u8 len)
604 {        /* any changes must be registered before establishing the connection */
605         if (sk->sk_state != DCCP_CLOSED)
606                 return -EISCONN;
607         if (dccp_feat_type(feat) != FEAT_SP)
608                 return -EINVAL;
609         return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
610                                   0, list, len);
611 }
612
613 /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
614 int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
615 {
616         /* any changes must be registered before establishing the connection */
617         if (sk->sk_state != DCCP_CLOSED)
618                 return -EISCONN;
619         if (dccp_feat_type(feat) != FEAT_NN)
620                 return -EINVAL;
621         return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
622 }
623
624 /*
625  *      Tracking features whose value depend on the choice of CCID
626  *
627  * This is designed with an extension in mind so that a list walk could be done
628  * before activating any features. However, the existing framework was found to
629  * work satisfactorily up until now, the automatic verification is left open.
630  * When adding new CCIDs, add a corresponding dependency table here.
631  */
632 static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
633 {
634         static const struct ccid_dependency ccid2_dependencies[2][2] = {
635                 /*
636                  * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
637                  * feature and Send Ack Vector is an RX feature, `is_local'
638                  * needs to be reversed.
639                  */
640                 {       /* Dependencies of the receiver-side (remote) CCID2 */
641                         {
642                                 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
643                                 .is_local       = true,
644                                 .is_mandatory   = true,
645                                 .val            = 1
646                         },
647                         { 0, 0, 0, 0 }
648                 },
649                 {       /* Dependencies of the sender-side (local) CCID2 */
650                         {
651                                 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
652                                 .is_local       = false,
653                                 .is_mandatory   = true,
654                                 .val            = 1
655                         },
656                         { 0, 0, 0, 0 }
657                 }
658         };
659         static const struct ccid_dependency ccid3_dependencies[2][5] = {
660                 {       /*
661                          * Dependencies of the receiver-side CCID3
662                          */
663                         {       /* locally disable Ack Vectors */
664                                 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
665                                 .is_local       = true,
666                                 .is_mandatory   = false,
667                                 .val            = 0
668                         },
669                         {       /* see below why Send Loss Event Rate is on */
670                                 .dependent_feat = DCCPF_SEND_LEV_RATE,
671                                 .is_local       = true,
672                                 .is_mandatory   = true,
673                                 .val            = 1
674                         },
675                         {       /* NDP Count is needed as per RFC 4342, 6.1.1 */
676                                 .dependent_feat = DCCPF_SEND_NDP_COUNT,
677                                 .is_local       = false,
678                                 .is_mandatory   = true,
679                                 .val            = 1
680                         },
681                         { 0, 0, 0, 0 },
682                 },
683                 {       /*
684                          * CCID3 at the TX side: we request that the HC-receiver
685                          * will not send Ack Vectors (they will be ignored, so
686                          * Mandatory is not set); we enable Send Loss Event Rate
687                          * (Mandatory since the implementation does not support
688                          * the Loss Intervals option of RFC 4342, 8.6).
689                          * The last two options are for peer's information only.
690                         */
691                         {
692                                 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
693                                 .is_local       = false,
694                                 .is_mandatory   = false,
695                                 .val            = 0
696                         },
697                         {
698                                 .dependent_feat = DCCPF_SEND_LEV_RATE,
699                                 .is_local       = false,
700                                 .is_mandatory   = true,
701                                 .val            = 1
702                         },
703                         {       /* this CCID does not support Ack Ratio */
704                                 .dependent_feat = DCCPF_ACK_RATIO,
705                                 .is_local       = true,
706                                 .is_mandatory   = false,
707                                 .val            = 0
708                         },
709                         {       /* tell receiver we are sending NDP counts */
710                                 .dependent_feat = DCCPF_SEND_NDP_COUNT,
711                                 .is_local       = true,
712                                 .is_mandatory   = false,
713                                 .val            = 1
714                         },
715                         { 0, 0, 0, 0 }
716                 }
717         };
718         switch (ccid) {
719         case DCCPC_CCID2:
720                 return ccid2_dependencies[is_local];
721         case DCCPC_CCID3:
722                 return ccid3_dependencies[is_local];
723         default:
724                 return NULL;
725         }
726 }
727
728 /**
729  * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
730  * @fn: feature-negotiation list to update
731  * @id: CCID number to track
732  * @is_local: whether TX CCID (1) or RX CCID (0) is meant
733  * This function needs to be called after registering all other features.
734  */
735 static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
736 {
737         const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
738         int i, rc = (table == NULL);
739
740         for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
741                 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
742                         rc = __feat_register_sp(fn, table[i].dependent_feat,
743                                                     table[i].is_local,
744                                                     table[i].is_mandatory,
745                                                     &table[i].val, 1);
746                 else
747                         rc = __feat_register_nn(fn, table[i].dependent_feat,
748                                                     table[i].is_mandatory,
749                                                     table[i].val);
750         return rc;
751 }
752
753 /**
754  * dccp_feat_finalise_settings  -  Finalise settings before starting negotiation
755  * @dp: client or listening socket (settings will be inherited)
756  * This is called after all registrations (socket initialisation, sysctls, and
757  * sockopt calls), and before sending the first packet containing Change options
758  * (ie. client-Request or server-Response), to ensure internal consistency.
759  */
760 int dccp_feat_finalise_settings(struct dccp_sock *dp)
761 {
762         struct list_head *fn = &dp->dccps_featneg;
763         struct dccp_feat_entry *entry;
764         int i = 2, ccids[2] = { -1, -1 };
765
766         /*
767          * Propagating CCIDs:
768          * 1) not useful to propagate CCID settings if this host advertises more
769          *    than one CCID: the choice of CCID  may still change - if this is
770          *    the client, or if this is the server and the client sends
771          *    singleton CCID values.
772          * 2) since is that propagate_ccid changes the list, we defer changing
773          *    the sorted list until after the traversal.
774          */
775         list_for_each_entry(entry, fn, node)
776                 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
777                         ccids[entry->is_local] = entry->val.sp.vec[0];
778         while (i--)
779                 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
780                         return -1;
781         return 0;
782 }
783
784 /**
785  * dccp_feat_server_ccid_dependencies  -  Resolve CCID-dependent features
786  * It is the server which resolves the dependencies once the CCID has been
787  * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
788  */
789 int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
790 {
791         struct list_head *fn = &dreq->dreq_featneg;
792         struct dccp_feat_entry *entry;
793         u8 is_local, ccid;
794
795         for (is_local = 0; is_local <= 1; is_local++) {
796                 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
797
798                 if (entry != NULL && !entry->empty_confirm)
799                         ccid = entry->val.sp.vec[0];
800                 else
801                         ccid = dccp_feat_default_value(DCCPF_CCID);
802
803                 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
804                         return -1;
805         }
806         return 0;
807 }
808
809 /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
810 static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
811 {
812         u8 c, s;
813
814         for (s = 0; s < slen; s++)
815                 for (c = 0; c < clen; c++)
816                         if (servlist[s] == clilist[c])
817                                 return servlist[s];
818         return -1;
819 }
820
821 /**
822  * dccp_feat_prefer  -  Move preferred entry to the start of array
823  * Reorder the @array_len elements in @array so that @preferred_value comes
824  * first. Returns >0 to indicate that @preferred_value does occur in @array.
825  */
826 static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
827 {
828         u8 i, does_occur = 0;
829
830         if (array != NULL) {
831                 for (i = 0; i < array_len; i++)
832                         if (array[i] == preferred_value) {
833                                 array[i] = array[0];
834                                 does_occur++;
835                         }
836                 if (does_occur)
837                         array[0] = preferred_value;
838         }
839         return does_occur;
840 }
841
842 /**
843  * dccp_feat_reconcile  -  Reconcile SP preference lists
844  *  @fval: SP list to reconcile into
845  *  @arr: received SP preference list
846  *  @len: length of @arr in bytes
847  *  @is_server: whether this side is the server (and @fv is the server's list)
848  *  @reorder: whether to reorder the list in @fv after reconciling with @arr
849  * When successful, > 0 is returned and the reconciled list is in @fval.
850  * A value of 0 means that negotiation failed (no shared entry).
851  */
852 static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
853                                bool is_server, bool reorder)
854 {
855         int rc;
856
857         if (!fv->sp.vec || !arr) {
858                 DCCP_CRIT("NULL feature value or array");
859                 return 0;
860         }
861
862         if (is_server)
863                 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
864         else
865                 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
866
867         if (!reorder)
868                 return rc;
869         if (rc < 0)
870                 return 0;
871
872         /*
873          * Reorder list: used for activating features and in dccp_insert_fn_opt.
874          */
875         return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
876 }
877
878 /**
879  * dccp_feat_change_recv  -  Process incoming ChangeL/R options
880  * @fn: feature-negotiation list to update
881  * @is_mandatory: whether the Change was preceded by a Mandatory option
882  * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
883  * @feat: one of %dccp_feature_numbers
884  * @val: NN value or SP value/preference list
885  * @len: length of @val in bytes
886  * @server: whether this node is the server (1) or the client (0)
887  */
888 static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
889                                 u8 feat, u8 *val, u8 len, const bool server)
890 {
891         u8 defval, type = dccp_feat_type(feat);
892         const bool local = (opt == DCCPO_CHANGE_R);
893         struct dccp_feat_entry *entry;
894         dccp_feat_val fval;
895
896         if (len == 0 || type == FEAT_UNKNOWN)           /* 6.1 and 6.6.8 */
897                 goto unknown_feature_or_value;
898
899         /*
900          *      Negotiation of NN features: Change R is invalid, so there is no
901          *      simultaneous negotiation; hence we do not look up in the list.
902          */
903         if (type == FEAT_NN) {
904                 if (local || len > sizeof(fval.nn))
905                         goto unknown_feature_or_value;
906
907                 /* 6.3.2: "The feature remote MUST accept any valid value..." */
908                 fval.nn = dccp_decode_value_var(val, len);
909                 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
910                         goto unknown_feature_or_value;
911
912                 return dccp_feat_push_confirm(fn, feat, local, &fval);
913         }
914
915         /*
916          *      Unidirectional/simultaneous negotiation of SP features (6.3.1)
917          */
918         entry = dccp_feat_list_lookup(fn, feat, local);
919         if (entry == NULL) {
920                 /*
921                  * No particular preferences have been registered. We deal with
922                  * this situation by assuming that all valid values are equally
923                  * acceptable, and apply the following checks:
924                  * - if the peer's list is a singleton, we accept a valid value;
925                  * - if we are the server, we first try to see if the peer (the
926                  *   client) advertises the default value. If yes, we use it,
927                  *   otherwise we accept the preferred value;
928                  * - else if we are the client, we use the first list element.
929                  */
930                 if (dccp_feat_clone_sp_val(&fval, val, 1))
931                         return DCCP_RESET_CODE_TOO_BUSY;
932
933                 if (len > 1 && server) {
934                         defval = dccp_feat_default_value(feat);
935                         if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
936                                 fval.sp.vec[0] = defval;
937                 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
938                         kfree(fval.sp.vec);
939                         goto unknown_feature_or_value;
940                 }
941
942                 /* Treat unsupported CCIDs like invalid values */
943                 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
944                         kfree(fval.sp.vec);
945                         goto not_valid_or_not_known;
946                 }
947
948                 return dccp_feat_push_confirm(fn, feat, local, &fval);
949
950         } else if (entry->state == FEAT_UNSTABLE) {     /* 6.6.2 */
951                 return 0;
952         }
953
954         if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
955                 entry->empty_confirm = 0;
956         } else if (is_mandatory) {
957                 return DCCP_RESET_CODE_MANDATORY_ERROR;
958         } else if (entry->state == FEAT_INITIALISING) {
959                 /*
960                  * Failed simultaneous negotiation (server only): try to `save'
961                  * the connection by checking whether entry contains the default
962                  * value for @feat. If yes, send an empty Confirm to signal that
963                  * the received Change was not understood - which implies using
964                  * the default value.
965                  * If this also fails, we use Reset as the last resort.
966                  */
967                 WARN_ON(!server);
968                 defval = dccp_feat_default_value(feat);
969                 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
970                         return DCCP_RESET_CODE_OPTION_ERROR;
971                 entry->empty_confirm = 1;
972         }
973         entry->needs_confirm   = 1;
974         entry->needs_mandatory = 0;
975         entry->state           = FEAT_STABLE;
976         return 0;
977
978 unknown_feature_or_value:
979         if (!is_mandatory)
980                 return dccp_push_empty_confirm(fn, feat, local);
981
982 not_valid_or_not_known:
983         return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
984                             : DCCP_RESET_CODE_OPTION_ERROR;
985 }
986
987 /**
988  * dccp_feat_confirm_recv  -  Process received Confirm options
989  * @fn: feature-negotiation list to update
990  * @is_mandatory: whether @opt was preceded by a Mandatory option
991  * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
992  * @feat: one of %dccp_feature_numbers
993  * @val: NN value or SP value/preference list
994  * @len: length of @val in bytes
995  * @server: whether this node is server (1) or client (0)
996  */
997 static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
998                                  u8 feat, u8 *val, u8 len, const bool server)
999 {
1000         u8 *plist, plen, type = dccp_feat_type(feat);
1001         const bool local = (opt == DCCPO_CONFIRM_R);
1002         struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1003
1004         if (entry == NULL) {    /* nothing queued: ignore or handle error */
1005                 if (is_mandatory && type == FEAT_UNKNOWN)
1006                         return DCCP_RESET_CODE_MANDATORY_ERROR;
1007
1008                 if (!local && type == FEAT_NN)          /* 6.3.2 */
1009                         goto confirmation_failed;
1010                 return 0;
1011         }
1012
1013         if (entry->state != FEAT_CHANGING)              /* 6.6.2 */
1014                 return 0;
1015
1016         if (len == 0) {
1017                 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1018                         goto confirmation_failed;
1019                 /*
1020                  * Empty Confirm during connection setup: this means reverting
1021                  * to the `old' value, which in this case is the default. Since
1022                  * we handle default values automatically when no other values
1023                  * have been set, we revert to the old value by removing this
1024                  * entry from the list.
1025                  */
1026                 dccp_feat_list_pop(entry);
1027                 return 0;
1028         }
1029
1030         if (type == FEAT_NN) {
1031                 if (len > sizeof(entry->val.nn))
1032                         goto confirmation_failed;
1033
1034                 if (entry->val.nn == dccp_decode_value_var(val, len))
1035                         goto confirmation_succeeded;
1036
1037                 DCCP_WARN("Bogus Confirm for non-existing value\n");
1038                 goto confirmation_failed;
1039         }
1040
1041         /*
1042          * Parsing SP Confirms: the first element of @val is the preferred
1043          * SP value which the peer confirms, the remainder depends on @len.
1044          * Note that only the confirmed value need to be a valid SP value.
1045          */
1046         if (!dccp_feat_is_valid_sp_val(feat, *val))
1047                 goto confirmation_failed;
1048
1049         if (len == 1) {         /* peer didn't supply a preference list */
1050                 plist = val;
1051                 plen  = len;
1052         } else {                /* preferred value + preference list */
1053                 plist = val + 1;
1054                 plen  = len - 1;
1055         }
1056
1057         /* Check whether the peer got the reconciliation right (6.6.8) */
1058         if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1059                 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1060                 return DCCP_RESET_CODE_OPTION_ERROR;
1061         }
1062         entry->val.sp.vec[0] = *val;
1063
1064 confirmation_succeeded:
1065         entry->state = FEAT_STABLE;
1066         return 0;
1067
1068 confirmation_failed:
1069         DCCP_WARN("Confirmation failed\n");
1070         return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1071                             : DCCP_RESET_CODE_OPTION_ERROR;
1072 }
1073
1074 /**
1075  * dccp_feat_parse_options  -  Process Feature-Negotiation Options
1076  * @sk: for general use and used by the client during connection setup
1077  * @dreq: used by the server during connection setup
1078  * @mandatory: whether @opt was preceded by a Mandatory option
1079  * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1080  * @feat: one of %dccp_feature_numbers
1081  * @val: value contents of @opt
1082  * @len: length of @val in bytes
1083  * Returns 0 on success, a Reset code for ending the connection otherwise.
1084  */
1085 int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1086                             u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1087 {
1088         struct dccp_sock *dp = dccp_sk(sk);
1089         struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1090         bool server = false;
1091
1092         switch (sk->sk_state) {
1093         /*
1094          *      Negotiation during connection setup
1095          */
1096         case DCCP_LISTEN:
1097                 server = true;                  /* fall through */
1098         case DCCP_REQUESTING:
1099                 switch (opt) {
1100                 case DCCPO_CHANGE_L:
1101                 case DCCPO_CHANGE_R:
1102                         return dccp_feat_change_recv(fn, mandatory, opt, feat,
1103                                                      val, len, server);
1104                 case DCCPO_CONFIRM_R:
1105                 case DCCPO_CONFIRM_L:
1106                         return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1107                                                       val, len, server);
1108                 }
1109         }
1110         return 0;       /* ignore FN options in all other states */
1111 }
1112
1113 int dccp_feat_init(struct sock *sk)
1114 {
1115         struct dccp_sock *dp = dccp_sk(sk);
1116         struct dccp_minisock *dmsk = dccp_msk(sk);
1117         int rc;
1118
1119         INIT_LIST_HEAD(&dmsk->dccpms_pending);  /* XXX no longer used */
1120         INIT_LIST_HEAD(&dmsk->dccpms_conf);     /* XXX no longer used */
1121
1122         /* CCID L */
1123         rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
1124                                 &dmsk->dccpms_tx_ccid, 1);
1125         if (rc)
1126                 goto out;
1127
1128         /* CCID R */
1129         rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
1130                                 &dmsk->dccpms_rx_ccid, 1);
1131         if (rc)
1132                 goto out;
1133
1134         /* Ack ratio */
1135         rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
1136                                 dp->dccps_l_ack_ratio);
1137 out:
1138         return rc;
1139 }
1140
1141 EXPORT_SYMBOL_GPL(dccp_feat_init);
1142
1143 int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1144 {
1145         struct dccp_sock *dp = dccp_sk(sk);
1146         struct dccp_feat_entry *cur, *next;
1147         int idx;
1148         dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1149                  [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1150         };
1151
1152         list_for_each_entry(cur, fn_list, node) {
1153                 /*
1154                  * An empty Confirm means that either an unknown feature type
1155                  * or an invalid value was present. In the first case there is
1156                  * nothing to activate, in the other the default value is used.
1157                  */
1158                 if (cur->empty_confirm)
1159                         continue;
1160
1161                 idx = dccp_feat_index(cur->feat_num);
1162                 if (idx < 0) {
1163                         DCCP_BUG("Unknown feature %u", cur->feat_num);
1164                         goto activation_failed;
1165                 }
1166                 if (cur->state != FEAT_STABLE) {
1167                         DCCP_CRIT("Negotiation of %s %u failed in state %u",
1168                                   cur->is_local ? "local" : "remote",
1169                                   cur->feat_num, cur->state);
1170                         goto activation_failed;
1171                 }
1172                 fvals[idx][cur->is_local] = &cur->val;
1173         }
1174
1175         /*
1176          * Activate in decreasing order of index, so that the CCIDs are always
1177          * activated as the last feature. This avoids the case where a CCID
1178          * relies on the initialisation of one or more features that it depends
1179          * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1180          */
1181         for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1182                 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1183                     __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1184                         DCCP_CRIT("Could not activate %d", idx);
1185                         goto activation_failed;
1186                 }
1187
1188         /* Clean up Change options which have been confirmed already */
1189         list_for_each_entry_safe(cur, next, fn_list, node)
1190                 if (!cur->needs_confirm)
1191                         dccp_feat_list_pop(cur);
1192
1193         dccp_pr_debug("Activation OK\n");
1194         return 0;
1195
1196 activation_failed:
1197         /*
1198          * We clean up everything that may have been allocated, since
1199          * it is difficult to track at which stage negotiation failed.
1200          * This is ok, since all allocation functions below are robust
1201          * against NULL arguments.
1202          */
1203         ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1204         ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1205         dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1206         dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1207         dp->dccps_hc_rx_ackvec = NULL;
1208         return -1;
1209 }
1210
1211 #ifdef CONFIG_IP_DCCP_DEBUG
1212 const char *dccp_feat_typename(const u8 type)
1213 {
1214         switch(type) {
1215         case DCCPO_CHANGE_L:  return("ChangeL");
1216         case DCCPO_CONFIRM_L: return("ConfirmL");
1217         case DCCPO_CHANGE_R:  return("ChangeR");
1218         case DCCPO_CONFIRM_R: return("ConfirmR");
1219         /* the following case must not appear in feature negotation  */
1220         default:              dccp_pr_debug("unknown type %d [BUG!]\n", type);
1221         }
1222         return NULL;
1223 }
1224
1225 EXPORT_SYMBOL_GPL(dccp_feat_typename);
1226
1227 const char *dccp_feat_name(const u8 feat)
1228 {
1229         static const char *feature_names[] = {
1230                 [DCCPF_RESERVED]        = "Reserved",
1231                 [DCCPF_CCID]            = "CCID",
1232                 [DCCPF_SHORT_SEQNOS]    = "Allow Short Seqnos",
1233                 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
1234                 [DCCPF_ECN_INCAPABLE]   = "ECN Incapable",
1235                 [DCCPF_ACK_RATIO]       = "Ack Ratio",
1236                 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
1237                 [DCCPF_SEND_NDP_COUNT]  = "Send NDP Count",
1238                 [DCCPF_MIN_CSUM_COVER]  = "Min. Csum Coverage",
1239                 [DCCPF_DATA_CHECKSUM]   = "Send Data Checksum",
1240         };
1241         if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1242                 return feature_names[DCCPF_RESERVED];
1243
1244         if (feat ==  DCCPF_SEND_LEV_RATE)
1245                 return "Send Loss Event Rate";
1246         if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1247                 return "CCID-specific";
1248
1249         return feature_names[feat];
1250 }
1251
1252 EXPORT_SYMBOL_GPL(dccp_feat_name);
1253 #endif /* CONFIG_IP_DCCP_DEBUG */