]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/dccp/feat.c
dccp: List management for new feature negotiation
[net-next-2.6.git] / net / dccp / feat.c
CommitLineData
afe00251
AB
1/*
2 * net/dccp/feat.c
3 *
4 * An implementation of the DCCP protocol
5 * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
6 *
5cdae198
GR
7 * 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.
afe00251
AB
17 */
18
afe00251
AB
19#include <linux/module.h>
20
6ffd30fb 21#include "ccid.h"
afe00251
AB
22#include "feat.h"
23
24#define DCCP_FEAT_SP_NOAGREE (-123)
25
7d43d1a0
GR
26static 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
61e6473e
GR
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 void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
94{
95 if (unlikely(val == NULL))
96 return;
97 if (dccp_feat_type(feat_num) == FEAT_SP)
98 kfree(val->sp.vec);
99 memset(val, 0, sizeof(*val));
100}
101
102static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
103{
104 if (entry != NULL) {
105 dccp_feat_val_destructor(entry->feat_num, &entry->val);
106 kfree(entry);
107 }
108}
109
110/*
111 * List management functions
112 *
113 * Feature negotiation lists rely on and maintain the following invariants:
114 * - each feat_num in the list is known, i.e. we know its type and default value
115 * - each feat_num/is_local combination is unique (old entries are overwritten)
116 * - SP values are always freshly allocated
117 * - list is sorted in increasing order of feature number (faster lookup)
118 */
119
120static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
121{
122 list_del(&entry->node);
123 dccp_feat_entry_destructor(entry);
124}
125
126void dccp_feat_list_purge(struct list_head *fn_list)
127{
128 struct dccp_feat_entry *entry, *next;
129
130 list_for_each_entry_safe(entry, next, fn_list, node)
131 dccp_feat_entry_destructor(entry);
132 INIT_LIST_HEAD(fn_list);
133}
134EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
135
8ca0d17b
ACM
136int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
137 u8 *val, u8 len, gfp_t gfp)
afe00251 138{
afe00251
AB
139 struct dccp_opt_pend *opt;
140
c02fdc0e 141 dccp_feat_debug(type, feature, *val);
afe00251 142
dd6303df 143 if (len > 3) {
59348b19 144 DCCP_WARN("invalid length %d\n", len);
19443178 145 return -EINVAL;
c02fdc0e
GR
146 }
147 /* XXX add further sanity checks */
6ffd30fb 148
afe00251 149 /* check if that feature is already being negotiated */
a4bf3902 150 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
afe00251
AB
151 /* ok we found a negotiation for this option already */
152 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
153 dccp_pr_debug("Replacing old\n");
154 /* replace */
155 BUG_ON(opt->dccpop_val == NULL);
156 kfree(opt->dccpop_val);
157 opt->dccpop_val = val;
158 opt->dccpop_len = len;
159 opt->dccpop_conf = 0;
160 return 0;
161 }
162 }
163
164 /* negotiation for a new feature */
165 opt = kmalloc(sizeof(*opt), gfp);
166 if (opt == NULL)
167 return -ENOMEM;
168
169 opt->dccpop_type = type;
170 opt->dccpop_feat = feature;
171 opt->dccpop_len = len;
172 opt->dccpop_val = val;
173 opt->dccpop_conf = 0;
174 opt->dccpop_sc = NULL;
175
176 BUG_ON(opt->dccpop_val == NULL);
177
a4bf3902 178 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
afe00251
AB
179 return 0;
180}
181
182EXPORT_SYMBOL_GPL(dccp_feat_change);
183
6ffd30fb
AB
184static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
185{
186 struct dccp_sock *dp = dccp_sk(sk);
a4bf3902 187 struct dccp_minisock *dmsk = dccp_msk(sk);
6ffd30fb
AB
188 /* figure out if we are changing our CCID or the peer's */
189 const int rx = type == DCCPO_CHANGE_R;
a4bf3902 190 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
6ffd30fb
AB
191 struct ccid *new_ccid;
192
193 /* Check if nothing is being changed. */
194 if (ccid_nr == new_ccid_nr)
195 return 0;
196
197 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
198 if (new_ccid == NULL)
199 return -ENOMEM;
200
201 if (rx) {
202 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
203 dp->dccps_hc_rx_ccid = new_ccid;
a4bf3902 204 dmsk->dccpms_rx_ccid = new_ccid_nr;
6ffd30fb
AB
205 } else {
206 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
207 dp->dccps_hc_tx_ccid = new_ccid;
a4bf3902 208 dmsk->dccpms_tx_ccid = new_ccid_nr;
6ffd30fb
AB
209 }
210
211 return 0;
212}
213
afe00251
AB
214static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
215{
c02fdc0e 216 dccp_feat_debug(type, feat, val);
6ffd30fb
AB
217
218 switch (feat) {
219 case DCCPF_CCID:
220 return dccp_feat_update_ccid(sk, type, val);
221 default:
c02fdc0e
GR
222 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
223 dccp_feat_typename(type), feat);
6ffd30fb
AB
224 break;
225 }
afe00251
AB
226 return 0;
227}
228
229static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
230 u8 *rpref, u8 rlen)
231{
232 struct dccp_sock *dp = dccp_sk(sk);
233 u8 *spref, slen, *res = NULL;
234 int i, j, rc, agree = 1;
235
236 BUG_ON(rpref == NULL);
237
238 /* check if we are the black sheep */
239 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
240 spref = rpref;
241 slen = rlen;
242 rpref = opt->dccpop_val;
243 rlen = opt->dccpop_len;
244 } else {
245 spref = opt->dccpop_val;
246 slen = opt->dccpop_len;
247 }
248 /*
249 * Now we have server preference list in spref and client preference in
250 * rpref
251 */
252 BUG_ON(spref == NULL);
253 BUG_ON(rpref == NULL);
254
255 /* FIXME sanity check vals */
256
257 /* Are values in any order? XXX Lame "algorithm" here */
afe00251
AB
258 for (i = 0; i < slen; i++) {
259 for (j = 0; j < rlen; j++) {
260 if (spref[i] == rpref[j]) {
261 res = &spref[i];
262 break;
263 }
264 }
265 if (res)
266 break;
267 }
268
269 /* we didn't agree on anything */
270 if (res == NULL) {
271 /* confirm previous value */
272 switch (opt->dccpop_feat) {
273 case DCCPF_CCID:
274 /* XXX did i get this right? =P */
275 if (opt->dccpop_type == DCCPO_CHANGE_L)
a4bf3902 276 res = &dccp_msk(sk)->dccpms_tx_ccid;
afe00251 277 else
a4bf3902 278 res = &dccp_msk(sk)->dccpms_rx_ccid;
afe00251
AB
279 break;
280
281 default:
59348b19
GR
282 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
283 /* XXX implement res */
afe00251
AB
284 return -EFAULT;
285 }
286
287 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
288 agree = 0; /* this is used for mandatory options... */
289 }
290
291 /* need to put result and our preference list */
afe00251
AB
292 rlen = 1 + opt->dccpop_len;
293 rpref = kmalloc(rlen, GFP_ATOMIC);
294 if (rpref == NULL)
295 return -ENOMEM;
296
297 *rpref = *res;
298 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
299
300 /* put it in the "confirm queue" */
301 if (opt->dccpop_sc == NULL) {
302 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
303 if (opt->dccpop_sc == NULL) {
304 kfree(rpref);
305 return -ENOMEM;
306 }
307 } else {
308 /* recycle the confirm slot */
309 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
310 kfree(opt->dccpop_sc->dccpoc_val);
311 dccp_pr_debug("recycling confirm slot\n");
312 }
313 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
314
315 opt->dccpop_sc->dccpoc_val = rpref;
316 opt->dccpop_sc->dccpoc_len = rlen;
317
318 /* update the option on our side [we are about to send the confirm] */
319 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
320 if (rc) {
321 kfree(opt->dccpop_sc->dccpoc_val);
322 kfree(opt->dccpop_sc);
68907dad 323 opt->dccpop_sc = NULL;
afe00251
AB
324 return rc;
325 }
326
327 dccp_pr_debug("Will confirm %d\n", *rpref);
328
329 /* say we want to change to X but we just got a confirm X, suppress our
330 * change
331 */
332 if (!opt->dccpop_conf) {
333 if (*opt->dccpop_val == *res)
334 opt->dccpop_conf = 1;
335 dccp_pr_debug("won't ask for change of same feature\n");
336 }
337
338 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
339}
340
341static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
342{
a4bf3902 343 struct dccp_minisock *dmsk = dccp_msk(sk);
afe00251
AB
344 struct dccp_opt_pend *opt;
345 int rc = 1;
346 u8 t;
347
348 /*
349 * We received a CHANGE. We gotta match it against our own preference
350 * list. If we got a CHANGE_R it means it's a change for us, so we need
351 * to compare our CHANGE_L list.
352 */
353 if (type == DCCPO_CHANGE_L)
354 t = DCCPO_CHANGE_R;
355 else
356 t = DCCPO_CHANGE_L;
357
358 /* find our preference list for this feature */
a4bf3902 359 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
afe00251
AB
360 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
361 continue;
362
363 /* find the winner from the two preference lists */
364 rc = dccp_feat_reconcile(sk, opt, val, len);
365 break;
366 }
367
368 /* We didn't deal with the change. This can happen if we have no
369 * preference list for the feature. In fact, it just shouldn't
370 * happen---if we understand a feature, we should have a preference list
371 * with at least the default value.
372 */
373 BUG_ON(rc == 1);
374
375 return rc;
376}
377
378static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
379{
380 struct dccp_opt_pend *opt;
a4bf3902 381 struct dccp_minisock *dmsk = dccp_msk(sk);
afe00251
AB
382 u8 *copy;
383 int rc;
384
c02fdc0e
GR
385 /* NN features must be Change L (sec. 6.3.2) */
386 if (type != DCCPO_CHANGE_L) {
387 dccp_pr_debug("received %s for NN feature %d\n",
388 dccp_feat_typename(type), feature);
afe00251
AB
389 return -EFAULT;
390 }
391
392 /* XXX sanity check opt val */
393
394 /* copy option so we can confirm it */
395 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
396 if (opt == NULL)
397 return -ENOMEM;
398
eed73417 399 copy = kmemdup(val, len, GFP_ATOMIC);
afe00251
AB
400 if (copy == NULL) {
401 kfree(opt);
402 return -ENOMEM;
403 }
afe00251
AB
404
405 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
406 opt->dccpop_feat = feature;
407 opt->dccpop_val = copy;
408 opt->dccpop_len = len;
409
410 /* change feature */
411 rc = dccp_feat_update(sk, type, feature, *val);
412 if (rc) {
413 kfree(opt->dccpop_val);
414 kfree(opt);
415 return rc;
416 }
417
c02fdc0e
GR
418 dccp_feat_debug(type, feature, *copy);
419
a4bf3902 420 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
afe00251
AB
421
422 return 0;
423}
424
8ca0d17b
ACM
425static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
426 u8 type, u8 feature)
afe00251 427{
afe00251
AB
428 /* XXX check if other confirms for that are queued and recycle slot */
429 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
430
431 if (opt == NULL) {
432 /* XXX what do we do? Ignoring should be fine. It's a change
433 * after all =P
434 */
435 return;
436 }
437
c02fdc0e 438 switch (type) {
e576de82
JJ
439 case DCCPO_CHANGE_L:
440 opt->dccpop_type = DCCPO_CONFIRM_R;
441 break;
442 case DCCPO_CHANGE_R:
443 opt->dccpop_type = DCCPO_CONFIRM_L;
444 break;
445 default:
446 DCCP_WARN("invalid type %d\n", type);
447 kfree(opt);
448 return;
c02fdc0e 449 }
afe00251 450 opt->dccpop_feat = feature;
68907dad 451 opt->dccpop_val = NULL;
afe00251
AB
452 opt->dccpop_len = 0;
453
454 /* change feature */
c02fdc0e
GR
455 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
456
a4bf3902 457 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
afe00251
AB
458}
459
460static void dccp_feat_flush_confirm(struct sock *sk)
461{
a4bf3902 462 struct dccp_minisock *dmsk = dccp_msk(sk);
afe00251 463 /* Check if there is anything to confirm in the first place */
a4bf3902 464 int yes = !list_empty(&dmsk->dccpms_conf);
afe00251
AB
465
466 if (!yes) {
467 struct dccp_opt_pend *opt;
468
a4bf3902 469 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
afe00251
AB
470 if (opt->dccpop_conf) {
471 yes = 1;
472 break;
473 }
474 }
475 }
476
477 if (!yes)
478 return;
479
480 /* OK there is something to confirm... */
481 /* XXX check if packet is in flight? Send delayed ack?? */
482 if (sk->sk_state == DCCP_OPEN)
483 dccp_send_ack(sk);
484}
485
486int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
487{
488 int rc;
489
c02fdc0e 490 dccp_feat_debug(type, feature, *val);
afe00251
AB
491
492 /* figure out if it's SP or NN feature */
493 switch (feature) {
494 /* deal with SP features */
495 case DCCPF_CCID:
496 rc = dccp_feat_sp(sk, type, feature, val, len);
497 break;
498
499 /* deal with NN features */
500 case DCCPF_ACK_RATIO:
501 rc = dccp_feat_nn(sk, type, feature, val, len);
502 break;
503
504 /* XXX implement other features */
505 default:
c02fdc0e
GR
506 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
507 dccp_feat_typename(type), feature);
afe00251
AB
508 rc = -EFAULT;
509 break;
510 }
511
512 /* check if there were problems changing features */
513 if (rc) {
514 /* If we don't agree on SP, we sent a confirm for old value.
515 * However we propagate rc to caller in case option was
516 * mandatory
517 */
518 if (rc != DCCP_FEAT_SP_NOAGREE)
8ca0d17b 519 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
afe00251
AB
520 }
521
522 /* generate the confirm [if required] */
523 dccp_feat_flush_confirm(sk);
524
525 return rc;
526}
527
528EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
529
530int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
531 u8 *val, u8 len)
532{
533 u8 t;
534 struct dccp_opt_pend *opt;
a4bf3902 535 struct dccp_minisock *dmsk = dccp_msk(sk);
c02fdc0e 536 int found = 0;
afe00251
AB
537 int all_confirmed = 1;
538
c02fdc0e 539 dccp_feat_debug(type, feature, *val);
afe00251
AB
540
541 /* locate our change request */
c02fdc0e
GR
542 switch (type) {
543 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
544 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
8109b02b 545 default: DCCP_WARN("invalid type %d\n", type);
c02fdc0e
GR
546 return 1;
547
548 }
549 /* XXX sanity check feature value */
afe00251 550
a4bf3902 551 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
afe00251
AB
552 if (!opt->dccpop_conf && opt->dccpop_type == t &&
553 opt->dccpop_feat == feature) {
c02fdc0e
GR
554 found = 1;
555 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
556
afe00251
AB
557 /* XXX do sanity check */
558
559 opt->dccpop_conf = 1;
560
561 /* We got a confirmation---change the option */
562 dccp_feat_update(sk, opt->dccpop_type,
563 opt->dccpop_feat, *val);
564
c02fdc0e 565 /* XXX check the return value of dccp_feat_update */
afe00251
AB
566 break;
567 }
568
569 if (!opt->dccpop_conf)
570 all_confirmed = 0;
571 }
572
573 /* fix re-transmit timer */
574 /* XXX gotta make sure that no option negotiation occurs during
575 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
576 * on. if all options are confirmed it might kill timer which should
577 * remain alive until close is received.
578 */
579 if (all_confirmed) {
580 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
581 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
582 }
583
c02fdc0e
GR
584 if (!found)
585 dccp_pr_debug("%s(%d, ...) never requested\n",
586 dccp_feat_typename(type), feature);
afe00251
AB
587 return 0;
588}
589
590EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
591
8ca0d17b 592void dccp_feat_clean(struct dccp_minisock *dmsk)
afe00251 593{
afe00251
AB
594 struct dccp_opt_pend *opt, *next;
595
a4bf3902 596 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
afe00251 597 dccpop_node) {
c9eaf173
YH
598 BUG_ON(opt->dccpop_val == NULL);
599 kfree(opt->dccpop_val);
afe00251
AB
600
601 if (opt->dccpop_sc != NULL) {
602 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
603 kfree(opt->dccpop_sc->dccpoc_val);
604 kfree(opt->dccpop_sc);
605 }
606
c9eaf173
YH
607 kfree(opt);
608 }
a4bf3902 609 INIT_LIST_HEAD(&dmsk->dccpms_pending);
afe00251 610
a4bf3902 611 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
afe00251
AB
612 BUG_ON(opt == NULL);
613 if (opt->dccpop_val != NULL)
614 kfree(opt->dccpop_val);
615 kfree(opt);
616 }
a4bf3902 617 INIT_LIST_HEAD(&dmsk->dccpms_conf);
afe00251
AB
618}
619
620EXPORT_SYMBOL_GPL(dccp_feat_clean);
621
622/* this is to be called only when a listening sock creates its child. It is
623 * assumed by the function---the confirm is not duplicated, but rather it is
624 * "passed on".
625 */
626int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
627{
a4bf3902
ACM
628 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
629 struct dccp_minisock *newdmsk = dccp_msk(newsk);
afe00251
AB
630 struct dccp_opt_pend *opt;
631 int rc = 0;
632
a4bf3902
ACM
633 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
634 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
afe00251 635
a4bf3902 636 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
afe00251
AB
637 struct dccp_opt_pend *newopt;
638 /* copy the value of the option */
eed73417 639 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
afe00251
AB
640
641 if (val == NULL)
642 goto out_clean;
afe00251 643
eed73417 644 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
afe00251
AB
645 if (newopt == NULL) {
646 kfree(val);
647 goto out_clean;
648 }
649
650 /* insert the option */
afe00251 651 newopt->dccpop_val = val;
a4bf3902 652 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
afe00251
AB
653
654 /* XXX what happens with backlogs and multiple connections at
655 * once...
656 */
657 /* the master socket no longer needs to worry about confirms */
68907dad 658 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
afe00251
AB
659
660 /* reset state for a new socket */
661 opt->dccpop_conf = 0;
662 }
663
664 /* XXX not doing anything about the conf queue */
665
666out:
667 return rc;
668
669out_clean:
8ca0d17b 670 dccp_feat_clean(newdmsk);
afe00251
AB
671 rc = -ENOMEM;
672 goto out;
673}
674
675EXPORT_SYMBOL_GPL(dccp_feat_clone);
676
8ca0d17b
ACM
677static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
678 u8 *val, u8 len)
afe00251
AB
679{
680 int rc = -ENOMEM;
eed73417 681 u8 *copy = kmemdup(val, len, GFP_KERNEL);
afe00251
AB
682
683 if (copy != NULL) {
8ca0d17b 684 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
afe00251
AB
685 if (rc)
686 kfree(copy);
687 }
688 return rc;
689}
690
8ca0d17b 691int dccp_feat_init(struct dccp_minisock *dmsk)
afe00251 692{
afe00251
AB
693 int rc;
694
a4bf3902
ACM
695 INIT_LIST_HEAD(&dmsk->dccpms_pending);
696 INIT_LIST_HEAD(&dmsk->dccpms_conf);
afe00251
AB
697
698 /* CCID L */
8ca0d17b 699 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
a4bf3902 700 &dmsk->dccpms_tx_ccid, 1);
afe00251
AB
701 if (rc)
702 goto out;
703
704 /* CCID R */
8ca0d17b 705 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
a4bf3902 706 &dmsk->dccpms_rx_ccid, 1);
afe00251
AB
707 if (rc)
708 goto out;
709
710 /* Ack ratio */
8ca0d17b 711 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
a4bf3902 712 &dmsk->dccpms_ack_ratio, 1);
afe00251
AB
713out:
714 return rc;
715}
716
717EXPORT_SYMBOL_GPL(dccp_feat_init);
c02fdc0e
GR
718
719#ifdef CONFIG_IP_DCCP_DEBUG
720const char *dccp_feat_typename(const u8 type)
721{
722 switch(type) {
723 case DCCPO_CHANGE_L: return("ChangeL");
724 case DCCPO_CONFIRM_L: return("ConfirmL");
725 case DCCPO_CHANGE_R: return("ChangeR");
726 case DCCPO_CONFIRM_R: return("ConfirmR");
727 /* the following case must not appear in feature negotation */
8109b02b 728 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
c02fdc0e
GR
729 }
730 return NULL;
731}
732
733EXPORT_SYMBOL_GPL(dccp_feat_typename);
734
735const char *dccp_feat_name(const u8 feat)
736{
737 static const char *feature_names[] = {
738 [DCCPF_RESERVED] = "Reserved",
739 [DCCPF_CCID] = "CCID",
740 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
741 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
742 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
743 [DCCPF_ACK_RATIO] = "Ack Ratio",
744 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
745 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
746 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
747 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
748 };
dd6303df
GR
749 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
750 return feature_names[DCCPF_RESERVED];
751
7d43d1a0
GR
752 if (feat == DCCPF_SEND_LEV_RATE)
753 return "Send Loss Event Rate";
c02fdc0e
GR
754 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
755 return "CCID-specific";
756
c02fdc0e
GR
757 return feature_names[feat];
758}
759
760EXPORT_SYMBOL_GPL(dccp_feat_name);
761#endif /* CONFIG_IP_DCCP_DEBUG */