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