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