dccp: support for the exchange of NN options in established state 1/2
[deliverable/linux.git] / net / dccp / feat.c
CommitLineData
afe00251
AB
1/*
2 * net/dccp/feat.c
3 *
63b8e286
GR
4 * Feature negotiation for the DCCP protocol (RFC 4340, section 6)
5 *
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>
9 *
afe00251 10 *
5cdae198
GR
11 * ASSUMPTIONS
12 * -----------
f74e91b6
GR
13 * o Feature negotiation is coordinated with connection setup (as in TCP), wild
14 * changes of parameters of an established connection are not supported.
d6916f87 15 * o Changing non-negotiable (NN) values is supported in state OPEN/PARTOPEN.
5cdae198
GR
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.
19 *
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.
afe00251 24 */
afe00251 25#include <linux/module.h>
5a0e3ad6 26#include <linux/slab.h>
6ffd30fb 27#include "ccid.h"
afe00251
AB
28#include "feat.h"
29
883ca833
GR
30/* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */
31unsigned long sysctl_dccp_sequence_window __read_mostly = 100;
32int sysctl_dccp_rx_ccid __read_mostly = 2,
33 sysctl_dccp_tx_ccid __read_mostly = 2;
34
422d9cdc
GR
35/*
36 * Feature activation handlers.
37 *
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.
40 */
41static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
42{
43 struct dccp_sock *dp = dccp_sk(sk);
e5fd56ca 44 struct ccid *new_ccid = ccid_new(ccid, sk, rx);
422d9cdc
GR
45
46 if (new_ccid == NULL)
47 return -ENOMEM;
48
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;
55 }
56 return 0;
57}
58
59static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
60{
792b4878
GR
61 struct dccp_sock *dp = dccp_sk(sk);
62
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);
71 }
422d9cdc
GR
72 return 0;
73}
74
75static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
76{
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;
82}
83
84static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
85{
86 struct dccp_sock *dp = dccp_sk(sk);
87
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;
96 }
97 }
98 return 0;
99}
100
101static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
102{
103 if (!rx)
4098dce5 104 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
422d9cdc
GR
105 return 0;
106}
107
108/*
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.
116 */
117static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
118{
119 struct dccp_sock *dp = dccp_sk(sk);
120
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);
129 }
130 return 0;
131}
132
7d43d1a0
GR
133static 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 */
422d9cdc 138 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
7d43d1a0
GR
139/*
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 * +--------------------------+----+-----+----+----+---------+-----------+
156 */
157} dccp_feat_table[] = {
422d9cdc
GR
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 },
7d43d1a0
GR
168};
169#define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
170
61e6473e
GR
171/**
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.
174 */
175static int dccp_feat_index(u8 feat_num)
176{
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;
180
181 /*
182 * Other features: add cases for new feature types here after adding
183 * them to the above table.
184 */
185 switch (feat_num) {
186 case DCCPF_SEND_LEV_RATE:
187 return DCCP_FEAT_SUPPORTED_MAX - 1;
188 }
189 return -1;
190}
191
192static u8 dccp_feat_type(u8 feat_num)
193{
194 int idx = dccp_feat_index(feat_num);
195
196 if (idx < 0)
197 return FEAT_UNKNOWN;
198 return dccp_feat_table[idx].reconciliation;
199}
200
e8ef967a
GR
201static int dccp_feat_default_value(u8 feat_num)
202{
203 int idx = dccp_feat_index(feat_num);
204 /*
205 * There are no default values for unknown features, so encountering a
206 * negative index here indicates a serious problem somewhere else.
207 */
208 DCCP_BUG_ON(idx < 0);
209
210 return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
211}
212
f3f3abb6
GR
213/*
214 * Debugging and verbose-printing section
215 */
216static const char *dccp_feat_fname(const u8 feat)
217{
36cbd3dc 218 static const char *const feature_names[] = {
f3f3abb6
GR
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",
229 };
230 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
231 return feature_names[DCCPF_RESERVED];
232
233 if (feat == DCCPF_SEND_LEV_RATE)
234 return "Send Loss Event Rate";
235 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
236 return "CCID-specific";
237
238 return feature_names[feat];
239}
240
36cbd3dc
JE
241static const char *const dccp_feat_sname[] = {
242 "DEFAULT", "INITIALISING", "CHANGING", "UNSTABLE", "STABLE",
243};
f3f3abb6
GR
244
245#ifdef CONFIG_IP_DCCP_DEBUG
246static const char *dccp_feat_oname(const u8 opt)
247{
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";
253 }
254 return NULL;
255}
256
257static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
258{
259 u8 i, type = dccp_feat_type(feat_num);
260
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);
270}
271
272static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
273{
274 u8 type = dccp_feat_type(feat_num);
275 dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
276
277 if (type == FEAT_NN)
278 fval.nn = dccp_decode_value_var(list, len);
279 dccp_feat_printval(feat_num, &fval);
280}
281
282static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
283{
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)" : "");
289}
290
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)
295
296#define dccp_feat_print_fnlist(fn_list) { \
297 const struct dccp_feat_entry *___entry; \
298 \
299 dccp_pr_debug("List Dump:\n"); \
300 list_for_each_entry(___entry, fn_list, node) \
301 dccp_feat_print_entry(___entry); \
302}
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
307
422d9cdc
GR
308static int __dccp_feat_activate(struct sock *sk, const int idx,
309 const bool is_local, dccp_feat_val const *fval)
310{
311 bool rx;
312 u64 val;
313
314 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
315 return -1;
316 if (dccp_feat_table[idx].activation_hdlr == NULL)
317 return 0;
318
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) {
323 /*
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.
327 */
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];
332 }
333 } else {
334 val = fval->nn;
335 }
336
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));
339
f3f3abb6
GR
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);
343
422d9cdc
GR
344 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
345}
346
b1ad0042
GR
347/* Test for "Req'd" feature (RFC 4340, 6.4) */
348static inline int dccp_feat_must_be_understood(u8 feat_num)
349{
350 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
351 feat_num == DCCPF_SEQUENCE_WINDOW;
352}
353
ac75773c
GR
354/* copy constructor, fval must not already contain allocated memory */
355static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
356{
357 fval->sp.len = len;
358 if (fval->sp.len > 0) {
359 fval->sp.vec = kmemdup(val, len, gfp_any());
360 if (fval->sp.vec == NULL) {
361 fval->sp.len = 0;
362 return -ENOBUFS;
363 }
364 }
365 return 0;
366}
367
61e6473e
GR
368static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
369{
370 if (unlikely(val == NULL))
371 return;
372 if (dccp_feat_type(feat_num) == FEAT_SP)
373 kfree(val->sp.vec);
374 memset(val, 0, sizeof(*val));
375}
376
ac75773c
GR
377static struct dccp_feat_entry *
378 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
379{
380 struct dccp_feat_entry *new;
381 u8 type = dccp_feat_type(original->feat_num);
382
383 if (type == FEAT_UNKNOWN)
384 return NULL;
385
386 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
387 if (new == NULL)
388 return NULL;
389
390 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
391 original->val.sp.vec,
392 original->val.sp.len)) {
393 kfree(new);
394 return NULL;
395 }
396 return new;
397}
398
61e6473e
GR
399static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
400{
401 if (entry != NULL) {
402 dccp_feat_val_destructor(entry->feat_num, &entry->val);
403 kfree(entry);
404 }
405}
406
407/*
408 * List management functions
409 *
410 * Feature negotiation lists rely on and maintain the following invariants:
411 * - each feat_num in the list is known, i.e. we know its type and default value
412 * - each feat_num/is_local combination is unique (old entries are overwritten)
413 * - SP values are always freshly allocated
414 * - list is sorted in increasing order of feature number (faster lookup)
415 */
0c116839
GR
416static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
417 u8 feat_num, bool is_local)
418{
419 struct dccp_feat_entry *entry;
420
3d3e35aa 421 list_for_each_entry(entry, fn_list, node) {
0c116839
GR
422 if (entry->feat_num == feat_num && entry->is_local == is_local)
423 return entry;
424 else if (entry->feat_num > feat_num)
425 break;
3d3e35aa 426 }
0c116839
GR
427 return NULL;
428}
61e6473e 429
e8ef967a
GR
430/**
431 * dccp_feat_entry_new - Central list update routine (called by all others)
432 * @head: list to add to
433 * @feat: feature number
434 * @local: whether the local (1) or remote feature with number @feat is meant
435 * This is the only constructor and serves to ensure the above invariants.
436 */
437static struct dccp_feat_entry *
438 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
439{
440 struct dccp_feat_entry *entry;
441
442 list_for_each_entry(entry, head, node)
443 if (entry->feat_num == feat && entry->is_local == local) {
444 dccp_feat_val_destructor(entry->feat_num, &entry->val);
445 return entry;
446 } else if (entry->feat_num > feat) {
447 head = &entry->node;
448 break;
449 }
450
451 entry = kmalloc(sizeof(*entry), gfp_any());
452 if (entry != NULL) {
453 entry->feat_num = feat;
454 entry->is_local = local;
455 list_add_tail(&entry->node, head);
456 }
457 return entry;
458}
459
460/**
461 * dccp_feat_push_change - Add/overwrite a Change option in the list
462 * @fn_list: feature-negotiation list to update
463 * @feat: one of %dccp_feature_numbers
464 * @local: whether local (1) or remote (0) @feat_num is meant
465 * @needs_mandatory: whether to use Mandatory feature negotiation options
466 * @fval: pointer to NN/SP value to be inserted (will be copied)
467 */
468static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
469 u8 mandatory, dccp_feat_val *fval)
470{
471 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
472
473 if (new == NULL)
474 return -ENOMEM;
475
476 new->feat_num = feat;
477 new->is_local = local;
478 new->state = FEAT_INITIALISING;
479 new->needs_confirm = 0;
480 new->empty_confirm = 0;
481 new->val = *fval;
482 new->needs_mandatory = mandatory;
483
484 return 0;
485}
486
e77b8363
GR
487/**
488 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
489 * @fn_list: feature-negotiation list to add to
490 * @feat: one of %dccp_feature_numbers
491 * @local: whether local (1) or remote (0) @feat_num is being confirmed
492 * @fval: pointer to NN/SP value to be inserted or NULL
493 * Returns 0 on success, a Reset code for further processing otherwise.
494 */
495static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
496 dccp_feat_val *fval)
497{
498 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
499
500 if (new == NULL)
501 return DCCP_RESET_CODE_TOO_BUSY;
502
503 new->feat_num = feat;
504 new->is_local = local;
505 new->state = FEAT_STABLE; /* transition in 6.6.2 */
506 new->needs_confirm = 1;
507 new->empty_confirm = (fval == NULL);
508 new->val.nn = 0; /* zeroes the whole structure */
509 if (!new->empty_confirm)
510 new->val = *fval;
511 new->needs_mandatory = 0;
512
513 return 0;
514}
515
516static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
517{
518 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
519}
520
61e6473e
GR
521static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
522{
523 list_del(&entry->node);
524 dccp_feat_entry_destructor(entry);
525}
526
527void dccp_feat_list_purge(struct list_head *fn_list)
528{
529 struct dccp_feat_entry *entry, *next;
530
531 list_for_each_entry_safe(entry, next, fn_list, node)
532 dccp_feat_entry_destructor(entry);
533 INIT_LIST_HEAD(fn_list);
534}
535EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
536
ac75773c
GR
537/* generate @to as full clone of @from - @to must not contain any nodes */
538int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
539{
540 struct dccp_feat_entry *entry, *new;
541
542 INIT_LIST_HEAD(to);
543 list_for_each_entry(entry, from, node) {
544 new = dccp_feat_clone_entry(entry);
545 if (new == NULL)
546 goto cloning_failed;
547 list_add_tail(&new->node, to);
548 }
549 return 0;
550
551cloning_failed:
552 dccp_feat_list_purge(to);
553 return -ENOMEM;
554}
555
0971d17c
GR
556/**
557 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
558 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
559 * incoming options are accepted as long as their values are valid.
560 */
561static u8 dccp_feat_valid_nn_length(u8 feat_num)
562{
563 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
564 return 2;
565 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
566 return 6;
567 return 0;
568}
569
e8ef967a
GR
570static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
571{
572 switch (feat_num) {
573 case DCCPF_ACK_RATIO:
574 return val <= DCCPF_ACK_RATIO_MAX;
575 case DCCPF_SEQUENCE_WINDOW:
576 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
577 }
578 return 0; /* feature unknown - so we can't tell */
579}
580
581/* check that SP values are within the ranges defined in RFC 4340 */
582static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
583{
584 switch (feat_num) {
585 case DCCPF_CCID:
586 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
587 /* Type-check Boolean feature values: */
588 case DCCPF_SHORT_SEQNOS:
589 case DCCPF_ECN_INCAPABLE:
590 case DCCPF_SEND_ACK_VECTOR:
591 case DCCPF_SEND_NDP_COUNT:
592 case DCCPF_DATA_CHECKSUM:
593 case DCCPF_SEND_LEV_RATE:
594 return val < 2;
595 case DCCPF_MIN_CSUM_COVER:
596 return val < 16;
597 }
598 return 0; /* feature unknown */
599}
600
601static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
602{
603 if (sp_list == NULL || sp_len < 1)
604 return 0;
605 while (sp_len--)
606 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
607 return 0;
608 return 1;
609}
610
0971d17c
GR
611/**
612 * dccp_feat_insert_opts - Generate FN options from current list state
613 * @skb: next sk_buff to be sent to the peer
614 * @dp: for client during handshake and general negotiation
615 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
616 */
617int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
618 struct sk_buff *skb)
619{
620 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
621 struct dccp_feat_entry *pos, *next;
622 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
623 bool rpt;
624
625 /* put entries into @skb in the order they appear in the list */
626 list_for_each_entry_safe_reverse(pos, next, fn, node) {
627 opt = dccp_feat_genopt(pos);
628 type = dccp_feat_type(pos->feat_num);
629 rpt = false;
630
631 if (pos->empty_confirm) {
632 len = 0;
633 ptr = NULL;
634 } else {
635 if (type == FEAT_SP) {
636 len = pos->val.sp.len;
637 ptr = pos->val.sp.vec;
638 rpt = pos->needs_confirm;
639 } else if (type == FEAT_NN) {
640 len = dccp_feat_valid_nn_length(pos->feat_num);
641 ptr = nn_in_nbo;
642 dccp_encode_value_var(pos->val.nn, ptr, len);
643 } else {
644 DCCP_BUG("unknown feature %u", pos->feat_num);
645 return -1;
646 }
647 }
f3f3abb6 648 dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
0971d17c
GR
649
650 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
651 return -1;
652 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
653 return -1;
654 /*
655 * Enter CHANGING after transmitting the Change option (6.6.2).
656 */
657 if (pos->state == FEAT_INITIALISING)
658 pos->state = FEAT_CHANGING;
659 }
660 return 0;
661}
662
e8ef967a
GR
663/**
664 * __feat_register_nn - Register new NN value on socket
665 * @fn: feature-negotiation list to register with
666 * @feat: an NN feature from %dccp_feature_numbers
667 * @mandatory: use Mandatory option if 1
668 * @nn_val: value to register (restricted to 4 bytes)
669 * Note that NN features are local by definition (RFC 4340, 6.3.2).
670 */
671static int __feat_register_nn(struct list_head *fn, u8 feat,
672 u8 mandatory, u64 nn_val)
673{
674 dccp_feat_val fval = { .nn = nn_val };
675
676 if (dccp_feat_type(feat) != FEAT_NN ||
677 !dccp_feat_is_valid_nn_val(feat, nn_val))
678 return -EINVAL;
679
680 /* Don't bother with default values, they will be activated anyway. */
681 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
682 return 0;
683
684 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
685}
686
687/**
688 * __feat_register_sp - Register new SP value/list on socket
689 * @fn: feature-negotiation list to register with
690 * @feat: an SP feature from %dccp_feature_numbers
691 * @is_local: whether the local (1) or the remote (0) @feat is meant
692 * @mandatory: use Mandatory option if 1
693 * @sp_val: SP value followed by optional preference list
694 * @sp_len: length of @sp_val in bytes
695 */
696static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
697 u8 mandatory, u8 const *sp_val, u8 sp_len)
698{
699 dccp_feat_val fval;
700
701 if (dccp_feat_type(feat) != FEAT_SP ||
702 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
703 return -EINVAL;
704
d90ebcbf
GR
705 /* Avoid negotiating alien CCIDs by only advertising supported ones */
706 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
707 return -EOPNOTSUPP;
708
e8ef967a
GR
709 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
710 return -ENOMEM;
711
712 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
713}
714
49aebc66
GR
715/**
716 * dccp_feat_register_sp - Register requests to change SP feature values
717 * @sk: client or listening socket
718 * @feat: one of %dccp_feature_numbers
719 * @is_local: whether the local (1) or remote (0) @feat is meant
720 * @list: array of preferred values, in descending order of preference
721 * @len: length of @list in bytes
722 */
723int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
724 u8 const *list, u8 len)
725{ /* any changes must be registered before establishing the connection */
726 if (sk->sk_state != DCCP_CLOSED)
727 return -EISCONN;
728 if (dccp_feat_type(feat) != FEAT_SP)
19443178 729 return -EINVAL;
49aebc66
GR
730 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
731 0, list, len);
afe00251
AB
732}
733
d6916f87
GR
734/**
735 * dccp_feat_nn_get - Query current/pending value of NN feature
736 * @sk: DCCP socket of an established connection
737 * @feat: NN feature number from %dccp_feature_numbers
738 * For a known NN feature, returns value currently being negotiated, or
739 * current (confirmed) value if no negotiation is going on.
740 */
741u64 dccp_feat_nn_get(struct sock *sk, u8 feat)
742{
743 if (dccp_feat_type(feat) == FEAT_NN) {
744 struct dccp_sock *dp = dccp_sk(sk);
745 struct dccp_feat_entry *entry;
746
747 entry = dccp_feat_list_lookup(&dp->dccps_featneg, feat, 1);
748 if (entry != NULL)
749 return entry->val.nn;
750
751 switch (feat) {
752 case DCCPF_ACK_RATIO:
753 return dp->dccps_l_ack_ratio;
754 case DCCPF_SEQUENCE_WINDOW:
755 return dp->dccps_l_seq_win;
756 }
757 }
758 DCCP_BUG("attempt to look up unsupported feature %u", feat);
759 return 0;
760}
761EXPORT_SYMBOL_GPL(dccp_feat_nn_get);
762
763/**
764 * dccp_feat_signal_nn_change - Update NN values for an established connection
765 * @sk: DCCP socket of an established connection
766 * @feat: NN feature number from %dccp_feature_numbers
767 * @nn_val: the new value to use
768 * This function is used to communicate NN updates out-of-band.
769 */
770int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
771{
772 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
773 dccp_feat_val fval = { .nn = nn_val };
774 struct dccp_feat_entry *entry;
775
776 if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
777 return 0;
778
779 if (dccp_feat_type(feat) != FEAT_NN ||
780 !dccp_feat_is_valid_nn_val(feat, nn_val))
781 return -EINVAL;
782
783 if (nn_val == dccp_feat_nn_get(sk, feat))
784 return 0; /* already set or negotiation under way */
785
786 entry = dccp_feat_list_lookup(fn, feat, 1);
787 if (entry != NULL) {
788 dccp_pr_debug("Clobbering existing NN entry %llu -> %llu\n",
789 (unsigned long long)entry->val.nn,
790 (unsigned long long)nn_val);
791 dccp_feat_list_pop(entry);
792 }
793
794 inet_csk_schedule_ack(sk);
795 return dccp_feat_push_change(fn, feat, 1, 0, &fval);
796}
797EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
afe00251 798
9eca0a47
GR
799/*
800 * Tracking features whose value depend on the choice of CCID
801 *
802 * This is designed with an extension in mind so that a list walk could be done
803 * before activating any features. However, the existing framework was found to
804 * work satisfactorily up until now, the automatic verification is left open.
805 * When adding new CCIDs, add a corresponding dependency table here.
806 */
807static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
808{
809 static const struct ccid_dependency ccid2_dependencies[2][2] = {
810 /*
811 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
812 * feature and Send Ack Vector is an RX feature, `is_local'
813 * needs to be reversed.
814 */
815 { /* Dependencies of the receiver-side (remote) CCID2 */
816 {
817 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
818 .is_local = true,
819 .is_mandatory = true,
820 .val = 1
821 },
822 { 0, 0, 0, 0 }
823 },
824 { /* Dependencies of the sender-side (local) CCID2 */
825 {
826 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
827 .is_local = false,
828 .is_mandatory = true,
829 .val = 1
830 },
831 { 0, 0, 0, 0 }
832 }
833 };
834 static const struct ccid_dependency ccid3_dependencies[2][5] = {
835 { /*
836 * Dependencies of the receiver-side CCID3
837 */
838 { /* locally disable Ack Vectors */
839 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
840 .is_local = true,
841 .is_mandatory = false,
842 .val = 0
843 },
844 { /* see below why Send Loss Event Rate is on */
845 .dependent_feat = DCCPF_SEND_LEV_RATE,
846 .is_local = true,
847 .is_mandatory = true,
848 .val = 1
849 },
850 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
851 .dependent_feat = DCCPF_SEND_NDP_COUNT,
852 .is_local = false,
853 .is_mandatory = true,
854 .val = 1
855 },
856 { 0, 0, 0, 0 },
857 },
858 { /*
859 * CCID3 at the TX side: we request that the HC-receiver
860 * will not send Ack Vectors (they will be ignored, so
861 * Mandatory is not set); we enable Send Loss Event Rate
862 * (Mandatory since the implementation does not support
863 * the Loss Intervals option of RFC 4342, 8.6).
864 * The last two options are for peer's information only.
865 */
866 {
867 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
868 .is_local = false,
869 .is_mandatory = false,
870 .val = 0
871 },
872 {
873 .dependent_feat = DCCPF_SEND_LEV_RATE,
874 .is_local = false,
875 .is_mandatory = true,
876 .val = 1
877 },
878 { /* this CCID does not support Ack Ratio */
879 .dependent_feat = DCCPF_ACK_RATIO,
880 .is_local = true,
881 .is_mandatory = false,
882 .val = 0
883 },
884 { /* tell receiver we are sending NDP counts */
885 .dependent_feat = DCCPF_SEND_NDP_COUNT,
886 .is_local = true,
887 .is_mandatory = false,
888 .val = 1
889 },
890 { 0, 0, 0, 0 }
891 }
892 };
893 switch (ccid) {
894 case DCCPC_CCID2:
895 return ccid2_dependencies[is_local];
896 case DCCPC_CCID3:
897 return ccid3_dependencies[is_local];
898 default:
899 return NULL;
900 }
901}
902
903/**
904 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
905 * @fn: feature-negotiation list to update
906 * @id: CCID number to track
907 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
908 * This function needs to be called after registering all other features.
909 */
910static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
911{
912 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
913 int i, rc = (table == NULL);
914
915 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
916 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
917 rc = __feat_register_sp(fn, table[i].dependent_feat,
918 table[i].is_local,
919 table[i].is_mandatory,
920 &table[i].val, 1);
921 else
922 rc = __feat_register_nn(fn, table[i].dependent_feat,
923 table[i].is_mandatory,
924 table[i].val);
925 return rc;
926}
927
928/**
929 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
930 * @dp: client or listening socket (settings will be inherited)
931 * This is called after all registrations (socket initialisation, sysctls, and
932 * sockopt calls), and before sending the first packet containing Change options
933 * (ie. client-Request or server-Response), to ensure internal consistency.
934 */
935int dccp_feat_finalise_settings(struct dccp_sock *dp)
936{
937 struct list_head *fn = &dp->dccps_featneg;
938 struct dccp_feat_entry *entry;
939 int i = 2, ccids[2] = { -1, -1 };
940
941 /*
942 * Propagating CCIDs:
943 * 1) not useful to propagate CCID settings if this host advertises more
944 * than one CCID: the choice of CCID may still change - if this is
945 * the client, or if this is the server and the client sends
946 * singleton CCID values.
947 * 2) since is that propagate_ccid changes the list, we defer changing
948 * the sorted list until after the traversal.
949 */
950 list_for_each_entry(entry, fn, node)
951 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
952 ccids[entry->is_local] = entry->val.sp.vec[0];
953 while (i--)
954 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
955 return -1;
f3f3abb6 956 dccp_feat_print_fnlist(fn);
9eca0a47
GR
957 return 0;
958}
959
0c116839
GR
960/**
961 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
962 * It is the server which resolves the dependencies once the CCID has been
963 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
964 */
965int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
966{
967 struct list_head *fn = &dreq->dreq_featneg;
968 struct dccp_feat_entry *entry;
969 u8 is_local, ccid;
970
971 for (is_local = 0; is_local <= 1; is_local++) {
972 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
973
974 if (entry != NULL && !entry->empty_confirm)
975 ccid = entry->val.sp.vec[0];
976 else
977 ccid = dccp_feat_default_value(DCCPF_CCID);
978
979 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
980 return -1;
981 }
982 return 0;
983}
984
75757a7d
GR
985/* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
986static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
987{
988 u8 c, s;
989
990 for (s = 0; s < slen; s++)
991 for (c = 0; c < clen; c++)
992 if (servlist[s] == clilist[c])
993 return servlist[s];
994 return -1;
995}
996
997/**
998 * dccp_feat_prefer - Move preferred entry to the start of array
999 * Reorder the @array_len elements in @array so that @preferred_value comes
1000 * first. Returns >0 to indicate that @preferred_value does occur in @array.
1001 */
1002static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
1003{
1004 u8 i, does_occur = 0;
1005
1006 if (array != NULL) {
1007 for (i = 0; i < array_len; i++)
1008 if (array[i] == preferred_value) {
1009 array[i] = array[0];
1010 does_occur++;
1011 }
1012 if (does_occur)
1013 array[0] = preferred_value;
1014 }
1015 return does_occur;
1016}
1017
1018/**
1019 * dccp_feat_reconcile - Reconcile SP preference lists
1020 * @fval: SP list to reconcile into
1021 * @arr: received SP preference list
1022 * @len: length of @arr in bytes
1023 * @is_server: whether this side is the server (and @fv is the server's list)
1024 * @reorder: whether to reorder the list in @fv after reconciling with @arr
1025 * When successful, > 0 is returned and the reconciled list is in @fval.
1026 * A value of 0 means that negotiation failed (no shared entry).
1027 */
1028static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
1029 bool is_server, bool reorder)
1030{
1031 int rc;
1032
1033 if (!fv->sp.vec || !arr) {
1034 DCCP_CRIT("NULL feature value or array");
1035 return 0;
1036 }
1037
1038 if (is_server)
1039 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
1040 else
1041 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
1042
1043 if (!reorder)
1044 return rc;
1045 if (rc < 0)
1046 return 0;
1047
1048 /*
1049 * Reorder list: used for activating features and in dccp_insert_fn_opt.
1050 */
1051 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
1052}
1053
e77b8363
GR
1054/**
1055 * dccp_feat_change_recv - Process incoming ChangeL/R options
1056 * @fn: feature-negotiation list to update
1057 * @is_mandatory: whether the Change was preceded by a Mandatory option
1058 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1059 * @feat: one of %dccp_feature_numbers
1060 * @val: NN value or SP value/preference list
1061 * @len: length of @val in bytes
1062 * @server: whether this node is the server (1) or the client (0)
1063 */
1064static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1065 u8 feat, u8 *val, u8 len, const bool server)
1066{
1067 u8 defval, type = dccp_feat_type(feat);
1068 const bool local = (opt == DCCPO_CHANGE_R);
1069 struct dccp_feat_entry *entry;
1070 dccp_feat_val fval;
1071
1072 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
1073 goto unknown_feature_or_value;
1074
f3f3abb6
GR
1075 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1076
e77b8363
GR
1077 /*
1078 * Negotiation of NN features: Change R is invalid, so there is no
1079 * simultaneous negotiation; hence we do not look up in the list.
1080 */
1081 if (type == FEAT_NN) {
1082 if (local || len > sizeof(fval.nn))
1083 goto unknown_feature_or_value;
1084
1085 /* 6.3.2: "The feature remote MUST accept any valid value..." */
1086 fval.nn = dccp_decode_value_var(val, len);
1087 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1088 goto unknown_feature_or_value;
1089
1090 return dccp_feat_push_confirm(fn, feat, local, &fval);
1091 }
1092
1093 /*
1094 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
1095 */
1096 entry = dccp_feat_list_lookup(fn, feat, local);
1097 if (entry == NULL) {
1098 /*
1099 * No particular preferences have been registered. We deal with
1100 * this situation by assuming that all valid values are equally
1101 * acceptable, and apply the following checks:
1102 * - if the peer's list is a singleton, we accept a valid value;
1103 * - if we are the server, we first try to see if the peer (the
1104 * client) advertises the default value. If yes, we use it,
1105 * otherwise we accept the preferred value;
1106 * - else if we are the client, we use the first list element.
1107 */
1108 if (dccp_feat_clone_sp_val(&fval, val, 1))
1109 return DCCP_RESET_CODE_TOO_BUSY;
1110
1111 if (len > 1 && server) {
1112 defval = dccp_feat_default_value(feat);
1113 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1114 fval.sp.vec[0] = defval;
1115 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1116 kfree(fval.sp.vec);
1117 goto unknown_feature_or_value;
1118 }
1119
1120 /* Treat unsupported CCIDs like invalid values */
1121 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1122 kfree(fval.sp.vec);
1123 goto not_valid_or_not_known;
1124 }
1125
1126 return dccp_feat_push_confirm(fn, feat, local, &fval);
1127
1128 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
1129 return 0;
1130 }
1131
1132 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1133 entry->empty_confirm = 0;
1134 } else if (is_mandatory) {
1135 return DCCP_RESET_CODE_MANDATORY_ERROR;
1136 } else if (entry->state == FEAT_INITIALISING) {
1137 /*
1138 * Failed simultaneous negotiation (server only): try to `save'
1139 * the connection by checking whether entry contains the default
1140 * value for @feat. If yes, send an empty Confirm to signal that
1141 * the received Change was not understood - which implies using
1142 * the default value.
1143 * If this also fails, we use Reset as the last resort.
1144 */
1145 WARN_ON(!server);
1146 defval = dccp_feat_default_value(feat);
1147 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1148 return DCCP_RESET_CODE_OPTION_ERROR;
1149 entry->empty_confirm = 1;
1150 }
1151 entry->needs_confirm = 1;
1152 entry->needs_mandatory = 0;
1153 entry->state = FEAT_STABLE;
1154 return 0;
1155
1156unknown_feature_or_value:
1157 if (!is_mandatory)
1158 return dccp_push_empty_confirm(fn, feat, local);
1159
1160not_valid_or_not_known:
1161 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1162 : DCCP_RESET_CODE_OPTION_ERROR;
1163}
1164
b1ad0042
GR
1165/**
1166 * dccp_feat_confirm_recv - Process received Confirm options
1167 * @fn: feature-negotiation list to update
1168 * @is_mandatory: whether @opt was preceded by a Mandatory option
1169 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1170 * @feat: one of %dccp_feature_numbers
1171 * @val: NN value or SP value/preference list
1172 * @len: length of @val in bytes
1173 * @server: whether this node is server (1) or client (0)
1174 */
1175static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1176 u8 feat, u8 *val, u8 len, const bool server)
1177{
1178 u8 *plist, plen, type = dccp_feat_type(feat);
1179 const bool local = (opt == DCCPO_CONFIRM_R);
1180 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1181
f3f3abb6
GR
1182 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1183
b1ad0042
GR
1184 if (entry == NULL) { /* nothing queued: ignore or handle error */
1185 if (is_mandatory && type == FEAT_UNKNOWN)
1186 return DCCP_RESET_CODE_MANDATORY_ERROR;
1187
1188 if (!local && type == FEAT_NN) /* 6.3.2 */
1189 goto confirmation_failed;
1190 return 0;
1191 }
1192
1193 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
1194 return 0;
1195
1196 if (len == 0) {
1197 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1198 goto confirmation_failed;
1199 /*
1200 * Empty Confirm during connection setup: this means reverting
1201 * to the `old' value, which in this case is the default. Since
1202 * we handle default values automatically when no other values
1203 * have been set, we revert to the old value by removing this
1204 * entry from the list.
1205 */
1206 dccp_feat_list_pop(entry);
1207 return 0;
1208 }
1209
1210 if (type == FEAT_NN) {
1211 if (len > sizeof(entry->val.nn))
1212 goto confirmation_failed;
1213
1214 if (entry->val.nn == dccp_decode_value_var(val, len))
1215 goto confirmation_succeeded;
1216
1217 DCCP_WARN("Bogus Confirm for non-existing value\n");
1218 goto confirmation_failed;
1219 }
1220
1221 /*
1222 * Parsing SP Confirms: the first element of @val is the preferred
1223 * SP value which the peer confirms, the remainder depends on @len.
1224 * Note that only the confirmed value need to be a valid SP value.
1225 */
1226 if (!dccp_feat_is_valid_sp_val(feat, *val))
1227 goto confirmation_failed;
1228
1229 if (len == 1) { /* peer didn't supply a preference list */
1230 plist = val;
1231 plen = len;
1232 } else { /* preferred value + preference list */
1233 plist = val + 1;
1234 plen = len - 1;
1235 }
1236
1237 /* Check whether the peer got the reconciliation right (6.6.8) */
1238 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1239 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1240 return DCCP_RESET_CODE_OPTION_ERROR;
1241 }
1242 entry->val.sp.vec[0] = *val;
1243
1244confirmation_succeeded:
1245 entry->state = FEAT_STABLE;
1246 return 0;
1247
1248confirmation_failed:
1249 DCCP_WARN("Confirmation failed\n");
1250 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1251 : DCCP_RESET_CODE_OPTION_ERROR;
1252}
1253
e77b8363
GR
1254/**
1255 * dccp_feat_parse_options - Process Feature-Negotiation Options
1256 * @sk: for general use and used by the client during connection setup
1257 * @dreq: used by the server during connection setup
1258 * @mandatory: whether @opt was preceded by a Mandatory option
1259 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1260 * @feat: one of %dccp_feature_numbers
1261 * @val: value contents of @opt
1262 * @len: length of @val in bytes
1263 * Returns 0 on success, a Reset code for ending the connection otherwise.
1264 */
1265int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1266 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1267{
1268 struct dccp_sock *dp = dccp_sk(sk);
1269 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1270 bool server = false;
1271
1272 switch (sk->sk_state) {
1273 /*
1274 * Negotiation during connection setup
1275 */
1276 case DCCP_LISTEN:
1277 server = true; /* fall through */
1278 case DCCP_REQUESTING:
1279 switch (opt) {
1280 case DCCPO_CHANGE_L:
1281 case DCCPO_CHANGE_R:
1282 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1283 val, len, server);
b1ad0042
GR
1284 case DCCPO_CONFIRM_R:
1285 case DCCPO_CONFIRM_L:
1286 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1287 val, len, server);
e77b8363
GR
1288 }
1289 }
1290 return 0; /* ignore FN options in all other states */
1291}
1292
f90f92ee
GR
1293/**
1294 * dccp_feat_init - Seed feature negotiation with host-specific defaults
1295 * This initialises global defaults, depending on the value of the sysctls.
1296 * These can later be overridden by registering changes via setsockopt calls.
1297 * The last link in the chain is finalise_settings, to make sure that between
1298 * here and the start of actual feature negotiation no inconsistencies enter.
1299 *
1300 * All features not appearing below use either defaults or are otherwise
1301 * later adjusted through dccp_feat_finalise_settings().
1302 */
e8ef967a 1303int dccp_feat_init(struct sock *sk)
afe00251 1304{
f90f92ee
GR
1305 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1306 u8 on = 1, off = 0;
afe00251 1307 int rc;
f90f92ee
GR
1308 struct {
1309 u8 *val;
1310 u8 len;
1311 } tx, rx;
1312
1313 /* Non-negotiable (NN) features */
1314 rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
883ca833 1315 sysctl_dccp_sequence_window);
f90f92ee
GR
1316 if (rc)
1317 return rc;
1318
1319 /* Server-priority (SP) features */
1320
1321 /* Advertise that short seqnos are not supported (7.6.1) */
1322 rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1323 if (rc)
1324 return rc;
afe00251 1325
f90f92ee
GR
1326 /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1327 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1328 if (rc)
1329 return rc;
1330
1331 /*
1332 * We advertise the available list of CCIDs and reorder according to
1333 * preferences, to avoid failure resulting from negotiating different
1334 * singleton values (which always leads to failure).
1335 * These settings can still (later) be overridden via sockopts.
1336 */
1337 if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1338 ccid_get_builtin_ccids(&rx.val, &rx.len))
1339 return -ENOBUFS;
afe00251 1340
883ca833
GR
1341 if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1342 !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
f90f92ee
GR
1343 goto free_ccid_lists;
1344
1345 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1346 if (rc)
1347 goto free_ccid_lists;
1348
1349 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1350
1351free_ccid_lists:
1352 kfree(tx.val);
1353 kfree(rx.val);
afe00251
AB
1354 return rc;
1355}
1356
422d9cdc
GR
1357int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1358{
1359 struct dccp_sock *dp = dccp_sk(sk);
1360 struct dccp_feat_entry *cur, *next;
1361 int idx;
1362 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1363 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1364 };
1365
1366 list_for_each_entry(cur, fn_list, node) {
1367 /*
1368 * An empty Confirm means that either an unknown feature type
1369 * or an invalid value was present. In the first case there is
1370 * nothing to activate, in the other the default value is used.
1371 */
1372 if (cur->empty_confirm)
1373 continue;
1374
1375 idx = dccp_feat_index(cur->feat_num);
1376 if (idx < 0) {
1377 DCCP_BUG("Unknown feature %u", cur->feat_num);
1378 goto activation_failed;
1379 }
1380 if (cur->state != FEAT_STABLE) {
f3f3abb6 1381 DCCP_CRIT("Negotiation of %s %s failed in state %s",
422d9cdc 1382 cur->is_local ? "local" : "remote",
f3f3abb6
GR
1383 dccp_feat_fname(cur->feat_num),
1384 dccp_feat_sname[cur->state]);
422d9cdc
GR
1385 goto activation_failed;
1386 }
1387 fvals[idx][cur->is_local] = &cur->val;
1388 }
1389
1390 /*
1391 * Activate in decreasing order of index, so that the CCIDs are always
1392 * activated as the last feature. This avoids the case where a CCID
1393 * relies on the initialisation of one or more features that it depends
1394 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1395 */
1396 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1397 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1398 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1399 DCCP_CRIT("Could not activate %d", idx);
1400 goto activation_failed;
1401 }
1402
1403 /* Clean up Change options which have been confirmed already */
1404 list_for_each_entry_safe(cur, next, fn_list, node)
1405 if (!cur->needs_confirm)
1406 dccp_feat_list_pop(cur);
1407
1408 dccp_pr_debug("Activation OK\n");
1409 return 0;
1410
1411activation_failed:
1412 /*
1413 * We clean up everything that may have been allocated, since
1414 * it is difficult to track at which stage negotiation failed.
1415 * This is ok, since all allocation functions below are robust
1416 * against NULL arguments.
1417 */
1418 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1419 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1420 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1421 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1422 dp->dccps_hc_rx_ackvec = NULL;
1423 return -1;
1424}
This page took 0.793448 seconds and 5 git commands to generate.