sungem: Fix WakeOnLan
[zen-stable.git] / net / dccp / feat.c
blob78a2ad70e1b036db59ee35b49d09657cb03b9aa6
1 /*
2 * net/dccp/feat.c
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>
11 * ASSUMPTIONS
12 * -----------
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 Changing non-negotiable (NN) values is supported in state OPEN/PARTOPEN.
16 * o All currently known SP features have 1-byte quantities. If in the future
17 * extensions of RFCs 4340..42 define features with item lengths larger than
18 * one byte, a feature-specific extension of the code will be required.
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version
23 * 2 of the License, or (at your option) any later version.
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include "ccid.h"
28 #include "feat.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);
46 if (new_ccid == NULL)
47 return -ENOMEM;
49 if (rx) {
50 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
51 dp->dccps_hc_rx_ccid = new_ccid;
52 } else {
53 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
54 dp->dccps_hc_tx_ccid = new_ccid;
56 return 0;
59 static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
61 struct dccp_sock *dp = dccp_sk(sk);
63 if (rx) {
64 dp->dccps_r_seq_win = seq_win;
65 /* propagate changes to update SWL/SWH */
66 dccp_update_gsr(sk, dp->dccps_gsr);
67 } else {
68 dp->dccps_l_seq_win = seq_win;
69 /* propagate changes to update AWL */
70 dccp_update_gss(sk, dp->dccps_gss);
72 return 0;
75 static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
77 if (rx)
78 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
79 else
80 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
81 return 0;
84 static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
86 struct dccp_sock *dp = dccp_sk(sk);
88 if (rx) {
89 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
90 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
91 if (dp->dccps_hc_rx_ackvec == NULL)
92 return -ENOMEM;
93 } else if (!enable) {
94 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
95 dp->dccps_hc_rx_ackvec = NULL;
98 return 0;
101 static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
103 if (!rx)
104 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
105 return 0;
109 * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
110 * `rx' holds when the sending peer informs about his partial coverage via a
111 * ChangeR() option. In the other case, we are the sender and the receiver
112 * announces its coverage via ChangeL() options. The policy here is to honour
113 * such communication by enabling the corresponding partial coverage - but only
114 * if it has not been set manually before; the warning here means that all
115 * packets will be dropped.
117 static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
119 struct dccp_sock *dp = dccp_sk(sk);
121 if (rx)
122 dp->dccps_pcrlen = cscov;
123 else {
124 if (dp->dccps_pcslen == 0)
125 dp->dccps_pcslen = cscov;
126 else if (cscov > dp->dccps_pcslen)
127 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
128 dp->dccps_pcslen, (u8)cscov);
130 return 0;
133 static const struct {
134 u8 feat_num; /* DCCPF_xxx */
135 enum dccp_feat_type rxtx; /* RX or TX */
136 enum dccp_feat_type reconciliation; /* SP or NN */
137 u8 default_value; /* as in 6.4 */
138 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
140 * Lookup table for location and type of features (from RFC 4340/4342)
141 * +--------------------------+----+-----+----+----+---------+-----------+
142 * | Feature | Location | Reconc. | Initial | Section |
143 * | | RX | TX | SP | NN | Value | Reference |
144 * +--------------------------+----+-----+----+----+---------+-----------+
145 * | DCCPF_CCID | | X | X | | 2 | 10 |
146 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
147 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
148 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
149 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
150 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
151 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
152 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
153 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
154 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
155 * +--------------------------+----+-----+----+----+---------+-----------+
157 } dccp_feat_table[] = {
158 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
159 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
160 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
161 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
162 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
163 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
164 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
165 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
166 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
167 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
169 #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
172 * dccp_feat_index - Hash function to map feature number into array position
173 * Returns consecutive array index or -1 if the feature is not understood.
175 static int dccp_feat_index(u8 feat_num)
177 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
178 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
179 return feat_num - 1;
182 * Other features: add cases for new feature types here after adding
183 * them to the above table.
185 switch (feat_num) {
186 case DCCPF_SEND_LEV_RATE:
187 return DCCP_FEAT_SUPPORTED_MAX - 1;
189 return -1;
192 static u8 dccp_feat_type(u8 feat_num)
194 int idx = dccp_feat_index(feat_num);
196 if (idx < 0)
197 return FEAT_UNKNOWN;
198 return dccp_feat_table[idx].reconciliation;
201 static int dccp_feat_default_value(u8 feat_num)
203 int idx = dccp_feat_index(feat_num);
205 * There are no default values for unknown features, so encountering a
206 * negative index here indicates a serious problem somewhere else.
208 DCCP_BUG_ON(idx < 0);
210 return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
214 * Debugging and verbose-printing section
216 static const char *dccp_feat_fname(const u8 feat)
218 static const char *const feature_names[] = {
219 [DCCPF_RESERVED] = "Reserved",
220 [DCCPF_CCID] = "CCID",
221 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
222 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
223 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
224 [DCCPF_ACK_RATIO] = "Ack Ratio",
225 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
226 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
227 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
228 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
230 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
231 return feature_names[DCCPF_RESERVED];
233 if (feat == DCCPF_SEND_LEV_RATE)
234 return "Send Loss Event Rate";
235 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
236 return "CCID-specific";
238 return feature_names[feat];
241 static const char *const dccp_feat_sname[] = {
242 "DEFAULT", "INITIALISING", "CHANGING", "UNSTABLE", "STABLE",
245 #ifdef CONFIG_IP_DCCP_DEBUG
246 static const char *dccp_feat_oname(const u8 opt)
248 switch (opt) {
249 case DCCPO_CHANGE_L: return "Change_L";
250 case DCCPO_CONFIRM_L: return "Confirm_L";
251 case DCCPO_CHANGE_R: return "Change_R";
252 case DCCPO_CONFIRM_R: return "Confirm_R";
254 return NULL;
257 static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
259 u8 i, type = dccp_feat_type(feat_num);
261 if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL))
262 dccp_pr_debug_cat("(NULL)");
263 else if (type == FEAT_SP)
264 for (i = 0; i < val->sp.len; i++)
265 dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]);
266 else if (type == FEAT_NN)
267 dccp_pr_debug_cat("%llu", (unsigned long long)val->nn);
268 else
269 dccp_pr_debug_cat("unknown type %u", type);
272 static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
274 u8 type = dccp_feat_type(feat_num);
275 dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
277 if (type == FEAT_NN)
278 fval.nn = dccp_decode_value_var(list, len);
279 dccp_feat_printval(feat_num, &fval);
282 static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
284 dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote",
285 dccp_feat_fname(entry->feat_num));
286 dccp_feat_printval(entry->feat_num, &entry->val);
287 dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
288 entry->needs_confirm ? "(Confirm pending)" : "");
291 #define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \
292 dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\
293 dccp_feat_printvals(feat, val, len); \
294 dccp_pr_debug_cat(") %s\n", mandatory ? "!" : ""); } while (0)
296 #define dccp_feat_print_fnlist(fn_list) { \
297 const struct dccp_feat_entry *___entry; \
299 dccp_pr_debug("List Dump:\n"); \
300 list_for_each_entry(___entry, fn_list, node) \
301 dccp_feat_print_entry(___entry); \
303 #else /* ! CONFIG_IP_DCCP_DEBUG */
304 #define dccp_feat_print_opt(opt, feat, val, len, mandatory)
305 #define dccp_feat_print_fnlist(fn_list)
306 #endif
308 static int __dccp_feat_activate(struct sock *sk, const int idx,
309 const bool is_local, dccp_feat_val const *fval)
311 bool rx;
312 u64 val;
314 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
315 return -1;
316 if (dccp_feat_table[idx].activation_hdlr == NULL)
317 return 0;
319 if (fval == NULL) {
320 val = dccp_feat_table[idx].default_value;
321 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
322 if (fval->sp.vec == NULL) {
324 * This can happen when an empty Confirm is sent
325 * for an SP (i.e. known) feature. In this case
326 * we would be using the default anyway.
328 DCCP_CRIT("Feature #%d undefined: using default", idx);
329 val = dccp_feat_table[idx].default_value;
330 } else {
331 val = fval->sp.vec[0];
333 } else {
334 val = fval->nn;
337 /* Location is RX if this is a local-RX or remote-TX feature */
338 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
340 dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
341 dccp_feat_fname(dccp_feat_table[idx].feat_num),
342 fval ? "" : "default ", (unsigned long long)val);
344 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
348 * dccp_feat_activate - Activate feature value on socket
349 * @sk: fully connected DCCP socket (after handshake is complete)
350 * @feat_num: feature to activate, one of %dccp_feature_numbers
351 * @local: whether local (1) or remote (0) @feat_num is meant
352 * @fval: the value (SP or NN) to activate, or NULL to use the default value
353 * For general use this function is preferable over __dccp_feat_activate().
355 static int dccp_feat_activate(struct sock *sk, u8 feat_num, bool local,
356 dccp_feat_val const *fval)
358 return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval);
361 /* Test for "Req'd" feature (RFC 4340, 6.4) */
362 static inline int dccp_feat_must_be_understood(u8 feat_num)
364 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
365 feat_num == DCCPF_SEQUENCE_WINDOW;
368 /* copy constructor, fval must not already contain allocated memory */
369 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
371 fval->sp.len = len;
372 if (fval->sp.len > 0) {
373 fval->sp.vec = kmemdup(val, len, gfp_any());
374 if (fval->sp.vec == NULL) {
375 fval->sp.len = 0;
376 return -ENOBUFS;
379 return 0;
382 static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
384 if (unlikely(val == NULL))
385 return;
386 if (dccp_feat_type(feat_num) == FEAT_SP)
387 kfree(val->sp.vec);
388 memset(val, 0, sizeof(*val));
391 static struct dccp_feat_entry *
392 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
394 struct dccp_feat_entry *new;
395 u8 type = dccp_feat_type(original->feat_num);
397 if (type == FEAT_UNKNOWN)
398 return NULL;
400 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
401 if (new == NULL)
402 return NULL;
404 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
405 original->val.sp.vec,
406 original->val.sp.len)) {
407 kfree(new);
408 return NULL;
410 return new;
413 static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
415 if (entry != NULL) {
416 dccp_feat_val_destructor(entry->feat_num, &entry->val);
417 kfree(entry);
422 * List management functions
424 * Feature negotiation lists rely on and maintain the following invariants:
425 * - each feat_num in the list is known, i.e. we know its type and default value
426 * - each feat_num/is_local combination is unique (old entries are overwritten)
427 * - SP values are always freshly allocated
428 * - list is sorted in increasing order of feature number (faster lookup)
430 static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
431 u8 feat_num, bool is_local)
433 struct dccp_feat_entry *entry;
435 list_for_each_entry(entry, fn_list, node) {
436 if (entry->feat_num == feat_num && entry->is_local == is_local)
437 return entry;
438 else if (entry->feat_num > feat_num)
439 break;
441 return NULL;
445 * dccp_feat_entry_new - Central list update routine (called by all others)
446 * @head: list to add to
447 * @feat: feature number
448 * @local: whether the local (1) or remote feature with number @feat is meant
449 * This is the only constructor and serves to ensure the above invariants.
451 static struct dccp_feat_entry *
452 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
454 struct dccp_feat_entry *entry;
456 list_for_each_entry(entry, head, node)
457 if (entry->feat_num == feat && entry->is_local == local) {
458 dccp_feat_val_destructor(entry->feat_num, &entry->val);
459 return entry;
460 } else if (entry->feat_num > feat) {
461 head = &entry->node;
462 break;
465 entry = kmalloc(sizeof(*entry), gfp_any());
466 if (entry != NULL) {
467 entry->feat_num = feat;
468 entry->is_local = local;
469 list_add_tail(&entry->node, head);
471 return entry;
475 * dccp_feat_push_change - Add/overwrite a Change option in the list
476 * @fn_list: feature-negotiation list to update
477 * @feat: one of %dccp_feature_numbers
478 * @local: whether local (1) or remote (0) @feat_num is meant
479 * @needs_mandatory: whether to use Mandatory feature negotiation options
480 * @fval: pointer to NN/SP value to be inserted (will be copied)
482 static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
483 u8 mandatory, dccp_feat_val *fval)
485 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
487 if (new == NULL)
488 return -ENOMEM;
490 new->feat_num = feat;
491 new->is_local = local;
492 new->state = FEAT_INITIALISING;
493 new->needs_confirm = false;
494 new->empty_confirm = false;
495 new->val = *fval;
496 new->needs_mandatory = mandatory;
498 return 0;
502 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
503 * @fn_list: feature-negotiation list to add to
504 * @feat: one of %dccp_feature_numbers
505 * @local: whether local (1) or remote (0) @feat_num is being confirmed
506 * @fval: pointer to NN/SP value to be inserted or NULL
507 * Returns 0 on success, a Reset code for further processing otherwise.
509 static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
510 dccp_feat_val *fval)
512 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
514 if (new == NULL)
515 return DCCP_RESET_CODE_TOO_BUSY;
517 new->feat_num = feat;
518 new->is_local = local;
519 new->state = FEAT_STABLE; /* transition in 6.6.2 */
520 new->needs_confirm = true;
521 new->empty_confirm = (fval == NULL);
522 new->val.nn = 0; /* zeroes the whole structure */
523 if (!new->empty_confirm)
524 new->val = *fval;
525 new->needs_mandatory = false;
527 return 0;
530 static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
532 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
535 static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
537 list_del(&entry->node);
538 dccp_feat_entry_destructor(entry);
541 void dccp_feat_list_purge(struct list_head *fn_list)
543 struct dccp_feat_entry *entry, *next;
545 list_for_each_entry_safe(entry, next, fn_list, node)
546 dccp_feat_entry_destructor(entry);
547 INIT_LIST_HEAD(fn_list);
549 EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
551 /* generate @to as full clone of @from - @to must not contain any nodes */
552 int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
554 struct dccp_feat_entry *entry, *new;
556 INIT_LIST_HEAD(to);
557 list_for_each_entry(entry, from, node) {
558 new = dccp_feat_clone_entry(entry);
559 if (new == NULL)
560 goto cloning_failed;
561 list_add_tail(&new->node, to);
563 return 0;
565 cloning_failed:
566 dccp_feat_list_purge(to);
567 return -ENOMEM;
571 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
572 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
573 * incoming options are accepted as long as their values are valid.
575 static u8 dccp_feat_valid_nn_length(u8 feat_num)
577 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
578 return 2;
579 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
580 return 6;
581 return 0;
584 static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
586 switch (feat_num) {
587 case DCCPF_ACK_RATIO:
588 return val <= DCCPF_ACK_RATIO_MAX;
589 case DCCPF_SEQUENCE_WINDOW:
590 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
592 return 0; /* feature unknown - so we can't tell */
595 /* check that SP values are within the ranges defined in RFC 4340 */
596 static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
598 switch (feat_num) {
599 case DCCPF_CCID:
600 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
601 /* Type-check Boolean feature values: */
602 case DCCPF_SHORT_SEQNOS:
603 case DCCPF_ECN_INCAPABLE:
604 case DCCPF_SEND_ACK_VECTOR:
605 case DCCPF_SEND_NDP_COUNT:
606 case DCCPF_DATA_CHECKSUM:
607 case DCCPF_SEND_LEV_RATE:
608 return val < 2;
609 case DCCPF_MIN_CSUM_COVER:
610 return val < 16;
612 return 0; /* feature unknown */
615 static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
617 if (sp_list == NULL || sp_len < 1)
618 return 0;
619 while (sp_len--)
620 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
621 return 0;
622 return 1;
626 * dccp_feat_insert_opts - Generate FN options from current list state
627 * @skb: next sk_buff to be sent to the peer
628 * @dp: for client during handshake and general negotiation
629 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
631 int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
632 struct sk_buff *skb)
634 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
635 struct dccp_feat_entry *pos, *next;
636 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
637 bool rpt;
639 /* put entries into @skb in the order they appear in the list */
640 list_for_each_entry_safe_reverse(pos, next, fn, node) {
641 opt = dccp_feat_genopt(pos);
642 type = dccp_feat_type(pos->feat_num);
643 rpt = false;
645 if (pos->empty_confirm) {
646 len = 0;
647 ptr = NULL;
648 } else {
649 if (type == FEAT_SP) {
650 len = pos->val.sp.len;
651 ptr = pos->val.sp.vec;
652 rpt = pos->needs_confirm;
653 } else if (type == FEAT_NN) {
654 len = dccp_feat_valid_nn_length(pos->feat_num);
655 ptr = nn_in_nbo;
656 dccp_encode_value_var(pos->val.nn, ptr, len);
657 } else {
658 DCCP_BUG("unknown feature %u", pos->feat_num);
659 return -1;
662 dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
664 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
665 return -1;
666 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
667 return -1;
669 if (skb->sk->sk_state == DCCP_OPEN &&
670 (opt == DCCPO_CONFIRM_R || opt == DCCPO_CONFIRM_L)) {
672 * Confirms don't get retransmitted (6.6.3) once the
673 * connection is in state OPEN
675 dccp_feat_list_pop(pos);
676 } else {
678 * Enter CHANGING after transmitting the Change
679 * option (6.6.2).
681 if (pos->state == FEAT_INITIALISING)
682 pos->state = FEAT_CHANGING;
685 return 0;
689 * __feat_register_nn - Register new NN value on socket
690 * @fn: feature-negotiation list to register with
691 * @feat: an NN feature from %dccp_feature_numbers
692 * @mandatory: use Mandatory option if 1
693 * @nn_val: value to register (restricted to 4 bytes)
694 * Note that NN features are local by definition (RFC 4340, 6.3.2).
696 static int __feat_register_nn(struct list_head *fn, u8 feat,
697 u8 mandatory, u64 nn_val)
699 dccp_feat_val fval = { .nn = nn_val };
701 if (dccp_feat_type(feat) != FEAT_NN ||
702 !dccp_feat_is_valid_nn_val(feat, nn_val))
703 return -EINVAL;
705 /* Don't bother with default values, they will be activated anyway. */
706 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
707 return 0;
709 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
713 * __feat_register_sp - Register new SP value/list on socket
714 * @fn: feature-negotiation list to register with
715 * @feat: an SP feature from %dccp_feature_numbers
716 * @is_local: whether the local (1) or the remote (0) @feat is meant
717 * @mandatory: use Mandatory option if 1
718 * @sp_val: SP value followed by optional preference list
719 * @sp_len: length of @sp_val in bytes
721 static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
722 u8 mandatory, u8 const *sp_val, u8 sp_len)
724 dccp_feat_val fval;
726 if (dccp_feat_type(feat) != FEAT_SP ||
727 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
728 return -EINVAL;
730 /* Avoid negotiating alien CCIDs by only advertising supported ones */
731 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
732 return -EOPNOTSUPP;
734 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
735 return -ENOMEM;
737 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
741 * dccp_feat_register_sp - Register requests to change SP feature values
742 * @sk: client or listening socket
743 * @feat: one of %dccp_feature_numbers
744 * @is_local: whether the local (1) or remote (0) @feat is meant
745 * @list: array of preferred values, in descending order of preference
746 * @len: length of @list in bytes
748 int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
749 u8 const *list, u8 len)
750 { /* any changes must be registered before establishing the connection */
751 if (sk->sk_state != DCCP_CLOSED)
752 return -EISCONN;
753 if (dccp_feat_type(feat) != FEAT_SP)
754 return -EINVAL;
755 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
756 0, list, len);
760 * dccp_feat_nn_get - Query current/pending value of NN feature
761 * @sk: DCCP socket of an established connection
762 * @feat: NN feature number from %dccp_feature_numbers
763 * For a known NN feature, returns value currently being negotiated, or
764 * current (confirmed) value if no negotiation is going on.
766 u64 dccp_feat_nn_get(struct sock *sk, u8 feat)
768 if (dccp_feat_type(feat) == FEAT_NN) {
769 struct dccp_sock *dp = dccp_sk(sk);
770 struct dccp_feat_entry *entry;
772 entry = dccp_feat_list_lookup(&dp->dccps_featneg, feat, 1);
773 if (entry != NULL)
774 return entry->val.nn;
776 switch (feat) {
777 case DCCPF_ACK_RATIO:
778 return dp->dccps_l_ack_ratio;
779 case DCCPF_SEQUENCE_WINDOW:
780 return dp->dccps_l_seq_win;
783 DCCP_BUG("attempt to look up unsupported feature %u", feat);
784 return 0;
786 EXPORT_SYMBOL_GPL(dccp_feat_nn_get);
789 * dccp_feat_signal_nn_change - Update NN values for an established connection
790 * @sk: DCCP socket of an established connection
791 * @feat: NN feature number from %dccp_feature_numbers
792 * @nn_val: the new value to use
793 * This function is used to communicate NN updates out-of-band.
795 int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
797 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
798 dccp_feat_val fval = { .nn = nn_val };
799 struct dccp_feat_entry *entry;
801 if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
802 return 0;
804 if (dccp_feat_type(feat) != FEAT_NN ||
805 !dccp_feat_is_valid_nn_val(feat, nn_val))
806 return -EINVAL;
808 if (nn_val == dccp_feat_nn_get(sk, feat))
809 return 0; /* already set or negotiation under way */
811 entry = dccp_feat_list_lookup(fn, feat, 1);
812 if (entry != NULL) {
813 dccp_pr_debug("Clobbering existing NN entry %llu -> %llu\n",
814 (unsigned long long)entry->val.nn,
815 (unsigned long long)nn_val);
816 dccp_feat_list_pop(entry);
819 inet_csk_schedule_ack(sk);
820 return dccp_feat_push_change(fn, feat, 1, 0, &fval);
822 EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
825 * Tracking features whose value depend on the choice of CCID
827 * This is designed with an extension in mind so that a list walk could be done
828 * before activating any features. However, the existing framework was found to
829 * work satisfactorily up until now, the automatic verification is left open.
830 * When adding new CCIDs, add a corresponding dependency table here.
832 static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
834 static const struct ccid_dependency ccid2_dependencies[2][2] = {
836 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
837 * feature and Send Ack Vector is an RX feature, `is_local'
838 * needs to be reversed.
840 { /* Dependencies of the receiver-side (remote) CCID2 */
842 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
843 .is_local = true,
844 .is_mandatory = true,
845 .val = 1
847 { 0, 0, 0, 0 }
849 { /* Dependencies of the sender-side (local) CCID2 */
851 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
852 .is_local = false,
853 .is_mandatory = true,
854 .val = 1
856 { 0, 0, 0, 0 }
859 static const struct ccid_dependency ccid3_dependencies[2][5] = {
860 { /*
861 * Dependencies of the receiver-side CCID3
863 { /* locally disable Ack Vectors */
864 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
865 .is_local = true,
866 .is_mandatory = false,
867 .val = 0
869 { /* see below why Send Loss Event Rate is on */
870 .dependent_feat = DCCPF_SEND_LEV_RATE,
871 .is_local = true,
872 .is_mandatory = true,
873 .val = 1
875 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
876 .dependent_feat = DCCPF_SEND_NDP_COUNT,
877 .is_local = false,
878 .is_mandatory = true,
879 .val = 1
881 { 0, 0, 0, 0 },
883 { /*
884 * CCID3 at the TX side: we request that the HC-receiver
885 * will not send Ack Vectors (they will be ignored, so
886 * Mandatory is not set); we enable Send Loss Event Rate
887 * (Mandatory since the implementation does not support
888 * the Loss Intervals option of RFC 4342, 8.6).
889 * The last two options are for peer's information only.
892 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
893 .is_local = false,
894 .is_mandatory = false,
895 .val = 0
898 .dependent_feat = DCCPF_SEND_LEV_RATE,
899 .is_local = false,
900 .is_mandatory = true,
901 .val = 1
903 { /* this CCID does not support Ack Ratio */
904 .dependent_feat = DCCPF_ACK_RATIO,
905 .is_local = true,
906 .is_mandatory = false,
907 .val = 0
909 { /* tell receiver we are sending NDP counts */
910 .dependent_feat = DCCPF_SEND_NDP_COUNT,
911 .is_local = true,
912 .is_mandatory = false,
913 .val = 1
915 { 0, 0, 0, 0 }
918 switch (ccid) {
919 case DCCPC_CCID2:
920 return ccid2_dependencies[is_local];
921 case DCCPC_CCID3:
922 return ccid3_dependencies[is_local];
923 default:
924 return NULL;
929 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
930 * @fn: feature-negotiation list to update
931 * @id: CCID number to track
932 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
933 * This function needs to be called after registering all other features.
935 static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
937 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
938 int i, rc = (table == NULL);
940 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
941 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
942 rc = __feat_register_sp(fn, table[i].dependent_feat,
943 table[i].is_local,
944 table[i].is_mandatory,
945 &table[i].val, 1);
946 else
947 rc = __feat_register_nn(fn, table[i].dependent_feat,
948 table[i].is_mandatory,
949 table[i].val);
950 return rc;
954 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
955 * @dp: client or listening socket (settings will be inherited)
956 * This is called after all registrations (socket initialisation, sysctls, and
957 * sockopt calls), and before sending the first packet containing Change options
958 * (ie. client-Request or server-Response), to ensure internal consistency.
960 int dccp_feat_finalise_settings(struct dccp_sock *dp)
962 struct list_head *fn = &dp->dccps_featneg;
963 struct dccp_feat_entry *entry;
964 int i = 2, ccids[2] = { -1, -1 };
967 * Propagating CCIDs:
968 * 1) not useful to propagate CCID settings if this host advertises more
969 * than one CCID: the choice of CCID may still change - if this is
970 * the client, or if this is the server and the client sends
971 * singleton CCID values.
972 * 2) since is that propagate_ccid changes the list, we defer changing
973 * the sorted list until after the traversal.
975 list_for_each_entry(entry, fn, node)
976 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
977 ccids[entry->is_local] = entry->val.sp.vec[0];
978 while (i--)
979 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
980 return -1;
981 dccp_feat_print_fnlist(fn);
982 return 0;
986 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
987 * It is the server which resolves the dependencies once the CCID has been
988 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
990 int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
992 struct list_head *fn = &dreq->dreq_featneg;
993 struct dccp_feat_entry *entry;
994 u8 is_local, ccid;
996 for (is_local = 0; is_local <= 1; is_local++) {
997 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
999 if (entry != NULL && !entry->empty_confirm)
1000 ccid = entry->val.sp.vec[0];
1001 else
1002 ccid = dccp_feat_default_value(DCCPF_CCID);
1004 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
1005 return -1;
1007 return 0;
1010 /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
1011 static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
1013 u8 c, s;
1015 for (s = 0; s < slen; s++)
1016 for (c = 0; c < clen; c++)
1017 if (servlist[s] == clilist[c])
1018 return servlist[s];
1019 return -1;
1023 * dccp_feat_prefer - Move preferred entry to the start of array
1024 * Reorder the @array_len elements in @array so that @preferred_value comes
1025 * first. Returns >0 to indicate that @preferred_value does occur in @array.
1027 static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
1029 u8 i, does_occur = 0;
1031 if (array != NULL) {
1032 for (i = 0; i < array_len; i++)
1033 if (array[i] == preferred_value) {
1034 array[i] = array[0];
1035 does_occur++;
1037 if (does_occur)
1038 array[0] = preferred_value;
1040 return does_occur;
1044 * dccp_feat_reconcile - Reconcile SP preference lists
1045 * @fval: SP list to reconcile into
1046 * @arr: received SP preference list
1047 * @len: length of @arr in bytes
1048 * @is_server: whether this side is the server (and @fv is the server's list)
1049 * @reorder: whether to reorder the list in @fv after reconciling with @arr
1050 * When successful, > 0 is returned and the reconciled list is in @fval.
1051 * A value of 0 means that negotiation failed (no shared entry).
1053 static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
1054 bool is_server, bool reorder)
1056 int rc;
1058 if (!fv->sp.vec || !arr) {
1059 DCCP_CRIT("NULL feature value or array");
1060 return 0;
1063 if (is_server)
1064 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
1065 else
1066 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
1068 if (!reorder)
1069 return rc;
1070 if (rc < 0)
1071 return 0;
1074 * Reorder list: used for activating features and in dccp_insert_fn_opt.
1076 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
1080 * dccp_feat_change_recv - Process incoming ChangeL/R options
1081 * @fn: feature-negotiation list to update
1082 * @is_mandatory: whether the Change was preceded by a Mandatory option
1083 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1084 * @feat: one of %dccp_feature_numbers
1085 * @val: NN value or SP value/preference list
1086 * @len: length of @val in bytes
1087 * @server: whether this node is the server (1) or the client (0)
1089 static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1090 u8 feat, u8 *val, u8 len, const bool server)
1092 u8 defval, type = dccp_feat_type(feat);
1093 const bool local = (opt == DCCPO_CHANGE_R);
1094 struct dccp_feat_entry *entry;
1095 dccp_feat_val fval;
1097 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
1098 goto unknown_feature_or_value;
1100 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1103 * Negotiation of NN features: Change R is invalid, so there is no
1104 * simultaneous negotiation; hence we do not look up in the list.
1106 if (type == FEAT_NN) {
1107 if (local || len > sizeof(fval.nn))
1108 goto unknown_feature_or_value;
1110 /* 6.3.2: "The feature remote MUST accept any valid value..." */
1111 fval.nn = dccp_decode_value_var(val, len);
1112 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1113 goto unknown_feature_or_value;
1115 return dccp_feat_push_confirm(fn, feat, local, &fval);
1119 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
1121 entry = dccp_feat_list_lookup(fn, feat, local);
1122 if (entry == NULL) {
1124 * No particular preferences have been registered. We deal with
1125 * this situation by assuming that all valid values are equally
1126 * acceptable, and apply the following checks:
1127 * - if the peer's list is a singleton, we accept a valid value;
1128 * - if we are the server, we first try to see if the peer (the
1129 * client) advertises the default value. If yes, we use it,
1130 * otherwise we accept the preferred value;
1131 * - else if we are the client, we use the first list element.
1133 if (dccp_feat_clone_sp_val(&fval, val, 1))
1134 return DCCP_RESET_CODE_TOO_BUSY;
1136 if (len > 1 && server) {
1137 defval = dccp_feat_default_value(feat);
1138 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1139 fval.sp.vec[0] = defval;
1140 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1141 kfree(fval.sp.vec);
1142 goto unknown_feature_or_value;
1145 /* Treat unsupported CCIDs like invalid values */
1146 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1147 kfree(fval.sp.vec);
1148 goto not_valid_or_not_known;
1151 return dccp_feat_push_confirm(fn, feat, local, &fval);
1153 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
1154 return 0;
1157 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1158 entry->empty_confirm = false;
1159 } else if (is_mandatory) {
1160 return DCCP_RESET_CODE_MANDATORY_ERROR;
1161 } else if (entry->state == FEAT_INITIALISING) {
1163 * Failed simultaneous negotiation (server only): try to `save'
1164 * the connection by checking whether entry contains the default
1165 * value for @feat. If yes, send an empty Confirm to signal that
1166 * the received Change was not understood - which implies using
1167 * the default value.
1168 * If this also fails, we use Reset as the last resort.
1170 WARN_ON(!server);
1171 defval = dccp_feat_default_value(feat);
1172 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1173 return DCCP_RESET_CODE_OPTION_ERROR;
1174 entry->empty_confirm = true;
1176 entry->needs_confirm = true;
1177 entry->needs_mandatory = false;
1178 entry->state = FEAT_STABLE;
1179 return 0;
1181 unknown_feature_or_value:
1182 if (!is_mandatory)
1183 return dccp_push_empty_confirm(fn, feat, local);
1185 not_valid_or_not_known:
1186 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1187 : DCCP_RESET_CODE_OPTION_ERROR;
1191 * dccp_feat_confirm_recv - Process received Confirm options
1192 * @fn: feature-negotiation list to update
1193 * @is_mandatory: whether @opt was preceded by a Mandatory option
1194 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1195 * @feat: one of %dccp_feature_numbers
1196 * @val: NN value or SP value/preference list
1197 * @len: length of @val in bytes
1198 * @server: whether this node is server (1) or client (0)
1200 static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1201 u8 feat, u8 *val, u8 len, const bool server)
1203 u8 *plist, plen, type = dccp_feat_type(feat);
1204 const bool local = (opt == DCCPO_CONFIRM_R);
1205 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1207 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1209 if (entry == NULL) { /* nothing queued: ignore or handle error */
1210 if (is_mandatory && type == FEAT_UNKNOWN)
1211 return DCCP_RESET_CODE_MANDATORY_ERROR;
1213 if (!local && type == FEAT_NN) /* 6.3.2 */
1214 goto confirmation_failed;
1215 return 0;
1218 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
1219 return 0;
1221 if (len == 0) {
1222 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1223 goto confirmation_failed;
1225 * Empty Confirm during connection setup: this means reverting
1226 * to the `old' value, which in this case is the default. Since
1227 * we handle default values automatically when no other values
1228 * have been set, we revert to the old value by removing this
1229 * entry from the list.
1231 dccp_feat_list_pop(entry);
1232 return 0;
1235 if (type == FEAT_NN) {
1236 if (len > sizeof(entry->val.nn))
1237 goto confirmation_failed;
1239 if (entry->val.nn == dccp_decode_value_var(val, len))
1240 goto confirmation_succeeded;
1242 DCCP_WARN("Bogus Confirm for non-existing value\n");
1243 goto confirmation_failed;
1247 * Parsing SP Confirms: the first element of @val is the preferred
1248 * SP value which the peer confirms, the remainder depends on @len.
1249 * Note that only the confirmed value need to be a valid SP value.
1251 if (!dccp_feat_is_valid_sp_val(feat, *val))
1252 goto confirmation_failed;
1254 if (len == 1) { /* peer didn't supply a preference list */
1255 plist = val;
1256 plen = len;
1257 } else { /* preferred value + preference list */
1258 plist = val + 1;
1259 plen = len - 1;
1262 /* Check whether the peer got the reconciliation right (6.6.8) */
1263 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1264 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1265 return DCCP_RESET_CODE_OPTION_ERROR;
1267 entry->val.sp.vec[0] = *val;
1269 confirmation_succeeded:
1270 entry->state = FEAT_STABLE;
1271 return 0;
1273 confirmation_failed:
1274 DCCP_WARN("Confirmation failed\n");
1275 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1276 : DCCP_RESET_CODE_OPTION_ERROR;
1280 * dccp_feat_handle_nn_established - Fast-path reception of NN options
1281 * @sk: socket of an established DCCP connection
1282 * @mandatory: whether @opt was preceded by a Mandatory option
1283 * @opt: %DCCPO_CHANGE_L | %DCCPO_CONFIRM_R (NN only)
1284 * @feat: NN number, one of %dccp_feature_numbers
1285 * @val: NN value
1286 * @len: length of @val in bytes
1287 * This function combines the functionality of change_recv/confirm_recv, with
1288 * the following differences (reset codes are the same):
1289 * - cleanup after receiving the Confirm;
1290 * - values are directly activated after successful parsing;
1291 * - deliberately restricted to NN features.
1292 * The restriction to NN features is essential since SP features can have non-
1293 * predictable outcomes (depending on the remote configuration), and are inter-
1294 * dependent (CCIDs for instance cause further dependencies).
1296 static u8 dccp_feat_handle_nn_established(struct sock *sk, u8 mandatory, u8 opt,
1297 u8 feat, u8 *val, u8 len)
1299 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1300 const bool local = (opt == DCCPO_CONFIRM_R);
1301 struct dccp_feat_entry *entry;
1302 u8 type = dccp_feat_type(feat);
1303 dccp_feat_val fval;
1305 dccp_feat_print_opt(opt, feat, val, len, mandatory);
1307 /* Ignore non-mandatory unknown and non-NN features */
1308 if (type == FEAT_UNKNOWN) {
1309 if (local && !mandatory)
1310 return 0;
1311 goto fast_path_unknown;
1312 } else if (type != FEAT_NN) {
1313 return 0;
1317 * We don't accept empty Confirms, since in fast-path feature
1318 * negotiation the values are enabled immediately after sending
1319 * the Change option.
1320 * Empty Changes on the other hand are invalid (RFC 4340, 6.1).
1322 if (len == 0 || len > sizeof(fval.nn))
1323 goto fast_path_unknown;
1325 if (opt == DCCPO_CHANGE_L) {
1326 fval.nn = dccp_decode_value_var(val, len);
1327 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1328 goto fast_path_unknown;
1330 if (dccp_feat_push_confirm(fn, feat, local, &fval) ||
1331 dccp_feat_activate(sk, feat, local, &fval))
1332 return DCCP_RESET_CODE_TOO_BUSY;
1334 /* set the `Ack Pending' flag to piggyback a Confirm */
1335 inet_csk_schedule_ack(sk);
1337 } else if (opt == DCCPO_CONFIRM_R) {
1338 entry = dccp_feat_list_lookup(fn, feat, local);
1339 if (entry == NULL || entry->state != FEAT_CHANGING)
1340 return 0;
1342 fval.nn = dccp_decode_value_var(val, len);
1344 * Just ignore a value that doesn't match our current value.
1345 * If the option changes twice within two RTTs, then at least
1346 * one CONFIRM will be received for the old value after a
1347 * new CHANGE was sent.
1349 if (fval.nn != entry->val.nn)
1350 return 0;
1352 /* Only activate after receiving the Confirm option (6.6.1). */
1353 dccp_feat_activate(sk, feat, local, &fval);
1355 /* It has been confirmed - so remove the entry */
1356 dccp_feat_list_pop(entry);
1358 } else {
1359 DCCP_WARN("Received illegal option %u\n", opt);
1360 goto fast_path_failed;
1362 return 0;
1364 fast_path_unknown:
1365 if (!mandatory)
1366 return dccp_push_empty_confirm(fn, feat, local);
1368 fast_path_failed:
1369 return mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1370 : DCCP_RESET_CODE_OPTION_ERROR;
1374 * dccp_feat_parse_options - Process Feature-Negotiation Options
1375 * @sk: for general use and used by the client during connection setup
1376 * @dreq: used by the server during connection setup
1377 * @mandatory: whether @opt was preceded by a Mandatory option
1378 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1379 * @feat: one of %dccp_feature_numbers
1380 * @val: value contents of @opt
1381 * @len: length of @val in bytes
1382 * Returns 0 on success, a Reset code for ending the connection otherwise.
1384 int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1385 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1387 struct dccp_sock *dp = dccp_sk(sk);
1388 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1389 bool server = false;
1391 switch (sk->sk_state) {
1393 * Negotiation during connection setup
1395 case DCCP_LISTEN:
1396 server = true; /* fall through */
1397 case DCCP_REQUESTING:
1398 switch (opt) {
1399 case DCCPO_CHANGE_L:
1400 case DCCPO_CHANGE_R:
1401 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1402 val, len, server);
1403 case DCCPO_CONFIRM_R:
1404 case DCCPO_CONFIRM_L:
1405 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1406 val, len, server);
1408 break;
1410 * Support for exchanging NN options on an established connection.
1412 case DCCP_OPEN:
1413 case DCCP_PARTOPEN:
1414 return dccp_feat_handle_nn_established(sk, mandatory, opt, feat,
1415 val, len);
1417 return 0; /* ignore FN options in all other states */
1421 * dccp_feat_init - Seed feature negotiation with host-specific defaults
1422 * This initialises global defaults, depending on the value of the sysctls.
1423 * These can later be overridden by registering changes via setsockopt calls.
1424 * The last link in the chain is finalise_settings, to make sure that between
1425 * here and the start of actual feature negotiation no inconsistencies enter.
1427 * All features not appearing below use either defaults or are otherwise
1428 * later adjusted through dccp_feat_finalise_settings().
1430 int dccp_feat_init(struct sock *sk)
1432 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1433 u8 on = 1, off = 0;
1434 int rc;
1435 struct {
1436 u8 *val;
1437 u8 len;
1438 } tx, rx;
1440 /* Non-negotiable (NN) features */
1441 rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
1442 sysctl_dccp_sequence_window);
1443 if (rc)
1444 return rc;
1446 /* Server-priority (SP) features */
1448 /* Advertise that short seqnos are not supported (7.6.1) */
1449 rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1450 if (rc)
1451 return rc;
1453 /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1454 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1455 if (rc)
1456 return rc;
1459 * We advertise the available list of CCIDs and reorder according to
1460 * preferences, to avoid failure resulting from negotiating different
1461 * singleton values (which always leads to failure).
1462 * These settings can still (later) be overridden via sockopts.
1464 if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1465 ccid_get_builtin_ccids(&rx.val, &rx.len))
1466 return -ENOBUFS;
1468 if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1469 !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
1470 goto free_ccid_lists;
1472 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1473 if (rc)
1474 goto free_ccid_lists;
1476 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1478 free_ccid_lists:
1479 kfree(tx.val);
1480 kfree(rx.val);
1481 return rc;
1484 int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1486 struct dccp_sock *dp = dccp_sk(sk);
1487 struct dccp_feat_entry *cur, *next;
1488 int idx;
1489 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1490 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1493 list_for_each_entry(cur, fn_list, node) {
1495 * An empty Confirm means that either an unknown feature type
1496 * or an invalid value was present. In the first case there is
1497 * nothing to activate, in the other the default value is used.
1499 if (cur->empty_confirm)
1500 continue;
1502 idx = dccp_feat_index(cur->feat_num);
1503 if (idx < 0) {
1504 DCCP_BUG("Unknown feature %u", cur->feat_num);
1505 goto activation_failed;
1507 if (cur->state != FEAT_STABLE) {
1508 DCCP_CRIT("Negotiation of %s %s failed in state %s",
1509 cur->is_local ? "local" : "remote",
1510 dccp_feat_fname(cur->feat_num),
1511 dccp_feat_sname[cur->state]);
1512 goto activation_failed;
1514 fvals[idx][cur->is_local] = &cur->val;
1518 * Activate in decreasing order of index, so that the CCIDs are always
1519 * activated as the last feature. This avoids the case where a CCID
1520 * relies on the initialisation of one or more features that it depends
1521 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1523 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1524 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1525 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1526 DCCP_CRIT("Could not activate %d", idx);
1527 goto activation_failed;
1530 /* Clean up Change options which have been confirmed already */
1531 list_for_each_entry_safe(cur, next, fn_list, node)
1532 if (!cur->needs_confirm)
1533 dccp_feat_list_pop(cur);
1535 dccp_pr_debug("Activation OK\n");
1536 return 0;
1538 activation_failed:
1540 * We clean up everything that may have been allocated, since
1541 * it is difficult to track at which stage negotiation failed.
1542 * This is ok, since all allocation functions below are robust
1543 * against NULL arguments.
1545 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1546 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1547 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1548 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1549 dp->dccps_hc_rx_ackvec = NULL;
1550 return -1;