blob: 9a37b6ce3aca60eb56f375409e9d3c0b8ab1d235 [file] [log] [blame]
Andrea Bittauafe00252006-03-20 17:43:56 -08001/*
2 * net/dccp/feat.c
3 *
4 * An implementation of the DCCP protocol
5 * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
6 *
Gerrit Renker5cdae192007-12-13 12:41:46 -02007 * 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.
Andrea Bittauafe00252006-03-20 17:43:56 -080017 */
18
Andrea Bittauafe00252006-03-20 17:43:56 -080019#include <linux/module.h>
20
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080021#include "ccid.h"
Andrea Bittauafe00252006-03-20 17:43:56 -080022#include "feat.h"
23
24#define DCCP_FEAT_SP_NOAGREE (-123)
25
Gerrit Renker7d43d1a02008-11-04 23:43:47 -080026static 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
Gerrit Renker61e64732008-11-04 23:54:04 -080063/**
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 */
67static 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
84static 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
93static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
94{
95 if (unlikely(val == NULL))
96 return;
97 if (dccp_feat_type(feat_num) == FEAT_SP)
98 kfree(val->sp.vec);
99 memset(val, 0, sizeof(*val));
100}
101
102static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
103{
104 if (entry != NULL) {
105 dccp_feat_val_destructor(entry->feat_num, &entry->val);
106 kfree(entry);
107 }
108}
109
110/*
111 * List management functions
112 *
113 * Feature negotiation lists rely on and maintain the following invariants:
114 * - each feat_num in the list is known, i.e. we know its type and default value
115 * - each feat_num/is_local combination is unique (old entries are overwritten)
116 * - SP values are always freshly allocated
117 * - list is sorted in increasing order of feature number (faster lookup)
118 */
119
120static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
121{
122 list_del(&entry->node);
123 dccp_feat_entry_destructor(entry);
124}
125
126void dccp_feat_list_purge(struct list_head *fn_list)
127{
128 struct dccp_feat_entry *entry, *next;
129
130 list_for_each_entry_safe(entry, next, fn_list, node)
131 dccp_feat_entry_destructor(entry);
132 INIT_LIST_HEAD(fn_list);
133}
134EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
135
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800136int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
137 u8 *val, u8 len, gfp_t gfp)
Andrea Bittauafe00252006-03-20 17:43:56 -0800138{
Andrea Bittauafe00252006-03-20 17:43:56 -0800139 struct dccp_opt_pend *opt;
140
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200141 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800142
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200143 if (len > 3) {
Gerrit Renker59348b12006-11-20 18:39:23 -0200144 DCCP_WARN("invalid length %d\n", len);
Chris Wright19443172008-05-05 13:50:24 -0700145 return -EINVAL;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200146 }
147 /* XXX add further sanity checks */
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800148
Andrea Bittauafe00252006-03-20 17:43:56 -0800149 /* check if that feature is already being negotiated */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800150 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800151 /* ok we found a negotiation for this option already */
152 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
153 dccp_pr_debug("Replacing old\n");
154 /* replace */
155 BUG_ON(opt->dccpop_val == NULL);
156 kfree(opt->dccpop_val);
157 opt->dccpop_val = val;
158 opt->dccpop_len = len;
159 opt->dccpop_conf = 0;
160 return 0;
161 }
162 }
163
164 /* negotiation for a new feature */
165 opt = kmalloc(sizeof(*opt), gfp);
166 if (opt == NULL)
167 return -ENOMEM;
168
169 opt->dccpop_type = type;
170 opt->dccpop_feat = feature;
171 opt->dccpop_len = len;
172 opt->dccpop_val = val;
173 opt->dccpop_conf = 0;
174 opt->dccpop_sc = NULL;
175
176 BUG_ON(opt->dccpop_val == NULL);
177
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800178 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800179 return 0;
180}
181
182EXPORT_SYMBOL_GPL(dccp_feat_change);
183
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800184static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
185{
186 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800187 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800188 /* figure out if we are changing our CCID or the peer's */
189 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800190 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800191 struct ccid *new_ccid;
192
193 /* Check if nothing is being changed. */
194 if (ccid_nr == new_ccid_nr)
195 return 0;
196
197 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
198 if (new_ccid == NULL)
199 return -ENOMEM;
200
201 if (rx) {
202 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
203 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800204 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800205 } else {
206 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
207 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800208 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800209 }
210
211 return 0;
212}
213
Andrea Bittauafe00252006-03-20 17:43:56 -0800214static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
215{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200216 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800217
218 switch (feat) {
219 case DCCPF_CCID:
220 return dccp_feat_update_ccid(sk, type, val);
221 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200222 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
223 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800224 break;
225 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800226 return 0;
227}
228
229static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
230 u8 *rpref, u8 rlen)
231{
232 struct dccp_sock *dp = dccp_sk(sk);
233 u8 *spref, slen, *res = NULL;
234 int i, j, rc, agree = 1;
235
236 BUG_ON(rpref == NULL);
237
238 /* check if we are the black sheep */
239 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
240 spref = rpref;
241 slen = rlen;
242 rpref = opt->dccpop_val;
243 rlen = opt->dccpop_len;
244 } else {
245 spref = opt->dccpop_val;
246 slen = opt->dccpop_len;
247 }
248 /*
249 * Now we have server preference list in spref and client preference in
250 * rpref
251 */
252 BUG_ON(spref == NULL);
253 BUG_ON(rpref == NULL);
254
255 /* FIXME sanity check vals */
256
257 /* Are values in any order? XXX Lame "algorithm" here */
Andrea Bittauafe00252006-03-20 17:43:56 -0800258 for (i = 0; i < slen; i++) {
259 for (j = 0; j < rlen; j++) {
260 if (spref[i] == rpref[j]) {
261 res = &spref[i];
262 break;
263 }
264 }
265 if (res)
266 break;
267 }
268
269 /* we didn't agree on anything */
270 if (res == NULL) {
271 /* confirm previous value */
272 switch (opt->dccpop_feat) {
273 case DCCPF_CCID:
274 /* XXX did i get this right? =P */
275 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800276 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800277 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800278 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800279 break;
280
281 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200282 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
283 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800284 return -EFAULT;
285 }
286
287 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
288 agree = 0; /* this is used for mandatory options... */
289 }
290
291 /* need to put result and our preference list */
Andrea Bittauafe00252006-03-20 17:43:56 -0800292 rlen = 1 + opt->dccpop_len;
293 rpref = kmalloc(rlen, GFP_ATOMIC);
294 if (rpref == NULL)
295 return -ENOMEM;
296
297 *rpref = *res;
298 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
299
300 /* put it in the "confirm queue" */
301 if (opt->dccpop_sc == NULL) {
302 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
303 if (opt->dccpop_sc == NULL) {
304 kfree(rpref);
305 return -ENOMEM;
306 }
307 } else {
308 /* recycle the confirm slot */
309 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
310 kfree(opt->dccpop_sc->dccpoc_val);
311 dccp_pr_debug("recycling confirm slot\n");
312 }
313 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
314
315 opt->dccpop_sc->dccpoc_val = rpref;
316 opt->dccpop_sc->dccpoc_len = rlen;
317
318 /* update the option on our side [we are about to send the confirm] */
319 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
320 if (rc) {
321 kfree(opt->dccpop_sc->dccpoc_val);
322 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800323 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800324 return rc;
325 }
326
327 dccp_pr_debug("Will confirm %d\n", *rpref);
328
329 /* say we want to change to X but we just got a confirm X, suppress our
330 * change
331 */
332 if (!opt->dccpop_conf) {
333 if (*opt->dccpop_val == *res)
334 opt->dccpop_conf = 1;
335 dccp_pr_debug("won't ask for change of same feature\n");
336 }
337
338 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
339}
340
341static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
342{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800343 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800344 struct dccp_opt_pend *opt;
345 int rc = 1;
346 u8 t;
347
348 /*
349 * We received a CHANGE. We gotta match it against our own preference
350 * list. If we got a CHANGE_R it means it's a change for us, so we need
351 * to compare our CHANGE_L list.
352 */
353 if (type == DCCPO_CHANGE_L)
354 t = DCCPO_CHANGE_R;
355 else
356 t = DCCPO_CHANGE_L;
357
358 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800359 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800360 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
361 continue;
362
363 /* find the winner from the two preference lists */
364 rc = dccp_feat_reconcile(sk, opt, val, len);
365 break;
366 }
367
368 /* We didn't deal with the change. This can happen if we have no
369 * preference list for the feature. In fact, it just shouldn't
370 * happen---if we understand a feature, we should have a preference list
371 * with at least the default value.
372 */
373 BUG_ON(rc == 1);
374
375 return rc;
376}
377
378static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
379{
380 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800381 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800382 u8 *copy;
383 int rc;
384
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200385 /* NN features must be Change L (sec. 6.3.2) */
386 if (type != DCCPO_CHANGE_L) {
387 dccp_pr_debug("received %s for NN feature %d\n",
388 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800389 return -EFAULT;
390 }
391
392 /* XXX sanity check opt val */
393
394 /* copy option so we can confirm it */
395 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
396 if (opt == NULL)
397 return -ENOMEM;
398
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200399 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800400 if (copy == NULL) {
401 kfree(opt);
402 return -ENOMEM;
403 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800404
405 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
406 opt->dccpop_feat = feature;
407 opt->dccpop_val = copy;
408 opt->dccpop_len = len;
409
410 /* change feature */
411 rc = dccp_feat_update(sk, type, feature, *val);
412 if (rc) {
413 kfree(opt->dccpop_val);
414 kfree(opt);
415 return rc;
416 }
417
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200418 dccp_feat_debug(type, feature, *copy);
419
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800420 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800421
422 return 0;
423}
424
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800425static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
426 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800427{
Andrea Bittauafe00252006-03-20 17:43:56 -0800428 /* XXX check if other confirms for that are queued and recycle slot */
429 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
430
431 if (opt == NULL) {
432 /* XXX what do we do? Ignoring should be fine. It's a change
433 * after all =P
434 */
435 return;
436 }
437
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200438 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -0700439 case DCCPO_CHANGE_L:
440 opt->dccpop_type = DCCPO_CONFIRM_R;
441 break;
442 case DCCPO_CHANGE_R:
443 opt->dccpop_type = DCCPO_CONFIRM_L;
444 break;
445 default:
446 DCCP_WARN("invalid type %d\n", type);
447 kfree(opt);
448 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200449 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800450 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -0800451 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800452 opt->dccpop_len = 0;
453
454 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200455 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
456
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800457 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800458}
459
460static void dccp_feat_flush_confirm(struct sock *sk)
461{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800462 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800463 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800464 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800465
466 if (!yes) {
467 struct dccp_opt_pend *opt;
468
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800469 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800470 if (opt->dccpop_conf) {
471 yes = 1;
472 break;
473 }
474 }
475 }
476
477 if (!yes)
478 return;
479
480 /* OK there is something to confirm... */
481 /* XXX check if packet is in flight? Send delayed ack?? */
482 if (sk->sk_state == DCCP_OPEN)
483 dccp_send_ack(sk);
484}
485
486int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
487{
488 int rc;
489
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200490 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800491
492 /* figure out if it's SP or NN feature */
493 switch (feature) {
494 /* deal with SP features */
495 case DCCPF_CCID:
496 rc = dccp_feat_sp(sk, type, feature, val, len);
497 break;
498
499 /* deal with NN features */
500 case DCCPF_ACK_RATIO:
501 rc = dccp_feat_nn(sk, type, feature, val, len);
502 break;
503
504 /* XXX implement other features */
505 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200506 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
507 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800508 rc = -EFAULT;
509 break;
510 }
511
512 /* check if there were problems changing features */
513 if (rc) {
514 /* If we don't agree on SP, we sent a confirm for old value.
515 * However we propagate rc to caller in case option was
516 * mandatory
517 */
518 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800519 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800520 }
521
522 /* generate the confirm [if required] */
523 dccp_feat_flush_confirm(sk);
524
525 return rc;
526}
527
528EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
529
530int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
531 u8 *val, u8 len)
532{
533 u8 t;
534 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800535 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200536 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -0800537 int all_confirmed = 1;
538
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200539 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800540
541 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200542 switch (type) {
543 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
544 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200545 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200546 return 1;
547
548 }
549 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -0800550
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800551 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800552 if (!opt->dccpop_conf && opt->dccpop_type == t &&
553 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200554 found = 1;
555 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
556
Andrea Bittauafe00252006-03-20 17:43:56 -0800557 /* XXX do sanity check */
558
559 opt->dccpop_conf = 1;
560
561 /* We got a confirmation---change the option */
562 dccp_feat_update(sk, opt->dccpop_type,
563 opt->dccpop_feat, *val);
564
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200565 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -0800566 break;
567 }
568
569 if (!opt->dccpop_conf)
570 all_confirmed = 0;
571 }
572
573 /* fix re-transmit timer */
574 /* XXX gotta make sure that no option negotiation occurs during
575 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
576 * on. if all options are confirmed it might kill timer which should
577 * remain alive until close is received.
578 */
579 if (all_confirmed) {
580 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
581 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
582 }
583
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200584 if (!found)
585 dccp_pr_debug("%s(%d, ...) never requested\n",
586 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800587 return 0;
588}
589
590EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
591
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800592void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800593{
Andrea Bittauafe00252006-03-20 17:43:56 -0800594 struct dccp_opt_pend *opt, *next;
595
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800596 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -0800597 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900598 BUG_ON(opt->dccpop_val == NULL);
599 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800600
601 if (opt->dccpop_sc != NULL) {
602 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
603 kfree(opt->dccpop_sc->dccpoc_val);
604 kfree(opt->dccpop_sc);
605 }
606
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900607 kfree(opt);
608 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800609 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800610
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800611 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800612 BUG_ON(opt == NULL);
613 if (opt->dccpop_val != NULL)
614 kfree(opt->dccpop_val);
615 kfree(opt);
616 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800617 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800618}
619
620EXPORT_SYMBOL_GPL(dccp_feat_clean);
621
622/* this is to be called only when a listening sock creates its child. It is
623 * assumed by the function---the confirm is not duplicated, but rather it is
624 * "passed on".
625 */
626int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
627{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800628 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
629 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800630 struct dccp_opt_pend *opt;
631 int rc = 0;
632
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800633 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
634 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800635
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800636 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800637 struct dccp_opt_pend *newopt;
638 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200639 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800640
641 if (val == NULL)
642 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -0800643
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200644 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800645 if (newopt == NULL) {
646 kfree(val);
647 goto out_clean;
648 }
649
650 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -0800651 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800652 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800653
654 /* XXX what happens with backlogs and multiple connections at
655 * once...
656 */
657 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -0800658 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -0800659
660 /* reset state for a new socket */
661 opt->dccpop_conf = 0;
662 }
663
664 /* XXX not doing anything about the conf queue */
665
666out:
667 return rc;
668
669out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800670 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800671 rc = -ENOMEM;
672 goto out;
673}
674
675EXPORT_SYMBOL_GPL(dccp_feat_clone);
676
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800677static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
678 u8 *val, u8 len)
Andrea Bittauafe00252006-03-20 17:43:56 -0800679{
680 int rc = -ENOMEM;
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200681 u8 *copy = kmemdup(val, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800682
683 if (copy != NULL) {
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800684 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800685 if (rc)
686 kfree(copy);
687 }
688 return rc;
689}
690
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800691int dccp_feat_init(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800692{
Andrea Bittauafe00252006-03-20 17:43:56 -0800693 int rc;
694
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800695 INIT_LIST_HEAD(&dmsk->dccpms_pending);
696 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800697
698 /* CCID L */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800699 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800700 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800701 if (rc)
702 goto out;
703
704 /* CCID R */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800705 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800706 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800707 if (rc)
708 goto out;
709
710 /* Ack ratio */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800711 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800712 &dmsk->dccpms_ack_ratio, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800713out:
714 return rc;
715}
716
717EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200718
719#ifdef CONFIG_IP_DCCP_DEBUG
720const char *dccp_feat_typename(const u8 type)
721{
722 switch(type) {
723 case DCCPO_CHANGE_L: return("ChangeL");
724 case DCCPO_CONFIRM_L: return("ConfirmL");
725 case DCCPO_CHANGE_R: return("ChangeR");
726 case DCCPO_CONFIRM_R: return("ConfirmR");
727 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200728 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200729 }
730 return NULL;
731}
732
733EXPORT_SYMBOL_GPL(dccp_feat_typename);
734
735const char *dccp_feat_name(const u8 feat)
736{
737 static const char *feature_names[] = {
738 [DCCPF_RESERVED] = "Reserved",
739 [DCCPF_CCID] = "CCID",
740 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
741 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
742 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
743 [DCCPF_ACK_RATIO] = "Ack Ratio",
744 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
745 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
746 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
747 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
748 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200749 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
750 return feature_names[DCCPF_RESERVED];
751
Gerrit Renker7d43d1a02008-11-04 23:43:47 -0800752 if (feat == DCCPF_SEND_LEV_RATE)
753 return "Send Loss Event Rate";
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200754 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
755 return "CCID-specific";
756
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200757 return feature_names[feat];
758}
759
760EXPORT_SYMBOL_GPL(dccp_feat_name);
761#endif /* CONFIG_IP_DCCP_DEBUG */