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