blob: 94a81b8e5efbac229fb1dd773b8fe02322c0d249 [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 Renker5c7c9452008-09-04 07:30:19 +020026/* copy constructor, fval must not already contain allocated memory */
27static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
28{
29 fval->sp.len = len;
30 if (fval->sp.len > 0) {
31 fval->sp.vec = kmemdup(val, len, gfp_any());
32 if (fval->sp.vec == NULL) {
33 fval->sp.len = 0;
34 return -ENOBUFS;
35 }
36 }
37 return 0;
38}
39
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -080040int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
41 u8 *val, u8 len, gfp_t gfp)
Andrea Bittauafe00252006-03-20 17:43:56 -080042{
Andrea Bittauafe00252006-03-20 17:43:56 -080043 struct dccp_opt_pend *opt;
44
Gerrit Renkerc02fdc02006-11-14 12:48:10 -020045 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -080046
Gerrit Renkerdd6303d2007-12-13 12:40:40 -020047 if (len > 3) {
Gerrit Renker59348b12006-11-20 18:39:23 -020048 DCCP_WARN("invalid length %d\n", len);
Chris Wright19443172008-05-05 13:50:24 -070049 return -EINVAL;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -020050 }
51 /* XXX add further sanity checks */
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080052
Andrea Bittauafe00252006-03-20 17:43:56 -080053 /* check if that feature is already being negotiated */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080054 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -080055 /* ok we found a negotiation for this option already */
56 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
57 dccp_pr_debug("Replacing old\n");
58 /* replace */
59 BUG_ON(opt->dccpop_val == NULL);
60 kfree(opt->dccpop_val);
61 opt->dccpop_val = val;
62 opt->dccpop_len = len;
63 opt->dccpop_conf = 0;
64 return 0;
65 }
66 }
67
68 /* negotiation for a new feature */
69 opt = kmalloc(sizeof(*opt), gfp);
70 if (opt == NULL)
71 return -ENOMEM;
72
73 opt->dccpop_type = type;
74 opt->dccpop_feat = feature;
75 opt->dccpop_len = len;
76 opt->dccpop_val = val;
77 opt->dccpop_conf = 0;
78 opt->dccpop_sc = NULL;
79
80 BUG_ON(opt->dccpop_val == NULL);
81
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080082 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -080083 return 0;
84}
85
86EXPORT_SYMBOL_GPL(dccp_feat_change);
87
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080088static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
89{
90 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080091 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080092 /* figure out if we are changing our CCID or the peer's */
93 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080094 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080095 struct ccid *new_ccid;
96
97 /* Check if nothing is being changed. */
98 if (ccid_nr == new_ccid_nr)
99 return 0;
100
101 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
102 if (new_ccid == NULL)
103 return -ENOMEM;
104
105 if (rx) {
106 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
107 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800108 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800109 } else {
110 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
111 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800112 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800113 }
114
115 return 0;
116}
117
Andrea Bittauafe00252006-03-20 17:43:56 -0800118static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
119{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200120 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800121
122 switch (feat) {
123 case DCCPF_CCID:
124 return dccp_feat_update_ccid(sk, type, val);
125 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200126 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
127 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800128 break;
129 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800130 return 0;
131}
132
133static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
134 u8 *rpref, u8 rlen)
135{
136 struct dccp_sock *dp = dccp_sk(sk);
137 u8 *spref, slen, *res = NULL;
138 int i, j, rc, agree = 1;
139
140 BUG_ON(rpref == NULL);
141
142 /* check if we are the black sheep */
143 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
144 spref = rpref;
145 slen = rlen;
146 rpref = opt->dccpop_val;
147 rlen = opt->dccpop_len;
148 } else {
149 spref = opt->dccpop_val;
150 slen = opt->dccpop_len;
151 }
152 /*
153 * Now we have server preference list in spref and client preference in
154 * rpref
155 */
156 BUG_ON(spref == NULL);
157 BUG_ON(rpref == NULL);
158
159 /* FIXME sanity check vals */
160
161 /* Are values in any order? XXX Lame "algorithm" here */
Andrea Bittauafe00252006-03-20 17:43:56 -0800162 for (i = 0; i < slen; i++) {
163 for (j = 0; j < rlen; j++) {
164 if (spref[i] == rpref[j]) {
165 res = &spref[i];
166 break;
167 }
168 }
169 if (res)
170 break;
171 }
172
173 /* we didn't agree on anything */
174 if (res == NULL) {
175 /* confirm previous value */
176 switch (opt->dccpop_feat) {
177 case DCCPF_CCID:
178 /* XXX did i get this right? =P */
179 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800180 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800181 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800182 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800183 break;
184
185 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200186 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
187 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800188 return -EFAULT;
189 }
190
191 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
192 agree = 0; /* this is used for mandatory options... */
193 }
194
195 /* need to put result and our preference list */
Andrea Bittauafe00252006-03-20 17:43:56 -0800196 rlen = 1 + opt->dccpop_len;
197 rpref = kmalloc(rlen, GFP_ATOMIC);
198 if (rpref == NULL)
199 return -ENOMEM;
200
201 *rpref = *res;
202 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
203
204 /* put it in the "confirm queue" */
205 if (opt->dccpop_sc == NULL) {
206 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
207 if (opt->dccpop_sc == NULL) {
208 kfree(rpref);
209 return -ENOMEM;
210 }
211 } else {
212 /* recycle the confirm slot */
213 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
214 kfree(opt->dccpop_sc->dccpoc_val);
215 dccp_pr_debug("recycling confirm slot\n");
216 }
217 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
218
219 opt->dccpop_sc->dccpoc_val = rpref;
220 opt->dccpop_sc->dccpoc_len = rlen;
221
222 /* update the option on our side [we are about to send the confirm] */
223 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
224 if (rc) {
225 kfree(opt->dccpop_sc->dccpoc_val);
226 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800227 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800228 return rc;
229 }
230
231 dccp_pr_debug("Will confirm %d\n", *rpref);
232
233 /* say we want to change to X but we just got a confirm X, suppress our
234 * change
235 */
236 if (!opt->dccpop_conf) {
237 if (*opt->dccpop_val == *res)
238 opt->dccpop_conf = 1;
239 dccp_pr_debug("won't ask for change of same feature\n");
240 }
241
242 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
243}
244
245static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
246{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800247 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800248 struct dccp_opt_pend *opt;
249 int rc = 1;
250 u8 t;
251
252 /*
253 * We received a CHANGE. We gotta match it against our own preference
254 * list. If we got a CHANGE_R it means it's a change for us, so we need
255 * to compare our CHANGE_L list.
256 */
257 if (type == DCCPO_CHANGE_L)
258 t = DCCPO_CHANGE_R;
259 else
260 t = DCCPO_CHANGE_L;
261
262 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800263 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800264 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
265 continue;
266
267 /* find the winner from the two preference lists */
268 rc = dccp_feat_reconcile(sk, opt, val, len);
269 break;
270 }
271
272 /* We didn't deal with the change. This can happen if we have no
273 * preference list for the feature. In fact, it just shouldn't
274 * happen---if we understand a feature, we should have a preference list
275 * with at least the default value.
276 */
277 BUG_ON(rc == 1);
278
279 return rc;
280}
281
282static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
283{
284 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800285 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800286 u8 *copy;
287 int rc;
288
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200289 /* NN features must be Change L (sec. 6.3.2) */
290 if (type != DCCPO_CHANGE_L) {
291 dccp_pr_debug("received %s for NN feature %d\n",
292 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800293 return -EFAULT;
294 }
295
296 /* XXX sanity check opt val */
297
298 /* copy option so we can confirm it */
299 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
300 if (opt == NULL)
301 return -ENOMEM;
302
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200303 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800304 if (copy == NULL) {
305 kfree(opt);
306 return -ENOMEM;
307 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800308
309 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
310 opt->dccpop_feat = feature;
311 opt->dccpop_val = copy;
312 opt->dccpop_len = len;
313
314 /* change feature */
315 rc = dccp_feat_update(sk, type, feature, *val);
316 if (rc) {
317 kfree(opt->dccpop_val);
318 kfree(opt);
319 return rc;
320 }
321
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200322 dccp_feat_debug(type, feature, *copy);
323
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800324 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800325
326 return 0;
327}
328
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800329static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
330 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800331{
Andrea Bittauafe00252006-03-20 17:43:56 -0800332 /* XXX check if other confirms for that are queued and recycle slot */
333 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
334
335 if (opt == NULL) {
336 /* XXX what do we do? Ignoring should be fine. It's a change
337 * after all =P
338 */
339 return;
340 }
341
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200342 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -0700343 case DCCPO_CHANGE_L:
344 opt->dccpop_type = DCCPO_CONFIRM_R;
345 break;
346 case DCCPO_CHANGE_R:
347 opt->dccpop_type = DCCPO_CONFIRM_L;
348 break;
349 default:
350 DCCP_WARN("invalid type %d\n", type);
351 kfree(opt);
352 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200353 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800354 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -0800355 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800356 opt->dccpop_len = 0;
357
358 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200359 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
360
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800361 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800362}
363
364static void dccp_feat_flush_confirm(struct sock *sk)
365{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800366 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800367 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800368 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800369
370 if (!yes) {
371 struct dccp_opt_pend *opt;
372
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800373 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800374 if (opt->dccpop_conf) {
375 yes = 1;
376 break;
377 }
378 }
379 }
380
381 if (!yes)
382 return;
383
384 /* OK there is something to confirm... */
385 /* XXX check if packet is in flight? Send delayed ack?? */
386 if (sk->sk_state == DCCP_OPEN)
387 dccp_send_ack(sk);
388}
389
390int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
391{
392 int rc;
393
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200394 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800395
396 /* figure out if it's SP or NN feature */
397 switch (feature) {
398 /* deal with SP features */
399 case DCCPF_CCID:
400 rc = dccp_feat_sp(sk, type, feature, val, len);
401 break;
402
403 /* deal with NN features */
404 case DCCPF_ACK_RATIO:
405 rc = dccp_feat_nn(sk, type, feature, val, len);
406 break;
407
408 /* XXX implement other features */
409 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200410 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
411 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800412 rc = -EFAULT;
413 break;
414 }
415
416 /* check if there were problems changing features */
417 if (rc) {
418 /* If we don't agree on SP, we sent a confirm for old value.
419 * However we propagate rc to caller in case option was
420 * mandatory
421 */
422 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800423 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800424 }
425
426 /* generate the confirm [if required] */
427 dccp_feat_flush_confirm(sk);
428
429 return rc;
430}
431
432EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
433
434int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
435 u8 *val, u8 len)
436{
437 u8 t;
438 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800439 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200440 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -0800441 int all_confirmed = 1;
442
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200443 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800444
445 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200446 switch (type) {
447 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
448 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200449 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200450 return 1;
451
452 }
453 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -0800454
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800455 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800456 if (!opt->dccpop_conf && opt->dccpop_type == t &&
457 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200458 found = 1;
459 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
460
Andrea Bittauafe00252006-03-20 17:43:56 -0800461 /* XXX do sanity check */
462
463 opt->dccpop_conf = 1;
464
465 /* We got a confirmation---change the option */
466 dccp_feat_update(sk, opt->dccpop_type,
467 opt->dccpop_feat, *val);
468
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200469 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -0800470 break;
471 }
472
473 if (!opt->dccpop_conf)
474 all_confirmed = 0;
475 }
476
477 /* fix re-transmit timer */
478 /* XXX gotta make sure that no option negotiation occurs during
479 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
480 * on. if all options are confirmed it might kill timer which should
481 * remain alive until close is received.
482 */
483 if (all_confirmed) {
484 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
485 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
486 }
487
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200488 if (!found)
489 dccp_pr_debug("%s(%d, ...) never requested\n",
490 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800491 return 0;
492}
493
494EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
495
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800496void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800497{
Andrea Bittauafe00252006-03-20 17:43:56 -0800498 struct dccp_opt_pend *opt, *next;
499
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800500 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -0800501 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900502 BUG_ON(opt->dccpop_val == NULL);
503 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800504
505 if (opt->dccpop_sc != NULL) {
506 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
507 kfree(opt->dccpop_sc->dccpoc_val);
508 kfree(opt->dccpop_sc);
509 }
510
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900511 kfree(opt);
512 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800513 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800514
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800515 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800516 BUG_ON(opt == NULL);
517 if (opt->dccpop_val != NULL)
518 kfree(opt->dccpop_val);
519 kfree(opt);
520 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800521 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800522}
523
524EXPORT_SYMBOL_GPL(dccp_feat_clean);
525
526/* this is to be called only when a listening sock creates its child. It is
527 * assumed by the function---the confirm is not duplicated, but rather it is
528 * "passed on".
529 */
530int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
531{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800532 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
533 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800534 struct dccp_opt_pend *opt;
535 int rc = 0;
536
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800537 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
538 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800539
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800540 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800541 struct dccp_opt_pend *newopt;
542 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200543 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800544
545 if (val == NULL)
546 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -0800547
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200548 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800549 if (newopt == NULL) {
550 kfree(val);
551 goto out_clean;
552 }
553
554 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -0800555 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800556 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800557
558 /* XXX what happens with backlogs and multiple connections at
559 * once...
560 */
561 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -0800562 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -0800563
564 /* reset state for a new socket */
565 opt->dccpop_conf = 0;
566 }
567
568 /* XXX not doing anything about the conf queue */
569
570out:
571 return rc;
572
573out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800574 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800575 rc = -ENOMEM;
576 goto out;
577}
578
579EXPORT_SYMBOL_GPL(dccp_feat_clone);
580
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800581static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
582 u8 *val, u8 len)
Andrea Bittauafe00252006-03-20 17:43:56 -0800583{
584 int rc = -ENOMEM;
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200585 u8 *copy = kmemdup(val, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800586
587 if (copy != NULL) {
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800588 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800589 if (rc)
590 kfree(copy);
591 }
592 return rc;
593}
594
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800595int dccp_feat_init(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800596{
Andrea Bittauafe00252006-03-20 17:43:56 -0800597 int rc;
598
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800599 INIT_LIST_HEAD(&dmsk->dccpms_pending);
600 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800601
602 /* CCID L */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800603 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800604 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800605 if (rc)
606 goto out;
607
608 /* CCID R */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800609 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800610 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800611 if (rc)
612 goto out;
613
614 /* Ack ratio */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800615 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800616 &dmsk->dccpms_ack_ratio, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800617out:
618 return rc;
619}
620
621EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200622
623#ifdef CONFIG_IP_DCCP_DEBUG
624const char *dccp_feat_typename(const u8 type)
625{
626 switch(type) {
627 case DCCPO_CHANGE_L: return("ChangeL");
628 case DCCPO_CONFIRM_L: return("ConfirmL");
629 case DCCPO_CHANGE_R: return("ChangeR");
630 case DCCPO_CONFIRM_R: return("ConfirmR");
631 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200632 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200633 }
634 return NULL;
635}
636
637EXPORT_SYMBOL_GPL(dccp_feat_typename);
638
639const char *dccp_feat_name(const u8 feat)
640{
641 static const char *feature_names[] = {
642 [DCCPF_RESERVED] = "Reserved",
643 [DCCPF_CCID] = "CCID",
644 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
645 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
646 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
647 [DCCPF_ACK_RATIO] = "Ack Ratio",
648 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
649 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
650 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
651 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
652 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200653 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
654 return feature_names[DCCPF_RESERVED];
655
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200656 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
657 return "CCID-specific";
658
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200659 return feature_names[feat];
660}
661
662EXPORT_SYMBOL_GPL(dccp_feat_name);
663#endif /* CONFIG_IP_DCCP_DEBUG */