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)
56 dccp_msk(sk)->dccpms_sequence_window = seq_win;
60 static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
63 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
65 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
69 static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
71 struct dccp_sock *dp = dccp_sk(sk);
74 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
75 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
76 if (dp->dccps_hc_rx_ackvec == NULL)
79 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
80 dp->dccps_hc_rx_ackvec = NULL;
86 static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
89 dccp_msk(sk)->dccpms_send_ndp_count = (enable > 0);
94 * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
95 * `rx' holds when the sending peer informs about his partial coverage via a
96 * ChangeR() option. In the other case, we are the sender and the receiver
97 * announces its coverage via ChangeL() options. The policy here is to honour
98 * such communication by enabling the corresponding partial coverage - but only
99 * if it has not been set manually before; the warning here means that all
100 * packets will be dropped.
102 static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
104 struct dccp_sock *dp = dccp_sk(sk);
107 dp->dccps_pcrlen = cscov;
109 if (dp->dccps_pcslen == 0)
110 dp->dccps_pcslen = cscov;
111 else if (cscov > dp->dccps_pcslen)
112 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
113 dp->dccps_pcslen, (u8)cscov);
118 static const struct {
119 u8 feat_num; /* DCCPF_xxx */
120 enum dccp_feat_type rxtx; /* RX or TX */
121 enum dccp_feat_type reconciliation; /* SP or NN */
122 u8 default_value; /* as in 6.4 */
123 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
125 * Lookup table for location and type of features (from RFC 4340/4342)
126 * +--------------------------+----+-----+----+----+---------+-----------+
127 * | Feature | Location | Reconc. | Initial | Section |
128 * | | RX | TX | SP | NN | Value | Reference |
129 * +--------------------------+----+-----+----+----+---------+-----------+
130 * | DCCPF_CCID | | X | X | | 2 | 10 |
131 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
132 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
133 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
134 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
135 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
136 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
137 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
138 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
139 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
140 * +--------------------------+----+-----+----+----+---------+-----------+
142 } dccp_feat_table[] = {
143 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
144 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
145 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
146 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
147 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
148 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
149 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
150 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
151 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
152 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
154 #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
157 * dccp_feat_index - Hash function to map feature number into array position
158 * Returns consecutive array index or -1 if the feature is not understood.
160 static int dccp_feat_index(u8 feat_num)
162 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
163 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
167 * Other features: add cases for new feature types here after adding
168 * them to the above table.
171 case DCCPF_SEND_LEV_RATE:
172 return DCCP_FEAT_SUPPORTED_MAX - 1;
177 static u8 dccp_feat_type(u8 feat_num)
179 int idx = dccp_feat_index(feat_num);
183 return dccp_feat_table[idx].reconciliation;
186 static int dccp_feat_default_value(u8 feat_num)
188 int idx = dccp_feat_index(feat_num);
190 return idx < 0 ? : dccp_feat_table[idx].default_value;
193 static int __dccp_feat_activate(struct sock *sk, const int idx,
194 const bool is_local, dccp_feat_val const *fval)
199 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
201 if (dccp_feat_table[idx].activation_hdlr == NULL)
205 val = dccp_feat_table[idx].default_value;
206 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
207 if (fval->sp.vec == NULL) {
209 * This can happen when an empty Confirm is sent
210 * for an SP (i.e. known) feature. In this case
211 * we would be using the default anyway.
213 DCCP_CRIT("Feature #%d undefined: using default", idx);
214 val = dccp_feat_table[idx].default_value;
216 val = fval->sp.vec[0];
222 /* Location is RX if this is a local-RX or remote-TX feature */
223 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
225 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
228 /* Test for "Req'd" feature (RFC 4340, 6.4) */
229 static inline int dccp_feat_must_be_understood(u8 feat_num)
231 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
232 feat_num == DCCPF_SEQUENCE_WINDOW;
235 /* copy constructor, fval must not already contain allocated memory */
236 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
239 if (fval->sp.len > 0) {
240 fval->sp.vec = kmemdup(val, len, gfp_any());
241 if (fval->sp.vec == NULL) {
249 static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
251 if (unlikely(val == NULL))
253 if (dccp_feat_type(feat_num) == FEAT_SP)
255 memset(val, 0, sizeof(*val));
258 static struct dccp_feat_entry *
259 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
261 struct dccp_feat_entry *new;
262 u8 type = dccp_feat_type(original->feat_num);
264 if (type == FEAT_UNKNOWN)
267 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
271 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
272 original->val.sp.vec,
273 original->val.sp.len)) {
280 static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
283 dccp_feat_val_destructor(entry->feat_num, &entry->val);
289 * List management functions
291 * Feature negotiation lists rely on and maintain the following invariants:
292 * - each feat_num in the list is known, i.e. we know its type and default value
293 * - each feat_num/is_local combination is unique (old entries are overwritten)
294 * - SP values are always freshly allocated
295 * - list is sorted in increasing order of feature number (faster lookup)
297 static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
298 u8 feat_num, bool is_local)
300 struct dccp_feat_entry *entry;
302 list_for_each_entry(entry, fn_list, node)
303 if (entry->feat_num == feat_num && entry->is_local == is_local)
305 else if (entry->feat_num > feat_num)
311 * dccp_feat_entry_new - Central list update routine (called by all others)
312 * @head: list to add to
313 * @feat: feature number
314 * @local: whether the local (1) or remote feature with number @feat is meant
315 * This is the only constructor and serves to ensure the above invariants.
317 static struct dccp_feat_entry *
318 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
320 struct dccp_feat_entry *entry;
322 list_for_each_entry(entry, head, node)
323 if (entry->feat_num == feat && entry->is_local == local) {
324 dccp_feat_val_destructor(entry->feat_num, &entry->val);
326 } else if (entry->feat_num > feat) {
331 entry = kmalloc(sizeof(*entry), gfp_any());
333 entry->feat_num = feat;
334 entry->is_local = local;
335 list_add_tail(&entry->node, head);
341 * dccp_feat_push_change - Add/overwrite a Change option in the list
342 * @fn_list: feature-negotiation list to update
343 * @feat: one of %dccp_feature_numbers
344 * @local: whether local (1) or remote (0) @feat_num is meant
345 * @needs_mandatory: whether to use Mandatory feature negotiation options
346 * @fval: pointer to NN/SP value to be inserted (will be copied)
348 static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
349 u8 mandatory, dccp_feat_val *fval)
351 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
356 new->feat_num = feat;
357 new->is_local = local;
358 new->state = FEAT_INITIALISING;
359 new->needs_confirm = 0;
360 new->empty_confirm = 0;
362 new->needs_mandatory = mandatory;
368 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
369 * @fn_list: feature-negotiation list to add to
370 * @feat: one of %dccp_feature_numbers
371 * @local: whether local (1) or remote (0) @feat_num is being confirmed
372 * @fval: pointer to NN/SP value to be inserted or NULL
373 * Returns 0 on success, a Reset code for further processing otherwise.
375 static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
378 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
381 return DCCP_RESET_CODE_TOO_BUSY;
383 new->feat_num = feat;
384 new->is_local = local;
385 new->state = FEAT_STABLE; /* transition in 6.6.2 */
386 new->needs_confirm = 1;
387 new->empty_confirm = (fval == NULL);
388 new->val.nn = 0; /* zeroes the whole structure */
389 if (!new->empty_confirm)
391 new->needs_mandatory = 0;
396 static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
398 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
401 static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
403 list_del(&entry->node);
404 dccp_feat_entry_destructor(entry);
407 void dccp_feat_list_purge(struct list_head *fn_list)
409 struct dccp_feat_entry *entry, *next;
411 list_for_each_entry_safe(entry, next, fn_list, node)
412 dccp_feat_entry_destructor(entry);
413 INIT_LIST_HEAD(fn_list);
415 EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
417 /* generate @to as full clone of @from - @to must not contain any nodes */
418 int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
420 struct dccp_feat_entry *entry, *new;
423 list_for_each_entry(entry, from, node) {
424 new = dccp_feat_clone_entry(entry);
427 list_add_tail(&new->node, to);
432 dccp_feat_list_purge(to);
437 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
438 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
439 * incoming options are accepted as long as their values are valid.
441 static u8 dccp_feat_valid_nn_length(u8 feat_num)
443 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
445 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
450 static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
453 case DCCPF_ACK_RATIO:
454 return val <= DCCPF_ACK_RATIO_MAX;
455 case DCCPF_SEQUENCE_WINDOW:
456 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
458 return 0; /* feature unknown - so we can't tell */
461 /* check that SP values are within the ranges defined in RFC 4340 */
462 static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
466 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
467 /* Type-check Boolean feature values: */
468 case DCCPF_SHORT_SEQNOS:
469 case DCCPF_ECN_INCAPABLE:
470 case DCCPF_SEND_ACK_VECTOR:
471 case DCCPF_SEND_NDP_COUNT:
472 case DCCPF_DATA_CHECKSUM:
473 case DCCPF_SEND_LEV_RATE:
475 case DCCPF_MIN_CSUM_COVER:
478 return 0; /* feature unknown */
481 static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
483 if (sp_list == NULL || sp_len < 1)
486 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
492 * dccp_feat_insert_opts - Generate FN options from current list state
493 * @skb: next sk_buff to be sent to the peer
494 * @dp: for client during handshake and general negotiation
495 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
497 int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
500 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
501 struct dccp_feat_entry *pos, *next;
502 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
505 /* put entries into @skb in the order they appear in the list */
506 list_for_each_entry_safe_reverse(pos, next, fn, node) {
507 opt = dccp_feat_genopt(pos);
508 type = dccp_feat_type(pos->feat_num);
511 if (pos->empty_confirm) {
515 if (type == FEAT_SP) {
516 len = pos->val.sp.len;
517 ptr = pos->val.sp.vec;
518 rpt = pos->needs_confirm;
519 } else if (type == FEAT_NN) {
520 len = dccp_feat_valid_nn_length(pos->feat_num);
522 dccp_encode_value_var(pos->val.nn, ptr, len);
524 DCCP_BUG("unknown feature %u", pos->feat_num);
529 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
531 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
534 * Enter CHANGING after transmitting the Change option (6.6.2).
536 if (pos->state == FEAT_INITIALISING)
537 pos->state = FEAT_CHANGING;
543 * __feat_register_nn - Register new NN value on socket
544 * @fn: feature-negotiation list to register with
545 * @feat: an NN feature from %dccp_feature_numbers
546 * @mandatory: use Mandatory option if 1
547 * @nn_val: value to register (restricted to 4 bytes)
548 * Note that NN features are local by definition (RFC 4340, 6.3.2).
550 static int __feat_register_nn(struct list_head *fn, u8 feat,
551 u8 mandatory, u64 nn_val)
553 dccp_feat_val fval = { .nn = nn_val };
555 if (dccp_feat_type(feat) != FEAT_NN ||
556 !dccp_feat_is_valid_nn_val(feat, nn_val))
559 /* Don't bother with default values, they will be activated anyway. */
560 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
563 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
567 * __feat_register_sp - Register new SP value/list on socket
568 * @fn: feature-negotiation list to register with
569 * @feat: an SP feature from %dccp_feature_numbers
570 * @is_local: whether the local (1) or the remote (0) @feat is meant
571 * @mandatory: use Mandatory option if 1
572 * @sp_val: SP value followed by optional preference list
573 * @sp_len: length of @sp_val in bytes
575 static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
576 u8 mandatory, u8 const *sp_val, u8 sp_len)
580 if (dccp_feat_type(feat) != FEAT_SP ||
581 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
584 /* Avoid negotiating alien CCIDs by only advertising supported ones */
585 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
588 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
591 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
595 * dccp_feat_register_sp - Register requests to change SP feature values
596 * @sk: client or listening socket
597 * @feat: one of %dccp_feature_numbers
598 * @is_local: whether the local (1) or remote (0) @feat is meant
599 * @list: array of preferred values, in descending order of preference
600 * @len: length of @list in bytes
602 int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
603 u8 const *list, u8 len)
604 { /* any changes must be registered before establishing the connection */
605 if (sk->sk_state != DCCP_CLOSED)
607 if (dccp_feat_type(feat) != FEAT_SP)
609 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
613 /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
614 int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
616 /* any changes must be registered before establishing the connection */
617 if (sk->sk_state != DCCP_CLOSED)
619 if (dccp_feat_type(feat) != FEAT_NN)
621 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
625 * Tracking features whose value depend on the choice of CCID
627 * This is designed with an extension in mind so that a list walk could be done
628 * before activating any features. However, the existing framework was found to
629 * work satisfactorily up until now, the automatic verification is left open.
630 * When adding new CCIDs, add a corresponding dependency table here.
632 static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
634 static const struct ccid_dependency ccid2_dependencies[2][2] = {
636 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
637 * feature and Send Ack Vector is an RX feature, `is_local'
638 * needs to be reversed.
640 { /* Dependencies of the receiver-side (remote) CCID2 */
642 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
644 .is_mandatory = true,
649 { /* Dependencies of the sender-side (local) CCID2 */
651 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
653 .is_mandatory = true,
659 static const struct ccid_dependency ccid3_dependencies[2][5] = {
661 * Dependencies of the receiver-side CCID3
663 { /* locally disable Ack Vectors */
664 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
666 .is_mandatory = false,
669 { /* see below why Send Loss Event Rate is on */
670 .dependent_feat = DCCPF_SEND_LEV_RATE,
672 .is_mandatory = true,
675 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
676 .dependent_feat = DCCPF_SEND_NDP_COUNT,
678 .is_mandatory = true,
684 * CCID3 at the TX side: we request that the HC-receiver
685 * will not send Ack Vectors (they will be ignored, so
686 * Mandatory is not set); we enable Send Loss Event Rate
687 * (Mandatory since the implementation does not support
688 * the Loss Intervals option of RFC 4342, 8.6).
689 * The last two options are for peer's information only.
692 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
694 .is_mandatory = false,
698 .dependent_feat = DCCPF_SEND_LEV_RATE,
700 .is_mandatory = true,
703 { /* this CCID does not support Ack Ratio */
704 .dependent_feat = DCCPF_ACK_RATIO,
706 .is_mandatory = false,
709 { /* tell receiver we are sending NDP counts */
710 .dependent_feat = DCCPF_SEND_NDP_COUNT,
712 .is_mandatory = false,
720 return ccid2_dependencies[is_local];
722 return ccid3_dependencies[is_local];
729 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
730 * @fn: feature-negotiation list to update
731 * @id: CCID number to track
732 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
733 * This function needs to be called after registering all other features.
735 static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
737 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
738 int i, rc = (table == NULL);
740 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
741 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
742 rc = __feat_register_sp(fn, table[i].dependent_feat,
744 table[i].is_mandatory,
747 rc = __feat_register_nn(fn, table[i].dependent_feat,
748 table[i].is_mandatory,
754 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
755 * @dp: client or listening socket (settings will be inherited)
756 * This is called after all registrations (socket initialisation, sysctls, and
757 * sockopt calls), and before sending the first packet containing Change options
758 * (ie. client-Request or server-Response), to ensure internal consistency.
760 int dccp_feat_finalise_settings(struct dccp_sock *dp)
762 struct list_head *fn = &dp->dccps_featneg;
763 struct dccp_feat_entry *entry;
764 int i = 2, ccids[2] = { -1, -1 };
768 * 1) not useful to propagate CCID settings if this host advertises more
769 * than one CCID: the choice of CCID may still change - if this is
770 * the client, or if this is the server and the client sends
771 * singleton CCID values.
772 * 2) since is that propagate_ccid changes the list, we defer changing
773 * the sorted list until after the traversal.
775 list_for_each_entry(entry, fn, node)
776 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
777 ccids[entry->is_local] = entry->val.sp.vec[0];
779 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
785 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
786 * It is the server which resolves the dependencies once the CCID has been
787 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
789 int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
791 struct list_head *fn = &dreq->dreq_featneg;
792 struct dccp_feat_entry *entry;
795 for (is_local = 0; is_local <= 1; is_local++) {
796 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
798 if (entry != NULL && !entry->empty_confirm)
799 ccid = entry->val.sp.vec[0];
801 ccid = dccp_feat_default_value(DCCPF_CCID);
803 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
809 /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
810 static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
814 for (s = 0; s < slen; s++)
815 for (c = 0; c < clen; c++)
816 if (servlist[s] == clilist[c])
822 * dccp_feat_prefer - Move preferred entry to the start of array
823 * Reorder the @array_len elements in @array so that @preferred_value comes
824 * first. Returns >0 to indicate that @preferred_value does occur in @array.
826 static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
828 u8 i, does_occur = 0;
831 for (i = 0; i < array_len; i++)
832 if (array[i] == preferred_value) {
837 array[0] = preferred_value;
843 * dccp_feat_reconcile - Reconcile SP preference lists
844 * @fval: SP list to reconcile into
845 * @arr: received SP preference list
846 * @len: length of @arr in bytes
847 * @is_server: whether this side is the server (and @fv is the server's list)
848 * @reorder: whether to reorder the list in @fv after reconciling with @arr
849 * When successful, > 0 is returned and the reconciled list is in @fval.
850 * A value of 0 means that negotiation failed (no shared entry).
852 static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
853 bool is_server, bool reorder)
857 if (!fv->sp.vec || !arr) {
858 DCCP_CRIT("NULL feature value or array");
863 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
865 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
873 * Reorder list: used for activating features and in dccp_insert_fn_opt.
875 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
879 * dccp_feat_change_recv - Process incoming ChangeL/R options
880 * @fn: feature-negotiation list to update
881 * @is_mandatory: whether the Change was preceded by a Mandatory option
882 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
883 * @feat: one of %dccp_feature_numbers
884 * @val: NN value or SP value/preference list
885 * @len: length of @val in bytes
886 * @server: whether this node is the server (1) or the client (0)
888 static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
889 u8 feat, u8 *val, u8 len, const bool server)
891 u8 defval, type = dccp_feat_type(feat);
892 const bool local = (opt == DCCPO_CHANGE_R);
893 struct dccp_feat_entry *entry;
896 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
897 goto unknown_feature_or_value;
900 * Negotiation of NN features: Change R is invalid, so there is no
901 * simultaneous negotiation; hence we do not look up in the list.
903 if (type == FEAT_NN) {
904 if (local || len > sizeof(fval.nn))
905 goto unknown_feature_or_value;
907 /* 6.3.2: "The feature remote MUST accept any valid value..." */
908 fval.nn = dccp_decode_value_var(val, len);
909 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
910 goto unknown_feature_or_value;
912 return dccp_feat_push_confirm(fn, feat, local, &fval);
916 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
918 entry = dccp_feat_list_lookup(fn, feat, local);
921 * No particular preferences have been registered. We deal with
922 * this situation by assuming that all valid values are equally
923 * acceptable, and apply the following checks:
924 * - if the peer's list is a singleton, we accept a valid value;
925 * - if we are the server, we first try to see if the peer (the
926 * client) advertises the default value. If yes, we use it,
927 * otherwise we accept the preferred value;
928 * - else if we are the client, we use the first list element.
930 if (dccp_feat_clone_sp_val(&fval, val, 1))
931 return DCCP_RESET_CODE_TOO_BUSY;
933 if (len > 1 && server) {
934 defval = dccp_feat_default_value(feat);
935 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
936 fval.sp.vec[0] = defval;
937 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
939 goto unknown_feature_or_value;
942 /* Treat unsupported CCIDs like invalid values */
943 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
945 goto not_valid_or_not_known;
948 return dccp_feat_push_confirm(fn, feat, local, &fval);
950 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
954 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
955 entry->empty_confirm = 0;
956 } else if (is_mandatory) {
957 return DCCP_RESET_CODE_MANDATORY_ERROR;
958 } else if (entry->state == FEAT_INITIALISING) {
960 * Failed simultaneous negotiation (server only): try to `save'
961 * the connection by checking whether entry contains the default
962 * value for @feat. If yes, send an empty Confirm to signal that
963 * the received Change was not understood - which implies using
965 * If this also fails, we use Reset as the last resort.
968 defval = dccp_feat_default_value(feat);
969 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
970 return DCCP_RESET_CODE_OPTION_ERROR;
971 entry->empty_confirm = 1;
973 entry->needs_confirm = 1;
974 entry->needs_mandatory = 0;
975 entry->state = FEAT_STABLE;
978 unknown_feature_or_value:
980 return dccp_push_empty_confirm(fn, feat, local);
982 not_valid_or_not_known:
983 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
984 : DCCP_RESET_CODE_OPTION_ERROR;
988 * dccp_feat_confirm_recv - Process received Confirm options
989 * @fn: feature-negotiation list to update
990 * @is_mandatory: whether @opt was preceded by a Mandatory option
991 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
992 * @feat: one of %dccp_feature_numbers
993 * @val: NN value or SP value/preference list
994 * @len: length of @val in bytes
995 * @server: whether this node is server (1) or client (0)
997 static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
998 u8 feat, u8 *val, u8 len, const bool server)
1000 u8 *plist, plen, type = dccp_feat_type(feat);
1001 const bool local = (opt == DCCPO_CONFIRM_R);
1002 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1004 if (entry == NULL) { /* nothing queued: ignore or handle error */
1005 if (is_mandatory && type == FEAT_UNKNOWN)
1006 return DCCP_RESET_CODE_MANDATORY_ERROR;
1008 if (!local && type == FEAT_NN) /* 6.3.2 */
1009 goto confirmation_failed;
1013 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
1017 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1018 goto confirmation_failed;
1020 * Empty Confirm during connection setup: this means reverting
1021 * to the `old' value, which in this case is the default. Since
1022 * we handle default values automatically when no other values
1023 * have been set, we revert to the old value by removing this
1024 * entry from the list.
1026 dccp_feat_list_pop(entry);
1030 if (type == FEAT_NN) {
1031 if (len > sizeof(entry->val.nn))
1032 goto confirmation_failed;
1034 if (entry->val.nn == dccp_decode_value_var(val, len))
1035 goto confirmation_succeeded;
1037 DCCP_WARN("Bogus Confirm for non-existing value\n");
1038 goto confirmation_failed;
1042 * Parsing SP Confirms: the first element of @val is the preferred
1043 * SP value which the peer confirms, the remainder depends on @len.
1044 * Note that only the confirmed value need to be a valid SP value.
1046 if (!dccp_feat_is_valid_sp_val(feat, *val))
1047 goto confirmation_failed;
1049 if (len == 1) { /* peer didn't supply a preference list */
1052 } else { /* preferred value + preference list */
1057 /* Check whether the peer got the reconciliation right (6.6.8) */
1058 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1059 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1060 return DCCP_RESET_CODE_OPTION_ERROR;
1062 entry->val.sp.vec[0] = *val;
1064 confirmation_succeeded:
1065 entry->state = FEAT_STABLE;
1068 confirmation_failed:
1069 DCCP_WARN("Confirmation failed\n");
1070 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1071 : DCCP_RESET_CODE_OPTION_ERROR;
1075 * dccp_feat_parse_options - Process Feature-Negotiation Options
1076 * @sk: for general use and used by the client during connection setup
1077 * @dreq: used by the server during connection setup
1078 * @mandatory: whether @opt was preceded by a Mandatory option
1079 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1080 * @feat: one of %dccp_feature_numbers
1081 * @val: value contents of @opt
1082 * @len: length of @val in bytes
1083 * Returns 0 on success, a Reset code for ending the connection otherwise.
1085 int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1086 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1088 struct dccp_sock *dp = dccp_sk(sk);
1089 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1090 bool server = false;
1092 switch (sk->sk_state) {
1094 * Negotiation during connection setup
1097 server = true; /* fall through */
1098 case DCCP_REQUESTING:
1100 case DCCPO_CHANGE_L:
1101 case DCCPO_CHANGE_R:
1102 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1104 case DCCPO_CONFIRM_R:
1105 case DCCPO_CONFIRM_L:
1106 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1110 return 0; /* ignore FN options in all other states */
1113 int dccp_feat_init(struct sock *sk)
1115 struct dccp_sock *dp = dccp_sk(sk);
1116 struct dccp_minisock *dmsk = dccp_msk(sk);
1119 INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
1120 INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
1123 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
1124 &dmsk->dccpms_tx_ccid, 1);
1129 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
1130 &dmsk->dccpms_rx_ccid, 1);
1135 rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
1136 dp->dccps_l_ack_ratio);
1141 EXPORT_SYMBOL_GPL(dccp_feat_init);
1143 int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1145 struct dccp_sock *dp = dccp_sk(sk);
1146 struct dccp_feat_entry *cur, *next;
1148 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1149 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1152 list_for_each_entry(cur, fn_list, node) {
1154 * An empty Confirm means that either an unknown feature type
1155 * or an invalid value was present. In the first case there is
1156 * nothing to activate, in the other the default value is used.
1158 if (cur->empty_confirm)
1161 idx = dccp_feat_index(cur->feat_num);
1163 DCCP_BUG("Unknown feature %u", cur->feat_num);
1164 goto activation_failed;
1166 if (cur->state != FEAT_STABLE) {
1167 DCCP_CRIT("Negotiation of %s %u failed in state %u",
1168 cur->is_local ? "local" : "remote",
1169 cur->feat_num, cur->state);
1170 goto activation_failed;
1172 fvals[idx][cur->is_local] = &cur->val;
1176 * Activate in decreasing order of index, so that the CCIDs are always
1177 * activated as the last feature. This avoids the case where a CCID
1178 * relies on the initialisation of one or more features that it depends
1179 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1181 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1182 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1183 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1184 DCCP_CRIT("Could not activate %d", idx);
1185 goto activation_failed;
1188 /* Clean up Change options which have been confirmed already */
1189 list_for_each_entry_safe(cur, next, fn_list, node)
1190 if (!cur->needs_confirm)
1191 dccp_feat_list_pop(cur);
1193 dccp_pr_debug("Activation OK\n");
1198 * We clean up everything that may have been allocated, since
1199 * it is difficult to track at which stage negotiation failed.
1200 * This is ok, since all allocation functions below are robust
1201 * against NULL arguments.
1203 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1204 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1205 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1206 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1207 dp->dccps_hc_rx_ackvec = NULL;
1211 #ifdef CONFIG_IP_DCCP_DEBUG
1212 const char *dccp_feat_typename(const u8 type)
1215 case DCCPO_CHANGE_L: return("ChangeL");
1216 case DCCPO_CONFIRM_L: return("ConfirmL");
1217 case DCCPO_CHANGE_R: return("ChangeR");
1218 case DCCPO_CONFIRM_R: return("ConfirmR");
1219 /* the following case must not appear in feature negotation */
1220 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
1225 EXPORT_SYMBOL_GPL(dccp_feat_typename);
1227 const char *dccp_feat_name(const u8 feat)
1229 static const char *feature_names[] = {
1230 [DCCPF_RESERVED] = "Reserved",
1231 [DCCPF_CCID] = "CCID",
1232 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
1233 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
1234 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
1235 [DCCPF_ACK_RATIO] = "Ack Ratio",
1236 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
1237 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
1238 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
1239 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
1241 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1242 return feature_names[DCCPF_RESERVED];
1244 if (feat == DCCPF_SEND_LEV_RATE)
1245 return "Send Loss Event Rate";
1246 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1247 return "CCID-specific";
1249 return feature_names[feat];
1252 EXPORT_SYMBOL_GPL(dccp_feat_name);
1253 #endif /* CONFIG_IP_DCCP_DEBUG */