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