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          * 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 /**
368  * dccp_feat_register_sp  -  Register requests to change SP feature values
369  * @sk: client or listening socket
370  * @feat: one of %dccp_feature_numbers
371  * @is_local: whether the local (1) or remote (0) @feat is meant
372  * @list: array of preferred values, in descending order of preference
373  * @len: length of @list in bytes
374  */
375 int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
376                           u8 const *list, u8 len)
377 {        /* any changes must be registered before establishing the connection */
378         if (sk->sk_state != DCCP_CLOSED)
379                 return -EISCONN;
380         if (dccp_feat_type(feat) != FEAT_SP)
381                 return -EINVAL;
382         return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
383                                   0, list, len);
384 }
385
386 /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
387 int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
388 {
389         /* any changes must be registered before establishing the connection */
390         if (sk->sk_state != DCCP_CLOSED)
391                 return -EISCONN;
392         if (dccp_feat_type(feat) != FEAT_NN)
393                 return -EINVAL;
394         return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
395 }
396
397 /*
398  *      Tracking features whose value depend on the choice of CCID
399  *
400  * This is designed with an extension in mind so that a list walk could be done
401  * before activating any features. However, the existing framework was found to
402  * work satisfactorily up until now, the automatic verification is left open.
403  * When adding new CCIDs, add a corresponding dependency table here.
404  */
405 static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
406 {
407         static const struct ccid_dependency ccid2_dependencies[2][2] = {
408                 /*
409                  * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
410                  * feature and Send Ack Vector is an RX feature, `is_local'
411                  * needs to be reversed.
412                  */
413                 {       /* Dependencies of the receiver-side (remote) CCID2 */
414                         {
415                                 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
416                                 .is_local       = true,
417                                 .is_mandatory   = true,
418                                 .val            = 1
419                         },
420                         { 0, 0, 0, 0 }
421                 },
422                 {       /* Dependencies of the sender-side (local) CCID2 */
423                         {
424                                 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
425                                 .is_local       = false,
426                                 .is_mandatory   = true,
427                                 .val            = 1
428                         },
429                         { 0, 0, 0, 0 }
430                 }
431         };
432         static const struct ccid_dependency ccid3_dependencies[2][5] = {
433                 {       /*
434                          * Dependencies of the receiver-side CCID3
435                          */
436                         {       /* locally disable Ack Vectors */
437                                 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
438                                 .is_local       = true,
439                                 .is_mandatory   = false,
440                                 .val            = 0
441                         },
442                         {       /* see below why Send Loss Event Rate is on */
443                                 .dependent_feat = DCCPF_SEND_LEV_RATE,
444                                 .is_local       = true,
445                                 .is_mandatory   = true,
446                                 .val            = 1
447                         },
448                         {       /* NDP Count is needed as per RFC 4342, 6.1.1 */
449                                 .dependent_feat = DCCPF_SEND_NDP_COUNT,
450                                 .is_local       = false,
451                                 .is_mandatory   = true,
452                                 .val            = 1
453                         },
454                         { 0, 0, 0, 0 },
455                 },
456                 {       /*
457                          * CCID3 at the TX side: we request that the HC-receiver
458                          * will not send Ack Vectors (they will be ignored, so
459                          * Mandatory is not set); we enable Send Loss Event Rate
460                          * (Mandatory since the implementation does not support
461                          * the Loss Intervals option of RFC 4342, 8.6).
462                          * The last two options are for peer's information only.
463                         */
464                         {
465                                 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
466                                 .is_local       = false,
467                                 .is_mandatory   = false,
468                                 .val            = 0
469                         },
470                         {
471                                 .dependent_feat = DCCPF_SEND_LEV_RATE,
472                                 .is_local       = false,
473                                 .is_mandatory   = true,
474                                 .val            = 1
475                         },
476                         {       /* this CCID does not support Ack Ratio */
477                                 .dependent_feat = DCCPF_ACK_RATIO,
478                                 .is_local       = true,
479                                 .is_mandatory   = false,
480                                 .val            = 0
481                         },
482                         {       /* tell receiver we are sending NDP counts */
483                                 .dependent_feat = DCCPF_SEND_NDP_COUNT,
484                                 .is_local       = true,
485                                 .is_mandatory   = false,
486                                 .val            = 1
487                         },
488                         { 0, 0, 0, 0 }
489                 }
490         };
491         switch (ccid) {
492         case DCCPC_CCID2:
493                 return ccid2_dependencies[is_local];
494         case DCCPC_CCID3:
495                 return ccid3_dependencies[is_local];
496         default:
497                 return NULL;
498         }
499 }
500
501 /**
502  * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
503  * @fn: feature-negotiation list to update
504  * @id: CCID number to track
505  * @is_local: whether TX CCID (1) or RX CCID (0) is meant
506  * This function needs to be called after registering all other features.
507  */
508 static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
509 {
510         const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
511         int i, rc = (table == NULL);
512
513         for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
514                 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
515                         rc = __feat_register_sp(fn, table[i].dependent_feat,
516                                                     table[i].is_local,
517                                                     table[i].is_mandatory,
518                                                     &table[i].val, 1);
519                 else
520                         rc = __feat_register_nn(fn, table[i].dependent_feat,
521                                                     table[i].is_mandatory,
522                                                     table[i].val);
523         return rc;
524 }
525
526 /**
527  * dccp_feat_finalise_settings  -  Finalise settings before starting negotiation
528  * @dp: client or listening socket (settings will be inherited)
529  * This is called after all registrations (socket initialisation, sysctls, and
530  * sockopt calls), and before sending the first packet containing Change options
531  * (ie. client-Request or server-Response), to ensure internal consistency.
532  */
533 int dccp_feat_finalise_settings(struct dccp_sock *dp)
534 {
535         struct list_head *fn = &dp->dccps_featneg;
536         struct dccp_feat_entry *entry;
537         int i = 2, ccids[2] = { -1, -1 };
538
539         /*
540          * Propagating CCIDs:
541          * 1) not useful to propagate CCID settings if this host advertises more
542          *    than one CCID: the choice of CCID  may still change - if this is
543          *    the client, or if this is the server and the client sends
544          *    singleton CCID values.
545          * 2) since is that propagate_ccid changes the list, we defer changing
546          *    the sorted list until after the traversal.
547          */
548         list_for_each_entry(entry, fn, node)
549                 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
550                         ccids[entry->is_local] = entry->val.sp.vec[0];
551         while (i--)
552                 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
553                         return -1;
554         return 0;
555 }
556
557 /**
558  * dccp_feat_server_ccid_dependencies  -  Resolve CCID-dependent features
559  * It is the server which resolves the dependencies once the CCID has been
560  * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
561  */
562 int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
563 {
564         struct list_head *fn = &dreq->dreq_featneg;
565         struct dccp_feat_entry *entry;
566         u8 is_local, ccid;
567
568         for (is_local = 0; is_local <= 1; is_local++) {
569                 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
570
571                 if (entry != NULL && !entry->empty_confirm)
572                         ccid = entry->val.sp.vec[0];
573                 else
574                         ccid = dccp_feat_default_value(DCCPF_CCID);
575
576                 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
577                         return -1;
578         }
579         return 0;
580 }
581
582 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
583 {
584         struct dccp_sock *dp = dccp_sk(sk);
585         struct dccp_minisock *dmsk = dccp_msk(sk);
586         /* figure out if we are changing our CCID or the peer's */
587         const int rx = type == DCCPO_CHANGE_R;
588         const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
589         struct ccid *new_ccid;
590
591         /* Check if nothing is being changed. */
592         if (ccid_nr == new_ccid_nr)
593                 return 0;
594
595         new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
596         if (new_ccid == NULL)
597                 return -ENOMEM;
598
599         if (rx) {
600                 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
601                 dp->dccps_hc_rx_ccid = new_ccid;
602                 dmsk->dccpms_rx_ccid = new_ccid_nr;
603         } else {
604                 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
605                 dp->dccps_hc_tx_ccid = new_ccid;
606                 dmsk->dccpms_tx_ccid = new_ccid_nr;
607         }
608
609         return 0;
610 }
611
612 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
613 {
614         dccp_feat_debug(type, feat, val);
615
616         switch (feat) {
617         case DCCPF_CCID:
618                 return dccp_feat_update_ccid(sk, type, val);
619         default:
620                 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
621                               dccp_feat_typename(type), feat);
622                 break;
623         }
624         return 0;
625 }
626
627 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
628                                u8 *rpref, u8 rlen)
629 {
630         struct dccp_sock *dp = dccp_sk(sk);
631         u8 *spref, slen, *res = NULL;
632         int i, j, rc, agree = 1;
633
634         BUG_ON(rpref == NULL);
635
636         /* check if we are the black sheep */
637         if (dp->dccps_role == DCCP_ROLE_CLIENT) {
638                 spref = rpref;
639                 slen  = rlen;
640                 rpref = opt->dccpop_val;
641                 rlen  = opt->dccpop_len;
642         } else {
643                 spref = opt->dccpop_val;
644                 slen  = opt->dccpop_len;
645         }
646         /*
647          * Now we have server preference list in spref and client preference in
648          * rpref
649          */
650         BUG_ON(spref == NULL);
651         BUG_ON(rpref == NULL);
652
653         /* FIXME sanity check vals */
654
655         /* Are values in any order?  XXX Lame "algorithm" here */
656         for (i = 0; i < slen; i++) {
657                 for (j = 0; j < rlen; j++) {
658                         if (spref[i] == rpref[j]) {
659                                 res = &spref[i];
660                                 break;
661                         }
662                 }
663                 if (res)
664                         break;
665         }
666
667         /* we didn't agree on anything */
668         if (res == NULL) {
669                 /* confirm previous value */
670                 switch (opt->dccpop_feat) {
671                 case DCCPF_CCID:
672                         /* XXX did i get this right? =P */
673                         if (opt->dccpop_type == DCCPO_CHANGE_L)
674                                 res = &dccp_msk(sk)->dccpms_tx_ccid;
675                         else
676                                 res = &dccp_msk(sk)->dccpms_rx_ccid;
677                         break;
678
679                 default:
680                         DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
681                         /* XXX implement res */
682                         return -EFAULT;
683                 }
684
685                 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
686                 agree = 0; /* this is used for mandatory options... */
687         }
688
689         /* need to put result and our preference list */
690         rlen = 1 + opt->dccpop_len;
691         rpref = kmalloc(rlen, GFP_ATOMIC);
692         if (rpref == NULL)
693                 return -ENOMEM;
694
695         *rpref = *res;
696         memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
697
698         /* put it in the "confirm queue" */
699         if (opt->dccpop_sc == NULL) {
700                 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
701                 if (opt->dccpop_sc == NULL) {
702                         kfree(rpref);
703                         return -ENOMEM;
704                 }
705         } else {
706                 /* recycle the confirm slot */
707                 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
708                 kfree(opt->dccpop_sc->dccpoc_val);
709                 dccp_pr_debug("recycling confirm slot\n");
710         }
711         memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
712
713         opt->dccpop_sc->dccpoc_val = rpref;
714         opt->dccpop_sc->dccpoc_len = rlen;
715
716         /* update the option on our side [we are about to send the confirm] */
717         rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
718         if (rc) {
719                 kfree(opt->dccpop_sc->dccpoc_val);
720                 kfree(opt->dccpop_sc);
721                 opt->dccpop_sc = NULL;
722                 return rc;
723         }
724
725         dccp_pr_debug("Will confirm %d\n", *rpref);
726
727         /* say we want to change to X but we just got a confirm X, suppress our
728          * change
729          */
730         if (!opt->dccpop_conf) {
731                 if (*opt->dccpop_val == *res)
732                         opt->dccpop_conf = 1;
733                 dccp_pr_debug("won't ask for change of same feature\n");
734         }
735
736         return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
737 }
738
739 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
740 {
741         struct dccp_minisock *dmsk = dccp_msk(sk);
742         struct dccp_opt_pend *opt;
743         int rc = 1;
744         u8 t;
745
746         /*
747          * We received a CHANGE.  We gotta match it against our own preference
748          * list.  If we got a CHANGE_R it means it's a change for us, so we need
749          * to compare our CHANGE_L list.
750          */
751         if (type == DCCPO_CHANGE_L)
752                 t = DCCPO_CHANGE_R;
753         else
754                 t = DCCPO_CHANGE_L;
755
756         /* find our preference list for this feature */
757         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
758                 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
759                         continue;
760
761                 /* find the winner from the two preference lists */
762                 rc = dccp_feat_reconcile(sk, opt, val, len);
763                 break;
764         }
765
766         /* We didn't deal with the change.  This can happen if we have no
767          * preference list for the feature.  In fact, it just shouldn't
768          * happen---if we understand a feature, we should have a preference list
769          * with at least the default value.
770          */
771         BUG_ON(rc == 1);
772
773         return rc;
774 }
775
776 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
777 {
778         struct dccp_opt_pend *opt;
779         struct dccp_minisock *dmsk = dccp_msk(sk);
780         u8 *copy;
781         int rc;
782
783         /* NN features must be Change L (sec. 6.3.2) */
784         if (type != DCCPO_CHANGE_L) {
785                 dccp_pr_debug("received %s for NN feature %d\n",
786                                 dccp_feat_typename(type), feature);
787                 return -EFAULT;
788         }
789
790         /* XXX sanity check opt val */
791
792         /* copy option so we can confirm it */
793         opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
794         if (opt == NULL)
795                 return -ENOMEM;
796
797         copy = kmemdup(val, len, GFP_ATOMIC);
798         if (copy == NULL) {
799                 kfree(opt);
800                 return -ENOMEM;
801         }
802
803         opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
804         opt->dccpop_feat = feature;
805         opt->dccpop_val  = copy;
806         opt->dccpop_len  = len;
807
808         /* change feature */
809         rc = dccp_feat_update(sk, type, feature, *val);
810         if (rc) {
811                 kfree(opt->dccpop_val);
812                 kfree(opt);
813                 return rc;
814         }
815
816         dccp_feat_debug(type, feature, *copy);
817
818         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
819
820         return 0;
821 }
822
823 static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
824                                     u8 type, u8 feature)
825 {
826         /* XXX check if other confirms for that are queued and recycle slot */
827         struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
828
829         if (opt == NULL) {
830                 /* XXX what do we do?  Ignoring should be fine.  It's a change
831                  * after all =P
832                  */
833                 return;
834         }
835
836         switch (type) {
837         case DCCPO_CHANGE_L:
838                 opt->dccpop_type = DCCPO_CONFIRM_R;
839                 break;
840         case DCCPO_CHANGE_R:
841                 opt->dccpop_type = DCCPO_CONFIRM_L;
842                 break;
843         default:
844                 DCCP_WARN("invalid type %d\n", type);
845                 kfree(opt);
846                 return;
847         }
848         opt->dccpop_feat = feature;
849         opt->dccpop_val  = NULL;
850         opt->dccpop_len  = 0;
851
852         /* change feature */
853         dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
854
855         list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
856 }
857
858 static void dccp_feat_flush_confirm(struct sock *sk)
859 {
860         struct dccp_minisock *dmsk = dccp_msk(sk);
861         /* Check if there is anything to confirm in the first place */
862         int yes = !list_empty(&dmsk->dccpms_conf);
863
864         if (!yes) {
865                 struct dccp_opt_pend *opt;
866
867                 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
868                         if (opt->dccpop_conf) {
869                                 yes = 1;
870                                 break;
871                         }
872                 }
873         }
874
875         if (!yes)
876                 return;
877
878         /* OK there is something to confirm... */
879         /* XXX check if packet is in flight?  Send delayed ack?? */
880         if (sk->sk_state == DCCP_OPEN)
881                 dccp_send_ack(sk);
882 }
883
884 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
885 {
886         int rc;
887
888         /* Ignore Change requests other than during connection setup */
889         if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
890                 return 0;
891         dccp_feat_debug(type, feature, *val);
892
893         /* figure out if it's SP or NN feature */
894         switch (feature) {
895         /* deal with SP features */
896         case DCCPF_CCID:
897                 rc = dccp_feat_sp(sk, type, feature, val, len);
898                 break;
899
900         /* deal with NN features */
901         case DCCPF_ACK_RATIO:
902                 rc = dccp_feat_nn(sk, type, feature, val, len);
903                 break;
904
905         /* XXX implement other features */
906         default:
907                 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
908                               dccp_feat_typename(type), feature);
909                 rc = -EFAULT;
910                 break;
911         }
912
913         /* check if there were problems changing features */
914         if (rc) {
915                 /* If we don't agree on SP, we sent a confirm for old value.
916                  * However we propagate rc to caller in case option was
917                  * mandatory
918                  */
919                 if (rc != DCCP_FEAT_SP_NOAGREE)
920                         dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
921         }
922
923         /* generate the confirm [if required] */
924         dccp_feat_flush_confirm(sk);
925
926         return rc;
927 }
928
929 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
930
931 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
932                            u8 *val, u8 len)
933 {
934         u8 t;
935         struct dccp_opt_pend *opt;
936         struct dccp_minisock *dmsk = dccp_msk(sk);
937         int found = 0;
938         int all_confirmed = 1;
939
940         /* Ignore Confirm options other than during connection setup */
941         if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
942                 return 0;
943         dccp_feat_debug(type, feature, *val);
944
945         /* locate our change request */
946         switch (type) {
947         case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
948         case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
949         default:              DCCP_WARN("invalid type %d\n", type);
950                               return 1;
951
952         }
953         /* XXX sanity check feature value */
954
955         list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
956                 if (!opt->dccpop_conf && opt->dccpop_type == t &&
957                     opt->dccpop_feat == feature) {
958                         found = 1;
959                         dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
960
961                         /* XXX do sanity check */
962
963                         opt->dccpop_conf = 1;
964
965                         /* We got a confirmation---change the option */
966                         dccp_feat_update(sk, opt->dccpop_type,
967                                          opt->dccpop_feat, *val);
968
969                         /* XXX check the return value of dccp_feat_update */
970                         break;
971                 }
972
973                 if (!opt->dccpop_conf)
974                         all_confirmed = 0;
975         }
976
977         if (!found)
978                 dccp_pr_debug("%s(%d, ...) never requested\n",
979                               dccp_feat_typename(type), feature);
980         return 0;
981 }
982
983 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
984
985 void dccp_feat_clean(struct dccp_minisock *dmsk)
986 {
987         struct dccp_opt_pend *opt, *next;
988
989         list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
990                                  dccpop_node) {
991                 BUG_ON(opt->dccpop_val == NULL);
992                 kfree(opt->dccpop_val);
993
994                 if (opt->dccpop_sc != NULL) {
995                         BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
996                         kfree(opt->dccpop_sc->dccpoc_val);
997                         kfree(opt->dccpop_sc);
998                 }
999
1000                 kfree(opt);
1001         }
1002         INIT_LIST_HEAD(&dmsk->dccpms_pending);
1003
1004         list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
1005                 BUG_ON(opt == NULL);
1006                 if (opt->dccpop_val != NULL)
1007                         kfree(opt->dccpop_val);
1008                 kfree(opt);
1009         }
1010         INIT_LIST_HEAD(&dmsk->dccpms_conf);
1011 }
1012
1013 EXPORT_SYMBOL_GPL(dccp_feat_clean);
1014
1015 /* this is to be called only when a listening sock creates its child.  It is
1016  * assumed by the function---the confirm is not duplicated, but rather it is
1017  * "passed on".
1018  */
1019 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
1020 {
1021         struct dccp_minisock *olddmsk = dccp_msk(oldsk);
1022         struct dccp_minisock *newdmsk = dccp_msk(newsk);
1023         struct dccp_opt_pend *opt;
1024         int rc = 0;
1025
1026         INIT_LIST_HEAD(&newdmsk->dccpms_pending);
1027         INIT_LIST_HEAD(&newdmsk->dccpms_conf);
1028
1029         list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
1030                 struct dccp_opt_pend *newopt;
1031                 /* copy the value of the option */
1032                 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
1033
1034                 if (val == NULL)
1035                         goto out_clean;
1036
1037                 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
1038                 if (newopt == NULL) {
1039                         kfree(val);
1040                         goto out_clean;
1041                 }
1042
1043                 /* insert the option */
1044                 newopt->dccpop_val = val;
1045                 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
1046
1047                 /* XXX what happens with backlogs and multiple connections at
1048                  * once...
1049                  */
1050                 /* the master socket no longer needs to worry about confirms */
1051                 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
1052
1053                 /* reset state for a new socket */
1054                 opt->dccpop_conf = 0;
1055         }
1056
1057         /* XXX not doing anything about the conf queue */
1058
1059 out:
1060         return rc;
1061
1062 out_clean:
1063         dccp_feat_clean(newdmsk);
1064         rc = -ENOMEM;
1065         goto out;
1066 }
1067
1068 EXPORT_SYMBOL_GPL(dccp_feat_clone);
1069
1070 int dccp_feat_init(struct sock *sk)
1071 {
1072         struct dccp_sock *dp = dccp_sk(sk);
1073         struct dccp_minisock *dmsk = dccp_msk(sk);
1074         int rc;
1075
1076         INIT_LIST_HEAD(&dmsk->dccpms_pending);  /* XXX no longer used */
1077         INIT_LIST_HEAD(&dmsk->dccpms_conf);     /* XXX no longer used */
1078
1079         /* CCID L */
1080         rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
1081                                 &dmsk->dccpms_tx_ccid, 1);
1082         if (rc)
1083                 goto out;
1084
1085         /* CCID R */
1086         rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
1087                                 &dmsk->dccpms_rx_ccid, 1);
1088         if (rc)
1089                 goto out;
1090
1091         /* Ack ratio */
1092         rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
1093                                 dp->dccps_l_ack_ratio);
1094 out:
1095         return rc;
1096 }
1097
1098 EXPORT_SYMBOL_GPL(dccp_feat_init);
1099
1100 #ifdef CONFIG_IP_DCCP_DEBUG
1101 const char *dccp_feat_typename(const u8 type)
1102 {
1103         switch(type) {
1104         case DCCPO_CHANGE_L:  return("ChangeL");
1105         case DCCPO_CONFIRM_L: return("ConfirmL");
1106         case DCCPO_CHANGE_R:  return("ChangeR");
1107         case DCCPO_CONFIRM_R: return("ConfirmR");
1108         /* the following case must not appear in feature negotation  */
1109         default:              dccp_pr_debug("unknown type %d [BUG!]\n", type);
1110         }
1111         return NULL;
1112 }
1113
1114 EXPORT_SYMBOL_GPL(dccp_feat_typename);
1115
1116 const char *dccp_feat_name(const u8 feat)
1117 {
1118         static const char *feature_names[] = {
1119                 [DCCPF_RESERVED]        = "Reserved",
1120                 [DCCPF_CCID]            = "CCID",
1121                 [DCCPF_SHORT_SEQNOS]    = "Allow Short Seqnos",
1122                 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
1123                 [DCCPF_ECN_INCAPABLE]   = "ECN Incapable",
1124                 [DCCPF_ACK_RATIO]       = "Ack Ratio",
1125                 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
1126                 [DCCPF_SEND_NDP_COUNT]  = "Send NDP Count",
1127                 [DCCPF_MIN_CSUM_COVER]  = "Min. Csum Coverage",
1128                 [DCCPF_DATA_CHECKSUM]   = "Send Data Checksum",
1129         };
1130         if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1131                 return feature_names[DCCPF_RESERVED];
1132
1133         if (feat ==  DCCPF_SEND_LEV_RATE)
1134                 return "Send Loss Event Rate";
1135         if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1136                 return "CCID-specific";
1137
1138         return feature_names[feat];
1139 }
1140
1141 EXPORT_SYMBOL_GPL(dccp_feat_name);
1142 #endif /* CONFIG_IP_DCCP_DEBUG */