dccp: Per-socket initialisation of feature negotiation
[linux-2.6/mini2440.git] / net / dccp / feat.c
blobfaade82856fe36f931d1114eb84ac7a27354b41f
1 /*
2 * net/dccp/feat.c
4 * An implementation of the DCCP protocol
5 * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
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.
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.
19 #include <linux/module.h>
21 #include "ccid.h"
22 #include "feat.h"
24 #define DCCP_FEAT_SP_NOAGREE (-123)
26 static 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 */
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 * +--------------------------+----+-----+----+----+---------+-----------+
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 },
61 #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
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.
67 static int dccp_feat_index(u8 feat_num)
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;
74 * Other features: add cases for new feature types here after adding
75 * them to the above table.
77 switch (feat_num) {
78 case DCCPF_SEND_LEV_RATE:
79 return DCCP_FEAT_SUPPORTED_MAX - 1;
81 return -1;
84 static u8 dccp_feat_type(u8 feat_num)
86 int idx = dccp_feat_index(feat_num);
88 if (idx < 0)
89 return FEAT_UNKNOWN;
90 return dccp_feat_table[idx].reconciliation;
93 static int dccp_feat_default_value(u8 feat_num)
95 int idx = dccp_feat_index(feat_num);
97 return idx < 0 ? : dccp_feat_table[idx].default_value;
100 /* copy constructor, fval must not already contain allocated memory */
101 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
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;
111 return 0;
114 static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
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));
123 static struct dccp_feat_entry *
124 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
126 struct dccp_feat_entry *new;
127 u8 type = dccp_feat_type(original->feat_num);
129 if (type == FEAT_UNKNOWN)
130 return NULL;
132 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
133 if (new == NULL)
134 return NULL;
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;
142 return new;
145 static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
147 if (entry != NULL) {
148 dccp_feat_val_destructor(entry->feat_num, &entry->val);
149 kfree(entry);
154 * List management functions
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)
162 static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
163 u8 feat_num, bool is_local)
165 struct dccp_feat_entry *entry;
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;
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.
182 static struct dccp_feat_entry *
183 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
185 struct dccp_feat_entry *entry;
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;
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);
202 return entry;
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)
213 static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
214 u8 mandatory, dccp_feat_val *fval)
216 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
218 if (new == NULL)
219 return -ENOMEM;
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;
229 return 0;
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.
240 static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
241 dccp_feat_val *fval)
243 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
245 if (new == NULL)
246 return DCCP_RESET_CODE_TOO_BUSY;
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;
258 return 0;
261 static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
263 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
266 static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
268 list_del(&entry->node);
269 dccp_feat_entry_destructor(entry);
272 void dccp_feat_list_purge(struct list_head *fn_list)
274 struct dccp_feat_entry *entry, *next;
276 list_for_each_entry_safe(entry, next, fn_list, node)
277 dccp_feat_entry_destructor(entry);
278 INIT_LIST_HEAD(fn_list);
280 EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
282 /* generate @to as full clone of @from - @to must not contain any nodes */
283 int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
285 struct dccp_feat_entry *entry, *new;
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);
294 return 0;
296 cloning_failed:
297 dccp_feat_list_purge(to);
298 return -ENOMEM;
301 int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
302 u8 *val, u8 len, gfp_t gfp)
304 struct dccp_opt_pend *opt;
306 dccp_feat_debug(type, feature, *val);
308 if (len > 3) {
309 DCCP_WARN("invalid length %d\n", len);
310 return -EINVAL;
312 /* XXX add further sanity checks */
314 /* check if that feature is already being negotiated */
315 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
316 /* 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;
329 /* negotiation for a new feature */
330 opt = kmalloc(sizeof(*opt), gfp);
331 if (opt == NULL)
332 return -ENOMEM;
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;
341 BUG_ON(opt->dccpop_val == NULL);
343 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
344 return 0;
347 EXPORT_SYMBOL_GPL(dccp_feat_change);
349 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
351 struct dccp_sock *dp = dccp_sk(sk);
352 struct dccp_minisock *dmsk = dccp_msk(sk);
353 /* figure out if we are changing our CCID or the peer's */
354 const int rx = type == DCCPO_CHANGE_R;
355 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
356 struct ccid *new_ccid;
358 /* Check if nothing is being changed. */
359 if (ccid_nr == new_ccid_nr)
360 return 0;
362 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
363 if (new_ccid == NULL)
364 return -ENOMEM;
366 if (rx) {
367 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
368 dp->dccps_hc_rx_ccid = new_ccid;
369 dmsk->dccpms_rx_ccid = new_ccid_nr;
370 } else {
371 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
372 dp->dccps_hc_tx_ccid = new_ccid;
373 dmsk->dccpms_tx_ccid = new_ccid_nr;
376 return 0;
379 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
381 dccp_feat_debug(type, feat, val);
383 switch (feat) {
384 case DCCPF_CCID:
385 return dccp_feat_update_ccid(sk, type, val);
386 default:
387 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
388 dccp_feat_typename(type), feat);
389 break;
391 return 0;
394 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
395 u8 *rpref, u8 rlen)
397 struct dccp_sock *dp = dccp_sk(sk);
398 u8 *spref, slen, *res = NULL;
399 int i, j, rc, agree = 1;
401 BUG_ON(rpref == NULL);
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;
414 * Now we have server preference list in spref and client preference in
415 * rpref
417 BUG_ON(spref == NULL);
418 BUG_ON(rpref == NULL);
420 /* FIXME sanity check vals */
422 /* Are values in any order? XXX Lame "algorithm" here */
423 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;
430 if (res)
431 break;
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)
441 res = &dccp_msk(sk)->dccpms_tx_ccid;
442 else
443 res = &dccp_msk(sk)->dccpms_rx_ccid;
444 break;
446 default:
447 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
448 /* XXX implement res */
449 return -EFAULT;
452 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
453 agree = 0; /* this is used for mandatory options... */
456 /* need to put result and our preference list */
457 rlen = 1 + opt->dccpop_len;
458 rpref = kmalloc(rlen, GFP_ATOMIC);
459 if (rpref == NULL)
460 return -ENOMEM;
462 *rpref = *res;
463 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
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;
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");
478 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
480 opt->dccpop_sc->dccpoc_val = rpref;
481 opt->dccpop_sc->dccpoc_len = rlen;
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);
488 opt->dccpop_sc = NULL;
489 return rc;
492 dccp_pr_debug("Will confirm %d\n", *rpref);
494 /* say we want to change to X but we just got a confirm X, suppress our
495 * change
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");
503 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
506 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
508 struct dccp_minisock *dmsk = dccp_msk(sk);
509 struct dccp_opt_pend *opt;
510 int rc = 1;
511 u8 t;
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.
518 if (type == DCCPO_CHANGE_L)
519 t = DCCPO_CHANGE_R;
520 else
521 t = DCCPO_CHANGE_L;
523 /* find our preference list for this feature */
524 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
525 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
526 continue;
528 /* find the winner from the two preference lists */
529 rc = dccp_feat_reconcile(sk, opt, val, len);
530 break;
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.
538 BUG_ON(rc == 1);
540 return rc;
543 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
545 struct dccp_opt_pend *opt;
546 struct dccp_minisock *dmsk = dccp_msk(sk);
547 u8 *copy;
548 int rc;
550 /* 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);
554 return -EFAULT;
557 /* XXX sanity check opt val */
559 /* copy option so we can confirm it */
560 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
561 if (opt == NULL)
562 return -ENOMEM;
564 copy = kmemdup(val, len, GFP_ATOMIC);
565 if (copy == NULL) {
566 kfree(opt);
567 return -ENOMEM;
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;
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;
583 dccp_feat_debug(type, feature, *copy);
585 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
587 return 0;
590 static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
591 u8 type, u8 feature)
593 /* XXX check if other confirms for that are queued and recycle slot */
594 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
596 if (opt == NULL) {
597 /* XXX what do we do? Ignoring should be fine. It's a change
598 * after all =P
600 return;
603 switch (type) {
604 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;
615 opt->dccpop_feat = feature;
616 opt->dccpop_val = NULL;
617 opt->dccpop_len = 0;
619 /* change feature */
620 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
622 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
625 static void dccp_feat_flush_confirm(struct sock *sk)
627 struct dccp_minisock *dmsk = dccp_msk(sk);
628 /* Check if there is anything to confirm in the first place */
629 int yes = !list_empty(&dmsk->dccpms_conf);
631 if (!yes) {
632 struct dccp_opt_pend *opt;
634 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
635 if (opt->dccpop_conf) {
636 yes = 1;
637 break;
642 if (!yes)
643 return;
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);
651 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
653 int rc;
655 dccp_feat_debug(type, feature, *val);
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;
664 /* deal with NN features */
665 case DCCPF_ACK_RATIO:
666 rc = dccp_feat_nn(sk, type, feature, val, len);
667 break;
669 /* XXX implement other features */
670 default:
671 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
672 dccp_feat_typename(type), feature);
673 rc = -EFAULT;
674 break;
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
683 if (rc != DCCP_FEAT_SP_NOAGREE)
684 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
687 /* generate the confirm [if required] */
688 dccp_feat_flush_confirm(sk);
690 return rc;
693 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
695 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
696 u8 *val, u8 len)
698 u8 t;
699 struct dccp_opt_pend *opt;
700 struct dccp_minisock *dmsk = dccp_msk(sk);
701 int found = 0;
702 int all_confirmed = 1;
704 dccp_feat_debug(type, feature, *val);
706 /* locate our change request */
707 switch (type) {
708 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
709 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
710 default: DCCP_WARN("invalid type %d\n", type);
711 return 1;
714 /* XXX sanity check feature value */
716 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
717 if (!opt->dccpop_conf && opt->dccpop_type == t &&
718 opt->dccpop_feat == feature) {
719 found = 1;
720 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
722 /* XXX do sanity check */
724 opt->dccpop_conf = 1;
726 /* We got a confirmation---change the option */
727 dccp_feat_update(sk, opt->dccpop_type,
728 opt->dccpop_feat, *val);
730 /* XXX check the return value of dccp_feat_update */
731 break;
734 if (!opt->dccpop_conf)
735 all_confirmed = 0;
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.
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);
749 if (!found)
750 dccp_pr_debug("%s(%d, ...) never requested\n",
751 dccp_feat_typename(type), feature);
752 return 0;
755 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
757 void dccp_feat_clean(struct dccp_minisock *dmsk)
759 struct dccp_opt_pend *opt, *next;
761 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
762 dccpop_node) {
763 BUG_ON(opt->dccpop_val == NULL);
764 kfree(opt->dccpop_val);
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);
772 kfree(opt);
774 INIT_LIST_HEAD(&dmsk->dccpms_pending);
776 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
777 BUG_ON(opt == NULL);
778 if (opt->dccpop_val != NULL)
779 kfree(opt->dccpop_val);
780 kfree(opt);
782 INIT_LIST_HEAD(&dmsk->dccpms_conf);
785 EXPORT_SYMBOL_GPL(dccp_feat_clean);
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".
791 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
793 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
794 struct dccp_minisock *newdmsk = dccp_msk(newsk);
795 struct dccp_opt_pend *opt;
796 int rc = 0;
798 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
799 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
801 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
802 struct dccp_opt_pend *newopt;
803 /* copy the value of the option */
804 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
806 if (val == NULL)
807 goto out_clean;
809 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
810 if (newopt == NULL) {
811 kfree(val);
812 goto out_clean;
815 /* insert the option */
816 newopt->dccpop_val = val;
817 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
819 /* XXX what happens with backlogs and multiple connections at
820 * once...
822 /* the master socket no longer needs to worry about confirms */
823 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
825 /* reset state for a new socket */
826 opt->dccpop_conf = 0;
829 /* XXX not doing anything about the conf queue */
831 out:
832 return rc;
834 out_clean:
835 dccp_feat_clean(newdmsk);
836 rc = -ENOMEM;
837 goto out;
840 EXPORT_SYMBOL_GPL(dccp_feat_clone);
842 static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
843 u8 *val, u8 len)
845 int rc = -ENOMEM;
846 u8 *copy = kmemdup(val, len, GFP_KERNEL);
848 if (copy != NULL) {
849 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
850 if (rc)
851 kfree(copy);
853 return rc;
856 int dccp_feat_init(struct dccp_minisock *dmsk)
858 int rc;
860 INIT_LIST_HEAD(&dmsk->dccpms_pending);
861 INIT_LIST_HEAD(&dmsk->dccpms_conf);
863 /* CCID L */
864 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
865 &dmsk->dccpms_tx_ccid, 1);
866 if (rc)
867 goto out;
869 /* CCID R */
870 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
871 &dmsk->dccpms_rx_ccid, 1);
872 if (rc)
873 goto out;
875 /* Ack ratio */
876 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
877 &dmsk->dccpms_ack_ratio, 1);
878 out:
879 return rc;
882 EXPORT_SYMBOL_GPL(dccp_feat_init);
884 #ifdef CONFIG_IP_DCCP_DEBUG
885 const char *dccp_feat_typename(const u8 type)
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 */
893 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
895 return NULL;
898 EXPORT_SYMBOL_GPL(dccp_feat_typename);
900 const char *dccp_feat_name(const u8 feat)
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",
914 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
915 return feature_names[DCCPF_RESERVED];
917 if (feat == DCCPF_SEND_LEV_RATE)
918 return "Send Loss Event Rate";
919 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
920 return "CCID-specific";
922 return feature_names[feat];
925 EXPORT_SYMBOL_GPL(dccp_feat_name);
926 #endif /* CONFIG_IP_DCCP_DEBUG */