2 * VXLAN: Virtual eXtensible Local Area Network
4 * Copyright (c) 2012-2013 Vyatta Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/rculist.h>
20 #include <linux/netdevice.h>
21 #include <linux/netdev_features.h>
24 #include <linux/udp.h>
25 #include <linux/igmp.h>
26 #include <linux/etherdevice.h>
27 #include <linux/if_ether.h>
28 #include <linux/if_vlan.h>
29 #include <linux/hash.h>
30 #include <linux/ethtool.h>
32 #include <net/dst_metadata.h>
33 #include <net/ndisc.h>
35 #include <net/ip_tunnels.h>
38 #include <net/udp_tunnel.h>
39 #include <net/rtnetlink.h>
40 #include <net/route.h>
41 #include <net/dsfield.h>
42 #include <net/inet_ecn.h>
43 #include <net/net_namespace.h>
44 #include <net/netns/generic.h>
45 #include <net/protocol.h>
47 #if IS_ENABLED(CONFIG_IPV6)
49 #include <net/addrconf.h>
50 #include <net/ip6_tunnel.h>
51 #include <net/ip6_checksum.h>
52 #include <net/ip6_route.h>
55 #include <net/vxlan.h>
57 #include "vport-netdev.h"
60 #ifndef USE_UPSTREAM_TUNNEL
61 #define VXLAN_VERSION "0.1"
63 #define PORT_HASH_BITS 8
64 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
65 #define FDB_AGE_DEFAULT 300 /* 5 min */
66 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
68 /* UDP port for VXLAN traffic.
69 * The IANA assigned port is 4789, but the Linux default is 8472
70 * for compatibility with early adopters.
72 static unsigned short vxlan_port __read_mostly = 8472;
73 module_param_named(udp_port, vxlan_port, ushort, 0444);
74 MODULE_PARM_DESC(udp_port, "Destination UDP port");
76 static int vxlan_net_id;
77 static struct rtnl_link_ops vxlan_link_ops;
79 static const u8 all_zeros_mac[ETH_ALEN + 2];
81 static int vxlan_sock_add(struct vxlan_dev *vxlan);
83 /* per-network namespace private data for this module */
85 struct list_head vxlan_list;
86 struct hlist_head sock_list[PORT_HASH_SIZE];
90 /* Forwarding table entry */
92 struct hlist_node hlist; /* linked list of entries */
94 unsigned long updated; /* jiffies */
96 struct list_head remotes;
97 u8 eth_addr[ETH_ALEN];
98 u16 state; /* see ndm_state */
99 u8 flags; /* see ndm_flags */
102 /* salt for hash table */
103 static u32 vxlan_salt __read_mostly;
105 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
107 return vs->flags & VXLAN_F_COLLECT_METADATA ||
108 ip_tunnel_collect_metadata();
111 #if IS_ENABLED(CONFIG_IPV6)
113 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
115 if (a->sa.sa_family != b->sa.sa_family)
117 if (a->sa.sa_family == AF_INET6)
118 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
120 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
123 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
125 if (ipa->sa.sa_family == AF_INET6)
126 return ipv6_addr_any(&ipa->sin6.sin6_addr);
128 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
131 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
133 if (ipa->sa.sa_family == AF_INET6)
134 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
136 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
139 #else /* !CONFIG_IPV6 */
142 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
144 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
147 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
149 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
152 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
154 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
158 /* Virtual Network hash table head */
159 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
161 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
164 /* Socket hash table head */
165 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
167 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
169 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
172 /* Find VXLAN socket based on network namespace, address family and UDP port
173 * and enabled unshareable flags.
175 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
176 __be16 port, u32 flags)
178 struct vxlan_sock *vs;
180 flags &= VXLAN_F_RCV_FLAGS;
182 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
183 if (inet_sk(vs->sock->sk)->inet_sport == port &&
184 vxlan_get_sk_family(vs) == family &&
191 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni)
193 struct vxlan_dev *vxlan;
195 /* For flow based devices, map all packets to VNI 0 */
196 if (vs->flags & VXLAN_F_COLLECT_METADATA)
199 hlist_for_each_entry_rcu(vxlan, vni_head(vs, vni), hlist) {
200 if (vxlan->default_dst.remote_vni == vni)
207 /* Look up VNI in a per net namespace table */
208 static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni,
209 sa_family_t family, __be16 port,
212 struct vxlan_sock *vs;
214 vs = vxlan_find_sock(net, family, port, flags);
218 return vxlan_vs_find_vni(vs, vni);
221 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
222 const u8 *mac, union vxlan_addr *ip,
223 __u16 state, __u16 flags,
224 __be16 port, __be32 vni, __u32 ifindex,
230 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
235 static inline size_t vxlan_nlmsg_size(void)
237 return NLMSG_ALIGN(sizeof(struct ndmsg))
238 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
239 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
240 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
241 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
242 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
243 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
244 + nla_total_size(sizeof(struct nda_cacheinfo));
247 #ifdef HAVE_UDP_OFFLOAD
248 #ifdef HAVE_NETIF_F_GSO_TUNNEL_REMCSUM
250 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
252 struct vxlanhdr *vh, size_t hdrlen,
254 struct gro_remcsum *grc,
257 size_t start, offset;
259 if (skb->remcsum_offload)
262 if (!NAPI_GRO_CB(skb)->csum_valid)
265 start = vxlan_rco_start(vni_field);
266 offset = start + vxlan_rco_offset(vni_field);
268 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
269 start, offset, grc, nopartial);
271 skb->remcsum_offload = 1;
276 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
278 struct vxlanhdr *vh, size_t hdrlen,
279 u32 data, struct gro_remcsum *grc,
286 #ifndef HAVE_UDP_OFFLOAD_ARG_UOFF
287 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
290 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
292 struct udp_offload *uoff)
295 #ifdef HAVE_UDP_OFFLOAD_ARG_UOFF
296 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
299 struct vxlan_sock *vs = NULL;
301 struct sk_buff *p, **pp = NULL;
302 struct vxlanhdr *vh, *vh2;
303 unsigned int hlen, off_vx;
306 struct gro_remcsum grc;
308 skb_gro_remcsum_init(&grc);
310 off_vx = skb_gro_offset(skb);
311 hlen = off_vx + sizeof(*vh);
312 vh = skb_gro_header_fast(skb, off_vx);
313 if (skb_gro_header_hard(skb, hlen)) {
314 vh = skb_gro_header_slow(skb, hlen, off_vx);
319 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
321 flags = vh->vx_flags;
323 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
324 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
327 VXLAN_F_REMCSUM_NOPARTIAL));
333 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
335 for (p = *head; p; p = p->next) {
336 if (!NAPI_GRO_CB(p)->same_flow)
339 vh2 = (struct vxlanhdr *)(p->data + off_vx);
340 if (vh->vx_flags != vh2->vx_flags ||
341 vh->vx_vni != vh2->vx_vni) {
342 NAPI_GRO_CB(p)->same_flow = 0;
347 pp = eth_gro_receive(head, skb);
351 skb_gro_remcsum_cleanup(skb, &grc);
352 NAPI_GRO_CB(skb)->flush |= flush;
357 #ifndef HAVE_UDP_OFFLOAD_ARG_UOFF
358 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff)
360 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
361 struct udp_offload *uoff)
364 /* Sets 'skb->inner_mac_header' since we are always called with
365 * 'skb->encapsulation' set.
367 udp_tunnel_gro_complete(skb, nhoff);
369 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
373 /* Notify netdevs that UDP port started listening */
374 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
376 #ifdef HAVE_NDO_ADD_VXLAN_PORT
377 struct net_device *dev;
378 struct sock *sk = vs->sock->sk;
379 struct net *net = sock_net(sk);
380 sa_family_t sa_family = vxlan_get_sk_family(vs);
381 __be16 port = inet_sk(sk)->inet_sport;
384 for_each_netdev_rcu(net, dev) {
385 if (dev->netdev_ops->ndo_add_vxlan_port)
386 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
392 #ifdef HAVE_UDP_OFFLOAD
393 struct net_device *dev;
394 struct sock *sk = vs->sock->sk;
395 sa_family_t sa_family = vxlan_get_sk_family(vs);
397 if (sa_family == AF_INET) {
400 err = udp_add_offload(&vs->udp_offloads);
402 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
409 /* Notify netdevs that UDP port is no more listening */
410 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
412 #ifdef HAVE_NDO_ADD_VXLAN_PORT
413 struct net_device *dev;
414 struct sock *sk = vs->sock->sk;
415 struct net *net = sock_net(sk);
416 sa_family_t sa_family = vxlan_get_sk_family(vs);
417 __be16 port = inet_sk(sk)->inet_sport;
420 for_each_netdev_rcu(net, dev) {
421 if (dev->netdev_ops->ndo_del_vxlan_port)
422 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
427 #ifdef HAVE_UDP_OFFLOAD
428 struct sock *sk = vs->sock->sk;
429 sa_family_t sa_family = vxlan_get_sk_family(vs);
431 if (sa_family == AF_INET) {
432 udp_del_offload(&vs->udp_offloads);
437 /* See if multicast group is already in use by other ID */
438 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
440 struct vxlan_dev *vxlan;
441 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
443 /* The vxlan_sock is only used by dev, leaving group has
444 * no effect on other vxlan devices.
446 if (family == AF_INET && dev->vn4_sock &&
447 atomic_read(&dev->vn4_sock->refcnt) == 1)
449 #if IS_ENABLED(CONFIG_IPV6)
450 if (family == AF_INET6 && dev->vn6_sock &&
451 atomic_read(&dev->vn6_sock->refcnt) == 1)
455 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
456 if (!netif_running(vxlan->dev) || vxlan == dev)
459 if (family == AF_INET && vxlan->vn4_sock != dev->vn4_sock)
461 #if IS_ENABLED(CONFIG_IPV6)
462 if (family == AF_INET6 && vxlan->vn6_sock != dev->vn6_sock)
466 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
467 &dev->default_dst.remote_ip))
470 if (vxlan->default_dst.remote_ifindex !=
471 dev->default_dst.remote_ifindex)
480 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
482 struct vxlan_net *vn;
486 if (!atomic_dec_and_test(&vs->refcnt))
489 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
490 spin_lock(&vn->sock_lock);
491 hlist_del_rcu(&vs->hlist);
492 vxlan_notify_del_rx_port(vs);
493 spin_unlock(&vn->sock_lock);
498 static void vxlan_sock_release(struct vxlan_dev *vxlan)
500 bool ipv4 = __vxlan_sock_release_prep(vxlan->vn4_sock);
501 #if IS_ENABLED(CONFIG_IPV6)
502 bool ipv6 = __vxlan_sock_release_prep(vxlan->vn6_sock);
508 udp_tunnel_sock_release(vxlan->vn4_sock->sock);
509 kfree(vxlan->vn4_sock);
512 #if IS_ENABLED(CONFIG_IPV6)
514 udp_tunnel_sock_release(vxlan->vn6_sock->sock);
515 kfree(vxlan->vn6_sock);
520 /* Update multicast group membership when first VNI on
521 * multicast address is brought up
523 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
528 /* Inverse of vxlan_igmp_join when last VNI is brought down */
529 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
534 static bool vxlan_remcsum(struct vxlanhdr *unparsed,
535 struct sk_buff *skb, u32 vxflags)
537 #ifndef USE_UPSTREAM_TUNNEL
540 size_t start, offset;
542 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
545 start = vxlan_rco_start(unparsed->vx_vni);
546 offset = start + vxlan_rco_offset(unparsed->vx_vni);
548 if (!pskb_may_pull(skb, offset + sizeof(u16)))
551 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
552 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
554 unparsed->vx_flags &= ~VXLAN_HF_RCO;
555 unparsed->vx_vni &= VXLAN_VNI_MASK;
560 static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
561 struct sk_buff *skb, u32 vxflags,
562 struct vxlan_metadata *md)
564 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
565 struct metadata_dst *tun_dst;
567 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
570 md->gbp = ntohs(gbp->policy_id);
572 tun_dst = (struct metadata_dst *)skb_dst(skb);
574 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
575 tun_dst->u.tun_info.options_len = sizeof(*md);
578 md->gbp |= VXLAN_GBP_DONT_LEARN;
580 if (gbp->policy_applied)
581 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
583 /* In flow-based mode, GBP is carried in dst_metadata */
584 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
587 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
590 static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
592 struct sk_buff *skb, u32 vxflags)
594 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
596 /* Need to have Next Protocol set for interfaces in GPE mode. */
597 if (!gpe->np_applied)
599 /* "The initial version is 0. If a receiver does not support the
600 * version indicated it MUST drop the packet.
602 if (gpe->version != 0)
604 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
605 * processing MUST occur." However, we don't implement OAM
606 * processing, thus drop the packet.
611 switch (gpe->next_protocol) {
612 case VXLAN_GPE_NP_IPV4:
613 *protocol = htons(ETH_P_IP);
615 case VXLAN_GPE_NP_IPV6:
616 *protocol = htons(ETH_P_IPV6);
618 case VXLAN_GPE_NP_ETHERNET:
619 *protocol = htons(ETH_P_TEB);
625 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
629 static bool vxlan_set_mac(struct vxlan_dev *vxlan,
630 struct vxlan_sock *vs,
636 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
641 if (vxlan_get_sk_family(vs) == AF_INET)
642 err = IP_ECN_decapsulate(oiph, skb);
643 #if IS_ENABLED(CONFIG_IPV6)
645 err = IP6_ECN_decapsulate(oiph, skb);
650 /* Callback from net/ipv4/udp.c to receive packets */
651 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
654 struct metadata_dst dst;
655 char buf[sizeof(struct metadata_dst) + sizeof(struct vxlan_metadata)];
658 struct pcpu_sw_netstats *stats;
659 struct vxlan_dev *vxlan;
660 struct vxlan_sock *vs;
661 struct vxlanhdr unparsed;
662 struct vxlan_metadata _md;
663 struct vxlan_metadata *md = &_md;
664 __be16 protocol = htons(ETH_P_TEB);
665 bool raw_proto = false;
668 /* Need UDP and VXLAN header to be present */
669 if (!pskb_may_pull(skb, VXLAN_HLEN))
672 unparsed = *vxlan_hdr(skb);
673 /* VNI flag always required to be set */
674 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
675 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
676 ntohl(vxlan_hdr(skb)->vx_flags),
677 ntohl(vxlan_hdr(skb)->vx_vni));
678 /* Return non vxlan pkt */
682 unparsed.vx_flags &= ~VXLAN_HF_VNI;
683 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
685 vs = rcu_dereference_sk_user_data(sk);
689 #if IS_ENABLED(CONFIG_IPV6)
690 #ifdef OVS_CHECK_UDP_TUNNEL_ZERO_CSUM
691 if (vxlan_get_sk_family(vs) == AF_INET6 &&
692 !udp_hdr(skb)->check &&
693 !(vs->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) {
694 udp6_csum_zero_error(skb);
699 vxlan = vxlan_vs_find_vni(vs, vxlan_vni(vxlan_hdr(skb)->vx_vni));
703 /* For backwards compatibility, only allow reserved fields to be
704 * used by VXLAN extensions if explicitly requested.
706 if (vs->flags & VXLAN_F_GPE) {
707 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
712 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
713 !net_eq(vxlan->net, dev_net(vxlan->dev))))
716 if (vxlan_collect_metadata(vs)) {
717 __be32 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
718 struct metadata_dst *tun_dst;
721 ovs_udp_tun_rx_dst(tun_dst, skb,
722 vxlan_get_sk_family(vs), TUNNEL_KEY,
723 vxlan_vni_to_tun_id(vni), sizeof(*md));
728 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
730 ovs_skb_dst_set(skb, (struct dst_entry *)tun_dst);
732 memset(md, 0, sizeof(*md));
735 if (vs->flags & VXLAN_F_REMCSUM_RX)
736 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
739 if (vs->flags & VXLAN_F_GBP)
740 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
741 /* Note that GBP and GPE can never be active together. This is
742 * ensured in vxlan_dev_configure.
745 if (unparsed.vx_flags || unparsed.vx_vni) {
746 /* If there are any unprocessed flags remaining treat
747 * this as a malformed packet. This behavior diverges from
748 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
749 * in reserved fields are to be ignored. The approach here
750 * maintains compatibility with previous stack code, and also
751 * is more robust and provides a little more security in
752 * adding extensions to VXLAN.
758 if (!vxlan_set_mac(vxlan, vs, skb))
760 skb_reset_mac_header(skb);
761 skb->protocol = eth_type_trans(skb, vxlan->dev);
762 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
764 skb_reset_mac_header(skb);
765 skb->dev = vxlan->dev;
766 skb->pkt_type = PACKET_HOST;
769 oiph = skb_network_header(skb);
770 skb_reset_network_header(skb);
772 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
773 ++vxlan->dev->stats.rx_frame_errors;
774 ++vxlan->dev->stats.rx_errors;
778 stats = this_cpu_ptr(vxlan->dev->tstats);
779 u64_stats_update_begin(&stats->syncp);
781 stats->rx_bytes += skb->len;
782 u64_stats_update_end(&stats->syncp);
784 netdev_port_receive(skb, skb_tunnel_info(skb));
788 /* Consume bad packet */
793 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
794 struct vxlan_metadata *md)
796 struct vxlanhdr_gbp *gbp;
801 gbp = (struct vxlanhdr_gbp *)vxh;
802 vxh->vx_flags |= VXLAN_HF_GBP;
804 if (md->gbp & VXLAN_GBP_DONT_LEARN)
807 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
808 gbp->policy_applied = 1;
810 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
813 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
816 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
821 case htons(ETH_P_IP):
822 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
824 case htons(ETH_P_IPV6):
825 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
827 case htons(ETH_P_TEB):
828 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
831 return -EPFNOSUPPORT;
834 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
835 int iphdr_len, __be32 vni,
836 struct vxlan_metadata *md, u32 vxflags,
839 void (*fix_segment)(struct sk_buff *);
840 struct vxlanhdr *vxh;
843 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
844 __be16 inner_protocol = htons(ETH_P_TEB);
846 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
847 skb->ip_summed == CHECKSUM_PARTIAL) {
848 int csum_start = skb_checksum_start_offset(skb);
850 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
851 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
852 (skb->csum_offset == offsetof(struct udphdr, check) ||
853 skb->csum_offset == offsetof(struct tcphdr, check)))
854 type |= SKB_GSO_TUNNEL_REMCSUM;
857 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
858 + VXLAN_HLEN + iphdr_len
859 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
861 /* Need space for new headers (invalidates iph ptr) */
862 err = skb_cow_head(skb, min_headroom);
866 skb = vlan_hwaccel_push_inside(skb);
870 type |= udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
871 fix_segment = udp_sum ? ovs_udp_gso : ovs_udp_csum_gso;
872 err = ovs_iptunnel_handle_offloads(skb, udp_sum, type, fix_segment);
876 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
877 vxh->vx_flags = VXLAN_HF_VNI;
878 vxh->vx_vni = vxlan_vni_field(vni);
880 if (type & SKB_GSO_TUNNEL_REMCSUM) {
883 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
884 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
885 vxh->vx_flags |= VXLAN_HF_RCO;
887 if (!skb_is_gso(skb)) {
888 skb->ip_summed = CHECKSUM_NONE;
889 skb->encapsulation = 0;
893 if (vxflags & VXLAN_F_GBP)
894 vxlan_build_gbp_hdr(vxh, vxflags, md);
895 if (vxflags & VXLAN_F_GPE) {
896 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
899 inner_protocol = skb->protocol;
902 ovs_skb_set_inner_protocol(skb, inner_protocol);
910 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan,
911 struct sk_buff *skb, int oif, u8 tos,
912 __be32 daddr, __be32 *saddr,
913 struct dst_cache *dst_cache,
914 const struct ip_tunnel_info *info)
916 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
917 struct rtable *rt = NULL;
923 rt = dst_cache_get_ip4(dst_cache, saddr);
928 memset(&fl4, 0, sizeof(fl4));
929 fl4.flowi4_oif = oif;
930 fl4.flowi4_tos = RT_TOS(tos);
931 fl4.flowi4_mark = skb->mark;
932 fl4.flowi4_proto = IPPROTO_UDP;
934 fl4.saddr = vxlan->cfg.saddr.sin.sin_addr.s_addr;
936 rt = ip_route_output_key(vxlan->net, &fl4);
940 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
945 #if IS_ENABLED(CONFIG_IPV6)
946 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
947 struct sk_buff *skb, int oif, u8 tos,
949 const struct in6_addr *daddr,
950 struct in6_addr *saddr,
951 struct dst_cache *dst_cache,
952 const struct ip_tunnel_info *info)
954 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
955 struct dst_entry *ndst;
962 ndst = dst_cache_get_ip6(dst_cache, saddr);
967 memset(&fl6, 0, sizeof(fl6));
968 fl6.flowi6_oif = oif;
970 fl6.saddr = vxlan->cfg.saddr.sin6.sin6_addr;
971 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
972 fl6.flowi6_mark = skb->mark;
973 fl6.flowi6_proto = IPPROTO_UDP;
975 #ifdef HAVE_IPV6_DST_LOOKUP_NET
976 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
977 vxlan->vn6_sock->sock->sk,
980 #ifdef HAVE_IPV6_STUB
981 err = ipv6_stub->ipv6_dst_lookup(vxlan->vn6_sock->sock->sk,
984 err = ip6_dst_lookup(vxlan->vn6_sock->sock->sk, &ndst, &fl6);
992 dst_cache_set_ip6(dst_cache, ndst, saddr);
997 /* Bypass encapsulation if the destination is local */
998 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
999 struct vxlan_dev *dst_vxlan)
1001 skb->dev->stats.rx_dropped++;
1005 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1006 struct vxlan_rdst *rdst, bool did_rsc)
1008 struct dst_cache *dst_cache;
1009 struct ip_tunnel_info *info;
1010 struct vxlan_dev *vxlan = netdev_priv(dev);
1012 struct rtable *rt = NULL;
1013 const struct iphdr *old_iph;
1014 union vxlan_addr *dst;
1015 union vxlan_addr remote_ip;
1016 struct vxlan_metadata _md;
1017 struct vxlan_metadata *md = &_md;
1018 __be16 src_port = 0, dst_port;
1023 u32 flags = vxlan->flags;
1024 bool udp_sum = false;
1025 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
1027 info = skb_tunnel_info(skb);
1030 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
1031 vni = rdst->remote_vni;
1032 dst = &rdst->remote_ip;
1033 dst_cache = &rdst->dst_cache;
1036 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1040 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
1041 vni = vxlan_tun_id_to_vni(info->key.tun_id);
1042 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
1043 if (remote_ip.sa.sa_family == AF_INET)
1044 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
1046 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
1048 dst_cache = &info->dst_cache;
1051 if (vxlan_addr_any(dst)) {
1053 /* short-circuited back to local bridge */
1054 vxlan_encap_bypass(skb, vxlan, vxlan);
1060 old_iph = ip_hdr(skb);
1062 ttl = vxlan->cfg.ttl;
1063 if (!ttl && vxlan_addr_multicast(dst))
1066 tos = vxlan->cfg.tos;
1068 tos = ip_tunnel_get_dsfield(old_iph, skb);
1070 label = vxlan->cfg.label;
1071 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
1072 vxlan->cfg.port_max, true);
1075 ttl = info->key.ttl;
1076 tos = info->key.tos;
1077 label = info->key.label;
1078 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
1080 if (info->options_len)
1081 md = ip_tunnel_info_opts(info);
1083 md->gbp = skb->mark;
1086 if (dst->sa.sa_family == AF_INET) {
1089 if (!vxlan->vn4_sock)
1091 sk = vxlan->vn4_sock->sock->sk;
1093 rt = vxlan_get_route(vxlan, skb,
1094 rdst ? rdst->remote_ifindex : 0, tos,
1095 dst->sin.sin_addr.s_addr, &saddr,
1098 netdev_dbg(dev, "no route to %pI4\n",
1099 &dst->sin.sin_addr.s_addr);
1100 dev->stats.tx_carrier_errors++;
1104 if (rt->dst.dev == dev) {
1105 netdev_dbg(dev, "circular route to %pI4\n",
1106 &dst->sin.sin_addr.s_addr);
1107 dev->stats.collisions++;
1111 /* Bypass encapsulation if the destination is local */
1112 if (rt->rt_flags & RTCF_LOCAL &&
1113 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1114 struct vxlan_dev *dst_vxlan;
1117 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1118 dst->sa.sa_family, dst_port,
1122 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1127 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
1128 else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
1131 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1132 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1133 err = vxlan_build_skb(skb, &rt->dst, sizeof(struct iphdr),
1134 vni, md, flags, udp_sum);
1138 udp_tunnel_xmit_skb(rt, sk, skb, saddr,
1139 dst->sin.sin_addr.s_addr, tos, ttl, df,
1140 src_port, dst_port, xnet, !udp_sum);
1141 #if IS_ENABLED(CONFIG_IPV6)
1143 struct dst_entry *ndst;
1144 struct in6_addr saddr;
1147 if (!vxlan->vn6_sock)
1149 sk = vxlan->vn6_sock->sock->sk;
1151 ndst = vxlan6_get_route(vxlan, skb,
1152 rdst ? rdst->remote_ifindex : 0, tos,
1153 label, &dst->sin6.sin6_addr, &saddr,
1156 netdev_dbg(dev, "no route to %pI6\n",
1157 &dst->sin6.sin6_addr);
1158 dev->stats.tx_carrier_errors++;
1162 if (ndst->dev == dev) {
1163 netdev_dbg(dev, "circular route to %pI6\n",
1164 &dst->sin6.sin6_addr);
1166 dev->stats.collisions++;
1170 /* Bypass encapsulation if the destination is local */
1171 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
1172 if (rt6i_flags & RTF_LOCAL &&
1173 !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1174 struct vxlan_dev *dst_vxlan;
1177 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1178 dst->sa.sa_family, dst_port,
1182 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1187 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
1189 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1190 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1191 skb_scrub_packet(skb, xnet);
1192 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
1193 vni, md, flags, udp_sum);
1198 udp_tunnel6_xmit_skb(ndst, sk, skb, dev,
1199 &saddr, &dst->sin6.sin6_addr, tos, ttl,
1200 label, src_port, dst_port, !udp_sum);
1207 dev->stats.tx_dropped++;
1211 /* skb is already freed. */
1216 dev->stats.tx_errors++;
1221 /* Transmit local packets over Vxlan
1223 * Outer IP header inherits ECN and DF from inner header.
1224 * Outer UDP destination is the VXLAN assigned port.
1225 * source port is based on hash of flow
1227 netdev_tx_t rpl_vxlan_xmit(struct sk_buff *skb)
1229 struct net_device *dev = skb->dev;
1230 struct vxlan_dev *vxlan = netdev_priv(dev);
1231 const struct ip_tunnel_info *info;
1233 info = skb_tunnel_info(skb);
1234 skb_reset_mac_header(skb);
1235 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
1236 if (info && info->mode & IP_TUNNEL_INFO_TX) {
1237 vxlan_xmit_one(skb, dev, NULL, false);
1238 return NETDEV_TX_OK;
1242 dev->stats.tx_dropped++;
1244 return NETDEV_TX_OK;
1246 EXPORT_SYMBOL_GPL(rpl_vxlan_xmit);
1248 /* Walk the forwarding table and purge stale entries */
1249 static void vxlan_cleanup(unsigned long arg)
1251 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1252 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1255 if (!netif_running(vxlan->dev))
1258 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1259 struct hlist_node *p, *n;
1261 spin_lock_bh(&vxlan->hash_lock);
1262 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1264 = container_of(p, struct vxlan_fdb, hlist);
1265 unsigned long timeout;
1267 if (f->state & NUD_PERMANENT)
1270 timeout = f->used + vxlan->cfg.age_interval * HZ;
1271 if (time_before_eq(timeout, jiffies)) {
1272 netdev_dbg(vxlan->dev,
1273 "garbage collect %pM\n",
1275 f->state = NUD_STALE;
1276 vxlan_fdb_destroy(vxlan, f);
1277 } else if (time_before(timeout, next_timer))
1278 next_timer = timeout;
1280 spin_unlock_bh(&vxlan->hash_lock);
1283 mod_timer(&vxlan->age_timer, next_timer);
1286 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1288 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1289 __be32 vni = vxlan->default_dst.remote_vni;
1291 spin_lock(&vn->sock_lock);
1292 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1293 spin_unlock(&vn->sock_lock);
1296 /* Setup stats when device is created */
1297 static int vxlan_init(struct net_device *dev)
1299 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1306 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
1310 static void vxlan_uninit(struct net_device *dev)
1312 struct vxlan_dev *vxlan = netdev_priv(dev);
1314 vxlan_fdb_delete_default(vxlan);
1316 free_percpu(dev->tstats);
1319 /* Start ageing timer and join group when device is brought up */
1320 static int vxlan_open(struct net_device *dev)
1322 struct vxlan_dev *vxlan = netdev_priv(dev);
1325 ret = vxlan_sock_add(vxlan);
1329 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
1330 ret = vxlan_igmp_join(vxlan);
1331 if (ret == -EADDRINUSE)
1334 vxlan_sock_release(vxlan);
1339 if (vxlan->cfg.age_interval)
1340 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1345 /* Purge the forwarding table */
1346 static void vxlan_flush(struct vxlan_dev *vxlan)
1350 spin_lock_bh(&vxlan->hash_lock);
1351 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1352 struct hlist_node *p, *n;
1353 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1355 = container_of(p, struct vxlan_fdb, hlist);
1356 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1357 if (!is_zero_ether_addr(f->eth_addr))
1358 vxlan_fdb_destroy(vxlan, f);
1361 spin_unlock_bh(&vxlan->hash_lock);
1364 /* Cleanup timer and forwarding table on shutdown */
1365 static int vxlan_stop(struct net_device *dev)
1367 struct vxlan_dev *vxlan = netdev_priv(dev);
1368 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1371 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1372 !vxlan_group_used(vn, vxlan))
1373 ret = vxlan_igmp_leave(vxlan);
1375 del_timer_sync(&vxlan->age_timer);
1378 vxlan_sock_release(vxlan);
1383 /* Stub, nothing needs to be done. */
1384 static void vxlan_set_multicast_list(struct net_device *dev)
1388 static int __vxlan_change_mtu(struct net_device *dev,
1389 struct net_device *lowerdev,
1390 struct vxlan_rdst *dst, int new_mtu, bool strict)
1392 int max_mtu = IP_MAX_MTU;
1395 max_mtu = lowerdev->mtu;
1397 if (dst->remote_ip.sa.sa_family == AF_INET6)
1398 max_mtu -= VXLAN6_HEADROOM;
1400 max_mtu -= VXLAN_HEADROOM;
1405 if (new_mtu > max_mtu) {
1416 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
1418 struct vxlan_dev *vxlan = netdev_priv(dev);
1419 struct vxlan_rdst *dst = &vxlan->default_dst;
1420 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
1421 dst->remote_ifindex);
1422 return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true);
1425 int ovs_vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1427 struct vxlan_dev *vxlan = netdev_priv(dev);
1428 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1429 __be16 sport, dport;
1431 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
1432 vxlan->cfg.port_max, true);
1433 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
1435 if (ip_tunnel_info_af(info) == AF_INET) {
1438 if (!vxlan->vn4_sock)
1440 rt = vxlan_get_route(vxlan, skb, 0, info->key.tos,
1441 info->key.u.ipv4.dst,
1442 &info->key.u.ipv4.src, NULL, info);
1447 #if IS_ENABLED(CONFIG_IPV6)
1448 struct dst_entry *ndst;
1450 if (!vxlan->vn6_sock)
1452 ndst = vxlan6_get_route(vxlan, skb, 0, info->key.tos,
1453 info->key.label, &info->key.u.ipv6.dst,
1454 &info->key.u.ipv6.src, NULL, info);
1456 return PTR_ERR(ndst);
1458 #else /* !CONFIG_IPV6 */
1459 return -EPFNOSUPPORT;
1462 info->key.tp_src = sport;
1463 info->key.tp_dst = dport;
1466 EXPORT_SYMBOL_GPL(ovs_vxlan_fill_metadata_dst);
1468 static netdev_tx_t vxlan_dev_xmit(struct sk_buff *skb, struct net_device *dev)
1470 /* Drop All packets coming from networking stack. OVS-CB is
1471 * not initialized for these packets.
1475 dev->stats.tx_dropped++;
1476 return NETDEV_TX_OK;
1479 static const struct net_device_ops vxlan_netdev_ether_ops = {
1480 .ndo_init = vxlan_init,
1481 .ndo_uninit = vxlan_uninit,
1482 .ndo_open = vxlan_open,
1483 .ndo_stop = vxlan_stop,
1484 .ndo_start_xmit = vxlan_dev_xmit,
1485 .ndo_get_stats64 = ip_tunnel_get_stats64,
1486 .ndo_set_rx_mode = vxlan_set_multicast_list,
1487 .ndo_change_mtu = vxlan_change_mtu,
1488 .ndo_validate_addr = eth_validate_addr,
1489 .ndo_set_mac_address = eth_mac_addr,
1490 #ifdef HAVE_NDO_FILL_METADATA_DST
1491 .ndo_fill_metadata_dst = ovs_vxlan_fill_metadata_dst,
1495 static const struct net_device_ops vxlan_netdev_raw_ops = {
1496 .ndo_init = vxlan_init,
1497 .ndo_uninit = vxlan_uninit,
1498 .ndo_open = vxlan_open,
1499 .ndo_stop = vxlan_stop,
1500 .ndo_start_xmit = vxlan_dev_xmit,
1501 .ndo_get_stats64 = ip_tunnel_get_stats64,
1502 .ndo_change_mtu = vxlan_change_mtu,
1503 #ifdef HAVE_NDO_FILL_METADATA_DST
1504 .ndo_fill_metadata_dst = ovs_vxlan_fill_metadata_dst,
1508 /* Info for udev, that this is a virtual tunnel endpoint */
1509 static struct device_type vxlan_type = {
1513 /* Calls the ndo_add_vxlan_port of the caller in order to
1514 * supply the listening VXLAN udp ports. Callers are expected
1515 * to implement the ndo_add_vxlan_port.
1517 static void vxlan_push_rx_ports(struct net_device *dev)
1519 #ifdef HAVE_NDO_ADD_VXLAN_PORT
1520 struct vxlan_sock *vs;
1521 struct net *net = dev_net(dev);
1522 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1523 sa_family_t sa_family;
1527 if (!dev->netdev_ops->ndo_add_vxlan_port)
1530 spin_lock(&vn->sock_lock);
1531 for (i = 0; i < PORT_HASH_SIZE; ++i) {
1532 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
1533 port = inet_sk(vs->sock->sk)->inet_sport;
1534 sa_family = vxlan_get_sk_family(vs);
1535 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
1539 spin_unlock(&vn->sock_lock);
1543 /* Initialize the device structure. */
1544 static void vxlan_setup(struct net_device *dev)
1546 struct vxlan_dev *vxlan = netdev_priv(dev);
1549 eth_hw_addr_random(dev);
1552 dev->destructor = free_netdev;
1553 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1555 dev->features |= NETIF_F_LLTX;
1556 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1557 dev->features |= NETIF_F_RXCSUM;
1558 dev->features |= NETIF_F_GSO_SOFTWARE;
1560 dev->vlan_features = dev->features;
1561 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
1562 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1563 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1564 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
1566 netif_keep_dst(dev);
1568 dev->priv_flags |= IFF_NO_QUEUE;
1570 INIT_LIST_HEAD(&vxlan->next);
1571 spin_lock_init(&vxlan->hash_lock);
1573 init_timer_deferrable(&vxlan->age_timer);
1574 vxlan->age_timer.function = vxlan_cleanup;
1575 vxlan->age_timer.data = (unsigned long) vxlan;
1577 vxlan->cfg.dst_port = htons(vxlan_port);
1581 for (h = 0; h < FDB_HASH_SIZE; ++h)
1582 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1585 static void vxlan_ether_setup(struct net_device *dev)
1587 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1588 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1589 dev->netdev_ops = &vxlan_netdev_ether_ops;
1592 static void vxlan_raw_setup(struct net_device *dev)
1594 dev->header_ops = NULL;
1595 dev->type = ARPHRD_NONE;
1596 dev->hard_header_len = 0;
1598 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1599 dev->netdev_ops = &vxlan_netdev_raw_ops;
1602 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1603 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1604 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1605 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
1606 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1607 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1608 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
1609 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1610 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1611 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
1612 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1613 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1614 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
1615 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
1616 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1617 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1618 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1619 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
1620 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
1621 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
1622 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
1623 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1624 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
1625 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
1626 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
1627 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
1628 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
1629 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
1632 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1634 if (tb[IFLA_ADDRESS]) {
1635 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1636 pr_debug("invalid link address (not ethernet)\n");
1640 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1641 pr_debug("invalid all zero ethernet address\n");
1642 return -EADDRNOTAVAIL;
1649 if (data[IFLA_VXLAN_ID]) {
1650 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1651 if (id >= VXLAN_VID_MASK)
1655 if (data[IFLA_VXLAN_PORT_RANGE]) {
1656 const struct ifla_vxlan_port_range *p
1657 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1659 if (ntohs(p->high) < ntohs(p->low)) {
1660 pr_debug("port range %u .. %u not valid\n",
1661 ntohs(p->low), ntohs(p->high));
1669 static void vxlan_get_drvinfo(struct net_device *netdev,
1670 struct ethtool_drvinfo *drvinfo)
1672 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1673 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1676 static const struct ethtool_ops vxlan_ethtool_ops = {
1677 .get_drvinfo = vxlan_get_drvinfo,
1678 .get_link = ethtool_op_get_link,
1681 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
1682 __be16 port, u32 flags)
1684 struct socket *sock;
1685 struct udp_port_cfg udp_conf;
1688 memset(&udp_conf, 0, sizeof(udp_conf));
1691 udp_conf.family = AF_INET6;
1692 udp_conf.use_udp6_rx_checksums =
1693 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
1694 udp_conf.ipv6_v6only = 1;
1696 udp_conf.family = AF_INET;
1699 udp_conf.local_udp_port = port;
1701 /* Open UDP socket */
1702 err = udp_sock_create(net, &udp_conf, &sock);
1704 return ERR_PTR(err);
1709 /* Create new listen socket if needed */
1710 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
1711 __be16 port, u32 flags)
1713 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1714 struct vxlan_sock *vs;
1715 struct socket *sock;
1717 struct udp_tunnel_sock_cfg tunnel_cfg;
1719 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
1721 return ERR_PTR(-ENOMEM);
1723 for (h = 0; h < VNI_HASH_SIZE; ++h)
1724 INIT_HLIST_HEAD(&vs->vni_list[h]);
1726 sock = vxlan_create_sock(net, ipv6, port, flags);
1728 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
1731 return ERR_CAST(sock);
1735 atomic_set(&vs->refcnt, 1);
1736 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
1738 #ifdef HAVE_UDP_OFFLOAD
1739 vs->udp_offloads.port = port;
1740 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
1741 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
1744 spin_lock(&vn->sock_lock);
1745 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1746 vxlan_notify_add_rx_port(vs);
1747 spin_unlock(&vn->sock_lock);
1749 /* Mark socket as an encapsulation socket. */
1750 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
1751 tunnel_cfg.sk_user_data = vs;
1752 tunnel_cfg.encap_type = 1;
1753 tunnel_cfg.encap_rcv = vxlan_rcv;
1754 tunnel_cfg.encap_destroy = NULL;
1755 #ifdef HAVE_UDP_TUNNEL_SOCK_CFG_GRO_RECEIVE
1756 tunnel_cfg.gro_receive = vxlan_gro_receive;
1757 tunnel_cfg.gro_complete = vxlan_gro_complete;
1759 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
1764 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
1766 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1767 struct vxlan_sock *vs = NULL;
1769 if (!vxlan->cfg.no_share) {
1770 spin_lock(&vn->sock_lock);
1771 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
1772 vxlan->cfg.dst_port, vxlan->flags);
1773 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
1774 spin_unlock(&vn->sock_lock);
1777 spin_unlock(&vn->sock_lock);
1780 vs = vxlan_socket_create(vxlan->net, ipv6,
1781 vxlan->cfg.dst_port, vxlan->flags);
1784 #if IS_ENABLED(CONFIG_IPV6)
1786 vxlan->vn6_sock = vs;
1789 vxlan->vn4_sock = vs;
1790 vxlan_vs_add_dev(vs, vxlan);
1794 static int vxlan_sock_add(struct vxlan_dev *vxlan)
1796 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
1797 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
1800 vxlan->vn4_sock = NULL;
1801 #if IS_ENABLED(CONFIG_IPV6)
1802 vxlan->vn6_sock = NULL;
1803 if (ipv6 || metadata)
1804 ret = __vxlan_sock_add(vxlan, true);
1806 if (!ret && (!ipv6 || metadata))
1807 ret = __vxlan_sock_add(vxlan, false);
1809 vxlan_sock_release(vxlan);
1813 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
1814 struct vxlan_config *conf)
1816 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
1817 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
1818 struct vxlan_rdst *dst = &vxlan->default_dst;
1819 unsigned short needed_headroom = ETH_HLEN;
1821 bool use_ipv6 = false;
1822 __be16 default_port = vxlan->cfg.dst_port;
1823 struct net_device *lowerdev = NULL;
1825 if (conf->flags & VXLAN_F_GPE) {
1826 if (conf->flags & ~VXLAN_F_ALLOWED_GPE)
1828 /* For now, allow GPE only together with COLLECT_METADATA.
1829 * This can be relaxed later; in such case, the other side
1830 * of the PtP link will have to be provided.
1832 if (!(conf->flags & VXLAN_F_COLLECT_METADATA))
1835 vxlan_raw_setup(dev);
1837 vxlan_ether_setup(dev);
1840 vxlan->net = src_net;
1842 dst->remote_vni = conf->vni;
1844 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
1846 /* Unless IPv6 is explicitly requested, assume IPv4 */
1847 if (!dst->remote_ip.sa.sa_family)
1848 dst->remote_ip.sa.sa_family = AF_INET;
1850 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
1851 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
1852 if (!IS_ENABLED(CONFIG_IPV6))
1853 return -EPFNOSUPPORT;
1855 vxlan->flags |= VXLAN_F_IPV6;
1858 if (conf->label && !use_ipv6) {
1859 pr_info("label only supported in use with IPv6\n");
1863 if (conf->remote_ifindex) {
1864 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
1865 dst->remote_ifindex = conf->remote_ifindex;
1868 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
1872 #if IS_ENABLED(CONFIG_IPV6)
1874 struct inet6_dev *idev = __in6_dev_get(lowerdev);
1875 if (idev && idev->cnf.disable_ipv6) {
1876 pr_info("IPv6 is disabled via sysctl\n");
1883 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
1885 needed_headroom = lowerdev->hard_header_len;
1889 err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false);
1894 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
1895 needed_headroom += VXLAN6_HEADROOM;
1897 needed_headroom += VXLAN_HEADROOM;
1898 dev->needed_headroom = needed_headroom;
1900 memcpy(&vxlan->cfg, conf, sizeof(*conf));
1901 if (!vxlan->cfg.dst_port) {
1902 if (conf->flags & VXLAN_F_GPE)
1903 vxlan->cfg.dst_port = 4790; /* IANA assigned VXLAN-GPE port */
1905 vxlan->cfg.dst_port = default_port;
1907 vxlan->flags |= conf->flags;
1909 if (!vxlan->cfg.age_interval)
1910 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
1912 list_for_each_entry(tmp, &vn->vxlan_list, next) {
1913 if (tmp->cfg.vni == conf->vni &&
1914 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
1915 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
1916 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
1917 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
1918 (vxlan->flags & VXLAN_F_RCV_FLAGS))
1922 dev->ethtool_ops = &vxlan_ethtool_ops;
1924 /* create an fdb entry for a valid default destination */
1925 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
1926 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1927 &vxlan->default_dst.remote_ip,
1928 NUD_REACHABLE|NUD_PERMANENT,
1929 NLM_F_EXCL|NLM_F_CREATE,
1930 vxlan->cfg.dst_port,
1931 vxlan->default_dst.remote_vni,
1932 vxlan->default_dst.remote_ifindex,
1938 err = register_netdevice(dev);
1940 vxlan_fdb_delete_default(vxlan);
1944 list_add(&vxlan->next, &vn->vxlan_list);
1949 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
1950 struct nlattr *tb[], struct nlattr *data[])
1952 pr_info("unsupported operation\n");
1956 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1958 struct vxlan_dev *vxlan = netdev_priv(dev);
1959 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1961 spin_lock(&vn->sock_lock);
1962 if (!hlist_unhashed(&vxlan->hlist))
1963 hlist_del_rcu(&vxlan->hlist);
1964 spin_unlock(&vn->sock_lock);
1966 list_del(&vxlan->next);
1967 unregister_netdevice_queue(dev, head);
1970 static size_t vxlan_get_size(const struct net_device *dev)
1973 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1974 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
1975 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1976 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
1977 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1978 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1979 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
1980 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
1981 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1982 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1983 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1984 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
1985 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
1986 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1987 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
1988 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
1989 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
1990 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
1991 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
1992 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
1993 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
1994 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
1998 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2000 const struct vxlan_dev *vxlan = netdev_priv(dev);
2001 const struct vxlan_rdst *dst = &vxlan->default_dst;
2002 struct ifla_vxlan_port_range ports = {
2003 .low = htons(vxlan->cfg.port_min),
2004 .high = htons(vxlan->cfg.port_max),
2007 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
2008 goto nla_put_failure;
2010 if (!vxlan_addr_any(&dst->remote_ip)) {
2011 if (dst->remote_ip.sa.sa_family == AF_INET) {
2012 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
2013 dst->remote_ip.sin.sin_addr.s_addr))
2014 goto nla_put_failure;
2015 #if IS_ENABLED(CONFIG_IPV6)
2017 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
2018 &dst->remote_ip.sin6.sin6_addr))
2019 goto nla_put_failure;
2024 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2025 goto nla_put_failure;
2027 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
2028 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
2029 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
2030 vxlan->cfg.saddr.sin.sin_addr.s_addr))
2031 goto nla_put_failure;
2032 #if IS_ENABLED(CONFIG_IPV6)
2034 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
2035 &vxlan->cfg.saddr.sin6.sin6_addr))
2036 goto nla_put_failure;
2041 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
2042 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
2043 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
2044 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2045 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2046 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2047 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2048 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2049 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2050 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2051 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2052 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2053 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
2054 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
2055 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
2056 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
2057 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
2058 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2059 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
2060 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2061 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2062 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2063 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2064 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2065 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2066 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2067 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
2068 goto nla_put_failure;
2070 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2071 goto nla_put_failure;
2073 if (vxlan->flags & VXLAN_F_GBP &&
2074 nla_put_flag(skb, IFLA_VXLAN_GBP))
2075 goto nla_put_failure;
2077 if (vxlan->flags & VXLAN_F_GPE &&
2078 nla_put_flag(skb, IFLA_VXLAN_GPE))
2079 goto nla_put_failure;
2081 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
2082 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
2083 goto nla_put_failure;
2091 #ifdef HAVE_GET_LINK_NET
2092 static struct net *vxlan_get_link_net(const struct net_device *dev)
2094 struct vxlan_dev *vxlan = netdev_priv(dev);
2100 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2101 .kind = "ovs_vxlan",
2102 .maxtype = IFLA_VXLAN_MAX,
2103 .policy = vxlan_policy,
2104 .priv_size = sizeof(struct vxlan_dev),
2105 .setup = vxlan_setup,
2106 .validate = vxlan_validate,
2107 .newlink = vxlan_newlink,
2108 .dellink = vxlan_dellink,
2109 .get_size = vxlan_get_size,
2110 .fill_info = vxlan_fill_info,
2111 #ifdef HAVE_GET_LINK_NET
2112 .get_link_net = vxlan_get_link_net,
2116 struct net_device *rpl_vxlan_dev_create(struct net *net, const char *name,
2117 u8 name_assign_type,
2118 struct vxlan_config *conf)
2120 struct nlattr *tb[IFLA_MAX + 1];
2121 struct net_device *dev;
2124 memset(&tb, 0, sizeof(tb));
2126 dev = rtnl_create_link(net, name, name_assign_type,
2127 &vxlan_link_ops, tb);
2131 err = vxlan_dev_configure(net, dev, conf);
2134 return ERR_PTR(err);
2137 err = rtnl_configure_link(dev, NULL);
2139 LIST_HEAD(list_kill);
2141 vxlan_dellink(dev, &list_kill);
2142 unregister_netdevice_many(&list_kill);
2143 return ERR_PTR(err);
2148 EXPORT_SYMBOL_GPL(rpl_vxlan_dev_create);
2150 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2151 struct net_device *dev)
2153 struct vxlan_dev *vxlan, *next;
2154 LIST_HEAD(list_kill);
2156 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2157 struct vxlan_rdst *dst = &vxlan->default_dst;
2159 /* In case we created vxlan device with carrier
2160 * and we loose the carrier due to module unload
2161 * we also need to remove vxlan device. In other
2162 * cases, it's not necessary and remote_ifindex
2163 * is 0 here, so no matches.
2165 if (dst->remote_ifindex == dev->ifindex)
2166 vxlan_dellink(vxlan->dev, &list_kill);
2169 unregister_netdevice_many(&list_kill);
2172 static int vxlan_netdevice_event(struct notifier_block *unused,
2173 unsigned long event, void *ptr)
2175 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2176 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2178 if (event == NETDEV_UNREGISTER)
2179 vxlan_handle_lowerdev_unregister(vn, dev);
2180 else if (event == NETDEV_OFFLOAD_PUSH_VXLAN)
2181 vxlan_push_rx_ports(dev);
2186 static struct notifier_block vxlan_notifier_block __read_mostly = {
2187 .notifier_call = vxlan_netdevice_event,
2190 static __net_init int vxlan_init_net(struct net *net)
2192 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2195 INIT_LIST_HEAD(&vn->vxlan_list);
2196 spin_lock_init(&vn->sock_lock);
2198 for (h = 0; h < PORT_HASH_SIZE; ++h)
2199 INIT_HLIST_HEAD(&vn->sock_list[h]);
2204 static void __net_exit vxlan_exit_net(struct net *net)
2206 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2207 struct vxlan_dev *vxlan, *next;
2208 struct net_device *dev, *aux;
2212 for_each_netdev_safe(net, dev, aux)
2213 if (dev->rtnl_link_ops == &vxlan_link_ops)
2214 unregister_netdevice_queue(dev, &list);
2216 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2217 /* If vxlan->dev is in the same netns, it has already been added
2218 * to the list by the previous loop.
2220 if (!net_eq(dev_net(vxlan->dev), net)) {
2221 unregister_netdevice_queue(vxlan->dev, &list);
2225 unregister_netdevice_many(&list);
2229 static struct pernet_operations vxlan_net_ops = {
2230 .init = vxlan_init_net,
2231 .exit = vxlan_exit_net,
2232 .id = &vxlan_net_id,
2233 .size = sizeof(struct vxlan_net),
2236 int rpl_vxlan_init_module(void)
2240 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2242 rc = register_pernet_subsys(&vxlan_net_ops);
2246 rc = register_netdevice_notifier(&vxlan_notifier_block);
2250 rc = rtnl_link_register(&vxlan_link_ops);
2254 pr_info("VxLAN tunneling driver\n");
2257 unregister_netdevice_notifier(&vxlan_notifier_block);
2259 unregister_pernet_subsys(&vxlan_net_ops);
2264 void rpl_vxlan_cleanup_module(void)
2266 rtnl_link_unregister(&vxlan_link_ops);
2267 unregister_netdevice_notifier(&vxlan_notifier_block);
2268 unregister_pernet_subsys(&vxlan_net_ops);
2269 /* rcu_barrier() is called by netns */