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