4 * An implementation of the DCCP protocol
5 * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
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>
24 #define DCCP_FEAT_SP_NOAGREE (-123)
26 /* copy constructor, fval must not already contain allocated memory */
27 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
30 if (fval->sp.len > 0) {
31 fval->sp.vec = kmemdup(val, len, gfp_any());
32 if (fval->sp.vec == NULL) {
40 int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
41 u8 *val, u8 len, gfp_t gfp)
43 struct dccp_opt_pend *opt;
45 dccp_feat_debug(type, feature, *val);
48 DCCP_WARN("invalid length %d\n", len);
51 /* XXX add further sanity checks */
53 /* check if that feature is already being negotiated */
54 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
55 /* ok we found a negotiation for this option already */
56 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
57 dccp_pr_debug("Replacing old\n");
59 BUG_ON(opt->dccpop_val == NULL);
60 kfree(opt->dccpop_val);
61 opt->dccpop_val = val;
62 opt->dccpop_len = len;
68 /* negotiation for a new feature */
69 opt = kmalloc(sizeof(*opt), gfp);
73 opt->dccpop_type = type;
74 opt->dccpop_feat = feature;
75 opt->dccpop_len = len;
76 opt->dccpop_val = val;
78 opt->dccpop_sc = NULL;
80 BUG_ON(opt->dccpop_val == NULL);
82 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
86 EXPORT_SYMBOL_GPL(dccp_feat_change);
88 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
90 struct dccp_sock *dp = dccp_sk(sk);
91 struct dccp_minisock *dmsk = dccp_msk(sk);
92 /* figure out if we are changing our CCID or the peer's */
93 const int rx = type == DCCPO_CHANGE_R;
94 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
95 struct ccid *new_ccid;
97 /* Check if nothing is being changed. */
98 if (ccid_nr == new_ccid_nr)
101 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
102 if (new_ccid == NULL)
106 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
107 dp->dccps_hc_rx_ccid = new_ccid;
108 dmsk->dccpms_rx_ccid = new_ccid_nr;
110 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
111 dp->dccps_hc_tx_ccid = new_ccid;
112 dmsk->dccpms_tx_ccid = new_ccid_nr;
118 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
120 dccp_feat_debug(type, feat, val);
124 return dccp_feat_update_ccid(sk, type, val);
126 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
127 dccp_feat_typename(type), feat);
133 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
136 struct dccp_sock *dp = dccp_sk(sk);
137 u8 *spref, slen, *res = NULL;
138 int i, j, rc, agree = 1;
140 BUG_ON(rpref == NULL);
142 /* check if we are the black sheep */
143 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
146 rpref = opt->dccpop_val;
147 rlen = opt->dccpop_len;
149 spref = opt->dccpop_val;
150 slen = opt->dccpop_len;
153 * Now we have server preference list in spref and client preference in
156 BUG_ON(spref == NULL);
157 BUG_ON(rpref == NULL);
159 /* FIXME sanity check vals */
161 /* Are values in any order? XXX Lame "algorithm" here */
162 for (i = 0; i < slen; i++) {
163 for (j = 0; j < rlen; j++) {
164 if (spref[i] == rpref[j]) {
173 /* we didn't agree on anything */
175 /* confirm previous value */
176 switch (opt->dccpop_feat) {
178 /* XXX did i get this right? =P */
179 if (opt->dccpop_type == DCCPO_CHANGE_L)
180 res = &dccp_msk(sk)->dccpms_tx_ccid;
182 res = &dccp_msk(sk)->dccpms_rx_ccid;
186 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
187 /* XXX implement res */
191 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
192 agree = 0; /* this is used for mandatory options... */
195 /* need to put result and our preference list */
196 rlen = 1 + opt->dccpop_len;
197 rpref = kmalloc(rlen, GFP_ATOMIC);
202 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
204 /* put it in the "confirm queue" */
205 if (opt->dccpop_sc == NULL) {
206 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
207 if (opt->dccpop_sc == NULL) {
212 /* recycle the confirm slot */
213 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
214 kfree(opt->dccpop_sc->dccpoc_val);
215 dccp_pr_debug("recycling confirm slot\n");
217 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
219 opt->dccpop_sc->dccpoc_val = rpref;
220 opt->dccpop_sc->dccpoc_len = rlen;
222 /* update the option on our side [we are about to send the confirm] */
223 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
225 kfree(opt->dccpop_sc->dccpoc_val);
226 kfree(opt->dccpop_sc);
227 opt->dccpop_sc = NULL;
231 dccp_pr_debug("Will confirm %d\n", *rpref);
233 /* say we want to change to X but we just got a confirm X, suppress our
236 if (!opt->dccpop_conf) {
237 if (*opt->dccpop_val == *res)
238 opt->dccpop_conf = 1;
239 dccp_pr_debug("won't ask for change of same feature\n");
242 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
245 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
247 struct dccp_minisock *dmsk = dccp_msk(sk);
248 struct dccp_opt_pend *opt;
253 * We received a CHANGE. We gotta match it against our own preference
254 * list. If we got a CHANGE_R it means it's a change for us, so we need
255 * to compare our CHANGE_L list.
257 if (type == DCCPO_CHANGE_L)
262 /* find our preference list for this feature */
263 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
264 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
267 /* find the winner from the two preference lists */
268 rc = dccp_feat_reconcile(sk, opt, val, len);
272 /* We didn't deal with the change. This can happen if we have no
273 * preference list for the feature. In fact, it just shouldn't
274 * happen---if we understand a feature, we should have a preference list
275 * with at least the default value.
282 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
284 struct dccp_opt_pend *opt;
285 struct dccp_minisock *dmsk = dccp_msk(sk);
289 /* NN features must be Change L (sec. 6.3.2) */
290 if (type != DCCPO_CHANGE_L) {
291 dccp_pr_debug("received %s for NN feature %d\n",
292 dccp_feat_typename(type), feature);
296 /* XXX sanity check opt val */
298 /* copy option so we can confirm it */
299 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
303 copy = kmemdup(val, len, GFP_ATOMIC);
309 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
310 opt->dccpop_feat = feature;
311 opt->dccpop_val = copy;
312 opt->dccpop_len = len;
315 rc = dccp_feat_update(sk, type, feature, *val);
317 kfree(opt->dccpop_val);
322 dccp_feat_debug(type, feature, *copy);
324 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
329 static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
332 /* XXX check if other confirms for that are queued and recycle slot */
333 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
336 /* XXX what do we do? Ignoring should be fine. It's a change
344 opt->dccpop_type = DCCPO_CONFIRM_R;
347 opt->dccpop_type = DCCPO_CONFIRM_L;
350 DCCP_WARN("invalid type %d\n", type);
354 opt->dccpop_feat = feature;
355 opt->dccpop_val = NULL;
359 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
361 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
364 static void dccp_feat_flush_confirm(struct sock *sk)
366 struct dccp_minisock *dmsk = dccp_msk(sk);
367 /* Check if there is anything to confirm in the first place */
368 int yes = !list_empty(&dmsk->dccpms_conf);
371 struct dccp_opt_pend *opt;
373 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
374 if (opt->dccpop_conf) {
384 /* OK there is something to confirm... */
385 /* XXX check if packet is in flight? Send delayed ack?? */
386 if (sk->sk_state == DCCP_OPEN)
390 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
394 dccp_feat_debug(type, feature, *val);
396 /* figure out if it's SP or NN feature */
398 /* deal with SP features */
400 rc = dccp_feat_sp(sk, type, feature, val, len);
403 /* deal with NN features */
404 case DCCPF_ACK_RATIO:
405 rc = dccp_feat_nn(sk, type, feature, val, len);
408 /* XXX implement other features */
410 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
411 dccp_feat_typename(type), feature);
416 /* check if there were problems changing features */
418 /* If we don't agree on SP, we sent a confirm for old value.
419 * However we propagate rc to caller in case option was
422 if (rc != DCCP_FEAT_SP_NOAGREE)
423 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
426 /* generate the confirm [if required] */
427 dccp_feat_flush_confirm(sk);
432 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
434 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
438 struct dccp_opt_pend *opt;
439 struct dccp_minisock *dmsk = dccp_msk(sk);
441 int all_confirmed = 1;
443 dccp_feat_debug(type, feature, *val);
445 /* locate our change request */
447 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
448 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
449 default: DCCP_WARN("invalid type %d\n", type);
453 /* XXX sanity check feature value */
455 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
456 if (!opt->dccpop_conf && opt->dccpop_type == t &&
457 opt->dccpop_feat == feature) {
459 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
461 /* XXX do sanity check */
463 opt->dccpop_conf = 1;
465 /* We got a confirmation---change the option */
466 dccp_feat_update(sk, opt->dccpop_type,
467 opt->dccpop_feat, *val);
469 /* XXX check the return value of dccp_feat_update */
473 if (!opt->dccpop_conf)
477 /* fix re-transmit timer */
478 /* XXX gotta make sure that no option negotiation occurs during
479 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
480 * on. if all options are confirmed it might kill timer which should
481 * remain alive until close is received.
484 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
485 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
489 dccp_pr_debug("%s(%d, ...) never requested\n",
490 dccp_feat_typename(type), feature);
494 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
496 void dccp_feat_clean(struct dccp_minisock *dmsk)
498 struct dccp_opt_pend *opt, *next;
500 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
502 BUG_ON(opt->dccpop_val == NULL);
503 kfree(opt->dccpop_val);
505 if (opt->dccpop_sc != NULL) {
506 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
507 kfree(opt->dccpop_sc->dccpoc_val);
508 kfree(opt->dccpop_sc);
513 INIT_LIST_HEAD(&dmsk->dccpms_pending);
515 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
517 if (opt->dccpop_val != NULL)
518 kfree(opt->dccpop_val);
521 INIT_LIST_HEAD(&dmsk->dccpms_conf);
524 EXPORT_SYMBOL_GPL(dccp_feat_clean);
526 /* this is to be called only when a listening sock creates its child. It is
527 * assumed by the function---the confirm is not duplicated, but rather it is
530 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
532 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
533 struct dccp_minisock *newdmsk = dccp_msk(newsk);
534 struct dccp_opt_pend *opt;
537 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
538 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
540 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
541 struct dccp_opt_pend *newopt;
542 /* copy the value of the option */
543 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
548 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
549 if (newopt == NULL) {
554 /* insert the option */
555 newopt->dccpop_val = val;
556 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
558 /* XXX what happens with backlogs and multiple connections at
561 /* the master socket no longer needs to worry about confirms */
562 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
564 /* reset state for a new socket */
565 opt->dccpop_conf = 0;
568 /* XXX not doing anything about the conf queue */
574 dccp_feat_clean(newdmsk);
579 EXPORT_SYMBOL_GPL(dccp_feat_clone);
581 static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
585 u8 *copy = kmemdup(val, len, GFP_KERNEL);
588 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
595 int dccp_feat_init(struct dccp_minisock *dmsk)
599 INIT_LIST_HEAD(&dmsk->dccpms_pending);
600 INIT_LIST_HEAD(&dmsk->dccpms_conf);
603 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
604 &dmsk->dccpms_tx_ccid, 1);
609 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
610 &dmsk->dccpms_rx_ccid, 1);
615 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
616 &dmsk->dccpms_ack_ratio, 1);
621 EXPORT_SYMBOL_GPL(dccp_feat_init);
623 #ifdef CONFIG_IP_DCCP_DEBUG
624 const char *dccp_feat_typename(const u8 type)
627 case DCCPO_CHANGE_L: return("ChangeL");
628 case DCCPO_CONFIRM_L: return("ConfirmL");
629 case DCCPO_CHANGE_R: return("ChangeR");
630 case DCCPO_CONFIRM_R: return("ConfirmR");
631 /* the following case must not appear in feature negotation */
632 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
637 EXPORT_SYMBOL_GPL(dccp_feat_typename);
639 const char *dccp_feat_name(const u8 feat)
641 static const char *feature_names[] = {
642 [DCCPF_RESERVED] = "Reserved",
643 [DCCPF_CCID] = "CCID",
644 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
645 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
646 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
647 [DCCPF_ACK_RATIO] = "Ack Ratio",
648 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
649 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
650 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
651 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
653 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
654 return feature_names[DCCPF_RESERVED];
656 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
657 return "CCID-specific";
659 return feature_names[feat];
662 EXPORT_SYMBOL_GPL(dccp_feat_name);
663 #endif /* CONFIG_IP_DCCP_DEBUG */