1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include "ccid.h"
26#include "feat.h"
27
28
29unsigned long sysctl_dccp_sequence_window __read_mostly = 100;
30int sysctl_dccp_rx_ccid __read_mostly = 2,
31 sysctl_dccp_tx_ccid __read_mostly = 2;
32
33
34
35
36
37
38
39static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
40{
41 struct dccp_sock *dp = dccp_sk(sk);
42 struct ccid *new_ccid = ccid_new(ccid, sk, rx);
43
44 if (new_ccid == NULL)
45 return -ENOMEM;
46
47 if (rx) {
48 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
49 dp->dccps_hc_rx_ccid = new_ccid;
50 } else {
51 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
52 dp->dccps_hc_tx_ccid = new_ccid;
53 }
54 return 0;
55}
56
57static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
58{
59 struct dccp_sock *dp = dccp_sk(sk);
60
61 if (rx) {
62 dp->dccps_r_seq_win = seq_win;
63
64 dccp_update_gsr(sk, dp->dccps_gsr);
65 } else {
66 dp->dccps_l_seq_win = seq_win;
67
68 dccp_update_gss(sk, dp->dccps_gss);
69 }
70 return 0;
71}
72
73static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
74{
75 if (rx)
76 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
77 else
78 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
79 return 0;
80}
81
82static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
83{
84 struct dccp_sock *dp = dccp_sk(sk);
85
86 if (rx) {
87 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
88 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
89 if (dp->dccps_hc_rx_ackvec == NULL)
90 return -ENOMEM;
91 } else if (!enable) {
92 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
93 dp->dccps_hc_rx_ackvec = NULL;
94 }
95 }
96 return 0;
97}
98
99static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
100{
101 if (!rx)
102 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
103 return 0;
104}
105
106
107
108
109
110
111
112
113
114
115static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
116{
117 struct dccp_sock *dp = dccp_sk(sk);
118
119 if (rx)
120 dp->dccps_pcrlen = cscov;
121 else {
122 if (dp->dccps_pcslen == 0)
123 dp->dccps_pcslen = cscov;
124 else if (cscov > dp->dccps_pcslen)
125 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
126 dp->dccps_pcslen, (u8)cscov);
127 }
128 return 0;
129}
130
131static const struct {
132 u8 feat_num;
133 enum dccp_feat_type rxtx;
134 enum dccp_feat_type reconciliation;
135 u8 default_value;
136 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155} dccp_feat_table[] = {
156 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
157 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
158 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
159 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
160 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
161 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
162 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
163 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
164 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
165 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
166};
167#define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
168
169
170
171
172
173static int dccp_feat_index(u8 feat_num)
174{
175
176 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
177 return feat_num - 1;
178
179
180
181
182
183 switch (feat_num) {
184 case DCCPF_SEND_LEV_RATE:
185 return DCCP_FEAT_SUPPORTED_MAX - 1;
186 }
187 return -1;
188}
189
190static u8 dccp_feat_type(u8 feat_num)
191{
192 int idx = dccp_feat_index(feat_num);
193
194 if (idx < 0)
195 return FEAT_UNKNOWN;
196 return dccp_feat_table[idx].reconciliation;
197}
198
199static int dccp_feat_default_value(u8 feat_num)
200{
201 int idx = dccp_feat_index(feat_num);
202
203
204
205
206 DCCP_BUG_ON(idx < 0);
207
208 return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
209}
210
211
212
213
214static const char *dccp_feat_fname(const u8 feat)
215{
216 static const char *const feature_names[] = {
217 [DCCPF_RESERVED] = "Reserved",
218 [DCCPF_CCID] = "CCID",
219 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
220 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
221 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
222 [DCCPF_ACK_RATIO] = "Ack Ratio",
223 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
224 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
225 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
226 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
227 };
228 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
229 return feature_names[DCCPF_RESERVED];
230
231 if (feat == DCCPF_SEND_LEV_RATE)
232 return "Send Loss Event Rate";
233 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
234 return "CCID-specific";
235
236 return feature_names[feat];
237}
238
239static const char *const dccp_feat_sname[] = {
240 "DEFAULT", "INITIALISING", "CHANGING", "UNSTABLE", "STABLE",
241};
242
243#ifdef CONFIG_IP_DCCP_DEBUG
244static const char *dccp_feat_oname(const u8 opt)
245{
246 switch (opt) {
247 case DCCPO_CHANGE_L: return "Change_L";
248 case DCCPO_CONFIRM_L: return "Confirm_L";
249 case DCCPO_CHANGE_R: return "Change_R";
250 case DCCPO_CONFIRM_R: return "Confirm_R";
251 }
252 return NULL;
253}
254
255static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
256{
257 u8 i, type = dccp_feat_type(feat_num);
258
259 if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL))
260 dccp_pr_debug_cat("(NULL)");
261 else if (type == FEAT_SP)
262 for (i = 0; i < val->sp.len; i++)
263 dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]);
264 else if (type == FEAT_NN)
265 dccp_pr_debug_cat("%llu", (unsigned long long)val->nn);
266 else
267 dccp_pr_debug_cat("unknown type %u", type);
268}
269
270static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
271{
272 u8 type = dccp_feat_type(feat_num);
273 dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
274
275 if (type == FEAT_NN)
276 fval.nn = dccp_decode_value_var(list, len);
277 dccp_feat_printval(feat_num, &fval);
278}
279
280static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
281{
282 dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote",
283 dccp_feat_fname(entry->feat_num));
284 dccp_feat_printval(entry->feat_num, &entry->val);
285 dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
286 entry->needs_confirm ? "(Confirm pending)" : "");
287}
288
289#define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \
290 dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\
291 dccp_feat_printvals(feat, val, len); \
292 dccp_pr_debug_cat(") %s\n", mandatory ? "!" : ""); } while (0)
293
294#define dccp_feat_print_fnlist(fn_list) { \
295 const struct dccp_feat_entry *___entry; \
296 \
297 dccp_pr_debug("List Dump:\n"); \
298 list_for_each_entry(___entry, fn_list, node) \
299 dccp_feat_print_entry(___entry); \
300}
301#else
302#define dccp_feat_print_opt(opt, feat, val, len, mandatory)
303#define dccp_feat_print_fnlist(fn_list)
304#endif
305
306static int __dccp_feat_activate(struct sock *sk, const int idx,
307 const bool is_local, dccp_feat_val const *fval)
308{
309 bool rx;
310 u64 val;
311
312 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
313 return -1;
314 if (dccp_feat_table[idx].activation_hdlr == NULL)
315 return 0;
316
317 if (fval == NULL) {
318 val = dccp_feat_table[idx].default_value;
319 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
320 if (fval->sp.vec == NULL) {
321
322
323
324
325
326 DCCP_CRIT("Feature #%d undefined: using default", idx);
327 val = dccp_feat_table[idx].default_value;
328 } else {
329 val = fval->sp.vec[0];
330 }
331 } else {
332 val = fval->nn;
333 }
334
335
336 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
337
338 dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
339 dccp_feat_fname(dccp_feat_table[idx].feat_num),
340 fval ? "" : "default ", (unsigned long long)val);
341
342 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
343}
344
345
346static inline int dccp_feat_must_be_understood(u8 feat_num)
347{
348 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
349 feat_num == DCCPF_SEQUENCE_WINDOW;
350}
351
352
353static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
354{
355 fval->sp.len = len;
356 if (fval->sp.len > 0) {
357 fval->sp.vec = kmemdup(val, len, gfp_any());
358 if (fval->sp.vec == NULL) {
359 fval->sp.len = 0;
360 return -ENOBUFS;
361 }
362 }
363 return 0;
364}
365
366static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
367{
368 if (unlikely(val == NULL))
369 return;
370 if (dccp_feat_type(feat_num) == FEAT_SP)
371 kfree(val->sp.vec);
372 memset(val, 0, sizeof(*val));
373}
374
375static struct dccp_feat_entry *
376 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
377{
378 struct dccp_feat_entry *new;
379 u8 type = dccp_feat_type(original->feat_num);
380
381 if (type == FEAT_UNKNOWN)
382 return NULL;
383
384 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
385 if (new == NULL)
386 return NULL;
387
388 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
389 original->val.sp.vec,
390 original->val.sp.len)) {
391 kfree(new);
392 return NULL;
393 }
394 return new;
395}
396
397static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
398{
399 if (entry != NULL) {
400 dccp_feat_val_destructor(entry->feat_num, &entry->val);
401 kfree(entry);
402 }
403}
404
405
406
407
408
409
410
411
412
413
414static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
415 u8 feat_num, bool is_local)
416{
417 struct dccp_feat_entry *entry;
418
419 list_for_each_entry(entry, fn_list, node) {
420 if (entry->feat_num == feat_num && entry->is_local == is_local)
421 return entry;
422 else if (entry->feat_num > feat_num)
423 break;
424 }
425 return NULL;
426}
427
428
429
430
431
432
433
434
435static struct dccp_feat_entry *
436 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
437{
438 struct dccp_feat_entry *entry;
439
440 list_for_each_entry(entry, head, node)
441 if (entry->feat_num == feat && entry->is_local == local) {
442 dccp_feat_val_destructor(entry->feat_num, &entry->val);
443 return entry;
444 } else if (entry->feat_num > feat) {
445 head = &entry->node;
446 break;
447 }
448
449 entry = kmalloc(sizeof(*entry), gfp_any());
450 if (entry != NULL) {
451 entry->feat_num = feat;
452 entry->is_local = local;
453 list_add_tail(&entry->node, head);
454 }
455 return entry;
456}
457
458
459
460
461
462
463
464
465
466static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
467 u8 mandatory, dccp_feat_val *fval)
468{
469 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
470
471 if (new == NULL)
472 return -ENOMEM;
473
474 new->feat_num = feat;
475 new->is_local = local;
476 new->state = FEAT_INITIALISING;
477 new->needs_confirm = 0;
478 new->empty_confirm = 0;
479 new->val = *fval;
480 new->needs_mandatory = mandatory;
481
482 return 0;
483}
484
485
486
487
488
489
490
491
492
493static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
494 dccp_feat_val *fval)
495{
496 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
497
498 if (new == NULL)
499 return DCCP_RESET_CODE_TOO_BUSY;
500
501 new->feat_num = feat;
502 new->is_local = local;
503 new->state = FEAT_STABLE;
504 new->needs_confirm = 1;
505 new->empty_confirm = (fval == NULL);
506 new->val.nn = 0;
507 if (!new->empty_confirm)
508 new->val = *fval;
509 new->needs_mandatory = 0;
510
511 return 0;
512}
513
514static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
515{
516 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
517}
518
519static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
520{
521 list_del(&entry->node);
522 dccp_feat_entry_destructor(entry);
523}
524
525void dccp_feat_list_purge(struct list_head *fn_list)
526{
527 struct dccp_feat_entry *entry, *next;
528
529 list_for_each_entry_safe(entry, next, fn_list, node)
530 dccp_feat_entry_destructor(entry);
531 INIT_LIST_HEAD(fn_list);
532}
533EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
534
535
536int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
537{
538 struct dccp_feat_entry *entry, *new;
539
540 INIT_LIST_HEAD(to);
541 list_for_each_entry(entry, from, node) {
542 new = dccp_feat_clone_entry(entry);
543 if (new == NULL)
544 goto cloning_failed;
545 list_add_tail(&new->node, to);
546 }
547 return 0;
548
549cloning_failed:
550 dccp_feat_list_purge(to);
551 return -ENOMEM;
552}
553
554
555
556
557
558
559static u8 dccp_feat_valid_nn_length(u8 feat_num)
560{
561 if (feat_num == DCCPF_ACK_RATIO)
562 return 2;
563 if (feat_num == DCCPF_SEQUENCE_WINDOW)
564 return 6;
565 return 0;
566}
567
568static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
569{
570 switch (feat_num) {
571 case DCCPF_ACK_RATIO:
572 return val <= DCCPF_ACK_RATIO_MAX;
573 case DCCPF_SEQUENCE_WINDOW:
574 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
575 }
576 return 0;
577}
578
579
580static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
581{
582 switch (feat_num) {
583 case DCCPF_CCID:
584 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
585
586 case DCCPF_SHORT_SEQNOS:
587 case DCCPF_ECN_INCAPABLE:
588 case DCCPF_SEND_ACK_VECTOR:
589 case DCCPF_SEND_NDP_COUNT:
590 case DCCPF_DATA_CHECKSUM:
591 case DCCPF_SEND_LEV_RATE:
592 return val < 2;
593 case DCCPF_MIN_CSUM_COVER:
594 return val < 16;
595 }
596 return 0;
597}
598
599static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
600{
601 if (sp_list == NULL || sp_len < 1)
602 return 0;
603 while (sp_len--)
604 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
605 return 0;
606 return 1;
607}
608
609
610
611
612
613
614
615int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
616 struct sk_buff *skb)
617{
618 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
619 struct dccp_feat_entry *pos, *next;
620 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
621 bool rpt;
622
623
624 list_for_each_entry_safe_reverse(pos, next, fn, node) {
625 opt = dccp_feat_genopt(pos);
626 type = dccp_feat_type(pos->feat_num);
627 rpt = false;
628
629 if (pos->empty_confirm) {
630 len = 0;
631 ptr = NULL;
632 } else {
633 if (type == FEAT_SP) {
634 len = pos->val.sp.len;
635 ptr = pos->val.sp.vec;
636 rpt = pos->needs_confirm;
637 } else if (type == FEAT_NN) {
638 len = dccp_feat_valid_nn_length(pos->feat_num);
639 ptr = nn_in_nbo;
640 dccp_encode_value_var(pos->val.nn, ptr, len);
641 } else {
642 DCCP_BUG("unknown feature %u", pos->feat_num);
643 return -1;
644 }
645 }
646 dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
647
648 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
649 return -1;
650 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
651 return -1;
652
653
654
655 if (pos->state == FEAT_INITIALISING)
656 pos->state = FEAT_CHANGING;
657 }
658 return 0;
659}
660
661
662
663
664
665
666
667
668
669static int __feat_register_nn(struct list_head *fn, u8 feat,
670 u8 mandatory, u64 nn_val)
671{
672 dccp_feat_val fval = { .nn = nn_val };
673
674 if (dccp_feat_type(feat) != FEAT_NN ||
675 !dccp_feat_is_valid_nn_val(feat, nn_val))
676 return -EINVAL;
677
678
679 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
680 return 0;
681
682 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
683}
684
685
686
687
688
689
690
691
692
693
694static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
695 u8 mandatory, u8 const *sp_val, u8 sp_len)
696{
697 dccp_feat_val fval;
698
699 if (dccp_feat_type(feat) != FEAT_SP ||
700 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
701 return -EINVAL;
702
703
704 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
705 return -EOPNOTSUPP;
706
707 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
708 return -ENOMEM;
709
710 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
711}
712
713
714
715
716
717
718
719
720
721int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
722 u8 const *list, u8 len)
723{
724 if (sk->sk_state != DCCP_CLOSED)
725 return -EISCONN;
726 if (dccp_feat_type(feat) != FEAT_SP)
727 return -EINVAL;
728 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
729 0, list, len);
730}
731
732
733int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
734{
735
736 if (sk->sk_state != DCCP_CLOSED)
737 return -EISCONN;
738 if (dccp_feat_type(feat) != FEAT_NN)
739 return -EINVAL;
740 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
741}
742
743
744
745
746
747
748
749
750
751static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
752{
753 static const struct ccid_dependency ccid2_dependencies[2][2] = {
754
755
756
757
758
759 {
760 {
761 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
762 .is_local = true,
763 .is_mandatory = true,
764 .val = 1
765 },
766 { 0, 0, 0, 0 }
767 },
768 {
769 {
770 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
771 .is_local = false,
772 .is_mandatory = true,
773 .val = 1
774 },
775 { 0, 0, 0, 0 }
776 }
777 };
778 static const struct ccid_dependency ccid3_dependencies[2][5] = {
779 {
780
781
782 {
783 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
784 .is_local = true,
785 .is_mandatory = false,
786 .val = 0
787 },
788 {
789 .dependent_feat = DCCPF_SEND_LEV_RATE,
790 .is_local = true,
791 .is_mandatory = true,
792 .val = 1
793 },
794 {
795 .dependent_feat = DCCPF_SEND_NDP_COUNT,
796 .is_local = false,
797 .is_mandatory = true,
798 .val = 1
799 },
800 { 0, 0, 0, 0 },
801 },
802 {
803
804
805
806
807
808
809
810 {
811 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
812 .is_local = false,
813 .is_mandatory = false,
814 .val = 0
815 },
816 {
817 .dependent_feat = DCCPF_SEND_LEV_RATE,
818 .is_local = false,
819 .is_mandatory = true,
820 .val = 1
821 },
822 {
823 .dependent_feat = DCCPF_ACK_RATIO,
824 .is_local = true,
825 .is_mandatory = false,
826 .val = 0
827 },
828 {
829 .dependent_feat = DCCPF_SEND_NDP_COUNT,
830 .is_local = true,
831 .is_mandatory = false,
832 .val = 1
833 },
834 { 0, 0, 0, 0 }
835 }
836 };
837 switch (ccid) {
838 case DCCPC_CCID2:
839 return ccid2_dependencies[is_local];
840 case DCCPC_CCID3:
841 return ccid3_dependencies[is_local];
842 default:
843 return NULL;
844 }
845}
846
847
848
849
850
851
852
853
854static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
855{
856 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
857 int i, rc = (table == NULL);
858
859 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
860 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
861 rc = __feat_register_sp(fn, table[i].dependent_feat,
862 table[i].is_local,
863 table[i].is_mandatory,
864 &table[i].val, 1);
865 else
866 rc = __feat_register_nn(fn, table[i].dependent_feat,
867 table[i].is_mandatory,
868 table[i].val);
869 return rc;
870}
871
872
873
874
875
876
877
878
879int dccp_feat_finalise_settings(struct dccp_sock *dp)
880{
881 struct list_head *fn = &dp->dccps_featneg;
882 struct dccp_feat_entry *entry;
883 int i = 2, ccids[2] = { -1, -1 };
884
885
886
887
888
889
890
891
892
893
894 list_for_each_entry(entry, fn, node)
895 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
896 ccids[entry->is_local] = entry->val.sp.vec[0];
897 while (i--)
898 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
899 return -1;
900 dccp_feat_print_fnlist(fn);
901 return 0;
902}
903
904
905
906
907
908
909int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
910{
911 struct list_head *fn = &dreq->dreq_featneg;
912 struct dccp_feat_entry *entry;
913 u8 is_local, ccid;
914
915 for (is_local = 0; is_local <= 1; is_local++) {
916 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
917
918 if (entry != NULL && !entry->empty_confirm)
919 ccid = entry->val.sp.vec[0];
920 else
921 ccid = dccp_feat_default_value(DCCPF_CCID);
922
923 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
924 return -1;
925 }
926 return 0;
927}
928
929
930static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
931{
932 u8 c, s;
933
934 for (s = 0; s < slen; s++)
935 for (c = 0; c < clen; c++)
936 if (servlist[s] == clilist[c])
937 return servlist[s];
938 return -1;
939}
940
941
942
943
944
945
946static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
947{
948 u8 i, does_occur = 0;
949
950 if (array != NULL) {
951 for (i = 0; i < array_len; i++)
952 if (array[i] == preferred_value) {
953 array[i] = array[0];
954 does_occur++;
955 }
956 if (does_occur)
957 array[0] = preferred_value;
958 }
959 return does_occur;
960}
961
962
963
964
965
966
967
968
969
970
971
972static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
973 bool is_server, bool reorder)
974{
975 int rc;
976
977 if (!fv->sp.vec || !arr) {
978 DCCP_CRIT("NULL feature value or array");
979 return 0;
980 }
981
982 if (is_server)
983 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
984 else
985 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
986
987 if (!reorder)
988 return rc;
989 if (rc < 0)
990 return 0;
991
992
993
994
995 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
996}
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1009 u8 feat, u8 *val, u8 len, const bool server)
1010{
1011 u8 defval, type = dccp_feat_type(feat);
1012 const bool local = (opt == DCCPO_CHANGE_R);
1013 struct dccp_feat_entry *entry;
1014 dccp_feat_val fval;
1015
1016 if (len == 0 || type == FEAT_UNKNOWN)
1017 goto unknown_feature_or_value;
1018
1019 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1020
1021
1022
1023
1024
1025 if (type == FEAT_NN) {
1026 if (local || len > sizeof(fval.nn))
1027 goto unknown_feature_or_value;
1028
1029
1030 fval.nn = dccp_decode_value_var(val, len);
1031 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1032 goto unknown_feature_or_value;
1033
1034 return dccp_feat_push_confirm(fn, feat, local, &fval);
1035 }
1036
1037
1038
1039
1040 entry = dccp_feat_list_lookup(fn, feat, local);
1041 if (entry == NULL) {
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 if (dccp_feat_clone_sp_val(&fval, val, 1))
1053 return DCCP_RESET_CODE_TOO_BUSY;
1054
1055 if (len > 1 && server) {
1056 defval = dccp_feat_default_value(feat);
1057 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1058 fval.sp.vec[0] = defval;
1059 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1060 kfree(fval.sp.vec);
1061 goto unknown_feature_or_value;
1062 }
1063
1064
1065 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1066 kfree(fval.sp.vec);
1067 goto not_valid_or_not_known;
1068 }
1069
1070 return dccp_feat_push_confirm(fn, feat, local, &fval);
1071
1072 } else if (entry->state == FEAT_UNSTABLE) {
1073 return 0;
1074 }
1075
1076 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1077 entry->empty_confirm = 0;
1078 } else if (is_mandatory) {
1079 return DCCP_RESET_CODE_MANDATORY_ERROR;
1080 } else if (entry->state == FEAT_INITIALISING) {
1081
1082
1083
1084
1085
1086
1087
1088
1089 WARN_ON(!server);
1090 defval = dccp_feat_default_value(feat);
1091 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1092 return DCCP_RESET_CODE_OPTION_ERROR;
1093 entry->empty_confirm = 1;
1094 }
1095 entry->needs_confirm = 1;
1096 entry->needs_mandatory = 0;
1097 entry->state = FEAT_STABLE;
1098 return 0;
1099
1100unknown_feature_or_value:
1101 if (!is_mandatory)
1102 return dccp_push_empty_confirm(fn, feat, local);
1103
1104not_valid_or_not_known:
1105 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1106 : DCCP_RESET_CODE_OPTION_ERROR;
1107}
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1120 u8 feat, u8 *val, u8 len, const bool server)
1121{
1122 u8 *plist, plen, type = dccp_feat_type(feat);
1123 const bool local = (opt == DCCPO_CONFIRM_R);
1124 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1125
1126 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1127
1128 if (entry == NULL) {
1129 if (is_mandatory && type == FEAT_UNKNOWN)
1130 return DCCP_RESET_CODE_MANDATORY_ERROR;
1131
1132 if (!local && type == FEAT_NN)
1133 goto confirmation_failed;
1134 return 0;
1135 }
1136
1137 if (entry->state != FEAT_CHANGING)
1138 return 0;
1139
1140 if (len == 0) {
1141 if (dccp_feat_must_be_understood(feat))
1142 goto confirmation_failed;
1143
1144
1145
1146
1147
1148
1149
1150 dccp_feat_list_pop(entry);
1151 return 0;
1152 }
1153
1154 if (type == FEAT_NN) {
1155 if (len > sizeof(entry->val.nn))
1156 goto confirmation_failed;
1157
1158 if (entry->val.nn == dccp_decode_value_var(val, len))
1159 goto confirmation_succeeded;
1160
1161 DCCP_WARN("Bogus Confirm for non-existing value\n");
1162 goto confirmation_failed;
1163 }
1164
1165
1166
1167
1168
1169
1170 if (!dccp_feat_is_valid_sp_val(feat, *val))
1171 goto confirmation_failed;
1172
1173 if (len == 1) {
1174 plist = val;
1175 plen = len;
1176 } else {
1177 plist = val + 1;
1178 plen = len - 1;
1179 }
1180
1181
1182 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1183 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1184 return DCCP_RESET_CODE_OPTION_ERROR;
1185 }
1186 entry->val.sp.vec[0] = *val;
1187
1188confirmation_succeeded:
1189 entry->state = FEAT_STABLE;
1190 return 0;
1191
1192confirmation_failed:
1193 DCCP_WARN("Confirmation failed\n");
1194 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1195 : DCCP_RESET_CODE_OPTION_ERROR;
1196}
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1210 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1211{
1212 struct dccp_sock *dp = dccp_sk(sk);
1213 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1214 bool server = false;
1215
1216 switch (sk->sk_state) {
1217
1218
1219
1220 case DCCP_LISTEN:
1221 server = true;
1222 case DCCP_REQUESTING:
1223 switch (opt) {
1224 case DCCPO_CHANGE_L:
1225 case DCCPO_CHANGE_R:
1226 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1227 val, len, server);
1228 case DCCPO_CONFIRM_R:
1229 case DCCPO_CONFIRM_L:
1230 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1231 val, len, server);
1232 }
1233 }
1234 return 0;
1235}
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247int dccp_feat_init(struct sock *sk)
1248{
1249 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1250 u8 on = 1, off = 0;
1251 int rc;
1252 struct {
1253 u8 *val;
1254 u8 len;
1255 } tx, rx;
1256
1257
1258 rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
1259 sysctl_dccp_sequence_window);
1260 if (rc)
1261 return rc;
1262
1263
1264
1265
1266 rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1267 if (rc)
1268 return rc;
1269
1270
1271 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1272 if (rc)
1273 return rc;
1274
1275
1276
1277
1278
1279
1280
1281 if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1282 ccid_get_builtin_ccids(&rx.val, &rx.len))
1283 return -ENOBUFS;
1284
1285 if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1286 !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
1287 goto free_ccid_lists;
1288
1289 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1290 if (rc)
1291 goto free_ccid_lists;
1292
1293 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1294
1295free_ccid_lists:
1296 kfree(tx.val);
1297 kfree(rx.val);
1298 return rc;
1299}
1300
1301int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1302{
1303 struct dccp_sock *dp = dccp_sk(sk);
1304 struct dccp_feat_entry *cur, *next;
1305 int idx;
1306 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1307 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1308 };
1309
1310 list_for_each_entry(cur, fn_list, node) {
1311
1312
1313
1314
1315
1316 if (cur->empty_confirm)
1317 continue;
1318
1319 idx = dccp_feat_index(cur->feat_num);
1320 if (idx < 0) {
1321 DCCP_BUG("Unknown feature %u", cur->feat_num);
1322 goto activation_failed;
1323 }
1324 if (cur->state != FEAT_STABLE) {
1325 DCCP_CRIT("Negotiation of %s %s failed in state %s",
1326 cur->is_local ? "local" : "remote",
1327 dccp_feat_fname(cur->feat_num),
1328 dccp_feat_sname[cur->state]);
1329 goto activation_failed;
1330 }
1331 fvals[idx][cur->is_local] = &cur->val;
1332 }
1333
1334
1335
1336
1337
1338
1339
1340 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1341 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1342 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1343 DCCP_CRIT("Could not activate %d", idx);
1344 goto activation_failed;
1345 }
1346
1347
1348 list_for_each_entry_safe(cur, next, fn_list, node)
1349 if (!cur->needs_confirm)
1350 dccp_feat_list_pop(cur);
1351
1352 dccp_pr_debug("Activation OK\n");
1353 return 0;
1354
1355activation_failed:
1356
1357
1358
1359
1360
1361
1362 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1363 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1364 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1365 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1366 dp->dccps_hc_rx_ackvec = NULL;
1367 return -1;
1368}
1369