1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/netlink.h>
3 #include <linux/nospec.h>
4 #include <linux/rtnetlink.h>
5 #include <linux/types.h>
7 #include <net/net_namespace.h>
10 static int ip_metrics_convert(struct net *net, struct nlattr *fc_mx,
11 int fc_mx_len, u32 *metrics,
12 struct netlink_ext_ack *extack)
18 nla_for_each_attr(nla, fc_mx, fc_mx_len, remaining) {
19 int type = nla_type(nla);
24 if (type > RTAX_MAX) {
25 NL_SET_ERR_MSG(extack, "Invalid metric type");
29 type = array_index_nospec(type, RTAX_MAX + 1);
30 if (type == RTAX_CC_ALGO) {
31 char tmp[TCP_CA_NAME_MAX];
33 nla_strscpy(tmp, nla, sizeof(tmp));
34 val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca);
35 if (val == TCP_CA_UNSPEC) {
36 NL_SET_ERR_MSG(extack, "Unknown tcp congestion algorithm");
40 if (nla_len(nla) != sizeof(u32)) {
41 NL_SET_ERR_MSG_ATTR(extack, nla,
42 "Invalid attribute in metrics");
45 val = nla_get_u32(nla);
47 if (type == RTAX_ADVMSS && val > 65535 - 40)
49 if (type == RTAX_MTU && val > 65535 - 15)
51 if (type == RTAX_HOPLIMIT && val > 255)
53 if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) {
54 NL_SET_ERR_MSG(extack, "Unknown flag set in feature mask in metrics attribute");
57 metrics[type - 1] = val;
61 metrics[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
66 struct dst_metrics *ip_fib_metrics_init(struct net *net, struct nlattr *fc_mx,
68 struct netlink_ext_ack *extack)
70 struct dst_metrics *fib_metrics;
74 return (struct dst_metrics *)&dst_default_metrics;
76 fib_metrics = kzalloc(sizeof(*fib_metrics), GFP_KERNEL);
77 if (unlikely(!fib_metrics))
78 return ERR_PTR(-ENOMEM);
80 err = ip_metrics_convert(net, fc_mx, fc_mx_len, fib_metrics->metrics,
83 refcount_set(&fib_metrics->refcnt, 1);
86 fib_metrics = ERR_PTR(err);
91 EXPORT_SYMBOL_GPL(ip_fib_metrics_init);