4 * Feature negotiation for the DCCP protocol (RFC 4340, section 6)
6 * Copyright (c) 2008 Gerrit Renker <gerrit@erg.abdn.ac.uk>
7 * Rewrote from scratch, some bits from earlier code by
8 * Copyright (c) 2005 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
13 * o Feature negotiation is coordinated with connection setup (as in TCP), wild
14 * changes of parameters of an established connection are not supported.
15 * o All currently known SP features have 1-byte quantities. If in the future
16 * extensions of RFCs 4340..42 define features with item lengths larger than
17 * one byte, a feature-specific extension of the code will be required.
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
24 #include <linux/module.h>
28 /* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */
29 unsigned long sysctl_dccp_sequence_window __read_mostly = 100;
30 int sysctl_dccp_rx_ccid __read_mostly = 2,
31 sysctl_dccp_tx_ccid __read_mostly = 2;
34 * Feature activation handlers.
36 * These all use an u64 argument, to provide enough room for NN/SP features. At
37 * this stage the negotiated values have been checked to be within their range.
39 static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
41 struct dccp_sock *dp = dccp_sk(sk);
42 struct ccid *new_ccid = ccid_new(ccid, sk, rx);
48 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
49 dp->dccps_hc_rx_ccid = new_ccid;
51 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
52 dp->dccps_hc_tx_ccid = new_ccid;
57 static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
59 struct dccp_sock *dp = dccp_sk(sk);
62 dp->dccps_r_seq_win = seq_win;
63 /* propagate changes to update SWL/SWH */
64 dccp_update_gsr(sk, dp->dccps_gsr);
66 dp->dccps_l_seq_win = seq_win;
67 /* propagate changes to update AWL */
68 dccp_update_gss(sk, dp->dccps_gss);
73 static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
76 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
78 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
82 static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
84 struct dccp_sock *dp = dccp_sk(sk);
87 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
88 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
89 if (dp->dccps_hc_rx_ackvec == NULL)
92 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
93 dp->dccps_hc_rx_ackvec = NULL;
99 static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
102 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
107 * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
108 * `rx' holds when the sending peer informs about his partial coverage via a
109 * ChangeR() option. In the other case, we are the sender and the receiver
110 * announces its coverage via ChangeL() options. The policy here is to honour
111 * such communication by enabling the corresponding partial coverage - but only
112 * if it has not been set manually before; the warning here means that all
113 * packets will be dropped.
115 static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
117 struct dccp_sock *dp = dccp_sk(sk);
120 dp->dccps_pcrlen = cscov;
122 if (dp->dccps_pcslen == 0)
123 dp->dccps_pcslen = cscov;
124 else if (cscov > dp->dccps_pcslen)
125 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
126 dp->dccps_pcslen, (u8)cscov);
131 static const struct {
132 u8 feat_num; /* DCCPF_xxx */
133 enum dccp_feat_type rxtx; /* RX or TX */
134 enum dccp_feat_type reconciliation; /* SP or NN */
135 u8 default_value; /* as in 6.4 */
136 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
138 * Lookup table for location and type of features (from RFC 4340/4342)
139 * +--------------------------+----+-----+----+----+---------+-----------+
140 * | Feature | Location | Reconc. | Initial | Section |
141 * | | RX | TX | SP | NN | Value | Reference |
142 * +--------------------------+----+-----+----+----+---------+-----------+
143 * | DCCPF_CCID | | X | X | | 2 | 10 |
144 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
145 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
146 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
147 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
148 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
149 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
150 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
151 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
152 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
153 * +--------------------------+----+-----+----+----+---------+-----------+
155 } dccp_feat_table[] = {
156 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
157 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
158 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
159 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
160 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
161 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
162 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
163 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
164 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
165 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
167 #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
170 * dccp_feat_index - Hash function to map feature number into array position
171 * Returns consecutive array index or -1 if the feature is not understood.
173 static int dccp_feat_index(u8 feat_num)
175 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
176 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
180 * Other features: add cases for new feature types here after adding
181 * them to the above table.
184 case DCCPF_SEND_LEV_RATE:
185 return DCCP_FEAT_SUPPORTED_MAX - 1;
190 static u8 dccp_feat_type(u8 feat_num)
192 int idx = dccp_feat_index(feat_num);
196 return dccp_feat_table[idx].reconciliation;
199 static int dccp_feat_default_value(u8 feat_num)
201 int idx = dccp_feat_index(feat_num);
203 * There are no default values for unknown features, so encountering a
204 * negative index here indicates a serious problem somewhere else.
206 DCCP_BUG_ON(idx < 0);
208 return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
212 * Debugging and verbose-printing section
214 static const char *dccp_feat_fname(const u8 feat)
216 static const char *feature_names[] = {
217 [DCCPF_RESERVED] = "Reserved",
218 [DCCPF_CCID] = "CCID",
219 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
220 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
221 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
222 [DCCPF_ACK_RATIO] = "Ack Ratio",
223 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
224 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
225 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
226 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
228 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
229 return feature_names[DCCPF_RESERVED];
231 if (feat == DCCPF_SEND_LEV_RATE)
232 return "Send Loss Event Rate";
233 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
234 return "CCID-specific";
236 return feature_names[feat];
239 static const char *dccp_feat_sname[] = { "DEFAULT", "INITIALISING", "CHANGING",
240 "UNSTABLE", "STABLE" };
242 #ifdef CONFIG_IP_DCCP_DEBUG
243 static const char *dccp_feat_oname(const u8 opt)
246 case DCCPO_CHANGE_L: return "Change_L";
247 case DCCPO_CONFIRM_L: return "Confirm_L";
248 case DCCPO_CHANGE_R: return "Change_R";
249 case DCCPO_CONFIRM_R: return "Confirm_R";
254 static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
256 u8 i, type = dccp_feat_type(feat_num);
258 if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL))
259 dccp_pr_debug_cat("(NULL)");
260 else if (type == FEAT_SP)
261 for (i = 0; i < val->sp.len; i++)
262 dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]);
263 else if (type == FEAT_NN)
264 dccp_pr_debug_cat("%llu", (unsigned long long)val->nn);
266 dccp_pr_debug_cat("unknown type %u", type);
269 static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
271 u8 type = dccp_feat_type(feat_num);
272 dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
275 fval.nn = dccp_decode_value_var(list, len);
276 dccp_feat_printval(feat_num, &fval);
279 static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
281 dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote",
282 dccp_feat_fname(entry->feat_num));
283 dccp_feat_printval(entry->feat_num, &entry->val);
284 dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
285 entry->needs_confirm ? "(Confirm pending)" : "");
288 #define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \
289 dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\
290 dccp_feat_printvals(feat, val, len); \
291 dccp_pr_debug_cat(") %s\n", mandatory ? "!" : ""); } while (0)
293 #define dccp_feat_print_fnlist(fn_list) { \
294 const struct dccp_feat_entry *___entry; \
296 dccp_pr_debug("List Dump:\n"); \
297 list_for_each_entry(___entry, fn_list, node) \
298 dccp_feat_print_entry(___entry); \
300 #else /* ! CONFIG_IP_DCCP_DEBUG */
301 #define dccp_feat_print_opt(opt, feat, val, len, mandatory)
302 #define dccp_feat_print_fnlist(fn_list)
305 static int __dccp_feat_activate(struct sock *sk, const int idx,
306 const bool is_local, dccp_feat_val const *fval)
311 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
313 if (dccp_feat_table[idx].activation_hdlr == NULL)
317 val = dccp_feat_table[idx].default_value;
318 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
319 if (fval->sp.vec == NULL) {
321 * This can happen when an empty Confirm is sent
322 * for an SP (i.e. known) feature. In this case
323 * we would be using the default anyway.
325 DCCP_CRIT("Feature #%d undefined: using default", idx);
326 val = dccp_feat_table[idx].default_value;
328 val = fval->sp.vec[0];
334 /* Location is RX if this is a local-RX or remote-TX feature */
335 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
337 dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
338 dccp_feat_fname(dccp_feat_table[idx].feat_num),
339 fval ? "" : "default ", (unsigned long long)val);
341 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
344 /* Test for "Req'd" feature (RFC 4340, 6.4) */
345 static inline int dccp_feat_must_be_understood(u8 feat_num)
347 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
348 feat_num == DCCPF_SEQUENCE_WINDOW;
351 /* copy constructor, fval must not already contain allocated memory */
352 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
355 if (fval->sp.len > 0) {
356 fval->sp.vec = kmemdup(val, len, gfp_any());
357 if (fval->sp.vec == NULL) {
365 static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
367 if (unlikely(val == NULL))
369 if (dccp_feat_type(feat_num) == FEAT_SP)
371 memset(val, 0, sizeof(*val));
374 static struct dccp_feat_entry *
375 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
377 struct dccp_feat_entry *new;
378 u8 type = dccp_feat_type(original->feat_num);
380 if (type == FEAT_UNKNOWN)
383 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
387 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
388 original->val.sp.vec,
389 original->val.sp.len)) {
396 static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
399 dccp_feat_val_destructor(entry->feat_num, &entry->val);
405 * List management functions
407 * Feature negotiation lists rely on and maintain the following invariants:
408 * - each feat_num in the list is known, i.e. we know its type and default value
409 * - each feat_num/is_local combination is unique (old entries are overwritten)
410 * - SP values are always freshly allocated
411 * - list is sorted in increasing order of feature number (faster lookup)
413 static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
414 u8 feat_num, bool is_local)
416 struct dccp_feat_entry *entry;
418 list_for_each_entry(entry, fn_list, node) {
419 if (entry->feat_num == feat_num && entry->is_local == is_local)
421 else if (entry->feat_num > feat_num)
428 * dccp_feat_entry_new - Central list update routine (called by all others)
429 * @head: list to add to
430 * @feat: feature number
431 * @local: whether the local (1) or remote feature with number @feat is meant
432 * This is the only constructor and serves to ensure the above invariants.
434 static struct dccp_feat_entry *
435 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
437 struct dccp_feat_entry *entry;
439 list_for_each_entry(entry, head, node)
440 if (entry->feat_num == feat && entry->is_local == local) {
441 dccp_feat_val_destructor(entry->feat_num, &entry->val);
443 } else if (entry->feat_num > feat) {
448 entry = kmalloc(sizeof(*entry), gfp_any());
450 entry->feat_num = feat;
451 entry->is_local = local;
452 list_add_tail(&entry->node, head);
458 * dccp_feat_push_change - Add/overwrite a Change option in the list
459 * @fn_list: feature-negotiation list to update
460 * @feat: one of %dccp_feature_numbers
461 * @local: whether local (1) or remote (0) @feat_num is meant
462 * @needs_mandatory: whether to use Mandatory feature negotiation options
463 * @fval: pointer to NN/SP value to be inserted (will be copied)
465 static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
466 u8 mandatory, dccp_feat_val *fval)
468 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
473 new->feat_num = feat;
474 new->is_local = local;
475 new->state = FEAT_INITIALISING;
476 new->needs_confirm = 0;
477 new->empty_confirm = 0;
479 new->needs_mandatory = mandatory;
485 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
486 * @fn_list: feature-negotiation list to add to
487 * @feat: one of %dccp_feature_numbers
488 * @local: whether local (1) or remote (0) @feat_num is being confirmed
489 * @fval: pointer to NN/SP value to be inserted or NULL
490 * Returns 0 on success, a Reset code for further processing otherwise.
492 static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
495 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
498 return DCCP_RESET_CODE_TOO_BUSY;
500 new->feat_num = feat;
501 new->is_local = local;
502 new->state = FEAT_STABLE; /* transition in 6.6.2 */
503 new->needs_confirm = 1;
504 new->empty_confirm = (fval == NULL);
505 new->val.nn = 0; /* zeroes the whole structure */
506 if (!new->empty_confirm)
508 new->needs_mandatory = 0;
513 static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
515 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
518 static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
520 list_del(&entry->node);
521 dccp_feat_entry_destructor(entry);
524 void dccp_feat_list_purge(struct list_head *fn_list)
526 struct dccp_feat_entry *entry, *next;
528 list_for_each_entry_safe(entry, next, fn_list, node)
529 dccp_feat_entry_destructor(entry);
530 INIT_LIST_HEAD(fn_list);
532 EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
534 /* generate @to as full clone of @from - @to must not contain any nodes */
535 int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
537 struct dccp_feat_entry *entry, *new;
540 list_for_each_entry(entry, from, node) {
541 new = dccp_feat_clone_entry(entry);
544 list_add_tail(&new->node, to);
549 dccp_feat_list_purge(to);
554 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
555 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
556 * incoming options are accepted as long as their values are valid.
558 static u8 dccp_feat_valid_nn_length(u8 feat_num)
560 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
562 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
567 static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
570 case DCCPF_ACK_RATIO:
571 return val <= DCCPF_ACK_RATIO_MAX;
572 case DCCPF_SEQUENCE_WINDOW:
573 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
575 return 0; /* feature unknown - so we can't tell */
578 /* check that SP values are within the ranges defined in RFC 4340 */
579 static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
583 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
584 /* Type-check Boolean feature values: */
585 case DCCPF_SHORT_SEQNOS:
586 case DCCPF_ECN_INCAPABLE:
587 case DCCPF_SEND_ACK_VECTOR:
588 case DCCPF_SEND_NDP_COUNT:
589 case DCCPF_DATA_CHECKSUM:
590 case DCCPF_SEND_LEV_RATE:
592 case DCCPF_MIN_CSUM_COVER:
595 return 0; /* feature unknown */
598 static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
600 if (sp_list == NULL || sp_len < 1)
603 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
609 * dccp_feat_insert_opts - Generate FN options from current list state
610 * @skb: next sk_buff to be sent to the peer
611 * @dp: for client during handshake and general negotiation
612 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
614 int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
617 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
618 struct dccp_feat_entry *pos, *next;
619 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
622 /* put entries into @skb in the order they appear in the list */
623 list_for_each_entry_safe_reverse(pos, next, fn, node) {
624 opt = dccp_feat_genopt(pos);
625 type = dccp_feat_type(pos->feat_num);
628 if (pos->empty_confirm) {
632 if (type == FEAT_SP) {
633 len = pos->val.sp.len;
634 ptr = pos->val.sp.vec;
635 rpt = pos->needs_confirm;
636 } else if (type == FEAT_NN) {
637 len = dccp_feat_valid_nn_length(pos->feat_num);
639 dccp_encode_value_var(pos->val.nn, ptr, len);
641 DCCP_BUG("unknown feature %u", pos->feat_num);
645 dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
647 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
649 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
652 * Enter CHANGING after transmitting the Change option (6.6.2).
654 if (pos->state == FEAT_INITIALISING)
655 pos->state = FEAT_CHANGING;
661 * __feat_register_nn - Register new NN value on socket
662 * @fn: feature-negotiation list to register with
663 * @feat: an NN feature from %dccp_feature_numbers
664 * @mandatory: use Mandatory option if 1
665 * @nn_val: value to register (restricted to 4 bytes)
666 * Note that NN features are local by definition (RFC 4340, 6.3.2).
668 static int __feat_register_nn(struct list_head *fn, u8 feat,
669 u8 mandatory, u64 nn_val)
671 dccp_feat_val fval = { .nn = nn_val };
673 if (dccp_feat_type(feat) != FEAT_NN ||
674 !dccp_feat_is_valid_nn_val(feat, nn_val))
677 /* Don't bother with default values, they will be activated anyway. */
678 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
681 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
685 * __feat_register_sp - Register new SP value/list on socket
686 * @fn: feature-negotiation list to register with
687 * @feat: an SP feature from %dccp_feature_numbers
688 * @is_local: whether the local (1) or the remote (0) @feat is meant
689 * @mandatory: use Mandatory option if 1
690 * @sp_val: SP value followed by optional preference list
691 * @sp_len: length of @sp_val in bytes
693 static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
694 u8 mandatory, u8 const *sp_val, u8 sp_len)
698 if (dccp_feat_type(feat) != FEAT_SP ||
699 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
702 /* Avoid negotiating alien CCIDs by only advertising supported ones */
703 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
706 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
709 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
713 * dccp_feat_register_sp - Register requests to change SP feature values
714 * @sk: client or listening socket
715 * @feat: one of %dccp_feature_numbers
716 * @is_local: whether the local (1) or remote (0) @feat is meant
717 * @list: array of preferred values, in descending order of preference
718 * @len: length of @list in bytes
720 int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
721 u8 const *list, u8 len)
722 { /* any changes must be registered before establishing the connection */
723 if (sk->sk_state != DCCP_CLOSED)
725 if (dccp_feat_type(feat) != FEAT_SP)
727 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
731 /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
732 int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
734 /* any changes must be registered before establishing the connection */
735 if (sk->sk_state != DCCP_CLOSED)
737 if (dccp_feat_type(feat) != FEAT_NN)
739 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
743 * Tracking features whose value depend on the choice of CCID
745 * This is designed with an extension in mind so that a list walk could be done
746 * before activating any features. However, the existing framework was found to
747 * work satisfactorily up until now, the automatic verification is left open.
748 * When adding new CCIDs, add a corresponding dependency table here.
750 static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
752 static const struct ccid_dependency ccid2_dependencies[2][2] = {
754 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
755 * feature and Send Ack Vector is an RX feature, `is_local'
756 * needs to be reversed.
758 { /* Dependencies of the receiver-side (remote) CCID2 */
760 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
762 .is_mandatory = true,
767 { /* Dependencies of the sender-side (local) CCID2 */
769 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
771 .is_mandatory = true,
777 static const struct ccid_dependency ccid3_dependencies[2][5] = {
779 * Dependencies of the receiver-side CCID3
781 { /* locally disable Ack Vectors */
782 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
784 .is_mandatory = false,
787 { /* see below why Send Loss Event Rate is on */
788 .dependent_feat = DCCPF_SEND_LEV_RATE,
790 .is_mandatory = true,
793 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
794 .dependent_feat = DCCPF_SEND_NDP_COUNT,
796 .is_mandatory = true,
802 * CCID3 at the TX side: we request that the HC-receiver
803 * will not send Ack Vectors (they will be ignored, so
804 * Mandatory is not set); we enable Send Loss Event Rate
805 * (Mandatory since the implementation does not support
806 * the Loss Intervals option of RFC 4342, 8.6).
807 * The last two options are for peer's information only.
810 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
812 .is_mandatory = false,
816 .dependent_feat = DCCPF_SEND_LEV_RATE,
818 .is_mandatory = true,
821 { /* this CCID does not support Ack Ratio */
822 .dependent_feat = DCCPF_ACK_RATIO,
824 .is_mandatory = false,
827 { /* tell receiver we are sending NDP counts */
828 .dependent_feat = DCCPF_SEND_NDP_COUNT,
830 .is_mandatory = false,
838 return ccid2_dependencies[is_local];
840 return ccid3_dependencies[is_local];
847 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
848 * @fn: feature-negotiation list to update
849 * @id: CCID number to track
850 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
851 * This function needs to be called after registering all other features.
853 static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
855 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
856 int i, rc = (table == NULL);
858 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
859 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
860 rc = __feat_register_sp(fn, table[i].dependent_feat,
862 table[i].is_mandatory,
865 rc = __feat_register_nn(fn, table[i].dependent_feat,
866 table[i].is_mandatory,
872 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
873 * @dp: client or listening socket (settings will be inherited)
874 * This is called after all registrations (socket initialisation, sysctls, and
875 * sockopt calls), and before sending the first packet containing Change options
876 * (ie. client-Request or server-Response), to ensure internal consistency.
878 int dccp_feat_finalise_settings(struct dccp_sock *dp)
880 struct list_head *fn = &dp->dccps_featneg;
881 struct dccp_feat_entry *entry;
882 int i = 2, ccids[2] = { -1, -1 };
886 * 1) not useful to propagate CCID settings if this host advertises more
887 * than one CCID: the choice of CCID may still change - if this is
888 * the client, or if this is the server and the client sends
889 * singleton CCID values.
890 * 2) since is that propagate_ccid changes the list, we defer changing
891 * the sorted list until after the traversal.
893 list_for_each_entry(entry, fn, node)
894 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
895 ccids[entry->is_local] = entry->val.sp.vec[0];
897 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
899 dccp_feat_print_fnlist(fn);
904 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
905 * It is the server which resolves the dependencies once the CCID has been
906 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
908 int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
910 struct list_head *fn = &dreq->dreq_featneg;
911 struct dccp_feat_entry *entry;
914 for (is_local = 0; is_local <= 1; is_local++) {
915 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
917 if (entry != NULL && !entry->empty_confirm)
918 ccid = entry->val.sp.vec[0];
920 ccid = dccp_feat_default_value(DCCPF_CCID);
922 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
928 /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
929 static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
933 for (s = 0; s < slen; s++)
934 for (c = 0; c < clen; c++)
935 if (servlist[s] == clilist[c])
941 * dccp_feat_prefer - Move preferred entry to the start of array
942 * Reorder the @array_len elements in @array so that @preferred_value comes
943 * first. Returns >0 to indicate that @preferred_value does occur in @array.
945 static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
947 u8 i, does_occur = 0;
950 for (i = 0; i < array_len; i++)
951 if (array[i] == preferred_value) {
956 array[0] = preferred_value;
962 * dccp_feat_reconcile - Reconcile SP preference lists
963 * @fval: SP list to reconcile into
964 * @arr: received SP preference list
965 * @len: length of @arr in bytes
966 * @is_server: whether this side is the server (and @fv is the server's list)
967 * @reorder: whether to reorder the list in @fv after reconciling with @arr
968 * When successful, > 0 is returned and the reconciled list is in @fval.
969 * A value of 0 means that negotiation failed (no shared entry).
971 static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
972 bool is_server, bool reorder)
976 if (!fv->sp.vec || !arr) {
977 DCCP_CRIT("NULL feature value or array");
982 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
984 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
992 * Reorder list: used for activating features and in dccp_insert_fn_opt.
994 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
998 * dccp_feat_change_recv - Process incoming ChangeL/R options
999 * @fn: feature-negotiation list to update
1000 * @is_mandatory: whether the Change was preceded by a Mandatory option
1001 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1002 * @feat: one of %dccp_feature_numbers
1003 * @val: NN value or SP value/preference list
1004 * @len: length of @val in bytes
1005 * @server: whether this node is the server (1) or the client (0)
1007 static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1008 u8 feat, u8 *val, u8 len, const bool server)
1010 u8 defval, type = dccp_feat_type(feat);
1011 const bool local = (opt == DCCPO_CHANGE_R);
1012 struct dccp_feat_entry *entry;
1015 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
1016 goto unknown_feature_or_value;
1018 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1021 * Negotiation of NN features: Change R is invalid, so there is no
1022 * simultaneous negotiation; hence we do not look up in the list.
1024 if (type == FEAT_NN) {
1025 if (local || len > sizeof(fval.nn))
1026 goto unknown_feature_or_value;
1028 /* 6.3.2: "The feature remote MUST accept any valid value..." */
1029 fval.nn = dccp_decode_value_var(val, len);
1030 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1031 goto unknown_feature_or_value;
1033 return dccp_feat_push_confirm(fn, feat, local, &fval);
1037 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
1039 entry = dccp_feat_list_lookup(fn, feat, local);
1040 if (entry == NULL) {
1042 * No particular preferences have been registered. We deal with
1043 * this situation by assuming that all valid values are equally
1044 * acceptable, and apply the following checks:
1045 * - if the peer's list is a singleton, we accept a valid value;
1046 * - if we are the server, we first try to see if the peer (the
1047 * client) advertises the default value. If yes, we use it,
1048 * otherwise we accept the preferred value;
1049 * - else if we are the client, we use the first list element.
1051 if (dccp_feat_clone_sp_val(&fval, val, 1))
1052 return DCCP_RESET_CODE_TOO_BUSY;
1054 if (len > 1 && server) {
1055 defval = dccp_feat_default_value(feat);
1056 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1057 fval.sp.vec[0] = defval;
1058 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1060 goto unknown_feature_or_value;
1063 /* Treat unsupported CCIDs like invalid values */
1064 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1066 goto not_valid_or_not_known;
1069 return dccp_feat_push_confirm(fn, feat, local, &fval);
1071 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
1075 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1076 entry->empty_confirm = 0;
1077 } else if (is_mandatory) {
1078 return DCCP_RESET_CODE_MANDATORY_ERROR;
1079 } else if (entry->state == FEAT_INITIALISING) {
1081 * Failed simultaneous negotiation (server only): try to `save'
1082 * the connection by checking whether entry contains the default
1083 * value for @feat. If yes, send an empty Confirm to signal that
1084 * the received Change was not understood - which implies using
1085 * the default value.
1086 * If this also fails, we use Reset as the last resort.
1089 defval = dccp_feat_default_value(feat);
1090 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1091 return DCCP_RESET_CODE_OPTION_ERROR;
1092 entry->empty_confirm = 1;
1094 entry->needs_confirm = 1;
1095 entry->needs_mandatory = 0;
1096 entry->state = FEAT_STABLE;
1099 unknown_feature_or_value:
1101 return dccp_push_empty_confirm(fn, feat, local);
1103 not_valid_or_not_known:
1104 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1105 : DCCP_RESET_CODE_OPTION_ERROR;
1109 * dccp_feat_confirm_recv - Process received Confirm options
1110 * @fn: feature-negotiation list to update
1111 * @is_mandatory: whether @opt was preceded by a Mandatory option
1112 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1113 * @feat: one of %dccp_feature_numbers
1114 * @val: NN value or SP value/preference list
1115 * @len: length of @val in bytes
1116 * @server: whether this node is server (1) or client (0)
1118 static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1119 u8 feat, u8 *val, u8 len, const bool server)
1121 u8 *plist, plen, type = dccp_feat_type(feat);
1122 const bool local = (opt == DCCPO_CONFIRM_R);
1123 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1125 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1127 if (entry == NULL) { /* nothing queued: ignore or handle error */
1128 if (is_mandatory && type == FEAT_UNKNOWN)
1129 return DCCP_RESET_CODE_MANDATORY_ERROR;
1131 if (!local && type == FEAT_NN) /* 6.3.2 */
1132 goto confirmation_failed;
1136 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
1140 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1141 goto confirmation_failed;
1143 * Empty Confirm during connection setup: this means reverting
1144 * to the `old' value, which in this case is the default. Since
1145 * we handle default values automatically when no other values
1146 * have been set, we revert to the old value by removing this
1147 * entry from the list.
1149 dccp_feat_list_pop(entry);
1153 if (type == FEAT_NN) {
1154 if (len > sizeof(entry->val.nn))
1155 goto confirmation_failed;
1157 if (entry->val.nn == dccp_decode_value_var(val, len))
1158 goto confirmation_succeeded;
1160 DCCP_WARN("Bogus Confirm for non-existing value\n");
1161 goto confirmation_failed;
1165 * Parsing SP Confirms: the first element of @val is the preferred
1166 * SP value which the peer confirms, the remainder depends on @len.
1167 * Note that only the confirmed value need to be a valid SP value.
1169 if (!dccp_feat_is_valid_sp_val(feat, *val))
1170 goto confirmation_failed;
1172 if (len == 1) { /* peer didn't supply a preference list */
1175 } else { /* preferred value + preference list */
1180 /* Check whether the peer got the reconciliation right (6.6.8) */
1181 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1182 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1183 return DCCP_RESET_CODE_OPTION_ERROR;
1185 entry->val.sp.vec[0] = *val;
1187 confirmation_succeeded:
1188 entry->state = FEAT_STABLE;
1191 confirmation_failed:
1192 DCCP_WARN("Confirmation failed\n");
1193 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1194 : DCCP_RESET_CODE_OPTION_ERROR;
1198 * dccp_feat_parse_options - Process Feature-Negotiation Options
1199 * @sk: for general use and used by the client during connection setup
1200 * @dreq: used by the server during connection setup
1201 * @mandatory: whether @opt was preceded by a Mandatory option
1202 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1203 * @feat: one of %dccp_feature_numbers
1204 * @val: value contents of @opt
1205 * @len: length of @val in bytes
1206 * Returns 0 on success, a Reset code for ending the connection otherwise.
1208 int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1209 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1211 struct dccp_sock *dp = dccp_sk(sk);
1212 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1213 bool server = false;
1215 switch (sk->sk_state) {
1217 * Negotiation during connection setup
1220 server = true; /* fall through */
1221 case DCCP_REQUESTING:
1223 case DCCPO_CHANGE_L:
1224 case DCCPO_CHANGE_R:
1225 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1227 case DCCPO_CONFIRM_R:
1228 case DCCPO_CONFIRM_L:
1229 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1233 return 0; /* ignore FN options in all other states */
1237 * dccp_feat_init - Seed feature negotiation with host-specific defaults
1238 * This initialises global defaults, depending on the value of the sysctls.
1239 * These can later be overridden by registering changes via setsockopt calls.
1240 * The last link in the chain is finalise_settings, to make sure that between
1241 * here and the start of actual feature negotiation no inconsistencies enter.
1243 * All features not appearing below use either defaults or are otherwise
1244 * later adjusted through dccp_feat_finalise_settings().
1246 int dccp_feat_init(struct sock *sk)
1248 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1256 /* Non-negotiable (NN) features */
1257 rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
1258 sysctl_dccp_sequence_window);
1262 /* Server-priority (SP) features */
1264 /* Advertise that short seqnos are not supported (7.6.1) */
1265 rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1269 /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1270 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1275 * We advertise the available list of CCIDs and reorder according to
1276 * preferences, to avoid failure resulting from negotiating different
1277 * singleton values (which always leads to failure).
1278 * These settings can still (later) be overridden via sockopts.
1280 if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1281 ccid_get_builtin_ccids(&rx.val, &rx.len))
1284 if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1285 !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
1286 goto free_ccid_lists;
1288 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1290 goto free_ccid_lists;
1292 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1300 int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1302 struct dccp_sock *dp = dccp_sk(sk);
1303 struct dccp_feat_entry *cur, *next;
1305 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1306 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1309 list_for_each_entry(cur, fn_list, node) {
1311 * An empty Confirm means that either an unknown feature type
1312 * or an invalid value was present. In the first case there is
1313 * nothing to activate, in the other the default value is used.
1315 if (cur->empty_confirm)
1318 idx = dccp_feat_index(cur->feat_num);
1320 DCCP_BUG("Unknown feature %u", cur->feat_num);
1321 goto activation_failed;
1323 if (cur->state != FEAT_STABLE) {
1324 DCCP_CRIT("Negotiation of %s %s failed in state %s",
1325 cur->is_local ? "local" : "remote",
1326 dccp_feat_fname(cur->feat_num),
1327 dccp_feat_sname[cur->state]);
1328 goto activation_failed;
1330 fvals[idx][cur->is_local] = &cur->val;
1334 * Activate in decreasing order of index, so that the CCIDs are always
1335 * activated as the last feature. This avoids the case where a CCID
1336 * relies on the initialisation of one or more features that it depends
1337 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1339 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1340 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1341 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1342 DCCP_CRIT("Could not activate %d", idx);
1343 goto activation_failed;
1346 /* Clean up Change options which have been confirmed already */
1347 list_for_each_entry_safe(cur, next, fn_list, node)
1348 if (!cur->needs_confirm)
1349 dccp_feat_list_pop(cur);
1351 dccp_pr_debug("Activation OK\n");
1356 * We clean up everything that may have been allocated, since
1357 * it is difficult to track at which stage negotiation failed.
1358 * This is ok, since all allocation functions below are robust
1359 * against NULL arguments.
1361 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1362 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1363 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1364 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1365 dp->dccps_hc_rx_ackvec = NULL;