4 * Feature negotiation for the DCCP protocol (RFC 4340, section 6)
6 * Copyright (c) 2008 The University of Aberdeen, Scotland, UK
7 * Copyright (c) 2008 Gerrit Renker <gerrit@erg.abdn.ac.uk>
8 * Rewrote from scratch, some bits from earlier code by
9 * Copyright (c) 2005 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
14 * o Feature negotiation is coordinated with connection setup (as in TCP), wild
15 * changes of parameters of an established connection are not supported.
16 * o Changing NN values (Ack Ratio only) is supported in state OPEN/PARTOPEN.
17 * o All currently known SP features have 1-byte quantities. If in the future
18 * extensions of RFCs 4340..42 define features with item lengths larger than
19 * one byte, a feature-specific extension of the code will be required.
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License
23 * as published by the Free Software Foundation; either version
24 * 2 of the License, or (at your option) any later version.
26 #include <linux/module.h>
30 /* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */
31 unsigned long sysctl_dccp_sequence_window __read_mostly = 100;
32 int sysctl_dccp_rx_ccid __read_mostly = 2,
33 sysctl_dccp_tx_ccid __read_mostly = 2;
36 * Feature activation handlers.
38 * These all use an u64 argument, to provide enough room for NN/SP features. At
39 * this stage the negotiated values have been checked to be within their range.
41 static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
43 struct dccp_sock *dp = dccp_sk(sk);
44 struct ccid *new_ccid = ccid_new(ccid, sk, rx, gfp_any());
50 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
51 dp->dccps_hc_rx_ccid = new_ccid;
53 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
54 dp->dccps_hc_tx_ccid = new_ccid;
59 static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
61 struct dccp_sock *dp = dccp_sk(sk);
64 dp->dccps_r_seq_win = seq_win;
65 /* propagate changes to update SWL/SWH */
66 dccp_update_gsr(sk, dp->dccps_gsr);
68 dp->dccps_l_seq_win = seq_win;
69 /* propagate changes to update AWL */
70 dccp_update_gss(sk, dp->dccps_gss);
75 static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
77 #ifndef __CCID2_COPES_GRACEFULLY_WITH_DYNAMIC_ACK_RATIO_UPDATES__
79 * FIXME: This is required until several problems in the CCID-2 code are
80 * resolved. The CCID-2 code currently does not cope well; using dynamic
81 * Ack Ratios greater than 1 caused instabilities. These were manifest
82 * in hangups and long RTO timeouts (1...3 seconds). Until this has been
83 * stabilised, it is safer not to activate dynamic Ack Ratio changes.
85 dccp_pr_debug("Not changing %s Ack Ratio from 1 to %u\n",
86 rx ? "RX" : "TX", (u16)ratio);
90 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
92 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
96 static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
98 struct dccp_sock *dp = dccp_sk(sk);
101 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
102 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
103 if (dp->dccps_hc_rx_ackvec == NULL)
105 } else if (!enable) {
106 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
107 dp->dccps_hc_rx_ackvec = NULL;
113 static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
116 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
121 * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
122 * `rx' holds when the sending peer informs about his partial coverage via a
123 * ChangeR() option. In the other case, we are the sender and the receiver
124 * announces its coverage via ChangeL() options. The policy here is to honour
125 * such communication by enabling the corresponding partial coverage - but only
126 * if it has not been set manually before; the warning here means that all
127 * packets will be dropped.
129 static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
131 struct dccp_sock *dp = dccp_sk(sk);
134 dp->dccps_pcrlen = cscov;
136 if (dp->dccps_pcslen == 0)
137 dp->dccps_pcslen = cscov;
138 else if (cscov > dp->dccps_pcslen)
139 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
140 dp->dccps_pcslen, (u8)cscov);
145 static const struct {
146 u8 feat_num; /* DCCPF_xxx */
147 enum dccp_feat_type rxtx; /* RX or TX */
148 enum dccp_feat_type reconciliation; /* SP or NN */
149 u8 default_value; /* as in 6.4 */
150 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
152 * Lookup table for location and type of features (from RFC 4340/4342)
153 * +--------------------------+----+-----+----+----+---------+-----------+
154 * | Feature | Location | Reconc. | Initial | Section |
155 * | | RX | TX | SP | NN | Value | Reference |
156 * +--------------------------+----+-----+----+----+---------+-----------+
157 * | DCCPF_CCID | | X | X | | 2 | 10 |
158 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
159 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
160 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
161 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
162 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
163 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
164 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
165 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
166 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
167 * +--------------------------+----+-----+----+----+---------+-----------+
169 } dccp_feat_table[] = {
170 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
171 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
172 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
173 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
174 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
175 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
176 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
177 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
178 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
179 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
181 #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
184 * dccp_feat_index - Hash function to map feature number into array position
185 * Returns consecutive array index or -1 if the feature is not understood.
187 static int dccp_feat_index(u8 feat_num)
189 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
190 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
194 * Other features: add cases for new feature types here after adding
195 * them to the above table.
198 case DCCPF_SEND_LEV_RATE:
199 return DCCP_FEAT_SUPPORTED_MAX - 1;
204 static u8 dccp_feat_type(u8 feat_num)
206 int idx = dccp_feat_index(feat_num);
210 return dccp_feat_table[idx].reconciliation;
213 static int dccp_feat_default_value(u8 feat_num)
215 int idx = dccp_feat_index(feat_num);
217 return idx < 0 ? : dccp_feat_table[idx].default_value;
221 * Debugging and verbose-printing section
223 static const char *dccp_feat_fname(const u8 feat)
225 static const char *feature_names[] = {
226 [DCCPF_RESERVED] = "Reserved",
227 [DCCPF_CCID] = "CCID",
228 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
229 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
230 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
231 [DCCPF_ACK_RATIO] = "Ack Ratio",
232 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
233 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
234 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
235 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
237 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
238 return feature_names[DCCPF_RESERVED];
240 if (feat == DCCPF_SEND_LEV_RATE)
241 return "Send Loss Event Rate";
242 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
243 return "CCID-specific";
245 return feature_names[feat];
248 static const char *dccp_feat_sname[] = { "DEFAULT", "INITIALISING", "CHANGING",
249 "UNSTABLE", "STABLE" };
251 #ifdef CONFIG_IP_DCCP_DEBUG
252 static const char *dccp_feat_oname(const u8 opt)
255 case DCCPO_CHANGE_L: return "Change_L";
256 case DCCPO_CONFIRM_L: return "Confirm_L";
257 case DCCPO_CHANGE_R: return "Change_R";
258 case DCCPO_CONFIRM_R: return "Confirm_R";
263 static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
265 u8 i, type = dccp_feat_type(feat_num);
267 if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL))
268 dccp_pr_debug_cat("(NULL)");
269 else if (type == FEAT_SP)
270 for (i = 0; i < val->sp.len; i++)
271 dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]);
272 else if (type == FEAT_NN)
273 dccp_pr_debug_cat("%llu", (unsigned long long)val->nn);
275 dccp_pr_debug_cat("unknown type %u", type);
278 static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
280 u8 type = dccp_feat_type(feat_num);
281 dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
284 fval.nn = dccp_decode_value_var(list, len);
285 dccp_feat_printval(feat_num, &fval);
288 static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
290 dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote",
291 dccp_feat_fname(entry->feat_num));
292 dccp_feat_printval(entry->feat_num, &entry->val);
293 dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
294 entry->needs_confirm ? "(Confirm pending)" : "");
297 #define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \
298 dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\
299 dccp_feat_printvals(feat, val, len); \
300 dccp_pr_debug_cat(") %s\n", mandatory ? "!" : ""); } while (0)
302 #define dccp_feat_print_fnlist(fn_list) { \
303 const struct dccp_feat_entry *___entry; \
305 dccp_pr_debug("List Dump:\n"); \
306 list_for_each_entry(___entry, fn_list, node) \
307 dccp_feat_print_entry(___entry); \
309 #else /* ! CONFIG_IP_DCCP_DEBUG */
310 #define dccp_feat_print_opt(opt, feat, val, len, mandatory)
311 #define dccp_feat_print_fnlist(fn_list)
314 static int __dccp_feat_activate(struct sock *sk, const int idx,
315 const bool is_local, dccp_feat_val const *fval)
320 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
322 if (dccp_feat_table[idx].activation_hdlr == NULL)
326 val = dccp_feat_table[idx].default_value;
327 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
328 if (fval->sp.vec == NULL) {
330 * This can happen when an empty Confirm is sent
331 * for an SP (i.e. known) feature. In this case
332 * we would be using the default anyway.
334 DCCP_CRIT("Feature #%d undefined: using default", idx);
335 val = dccp_feat_table[idx].default_value;
337 val = fval->sp.vec[0];
343 /* Location is RX if this is a local-RX or remote-TX feature */
344 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
346 dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
347 dccp_feat_fname(dccp_feat_table[idx].feat_num),
348 fval ? "" : "default ", (unsigned long long)val);
350 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
354 * dccp_feat_activate - Activate feature value on socket
355 * @sk: fully connected DCCP socket (after handshake is complete)
356 * @feat_num: feature to activate, one of %dccp_feature_numbers
357 * @local: whether local (1) or remote (0) @feat_num is meant
358 * @fval: the value (SP or NN) to activate, or NULL to use the default value
359 * For general use this function is preferable over __dccp_feat_activate().
361 static int dccp_feat_activate(struct sock *sk, u8 feat_num, bool local,
362 dccp_feat_val const *fval)
364 return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval);
367 /* Test for "Req'd" feature (RFC 4340, 6.4) */
368 static inline int dccp_feat_must_be_understood(u8 feat_num)
370 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
371 feat_num == DCCPF_SEQUENCE_WINDOW;
374 /* copy constructor, fval must not already contain allocated memory */
375 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
378 if (fval->sp.len > 0) {
379 fval->sp.vec = kmemdup(val, len, gfp_any());
380 if (fval->sp.vec == NULL) {
388 static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
390 if (unlikely(val == NULL))
392 if (dccp_feat_type(feat_num) == FEAT_SP)
394 memset(val, 0, sizeof(*val));
397 static struct dccp_feat_entry *
398 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
400 struct dccp_feat_entry *new;
401 u8 type = dccp_feat_type(original->feat_num);
403 if (type == FEAT_UNKNOWN)
406 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
410 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
411 original->val.sp.vec,
412 original->val.sp.len)) {
419 static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
422 dccp_feat_val_destructor(entry->feat_num, &entry->val);
428 * List management functions
430 * Feature negotiation lists rely on and maintain the following invariants:
431 * - each feat_num in the list is known, i.e. we know its type and default value
432 * - each feat_num/is_local combination is unique (old entries are overwritten)
433 * - SP values are always freshly allocated
434 * - list is sorted in increasing order of feature number (faster lookup)
436 static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
437 u8 feat_num, bool is_local)
439 struct dccp_feat_entry *entry;
441 list_for_each_entry(entry, fn_list, node)
442 if (entry->feat_num == feat_num && entry->is_local == is_local)
444 else if (entry->feat_num > feat_num)
450 * dccp_feat_entry_new - Central list update routine (called by all others)
451 * @head: list to add to
452 * @feat: feature number
453 * @local: whether the local (1) or remote feature with number @feat is meant
454 * This is the only constructor and serves to ensure the above invariants.
456 static struct dccp_feat_entry *
457 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
459 struct dccp_feat_entry *entry;
461 list_for_each_entry(entry, head, node)
462 if (entry->feat_num == feat && entry->is_local == local) {
463 dccp_feat_val_destructor(entry->feat_num, &entry->val);
465 } else if (entry->feat_num > feat) {
470 entry = kmalloc(sizeof(*entry), gfp_any());
472 entry->feat_num = feat;
473 entry->is_local = local;
474 list_add_tail(&entry->node, head);
480 * dccp_feat_push_change - Add/overwrite a Change option in the list
481 * @fn_list: feature-negotiation list to update
482 * @feat: one of %dccp_feature_numbers
483 * @local: whether local (1) or remote (0) @feat_num is meant
484 * @needs_mandatory: whether to use Mandatory feature negotiation options
485 * @fval: pointer to NN/SP value to be inserted (will be copied)
487 static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
488 u8 mandatory, dccp_feat_val *fval)
490 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
495 new->feat_num = feat;
496 new->is_local = local;
497 new->state = FEAT_INITIALISING;
498 new->needs_confirm = 0;
499 new->empty_confirm = 0;
501 new->needs_mandatory = mandatory;
507 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
508 * @fn_list: feature-negotiation list to add to
509 * @feat: one of %dccp_feature_numbers
510 * @local: whether local (1) or remote (0) @feat_num is being confirmed
511 * @fval: pointer to NN/SP value to be inserted or NULL
512 * Returns 0 on success, a Reset code for further processing otherwise.
514 static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
517 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
520 return DCCP_RESET_CODE_TOO_BUSY;
522 new->feat_num = feat;
523 new->is_local = local;
524 new->state = FEAT_STABLE; /* transition in 6.6.2 */
525 new->needs_confirm = 1;
526 new->empty_confirm = (fval == NULL);
527 new->val.nn = 0; /* zeroes the whole structure */
528 if (!new->empty_confirm)
530 new->needs_mandatory = 0;
535 static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
537 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
540 static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
542 list_del(&entry->node);
543 dccp_feat_entry_destructor(entry);
546 void dccp_feat_list_purge(struct list_head *fn_list)
548 struct dccp_feat_entry *entry, *next;
550 list_for_each_entry_safe(entry, next, fn_list, node)
551 dccp_feat_entry_destructor(entry);
552 INIT_LIST_HEAD(fn_list);
554 EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
556 /* generate @to as full clone of @from - @to must not contain any nodes */
557 int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
559 struct dccp_feat_entry *entry, *new;
562 list_for_each_entry(entry, from, node) {
563 new = dccp_feat_clone_entry(entry);
566 list_add_tail(&new->node, to);
571 dccp_feat_list_purge(to);
576 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
577 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
578 * incoming options are accepted as long as their values are valid.
580 static u8 dccp_feat_valid_nn_length(u8 feat_num)
582 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
584 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
589 static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
592 case DCCPF_ACK_RATIO:
593 return val <= DCCPF_ACK_RATIO_MAX;
594 case DCCPF_SEQUENCE_WINDOW:
595 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
597 return 0; /* feature unknown - so we can't tell */
600 /* check that SP values are within the ranges defined in RFC 4340 */
601 static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
605 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
606 /* Type-check Boolean feature values: */
607 case DCCPF_SHORT_SEQNOS:
608 case DCCPF_ECN_INCAPABLE:
609 case DCCPF_SEND_ACK_VECTOR:
610 case DCCPF_SEND_NDP_COUNT:
611 case DCCPF_DATA_CHECKSUM:
612 case DCCPF_SEND_LEV_RATE:
614 case DCCPF_MIN_CSUM_COVER:
617 return 0; /* feature unknown */
620 static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
622 if (sp_list == NULL || sp_len < 1)
625 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
631 * dccp_feat_insert_opts - Generate FN options from current list state
632 * @skb: next sk_buff to be sent to the peer
633 * @dp: for client during handshake and general negotiation
634 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
636 int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
639 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
640 struct dccp_feat_entry *pos, *next;
641 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
644 /* put entries into @skb in the order they appear in the list */
645 list_for_each_entry_safe_reverse(pos, next, fn, node) {
646 opt = dccp_feat_genopt(pos);
647 type = dccp_feat_type(pos->feat_num);
650 if (pos->empty_confirm) {
654 if (type == FEAT_SP) {
655 len = pos->val.sp.len;
656 ptr = pos->val.sp.vec;
657 rpt = pos->needs_confirm;
658 } else if (type == FEAT_NN) {
659 len = dccp_feat_valid_nn_length(pos->feat_num);
661 dccp_encode_value_var(pos->val.nn, ptr, len);
663 DCCP_BUG("unknown feature %u", pos->feat_num);
667 dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
669 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
671 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
674 * Enter CHANGING after transmitting the Change option (6.6.2).
676 if (pos->state == FEAT_INITIALISING)
677 pos->state = FEAT_CHANGING;
683 * __feat_register_nn - Register new NN value on socket
684 * @fn: feature-negotiation list to register with
685 * @feat: an NN feature from %dccp_feature_numbers
686 * @mandatory: use Mandatory option if 1
687 * @nn_val: value to register (restricted to 4 bytes)
688 * Note that NN features are local by definition (RFC 4340, 6.3.2).
690 static int __feat_register_nn(struct list_head *fn, u8 feat,
691 u8 mandatory, u64 nn_val)
693 dccp_feat_val fval = { .nn = nn_val };
695 if (dccp_feat_type(feat) != FEAT_NN ||
696 !dccp_feat_is_valid_nn_val(feat, nn_val))
699 /* Don't bother with default values, they will be activated anyway. */
700 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
703 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
707 * __feat_register_sp - Register new SP value/list on socket
708 * @fn: feature-negotiation list to register with
709 * @feat: an SP feature from %dccp_feature_numbers
710 * @is_local: whether the local (1) or the remote (0) @feat is meant
711 * @mandatory: use Mandatory option if 1
712 * @sp_val: SP value followed by optional preference list
713 * @sp_len: length of @sp_val in bytes
715 static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
716 u8 mandatory, u8 const *sp_val, u8 sp_len)
720 if (dccp_feat_type(feat) != FEAT_SP ||
721 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
724 /* Avoid negotiating alien CCIDs by only advertising supported ones */
725 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
728 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
731 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
735 * dccp_feat_register_sp - Register requests to change SP feature values
736 * @sk: client or listening socket
737 * @feat: one of %dccp_feature_numbers
738 * @is_local: whether the local (1) or remote (0) @feat is meant
739 * @list: array of preferred values, in descending order of preference
740 * @len: length of @list in bytes
742 int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
743 u8 const *list, u8 len)
744 { /* any changes must be registered before establishing the connection */
745 if (sk->sk_state != DCCP_CLOSED)
747 if (dccp_feat_type(feat) != FEAT_SP)
749 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
753 /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
754 int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
756 /* any changes must be registered before establishing the connection */
757 if (sk->sk_state != DCCP_CLOSED)
759 if (dccp_feat_type(feat) != FEAT_NN)
761 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
765 * dccp_feat_signal_nn_change - Update NN values for an established connection
766 * @sk: DCCP socket of an established connection
767 * @feat: NN feature number from %dccp_feature_numbers
768 * @nn_val: the new value to use
769 * This function is used to communicate NN updates out-of-band. The difference
770 * to feature negotiation during connection setup is that values are activated
771 * immediately after validation, i.e. we don't wait for the Confirm: either the
772 * value is accepted by the peer (and then the waiting is futile), or it is not
773 * (Reset or empty Confirm). We don't accept empty Confirms - transmitted values
774 * are validated, and the peer "MUST accept any valid value" (RFC 4340, 6.3.2).
776 int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
778 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
779 dccp_feat_val fval = { .nn = nn_val };
780 struct dccp_feat_entry *entry;
782 if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
785 if (dccp_feat_type(feat) != FEAT_NN ||
786 !dccp_feat_is_valid_nn_val(feat, nn_val))
789 entry = dccp_feat_list_lookup(fn, feat, 1);
791 dccp_pr_debug("Ignoring %llu, entry %llu exists in state %s\n",
792 (unsigned long long)nn_val,
793 (unsigned long long)entry->val.nn,
794 dccp_feat_sname[entry->state]);
798 if (dccp_feat_activate(sk, feat, 1, &fval))
801 inet_csk_schedule_ack(sk);
802 return dccp_feat_push_change(fn, feat, 1, 0, &fval);
804 EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
807 * Tracking features whose value depend on the choice of CCID
809 * This is designed with an extension in mind so that a list walk could be done
810 * before activating any features. However, the existing framework was found to
811 * work satisfactorily up until now, the automatic verification is left open.
812 * When adding new CCIDs, add a corresponding dependency table here.
814 static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
816 static const struct ccid_dependency ccid2_dependencies[2][2] = {
818 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
819 * feature and Send Ack Vector is an RX feature, `is_local'
820 * needs to be reversed.
822 { /* Dependencies of the receiver-side (remote) CCID2 */
824 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
826 .is_mandatory = true,
831 { /* Dependencies of the sender-side (local) CCID2 */
833 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
835 .is_mandatory = true,
841 static const struct ccid_dependency ccid3_dependencies[2][5] = {
843 * Dependencies of the receiver-side CCID3
845 { /* locally disable Ack Vectors */
846 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
848 .is_mandatory = false,
851 { /* see below why Send Loss Event Rate is on */
852 .dependent_feat = DCCPF_SEND_LEV_RATE,
854 .is_mandatory = true,
857 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
858 .dependent_feat = DCCPF_SEND_NDP_COUNT,
860 .is_mandatory = true,
866 * CCID3 at the TX side: we request that the HC-receiver
867 * will not send Ack Vectors (they will be ignored, so
868 * Mandatory is not set); we enable Send Loss Event Rate
869 * (Mandatory since the implementation does not support
870 * the Loss Intervals option of RFC 4342, 8.6).
871 * The last two options are for peer's information only.
874 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
876 .is_mandatory = false,
880 .dependent_feat = DCCPF_SEND_LEV_RATE,
882 .is_mandatory = true,
885 { /* this CCID does not support Ack Ratio */
886 .dependent_feat = DCCPF_ACK_RATIO,
888 .is_mandatory = false,
891 { /* tell receiver we are sending NDP counts */
892 .dependent_feat = DCCPF_SEND_NDP_COUNT,
894 .is_mandatory = false,
902 return ccid2_dependencies[is_local];
904 return ccid3_dependencies[is_local];
911 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
912 * @fn: feature-negotiation list to update
913 * @id: CCID number to track
914 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
915 * This function needs to be called after registering all other features.
917 static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
919 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
920 int i, rc = (table == NULL);
922 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
923 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
924 rc = __feat_register_sp(fn, table[i].dependent_feat,
926 table[i].is_mandatory,
929 rc = __feat_register_nn(fn, table[i].dependent_feat,
930 table[i].is_mandatory,
936 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
937 * @dp: client or listening socket (settings will be inherited)
938 * This is called after all registrations (socket initialisation, sysctls, and
939 * sockopt calls), and before sending the first packet containing Change options
940 * (ie. client-Request or server-Response), to ensure internal consistency.
942 int dccp_feat_finalise_settings(struct dccp_sock *dp)
944 struct list_head *fn = &dp->dccps_featneg;
945 struct dccp_feat_entry *entry;
946 int i = 2, ccids[2] = { -1, -1 };
950 * 1) not useful to propagate CCID settings if this host advertises more
951 * than one CCID: the choice of CCID may still change - if this is
952 * the client, or if this is the server and the client sends
953 * singleton CCID values.
954 * 2) since is that propagate_ccid changes the list, we defer changing
955 * the sorted list until after the traversal.
957 list_for_each_entry(entry, fn, node)
958 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
959 ccids[entry->is_local] = entry->val.sp.vec[0];
961 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
963 dccp_feat_print_fnlist(fn);
968 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
969 * It is the server which resolves the dependencies once the CCID has been
970 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
972 int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
974 struct list_head *fn = &dreq->dreq_featneg;
975 struct dccp_feat_entry *entry;
978 for (is_local = 0; is_local <= 1; is_local++) {
979 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
981 if (entry != NULL && !entry->empty_confirm)
982 ccid = entry->val.sp.vec[0];
984 ccid = dccp_feat_default_value(DCCPF_CCID);
986 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
992 /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
993 static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
997 for (s = 0; s < slen; s++)
998 for (c = 0; c < clen; c++)
999 if (servlist[s] == clilist[c])
1005 * dccp_feat_prefer - Move preferred entry to the start of array
1006 * Reorder the @array_len elements in @array so that @preferred_value comes
1007 * first. Returns >0 to indicate that @preferred_value does occur in @array.
1009 static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
1011 u8 i, does_occur = 0;
1013 if (array != NULL) {
1014 for (i = 0; i < array_len; i++)
1015 if (array[i] == preferred_value) {
1016 array[i] = array[0];
1020 array[0] = preferred_value;
1026 * dccp_feat_reconcile - Reconcile SP preference lists
1027 * @fval: SP list to reconcile into
1028 * @arr: received SP preference list
1029 * @len: length of @arr in bytes
1030 * @is_server: whether this side is the server (and @fv is the server's list)
1031 * @reorder: whether to reorder the list in @fv after reconciling with @arr
1032 * When successful, > 0 is returned and the reconciled list is in @fval.
1033 * A value of 0 means that negotiation failed (no shared entry).
1035 static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
1036 bool is_server, bool reorder)
1040 if (!fv->sp.vec || !arr) {
1041 DCCP_CRIT("NULL feature value or array");
1046 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
1048 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
1056 * Reorder list: used for activating features and in dccp_insert_fn_opt.
1058 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
1062 * dccp_feat_change_recv - Process incoming ChangeL/R options
1063 * @fn: feature-negotiation list to update
1064 * @is_mandatory: whether the Change was preceded by a Mandatory option
1065 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1066 * @feat: one of %dccp_feature_numbers
1067 * @val: NN value or SP value/preference list
1068 * @len: length of @val in bytes
1069 * @server: whether this node is the server (1) or the client (0)
1071 static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1072 u8 feat, u8 *val, u8 len, const bool server)
1074 u8 defval, type = dccp_feat_type(feat);
1075 const bool local = (opt == DCCPO_CHANGE_R);
1076 struct dccp_feat_entry *entry;
1079 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
1080 goto unknown_feature_or_value;
1082 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1085 * Negotiation of NN features: Change R is invalid, so there is no
1086 * simultaneous negotiation; hence we do not look up in the list.
1088 if (type == FEAT_NN) {
1089 if (local || len > sizeof(fval.nn))
1090 goto unknown_feature_or_value;
1092 /* 6.3.2: "The feature remote MUST accept any valid value..." */
1093 fval.nn = dccp_decode_value_var(val, len);
1094 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1095 goto unknown_feature_or_value;
1097 return dccp_feat_push_confirm(fn, feat, local, &fval);
1101 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
1103 entry = dccp_feat_list_lookup(fn, feat, local);
1104 if (entry == NULL) {
1106 * No particular preferences have been registered. We deal with
1107 * this situation by assuming that all valid values are equally
1108 * acceptable, and apply the following checks:
1109 * - if the peer's list is a singleton, we accept a valid value;
1110 * - if we are the server, we first try to see if the peer (the
1111 * client) advertises the default value. If yes, we use it,
1112 * otherwise we accept the preferred value;
1113 * - else if we are the client, we use the first list element.
1115 if (dccp_feat_clone_sp_val(&fval, val, 1))
1116 return DCCP_RESET_CODE_TOO_BUSY;
1118 if (len > 1 && server) {
1119 defval = dccp_feat_default_value(feat);
1120 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1121 fval.sp.vec[0] = defval;
1122 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1124 goto unknown_feature_or_value;
1127 /* Treat unsupported CCIDs like invalid values */
1128 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1130 goto not_valid_or_not_known;
1133 return dccp_feat_push_confirm(fn, feat, local, &fval);
1135 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
1139 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1140 entry->empty_confirm = 0;
1141 } else if (is_mandatory) {
1142 return DCCP_RESET_CODE_MANDATORY_ERROR;
1143 } else if (entry->state == FEAT_INITIALISING) {
1145 * Failed simultaneous negotiation (server only): try to `save'
1146 * the connection by checking whether entry contains the default
1147 * value for @feat. If yes, send an empty Confirm to signal that
1148 * the received Change was not understood - which implies using
1149 * the default value.
1150 * If this also fails, we use Reset as the last resort.
1153 defval = dccp_feat_default_value(feat);
1154 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1155 return DCCP_RESET_CODE_OPTION_ERROR;
1156 entry->empty_confirm = 1;
1158 entry->needs_confirm = 1;
1159 entry->needs_mandatory = 0;
1160 entry->state = FEAT_STABLE;
1163 unknown_feature_or_value:
1165 return dccp_push_empty_confirm(fn, feat, local);
1167 not_valid_or_not_known:
1168 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1169 : DCCP_RESET_CODE_OPTION_ERROR;
1173 * dccp_feat_confirm_recv - Process received Confirm options
1174 * @fn: feature-negotiation list to update
1175 * @is_mandatory: whether @opt was preceded by a Mandatory option
1176 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1177 * @feat: one of %dccp_feature_numbers
1178 * @val: NN value or SP value/preference list
1179 * @len: length of @val in bytes
1180 * @server: whether this node is server (1) or client (0)
1182 static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1183 u8 feat, u8 *val, u8 len, const bool server)
1185 u8 *plist, plen, type = dccp_feat_type(feat);
1186 const bool local = (opt == DCCPO_CONFIRM_R);
1187 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1189 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1191 if (entry == NULL) { /* nothing queued: ignore or handle error */
1192 if (is_mandatory && type == FEAT_UNKNOWN)
1193 return DCCP_RESET_CODE_MANDATORY_ERROR;
1195 if (!local && type == FEAT_NN) /* 6.3.2 */
1196 goto confirmation_failed;
1200 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
1204 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1205 goto confirmation_failed;
1207 * Empty Confirm during connection setup: this means reverting
1208 * to the `old' value, which in this case is the default. Since
1209 * we handle default values automatically when no other values
1210 * have been set, we revert to the old value by removing this
1211 * entry from the list.
1213 dccp_feat_list_pop(entry);
1217 if (type == FEAT_NN) {
1218 if (len > sizeof(entry->val.nn))
1219 goto confirmation_failed;
1221 if (entry->val.nn == dccp_decode_value_var(val, len))
1222 goto confirmation_succeeded;
1224 DCCP_WARN("Bogus Confirm for non-existing value\n");
1225 goto confirmation_failed;
1229 * Parsing SP Confirms: the first element of @val is the preferred
1230 * SP value which the peer confirms, the remainder depends on @len.
1231 * Note that only the confirmed value need to be a valid SP value.
1233 if (!dccp_feat_is_valid_sp_val(feat, *val))
1234 goto confirmation_failed;
1236 if (len == 1) { /* peer didn't supply a preference list */
1239 } else { /* preferred value + preference list */
1244 /* Check whether the peer got the reconciliation right (6.6.8) */
1245 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1246 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1247 return DCCP_RESET_CODE_OPTION_ERROR;
1249 entry->val.sp.vec[0] = *val;
1251 confirmation_succeeded:
1252 entry->state = FEAT_STABLE;
1255 confirmation_failed:
1256 DCCP_WARN("Confirmation failed\n");
1257 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1258 : DCCP_RESET_CODE_OPTION_ERROR;
1262 * dccp_feat_handle_nn_established - Fast-path reception of NN options
1263 * @sk: socket of an established DCCP connection
1264 * @mandatory: whether @opt was preceded by a Mandatory option
1265 * @opt: %DCCPO_CHANGE_L | %DCCPO_CONFIRM_R (NN only)
1266 * @feat: NN number, one of %dccp_feature_numbers
1268 * @len: length of @val in bytes
1269 * This function combines the functionality of change_recv/confirm_recv, with
1270 * the following differences (reset codes are the same):
1271 * - cleanup after receiving the Confirm;
1272 * - values are directly activated after successful parsing;
1273 * - deliberately restricted to NN features.
1274 * The restriction to NN features is essential since SP features can have non-
1275 * predictable outcomes (depending on the remote configuration), and are inter-
1276 * dependent (CCIDs for instance cause further dependencies).
1278 static u8 dccp_feat_handle_nn_established(struct sock *sk, u8 mandatory, u8 opt,
1279 u8 feat, u8 *val, u8 len)
1281 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1282 const bool local = (opt == DCCPO_CONFIRM_R);
1283 struct dccp_feat_entry *entry;
1284 u8 type = dccp_feat_type(feat);
1287 dccp_feat_print_opt(opt, feat, val, len, mandatory);
1289 /* Ignore non-mandatory unknown and non-NN features */
1290 if (type == FEAT_UNKNOWN) {
1291 if (local && !mandatory)
1293 goto fast_path_unknown;
1294 } else if (type != FEAT_NN) {
1299 * We don't accept empty Confirms, since in fast-path feature
1300 * negotiation the values are enabled immediately after sending
1301 * the Change option.
1302 * Empty Changes on the other hand are invalid (RFC 4340, 6.1).
1304 if (len == 0 || len > sizeof(fval.nn))
1305 goto fast_path_unknown;
1307 if (opt == DCCPO_CHANGE_L) {
1308 fval.nn = dccp_decode_value_var(val, len);
1309 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1310 goto fast_path_unknown;
1312 if (dccp_feat_push_confirm(fn, feat, local, &fval) ||
1313 dccp_feat_activate(sk, feat, local, &fval))
1314 return DCCP_RESET_CODE_TOO_BUSY;
1316 /* set the `Ack Pending' flag to piggyback a Confirm */
1317 inet_csk_schedule_ack(sk);
1319 } else if (opt == DCCPO_CONFIRM_R) {
1320 entry = dccp_feat_list_lookup(fn, feat, local);
1321 if (entry == NULL || entry->state != FEAT_CHANGING)
1324 fval.nn = dccp_decode_value_var(val, len);
1325 if (fval.nn != entry->val.nn) {
1326 DCCP_WARN("Bogus Confirm for non-existing value\n");
1327 goto fast_path_failed;
1330 /* It has been confirmed - so remove the entry */
1331 dccp_feat_list_pop(entry);
1334 DCCP_WARN("Received illegal option %u\n", opt);
1335 goto fast_path_failed;
1341 return dccp_push_empty_confirm(fn, feat, local);
1344 return mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1345 : DCCP_RESET_CODE_OPTION_ERROR;
1349 * dccp_feat_parse_options - Process Feature-Negotiation Options
1350 * @sk: for general use and used by the client during connection setup
1351 * @dreq: used by the server during connection setup
1352 * @mandatory: whether @opt was preceded by a Mandatory option
1353 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1354 * @feat: one of %dccp_feature_numbers
1355 * @val: value contents of @opt
1356 * @len: length of @val in bytes
1357 * Returns 0 on success, a Reset code for ending the connection otherwise.
1359 int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1360 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1362 struct dccp_sock *dp = dccp_sk(sk);
1363 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1364 bool server = false;
1366 switch (sk->sk_state) {
1368 * Negotiation during connection setup
1371 server = true; /* fall through */
1372 case DCCP_REQUESTING:
1374 case DCCPO_CHANGE_L:
1375 case DCCPO_CHANGE_R:
1376 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1378 case DCCPO_CONFIRM_R:
1379 case DCCPO_CONFIRM_L:
1380 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1385 * Support for exchanging NN options on an established connection
1386 * This is currently restricted to Ack Ratio (RFC 4341, 6.1.2)
1390 return dccp_feat_handle_nn_established(sk, mandatory, opt, feat,
1393 return 0; /* ignore FN options in all other states */
1397 * dccp_feat_init - Seed feature negotiation with host-specific defaults
1398 * This initialises global defaults, depending on the value of the sysctls.
1399 * These can later be overridden by registering changes via setsockopt calls.
1400 * The last link in the chain is finalise_settings, to make sure that between
1401 * here and the start of actual feature negotiation no inconsistencies enter.
1403 * All features not appearing below use either defaults or are otherwise
1404 * later adjusted through dccp_feat_finalise_settings().
1406 int dccp_feat_init(struct sock *sk)
1408 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1416 /* Non-negotiable (NN) features */
1417 rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
1418 sysctl_dccp_sequence_window);
1422 /* Server-priority (SP) features */
1424 /* Advertise that short seqnos are not supported (7.6.1) */
1425 rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1429 /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1430 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1435 * We advertise the available list of CCIDs and reorder according to
1436 * preferences, to avoid failure resulting from negotiating different
1437 * singleton values (which always leads to failure).
1438 * These settings can still (later) be overridden via sockopts.
1440 if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1441 ccid_get_builtin_ccids(&rx.val, &rx.len))
1444 /* Pre-load all CCID modules that are going to be advertised */
1446 if (ccid_request_modules(tx.val, tx.len))
1447 goto free_ccid_lists;
1449 if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1450 !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
1451 goto free_ccid_lists;
1453 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1455 goto free_ccid_lists;
1457 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1465 int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1467 struct dccp_sock *dp = dccp_sk(sk);
1468 struct dccp_feat_entry *cur, *next;
1470 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1471 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1474 list_for_each_entry(cur, fn_list, node) {
1476 * An empty Confirm means that either an unknown feature type
1477 * or an invalid value was present. In the first case there is
1478 * nothing to activate, in the other the default value is used.
1480 if (cur->empty_confirm)
1483 idx = dccp_feat_index(cur->feat_num);
1485 DCCP_BUG("Unknown feature %u", cur->feat_num);
1486 goto activation_failed;
1488 if (cur->state != FEAT_STABLE) {
1489 DCCP_CRIT("Negotiation of %s %s failed in state %s",
1490 cur->is_local ? "local" : "remote",
1491 dccp_feat_fname(cur->feat_num),
1492 dccp_feat_sname[cur->state]);
1493 goto activation_failed;
1495 fvals[idx][cur->is_local] = &cur->val;
1499 * Activate in decreasing order of index, so that the CCIDs are always
1500 * activated as the last feature. This avoids the case where a CCID
1501 * relies on the initialisation of one or more features that it depends
1502 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1504 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1505 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1506 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1507 DCCP_CRIT("Could not activate %d", idx);
1508 goto activation_failed;
1511 /* Clean up Change options which have been confirmed already */
1512 list_for_each_entry_safe(cur, next, fn_list, node)
1513 if (!cur->needs_confirm)
1514 dccp_feat_list_pop(cur);
1516 dccp_pr_debug("Activation OK\n");
1521 * We clean up everything that may have been allocated, since
1522 * it is difficult to track at which stage negotiation failed.
1523 * This is ok, since all allocation functions below are robust
1524 * against NULL arguments.
1526 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1527 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1528 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1529 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1530 dp->dccps_hc_rx_ackvec = NULL;