blob: 9399554878ccc782e3fde891a9d9d89aed7342dd [file] [log] [blame]
Andrea Bittauafe00252006-03-20 17:43:56 -08001/*
2 * net/dccp/feat.c
3 *
4 * An implementation of the DCCP protocol
5 * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
6 *
Gerrit Renker5cdae192007-12-13 12:41:46 -02007 * ASSUMPTIONS
8 * -----------
Gerrit Renker5591d282008-09-04 07:30:19 +02009 * o Feature negotiation is coordinated with connection setup (as in TCP), wild
10 * changes of parameters of an established connection are not supported.
Gerrit Renker5cdae192007-12-13 12:41:46 -020011 * 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.
Andrea Bittauafe00252006-03-20 17:43:56 -080019 */
20
Andrea Bittauafe00252006-03-20 17:43:56 -080021#include <linux/module.h>
22
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080023#include "ccid.h"
Andrea Bittauafe00252006-03-20 17:43:56 -080024#include "feat.h"
25
26#define DCCP_FEAT_SP_NOAGREE (-123)
27
Gerrit Renkerb4eec202008-09-04 07:30:19 +020028static 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 */
69static 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
86static 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
95static int dccp_feat_default_value(u8 feat_num)
96{
97 int idx = dccp_feat_index(feat_num);
98
99 return idx < 0 ? : dccp_feat_table[idx].default_value;
100}
101
Gerrit Renker5c7c9452008-09-04 07:30:19 +0200102/* copy constructor, fval must not already contain allocated memory */
103static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
104{
105 fval->sp.len = len;
106 if (fval->sp.len > 0) {
107 fval->sp.vec = kmemdup(val, len, gfp_any());
108 if (fval->sp.vec == NULL) {
109 fval->sp.len = 0;
110 return -ENOBUFS;
111 }
112 }
113 return 0;
114}
115
Gerrit Renkerb4eec202008-09-04 07:30:19 +0200116static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
117{
118 if (unlikely(val == NULL))
119 return;
120 if (dccp_feat_type(feat_num) == FEAT_SP)
121 kfree(val->sp.vec);
122 memset(val, 0, sizeof(*val));
123}
124
125static struct dccp_feat_entry *
126 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
127{
128 struct dccp_feat_entry *new;
129 u8 type = dccp_feat_type(original->feat_num);
130
131 if (type == FEAT_UNKNOWN)
132 return NULL;
133
134 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
135 if (new == NULL)
136 return NULL;
137
138 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
139 original->val.sp.vec,
140 original->val.sp.len)) {
141 kfree(new);
142 return NULL;
143 }
144 return new;
145}
146
147static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
148{
149 if (entry != NULL) {
150 dccp_feat_val_destructor(entry->feat_num, &entry->val);
151 kfree(entry);
152 }
153}
154
Gerrit Renker3001fc02008-09-04 07:30:19 +0200155/*
156 * List management functions
157 *
158 * Feature negotiation lists rely on and maintain the following invariants:
159 * - each feat_num in the list is known, i.e. we know its type and default value
160 * - each feat_num/is_local combination is unique (old entries are overwritten)
161 * - SP values are always freshly allocated
162 * - list is sorted in increasing order of feature number (faster lookup)
163 */
164static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
165 u8 feat_num, bool is_local)
166{
167 struct dccp_feat_entry *entry;
168
169 list_for_each_entry(entry, fn_list, node)
170 if (entry->feat_num == feat_num && entry->is_local == is_local)
171 return entry;
172 else if (entry->feat_num > feat_num)
173 break;
174 return NULL;
175}
176
177/**
178 * dccp_feat_entry_new - Central list update routine (called by all others)
179 * @head: list to add to
180 * @feat: feature number
181 * @local: whether the local (1) or remote feature with number @feat is meant
182 * This is the only constructor and serves to ensure the above invariants.
183 */
184static struct dccp_feat_entry *
185 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
186{
187 struct dccp_feat_entry *entry;
188
189 list_for_each_entry(entry, head, node)
190 if (entry->feat_num == feat && entry->is_local == local) {
191 dccp_feat_val_destructor(entry->feat_num, &entry->val);
192 return entry;
193 } else if (entry->feat_num > feat) {
194 head = &entry->node;
195 break;
196 }
197
198 entry = kmalloc(sizeof(*entry), gfp_any());
199 if (entry != NULL) {
200 entry->feat_num = feat;
201 entry->is_local = local;
202 list_add_tail(&entry->node, head);
203 }
204 return entry;
205}
206
207/**
208 * dccp_feat_push_change - Add/overwrite a Change option in the list
209 * @fn_list: feature-negotiation list to update
210 * @feat: one of %dccp_feature_numbers
211 * @local: whether local (1) or remote (0) @feat_num is meant
212 * @needs_mandatory: whether to use Mandatory feature negotiation options
213 * @fval: pointer to NN/SP value to be inserted (will be copied)
214 */
215static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
216 u8 mandatory, dccp_feat_val *fval)
217{
218 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
219
220 if (new == NULL)
221 return -ENOMEM;
222
223 new->feat_num = feat;
224 new->is_local = local;
225 new->state = FEAT_INITIALISING;
226 new->needs_confirm = 0;
227 new->empty_confirm = 0;
228 new->val = *fval;
229 new->needs_mandatory = mandatory;
230
231 return 0;
232}
233
234/**
235 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
236 * @fn_list: feature-negotiation list to add to
237 * @feat: one of %dccp_feature_numbers
238 * @local: whether local (1) or remote (0) @feat_num is being confirmed
239 * @fval: pointer to NN/SP value to be inserted or NULL
240 * Returns 0 on success, a Reset code for further processing otherwise.
241 */
242static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
243 dccp_feat_val *fval)
244{
245 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
246
247 if (new == NULL)
248 return DCCP_RESET_CODE_TOO_BUSY;
249
250 new->feat_num = feat;
251 new->is_local = local;
252 new->state = FEAT_STABLE; /* transition in 6.6.2 */
253 new->needs_confirm = 1;
254 new->empty_confirm = (fval == NULL);
255 new->val.nn = 0; /* zeroes the whole structure */
256 if (!new->empty_confirm)
257 new->val = *fval;
258 new->needs_mandatory = 0;
259
260 return 0;
261}
262
263static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
264{
265 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
266}
267
268static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
269{
270 list_del(&entry->node);
271 dccp_feat_entry_destructor(entry);
272}
273
274void dccp_feat_list_purge(struct list_head *fn_list)
275{
276 struct dccp_feat_entry *entry, *next;
277
278 list_for_each_entry_safe(entry, next, fn_list, node)
279 dccp_feat_entry_destructor(entry);
280 INIT_LIST_HEAD(fn_list);
281}
282EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
283
Gerrit Renker828755c2008-09-04 07:30:19 +0200284/* generate @to as full clone of @from - @to must not contain any nodes */
285int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
286{
287 struct dccp_feat_entry *entry, *new;
288
289 INIT_LIST_HEAD(to);
290 list_for_each_entry(entry, from, node) {
291 new = dccp_feat_clone_entry(entry);
292 if (new == NULL)
293 goto cloning_failed;
294 list_add_tail(&new->node, to);
295 }
296 return 0;
297
298cloning_failed:
299 dccp_feat_list_purge(to);
300 return -ENOMEM;
301}
302
Gerrit Renker86349c82008-09-04 07:30:19 +0200303static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
304{
305 switch (feat_num) {
306 case DCCPF_ACK_RATIO:
307 return val <= DCCPF_ACK_RATIO_MAX;
308 case DCCPF_SEQUENCE_WINDOW:
309 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
310 }
311 return 0; /* feature unknown - so we can't tell */
312}
313
314/* check that SP values are within the ranges defined in RFC 4340 */
315static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
316{
317 switch (feat_num) {
318 case DCCPF_CCID:
319 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
320 /* Type-check Boolean feature values: */
321 case DCCPF_SHORT_SEQNOS:
322 case DCCPF_ECN_INCAPABLE:
323 case DCCPF_SEND_ACK_VECTOR:
324 case DCCPF_SEND_NDP_COUNT:
325 case DCCPF_DATA_CHECKSUM:
326 case DCCPF_SEND_LEV_RATE:
327 return val < 2;
328 case DCCPF_MIN_CSUM_COVER:
329 return val < 16;
330 }
331 return 0; /* feature unknown */
332}
333
334static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
335{
336 if (sp_list == NULL || sp_len < 1)
337 return 0;
338 while (sp_len--)
339 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
340 return 0;
341 return 1;
342}
343
344/**
345 * __feat_register_nn - Register new NN value on socket
346 * @fn: feature-negotiation list to register with
347 * @feat: an NN feature from %dccp_feature_numbers
348 * @mandatory: use Mandatory option if 1
349 * @nn_val: value to register (restricted to 4 bytes)
350 * Note that NN features are local by definition (RFC 4340, 6.3.2).
351 */
352static int __feat_register_nn(struct list_head *fn, u8 feat,
353 u8 mandatory, u64 nn_val)
354{
355 dccp_feat_val fval = { .nn = nn_val };
356
357 if (dccp_feat_type(feat) != FEAT_NN ||
358 !dccp_feat_is_valid_nn_val(feat, nn_val))
359 return -EINVAL;
360
361 /* Don't bother with default values, they will be activated anyway. */
362 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
363 return 0;
364
365 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
366}
367
368/**
369 * __feat_register_sp - Register new SP value/list on socket
370 * @fn: feature-negotiation list to register with
371 * @feat: an SP feature from %dccp_feature_numbers
372 * @is_local: whether the local (1) or the remote (0) @feat is meant
373 * @mandatory: use Mandatory option if 1
374 * @sp_val: SP value followed by optional preference list
375 * @sp_len: length of @sp_val in bytes
376 */
377static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
378 u8 mandatory, u8 const *sp_val, u8 sp_len)
379{
380 dccp_feat_val fval;
381
382 if (dccp_feat_type(feat) != FEAT_SP ||
383 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
384 return -EINVAL;
385
Gerrit Renker71bb4952008-09-04 07:30:19 +0200386 /* Avoid negotiating alien CCIDs by only advertising supported ones */
387 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
388 return -EOPNOTSUPP;
389
Gerrit Renker86349c82008-09-04 07:30:19 +0200390 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
391 return -ENOMEM;
392
393 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
394}
395
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800396int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
397 u8 *val, u8 len, gfp_t gfp)
Andrea Bittauafe00252006-03-20 17:43:56 -0800398{
Andrea Bittauafe00252006-03-20 17:43:56 -0800399 struct dccp_opt_pend *opt;
400
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200401 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800402
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200403 if (len > 3) {
Gerrit Renker59348b12006-11-20 18:39:23 -0200404 DCCP_WARN("invalid length %d\n", len);
Chris Wright19443172008-05-05 13:50:24 -0700405 return -EINVAL;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200406 }
407 /* XXX add further sanity checks */
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800408
Andrea Bittauafe00252006-03-20 17:43:56 -0800409 /* check if that feature is already being negotiated */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800410 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800411 /* ok we found a negotiation for this option already */
412 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
413 dccp_pr_debug("Replacing old\n");
414 /* replace */
415 BUG_ON(opt->dccpop_val == NULL);
416 kfree(opt->dccpop_val);
417 opt->dccpop_val = val;
418 opt->dccpop_len = len;
419 opt->dccpop_conf = 0;
420 return 0;
421 }
422 }
423
424 /* negotiation for a new feature */
425 opt = kmalloc(sizeof(*opt), gfp);
426 if (opt == NULL)
427 return -ENOMEM;
428
429 opt->dccpop_type = type;
430 opt->dccpop_feat = feature;
431 opt->dccpop_len = len;
432 opt->dccpop_val = val;
433 opt->dccpop_conf = 0;
434 opt->dccpop_sc = NULL;
435
436 BUG_ON(opt->dccpop_val == NULL);
437
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800438 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800439 return 0;
440}
441
442EXPORT_SYMBOL_GPL(dccp_feat_change);
443
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800444static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
445{
446 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800447 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800448 /* figure out if we are changing our CCID or the peer's */
449 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800450 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800451 struct ccid *new_ccid;
452
453 /* Check if nothing is being changed. */
454 if (ccid_nr == new_ccid_nr)
455 return 0;
456
457 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
458 if (new_ccid == NULL)
459 return -ENOMEM;
460
461 if (rx) {
462 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
463 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800464 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800465 } else {
466 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
467 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800468 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800469 }
470
471 return 0;
472}
473
Andrea Bittauafe00252006-03-20 17:43:56 -0800474static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
475{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200476 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800477
478 switch (feat) {
479 case DCCPF_CCID:
480 return dccp_feat_update_ccid(sk, type, val);
481 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200482 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
483 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800484 break;
485 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800486 return 0;
487}
488
489static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
490 u8 *rpref, u8 rlen)
491{
492 struct dccp_sock *dp = dccp_sk(sk);
493 u8 *spref, slen, *res = NULL;
494 int i, j, rc, agree = 1;
495
496 BUG_ON(rpref == NULL);
497
498 /* check if we are the black sheep */
499 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
500 spref = rpref;
501 slen = rlen;
502 rpref = opt->dccpop_val;
503 rlen = opt->dccpop_len;
504 } else {
505 spref = opt->dccpop_val;
506 slen = opt->dccpop_len;
507 }
508 /*
509 * Now we have server preference list in spref and client preference in
510 * rpref
511 */
512 BUG_ON(spref == NULL);
513 BUG_ON(rpref == NULL);
514
515 /* FIXME sanity check vals */
516
517 /* Are values in any order? XXX Lame "algorithm" here */
Andrea Bittauafe00252006-03-20 17:43:56 -0800518 for (i = 0; i < slen; i++) {
519 for (j = 0; j < rlen; j++) {
520 if (spref[i] == rpref[j]) {
521 res = &spref[i];
522 break;
523 }
524 }
525 if (res)
526 break;
527 }
528
529 /* we didn't agree on anything */
530 if (res == NULL) {
531 /* confirm previous value */
532 switch (opt->dccpop_feat) {
533 case DCCPF_CCID:
534 /* XXX did i get this right? =P */
535 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800536 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800537 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800538 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800539 break;
540
541 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200542 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
543 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800544 return -EFAULT;
545 }
546
547 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
548 agree = 0; /* this is used for mandatory options... */
549 }
550
551 /* need to put result and our preference list */
Andrea Bittauafe00252006-03-20 17:43:56 -0800552 rlen = 1 + opt->dccpop_len;
553 rpref = kmalloc(rlen, GFP_ATOMIC);
554 if (rpref == NULL)
555 return -ENOMEM;
556
557 *rpref = *res;
558 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
559
560 /* put it in the "confirm queue" */
561 if (opt->dccpop_sc == NULL) {
562 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
563 if (opt->dccpop_sc == NULL) {
564 kfree(rpref);
565 return -ENOMEM;
566 }
567 } else {
568 /* recycle the confirm slot */
569 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
570 kfree(opt->dccpop_sc->dccpoc_val);
571 dccp_pr_debug("recycling confirm slot\n");
572 }
573 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
574
575 opt->dccpop_sc->dccpoc_val = rpref;
576 opt->dccpop_sc->dccpoc_len = rlen;
577
578 /* update the option on our side [we are about to send the confirm] */
579 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
580 if (rc) {
581 kfree(opt->dccpop_sc->dccpoc_val);
582 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800583 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800584 return rc;
585 }
586
587 dccp_pr_debug("Will confirm %d\n", *rpref);
588
589 /* say we want to change to X but we just got a confirm X, suppress our
590 * change
591 */
592 if (!opt->dccpop_conf) {
593 if (*opt->dccpop_val == *res)
594 opt->dccpop_conf = 1;
595 dccp_pr_debug("won't ask for change of same feature\n");
596 }
597
598 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
599}
600
601static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
602{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800603 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800604 struct dccp_opt_pend *opt;
605 int rc = 1;
606 u8 t;
607
608 /*
609 * We received a CHANGE. We gotta match it against our own preference
610 * list. If we got a CHANGE_R it means it's a change for us, so we need
611 * to compare our CHANGE_L list.
612 */
613 if (type == DCCPO_CHANGE_L)
614 t = DCCPO_CHANGE_R;
615 else
616 t = DCCPO_CHANGE_L;
617
618 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800619 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800620 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
621 continue;
622
623 /* find the winner from the two preference lists */
624 rc = dccp_feat_reconcile(sk, opt, val, len);
625 break;
626 }
627
628 /* We didn't deal with the change. This can happen if we have no
629 * preference list for the feature. In fact, it just shouldn't
630 * happen---if we understand a feature, we should have a preference list
631 * with at least the default value.
632 */
633 BUG_ON(rc == 1);
634
635 return rc;
636}
637
638static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
639{
640 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800641 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800642 u8 *copy;
643 int rc;
644
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200645 /* NN features must be Change L (sec. 6.3.2) */
646 if (type != DCCPO_CHANGE_L) {
647 dccp_pr_debug("received %s for NN feature %d\n",
648 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800649 return -EFAULT;
650 }
651
652 /* XXX sanity check opt val */
653
654 /* copy option so we can confirm it */
655 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
656 if (opt == NULL)
657 return -ENOMEM;
658
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200659 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800660 if (copy == NULL) {
661 kfree(opt);
662 return -ENOMEM;
663 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800664
665 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
666 opt->dccpop_feat = feature;
667 opt->dccpop_val = copy;
668 opt->dccpop_len = len;
669
670 /* change feature */
671 rc = dccp_feat_update(sk, type, feature, *val);
672 if (rc) {
673 kfree(opt->dccpop_val);
674 kfree(opt);
675 return rc;
676 }
677
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200678 dccp_feat_debug(type, feature, *copy);
679
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800680 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800681
682 return 0;
683}
684
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800685static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
686 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800687{
Andrea Bittauafe00252006-03-20 17:43:56 -0800688 /* XXX check if other confirms for that are queued and recycle slot */
689 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
690
691 if (opt == NULL) {
692 /* XXX what do we do? Ignoring should be fine. It's a change
693 * after all =P
694 */
695 return;
696 }
697
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200698 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -0700699 case DCCPO_CHANGE_L:
700 opt->dccpop_type = DCCPO_CONFIRM_R;
701 break;
702 case DCCPO_CHANGE_R:
703 opt->dccpop_type = DCCPO_CONFIRM_L;
704 break;
705 default:
706 DCCP_WARN("invalid type %d\n", type);
707 kfree(opt);
708 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200709 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800710 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -0800711 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800712 opt->dccpop_len = 0;
713
714 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200715 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
716
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800717 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800718}
719
720static void dccp_feat_flush_confirm(struct sock *sk)
721{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800722 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800723 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800724 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800725
726 if (!yes) {
727 struct dccp_opt_pend *opt;
728
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800729 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800730 if (opt->dccpop_conf) {
731 yes = 1;
732 break;
733 }
734 }
735 }
736
737 if (!yes)
738 return;
739
740 /* OK there is something to confirm... */
741 /* XXX check if packet is in flight? Send delayed ack?? */
742 if (sk->sk_state == DCCP_OPEN)
743 dccp_send_ack(sk);
744}
745
746int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
747{
748 int rc;
749
Gerrit Renker5591d282008-09-04 07:30:19 +0200750 /* Ignore Change requests other than during connection setup */
751 if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
752 return 0;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200753 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800754
755 /* figure out if it's SP or NN feature */
756 switch (feature) {
757 /* deal with SP features */
758 case DCCPF_CCID:
759 rc = dccp_feat_sp(sk, type, feature, val, len);
760 break;
761
762 /* deal with NN features */
763 case DCCPF_ACK_RATIO:
764 rc = dccp_feat_nn(sk, type, feature, val, len);
765 break;
766
767 /* XXX implement other features */
768 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200769 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
770 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800771 rc = -EFAULT;
772 break;
773 }
774
775 /* check if there were problems changing features */
776 if (rc) {
777 /* If we don't agree on SP, we sent a confirm for old value.
778 * However we propagate rc to caller in case option was
779 * mandatory
780 */
781 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800782 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800783 }
784
785 /* generate the confirm [if required] */
786 dccp_feat_flush_confirm(sk);
787
788 return rc;
789}
790
791EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
792
793int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
794 u8 *val, u8 len)
795{
796 u8 t;
797 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800798 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200799 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -0800800 int all_confirmed = 1;
801
Gerrit Renker5591d282008-09-04 07:30:19 +0200802 /* Ignore Confirm options other than during connection setup */
803 if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
804 return 0;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200805 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800806
807 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200808 switch (type) {
809 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
810 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200811 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200812 return 1;
813
814 }
815 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -0800816
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800817 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800818 if (!opt->dccpop_conf && opt->dccpop_type == t &&
819 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200820 found = 1;
821 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
822
Andrea Bittauafe00252006-03-20 17:43:56 -0800823 /* XXX do sanity check */
824
825 opt->dccpop_conf = 1;
826
827 /* We got a confirmation---change the option */
828 dccp_feat_update(sk, opt->dccpop_type,
829 opt->dccpop_feat, *val);
830
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200831 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -0800832 break;
833 }
834
835 if (!opt->dccpop_conf)
836 all_confirmed = 0;
837 }
838
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200839 if (!found)
840 dccp_pr_debug("%s(%d, ...) never requested\n",
841 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800842 return 0;
843}
844
845EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
846
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800847void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800848{
Andrea Bittauafe00252006-03-20 17:43:56 -0800849 struct dccp_opt_pend *opt, *next;
850
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800851 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -0800852 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900853 BUG_ON(opt->dccpop_val == NULL);
854 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800855
856 if (opt->dccpop_sc != NULL) {
857 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
858 kfree(opt->dccpop_sc->dccpoc_val);
859 kfree(opt->dccpop_sc);
860 }
861
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900862 kfree(opt);
863 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800864 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800865
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800866 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800867 BUG_ON(opt == NULL);
868 if (opt->dccpop_val != NULL)
869 kfree(opt->dccpop_val);
870 kfree(opt);
871 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800872 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800873}
874
875EXPORT_SYMBOL_GPL(dccp_feat_clean);
876
877/* this is to be called only when a listening sock creates its child. It is
878 * assumed by the function---the confirm is not duplicated, but rather it is
879 * "passed on".
880 */
881int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
882{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800883 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
884 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800885 struct dccp_opt_pend *opt;
886 int rc = 0;
887
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800888 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
889 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800890
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800891 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800892 struct dccp_opt_pend *newopt;
893 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200894 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800895
896 if (val == NULL)
897 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -0800898
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200899 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800900 if (newopt == NULL) {
901 kfree(val);
902 goto out_clean;
903 }
904
905 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -0800906 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800907 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800908
909 /* XXX what happens with backlogs and multiple connections at
910 * once...
911 */
912 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -0800913 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -0800914
915 /* reset state for a new socket */
916 opt->dccpop_conf = 0;
917 }
918
919 /* XXX not doing anything about the conf queue */
920
921out:
922 return rc;
923
924out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800925 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800926 rc = -ENOMEM;
927 goto out;
928}
929
930EXPORT_SYMBOL_GPL(dccp_feat_clone);
931
Gerrit Renker86349c82008-09-04 07:30:19 +0200932int dccp_feat_init(struct sock *sk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800933{
Gerrit Renker86349c82008-09-04 07:30:19 +0200934 struct dccp_sock *dp = dccp_sk(sk);
935 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800936 int rc;
937
Gerrit Renker86349c82008-09-04 07:30:19 +0200938 INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
939 INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
Andrea Bittauafe00252006-03-20 17:43:56 -0800940
941 /* CCID L */
Gerrit Renker86349c82008-09-04 07:30:19 +0200942 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
943 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800944 if (rc)
945 goto out;
946
947 /* CCID R */
Gerrit Renker86349c82008-09-04 07:30:19 +0200948 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
949 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800950 if (rc)
951 goto out;
952
953 /* Ack ratio */
Gerrit Renker86349c82008-09-04 07:30:19 +0200954 rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
955 dmsk->dccpms_ack_ratio);
Andrea Bittauafe00252006-03-20 17:43:56 -0800956out:
957 return rc;
958}
959
960EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200961
962#ifdef CONFIG_IP_DCCP_DEBUG
963const char *dccp_feat_typename(const u8 type)
964{
965 switch(type) {
966 case DCCPO_CHANGE_L: return("ChangeL");
967 case DCCPO_CONFIRM_L: return("ConfirmL");
968 case DCCPO_CHANGE_R: return("ChangeR");
969 case DCCPO_CONFIRM_R: return("ConfirmR");
970 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200971 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200972 }
973 return NULL;
974}
975
976EXPORT_SYMBOL_GPL(dccp_feat_typename);
977
978const char *dccp_feat_name(const u8 feat)
979{
980 static const char *feature_names[] = {
981 [DCCPF_RESERVED] = "Reserved",
982 [DCCPF_CCID] = "CCID",
983 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
984 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
985 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
986 [DCCPF_ACK_RATIO] = "Ack Ratio",
987 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
988 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
989 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
990 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
991 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200992 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
993 return feature_names[DCCPF_RESERVED];
994
Gerrit Renkerb4eec202008-09-04 07:30:19 +0200995 if (feat == DCCPF_SEND_LEV_RATE)
996 return "Send Loss Event Rate";
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200997 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
998 return "CCID-specific";
999
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001000 return feature_names[feat];
1001}
1002
1003EXPORT_SYMBOL_GPL(dccp_feat_name);
1004#endif /* CONFIG_IP_DCCP_DEBUG */