Commit a985343b authored by Matthias Schiffer's avatar Matthias Schiffer Committed by David S. Miller

vxlan: refactor verification and application of configuration

The vxlan_dev_configure function was mixing validation and application of
the vxlan configuration; this could easily lead to bugs with the changelink
operation, as it was hard to see if the function wcould return an error
after parts of the configuration had already been applied.

This commit splits validation and application out of vxlan_dev_configure as
separate functions to make it clearer where error returns are allowed and
where the vxlan_dev or net_device may be configured. Log messages in these
functions are removed, as it is generally unexpected to find error output
for netlink requests in the kernel log. Userspace should be able to handle
errors based on the error codes returned via netlink just fine.

In addition, some validation and initialization is moved to vxlan_validate
and vxlan_setup respectively to improve grouping of similar settings.

Finally, this also fixes two actual bugs:

* if set, conf->mtu would overwrite dev->mtu in each changelink operation,
  reverting other changes of dev->mtu
* the "if (!conf->dst_port)" branch would never be run, as conf->dst_port
  was set in vxlan_setup before. This caused VXLAN-GPE to use the same
  default port as other VXLAN sockets instead of the intended IANA-assigned
  4790.
Signed-off-by: default avatarMatthias Schiffer <mschiffer@universe-factory.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent c0ca9e72
...@@ -2623,6 +2623,10 @@ static void vxlan_setup(struct net_device *dev) ...@@ -2623,6 +2623,10 @@ static void vxlan_setup(struct net_device *dev)
netif_keep_dst(dev); netif_keep_dst(dev);
dev->priv_flags |= IFF_NO_QUEUE; dev->priv_flags |= IFF_NO_QUEUE;
/* MTU range: 68 - 65535 */
dev->min_mtu = ETH_MIN_MTU;
dev->max_mtu = ETH_MAX_MTU;
INIT_LIST_HEAD(&vxlan->next); INIT_LIST_HEAD(&vxlan->next);
spin_lock_init(&vxlan->hash_lock); spin_lock_init(&vxlan->hash_lock);
...@@ -2630,9 +2634,8 @@ static void vxlan_setup(struct net_device *dev) ...@@ -2630,9 +2634,8 @@ static void vxlan_setup(struct net_device *dev)
vxlan->age_timer.function = vxlan_cleanup; vxlan->age_timer.function = vxlan_cleanup;
vxlan->age_timer.data = (unsigned long) vxlan; vxlan->age_timer.data = (unsigned long) vxlan;
vxlan->cfg.dst_port = htons(vxlan_port);
vxlan->dev = dev; vxlan->dev = dev;
vxlan->net = dev_net(dev);
gro_cells_init(&vxlan->gro_cells, dev); gro_cells_init(&vxlan->gro_cells, dev);
...@@ -2701,11 +2704,19 @@ static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) ...@@ -2701,11 +2704,19 @@ static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
} }
} }
if (tb[IFLA_MTU]) {
u32 mtu = nla_get_u32(data[IFLA_MTU]);
if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU)
return -EINVAL;
}
if (!data) if (!data)
return -EINVAL; return -EINVAL;
if (data[IFLA_VXLAN_ID]) { if (data[IFLA_VXLAN_ID]) {
__u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
if (id >= VXLAN_N_VID) if (id >= VXLAN_N_VID)
return -ERANGE; return -ERANGE;
} }
...@@ -2866,19 +2877,14 @@ static int vxlan_sock_add(struct vxlan_dev *vxlan) ...@@ -2866,19 +2877,14 @@ static int vxlan_sock_add(struct vxlan_dev *vxlan)
return ret; return ret;
} }
static int vxlan_dev_configure(struct net *src_net, struct net_device *dev, static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
struct vxlan_config *conf, struct net_device **lower,
bool changelink) struct vxlan_dev *old)
{ {
struct vxlan_net *vn = net_generic(src_net, vxlan_net_id); struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
struct vxlan_dev *vxlan = netdev_priv(dev), *tmp; struct vxlan_dev *tmp;
struct vxlan_rdst *dst = &vxlan->default_dst;
unsigned short needed_headroom = ETH_HLEN;
bool use_ipv6 = false; bool use_ipv6 = false;
__be16 default_port = vxlan->cfg.dst_port;
struct net_device *lowerdev = NULL;
if (!changelink) {
if (conf->flags & VXLAN_F_GPE) { if (conf->flags & VXLAN_F_GPE) {
/* For now, allow GPE only together with /* For now, allow GPE only together with
* COLLECT_METADATA. This can be relaxed later; in such * COLLECT_METADATA. This can be relaxed later; in such
...@@ -2887,95 +2893,112 @@ static int vxlan_dev_configure(struct net *src_net, struct net_device *dev, ...@@ -2887,95 +2893,112 @@ static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
*/ */
if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) || if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
!(conf->flags & VXLAN_F_COLLECT_METADATA)) { !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
pr_info("unsupported combination of extensions\n");
return -EINVAL; return -EINVAL;
} }
vxlan_raw_setup(dev);
} else {
vxlan_ether_setup(dev);
} }
/* MTU range: 68 - 65535 */ if (!conf->remote_ip.sa.sa_family)
dev->min_mtu = ETH_MIN_MTU; conf->remote_ip.sa.sa_family = AF_INET;
dev->max_mtu = ETH_MAX_MTU;
vxlan->net = src_net;
}
dst->remote_vni = conf->vni;
memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
/* Unless IPv6 is explicitly requested, assume IPv4 */
if (!dst->remote_ip.sa.sa_family)
dst->remote_ip.sa.sa_family = AF_INET;
if (dst->remote_ip.sa.sa_family == AF_INET6 || if (conf->remote_ip.sa.sa_family == AF_INET6 ||
vxlan->cfg.saddr.sa.sa_family == AF_INET6) { conf->saddr.sa.sa_family == AF_INET6) {
if (!IS_ENABLED(CONFIG_IPV6)) if (!IS_ENABLED(CONFIG_IPV6))
return -EPFNOSUPPORT; return -EPFNOSUPPORT;
use_ipv6 = true; use_ipv6 = true;
vxlan->flags |= VXLAN_F_IPV6; conf->flags |= VXLAN_F_IPV6;
} }
if (conf->label && !use_ipv6) { if (conf->label && !use_ipv6)
pr_info("label only supported in use with IPv6\n");
return -EINVAL; return -EINVAL;
}
if (conf->remote_ifindex && if (conf->remote_ifindex) {
conf->remote_ifindex != vxlan->cfg.remote_ifindex) { struct net_device *lowerdev;
lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
dst->remote_ifindex = conf->remote_ifindex;
if (!lowerdev) { lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
pr_info("ifindex %d does not exist\n", if (!lowerdev)
dst->remote_ifindex);
return -ENODEV; return -ENODEV;
}
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
if (use_ipv6) { if (use_ipv6) {
struct inet6_dev *idev = __in6_dev_get(lowerdev); struct inet6_dev *idev = __in6_dev_get(lowerdev);
if (idev && idev->cnf.disable_ipv6) { if (idev && idev->cnf.disable_ipv6)
pr_info("IPv6 is disabled via sysctl\n");
return -EPERM; return -EPERM;
} }
}
#endif #endif
if (!conf->mtu) *lower = lowerdev;
dev->mtu = lowerdev->mtu - } else {
(use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); if (vxlan_addr_multicast(&conf->remote_ip))
needed_headroom = lowerdev->hard_header_len;
} else if (!conf->remote_ifindex &&
vxlan_addr_multicast(&dst->remote_ip)) {
pr_info("multicast destination requires interface to be specified\n");
return -EINVAL; return -EINVAL;
*lower = NULL;
} }
if (lowerdev) { if (!conf->dst_port) {
dev->gso_max_size = lowerdev->gso_max_size; if (conf->flags & VXLAN_F_GPE)
dev->gso_max_segs = lowerdev->gso_max_segs; conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
else
conf->dst_port = htons(vxlan_port);
} }
if (conf->mtu) { if (!conf->age_interval)
int max_mtu = ETH_MAX_MTU; conf->age_interval = FDB_AGE_DEFAULT;
list_for_each_entry(tmp, &vn->vxlan_list, next) {
if (tmp == old)
continue;
if (tmp->cfg.vni == conf->vni &&
(tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
tmp->cfg.dst_port == conf->dst_port &&
(tmp->flags & VXLAN_F_RCV_FLAGS) ==
(conf->flags & VXLAN_F_RCV_FLAGS))
return -EEXIST;
}
if (lowerdev) return 0;
max_mtu = lowerdev->mtu; }
max_mtu -= (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); static void vxlan_config_apply(struct net_device *dev,
struct vxlan_config *conf,
struct net_device *lowerdev, bool changelink)
{
struct vxlan_dev *vxlan = netdev_priv(dev);
struct vxlan_rdst *dst = &vxlan->default_dst;
unsigned short needed_headroom = ETH_HLEN;
bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
int max_mtu = ETH_MAX_MTU;
if (conf->mtu < dev->min_mtu || conf->mtu > dev->max_mtu) if (!changelink) {
return -EINVAL; if (conf->flags & VXLAN_F_GPE)
vxlan_raw_setup(dev);
else
vxlan_ether_setup(dev);
if (conf->mtu)
dev->mtu = conf->mtu; dev->mtu = conf->mtu;
}
if (conf->mtu > max_mtu) dst->remote_vni = conf->vni;
dev->mtu = max_mtu;
memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
if (lowerdev) {
dst->remote_ifindex = conf->remote_ifindex;
dev->gso_max_size = lowerdev->gso_max_size;
dev->gso_max_segs = lowerdev->gso_max_segs;
needed_headroom = lowerdev->hard_header_len;
max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
VXLAN_HEADROOM);
} }
if (dev->mtu > max_mtu)
dev->mtu = max_mtu;
if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA) if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
needed_headroom += VXLAN6_HEADROOM; needed_headroom += VXLAN6_HEADROOM;
else else
...@@ -2983,31 +3006,22 @@ static int vxlan_dev_configure(struct net *src_net, struct net_device *dev, ...@@ -2983,31 +3006,22 @@ static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
dev->needed_headroom = needed_headroom; dev->needed_headroom = needed_headroom;
memcpy(&vxlan->cfg, conf, sizeof(*conf)); memcpy(&vxlan->cfg, conf, sizeof(*conf));
if (!vxlan->cfg.dst_port) {
if (conf->flags & VXLAN_F_GPE)
vxlan->cfg.dst_port = htons(4790); /* IANA VXLAN-GPE port */
else
vxlan->cfg.dst_port = default_port;
}
vxlan->flags |= conf->flags; vxlan->flags |= conf->flags;
}
if (!vxlan->cfg.age_interval) static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
vxlan->cfg.age_interval = FDB_AGE_DEFAULT; struct vxlan_config *conf,
bool changelink)
{
struct vxlan_dev *vxlan = netdev_priv(dev);
struct net_device *lowerdev;
int ret;
if (changelink) ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan);
return 0; if (ret)
return ret;
list_for_each_entry(tmp, &vn->vxlan_list, next) { vxlan_config_apply(dev, conf, lowerdev, changelink);
if (tmp->cfg.vni == conf->vni &&
(tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
tmp->cfg.dst_port == vxlan->cfg.dst_port &&
(tmp->flags & VXLAN_F_RCV_FLAGS) ==
(vxlan->flags & VXLAN_F_RCV_FLAGS)) {
pr_info("duplicate VNI %u\n", be32_to_cpu(conf->vni));
return -EEXIST;
}
}
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment