blob: ad6f9e2cac1a23946f0e72e9a4405851d76bd39c [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 Renkerd6916f82011-07-24 20:22:29 -060015 * o Changing non-negotiable (NN) values is supported in state OPEN/PARTOPEN.
Gerrit Renker5cdae192007-12-13 12:41:46 -020016 * 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.
Andrea Bittauafe00252006-03-20 17:43:56 -080024 */
Andrea Bittauafe00252006-03-20 17:43:56 -080025#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080027#include "ccid.h"
Andrea Bittauafe00252006-03-20 17:43:56 -080028#include "feat.h"
29
Gerrit Renker883ca832009-01-16 23:36:32 +000030/* 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
Gerrit Renker422d9cd2008-12-01 23:34:01 -080035/*
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);
Gerrit Renkere5fd56c2009-01-04 21:43:23 -080044 struct ccid *new_ccid = ccid_new(ccid, sk, rx);
Gerrit Renker422d9cd2008-12-01 23:34:01 -080045
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{
Gerrit Renker792b4872009-01-16 23:36:31 +000061 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 }
Gerrit Renker422d9cd2008-12-01 23:34:01 -080072 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)
Gerrit Renker4098dce2008-12-08 01:18:37 -0800104 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800105 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
Gerrit Renker7d43d1a02008-11-04 23:43:47 -0800133static 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 */
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800138 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
Gerrit Renker7d43d1a02008-11-04 23:43:47 -0800139/*
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[] = {
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800158 { 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 },
Gerrit Renker7d43d1a02008-11-04 23:43:47 -0800168};
169#define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
170
Gerrit Renker61e64732008-11-04 23:54:04 -0800171/**
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
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800201static 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
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000213/*
214 * Debugging and verbose-printing section
215 */
216static const char *dccp_feat_fname(const u8 feat)
217{
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700218 static const char *const feature_names[] = {
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000219 [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
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700241static const char *const dccp_feat_sname[] = {
242 "DEFAULT", "INITIALISING", "CHANGING", "UNSTABLE", "STABLE",
243};
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000244
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
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800308static 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
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000340 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
Gerrit Renker422d9cd2008-12-01 23:34:01 -0800344 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
345}
346
Gerrit Renker44e6fd92011-07-24 20:27:59 -0600347/**
348 * dccp_feat_activate - Activate feature value on socket
349 * @sk: fully connected DCCP socket (after handshake is complete)
350 * @feat_num: feature to activate, one of %dccp_feature_numbers
351 * @local: whether local (1) or remote (0) @feat_num is meant
352 * @fval: the value (SP or NN) to activate, or NULL to use the default value
353 * For general use this function is preferable over __dccp_feat_activate().
354 */
355static int dccp_feat_activate(struct sock *sk, u8 feat_num, bool local,
356 dccp_feat_val const *fval)
357{
358 return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval);
359}
360
Gerrit Renkerb1ad0042008-12-01 23:33:18 -0800361/* Test for "Req'd" feature (RFC 4340, 6.4) */
362static inline int dccp_feat_must_be_understood(u8 feat_num)
363{
364 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
365 feat_num == DCCPF_SEQUENCE_WINDOW;
366}
367
Gerrit Renkerac757732008-11-04 23:55:49 -0800368/* copy constructor, fval must not already contain allocated memory */
369static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
370{
371 fval->sp.len = len;
372 if (fval->sp.len > 0) {
373 fval->sp.vec = kmemdup(val, len, gfp_any());
374 if (fval->sp.vec == NULL) {
375 fval->sp.len = 0;
376 return -ENOBUFS;
377 }
378 }
379 return 0;
380}
381
Gerrit Renker61e64732008-11-04 23:54:04 -0800382static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
383{
384 if (unlikely(val == NULL))
385 return;
386 if (dccp_feat_type(feat_num) == FEAT_SP)
387 kfree(val->sp.vec);
388 memset(val, 0, sizeof(*val));
389}
390
Gerrit Renkerac757732008-11-04 23:55:49 -0800391static struct dccp_feat_entry *
392 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
393{
394 struct dccp_feat_entry *new;
395 u8 type = dccp_feat_type(original->feat_num);
396
397 if (type == FEAT_UNKNOWN)
398 return NULL;
399
400 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
401 if (new == NULL)
402 return NULL;
403
404 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
405 original->val.sp.vec,
406 original->val.sp.len)) {
407 kfree(new);
408 return NULL;
409 }
410 return new;
411}
412
Gerrit Renker61e64732008-11-04 23:54:04 -0800413static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
414{
415 if (entry != NULL) {
416 dccp_feat_val_destructor(entry->feat_num, &entry->val);
417 kfree(entry);
418 }
419}
420
421/*
422 * List management functions
423 *
424 * Feature negotiation lists rely on and maintain the following invariants:
425 * - each feat_num in the list is known, i.e. we know its type and default value
426 * - each feat_num/is_local combination is unique (old entries are overwritten)
427 * - SP values are always freshly allocated
428 * - list is sorted in increasing order of feature number (faster lookup)
429 */
Gerrit Renker0c116832008-11-16 22:49:52 -0800430static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
431 u8 feat_num, bool is_local)
432{
433 struct dccp_feat_entry *entry;
434
Gerrit Renker3d3e35a2008-11-20 01:03:08 -0800435 list_for_each_entry(entry, fn_list, node) {
Gerrit Renker0c116832008-11-16 22:49:52 -0800436 if (entry->feat_num == feat_num && entry->is_local == is_local)
437 return entry;
438 else if (entry->feat_num > feat_num)
439 break;
Gerrit Renker3d3e35a2008-11-20 01:03:08 -0800440 }
Gerrit Renker0c116832008-11-16 22:49:52 -0800441 return NULL;
442}
Gerrit Renker61e64732008-11-04 23:54:04 -0800443
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800444/**
445 * dccp_feat_entry_new - Central list update routine (called by all others)
446 * @head: list to add to
447 * @feat: feature number
448 * @local: whether the local (1) or remote feature with number @feat is meant
449 * This is the only constructor and serves to ensure the above invariants.
450 */
451static struct dccp_feat_entry *
452 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
453{
454 struct dccp_feat_entry *entry;
455
456 list_for_each_entry(entry, head, node)
457 if (entry->feat_num == feat && entry->is_local == local) {
458 dccp_feat_val_destructor(entry->feat_num, &entry->val);
459 return entry;
460 } else if (entry->feat_num > feat) {
461 head = &entry->node;
462 break;
463 }
464
465 entry = kmalloc(sizeof(*entry), gfp_any());
466 if (entry != NULL) {
467 entry->feat_num = feat;
468 entry->is_local = local;
469 list_add_tail(&entry->node, head);
470 }
471 return entry;
472}
473
474/**
475 * dccp_feat_push_change - Add/overwrite a Change option in the list
476 * @fn_list: feature-negotiation list to update
477 * @feat: one of %dccp_feature_numbers
478 * @local: whether local (1) or remote (0) @feat_num is meant
479 * @needs_mandatory: whether to use Mandatory feature negotiation options
480 * @fval: pointer to NN/SP value to be inserted (will be copied)
481 */
482static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
483 u8 mandatory, dccp_feat_val *fval)
484{
485 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
486
487 if (new == NULL)
488 return -ENOMEM;
489
490 new->feat_num = feat;
491 new->is_local = local;
492 new->state = FEAT_INITIALISING;
493 new->needs_confirm = 0;
494 new->empty_confirm = 0;
495 new->val = *fval;
496 new->needs_mandatory = mandatory;
497
498 return 0;
499}
500
Gerrit Renkere77b8362008-12-01 23:32:35 -0800501/**
502 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
503 * @fn_list: feature-negotiation list to add to
504 * @feat: one of %dccp_feature_numbers
505 * @local: whether local (1) or remote (0) @feat_num is being confirmed
506 * @fval: pointer to NN/SP value to be inserted or NULL
507 * Returns 0 on success, a Reset code for further processing otherwise.
508 */
509static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
510 dccp_feat_val *fval)
511{
512 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
513
514 if (new == NULL)
515 return DCCP_RESET_CODE_TOO_BUSY;
516
517 new->feat_num = feat;
518 new->is_local = local;
519 new->state = FEAT_STABLE; /* transition in 6.6.2 */
520 new->needs_confirm = 1;
521 new->empty_confirm = (fval == NULL);
522 new->val.nn = 0; /* zeroes the whole structure */
523 if (!new->empty_confirm)
524 new->val = *fval;
525 new->needs_mandatory = 0;
526
527 return 0;
528}
529
530static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
531{
532 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
533}
534
Gerrit Renker61e64732008-11-04 23:54:04 -0800535static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
536{
537 list_del(&entry->node);
538 dccp_feat_entry_destructor(entry);
539}
540
541void dccp_feat_list_purge(struct list_head *fn_list)
542{
543 struct dccp_feat_entry *entry, *next;
544
545 list_for_each_entry_safe(entry, next, fn_list, node)
546 dccp_feat_entry_destructor(entry);
547 INIT_LIST_HEAD(fn_list);
548}
549EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
550
Gerrit Renkerac757732008-11-04 23:55:49 -0800551/* generate @to as full clone of @from - @to must not contain any nodes */
552int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
553{
554 struct dccp_feat_entry *entry, *new;
555
556 INIT_LIST_HEAD(to);
557 list_for_each_entry(entry, from, node) {
558 new = dccp_feat_clone_entry(entry);
559 if (new == NULL)
560 goto cloning_failed;
561 list_add_tail(&new->node, to);
562 }
563 return 0;
564
565cloning_failed:
566 dccp_feat_list_purge(to);
567 return -ENOMEM;
568}
569
Gerrit Renker0971d172008-12-01 23:27:31 -0800570/**
571 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
572 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
573 * incoming options are accepted as long as their values are valid.
574 */
575static u8 dccp_feat_valid_nn_length(u8 feat_num)
576{
577 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
578 return 2;
579 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
580 return 6;
581 return 0;
582}
583
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800584static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
585{
586 switch (feat_num) {
587 case DCCPF_ACK_RATIO:
588 return val <= DCCPF_ACK_RATIO_MAX;
589 case DCCPF_SEQUENCE_WINDOW:
590 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
591 }
592 return 0; /* feature unknown - so we can't tell */
593}
594
595/* check that SP values are within the ranges defined in RFC 4340 */
596static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
597{
598 switch (feat_num) {
599 case DCCPF_CCID:
600 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
601 /* Type-check Boolean feature values: */
602 case DCCPF_SHORT_SEQNOS:
603 case DCCPF_ECN_INCAPABLE:
604 case DCCPF_SEND_ACK_VECTOR:
605 case DCCPF_SEND_NDP_COUNT:
606 case DCCPF_DATA_CHECKSUM:
607 case DCCPF_SEND_LEV_RATE:
608 return val < 2;
609 case DCCPF_MIN_CSUM_COVER:
610 return val < 16;
611 }
612 return 0; /* feature unknown */
613}
614
615static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
616{
617 if (sp_list == NULL || sp_len < 1)
618 return 0;
619 while (sp_len--)
620 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
621 return 0;
622 return 1;
623}
624
625/**
Gerrit Renker0971d172008-12-01 23:27:31 -0800626 * dccp_feat_insert_opts - Generate FN options from current list state
627 * @skb: next sk_buff to be sent to the peer
628 * @dp: for client during handshake and general negotiation
629 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
630 */
631int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
632 struct sk_buff *skb)
633{
634 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
635 struct dccp_feat_entry *pos, *next;
636 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
637 bool rpt;
638
639 /* put entries into @skb in the order they appear in the list */
640 list_for_each_entry_safe_reverse(pos, next, fn, node) {
641 opt = dccp_feat_genopt(pos);
642 type = dccp_feat_type(pos->feat_num);
643 rpt = false;
644
645 if (pos->empty_confirm) {
646 len = 0;
647 ptr = NULL;
648 } else {
649 if (type == FEAT_SP) {
650 len = pos->val.sp.len;
651 ptr = pos->val.sp.vec;
652 rpt = pos->needs_confirm;
653 } else if (type == FEAT_NN) {
654 len = dccp_feat_valid_nn_length(pos->feat_num);
655 ptr = nn_in_nbo;
656 dccp_encode_value_var(pos->val.nn, ptr, len);
657 } else {
658 DCCP_BUG("unknown feature %u", pos->feat_num);
659 return -1;
660 }
661 }
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000662 dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
Gerrit Renker0971d172008-12-01 23:27:31 -0800663
664 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
665 return -1;
666 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
667 return -1;
668 /*
669 * Enter CHANGING after transmitting the Change option (6.6.2).
670 */
671 if (pos->state == FEAT_INITIALISING)
672 pos->state = FEAT_CHANGING;
673 }
674 return 0;
675}
676
677/**
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800678 * __feat_register_nn - Register new NN value on socket
679 * @fn: feature-negotiation list to register with
680 * @feat: an NN feature from %dccp_feature_numbers
681 * @mandatory: use Mandatory option if 1
682 * @nn_val: value to register (restricted to 4 bytes)
683 * Note that NN features are local by definition (RFC 4340, 6.3.2).
684 */
685static int __feat_register_nn(struct list_head *fn, u8 feat,
686 u8 mandatory, u64 nn_val)
687{
688 dccp_feat_val fval = { .nn = nn_val };
689
690 if (dccp_feat_type(feat) != FEAT_NN ||
691 !dccp_feat_is_valid_nn_val(feat, nn_val))
692 return -EINVAL;
693
694 /* Don't bother with default values, they will be activated anyway. */
695 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
696 return 0;
697
698 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
699}
700
701/**
702 * __feat_register_sp - Register new SP value/list on socket
703 * @fn: feature-negotiation list to register with
704 * @feat: an SP feature from %dccp_feature_numbers
705 * @is_local: whether the local (1) or the remote (0) @feat is meant
706 * @mandatory: use Mandatory option if 1
707 * @sp_val: SP value followed by optional preference list
708 * @sp_len: length of @sp_val in bytes
709 */
710static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
711 u8 mandatory, u8 const *sp_val, u8 sp_len)
712{
713 dccp_feat_val fval;
714
715 if (dccp_feat_type(feat) != FEAT_SP ||
716 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
717 return -EINVAL;
718
Gerrit Renkerd90ebcb2008-11-12 00:47:26 -0800719 /* Avoid negotiating alien CCIDs by only advertising supported ones */
720 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
721 return -EOPNOTSUPP;
722
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800723 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
724 return -ENOMEM;
725
726 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
727}
728
Gerrit Renker49aebc62008-11-16 22:51:23 -0800729/**
730 * dccp_feat_register_sp - Register requests to change SP feature values
731 * @sk: client or listening socket
732 * @feat: one of %dccp_feature_numbers
733 * @is_local: whether the local (1) or remote (0) @feat is meant
734 * @list: array of preferred values, in descending order of preference
735 * @len: length of @list in bytes
736 */
737int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
738 u8 const *list, u8 len)
739{ /* any changes must be registered before establishing the connection */
740 if (sk->sk_state != DCCP_CLOSED)
741 return -EISCONN;
742 if (dccp_feat_type(feat) != FEAT_SP)
Chris Wright19443172008-05-05 13:50:24 -0700743 return -EINVAL;
Gerrit Renker49aebc62008-11-16 22:51:23 -0800744 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
745 0, list, len);
Andrea Bittauafe00252006-03-20 17:43:56 -0800746}
747
Gerrit Renkerd6916f82011-07-24 20:22:29 -0600748/**
749 * dccp_feat_nn_get - Query current/pending value of NN feature
750 * @sk: DCCP socket of an established connection
751 * @feat: NN feature number from %dccp_feature_numbers
752 * For a known NN feature, returns value currently being negotiated, or
753 * current (confirmed) value if no negotiation is going on.
754 */
755u64 dccp_feat_nn_get(struct sock *sk, u8 feat)
756{
757 if (dccp_feat_type(feat) == FEAT_NN) {
758 struct dccp_sock *dp = dccp_sk(sk);
759 struct dccp_feat_entry *entry;
760
761 entry = dccp_feat_list_lookup(&dp->dccps_featneg, feat, 1);
762 if (entry != NULL)
763 return entry->val.nn;
764
765 switch (feat) {
766 case DCCPF_ACK_RATIO:
767 return dp->dccps_l_ack_ratio;
768 case DCCPF_SEQUENCE_WINDOW:
769 return dp->dccps_l_seq_win;
770 }
771 }
772 DCCP_BUG("attempt to look up unsupported feature %u", feat);
773 return 0;
774}
775EXPORT_SYMBOL_GPL(dccp_feat_nn_get);
776
777/**
778 * dccp_feat_signal_nn_change - Update NN values for an established connection
779 * @sk: DCCP socket of an established connection
780 * @feat: NN feature number from %dccp_feature_numbers
781 * @nn_val: the new value to use
782 * This function is used to communicate NN updates out-of-band.
783 */
784int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
785{
786 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
787 dccp_feat_val fval = { .nn = nn_val };
788 struct dccp_feat_entry *entry;
789
790 if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
791 return 0;
792
793 if (dccp_feat_type(feat) != FEAT_NN ||
794 !dccp_feat_is_valid_nn_val(feat, nn_val))
795 return -EINVAL;
796
797 if (nn_val == dccp_feat_nn_get(sk, feat))
798 return 0; /* already set or negotiation under way */
799
800 entry = dccp_feat_list_lookup(fn, feat, 1);
801 if (entry != NULL) {
802 dccp_pr_debug("Clobbering existing NN entry %llu -> %llu\n",
803 (unsigned long long)entry->val.nn,
804 (unsigned long long)nn_val);
805 dccp_feat_list_pop(entry);
806 }
807
808 inet_csk_schedule_ack(sk);
809 return dccp_feat_push_change(fn, feat, 1, 0, &fval);
810}
811EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
Andrea Bittauafe00252006-03-20 17:43:56 -0800812
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800813/*
814 * Tracking features whose value depend on the choice of CCID
815 *
816 * This is designed with an extension in mind so that a list walk could be done
817 * before activating any features. However, the existing framework was found to
818 * work satisfactorily up until now, the automatic verification is left open.
819 * When adding new CCIDs, add a corresponding dependency table here.
820 */
821static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
822{
823 static const struct ccid_dependency ccid2_dependencies[2][2] = {
824 /*
825 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
826 * feature and Send Ack Vector is an RX feature, `is_local'
827 * needs to be reversed.
828 */
829 { /* Dependencies of the receiver-side (remote) CCID2 */
830 {
831 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
832 .is_local = true,
833 .is_mandatory = true,
834 .val = 1
835 },
836 { 0, 0, 0, 0 }
837 },
838 { /* Dependencies of the sender-side (local) CCID2 */
839 {
840 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
841 .is_local = false,
842 .is_mandatory = true,
843 .val = 1
844 },
845 { 0, 0, 0, 0 }
846 }
847 };
848 static const struct ccid_dependency ccid3_dependencies[2][5] = {
849 { /*
850 * Dependencies of the receiver-side CCID3
851 */
852 { /* locally disable Ack Vectors */
853 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
854 .is_local = true,
855 .is_mandatory = false,
856 .val = 0
857 },
858 { /* see below why Send Loss Event Rate is on */
859 .dependent_feat = DCCPF_SEND_LEV_RATE,
860 .is_local = true,
861 .is_mandatory = true,
862 .val = 1
863 },
864 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
865 .dependent_feat = DCCPF_SEND_NDP_COUNT,
866 .is_local = false,
867 .is_mandatory = true,
868 .val = 1
869 },
870 { 0, 0, 0, 0 },
871 },
872 { /*
873 * CCID3 at the TX side: we request that the HC-receiver
874 * will not send Ack Vectors (they will be ignored, so
875 * Mandatory is not set); we enable Send Loss Event Rate
876 * (Mandatory since the implementation does not support
877 * the Loss Intervals option of RFC 4342, 8.6).
878 * The last two options are for peer's information only.
879 */
880 {
881 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
882 .is_local = false,
883 .is_mandatory = false,
884 .val = 0
885 },
886 {
887 .dependent_feat = DCCPF_SEND_LEV_RATE,
888 .is_local = false,
889 .is_mandatory = true,
890 .val = 1
891 },
892 { /* this CCID does not support Ack Ratio */
893 .dependent_feat = DCCPF_ACK_RATIO,
894 .is_local = true,
895 .is_mandatory = false,
896 .val = 0
897 },
898 { /* tell receiver we are sending NDP counts */
899 .dependent_feat = DCCPF_SEND_NDP_COUNT,
900 .is_local = true,
901 .is_mandatory = false,
902 .val = 1
903 },
904 { 0, 0, 0, 0 }
905 }
906 };
907 switch (ccid) {
908 case DCCPC_CCID2:
909 return ccid2_dependencies[is_local];
910 case DCCPC_CCID3:
911 return ccid3_dependencies[is_local];
912 default:
913 return NULL;
914 }
915}
916
917/**
918 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
919 * @fn: feature-negotiation list to update
920 * @id: CCID number to track
921 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
922 * This function needs to be called after registering all other features.
923 */
924static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
925{
926 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
927 int i, rc = (table == NULL);
928
929 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
930 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
931 rc = __feat_register_sp(fn, table[i].dependent_feat,
932 table[i].is_local,
933 table[i].is_mandatory,
934 &table[i].val, 1);
935 else
936 rc = __feat_register_nn(fn, table[i].dependent_feat,
937 table[i].is_mandatory,
938 table[i].val);
939 return rc;
940}
941
942/**
943 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
944 * @dp: client or listening socket (settings will be inherited)
945 * This is called after all registrations (socket initialisation, sysctls, and
946 * sockopt calls), and before sending the first packet containing Change options
947 * (ie. client-Request or server-Response), to ensure internal consistency.
948 */
949int dccp_feat_finalise_settings(struct dccp_sock *dp)
950{
951 struct list_head *fn = &dp->dccps_featneg;
952 struct dccp_feat_entry *entry;
953 int i = 2, ccids[2] = { -1, -1 };
954
955 /*
956 * Propagating CCIDs:
957 * 1) not useful to propagate CCID settings if this host advertises more
958 * than one CCID: the choice of CCID may still change - if this is
959 * the client, or if this is the server and the client sends
960 * singleton CCID values.
961 * 2) since is that propagate_ccid changes the list, we defer changing
962 * the sorted list until after the traversal.
963 */
964 list_for_each_entry(entry, fn, node)
965 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
966 ccids[entry->is_local] = entry->val.sp.vec[0];
967 while (i--)
968 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
969 return -1;
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +0000970 dccp_feat_print_fnlist(fn);
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800971 return 0;
972}
973
Gerrit Renker0c116832008-11-16 22:49:52 -0800974/**
975 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
976 * It is the server which resolves the dependencies once the CCID has been
977 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
978 */
979int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
980{
981 struct list_head *fn = &dreq->dreq_featneg;
982 struct dccp_feat_entry *entry;
983 u8 is_local, ccid;
984
985 for (is_local = 0; is_local <= 1; is_local++) {
986 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
987
988 if (entry != NULL && !entry->empty_confirm)
989 ccid = entry->val.sp.vec[0];
990 else
991 ccid = dccp_feat_default_value(DCCPF_CCID);
992
993 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
994 return -1;
995 }
996 return 0;
997}
998
Gerrit Renker75757a72008-12-01 23:31:04 -0800999/* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
1000static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
1001{
1002 u8 c, s;
1003
1004 for (s = 0; s < slen; s++)
1005 for (c = 0; c < clen; c++)
1006 if (servlist[s] == clilist[c])
1007 return servlist[s];
1008 return -1;
1009}
1010
1011/**
1012 * dccp_feat_prefer - Move preferred entry to the start of array
1013 * Reorder the @array_len elements in @array so that @preferred_value comes
1014 * first. Returns >0 to indicate that @preferred_value does occur in @array.
1015 */
1016static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
1017{
1018 u8 i, does_occur = 0;
1019
1020 if (array != NULL) {
1021 for (i = 0; i < array_len; i++)
1022 if (array[i] == preferred_value) {
1023 array[i] = array[0];
1024 does_occur++;
1025 }
1026 if (does_occur)
1027 array[0] = preferred_value;
1028 }
1029 return does_occur;
1030}
1031
1032/**
1033 * dccp_feat_reconcile - Reconcile SP preference lists
1034 * @fval: SP list to reconcile into
1035 * @arr: received SP preference list
1036 * @len: length of @arr in bytes
1037 * @is_server: whether this side is the server (and @fv is the server's list)
1038 * @reorder: whether to reorder the list in @fv after reconciling with @arr
1039 * When successful, > 0 is returned and the reconciled list is in @fval.
1040 * A value of 0 means that negotiation failed (no shared entry).
1041 */
1042static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
1043 bool is_server, bool reorder)
1044{
1045 int rc;
1046
1047 if (!fv->sp.vec || !arr) {
1048 DCCP_CRIT("NULL feature value or array");
1049 return 0;
1050 }
1051
1052 if (is_server)
1053 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
1054 else
1055 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
1056
1057 if (!reorder)
1058 return rc;
1059 if (rc < 0)
1060 return 0;
1061
1062 /*
1063 * Reorder list: used for activating features and in dccp_insert_fn_opt.
1064 */
1065 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
1066}
1067
Gerrit Renkere77b8362008-12-01 23:32:35 -08001068/**
1069 * dccp_feat_change_recv - Process incoming ChangeL/R options
1070 * @fn: feature-negotiation list to update
1071 * @is_mandatory: whether the Change was preceded by a Mandatory option
1072 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1073 * @feat: one of %dccp_feature_numbers
1074 * @val: NN value or SP value/preference list
1075 * @len: length of @val in bytes
1076 * @server: whether this node is the server (1) or the client (0)
1077 */
1078static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1079 u8 feat, u8 *val, u8 len, const bool server)
1080{
1081 u8 defval, type = dccp_feat_type(feat);
1082 const bool local = (opt == DCCPO_CHANGE_R);
1083 struct dccp_feat_entry *entry;
1084 dccp_feat_val fval;
1085
1086 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
1087 goto unknown_feature_or_value;
1088
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +00001089 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1090
Gerrit Renkere77b8362008-12-01 23:32:35 -08001091 /*
1092 * Negotiation of NN features: Change R is invalid, so there is no
1093 * simultaneous negotiation; hence we do not look up in the list.
1094 */
1095 if (type == FEAT_NN) {
1096 if (local || len > sizeof(fval.nn))
1097 goto unknown_feature_or_value;
1098
1099 /* 6.3.2: "The feature remote MUST accept any valid value..." */
1100 fval.nn = dccp_decode_value_var(val, len);
1101 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1102 goto unknown_feature_or_value;
1103
1104 return dccp_feat_push_confirm(fn, feat, local, &fval);
1105 }
1106
1107 /*
1108 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
1109 */
1110 entry = dccp_feat_list_lookup(fn, feat, local);
1111 if (entry == NULL) {
1112 /*
1113 * No particular preferences have been registered. We deal with
1114 * this situation by assuming that all valid values are equally
1115 * acceptable, and apply the following checks:
1116 * - if the peer's list is a singleton, we accept a valid value;
1117 * - if we are the server, we first try to see if the peer (the
1118 * client) advertises the default value. If yes, we use it,
1119 * otherwise we accept the preferred value;
1120 * - else if we are the client, we use the first list element.
1121 */
1122 if (dccp_feat_clone_sp_val(&fval, val, 1))
1123 return DCCP_RESET_CODE_TOO_BUSY;
1124
1125 if (len > 1 && server) {
1126 defval = dccp_feat_default_value(feat);
1127 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1128 fval.sp.vec[0] = defval;
1129 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1130 kfree(fval.sp.vec);
1131 goto unknown_feature_or_value;
1132 }
1133
1134 /* Treat unsupported CCIDs like invalid values */
1135 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1136 kfree(fval.sp.vec);
1137 goto not_valid_or_not_known;
1138 }
1139
1140 return dccp_feat_push_confirm(fn, feat, local, &fval);
1141
1142 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
1143 return 0;
1144 }
1145
1146 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1147 entry->empty_confirm = 0;
1148 } else if (is_mandatory) {
1149 return DCCP_RESET_CODE_MANDATORY_ERROR;
1150 } else if (entry->state == FEAT_INITIALISING) {
1151 /*
1152 * Failed simultaneous negotiation (server only): try to `save'
1153 * the connection by checking whether entry contains the default
1154 * value for @feat. If yes, send an empty Confirm to signal that
1155 * the received Change was not understood - which implies using
1156 * the default value.
1157 * If this also fails, we use Reset as the last resort.
1158 */
1159 WARN_ON(!server);
1160 defval = dccp_feat_default_value(feat);
1161 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1162 return DCCP_RESET_CODE_OPTION_ERROR;
1163 entry->empty_confirm = 1;
1164 }
1165 entry->needs_confirm = 1;
1166 entry->needs_mandatory = 0;
1167 entry->state = FEAT_STABLE;
1168 return 0;
1169
1170unknown_feature_or_value:
1171 if (!is_mandatory)
1172 return dccp_push_empty_confirm(fn, feat, local);
1173
1174not_valid_or_not_known:
1175 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1176 : DCCP_RESET_CODE_OPTION_ERROR;
1177}
1178
1179/**
Gerrit Renkerb1ad0042008-12-01 23:33:18 -08001180 * dccp_feat_confirm_recv - Process received Confirm options
1181 * @fn: feature-negotiation list to update
1182 * @is_mandatory: whether @opt was preceded by a Mandatory option
1183 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1184 * @feat: one of %dccp_feature_numbers
1185 * @val: NN value or SP value/preference list
1186 * @len: length of @val in bytes
1187 * @server: whether this node is server (1) or client (0)
1188 */
1189static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1190 u8 feat, u8 *val, u8 len, const bool server)
1191{
1192 u8 *plist, plen, type = dccp_feat_type(feat);
1193 const bool local = (opt == DCCPO_CONFIRM_R);
1194 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1195
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +00001196 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1197
Gerrit Renkerb1ad0042008-12-01 23:33:18 -08001198 if (entry == NULL) { /* nothing queued: ignore or handle error */
1199 if (is_mandatory && type == FEAT_UNKNOWN)
1200 return DCCP_RESET_CODE_MANDATORY_ERROR;
1201
1202 if (!local && type == FEAT_NN) /* 6.3.2 */
1203 goto confirmation_failed;
1204 return 0;
1205 }
1206
1207 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
1208 return 0;
1209
1210 if (len == 0) {
1211 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1212 goto confirmation_failed;
1213 /*
1214 * Empty Confirm during connection setup: this means reverting
1215 * to the `old' value, which in this case is the default. Since
1216 * we handle default values automatically when no other values
1217 * have been set, we revert to the old value by removing this
1218 * entry from the list.
1219 */
1220 dccp_feat_list_pop(entry);
1221 return 0;
1222 }
1223
1224 if (type == FEAT_NN) {
1225 if (len > sizeof(entry->val.nn))
1226 goto confirmation_failed;
1227
1228 if (entry->val.nn == dccp_decode_value_var(val, len))
1229 goto confirmation_succeeded;
1230
1231 DCCP_WARN("Bogus Confirm for non-existing value\n");
1232 goto confirmation_failed;
1233 }
1234
1235 /*
1236 * Parsing SP Confirms: the first element of @val is the preferred
1237 * SP value which the peer confirms, the remainder depends on @len.
1238 * Note that only the confirmed value need to be a valid SP value.
1239 */
1240 if (!dccp_feat_is_valid_sp_val(feat, *val))
1241 goto confirmation_failed;
1242
1243 if (len == 1) { /* peer didn't supply a preference list */
1244 plist = val;
1245 plen = len;
1246 } else { /* preferred value + preference list */
1247 plist = val + 1;
1248 plen = len - 1;
1249 }
1250
1251 /* Check whether the peer got the reconciliation right (6.6.8) */
1252 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1253 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1254 return DCCP_RESET_CODE_OPTION_ERROR;
1255 }
1256 entry->val.sp.vec[0] = *val;
1257
1258confirmation_succeeded:
1259 entry->state = FEAT_STABLE;
1260 return 0;
1261
1262confirmation_failed:
1263 DCCP_WARN("Confirmation failed\n");
1264 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1265 : DCCP_RESET_CODE_OPTION_ERROR;
1266}
1267
1268/**
Gerrit Renker44e6fd92011-07-24 20:27:59 -06001269 * dccp_feat_handle_nn_established - Fast-path reception of NN options
1270 * @sk: socket of an established DCCP connection
1271 * @mandatory: whether @opt was preceded by a Mandatory option
1272 * @opt: %DCCPO_CHANGE_L | %DCCPO_CONFIRM_R (NN only)
1273 * @feat: NN number, one of %dccp_feature_numbers
1274 * @val: NN value
1275 * @len: length of @val in bytes
1276 * This function combines the functionality of change_recv/confirm_recv, with
1277 * the following differences (reset codes are the same):
1278 * - cleanup after receiving the Confirm;
1279 * - values are directly activated after successful parsing;
1280 * - deliberately restricted to NN features.
1281 * The restriction to NN features is essential since SP features can have non-
1282 * predictable outcomes (depending on the remote configuration), and are inter-
1283 * dependent (CCIDs for instance cause further dependencies).
1284 */
1285static u8 dccp_feat_handle_nn_established(struct sock *sk, u8 mandatory, u8 opt,
1286 u8 feat, u8 *val, u8 len)
1287{
1288 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1289 const bool local = (opt == DCCPO_CONFIRM_R);
1290 struct dccp_feat_entry *entry;
1291 u8 type = dccp_feat_type(feat);
1292 dccp_feat_val fval;
1293
1294 dccp_feat_print_opt(opt, feat, val, len, mandatory);
1295
1296 /* Ignore non-mandatory unknown and non-NN features */
1297 if (type == FEAT_UNKNOWN) {
1298 if (local && !mandatory)
1299 return 0;
1300 goto fast_path_unknown;
1301 } else if (type != FEAT_NN) {
1302 return 0;
1303 }
1304
1305 /*
1306 * We don't accept empty Confirms, since in fast-path feature
1307 * negotiation the values are enabled immediately after sending
1308 * the Change option.
1309 * Empty Changes on the other hand are invalid (RFC 4340, 6.1).
1310 */
1311 if (len == 0 || len > sizeof(fval.nn))
1312 goto fast_path_unknown;
1313
1314 if (opt == DCCPO_CHANGE_L) {
1315 fval.nn = dccp_decode_value_var(val, len);
1316 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1317 goto fast_path_unknown;
1318
1319 if (dccp_feat_push_confirm(fn, feat, local, &fval) ||
1320 dccp_feat_activate(sk, feat, local, &fval))
1321 return DCCP_RESET_CODE_TOO_BUSY;
1322
1323 /* set the `Ack Pending' flag to piggyback a Confirm */
1324 inet_csk_schedule_ack(sk);
1325
1326 } else if (opt == DCCPO_CONFIRM_R) {
1327 entry = dccp_feat_list_lookup(fn, feat, local);
1328 if (entry == NULL || entry->state != FEAT_CHANGING)
1329 return 0;
1330
1331 fval.nn = dccp_decode_value_var(val, len);
1332 /*
1333 * Just ignore a value that doesn't match our current value.
1334 * If the option changes twice within two RTTs, then at least
1335 * one CONFIRM will be received for the old value after a
1336 * new CHANGE was sent.
1337 */
1338 if (fval.nn != entry->val.nn)
1339 return 0;
1340
1341 /* Only activate after receiving the Confirm option (6.6.1). */
1342 dccp_feat_activate(sk, feat, local, &fval);
1343
1344 /* It has been confirmed - so remove the entry */
1345 dccp_feat_list_pop(entry);
1346
1347 } else {
1348 DCCP_WARN("Received illegal option %u\n", opt);
1349 goto fast_path_failed;
1350 }
1351 return 0;
1352
1353fast_path_unknown:
1354 if (!mandatory)
1355 return dccp_push_empty_confirm(fn, feat, local);
1356
1357fast_path_failed:
1358 return mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1359 : DCCP_RESET_CODE_OPTION_ERROR;
1360}
1361
1362/**
Gerrit Renkere77b8362008-12-01 23:32:35 -08001363 * dccp_feat_parse_options - Process Feature-Negotiation Options
1364 * @sk: for general use and used by the client during connection setup
1365 * @dreq: used by the server during connection setup
1366 * @mandatory: whether @opt was preceded by a Mandatory option
1367 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1368 * @feat: one of %dccp_feature_numbers
1369 * @val: value contents of @opt
1370 * @len: length of @val in bytes
1371 * Returns 0 on success, a Reset code for ending the connection otherwise.
1372 */
1373int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1374 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1375{
1376 struct dccp_sock *dp = dccp_sk(sk);
1377 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1378 bool server = false;
1379
1380 switch (sk->sk_state) {
1381 /*
1382 * Negotiation during connection setup
1383 */
1384 case DCCP_LISTEN:
1385 server = true; /* fall through */
1386 case DCCP_REQUESTING:
1387 switch (opt) {
1388 case DCCPO_CHANGE_L:
1389 case DCCPO_CHANGE_R:
1390 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1391 val, len, server);
Gerrit Renkerb1ad0042008-12-01 23:33:18 -08001392 case DCCPO_CONFIRM_R:
1393 case DCCPO_CONFIRM_L:
1394 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1395 val, len, server);
Gerrit Renkere77b8362008-12-01 23:32:35 -08001396 }
Gerrit Renker44e6fd92011-07-24 20:27:59 -06001397 break;
1398 /*
1399 * Support for exchanging NN options on an established connection.
1400 */
1401 case DCCP_OPEN:
1402 case DCCP_PARTOPEN:
1403 return dccp_feat_handle_nn_established(sk, mandatory, opt, feat,
1404 val, len);
Gerrit Renkere77b8362008-12-01 23:32:35 -08001405 }
1406 return 0; /* ignore FN options in all other states */
1407}
1408
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001409/**
1410 * dccp_feat_init - Seed feature negotiation with host-specific defaults
1411 * This initialises global defaults, depending on the value of the sysctls.
1412 * These can later be overridden by registering changes via setsockopt calls.
1413 * The last link in the chain is finalise_settings, to make sure that between
1414 * here and the start of actual feature negotiation no inconsistencies enter.
1415 *
1416 * All features not appearing below use either defaults or are otherwise
1417 * later adjusted through dccp_feat_finalise_settings().
1418 */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001419int dccp_feat_init(struct sock *sk)
Andrea Bittauafe00252006-03-20 17:43:56 -08001420{
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001421 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1422 u8 on = 1, off = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -08001423 int rc;
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001424 struct {
1425 u8 *val;
1426 u8 len;
1427 } tx, rx;
Andrea Bittauafe00252006-03-20 17:43:56 -08001428
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001429 /* Non-negotiable (NN) features */
1430 rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
Gerrit Renker883ca832009-01-16 23:36:32 +00001431 sysctl_dccp_sequence_window);
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001432 if (rc)
1433 return rc;
Andrea Bittauafe00252006-03-20 17:43:56 -08001434
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001435 /* Server-priority (SP) features */
1436
1437 /* Advertise that short seqnos are not supported (7.6.1) */
1438 rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1439 if (rc)
1440 return rc;
1441
1442 /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1443 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1444 if (rc)
1445 return rc;
1446
1447 /*
1448 * We advertise the available list of CCIDs and reorder according to
1449 * preferences, to avoid failure resulting from negotiating different
1450 * singleton values (which always leads to failure).
1451 * These settings can still (later) be overridden via sockopts.
1452 */
1453 if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1454 ccid_get_builtin_ccids(&rx.val, &rx.len))
1455 return -ENOBUFS;
1456
Gerrit Renker883ca832009-01-16 23:36:32 +00001457 if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1458 !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
Gerrit Renkerf90f92e2009-01-16 23:36:30 +00001459 goto free_ccid_lists;
1460
1461 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1462 if (rc)
1463 goto free_ccid_lists;
1464
1465 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1466
1467free_ccid_lists:
1468 kfree(tx.val);
1469 kfree(rx.val);
Andrea Bittauafe00252006-03-20 17:43:56 -08001470 return rc;
1471}
1472
Gerrit Renker422d9cd2008-12-01 23:34:01 -08001473int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1474{
1475 struct dccp_sock *dp = dccp_sk(sk);
1476 struct dccp_feat_entry *cur, *next;
1477 int idx;
1478 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1479 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1480 };
1481
1482 list_for_each_entry(cur, fn_list, node) {
1483 /*
1484 * An empty Confirm means that either an unknown feature type
1485 * or an invalid value was present. In the first case there is
1486 * nothing to activate, in the other the default value is used.
1487 */
1488 if (cur->empty_confirm)
1489 continue;
1490
1491 idx = dccp_feat_index(cur->feat_num);
1492 if (idx < 0) {
1493 DCCP_BUG("Unknown feature %u", cur->feat_num);
1494 goto activation_failed;
1495 }
1496 if (cur->state != FEAT_STABLE) {
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +00001497 DCCP_CRIT("Negotiation of %s %s failed in state %s",
Gerrit Renker422d9cd2008-12-01 23:34:01 -08001498 cur->is_local ? "local" : "remote",
Gerrit Renkerf3f3abb2009-01-16 23:36:33 +00001499 dccp_feat_fname(cur->feat_num),
1500 dccp_feat_sname[cur->state]);
Gerrit Renker422d9cd2008-12-01 23:34:01 -08001501 goto activation_failed;
1502 }
1503 fvals[idx][cur->is_local] = &cur->val;
1504 }
1505
1506 /*
1507 * Activate in decreasing order of index, so that the CCIDs are always
1508 * activated as the last feature. This avoids the case where a CCID
1509 * relies on the initialisation of one or more features that it depends
1510 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1511 */
1512 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1513 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1514 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1515 DCCP_CRIT("Could not activate %d", idx);
1516 goto activation_failed;
1517 }
1518
1519 /* Clean up Change options which have been confirmed already */
1520 list_for_each_entry_safe(cur, next, fn_list, node)
1521 if (!cur->needs_confirm)
1522 dccp_feat_list_pop(cur);
1523
1524 dccp_pr_debug("Activation OK\n");
1525 return 0;
1526
1527activation_failed:
1528 /*
1529 * We clean up everything that may have been allocated, since
1530 * it is difficult to track at which stage negotiation failed.
1531 * This is ok, since all allocation functions below are robust
1532 * against NULL arguments.
1533 */
1534 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1535 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1536 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1537 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1538 dp->dccps_hc_rx_ackvec = NULL;
1539 return -1;
1540}