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 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>
30 * Feature activation handlers.
32 * These all use an u64 argument, to provide enough room for NN/SP features. At
33 * this stage the negotiated values have been checked to be within their range.
35 static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
37 struct dccp_sock *dp = dccp_sk(sk);
38 struct ccid *new_ccid = ccid_new(ccid, sk, rx, gfp_any());
44 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
45 dp->dccps_hc_rx_ccid = new_ccid;
47 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
48 dp->dccps_hc_tx_ccid = new_ccid;
53 static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
55 struct dccp_sock *dp = dccp_sk(sk);
58 dp->dccps_r_seq_win = seq_win;
59 /* propagate changes to update SWL/SWH */
60 dccp_update_gsr(sk, dp->dccps_gsr);
62 dp->dccps_l_seq_win = seq_win;
63 /* propagate changes to update AWL */
64 dccp_update_gss(sk, dp->dccps_gss);
69 static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
72 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
74 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
78 static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
80 struct dccp_sock *dp = dccp_sk(sk);
83 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
84 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
85 if (dp->dccps_hc_rx_ackvec == NULL)
88 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
89 dp->dccps_hc_rx_ackvec = NULL;
95 static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
98 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
103 * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
104 * `rx' holds when the sending peer informs about his partial coverage via a
105 * ChangeR() option. In the other case, we are the sender and the receiver
106 * announces its coverage via ChangeL() options. The policy here is to honour
107 * such communication by enabling the corresponding partial coverage - but only
108 * if it has not been set manually before; the warning here means that all
109 * packets will be dropped.
111 static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
113 struct dccp_sock *dp = dccp_sk(sk);
116 dp->dccps_pcrlen = cscov;
118 if (dp->dccps_pcslen == 0)
119 dp->dccps_pcslen = cscov;
120 else if (cscov > dp->dccps_pcslen)
121 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
122 dp->dccps_pcslen, (u8)cscov);
127 static const struct {
128 u8 feat_num; /* DCCPF_xxx */
129 enum dccp_feat_type rxtx; /* RX or TX */
130 enum dccp_feat_type reconciliation; /* SP or NN */
131 u8 default_value; /* as in 6.4 */
132 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
134 * Lookup table for location and type of features (from RFC 4340/4342)
135 * +--------------------------+----+-----+----+----+---------+-----------+
136 * | Feature | Location | Reconc. | Initial | Section |
137 * | | RX | TX | SP | NN | Value | Reference |
138 * +--------------------------+----+-----+----+----+---------+-----------+
139 * | DCCPF_CCID | | X | X | | 2 | 10 |
140 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
141 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
142 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
143 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
144 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
145 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
146 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
147 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
148 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
149 * +--------------------------+----+-----+----+----+---------+-----------+
151 } dccp_feat_table[] = {
152 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
153 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
154 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
155 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
156 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
157 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
158 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
159 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
160 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
161 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
163 #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
166 * dccp_feat_index - Hash function to map feature number into array position
167 * Returns consecutive array index or -1 if the feature is not understood.
169 static int dccp_feat_index(u8 feat_num)
171 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
172 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
176 * Other features: add cases for new feature types here after adding
177 * them to the above table.
180 case DCCPF_SEND_LEV_RATE:
181 return DCCP_FEAT_SUPPORTED_MAX - 1;
186 static u8 dccp_feat_type(u8 feat_num)
188 int idx = dccp_feat_index(feat_num);
192 return dccp_feat_table[idx].reconciliation;
195 static int dccp_feat_default_value(u8 feat_num)
197 int idx = dccp_feat_index(feat_num);
199 return idx < 0 ? : dccp_feat_table[idx].default_value;
202 static int __dccp_feat_activate(struct sock *sk, const int idx,
203 const bool is_local, dccp_feat_val const *fval)
208 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
210 if (dccp_feat_table[idx].activation_hdlr == NULL)
214 val = dccp_feat_table[idx].default_value;
215 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
216 if (fval->sp.vec == NULL) {
218 * This can happen when an empty Confirm is sent
219 * for an SP (i.e. known) feature. In this case
220 * we would be using the default anyway.
222 DCCP_CRIT("Feature #%d undefined: using default", idx);
223 val = dccp_feat_table[idx].default_value;
225 val = fval->sp.vec[0];
231 /* Location is RX if this is a local-RX or remote-TX feature */
232 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
234 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
237 /* Test for "Req'd" feature (RFC 4340, 6.4) */
238 static inline int dccp_feat_must_be_understood(u8 feat_num)
240 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
241 feat_num == DCCPF_SEQUENCE_WINDOW;
244 /* copy constructor, fval must not already contain allocated memory */
245 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
248 if (fval->sp.len > 0) {
249 fval->sp.vec = kmemdup(val, len, gfp_any());
250 if (fval->sp.vec == NULL) {
258 static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
260 if (unlikely(val == NULL))
262 if (dccp_feat_type(feat_num) == FEAT_SP)
264 memset(val, 0, sizeof(*val));
267 static struct dccp_feat_entry *
268 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
270 struct dccp_feat_entry *new;
271 u8 type = dccp_feat_type(original->feat_num);
273 if (type == FEAT_UNKNOWN)
276 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
280 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
281 original->val.sp.vec,
282 original->val.sp.len)) {
289 static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
292 dccp_feat_val_destructor(entry->feat_num, &entry->val);
298 * List management functions
300 * Feature negotiation lists rely on and maintain the following invariants:
301 * - each feat_num in the list is known, i.e. we know its type and default value
302 * - each feat_num/is_local combination is unique (old entries are overwritten)
303 * - SP values are always freshly allocated
304 * - list is sorted in increasing order of feature number (faster lookup)
306 static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
307 u8 feat_num, bool is_local)
309 struct dccp_feat_entry *entry;
311 list_for_each_entry(entry, fn_list, node)
312 if (entry->feat_num == feat_num && entry->is_local == is_local)
314 else if (entry->feat_num > feat_num)
320 * dccp_feat_entry_new - Central list update routine (called by all others)
321 * @head: list to add to
322 * @feat: feature number
323 * @local: whether the local (1) or remote feature with number @feat is meant
324 * This is the only constructor and serves to ensure the above invariants.
326 static struct dccp_feat_entry *
327 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
329 struct dccp_feat_entry *entry;
331 list_for_each_entry(entry, head, node)
332 if (entry->feat_num == feat && entry->is_local == local) {
333 dccp_feat_val_destructor(entry->feat_num, &entry->val);
335 } else if (entry->feat_num > feat) {
340 entry = kmalloc(sizeof(*entry), gfp_any());
342 entry->feat_num = feat;
343 entry->is_local = local;
344 list_add_tail(&entry->node, head);
350 * dccp_feat_push_change - Add/overwrite a Change option in the list
351 * @fn_list: feature-negotiation list to update
352 * @feat: one of %dccp_feature_numbers
353 * @local: whether local (1) or remote (0) @feat_num is meant
354 * @needs_mandatory: whether to use Mandatory feature negotiation options
355 * @fval: pointer to NN/SP value to be inserted (will be copied)
357 static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
358 u8 mandatory, dccp_feat_val *fval)
360 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
365 new->feat_num = feat;
366 new->is_local = local;
367 new->state = FEAT_INITIALISING;
368 new->needs_confirm = 0;
369 new->empty_confirm = 0;
371 new->needs_mandatory = mandatory;
377 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
378 * @fn_list: feature-negotiation list to add to
379 * @feat: one of %dccp_feature_numbers
380 * @local: whether local (1) or remote (0) @feat_num is being confirmed
381 * @fval: pointer to NN/SP value to be inserted or NULL
382 * Returns 0 on success, a Reset code for further processing otherwise.
384 static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
387 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
390 return DCCP_RESET_CODE_TOO_BUSY;
392 new->feat_num = feat;
393 new->is_local = local;
394 new->state = FEAT_STABLE; /* transition in 6.6.2 */
395 new->needs_confirm = 1;
396 new->empty_confirm = (fval == NULL);
397 new->val.nn = 0; /* zeroes the whole structure */
398 if (!new->empty_confirm)
400 new->needs_mandatory = 0;
405 static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
407 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
410 static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
412 list_del(&entry->node);
413 dccp_feat_entry_destructor(entry);
416 void dccp_feat_list_purge(struct list_head *fn_list)
418 struct dccp_feat_entry *entry, *next;
420 list_for_each_entry_safe(entry, next, fn_list, node)
421 dccp_feat_entry_destructor(entry);
422 INIT_LIST_HEAD(fn_list);
424 EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
426 /* generate @to as full clone of @from - @to must not contain any nodes */
427 int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
429 struct dccp_feat_entry *entry, *new;
432 list_for_each_entry(entry, from, node) {
433 new = dccp_feat_clone_entry(entry);
436 list_add_tail(&new->node, to);
441 dccp_feat_list_purge(to);
446 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
447 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
448 * incoming options are accepted as long as their values are valid.
450 static u8 dccp_feat_valid_nn_length(u8 feat_num)
452 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
454 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
459 static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
462 case DCCPF_ACK_RATIO:
463 return val <= DCCPF_ACK_RATIO_MAX;
464 case DCCPF_SEQUENCE_WINDOW:
465 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
467 return 0; /* feature unknown - so we can't tell */
470 /* check that SP values are within the ranges defined in RFC 4340 */
471 static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
475 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
476 /* Type-check Boolean feature values: */
477 case DCCPF_SHORT_SEQNOS:
478 case DCCPF_ECN_INCAPABLE:
479 case DCCPF_SEND_ACK_VECTOR:
480 case DCCPF_SEND_NDP_COUNT:
481 case DCCPF_DATA_CHECKSUM:
482 case DCCPF_SEND_LEV_RATE:
484 case DCCPF_MIN_CSUM_COVER:
487 return 0; /* feature unknown */
490 static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
492 if (sp_list == NULL || sp_len < 1)
495 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
501 * dccp_feat_insert_opts - Generate FN options from current list state
502 * @skb: next sk_buff to be sent to the peer
503 * @dp: for client during handshake and general negotiation
504 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
506 int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
509 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
510 struct dccp_feat_entry *pos, *next;
511 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
514 /* put entries into @skb in the order they appear in the list */
515 list_for_each_entry_safe_reverse(pos, next, fn, node) {
516 opt = dccp_feat_genopt(pos);
517 type = dccp_feat_type(pos->feat_num);
520 if (pos->empty_confirm) {
524 if (type == FEAT_SP) {
525 len = pos->val.sp.len;
526 ptr = pos->val.sp.vec;
527 rpt = pos->needs_confirm;
528 } else if (type == FEAT_NN) {
529 len = dccp_feat_valid_nn_length(pos->feat_num);
531 dccp_encode_value_var(pos->val.nn, ptr, len);
533 DCCP_BUG("unknown feature %u", pos->feat_num);
538 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
540 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
543 * Enter CHANGING after transmitting the Change option (6.6.2).
545 if (pos->state == FEAT_INITIALISING)
546 pos->state = FEAT_CHANGING;
552 * __feat_register_nn - Register new NN value on socket
553 * @fn: feature-negotiation list to register with
554 * @feat: an NN feature from %dccp_feature_numbers
555 * @mandatory: use Mandatory option if 1
556 * @nn_val: value to register (restricted to 4 bytes)
557 * Note that NN features are local by definition (RFC 4340, 6.3.2).
559 static int __feat_register_nn(struct list_head *fn, u8 feat,
560 u8 mandatory, u64 nn_val)
562 dccp_feat_val fval = { .nn = nn_val };
564 if (dccp_feat_type(feat) != FEAT_NN ||
565 !dccp_feat_is_valid_nn_val(feat, nn_val))
568 /* Don't bother with default values, they will be activated anyway. */
569 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
572 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
576 * __feat_register_sp - Register new SP value/list on socket
577 * @fn: feature-negotiation list to register with
578 * @feat: an SP feature from %dccp_feature_numbers
579 * @is_local: whether the local (1) or the remote (0) @feat is meant
580 * @mandatory: use Mandatory option if 1
581 * @sp_val: SP value followed by optional preference list
582 * @sp_len: length of @sp_val in bytes
584 static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
585 u8 mandatory, u8 const *sp_val, u8 sp_len)
589 if (dccp_feat_type(feat) != FEAT_SP ||
590 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
593 /* Avoid negotiating alien CCIDs by only advertising supported ones */
594 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
597 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
600 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
604 * dccp_feat_register_sp - Register requests to change SP feature values
605 * @sk: client or listening socket
606 * @feat: one of %dccp_feature_numbers
607 * @is_local: whether the local (1) or remote (0) @feat is meant
608 * @list: array of preferred values, in descending order of preference
609 * @len: length of @list in bytes
611 int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
612 u8 const *list, u8 len)
613 { /* any changes must be registered before establishing the connection */
614 if (sk->sk_state != DCCP_CLOSED)
616 if (dccp_feat_type(feat) != FEAT_SP)
618 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
622 /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
623 int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
625 /* any changes must be registered before establishing the connection */
626 if (sk->sk_state != DCCP_CLOSED)
628 if (dccp_feat_type(feat) != FEAT_NN)
630 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
634 * Tracking features whose value depend on the choice of CCID
636 * This is designed with an extension in mind so that a list walk could be done
637 * before activating any features. However, the existing framework was found to
638 * work satisfactorily up until now, the automatic verification is left open.
639 * When adding new CCIDs, add a corresponding dependency table here.
641 static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
643 static const struct ccid_dependency ccid2_dependencies[2][2] = {
645 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
646 * feature and Send Ack Vector is an RX feature, `is_local'
647 * needs to be reversed.
649 { /* Dependencies of the receiver-side (remote) CCID2 */
651 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
653 .is_mandatory = true,
658 { /* Dependencies of the sender-side (local) CCID2 */
660 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
662 .is_mandatory = true,
668 static const struct ccid_dependency ccid3_dependencies[2][5] = {
670 * Dependencies of the receiver-side CCID3
672 { /* locally disable Ack Vectors */
673 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
675 .is_mandatory = false,
678 { /* see below why Send Loss Event Rate is on */
679 .dependent_feat = DCCPF_SEND_LEV_RATE,
681 .is_mandatory = true,
684 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
685 .dependent_feat = DCCPF_SEND_NDP_COUNT,
687 .is_mandatory = true,
693 * CCID3 at the TX side: we request that the HC-receiver
694 * will not send Ack Vectors (they will be ignored, so
695 * Mandatory is not set); we enable Send Loss Event Rate
696 * (Mandatory since the implementation does not support
697 * the Loss Intervals option of RFC 4342, 8.6).
698 * The last two options are for peer's information only.
701 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
703 .is_mandatory = false,
707 .dependent_feat = DCCPF_SEND_LEV_RATE,
709 .is_mandatory = true,
712 { /* this CCID does not support Ack Ratio */
713 .dependent_feat = DCCPF_ACK_RATIO,
715 .is_mandatory = false,
718 { /* tell receiver we are sending NDP counts */
719 .dependent_feat = DCCPF_SEND_NDP_COUNT,
721 .is_mandatory = false,
729 return ccid2_dependencies[is_local];
731 return ccid3_dependencies[is_local];
738 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
739 * @fn: feature-negotiation list to update
740 * @id: CCID number to track
741 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
742 * This function needs to be called after registering all other features.
744 static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
746 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
747 int i, rc = (table == NULL);
749 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
750 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
751 rc = __feat_register_sp(fn, table[i].dependent_feat,
753 table[i].is_mandatory,
756 rc = __feat_register_nn(fn, table[i].dependent_feat,
757 table[i].is_mandatory,
763 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
764 * @dp: client or listening socket (settings will be inherited)
765 * This is called after all registrations (socket initialisation, sysctls, and
766 * sockopt calls), and before sending the first packet containing Change options
767 * (ie. client-Request or server-Response), to ensure internal consistency.
769 int dccp_feat_finalise_settings(struct dccp_sock *dp)
771 struct list_head *fn = &dp->dccps_featneg;
772 struct dccp_feat_entry *entry;
773 int i = 2, ccids[2] = { -1, -1 };
777 * 1) not useful to propagate CCID settings if this host advertises more
778 * than one CCID: the choice of CCID may still change - if this is
779 * the client, or if this is the server and the client sends
780 * singleton CCID values.
781 * 2) since is that propagate_ccid changes the list, we defer changing
782 * the sorted list until after the traversal.
784 list_for_each_entry(entry, fn, node)
785 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
786 ccids[entry->is_local] = entry->val.sp.vec[0];
788 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
794 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
795 * It is the server which resolves the dependencies once the CCID has been
796 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
798 int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
800 struct list_head *fn = &dreq->dreq_featneg;
801 struct dccp_feat_entry *entry;
804 for (is_local = 0; is_local <= 1; is_local++) {
805 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
807 if (entry != NULL && !entry->empty_confirm)
808 ccid = entry->val.sp.vec[0];
810 ccid = dccp_feat_default_value(DCCPF_CCID);
812 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
818 /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
819 static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
823 for (s = 0; s < slen; s++)
824 for (c = 0; c < clen; c++)
825 if (servlist[s] == clilist[c])
831 * dccp_feat_prefer - Move preferred entry to the start of array
832 * Reorder the @array_len elements in @array so that @preferred_value comes
833 * first. Returns >0 to indicate that @preferred_value does occur in @array.
835 static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
837 u8 i, does_occur = 0;
840 for (i = 0; i < array_len; i++)
841 if (array[i] == preferred_value) {
846 array[0] = preferred_value;
852 * dccp_feat_reconcile - Reconcile SP preference lists
853 * @fval: SP list to reconcile into
854 * @arr: received SP preference list
855 * @len: length of @arr in bytes
856 * @is_server: whether this side is the server (and @fv is the server's list)
857 * @reorder: whether to reorder the list in @fv after reconciling with @arr
858 * When successful, > 0 is returned and the reconciled list is in @fval.
859 * A value of 0 means that negotiation failed (no shared entry).
861 static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
862 bool is_server, bool reorder)
866 if (!fv->sp.vec || !arr) {
867 DCCP_CRIT("NULL feature value or array");
872 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
874 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
882 * Reorder list: used for activating features and in dccp_insert_fn_opt.
884 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
888 * dccp_feat_change_recv - Process incoming ChangeL/R options
889 * @fn: feature-negotiation list to update
890 * @is_mandatory: whether the Change was preceded by a Mandatory option
891 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
892 * @feat: one of %dccp_feature_numbers
893 * @val: NN value or SP value/preference list
894 * @len: length of @val in bytes
895 * @server: whether this node is the server (1) or the client (0)
897 static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
898 u8 feat, u8 *val, u8 len, const bool server)
900 u8 defval, type = dccp_feat_type(feat);
901 const bool local = (opt == DCCPO_CHANGE_R);
902 struct dccp_feat_entry *entry;
905 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
906 goto unknown_feature_or_value;
909 * Negotiation of NN features: Change R is invalid, so there is no
910 * simultaneous negotiation; hence we do not look up in the list.
912 if (type == FEAT_NN) {
913 if (local || len > sizeof(fval.nn))
914 goto unknown_feature_or_value;
916 /* 6.3.2: "The feature remote MUST accept any valid value..." */
917 fval.nn = dccp_decode_value_var(val, len);
918 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
919 goto unknown_feature_or_value;
921 return dccp_feat_push_confirm(fn, feat, local, &fval);
925 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
927 entry = dccp_feat_list_lookup(fn, feat, local);
930 * No particular preferences have been registered. We deal with
931 * this situation by assuming that all valid values are equally
932 * acceptable, and apply the following checks:
933 * - if the peer's list is a singleton, we accept a valid value;
934 * - if we are the server, we first try to see if the peer (the
935 * client) advertises the default value. If yes, we use it,
936 * otherwise we accept the preferred value;
937 * - else if we are the client, we use the first list element.
939 if (dccp_feat_clone_sp_val(&fval, val, 1))
940 return DCCP_RESET_CODE_TOO_BUSY;
942 if (len > 1 && server) {
943 defval = dccp_feat_default_value(feat);
944 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
945 fval.sp.vec[0] = defval;
946 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
948 goto unknown_feature_or_value;
951 /* Treat unsupported CCIDs like invalid values */
952 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
954 goto not_valid_or_not_known;
957 return dccp_feat_push_confirm(fn, feat, local, &fval);
959 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
963 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
964 entry->empty_confirm = 0;
965 } else if (is_mandatory) {
966 return DCCP_RESET_CODE_MANDATORY_ERROR;
967 } else if (entry->state == FEAT_INITIALISING) {
969 * Failed simultaneous negotiation (server only): try to `save'
970 * the connection by checking whether entry contains the default
971 * value for @feat. If yes, send an empty Confirm to signal that
972 * the received Change was not understood - which implies using
974 * If this also fails, we use Reset as the last resort.
977 defval = dccp_feat_default_value(feat);
978 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
979 return DCCP_RESET_CODE_OPTION_ERROR;
980 entry->empty_confirm = 1;
982 entry->needs_confirm = 1;
983 entry->needs_mandatory = 0;
984 entry->state = FEAT_STABLE;
987 unknown_feature_or_value:
989 return dccp_push_empty_confirm(fn, feat, local);
991 not_valid_or_not_known:
992 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
993 : DCCP_RESET_CODE_OPTION_ERROR;
997 * dccp_feat_confirm_recv - Process received Confirm options
998 * @fn: feature-negotiation list to update
999 * @is_mandatory: whether @opt was preceded by a Mandatory option
1000 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1001 * @feat: one of %dccp_feature_numbers
1002 * @val: NN value or SP value/preference list
1003 * @len: length of @val in bytes
1004 * @server: whether this node is server (1) or client (0)
1006 static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1007 u8 feat, u8 *val, u8 len, const bool server)
1009 u8 *plist, plen, type = dccp_feat_type(feat);
1010 const bool local = (opt == DCCPO_CONFIRM_R);
1011 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1013 if (entry == NULL) { /* nothing queued: ignore or handle error */
1014 if (is_mandatory && type == FEAT_UNKNOWN)
1015 return DCCP_RESET_CODE_MANDATORY_ERROR;
1017 if (!local && type == FEAT_NN) /* 6.3.2 */
1018 goto confirmation_failed;
1022 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
1026 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1027 goto confirmation_failed;
1029 * Empty Confirm during connection setup: this means reverting
1030 * to the `old' value, which in this case is the default. Since
1031 * we handle default values automatically when no other values
1032 * have been set, we revert to the old value by removing this
1033 * entry from the list.
1035 dccp_feat_list_pop(entry);
1039 if (type == FEAT_NN) {
1040 if (len > sizeof(entry->val.nn))
1041 goto confirmation_failed;
1043 if (entry->val.nn == dccp_decode_value_var(val, len))
1044 goto confirmation_succeeded;
1046 DCCP_WARN("Bogus Confirm for non-existing value\n");
1047 goto confirmation_failed;
1051 * Parsing SP Confirms: the first element of @val is the preferred
1052 * SP value which the peer confirms, the remainder depends on @len.
1053 * Note that only the confirmed value need to be a valid SP value.
1055 if (!dccp_feat_is_valid_sp_val(feat, *val))
1056 goto confirmation_failed;
1058 if (len == 1) { /* peer didn't supply a preference list */
1061 } else { /* preferred value + preference list */
1066 /* Check whether the peer got the reconciliation right (6.6.8) */
1067 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1068 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1069 return DCCP_RESET_CODE_OPTION_ERROR;
1071 entry->val.sp.vec[0] = *val;
1073 confirmation_succeeded:
1074 entry->state = FEAT_STABLE;
1077 confirmation_failed:
1078 DCCP_WARN("Confirmation failed\n");
1079 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1080 : DCCP_RESET_CODE_OPTION_ERROR;
1084 * dccp_feat_parse_options - Process Feature-Negotiation Options
1085 * @sk: for general use and used by the client during connection setup
1086 * @dreq: used by the server during connection setup
1087 * @mandatory: whether @opt was preceded by a Mandatory option
1088 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1089 * @feat: one of %dccp_feature_numbers
1090 * @val: value contents of @opt
1091 * @len: length of @val in bytes
1092 * Returns 0 on success, a Reset code for ending the connection otherwise.
1094 int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1095 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1097 struct dccp_sock *dp = dccp_sk(sk);
1098 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1099 bool server = false;
1101 switch (sk->sk_state) {
1103 * Negotiation during connection setup
1106 server = true; /* fall through */
1107 case DCCP_REQUESTING:
1109 case DCCPO_CHANGE_L:
1110 case DCCPO_CHANGE_R:
1111 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1113 case DCCPO_CONFIRM_R:
1114 case DCCPO_CONFIRM_L:
1115 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1119 return 0; /* ignore FN options in all other states */
1123 * dccp_feat_init - Seed feature negotiation with host-specific defaults
1124 * This initialises global defaults, depending on the value of the sysctls.
1125 * These can later be overridden by registering changes via setsockopt calls.
1126 * The last link in the chain is finalise_settings, to make sure that between
1127 * here and the start of actual feature negotiation no inconsistencies enter.
1129 * All features not appearing below use either defaults or are otherwise
1130 * later adjusted through dccp_feat_finalise_settings().
1132 int dccp_feat_init(struct sock *sk)
1134 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1142 /* Non-negotiable (NN) features */
1143 rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
1144 sysctl_dccp_feat_sequence_window);
1148 /* Server-priority (SP) features */
1150 /* Advertise that short seqnos are not supported (7.6.1) */
1151 rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1155 /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1156 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1161 * We advertise the available list of CCIDs and reorder according to
1162 * preferences, to avoid failure resulting from negotiating different
1163 * singleton values (which always leads to failure).
1164 * These settings can still (later) be overridden via sockopts.
1166 if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1167 ccid_get_builtin_ccids(&rx.val, &rx.len))
1170 /* Pre-load all CCID modules that are going to be advertised */
1172 if (ccid_request_modules(tx.val, tx.len))
1173 goto free_ccid_lists;
1175 if (!dccp_feat_prefer(sysctl_dccp_feat_tx_ccid, tx.val, tx.len) ||
1176 !dccp_feat_prefer(sysctl_dccp_feat_rx_ccid, rx.val, rx.len))
1177 goto free_ccid_lists;
1179 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1181 goto free_ccid_lists;
1183 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1191 int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1193 struct dccp_sock *dp = dccp_sk(sk);
1194 struct dccp_feat_entry *cur, *next;
1196 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1197 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1200 list_for_each_entry(cur, fn_list, node) {
1202 * An empty Confirm means that either an unknown feature type
1203 * or an invalid value was present. In the first case there is
1204 * nothing to activate, in the other the default value is used.
1206 if (cur->empty_confirm)
1209 idx = dccp_feat_index(cur->feat_num);
1211 DCCP_BUG("Unknown feature %u", cur->feat_num);
1212 goto activation_failed;
1214 if (cur->state != FEAT_STABLE) {
1215 DCCP_CRIT("Negotiation of %s %u failed in state %u",
1216 cur->is_local ? "local" : "remote",
1217 cur->feat_num, cur->state);
1218 goto activation_failed;
1220 fvals[idx][cur->is_local] = &cur->val;
1224 * Activate in decreasing order of index, so that the CCIDs are always
1225 * activated as the last feature. This avoids the case where a CCID
1226 * relies on the initialisation of one or more features that it depends
1227 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1229 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1230 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1231 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1232 DCCP_CRIT("Could not activate %d", idx);
1233 goto activation_failed;
1236 /* Clean up Change options which have been confirmed already */
1237 list_for_each_entry_safe(cur, next, fn_list, node)
1238 if (!cur->needs_confirm)
1239 dccp_feat_list_pop(cur);
1241 dccp_pr_debug("Activation OK\n");
1246 * We clean up everything that may have been allocated, since
1247 * it is difficult to track at which stage negotiation failed.
1248 * This is ok, since all allocation functions below are robust
1249 * against NULL arguments.
1251 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1252 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1253 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1254 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1255 dp->dccps_hc_rx_ackvec = NULL;
1259 #ifdef CONFIG_IP_DCCP_DEBUG
1260 const char *dccp_feat_typename(const u8 type)
1263 case DCCPO_CHANGE_L: return("ChangeL");
1264 case DCCPO_CONFIRM_L: return("ConfirmL");
1265 case DCCPO_CHANGE_R: return("ChangeR");
1266 case DCCPO_CONFIRM_R: return("ConfirmR");
1267 /* the following case must not appear in feature negotation */
1268 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
1273 EXPORT_SYMBOL_GPL(dccp_feat_typename);
1275 const char *dccp_feat_name(const u8 feat)
1277 static const char *feature_names[] = {
1278 [DCCPF_RESERVED] = "Reserved",
1279 [DCCPF_CCID] = "CCID",
1280 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
1281 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
1282 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
1283 [DCCPF_ACK_RATIO] = "Ack Ratio",
1284 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
1285 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
1286 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
1287 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
1289 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1290 return feature_names[DCCPF_RESERVED];
1292 if (feat == DCCPF_SEND_LEV_RATE)
1293 return "Send Loss Event Rate";
1294 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1295 return "CCID-specific";
1297 return feature_names[feat];
1300 EXPORT_SYMBOL_GPL(dccp_feat_name);
1301 #endif /* CONFIG_IP_DCCP_DEBUG */