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