0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <linux/module.h>
0021 #include <linux/slab.h>
0022 #include "ccid.h"
0023 #include "feat.h"
0024
0025
0026 unsigned long sysctl_dccp_sequence_window __read_mostly = 100;
0027 int sysctl_dccp_rx_ccid __read_mostly = 2,
0028 sysctl_dccp_tx_ccid __read_mostly = 2;
0029
0030
0031
0032
0033
0034
0035
0036 static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
0037 {
0038 struct dccp_sock *dp = dccp_sk(sk);
0039 struct ccid *new_ccid = ccid_new(ccid, sk, rx);
0040
0041 if (new_ccid == NULL)
0042 return -ENOMEM;
0043
0044 if (rx) {
0045 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
0046 dp->dccps_hc_rx_ccid = new_ccid;
0047 } else {
0048 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
0049 dp->dccps_hc_tx_ccid = new_ccid;
0050 }
0051 return 0;
0052 }
0053
0054 static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
0055 {
0056 struct dccp_sock *dp = dccp_sk(sk);
0057
0058 if (rx) {
0059 dp->dccps_r_seq_win = seq_win;
0060
0061 dccp_update_gsr(sk, dp->dccps_gsr);
0062 } else {
0063 dp->dccps_l_seq_win = seq_win;
0064
0065 dccp_update_gss(sk, dp->dccps_gss);
0066 }
0067 return 0;
0068 }
0069
0070 static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
0071 {
0072 if (rx)
0073 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
0074 else
0075 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
0076 return 0;
0077 }
0078
0079 static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
0080 {
0081 struct dccp_sock *dp = dccp_sk(sk);
0082
0083 if (rx) {
0084 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
0085 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
0086 if (dp->dccps_hc_rx_ackvec == NULL)
0087 return -ENOMEM;
0088 } else if (!enable) {
0089 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
0090 dp->dccps_hc_rx_ackvec = NULL;
0091 }
0092 }
0093 return 0;
0094 }
0095
0096 static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
0097 {
0098 if (!rx)
0099 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
0100 return 0;
0101 }
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
0113 {
0114 struct dccp_sock *dp = dccp_sk(sk);
0115
0116 if (rx)
0117 dp->dccps_pcrlen = cscov;
0118 else {
0119 if (dp->dccps_pcslen == 0)
0120 dp->dccps_pcslen = cscov;
0121 else if (cscov > dp->dccps_pcslen)
0122 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
0123 dp->dccps_pcslen, (u8)cscov);
0124 }
0125 return 0;
0126 }
0127
0128 static const struct {
0129 u8 feat_num;
0130 enum dccp_feat_type rxtx;
0131 enum dccp_feat_type reconciliation;
0132 u8 default_value;
0133 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 } dccp_feat_table[] = {
0153 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
0154 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
0155 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
0156 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
0157 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
0158 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
0159 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
0160 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
0161 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
0162 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
0163 };
0164 #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
0165
0166
0167
0168
0169
0170
0171
0172 static int dccp_feat_index(u8 feat_num)
0173 {
0174
0175 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
0176 return feat_num - 1;
0177
0178
0179
0180
0181
0182 switch (feat_num) {
0183 case DCCPF_SEND_LEV_RATE:
0184 return DCCP_FEAT_SUPPORTED_MAX - 1;
0185 }
0186 return -1;
0187 }
0188
0189 static u8 dccp_feat_type(u8 feat_num)
0190 {
0191 int idx = dccp_feat_index(feat_num);
0192
0193 if (idx < 0)
0194 return FEAT_UNKNOWN;
0195 return dccp_feat_table[idx].reconciliation;
0196 }
0197
0198 static int dccp_feat_default_value(u8 feat_num)
0199 {
0200 int idx = dccp_feat_index(feat_num);
0201
0202
0203
0204
0205 DCCP_BUG_ON(idx < 0);
0206
0207 return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
0208 }
0209
0210
0211
0212
0213 static const char *dccp_feat_fname(const u8 feat)
0214 {
0215 static const char *const feature_names[] = {
0216 [DCCPF_RESERVED] = "Reserved",
0217 [DCCPF_CCID] = "CCID",
0218 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
0219 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
0220 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
0221 [DCCPF_ACK_RATIO] = "Ack Ratio",
0222 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
0223 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
0224 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
0225 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
0226 };
0227 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
0228 return feature_names[DCCPF_RESERVED];
0229
0230 if (feat == DCCPF_SEND_LEV_RATE)
0231 return "Send Loss Event Rate";
0232 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
0233 return "CCID-specific";
0234
0235 return feature_names[feat];
0236 }
0237
0238 static const char *const dccp_feat_sname[] = {
0239 "DEFAULT", "INITIALISING", "CHANGING", "UNSTABLE", "STABLE",
0240 };
0241
0242 #ifdef CONFIG_IP_DCCP_DEBUG
0243 static const char *dccp_feat_oname(const u8 opt)
0244 {
0245 switch (opt) {
0246 case DCCPO_CHANGE_L: return "Change_L";
0247 case DCCPO_CONFIRM_L: return "Confirm_L";
0248 case DCCPO_CHANGE_R: return "Change_R";
0249 case DCCPO_CONFIRM_R: return "Confirm_R";
0250 }
0251 return NULL;
0252 }
0253
0254 static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
0255 {
0256 u8 i, type = dccp_feat_type(feat_num);
0257
0258 if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL))
0259 dccp_pr_debug_cat("(NULL)");
0260 else if (type == FEAT_SP)
0261 for (i = 0; i < val->sp.len; i++)
0262 dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]);
0263 else if (type == FEAT_NN)
0264 dccp_pr_debug_cat("%llu", (unsigned long long)val->nn);
0265 else
0266 dccp_pr_debug_cat("unknown type %u", type);
0267 }
0268
0269 static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
0270 {
0271 u8 type = dccp_feat_type(feat_num);
0272 dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
0273
0274 if (type == FEAT_NN)
0275 fval.nn = dccp_decode_value_var(list, len);
0276 dccp_feat_printval(feat_num, &fval);
0277 }
0278
0279 static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
0280 {
0281 dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote",
0282 dccp_feat_fname(entry->feat_num));
0283 dccp_feat_printval(entry->feat_num, &entry->val);
0284 dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
0285 entry->needs_confirm ? "(Confirm pending)" : "");
0286 }
0287
0288 #define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \
0289 dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\
0290 dccp_feat_printvals(feat, val, len); \
0291 dccp_pr_debug_cat(") %s\n", mandatory ? "!" : ""); } while (0)
0292
0293 #define dccp_feat_print_fnlist(fn_list) { \
0294 const struct dccp_feat_entry *___entry; \
0295 \
0296 dccp_pr_debug("List Dump:\n"); \
0297 list_for_each_entry(___entry, fn_list, node) \
0298 dccp_feat_print_entry(___entry); \
0299 }
0300 #else
0301 #define dccp_feat_print_opt(opt, feat, val, len, mandatory)
0302 #define dccp_feat_print_fnlist(fn_list)
0303 #endif
0304
0305 static int __dccp_feat_activate(struct sock *sk, const int idx,
0306 const bool is_local, dccp_feat_val const *fval)
0307 {
0308 bool rx;
0309 u64 val;
0310
0311 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
0312 return -1;
0313 if (dccp_feat_table[idx].activation_hdlr == NULL)
0314 return 0;
0315
0316 if (fval == NULL) {
0317 val = dccp_feat_table[idx].default_value;
0318 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
0319 if (fval->sp.vec == NULL) {
0320
0321
0322
0323
0324
0325 DCCP_CRIT("Feature #%d undefined: using default", idx);
0326 val = dccp_feat_table[idx].default_value;
0327 } else {
0328 val = fval->sp.vec[0];
0329 }
0330 } else {
0331 val = fval->nn;
0332 }
0333
0334
0335 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
0336
0337 dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
0338 dccp_feat_fname(dccp_feat_table[idx].feat_num),
0339 fval ? "" : "default ", (unsigned long long)val);
0340
0341 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
0342 }
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353 static int dccp_feat_activate(struct sock *sk, u8 feat_num, bool local,
0354 dccp_feat_val const *fval)
0355 {
0356 return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval);
0357 }
0358
0359
0360 static inline int dccp_feat_must_be_understood(u8 feat_num)
0361 {
0362 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
0363 feat_num == DCCPF_SEQUENCE_WINDOW;
0364 }
0365
0366
0367 static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
0368 {
0369 fval->sp.len = len;
0370 if (fval->sp.len > 0) {
0371 fval->sp.vec = kmemdup(val, len, gfp_any());
0372 if (fval->sp.vec == NULL) {
0373 fval->sp.len = 0;
0374 return -ENOMEM;
0375 }
0376 }
0377 return 0;
0378 }
0379
0380 static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
0381 {
0382 if (unlikely(val == NULL))
0383 return;
0384 if (dccp_feat_type(feat_num) == FEAT_SP)
0385 kfree(val->sp.vec);
0386 memset(val, 0, sizeof(*val));
0387 }
0388
0389 static struct dccp_feat_entry *
0390 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
0391 {
0392 struct dccp_feat_entry *new;
0393 u8 type = dccp_feat_type(original->feat_num);
0394
0395 if (type == FEAT_UNKNOWN)
0396 return NULL;
0397
0398 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
0399 if (new == NULL)
0400 return NULL;
0401
0402 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
0403 original->val.sp.vec,
0404 original->val.sp.len)) {
0405 kfree(new);
0406 return NULL;
0407 }
0408 return new;
0409 }
0410
0411 static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
0412 {
0413 if (entry != NULL) {
0414 dccp_feat_val_destructor(entry->feat_num, &entry->val);
0415 kfree(entry);
0416 }
0417 }
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428 static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
0429 u8 feat_num, bool is_local)
0430 {
0431 struct dccp_feat_entry *entry;
0432
0433 list_for_each_entry(entry, fn_list, node) {
0434 if (entry->feat_num == feat_num && entry->is_local == is_local)
0435 return entry;
0436 else if (entry->feat_num > feat_num)
0437 break;
0438 }
0439 return NULL;
0440 }
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450 static struct dccp_feat_entry *
0451 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
0452 {
0453 struct dccp_feat_entry *entry;
0454
0455 list_for_each_entry(entry, head, node)
0456 if (entry->feat_num == feat && entry->is_local == local) {
0457 dccp_feat_val_destructor(entry->feat_num, &entry->val);
0458 return entry;
0459 } else if (entry->feat_num > feat) {
0460 head = &entry->node;
0461 break;
0462 }
0463
0464 entry = kmalloc(sizeof(*entry), gfp_any());
0465 if (entry != NULL) {
0466 entry->feat_num = feat;
0467 entry->is_local = local;
0468 list_add_tail(&entry->node, head);
0469 }
0470 return entry;
0471 }
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481 static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
0482 u8 mandatory, dccp_feat_val *fval)
0483 {
0484 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
0485
0486 if (new == NULL)
0487 return -ENOMEM;
0488
0489 new->feat_num = feat;
0490 new->is_local = local;
0491 new->state = FEAT_INITIALISING;
0492 new->needs_confirm = false;
0493 new->empty_confirm = false;
0494 new->val = *fval;
0495 new->needs_mandatory = mandatory;
0496
0497 return 0;
0498 }
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509 static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
0510 dccp_feat_val *fval)
0511 {
0512 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
0513
0514 if (new == NULL)
0515 return DCCP_RESET_CODE_TOO_BUSY;
0516
0517 new->feat_num = feat;
0518 new->is_local = local;
0519 new->state = FEAT_STABLE;
0520 new->needs_confirm = true;
0521 new->empty_confirm = (fval == NULL);
0522 new->val.nn = 0;
0523 if (!new->empty_confirm)
0524 new->val = *fval;
0525 new->needs_mandatory = false;
0526
0527 return 0;
0528 }
0529
0530 static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
0531 {
0532 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
0533 }
0534
0535 static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
0536 {
0537 list_del(&entry->node);
0538 dccp_feat_entry_destructor(entry);
0539 }
0540
0541 void dccp_feat_list_purge(struct list_head *fn_list)
0542 {
0543 struct dccp_feat_entry *entry, *next;
0544
0545 list_for_each_entry_safe(entry, next, fn_list, node)
0546 dccp_feat_entry_destructor(entry);
0547 INIT_LIST_HEAD(fn_list);
0548 }
0549 EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
0550
0551
0552 int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
0553 {
0554 struct dccp_feat_entry *entry, *new;
0555
0556 INIT_LIST_HEAD(to);
0557 list_for_each_entry(entry, from, node) {
0558 new = dccp_feat_clone_entry(entry);
0559 if (new == NULL)
0560 goto cloning_failed;
0561 list_add_tail(&new->node, to);
0562 }
0563 return 0;
0564
0565 cloning_failed:
0566 dccp_feat_list_purge(to);
0567 return -ENOMEM;
0568 }
0569
0570
0571
0572
0573
0574
0575
0576
0577 static u8 dccp_feat_valid_nn_length(u8 feat_num)
0578 {
0579 if (feat_num == DCCPF_ACK_RATIO)
0580 return 2;
0581 if (feat_num == DCCPF_SEQUENCE_WINDOW)
0582 return 6;
0583 return 0;
0584 }
0585
0586 static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
0587 {
0588 switch (feat_num) {
0589 case DCCPF_ACK_RATIO:
0590 return val <= DCCPF_ACK_RATIO_MAX;
0591 case DCCPF_SEQUENCE_WINDOW:
0592 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
0593 }
0594 return 0;
0595 }
0596
0597
0598 static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
0599 {
0600 switch (feat_num) {
0601 case DCCPF_CCID:
0602 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
0603
0604 case DCCPF_SHORT_SEQNOS:
0605 case DCCPF_ECN_INCAPABLE:
0606 case DCCPF_SEND_ACK_VECTOR:
0607 case DCCPF_SEND_NDP_COUNT:
0608 case DCCPF_DATA_CHECKSUM:
0609 case DCCPF_SEND_LEV_RATE:
0610 return val < 2;
0611 case DCCPF_MIN_CSUM_COVER:
0612 return val < 16;
0613 }
0614 return 0;
0615 }
0616
0617 static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
0618 {
0619 if (sp_list == NULL || sp_len < 1)
0620 return 0;
0621 while (sp_len--)
0622 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
0623 return 0;
0624 return 1;
0625 }
0626
0627
0628
0629
0630
0631
0632
0633 int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
0634 struct sk_buff *skb)
0635 {
0636 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
0637 struct dccp_feat_entry *pos, *next;
0638 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
0639 bool rpt;
0640
0641
0642 list_for_each_entry_safe_reverse(pos, next, fn, node) {
0643 opt = dccp_feat_genopt(pos);
0644 type = dccp_feat_type(pos->feat_num);
0645 rpt = false;
0646
0647 if (pos->empty_confirm) {
0648 len = 0;
0649 ptr = NULL;
0650 } else {
0651 if (type == FEAT_SP) {
0652 len = pos->val.sp.len;
0653 ptr = pos->val.sp.vec;
0654 rpt = pos->needs_confirm;
0655 } else if (type == FEAT_NN) {
0656 len = dccp_feat_valid_nn_length(pos->feat_num);
0657 ptr = nn_in_nbo;
0658 dccp_encode_value_var(pos->val.nn, ptr, len);
0659 } else {
0660 DCCP_BUG("unknown feature %u", pos->feat_num);
0661 return -1;
0662 }
0663 }
0664 dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
0665
0666 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
0667 return -1;
0668 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
0669 return -1;
0670
0671 if (skb->sk->sk_state == DCCP_OPEN &&
0672 (opt == DCCPO_CONFIRM_R || opt == DCCPO_CONFIRM_L)) {
0673
0674
0675
0676
0677 dccp_feat_list_pop(pos);
0678 } else {
0679
0680
0681
0682
0683 if (pos->state == FEAT_INITIALISING)
0684 pos->state = FEAT_CHANGING;
0685 }
0686 }
0687 return 0;
0688 }
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699 static int __feat_register_nn(struct list_head *fn, u8 feat,
0700 u8 mandatory, u64 nn_val)
0701 {
0702 dccp_feat_val fval = { .nn = nn_val };
0703
0704 if (dccp_feat_type(feat) != FEAT_NN ||
0705 !dccp_feat_is_valid_nn_val(feat, nn_val))
0706 return -EINVAL;
0707
0708
0709 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
0710 return 0;
0711
0712 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
0713 }
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724 static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
0725 u8 mandatory, u8 const *sp_val, u8 sp_len)
0726 {
0727 dccp_feat_val fval;
0728
0729 if (dccp_feat_type(feat) != FEAT_SP ||
0730 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
0731 return -EINVAL;
0732
0733
0734 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
0735 return -EOPNOTSUPP;
0736
0737 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
0738 return -ENOMEM;
0739
0740 if (dccp_feat_push_change(fn, feat, is_local, mandatory, &fval)) {
0741 kfree(fval.sp.vec);
0742 return -ENOMEM;
0743 }
0744
0745 return 0;
0746 }
0747
0748
0749
0750
0751
0752
0753
0754
0755
0756 int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
0757 u8 const *list, u8 len)
0758 {
0759 if (sk->sk_state != DCCP_CLOSED)
0760 return -EISCONN;
0761 if (dccp_feat_type(feat) != FEAT_SP)
0762 return -EINVAL;
0763 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
0764 0, list, len);
0765 }
0766
0767
0768
0769
0770
0771
0772
0773
0774
0775 u64 dccp_feat_nn_get(struct sock *sk, u8 feat)
0776 {
0777 if (dccp_feat_type(feat) == FEAT_NN) {
0778 struct dccp_sock *dp = dccp_sk(sk);
0779 struct dccp_feat_entry *entry;
0780
0781 entry = dccp_feat_list_lookup(&dp->dccps_featneg, feat, 1);
0782 if (entry != NULL)
0783 return entry->val.nn;
0784
0785 switch (feat) {
0786 case DCCPF_ACK_RATIO:
0787 return dp->dccps_l_ack_ratio;
0788 case DCCPF_SEQUENCE_WINDOW:
0789 return dp->dccps_l_seq_win;
0790 }
0791 }
0792 DCCP_BUG("attempt to look up unsupported feature %u", feat);
0793 return 0;
0794 }
0795 EXPORT_SYMBOL_GPL(dccp_feat_nn_get);
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805 int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
0806 {
0807 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
0808 dccp_feat_val fval = { .nn = nn_val };
0809 struct dccp_feat_entry *entry;
0810
0811 if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
0812 return 0;
0813
0814 if (dccp_feat_type(feat) != FEAT_NN ||
0815 !dccp_feat_is_valid_nn_val(feat, nn_val))
0816 return -EINVAL;
0817
0818 if (nn_val == dccp_feat_nn_get(sk, feat))
0819 return 0;
0820
0821 entry = dccp_feat_list_lookup(fn, feat, 1);
0822 if (entry != NULL) {
0823 dccp_pr_debug("Clobbering existing NN entry %llu -> %llu\n",
0824 (unsigned long long)entry->val.nn,
0825 (unsigned long long)nn_val);
0826 dccp_feat_list_pop(entry);
0827 }
0828
0829 inet_csk_schedule_ack(sk);
0830 return dccp_feat_push_change(fn, feat, 1, 0, &fval);
0831 }
0832 EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842 static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
0843 {
0844 static const struct ccid_dependency ccid2_dependencies[2][2] = {
0845
0846
0847
0848
0849
0850 {
0851 {
0852 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
0853 .is_local = true,
0854 .is_mandatory = true,
0855 .val = 1
0856 },
0857 { 0, 0, 0, 0 }
0858 },
0859 {
0860 {
0861 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
0862 .is_local = false,
0863 .is_mandatory = true,
0864 .val = 1
0865 },
0866 { 0, 0, 0, 0 }
0867 }
0868 };
0869 static const struct ccid_dependency ccid3_dependencies[2][5] = {
0870 {
0871
0872
0873 {
0874 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
0875 .is_local = true,
0876 .is_mandatory = false,
0877 .val = 0
0878 },
0879 {
0880 .dependent_feat = DCCPF_SEND_LEV_RATE,
0881 .is_local = true,
0882 .is_mandatory = true,
0883 .val = 1
0884 },
0885 {
0886 .dependent_feat = DCCPF_SEND_NDP_COUNT,
0887 .is_local = false,
0888 .is_mandatory = true,
0889 .val = 1
0890 },
0891 { 0, 0, 0, 0 },
0892 },
0893 {
0894
0895
0896
0897
0898
0899
0900
0901 {
0902 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
0903 .is_local = false,
0904 .is_mandatory = false,
0905 .val = 0
0906 },
0907 {
0908 .dependent_feat = DCCPF_SEND_LEV_RATE,
0909 .is_local = false,
0910 .is_mandatory = true,
0911 .val = 1
0912 },
0913 {
0914 .dependent_feat = DCCPF_ACK_RATIO,
0915 .is_local = true,
0916 .is_mandatory = false,
0917 .val = 0
0918 },
0919 {
0920 .dependent_feat = DCCPF_SEND_NDP_COUNT,
0921 .is_local = true,
0922 .is_mandatory = false,
0923 .val = 1
0924 },
0925 { 0, 0, 0, 0 }
0926 }
0927 };
0928 switch (ccid) {
0929 case DCCPC_CCID2:
0930 return ccid2_dependencies[is_local];
0931 case DCCPC_CCID3:
0932 return ccid3_dependencies[is_local];
0933 default:
0934 return NULL;
0935 }
0936 }
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946 static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
0947 {
0948 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
0949 int i, rc = (table == NULL);
0950
0951 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
0952 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
0953 rc = __feat_register_sp(fn, table[i].dependent_feat,
0954 table[i].is_local,
0955 table[i].is_mandatory,
0956 &table[i].val, 1);
0957 else
0958 rc = __feat_register_nn(fn, table[i].dependent_feat,
0959 table[i].is_mandatory,
0960 table[i].val);
0961 return rc;
0962 }
0963
0964
0965
0966
0967
0968
0969
0970
0971
0972 int dccp_feat_finalise_settings(struct dccp_sock *dp)
0973 {
0974 struct list_head *fn = &dp->dccps_featneg;
0975 struct dccp_feat_entry *entry;
0976 int i = 2, ccids[2] = { -1, -1 };
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987 list_for_each_entry(entry, fn, node)
0988 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
0989 ccids[entry->is_local] = entry->val.sp.vec[0];
0990 while (i--)
0991 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
0992 return -1;
0993 dccp_feat_print_fnlist(fn);
0994 return 0;
0995 }
0996
0997
0998
0999
1000
1001
1002
1003
1004 int 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
1025 static 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
1045 static 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
1071 static 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
1107 static 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
1199 unknown_feature_or_value:
1200 if (!is_mandatory)
1201 return dccp_push_empty_confirm(fn, feat, local);
1202
1203 not_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
1218 static 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
1287 confirmation_succeeded:
1288 entry->state = FEAT_STABLE;
1289 return 0;
1290
1291 confirmation_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
1315 static 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
1383 fast_path_unknown:
1384 if (!mandatory)
1385 return dccp_push_empty_confirm(fn, feat, local);
1386
1387 fast_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
1404 int 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
1453 int 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
1504 free_ccid_lists:
1505 kfree(tx.val);
1506 kfree(rx.val);
1507 return rc;
1508 }
1509
1510 int 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
1564 activation_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 }