25 #include <linux/module.h>
26 #include <linux/slab.h>
44 struct ccid *new_ccid =
ccid_new(ccid, sk, rx);
59 static int dccp_hdlr_seq_win(
struct sock *
sk,
u64 seq_win,
bool rx)
75 static int dccp_hdlr_ack_ratio(
struct sock *sk,
u64 ratio,
bool rx)
78 dccp_sk(sk)->dccps_r_ack_ratio =
ratio;
80 dccp_sk(sk)->dccps_l_ack_ratio =
ratio;
84 static int dccp_hdlr_ackvec(
struct sock *sk,
u64 enable,
bool rx)
101 static int dccp_hdlr_ndp(
struct sock *sk,
u64 enable,
bool rx)
104 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
117 static int dccp_hdlr_min_cscov(
struct sock *sk,
u64 cscov,
bool rx)
127 DCCP_WARN(
"CsCov %u too small, peer requires >= %u\n",
133 static const struct {
157 } dccp_feat_table[] = {
169 #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
194 int idx = dccp_feat_index(feat_num);
198 return dccp_feat_table[
idx].reconciliation;
201 static int dccp_feat_default_value(
u8 feat_num)
203 int idx = dccp_feat_index(feat_num);
210 return idx < 0 ? 0 : dccp_feat_table[
idx].default_value;
216 static const char *dccp_feat_fname(
const u8 feat)
218 static const char *
const feature_names[] = {
234 return "Send Loss Event Rate";
236 return "CCID-specific";
238 return feature_names[
feat];
241 static const char *
const dccp_feat_sname[] = {
242 "DEFAULT",
"INITIALISING",
"CHANGING",
"UNSTABLE",
"STABLE",
245 #ifdef CONFIG_IP_DCCP_DEBUG
246 static const char *dccp_feat_oname(
const u8 opt)
264 for (i = 0; i < val->
sp.len; i++)
272 static void dccp_feat_printvals(
u8 feat_num,
u8 *
list,
u8 len)
279 dccp_feat_printval(feat_num, &fval);
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)
296 #define dccp_feat_print_fnlist(fn_list) { \
297 const struct dccp_feat_entry *___entry; \
299 dccp_pr_debug("List Dump:\n"); \
300 list_for_each_entry(___entry, fn_list, node) \
301 dccp_feat_print_entry(___entry); \
304 #define dccp_feat_print_opt(opt, feat, val, len, mandatory)
305 #define dccp_feat_print_fnlist(fn_list)
308 static int __dccp_feat_activate(
struct sock *sk,
const int idx,
320 val = dccp_feat_table[
idx].default_value;
322 if (fval->
sp.vec ==
NULL) {
328 DCCP_CRIT(
"Feature #%d undefined: using default", idx);
329 val = dccp_feat_table[
idx].default_value;
331 val = fval->
sp.vec[0];
338 rx = (is_local == (dccp_feat_table[
idx].rxtx ==
FEAT_AT_RX));
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);
344 return dccp_feat_table[
idx].activation_hdlr(sk, val, rx);
356 static int dccp_feat_activate(
struct sock *sk,
u8 feat_num,
bool local,
359 return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval);
363 static inline int dccp_feat_must_be_understood(
u8 feat_num)
373 if (fval->
sp.len > 0) {
374 fval->
sp.vec =
kmemdup(val, len, gfp_any());
375 if (fval->
sp.vec ==
NULL) {
389 memset(val, 0,
sizeof(*val));
405 if (type ==
FEAT_SP && dccp_feat_clone_sp_val(&new->val,
406 original->
val.
sp.vec,
407 original->
val.
sp.len)) {
417 dccp_feat_val_destructor(entry->
feat_num, &entry->
val);
439 else if (entry->
feat_num > feat_num)
459 if (entry->feat_num == feat && entry->is_local == local) {
460 dccp_feat_val_destructor(entry->
feat_num, &entry->
val);
462 }
else if (entry->
feat_num > feat) {
467 entry =
kmalloc(
sizeof(*entry), gfp_any());
484 static int dccp_feat_push_change(
struct list_head *fn_list,
u8 feat,
u8 local,
487 struct dccp_feat_entry *
new = dccp_feat_entry_new(fn_list, feat, local);
492 new->feat_num =
feat;
493 new->is_local = local;
495 new->needs_confirm =
false;
496 new->empty_confirm =
false;
498 new->needs_mandatory = mandatory;
512 static int dccp_feat_push_confirm(
struct list_head *fn_list,
u8 feat,
u8 local,
515 struct dccp_feat_entry *
new = dccp_feat_entry_new(fn_list, feat, local);
520 new->feat_num =
feat;
521 new->is_local = local;
523 new->needs_confirm =
true;
524 new->empty_confirm = (fval ==
NULL);
526 if (!new->empty_confirm)
528 new->needs_mandatory =
false;
533 static int dccp_push_empty_confirm(
struct list_head *fn_list,
u8 feat,
u8 local)
535 return dccp_feat_push_confirm(fn_list, feat, local,
NULL);
541 dccp_feat_entry_destructor(entry);
549 dccp_feat_entry_destructor(entry);
550 INIT_LIST_HEAD(fn_list);
561 new = dccp_feat_clone_entry(entry);
578 static u8 dccp_feat_valid_nn_length(
u8 feat_num)
587 static u8 dccp_feat_is_valid_nn_val(
u8 feat_num,
u64 val)
599 static u8 dccp_feat_is_valid_sp_val(
u8 feat_num,
u8 val)
618 static u8 dccp_feat_sp_list_ok(
u8 feat_num,
u8 const *sp_list,
u8 sp_len)
620 if (sp_list ==
NULL || sp_len < 1)
623 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
644 opt = dccp_feat_genopt(pos);
653 len = pos->
val.
sp.len;
654 ptr = pos->
val.
sp.vec;
657 len = dccp_feat_valid_nn_length(pos->
feat_num);
678 dccp_feat_list_pop(pos);
701 u8 mandatory,
u64 nn_val)
706 !dccp_feat_is_valid_nn_val(feat, nn_val))
710 if (nn_val - (
u64)dccp_feat_default_value(feat) == 0)
713 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
725 static int __feat_register_sp(
struct list_head *fn,
u8 feat,
u8 is_local,
726 u8 mandatory,
u8 const *sp_val,
u8 sp_len)
731 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
738 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
741 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
753 u8 const *list,
u8 len)
759 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
779 return entry->
val.
nn;
788 DCCP_BUG(
"attempt to look up unsupported feature %u", feat);
803 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
811 !dccp_feat_is_valid_nn_val(feat, nn_val))
817 entry = dccp_feat_list_lookup(fn, feat, 1);
820 (
unsigned long long)entry->
val.
nn,
821 (
unsigned long long)nn_val);
822 dccp_feat_list_pop(entry);
825 inet_csk_schedule_ack(sk);
826 return dccp_feat_push_change(fn, feat, 1, 0, &fval);
850 .is_mandatory =
true,
859 .is_mandatory =
true,
872 .is_mandatory =
false,
878 .is_mandatory =
true,
884 .is_mandatory =
true,
900 .is_mandatory =
false,
906 .is_mandatory =
true,
912 .is_mandatory =
false,
918 .is_mandatory =
false,
926 return ccid2_dependencies[
is_local];
928 return ccid3_dependencies[
is_local];
942 static int dccp_feat_propagate_ccid(
struct list_head *fn,
u8 id,
bool is_local)
949 rc = __feat_register_sp(fn, table[i].dependent_feat,
954 rc = __feat_register_nn(fn, table[i].dependent_feat,
955 table[i].is_mandatory,
972 int i = 2, ccids[2] = { -1, -1 };
987 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
1004 for (is_local = 0; is_local <= 1; is_local++) {
1005 entry = dccp_feat_list_lookup(fn,
DCCPF_CCID, is_local);
1008 ccid = entry->
val.
sp.vec[0];
1012 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
1019 static int dccp_feat_preflist_match(
u8 *servlist,
u8 slen,
u8 *clilist,
u8 clen)
1023 for (s = 0; s < slen; s++)
1024 for (c = 0; c <
clen; c++)
1025 if (servlist[s] == clilist[c])
1035 static u8 dccp_feat_prefer(
u8 preferred_value,
u8 *array,
u8 array_len)
1037 u8 i, does_occur = 0;
1039 if (array !=
NULL) {
1040 for (i = 0; i < array_len; i++)
1041 if (array[i] == preferred_value) {
1042 array[
i] = array[0];
1046 array[0] = preferred_value;
1066 if (!fv->
sp.vec || !arr) {
1067 DCCP_CRIT(
"NULL feature value or array");
1072 rc = dccp_feat_preflist_match(fv->
sp.vec, fv->
sp.len, arr, len);
1074 rc = dccp_feat_preflist_match(arr, len, fv->
sp.vec, fv->
sp.len);
1084 return dccp_feat_prefer(rc, fv->
sp.vec, fv->
sp.len);
1097 static u8 dccp_feat_change_recv(
struct list_head *fn,
u8 is_mandatory,
u8 opt,
1098 u8 feat,
u8 *val,
u8 len,
const bool server)
1106 goto unknown_feature_or_value;
1115 if (local || len >
sizeof(fval.
nn))
1116 goto unknown_feature_or_value;
1120 if (!dccp_feat_is_valid_nn_val(feat, fval.
nn))
1121 goto unknown_feature_or_value;
1123 return dccp_feat_push_confirm(fn, feat, local, &fval);
1129 entry = dccp_feat_list_lookup(fn, feat, local);
1130 if (entry ==
NULL) {
1141 if (dccp_feat_clone_sp_val(&fval, val, 1))
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])) {
1150 goto unknown_feature_or_value;
1156 goto not_valid_or_not_known;
1159 return dccp_feat_push_confirm(fn, feat, local, &fval);
1165 if (dccp_feat_reconcile(&entry->
val, val, len, server,
true)) {
1167 }
else if (is_mandatory) {
1179 defval = dccp_feat_default_value(feat);
1180 if (!dccp_feat_reconcile(&entry->
val, &defval, 1, server,
true))
1189 unknown_feature_or_value:
1191 return dccp_push_empty_confirm(fn, feat, local);
1193 not_valid_or_not_known:
1208 static u8 dccp_feat_confirm_recv(
struct list_head *fn,
u8 is_mandatory,
u8 opt,
1209 u8 feat,
u8 *val,
u8 len,
const bool server)
1213 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1217 if (entry ==
NULL) {
1221 if (!local && type ==
FEAT_NN)
1222 goto confirmation_failed;
1230 if (dccp_feat_must_be_understood(feat))
1231 goto confirmation_failed;
1239 dccp_feat_list_pop(entry);
1244 if (len >
sizeof(entry->
val.
nn))
1245 goto confirmation_failed;
1248 goto confirmation_succeeded;
1250 DCCP_WARN(
"Bogus Confirm for non-existing value\n");
1251 goto confirmation_failed;
1259 if (!dccp_feat_is_valid_sp_val(feat, *val))
1260 goto confirmation_failed;
1271 if (dccp_feat_reconcile(&entry->
val, plist, plen, server, 0) != *val) {
1272 DCCP_WARN(
"Confirm selected the wrong value %u\n", *val);
1277 confirmation_succeeded:
1281 confirmation_failed:
1305 static u8 dccp_feat_handle_nn_established(
struct sock *sk,
u8 mandatory,
u8 opt,
1306 u8 feat,
u8 *val,
u8 len)
1308 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1318 if (local && !mandatory)
1320 goto fast_path_unknown;
1331 if (len == 0 || len >
sizeof(fval.
nn))
1332 goto fast_path_unknown;
1336 if (!dccp_feat_is_valid_nn_val(feat, fval.
nn))
1337 goto fast_path_unknown;
1339 if (dccp_feat_push_confirm(fn, feat, local, &fval) ||
1340 dccp_feat_activate(sk, feat, local, &fval))
1344 inet_csk_schedule_ack(sk);
1347 entry = dccp_feat_list_lookup(fn, feat, local);
1362 dccp_feat_activate(sk, feat, local, &fval);
1365 dccp_feat_list_pop(entry);
1368 DCCP_WARN(
"Received illegal option %u\n", opt);
1369 goto fast_path_failed;
1375 return dccp_push_empty_confirm(fn, feat, local);
1395 u8 mandatory,
u8 opt,
u8 feat,
u8 *val,
u8 len)
1399 bool server =
false;
1401 switch (sk->sk_state) {
1411 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1415 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1424 return dccp_feat_handle_nn_established(sk, mandatory, opt, feat,
1442 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1480 goto free_ccid_lists;
1482 rc = __feat_register_sp(fn,
DCCPF_CCID,
true,
false,
tx.val,
tx.len);
1484 goto free_ccid_lists;
1486 rc = __feat_register_sp(fn,
DCCPF_CCID,
false,
false, rx.val, rx.len);
1512 idx = dccp_feat_index(cur->
feat_num);
1515 goto activation_failed;
1518 DCCP_CRIT(
"Negotiation of %s %s failed in state %s",
1519 cur->
is_local ?
"local" :
"remote",
1521 dccp_feat_sname[cur->state]);
1522 goto activation_failed;
1534 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1535 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1536 DCCP_CRIT(
"Could not activate %d", idx);
1537 goto activation_failed;
1543 dccp_feat_list_pop(cur);