blob: 45e36fc7943bfeda83f019586a1773c916999209 [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 Renker7d43d1a2008-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
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -080063int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
64 u8 *val, u8 len, gfp_t gfp)
Andrea Bittauafe00252006-03-20 17:43:56 -080065{
Andrea Bittauafe00252006-03-20 17:43:56 -080066 struct dccp_opt_pend *opt;
67
Gerrit Renkerc02fdc02006-11-14 12:48:10 -020068 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -080069
Gerrit Renkerdd6303d2007-12-13 12:40:40 -020070 if (len > 3) {
Gerrit Renker59348b12006-11-20 18:39:23 -020071 DCCP_WARN("invalid length %d\n", len);
Chris Wright19443172008-05-05 13:50:24 -070072 return -EINVAL;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -020073 }
74 /* XXX add further sanity checks */
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080075
Andrea Bittauafe00252006-03-20 17:43:56 -080076 /* check if that feature is already being negotiated */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -080077 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -080078 /* ok we found a negotiation for this option already */
79 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
80 dccp_pr_debug("Replacing old\n");
81 /* replace */
82 BUG_ON(opt->dccpop_val == NULL);
83 kfree(opt->dccpop_val);
84 opt->dccpop_val = val;
85 opt->dccpop_len = len;
86 opt->dccpop_conf = 0;
87 return 0;
88 }
89 }
90
91 /* negotiation for a new feature */
92 opt = kmalloc(sizeof(*opt), gfp);
93 if (opt == NULL)
94 return -ENOMEM;
95
96 opt->dccpop_type = type;
97 opt->dccpop_feat = feature;
98 opt->dccpop_len = len;
99 opt->dccpop_val = val;
100 opt->dccpop_conf = 0;
101 opt->dccpop_sc = NULL;
102
103 BUG_ON(opt->dccpop_val == NULL);
104
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800105 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800106 return 0;
107}
108
109EXPORT_SYMBOL_GPL(dccp_feat_change);
110
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800111static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
112{
113 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800114 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800115 /* figure out if we are changing our CCID or the peer's */
116 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800117 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800118 struct ccid *new_ccid;
119
120 /* Check if nothing is being changed. */
121 if (ccid_nr == new_ccid_nr)
122 return 0;
123
124 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
125 if (new_ccid == NULL)
126 return -ENOMEM;
127
128 if (rx) {
129 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
130 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800131 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800132 } else {
133 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
134 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800135 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800136 }
137
138 return 0;
139}
140
Andrea Bittauafe00252006-03-20 17:43:56 -0800141static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
142{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200143 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800144
145 switch (feat) {
146 case DCCPF_CCID:
147 return dccp_feat_update_ccid(sk, type, val);
148 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200149 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
150 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800151 break;
152 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800153 return 0;
154}
155
156static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
157 u8 *rpref, u8 rlen)
158{
159 struct dccp_sock *dp = dccp_sk(sk);
160 u8 *spref, slen, *res = NULL;
161 int i, j, rc, agree = 1;
162
163 BUG_ON(rpref == NULL);
164
165 /* check if we are the black sheep */
166 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
167 spref = rpref;
168 slen = rlen;
169 rpref = opt->dccpop_val;
170 rlen = opt->dccpop_len;
171 } else {
172 spref = opt->dccpop_val;
173 slen = opt->dccpop_len;
174 }
175 /*
176 * Now we have server preference list in spref and client preference in
177 * rpref
178 */
179 BUG_ON(spref == NULL);
180 BUG_ON(rpref == NULL);
181
182 /* FIXME sanity check vals */
183
184 /* Are values in any order? XXX Lame "algorithm" here */
Andrea Bittauafe00252006-03-20 17:43:56 -0800185 for (i = 0; i < slen; i++) {
186 for (j = 0; j < rlen; j++) {
187 if (spref[i] == rpref[j]) {
188 res = &spref[i];
189 break;
190 }
191 }
192 if (res)
193 break;
194 }
195
196 /* we didn't agree on anything */
197 if (res == NULL) {
198 /* confirm previous value */
199 switch (opt->dccpop_feat) {
200 case DCCPF_CCID:
201 /* XXX did i get this right? =P */
202 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800203 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800204 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800205 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800206 break;
207
208 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200209 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
210 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800211 return -EFAULT;
212 }
213
214 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
215 agree = 0; /* this is used for mandatory options... */
216 }
217
218 /* need to put result and our preference list */
Andrea Bittauafe00252006-03-20 17:43:56 -0800219 rlen = 1 + opt->dccpop_len;
220 rpref = kmalloc(rlen, GFP_ATOMIC);
221 if (rpref == NULL)
222 return -ENOMEM;
223
224 *rpref = *res;
225 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
226
227 /* put it in the "confirm queue" */
228 if (opt->dccpop_sc == NULL) {
229 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
230 if (opt->dccpop_sc == NULL) {
231 kfree(rpref);
232 return -ENOMEM;
233 }
234 } else {
235 /* recycle the confirm slot */
236 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
237 kfree(opt->dccpop_sc->dccpoc_val);
238 dccp_pr_debug("recycling confirm slot\n");
239 }
240 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
241
242 opt->dccpop_sc->dccpoc_val = rpref;
243 opt->dccpop_sc->dccpoc_len = rlen;
244
245 /* update the option on our side [we are about to send the confirm] */
246 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
247 if (rc) {
248 kfree(opt->dccpop_sc->dccpoc_val);
249 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800250 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800251 return rc;
252 }
253
254 dccp_pr_debug("Will confirm %d\n", *rpref);
255
256 /* say we want to change to X but we just got a confirm X, suppress our
257 * change
258 */
259 if (!opt->dccpop_conf) {
260 if (*opt->dccpop_val == *res)
261 opt->dccpop_conf = 1;
262 dccp_pr_debug("won't ask for change of same feature\n");
263 }
264
265 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
266}
267
268static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
269{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800270 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800271 struct dccp_opt_pend *opt;
272 int rc = 1;
273 u8 t;
274
275 /*
276 * We received a CHANGE. We gotta match it against our own preference
277 * list. If we got a CHANGE_R it means it's a change for us, so we need
278 * to compare our CHANGE_L list.
279 */
280 if (type == DCCPO_CHANGE_L)
281 t = DCCPO_CHANGE_R;
282 else
283 t = DCCPO_CHANGE_L;
284
285 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800286 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800287 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
288 continue;
289
290 /* find the winner from the two preference lists */
291 rc = dccp_feat_reconcile(sk, opt, val, len);
292 break;
293 }
294
295 /* We didn't deal with the change. This can happen if we have no
296 * preference list for the feature. In fact, it just shouldn't
297 * happen---if we understand a feature, we should have a preference list
298 * with at least the default value.
299 */
300 BUG_ON(rc == 1);
301
302 return rc;
303}
304
305static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
306{
307 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800308 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800309 u8 *copy;
310 int rc;
311
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200312 /* NN features must be Change L (sec. 6.3.2) */
313 if (type != DCCPO_CHANGE_L) {
314 dccp_pr_debug("received %s for NN feature %d\n",
315 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800316 return -EFAULT;
317 }
318
319 /* XXX sanity check opt val */
320
321 /* copy option so we can confirm it */
322 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
323 if (opt == NULL)
324 return -ENOMEM;
325
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200326 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800327 if (copy == NULL) {
328 kfree(opt);
329 return -ENOMEM;
330 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800331
332 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
333 opt->dccpop_feat = feature;
334 opt->dccpop_val = copy;
335 opt->dccpop_len = len;
336
337 /* change feature */
338 rc = dccp_feat_update(sk, type, feature, *val);
339 if (rc) {
340 kfree(opt->dccpop_val);
341 kfree(opt);
342 return rc;
343 }
344
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200345 dccp_feat_debug(type, feature, *copy);
346
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800347 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800348
349 return 0;
350}
351
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800352static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
353 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800354{
Andrea Bittauafe00252006-03-20 17:43:56 -0800355 /* XXX check if other confirms for that are queued and recycle slot */
356 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
357
358 if (opt == NULL) {
359 /* XXX what do we do? Ignoring should be fine. It's a change
360 * after all =P
361 */
362 return;
363 }
364
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200365 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -0700366 case DCCPO_CHANGE_L:
367 opt->dccpop_type = DCCPO_CONFIRM_R;
368 break;
369 case DCCPO_CHANGE_R:
370 opt->dccpop_type = DCCPO_CONFIRM_L;
371 break;
372 default:
373 DCCP_WARN("invalid type %d\n", type);
374 kfree(opt);
375 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200376 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800377 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -0800378 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800379 opt->dccpop_len = 0;
380
381 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200382 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
383
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800384 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800385}
386
387static void dccp_feat_flush_confirm(struct sock *sk)
388{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800389 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800390 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800391 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800392
393 if (!yes) {
394 struct dccp_opt_pend *opt;
395
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800396 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800397 if (opt->dccpop_conf) {
398 yes = 1;
399 break;
400 }
401 }
402 }
403
404 if (!yes)
405 return;
406
407 /* OK there is something to confirm... */
408 /* XXX check if packet is in flight? Send delayed ack?? */
409 if (sk->sk_state == DCCP_OPEN)
410 dccp_send_ack(sk);
411}
412
413int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
414{
415 int rc;
416
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200417 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800418
419 /* figure out if it's SP or NN feature */
420 switch (feature) {
421 /* deal with SP features */
422 case DCCPF_CCID:
423 rc = dccp_feat_sp(sk, type, feature, val, len);
424 break;
425
426 /* deal with NN features */
427 case DCCPF_ACK_RATIO:
428 rc = dccp_feat_nn(sk, type, feature, val, len);
429 break;
430
431 /* XXX implement other features */
432 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200433 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
434 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800435 rc = -EFAULT;
436 break;
437 }
438
439 /* check if there were problems changing features */
440 if (rc) {
441 /* If we don't agree on SP, we sent a confirm for old value.
442 * However we propagate rc to caller in case option was
443 * mandatory
444 */
445 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800446 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800447 }
448
449 /* generate the confirm [if required] */
450 dccp_feat_flush_confirm(sk);
451
452 return rc;
453}
454
455EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
456
457int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
458 u8 *val, u8 len)
459{
460 u8 t;
461 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800462 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200463 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -0800464 int all_confirmed = 1;
465
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200466 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800467
468 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200469 switch (type) {
470 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
471 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200472 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200473 return 1;
474
475 }
476 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -0800477
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800478 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800479 if (!opt->dccpop_conf && opt->dccpop_type == t &&
480 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200481 found = 1;
482 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
483
Andrea Bittauafe00252006-03-20 17:43:56 -0800484 /* XXX do sanity check */
485
486 opt->dccpop_conf = 1;
487
488 /* We got a confirmation---change the option */
489 dccp_feat_update(sk, opt->dccpop_type,
490 opt->dccpop_feat, *val);
491
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200492 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -0800493 break;
494 }
495
496 if (!opt->dccpop_conf)
497 all_confirmed = 0;
498 }
499
500 /* fix re-transmit timer */
501 /* XXX gotta make sure that no option negotiation occurs during
502 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
503 * on. if all options are confirmed it might kill timer which should
504 * remain alive until close is received.
505 */
506 if (all_confirmed) {
507 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
508 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
509 }
510
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200511 if (!found)
512 dccp_pr_debug("%s(%d, ...) never requested\n",
513 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800514 return 0;
515}
516
517EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
518
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800519void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800520{
Andrea Bittauafe00252006-03-20 17:43:56 -0800521 struct dccp_opt_pend *opt, *next;
522
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800523 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -0800524 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900525 BUG_ON(opt->dccpop_val == NULL);
526 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800527
528 if (opt->dccpop_sc != NULL) {
529 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
530 kfree(opt->dccpop_sc->dccpoc_val);
531 kfree(opt->dccpop_sc);
532 }
533
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900534 kfree(opt);
535 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800536 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800537
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800538 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800539 BUG_ON(opt == NULL);
540 if (opt->dccpop_val != NULL)
541 kfree(opt->dccpop_val);
542 kfree(opt);
543 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800544 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800545}
546
547EXPORT_SYMBOL_GPL(dccp_feat_clean);
548
549/* this is to be called only when a listening sock creates its child. It is
550 * assumed by the function---the confirm is not duplicated, but rather it is
551 * "passed on".
552 */
553int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
554{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800555 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
556 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800557 struct dccp_opt_pend *opt;
558 int rc = 0;
559
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800560 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
561 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800562
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800563 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800564 struct dccp_opt_pend *newopt;
565 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200566 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800567
568 if (val == NULL)
569 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -0800570
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200571 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800572 if (newopt == NULL) {
573 kfree(val);
574 goto out_clean;
575 }
576
577 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -0800578 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800579 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800580
581 /* XXX what happens with backlogs and multiple connections at
582 * once...
583 */
584 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -0800585 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -0800586
587 /* reset state for a new socket */
588 opt->dccpop_conf = 0;
589 }
590
591 /* XXX not doing anything about the conf queue */
592
593out:
594 return rc;
595
596out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800597 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800598 rc = -ENOMEM;
599 goto out;
600}
601
602EXPORT_SYMBOL_GPL(dccp_feat_clone);
603
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800604static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
605 u8 *val, u8 len)
Andrea Bittauafe00252006-03-20 17:43:56 -0800606{
607 int rc = -ENOMEM;
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200608 u8 *copy = kmemdup(val, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800609
610 if (copy != NULL) {
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800611 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800612 if (rc)
613 kfree(copy);
614 }
615 return rc;
616}
617
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800618int dccp_feat_init(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800619{
Andrea Bittauafe00252006-03-20 17:43:56 -0800620 int rc;
621
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800622 INIT_LIST_HEAD(&dmsk->dccpms_pending);
623 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800624
625 /* CCID L */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800626 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800627 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800628 if (rc)
629 goto out;
630
631 /* CCID R */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800632 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800633 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800634 if (rc)
635 goto out;
636
637 /* Ack ratio */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800638 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800639 &dmsk->dccpms_ack_ratio, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800640out:
641 return rc;
642}
643
644EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200645
646#ifdef CONFIG_IP_DCCP_DEBUG
647const char *dccp_feat_typename(const u8 type)
648{
649 switch(type) {
650 case DCCPO_CHANGE_L: return("ChangeL");
651 case DCCPO_CONFIRM_L: return("ConfirmL");
652 case DCCPO_CHANGE_R: return("ChangeR");
653 case DCCPO_CONFIRM_R: return("ConfirmR");
654 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200655 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200656 }
657 return NULL;
658}
659
660EXPORT_SYMBOL_GPL(dccp_feat_typename);
661
662const char *dccp_feat_name(const u8 feat)
663{
664 static const char *feature_names[] = {
665 [DCCPF_RESERVED] = "Reserved",
666 [DCCPF_CCID] = "CCID",
667 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
668 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
669 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
670 [DCCPF_ACK_RATIO] = "Ack Ratio",
671 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
672 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
673 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
674 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
675 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200676 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
677 return feature_names[DCCPF_RESERVED];
678
Gerrit Renker7d43d1a2008-11-04 23:43:47 -0800679 if (feat == DCCPF_SEND_LEV_RATE)
680 return "Send Loss Event Rate";
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200681 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
682 return "CCID-specific";
683
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200684 return feature_names[feat];
685}
686
687EXPORT_SYMBOL_GPL(dccp_feat_name);
688#endif /* CONFIG_IP_DCCP_DEBUG */