blob: 972b8dc918d6ab90d5d11f55f2e2e7c585def32a [file] [log] [blame]
Andrea Bittauafe00252006-03-20 17:43:56 -08001/*
2 * net/dccp/feat.c
3 *
Gerrit Renker63b8e282008-12-08 01:17:32 -08004 * 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 *
Andrea Bittauafe00252006-03-20 17:43:56 -080010 *
Gerrit Renker5cdae192007-12-13 12:41:46 -020011 * ASSUMPTIONS
12 * -----------
Gerrit Renkerf74e91b2008-11-12 00:42:58 -080013 * o Feature negotiation is coordinated with connection setup (as in TCP), wild
14 * changes of parameters of an established connection are not supported.
Gerrit Renker5cdae192007-12-13 12:41:46 -020015 * o All currently known SP features have 1-byte quantities. If in the future
16 * extensions of RFCs 4340..42 define features with item lengths larger than
17 * one byte, a feature-specific extension of the code will be required.
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
Andrea Bittauafe00252006-03-20 17:43:56 -080023 */
Andrea Bittauafe00252006-03-20 17:43:56 -080024#include <linux/module.h>
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080025#include "ccid.h"
Andrea Bittauafe00252006-03-20 17:43:56 -080026#include "feat.h"
27
Gerrit Renker883ca832009-01-16 23:36:32 +000028/* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */
29unsigned long sysctl_dccp_sequence_window __read_mostly = 100;
30int sysctl_dccp_rx_ccid __read_mostly = 2,
31 sysctl_dccp_tx_ccid __read_mostly = 2;
32
Gerrit Renker422d9cd2008-12-01 23:34:01 -080033/*
34 * Feature activation handlers.
35 *
36 * These all use an u64 argument, to provide enough room for NN/SP features. At
37 * this stage the negotiated values have been checked to be within their range.
38 */
39static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
40{
41 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renkere5fd56c2009-01-04 21:43:23 -080042 struct ccid *new_ccid = ccid_new(ccid, sk, rx);
Gerrit Renker422d9cd2008-12-01 23:34:01 -080043
44 if (new_ccid == NULL)
45 return -ENOMEM;
46
47 if (rx) {
48 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
49 dp->dccps_hc_rx_ccid = new_ccid;
50 } else {
51 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
52 dp->dccps_hc_tx_ccid = new_ccid;
53 }
54 return 0;
55}
56
57static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
58{
Gerrit Renker792b4872009-01-16 23:36:31 +000059 struct dccp_sock *dp = dccp_sk(sk);
60
61 if (rx) {
62 dp->dccps_r_seq_win = seq_win;
63 /* propagate changes to update SWL/SWH */
64 dccp_update_gsr(sk, dp->dccps_gsr);
65 } else {
66 dp->dccps_l_seq_win = seq_win;
67 /* propagate changes to update AWL */
68 dccp_update_gss(sk, dp->dccps_gss);
69 }
Gerrit Renker422d9cd2008-12-01 23:34:01 -080070 return 0;
71}
72
73static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
74{
75 if (rx)
76 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
77 else
78 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
79 return 0;
80}
81
82static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
83{
84 struct dccp_sock *dp = dccp_sk(sk);
85
86 if (rx) {
87 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
88 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
89 if (dp->dccps_hc_rx_ackvec == NULL)
90 return -ENOMEM;
91 } else if (!enable) {
92 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
93 dp->dccps_hc_rx_ackvec = NULL;
94 }
95 }
96 return 0;
97}
98
99static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
100{
101 if (!rx)
Gerrit Renker4098dce2008-12-08 01:18:37 -0800102 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800103 return 0;
104}
105
106/*
107 * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
108 * `rx' holds when the sending peer informs about his partial coverage via a
109 * ChangeR() option. In the other case, we are the sender and the receiver
110 * announces its coverage via ChangeL() options. The policy here is to honour
111 * such communication by enabling the corresponding partial coverage - but only
112 * if it has not been set manually before; the warning here means that all
113 * packets will be dropped.
114 */
115static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
116{
117 struct dccp_sock *dp = dccp_sk(sk);
118
119 if (rx)
120 dp->dccps_pcrlen = cscov;
121 else {
122 if (dp->dccps_pcslen == 0)
123 dp->dccps_pcslen = cscov;
124 else if (cscov > dp->dccps_pcslen)
125 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
126 dp->dccps_pcslen, (u8)cscov);
127 }
128 return 0;
129}
130
Gerrit Renker7d43d1a2008-11-04 23:43:47 -0800131static const struct {
132 u8 feat_num; /* DCCPF_xxx */
133 enum dccp_feat_type rxtx; /* RX or TX */
134 enum dccp_feat_type reconciliation; /* SP or NN */
135 u8 default_value; /* as in 6.4 */
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800136 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
Gerrit Renker7d43d1a2008-11-04 23:43:47 -0800137/*
138 * Lookup table for location and type of features (from RFC 4340/4342)
139 * +--------------------------+----+-----+----+----+---------+-----------+
140 * | Feature | Location | Reconc. | Initial | Section |
141 * | | RX | TX | SP | NN | Value | Reference |
142 * +--------------------------+----+-----+----+----+---------+-----------+
143 * | DCCPF_CCID | | X | X | | 2 | 10 |
144 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
145 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
146 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
147 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
148 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
149 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
150 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
151 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
152 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
153 * +--------------------------+----+-----+----+----+---------+-----------+
154 */
155} dccp_feat_table[] = {
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800156 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
157 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
158 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
159 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
160 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
161 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
162 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
163 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
164 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
165 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
Gerrit Renker7d43d1a2008-11-04 23:43:47 -0800166};
167#define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
168
Gerrit Renker61e64732008-11-04 23:54:04 -0800169/**
170 * dccp_feat_index - Hash function to map feature number into array position
171 * Returns consecutive array index or -1 if the feature is not understood.
172 */
173static int dccp_feat_index(u8 feat_num)
174{
175 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
176 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
177 return feat_num - 1;
178
179 /*
180 * Other features: add cases for new feature types here after adding
181 * them to the above table.
182 */
183 switch (feat_num) {
184 case DCCPF_SEND_LEV_RATE:
185 return DCCP_FEAT_SUPPORTED_MAX - 1;
186 }
187 return -1;
188}
189
190static u8 dccp_feat_type(u8 feat_num)
191{
192 int idx = dccp_feat_index(feat_num);
193
194 if (idx < 0)
195 return FEAT_UNKNOWN;
196 return dccp_feat_table[idx].reconciliation;
197}
198
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800199static int dccp_feat_default_value(u8 feat_num)
200{
201 int idx = dccp_feat_index(feat_num);
202 /*
203 * There are no default values for unknown features, so encountering a
204 * negative index here indicates a serious problem somewhere else.
205 */
206 DCCP_BUG_ON(idx < 0);
207
208 return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
209}
210
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000211/*
212 * Debugging and verbose-printing section
213 */
214static const char *dccp_feat_fname(const u8 feat)
215{
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700216 static const char *const feature_names[] = {
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000217 [DCCPF_RESERVED] = "Reserved",
218 [DCCPF_CCID] = "CCID",
219 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
220 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
221 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
222 [DCCPF_ACK_RATIO] = "Ack Ratio",
223 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
224 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
225 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
226 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
227 };
228 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
229 return feature_names[DCCPF_RESERVED];
230
231 if (feat == DCCPF_SEND_LEV_RATE)
232 return "Send Loss Event Rate";
233 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
234 return "CCID-specific";
235
236 return feature_names[feat];
237}
238
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700239static const char *const dccp_feat_sname[] = {
240 "DEFAULT", "INITIALISING", "CHANGING", "UNSTABLE", "STABLE",
241};
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000242
243#ifdef CONFIG_IP_DCCP_DEBUG
244static const char *dccp_feat_oname(const u8 opt)
245{
246 switch (opt) {
247 case DCCPO_CHANGE_L: return "Change_L";
248 case DCCPO_CONFIRM_L: return "Confirm_L";
249 case DCCPO_CHANGE_R: return "Change_R";
250 case DCCPO_CONFIRM_R: return "Confirm_R";
251 }
252 return NULL;
253}
254
255static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
256{
257 u8 i, type = dccp_feat_type(feat_num);
258
259 if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL))
260 dccp_pr_debug_cat("(NULL)");
261 else if (type == FEAT_SP)
262 for (i = 0; i < val->sp.len; i++)
263 dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]);
264 else if (type == FEAT_NN)
265 dccp_pr_debug_cat("%llu", (unsigned long long)val->nn);
266 else
267 dccp_pr_debug_cat("unknown type %u", type);
268}
269
270static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
271{
272 u8 type = dccp_feat_type(feat_num);
273 dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
274
275 if (type == FEAT_NN)
276 fval.nn = dccp_decode_value_var(list, len);
277 dccp_feat_printval(feat_num, &fval);
278}
279
280static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
281{
282 dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote",
283 dccp_feat_fname(entry->feat_num));
284 dccp_feat_printval(entry->feat_num, &entry->val);
285 dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
286 entry->needs_confirm ? "(Confirm pending)" : "");
287}
288
289#define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \
290 dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\
291 dccp_feat_printvals(feat, val, len); \
292 dccp_pr_debug_cat(") %s\n", mandatory ? "!" : ""); } while (0)
293
294#define dccp_feat_print_fnlist(fn_list) { \
295 const struct dccp_feat_entry *___entry; \
296 \
297 dccp_pr_debug("List Dump:\n"); \
298 list_for_each_entry(___entry, fn_list, node) \
299 dccp_feat_print_entry(___entry); \
300}
301#else /* ! CONFIG_IP_DCCP_DEBUG */
302#define dccp_feat_print_opt(opt, feat, val, len, mandatory)
303#define dccp_feat_print_fnlist(fn_list)
304#endif
305
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800306static int __dccp_feat_activate(struct sock *sk, const int idx,
307 const bool is_local, dccp_feat_val const *fval)
308{
309 bool rx;
310 u64 val;
311
312 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
313 return -1;
314 if (dccp_feat_table[idx].activation_hdlr == NULL)
315 return 0;
316
317 if (fval == NULL) {
318 val = dccp_feat_table[idx].default_value;
319 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
320 if (fval->sp.vec == NULL) {
321 /*
322 * This can happen when an empty Confirm is sent
323 * for an SP (i.e. known) feature. In this case
324 * we would be using the default anyway.
325 */
326 DCCP_CRIT("Feature #%d undefined: using default", idx);
327 val = dccp_feat_table[idx].default_value;
328 } else {
329 val = fval->sp.vec[0];
330 }
331 } else {
332 val = fval->nn;
333 }
334
335 /* Location is RX if this is a local-RX or remote-TX feature */
336 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
337
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000338 dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
339 dccp_feat_fname(dccp_feat_table[idx].feat_num),
340 fval ? "" : "default ", (unsigned long long)val);
341
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800342 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
343}
344
Gerrit Renkerb1ad0042008-12-01 23:33:18 -0800345/* Test for "Req'd" feature (RFC 4340, 6.4) */
346static inline int dccp_feat_must_be_understood(u8 feat_num)
347{
348 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
349 feat_num == DCCPF_SEQUENCE_WINDOW;
350}
351
Gerrit Renkerac757732008-11-04 23:55:49 -0800352/* copy constructor, fval must not already contain allocated memory */
353static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
354{
355 fval->sp.len = len;
356 if (fval->sp.len > 0) {
357 fval->sp.vec = kmemdup(val, len, gfp_any());
358 if (fval->sp.vec == NULL) {
359 fval->sp.len = 0;
360 return -ENOBUFS;
361 }
362 }
363 return 0;
364}
365
Gerrit Renker61e64732008-11-04 23:54:04 -0800366static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
367{
368 if (unlikely(val == NULL))
369 return;
370 if (dccp_feat_type(feat_num) == FEAT_SP)
371 kfree(val->sp.vec);
372 memset(val, 0, sizeof(*val));
373}
374
Gerrit Renkerac757732008-11-04 23:55:49 -0800375static struct dccp_feat_entry *
376 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
377{
378 struct dccp_feat_entry *new;
379 u8 type = dccp_feat_type(original->feat_num);
380
381 if (type == FEAT_UNKNOWN)
382 return NULL;
383
384 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
385 if (new == NULL)
386 return NULL;
387
388 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
389 original->val.sp.vec,
390 original->val.sp.len)) {
391 kfree(new);
392 return NULL;
393 }
394 return new;
395}
396
Gerrit Renker61e64732008-11-04 23:54:04 -0800397static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
398{
399 if (entry != NULL) {
400 dccp_feat_val_destructor(entry->feat_num, &entry->val);
401 kfree(entry);
402 }
403}
404
405/*
406 * List management functions
407 *
408 * Feature negotiation lists rely on and maintain the following invariants:
409 * - each feat_num in the list is known, i.e. we know its type and default value
410 * - each feat_num/is_local combination is unique (old entries are overwritten)
411 * - SP values are always freshly allocated
412 * - list is sorted in increasing order of feature number (faster lookup)
413 */
Gerrit Renker0c116832008-11-16 22:49:52 -0800414static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
415 u8 feat_num, bool is_local)
416{
417 struct dccp_feat_entry *entry;
418
Gerrit Renker3d3e35a2008-11-20 01:03:08 -0800419 list_for_each_entry(entry, fn_list, node) {
Gerrit Renker0c116832008-11-16 22:49:52 -0800420 if (entry->feat_num == feat_num && entry->is_local == is_local)
421 return entry;
422 else if (entry->feat_num > feat_num)
423 break;
Gerrit Renker3d3e35a2008-11-20 01:03:08 -0800424 }
Gerrit Renker0c116832008-11-16 22:49:52 -0800425 return NULL;
426}
Gerrit Renker61e64732008-11-04 23:54:04 -0800427
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800428/**
429 * dccp_feat_entry_new - Central list update routine (called by all others)
430 * @head: list to add to
431 * @feat: feature number
432 * @local: whether the local (1) or remote feature with number @feat is meant
433 * This is the only constructor and serves to ensure the above invariants.
434 */
435static struct dccp_feat_entry *
436 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
437{
438 struct dccp_feat_entry *entry;
439
440 list_for_each_entry(entry, head, node)
441 if (entry->feat_num == feat && entry->is_local == local) {
442 dccp_feat_val_destructor(entry->feat_num, &entry->val);
443 return entry;
444 } else if (entry->feat_num > feat) {
445 head = &entry->node;
446 break;
447 }
448
449 entry = kmalloc(sizeof(*entry), gfp_any());
450 if (entry != NULL) {
451 entry->feat_num = feat;
452 entry->is_local = local;
453 list_add_tail(&entry->node, head);
454 }
455 return entry;
456}
457
458/**
459 * dccp_feat_push_change - Add/overwrite a Change option in the list
460 * @fn_list: feature-negotiation list to update
461 * @feat: one of %dccp_feature_numbers
462 * @local: whether local (1) or remote (0) @feat_num is meant
463 * @needs_mandatory: whether to use Mandatory feature negotiation options
464 * @fval: pointer to NN/SP value to be inserted (will be copied)
465 */
466static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
467 u8 mandatory, dccp_feat_val *fval)
468{
469 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
470
471 if (new == NULL)
472 return -ENOMEM;
473
474 new->feat_num = feat;
475 new->is_local = local;
476 new->state = FEAT_INITIALISING;
477 new->needs_confirm = 0;
478 new->empty_confirm = 0;
479 new->val = *fval;
480 new->needs_mandatory = mandatory;
481
482 return 0;
483}
484
Gerrit Renkere77b8362008-12-01 23:32:35 -0800485/**
486 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
487 * @fn_list: feature-negotiation list to add to
488 * @feat: one of %dccp_feature_numbers
489 * @local: whether local (1) or remote (0) @feat_num is being confirmed
490 * @fval: pointer to NN/SP value to be inserted or NULL
491 * Returns 0 on success, a Reset code for further processing otherwise.
492 */
493static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
494 dccp_feat_val *fval)
495{
496 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
497
498 if (new == NULL)
499 return DCCP_RESET_CODE_TOO_BUSY;
500
501 new->feat_num = feat;
502 new->is_local = local;
503 new->state = FEAT_STABLE; /* transition in 6.6.2 */
504 new->needs_confirm = 1;
505 new->empty_confirm = (fval == NULL);
506 new->val.nn = 0; /* zeroes the whole structure */
507 if (!new->empty_confirm)
508 new->val = *fval;
509 new->needs_mandatory = 0;
510
511 return 0;
512}
513
514static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
515{
516 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
517}
518
Gerrit Renker61e64732008-11-04 23:54:04 -0800519static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
520{
521 list_del(&entry->node);
522 dccp_feat_entry_destructor(entry);
523}
524
525void dccp_feat_list_purge(struct list_head *fn_list)
526{
527 struct dccp_feat_entry *entry, *next;
528
529 list_for_each_entry_safe(entry, next, fn_list, node)
530 dccp_feat_entry_destructor(entry);
531 INIT_LIST_HEAD(fn_list);
532}
533EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
534
Gerrit Renkerac757732008-11-04 23:55:49 -0800535/* generate @to as full clone of @from - @to must not contain any nodes */
536int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
537{
538 struct dccp_feat_entry *entry, *new;
539
540 INIT_LIST_HEAD(to);
541 list_for_each_entry(entry, from, node) {
542 new = dccp_feat_clone_entry(entry);
543 if (new == NULL)
544 goto cloning_failed;
545 list_add_tail(&new->node, to);
546 }
547 return 0;
548
549cloning_failed:
550 dccp_feat_list_purge(to);
551 return -ENOMEM;
552}
553
Gerrit Renker0971d172008-12-01 23:27:31 -0800554/**
555 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
556 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
557 * incoming options are accepted as long as their values are valid.
558 */
559static u8 dccp_feat_valid_nn_length(u8 feat_num)
560{
561 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
562 return 2;
563 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
564 return 6;
565 return 0;
566}
567
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800568static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
569{
570 switch (feat_num) {
571 case DCCPF_ACK_RATIO:
572 return val <= DCCPF_ACK_RATIO_MAX;
573 case DCCPF_SEQUENCE_WINDOW:
574 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
575 }
576 return 0; /* feature unknown - so we can't tell */
577}
578
579/* check that SP values are within the ranges defined in RFC 4340 */
580static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
581{
582 switch (feat_num) {
583 case DCCPF_CCID:
584 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
585 /* Type-check Boolean feature values: */
586 case DCCPF_SHORT_SEQNOS:
587 case DCCPF_ECN_INCAPABLE:
588 case DCCPF_SEND_ACK_VECTOR:
589 case DCCPF_SEND_NDP_COUNT:
590 case DCCPF_DATA_CHECKSUM:
591 case DCCPF_SEND_LEV_RATE:
592 return val < 2;
593 case DCCPF_MIN_CSUM_COVER:
594 return val < 16;
595 }
596 return 0; /* feature unknown */
597}
598
599static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
600{
601 if (sp_list == NULL || sp_len < 1)
602 return 0;
603 while (sp_len--)
604 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
605 return 0;
606 return 1;
607}
608
609/**
Gerrit Renker0971d172008-12-01 23:27:31 -0800610 * dccp_feat_insert_opts - Generate FN options from current list state
611 * @skb: next sk_buff to be sent to the peer
612 * @dp: for client during handshake and general negotiation
613 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
614 */
615int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
616 struct sk_buff *skb)
617{
618 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
619 struct dccp_feat_entry *pos, *next;
620 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
621 bool rpt;
622
623 /* put entries into @skb in the order they appear in the list */
624 list_for_each_entry_safe_reverse(pos, next, fn, node) {
625 opt = dccp_feat_genopt(pos);
626 type = dccp_feat_type(pos->feat_num);
627 rpt = false;
628
629 if (pos->empty_confirm) {
630 len = 0;
631 ptr = NULL;
632 } else {
633 if (type == FEAT_SP) {
634 len = pos->val.sp.len;
635 ptr = pos->val.sp.vec;
636 rpt = pos->needs_confirm;
637 } else if (type == FEAT_NN) {
638 len = dccp_feat_valid_nn_length(pos->feat_num);
639 ptr = nn_in_nbo;
640 dccp_encode_value_var(pos->val.nn, ptr, len);
641 } else {
642 DCCP_BUG("unknown feature %u", pos->feat_num);
643 return -1;
644 }
645 }
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000646 dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
Gerrit Renker0971d172008-12-01 23:27:31 -0800647
648 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
649 return -1;
650 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
651 return -1;
652 /*
653 * Enter CHANGING after transmitting the Change option (6.6.2).
654 */
655 if (pos->state == FEAT_INITIALISING)
656 pos->state = FEAT_CHANGING;
657 }
658 return 0;
659}
660
661/**
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800662 * __feat_register_nn - Register new NN value on socket
663 * @fn: feature-negotiation list to register with
664 * @feat: an NN feature from %dccp_feature_numbers
665 * @mandatory: use Mandatory option if 1
666 * @nn_val: value to register (restricted to 4 bytes)
667 * Note that NN features are local by definition (RFC 4340, 6.3.2).
668 */
669static int __feat_register_nn(struct list_head *fn, u8 feat,
670 u8 mandatory, u64 nn_val)
671{
672 dccp_feat_val fval = { .nn = nn_val };
673
674 if (dccp_feat_type(feat) != FEAT_NN ||
675 !dccp_feat_is_valid_nn_val(feat, nn_val))
676 return -EINVAL;
677
678 /* Don't bother with default values, they will be activated anyway. */
679 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
680 return 0;
681
682 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
683}
684
685/**
686 * __feat_register_sp - Register new SP value/list on socket
687 * @fn: feature-negotiation list to register with
688 * @feat: an SP feature from %dccp_feature_numbers
689 * @is_local: whether the local (1) or the remote (0) @feat is meant
690 * @mandatory: use Mandatory option if 1
691 * @sp_val: SP value followed by optional preference list
692 * @sp_len: length of @sp_val in bytes
693 */
694static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
695 u8 mandatory, u8 const *sp_val, u8 sp_len)
696{
697 dccp_feat_val fval;
698
699 if (dccp_feat_type(feat) != FEAT_SP ||
700 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
701 return -EINVAL;
702
Gerrit Renkerd90ebcb2008-11-12 00:47:26 -0800703 /* Avoid negotiating alien CCIDs by only advertising supported ones */
704 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
705 return -EOPNOTSUPP;
706
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800707 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
708 return -ENOMEM;
709
710 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
711}
712
Gerrit Renker49aebc62008-11-16 22:51:23 -0800713/**
714 * dccp_feat_register_sp - Register requests to change SP feature values
715 * @sk: client or listening socket
716 * @feat: one of %dccp_feature_numbers
717 * @is_local: whether the local (1) or remote (0) @feat is meant
718 * @list: array of preferred values, in descending order of preference
719 * @len: length of @list in bytes
720 */
721int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
722 u8 const *list, u8 len)
723{ /* any changes must be registered before establishing the connection */
724 if (sk->sk_state != DCCP_CLOSED)
725 return -EISCONN;
726 if (dccp_feat_type(feat) != FEAT_SP)
Chris Wright19443172008-05-05 13:50:24 -0700727 return -EINVAL;
Gerrit Renker49aebc62008-11-16 22:51:23 -0800728 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
729 0, list, len);
Andrea Bittauafe00252006-03-20 17:43:56 -0800730}
731
Gerrit Renker49aebc62008-11-16 22:51:23 -0800732/* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
733int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
734{
735 /* any changes must be registered before establishing the connection */
736 if (sk->sk_state != DCCP_CLOSED)
737 return -EISCONN;
738 if (dccp_feat_type(feat) != FEAT_NN)
739 return -EINVAL;
740 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
741}
Andrea Bittauafe00252006-03-20 17:43:56 -0800742
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800743/*
744 * Tracking features whose value depend on the choice of CCID
745 *
746 * This is designed with an extension in mind so that a list walk could be done
747 * before activating any features. However, the existing framework was found to
748 * work satisfactorily up until now, the automatic verification is left open.
749 * When adding new CCIDs, add a corresponding dependency table here.
750 */
751static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
752{
753 static const struct ccid_dependency ccid2_dependencies[2][2] = {
754 /*
755 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
756 * feature and Send Ack Vector is an RX feature, `is_local'
757 * needs to be reversed.
758 */
759 { /* Dependencies of the receiver-side (remote) CCID2 */
760 {
761 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
762 .is_local = true,
763 .is_mandatory = true,
764 .val = 1
765 },
766 { 0, 0, 0, 0 }
767 },
768 { /* Dependencies of the sender-side (local) CCID2 */
769 {
770 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
771 .is_local = false,
772 .is_mandatory = true,
773 .val = 1
774 },
775 { 0, 0, 0, 0 }
776 }
777 };
778 static const struct ccid_dependency ccid3_dependencies[2][5] = {
779 { /*
780 * Dependencies of the receiver-side CCID3
781 */
782 { /* locally disable Ack Vectors */
783 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
784 .is_local = true,
785 .is_mandatory = false,
786 .val = 0
787 },
788 { /* see below why Send Loss Event Rate is on */
789 .dependent_feat = DCCPF_SEND_LEV_RATE,
790 .is_local = true,
791 .is_mandatory = true,
792 .val = 1
793 },
794 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
795 .dependent_feat = DCCPF_SEND_NDP_COUNT,
796 .is_local = false,
797 .is_mandatory = true,
798 .val = 1
799 },
800 { 0, 0, 0, 0 },
801 },
802 { /*
803 * CCID3 at the TX side: we request that the HC-receiver
804 * will not send Ack Vectors (they will be ignored, so
805 * Mandatory is not set); we enable Send Loss Event Rate
806 * (Mandatory since the implementation does not support
807 * the Loss Intervals option of RFC 4342, 8.6).
808 * The last two options are for peer's information only.
809 */
810 {
811 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
812 .is_local = false,
813 .is_mandatory = false,
814 .val = 0
815 },
816 {
817 .dependent_feat = DCCPF_SEND_LEV_RATE,
818 .is_local = false,
819 .is_mandatory = true,
820 .val = 1
821 },
822 { /* this CCID does not support Ack Ratio */
823 .dependent_feat = DCCPF_ACK_RATIO,
824 .is_local = true,
825 .is_mandatory = false,
826 .val = 0
827 },
828 { /* tell receiver we are sending NDP counts */
829 .dependent_feat = DCCPF_SEND_NDP_COUNT,
830 .is_local = true,
831 .is_mandatory = false,
832 .val = 1
833 },
834 { 0, 0, 0, 0 }
835 }
836 };
837 switch (ccid) {
838 case DCCPC_CCID2:
839 return ccid2_dependencies[is_local];
840 case DCCPC_CCID3:
841 return ccid3_dependencies[is_local];
842 default:
843 return NULL;
844 }
845}
846
847/**
848 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
849 * @fn: feature-negotiation list to update
850 * @id: CCID number to track
851 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
852 * This function needs to be called after registering all other features.
853 */
854static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
855{
856 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
857 int i, rc = (table == NULL);
858
859 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
860 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
861 rc = __feat_register_sp(fn, table[i].dependent_feat,
862 table[i].is_local,
863 table[i].is_mandatory,
864 &table[i].val, 1);
865 else
866 rc = __feat_register_nn(fn, table[i].dependent_feat,
867 table[i].is_mandatory,
868 table[i].val);
869 return rc;
870}
871
872/**
873 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
874 * @dp: client or listening socket (settings will be inherited)
875 * This is called after all registrations (socket initialisation, sysctls, and
876 * sockopt calls), and before sending the first packet containing Change options
877 * (ie. client-Request or server-Response), to ensure internal consistency.
878 */
879int dccp_feat_finalise_settings(struct dccp_sock *dp)
880{
881 struct list_head *fn = &dp->dccps_featneg;
882 struct dccp_feat_entry *entry;
883 int i = 2, ccids[2] = { -1, -1 };
884
885 /*
886 * Propagating CCIDs:
887 * 1) not useful to propagate CCID settings if this host advertises more
888 * than one CCID: the choice of CCID may still change - if this is
889 * the client, or if this is the server and the client sends
890 * singleton CCID values.
891 * 2) since is that propagate_ccid changes the list, we defer changing
892 * the sorted list until after the traversal.
893 */
894 list_for_each_entry(entry, fn, node)
895 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
896 ccids[entry->is_local] = entry->val.sp.vec[0];
897 while (i--)
898 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
899 return -1;
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000900 dccp_feat_print_fnlist(fn);
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800901 return 0;
902}
903
Gerrit Renker0c116832008-11-16 22:49:52 -0800904/**
905 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
906 * It is the server which resolves the dependencies once the CCID has been
907 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
908 */
909int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
910{
911 struct list_head *fn = &dreq->dreq_featneg;
912 struct dccp_feat_entry *entry;
913 u8 is_local, ccid;
914
915 for (is_local = 0; is_local <= 1; is_local++) {
916 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
917
918 if (entry != NULL && !entry->empty_confirm)
919 ccid = entry->val.sp.vec[0];
920 else
921 ccid = dccp_feat_default_value(DCCPF_CCID);
922
923 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
924 return -1;
925 }
926 return 0;
927}
928
Gerrit Renker75757a72008-12-01 23:31:04 -0800929/* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
930static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
931{
932 u8 c, s;
933
934 for (s = 0; s < slen; s++)
935 for (c = 0; c < clen; c++)
936 if (servlist[s] == clilist[c])
937 return servlist[s];
938 return -1;
939}
940
941/**
942 * dccp_feat_prefer - Move preferred entry to the start of array
943 * Reorder the @array_len elements in @array so that @preferred_value comes
944 * first. Returns >0 to indicate that @preferred_value does occur in @array.
945 */
946static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
947{
948 u8 i, does_occur = 0;
949
950 if (array != NULL) {
951 for (i = 0; i < array_len; i++)
952 if (array[i] == preferred_value) {
953 array[i] = array[0];
954 does_occur++;
955 }
956 if (does_occur)
957 array[0] = preferred_value;
958 }
959 return does_occur;
960}
961
962/**
963 * dccp_feat_reconcile - Reconcile SP preference lists
964 * @fval: SP list to reconcile into
965 * @arr: received SP preference list
966 * @len: length of @arr in bytes
967 * @is_server: whether this side is the server (and @fv is the server's list)
968 * @reorder: whether to reorder the list in @fv after reconciling with @arr
969 * When successful, > 0 is returned and the reconciled list is in @fval.
970 * A value of 0 means that negotiation failed (no shared entry).
971 */
972static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
973 bool is_server, bool reorder)
974{
975 int rc;
976
977 if (!fv->sp.vec || !arr) {
978 DCCP_CRIT("NULL feature value or array");
979 return 0;
980 }
981
982 if (is_server)
983 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
984 else
985 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
986
987 if (!reorder)
988 return rc;
989 if (rc < 0)
990 return 0;
991
992 /*
993 * Reorder list: used for activating features and in dccp_insert_fn_opt.
994 */
995 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
996}
997
Gerrit Renkere77b8362008-12-01 23:32:35 -0800998/**
999 * dccp_feat_change_recv - Process incoming ChangeL/R options
1000 * @fn: feature-negotiation list to update
1001 * @is_mandatory: whether the Change was preceded by a Mandatory option
1002 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1003 * @feat: one of %dccp_feature_numbers
1004 * @val: NN value or SP value/preference list
1005 * @len: length of @val in bytes
1006 * @server: whether this node is the server (1) or the client (0)
1007 */
1008static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1009 u8 feat, u8 *val, u8 len, const bool server)
1010{
1011 u8 defval, type = dccp_feat_type(feat);
1012 const bool local = (opt == DCCPO_CHANGE_R);
1013 struct dccp_feat_entry *entry;
1014 dccp_feat_val fval;
1015
1016 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
1017 goto unknown_feature_or_value;
1018
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +00001019 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1020
Gerrit Renkere77b8362008-12-01 23:32:35 -08001021 /*
1022 * Negotiation of NN features: Change R is invalid, so there is no
1023 * simultaneous negotiation; hence we do not look up in the list.
1024 */
1025 if (type == FEAT_NN) {
1026 if (local || len > sizeof(fval.nn))
1027 goto unknown_feature_or_value;
1028
1029 /* 6.3.2: "The feature remote MUST accept any valid value..." */
1030 fval.nn = dccp_decode_value_var(val, len);
1031 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1032 goto unknown_feature_or_value;
1033
1034 return dccp_feat_push_confirm(fn, feat, local, &fval);
1035 }
1036
1037 /*
1038 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
1039 */
1040 entry = dccp_feat_list_lookup(fn, feat, local);
1041 if (entry == NULL) {
1042 /*
1043 * No particular preferences have been registered. We deal with
1044 * this situation by assuming that all valid values are equally
1045 * acceptable, and apply the following checks:
1046 * - if the peer's list is a singleton, we accept a valid value;
1047 * - if we are the server, we first try to see if the peer (the
1048 * client) advertises the default value. If yes, we use it,
1049 * otherwise we accept the preferred value;
1050 * - else if we are the client, we use the first list element.
1051 */
1052 if (dccp_feat_clone_sp_val(&fval, val, 1))
1053 return DCCP_RESET_CODE_TOO_BUSY;
1054
1055 if (len > 1 && server) {
1056 defval = dccp_feat_default_value(feat);
1057 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1058 fval.sp.vec[0] = defval;
1059 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1060 kfree(fval.sp.vec);
1061 goto unknown_feature_or_value;
1062 }
1063
1064 /* Treat unsupported CCIDs like invalid values */
1065 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1066 kfree(fval.sp.vec);
1067 goto not_valid_or_not_known;
1068 }
1069
1070 return dccp_feat_push_confirm(fn, feat, local, &fval);
1071
1072 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
1073 return 0;
1074 }
1075
1076 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1077 entry->empty_confirm = 0;
1078 } else if (is_mandatory) {
1079 return DCCP_RESET_CODE_MANDATORY_ERROR;
1080 } else if (entry->state == FEAT_INITIALISING) {
1081 /*
1082 * Failed simultaneous negotiation (server only): try to `save'
1083 * the connection by checking whether entry contains the default
1084 * value for @feat. If yes, send an empty Confirm to signal that
1085 * the received Change was not understood - which implies using
1086 * the default value.
1087 * If this also fails, we use Reset as the last resort.
1088 */
1089 WARN_ON(!server);
1090 defval = dccp_feat_default_value(feat);
1091 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1092 return DCCP_RESET_CODE_OPTION_ERROR;
1093 entry->empty_confirm = 1;
1094 }
1095 entry->needs_confirm = 1;
1096 entry->needs_mandatory = 0;
1097 entry->state = FEAT_STABLE;
1098 return 0;
1099
1100unknown_feature_or_value:
1101 if (!is_mandatory)
1102 return dccp_push_empty_confirm(fn, feat, local);
1103
1104not_valid_or_not_known:
1105 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1106 : DCCP_RESET_CODE_OPTION_ERROR;
1107}
1108
1109/**
Gerrit Renkerb1ad0042008-12-01 23:33:18 -08001110 * dccp_feat_confirm_recv - Process received Confirm options
1111 * @fn: feature-negotiation list to update
1112 * @is_mandatory: whether @opt was preceded by a Mandatory option
1113 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1114 * @feat: one of %dccp_feature_numbers
1115 * @val: NN value or SP value/preference list
1116 * @len: length of @val in bytes
1117 * @server: whether this node is server (1) or client (0)
1118 */
1119static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1120 u8 feat, u8 *val, u8 len, const bool server)
1121{
1122 u8 *plist, plen, type = dccp_feat_type(feat);
1123 const bool local = (opt == DCCPO_CONFIRM_R);
1124 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1125
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +00001126 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1127
Gerrit Renkerb1ad0042008-12-01 23:33:18 -08001128 if (entry == NULL) { /* nothing queued: ignore or handle error */
1129 if (is_mandatory && type == FEAT_UNKNOWN)
1130 return DCCP_RESET_CODE_MANDATORY_ERROR;
1131
1132 if (!local && type == FEAT_NN) /* 6.3.2 */
1133 goto confirmation_failed;
1134 return 0;
1135 }
1136
1137 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
1138 return 0;
1139
1140 if (len == 0) {
1141 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1142 goto confirmation_failed;
1143 /*
1144 * Empty Confirm during connection setup: this means reverting
1145 * to the `old' value, which in this case is the default. Since
1146 * we handle default values automatically when no other values
1147 * have been set, we revert to the old value by removing this
1148 * entry from the list.
1149 */
1150 dccp_feat_list_pop(entry);
1151 return 0;
1152 }
1153
1154 if (type == FEAT_NN) {
1155 if (len > sizeof(entry->val.nn))
1156 goto confirmation_failed;
1157
1158 if (entry->val.nn == dccp_decode_value_var(val, len))
1159 goto confirmation_succeeded;
1160
1161 DCCP_WARN("Bogus Confirm for non-existing value\n");
1162 goto confirmation_failed;
1163 }
1164
1165 /*
1166 * Parsing SP Confirms: the first element of @val is the preferred
1167 * SP value which the peer confirms, the remainder depends on @len.
1168 * Note that only the confirmed value need to be a valid SP value.
1169 */
1170 if (!dccp_feat_is_valid_sp_val(feat, *val))
1171 goto confirmation_failed;
1172
1173 if (len == 1) { /* peer didn't supply a preference list */
1174 plist = val;
1175 plen = len;
1176 } else { /* preferred value + preference list */
1177 plist = val + 1;
1178 plen = len - 1;
1179 }
1180
1181 /* Check whether the peer got the reconciliation right (6.6.8) */
1182 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1183 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1184 return DCCP_RESET_CODE_OPTION_ERROR;
1185 }
1186 entry->val.sp.vec[0] = *val;
1187
1188confirmation_succeeded:
1189 entry->state = FEAT_STABLE;
1190 return 0;
1191
1192confirmation_failed:
1193 DCCP_WARN("Confirmation failed\n");
1194 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1195 : DCCP_RESET_CODE_OPTION_ERROR;
1196}
1197
1198/**
Gerrit Renkere77b8362008-12-01 23:32:35 -08001199 * dccp_feat_parse_options - Process Feature-Negotiation Options
1200 * @sk: for general use and used by the client during connection setup
1201 * @dreq: used by the server during connection setup
1202 * @mandatory: whether @opt was preceded by a Mandatory option
1203 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1204 * @feat: one of %dccp_feature_numbers
1205 * @val: value contents of @opt
1206 * @len: length of @val in bytes
1207 * Returns 0 on success, a Reset code for ending the connection otherwise.
1208 */
1209int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1210 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1211{
1212 struct dccp_sock *dp = dccp_sk(sk);
1213 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1214 bool server = false;
1215
1216 switch (sk->sk_state) {
1217 /*
1218 * Negotiation during connection setup
1219 */
1220 case DCCP_LISTEN:
1221 server = true; /* fall through */
1222 case DCCP_REQUESTING:
1223 switch (opt) {
1224 case DCCPO_CHANGE_L:
1225 case DCCPO_CHANGE_R:
1226 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1227 val, len, server);
Gerrit Renkerb1ad0042008-12-01 23:33:18 -08001228 case DCCPO_CONFIRM_R:
1229 case DCCPO_CONFIRM_L:
1230 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1231 val, len, server);
Gerrit Renkere77b8362008-12-01 23:32:35 -08001232 }
1233 }
1234 return 0; /* ignore FN options in all other states */
1235}
1236
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001237/**
1238 * dccp_feat_init - Seed feature negotiation with host-specific defaults
1239 * This initialises global defaults, depending on the value of the sysctls.
1240 * These can later be overridden by registering changes via setsockopt calls.
1241 * The last link in the chain is finalise_settings, to make sure that between
1242 * here and the start of actual feature negotiation no inconsistencies enter.
1243 *
1244 * All features not appearing below use either defaults or are otherwise
1245 * later adjusted through dccp_feat_finalise_settings().
1246 */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001247int dccp_feat_init(struct sock *sk)
Andrea Bittauafe00252006-03-20 17:43:56 -08001248{
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001249 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1250 u8 on = 1, off = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -08001251 int rc;
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001252 struct {
1253 u8 *val;
1254 u8 len;
1255 } tx, rx;
Andrea Bittauafe00252006-03-20 17:43:56 -08001256
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001257 /* Non-negotiable (NN) features */
1258 rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
Gerrit Renker883ca832009-01-16 23:36:32 +00001259 sysctl_dccp_sequence_window);
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001260 if (rc)
1261 return rc;
Andrea Bittauafe00252006-03-20 17:43:56 -08001262
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001263 /* Server-priority (SP) features */
1264
1265 /* Advertise that short seqnos are not supported (7.6.1) */
1266 rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1267 if (rc)
1268 return rc;
1269
1270 /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1271 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1272 if (rc)
1273 return rc;
1274
1275 /*
1276 * We advertise the available list of CCIDs and reorder according to
1277 * preferences, to avoid failure resulting from negotiating different
1278 * singleton values (which always leads to failure).
1279 * These settings can still (later) be overridden via sockopts.
1280 */
1281 if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1282 ccid_get_builtin_ccids(&rx.val, &rx.len))
1283 return -ENOBUFS;
1284
Gerrit Renker883ca832009-01-16 23:36:32 +00001285 if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1286 !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001287 goto free_ccid_lists;
1288
1289 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1290 if (rc)
1291 goto free_ccid_lists;
1292
1293 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1294
1295free_ccid_lists:
1296 kfree(tx.val);
1297 kfree(rx.val);
Andrea Bittauafe00252006-03-20 17:43:56 -08001298 return rc;
1299}
1300
Gerrit Renker422d9cd2008-12-01 23:34:01 -08001301int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1302{
1303 struct dccp_sock *dp = dccp_sk(sk);
1304 struct dccp_feat_entry *cur, *next;
1305 int idx;
1306 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1307 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1308 };
1309
1310 list_for_each_entry(cur, fn_list, node) {
1311 /*
1312 * An empty Confirm means that either an unknown feature type
1313 * or an invalid value was present. In the first case there is
1314 * nothing to activate, in the other the default value is used.
1315 */
1316 if (cur->empty_confirm)
1317 continue;
1318
1319 idx = dccp_feat_index(cur->feat_num);
1320 if (idx < 0) {
1321 DCCP_BUG("Unknown feature %u", cur->feat_num);
1322 goto activation_failed;
1323 }
1324 if (cur->state != FEAT_STABLE) {
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +00001325 DCCP_CRIT("Negotiation of %s %s failed in state %s",
Gerrit Renker422d9cd2008-12-01 23:34:01 -08001326 cur->is_local ? "local" : "remote",
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +00001327 dccp_feat_fname(cur->feat_num),
1328 dccp_feat_sname[cur->state]);
Gerrit Renker422d9cd2008-12-01 23:34:01 -08001329 goto activation_failed;
1330 }
1331 fvals[idx][cur->is_local] = &cur->val;
1332 }
1333
1334 /*
1335 * Activate in decreasing order of index, so that the CCIDs are always
1336 * activated as the last feature. This avoids the case where a CCID
1337 * relies on the initialisation of one or more features that it depends
1338 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1339 */
1340 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1341 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1342 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1343 DCCP_CRIT("Could not activate %d", idx);
1344 goto activation_failed;
1345 }
1346
1347 /* Clean up Change options which have been confirmed already */
1348 list_for_each_entry_safe(cur, next, fn_list, node)
1349 if (!cur->needs_confirm)
1350 dccp_feat_list_pop(cur);
1351
1352 dccp_pr_debug("Activation OK\n");
1353 return 0;
1354
1355activation_failed:
1356 /*
1357 * We clean up everything that may have been allocated, since
1358 * it is difficult to track at which stage negotiation failed.
1359 * This is ok, since all allocation functions below are robust
1360 * against NULL arguments.
1361 */
1362 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1363 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1364 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1365 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1366 dp->dccps_hc_rx_ackvec = NULL;
1367 return -1;
1368}