| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | #include <linux/netlink.h> |
|---|
| 2 | 3 | #include <linux/rtnetlink.h> |
|---|
| 3 | 4 | #include <linux/types.h> |
|---|
| .. | .. |
|---|
| 5 | 6 | #include <net/net_namespace.h> |
|---|
| 6 | 7 | #include <net/tcp.h> |
|---|
| 7 | 8 | |
|---|
| 8 | | -int ip_metrics_convert(struct net *net, struct nlattr *fc_mx, int fc_mx_len, |
|---|
| 9 | | - u32 *metrics) |
|---|
| 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) |
|---|
| 10 | 12 | { |
|---|
| 11 | 13 | bool ecn_ca = false; |
|---|
| 12 | 14 | struct nlattr *nla; |
|---|
| .. | .. |
|---|
| 21 | 23 | |
|---|
| 22 | 24 | if (!type) |
|---|
| 23 | 25 | continue; |
|---|
| 24 | | - if (type > RTAX_MAX) |
|---|
| 26 | + if (type > RTAX_MAX) { |
|---|
| 27 | + NL_SET_ERR_MSG(extack, "Invalid metric type"); |
|---|
| 25 | 28 | return -EINVAL; |
|---|
| 29 | + } |
|---|
| 26 | 30 | |
|---|
| 27 | 31 | if (type == RTAX_CC_ALGO) { |
|---|
| 28 | 32 | char tmp[TCP_CA_NAME_MAX]; |
|---|
| 29 | 33 | |
|---|
| 30 | 34 | nla_strlcpy(tmp, nla, sizeof(tmp)); |
|---|
| 31 | 35 | val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca); |
|---|
| 32 | | - if (val == TCP_CA_UNSPEC) |
|---|
| 36 | + if (val == TCP_CA_UNSPEC) { |
|---|
| 37 | + NL_SET_ERR_MSG(extack, "Unknown tcp congestion algorithm"); |
|---|
| 33 | 38 | return -EINVAL; |
|---|
| 39 | + } |
|---|
| 34 | 40 | } else { |
|---|
| 35 | | - if (nla_len(nla) != sizeof(u32)) |
|---|
| 41 | + if (nla_len(nla) != sizeof(u32)) { |
|---|
| 42 | + NL_SET_ERR_MSG_ATTR(extack, nla, |
|---|
| 43 | + "Invalid attribute in metrics"); |
|---|
| 36 | 44 | return -EINVAL; |
|---|
| 45 | + } |
|---|
| 37 | 46 | val = nla_get_u32(nla); |
|---|
| 38 | 47 | } |
|---|
| 39 | 48 | if (type == RTAX_ADVMSS && val > 65535 - 40) |
|---|
| .. | .. |
|---|
| 42 | 51 | val = 65535 - 15; |
|---|
| 43 | 52 | if (type == RTAX_HOPLIMIT && val > 255) |
|---|
| 44 | 53 | val = 255; |
|---|
| 45 | | - if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) |
|---|
| 54 | + if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) { |
|---|
| 55 | + NL_SET_ERR_MSG(extack, "Unknown flag set in feature mask in metrics attribute"); |
|---|
| 46 | 56 | return -EINVAL; |
|---|
| 57 | + } |
|---|
| 47 | 58 | metrics[type - 1] = val; |
|---|
| 48 | 59 | } |
|---|
| 49 | 60 | |
|---|
| .. | .. |
|---|
| 52 | 63 | |
|---|
| 53 | 64 | return 0; |
|---|
| 54 | 65 | } |
|---|
| 55 | | -EXPORT_SYMBOL_GPL(ip_metrics_convert); |
|---|
| 66 | + |
|---|
| 67 | +struct dst_metrics *ip_fib_metrics_init(struct net *net, struct nlattr *fc_mx, |
|---|
| 68 | + int fc_mx_len, |
|---|
| 69 | + struct netlink_ext_ack *extack) |
|---|
| 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 | + |
|---|
| 81 | + err = ip_metrics_convert(net, fc_mx, fc_mx_len, fib_metrics->metrics, |
|---|
| 82 | + extack); |
|---|
| 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); |
|---|