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