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