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