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 | * ----------- | |
9 | * o All currently known SP features have 1-byte quantities. If in the future | |
10 | * extensions of RFCs 4340..42 define features with item lengths larger than | |
11 | * one byte, a feature-specific extension of the code will be required. | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or | |
14 | * modify it under the terms of the GNU General Public License | |
15 | * as published by the Free Software Foundation; either version | |
16 | * 2 of the License, or (at your option) any later version. | |
afe00251 AB |
17 | */ |
18 | ||
afe00251 AB |
19 | #include <linux/module.h> |
20 | ||
6ffd30fb | 21 | #include "ccid.h" |
afe00251 AB |
22 | #include "feat.h" |
23 | ||
24 | #define DCCP_FEAT_SP_NOAGREE (-123) | |
25 | ||
b4eec206 GR |
26 | static const struct { |
27 | u8 feat_num; /* DCCPF_xxx */ | |
28 | enum dccp_feat_type rxtx; /* RX or TX */ | |
29 | enum dccp_feat_type reconciliation; /* SP or NN */ | |
30 | u8 default_value; /* as in 6.4 */ | |
31 | /* | |
32 | * Lookup table for location and type of features (from RFC 4340/4342) | |
33 | * +--------------------------+----+-----+----+----+---------+-----------+ | |
34 | * | Feature | Location | Reconc. | Initial | Section | | |
35 | * | | RX | TX | SP | NN | Value | Reference | | |
36 | * +--------------------------+----+-----+----+----+---------+-----------+ | |
37 | * | DCCPF_CCID | | X | X | | 2 | 10 | | |
38 | * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 | | |
39 | * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 | | |
40 | * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 | | |
41 | * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 | | |
42 | * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 | | |
43 | * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 | | |
44 | * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 | | |
45 | * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 | | |
46 | * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 | | |
47 | * +--------------------------+----+-----+----+----+---------+-----------+ | |
48 | */ | |
49 | } dccp_feat_table[] = { | |
50 | { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2 }, | |
51 | { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0 }, | |
52 | { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100 }, | |
53 | { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0 }, | |
54 | { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2 }, | |
55 | { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0 }, | |
56 | { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0 }, | |
57 | { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0 }, | |
58 | { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0 }, | |
59 | { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0 }, | |
60 | }; | |
61 | #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table) | |
62 | ||
63 | /** | |
64 | * dccp_feat_index - Hash function to map feature number into array position | |
65 | * Returns consecutive array index or -1 if the feature is not understood. | |
66 | */ | |
67 | static int dccp_feat_index(u8 feat_num) | |
68 | { | |
69 | /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */ | |
70 | if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM) | |
71 | return feat_num - 1; | |
72 | ||
73 | /* | |
74 | * Other features: add cases for new feature types here after adding | |
75 | * them to the above table. | |
76 | */ | |
77 | switch (feat_num) { | |
78 | case DCCPF_SEND_LEV_RATE: | |
79 | return DCCP_FEAT_SUPPORTED_MAX - 1; | |
80 | } | |
81 | return -1; | |
82 | } | |
83 | ||
84 | static u8 dccp_feat_type(u8 feat_num) | |
85 | { | |
86 | int idx = dccp_feat_index(feat_num); | |
87 | ||
88 | if (idx < 0) | |
89 | return FEAT_UNKNOWN; | |
90 | return dccp_feat_table[idx].reconciliation; | |
91 | } | |
92 | ||
93 | static int dccp_feat_default_value(u8 feat_num) | |
94 | { | |
95 | int idx = dccp_feat_index(feat_num); | |
96 | ||
97 | return idx < 0 ? : dccp_feat_table[idx].default_value; | |
98 | } | |
99 | ||
5c7c9451 GR |
100 | /* copy constructor, fval must not already contain allocated memory */ |
101 | static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len) | |
102 | { | |
103 | fval->sp.len = len; | |
104 | if (fval->sp.len > 0) { | |
105 | fval->sp.vec = kmemdup(val, len, gfp_any()); | |
106 | if (fval->sp.vec == NULL) { | |
107 | fval->sp.len = 0; | |
108 | return -ENOBUFS; | |
109 | } | |
110 | } | |
111 | return 0; | |
112 | } | |
113 | ||
b4eec206 GR |
114 | static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val) |
115 | { | |
116 | if (unlikely(val == NULL)) | |
117 | return; | |
118 | if (dccp_feat_type(feat_num) == FEAT_SP) | |
119 | kfree(val->sp.vec); | |
120 | memset(val, 0, sizeof(*val)); | |
121 | } | |
122 | ||
123 | static struct dccp_feat_entry * | |
124 | dccp_feat_clone_entry(struct dccp_feat_entry const *original) | |
125 | { | |
126 | struct dccp_feat_entry *new; | |
127 | u8 type = dccp_feat_type(original->feat_num); | |
128 | ||
129 | if (type == FEAT_UNKNOWN) | |
130 | return NULL; | |
131 | ||
132 | new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any()); | |
133 | if (new == NULL) | |
134 | return NULL; | |
135 | ||
136 | if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val, | |
137 | original->val.sp.vec, | |
138 | original->val.sp.len)) { | |
139 | kfree(new); | |
140 | return NULL; | |
141 | } | |
142 | return new; | |
143 | } | |
144 | ||
145 | static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry) | |
146 | { | |
147 | if (entry != NULL) { | |
148 | dccp_feat_val_destructor(entry->feat_num, &entry->val); | |
149 | kfree(entry); | |
150 | } | |
151 | } | |
152 | ||
3001fc05 GR |
153 | /* |
154 | * List management functions | |
155 | * | |
156 | * Feature negotiation lists rely on and maintain the following invariants: | |
157 | * - each feat_num in the list is known, i.e. we know its type and default value | |
158 | * - each feat_num/is_local combination is unique (old entries are overwritten) | |
159 | * - SP values are always freshly allocated | |
160 | * - list is sorted in increasing order of feature number (faster lookup) | |
161 | */ | |
162 | static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list, | |
163 | u8 feat_num, bool is_local) | |
164 | { | |
165 | struct dccp_feat_entry *entry; | |
166 | ||
167 | list_for_each_entry(entry, fn_list, node) | |
168 | if (entry->feat_num == feat_num && entry->is_local == is_local) | |
169 | return entry; | |
170 | else if (entry->feat_num > feat_num) | |
171 | break; | |
172 | return NULL; | |
173 | } | |
174 | ||
175 | /** | |
176 | * dccp_feat_entry_new - Central list update routine (called by all others) | |
177 | * @head: list to add to | |
178 | * @feat: feature number | |
179 | * @local: whether the local (1) or remote feature with number @feat is meant | |
180 | * This is the only constructor and serves to ensure the above invariants. | |
181 | */ | |
182 | static struct dccp_feat_entry * | |
183 | dccp_feat_entry_new(struct list_head *head, u8 feat, bool local) | |
184 | { | |
185 | struct dccp_feat_entry *entry; | |
186 | ||
187 | list_for_each_entry(entry, head, node) | |
188 | if (entry->feat_num == feat && entry->is_local == local) { | |
189 | dccp_feat_val_destructor(entry->feat_num, &entry->val); | |
190 | return entry; | |
191 | } else if (entry->feat_num > feat) { | |
192 | head = &entry->node; | |
193 | break; | |
194 | } | |
195 | ||
196 | entry = kmalloc(sizeof(*entry), gfp_any()); | |
197 | if (entry != NULL) { | |
198 | entry->feat_num = feat; | |
199 | entry->is_local = local; | |
200 | list_add_tail(&entry->node, head); | |
201 | } | |
202 | return entry; | |
203 | } | |
204 | ||
205 | /** | |
206 | * dccp_feat_push_change - Add/overwrite a Change option in the list | |
207 | * @fn_list: feature-negotiation list to update | |
208 | * @feat: one of %dccp_feature_numbers | |
209 | * @local: whether local (1) or remote (0) @feat_num is meant | |
210 | * @needs_mandatory: whether to use Mandatory feature negotiation options | |
211 | * @fval: pointer to NN/SP value to be inserted (will be copied) | |
212 | */ | |
213 | static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local, | |
214 | u8 mandatory, dccp_feat_val *fval) | |
215 | { | |
216 | struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local); | |
217 | ||
218 | if (new == NULL) | |
219 | return -ENOMEM; | |
220 | ||
221 | new->feat_num = feat; | |
222 | new->is_local = local; | |
223 | new->state = FEAT_INITIALISING; | |
224 | new->needs_confirm = 0; | |
225 | new->empty_confirm = 0; | |
226 | new->val = *fval; | |
227 | new->needs_mandatory = mandatory; | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
232 | /** | |
233 | * dccp_feat_push_confirm - Add a Confirm entry to the FN list | |
234 | * @fn_list: feature-negotiation list to add to | |
235 | * @feat: one of %dccp_feature_numbers | |
236 | * @local: whether local (1) or remote (0) @feat_num is being confirmed | |
237 | * @fval: pointer to NN/SP value to be inserted or NULL | |
238 | * Returns 0 on success, a Reset code for further processing otherwise. | |
239 | */ | |
240 | static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local, | |
241 | dccp_feat_val *fval) | |
242 | { | |
243 | struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local); | |
244 | ||
245 | if (new == NULL) | |
246 | return DCCP_RESET_CODE_TOO_BUSY; | |
247 | ||
248 | new->feat_num = feat; | |
249 | new->is_local = local; | |
250 | new->state = FEAT_STABLE; /* transition in 6.6.2 */ | |
251 | new->needs_confirm = 1; | |
252 | new->empty_confirm = (fval == NULL); | |
253 | new->val.nn = 0; /* zeroes the whole structure */ | |
254 | if (!new->empty_confirm) | |
255 | new->val = *fval; | |
256 | new->needs_mandatory = 0; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local) | |
262 | { | |
263 | return dccp_feat_push_confirm(fn_list, feat, local, NULL); | |
264 | } | |
265 | ||
266 | static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry) | |
267 | { | |
268 | list_del(&entry->node); | |
269 | dccp_feat_entry_destructor(entry); | |
270 | } | |
271 | ||
272 | void dccp_feat_list_purge(struct list_head *fn_list) | |
273 | { | |
274 | struct dccp_feat_entry *entry, *next; | |
275 | ||
276 | list_for_each_entry_safe(entry, next, fn_list, node) | |
277 | dccp_feat_entry_destructor(entry); | |
278 | INIT_LIST_HEAD(fn_list); | |
279 | } | |
280 | EXPORT_SYMBOL_GPL(dccp_feat_list_purge); | |
281 | ||
828755ce GR |
282 | /* generate @to as full clone of @from - @to must not contain any nodes */ |
283 | int dccp_feat_clone_list(struct list_head const *from, struct list_head *to) | |
284 | { | |
285 | struct dccp_feat_entry *entry, *new; | |
286 | ||
287 | INIT_LIST_HEAD(to); | |
288 | list_for_each_entry(entry, from, node) { | |
289 | new = dccp_feat_clone_entry(entry); | |
290 | if (new == NULL) | |
291 | goto cloning_failed; | |
292 | list_add_tail(&new->node, to); | |
293 | } | |
294 | return 0; | |
295 | ||
296 | cloning_failed: | |
297 | dccp_feat_list_purge(to); | |
298 | return -ENOMEM; | |
299 | } | |
300 | ||
8ca0d17b ACM |
301 | int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature, |
302 | u8 *val, u8 len, gfp_t gfp) | |
afe00251 | 303 | { |
afe00251 AB |
304 | struct dccp_opt_pend *opt; |
305 | ||
c02fdc0e | 306 | dccp_feat_debug(type, feature, *val); |
afe00251 | 307 | |
dd6303df | 308 | if (len > 3) { |
59348b19 | 309 | DCCP_WARN("invalid length %d\n", len); |
19443178 | 310 | return -EINVAL; |
c02fdc0e GR |
311 | } |
312 | /* XXX add further sanity checks */ | |
6ffd30fb | 313 | |
afe00251 | 314 | /* check if that feature is already being negotiated */ |
a4bf3902 | 315 | list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { |
afe00251 AB |
316 | /* ok we found a negotiation for this option already */ |
317 | if (opt->dccpop_feat == feature && opt->dccpop_type == type) { | |
318 | dccp_pr_debug("Replacing old\n"); | |
319 | /* replace */ | |
320 | BUG_ON(opt->dccpop_val == NULL); | |
321 | kfree(opt->dccpop_val); | |
322 | opt->dccpop_val = val; | |
323 | opt->dccpop_len = len; | |
324 | opt->dccpop_conf = 0; | |
325 | return 0; | |
326 | } | |
327 | } | |
328 | ||
329 | /* negotiation for a new feature */ | |
330 | opt = kmalloc(sizeof(*opt), gfp); | |
331 | if (opt == NULL) | |
332 | return -ENOMEM; | |
333 | ||
334 | opt->dccpop_type = type; | |
335 | opt->dccpop_feat = feature; | |
336 | opt->dccpop_len = len; | |
337 | opt->dccpop_val = val; | |
338 | opt->dccpop_conf = 0; | |
339 | opt->dccpop_sc = NULL; | |
340 | ||
341 | BUG_ON(opt->dccpop_val == NULL); | |
342 | ||
a4bf3902 | 343 | list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending); |
afe00251 AB |
344 | return 0; |
345 | } | |
346 | ||
347 | EXPORT_SYMBOL_GPL(dccp_feat_change); | |
348 | ||
6ffd30fb AB |
349 | static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr) |
350 | { | |
351 | struct dccp_sock *dp = dccp_sk(sk); | |
a4bf3902 | 352 | struct dccp_minisock *dmsk = dccp_msk(sk); |
6ffd30fb AB |
353 | /* figure out if we are changing our CCID or the peer's */ |
354 | const int rx = type == DCCPO_CHANGE_R; | |
a4bf3902 | 355 | const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid; |
6ffd30fb AB |
356 | struct ccid *new_ccid; |
357 | ||
358 | /* Check if nothing is being changed. */ | |
359 | if (ccid_nr == new_ccid_nr) | |
360 | return 0; | |
361 | ||
362 | new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC); | |
363 | if (new_ccid == NULL) | |
364 | return -ENOMEM; | |
365 | ||
366 | if (rx) { | |
367 | ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); | |
368 | dp->dccps_hc_rx_ccid = new_ccid; | |
a4bf3902 | 369 | dmsk->dccpms_rx_ccid = new_ccid_nr; |
6ffd30fb AB |
370 | } else { |
371 | ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); | |
372 | dp->dccps_hc_tx_ccid = new_ccid; | |
a4bf3902 | 373 | dmsk->dccpms_tx_ccid = new_ccid_nr; |
6ffd30fb AB |
374 | } |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
afe00251 AB |
379 | static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val) |
380 | { | |
c02fdc0e | 381 | dccp_feat_debug(type, feat, val); |
6ffd30fb AB |
382 | |
383 | switch (feat) { | |
384 | case DCCPF_CCID: | |
385 | return dccp_feat_update_ccid(sk, type, val); | |
386 | default: | |
c02fdc0e GR |
387 | dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n", |
388 | dccp_feat_typename(type), feat); | |
6ffd30fb AB |
389 | break; |
390 | } | |
afe00251 AB |
391 | return 0; |
392 | } | |
393 | ||
394 | static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt, | |
395 | u8 *rpref, u8 rlen) | |
396 | { | |
397 | struct dccp_sock *dp = dccp_sk(sk); | |
398 | u8 *spref, slen, *res = NULL; | |
399 | int i, j, rc, agree = 1; | |
400 | ||
401 | BUG_ON(rpref == NULL); | |
402 | ||
403 | /* check if we are the black sheep */ | |
404 | if (dp->dccps_role == DCCP_ROLE_CLIENT) { | |
405 | spref = rpref; | |
406 | slen = rlen; | |
407 | rpref = opt->dccpop_val; | |
408 | rlen = opt->dccpop_len; | |
409 | } else { | |
410 | spref = opt->dccpop_val; | |
411 | slen = opt->dccpop_len; | |
412 | } | |
413 | /* | |
414 | * Now we have server preference list in spref and client preference in | |
415 | * rpref | |
416 | */ | |
417 | BUG_ON(spref == NULL); | |
418 | BUG_ON(rpref == NULL); | |
419 | ||
420 | /* FIXME sanity check vals */ | |
421 | ||
422 | /* Are values in any order? XXX Lame "algorithm" here */ | |
afe00251 AB |
423 | for (i = 0; i < slen; i++) { |
424 | for (j = 0; j < rlen; j++) { | |
425 | if (spref[i] == rpref[j]) { | |
426 | res = &spref[i]; | |
427 | break; | |
428 | } | |
429 | } | |
430 | if (res) | |
431 | break; | |
432 | } | |
433 | ||
434 | /* we didn't agree on anything */ | |
435 | if (res == NULL) { | |
436 | /* confirm previous value */ | |
437 | switch (opt->dccpop_feat) { | |
438 | case DCCPF_CCID: | |
439 | /* XXX did i get this right? =P */ | |
440 | if (opt->dccpop_type == DCCPO_CHANGE_L) | |
a4bf3902 | 441 | res = &dccp_msk(sk)->dccpms_tx_ccid; |
afe00251 | 442 | else |
a4bf3902 | 443 | res = &dccp_msk(sk)->dccpms_rx_ccid; |
afe00251 AB |
444 | break; |
445 | ||
446 | default: | |
59348b19 GR |
447 | DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat); |
448 | /* XXX implement res */ | |
afe00251 AB |
449 | return -EFAULT; |
450 | } | |
451 | ||
452 | dccp_pr_debug("Don't agree... reconfirming %d\n", *res); | |
453 | agree = 0; /* this is used for mandatory options... */ | |
454 | } | |
455 | ||
456 | /* need to put result and our preference list */ | |
afe00251 AB |
457 | rlen = 1 + opt->dccpop_len; |
458 | rpref = kmalloc(rlen, GFP_ATOMIC); | |
459 | if (rpref == NULL) | |
460 | return -ENOMEM; | |
461 | ||
462 | *rpref = *res; | |
463 | memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len); | |
464 | ||
465 | /* put it in the "confirm queue" */ | |
466 | if (opt->dccpop_sc == NULL) { | |
467 | opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC); | |
468 | if (opt->dccpop_sc == NULL) { | |
469 | kfree(rpref); | |
470 | return -ENOMEM; | |
471 | } | |
472 | } else { | |
473 | /* recycle the confirm slot */ | |
474 | BUG_ON(opt->dccpop_sc->dccpoc_val == NULL); | |
475 | kfree(opt->dccpop_sc->dccpoc_val); | |
476 | dccp_pr_debug("recycling confirm slot\n"); | |
477 | } | |
478 | memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc)); | |
479 | ||
480 | opt->dccpop_sc->dccpoc_val = rpref; | |
481 | opt->dccpop_sc->dccpoc_len = rlen; | |
482 | ||
483 | /* update the option on our side [we are about to send the confirm] */ | |
484 | rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res); | |
485 | if (rc) { | |
486 | kfree(opt->dccpop_sc->dccpoc_val); | |
487 | kfree(opt->dccpop_sc); | |
68907dad | 488 | opt->dccpop_sc = NULL; |
afe00251 AB |
489 | return rc; |
490 | } | |
491 | ||
492 | dccp_pr_debug("Will confirm %d\n", *rpref); | |
493 | ||
494 | /* say we want to change to X but we just got a confirm X, suppress our | |
495 | * change | |
496 | */ | |
497 | if (!opt->dccpop_conf) { | |
498 | if (*opt->dccpop_val == *res) | |
499 | opt->dccpop_conf = 1; | |
500 | dccp_pr_debug("won't ask for change of same feature\n"); | |
501 | } | |
502 | ||
503 | return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */ | |
504 | } | |
505 | ||
506 | static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) | |
507 | { | |
a4bf3902 | 508 | struct dccp_minisock *dmsk = dccp_msk(sk); |
afe00251 AB |
509 | struct dccp_opt_pend *opt; |
510 | int rc = 1; | |
511 | u8 t; | |
512 | ||
513 | /* | |
514 | * We received a CHANGE. We gotta match it against our own preference | |
515 | * list. If we got a CHANGE_R it means it's a change for us, so we need | |
516 | * to compare our CHANGE_L list. | |
517 | */ | |
518 | if (type == DCCPO_CHANGE_L) | |
519 | t = DCCPO_CHANGE_R; | |
520 | else | |
521 | t = DCCPO_CHANGE_L; | |
522 | ||
523 | /* find our preference list for this feature */ | |
a4bf3902 | 524 | list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { |
afe00251 AB |
525 | if (opt->dccpop_type != t || opt->dccpop_feat != feature) |
526 | continue; | |
527 | ||
528 | /* find the winner from the two preference lists */ | |
529 | rc = dccp_feat_reconcile(sk, opt, val, len); | |
530 | break; | |
531 | } | |
532 | ||
533 | /* We didn't deal with the change. This can happen if we have no | |
534 | * preference list for the feature. In fact, it just shouldn't | |
535 | * happen---if we understand a feature, we should have a preference list | |
536 | * with at least the default value. | |
537 | */ | |
538 | BUG_ON(rc == 1); | |
539 | ||
540 | return rc; | |
541 | } | |
542 | ||
543 | static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) | |
544 | { | |
545 | struct dccp_opt_pend *opt; | |
a4bf3902 | 546 | struct dccp_minisock *dmsk = dccp_msk(sk); |
afe00251 AB |
547 | u8 *copy; |
548 | int rc; | |
549 | ||
c02fdc0e GR |
550 | /* NN features must be Change L (sec. 6.3.2) */ |
551 | if (type != DCCPO_CHANGE_L) { | |
552 | dccp_pr_debug("received %s for NN feature %d\n", | |
553 | dccp_feat_typename(type), feature); | |
afe00251 AB |
554 | return -EFAULT; |
555 | } | |
556 | ||
557 | /* XXX sanity check opt val */ | |
558 | ||
559 | /* copy option so we can confirm it */ | |
560 | opt = kzalloc(sizeof(*opt), GFP_ATOMIC); | |
561 | if (opt == NULL) | |
562 | return -ENOMEM; | |
563 | ||
eed73417 | 564 | copy = kmemdup(val, len, GFP_ATOMIC); |
afe00251 AB |
565 | if (copy == NULL) { |
566 | kfree(opt); | |
567 | return -ENOMEM; | |
568 | } | |
afe00251 AB |
569 | |
570 | opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */ | |
571 | opt->dccpop_feat = feature; | |
572 | opt->dccpop_val = copy; | |
573 | opt->dccpop_len = len; | |
574 | ||
575 | /* change feature */ | |
576 | rc = dccp_feat_update(sk, type, feature, *val); | |
577 | if (rc) { | |
578 | kfree(opt->dccpop_val); | |
579 | kfree(opt); | |
580 | return rc; | |
581 | } | |
582 | ||
c02fdc0e GR |
583 | dccp_feat_debug(type, feature, *copy); |
584 | ||
a4bf3902 | 585 | list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf); |
afe00251 AB |
586 | |
587 | return 0; | |
588 | } | |
589 | ||
8ca0d17b ACM |
590 | static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk, |
591 | u8 type, u8 feature) | |
afe00251 | 592 | { |
afe00251 AB |
593 | /* XXX check if other confirms for that are queued and recycle slot */ |
594 | struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC); | |
595 | ||
596 | if (opt == NULL) { | |
597 | /* XXX what do we do? Ignoring should be fine. It's a change | |
598 | * after all =P | |
599 | */ | |
600 | return; | |
601 | } | |
602 | ||
c02fdc0e | 603 | switch (type) { |
e576de82 JJ |
604 | case DCCPO_CHANGE_L: |
605 | opt->dccpop_type = DCCPO_CONFIRM_R; | |
606 | break; | |
607 | case DCCPO_CHANGE_R: | |
608 | opt->dccpop_type = DCCPO_CONFIRM_L; | |
609 | break; | |
610 | default: | |
611 | DCCP_WARN("invalid type %d\n", type); | |
612 | kfree(opt); | |
613 | return; | |
c02fdc0e | 614 | } |
afe00251 | 615 | opt->dccpop_feat = feature; |
68907dad | 616 | opt->dccpop_val = NULL; |
afe00251 AB |
617 | opt->dccpop_len = 0; |
618 | ||
619 | /* change feature */ | |
c02fdc0e GR |
620 | dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature); |
621 | ||
a4bf3902 | 622 | list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf); |
afe00251 AB |
623 | } |
624 | ||
625 | static void dccp_feat_flush_confirm(struct sock *sk) | |
626 | { | |
a4bf3902 | 627 | struct dccp_minisock *dmsk = dccp_msk(sk); |
afe00251 | 628 | /* Check if there is anything to confirm in the first place */ |
a4bf3902 | 629 | int yes = !list_empty(&dmsk->dccpms_conf); |
afe00251 AB |
630 | |
631 | if (!yes) { | |
632 | struct dccp_opt_pend *opt; | |
633 | ||
a4bf3902 | 634 | list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { |
afe00251 AB |
635 | if (opt->dccpop_conf) { |
636 | yes = 1; | |
637 | break; | |
638 | } | |
639 | } | |
640 | } | |
641 | ||
642 | if (!yes) | |
643 | return; | |
644 | ||
645 | /* OK there is something to confirm... */ | |
646 | /* XXX check if packet is in flight? Send delayed ack?? */ | |
647 | if (sk->sk_state == DCCP_OPEN) | |
648 | dccp_send_ack(sk); | |
649 | } | |
650 | ||
651 | int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) | |
652 | { | |
653 | int rc; | |
654 | ||
c02fdc0e | 655 | dccp_feat_debug(type, feature, *val); |
afe00251 AB |
656 | |
657 | /* figure out if it's SP or NN feature */ | |
658 | switch (feature) { | |
659 | /* deal with SP features */ | |
660 | case DCCPF_CCID: | |
661 | rc = dccp_feat_sp(sk, type, feature, val, len); | |
662 | break; | |
663 | ||
664 | /* deal with NN features */ | |
665 | case DCCPF_ACK_RATIO: | |
666 | rc = dccp_feat_nn(sk, type, feature, val, len); | |
667 | break; | |
668 | ||
669 | /* XXX implement other features */ | |
670 | default: | |
c02fdc0e GR |
671 | dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n", |
672 | dccp_feat_typename(type), feature); | |
afe00251 AB |
673 | rc = -EFAULT; |
674 | break; | |
675 | } | |
676 | ||
677 | /* check if there were problems changing features */ | |
678 | if (rc) { | |
679 | /* If we don't agree on SP, we sent a confirm for old value. | |
680 | * However we propagate rc to caller in case option was | |
681 | * mandatory | |
682 | */ | |
683 | if (rc != DCCP_FEAT_SP_NOAGREE) | |
8ca0d17b | 684 | dccp_feat_empty_confirm(dccp_msk(sk), type, feature); |
afe00251 AB |
685 | } |
686 | ||
687 | /* generate the confirm [if required] */ | |
688 | dccp_feat_flush_confirm(sk); | |
689 | ||
690 | return rc; | |
691 | } | |
692 | ||
693 | EXPORT_SYMBOL_GPL(dccp_feat_change_recv); | |
694 | ||
695 | int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature, | |
696 | u8 *val, u8 len) | |
697 | { | |
698 | u8 t; | |
699 | struct dccp_opt_pend *opt; | |
a4bf3902 | 700 | struct dccp_minisock *dmsk = dccp_msk(sk); |
c02fdc0e | 701 | int found = 0; |
afe00251 AB |
702 | int all_confirmed = 1; |
703 | ||
c02fdc0e | 704 | dccp_feat_debug(type, feature, *val); |
afe00251 AB |
705 | |
706 | /* locate our change request */ | |
c02fdc0e GR |
707 | switch (type) { |
708 | case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break; | |
709 | case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break; | |
8109b02b | 710 | default: DCCP_WARN("invalid type %d\n", type); |
c02fdc0e GR |
711 | return 1; |
712 | ||
713 | } | |
714 | /* XXX sanity check feature value */ | |
afe00251 | 715 | |
a4bf3902 | 716 | list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { |
afe00251 AB |
717 | if (!opt->dccpop_conf && opt->dccpop_type == t && |
718 | opt->dccpop_feat == feature) { | |
c02fdc0e GR |
719 | found = 1; |
720 | dccp_pr_debug("feature %d found\n", opt->dccpop_feat); | |
721 | ||
afe00251 AB |
722 | /* XXX do sanity check */ |
723 | ||
724 | opt->dccpop_conf = 1; | |
725 | ||
726 | /* We got a confirmation---change the option */ | |
727 | dccp_feat_update(sk, opt->dccpop_type, | |
728 | opt->dccpop_feat, *val); | |
729 | ||
c02fdc0e | 730 | /* XXX check the return value of dccp_feat_update */ |
afe00251 AB |
731 | break; |
732 | } | |
733 | ||
734 | if (!opt->dccpop_conf) | |
735 | all_confirmed = 0; | |
736 | } | |
737 | ||
738 | /* fix re-transmit timer */ | |
739 | /* XXX gotta make sure that no option negotiation occurs during | |
740 | * connection shutdown. Consider that the CLOSEREQ is sent and timer is | |
741 | * on. if all options are confirmed it might kill timer which should | |
742 | * remain alive until close is received. | |
743 | */ | |
744 | if (all_confirmed) { | |
745 | dccp_pr_debug("clear feat negotiation timer %p\n", sk); | |
746 | inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS); | |
747 | } | |
748 | ||
c02fdc0e GR |
749 | if (!found) |
750 | dccp_pr_debug("%s(%d, ...) never requested\n", | |
751 | dccp_feat_typename(type), feature); | |
afe00251 AB |
752 | return 0; |
753 | } | |
754 | ||
755 | EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv); | |
756 | ||
8ca0d17b | 757 | void dccp_feat_clean(struct dccp_minisock *dmsk) |
afe00251 | 758 | { |
afe00251 AB |
759 | struct dccp_opt_pend *opt, *next; |
760 | ||
a4bf3902 | 761 | list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending, |
afe00251 | 762 | dccpop_node) { |
c9eaf173 YH |
763 | BUG_ON(opt->dccpop_val == NULL); |
764 | kfree(opt->dccpop_val); | |
afe00251 AB |
765 | |
766 | if (opt->dccpop_sc != NULL) { | |
767 | BUG_ON(opt->dccpop_sc->dccpoc_val == NULL); | |
768 | kfree(opt->dccpop_sc->dccpoc_val); | |
769 | kfree(opt->dccpop_sc); | |
770 | } | |
771 | ||
c9eaf173 YH |
772 | kfree(opt); |
773 | } | |
a4bf3902 | 774 | INIT_LIST_HEAD(&dmsk->dccpms_pending); |
afe00251 | 775 | |
a4bf3902 | 776 | list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) { |
afe00251 AB |
777 | BUG_ON(opt == NULL); |
778 | if (opt->dccpop_val != NULL) | |
779 | kfree(opt->dccpop_val); | |
780 | kfree(opt); | |
781 | } | |
a4bf3902 | 782 | INIT_LIST_HEAD(&dmsk->dccpms_conf); |
afe00251 AB |
783 | } |
784 | ||
785 | EXPORT_SYMBOL_GPL(dccp_feat_clean); | |
786 | ||
787 | /* this is to be called only when a listening sock creates its child. It is | |
788 | * assumed by the function---the confirm is not duplicated, but rather it is | |
789 | * "passed on". | |
790 | */ | |
791 | int dccp_feat_clone(struct sock *oldsk, struct sock *newsk) | |
792 | { | |
a4bf3902 ACM |
793 | struct dccp_minisock *olddmsk = dccp_msk(oldsk); |
794 | struct dccp_minisock *newdmsk = dccp_msk(newsk); | |
afe00251 AB |
795 | struct dccp_opt_pend *opt; |
796 | int rc = 0; | |
797 | ||
a4bf3902 ACM |
798 | INIT_LIST_HEAD(&newdmsk->dccpms_pending); |
799 | INIT_LIST_HEAD(&newdmsk->dccpms_conf); | |
afe00251 | 800 | |
a4bf3902 | 801 | list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) { |
afe00251 AB |
802 | struct dccp_opt_pend *newopt; |
803 | /* copy the value of the option */ | |
eed73417 | 804 | u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC); |
afe00251 AB |
805 | |
806 | if (val == NULL) | |
807 | goto out_clean; | |
afe00251 | 808 | |
eed73417 | 809 | newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC); |
afe00251 AB |
810 | if (newopt == NULL) { |
811 | kfree(val); | |
812 | goto out_clean; | |
813 | } | |
814 | ||
815 | /* insert the option */ | |
afe00251 | 816 | newopt->dccpop_val = val; |
a4bf3902 | 817 | list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending); |
afe00251 AB |
818 | |
819 | /* XXX what happens with backlogs and multiple connections at | |
820 | * once... | |
821 | */ | |
822 | /* the master socket no longer needs to worry about confirms */ | |
68907dad | 823 | opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */ |
afe00251 AB |
824 | |
825 | /* reset state for a new socket */ | |
826 | opt->dccpop_conf = 0; | |
827 | } | |
828 | ||
829 | /* XXX not doing anything about the conf queue */ | |
830 | ||
831 | out: | |
832 | return rc; | |
833 | ||
834 | out_clean: | |
8ca0d17b | 835 | dccp_feat_clean(newdmsk); |
afe00251 AB |
836 | rc = -ENOMEM; |
837 | goto out; | |
838 | } | |
839 | ||
840 | EXPORT_SYMBOL_GPL(dccp_feat_clone); | |
841 | ||
8ca0d17b ACM |
842 | static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat, |
843 | u8 *val, u8 len) | |
afe00251 AB |
844 | { |
845 | int rc = -ENOMEM; | |
eed73417 | 846 | u8 *copy = kmemdup(val, len, GFP_KERNEL); |
afe00251 AB |
847 | |
848 | if (copy != NULL) { | |
8ca0d17b | 849 | rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL); |
afe00251 AB |
850 | if (rc) |
851 | kfree(copy); | |
852 | } | |
853 | return rc; | |
854 | } | |
855 | ||
8ca0d17b | 856 | int dccp_feat_init(struct dccp_minisock *dmsk) |
afe00251 | 857 | { |
afe00251 AB |
858 | int rc; |
859 | ||
a4bf3902 ACM |
860 | INIT_LIST_HEAD(&dmsk->dccpms_pending); |
861 | INIT_LIST_HEAD(&dmsk->dccpms_conf); | |
afe00251 AB |
862 | |
863 | /* CCID L */ | |
8ca0d17b | 864 | rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID, |
a4bf3902 | 865 | &dmsk->dccpms_tx_ccid, 1); |
afe00251 AB |
866 | if (rc) |
867 | goto out; | |
868 | ||
869 | /* CCID R */ | |
8ca0d17b | 870 | rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID, |
a4bf3902 | 871 | &dmsk->dccpms_rx_ccid, 1); |
afe00251 AB |
872 | if (rc) |
873 | goto out; | |
874 | ||
875 | /* Ack ratio */ | |
8ca0d17b | 876 | rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO, |
a4bf3902 | 877 | &dmsk->dccpms_ack_ratio, 1); |
afe00251 AB |
878 | out: |
879 | return rc; | |
880 | } | |
881 | ||
882 | EXPORT_SYMBOL_GPL(dccp_feat_init); | |
c02fdc0e GR |
883 | |
884 | #ifdef CONFIG_IP_DCCP_DEBUG | |
885 | const char *dccp_feat_typename(const u8 type) | |
886 | { | |
887 | switch(type) { | |
888 | case DCCPO_CHANGE_L: return("ChangeL"); | |
889 | case DCCPO_CONFIRM_L: return("ConfirmL"); | |
890 | case DCCPO_CHANGE_R: return("ChangeR"); | |
891 | case DCCPO_CONFIRM_R: return("ConfirmR"); | |
892 | /* the following case must not appear in feature negotation */ | |
8109b02b | 893 | default: dccp_pr_debug("unknown type %d [BUG!]\n", type); |
c02fdc0e GR |
894 | } |
895 | return NULL; | |
896 | } | |
897 | ||
898 | EXPORT_SYMBOL_GPL(dccp_feat_typename); | |
899 | ||
900 | const char *dccp_feat_name(const u8 feat) | |
901 | { | |
902 | static const char *feature_names[] = { | |
903 | [DCCPF_RESERVED] = "Reserved", | |
904 | [DCCPF_CCID] = "CCID", | |
905 | [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos", | |
906 | [DCCPF_SEQUENCE_WINDOW] = "Sequence Window", | |
907 | [DCCPF_ECN_INCAPABLE] = "ECN Incapable", | |
908 | [DCCPF_ACK_RATIO] = "Ack Ratio", | |
909 | [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector", | |
910 | [DCCPF_SEND_NDP_COUNT] = "Send NDP Count", | |
911 | [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage", | |
912 | [DCCPF_DATA_CHECKSUM] = "Send Data Checksum", | |
913 | }; | |
dd6303df GR |
914 | if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC) |
915 | return feature_names[DCCPF_RESERVED]; | |
916 | ||
b4eec206 GR |
917 | if (feat == DCCPF_SEND_LEV_RATE) |
918 | return "Send Loss Event Rate"; | |
c02fdc0e GR |
919 | if (feat >= DCCPF_MIN_CCID_SPECIFIC) |
920 | return "CCID-specific"; | |
921 | ||
c02fdc0e GR |
922 | return feature_names[feat]; |
923 | } | |
924 | ||
925 | EXPORT_SYMBOL_GPL(dccp_feat_name); | |
926 | #endif /* CONFIG_IP_DCCP_DEBUG */ |