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)
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 int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
64 u8 *val, u8 len, gfp_t gfp)
66 struct dccp_opt_pend *opt;
68 dccp_feat_debug(type, feature, *val);
71 DCCP_WARN("invalid length %d\n", len);
74 /* XXX add further sanity checks */
76 /* check if that feature is already being negotiated */
77 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
78 /* ok we found a negotiation for this option already */
79 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
80 dccp_pr_debug("Replacing old\n");
82 BUG_ON(opt->dccpop_val == NULL);
83 kfree(opt->dccpop_val);
84 opt->dccpop_val = val;
85 opt->dccpop_len = len;
91 /* negotiation for a new feature */
92 opt = kmalloc(sizeof(*opt), gfp);
96 opt->dccpop_type = type;
97 opt->dccpop_feat = feature;
98 opt->dccpop_len = len;
99 opt->dccpop_val = val;
100 opt->dccpop_conf = 0;
101 opt->dccpop_sc = NULL;
103 BUG_ON(opt->dccpop_val == NULL);
105 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
109 EXPORT_SYMBOL_GPL(dccp_feat_change);
111 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
113 struct dccp_sock *dp = dccp_sk(sk);
114 struct dccp_minisock *dmsk = dccp_msk(sk);
115 /* figure out if we are changing our CCID or the peer's */
116 const int rx = type == DCCPO_CHANGE_R;
117 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
118 struct ccid *new_ccid;
120 /* Check if nothing is being changed. */
121 if (ccid_nr == new_ccid_nr)
124 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
125 if (new_ccid == NULL)
129 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
130 dp->dccps_hc_rx_ccid = new_ccid;
131 dmsk->dccpms_rx_ccid = new_ccid_nr;
133 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
134 dp->dccps_hc_tx_ccid = new_ccid;
135 dmsk->dccpms_tx_ccid = new_ccid_nr;
141 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
143 dccp_feat_debug(type, feat, val);
147 return dccp_feat_update_ccid(sk, type, val);
149 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
150 dccp_feat_typename(type), feat);
156 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
159 struct dccp_sock *dp = dccp_sk(sk);
160 u8 *spref, slen, *res = NULL;
161 int i, j, rc, agree = 1;
163 BUG_ON(rpref == NULL);
165 /* check if we are the black sheep */
166 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
169 rpref = opt->dccpop_val;
170 rlen = opt->dccpop_len;
172 spref = opt->dccpop_val;
173 slen = opt->dccpop_len;
176 * Now we have server preference list in spref and client preference in
179 BUG_ON(spref == NULL);
180 BUG_ON(rpref == NULL);
182 /* FIXME sanity check vals */
184 /* Are values in any order? XXX Lame "algorithm" here */
185 for (i = 0; i < slen; i++) {
186 for (j = 0; j < rlen; j++) {
187 if (spref[i] == rpref[j]) {
196 /* we didn't agree on anything */
198 /* confirm previous value */
199 switch (opt->dccpop_feat) {
201 /* XXX did i get this right? =P */
202 if (opt->dccpop_type == DCCPO_CHANGE_L)
203 res = &dccp_msk(sk)->dccpms_tx_ccid;
205 res = &dccp_msk(sk)->dccpms_rx_ccid;
209 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
210 /* XXX implement res */
214 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
215 agree = 0; /* this is used for mandatory options... */
218 /* need to put result and our preference list */
219 rlen = 1 + opt->dccpop_len;
220 rpref = kmalloc(rlen, GFP_ATOMIC);
225 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
227 /* put it in the "confirm queue" */
228 if (opt->dccpop_sc == NULL) {
229 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
230 if (opt->dccpop_sc == NULL) {
235 /* recycle the confirm slot */
236 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
237 kfree(opt->dccpop_sc->dccpoc_val);
238 dccp_pr_debug("recycling confirm slot\n");
240 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
242 opt->dccpop_sc->dccpoc_val = rpref;
243 opt->dccpop_sc->dccpoc_len = rlen;
245 /* update the option on our side [we are about to send the confirm] */
246 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
248 kfree(opt->dccpop_sc->dccpoc_val);
249 kfree(opt->dccpop_sc);
250 opt->dccpop_sc = NULL;
254 dccp_pr_debug("Will confirm %d\n", *rpref);
256 /* say we want to change to X but we just got a confirm X, suppress our
259 if (!opt->dccpop_conf) {
260 if (*opt->dccpop_val == *res)
261 opt->dccpop_conf = 1;
262 dccp_pr_debug("won't ask for change of same feature\n");
265 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
268 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
270 struct dccp_minisock *dmsk = dccp_msk(sk);
271 struct dccp_opt_pend *opt;
276 * We received a CHANGE. We gotta match it against our own preference
277 * list. If we got a CHANGE_R it means it's a change for us, so we need
278 * to compare our CHANGE_L list.
280 if (type == DCCPO_CHANGE_L)
285 /* find our preference list for this feature */
286 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
287 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
290 /* find the winner from the two preference lists */
291 rc = dccp_feat_reconcile(sk, opt, val, len);
295 /* We didn't deal with the change. This can happen if we have no
296 * preference list for the feature. In fact, it just shouldn't
297 * happen---if we understand a feature, we should have a preference list
298 * with at least the default value.
305 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
307 struct dccp_opt_pend *opt;
308 struct dccp_minisock *dmsk = dccp_msk(sk);
312 /* NN features must be Change L (sec. 6.3.2) */
313 if (type != DCCPO_CHANGE_L) {
314 dccp_pr_debug("received %s for NN feature %d\n",
315 dccp_feat_typename(type), feature);
319 /* XXX sanity check opt val */
321 /* copy option so we can confirm it */
322 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
326 copy = kmemdup(val, len, GFP_ATOMIC);
332 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
333 opt->dccpop_feat = feature;
334 opt->dccpop_val = copy;
335 opt->dccpop_len = len;
338 rc = dccp_feat_update(sk, type, feature, *val);
340 kfree(opt->dccpop_val);
345 dccp_feat_debug(type, feature, *copy);
347 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
352 static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
355 /* XXX check if other confirms for that are queued and recycle slot */
356 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
359 /* XXX what do we do? Ignoring should be fine. It's a change
367 opt->dccpop_type = DCCPO_CONFIRM_R;
370 opt->dccpop_type = DCCPO_CONFIRM_L;
373 DCCP_WARN("invalid type %d\n", type);
377 opt->dccpop_feat = feature;
378 opt->dccpop_val = NULL;
382 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
384 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
387 static void dccp_feat_flush_confirm(struct sock *sk)
389 struct dccp_minisock *dmsk = dccp_msk(sk);
390 /* Check if there is anything to confirm in the first place */
391 int yes = !list_empty(&dmsk->dccpms_conf);
394 struct dccp_opt_pend *opt;
396 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
397 if (opt->dccpop_conf) {
407 /* OK there is something to confirm... */
408 /* XXX check if packet is in flight? Send delayed ack?? */
409 if (sk->sk_state == DCCP_OPEN)
413 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
417 dccp_feat_debug(type, feature, *val);
419 /* figure out if it's SP or NN feature */
421 /* deal with SP features */
423 rc = dccp_feat_sp(sk, type, feature, val, len);
426 /* deal with NN features */
427 case DCCPF_ACK_RATIO:
428 rc = dccp_feat_nn(sk, type, feature, val, len);
431 /* XXX implement other features */
433 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
434 dccp_feat_typename(type), feature);
439 /* check if there were problems changing features */
441 /* If we don't agree on SP, we sent a confirm for old value.
442 * However we propagate rc to caller in case option was
445 if (rc != DCCP_FEAT_SP_NOAGREE)
446 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
449 /* generate the confirm [if required] */
450 dccp_feat_flush_confirm(sk);
455 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
457 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
461 struct dccp_opt_pend *opt;
462 struct dccp_minisock *dmsk = dccp_msk(sk);
464 int all_confirmed = 1;
466 dccp_feat_debug(type, feature, *val);
468 /* locate our change request */
470 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
471 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
472 default: DCCP_WARN("invalid type %d\n", type);
476 /* XXX sanity check feature value */
478 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
479 if (!opt->dccpop_conf && opt->dccpop_type == t &&
480 opt->dccpop_feat == feature) {
482 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
484 /* XXX do sanity check */
486 opt->dccpop_conf = 1;
488 /* We got a confirmation---change the option */
489 dccp_feat_update(sk, opt->dccpop_type,
490 opt->dccpop_feat, *val);
492 /* XXX check the return value of dccp_feat_update */
496 if (!opt->dccpop_conf)
500 /* fix re-transmit timer */
501 /* XXX gotta make sure that no option negotiation occurs during
502 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
503 * on. if all options are confirmed it might kill timer which should
504 * remain alive until close is received.
507 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
508 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
512 dccp_pr_debug("%s(%d, ...) never requested\n",
513 dccp_feat_typename(type), feature);
517 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
519 void dccp_feat_clean(struct dccp_minisock *dmsk)
521 struct dccp_opt_pend *opt, *next;
523 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
525 BUG_ON(opt->dccpop_val == NULL);
526 kfree(opt->dccpop_val);
528 if (opt->dccpop_sc != NULL) {
529 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
530 kfree(opt->dccpop_sc->dccpoc_val);
531 kfree(opt->dccpop_sc);
536 INIT_LIST_HEAD(&dmsk->dccpms_pending);
538 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
540 if (opt->dccpop_val != NULL)
541 kfree(opt->dccpop_val);
544 INIT_LIST_HEAD(&dmsk->dccpms_conf);
547 EXPORT_SYMBOL_GPL(dccp_feat_clean);
549 /* this is to be called only when a listening sock creates its child. It is
550 * assumed by the function---the confirm is not duplicated, but rather it is
553 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
555 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
556 struct dccp_minisock *newdmsk = dccp_msk(newsk);
557 struct dccp_opt_pend *opt;
560 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
561 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
563 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
564 struct dccp_opt_pend *newopt;
565 /* copy the value of the option */
566 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
571 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
572 if (newopt == NULL) {
577 /* insert the option */
578 newopt->dccpop_val = val;
579 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
581 /* XXX what happens with backlogs and multiple connections at
584 /* the master socket no longer needs to worry about confirms */
585 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
587 /* reset state for a new socket */
588 opt->dccpop_conf = 0;
591 /* XXX not doing anything about the conf queue */
597 dccp_feat_clean(newdmsk);
602 EXPORT_SYMBOL_GPL(dccp_feat_clone);
604 static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
608 u8 *copy = kmemdup(val, len, GFP_KERNEL);
611 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
618 int dccp_feat_init(struct dccp_minisock *dmsk)
622 INIT_LIST_HEAD(&dmsk->dccpms_pending);
623 INIT_LIST_HEAD(&dmsk->dccpms_conf);
626 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
627 &dmsk->dccpms_tx_ccid, 1);
632 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
633 &dmsk->dccpms_rx_ccid, 1);
638 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
639 &dmsk->dccpms_ack_ratio, 1);
644 EXPORT_SYMBOL_GPL(dccp_feat_init);
646 #ifdef CONFIG_IP_DCCP_DEBUG
647 const char *dccp_feat_typename(const u8 type)
650 case DCCPO_CHANGE_L: return("ChangeL");
651 case DCCPO_CONFIRM_L: return("ConfirmL");
652 case DCCPO_CHANGE_R: return("ChangeR");
653 case DCCPO_CONFIRM_R: return("ConfirmR");
654 /* the following case must not appear in feature negotation */
655 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
660 EXPORT_SYMBOL_GPL(dccp_feat_typename);
662 const char *dccp_feat_name(const u8 feat)
664 static const char *feature_names[] = {
665 [DCCPF_RESERVED] = "Reserved",
666 [DCCPF_CCID] = "CCID",
667 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
668 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
669 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
670 [DCCPF_ACK_RATIO] = "Ack Ratio",
671 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
672 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
673 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
674 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
676 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
677 return feature_names[DCCPF_RESERVED];
679 if (feat == DCCPF_SEND_LEV_RATE)
680 return "Send Loss Event Rate";
681 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
682 return "CCID-specific";
684 return feature_names[feat];
687 EXPORT_SYMBOL_GPL(dccp_feat_name);
688 #endif /* CONFIG_IP_DCCP_DEBUG */