1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/netlink.h>
3 #include <linux/rtnetlink.h>
4 #include <linux/types.h>
6 #include <net/net_namespace.h>
9 static int ip_metrics_convert(struct net
*net
, struct nlattr
*fc_mx
,
10 int fc_mx_len
, u32
*metrics
,
11 struct netlink_ext_ack
*extack
)
20 nla_for_each_attr(nla
, fc_mx
, fc_mx_len
, remaining
) {
21 int type
= nla_type(nla
);
26 if (type
> RTAX_MAX
) {
27 NL_SET_ERR_MSG(extack
, "Invalid metric type");
31 if (type
== RTAX_CC_ALGO
) {
32 char tmp
[TCP_CA_NAME_MAX
];
34 nla_strscpy(tmp
, nla
, sizeof(tmp
));
35 val
= tcp_ca_get_key_by_name(net
, tmp
, &ecn_ca
);
36 if (val
== TCP_CA_UNSPEC
) {
37 NL_SET_ERR_MSG(extack
, "Unknown tcp congestion algorithm");
41 if (nla_len(nla
) != sizeof(u32
)) {
42 NL_SET_ERR_MSG_ATTR(extack
, nla
,
43 "Invalid attribute in metrics");
46 val
= nla_get_u32(nla
);
48 if (type
== RTAX_ADVMSS
&& val
> 65535 - 40)
50 if (type
== RTAX_MTU
&& val
> 65535 - 15)
52 if (type
== RTAX_HOPLIMIT
&& val
> 255)
54 if (type
== RTAX_FEATURES
&& (val
& ~RTAX_FEATURE_MASK
)) {
55 NL_SET_ERR_MSG(extack
, "Unknown flag set in feature mask in metrics attribute");
58 metrics
[type
- 1] = val
;
62 metrics
[RTAX_FEATURES
- 1] |= DST_FEATURE_ECN_CA
;
67 struct dst_metrics
*ip_fib_metrics_init(struct net
*net
, struct nlattr
*fc_mx
,
69 struct netlink_ext_ack
*extack
)
71 struct dst_metrics
*fib_metrics
;
75 return (struct dst_metrics
*)&dst_default_metrics
;
77 fib_metrics
= kzalloc(sizeof(*fib_metrics
), GFP_KERNEL
);
78 if (unlikely(!fib_metrics
))
79 return ERR_PTR(-ENOMEM
);
81 err
= ip_metrics_convert(net
, fc_mx
, fc_mx_len
, fib_metrics
->metrics
,
84 refcount_set(&fib_metrics
->refcnt
, 1);
87 fib_metrics
= ERR_PTR(err
);
92 EXPORT_SYMBOL_GPL(ip_fib_metrics_init
);