blob: 283c7fb8c7dc09a44944982efba52f7c3b2f79c9 [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 */
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 Renkere77b8362008-12-01 23:32:35 -0800240/**
241 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
242 * @fn_list: feature-negotiation list to add to
243 * @feat: one of %dccp_feature_numbers
244 * @local: whether local (1) or remote (0) @feat_num is being confirmed
245 * @fval: pointer to NN/SP value to be inserted or NULL
246 * Returns 0 on success, a Reset code for further processing otherwise.
247 */
248static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
249 dccp_feat_val *fval)
250{
251 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
252
253 if (new == NULL)
254 return DCCP_RESET_CODE_TOO_BUSY;
255
256 new->feat_num = feat;
257 new->is_local = local;
258 new->state = FEAT_STABLE; /* transition in 6.6.2 */
259 new->needs_confirm = 1;
260 new->empty_confirm = (fval == NULL);
261 new->val.nn = 0; /* zeroes the whole structure */
262 if (!new->empty_confirm)
263 new->val = *fval;
264 new->needs_mandatory = 0;
265
266 return 0;
267}
268
269static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
270{
271 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
272}
273
Gerrit Renker61e64732008-11-04 23:54:04 -0800274static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
275{
276 list_del(&entry->node);
277 dccp_feat_entry_destructor(entry);
278}
279
280void dccp_feat_list_purge(struct list_head *fn_list)
281{
282 struct dccp_feat_entry *entry, *next;
283
284 list_for_each_entry_safe(entry, next, fn_list, node)
285 dccp_feat_entry_destructor(entry);
286 INIT_LIST_HEAD(fn_list);
287}
288EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
289
Gerrit Renkerac757732008-11-04 23:55:49 -0800290/* generate @to as full clone of @from - @to must not contain any nodes */
291int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
292{
293 struct dccp_feat_entry *entry, *new;
294
295 INIT_LIST_HEAD(to);
296 list_for_each_entry(entry, from, node) {
297 new = dccp_feat_clone_entry(entry);
298 if (new == NULL)
299 goto cloning_failed;
300 list_add_tail(&new->node, to);
301 }
302 return 0;
303
304cloning_failed:
305 dccp_feat_list_purge(to);
306 return -ENOMEM;
307}
308
Gerrit Renker0971d172008-12-01 23:27:31 -0800309/**
310 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
311 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
312 * incoming options are accepted as long as their values are valid.
313 */
314static u8 dccp_feat_valid_nn_length(u8 feat_num)
315{
316 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
317 return 2;
318 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
319 return 6;
320 return 0;
321}
322
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800323static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
324{
325 switch (feat_num) {
326 case DCCPF_ACK_RATIO:
327 return val <= DCCPF_ACK_RATIO_MAX;
328 case DCCPF_SEQUENCE_WINDOW:
329 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
330 }
331 return 0; /* feature unknown - so we can't tell */
332}
333
334/* check that SP values are within the ranges defined in RFC 4340 */
335static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
336{
337 switch (feat_num) {
338 case DCCPF_CCID:
339 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
340 /* Type-check Boolean feature values: */
341 case DCCPF_SHORT_SEQNOS:
342 case DCCPF_ECN_INCAPABLE:
343 case DCCPF_SEND_ACK_VECTOR:
344 case DCCPF_SEND_NDP_COUNT:
345 case DCCPF_DATA_CHECKSUM:
346 case DCCPF_SEND_LEV_RATE:
347 return val < 2;
348 case DCCPF_MIN_CSUM_COVER:
349 return val < 16;
350 }
351 return 0; /* feature unknown */
352}
353
354static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
355{
356 if (sp_list == NULL || sp_len < 1)
357 return 0;
358 while (sp_len--)
359 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
360 return 0;
361 return 1;
362}
363
364/**
Gerrit Renker0971d172008-12-01 23:27:31 -0800365 * dccp_feat_insert_opts - Generate FN options from current list state
366 * @skb: next sk_buff to be sent to the peer
367 * @dp: for client during handshake and general negotiation
368 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
369 */
370int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
371 struct sk_buff *skb)
372{
373 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
374 struct dccp_feat_entry *pos, *next;
375 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
376 bool rpt;
377
378 /* put entries into @skb in the order they appear in the list */
379 list_for_each_entry_safe_reverse(pos, next, fn, node) {
380 opt = dccp_feat_genopt(pos);
381 type = dccp_feat_type(pos->feat_num);
382 rpt = false;
383
384 if (pos->empty_confirm) {
385 len = 0;
386 ptr = NULL;
387 } else {
388 if (type == FEAT_SP) {
389 len = pos->val.sp.len;
390 ptr = pos->val.sp.vec;
391 rpt = pos->needs_confirm;
392 } else if (type == FEAT_NN) {
393 len = dccp_feat_valid_nn_length(pos->feat_num);
394 ptr = nn_in_nbo;
395 dccp_encode_value_var(pos->val.nn, ptr, len);
396 } else {
397 DCCP_BUG("unknown feature %u", pos->feat_num);
398 return -1;
399 }
400 }
401
402 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
403 return -1;
404 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
405 return -1;
406 /*
407 * Enter CHANGING after transmitting the Change option (6.6.2).
408 */
409 if (pos->state == FEAT_INITIALISING)
410 pos->state = FEAT_CHANGING;
411 }
412 return 0;
413}
414
415/**
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800416 * __feat_register_nn - Register new NN value on socket
417 * @fn: feature-negotiation list to register with
418 * @feat: an NN feature from %dccp_feature_numbers
419 * @mandatory: use Mandatory option if 1
420 * @nn_val: value to register (restricted to 4 bytes)
421 * Note that NN features are local by definition (RFC 4340, 6.3.2).
422 */
423static int __feat_register_nn(struct list_head *fn, u8 feat,
424 u8 mandatory, u64 nn_val)
425{
426 dccp_feat_val fval = { .nn = nn_val };
427
428 if (dccp_feat_type(feat) != FEAT_NN ||
429 !dccp_feat_is_valid_nn_val(feat, nn_val))
430 return -EINVAL;
431
432 /* Don't bother with default values, they will be activated anyway. */
433 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
434 return 0;
435
436 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
437}
438
439/**
440 * __feat_register_sp - Register new SP value/list on socket
441 * @fn: feature-negotiation list to register with
442 * @feat: an SP feature from %dccp_feature_numbers
443 * @is_local: whether the local (1) or the remote (0) @feat is meant
444 * @mandatory: use Mandatory option if 1
445 * @sp_val: SP value followed by optional preference list
446 * @sp_len: length of @sp_val in bytes
447 */
448static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
449 u8 mandatory, u8 const *sp_val, u8 sp_len)
450{
451 dccp_feat_val fval;
452
453 if (dccp_feat_type(feat) != FEAT_SP ||
454 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
455 return -EINVAL;
456
Gerrit Renkerd90ebcb2008-11-12 00:47:26 -0800457 /* Avoid negotiating alien CCIDs by only advertising supported ones */
458 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
459 return -EOPNOTSUPP;
460
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800461 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
462 return -ENOMEM;
463
464 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
465}
466
Gerrit Renker49aebc62008-11-16 22:51:23 -0800467/**
468 * dccp_feat_register_sp - Register requests to change SP feature values
469 * @sk: client or listening socket
470 * @feat: one of %dccp_feature_numbers
471 * @is_local: whether the local (1) or remote (0) @feat is meant
472 * @list: array of preferred values, in descending order of preference
473 * @len: length of @list in bytes
474 */
475int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
476 u8 const *list, u8 len)
477{ /* any changes must be registered before establishing the connection */
478 if (sk->sk_state != DCCP_CLOSED)
479 return -EISCONN;
480 if (dccp_feat_type(feat) != FEAT_SP)
Chris Wright19443172008-05-05 13:50:24 -0700481 return -EINVAL;
Gerrit Renker49aebc62008-11-16 22:51:23 -0800482 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
483 0, list, len);
Andrea Bittauafe00252006-03-20 17:43:56 -0800484}
485
Gerrit Renker49aebc62008-11-16 22:51:23 -0800486/* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
487int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
488{
489 /* any changes must be registered before establishing the connection */
490 if (sk->sk_state != DCCP_CLOSED)
491 return -EISCONN;
492 if (dccp_feat_type(feat) != FEAT_NN)
493 return -EINVAL;
494 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
495}
Andrea Bittauafe00252006-03-20 17:43:56 -0800496
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800497/*
498 * Tracking features whose value depend on the choice of CCID
499 *
500 * This is designed with an extension in mind so that a list walk could be done
501 * before activating any features. However, the existing framework was found to
502 * work satisfactorily up until now, the automatic verification is left open.
503 * When adding new CCIDs, add a corresponding dependency table here.
504 */
505static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
506{
507 static const struct ccid_dependency ccid2_dependencies[2][2] = {
508 /*
509 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
510 * feature and Send Ack Vector is an RX feature, `is_local'
511 * needs to be reversed.
512 */
513 { /* Dependencies of the receiver-side (remote) CCID2 */
514 {
515 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
516 .is_local = true,
517 .is_mandatory = true,
518 .val = 1
519 },
520 { 0, 0, 0, 0 }
521 },
522 { /* Dependencies of the sender-side (local) CCID2 */
523 {
524 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
525 .is_local = false,
526 .is_mandatory = true,
527 .val = 1
528 },
529 { 0, 0, 0, 0 }
530 }
531 };
532 static const struct ccid_dependency ccid3_dependencies[2][5] = {
533 { /*
534 * Dependencies of the receiver-side CCID3
535 */
536 { /* locally disable Ack Vectors */
537 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
538 .is_local = true,
539 .is_mandatory = false,
540 .val = 0
541 },
542 { /* see below why Send Loss Event Rate is on */
543 .dependent_feat = DCCPF_SEND_LEV_RATE,
544 .is_local = true,
545 .is_mandatory = true,
546 .val = 1
547 },
548 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
549 .dependent_feat = DCCPF_SEND_NDP_COUNT,
550 .is_local = false,
551 .is_mandatory = true,
552 .val = 1
553 },
554 { 0, 0, 0, 0 },
555 },
556 { /*
557 * CCID3 at the TX side: we request that the HC-receiver
558 * will not send Ack Vectors (they will be ignored, so
559 * Mandatory is not set); we enable Send Loss Event Rate
560 * (Mandatory since the implementation does not support
561 * the Loss Intervals option of RFC 4342, 8.6).
562 * The last two options are for peer's information only.
563 */
564 {
565 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
566 .is_local = false,
567 .is_mandatory = false,
568 .val = 0
569 },
570 {
571 .dependent_feat = DCCPF_SEND_LEV_RATE,
572 .is_local = false,
573 .is_mandatory = true,
574 .val = 1
575 },
576 { /* this CCID does not support Ack Ratio */
577 .dependent_feat = DCCPF_ACK_RATIO,
578 .is_local = true,
579 .is_mandatory = false,
580 .val = 0
581 },
582 { /* tell receiver we are sending NDP counts */
583 .dependent_feat = DCCPF_SEND_NDP_COUNT,
584 .is_local = true,
585 .is_mandatory = false,
586 .val = 1
587 },
588 { 0, 0, 0, 0 }
589 }
590 };
591 switch (ccid) {
592 case DCCPC_CCID2:
593 return ccid2_dependencies[is_local];
594 case DCCPC_CCID3:
595 return ccid3_dependencies[is_local];
596 default:
597 return NULL;
598 }
599}
600
601/**
602 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
603 * @fn: feature-negotiation list to update
604 * @id: CCID number to track
605 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
606 * This function needs to be called after registering all other features.
607 */
608static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
609{
610 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
611 int i, rc = (table == NULL);
612
613 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
614 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
615 rc = __feat_register_sp(fn, table[i].dependent_feat,
616 table[i].is_local,
617 table[i].is_mandatory,
618 &table[i].val, 1);
619 else
620 rc = __feat_register_nn(fn, table[i].dependent_feat,
621 table[i].is_mandatory,
622 table[i].val);
623 return rc;
624}
625
626/**
627 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
628 * @dp: client or listening socket (settings will be inherited)
629 * This is called after all registrations (socket initialisation, sysctls, and
630 * sockopt calls), and before sending the first packet containing Change options
631 * (ie. client-Request or server-Response), to ensure internal consistency.
632 */
633int dccp_feat_finalise_settings(struct dccp_sock *dp)
634{
635 struct list_head *fn = &dp->dccps_featneg;
636 struct dccp_feat_entry *entry;
637 int i = 2, ccids[2] = { -1, -1 };
638
639 /*
640 * Propagating CCIDs:
641 * 1) not useful to propagate CCID settings if this host advertises more
642 * than one CCID: the choice of CCID may still change - if this is
643 * the client, or if this is the server and the client sends
644 * singleton CCID values.
645 * 2) since is that propagate_ccid changes the list, we defer changing
646 * the sorted list until after the traversal.
647 */
648 list_for_each_entry(entry, fn, node)
649 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
650 ccids[entry->is_local] = entry->val.sp.vec[0];
651 while (i--)
652 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
653 return -1;
654 return 0;
655}
656
Gerrit Renker0c116832008-11-16 22:49:52 -0800657/**
658 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
659 * It is the server which resolves the dependencies once the CCID has been
660 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
661 */
662int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
663{
664 struct list_head *fn = &dreq->dreq_featneg;
665 struct dccp_feat_entry *entry;
666 u8 is_local, ccid;
667
668 for (is_local = 0; is_local <= 1; is_local++) {
669 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
670
671 if (entry != NULL && !entry->empty_confirm)
672 ccid = entry->val.sp.vec[0];
673 else
674 ccid = dccp_feat_default_value(DCCPF_CCID);
675
676 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
677 return -1;
678 }
679 return 0;
680}
681
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800682static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
683{
684 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800685 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800686 /* figure out if we are changing our CCID or the peer's */
687 const int rx = type == DCCPO_CHANGE_R;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800688 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800689 struct ccid *new_ccid;
690
691 /* Check if nothing is being changed. */
692 if (ccid_nr == new_ccid_nr)
693 return 0;
694
695 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
696 if (new_ccid == NULL)
697 return -ENOMEM;
698
699 if (rx) {
700 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
701 dp->dccps_hc_rx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800702 dmsk->dccpms_rx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800703 } else {
704 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
705 dp->dccps_hc_tx_ccid = new_ccid;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800706 dmsk->dccpms_tx_ccid = new_ccid_nr;
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800707 }
708
709 return 0;
710}
711
Andrea Bittauafe00252006-03-20 17:43:56 -0800712static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
713{
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200714 dccp_feat_debug(type, feat, val);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800715
716 switch (feat) {
717 case DCCPF_CCID:
718 return dccp_feat_update_ccid(sk, type, val);
719 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200720 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
721 dccp_feat_typename(type), feat);
Andrea Bittau6ffd30f2006-03-20 19:22:37 -0800722 break;
723 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800724 return 0;
725}
726
Gerrit Renker75757a72008-12-01 23:31:04 -0800727/* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
728static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
729{
730 u8 c, s;
731
732 for (s = 0; s < slen; s++)
733 for (c = 0; c < clen; c++)
734 if (servlist[s] == clilist[c])
735 return servlist[s];
736 return -1;
737}
738
739/**
740 * dccp_feat_prefer - Move preferred entry to the start of array
741 * Reorder the @array_len elements in @array so that @preferred_value comes
742 * first. Returns >0 to indicate that @preferred_value does occur in @array.
743 */
744static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
745{
746 u8 i, does_occur = 0;
747
748 if (array != NULL) {
749 for (i = 0; i < array_len; i++)
750 if (array[i] == preferred_value) {
751 array[i] = array[0];
752 does_occur++;
753 }
754 if (does_occur)
755 array[0] = preferred_value;
756 }
757 return does_occur;
758}
759
760/**
761 * dccp_feat_reconcile - Reconcile SP preference lists
762 * @fval: SP list to reconcile into
763 * @arr: received SP preference list
764 * @len: length of @arr in bytes
765 * @is_server: whether this side is the server (and @fv is the server's list)
766 * @reorder: whether to reorder the list in @fv after reconciling with @arr
767 * When successful, > 0 is returned and the reconciled list is in @fval.
768 * A value of 0 means that negotiation failed (no shared entry).
769 */
770static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
771 bool is_server, bool reorder)
772{
773 int rc;
774
775 if (!fv->sp.vec || !arr) {
776 DCCP_CRIT("NULL feature value or array");
777 return 0;
778 }
779
780 if (is_server)
781 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
782 else
783 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
784
785 if (!reorder)
786 return rc;
787 if (rc < 0)
788 return 0;
789
790 /*
791 * Reorder list: used for activating features and in dccp_insert_fn_opt.
792 */
793 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
794}
795
796#ifdef __this_is_the_old_framework_and_will_be_removed_later_in_a_subsequent_patch
Andrea Bittauafe00252006-03-20 17:43:56 -0800797static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
798 u8 *rpref, u8 rlen)
799{
800 struct dccp_sock *dp = dccp_sk(sk);
801 u8 *spref, slen, *res = NULL;
802 int i, j, rc, agree = 1;
803
804 BUG_ON(rpref == NULL);
805
806 /* check if we are the black sheep */
807 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
808 spref = rpref;
809 slen = rlen;
810 rpref = opt->dccpop_val;
811 rlen = opt->dccpop_len;
812 } else {
813 spref = opt->dccpop_val;
814 slen = opt->dccpop_len;
815 }
816 /*
817 * Now we have server preference list in spref and client preference in
818 * rpref
819 */
820 BUG_ON(spref == NULL);
821 BUG_ON(rpref == NULL);
822
823 /* FIXME sanity check vals */
824
825 /* Are values in any order? XXX Lame "algorithm" here */
Andrea Bittauafe00252006-03-20 17:43:56 -0800826 for (i = 0; i < slen; i++) {
827 for (j = 0; j < rlen; j++) {
828 if (spref[i] == rpref[j]) {
829 res = &spref[i];
830 break;
831 }
832 }
833 if (res)
834 break;
835 }
836
837 /* we didn't agree on anything */
838 if (res == NULL) {
839 /* confirm previous value */
840 switch (opt->dccpop_feat) {
841 case DCCPF_CCID:
842 /* XXX did i get this right? =P */
843 if (opt->dccpop_type == DCCPO_CHANGE_L)
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800844 res = &dccp_msk(sk)->dccpms_tx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800845 else
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800846 res = &dccp_msk(sk)->dccpms_rx_ccid;
Andrea Bittauafe00252006-03-20 17:43:56 -0800847 break;
848
849 default:
Gerrit Renker59348b12006-11-20 18:39:23 -0200850 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
851 /* XXX implement res */
Andrea Bittauafe00252006-03-20 17:43:56 -0800852 return -EFAULT;
853 }
854
855 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
856 agree = 0; /* this is used for mandatory options... */
857 }
858
859 /* need to put result and our preference list */
Andrea Bittauafe00252006-03-20 17:43:56 -0800860 rlen = 1 + opt->dccpop_len;
861 rpref = kmalloc(rlen, GFP_ATOMIC);
862 if (rpref == NULL)
863 return -ENOMEM;
864
865 *rpref = *res;
866 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
867
868 /* put it in the "confirm queue" */
869 if (opt->dccpop_sc == NULL) {
870 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
871 if (opt->dccpop_sc == NULL) {
872 kfree(rpref);
873 return -ENOMEM;
874 }
875 } else {
876 /* recycle the confirm slot */
877 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
878 kfree(opt->dccpop_sc->dccpoc_val);
879 dccp_pr_debug("recycling confirm slot\n");
880 }
881 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
882
883 opt->dccpop_sc->dccpoc_val = rpref;
884 opt->dccpop_sc->dccpoc_len = rlen;
885
886 /* update the option on our side [we are about to send the confirm] */
887 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
888 if (rc) {
889 kfree(opt->dccpop_sc->dccpoc_val);
890 kfree(opt->dccpop_sc);
Randy Dunlap68907da2006-03-29 13:58:25 -0800891 opt->dccpop_sc = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -0800892 return rc;
893 }
894
895 dccp_pr_debug("Will confirm %d\n", *rpref);
896
897 /* say we want to change to X but we just got a confirm X, suppress our
898 * change
899 */
900 if (!opt->dccpop_conf) {
901 if (*opt->dccpop_val == *res)
902 opt->dccpop_conf = 1;
903 dccp_pr_debug("won't ask for change of same feature\n");
904 }
905
906 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
907}
908
909static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
910{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800911 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800912 struct dccp_opt_pend *opt;
913 int rc = 1;
914 u8 t;
915
916 /*
917 * We received a CHANGE. We gotta match it against our own preference
918 * list. If we got a CHANGE_R it means it's a change for us, so we need
919 * to compare our CHANGE_L list.
920 */
921 if (type == DCCPO_CHANGE_L)
922 t = DCCPO_CHANGE_R;
923 else
924 t = DCCPO_CHANGE_L;
925
926 /* find our preference list for this feature */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800927 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -0800928 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
929 continue;
930
931 /* find the winner from the two preference lists */
932 rc = dccp_feat_reconcile(sk, opt, val, len);
933 break;
934 }
935
936 /* We didn't deal with the change. This can happen if we have no
937 * preference list for the feature. In fact, it just shouldn't
938 * happen---if we understand a feature, we should have a preference list
939 * with at least the default value.
940 */
941 BUG_ON(rc == 1);
942
943 return rc;
944}
945
946static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
947{
948 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800949 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -0800950 u8 *copy;
951 int rc;
952
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200953 /* NN features must be Change L (sec. 6.3.2) */
954 if (type != DCCPO_CHANGE_L) {
955 dccp_pr_debug("received %s for NN feature %d\n",
956 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -0800957 return -EFAULT;
958 }
959
960 /* XXX sanity check opt val */
961
962 /* copy option so we can confirm it */
963 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
964 if (opt == NULL)
965 return -ENOMEM;
966
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -0200967 copy = kmemdup(val, len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -0800968 if (copy == NULL) {
969 kfree(opt);
970 return -ENOMEM;
971 }
Andrea Bittauafe00252006-03-20 17:43:56 -0800972
973 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
974 opt->dccpop_feat = feature;
975 opt->dccpop_val = copy;
976 opt->dccpop_len = len;
977
978 /* change feature */
979 rc = dccp_feat_update(sk, type, feature, *val);
980 if (rc) {
981 kfree(opt->dccpop_val);
982 kfree(opt);
983 return rc;
984 }
985
Gerrit Renkerc02fdc02006-11-14 12:48:10 -0200986 dccp_feat_debug(type, feature, *copy);
987
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800988 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -0800989
990 return 0;
991}
992
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800993static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
994 u8 type, u8 feature)
Andrea Bittauafe00252006-03-20 17:43:56 -0800995{
Andrea Bittauafe00252006-03-20 17:43:56 -0800996 /* XXX check if other confirms for that are queued and recycle slot */
997 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
998
999 if (opt == NULL) {
1000 /* XXX what do we do? Ignoring should be fine. It's a change
1001 * after all =P
1002 */
1003 return;
1004 }
1005
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001006 switch (type) {
Jesper Juhle576de82007-08-10 15:23:54 -07001007 case DCCPO_CHANGE_L:
1008 opt->dccpop_type = DCCPO_CONFIRM_R;
1009 break;
1010 case DCCPO_CHANGE_R:
1011 opt->dccpop_type = DCCPO_CONFIRM_L;
1012 break;
1013 default:
1014 DCCP_WARN("invalid type %d\n", type);
1015 kfree(opt);
1016 return;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001017 }
Andrea Bittauafe00252006-03-20 17:43:56 -08001018 opt->dccpop_feat = feature;
Randy Dunlap68907da2006-03-29 13:58:25 -08001019 opt->dccpop_val = NULL;
Andrea Bittauafe00252006-03-20 17:43:56 -08001020 opt->dccpop_len = 0;
1021
1022 /* change feature */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001023 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
1024
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001025 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -08001026}
1027
1028static void dccp_feat_flush_confirm(struct sock *sk)
1029{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001030 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001031 /* Check if there is anything to confirm in the first place */
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001032 int yes = !list_empty(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -08001033
1034 if (!yes) {
1035 struct dccp_opt_pend *opt;
1036
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001037 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -08001038 if (opt->dccpop_conf) {
1039 yes = 1;
1040 break;
1041 }
1042 }
1043 }
1044
1045 if (!yes)
1046 return;
1047
1048 /* OK there is something to confirm... */
1049 /* XXX check if packet is in flight? Send delayed ack?? */
1050 if (sk->sk_state == DCCP_OPEN)
1051 dccp_send_ack(sk);
1052}
1053
1054int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
1055{
1056 int rc;
1057
Gerrit Renkerf74e91b2008-11-12 00:42:58 -08001058 /* Ignore Change requests other than during connection setup */
1059 if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
1060 return 0;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001061 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -08001062
1063 /* figure out if it's SP or NN feature */
1064 switch (feature) {
1065 /* deal with SP features */
1066 case DCCPF_CCID:
Gerrit Renker75757a72008-12-01 23:31:04 -08001067 /* XXX Obsoleted by next patch
1068 rc = dccp_feat_sp(sk, type, feature, val, len); */
Andrea Bittauafe00252006-03-20 17:43:56 -08001069 break;
1070
1071 /* deal with NN features */
1072 case DCCPF_ACK_RATIO:
Gerrit Renker75757a72008-12-01 23:31:04 -08001073 /* XXX Obsoleted by next patch
1074 rc = dccp_feat_nn(sk, type, feature, val, len); */
Andrea Bittauafe00252006-03-20 17:43:56 -08001075 break;
1076
1077 /* XXX implement other features */
1078 default:
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001079 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
1080 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -08001081 rc = -EFAULT;
1082 break;
1083 }
1084
1085 /* check if there were problems changing features */
1086 if (rc) {
1087 /* If we don't agree on SP, we sent a confirm for old value.
1088 * However we propagate rc to caller in case option was
1089 * mandatory
1090 */
1091 if (rc != DCCP_FEAT_SP_NOAGREE)
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -08001092 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
Andrea Bittauafe00252006-03-20 17:43:56 -08001093 }
1094
1095 /* generate the confirm [if required] */
1096 dccp_feat_flush_confirm(sk);
1097
1098 return rc;
1099}
1100
1101EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
Gerrit Renkere77b8362008-12-01 23:32:35 -08001102#endif /* (later) */
Andrea Bittauafe00252006-03-20 17:43:56 -08001103
1104int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
1105 u8 *val, u8 len)
1106{
1107 u8 t;
1108 struct dccp_opt_pend *opt;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001109 struct dccp_minisock *dmsk = dccp_msk(sk);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001110 int found = 0;
Andrea Bittauafe00252006-03-20 17:43:56 -08001111 int all_confirmed = 1;
1112
Gerrit Renkerf74e91b2008-11-12 00:42:58 -08001113 /* Ignore Confirm options other than during connection setup */
1114 if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
1115 return 0;
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001116 dccp_feat_debug(type, feature, *val);
Andrea Bittauafe00252006-03-20 17:43:56 -08001117
1118 /* locate our change request */
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001119 switch (type) {
1120 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
1121 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -02001122 default: DCCP_WARN("invalid type %d\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001123 return 1;
1124
1125 }
1126 /* XXX sanity check feature value */
Andrea Bittauafe00252006-03-20 17:43:56 -08001127
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001128 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -08001129 if (!opt->dccpop_conf && opt->dccpop_type == t &&
1130 opt->dccpop_feat == feature) {
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001131 found = 1;
1132 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
1133
Andrea Bittauafe00252006-03-20 17:43:56 -08001134 /* XXX do sanity check */
1135
1136 opt->dccpop_conf = 1;
1137
1138 /* We got a confirmation---change the option */
1139 dccp_feat_update(sk, opt->dccpop_type,
1140 opt->dccpop_feat, *val);
1141
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001142 /* XXX check the return value of dccp_feat_update */
Andrea Bittauafe00252006-03-20 17:43:56 -08001143 break;
1144 }
1145
1146 if (!opt->dccpop_conf)
1147 all_confirmed = 0;
1148 }
1149
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001150 if (!found)
1151 dccp_pr_debug("%s(%d, ...) never requested\n",
1152 dccp_feat_typename(type), feature);
Andrea Bittauafe00252006-03-20 17:43:56 -08001153 return 0;
1154}
1155
1156EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
1157
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -08001158void dccp_feat_clean(struct dccp_minisock *dmsk)
Andrea Bittauafe00252006-03-20 17:43:56 -08001159{
Andrea Bittauafe00252006-03-20 17:43:56 -08001160 struct dccp_opt_pend *opt, *next;
1161
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001162 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
Andrea Bittauafe00252006-03-20 17:43:56 -08001163 dccpop_node) {
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +09001164 BUG_ON(opt->dccpop_val == NULL);
1165 kfree(opt->dccpop_val);
Andrea Bittauafe00252006-03-20 17:43:56 -08001166
1167 if (opt->dccpop_sc != NULL) {
1168 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
1169 kfree(opt->dccpop_sc->dccpoc_val);
1170 kfree(opt->dccpop_sc);
1171 }
1172
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +09001173 kfree(opt);
1174 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001175 INIT_LIST_HEAD(&dmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -08001176
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001177 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -08001178 BUG_ON(opt == NULL);
1179 if (opt->dccpop_val != NULL)
1180 kfree(opt->dccpop_val);
1181 kfree(opt);
1182 }
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001183 INIT_LIST_HEAD(&dmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -08001184}
1185
1186EXPORT_SYMBOL_GPL(dccp_feat_clean);
1187
1188/* this is to be called only when a listening sock creates its child. It is
1189 * assumed by the function---the confirm is not duplicated, but rather it is
1190 * "passed on".
1191 */
1192int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
1193{
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001194 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
1195 struct dccp_minisock *newdmsk = dccp_msk(newsk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001196 struct dccp_opt_pend *opt;
1197 int rc = 0;
1198
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001199 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
1200 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
Andrea Bittauafe00252006-03-20 17:43:56 -08001201
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001202 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
Andrea Bittauafe00252006-03-20 17:43:56 -08001203 struct dccp_opt_pend *newopt;
1204 /* copy the value of the option */
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -02001205 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -08001206
1207 if (val == NULL)
1208 goto out_clean;
Andrea Bittauafe00252006-03-20 17:43:56 -08001209
Arnaldo Carvalho de Meloeed73412006-11-17 12:21:43 -02001210 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
Andrea Bittauafe00252006-03-20 17:43:56 -08001211 if (newopt == NULL) {
1212 kfree(val);
1213 goto out_clean;
1214 }
1215
1216 /* insert the option */
Andrea Bittauafe00252006-03-20 17:43:56 -08001217 newopt->dccpop_val = val;
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -08001218 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
Andrea Bittauafe00252006-03-20 17:43:56 -08001219
1220 /* XXX what happens with backlogs and multiple connections at
1221 * once...
1222 */
1223 /* the master socket no longer needs to worry about confirms */
Randy Dunlap68907da2006-03-29 13:58:25 -08001224 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
Andrea Bittauafe00252006-03-20 17:43:56 -08001225
1226 /* reset state for a new socket */
1227 opt->dccpop_conf = 0;
1228 }
1229
1230 /* XXX not doing anything about the conf queue */
1231
1232out:
1233 return rc;
1234
1235out_clean:
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -08001236 dccp_feat_clean(newdmsk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001237 rc = -ENOMEM;
1238 goto out;
1239}
1240
1241EXPORT_SYMBOL_GPL(dccp_feat_clone);
1242
Gerrit Renkere77b8362008-12-01 23:32:35 -08001243/**
1244 * dccp_feat_change_recv - Process incoming ChangeL/R options
1245 * @fn: feature-negotiation list to update
1246 * @is_mandatory: whether the Change was preceded by a Mandatory option
1247 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1248 * @feat: one of %dccp_feature_numbers
1249 * @val: NN value or SP value/preference list
1250 * @len: length of @val in bytes
1251 * @server: whether this node is the server (1) or the client (0)
1252 */
1253static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1254 u8 feat, u8 *val, u8 len, const bool server)
1255{
1256 u8 defval, type = dccp_feat_type(feat);
1257 const bool local = (opt == DCCPO_CHANGE_R);
1258 struct dccp_feat_entry *entry;
1259 dccp_feat_val fval;
1260
1261 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
1262 goto unknown_feature_or_value;
1263
1264 /*
1265 * Negotiation of NN features: Change R is invalid, so there is no
1266 * simultaneous negotiation; hence we do not look up in the list.
1267 */
1268 if (type == FEAT_NN) {
1269 if (local || len > sizeof(fval.nn))
1270 goto unknown_feature_or_value;
1271
1272 /* 6.3.2: "The feature remote MUST accept any valid value..." */
1273 fval.nn = dccp_decode_value_var(val, len);
1274 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1275 goto unknown_feature_or_value;
1276
1277 return dccp_feat_push_confirm(fn, feat, local, &fval);
1278 }
1279
1280 /*
1281 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
1282 */
1283 entry = dccp_feat_list_lookup(fn, feat, local);
1284 if (entry == NULL) {
1285 /*
1286 * No particular preferences have been registered. We deal with
1287 * this situation by assuming that all valid values are equally
1288 * acceptable, and apply the following checks:
1289 * - if the peer's list is a singleton, we accept a valid value;
1290 * - if we are the server, we first try to see if the peer (the
1291 * client) advertises the default value. If yes, we use it,
1292 * otherwise we accept the preferred value;
1293 * - else if we are the client, we use the first list element.
1294 */
1295 if (dccp_feat_clone_sp_val(&fval, val, 1))
1296 return DCCP_RESET_CODE_TOO_BUSY;
1297
1298 if (len > 1 && server) {
1299 defval = dccp_feat_default_value(feat);
1300 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1301 fval.sp.vec[0] = defval;
1302 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1303 kfree(fval.sp.vec);
1304 goto unknown_feature_or_value;
1305 }
1306
1307 /* Treat unsupported CCIDs like invalid values */
1308 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1309 kfree(fval.sp.vec);
1310 goto not_valid_or_not_known;
1311 }
1312
1313 return dccp_feat_push_confirm(fn, feat, local, &fval);
1314
1315 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
1316 return 0;
1317 }
1318
1319 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1320 entry->empty_confirm = 0;
1321 } else if (is_mandatory) {
1322 return DCCP_RESET_CODE_MANDATORY_ERROR;
1323 } else if (entry->state == FEAT_INITIALISING) {
1324 /*
1325 * Failed simultaneous negotiation (server only): try to `save'
1326 * the connection by checking whether entry contains the default
1327 * value for @feat. If yes, send an empty Confirm to signal that
1328 * the received Change was not understood - which implies using
1329 * the default value.
1330 * If this also fails, we use Reset as the last resort.
1331 */
1332 WARN_ON(!server);
1333 defval = dccp_feat_default_value(feat);
1334 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1335 return DCCP_RESET_CODE_OPTION_ERROR;
1336 entry->empty_confirm = 1;
1337 }
1338 entry->needs_confirm = 1;
1339 entry->needs_mandatory = 0;
1340 entry->state = FEAT_STABLE;
1341 return 0;
1342
1343unknown_feature_or_value:
1344 if (!is_mandatory)
1345 return dccp_push_empty_confirm(fn, feat, local);
1346
1347not_valid_or_not_known:
1348 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1349 : DCCP_RESET_CODE_OPTION_ERROR;
1350}
1351
1352/**
1353 * dccp_feat_parse_options - Process Feature-Negotiation Options
1354 * @sk: for general use and used by the client during connection setup
1355 * @dreq: used by the server during connection setup
1356 * @mandatory: whether @opt was preceded by a Mandatory option
1357 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1358 * @feat: one of %dccp_feature_numbers
1359 * @val: value contents of @opt
1360 * @len: length of @val in bytes
1361 * Returns 0 on success, a Reset code for ending the connection otherwise.
1362 */
1363int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1364 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1365{
1366 struct dccp_sock *dp = dccp_sk(sk);
1367 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1368 bool server = false;
1369
1370 switch (sk->sk_state) {
1371 /*
1372 * Negotiation during connection setup
1373 */
1374 case DCCP_LISTEN:
1375 server = true; /* fall through */
1376 case DCCP_REQUESTING:
1377 switch (opt) {
1378 case DCCPO_CHANGE_L:
1379 case DCCPO_CHANGE_R:
1380 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1381 val, len, server);
1382 }
1383 }
1384 return 0; /* ignore FN options in all other states */
1385}
1386
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001387int dccp_feat_init(struct sock *sk)
Andrea Bittauafe00252006-03-20 17:43:56 -08001388{
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001389 struct dccp_sock *dp = dccp_sk(sk);
1390 struct dccp_minisock *dmsk = dccp_msk(sk);
Andrea Bittauafe00252006-03-20 17:43:56 -08001391 int rc;
1392
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001393 INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
1394 INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
Andrea Bittauafe00252006-03-20 17:43:56 -08001395
1396 /* CCID L */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001397 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
1398 &dmsk->dccpms_tx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -08001399 if (rc)
1400 goto out;
1401
1402 /* CCID R */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001403 rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
1404 &dmsk->dccpms_rx_ccid, 1);
Andrea Bittauafe00252006-03-20 17:43:56 -08001405 if (rc)
1406 goto out;
1407
1408 /* Ack ratio */
Gerrit Renkere8ef9672008-11-12 00:43:40 -08001409 rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
Gerrit Renker49aebc62008-11-16 22:51:23 -08001410 dp->dccps_l_ack_ratio);
Andrea Bittauafe00252006-03-20 17:43:56 -08001411out:
1412 return rc;
1413}
1414
1415EXPORT_SYMBOL_GPL(dccp_feat_init);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001416
1417#ifdef CONFIG_IP_DCCP_DEBUG
1418const char *dccp_feat_typename(const u8 type)
1419{
1420 switch(type) {
1421 case DCCPO_CHANGE_L: return("ChangeL");
1422 case DCCPO_CONFIRM_L: return("ConfirmL");
1423 case DCCPO_CHANGE_R: return("ChangeR");
1424 case DCCPO_CONFIRM_R: return("ConfirmR");
1425 /* the following case must not appear in feature negotation */
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -02001426 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001427 }
1428 return NULL;
1429}
1430
1431EXPORT_SYMBOL_GPL(dccp_feat_typename);
1432
1433const char *dccp_feat_name(const u8 feat)
1434{
1435 static const char *feature_names[] = {
1436 [DCCPF_RESERVED] = "Reserved",
1437 [DCCPF_CCID] = "CCID",
1438 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
1439 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
1440 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
1441 [DCCPF_ACK_RATIO] = "Ack Ratio",
1442 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
1443 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
1444 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
1445 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
1446 };
Gerrit Renkerdd6303d2007-12-13 12:40:40 -02001447 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1448 return feature_names[DCCPF_RESERVED];
1449
Gerrit Renker7d43d1a02008-11-04 23:43:47 -08001450 if (feat == DCCPF_SEND_LEV_RATE)
1451 return "Send Loss Event Rate";
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001452 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1453 return "CCID-specific";
1454
Gerrit Renkerc02fdc02006-11-14 12:48:10 -02001455 return feature_names[feat];
1456}
1457
1458EXPORT_SYMBOL_GPL(dccp_feat_name);
1459#endif /* CONFIG_IP_DCCP_DEBUG */