blob: 4c41441473258597cf148c1c2a0eb828d65b258d [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 * -----------
Gerrit Renkerf74e91b2008-11-12 00:42:58 -08009 * o Feature negotiation is coordinated with connection setup (as in TCP), wild
10 * changes of parameters of an established connection are not supported.
Gerrit Renker5cdae192007-12-13 12:41:46 -020011 * o All currently known SP features have 1-byte quantities. If in the future
12 * extensions of RFCs 4340..42 define features with item lengths larger than
13 * one byte, a feature-specific extension of the code will be required.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
Andrea Bittauafe00252006-03-20 17:43:56 -080019 */
20
Andrea Bittauafe00252006-03-20 17:43:56 -080021#include <linux/module.h>
22
Andrea Bittau6ffd30f2006-03-20 19:22:37 -080023#include "ccid.h"
Andrea Bittauafe00252006-03-20 17:43:56 -080024#include "feat.h"
25
26#define DCCP_FEAT_SP_NOAGREE (-123)
27
Gerrit Renker7d43d1a02008-11-04 23:43:47 -080028static const struct {
29 u8 feat_num; /* DCCPF_xxx */
30 enum dccp_feat_type rxtx; /* RX or TX */
31 enum dccp_feat_type reconciliation; /* SP or NN */
32 u8 default_value; /* as in 6.4 */
33/*
34 * Lookup table for location and type of features (from RFC 4340/4342)
35 * +--------------------------+----+-----+----+----+---------+-----------+
36 * | Feature | Location | Reconc. | Initial | Section |
37 * | | RX | TX | SP | NN | Value | Reference |
38 * +--------------------------+----+-----+----+----+---------+-----------+
39 * | DCCPF_CCID | | X | X | | 2 | 10 |
40 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
41 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
42 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
43 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
44 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
45 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
46 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
47 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
48 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
49 * +--------------------------+----+-----+----+----+---------+-----------+
50 */
51} dccp_feat_table[] = {
52 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2 },
53 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0 },
54 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100 },
55 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0 },
56 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2 },
57 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0 },
58 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0 },
59 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0 },
60 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0 },
61 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0 },
62};
63#define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
64
Gerrit Renker61e64732008-11-04 23:54:04 -080065/**
66 * dccp_feat_index - Hash function to map feature number into array position
67 * Returns consecutive array index or -1 if the feature is not understood.
68 */
69static int dccp_feat_index(u8 feat_num)
70{
71 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
72 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
73 return feat_num - 1;
74
75 /*
76 * Other features: add cases for new feature types here after adding
77 * them to the above table.
78 */
79 switch (feat_num) {
80 case DCCPF_SEND_LEV_RATE:
81 return DCCP_FEAT_SUPPORTED_MAX - 1;
82 }
83 return -1;
84}
85
86static u8 dccp_feat_type(u8 feat_num)
87{
88 int idx = dccp_feat_index(feat_num);
89
90 if (idx < 0)
91 return FEAT_UNKNOWN;
92 return dccp_feat_table[idx].reconciliation;
93}
94
Gerrit Renkere8ef9672008-11-12 00:43:40 -080095static int dccp_feat_default_value(u8 feat_num)
96{
97 int idx = dccp_feat_index(feat_num);
98 /*
99 * There are no default values for unknown features, so encountering a
100 * negative index here indicates a serious problem somewhere else.
101 */
102 DCCP_BUG_ON(idx < 0);
103
104 return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
105}
106
Gerrit Renkerac757732008-11-04 23:55:49 -0800107/* copy constructor, fval must not already contain allocated memory */
108static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
109{
110 fval->sp.len = len;
111 if (fval->sp.len > 0) {
112 fval->sp.vec = kmemdup(val, len, gfp_any());
113 if (fval->sp.vec == NULL) {
114 fval->sp.len = 0;
115 return -ENOBUFS;
116 }
117 }
118 return 0;
119}
120
Gerrit Renker61e64732008-11-04 23:54:04 -0800121static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
122{
123 if (unlikely(val == NULL))
124 return;
125 if (dccp_feat_type(feat_num) == FEAT_SP)
126 kfree(val->sp.vec);
127 memset(val, 0, sizeof(*val));
128}
129
Gerrit Renkerac757732008-11-04 23:55:49 -0800130static struct dccp_feat_entry *
131 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
132{
133 struct dccp_feat_entry *new;
134 u8 type = dccp_feat_type(original->feat_num);
135
136 if (type == FEAT_UNKNOWN)
137 return NULL;
138
139 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
140 if (new == NULL)
141 return NULL;
142
143 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
144 original->val.sp.vec,
145 original->val.sp.len)) {
146 kfree(new);
147 return NULL;
148 }
149 return new;
150}
151
Gerrit Renker61e64732008-11-04 23:54:04 -0800152static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
153{
154 if (entry != NULL) {
155 dccp_feat_val_destructor(entry->feat_num, &entry->val);
156 kfree(entry);
157 }
158}
159
160/*
161 * List management functions
162 *
163 * Feature negotiation lists rely on and maintain the following invariants:
164 * - each feat_num in the list is known, i.e. we know its type and default value
165 * - each feat_num/is_local combination is unique (old entries are overwritten)
166 * - SP values are always freshly allocated
167 * - list is sorted in increasing order of feature number (faster lookup)
168 */
169
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800170/**
171 * dccp_feat_entry_new - Central list update routine (called by all others)
172 * @head: list to add to
173 * @feat: feature number
174 * @local: whether the local (1) or remote feature with number @feat is meant
175 * This is the only constructor and serves to ensure the above invariants.
176 */
177static struct dccp_feat_entry *
178 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
179{
180 struct dccp_feat_entry *entry;
181
182 list_for_each_entry(entry, head, node)
183 if (entry->feat_num == feat && entry->is_local == local) {
184 dccp_feat_val_destructor(entry->feat_num, &entry->val);
185 return entry;
186 } else if (entry->feat_num > feat) {
187 head = &entry->node;
188 break;
189 }
190
191 entry = kmalloc(sizeof(*entry), gfp_any());
192 if (entry != NULL) {
193 entry->feat_num = feat;
194 entry->is_local = local;
195 list_add_tail(&entry->node, head);
196 }
197 return entry;
198}
199
200/**
201 * dccp_feat_push_change - Add/overwrite a Change option in the list
202 * @fn_list: feature-negotiation list to update
203 * @feat: one of %dccp_feature_numbers
204 * @local: whether local (1) or remote (0) @feat_num is meant
205 * @needs_mandatory: whether to use Mandatory feature negotiation options
206 * @fval: pointer to NN/SP value to be inserted (will be copied)
207 */
208static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
209 u8 mandatory, dccp_feat_val *fval)
210{
211 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
212
213 if (new == NULL)
214 return -ENOMEM;
215
216 new->feat_num = feat;
217 new->is_local = local;
218 new->state = FEAT_INITIALISING;
219 new->needs_confirm = 0;
220 new->empty_confirm = 0;
221 new->val = *fval;
222 new->needs_mandatory = mandatory;
223
224 return 0;
225}
226
Gerrit Renker61e64732008-11-04 23:54:04 -0800227static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
228{
229 list_del(&entry->node);
230 dccp_feat_entry_destructor(entry);
231}
232
233void dccp_feat_list_purge(struct list_head *fn_list)
234{
235 struct dccp_feat_entry *entry, *next;
236
237 list_for_each_entry_safe(entry, next, fn_list, node)
238 dccp_feat_entry_destructor(entry);
239 INIT_LIST_HEAD(fn_list);
240}
241EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
242
Gerrit Renkerac757732008-11-04 23:55:49 -0800243/* generate @to as full clone of @from - @to must not contain any nodes */
244int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
245{
246 struct dccp_feat_entry *entry, *new;
247
248 INIT_LIST_HEAD(to);
249 list_for_each_entry(entry, from, node) {
250 new = dccp_feat_clone_entry(entry);
251 if (new == NULL)
252 goto cloning_failed;
253 list_add_tail(&new->node, to);
254 }
255 return 0;
256
257cloning_failed:
258 dccp_feat_list_purge(to);
259 return -ENOMEM;
260}
261
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800262static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
263{
264 switch (feat_num) {
265 case DCCPF_ACK_RATIO:
266 return val <= DCCPF_ACK_RATIO_MAX;
267 case DCCPF_SEQUENCE_WINDOW:
268 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
269 }
270 return 0; /* feature unknown - so we can't tell */
271}
272
273/* check that SP values are within the ranges defined in RFC 4340 */
274static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
275{
276 switch (feat_num) {
277 case DCCPF_CCID:
278 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
279 /* Type-check Boolean feature values: */
280 case DCCPF_SHORT_SEQNOS:
281 case DCCPF_ECN_INCAPABLE:
282 case DCCPF_SEND_ACK_VECTOR:
283 case DCCPF_SEND_NDP_COUNT:
284 case DCCPF_DATA_CHECKSUM:
285 case DCCPF_SEND_LEV_RATE:
286 return val < 2;
287 case DCCPF_MIN_CSUM_COVER:
288 return val < 16;
289 }
290 return 0; /* feature unknown */
291}
292
293static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
294{
295 if (sp_list == NULL || sp_len < 1)
296 return 0;
297 while (sp_len--)
298 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
299 return 0;
300 return 1;
301}
302
303/**
304 * __feat_register_nn - Register new NN value on socket
305 * @fn: feature-negotiation list to register with
306 * @feat: an NN feature from %dccp_feature_numbers
307 * @mandatory: use Mandatory option if 1
308 * @nn_val: value to register (restricted to 4 bytes)
309 * Note that NN features are local by definition (RFC 4340, 6.3.2).
310 */
311static int __feat_register_nn(struct list_head *fn, u8 feat,
312 u8 mandatory, u64 nn_val)
313{
314 dccp_feat_val fval = { .nn = nn_val };
315
316 if (dccp_feat_type(feat) != FEAT_NN ||
317 !dccp_feat_is_valid_nn_val(feat, nn_val))
318 return -EINVAL;
319
320 /* Don't bother with default values, they will be activated anyway. */
321 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
322 return 0;
323
324 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
325}
326
327/**
328 * __feat_register_sp - Register new SP value/list on socket
329 * @fn: feature-negotiation list to register with
330 * @feat: an SP feature from %dccp_feature_numbers
331 * @is_local: whether the local (1) or the remote (0) @feat is meant
332 * @mandatory: use Mandatory option if 1
333 * @sp_val: SP value followed by optional preference list
334 * @sp_len: length of @sp_val in bytes
335 */
336static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
337 u8 mandatory, u8 const *sp_val, u8 sp_len)
338{
339 dccp_feat_val fval;
340
341 if (dccp_feat_type(feat) != FEAT_SP ||
342 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
343 return -EINVAL;
344
Gerrit Renkerd90ebcb2008-11-12 00:47:26 -0800345 /* Avoid negotiating alien CCIDs by only advertising supported ones */
346 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
347 return -EOPNOTSUPP;
348
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800349 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
350 return -ENOMEM;
351
352 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
353}
354
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800355int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
356 u8 *val, u8 len, gfp_t gfp)
Andrea Bittauafe00252006-03-20 17:43:56 -0800357{
Andrea Bittauafe00252006-03-20 17:43:56 -0800358 struct dccp_opt_pend *opt;
359
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200360 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800361
Gerrit Renkerdd6303d2007-12-13 12:40:40 -0200362 if (len > 3) {
Gerrit Renker59348b12006-11-20 18:39:23 -0200363 DCCP_WARN("invalid length %d\n", len);
Chris Wright19443172008-05-05 13:50:24 -0700364 return -EINVAL;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200365 }
366 /* XXX add further sanity checks */
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800367
Andrea Bittauafe00252006-03-20 17:43:56 -0800368 /* check if that feature is already being negotiated */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800369 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800370 /* ok we found a negotiation for this option already */
371 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
372 dccp_pr_debug("Replacing old\n");
373 /* replace */
374 BUG_ON(opt->dccpop_val == NULL);
375 kfree(opt->dccpop_val);
376 opt->dccpop_val = val;
377 opt->dccpop_len = len;
378 opt->dccpop_conf = 0;
379 return 0;
380 }
381 }
382
383 /* negotiation for a new feature */
384 opt = kmalloc(sizeof(*opt), gfp);
385 if (opt == NULL)
386 return -ENOMEM;
387
388 opt->dccpop_type = type;
389 opt->dccpop_feat = feature;
390 opt->dccpop_len = len;
391 opt->dccpop_val = val;
392 opt->dccpop_conf = 0;
393 opt->dccpop_sc = NULL;
394
395 BUG_ON(opt->dccpop_val == NULL);
396
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800397 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800398 return 0;
399}
400
401EXPORT_SYMBOL_GPL(dccp_feat_change);
402
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800403/*
404 * Tracking features whose value depend on the choice of CCID
405 *
406 * This is designed with an extension in mind so that a list walk could be done
407 * before activating any features. However, the existing framework was found to
408 * work satisfactorily up until now, the automatic verification is left open.
409 * When adding new CCIDs, add a corresponding dependency table here.
410 */
411static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
412{
413 static const struct ccid_dependency ccid2_dependencies[2][2] = {
414 /*
415 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
416 * feature and Send Ack Vector is an RX feature, `is_local'
417 * needs to be reversed.
418 */
419 { /* Dependencies of the receiver-side (remote) CCID2 */
420 {
421 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
422 .is_local = true,
423 .is_mandatory = true,
424 .val = 1
425 },
426 { 0, 0, 0, 0 }
427 },
428 { /* Dependencies of the sender-side (local) CCID2 */
429 {
430 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
431 .is_local = false,
432 .is_mandatory = true,
433 .val = 1
434 },
435 { 0, 0, 0, 0 }
436 }
437 };
438 static const struct ccid_dependency ccid3_dependencies[2][5] = {
439 { /*
440 * Dependencies of the receiver-side CCID3
441 */
442 { /* locally disable Ack Vectors */
443 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
444 .is_local = true,
445 .is_mandatory = false,
446 .val = 0
447 },
448 { /* see below why Send Loss Event Rate is on */
449 .dependent_feat = DCCPF_SEND_LEV_RATE,
450 .is_local = true,
451 .is_mandatory = true,
452 .val = 1
453 },
454 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
455 .dependent_feat = DCCPF_SEND_NDP_COUNT,
456 .is_local = false,
457 .is_mandatory = true,
458 .val = 1
459 },
460 { 0, 0, 0, 0 },
461 },
462 { /*
463 * CCID3 at the TX side: we request that the HC-receiver
464 * will not send Ack Vectors (they will be ignored, so
465 * Mandatory is not set); we enable Send Loss Event Rate
466 * (Mandatory since the implementation does not support
467 * the Loss Intervals option of RFC 4342, 8.6).
468 * The last two options are for peer's information only.
469 */
470 {
471 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
472 .is_local = false,
473 .is_mandatory = false,
474 .val = 0
475 },
476 {
477 .dependent_feat = DCCPF_SEND_LEV_RATE,
478 .is_local = false,
479 .is_mandatory = true,
480 .val = 1
481 },
482 { /* this CCID does not support Ack Ratio */
483 .dependent_feat = DCCPF_ACK_RATIO,
484 .is_local = true,
485 .is_mandatory = false,
486 .val = 0
487 },
488 { /* tell receiver we are sending NDP counts */
489 .dependent_feat = DCCPF_SEND_NDP_COUNT,
490 .is_local = true,
491 .is_mandatory = false,
492 .val = 1
493 },
494 { 0, 0, 0, 0 }
495 }
496 };
497 switch (ccid) {
498 case DCCPC_CCID2:
499 return ccid2_dependencies[is_local];
500 case DCCPC_CCID3:
501 return ccid3_dependencies[is_local];
502 default:
503 return NULL;
504 }
505}
506
507/**
508 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
509 * @fn: feature-negotiation list to update
510 * @id: CCID number to track
511 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
512 * This function needs to be called after registering all other features.
513 */
514static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
515{
516 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
517 int i, rc = (table == NULL);
518
519 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
520 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
521 rc = __feat_register_sp(fn, table[i].dependent_feat,
522 table[i].is_local,
523 table[i].is_mandatory,
524 &table[i].val, 1);
525 else
526 rc = __feat_register_nn(fn, table[i].dependent_feat,
527 table[i].is_mandatory,
528 table[i].val);
529 return rc;
530}
531
532/**
533 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
534 * @dp: client or listening socket (settings will be inherited)
535 * This is called after all registrations (socket initialisation, sysctls, and
536 * sockopt calls), and before sending the first packet containing Change options
537 * (ie. client-Request or server-Response), to ensure internal consistency.
538 */
539int dccp_feat_finalise_settings(struct dccp_sock *dp)
540{
541 struct list_head *fn = &dp->dccps_featneg;
542 struct dccp_feat_entry *entry;
543 int i = 2, ccids[2] = { -1, -1 };
544
545 /*
546 * Propagating CCIDs:
547 * 1) not useful to propagate CCID settings if this host advertises more
548 * than one CCID: the choice of CCID may still change - if this is
549 * the client, or if this is the server and the client sends
550 * singleton CCID values.
551 * 2) since is that propagate_ccid changes the list, we defer changing
552 * the sorted list until after the traversal.
553 */
554 list_for_each_entry(entry, fn, node)
555 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
556 ccids[entry->is_local] = entry->val.sp.vec[0];
557 while (i--)
558 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
559 return -1;
560 return 0;
561}
562
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800563static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
564{
565 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800566 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800567 /* figure out if we are changing our CCID or the peer's */
568 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800569 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800570 struct ccid *new_ccid;
571
572 /* Check if nothing is being changed. */
573 if (ccid_nr == new_ccid_nr)
574 return 0;
575
576 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
577 if (new_ccid == NULL)
578 return -ENOMEM;
579
580 if (rx) {
581 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
582 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800583 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800584 } else {
585 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
586 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800587 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800588 }
589
590 return 0;
591}
592
Andrea Bittauafe00252006-03-20 17:43:56 -0800593static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
594{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200595 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800596
597 switch (feat) {
598 case DCCPF_CCID:
599 return dccp_feat_update_ccid(sk, type, val);
600 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200601 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
602 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800603 break;
604 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800605 return 0;
606}
607
608static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
609 u8 *rpref, u8 rlen)
610{
611 struct dccp_sock *dp = dccp_sk(sk);
612 u8 *spref, slen, *res = NULL;
613 int i, j, rc, agree = 1;
614
615 BUG_ON(rpref == NULL);
616
617 /* check if we are the black sheep */
618 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
619 spref = rpref;
620 slen = rlen;
621 rpref = opt->dccpop_val;
622 rlen = opt->dccpop_len;
623 } else {
624 spref = opt->dccpop_val;
625 slen = opt->dccpop_len;
626 }
627 /*
628 * Now we have server preference list in spref and client preference in
629 * rpref
630 */
631 BUG_ON(spref == NULL);
632 BUG_ON(rpref == NULL);
633
634 /* FIXME sanity check vals */
635
636 /* Are values in any order? XXX Lame "algorithm" here */
Andrea Bittauafe00252006-03-20 17:43:56 -0800637 for (i = 0; i < slen; i++) {
638 for (j = 0; j < rlen; j++) {
639 if (spref[i] == rpref[j]) {
640 res = &spref[i];
641 break;
642 }
643 }
644 if (res)
645 break;
646 }
647
648 /* we didn't agree on anything */
649 if (res == NULL) {
650 /* confirm previous value */
651 switch (opt->dccpop_feat) {
652 case DCCPF_CCID:
653 /* XXX did i get this right? =P */
654 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800655 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800656 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800657 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800658 break;
659
660 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200661 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
662 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800663 return -EFAULT;
664 }
665
666 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
667 agree = 0; /* this is used for mandatory options... */
668 }
669
670 /* need to put result and our preference list */
Andrea Bittauafe00252006-03-20 17:43:56 -0800671 rlen = 1 + opt->dccpop_len;
672 rpref = kmalloc(rlen, GFP_ATOMIC);
673 if (rpref == NULL)
674 return -ENOMEM;
675
676 *rpref = *res;
677 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
678
679 /* put it in the "confirm queue" */
680 if (opt->dccpop_sc == NULL) {
681 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
682 if (opt->dccpop_sc == NULL) {
683 kfree(rpref);
684 return -ENOMEM;
685 }
686 } else {
687 /* recycle the confirm slot */
688 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
689 kfree(opt->dccpop_sc->dccpoc_val);
690 dccp_pr_debug("recycling confirm slot\n");
691 }
692 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
693
694 opt->dccpop_sc->dccpoc_val = rpref;
695 opt->dccpop_sc->dccpoc_len = rlen;
696
697 /* update the option on our side [we are about to send the confirm] */
698 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
699 if (rc) {
700 kfree(opt->dccpop_sc->dccpoc_val);
701 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800702 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800703 return rc;
704 }
705
706 dccp_pr_debug("Will confirm %d\n", *rpref);
707
708 /* say we want to change to X but we just got a confirm X, suppress our
709 * change
710 */
711 if (!opt->dccpop_conf) {
712 if (*opt->dccpop_val == *res)
713 opt->dccpop_conf = 1;
714 dccp_pr_debug("won't ask for change of same feature\n");
715 }
716
717 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
718}
719
720static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
721{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800722 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800723 struct dccp_opt_pend *opt;
724 int rc = 1;
725 u8 t;
726
727 /*
728 * We received a CHANGE. We gotta match it against our own preference
729 * list. If we got a CHANGE_R it means it's a change for us, so we need
730 * to compare our CHANGE_L list.
731 */
732 if (type == DCCPO_CHANGE_L)
733 t = DCCPO_CHANGE_R;
734 else
735 t = DCCPO_CHANGE_L;
736
737 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800738 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800739 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
740 continue;
741
742 /* find the winner from the two preference lists */
743 rc = dccp_feat_reconcile(sk, opt, val, len);
744 break;
745 }
746
747 /* We didn't deal with the change. This can happen if we have no
748 * preference list for the feature. In fact, it just shouldn't
749 * happen---if we understand a feature, we should have a preference list
750 * with at least the default value.
751 */
752 BUG_ON(rc == 1);
753
754 return rc;
755}
756
757static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
758{
759 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800760 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800761 u8 *copy;
762 int rc;
763
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200764 /* NN features must be Change L (sec. 6.3.2) */
765 if (type != DCCPO_CHANGE_L) {
766 dccp_pr_debug("received %s for NN feature %d\n",
767 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800768 return -EFAULT;
769 }
770
771 /* XXX sanity check opt val */
772
773 /* copy option so we can confirm it */
774 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
775 if (opt == NULL)
776 return -ENOMEM;
777
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200778 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800779 if (copy == NULL) {
780 kfree(opt);
781 return -ENOMEM;
782 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800783
784 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
785 opt->dccpop_feat = feature;
786 opt->dccpop_val = copy;
787 opt->dccpop_len = len;
788
789 /* change feature */
790 rc = dccp_feat_update(sk, type, feature, *val);
791 if (rc) {
792 kfree(opt->dccpop_val);
793 kfree(opt);
794 return rc;
795 }
796
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200797 dccp_feat_debug(type, feature, *copy);
798
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800799 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800800
801 return 0;
802}
803
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800804static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
805 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800806{
Andrea Bittauafe00252006-03-20 17:43:56 -0800807 /* XXX check if other confirms for that are queued and recycle slot */
808 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
809
810 if (opt == NULL) {
811 /* XXX what do we do? Ignoring should be fine. It's a change
812 * after all =P
813 */
814 return;
815 }
816
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200817 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -0700818 case DCCPO_CHANGE_L:
819 opt->dccpop_type = DCCPO_CONFIRM_R;
820 break;
821 case DCCPO_CHANGE_R:
822 opt->dccpop_type = DCCPO_CONFIRM_L;
823 break;
824 default:
825 DCCP_WARN("invalid type %d\n", type);
826 kfree(opt);
827 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200828 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800829 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -0800830 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800831 opt->dccpop_len = 0;
832
833 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200834 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
835
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800836 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800837}
838
839static void dccp_feat_flush_confirm(struct sock *sk)
840{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800841 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800842 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800843 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800844
845 if (!yes) {
846 struct dccp_opt_pend *opt;
847
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800848 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800849 if (opt->dccpop_conf) {
850 yes = 1;
851 break;
852 }
853 }
854 }
855
856 if (!yes)
857 return;
858
859 /* OK there is something to confirm... */
860 /* XXX check if packet is in flight? Send delayed ack?? */
861 if (sk->sk_state == DCCP_OPEN)
862 dccp_send_ack(sk);
863}
864
865int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
866{
867 int rc;
868
Gerrit Renkerf74e91b2008-11-12 00:42:58 -0800869 /* Ignore Change requests other than during connection setup */
870 if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
871 return 0;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200872 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800873
874 /* figure out if it's SP or NN feature */
875 switch (feature) {
876 /* deal with SP features */
877 case DCCPF_CCID:
878 rc = dccp_feat_sp(sk, type, feature, val, len);
879 break;
880
881 /* deal with NN features */
882 case DCCPF_ACK_RATIO:
883 rc = dccp_feat_nn(sk, type, feature, val, len);
884 break;
885
886 /* XXX implement other features */
887 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200888 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
889 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800890 rc = -EFAULT;
891 break;
892 }
893
894 /* check if there were problems changing features */
895 if (rc) {
896 /* If we don't agree on SP, we sent a confirm for old value.
897 * However we propagate rc to caller in case option was
898 * mandatory
899 */
900 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800901 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800902 }
903
904 /* generate the confirm [if required] */
905 dccp_feat_flush_confirm(sk);
906
907 return rc;
908}
909
910EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
911
912int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
913 u8 *val, u8 len)
914{
915 u8 t;
916 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800917 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200918 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -0800919 int all_confirmed = 1;
920
Gerrit Renkerf74e91b2008-11-12 00:42:58 -0800921 /* Ignore Confirm options other than during connection setup */
922 if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
923 return 0;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200924 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800925
926 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200927 switch (type) {
928 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
929 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200930 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200931 return 1;
932
933 }
934 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -0800935
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800936 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800937 if (!opt->dccpop_conf && opt->dccpop_type == t &&
938 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200939 found = 1;
940 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
941
Andrea Bittauafe00252006-03-20 17:43:56 -0800942 /* XXX do sanity check */
943
944 opt->dccpop_conf = 1;
945
946 /* We got a confirmation---change the option */
947 dccp_feat_update(sk, opt->dccpop_type,
948 opt->dccpop_feat, *val);
949
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200950 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -0800951 break;
952 }
953
954 if (!opt->dccpop_conf)
955 all_confirmed = 0;
956 }
957
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200958 if (!found)
959 dccp_pr_debug("%s(%d, ...) never requested\n",
960 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800961 return 0;
962}
963
964EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
965
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800966void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800967{
Andrea Bittauafe00252006-03-20 17:43:56 -0800968 struct dccp_opt_pend *opt, *next;
969
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800970 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -0800971 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900972 BUG_ON(opt->dccpop_val == NULL);
973 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800974
975 if (opt->dccpop_sc != NULL) {
976 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
977 kfree(opt->dccpop_sc->dccpoc_val);
978 kfree(opt->dccpop_sc);
979 }
980
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900981 kfree(opt);
982 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800983 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -0800984
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800985 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800986 BUG_ON(opt == NULL);
987 if (opt->dccpop_val != NULL)
988 kfree(opt->dccpop_val);
989 kfree(opt);
990 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800991 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800992}
993
994EXPORT_SYMBOL_GPL(dccp_feat_clean);
995
996/* this is to be called only when a listening sock creates its child. It is
997 * assumed by the function---the confirm is not duplicated, but rather it is
998 * "passed on".
999 */
1000int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
1001{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001002 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
1003 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001004 struct dccp_opt_pend *opt;
1005 int rc = 0;
1006
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001007 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
1008 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -08001009
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001010 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -08001011 struct dccp_opt_pend *newopt;
1012 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -02001013 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -08001014
1015 if (val == NULL)
1016 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -08001017
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -02001018 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -08001019 if (newopt == NULL) {
1020 kfree(val);
1021 goto out_clean;
1022 }
1023
1024 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -08001025 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001026 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -08001027
1028 /* XXX what happens with backlogs and multiple connections at
1029 * once...
1030 */
1031 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -08001032 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -08001033
1034 /* reset state for a new socket */
1035 opt->dccpop_conf = 0;
1036 }
1037
1038 /* XXX not doing anything about the conf queue */
1039
1040out:
1041 return rc;
1042
1043out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -08001044 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001045 rc = -ENOMEM;
1046 goto out;
1047}
1048
1049EXPORT_SYMBOL_GPL(dccp_feat_clone);
1050
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001051int dccp_feat_init(struct sock *sk)
Andrea Bittauafe00252006-03-20 17:43:56 -08001052{
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001053 struct dccp_sock *dp = dccp_sk(sk);
1054 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001055 int rc;
1056
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001057 INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
1058 INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
Andrea Bittauafe00252006-03-20 17:43:56 -08001059
1060 /* CCID L */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001061 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
1062 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -08001063 if (rc)
1064 goto out;
1065
1066 /* CCID R */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001067 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
1068 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -08001069 if (rc)
1070 goto out;
1071
1072 /* Ack ratio */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001073 rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
1074 dmsk->dccpms_ack_ratio);
Andrea Bittauafe00252006-03-20 17:43:56 -08001075out:
1076 return rc;
1077}
1078
1079EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001080
1081#ifdef CONFIG_IP_DCCP_DEBUG
1082const char *dccp_feat_typename(const u8 type)
1083{
1084 switch(type) {
1085 case DCCPO_CHANGE_L: return("ChangeL");
1086 case DCCPO_CONFIRM_L: return("ConfirmL");
1087 case DCCPO_CHANGE_R: return("ChangeR");
1088 case DCCPO_CONFIRM_R: return("ConfirmR");
1089 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -02001090 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001091 }
1092 return NULL;
1093}
1094
1095EXPORT_SYMBOL_GPL(dccp_feat_typename);
1096
1097const char *dccp_feat_name(const u8 feat)
1098{
1099 static const char *feature_names[] = {
1100 [DCCPF_RESERVED] = "Reserved",
1101 [DCCPF_CCID] = "CCID",
1102 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
1103 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
1104 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
1105 [DCCPF_ACK_RATIO] = "Ack Ratio",
1106 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
1107 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
1108 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
1109 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
1110 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -02001111 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1112 return feature_names[DCCPF_RESERVED];
1113
Gerrit Renker7d43d1a02008-11-04 23:43:47 -08001114 if (feat == DCCPF_SEND_LEV_RATE)
1115 return "Send Loss Event Rate";
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001116 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1117 return "CCID-specific";
1118
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001119 return feature_names[feat];
1120}
1121
1122EXPORT_SYMBOL_GPL(dccp_feat_name);
1123#endif /* CONFIG_IP_DCCP_DEBUG */