blob: 2c2216f64b1b6aaccaece8e8644cad0cfc16e13c [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 Renker7d43d1a2008-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 */
Gerrit Renker0c116832008-11-16 22:49:52 -0800169static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
170 u8 feat_num, bool is_local)
171{
172 struct dccp_feat_entry *entry;
173
Gerrit Renker3d3e35a2008-11-20 01:03:08 -0800174 list_for_each_entry(entry, fn_list, node) {
Gerrit Renker0c116832008-11-16 22:49:52 -0800175 if (entry->feat_num == feat_num && entry->is_local == is_local)
176 return entry;
177 else if (entry->feat_num > feat_num)
178 break;
Gerrit Renker3d3e35a2008-11-20 01:03:08 -0800179 }
Gerrit Renker0c116832008-11-16 22:49:52 -0800180 return NULL;
181}
Gerrit Renker61e64732008-11-04 23:54:04 -0800182
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800183/**
184 * dccp_feat_entry_new - Central list update routine (called by all others)
185 * @head: list to add to
186 * @feat: feature number
187 * @local: whether the local (1) or remote feature with number @feat is meant
188 * This is the only constructor and serves to ensure the above invariants.
189 */
190static struct dccp_feat_entry *
191 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
192{
193 struct dccp_feat_entry *entry;
194
195 list_for_each_entry(entry, head, node)
196 if (entry->feat_num == feat && entry->is_local == local) {
197 dccp_feat_val_destructor(entry->feat_num, &entry->val);
198 return entry;
199 } else if (entry->feat_num > feat) {
200 head = &entry->node;
201 break;
202 }
203
204 entry = kmalloc(sizeof(*entry), gfp_any());
205 if (entry != NULL) {
206 entry->feat_num = feat;
207 entry->is_local = local;
208 list_add_tail(&entry->node, head);
209 }
210 return entry;
211}
212
213/**
214 * dccp_feat_push_change - Add/overwrite a Change option in the list
215 * @fn_list: feature-negotiation list to update
216 * @feat: one of %dccp_feature_numbers
217 * @local: whether local (1) or remote (0) @feat_num is meant
218 * @needs_mandatory: whether to use Mandatory feature negotiation options
219 * @fval: pointer to NN/SP value to be inserted (will be copied)
220 */
221static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
222 u8 mandatory, dccp_feat_val *fval)
223{
224 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
225
226 if (new == NULL)
227 return -ENOMEM;
228
229 new->feat_num = feat;
230 new->is_local = local;
231 new->state = FEAT_INITIALISING;
232 new->needs_confirm = 0;
233 new->empty_confirm = 0;
234 new->val = *fval;
235 new->needs_mandatory = mandatory;
236
237 return 0;
238}
239
Gerrit Renker61e64732008-11-04 23:54:04 -0800240static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
241{
242 list_del(&entry->node);
243 dccp_feat_entry_destructor(entry);
244}
245
246void dccp_feat_list_purge(struct list_head *fn_list)
247{
248 struct dccp_feat_entry *entry, *next;
249
250 list_for_each_entry_safe(entry, next, fn_list, node)
251 dccp_feat_entry_destructor(entry);
252 INIT_LIST_HEAD(fn_list);
253}
254EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
255
Gerrit Renkerac757732008-11-04 23:55:49 -0800256/* generate @to as full clone of @from - @to must not contain any nodes */
257int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
258{
259 struct dccp_feat_entry *entry, *new;
260
261 INIT_LIST_HEAD(to);
262 list_for_each_entry(entry, from, node) {
263 new = dccp_feat_clone_entry(entry);
264 if (new == NULL)
265 goto cloning_failed;
266 list_add_tail(&new->node, to);
267 }
268 return 0;
269
270cloning_failed:
271 dccp_feat_list_purge(to);
272 return -ENOMEM;
273}
274
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800275static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
276{
277 switch (feat_num) {
278 case DCCPF_ACK_RATIO:
279 return val <= DCCPF_ACK_RATIO_MAX;
280 case DCCPF_SEQUENCE_WINDOW:
281 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
282 }
283 return 0; /* feature unknown - so we can't tell */
284}
285
286/* check that SP values are within the ranges defined in RFC 4340 */
287static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
288{
289 switch (feat_num) {
290 case DCCPF_CCID:
291 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
292 /* Type-check Boolean feature values: */
293 case DCCPF_SHORT_SEQNOS:
294 case DCCPF_ECN_INCAPABLE:
295 case DCCPF_SEND_ACK_VECTOR:
296 case DCCPF_SEND_NDP_COUNT:
297 case DCCPF_DATA_CHECKSUM:
298 case DCCPF_SEND_LEV_RATE:
299 return val < 2;
300 case DCCPF_MIN_CSUM_COVER:
301 return val < 16;
302 }
303 return 0; /* feature unknown */
304}
305
306static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
307{
308 if (sp_list == NULL || sp_len < 1)
309 return 0;
310 while (sp_len--)
311 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
312 return 0;
313 return 1;
314}
315
316/**
317 * __feat_register_nn - Register new NN value on socket
318 * @fn: feature-negotiation list to register with
319 * @feat: an NN feature from %dccp_feature_numbers
320 * @mandatory: use Mandatory option if 1
321 * @nn_val: value to register (restricted to 4 bytes)
322 * Note that NN features are local by definition (RFC 4340, 6.3.2).
323 */
324static int __feat_register_nn(struct list_head *fn, u8 feat,
325 u8 mandatory, u64 nn_val)
326{
327 dccp_feat_val fval = { .nn = nn_val };
328
329 if (dccp_feat_type(feat) != FEAT_NN ||
330 !dccp_feat_is_valid_nn_val(feat, nn_val))
331 return -EINVAL;
332
333 /* Don't bother with default values, they will be activated anyway. */
334 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
335 return 0;
336
337 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
338}
339
340/**
341 * __feat_register_sp - Register new SP value/list on socket
342 * @fn: feature-negotiation list to register with
343 * @feat: an SP feature from %dccp_feature_numbers
344 * @is_local: whether the local (1) or the remote (0) @feat is meant
345 * @mandatory: use Mandatory option if 1
346 * @sp_val: SP value followed by optional preference list
347 * @sp_len: length of @sp_val in bytes
348 */
349static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
350 u8 mandatory, u8 const *sp_val, u8 sp_len)
351{
352 dccp_feat_val fval;
353
354 if (dccp_feat_type(feat) != FEAT_SP ||
355 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
356 return -EINVAL;
357
Gerrit Renkerd90ebcb2008-11-12 00:47:26 -0800358 /* Avoid negotiating alien CCIDs by only advertising supported ones */
359 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
360 return -EOPNOTSUPP;
361
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800362 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
363 return -ENOMEM;
364
365 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
366}
367
Gerrit Renker49aebc62008-11-16 22:51:23 -0800368/**
369 * dccp_feat_register_sp - Register requests to change SP feature values
370 * @sk: client or listening socket
371 * @feat: one of %dccp_feature_numbers
372 * @is_local: whether the local (1) or remote (0) @feat is meant
373 * @list: array of preferred values, in descending order of preference
374 * @len: length of @list in bytes
375 */
376int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
377 u8 const *list, u8 len)
378{ /* any changes must be registered before establishing the connection */
379 if (sk->sk_state != DCCP_CLOSED)
380 return -EISCONN;
381 if (dccp_feat_type(feat) != FEAT_SP)
Chris Wright19443172008-05-05 13:50:24 -0700382 return -EINVAL;
Gerrit Renker49aebc62008-11-16 22:51:23 -0800383 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
384 0, list, len);
Andrea Bittauafe00252006-03-20 17:43:56 -0800385}
386
Gerrit Renker49aebc62008-11-16 22:51:23 -0800387/* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
388int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
389{
390 /* any changes must be registered before establishing the connection */
391 if (sk->sk_state != DCCP_CLOSED)
392 return -EISCONN;
393 if (dccp_feat_type(feat) != FEAT_NN)
394 return -EINVAL;
395 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
396}
Andrea Bittauafe00252006-03-20 17:43:56 -0800397
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800398/*
399 * Tracking features whose value depend on the choice of CCID
400 *
401 * This is designed with an extension in mind so that a list walk could be done
402 * before activating any features. However, the existing framework was found to
403 * work satisfactorily up until now, the automatic verification is left open.
404 * When adding new CCIDs, add a corresponding dependency table here.
405 */
406static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
407{
408 static const struct ccid_dependency ccid2_dependencies[2][2] = {
409 /*
410 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
411 * feature and Send Ack Vector is an RX feature, `is_local'
412 * needs to be reversed.
413 */
414 { /* Dependencies of the receiver-side (remote) CCID2 */
415 {
416 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
417 .is_local = true,
418 .is_mandatory = true,
419 .val = 1
420 },
421 { 0, 0, 0, 0 }
422 },
423 { /* Dependencies of the sender-side (local) CCID2 */
424 {
425 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
426 .is_local = false,
427 .is_mandatory = true,
428 .val = 1
429 },
430 { 0, 0, 0, 0 }
431 }
432 };
433 static const struct ccid_dependency ccid3_dependencies[2][5] = {
434 { /*
435 * Dependencies of the receiver-side CCID3
436 */
437 { /* locally disable Ack Vectors */
438 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
439 .is_local = true,
440 .is_mandatory = false,
441 .val = 0
442 },
443 { /* see below why Send Loss Event Rate is on */
444 .dependent_feat = DCCPF_SEND_LEV_RATE,
445 .is_local = true,
446 .is_mandatory = true,
447 .val = 1
448 },
449 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
450 .dependent_feat = DCCPF_SEND_NDP_COUNT,
451 .is_local = false,
452 .is_mandatory = true,
453 .val = 1
454 },
455 { 0, 0, 0, 0 },
456 },
457 { /*
458 * CCID3 at the TX side: we request that the HC-receiver
459 * will not send Ack Vectors (they will be ignored, so
460 * Mandatory is not set); we enable Send Loss Event Rate
461 * (Mandatory since the implementation does not support
462 * the Loss Intervals option of RFC 4342, 8.6).
463 * The last two options are for peer's information only.
464 */
465 {
466 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
467 .is_local = false,
468 .is_mandatory = false,
469 .val = 0
470 },
471 {
472 .dependent_feat = DCCPF_SEND_LEV_RATE,
473 .is_local = false,
474 .is_mandatory = true,
475 .val = 1
476 },
477 { /* this CCID does not support Ack Ratio */
478 .dependent_feat = DCCPF_ACK_RATIO,
479 .is_local = true,
480 .is_mandatory = false,
481 .val = 0
482 },
483 { /* tell receiver we are sending NDP counts */
484 .dependent_feat = DCCPF_SEND_NDP_COUNT,
485 .is_local = true,
486 .is_mandatory = false,
487 .val = 1
488 },
489 { 0, 0, 0, 0 }
490 }
491 };
492 switch (ccid) {
493 case DCCPC_CCID2:
494 return ccid2_dependencies[is_local];
495 case DCCPC_CCID3:
496 return ccid3_dependencies[is_local];
497 default:
498 return NULL;
499 }
500}
501
502/**
503 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
504 * @fn: feature-negotiation list to update
505 * @id: CCID number to track
506 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
507 * This function needs to be called after registering all other features.
508 */
509static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
510{
511 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
512 int i, rc = (table == NULL);
513
514 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
515 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
516 rc = __feat_register_sp(fn, table[i].dependent_feat,
517 table[i].is_local,
518 table[i].is_mandatory,
519 &table[i].val, 1);
520 else
521 rc = __feat_register_nn(fn, table[i].dependent_feat,
522 table[i].is_mandatory,
523 table[i].val);
524 return rc;
525}
526
527/**
528 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
529 * @dp: client or listening socket (settings will be inherited)
530 * This is called after all registrations (socket initialisation, sysctls, and
531 * sockopt calls), and before sending the first packet containing Change options
532 * (ie. client-Request or server-Response), to ensure internal consistency.
533 */
534int dccp_feat_finalise_settings(struct dccp_sock *dp)
535{
536 struct list_head *fn = &dp->dccps_featneg;
537 struct dccp_feat_entry *entry;
538 int i = 2, ccids[2] = { -1, -1 };
539
540 /*
541 * Propagating CCIDs:
542 * 1) not useful to propagate CCID settings if this host advertises more
543 * than one CCID: the choice of CCID may still change - if this is
544 * the client, or if this is the server and the client sends
545 * singleton CCID values.
546 * 2) since is that propagate_ccid changes the list, we defer changing
547 * the sorted list until after the traversal.
548 */
549 list_for_each_entry(entry, fn, node)
550 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
551 ccids[entry->is_local] = entry->val.sp.vec[0];
552 while (i--)
553 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
554 return -1;
555 return 0;
556}
557
Gerrit Renker0c116832008-11-16 22:49:52 -0800558/**
559 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
560 * It is the server which resolves the dependencies once the CCID has been
561 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
562 */
563int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
564{
565 struct list_head *fn = &dreq->dreq_featneg;
566 struct dccp_feat_entry *entry;
567 u8 is_local, ccid;
568
569 for (is_local = 0; is_local <= 1; is_local++) {
570 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
571
572 if (entry != NULL && !entry->empty_confirm)
573 ccid = entry->val.sp.vec[0];
574 else
575 ccid = dccp_feat_default_value(DCCPF_CCID);
576
577 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
578 return -1;
579 }
580 return 0;
581}
582
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800583static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
584{
585 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800586 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800587 /* figure out if we are changing our CCID or the peer's */
588 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800589 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800590 struct ccid *new_ccid;
591
592 /* Check if nothing is being changed. */
593 if (ccid_nr == new_ccid_nr)
594 return 0;
595
596 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
597 if (new_ccid == NULL)
598 return -ENOMEM;
599
600 if (rx) {
601 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
602 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800603 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800604 } else {
605 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
606 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800607 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800608 }
609
610 return 0;
611}
612
Andrea Bittauafe00252006-03-20 17:43:56 -0800613static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
614{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200615 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800616
617 switch (feat) {
618 case DCCPF_CCID:
619 return dccp_feat_update_ccid(sk, type, val);
620 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200621 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
622 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800623 break;
624 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800625 return 0;
626}
627
628static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
629 u8 *rpref, u8 rlen)
630{
631 struct dccp_sock *dp = dccp_sk(sk);
632 u8 *spref, slen, *res = NULL;
633 int i, j, rc, agree = 1;
634
635 BUG_ON(rpref == NULL);
636
637 /* check if we are the black sheep */
638 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
639 spref = rpref;
640 slen = rlen;
641 rpref = opt->dccpop_val;
642 rlen = opt->dccpop_len;
643 } else {
644 spref = opt->dccpop_val;
645 slen = opt->dccpop_len;
646 }
647 /*
648 * Now we have server preference list in spref and client preference in
649 * rpref
650 */
651 BUG_ON(spref == NULL);
652 BUG_ON(rpref == NULL);
653
654 /* FIXME sanity check vals */
655
656 /* Are values in any order? XXX Lame "algorithm" here */
Andrea Bittauafe00252006-03-20 17:43:56 -0800657 for (i = 0; i < slen; i++) {
658 for (j = 0; j < rlen; j++) {
659 if (spref[i] == rpref[j]) {
660 res = &spref[i];
661 break;
662 }
663 }
664 if (res)
665 break;
666 }
667
668 /* we didn't agree on anything */
669 if (res == NULL) {
670 /* confirm previous value */
671 switch (opt->dccpop_feat) {
672 case DCCPF_CCID:
673 /* XXX did i get this right? =P */
674 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800675 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800676 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800677 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800678 break;
679
680 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200681 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
682 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800683 return -EFAULT;
684 }
685
686 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
687 agree = 0; /* this is used for mandatory options... */
688 }
689
690 /* need to put result and our preference list */
Andrea Bittauafe00252006-03-20 17:43:56 -0800691 rlen = 1 + opt->dccpop_len;
692 rpref = kmalloc(rlen, GFP_ATOMIC);
693 if (rpref == NULL)
694 return -ENOMEM;
695
696 *rpref = *res;
697 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
698
699 /* put it in the "confirm queue" */
700 if (opt->dccpop_sc == NULL) {
701 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
702 if (opt->dccpop_sc == NULL) {
703 kfree(rpref);
704 return -ENOMEM;
705 }
706 } else {
707 /* recycle the confirm slot */
708 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
709 kfree(opt->dccpop_sc->dccpoc_val);
710 dccp_pr_debug("recycling confirm slot\n");
711 }
712 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
713
714 opt->dccpop_sc->dccpoc_val = rpref;
715 opt->dccpop_sc->dccpoc_len = rlen;
716
717 /* update the option on our side [we are about to send the confirm] */
718 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
719 if (rc) {
720 kfree(opt->dccpop_sc->dccpoc_val);
721 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800722 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800723 return rc;
724 }
725
726 dccp_pr_debug("Will confirm %d\n", *rpref);
727
728 /* say we want to change to X but we just got a confirm X, suppress our
729 * change
730 */
731 if (!opt->dccpop_conf) {
732 if (*opt->dccpop_val == *res)
733 opt->dccpop_conf = 1;
734 dccp_pr_debug("won't ask for change of same feature\n");
735 }
736
737 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
738}
739
740static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
741{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800742 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800743 struct dccp_opt_pend *opt;
744 int rc = 1;
745 u8 t;
746
747 /*
748 * We received a CHANGE. We gotta match it against our own preference
749 * list. If we got a CHANGE_R it means it's a change for us, so we need
750 * to compare our CHANGE_L list.
751 */
752 if (type == DCCPO_CHANGE_L)
753 t = DCCPO_CHANGE_R;
754 else
755 t = DCCPO_CHANGE_L;
756
757 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800758 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800759 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
760 continue;
761
762 /* find the winner from the two preference lists */
763 rc = dccp_feat_reconcile(sk, opt, val, len);
764 break;
765 }
766
767 /* We didn't deal with the change. This can happen if we have no
768 * preference list for the feature. In fact, it just shouldn't
769 * happen---if we understand a feature, we should have a preference list
770 * with at least the default value.
771 */
772 BUG_ON(rc == 1);
773
774 return rc;
775}
776
777static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
778{
779 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800780 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800781 u8 *copy;
782 int rc;
783
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200784 /* NN features must be Change L (sec. 6.3.2) */
785 if (type != DCCPO_CHANGE_L) {
786 dccp_pr_debug("received %s for NN feature %d\n",
787 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800788 return -EFAULT;
789 }
790
791 /* XXX sanity check opt val */
792
793 /* copy option so we can confirm it */
794 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
795 if (opt == NULL)
796 return -ENOMEM;
797
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200798 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800799 if (copy == NULL) {
800 kfree(opt);
801 return -ENOMEM;
802 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800803
804 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
805 opt->dccpop_feat = feature;
806 opt->dccpop_val = copy;
807 opt->dccpop_len = len;
808
809 /* change feature */
810 rc = dccp_feat_update(sk, type, feature, *val);
811 if (rc) {
812 kfree(opt->dccpop_val);
813 kfree(opt);
814 return rc;
815 }
816
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200817 dccp_feat_debug(type, feature, *copy);
818
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800819 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800820
821 return 0;
822}
823
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800824static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
825 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800826{
Andrea Bittauafe00252006-03-20 17:43:56 -0800827 /* XXX check if other confirms for that are queued and recycle slot */
828 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
829
830 if (opt == NULL) {
831 /* XXX what do we do? Ignoring should be fine. It's a change
832 * after all =P
833 */
834 return;
835 }
836
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200837 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -0700838 case DCCPO_CHANGE_L:
839 opt->dccpop_type = DCCPO_CONFIRM_R;
840 break;
841 case DCCPO_CHANGE_R:
842 opt->dccpop_type = DCCPO_CONFIRM_L;
843 break;
844 default:
845 DCCP_WARN("invalid type %d\n", type);
846 kfree(opt);
847 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200848 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800849 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -0800850 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800851 opt->dccpop_len = 0;
852
853 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200854 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
855
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800856 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800857}
858
859static void dccp_feat_flush_confirm(struct sock *sk)
860{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800861 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800862 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800863 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800864
865 if (!yes) {
866 struct dccp_opt_pend *opt;
867
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800868 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800869 if (opt->dccpop_conf) {
870 yes = 1;
871 break;
872 }
873 }
874 }
875
876 if (!yes)
877 return;
878
879 /* OK there is something to confirm... */
880 /* XXX check if packet is in flight? Send delayed ack?? */
881 if (sk->sk_state == DCCP_OPEN)
882 dccp_send_ack(sk);
883}
884
885int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
886{
887 int rc;
888
Gerrit Renkerf74e91b2008-11-12 00:42:58 -0800889 /* Ignore Change requests other than during connection setup */
890 if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
891 return 0;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200892 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800893
894 /* figure out if it's SP or NN feature */
895 switch (feature) {
896 /* deal with SP features */
897 case DCCPF_CCID:
898 rc = dccp_feat_sp(sk, type, feature, val, len);
899 break;
900
901 /* deal with NN features */
902 case DCCPF_ACK_RATIO:
903 rc = dccp_feat_nn(sk, type, feature, val, len);
904 break;
905
906 /* XXX implement other features */
907 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200908 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
909 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800910 rc = -EFAULT;
911 break;
912 }
913
914 /* check if there were problems changing features */
915 if (rc) {
916 /* If we don't agree on SP, we sent a confirm for old value.
917 * However we propagate rc to caller in case option was
918 * mandatory
919 */
920 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800921 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800922 }
923
924 /* generate the confirm [if required] */
925 dccp_feat_flush_confirm(sk);
926
927 return rc;
928}
929
930EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
931
932int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
933 u8 *val, u8 len)
934{
935 u8 t;
936 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800937 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200938 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -0800939 int all_confirmed = 1;
940
Gerrit Renkerf74e91b2008-11-12 00:42:58 -0800941 /* Ignore Confirm options other than during connection setup */
942 if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
943 return 0;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200944 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800945
946 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200947 switch (type) {
948 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
949 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200950 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200951 return 1;
952
953 }
954 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -0800955
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800956 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800957 if (!opt->dccpop_conf && opt->dccpop_type == t &&
958 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200959 found = 1;
960 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
961
Andrea Bittauafe00252006-03-20 17:43:56 -0800962 /* XXX do sanity check */
963
964 opt->dccpop_conf = 1;
965
966 /* We got a confirmation---change the option */
967 dccp_feat_update(sk, opt->dccpop_type,
968 opt->dccpop_feat, *val);
969
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200970 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -0800971 break;
972 }
973
974 if (!opt->dccpop_conf)
975 all_confirmed = 0;
976 }
977
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200978 if (!found)
979 dccp_pr_debug("%s(%d, ...) never requested\n",
980 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800981 return 0;
982}
983
984EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
985
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800986void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -0800987{
Andrea Bittauafe00252006-03-20 17:43:56 -0800988 struct dccp_opt_pend *opt, *next;
989
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800990 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -0800991 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +0900992 BUG_ON(opt->dccpop_val == NULL);
993 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -0800994
995 if (opt->dccpop_sc != NULL) {
996 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
997 kfree(opt->dccpop_sc->dccpoc_val);
998 kfree(opt->dccpop_sc);
999 }
1000
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +09001001 kfree(opt);
1002 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001003 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -08001004
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001005 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -08001006 BUG_ON(opt == NULL);
1007 if (opt->dccpop_val != NULL)
1008 kfree(opt->dccpop_val);
1009 kfree(opt);
1010 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001011 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -08001012}
1013
1014EXPORT_SYMBOL_GPL(dccp_feat_clean);
1015
1016/* this is to be called only when a listening sock creates its child. It is
1017 * assumed by the function---the confirm is not duplicated, but rather it is
1018 * "passed on".
1019 */
1020int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
1021{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001022 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
1023 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001024 struct dccp_opt_pend *opt;
1025 int rc = 0;
1026
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001027 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
1028 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -08001029
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001030 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -08001031 struct dccp_opt_pend *newopt;
1032 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -02001033 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -08001034
1035 if (val == NULL)
1036 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -08001037
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -02001038 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -08001039 if (newopt == NULL) {
1040 kfree(val);
1041 goto out_clean;
1042 }
1043
1044 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -08001045 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001046 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -08001047
1048 /* XXX what happens with backlogs and multiple connections at
1049 * once...
1050 */
1051 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -08001052 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -08001053
1054 /* reset state for a new socket */
1055 opt->dccpop_conf = 0;
1056 }
1057
1058 /* XXX not doing anything about the conf queue */
1059
1060out:
1061 return rc;
1062
1063out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -08001064 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001065 rc = -ENOMEM;
1066 goto out;
1067}
1068
1069EXPORT_SYMBOL_GPL(dccp_feat_clone);
1070
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001071int dccp_feat_init(struct sock *sk)
Andrea Bittauafe00252006-03-20 17:43:56 -08001072{
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001073 struct dccp_sock *dp = dccp_sk(sk);
1074 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001075 int rc;
1076
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001077 INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
1078 INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
Andrea Bittauafe00252006-03-20 17:43:56 -08001079
1080 /* CCID L */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001081 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
1082 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -08001083 if (rc)
1084 goto out;
1085
1086 /* CCID R */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001087 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
1088 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -08001089 if (rc)
1090 goto out;
1091
1092 /* Ack ratio */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001093 rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
Gerrit Renker49aebc62008-11-16 22:51:23 -08001094 dp->dccps_l_ack_ratio);
Andrea Bittauafe00252006-03-20 17:43:56 -08001095out:
1096 return rc;
1097}
1098
1099EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001100
1101#ifdef CONFIG_IP_DCCP_DEBUG
1102const char *dccp_feat_typename(const u8 type)
1103{
1104 switch(type) {
1105 case DCCPO_CHANGE_L: return("ChangeL");
1106 case DCCPO_CONFIRM_L: return("ConfirmL");
1107 case DCCPO_CHANGE_R: return("ChangeR");
1108 case DCCPO_CONFIRM_R: return("ConfirmR");
1109 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -02001110 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001111 }
1112 return NULL;
1113}
1114
1115EXPORT_SYMBOL_GPL(dccp_feat_typename);
1116
1117const char *dccp_feat_name(const u8 feat)
1118{
1119 static const char *feature_names[] = {
1120 [DCCPF_RESERVED] = "Reserved",
1121 [DCCPF_CCID] = "CCID",
1122 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
1123 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
1124 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
1125 [DCCPF_ACK_RATIO] = "Ack Ratio",
1126 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
1127 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
1128 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
1129 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
1130 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -02001131 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1132 return feature_names[DCCPF_RESERVED];
1133
Gerrit Renker7d43d1a2008-11-04 23:43:47 -08001134 if (feat == DCCPF_SEND_LEV_RATE)
1135 return "Send Loss Event Rate";
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001136 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1137 return "CCID-specific";
1138
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001139 return feature_names[feat];
1140}
1141
1142EXPORT_SYMBOL_GPL(dccp_feat_name);
1143#endif /* CONFIG_IP_DCCP_DEBUG */