2 * VXLAN: Virtual eXtensible Local Area Network
4 * Copyright (c) 2012 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 * - use IANA UDP port number (when defined)
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/skbuff.h>
23 #include <linux/rculist.h>
24 #include <linux/netdevice.h>
27 #include <linux/udp.h>
28 #include <linux/igmp.h>
29 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/hash.h>
33 #include <net/ndisc.h>
37 #include <net/rtnetlink.h>
38 #include <net/route.h>
39 #include <net/dsfield.h>
40 #include <net/inet_ecn.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
44 #define VXLAN_VERSION "0.1"
46 #define VNI_HASH_BITS 10
47 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
48 #define FDB_HASH_BITS 8
49 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
50 #define FDB_AGE_DEFAULT 300 /* 5 min */
51 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
53 #define VXLAN_N_VID (1u << 24)
54 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
55 /* IP header + UDP + VXLAN + Ethernet header */
56 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
58 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
60 /* VXLAN protocol header */
66 /* UDP port for VXLAN traffic. */
67 static unsigned int vxlan_port __read_mostly
= 8472;
68 module_param_named(udp_port
, vxlan_port
, uint
, 0444);
69 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
71 static bool log_ecn_error
= true;
72 module_param(log_ecn_error
, bool, 0644);
73 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
75 /* per-net private data for this module */
76 static unsigned int vxlan_net_id
;
78 struct socket
*sock
; /* UDP encap socket */
79 struct hlist_head vni_list
[VNI_HASH_SIZE
];
82 /* Forwarding table entry */
84 struct hlist_node hlist
; /* linked list of entries */
86 unsigned long updated
; /* jiffies */
89 u16 state
; /* see ndm_state */
90 u8 eth_addr
[ETH_ALEN
];
93 /* Per-cpu network traffic stats */
99 struct u64_stats_sync syncp
;
102 /* Pseudo network device */
104 struct hlist_node hlist
;
105 struct net_device
*dev
;
106 struct vxlan_stats __percpu
*stats
;
107 __u32 vni
; /* virtual network id */
108 __be32 gaddr
; /* multicast group */
109 __be32 saddr
; /* source address */
110 unsigned int link
; /* link to multicast over */
111 __u16 port_min
; /* source port range */
113 __u8 tos
; /* TOS override */
115 u32 flags
; /* VXLAN_F_* below */
117 unsigned long age_interval
;
118 struct timer_list age_timer
;
119 spinlock_t hash_lock
;
120 unsigned int addrcnt
;
121 unsigned int addrmax
;
123 struct hlist_head fdb_head
[FDB_HASH_SIZE
];
126 #define VXLAN_F_LEARN 0x01
127 #define VXLAN_F_PROXY 0x02
128 #define VXLAN_F_RSC 0x04
129 #define VXLAN_F_L2MISS 0x08
130 #define VXLAN_F_L3MISS 0x10
132 /* salt for hash table */
133 static u32 vxlan_salt __read_mostly
;
135 static inline struct hlist_head
*vni_head(struct net
*net
, u32 id
)
137 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
139 return &vn
->vni_list
[hash_32(id
, VNI_HASH_BITS
)];
142 /* Look up VNI in a per net namespace table */
143 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, u32 id
)
145 struct vxlan_dev
*vxlan
;
146 struct hlist_node
*node
;
148 hlist_for_each_entry_rcu(vxlan
, node
, vni_head(net
, id
), hlist
) {
149 if (vxlan
->vni
== id
)
156 /* Fill in neighbour message in skbuff. */
157 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
158 const struct vxlan_fdb
*fdb
,
159 u32 portid
, u32 seq
, int type
, unsigned int flags
)
161 unsigned long now
= jiffies
;
162 struct nda_cacheinfo ci
;
163 struct nlmsghdr
*nlh
;
165 bool send_ip
, send_eth
;
167 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
171 ndm
= nlmsg_data(nlh
);
172 memset(ndm
, 0, sizeof(*ndm
));
174 send_eth
= send_ip
= true;
176 if (type
== RTM_GETNEIGH
) {
177 ndm
->ndm_family
= AF_INET
;
178 send_ip
= fdb
->remote_ip
!= 0;
179 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
181 ndm
->ndm_family
= AF_BRIDGE
;
182 ndm
->ndm_state
= fdb
->state
;
183 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
184 ndm
->ndm_flags
= NTF_SELF
;
185 ndm
->ndm_type
= NDA_DST
;
187 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
188 goto nla_put_failure
;
190 if (send_ip
&& nla_put_be32(skb
, NDA_DST
, fdb
->remote_ip
))
191 goto nla_put_failure
;
193 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
194 ci
.ndm_confirmed
= 0;
195 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
198 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
199 goto nla_put_failure
;
201 return nlmsg_end(skb
, nlh
);
204 nlmsg_cancel(skb
, nlh
);
208 static inline size_t vxlan_nlmsg_size(void)
210 return NLMSG_ALIGN(sizeof(struct ndmsg
))
211 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
212 + nla_total_size(sizeof(__be32
)) /* NDA_DST */
213 + nla_total_size(sizeof(struct nda_cacheinfo
));
216 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
,
217 const struct vxlan_fdb
*fdb
, int type
)
219 struct net
*net
= dev_net(vxlan
->dev
);
223 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
227 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0);
229 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
230 WARN_ON(err
== -EMSGSIZE
);
235 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
239 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
242 static void vxlan_ip_miss(struct net_device
*dev
, __be32 ipa
)
244 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
247 memset(&f
, 0, sizeof f
);
249 f
.remote_ip
= ipa
; /* goes to NDA_DST */
251 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
254 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
258 memset(&f
, 0, sizeof f
);
260 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
262 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
265 /* Hash Ethernet address */
266 static u32
eth_hash(const unsigned char *addr
)
268 u64 value
= get_unaligned((u64
*)addr
);
270 /* only want 6 bytes */
276 return hash_64(value
, FDB_HASH_BITS
);
279 /* Hash chain to use given mac address */
280 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
283 return &vxlan
->fdb_head
[eth_hash(mac
)];
286 /* Look up Ethernet address in forwarding table */
287 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
291 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
);
293 struct hlist_node
*node
;
295 hlist_for_each_entry_rcu(f
, node
, head
, hlist
) {
296 if (compare_ether_addr(mac
, f
->eth_addr
) == 0)
303 /* Add new entry to forwarding table -- assumes lock held */
304 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
305 const u8
*mac
, __be32 ip
,
306 __u16 state
, __u16 flags
)
311 f
= vxlan_find_mac(vxlan
, mac
);
313 if (flags
& NLM_F_EXCL
) {
314 netdev_dbg(vxlan
->dev
,
315 "lost race to create %pM\n", mac
);
318 if (f
->state
!= state
) {
320 f
->updated
= jiffies
;
324 if (!(flags
& NLM_F_CREATE
))
327 if (vxlan
->addrmax
&& vxlan
->addrcnt
>= vxlan
->addrmax
)
330 netdev_dbg(vxlan
->dev
, "add %pM -> %pI4\n", mac
, &ip
);
331 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
338 f
->updated
= f
->used
= jiffies
;
339 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
342 hlist_add_head_rcu(&f
->hlist
,
343 vxlan_fdb_head(vxlan
, mac
));
347 vxlan_fdb_notify(vxlan
, f
, RTM_NEWNEIGH
);
352 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
354 netdev_dbg(vxlan
->dev
,
355 "delete %pM\n", f
->eth_addr
);
358 vxlan_fdb_notify(vxlan
, f
, RTM_DELNEIGH
);
360 hlist_del_rcu(&f
->hlist
);
364 /* Add static entry (via netlink) */
365 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
366 struct net_device
*dev
,
367 const unsigned char *addr
, u16 flags
)
369 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
373 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
374 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
379 if (tb
[NDA_DST
] == NULL
)
382 if (nla_len(tb
[NDA_DST
]) != sizeof(__be32
))
383 return -EAFNOSUPPORT
;
385 ip
= nla_get_be32(tb
[NDA_DST
]);
387 spin_lock_bh(&vxlan
->hash_lock
);
388 err
= vxlan_fdb_create(vxlan
, addr
, ip
, ndm
->ndm_state
, flags
);
389 spin_unlock_bh(&vxlan
->hash_lock
);
394 /* Delete entry (via netlink) */
395 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct net_device
*dev
,
396 const unsigned char *addr
)
398 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
402 spin_lock_bh(&vxlan
->hash_lock
);
403 f
= vxlan_find_mac(vxlan
, addr
);
405 vxlan_fdb_destroy(vxlan
, f
);
408 spin_unlock_bh(&vxlan
->hash_lock
);
413 /* Dump forwarding table */
414 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
415 struct net_device
*dev
, int idx
)
417 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
420 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
422 struct hlist_node
*n
;
425 hlist_for_each_entry_rcu(f
, n
, &vxlan
->fdb_head
[h
], hlist
) {
426 if (idx
< cb
->args
[0])
429 err
= vxlan_fdb_info(skb
, vxlan
, f
,
430 NETLINK_CB(cb
->skb
).portid
,
444 /* Watch incoming packets to learn mapping between Ethernet address
445 * and Tunnel endpoint.
447 static void vxlan_snoop(struct net_device
*dev
,
448 __be32 src_ip
, const u8
*src_mac
)
450 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
454 f
= vxlan_find_mac(vxlan
, src_mac
);
457 if (likely(f
->remote_ip
== src_ip
))
462 "%pM migrated from %pI4 to %pI4\n",
463 src_mac
, &f
->remote_ip
, &src_ip
);
465 f
->remote_ip
= src_ip
;
466 f
->updated
= jiffies
;
468 /* learned new entry */
469 spin_lock(&vxlan
->hash_lock
);
470 err
= vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
472 NLM_F_EXCL
|NLM_F_CREATE
);
473 spin_unlock(&vxlan
->hash_lock
);
478 /* See if multicast group is already in use by other ID */
479 static bool vxlan_group_used(struct vxlan_net
*vn
,
480 const struct vxlan_dev
*this)
482 const struct vxlan_dev
*vxlan
;
483 struct hlist_node
*node
;
486 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
487 hlist_for_each_entry(vxlan
, node
, &vn
->vni_list
[h
], hlist
) {
491 if (!netif_running(vxlan
->dev
))
494 if (vxlan
->gaddr
== this->gaddr
)
501 /* kernel equivalent to IP_ADD_MEMBERSHIP */
502 static int vxlan_join_group(struct net_device
*dev
)
504 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
505 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
506 struct sock
*sk
= vn
->sock
->sk
;
507 struct ip_mreqn mreq
= {
508 .imr_multiaddr
.s_addr
= vxlan
->gaddr
,
512 /* Already a member of group */
513 if (vxlan_group_used(vn
, vxlan
))
516 /* Need to drop RTNL to call multicast join */
519 err
= ip_mc_join_group(sk
, &mreq
);
527 /* kernel equivalent to IP_DROP_MEMBERSHIP */
528 static int vxlan_leave_group(struct net_device
*dev
)
530 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
531 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
533 struct sock
*sk
= vn
->sock
->sk
;
534 struct ip_mreqn mreq
= {
535 .imr_multiaddr
.s_addr
= vxlan
->gaddr
,
538 /* Only leave group when last vxlan is done. */
539 if (vxlan_group_used(vn
, vxlan
))
542 /* Need to drop RTNL to call multicast leave */
545 err
= ip_mc_leave_group(sk
, &mreq
);
552 /* Callback from net/ipv4/udp.c to receive packets */
553 static int vxlan_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
556 struct vxlanhdr
*vxh
;
557 struct vxlan_dev
*vxlan
;
558 struct vxlan_stats
*stats
;
562 /* pop off outer UDP header */
563 __skb_pull(skb
, sizeof(struct udphdr
));
565 /* Need Vxlan and inner Ethernet header to be present */
566 if (!pskb_may_pull(skb
, sizeof(struct vxlanhdr
)))
569 /* Drop packets with reserved bits set */
570 vxh
= (struct vxlanhdr
*) skb
->data
;
571 if (vxh
->vx_flags
!= htonl(VXLAN_FLAGS
) ||
572 (vxh
->vx_vni
& htonl(0xff))) {
573 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
574 ntohl(vxh
->vx_flags
), ntohl(vxh
->vx_vni
));
578 __skb_pull(skb
, sizeof(struct vxlanhdr
));
580 /* Is this VNI defined? */
581 vni
= ntohl(vxh
->vx_vni
) >> 8;
582 vxlan
= vxlan_find_vni(sock_net(sk
), vni
);
584 netdev_dbg(skb
->dev
, "unknown vni %d\n", vni
);
588 if (!pskb_may_pull(skb
, ETH_HLEN
)) {
589 vxlan
->dev
->stats
.rx_length_errors
++;
590 vxlan
->dev
->stats
.rx_errors
++;
594 skb_reset_mac_header(skb
);
596 /* Re-examine inner Ethernet packet */
598 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
600 /* Ignore packet loops (and multicast echo) */
601 if (compare_ether_addr(eth_hdr(skb
)->h_source
,
602 vxlan
->dev
->dev_addr
) == 0)
605 if (vxlan
->flags
& VXLAN_F_LEARN
)
606 vxlan_snoop(skb
->dev
, oip
->saddr
, eth_hdr(skb
)->h_source
);
608 __skb_tunnel_rx(skb
, vxlan
->dev
);
609 skb_reset_network_header(skb
);
611 /* If the NIC driver gave us an encapsulated packet with
612 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
613 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
614 * for us. Otherwise force the upper layers to verify it.
616 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
|| !skb
->encapsulation
||
617 !(vxlan
->dev
->features
& NETIF_F_RXCSUM
))
618 skb
->ip_summed
= CHECKSUM_NONE
;
620 skb
->encapsulation
= 0;
622 err
= IP_ECN_decapsulate(oip
, skb
);
625 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
626 &oip
->saddr
, oip
->tos
);
628 ++vxlan
->dev
->stats
.rx_frame_errors
;
629 ++vxlan
->dev
->stats
.rx_errors
;
634 stats
= this_cpu_ptr(vxlan
->stats
);
635 u64_stats_update_begin(&stats
->syncp
);
637 stats
->rx_bytes
+= skb
->len
;
638 u64_stats_update_end(&stats
->syncp
);
644 /* Put UDP header back */
645 __skb_push(skb
, sizeof(struct udphdr
));
649 /* Consume bad packet */
654 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
656 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
662 if (dev
->flags
& IFF_NOARP
)
665 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
666 dev
->stats
.tx_dropped
++;
671 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
672 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
673 parp
->ar_pro
!= htons(ETH_P_IP
) ||
674 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
675 parp
->ar_hln
!= dev
->addr_len
||
678 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
680 arpptr
+= dev
->addr_len
; /* sha */
681 memcpy(&sip
, arpptr
, sizeof(sip
));
682 arpptr
+= sizeof(sip
);
683 arpptr
+= dev
->addr_len
; /* tha */
684 memcpy(&tip
, arpptr
, sizeof(tip
));
686 if (ipv4_is_loopback(tip
) ||
687 ipv4_is_multicast(tip
))
690 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
693 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
695 struct sk_buff
*reply
;
697 if (!(n
->nud_state
& NUD_CONNECTED
)) {
702 f
= vxlan_find_mac(vxlan
, n
->ha
);
703 if (f
&& f
->remote_ip
== 0) {
704 /* bridge-local neighbor */
709 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
714 skb_reset_mac_header(reply
);
715 __skb_pull(reply
, skb_network_offset(reply
));
716 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
717 reply
->pkt_type
= PACKET_HOST
;
719 if (netif_rx_ni(reply
) == NET_RX_DROP
)
720 dev
->stats
.rx_dropped
++;
721 } else if (vxlan
->flags
& VXLAN_F_L3MISS
)
722 vxlan_ip_miss(dev
, tip
);
728 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
730 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
734 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
738 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
740 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
743 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
752 diff
= compare_ether_addr(eth_hdr(skb
)->h_dest
, n
->ha
) != 0;
754 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
756 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
760 } else if (vxlan
->flags
& VXLAN_F_L3MISS
)
761 vxlan_ip_miss(dev
, pip
->daddr
);
765 /* Extract dsfield from inner protocol */
766 static inline u8
vxlan_get_dsfield(const struct iphdr
*iph
,
767 const struct sk_buff
*skb
)
769 if (skb
->protocol
== htons(ETH_P_IP
))
771 else if (skb
->protocol
== htons(ETH_P_IPV6
))
772 return ipv6_get_dsfield((const struct ipv6hdr
*)iph
);
777 /* Propogate ECN bits out */
778 static inline u8
vxlan_ecn_encap(u8 tos
,
779 const struct iphdr
*iph
,
780 const struct sk_buff
*skb
)
782 u8 inner
= vxlan_get_dsfield(iph
, skb
);
784 return INET_ECN_encapsulate(tos
, inner
);
787 static void vxlan_sock_free(struct sk_buff
*skb
)
792 /* On transmit, associate with the tunnel socket */
793 static void vxlan_set_owner(struct net_device
*dev
, struct sk_buff
*skb
)
795 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
796 struct sock
*sk
= vn
->sock
->sk
;
801 skb
->destructor
= vxlan_sock_free
;
804 /* Compute source port for outgoing packet
805 * first choice to use L4 flow hash since it will spread
806 * better and maybe available from hardware
807 * secondary choice is to use jhash on the Ethernet header
809 static u16
vxlan_src_port(const struct vxlan_dev
*vxlan
, struct sk_buff
*skb
)
811 unsigned int range
= (vxlan
->port_max
- vxlan
->port_min
) + 1;
814 hash
= skb_get_rxhash(skb
);
816 hash
= jhash(skb
->data
, 2 * ETH_ALEN
,
817 (__force u32
) skb
->protocol
);
819 return (((u64
) hash
* range
) >> 32) + vxlan
->port_min
;
822 /* Transmit local packets over Vxlan
824 * Outer IP header inherits ECN and DF from inner header.
825 * Outer UDP destination is the VXLAN assigned port.
826 * source port is based on hash of flow
828 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
830 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
832 const struct iphdr
*old_iph
;
835 struct vxlanhdr
*vxh
;
838 unsigned int pkt_len
= skb
->len
;
844 bool did_rsc
= false;
845 const struct vxlan_fdb
*f
;
847 skb_reset_mac_header(skb
);
850 if ((vxlan
->flags
& VXLAN_F_PROXY
) && ntohs(eth
->h_proto
) == ETH_P_ARP
)
851 return arp_reduce(dev
, skb
);
852 else if ((vxlan
->flags
&VXLAN_F_RSC
) && ntohs(eth
->h_proto
) == ETH_P_IP
)
853 did_rsc
= route_shortcircuit(dev
, skb
);
855 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
859 if (!dst
&& (vxlan
->flags
& VXLAN_F_L2MISS
) &&
860 !is_multicast_ether_addr(eth
->h_dest
))
861 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
867 __skb_pull(skb
, skb_network_offset(skb
));
868 skb
->ip_summed
= CHECKSUM_NONE
;
869 skb
->pkt_type
= PACKET_HOST
;
871 /* short-circuited back to local bridge */
872 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
873 struct vxlan_stats
*stats
=
874 this_cpu_ptr(vxlan
->stats
);
876 u64_stats_update_begin(&stats
->syncp
);
878 stats
->tx_bytes
+= pkt_len
;
879 u64_stats_update_end(&stats
->syncp
);
881 dev
->stats
.tx_errors
++;
882 dev
->stats
.tx_aborted_errors
++;
889 if (!skb
->encapsulation
) {
890 skb_reset_inner_headers(skb
);
891 skb
->encapsulation
= 1;
894 /* Need space for new headers (invalidates iph ptr) */
895 if (skb_cow_head(skb
, VXLAN_HEADROOM
))
898 old_iph
= ip_hdr(skb
);
901 if (!ttl
&& IN_MULTICAST(ntohl(dst
)))
906 tos
= vxlan_get_dsfield(old_iph
, skb
);
908 src_port
= vxlan_src_port(vxlan
, skb
);
910 memset(&fl4
, 0, sizeof(fl4
));
911 fl4
.flowi4_oif
= vxlan
->link
;
912 fl4
.flowi4_tos
= RT_TOS(tos
);
914 fl4
.saddr
= vxlan
->saddr
;
916 rt
= ip_route_output_key(dev_net(dev
), &fl4
);
918 netdev_dbg(dev
, "no route to %pI4\n", &dst
);
919 dev
->stats
.tx_carrier_errors
++;
923 if (rt
->dst
.dev
== dev
) {
924 netdev_dbg(dev
, "circular route to %pI4\n", &dst
);
926 dev
->stats
.collisions
++;
930 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
931 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
934 skb_dst_set(skb
, &rt
->dst
);
936 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
937 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
938 vxh
->vx_vni
= htonl(vxlan
->vni
<< 8);
940 __skb_push(skb
, sizeof(*uh
));
941 skb_reset_transport_header(skb
);
944 uh
->dest
= htons(vxlan_port
);
945 uh
->source
= htons(src_port
);
947 uh
->len
= htons(skb
->len
);
950 __skb_push(skb
, sizeof(*iph
));
951 skb_reset_network_header(skb
);
954 iph
->ihl
= sizeof(struct iphdr
) >> 2;
956 iph
->protocol
= IPPROTO_UDP
;
957 iph
->tos
= vxlan_ecn_encap(tos
, old_iph
, skb
);
959 iph
->saddr
= fl4
.saddr
;
960 iph
->ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
962 vxlan_set_owner(dev
, skb
);
964 /* See iptunnel_xmit() */
965 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
966 skb
->ip_summed
= CHECKSUM_NONE
;
967 ip_select_ident(iph
, &rt
->dst
, NULL
);
969 err
= ip_local_out(skb
);
970 if (likely(net_xmit_eval(err
) == 0)) {
971 struct vxlan_stats
*stats
= this_cpu_ptr(vxlan
->stats
);
973 u64_stats_update_begin(&stats
->syncp
);
975 stats
->tx_bytes
+= pkt_len
;
976 u64_stats_update_end(&stats
->syncp
);
978 dev
->stats
.tx_errors
++;
979 dev
->stats
.tx_aborted_errors
++;
984 dev
->stats
.tx_dropped
++;
988 dev
->stats
.tx_errors
++;
994 /* Walk the forwarding table and purge stale entries */
995 static void vxlan_cleanup(unsigned long arg
)
997 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
998 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
1001 if (!netif_running(vxlan
->dev
))
1004 spin_lock_bh(&vxlan
->hash_lock
);
1005 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1006 struct hlist_node
*p
, *n
;
1007 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1009 = container_of(p
, struct vxlan_fdb
, hlist
);
1010 unsigned long timeout
;
1012 if (f
->state
& NUD_PERMANENT
)
1015 timeout
= f
->used
+ vxlan
->age_interval
* HZ
;
1016 if (time_before_eq(timeout
, jiffies
)) {
1017 netdev_dbg(vxlan
->dev
,
1018 "garbage collect %pM\n",
1020 f
->state
= NUD_STALE
;
1021 vxlan_fdb_destroy(vxlan
, f
);
1022 } else if (time_before(timeout
, next_timer
))
1023 next_timer
= timeout
;
1026 spin_unlock_bh(&vxlan
->hash_lock
);
1028 mod_timer(&vxlan
->age_timer
, next_timer
);
1031 /* Setup stats when device is created */
1032 static int vxlan_init(struct net_device
*dev
)
1034 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1036 vxlan
->stats
= alloc_percpu(struct vxlan_stats
);
1043 /* Start ageing timer and join group when device is brought up */
1044 static int vxlan_open(struct net_device
*dev
)
1046 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1050 err
= vxlan_join_group(dev
);
1055 if (vxlan
->age_interval
)
1056 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
1061 /* Purge the forwarding table */
1062 static void vxlan_flush(struct vxlan_dev
*vxlan
)
1066 spin_lock_bh(&vxlan
->hash_lock
);
1067 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1068 struct hlist_node
*p
, *n
;
1069 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1071 = container_of(p
, struct vxlan_fdb
, hlist
);
1072 vxlan_fdb_destroy(vxlan
, f
);
1075 spin_unlock_bh(&vxlan
->hash_lock
);
1078 /* Cleanup timer and forwarding table on shutdown */
1079 static int vxlan_stop(struct net_device
*dev
)
1081 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1084 vxlan_leave_group(dev
);
1086 del_timer_sync(&vxlan
->age_timer
);
1093 /* Merge per-cpu statistics */
1094 static struct rtnl_link_stats64
*vxlan_stats64(struct net_device
*dev
,
1095 struct rtnl_link_stats64
*stats
)
1097 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1098 struct vxlan_stats tmp
, sum
= { 0 };
1101 for_each_possible_cpu(cpu
) {
1103 const struct vxlan_stats
*stats
1104 = per_cpu_ptr(vxlan
->stats
, cpu
);
1107 start
= u64_stats_fetch_begin_bh(&stats
->syncp
);
1108 memcpy(&tmp
, stats
, sizeof(tmp
));
1109 } while (u64_stats_fetch_retry_bh(&stats
->syncp
, start
));
1111 sum
.tx_bytes
+= tmp
.tx_bytes
;
1112 sum
.tx_packets
+= tmp
.tx_packets
;
1113 sum
.rx_bytes
+= tmp
.rx_bytes
;
1114 sum
.rx_packets
+= tmp
.rx_packets
;
1117 stats
->tx_bytes
= sum
.tx_bytes
;
1118 stats
->tx_packets
= sum
.tx_packets
;
1119 stats
->rx_bytes
= sum
.rx_bytes
;
1120 stats
->rx_packets
= sum
.rx_packets
;
1122 stats
->multicast
= dev
->stats
.multicast
;
1123 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
1124 stats
->rx_frame_errors
= dev
->stats
.rx_frame_errors
;
1125 stats
->rx_errors
= dev
->stats
.rx_errors
;
1127 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
1128 stats
->tx_carrier_errors
= dev
->stats
.tx_carrier_errors
;
1129 stats
->tx_aborted_errors
= dev
->stats
.tx_aborted_errors
;
1130 stats
->collisions
= dev
->stats
.collisions
;
1131 stats
->tx_errors
= dev
->stats
.tx_errors
;
1136 /* Stub, nothing needs to be done. */
1137 static void vxlan_set_multicast_list(struct net_device
*dev
)
1141 static const struct net_device_ops vxlan_netdev_ops
= {
1142 .ndo_init
= vxlan_init
,
1143 .ndo_open
= vxlan_open
,
1144 .ndo_stop
= vxlan_stop
,
1145 .ndo_start_xmit
= vxlan_xmit
,
1146 .ndo_get_stats64
= vxlan_stats64
,
1147 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
1148 .ndo_change_mtu
= eth_change_mtu
,
1149 .ndo_validate_addr
= eth_validate_addr
,
1150 .ndo_set_mac_address
= eth_mac_addr
,
1151 .ndo_fdb_add
= vxlan_fdb_add
,
1152 .ndo_fdb_del
= vxlan_fdb_delete
,
1153 .ndo_fdb_dump
= vxlan_fdb_dump
,
1156 /* Info for udev, that this is a virtual tunnel endpoint */
1157 static struct device_type vxlan_type
= {
1161 static void vxlan_free(struct net_device
*dev
)
1163 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1165 free_percpu(vxlan
->stats
);
1169 /* Initialize the device structure. */
1170 static void vxlan_setup(struct net_device
*dev
)
1172 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1176 eth_hw_addr_random(dev
);
1178 dev
->hard_header_len
= ETH_HLEN
+ VXLAN_HEADROOM
;
1180 dev
->netdev_ops
= &vxlan_netdev_ops
;
1181 dev
->destructor
= vxlan_free
;
1182 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
1184 dev
->tx_queue_len
= 0;
1185 dev
->features
|= NETIF_F_LLTX
;
1186 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1187 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
1188 dev
->features
|= NETIF_F_RXCSUM
;
1190 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1191 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1193 spin_lock_init(&vxlan
->hash_lock
);
1195 init_timer_deferrable(&vxlan
->age_timer
);
1196 vxlan
->age_timer
.function
= vxlan_cleanup
;
1197 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
1199 inet_get_local_port_range(&low
, &high
);
1200 vxlan
->port_min
= low
;
1201 vxlan
->port_max
= high
;
1205 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
1206 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
1209 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
1210 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
1211 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
1212 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
1213 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
1214 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
1215 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
1216 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
1217 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
1218 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
1219 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
1220 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
1221 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
1222 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
1223 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
1226 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1228 if (tb
[IFLA_ADDRESS
]) {
1229 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
1230 pr_debug("invalid link address (not ethernet)\n");
1234 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
1235 pr_debug("invalid all zero ethernet address\n");
1236 return -EADDRNOTAVAIL
;
1243 if (data
[IFLA_VXLAN_ID
]) {
1244 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
1245 if (id
>= VXLAN_VID_MASK
)
1249 if (data
[IFLA_VXLAN_GROUP
]) {
1250 __be32 gaddr
= nla_get_be32(data
[IFLA_VXLAN_GROUP
]);
1251 if (!IN_MULTICAST(ntohl(gaddr
))) {
1252 pr_debug("group address is not IPv4 multicast\n");
1253 return -EADDRNOTAVAIL
;
1257 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
1258 const struct ifla_vxlan_port_range
*p
1259 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
1261 if (ntohs(p
->high
) < ntohs(p
->low
)) {
1262 pr_debug("port range %u .. %u not valid\n",
1263 ntohs(p
->low
), ntohs(p
->high
));
1271 static int vxlan_newlink(struct net
*net
, struct net_device
*dev
,
1272 struct nlattr
*tb
[], struct nlattr
*data
[])
1274 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1278 if (!data
[IFLA_VXLAN_ID
])
1281 vni
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
1282 if (vxlan_find_vni(net
, vni
)) {
1283 pr_info("duplicate VNI %u\n", vni
);
1288 if (data
[IFLA_VXLAN_GROUP
])
1289 vxlan
->gaddr
= nla_get_be32(data
[IFLA_VXLAN_GROUP
]);
1291 if (data
[IFLA_VXLAN_LOCAL
])
1292 vxlan
->saddr
= nla_get_be32(data
[IFLA_VXLAN_LOCAL
]);
1294 if (data
[IFLA_VXLAN_LINK
] &&
1295 (vxlan
->link
= nla_get_u32(data
[IFLA_VXLAN_LINK
]))) {
1296 struct net_device
*lowerdev
1297 = __dev_get_by_index(net
, vxlan
->link
);
1300 pr_info("ifindex %d does not exist\n", vxlan
->link
);
1305 dev
->mtu
= lowerdev
->mtu
- VXLAN_HEADROOM
;
1307 /* update header length based on lower device */
1308 dev
->hard_header_len
= lowerdev
->hard_header_len
+
1312 if (data
[IFLA_VXLAN_TOS
])
1313 vxlan
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
1315 if (data
[IFLA_VXLAN_TTL
])
1316 vxlan
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
1318 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
1319 vxlan
->flags
|= VXLAN_F_LEARN
;
1321 if (data
[IFLA_VXLAN_AGEING
])
1322 vxlan
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
1324 vxlan
->age_interval
= FDB_AGE_DEFAULT
;
1326 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
1327 vxlan
->flags
|= VXLAN_F_PROXY
;
1329 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
1330 vxlan
->flags
|= VXLAN_F_RSC
;
1332 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
1333 vxlan
->flags
|= VXLAN_F_L2MISS
;
1335 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
1336 vxlan
->flags
|= VXLAN_F_L3MISS
;
1338 if (data
[IFLA_VXLAN_LIMIT
])
1339 vxlan
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
1341 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
1342 const struct ifla_vxlan_port_range
*p
1343 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
1344 vxlan
->port_min
= ntohs(p
->low
);
1345 vxlan
->port_max
= ntohs(p
->high
);
1348 err
= register_netdevice(dev
);
1350 hlist_add_head_rcu(&vxlan
->hlist
, vni_head(net
, vxlan
->vni
));
1355 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
1357 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1359 hlist_del_rcu(&vxlan
->hlist
);
1361 unregister_netdevice_queue(dev
, head
);
1364 static size_t vxlan_get_size(const struct net_device
*dev
)
1367 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
1368 nla_total_size(sizeof(__be32
)) +/* IFLA_VXLAN_GROUP */
1369 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
1370 nla_total_size(sizeof(__be32
))+ /* IFLA_VXLAN_LOCAL */
1371 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
1372 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
1373 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
1374 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
1375 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
1376 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
1377 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
1378 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
1379 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
1380 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
1384 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1386 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1387 struct ifla_vxlan_port_range ports
= {
1388 .low
= htons(vxlan
->port_min
),
1389 .high
= htons(vxlan
->port_max
),
1392 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, vxlan
->vni
))
1393 goto nla_put_failure
;
1395 if (vxlan
->gaddr
&& nla_put_be32(skb
, IFLA_VXLAN_GROUP
, vxlan
->gaddr
))
1396 goto nla_put_failure
;
1398 if (vxlan
->link
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, vxlan
->link
))
1399 goto nla_put_failure
;
1401 if (vxlan
->saddr
&& nla_put_be32(skb
, IFLA_VXLAN_LOCAL
, vxlan
->saddr
))
1402 goto nla_put_failure
;
1404 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->ttl
) ||
1405 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->tos
) ||
1406 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
1407 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
1408 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
1409 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
1410 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
1411 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
1412 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
1413 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
1414 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
1415 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->age_interval
) ||
1416 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->addrmax
))
1417 goto nla_put_failure
;
1419 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
1420 goto nla_put_failure
;
1428 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
1430 .maxtype
= IFLA_VXLAN_MAX
,
1431 .policy
= vxlan_policy
,
1432 .priv_size
= sizeof(struct vxlan_dev
),
1433 .setup
= vxlan_setup
,
1434 .validate
= vxlan_validate
,
1435 .newlink
= vxlan_newlink
,
1436 .dellink
= vxlan_dellink
,
1437 .get_size
= vxlan_get_size
,
1438 .fill_info
= vxlan_fill_info
,
1441 static __net_init
int vxlan_init_net(struct net
*net
)
1443 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
1445 struct sockaddr_in vxlan_addr
= {
1446 .sin_family
= AF_INET
,
1447 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
1452 /* Create UDP socket for encapsulation receive. */
1453 rc
= sock_create_kern(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &vn
->sock
);
1455 pr_debug("UDP socket create failed\n");
1458 /* Put in proper namespace */
1460 sk_change_net(sk
, net
);
1462 vxlan_addr
.sin_port
= htons(vxlan_port
);
1464 rc
= kernel_bind(vn
->sock
, (struct sockaddr
*) &vxlan_addr
,
1465 sizeof(vxlan_addr
));
1467 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1468 &vxlan_addr
.sin_addr
, ntohs(vxlan_addr
.sin_port
), rc
);
1469 sk_release_kernel(sk
);
1474 /* Disable multicast loopback */
1475 inet_sk(sk
)->mc_loop
= 0;
1477 /* Mark socket as an encapsulation socket. */
1478 udp_sk(sk
)->encap_type
= 1;
1479 udp_sk(sk
)->encap_rcv
= vxlan_udp_encap_recv
;
1482 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
1483 INIT_HLIST_HEAD(&vn
->vni_list
[h
]);
1488 static __net_exit
void vxlan_exit_net(struct net
*net
)
1490 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
1493 sk_release_kernel(vn
->sock
->sk
);
1498 static struct pernet_operations vxlan_net_ops
= {
1499 .init
= vxlan_init_net
,
1500 .exit
= vxlan_exit_net
,
1501 .id
= &vxlan_net_id
,
1502 .size
= sizeof(struct vxlan_net
),
1505 static int __init
vxlan_init_module(void)
1509 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
1511 rc
= register_pernet_device(&vxlan_net_ops
);
1515 rc
= rtnl_link_register(&vxlan_link_ops
);
1522 unregister_pernet_device(&vxlan_net_ops
);
1526 module_init(vxlan_init_module
);
1528 static void __exit
vxlan_cleanup_module(void)
1530 rtnl_link_unregister(&vxlan_link_ops
);
1531 unregister_pernet_device(&vxlan_net_ops
);
1533 module_exit(vxlan_cleanup_module
);
1535 MODULE_LICENSE("GPL");
1536 MODULE_VERSION(VXLAN_VERSION
);
1537 MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
1538 MODULE_ALIAS_RTNL_LINK("vxlan");