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