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