diff options
Diffstat (limited to 'drivers/net/vxlan.c')
| -rw-r--r-- | drivers/net/vxlan.c | 683 | 
1 files changed, 481 insertions, 202 deletions
diff --git a/drivers/net/vxlan.c b/drivers/net/vxlan.c index 2ef5b6219f3..9f79192c9aa 100644 --- a/drivers/net/vxlan.c +++ b/drivers/net/vxlan.c @@ -40,6 +40,7 @@  #include <net/net_namespace.h>  #include <net/netns/generic.h>  #include <net/vxlan.h> +#include <net/protocol.h>  #if IS_ENABLED(CONFIG_IPV6)  #include <net/ipv6.h>  #include <net/addrconf.h> @@ -60,10 +61,6 @@  #define VXLAN_N_VID	(1u << 24)  #define VXLAN_VID_MASK	(VXLAN_N_VID - 1) -/* IP header + UDP + VXLAN + Ethernet header */ -#define VXLAN_HEADROOM (20 + 8 + 8 + 14) -/* IPv6 header + UDP + VXLAN + Ethernet header */ -#define VXLAN6_HEADROOM (40 + 8 + 8 + 14)  #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))  #define VXLAN_FLAGS 0x08000000	/* struct vxlanhdr.vx_flags required value. */ @@ -130,6 +127,7 @@ struct vxlan_dev {  	struct list_head  next;		/* vxlan's per namespace list */  	struct vxlan_sock *vn_sock;	/* listening socket */  	struct net_device *dev; +	struct net	  *net;		/* netns for packet i/o */  	struct vxlan_rdst default_dst;	/* default destination */  	union vxlan_addr  saddr;	/* source address */  	__be16		  dst_port; @@ -137,7 +135,7 @@ struct vxlan_dev {  	__u16		  port_max;  	__u8		  tos;		/* TOS override */  	__u8		  ttl; -	u32		  flags;	/* VXLAN_F_* below */ +	u32		  flags;	/* VXLAN_F_* in vxlan.h */  	struct work_struct sock_work;  	struct work_struct igmp_join; @@ -152,13 +150,6 @@ struct vxlan_dev {  	struct hlist_head fdb_head[FDB_HASH_SIZE];  }; -#define VXLAN_F_LEARN	0x01 -#define VXLAN_F_PROXY	0x02 -#define VXLAN_F_RSC	0x04 -#define VXLAN_F_L2MISS	0x08 -#define VXLAN_F_L3MISS	0x10 -#define VXLAN_F_IPV6	0x20 /* internal flag */ -  /* salt for hash table */  static u32 vxlan_salt __read_mostly;  static struct workqueue_struct *vxlan_wq; @@ -348,7 +339,7 @@ static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,  	ndm->ndm_state = fdb->state;  	ndm->ndm_ifindex = vxlan->dev->ifindex;  	ndm->ndm_flags = fdb->flags; -	ndm->ndm_type = NDA_DST; +	ndm->ndm_type = RTN_UNICAST;  	if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))  		goto nla_put_failure; @@ -392,8 +383,8 @@ static inline size_t vxlan_nlmsg_size(void)  		+ nla_total_size(sizeof(struct nda_cacheinfo));  } -static void vxlan_fdb_notify(struct vxlan_dev *vxlan, -			     struct vxlan_fdb *fdb, int type) +static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, +			     struct vxlan_rdst *rd, int type)  {  	struct net *net = dev_net(vxlan->dev);  	struct sk_buff *skb; @@ -403,8 +394,7 @@ static void vxlan_fdb_notify(struct vxlan_dev *vxlan,  	if (skb == NULL)  		goto errout; -	err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, -			     first_remote_rtnl(fdb)); +	err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);  	if (err < 0) {  		/* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */  		WARN_ON(err == -EMSGSIZE); @@ -430,10 +420,7 @@ static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)  		.remote_vni = VXLAN_N_VID,  	}; -	INIT_LIST_HEAD(&f.remotes); -	list_add_rcu(&remote.list, &f.remotes); - -	vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); +	vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);  }  static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) @@ -441,11 +428,11 @@ static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])  	struct vxlan_fdb f = {  		.state = NUD_STALE,  	}; +	struct vxlan_rdst remote = { }; -	INIT_LIST_HEAD(&f.remotes);  	memcpy(f.eth_addr, eth_addr, ETH_ALEN); -	vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); +	vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);  }  /* Hash Ethernet address */ @@ -472,7 +459,6 @@ static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,  /* Look up Ethernet address in forwarding table */  static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,  					const u8 *mac) -  {  	struct hlist_head *head = vxlan_fdb_head(vxlan, mac);  	struct vxlan_fdb *f; @@ -537,7 +523,8 @@ static int vxlan_fdb_replace(struct vxlan_fdb *f,  /* Add/update destinations for multicast */  static int vxlan_fdb_append(struct vxlan_fdb *f, -			    union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex) +			    union vxlan_addr *ip, __be16 port, __u32 vni, +			    __u32 ifindex, struct vxlan_rdst **rdp)  {  	struct vxlan_rdst *rd; @@ -555,16 +542,110 @@ static int vxlan_fdb_append(struct vxlan_fdb *f,  	list_add_tail_rcu(&rd->list, &f->remotes); +	*rdp = rd;  	return 1;  } +static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, struct sk_buff *skb) +{ +	struct sk_buff *p, **pp = NULL; +	struct vxlanhdr *vh, *vh2; +	struct ethhdr *eh, *eh2; +	unsigned int hlen, off_vx, off_eth; +	const struct packet_offload *ptype; +	__be16 type; +	int flush = 1; + +	off_vx = skb_gro_offset(skb); +	hlen = off_vx + sizeof(*vh); +	vh   = skb_gro_header_fast(skb, off_vx); +	if (skb_gro_header_hard(skb, hlen)) { +		vh = skb_gro_header_slow(skb, hlen, off_vx); +		if (unlikely(!vh)) +			goto out; +	} +	skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */ +	skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr)); + +	off_eth = skb_gro_offset(skb); +	hlen = off_eth + sizeof(*eh); +	eh   = skb_gro_header_fast(skb, off_eth); +	if (skb_gro_header_hard(skb, hlen)) { +		eh = skb_gro_header_slow(skb, hlen, off_eth); +		if (unlikely(!eh)) +			goto out; +	} + +	flush = 0; + +	for (p = *head; p; p = p->next) { +		if (!NAPI_GRO_CB(p)->same_flow) +			continue; + +		vh2 = (struct vxlanhdr *)(p->data + off_vx); +		eh2 = (struct ethhdr   *)(p->data + off_eth); +		if (vh->vx_vni != vh2->vx_vni || compare_ether_header(eh, eh2)) { +			NAPI_GRO_CB(p)->same_flow = 0; +			continue; +		} +	} + +	type = eh->h_proto; + +	rcu_read_lock(); +	ptype = gro_find_receive_by_type(type); +	if (ptype == NULL) { +		flush = 1; +		goto out_unlock; +	} + +	skb_gro_pull(skb, sizeof(*eh)); /* pull inner eth header */ +	skb_gro_postpull_rcsum(skb, eh, sizeof(*eh)); +	pp = ptype->callbacks.gro_receive(head, skb); + +out_unlock: +	rcu_read_unlock(); +out: +	NAPI_GRO_CB(skb)->flush |= flush; + +	return pp; +} + +static int vxlan_gro_complete(struct sk_buff *skb, int nhoff) +{ +	struct ethhdr *eh; +	struct packet_offload *ptype; +	__be16 type; +	int vxlan_len  = sizeof(struct vxlanhdr) + sizeof(struct ethhdr); +	int err = -ENOSYS; + +	eh = (struct ethhdr *)(skb->data + nhoff + sizeof(struct vxlanhdr)); +	type = eh->h_proto; + +	rcu_read_lock(); +	ptype = gro_find_complete_by_type(type); +	if (ptype != NULL) +		err = ptype->callbacks.gro_complete(skb, nhoff + vxlan_len); + +	rcu_read_unlock(); +	return err; +} +  /* Notify netdevs that UDP port started listening */ -static void vxlan_notify_add_rx_port(struct sock *sk) +static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)  {  	struct net_device *dev; +	struct sock *sk = vs->sock->sk;  	struct net *net = sock_net(sk);  	sa_family_t sa_family = sk->sk_family;  	__be16 port = inet_sk(sk)->inet_sport; +	int err; + +	if (sa_family == AF_INET) { +		err = udp_add_offload(&vs->udp_offloads); +		if (err) +			pr_warn("vxlan: udp_add_offload failed with status %d\n", err); +	}  	rcu_read_lock();  	for_each_netdev_rcu(net, dev) { @@ -576,9 +657,10 @@ static void vxlan_notify_add_rx_port(struct sock *sk)  }  /* Notify netdevs that UDP port is no more listening */ -static void vxlan_notify_del_rx_port(struct sock *sk) +static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)  {  	struct net_device *dev; +	struct sock *sk = vs->sock->sk;  	struct net *net = sock_net(sk);  	sa_family_t sa_family = sk->sk_family;  	__be16 port = inet_sk(sk)->inet_sport; @@ -590,6 +672,9 @@ static void vxlan_notify_del_rx_port(struct sock *sk)  							    port);  	}  	rcu_read_unlock(); + +	if (sa_family == AF_INET) +		udp_del_offload(&vs->udp_offloads);  }  /* Add new entry to forwarding table -- assumes lock held */ @@ -599,6 +684,7 @@ static int vxlan_fdb_create(struct vxlan_dev *vxlan,  			    __be16 port, __u32 vni, __u32 ifindex,  			    __u8 ndm_flags)  { +	struct vxlan_rdst *rd = NULL;  	struct vxlan_fdb *f;  	int notify = 0; @@ -635,7 +721,8 @@ static int vxlan_fdb_create(struct vxlan_dev *vxlan,  		if ((flags & NLM_F_APPEND) &&  		    (is_multicast_ether_addr(f->eth_addr) ||  		     is_zero_ether_addr(f->eth_addr))) { -			int rc = vxlan_fdb_append(f, ip, port, vni, ifindex); +			int rc = vxlan_fdb_append(f, ip, port, vni, ifindex, +						  &rd);  			if (rc < 0)  				return rc; @@ -665,15 +752,18 @@ static int vxlan_fdb_create(struct vxlan_dev *vxlan,  		INIT_LIST_HEAD(&f->remotes);  		memcpy(f->eth_addr, mac, ETH_ALEN); -		vxlan_fdb_append(f, ip, port, vni, ifindex); +		vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);  		++vxlan->addrcnt;  		hlist_add_head_rcu(&f->hlist,  				   vxlan_fdb_head(vxlan, mac));  	} -	if (notify) -		vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); +	if (notify) { +		if (rd == NULL) +			rd = first_remote_rtnl(f); +		vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH); +	}  	return 0;  } @@ -694,7 +784,7 @@ static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)  		    "delete %pM\n", f->eth_addr);  	--vxlan->addrcnt; -	vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH); +	vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);  	hlist_del_rcu(&f->hlist);  	call_rcu(&f->rcu, vxlan_fdb_free); @@ -745,10 +835,9 @@ static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,  		if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))  			return -EINVAL;  		*ifindex = nla_get_u32(tb[NDA_IFINDEX]); -		tdev = dev_get_by_index(net, *ifindex); +		tdev = __dev_get_by_index(net, *ifindex);  		if (!tdev)  			return -EADDRNOTAVAIL; -		dev_put(tdev);  	} else {  		*ifindex = 0;  	} @@ -781,6 +870,9 @@ static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],  	if (err)  		return err; +	if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family) +		return -EAFNOSUPPORT; +  	spin_lock_bh(&vxlan->hash_lock);  	err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,  			       port, vni, ifindex, ndm->ndm_flags); @@ -826,6 +918,7 @@ static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],  	 */  	if (rd && !list_is_singular(&f->remotes)) {  		list_del_rcu(&rd->list); +		vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);  		kfree_rcu(rd, rcu);  		goto out;  	} @@ -900,7 +993,7 @@ static bool vxlan_snoop(struct net_device *dev,  		rdst->remote_ip = *src_ip;  		f->updated = jiffies; -		vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); +		vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);  	} else {  		/* learned new entry */  		spin_lock(&vxlan->hash_lock); @@ -920,17 +1013,32 @@ static bool vxlan_snoop(struct net_device *dev,  }  /* See if multicast group is already in use by other ID */ -static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip) +static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)  {  	struct vxlan_dev *vxlan; +	/* The vxlan_sock is only used by dev, leaving group has +	 * no effect on other vxlan devices. +	 */ +	if (atomic_read(&dev->vn_sock->refcnt) == 1) +		return false; +  	list_for_each_entry(vxlan, &vn->vxlan_list, next) { -		if (!netif_running(vxlan->dev)) +		if (!netif_running(vxlan->dev) || vxlan == dev)  			continue; -		if (vxlan_addr_equal(&vxlan->default_dst.remote_ip, -				     remote_ip)) -			return true; +		if (vxlan->vn_sock != dev->vn_sock) +			continue; + +		if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip, +				      &dev->default_dst.remote_ip)) +			continue; + +		if (vxlan->default_dst.remote_ifindex != +		    dev->default_dst.remote_ifindex) +			continue; + +		return true;  	}  	return false; @@ -953,7 +1061,7 @@ void vxlan_sock_release(struct vxlan_sock *vs)  	spin_lock(&vn->sock_lock);  	hlist_del_rcu(&vs->hlist);  	rcu_assign_sk_user_data(vs->sock->sk, NULL); -	vxlan_notify_del_rx_port(sk); +	vxlan_notify_del_rx_port(vs);  	spin_unlock(&vn->sock_lock);  	queue_work(vxlan_wq, &vs->del_work); @@ -1027,7 +1135,6 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)  {  	struct vxlan_sock *vs;  	struct vxlanhdr *vxh; -	__be16 port;  	/* Need Vxlan and inner Ethernet header to be present */  	if (!pskb_may_pull(skb, VXLAN_HLEN)) @@ -1045,12 +1152,12 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)  	if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))  		goto drop; -	port = inet_sk(sk)->inet_sport; -  	vs = rcu_dereference_sk_user_data(sk);  	if (!vs)  		goto drop; +	skb_pop_rcv_encapsulation(skb); +  	vs->rcv(vs, skb, vxh->vx_vni);  	return 0; @@ -1070,7 +1177,7 @@ static void vxlan_rcv(struct vxlan_sock *vs,  	struct iphdr *oip = NULL;  	struct ipv6hdr *oip6 = NULL;  	struct vxlan_dev *vxlan; -	struct pcpu_tstats *stats; +	struct pcpu_sw_netstats *stats;  	union vxlan_addr saddr;  	__u32 vni;  	int err = 0; @@ -1084,7 +1191,9 @@ static void vxlan_rcv(struct vxlan_sock *vs,  	remote_ip = &vxlan->default_dst.remote_ip;  	skb_reset_mac_header(skb); +	skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));  	skb->protocol = eth_type_trans(skb, vxlan->dev); +	skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);  	/* Ignore packet loops (and multicast echo) */  	if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr)) @@ -1109,17 +1218,6 @@ static void vxlan_rcv(struct vxlan_sock *vs,  	skb_reset_network_header(skb); -	/* If the NIC driver gave us an encapsulated packet with -	 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled, -	 * leave the CHECKSUM_UNNECESSARY, the device checksummed it -	 * for us. Otherwise force the upper layers to verify it. -	 */ -	if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation || -	    !(vxlan->dev->features & NETIF_F_RXCSUM)) -		skb->ip_summed = CHECKSUM_NONE; - -	skb->encapsulation = 0; -  	if (oip6)  		err = IP6_ECN_decapsulate(oip6, skb);  	if (oip) @@ -1214,6 +1312,9 @@ static int arp_reduce(struct net_device *dev, struct sk_buff *skb)  		neigh_release(n); +		if (reply == NULL) +			goto out; +  		skb_reset_mac_header(reply);  		__skb_pull(reply, skb_network_offset(reply));  		reply->ip_summed = CHECKSUM_UNNECESSARY; @@ -1235,15 +1336,103 @@ out:  }  #if IS_ENABLED(CONFIG_IPV6) + +static struct sk_buff *vxlan_na_create(struct sk_buff *request, +	struct neighbour *n, bool isrouter) +{ +	struct net_device *dev = request->dev; +	struct sk_buff *reply; +	struct nd_msg *ns, *na; +	struct ipv6hdr *pip6; +	u8 *daddr; +	int na_olen = 8; /* opt hdr + ETH_ALEN for target */ +	int ns_olen; +	int i, len; + +	if (dev == NULL) +		return NULL; + +	len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) + +		sizeof(*na) + na_olen + dev->needed_tailroom; +	reply = alloc_skb(len, GFP_ATOMIC); +	if (reply == NULL) +		return NULL; + +	reply->protocol = htons(ETH_P_IPV6); +	reply->dev = dev; +	skb_reserve(reply, LL_RESERVED_SPACE(request->dev)); +	skb_push(reply, sizeof(struct ethhdr)); +	skb_set_mac_header(reply, 0); + +	ns = (struct nd_msg *)skb_transport_header(request); + +	daddr = eth_hdr(request)->h_source; +	ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns); +	for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) { +		if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) { +			daddr = ns->opt + i + sizeof(struct nd_opt_hdr); +			break; +		} +	} + +	/* Ethernet header */ +	ether_addr_copy(eth_hdr(reply)->h_dest, daddr); +	ether_addr_copy(eth_hdr(reply)->h_source, n->ha); +	eth_hdr(reply)->h_proto = htons(ETH_P_IPV6); +	reply->protocol = htons(ETH_P_IPV6); + +	skb_pull(reply, sizeof(struct ethhdr)); +	skb_set_network_header(reply, 0); +	skb_put(reply, sizeof(struct ipv6hdr)); + +	/* IPv6 header */ + +	pip6 = ipv6_hdr(reply); +	memset(pip6, 0, sizeof(struct ipv6hdr)); +	pip6->version = 6; +	pip6->priority = ipv6_hdr(request)->priority; +	pip6->nexthdr = IPPROTO_ICMPV6; +	pip6->hop_limit = 255; +	pip6->daddr = ipv6_hdr(request)->saddr; +	pip6->saddr = *(struct in6_addr *)n->primary_key; + +	skb_pull(reply, sizeof(struct ipv6hdr)); +	skb_set_transport_header(reply, 0); + +	na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen); + +	/* Neighbor Advertisement */ +	memset(na, 0, sizeof(*na)+na_olen); +	na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; +	na->icmph.icmp6_router = isrouter; +	na->icmph.icmp6_override = 1; +	na->icmph.icmp6_solicited = 1; +	na->target = ns->target; +	ether_addr_copy(&na->opt[2], n->ha); +	na->opt[0] = ND_OPT_TARGET_LL_ADDR; +	na->opt[1] = na_olen >> 3; + +	na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr, +		&pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6, +		csum_partial(na, sizeof(*na)+na_olen, 0)); + +	pip6->payload_len = htons(sizeof(*na)+na_olen); + +	skb_push(reply, sizeof(struct ipv6hdr)); + +	reply->ip_summed = CHECKSUM_UNNECESSARY; + +	return reply; +} +  static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)  {  	struct vxlan_dev *vxlan = netdev_priv(dev); -	struct neighbour *n; -	union vxlan_addr ipa; +	struct nd_msg *msg;  	const struct ipv6hdr *iphdr;  	const struct in6_addr *saddr, *daddr; -	struct nd_msg *msg; -	struct inet6_dev *in6_dev = NULL; +	struct neighbour *n; +	struct inet6_dev *in6_dev;  	in6_dev = __in6_dev_get(dev);  	if (!in6_dev) @@ -1256,19 +1445,20 @@ static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)  	saddr = &iphdr->saddr;  	daddr = &iphdr->daddr; -	if (ipv6_addr_loopback(daddr) || -	    ipv6_addr_is_multicast(daddr)) -		goto out; -  	msg = (struct nd_msg *)skb_transport_header(skb);  	if (msg->icmph.icmp6_code != 0 ||  	    msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)  		goto out; -	n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev); +	if (ipv6_addr_loopback(daddr) || +	    ipv6_addr_is_multicast(&msg->target)) +		goto out; + +	n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);  	if (n) {  		struct vxlan_fdb *f; +		struct sk_buff *reply;  		if (!(n->nud_state & NUD_CONNECTED)) {  			neigh_release(n); @@ -1282,13 +1472,23 @@ static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)  			goto out;  		} -		ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target, -					 !!in6_dev->cnf.forwarding, -					 true, false, false); +		reply = vxlan_na_create(skb, n, +					!!(f ? f->flags & NTF_ROUTER : 0)); +  		neigh_release(n); + +		if (reply == NULL) +			goto out; + +		if (netif_rx_ni(reply) == NET_RX_DROP) +			dev->stats.rx_dropped++; +  	} else if (vxlan->flags & VXLAN_F_L3MISS) { -		ipa.sin6.sin6_addr = *daddr; -		ipa.sa.sa_family = AF_INET6; +		union vxlan_addr ipa = { +			.sin6.sin6_addr = msg->target, +			.sa.sa_family = AF_INET6, +		}; +  		vxlan_ip_miss(dev, &ipa);  	} @@ -1370,20 +1570,6 @@ static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)  	return false;  } -static void vxlan_sock_put(struct sk_buff *skb) -{ -	sock_put(skb->sk); -} - -/* On transmit, associate with the tunnel socket */ -static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb) -{ -	skb_orphan(skb); -	sock_hold(sk); -	skb->sk = sk; -	skb->destructor = vxlan_sock_put; -} -  /* Compute source port for outgoing packet   *   first choice to use L4 flow hash since it will spread   *     better and maybe available from hardware @@ -1394,7 +1580,7 @@ __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)  	unsigned int range = (port_max - port_min) + 1;  	u32 hash; -	hash = skb_get_rxhash(skb); +	hash = skb_get_hash(skb);  	if (!hash)  		hash = jhash(skb->data, 2 * ETH_ALEN,  			     (__force u32) skb->protocol); @@ -1403,18 +1589,11 @@ __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)  }  EXPORT_SYMBOL_GPL(vxlan_src_port); -static int handle_offloads(struct sk_buff *skb) +static inline struct sk_buff *vxlan_handle_offloads(struct sk_buff *skb, +						    bool udp_csum)  { -	if (skb_is_gso(skb)) { -		int err = skb_unclone(skb, GFP_ATOMIC); -		if (unlikely(err)) -			return err; - -		skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; -	} else if (skb->ip_summed != CHECKSUM_PARTIAL) -		skb->ip_summed = CHECKSUM_NONE; - -	return 0; +	int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; +	return iptunnel_handle_offloads(skb, udp_csum, type);  }  #if IS_ENABLED(CONFIG_IPV6) @@ -1422,7 +1601,8 @@ static int vxlan6_xmit_skb(struct vxlan_sock *vs,  			   struct dst_entry *dst, struct sk_buff *skb,  			   struct net_device *dev, struct in6_addr *saddr,  			   struct in6_addr *daddr, __u8 prio, __u8 ttl, -			   __be16 src_port, __be16 dst_port, __be32 vni) +			   __be16 src_port, __be16 dst_port, __be32 vni, +			   bool xnet)  {  	struct ipv6hdr *ip6h;  	struct vxlanhdr *vxh; @@ -1430,12 +1610,11 @@ static int vxlan6_xmit_skb(struct vxlan_sock *vs,  	int min_headroom;  	int err; -	if (!skb->encapsulation) { -		skb_reset_inner_headers(skb); -		skb->encapsulation = 1; -	} +	skb = vxlan_handle_offloads(skb, !udp_get_no_check6_tx(vs->sock->sk)); +	if (IS_ERR(skb)) +		return -EINVAL; -	skb_scrub_packet(skb, false); +	skb_scrub_packet(skb, xnet);  	min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len  			+ VXLAN_HLEN + sizeof(struct ipv6hdr) @@ -1467,27 +1646,14 @@ static int vxlan6_xmit_skb(struct vxlan_sock *vs,  	uh->source = src_port;  	uh->len = htons(skb->len); -	uh->check = 0;  	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));  	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |  			      IPSKB_REROUTED);  	skb_dst_set(skb, dst); -	if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) { -		__wsum csum = skb_checksum(skb, 0, skb->len, 0); -		skb->ip_summed = CHECKSUM_UNNECESSARY; -		uh->check = csum_ipv6_magic(saddr, daddr, skb->len, -					    IPPROTO_UDP, csum); -		if (uh->check == 0) -			uh->check = CSUM_MANGLED_0; -	} else { -		skb->ip_summed = CHECKSUM_PARTIAL; -		skb->csum_start = skb_transport_header(skb) - skb->head; -		skb->csum_offset = offsetof(struct udphdr, check); -		uh->check = ~csum_ipv6_magic(saddr, daddr, -					     skb->len, IPPROTO_UDP, 0); -	} +	udp6_set_csum(udp_get_no_check6_tx(vs->sock->sk), skb, +		      saddr, daddr, skb->len);  	__skb_push(skb, sizeof(*ip6h));  	skb_reset_network_header(skb); @@ -1503,12 +1669,6 @@ static int vxlan6_xmit_skb(struct vxlan_sock *vs,  	ip6h->daddr	  = *daddr;  	ip6h->saddr	  = *saddr; -	vxlan_set_owner(vs->sock->sk, skb); - -	err = handle_offloads(skb); -	if (err) -		return err; -  	ip6tunnel_xmit(skb, dev);  	return 0;  } @@ -1517,17 +1677,16 @@ static int vxlan6_xmit_skb(struct vxlan_sock *vs,  int vxlan_xmit_skb(struct vxlan_sock *vs,  		   struct rtable *rt, struct sk_buff *skb,  		   __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, -		   __be16 src_port, __be16 dst_port, __be32 vni) +		   __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)  {  	struct vxlanhdr *vxh;  	struct udphdr *uh;  	int min_headroom;  	int err; -	if (!skb->encapsulation) { -		skb_reset_inner_headers(skb); -		skb->encapsulation = 1; -	} +	skb = vxlan_handle_offloads(skb, !vs->sock->sk->sk_no_check_tx); +	if (IS_ERR(skb)) +		return -EINVAL;  	min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len  			+ VXLAN_HLEN + sizeof(struct iphdr) @@ -1559,16 +1718,12 @@ int vxlan_xmit_skb(struct vxlan_sock *vs,  	uh->source = src_port;  	uh->len = htons(skb->len); -	uh->check = 0; -	vxlan_set_owner(vs->sock->sk, skb); +	udp_set_csum(vs->sock->sk->sk_no_check_tx, skb, +		     src, dst, skb->len); -	err = handle_offloads(skb); -	if (err) -		return err; - -	return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, -			     false); +	return iptunnel_xmit(vs->sock->sk, rt, skb, src, dst, IPPROTO_UDP, +			     tos, ttl, df, xnet);  }  EXPORT_SYMBOL_GPL(vxlan_xmit_skb); @@ -1576,11 +1731,12 @@ EXPORT_SYMBOL_GPL(vxlan_xmit_skb);  static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,  			       struct vxlan_dev *dst_vxlan)  { -	struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); -	struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); +	struct pcpu_sw_netstats *tx_stats, *rx_stats;  	union vxlan_addr loopback;  	union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip; +	tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); +	rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);  	skb->pkt_type = PACKET_HOST;  	skb->encapsulation = 0;  	skb->dev = dst_vxlan->dev; @@ -1660,7 +1816,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,  		fl4.daddr = dst->sin.sin_addr.s_addr;  		fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr; -		rt = ip_route_output_key(dev_net(dev), &fl4); +		rt = ip_route_output_key(vxlan->net, &fl4);  		if (IS_ERR(rt)) {  			netdev_dbg(dev, "no route to %pI4\n",  				   &dst->sin.sin_addr.s_addr); @@ -1672,7 +1828,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,  			netdev_dbg(dev, "circular route to %pI4\n",  				   &dst->sin.sin_addr.s_addr);  			dev->stats.collisions++; -			goto tx_error; +			goto rt_tx_error;  		}  		/* Bypass encapsulation if the destination is local */ @@ -1681,7 +1837,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,  			struct vxlan_dev *dst_vxlan;  			ip_rt_put(rt); -			dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port); +			dst_vxlan = vxlan_find_vni(vxlan->net, vni, dst_port);  			if (!dst_vxlan)  				goto tx_error;  			vxlan_encap_bypass(skb, vxlan, dst_vxlan); @@ -1694,7 +1850,8 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,  		err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,  				     fl4.saddr, dst->sin.sin_addr.s_addr,  				     tos, ttl, df, src_port, dst_port, -				     htonl(vni << 8)); +				     htonl(vni << 8), +				     !net_eq(vxlan->net, dev_net(vxlan->dev)));  		if (err < 0)  			goto rt_tx_error; @@ -1734,7 +1891,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,  			struct vxlan_dev *dst_vxlan;  			dst_release(ndst); -			dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port); +			dst_vxlan = vxlan_find_vni(vxlan->net, vni, dst_port);  			if (!dst_vxlan)  				goto tx_error;  			vxlan_encap_bypass(skb, vxlan, dst_vxlan); @@ -1745,7 +1902,8 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,  		err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,  				      dev, &fl6.saddr, &fl6.daddr, 0, ttl, -				      src_port, dst_port, htonl(vni << 8)); +				      src_port, dst_port, htonl(vni << 8), +				      !net_eq(vxlan->net, dev_net(vxlan->dev)));  #endif  	} @@ -1774,7 +1932,7 @@ static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)  	struct vxlan_dev *vxlan = netdev_priv(dev);  	struct ethhdr *eth;  	bool did_rsc = false; -	struct vxlan_rdst *rdst; +	struct vxlan_rdst *rdst, *fdst = NULL;  	struct vxlan_fdb *f;  	skb_reset_mac_header(skb); @@ -1816,7 +1974,7 @@ static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)  				vxlan_fdb_miss(vxlan, eth->h_dest);  			dev->stats.tx_dropped++; -			dev_kfree_skb(skb); +			kfree_skb(skb);  			return NETDEV_TX_OK;  		}  	} @@ -1824,12 +1982,19 @@ static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)  	list_for_each_entry_rcu(rdst, &f->remotes, list) {  		struct sk_buff *skb1; +		if (!fdst) { +			fdst = rdst; +			continue; +		}  		skb1 = skb_clone(skb, GFP_ATOMIC);  		if (skb1)  			vxlan_xmit_one(skb1, dev, rdst, did_rsc);  	} -	dev_kfree_skb(skb); +	if (fdst) +		vxlan_xmit_one(skb, dev, fdst, did_rsc); +	else +		kfree_skb(skb);  	return NETDEV_TX_OK;  } @@ -1882,15 +2047,15 @@ static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)  static int vxlan_init(struct net_device *dev)  {  	struct vxlan_dev *vxlan = netdev_priv(dev); -	struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); +	struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);  	struct vxlan_sock *vs; -	dev->tstats = alloc_percpu(struct pcpu_tstats); +	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);  	if (!dev->tstats)  		return -ENOMEM;  	spin_lock(&vn->sock_lock); -	vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port); +	vs = vxlan_find_sock(vxlan->net, vxlan->dst_port);  	if (vs) {  		/* If we have a socket with same port already, reuse it */  		atomic_inc(&vs->refcnt); @@ -1931,7 +2096,6 @@ static void vxlan_uninit(struct net_device *dev)  /* Start ageing timer and join group when device is brought up */  static int vxlan_open(struct net_device *dev)  { -	struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);  	struct vxlan_dev *vxlan = netdev_priv(dev);  	struct vxlan_sock *vs = vxlan->vn_sock; @@ -1939,8 +2103,7 @@ static int vxlan_open(struct net_device *dev)  	if (!vs)  		return -ENOTCONN; -	if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && -	    vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) { +	if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {  		vxlan_sock_hold(vs);  		dev_hold(dev);  		queue_work(vxlan_wq, &vxlan->igmp_join); @@ -1974,12 +2137,12 @@ static void vxlan_flush(struct vxlan_dev *vxlan)  /* Cleanup timer and forwarding table on shutdown */  static int vxlan_stop(struct net_device *dev)  { -	struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);  	struct vxlan_dev *vxlan = netdev_priv(dev); +	struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);  	struct vxlan_sock *vs = vxlan->vn_sock;  	if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && -	    ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) { +	    !vxlan_group_used(vn, vxlan)) {  		vxlan_sock_hold(vs);  		dev_hold(dev);  		queue_work(vxlan_wq, &vxlan->igmp_leave); @@ -1997,6 +2160,29 @@ static void vxlan_set_multicast_list(struct net_device *dev)  {  } +static int vxlan_change_mtu(struct net_device *dev, int new_mtu) +{ +	struct vxlan_dev *vxlan = netdev_priv(dev); +	struct vxlan_rdst *dst = &vxlan->default_dst; +	struct net_device *lowerdev; +	int max_mtu; + +	lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex); +	if (lowerdev == NULL) +		return eth_change_mtu(dev, new_mtu); + +	if (dst->remote_ip.sa.sa_family == AF_INET6) +		max_mtu = lowerdev->mtu - VXLAN6_HEADROOM; +	else +		max_mtu = lowerdev->mtu - VXLAN_HEADROOM; + +	if (new_mtu < 68 || new_mtu > max_mtu) +		return -EINVAL; + +	dev->mtu = new_mtu; +	return 0; +} +  static const struct net_device_ops vxlan_netdev_ops = {  	.ndo_init		= vxlan_init,  	.ndo_uninit		= vxlan_uninit, @@ -2005,7 +2191,7 @@ static const struct net_device_ops vxlan_netdev_ops = {  	.ndo_start_xmit		= vxlan_xmit,  	.ndo_get_stats64	= ip_tunnel_get_stats64,  	.ndo_set_rx_mode	= vxlan_set_multicast_list, -	.ndo_change_mtu		= eth_change_mtu, +	.ndo_change_mtu		= vxlan_change_mtu,  	.ndo_validate_addr	= eth_validate_addr,  	.ndo_set_mac_address	= eth_mac_addr,  	.ndo_fdb_add		= vxlan_fdb_add, @@ -2054,9 +2240,9 @@ static void vxlan_setup(struct net_device *dev)  	eth_hw_addr_random(dev);  	ether_setup(dev);  	if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6) -		dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM; +		dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;  	else -		dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM; +		dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;  	dev->netdev_ops = &vxlan_netdev_ops;  	dev->destructor = free_netdev; @@ -2064,7 +2250,6 @@ static void vxlan_setup(struct net_device *dev)  	dev->tx_queue_len = 0;  	dev->features	|= NETIF_F_LLTX; -	dev->features	|= NETIF_F_NETNS_LOCAL;  	dev->features	|= NETIF_F_SG | NETIF_F_HW_CSUM;  	dev->features   |= NETIF_F_RXCSUM;  	dev->features   |= NETIF_F_GSO_SOFTWARE; @@ -2087,7 +2272,7 @@ static void vxlan_setup(struct net_device *dev)  	vxlan->age_timer.function = vxlan_cleanup;  	vxlan->age_timer.data = (unsigned long) vxlan; -	inet_get_local_port_range(&low, &high); +	inet_get_local_port_range(dev_net(dev), &low, &high);  	vxlan->port_min = low;  	vxlan->port_max = high;  	vxlan->dst_port = htons(vxlan_port); @@ -2180,7 +2365,7 @@ static void vxlan_del_work(struct work_struct *work)   * could be used for both IPv4 and IPv6 communications, but   * users may set bindv6only=1.   */ -static int create_v6_sock(struct net *net, __be16 port, struct socket **psock) +static struct socket *create_v6_sock(struct net *net, __be16 port, u32 flags)  {  	struct sock *sk;  	struct socket *sock; @@ -2193,7 +2378,7 @@ static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)  	rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);  	if (rc < 0) {  		pr_debug("UDPv6 socket create failed\n"); -		return rc; +		return ERR_PTR(rc);  	}  	/* Put in proper namespace */ @@ -2208,28 +2393,34 @@ static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)  		pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",  			 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);  		sk_release_kernel(sk); -		return rc; +		return ERR_PTR(rc);  	}  	/* At this point, IPv6 module should have been loaded in  	 * sock_create_kern().  	 */  	BUG_ON(!ipv6_stub); -	*psock = sock;  	/* Disable multicast loopback */  	inet_sk(sk)->mc_loop = 0; -	return 0; + +	if (flags & VXLAN_F_UDP_ZERO_CSUM6_TX) +		udp_set_no_check6_tx(sk, true); + +	if (flags & VXLAN_F_UDP_ZERO_CSUM6_RX) +		udp_set_no_check6_rx(sk, true); + +	return sock;  }  #else -static int create_v6_sock(struct net *net, __be16 port, struct socket **psock) +static struct socket *create_v6_sock(struct net *net, __be16 port, u32 flags)  { -		return -EPFNOSUPPORT; +		return ERR_PTR(-EPFNOSUPPORT);  }  #endif -static int create_v4_sock(struct net *net, __be16 port, struct socket **psock) +static struct socket *create_v4_sock(struct net *net, __be16 port, u32 flags)  {  	struct sock *sk;  	struct socket *sock; @@ -2244,7 +2435,7 @@ static int create_v4_sock(struct net *net, __be16 port, struct socket **psock)  	rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);  	if (rc < 0) {  		pr_debug("UDP socket create failed\n"); -		return rc; +		return ERR_PTR(rc);  	}  	/* Put in proper namespace */ @@ -2257,27 +2448,31 @@ static int create_v4_sock(struct net *net, __be16 port, struct socket **psock)  		pr_debug("bind for UDP socket %pI4:%u (%d)\n",  			 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);  		sk_release_kernel(sk); -		return rc; +		return ERR_PTR(rc);  	} -	*psock = sock;  	/* Disable multicast loopback */  	inet_sk(sk)->mc_loop = 0; -	return 0; + +	if (!(flags & VXLAN_F_UDP_CSUM)) +		sock->sk->sk_no_check_tx = 1; + +	return sock;  }  /* Create new listen socket if needed */  static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port, -					      vxlan_rcv_t *rcv, void *data, bool ipv6) +					      vxlan_rcv_t *rcv, void *data, +					      u32 flags)  {  	struct vxlan_net *vn = net_generic(net, vxlan_net_id);  	struct vxlan_sock *vs;  	struct socket *sock;  	struct sock *sk; -	int rc = 0;  	unsigned int h; +	bool ipv6 = !!(flags & VXLAN_F_IPV6); -	vs = kmalloc(sizeof(*vs), GFP_KERNEL); +	vs = kzalloc(sizeof(*vs), GFP_KERNEL);  	if (!vs)  		return ERR_PTR(-ENOMEM); @@ -2287,12 +2482,12 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,  	INIT_WORK(&vs->del_work, vxlan_del_work);  	if (ipv6) -		rc = create_v6_sock(net, port, &sock); +		sock = create_v6_sock(net, port, flags);  	else -		rc = create_v4_sock(net, port, &sock); -	if (rc < 0) { +		sock = create_v4_sock(net, port, flags); +	if (IS_ERR(sock)) {  		kfree(vs); -		return ERR_PTR(rc); +		return ERR_CAST(sock);  	}  	vs->sock = sock; @@ -2302,9 +2497,14 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,  	vs->data = data;  	rcu_assign_sk_user_data(vs->sock->sk, vs); +	/* Initialize the vxlan udp offloads structure */ +	vs->udp_offloads.port = port; +	vs->udp_offloads.callbacks.gro_receive  = vxlan_gro_receive; +	vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete; +  	spin_lock(&vn->sock_lock);  	hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); -	vxlan_notify_add_rx_port(sk); +	vxlan_notify_add_rx_port(vs);  	spin_unlock(&vn->sock_lock);  	/* Mark socket as an encapsulation socket. */ @@ -2322,12 +2522,12 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,  struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,  				  vxlan_rcv_t *rcv, void *data, -				  bool no_share, bool ipv6) +				  bool no_share, u32 flags)  {  	struct vxlan_net *vn = net_generic(net, vxlan_net_id);  	struct vxlan_sock *vs; -	vs = vxlan_socket_create(net, port, rcv, data, ipv6); +	vs = vxlan_socket_create(net, port, rcv, data, flags);  	if (!IS_ERR(vs))  		return vs; @@ -2355,12 +2555,12 @@ EXPORT_SYMBOL_GPL(vxlan_sock_add);  static void vxlan_sock_work(struct work_struct *work)  {  	struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work); -	struct net *net = dev_net(vxlan->dev); +	struct net *net = vxlan->net;  	struct vxlan_net *vn = net_generic(net, vxlan_net_id);  	__be16 port = vxlan->dst_port;  	struct vxlan_sock *nvs; -	nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6); +	nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);  	spin_lock(&vn->sock_lock);  	if (!IS_ERR(nvs))  		vxlan_vs_add_dev(nvs, vxlan); @@ -2382,12 +2582,15 @@ static int vxlan_newlink(struct net *net, struct net_device *dev,  	if (!data[IFLA_VXLAN_ID])  		return -EINVAL; +	vxlan->net = dev_net(dev); +  	vni = nla_get_u32(data[IFLA_VXLAN_ID]);  	dst->remote_vni = vni; +	/* Unless IPv6 is explicitly requested, assume IPv4 */ +	dst->remote_ip.sa.sa_family = AF_INET;  	if (data[IFLA_VXLAN_GROUP]) {  		dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]); -		dst->remote_ip.sa.sa_family = AF_INET;  	} else if (data[IFLA_VXLAN_GROUP6]) {  		if (!IS_ENABLED(CONFIG_IPV6))  			return -EPFNOSUPPORT; @@ -2436,10 +2639,10 @@ static int vxlan_newlink(struct net *net, struct net_device *dev,  		if (!tb[IFLA_MTU])  			dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); -		/* update header length based on lower device */ -		dev->hard_header_len = lowerdev->hard_header_len + +		dev->needed_headroom = lowerdev->hard_header_len +  				       (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); -	} +	} else if (use_ipv6) +		vxlan->flags |= VXLAN_F_IPV6;  	if (data[IFLA_VXLAN_TOS])  		vxlan->tos  = nla_get_u8(data[IFLA_VXLAN_TOS]); @@ -2480,12 +2683,23 @@ static int vxlan_newlink(struct net *net, struct net_device *dev,  	if (data[IFLA_VXLAN_PORT])  		vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); +	if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM])) +		vxlan->flags |= VXLAN_F_UDP_CSUM; + +	if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] && +	    nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX])) +		vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX; + +	if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] && +	    nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX])) +		vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX; +  	if (vxlan_find_vni(net, vni, vxlan->dst_port)) {  		pr_info("duplicate VNI %u\n", vni);  		return -EEXIST;  	} -	SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops); +	dev->ethtool_ops = &vxlan_ethtool_ops;  	/* create an fdb entry for a valid default destination */  	if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) { @@ -2514,8 +2728,8 @@ static int vxlan_newlink(struct net *net, struct net_device *dev,  static void vxlan_dellink(struct net_device *dev, struct list_head *head)  { -	struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);  	struct vxlan_dev *vxlan = netdev_priv(dev); +	struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);  	spin_lock(&vn->sock_lock);  	if (!hlist_unhashed(&vxlan->hlist)) @@ -2543,7 +2757,10 @@ static size_t vxlan_get_size(const struct net_device *dev)  		nla_total_size(sizeof(__u32)) +	/* IFLA_VXLAN_AGEING */  		nla_total_size(sizeof(__u32)) +	/* IFLA_VXLAN_LIMIT */  		nla_total_size(sizeof(struct ifla_vxlan_port_range)) + -		nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */ +		nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */ +		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */ +		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */ +		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */  		0;  } @@ -2603,7 +2820,13 @@ static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)  			!!(vxlan->flags & VXLAN_F_L3MISS)) ||  	    nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||  	    nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) || -	    nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port)) +	    nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) || +	    nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM, +			!!(vxlan->flags & VXLAN_F_UDP_CSUM)) || +	    nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, +			!!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) || +	    nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, +			!!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)))  		goto nla_put_failure;  	if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) @@ -2628,6 +2851,44 @@ static struct rtnl_link_ops vxlan_link_ops __read_mostly = {  	.fill_info	= vxlan_fill_info,  }; +static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn, +					     struct net_device *dev) +{ +	struct vxlan_dev *vxlan, *next; +	LIST_HEAD(list_kill); + +	list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { +		struct vxlan_rdst *dst = &vxlan->default_dst; + +		/* In case we created vxlan device with carrier +		 * and we loose the carrier due to module unload +		 * we also need to remove vxlan device. In other +		 * cases, it's not necessary and remote_ifindex +		 * is 0 here, so no matches. +		 */ +		if (dst->remote_ifindex == dev->ifindex) +			vxlan_dellink(vxlan->dev, &list_kill); +	} + +	unregister_netdevice_many(&list_kill); +} + +static int vxlan_lowerdev_event(struct notifier_block *unused, +				unsigned long event, void *ptr) +{ +	struct net_device *dev = netdev_notifier_info_to_dev(ptr); +	struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); + +	if (event == NETDEV_UNREGISTER) +		vxlan_handle_lowerdev_unregister(vn, dev); + +	return NOTIFY_DONE; +} + +static struct notifier_block vxlan_notifier_block __read_mostly = { +	.notifier_call = vxlan_lowerdev_event, +}; +  static __net_init int vxlan_init_net(struct net *net)  {  	struct vxlan_net *vn = net_generic(net, vxlan_net_id); @@ -2642,15 +2903,26 @@ static __net_init int vxlan_init_net(struct net *net)  	return 0;  } -static __net_exit void vxlan_exit_net(struct net *net) +static void __net_exit vxlan_exit_net(struct net *net)  {  	struct vxlan_net *vn = net_generic(net, vxlan_net_id); -	struct vxlan_dev *vxlan; +	struct vxlan_dev *vxlan, *next; +	struct net_device *dev, *aux;  	LIST_HEAD(list);  	rtnl_lock(); -	list_for_each_entry(vxlan, &vn->vxlan_list, next) -		unregister_netdevice_queue(vxlan->dev, &list); +	for_each_netdev_safe(net, dev, aux) +		if (dev->rtnl_link_ops == &vxlan_link_ops) +			unregister_netdevice_queue(dev, &list); + +	list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { +		/* If vxlan->dev is in the same netns, it has already been added +		 * to the list by the previous loop. +		 */ +		if (!net_eq(dev_net(vxlan->dev), net)) +			unregister_netdevice_queue(dev, &list); +	} +  	unregister_netdevice_many(&list);  	rtnl_unlock();  } @@ -2672,18 +2944,23 @@ static int __init vxlan_init_module(void)  	get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); -	rc = register_pernet_device(&vxlan_net_ops); +	rc = register_pernet_subsys(&vxlan_net_ops);  	if (rc)  		goto out1; -	rc = rtnl_link_register(&vxlan_link_ops); +	rc = register_netdevice_notifier(&vxlan_notifier_block);  	if (rc)  		goto out2; -	return 0; +	rc = rtnl_link_register(&vxlan_link_ops); +	if (rc) +		goto out3; +	return 0; +out3: +	unregister_netdevice_notifier(&vxlan_notifier_block);  out2: -	unregister_pernet_device(&vxlan_net_ops); +	unregister_pernet_subsys(&vxlan_net_ops);  out1:  	destroy_workqueue(vxlan_wq);  	return rc; @@ -2693,13 +2970,15 @@ late_initcall(vxlan_init_module);  static void __exit vxlan_cleanup_module(void)  {  	rtnl_link_unregister(&vxlan_link_ops); +	unregister_netdevice_notifier(&vxlan_notifier_block);  	destroy_workqueue(vxlan_wq); -	unregister_pernet_device(&vxlan_net_ops); -	rcu_barrier(); +	unregister_pernet_subsys(&vxlan_net_ops); +	/* rcu_barrier() is called by netns */  }  module_exit(vxlan_cleanup_module);  MODULE_LICENSE("GPL");  MODULE_VERSION(VXLAN_VERSION);  MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); +MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");  MODULE_ALIAS_RTNL_LINK("vxlan");  | 
