blob: 084744e624d3fc874d74b7acecc9511140f9ed42 [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 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
Andrea Bittauafe00252006-03-20 17:43:56 -080013#include <linux/module.h>
14
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080015#include "ccid.h"
Andrea Bittauafe00252006-03-20 17:43:56 -080016#include "feat.h"
17
18#define DCCP_FEAT_SP_NOAGREE (-123)
19
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -080020int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
21 u8 *val, u8 len, gfp_t gfp)
Andrea Bittauafe00252006-03-20 17:43:56 -080022{
Andrea Bittauafe00252006-03-20 17:43:56 -080023 struct dccp_opt_pend *opt;
24
Gerrit Renkerc02fdc02006-11-14 12:48:10 -020025 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -080026
Gerrit Renkerdd6303d2007-12-13 12:40:40 -020027 if (len > 3) {
Gerrit Renker59348b12006-11-20 18:39:23 -020028 DCCP_WARN("invalid length %d\n", len);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -020029 return 1;
30 }
31 /* XXX add further sanity checks */
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080032
Andrea Bittauafe00252006-03-20 17:43:56 -080033 /* check if that feature is already being negotiated */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080034 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -080035 /* ok we found a negotiation for this option already */
36 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
37 dccp_pr_debug("Replacing old\n");
38 /* replace */
39 BUG_ON(opt->dccpop_val == NULL);
40 kfree(opt->dccpop_val);
41 opt->dccpop_val = val;
42 opt->dccpop_len = len;
43 opt->dccpop_conf = 0;
44 return 0;
45 }
46 }
47
48 /* negotiation for a new feature */
49 opt = kmalloc(sizeof(*opt), gfp);
50 if (opt == NULL)
51 return -ENOMEM;
52
53 opt->dccpop_type = type;
54 opt->dccpop_feat = feature;
55 opt->dccpop_len = len;
56 opt->dccpop_val = val;
57 opt->dccpop_conf = 0;
58 opt->dccpop_sc = NULL;
59
60 BUG_ON(opt->dccpop_val == NULL);
61
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080062 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -080063 return 0;
64}
65
66EXPORT_SYMBOL_GPL(dccp_feat_change);
67
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080068static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
69{
70 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080071 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080072 /* figure out if we are changing our CCID or the peer's */
73 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080074 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080075 struct ccid *new_ccid;
76
77 /* Check if nothing is being changed. */
78 if (ccid_nr == new_ccid_nr)
79 return 0;
80
81 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
82 if (new_ccid == NULL)
83 return -ENOMEM;
84
85 if (rx) {
86 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
87 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080088 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080089 } else {
90 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
91 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080092 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080093 }
94
95 return 0;
96}
97
Andrea Bittauafe00252006-03-20 17:43:56 -080098/* XXX taking only u8 vals */
99static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
100{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200101 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800102
103 switch (feat) {
104 case DCCPF_CCID:
105 return dccp_feat_update_ccid(sk, type, val);
106 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200107 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
108 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800109 break;
110 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800111 return 0;
112}
113
114static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
115 u8 *rpref, u8 rlen)
116{
117 struct dccp_sock *dp = dccp_sk(sk);
118 u8 *spref, slen, *res = NULL;
119 int i, j, rc, agree = 1;
120
121 BUG_ON(rpref == NULL);
122
123 /* check if we are the black sheep */
124 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
125 spref = rpref;
126 slen = rlen;
127 rpref = opt->dccpop_val;
128 rlen = opt->dccpop_len;
129 } else {
130 spref = opt->dccpop_val;
131 slen = opt->dccpop_len;
132 }
133 /*
134 * Now we have server preference list in spref and client preference in
135 * rpref
136 */
137 BUG_ON(spref == NULL);
138 BUG_ON(rpref == NULL);
139
140 /* FIXME sanity check vals */
141
142 /* Are values in any order? XXX Lame "algorithm" here */
143 /* XXX assume values are 1 byte */
144 for (i = 0; i < slen; i++) {
145 for (j = 0; j < rlen; j++) {
146 if (spref[i] == rpref[j]) {
147 res = &spref[i];
148 break;
149 }
150 }
151 if (res)
152 break;
153 }
154
155 /* we didn't agree on anything */
156 if (res == NULL) {
157 /* confirm previous value */
158 switch (opt->dccpop_feat) {
159 case DCCPF_CCID:
160 /* XXX did i get this right? =P */
161 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800162 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800163 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800164 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800165 break;
166
167 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200168 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
169 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800170 return -EFAULT;
171 }
172
173 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
174 agree = 0; /* this is used for mandatory options... */
175 }
176
177 /* need to put result and our preference list */
178 /* XXX assume 1 byte vals */
179 rlen = 1 + opt->dccpop_len;
180 rpref = kmalloc(rlen, GFP_ATOMIC);
181 if (rpref == NULL)
182 return -ENOMEM;
183
184 *rpref = *res;
185 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
186
187 /* put it in the "confirm queue" */
188 if (opt->dccpop_sc == NULL) {
189 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
190 if (opt->dccpop_sc == NULL) {
191 kfree(rpref);
192 return -ENOMEM;
193 }
194 } else {
195 /* recycle the confirm slot */
196 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
197 kfree(opt->dccpop_sc->dccpoc_val);
198 dccp_pr_debug("recycling confirm slot\n");
199 }
200 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
201
202 opt->dccpop_sc->dccpoc_val = rpref;
203 opt->dccpop_sc->dccpoc_len = rlen;
204
205 /* update the option on our side [we are about to send the confirm] */
206 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
207 if (rc) {
208 kfree(opt->dccpop_sc->dccpoc_val);
209 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800210 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800211 return rc;
212 }
213
214 dccp_pr_debug("Will confirm %d\n", *rpref);
215
216 /* say we want to change to X but we just got a confirm X, suppress our
217 * change
218 */
219 if (!opt->dccpop_conf) {
220 if (*opt->dccpop_val == *res)
221 opt->dccpop_conf = 1;
222 dccp_pr_debug("won't ask for change of same feature\n");
223 }
224
225 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
226}
227
228static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
229{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800230 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800231 struct dccp_opt_pend *opt;
232 int rc = 1;
233 u8 t;
234
235 /*
236 * We received a CHANGE. We gotta match it against our own preference
237 * list. If we got a CHANGE_R it means it's a change for us, so we need
238 * to compare our CHANGE_L list.
239 */
240 if (type == DCCPO_CHANGE_L)
241 t = DCCPO_CHANGE_R;
242 else
243 t = DCCPO_CHANGE_L;
244
245 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800246 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800247 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
248 continue;
249
250 /* find the winner from the two preference lists */
251 rc = dccp_feat_reconcile(sk, opt, val, len);
252 break;
253 }
254
255 /* We didn't deal with the change. This can happen if we have no
256 * preference list for the feature. In fact, it just shouldn't
257 * happen---if we understand a feature, we should have a preference list
258 * with at least the default value.
259 */
260 BUG_ON(rc == 1);
261
262 return rc;
263}
264
265static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
266{
267 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800268 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800269 u8 *copy;
270 int rc;
271
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200272 /* NN features must be Change L (sec. 6.3.2) */
273 if (type != DCCPO_CHANGE_L) {
274 dccp_pr_debug("received %s for NN feature %d\n",
275 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800276 return -EFAULT;
277 }
278
279 /* XXX sanity check opt val */
280
281 /* copy option so we can confirm it */
282 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
283 if (opt == NULL)
284 return -ENOMEM;
285
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200286 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800287 if (copy == NULL) {
288 kfree(opt);
289 return -ENOMEM;
290 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800291
292 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
293 opt->dccpop_feat = feature;
294 opt->dccpop_val = copy;
295 opt->dccpop_len = len;
296
297 /* change feature */
298 rc = dccp_feat_update(sk, type, feature, *val);
299 if (rc) {
300 kfree(opt->dccpop_val);
301 kfree(opt);
302 return rc;
303 }
304
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200305 dccp_feat_debug(type, feature, *copy);
306
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800307 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800308
309 return 0;
310}
311
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800312static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
313 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800314{
Andrea Bittauafe00252006-03-20 17:43:56 -0800315 /* XXX check if other confirms for that are queued and recycle slot */
316 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
317
318 if (opt == NULL) {
319 /* XXX what do we do? Ignoring should be fine. It's a change
320 * after all =P
321 */
322 return;
323 }
324
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200325 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -0700326 case DCCPO_CHANGE_L:
327 opt->dccpop_type = DCCPO_CONFIRM_R;
328 break;
329 case DCCPO_CHANGE_R:
330 opt->dccpop_type = DCCPO_CONFIRM_L;
331 break;
332 default:
333 DCCP_WARN("invalid type %d\n", type);
334 kfree(opt);
335 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200336 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800337 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -0800338 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800339 opt->dccpop_len = 0;
340
341 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200342 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
343
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800344 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800345}
346
347static void dccp_feat_flush_confirm(struct sock *sk)
348{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800349 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800350 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800351 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800352
353 if (!yes) {
354 struct dccp_opt_pend *opt;
355
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800356 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800357 if (opt->dccpop_conf) {
358 yes = 1;
359 break;
360 }
361 }
362 }
363
364 if (!yes)
365 return;
366
367 /* OK there is something to confirm... */
368 /* XXX check if packet is in flight? Send delayed ack?? */
369 if (sk->sk_state == DCCP_OPEN)
370 dccp_send_ack(sk);
371}
372
373int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
374{
375 int rc;
376
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200377 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800378
379 /* figure out if it's SP or NN feature */
380 switch (feature) {
381 /* deal with SP features */
382 case DCCPF_CCID:
383 rc = dccp_feat_sp(sk, type, feature, val, len);
384 break;
385
386 /* deal with NN features */
387 case DCCPF_ACK_RATIO:
388 rc = dccp_feat_nn(sk, type, feature, val, len);
389 break;
390
391 /* XXX implement other features */
392 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200393 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
394 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800395 rc = -EFAULT;
396 break;
397 }
398
399 /* check if there were problems changing features */
400 if (rc) {
401 /* If we don't agree on SP, we sent a confirm for old value.
402 * However we propagate rc to caller in case option was
403 * mandatory
404 */
405 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800406 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800407 }
408
409 /* generate the confirm [if required] */
410 dccp_feat_flush_confirm(sk);
411
412 return rc;
413}
414
415EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
416
417int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
418 u8 *val, u8 len)
419{
420 u8 t;
421 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800422 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200423 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -0800424 int all_confirmed = 1;
425
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200426 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800427
428 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200429 switch (type) {
430 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
431 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200432 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200433 return 1;
434
435 }
436 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -0800437
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800438 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800439 if (!opt->dccpop_conf && opt->dccpop_type == t &&
440 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200441 found = 1;
442 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
443
Andrea Bittauafe00252006-03-20 17:43:56 -0800444 /* XXX do sanity check */
445
446 opt->dccpop_conf = 1;
447
448 /* We got a confirmation---change the option */
449 dccp_feat_update(sk, opt->dccpop_type,
450 opt->dccpop_feat, *val);
451
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200452 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -0800453 break;
454 }
455
456 if (!opt->dccpop_conf)
457 all_confirmed = 0;
458 }
459
460 /* fix re-transmit timer */
461 /* XXX gotta make sure that no option negotiation occurs during
462 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
463 * on. if all options are confirmed it might kill timer which should
464 * remain alive until close is received.
465 */
466 if (all_confirmed) {
467 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
468 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
469 }
470
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200471 if (!found)
472 dccp_pr_debug("%s(%d, ...) never requested\n",
473 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800474 return 0;
475}
476
477EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
478
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800479void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800480{
Andrea Bittauafe00252006-03-20 17:43:56 -0800481 struct dccp_opt_pend *opt, *next;
482
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800483 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -0800484 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900485 BUG_ON(opt->dccpop_val == NULL);
486 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800487
488 if (opt->dccpop_sc != NULL) {
489 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
490 kfree(opt->dccpop_sc->dccpoc_val);
491 kfree(opt->dccpop_sc);
492 }
493
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900494 kfree(opt);
495 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800496 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800497
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800498 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800499 BUG_ON(opt == NULL);
500 if (opt->dccpop_val != NULL)
501 kfree(opt->dccpop_val);
502 kfree(opt);
503 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800504 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800505}
506
507EXPORT_SYMBOL_GPL(dccp_feat_clean);
508
509/* this is to be called only when a listening sock creates its child. It is
510 * assumed by the function---the confirm is not duplicated, but rather it is
511 * "passed on".
512 */
513int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
514{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800515 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
516 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800517 struct dccp_opt_pend *opt;
518 int rc = 0;
519
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800520 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
521 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800522
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800523 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800524 struct dccp_opt_pend *newopt;
525 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200526 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800527
528 if (val == NULL)
529 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -0800530
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200531 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800532 if (newopt == NULL) {
533 kfree(val);
534 goto out_clean;
535 }
536
537 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -0800538 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800539 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800540
541 /* XXX what happens with backlogs and multiple connections at
542 * once...
543 */
544 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -0800545 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -0800546
547 /* reset state for a new socket */
548 opt->dccpop_conf = 0;
549 }
550
551 /* XXX not doing anything about the conf queue */
552
553out:
554 return rc;
555
556out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800557 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800558 rc = -ENOMEM;
559 goto out;
560}
561
562EXPORT_SYMBOL_GPL(dccp_feat_clone);
563
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800564static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
565 u8 *val, u8 len)
Andrea Bittauafe00252006-03-20 17:43:56 -0800566{
567 int rc = -ENOMEM;
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200568 u8 *copy = kmemdup(val, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800569
570 if (copy != NULL) {
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800571 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800572 if (rc)
573 kfree(copy);
574 }
575 return rc;
576}
577
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800578int dccp_feat_init(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800579{
Andrea Bittauafe00252006-03-20 17:43:56 -0800580 int rc;
581
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800582 INIT_LIST_HEAD(&dmsk->dccpms_pending);
583 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800584
585 /* CCID L */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800586 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800587 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800588 if (rc)
589 goto out;
590
591 /* CCID R */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800592 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800593 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800594 if (rc)
595 goto out;
596
597 /* Ack ratio */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800598 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800599 &dmsk->dccpms_ack_ratio, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800600out:
601 return rc;
602}
603
604EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200605
606#ifdef CONFIG_IP_DCCP_DEBUG
607const char *dccp_feat_typename(const u8 type)
608{
609 switch(type) {
610 case DCCPO_CHANGE_L: return("ChangeL");
611 case DCCPO_CONFIRM_L: return("ConfirmL");
612 case DCCPO_CHANGE_R: return("ChangeR");
613 case DCCPO_CONFIRM_R: return("ConfirmR");
614 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200615 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200616 }
617 return NULL;
618}
619
620EXPORT_SYMBOL_GPL(dccp_feat_typename);
621
622const char *dccp_feat_name(const u8 feat)
623{
624 static const char *feature_names[] = {
625 [DCCPF_RESERVED] = "Reserved",
626 [DCCPF_CCID] = "CCID",
627 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
628 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
629 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
630 [DCCPF_ACK_RATIO] = "Ack Ratio",
631 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
632 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
633 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
634 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
635 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200636 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
637 return feature_names[DCCPF_RESERVED];
638
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200639 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
640 return "CCID-specific";
641
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200642 return feature_names[feat];
643}
644
645EXPORT_SYMBOL_GPL(dccp_feat_name);
646#endif /* CONFIG_IP_DCCP_DEBUG */