blob: faade82856fe36f931d1114eb84ac7a27354b41f [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 * -----------
9 * o All currently known SP features have 1-byte quantities. If in the future
10 * extensions of RFCs 4340..42 define features with item lengths larger than
11 * one byte, a feature-specific extension of the code will be required.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
Andrea Bittauafe00252006-03-20 17:43:56 -080017 */
18
Andrea Bittauafe00252006-03-20 17:43:56 -080019#include <linux/module.h>
20
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080021#include "ccid.h"
Andrea Bittauafe00252006-03-20 17:43:56 -080022#include "feat.h"
23
24#define DCCP_FEAT_SP_NOAGREE (-123)
25
Gerrit Renkerb4eec202008-09-04 07:30:19 +020026static const struct {
27 u8 feat_num; /* DCCPF_xxx */
28 enum dccp_feat_type rxtx; /* RX or TX */
29 enum dccp_feat_type reconciliation; /* SP or NN */
30 u8 default_value; /* as in 6.4 */
31/*
32 * Lookup table for location and type of features (from RFC 4340/4342)
33 * +--------------------------+----+-----+----+----+---------+-----------+
34 * | Feature | Location | Reconc. | Initial | Section |
35 * | | RX | TX | SP | NN | Value | Reference |
36 * +--------------------------+----+-----+----+----+---------+-----------+
37 * | DCCPF_CCID | | X | X | | 2 | 10 |
38 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
39 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
40 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
41 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
42 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
43 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
44 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
45 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
46 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
47 * +--------------------------+----+-----+----+----+---------+-----------+
48 */
49} dccp_feat_table[] = {
50 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2 },
51 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0 },
52 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100 },
53 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0 },
54 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2 },
55 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0 },
56 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0 },
57 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0 },
58 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0 },
59 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0 },
60};
61#define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
62
63/**
64 * dccp_feat_index - Hash function to map feature number into array position
65 * Returns consecutive array index or -1 if the feature is not understood.
66 */
67static int dccp_feat_index(u8 feat_num)
68{
69 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
70 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
71 return feat_num - 1;
72
73 /*
74 * Other features: add cases for new feature types here after adding
75 * them to the above table.
76 */
77 switch (feat_num) {
78 case DCCPF_SEND_LEV_RATE:
79 return DCCP_FEAT_SUPPORTED_MAX - 1;
80 }
81 return -1;
82}
83
84static u8 dccp_feat_type(u8 feat_num)
85{
86 int idx = dccp_feat_index(feat_num);
87
88 if (idx < 0)
89 return FEAT_UNKNOWN;
90 return dccp_feat_table[idx].reconciliation;
91}
92
93static int dccp_feat_default_value(u8 feat_num)
94{
95 int idx = dccp_feat_index(feat_num);
96
97 return idx < 0 ? : dccp_feat_table[idx].default_value;
98}
99
Gerrit Renker5c7c9452008-09-04 07:30:19 +0200100/* copy constructor, fval must not already contain allocated memory */
101static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
102{
103 fval->sp.len = len;
104 if (fval->sp.len > 0) {
105 fval->sp.vec = kmemdup(val, len, gfp_any());
106 if (fval->sp.vec == NULL) {
107 fval->sp.len = 0;
108 return -ENOBUFS;
109 }
110 }
111 return 0;
112}
113
Gerrit Renkerb4eec202008-09-04 07:30:19 +0200114static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
115{
116 if (unlikely(val == NULL))
117 return;
118 if (dccp_feat_type(feat_num) == FEAT_SP)
119 kfree(val->sp.vec);
120 memset(val, 0, sizeof(*val));
121}
122
123static struct dccp_feat_entry *
124 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
125{
126 struct dccp_feat_entry *new;
127 u8 type = dccp_feat_type(original->feat_num);
128
129 if (type == FEAT_UNKNOWN)
130 return NULL;
131
132 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
133 if (new == NULL)
134 return NULL;
135
136 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
137 original->val.sp.vec,
138 original->val.sp.len)) {
139 kfree(new);
140 return NULL;
141 }
142 return new;
143}
144
145static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
146{
147 if (entry != NULL) {
148 dccp_feat_val_destructor(entry->feat_num, &entry->val);
149 kfree(entry);
150 }
151}
152
Gerrit Renker3001fc02008-09-04 07:30:19 +0200153/*
154 * List management functions
155 *
156 * Feature negotiation lists rely on and maintain the following invariants:
157 * - each feat_num in the list is known, i.e. we know its type and default value
158 * - each feat_num/is_local combination is unique (old entries are overwritten)
159 * - SP values are always freshly allocated
160 * - list is sorted in increasing order of feature number (faster lookup)
161 */
162static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
163 u8 feat_num, bool is_local)
164{
165 struct dccp_feat_entry *entry;
166
167 list_for_each_entry(entry, fn_list, node)
168 if (entry->feat_num == feat_num && entry->is_local == is_local)
169 return entry;
170 else if (entry->feat_num > feat_num)
171 break;
172 return NULL;
173}
174
175/**
176 * dccp_feat_entry_new - Central list update routine (called by all others)
177 * @head: list to add to
178 * @feat: feature number
179 * @local: whether the local (1) or remote feature with number @feat is meant
180 * This is the only constructor and serves to ensure the above invariants.
181 */
182static struct dccp_feat_entry *
183 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
184{
185 struct dccp_feat_entry *entry;
186
187 list_for_each_entry(entry, head, node)
188 if (entry->feat_num == feat && entry->is_local == local) {
189 dccp_feat_val_destructor(entry->feat_num, &entry->val);
190 return entry;
191 } else if (entry->feat_num > feat) {
192 head = &entry->node;
193 break;
194 }
195
196 entry = kmalloc(sizeof(*entry), gfp_any());
197 if (entry != NULL) {
198 entry->feat_num = feat;
199 entry->is_local = local;
200 list_add_tail(&entry->node, head);
201 }
202 return entry;
203}
204
205/**
206 * dccp_feat_push_change - Add/overwrite a Change option in the list
207 * @fn_list: feature-negotiation list to update
208 * @feat: one of %dccp_feature_numbers
209 * @local: whether local (1) or remote (0) @feat_num is meant
210 * @needs_mandatory: whether to use Mandatory feature negotiation options
211 * @fval: pointer to NN/SP value to be inserted (will be copied)
212 */
213static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
214 u8 mandatory, dccp_feat_val *fval)
215{
216 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
217
218 if (new == NULL)
219 return -ENOMEM;
220
221 new->feat_num = feat;
222 new->is_local = local;
223 new->state = FEAT_INITIALISING;
224 new->needs_confirm = 0;
225 new->empty_confirm = 0;
226 new->val = *fval;
227 new->needs_mandatory = mandatory;
228
229 return 0;
230}
231
232/**
233 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
234 * @fn_list: feature-negotiation list to add to
235 * @feat: one of %dccp_feature_numbers
236 * @local: whether local (1) or remote (0) @feat_num is being confirmed
237 * @fval: pointer to NN/SP value to be inserted or NULL
238 * Returns 0 on success, a Reset code for further processing otherwise.
239 */
240static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
241 dccp_feat_val *fval)
242{
243 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
244
245 if (new == NULL)
246 return DCCP_RESET_CODE_TOO_BUSY;
247
248 new->feat_num = feat;
249 new->is_local = local;
250 new->state = FEAT_STABLE; /* transition in 6.6.2 */
251 new->needs_confirm = 1;
252 new->empty_confirm = (fval == NULL);
253 new->val.nn = 0; /* zeroes the whole structure */
254 if (!new->empty_confirm)
255 new->val = *fval;
256 new->needs_mandatory = 0;
257
258 return 0;
259}
260
261static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
262{
263 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
264}
265
266static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
267{
268 list_del(&entry->node);
269 dccp_feat_entry_destructor(entry);
270}
271
272void dccp_feat_list_purge(struct list_head *fn_list)
273{
274 struct dccp_feat_entry *entry, *next;
275
276 list_for_each_entry_safe(entry, next, fn_list, node)
277 dccp_feat_entry_destructor(entry);
278 INIT_LIST_HEAD(fn_list);
279}
280EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
281
Gerrit Renker828755c2008-09-04 07:30:19 +0200282/* generate @to as full clone of @from - @to must not contain any nodes */
283int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
284{
285 struct dccp_feat_entry *entry, *new;
286
287 INIT_LIST_HEAD(to);
288 list_for_each_entry(entry, from, node) {
289 new = dccp_feat_clone_entry(entry);
290 if (new == NULL)
291 goto cloning_failed;
292 list_add_tail(&new->node, to);
293 }
294 return 0;
295
296cloning_failed:
297 dccp_feat_list_purge(to);
298 return -ENOMEM;
299}
300
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800301int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
302 u8 *val, u8 len, gfp_t gfp)
Andrea Bittauafe00252006-03-20 17:43:56 -0800303{
Andrea Bittauafe00252006-03-20 17:43:56 -0800304 struct dccp_opt_pend *opt;
305
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200306 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800307
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200308 if (len > 3) {
Gerrit Renker59348b12006-11-20 18:39:23 -0200309 DCCP_WARN("invalid length %d\n", len);
Chris Wright19443172008-05-05 13:50:24 -0700310 return -EINVAL;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200311 }
312 /* XXX add further sanity checks */
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800313
Andrea Bittauafe00252006-03-20 17:43:56 -0800314 /* check if that feature is already being negotiated */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800315 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800316 /* ok we found a negotiation for this option already */
317 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
318 dccp_pr_debug("Replacing old\n");
319 /* replace */
320 BUG_ON(opt->dccpop_val == NULL);
321 kfree(opt->dccpop_val);
322 opt->dccpop_val = val;
323 opt->dccpop_len = len;
324 opt->dccpop_conf = 0;
325 return 0;
326 }
327 }
328
329 /* negotiation for a new feature */
330 opt = kmalloc(sizeof(*opt), gfp);
331 if (opt == NULL)
332 return -ENOMEM;
333
334 opt->dccpop_type = type;
335 opt->dccpop_feat = feature;
336 opt->dccpop_len = len;
337 opt->dccpop_val = val;
338 opt->dccpop_conf = 0;
339 opt->dccpop_sc = NULL;
340
341 BUG_ON(opt->dccpop_val == NULL);
342
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800343 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800344 return 0;
345}
346
347EXPORT_SYMBOL_GPL(dccp_feat_change);
348
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800349static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
350{
351 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800352 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800353 /* figure out if we are changing our CCID or the peer's */
354 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800355 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800356 struct ccid *new_ccid;
357
358 /* Check if nothing is being changed. */
359 if (ccid_nr == new_ccid_nr)
360 return 0;
361
362 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
363 if (new_ccid == NULL)
364 return -ENOMEM;
365
366 if (rx) {
367 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
368 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800369 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800370 } else {
371 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
372 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800373 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800374 }
375
376 return 0;
377}
378
Andrea Bittauafe00252006-03-20 17:43:56 -0800379static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
380{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200381 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800382
383 switch (feat) {
384 case DCCPF_CCID:
385 return dccp_feat_update_ccid(sk, type, val);
386 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200387 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
388 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800389 break;
390 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800391 return 0;
392}
393
394static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
395 u8 *rpref, u8 rlen)
396{
397 struct dccp_sock *dp = dccp_sk(sk);
398 u8 *spref, slen, *res = NULL;
399 int i, j, rc, agree = 1;
400
401 BUG_ON(rpref == NULL);
402
403 /* check if we are the black sheep */
404 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
405 spref = rpref;
406 slen = rlen;
407 rpref = opt->dccpop_val;
408 rlen = opt->dccpop_len;
409 } else {
410 spref = opt->dccpop_val;
411 slen = opt->dccpop_len;
412 }
413 /*
414 * Now we have server preference list in spref and client preference in
415 * rpref
416 */
417 BUG_ON(spref == NULL);
418 BUG_ON(rpref == NULL);
419
420 /* FIXME sanity check vals */
421
422 /* Are values in any order? XXX Lame "algorithm" here */
Andrea Bittauafe00252006-03-20 17:43:56 -0800423 for (i = 0; i < slen; i++) {
424 for (j = 0; j < rlen; j++) {
425 if (spref[i] == rpref[j]) {
426 res = &spref[i];
427 break;
428 }
429 }
430 if (res)
431 break;
432 }
433
434 /* we didn't agree on anything */
435 if (res == NULL) {
436 /* confirm previous value */
437 switch (opt->dccpop_feat) {
438 case DCCPF_CCID:
439 /* XXX did i get this right? =P */
440 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800441 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800442 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800443 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800444 break;
445
446 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200447 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
448 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800449 return -EFAULT;
450 }
451
452 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
453 agree = 0; /* this is used for mandatory options... */
454 }
455
456 /* need to put result and our preference list */
Andrea Bittauafe00252006-03-20 17:43:56 -0800457 rlen = 1 + opt->dccpop_len;
458 rpref = kmalloc(rlen, GFP_ATOMIC);
459 if (rpref == NULL)
460 return -ENOMEM;
461
462 *rpref = *res;
463 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
464
465 /* put it in the "confirm queue" */
466 if (opt->dccpop_sc == NULL) {
467 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
468 if (opt->dccpop_sc == NULL) {
469 kfree(rpref);
470 return -ENOMEM;
471 }
472 } else {
473 /* recycle the confirm slot */
474 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
475 kfree(opt->dccpop_sc->dccpoc_val);
476 dccp_pr_debug("recycling confirm slot\n");
477 }
478 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
479
480 opt->dccpop_sc->dccpoc_val = rpref;
481 opt->dccpop_sc->dccpoc_len = rlen;
482
483 /* update the option on our side [we are about to send the confirm] */
484 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
485 if (rc) {
486 kfree(opt->dccpop_sc->dccpoc_val);
487 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800488 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800489 return rc;
490 }
491
492 dccp_pr_debug("Will confirm %d\n", *rpref);
493
494 /* say we want to change to X but we just got a confirm X, suppress our
495 * change
496 */
497 if (!opt->dccpop_conf) {
498 if (*opt->dccpop_val == *res)
499 opt->dccpop_conf = 1;
500 dccp_pr_debug("won't ask for change of same feature\n");
501 }
502
503 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
504}
505
506static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
507{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800508 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800509 struct dccp_opt_pend *opt;
510 int rc = 1;
511 u8 t;
512
513 /*
514 * We received a CHANGE. We gotta match it against our own preference
515 * list. If we got a CHANGE_R it means it's a change for us, so we need
516 * to compare our CHANGE_L list.
517 */
518 if (type == DCCPO_CHANGE_L)
519 t = DCCPO_CHANGE_R;
520 else
521 t = DCCPO_CHANGE_L;
522
523 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800524 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800525 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
526 continue;
527
528 /* find the winner from the two preference lists */
529 rc = dccp_feat_reconcile(sk, opt, val, len);
530 break;
531 }
532
533 /* We didn't deal with the change. This can happen if we have no
534 * preference list for the feature. In fact, it just shouldn't
535 * happen---if we understand a feature, we should have a preference list
536 * with at least the default value.
537 */
538 BUG_ON(rc == 1);
539
540 return rc;
541}
542
543static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
544{
545 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800546 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800547 u8 *copy;
548 int rc;
549
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200550 /* NN features must be Change L (sec. 6.3.2) */
551 if (type != DCCPO_CHANGE_L) {
552 dccp_pr_debug("received %s for NN feature %d\n",
553 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800554 return -EFAULT;
555 }
556
557 /* XXX sanity check opt val */
558
559 /* copy option so we can confirm it */
560 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
561 if (opt == NULL)
562 return -ENOMEM;
563
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200564 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800565 if (copy == NULL) {
566 kfree(opt);
567 return -ENOMEM;
568 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800569
570 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
571 opt->dccpop_feat = feature;
572 opt->dccpop_val = copy;
573 opt->dccpop_len = len;
574
575 /* change feature */
576 rc = dccp_feat_update(sk, type, feature, *val);
577 if (rc) {
578 kfree(opt->dccpop_val);
579 kfree(opt);
580 return rc;
581 }
582
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200583 dccp_feat_debug(type, feature, *copy);
584
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800585 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800586
587 return 0;
588}
589
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800590static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
591 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800592{
Andrea Bittauafe00252006-03-20 17:43:56 -0800593 /* XXX check if other confirms for that are queued and recycle slot */
594 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
595
596 if (opt == NULL) {
597 /* XXX what do we do? Ignoring should be fine. It's a change
598 * after all =P
599 */
600 return;
601 }
602
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200603 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -0700604 case DCCPO_CHANGE_L:
605 opt->dccpop_type = DCCPO_CONFIRM_R;
606 break;
607 case DCCPO_CHANGE_R:
608 opt->dccpop_type = DCCPO_CONFIRM_L;
609 break;
610 default:
611 DCCP_WARN("invalid type %d\n", type);
612 kfree(opt);
613 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200614 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800615 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -0800616 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800617 opt->dccpop_len = 0;
618
619 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200620 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
621
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800622 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800623}
624
625static void dccp_feat_flush_confirm(struct sock *sk)
626{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800627 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800628 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800629 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800630
631 if (!yes) {
632 struct dccp_opt_pend *opt;
633
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800634 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800635 if (opt->dccpop_conf) {
636 yes = 1;
637 break;
638 }
639 }
640 }
641
642 if (!yes)
643 return;
644
645 /* OK there is something to confirm... */
646 /* XXX check if packet is in flight? Send delayed ack?? */
647 if (sk->sk_state == DCCP_OPEN)
648 dccp_send_ack(sk);
649}
650
651int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
652{
653 int rc;
654
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200655 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800656
657 /* figure out if it's SP or NN feature */
658 switch (feature) {
659 /* deal with SP features */
660 case DCCPF_CCID:
661 rc = dccp_feat_sp(sk, type, feature, val, len);
662 break;
663
664 /* deal with NN features */
665 case DCCPF_ACK_RATIO:
666 rc = dccp_feat_nn(sk, type, feature, val, len);
667 break;
668
669 /* XXX implement other features */
670 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200671 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
672 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800673 rc = -EFAULT;
674 break;
675 }
676
677 /* check if there were problems changing features */
678 if (rc) {
679 /* If we don't agree on SP, we sent a confirm for old value.
680 * However we propagate rc to caller in case option was
681 * mandatory
682 */
683 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800684 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800685 }
686
687 /* generate the confirm [if required] */
688 dccp_feat_flush_confirm(sk);
689
690 return rc;
691}
692
693EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
694
695int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
696 u8 *val, u8 len)
697{
698 u8 t;
699 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800700 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200701 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -0800702 int all_confirmed = 1;
703
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200704 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800705
706 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200707 switch (type) {
708 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
709 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200710 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200711 return 1;
712
713 }
714 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -0800715
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800716 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800717 if (!opt->dccpop_conf && opt->dccpop_type == t &&
718 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200719 found = 1;
720 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
721
Andrea Bittauafe00252006-03-20 17:43:56 -0800722 /* XXX do sanity check */
723
724 opt->dccpop_conf = 1;
725
726 /* We got a confirmation---change the option */
727 dccp_feat_update(sk, opt->dccpop_type,
728 opt->dccpop_feat, *val);
729
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200730 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -0800731 break;
732 }
733
734 if (!opt->dccpop_conf)
735 all_confirmed = 0;
736 }
737
738 /* fix re-transmit timer */
739 /* XXX gotta make sure that no option negotiation occurs during
740 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
741 * on. if all options are confirmed it might kill timer which should
742 * remain alive until close is received.
743 */
744 if (all_confirmed) {
745 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
746 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
747 }
748
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200749 if (!found)
750 dccp_pr_debug("%s(%d, ...) never requested\n",
751 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800752 return 0;
753}
754
755EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
756
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800757void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800758{
Andrea Bittauafe00252006-03-20 17:43:56 -0800759 struct dccp_opt_pend *opt, *next;
760
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800761 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -0800762 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900763 BUG_ON(opt->dccpop_val == NULL);
764 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800765
766 if (opt->dccpop_sc != NULL) {
767 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
768 kfree(opt->dccpop_sc->dccpoc_val);
769 kfree(opt->dccpop_sc);
770 }
771
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900772 kfree(opt);
773 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800774 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800775
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800776 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800777 BUG_ON(opt == NULL);
778 if (opt->dccpop_val != NULL)
779 kfree(opt->dccpop_val);
780 kfree(opt);
781 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800782 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800783}
784
785EXPORT_SYMBOL_GPL(dccp_feat_clean);
786
787/* this is to be called only when a listening sock creates its child. It is
788 * assumed by the function---the confirm is not duplicated, but rather it is
789 * "passed on".
790 */
791int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
792{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800793 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
794 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800795 struct dccp_opt_pend *opt;
796 int rc = 0;
797
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800798 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
799 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800800
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800801 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800802 struct dccp_opt_pend *newopt;
803 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200804 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800805
806 if (val == NULL)
807 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -0800808
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200809 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800810 if (newopt == NULL) {
811 kfree(val);
812 goto out_clean;
813 }
814
815 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -0800816 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800817 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800818
819 /* XXX what happens with backlogs and multiple connections at
820 * once...
821 */
822 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -0800823 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -0800824
825 /* reset state for a new socket */
826 opt->dccpop_conf = 0;
827 }
828
829 /* XXX not doing anything about the conf queue */
830
831out:
832 return rc;
833
834out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800835 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800836 rc = -ENOMEM;
837 goto out;
838}
839
840EXPORT_SYMBOL_GPL(dccp_feat_clone);
841
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800842static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
843 u8 *val, u8 len)
Andrea Bittauafe00252006-03-20 17:43:56 -0800844{
845 int rc = -ENOMEM;
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200846 u8 *copy = kmemdup(val, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800847
848 if (copy != NULL) {
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800849 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800850 if (rc)
851 kfree(copy);
852 }
853 return rc;
854}
855
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800856int dccp_feat_init(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800857{
Andrea Bittauafe00252006-03-20 17:43:56 -0800858 int rc;
859
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800860 INIT_LIST_HEAD(&dmsk->dccpms_pending);
861 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800862
863 /* CCID L */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800864 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800865 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800866 if (rc)
867 goto out;
868
869 /* CCID R */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800870 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800871 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800872 if (rc)
873 goto out;
874
875 /* Ack ratio */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800876 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800877 &dmsk->dccpms_ack_ratio, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800878out:
879 return rc;
880}
881
882EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200883
884#ifdef CONFIG_IP_DCCP_DEBUG
885const char *dccp_feat_typename(const u8 type)
886{
887 switch(type) {
888 case DCCPO_CHANGE_L: return("ChangeL");
889 case DCCPO_CONFIRM_L: return("ConfirmL");
890 case DCCPO_CHANGE_R: return("ChangeR");
891 case DCCPO_CONFIRM_R: return("ConfirmR");
892 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200893 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200894 }
895 return NULL;
896}
897
898EXPORT_SYMBOL_GPL(dccp_feat_typename);
899
900const char *dccp_feat_name(const u8 feat)
901{
902 static const char *feature_names[] = {
903 [DCCPF_RESERVED] = "Reserved",
904 [DCCPF_CCID] = "CCID",
905 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
906 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
907 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
908 [DCCPF_ACK_RATIO] = "Ack Ratio",
909 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
910 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
911 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
912 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
913 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200914 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
915 return feature_names[DCCPF_RESERVED];
916
Gerrit Renkerb4eec202008-09-04 07:30:19 +0200917 if (feat == DCCPF_SEND_LEV_RATE)
918 return "Send Loss Event Rate";
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200919 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
920 return "CCID-specific";
921
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200922 return feature_names[feat];
923}
924
925EXPORT_SYMBOL_GPL(dccp_feat_name);
926#endif /* CONFIG_IP_DCCP_DEBUG */