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