blob: d7468f70544dc460397976afb04ff9a7e9517986 [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 Renkerb4eec202008-09-04 07:30:19 +020026static const struct {
27 u8 feat_num; /* DCCPF_xxx */
28 enum dccp_feat_type rxtx; /* RX or TX */
29 enum dccp_feat_type reconciliation; /* SP or NN */
30 u8 default_value; /* as in 6.4 */
31/*
32 * Lookup table for location and type of features (from RFC 4340/4342)
33 * +--------------------------+----+-----+----+----+---------+-----------+
34 * | Feature | Location | Reconc. | Initial | Section |
35 * | | RX | TX | SP | NN | Value | Reference |
36 * +--------------------------+----+-----+----+----+---------+-----------+
37 * | DCCPF_CCID | | X | X | | 2 | 10 |
38 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
39 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
40 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
41 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
42 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
43 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
44 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
45 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
46 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
47 * +--------------------------+----+-----+----+----+---------+-----------+
48 */
49} dccp_feat_table[] = {
50 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2 },
51 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0 },
52 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100 },
53 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0 },
54 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2 },
55 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0 },
56 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0 },
57 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0 },
58 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0 },
59 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0 },
60};
61#define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
62
63/**
64 * dccp_feat_index - Hash function to map feature number into array position
65 * Returns consecutive array index or -1 if the feature is not understood.
66 */
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 int dccp_feat_default_value(u8 feat_num)
94{
95 int idx = dccp_feat_index(feat_num);
96
97 return idx < 0 ? : dccp_feat_table[idx].default_value;
98}
99
Gerrit Renker5c7c9452008-09-04 07:30:19 +0200100/* copy constructor, fval must not already contain allocated memory */
101static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
102{
103 fval->sp.len = len;
104 if (fval->sp.len > 0) {
105 fval->sp.vec = kmemdup(val, len, gfp_any());
106 if (fval->sp.vec == NULL) {
107 fval->sp.len = 0;
108 return -ENOBUFS;
109 }
110 }
111 return 0;
112}
113
Gerrit Renkerb4eec202008-09-04 07:30:19 +0200114static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
115{
116 if (unlikely(val == NULL))
117 return;
118 if (dccp_feat_type(feat_num) == FEAT_SP)
119 kfree(val->sp.vec);
120 memset(val, 0, sizeof(*val));
121}
122
123static struct dccp_feat_entry *
124 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
125{
126 struct dccp_feat_entry *new;
127 u8 type = dccp_feat_type(original->feat_num);
128
129 if (type == FEAT_UNKNOWN)
130 return NULL;
131
132 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
133 if (new == NULL)
134 return NULL;
135
136 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
137 original->val.sp.vec,
138 original->val.sp.len)) {
139 kfree(new);
140 return NULL;
141 }
142 return new;
143}
144
145static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
146{
147 if (entry != NULL) {
148 dccp_feat_val_destructor(entry->feat_num, &entry->val);
149 kfree(entry);
150 }
151}
152
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800153int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
154 u8 *val, u8 len, gfp_t gfp)
Andrea Bittauafe00252006-03-20 17:43:56 -0800155{
Andrea Bittauafe00252006-03-20 17:43:56 -0800156 struct dccp_opt_pend *opt;
157
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200158 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800159
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200160 if (len > 3) {
Gerrit Renker59348b12006-11-20 18:39:23 -0200161 DCCP_WARN("invalid length %d\n", len);
Chris Wright19443172008-05-05 13:50:24 -0700162 return -EINVAL;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200163 }
164 /* XXX add further sanity checks */
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800165
Andrea Bittauafe00252006-03-20 17:43:56 -0800166 /* check if that feature is already being negotiated */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800167 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800168 /* ok we found a negotiation for this option already */
169 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
170 dccp_pr_debug("Replacing old\n");
171 /* replace */
172 BUG_ON(opt->dccpop_val == NULL);
173 kfree(opt->dccpop_val);
174 opt->dccpop_val = val;
175 opt->dccpop_len = len;
176 opt->dccpop_conf = 0;
177 return 0;
178 }
179 }
180
181 /* negotiation for a new feature */
182 opt = kmalloc(sizeof(*opt), gfp);
183 if (opt == NULL)
184 return -ENOMEM;
185
186 opt->dccpop_type = type;
187 opt->dccpop_feat = feature;
188 opt->dccpop_len = len;
189 opt->dccpop_val = val;
190 opt->dccpop_conf = 0;
191 opt->dccpop_sc = NULL;
192
193 BUG_ON(opt->dccpop_val == NULL);
194
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800195 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800196 return 0;
197}
198
199EXPORT_SYMBOL_GPL(dccp_feat_change);
200
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800201static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
202{
203 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800204 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800205 /* figure out if we are changing our CCID or the peer's */
206 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800207 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800208 struct ccid *new_ccid;
209
210 /* Check if nothing is being changed. */
211 if (ccid_nr == new_ccid_nr)
212 return 0;
213
214 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
215 if (new_ccid == NULL)
216 return -ENOMEM;
217
218 if (rx) {
219 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
220 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800221 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800222 } else {
223 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
224 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800225 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800226 }
227
228 return 0;
229}
230
Andrea Bittauafe00252006-03-20 17:43:56 -0800231static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
232{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200233 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800234
235 switch (feat) {
236 case DCCPF_CCID:
237 return dccp_feat_update_ccid(sk, type, val);
238 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200239 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
240 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800241 break;
242 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800243 return 0;
244}
245
246static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
247 u8 *rpref, u8 rlen)
248{
249 struct dccp_sock *dp = dccp_sk(sk);
250 u8 *spref, slen, *res = NULL;
251 int i, j, rc, agree = 1;
252
253 BUG_ON(rpref == NULL);
254
255 /* check if we are the black sheep */
256 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
257 spref = rpref;
258 slen = rlen;
259 rpref = opt->dccpop_val;
260 rlen = opt->dccpop_len;
261 } else {
262 spref = opt->dccpop_val;
263 slen = opt->dccpop_len;
264 }
265 /*
266 * Now we have server preference list in spref and client preference in
267 * rpref
268 */
269 BUG_ON(spref == NULL);
270 BUG_ON(rpref == NULL);
271
272 /* FIXME sanity check vals */
273
274 /* Are values in any order? XXX Lame "algorithm" here */
Andrea Bittauafe00252006-03-20 17:43:56 -0800275 for (i = 0; i < slen; i++) {
276 for (j = 0; j < rlen; j++) {
277 if (spref[i] == rpref[j]) {
278 res = &spref[i];
279 break;
280 }
281 }
282 if (res)
283 break;
284 }
285
286 /* we didn't agree on anything */
287 if (res == NULL) {
288 /* confirm previous value */
289 switch (opt->dccpop_feat) {
290 case DCCPF_CCID:
291 /* XXX did i get this right? =P */
292 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800293 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800294 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800295 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800296 break;
297
298 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200299 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
300 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800301 return -EFAULT;
302 }
303
304 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
305 agree = 0; /* this is used for mandatory options... */
306 }
307
308 /* need to put result and our preference list */
Andrea Bittauafe00252006-03-20 17:43:56 -0800309 rlen = 1 + opt->dccpop_len;
310 rpref = kmalloc(rlen, GFP_ATOMIC);
311 if (rpref == NULL)
312 return -ENOMEM;
313
314 *rpref = *res;
315 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
316
317 /* put it in the "confirm queue" */
318 if (opt->dccpop_sc == NULL) {
319 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
320 if (opt->dccpop_sc == NULL) {
321 kfree(rpref);
322 return -ENOMEM;
323 }
324 } else {
325 /* recycle the confirm slot */
326 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
327 kfree(opt->dccpop_sc->dccpoc_val);
328 dccp_pr_debug("recycling confirm slot\n");
329 }
330 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
331
332 opt->dccpop_sc->dccpoc_val = rpref;
333 opt->dccpop_sc->dccpoc_len = rlen;
334
335 /* update the option on our side [we are about to send the confirm] */
336 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
337 if (rc) {
338 kfree(opt->dccpop_sc->dccpoc_val);
339 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800340 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800341 return rc;
342 }
343
344 dccp_pr_debug("Will confirm %d\n", *rpref);
345
346 /* say we want to change to X but we just got a confirm X, suppress our
347 * change
348 */
349 if (!opt->dccpop_conf) {
350 if (*opt->dccpop_val == *res)
351 opt->dccpop_conf = 1;
352 dccp_pr_debug("won't ask for change of same feature\n");
353 }
354
355 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
356}
357
358static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
359{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800360 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800361 struct dccp_opt_pend *opt;
362 int rc = 1;
363 u8 t;
364
365 /*
366 * We received a CHANGE. We gotta match it against our own preference
367 * list. If we got a CHANGE_R it means it's a change for us, so we need
368 * to compare our CHANGE_L list.
369 */
370 if (type == DCCPO_CHANGE_L)
371 t = DCCPO_CHANGE_R;
372 else
373 t = DCCPO_CHANGE_L;
374
375 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800376 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800377 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
378 continue;
379
380 /* find the winner from the two preference lists */
381 rc = dccp_feat_reconcile(sk, opt, val, len);
382 break;
383 }
384
385 /* We didn't deal with the change. This can happen if we have no
386 * preference list for the feature. In fact, it just shouldn't
387 * happen---if we understand a feature, we should have a preference list
388 * with at least the default value.
389 */
390 BUG_ON(rc == 1);
391
392 return rc;
393}
394
395static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
396{
397 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800398 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800399 u8 *copy;
400 int rc;
401
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200402 /* NN features must be Change L (sec. 6.3.2) */
403 if (type != DCCPO_CHANGE_L) {
404 dccp_pr_debug("received %s for NN feature %d\n",
405 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800406 return -EFAULT;
407 }
408
409 /* XXX sanity check opt val */
410
411 /* copy option so we can confirm it */
412 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
413 if (opt == NULL)
414 return -ENOMEM;
415
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200416 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800417 if (copy == NULL) {
418 kfree(opt);
419 return -ENOMEM;
420 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800421
422 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
423 opt->dccpop_feat = feature;
424 opt->dccpop_val = copy;
425 opt->dccpop_len = len;
426
427 /* change feature */
428 rc = dccp_feat_update(sk, type, feature, *val);
429 if (rc) {
430 kfree(opt->dccpop_val);
431 kfree(opt);
432 return rc;
433 }
434
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200435 dccp_feat_debug(type, feature, *copy);
436
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800437 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800438
439 return 0;
440}
441
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800442static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
443 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800444{
Andrea Bittauafe00252006-03-20 17:43:56 -0800445 /* XXX check if other confirms for that are queued and recycle slot */
446 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
447
448 if (opt == NULL) {
449 /* XXX what do we do? Ignoring should be fine. It's a change
450 * after all =P
451 */
452 return;
453 }
454
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200455 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -0700456 case DCCPO_CHANGE_L:
457 opt->dccpop_type = DCCPO_CONFIRM_R;
458 break;
459 case DCCPO_CHANGE_R:
460 opt->dccpop_type = DCCPO_CONFIRM_L;
461 break;
462 default:
463 DCCP_WARN("invalid type %d\n", type);
464 kfree(opt);
465 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200466 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800467 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -0800468 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800469 opt->dccpop_len = 0;
470
471 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200472 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
473
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800474 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800475}
476
477static void dccp_feat_flush_confirm(struct sock *sk)
478{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800479 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800480 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800481 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800482
483 if (!yes) {
484 struct dccp_opt_pend *opt;
485
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800486 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800487 if (opt->dccpop_conf) {
488 yes = 1;
489 break;
490 }
491 }
492 }
493
494 if (!yes)
495 return;
496
497 /* OK there is something to confirm... */
498 /* XXX check if packet is in flight? Send delayed ack?? */
499 if (sk->sk_state == DCCP_OPEN)
500 dccp_send_ack(sk);
501}
502
503int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
504{
505 int rc;
506
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200507 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800508
509 /* figure out if it's SP or NN feature */
510 switch (feature) {
511 /* deal with SP features */
512 case DCCPF_CCID:
513 rc = dccp_feat_sp(sk, type, feature, val, len);
514 break;
515
516 /* deal with NN features */
517 case DCCPF_ACK_RATIO:
518 rc = dccp_feat_nn(sk, type, feature, val, len);
519 break;
520
521 /* XXX implement other features */
522 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200523 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
524 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800525 rc = -EFAULT;
526 break;
527 }
528
529 /* check if there were problems changing features */
530 if (rc) {
531 /* If we don't agree on SP, we sent a confirm for old value.
532 * However we propagate rc to caller in case option was
533 * mandatory
534 */
535 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800536 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800537 }
538
539 /* generate the confirm [if required] */
540 dccp_feat_flush_confirm(sk);
541
542 return rc;
543}
544
545EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
546
547int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
548 u8 *val, u8 len)
549{
550 u8 t;
551 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800552 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200553 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -0800554 int all_confirmed = 1;
555
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200556 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800557
558 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200559 switch (type) {
560 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
561 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200562 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200563 return 1;
564
565 }
566 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -0800567
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800568 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800569 if (!opt->dccpop_conf && opt->dccpop_type == t &&
570 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200571 found = 1;
572 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
573
Andrea Bittauafe00252006-03-20 17:43:56 -0800574 /* XXX do sanity check */
575
576 opt->dccpop_conf = 1;
577
578 /* We got a confirmation---change the option */
579 dccp_feat_update(sk, opt->dccpop_type,
580 opt->dccpop_feat, *val);
581
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200582 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -0800583 break;
584 }
585
586 if (!opt->dccpop_conf)
587 all_confirmed = 0;
588 }
589
590 /* fix re-transmit timer */
591 /* XXX gotta make sure that no option negotiation occurs during
592 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
593 * on. if all options are confirmed it might kill timer which should
594 * remain alive until close is received.
595 */
596 if (all_confirmed) {
597 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
598 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
599 }
600
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200601 if (!found)
602 dccp_pr_debug("%s(%d, ...) never requested\n",
603 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800604 return 0;
605}
606
607EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
608
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800609void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800610{
Andrea Bittauafe00252006-03-20 17:43:56 -0800611 struct dccp_opt_pend *opt, *next;
612
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800613 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -0800614 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900615 BUG_ON(opt->dccpop_val == NULL);
616 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800617
618 if (opt->dccpop_sc != NULL) {
619 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
620 kfree(opt->dccpop_sc->dccpoc_val);
621 kfree(opt->dccpop_sc);
622 }
623
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900624 kfree(opt);
625 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800626 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800627
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800628 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800629 BUG_ON(opt == NULL);
630 if (opt->dccpop_val != NULL)
631 kfree(opt->dccpop_val);
632 kfree(opt);
633 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800634 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800635}
636
637EXPORT_SYMBOL_GPL(dccp_feat_clean);
638
639/* this is to be called only when a listening sock creates its child. It is
640 * assumed by the function---the confirm is not duplicated, but rather it is
641 * "passed on".
642 */
643int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
644{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800645 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
646 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800647 struct dccp_opt_pend *opt;
648 int rc = 0;
649
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800650 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
651 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800652
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800653 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800654 struct dccp_opt_pend *newopt;
655 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200656 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800657
658 if (val == NULL)
659 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -0800660
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200661 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800662 if (newopt == NULL) {
663 kfree(val);
664 goto out_clean;
665 }
666
667 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -0800668 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800669 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800670
671 /* XXX what happens with backlogs and multiple connections at
672 * once...
673 */
674 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -0800675 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -0800676
677 /* reset state for a new socket */
678 opt->dccpop_conf = 0;
679 }
680
681 /* XXX not doing anything about the conf queue */
682
683out:
684 return rc;
685
686out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800687 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800688 rc = -ENOMEM;
689 goto out;
690}
691
692EXPORT_SYMBOL_GPL(dccp_feat_clone);
693
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800694static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
695 u8 *val, u8 len)
Andrea Bittauafe00252006-03-20 17:43:56 -0800696{
697 int rc = -ENOMEM;
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200698 u8 *copy = kmemdup(val, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800699
700 if (copy != NULL) {
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800701 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
Andrea Bittauafe00252006-03-20 17:43:56 -0800702 if (rc)
703 kfree(copy);
704 }
705 return rc;
706}
707
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800708int dccp_feat_init(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800709{
Andrea Bittauafe00252006-03-20 17:43:56 -0800710 int rc;
711
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800712 INIT_LIST_HEAD(&dmsk->dccpms_pending);
713 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800714
715 /* CCID L */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800716 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800717 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800718 if (rc)
719 goto out;
720
721 /* CCID R */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800722 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800723 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800724 if (rc)
725 goto out;
726
727 /* Ack ratio */
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800728 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800729 &dmsk->dccpms_ack_ratio, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -0800730out:
731 return rc;
732}
733
734EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200735
736#ifdef CONFIG_IP_DCCP_DEBUG
737const char *dccp_feat_typename(const u8 type)
738{
739 switch(type) {
740 case DCCPO_CHANGE_L: return("ChangeL");
741 case DCCPO_CONFIRM_L: return("ConfirmL");
742 case DCCPO_CHANGE_R: return("ChangeR");
743 case DCCPO_CONFIRM_R: return("ConfirmR");
744 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200745 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200746 }
747 return NULL;
748}
749
750EXPORT_SYMBOL_GPL(dccp_feat_typename);
751
752const char *dccp_feat_name(const u8 feat)
753{
754 static const char *feature_names[] = {
755 [DCCPF_RESERVED] = "Reserved",
756 [DCCPF_CCID] = "CCID",
757 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
758 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
759 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
760 [DCCPF_ACK_RATIO] = "Ack Ratio",
761 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
762 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
763 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
764 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
765 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200766 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
767 return feature_names[DCCPF_RESERVED];
768
Gerrit Renkerb4eec202008-09-04 07:30:19 +0200769 if (feat == DCCPF_SEND_LEV_RATE)
770 return "Send Loss Event Rate";
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200771 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
772 return "CCID-specific";
773
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200774 return feature_names[feat];
775}
776
777EXPORT_SYMBOL_GPL(dccp_feat_name);
778#endif /* CONFIG_IP_DCCP_DEBUG */