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