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