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