xref: /openbmc/linux/net/ipv4/metrics.c (revision ba61bb17)
1 #include <linux/netlink.h>
2 #include <linux/rtnetlink.h>
3 #include <linux/types.h>
4 #include <net/ip.h>
5 #include <net/net_namespace.h>
6 #include <net/tcp.h>
7 
8 int ip_metrics_convert(struct net *net, struct nlattr *fc_mx, int fc_mx_len,
9 		       u32 *metrics)
10 {
11 	bool ecn_ca = false;
12 	struct nlattr *nla;
13 	int remaining;
14 
15 	if (!fc_mx)
16 		return 0;
17 
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;
24 		if (type > RTAX_MAX)
25 			return -EINVAL;
26 
27 		if (type == RTAX_CC_ALGO) {
28 			char tmp[TCP_CA_NAME_MAX];
29 
30 			nla_strlcpy(tmp, nla, sizeof(tmp));
31 			val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca);
32 			if (val == TCP_CA_UNSPEC)
33 				return -EINVAL;
34 		} else {
35 			if (nla_len(nla) != sizeof(u32))
36 				return -EINVAL;
37 			val = nla_get_u32(nla);
38 		}
39 		if (type == RTAX_ADVMSS && val > 65535 - 40)
40 			val = 65535 - 40;
41 		if (type == RTAX_MTU && val > 65535 - 15)
42 			val = 65535 - 15;
43 		if (type == RTAX_HOPLIMIT && val > 255)
44 			val = 255;
45 		if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
46 			return -EINVAL;
47 		metrics[type - 1] = val;
48 	}
49 
50 	if (ecn_ca)
51 		metrics[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
52 
53 	return 0;
54 }
55 EXPORT_SYMBOL_GPL(ip_metrics_convert);
56