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