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