]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
a919525a | 2 | #include <linux/netlink.h> |
1d1d63b6 | 3 | #include <linux/nospec.h> |
a919525a DA |
4 | #include <linux/rtnetlink.h> |
5 | #include <linux/types.h> | |
6 | #include <net/ip.h> | |
7 | #include <net/net_namespace.h> | |
8 | #include <net/tcp.h> | |
9 | ||
61e2bbaf | 10 | static int ip_metrics_convert(struct nlattr *fc_mx, |
d7e774f3 DA |
11 | int fc_mx_len, u32 *metrics, |
12 | struct netlink_ext_ack *extack) | |
a919525a DA |
13 | { |
14 | bool ecn_ca = false; | |
15 | struct nlattr *nla; | |
16 | int remaining; | |
17 | ||
a919525a DA |
18 | nla_for_each_attr(nla, fc_mx, fc_mx_len, remaining) { |
19 | int type = nla_type(nla); | |
20 | u32 val; | |
21 | ||
22 | if (!type) | |
23 | continue; | |
d7e774f3 DA |
24 | if (type > RTAX_MAX) { |
25 | NL_SET_ERR_MSG(extack, "Invalid metric type"); | |
a919525a | 26 | return -EINVAL; |
d7e774f3 | 27 | } |
a919525a | 28 | |
1d1d63b6 | 29 | type = array_index_nospec(type, RTAX_MAX + 1); |
a919525a DA |
30 | if (type == RTAX_CC_ALGO) { |
31 | char tmp[TCP_CA_NAME_MAX]; | |
32 | ||
872f6903 | 33 | nla_strscpy(tmp, nla, sizeof(tmp)); |
61e2bbaf | 34 | val = tcp_ca_get_key_by_name(tmp, &ecn_ca); |
d7e774f3 DA |
35 | if (val == TCP_CA_UNSPEC) { |
36 | NL_SET_ERR_MSG(extack, "Unknown tcp congestion algorithm"); | |
a919525a | 37 | return -EINVAL; |
d7e774f3 | 38 | } |
a919525a | 39 | } else { |
d7e774f3 DA |
40 | if (nla_len(nla) != sizeof(u32)) { |
41 | NL_SET_ERR_MSG_ATTR(extack, nla, | |
42 | "Invalid attribute in metrics"); | |
5b5e7a0d | 43 | return -EINVAL; |
d7e774f3 | 44 | } |
a919525a DA |
45 | val = nla_get_u32(nla); |
46 | } | |
47 | if (type == RTAX_ADVMSS && val > 65535 - 40) | |
48 | val = 65535 - 40; | |
49 | if (type == RTAX_MTU && val > 65535 - 15) | |
50 | val = 65535 - 15; | |
51 | if (type == RTAX_HOPLIMIT && val > 255) | |
52 | val = 255; | |
d7e774f3 DA |
53 | if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) { |
54 | NL_SET_ERR_MSG(extack, "Unknown flag set in feature mask in metrics attribute"); | |
a919525a | 55 | return -EINVAL; |
d7e774f3 | 56 | } |
a919525a DA |
57 | metrics[type - 1] = val; |
58 | } | |
59 | ||
60 | if (ecn_ca) | |
61 | metrics[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; | |
62 | ||
63 | return 0; | |
64 | } | |
767a2217 | 65 | |
61e2bbaf | 66 | struct dst_metrics *ip_fib_metrics_init(struct nlattr *fc_mx, |
d7e774f3 DA |
67 | int fc_mx_len, |
68 | struct netlink_ext_ack *extack) | |
767a2217 DA |
69 | { |
70 | struct dst_metrics *fib_metrics; | |
71 | int err; | |
72 | ||
73 | if (!fc_mx) | |
74 | return (struct dst_metrics *)&dst_default_metrics; | |
75 | ||
76 | fib_metrics = kzalloc(sizeof(*fib_metrics), GFP_KERNEL); | |
77 | if (unlikely(!fib_metrics)) | |
78 | return ERR_PTR(-ENOMEM); | |
79 | ||
61e2bbaf | 80 | err = ip_metrics_convert(fc_mx, fc_mx_len, fib_metrics->metrics, |
d7e774f3 | 81 | extack); |
767a2217 DA |
82 | if (!err) { |
83 | refcount_set(&fib_metrics->refcnt, 1); | |
84 | } else { | |
85 | kfree(fib_metrics); | |
86 | fib_metrics = ERR_PTR(err); | |
87 | } | |
88 | ||
89 | return fib_metrics; | |
90 | } | |
91 | EXPORT_SYMBOL_GPL(ip_fib_metrics_init); |