4 * An implementation of the DCCP protocol
5 * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/module.h>
18 #define DCCP_FEAT_SP_NOAGREE (-123)
20 int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
21 u8 *val, u8 len, gfp_t gfp)
23 struct dccp_opt_pend *opt;
25 dccp_feat_debug(type, feature, *val);
28 DCCP_WARN("invalid length %d\n", len);
31 /* XXX add further sanity checks */
33 /* check if that feature is already being negotiated */
34 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
35 /* ok we found a negotiation for this option already */
36 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
37 dccp_pr_debug("Replacing old\n");
39 BUG_ON(opt->dccpop_val == NULL);
40 kfree(opt->dccpop_val);
41 opt->dccpop_val = val;
42 opt->dccpop_len = len;
48 /* negotiation for a new feature */
49 opt = kmalloc(sizeof(*opt), gfp);
53 opt->dccpop_type = type;
54 opt->dccpop_feat = feature;
55 opt->dccpop_len = len;
56 opt->dccpop_val = val;
58 opt->dccpop_sc = NULL;
60 BUG_ON(opt->dccpop_val == NULL);
62 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
66 EXPORT_SYMBOL_GPL(dccp_feat_change);
68 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
70 struct dccp_sock *dp = dccp_sk(sk);
71 struct dccp_minisock *dmsk = dccp_msk(sk);
72 /* figure out if we are changing our CCID or the peer's */
73 const int rx = type == DCCPO_CHANGE_R;
74 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
75 struct ccid *new_ccid;
77 /* Check if nothing is being changed. */
78 if (ccid_nr == new_ccid_nr)
81 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
86 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
87 dp->dccps_hc_rx_ccid = new_ccid;
88 dmsk->dccpms_rx_ccid = new_ccid_nr;
90 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
91 dp->dccps_hc_tx_ccid = new_ccid;
92 dmsk->dccpms_tx_ccid = new_ccid_nr;
98 /* XXX taking only u8 vals */
99 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
101 dccp_feat_debug(type, feat, val);
105 return dccp_feat_update_ccid(sk, type, val);
107 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
108 dccp_feat_typename(type), feat);
114 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
117 struct dccp_sock *dp = dccp_sk(sk);
118 u8 *spref, slen, *res = NULL;
119 int i, j, rc, agree = 1;
121 BUG_ON(rpref == NULL);
123 /* check if we are the black sheep */
124 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
127 rpref = opt->dccpop_val;
128 rlen = opt->dccpop_len;
130 spref = opt->dccpop_val;
131 slen = opt->dccpop_len;
134 * Now we have server preference list in spref and client preference in
137 BUG_ON(spref == NULL);
138 BUG_ON(rpref == NULL);
140 /* FIXME sanity check vals */
142 /* Are values in any order? XXX Lame "algorithm" here */
143 /* XXX assume values are 1 byte */
144 for (i = 0; i < slen; i++) {
145 for (j = 0; j < rlen; j++) {
146 if (spref[i] == rpref[j]) {
155 /* we didn't agree on anything */
157 /* confirm previous value */
158 switch (opt->dccpop_feat) {
160 /* XXX did i get this right? =P */
161 if (opt->dccpop_type == DCCPO_CHANGE_L)
162 res = &dccp_msk(sk)->dccpms_tx_ccid;
164 res = &dccp_msk(sk)->dccpms_rx_ccid;
168 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
169 /* XXX implement res */
173 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
174 agree = 0; /* this is used for mandatory options... */
177 /* need to put result and our preference list */
178 /* XXX assume 1 byte vals */
179 rlen = 1 + opt->dccpop_len;
180 rpref = kmalloc(rlen, GFP_ATOMIC);
185 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
187 /* put it in the "confirm queue" */
188 if (opt->dccpop_sc == NULL) {
189 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
190 if (opt->dccpop_sc == NULL) {
195 /* recycle the confirm slot */
196 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
197 kfree(opt->dccpop_sc->dccpoc_val);
198 dccp_pr_debug("recycling confirm slot\n");
200 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
202 opt->dccpop_sc->dccpoc_val = rpref;
203 opt->dccpop_sc->dccpoc_len = rlen;
205 /* update the option on our side [we are about to send the confirm] */
206 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
208 kfree(opt->dccpop_sc->dccpoc_val);
209 kfree(opt->dccpop_sc);
210 opt->dccpop_sc = NULL;
214 dccp_pr_debug("Will confirm %d\n", *rpref);
216 /* say we want to change to X but we just got a confirm X, suppress our
219 if (!opt->dccpop_conf) {
220 if (*opt->dccpop_val == *res)
221 opt->dccpop_conf = 1;
222 dccp_pr_debug("won't ask for change of same feature\n");
225 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
228 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
230 struct dccp_minisock *dmsk = dccp_msk(sk);
231 struct dccp_opt_pend *opt;
236 * We received a CHANGE. We gotta match it against our own preference
237 * list. If we got a CHANGE_R it means it's a change for us, so we need
238 * to compare our CHANGE_L list.
240 if (type == DCCPO_CHANGE_L)
245 /* find our preference list for this feature */
246 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
247 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
250 /* find the winner from the two preference lists */
251 rc = dccp_feat_reconcile(sk, opt, val, len);
255 /* We didn't deal with the change. This can happen if we have no
256 * preference list for the feature. In fact, it just shouldn't
257 * happen---if we understand a feature, we should have a preference list
258 * with at least the default value.
265 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
267 struct dccp_opt_pend *opt;
268 struct dccp_minisock *dmsk = dccp_msk(sk);
272 /* NN features must be Change L (sec. 6.3.2) */
273 if (type != DCCPO_CHANGE_L) {
274 dccp_pr_debug("received %s for NN feature %d\n",
275 dccp_feat_typename(type), feature);
279 /* XXX sanity check opt val */
281 /* copy option so we can confirm it */
282 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
286 copy = kmemdup(val, len, GFP_ATOMIC);
292 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
293 opt->dccpop_feat = feature;
294 opt->dccpop_val = copy;
295 opt->dccpop_len = len;
298 rc = dccp_feat_update(sk, type, feature, *val);
300 kfree(opt->dccpop_val);
305 dccp_feat_debug(type, feature, *copy);
307 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
312 static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
315 /* XXX check if other confirms for that are queued and recycle slot */
316 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
319 /* XXX what do we do? Ignoring should be fine. It's a change
327 opt->dccpop_type = DCCPO_CONFIRM_R;
330 opt->dccpop_type = DCCPO_CONFIRM_L;
333 DCCP_WARN("invalid type %d\n", type);
337 opt->dccpop_feat = feature;
338 opt->dccpop_val = NULL;
342 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
344 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
347 static void dccp_feat_flush_confirm(struct sock *sk)
349 struct dccp_minisock *dmsk = dccp_msk(sk);
350 /* Check if there is anything to confirm in the first place */
351 int yes = !list_empty(&dmsk->dccpms_conf);
354 struct dccp_opt_pend *opt;
356 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
357 if (opt->dccpop_conf) {
367 /* OK there is something to confirm... */
368 /* XXX check if packet is in flight? Send delayed ack?? */
369 if (sk->sk_state == DCCP_OPEN)
373 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
377 dccp_feat_debug(type, feature, *val);
379 /* figure out if it's SP or NN feature */
381 /* deal with SP features */
383 rc = dccp_feat_sp(sk, type, feature, val, len);
386 /* deal with NN features */
387 case DCCPF_ACK_RATIO:
388 rc = dccp_feat_nn(sk, type, feature, val, len);
391 /* XXX implement other features */
393 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
394 dccp_feat_typename(type), feature);
399 /* check if there were problems changing features */
401 /* If we don't agree on SP, we sent a confirm for old value.
402 * However we propagate rc to caller in case option was
405 if (rc != DCCP_FEAT_SP_NOAGREE)
406 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
409 /* generate the confirm [if required] */
410 dccp_feat_flush_confirm(sk);
415 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
417 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
421 struct dccp_opt_pend *opt;
422 struct dccp_minisock *dmsk = dccp_msk(sk);
424 int all_confirmed = 1;
426 dccp_feat_debug(type, feature, *val);
428 /* locate our change request */
430 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
431 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
432 default: DCCP_WARN("invalid type %d\n", type);
436 /* XXX sanity check feature value */
438 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
439 if (!opt->dccpop_conf && opt->dccpop_type == t &&
440 opt->dccpop_feat == feature) {
442 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
444 /* XXX do sanity check */
446 opt->dccpop_conf = 1;
448 /* We got a confirmation---change the option */
449 dccp_feat_update(sk, opt->dccpop_type,
450 opt->dccpop_feat, *val);
452 /* XXX check the return value of dccp_feat_update */
456 if (!opt->dccpop_conf)
460 /* fix re-transmit timer */
461 /* XXX gotta make sure that no option negotiation occurs during
462 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
463 * on. if all options are confirmed it might kill timer which should
464 * remain alive until close is received.
467 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
468 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
472 dccp_pr_debug("%s(%d, ...) never requested\n",
473 dccp_feat_typename(type), feature);
477 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
479 void dccp_feat_clean(struct dccp_minisock *dmsk)
481 struct dccp_opt_pend *opt, *next;
483 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
485 BUG_ON(opt->dccpop_val == NULL);
486 kfree(opt->dccpop_val);
488 if (opt->dccpop_sc != NULL) {
489 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
490 kfree(opt->dccpop_sc->dccpoc_val);
491 kfree(opt->dccpop_sc);
496 INIT_LIST_HEAD(&dmsk->dccpms_pending);
498 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
500 if (opt->dccpop_val != NULL)
501 kfree(opt->dccpop_val);
504 INIT_LIST_HEAD(&dmsk->dccpms_conf);
507 EXPORT_SYMBOL_GPL(dccp_feat_clean);
509 /* this is to be called only when a listening sock creates its child. It is
510 * assumed by the function---the confirm is not duplicated, but rather it is
513 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
515 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
516 struct dccp_minisock *newdmsk = dccp_msk(newsk);
517 struct dccp_opt_pend *opt;
520 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
521 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
523 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
524 struct dccp_opt_pend *newopt;
525 /* copy the value of the option */
526 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
531 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
532 if (newopt == NULL) {
537 /* insert the option */
538 newopt->dccpop_val = val;
539 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
541 /* XXX what happens with backlogs and multiple connections at
544 /* the master socket no longer needs to worry about confirms */
545 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
547 /* reset state for a new socket */
548 opt->dccpop_conf = 0;
551 /* XXX not doing anything about the conf queue */
557 dccp_feat_clean(newdmsk);
562 EXPORT_SYMBOL_GPL(dccp_feat_clone);
564 static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
568 u8 *copy = kmemdup(val, len, GFP_KERNEL);
571 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
578 int dccp_feat_init(struct dccp_minisock *dmsk)
582 INIT_LIST_HEAD(&dmsk->dccpms_pending);
583 INIT_LIST_HEAD(&dmsk->dccpms_conf);
586 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
587 &dmsk->dccpms_tx_ccid, 1);
592 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
593 &dmsk->dccpms_rx_ccid, 1);
598 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
599 &dmsk->dccpms_ack_ratio, 1);
604 EXPORT_SYMBOL_GPL(dccp_feat_init);
606 #ifdef CONFIG_IP_DCCP_DEBUG
607 const char *dccp_feat_typename(const u8 type)
610 case DCCPO_CHANGE_L: return("ChangeL");
611 case DCCPO_CONFIRM_L: return("ConfirmL");
612 case DCCPO_CHANGE_R: return("ChangeR");
613 case DCCPO_CONFIRM_R: return("ConfirmR");
614 /* the following case must not appear in feature negotation */
615 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
620 EXPORT_SYMBOL_GPL(dccp_feat_typename);
622 const char *dccp_feat_name(const u8 feat)
624 static const char *feature_names[] = {
625 [DCCPF_RESERVED] = "Reserved",
626 [DCCPF_CCID] = "CCID",
627 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
628 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
629 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
630 [DCCPF_ACK_RATIO] = "Ack Ratio",
631 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
632 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
633 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
634 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
636 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
637 return feature_names[DCCPF_RESERVED];
639 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
640 return "CCID-specific";
642 return feature_names[feat];
645 EXPORT_SYMBOL_GPL(dccp_feat_name);
646 #endif /* CONFIG_IP_DCCP_DEBUG */