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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/rculist.h>
23 #include <linux/netdevice.h>
26 #include <linux/udp.h>
27 #include <linux/igmp.h>
28 #include <linux/etherdevice.h>
29 #include <linux/if_ether.h>
30 #include <linux/hash.h>
31 #include <linux/ethtool.h>
33 #include <net/ndisc.h>
35 #include <net/ip_tunnels.h>
38 #include <net/rtnetlink.h>
39 #include <net/route.h>
40 #include <net/dsfield.h>
41 #include <net/inet_ecn.h>
42 #include <net/net_namespace.h>
43 #include <net/netns/generic.h>
45 #define VXLAN_VERSION "0.1"
47 #define VNI_HASH_BITS 10
48 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
49 #define FDB_HASH_BITS 8
50 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
51 #define FDB_AGE_DEFAULT 300 /* 5 min */
52 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
54 #define VXLAN_N_VID (1u << 24)
55 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
56 /* IP header + UDP + VXLAN + Ethernet header */
57 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
59 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
61 /* VXLAN protocol header */
67 /* UDP port for VXLAN traffic.
68 * The IANA assigned port is 4789, but the Linux default is 8472
69 * for compatability with early adopters.
71 static unsigned int vxlan_port __read_mostly
= 8472;
72 module_param_named(udp_port
, vxlan_port
, uint
, 0444);
73 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
75 static bool log_ecn_error
= true;
76 module_param(log_ecn_error
, bool, 0644);
77 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
79 /* per-net private data for this module */
80 static unsigned int vxlan_net_id
;
82 struct socket
*sock
; /* UDP encap socket */
83 struct hlist_head vni_list
[VNI_HASH_SIZE
];
92 struct vxlan_rdst
*remote_next
;
95 /* Forwarding table entry */
97 struct hlist_node hlist
; /* linked list of entries */
99 unsigned long updated
; /* jiffies */
101 struct vxlan_rdst remote
;
102 u16 state
; /* see ndm_state */
103 u8 flags
; /* see ndm_flags */
104 u8 eth_addr
[ETH_ALEN
];
107 /* Pseudo network device */
109 struct hlist_node hlist
;
110 struct net_device
*dev
;
111 struct vxlan_rdst default_dst
; /* default destination */
112 __be32 saddr
; /* source address */
113 __u16 port_min
; /* source port range */
115 __u8 tos
; /* TOS override */
117 u32 flags
; /* VXLAN_F_* below */
119 unsigned long age_interval
;
120 struct timer_list age_timer
;
121 spinlock_t hash_lock
;
122 unsigned int addrcnt
;
123 unsigned int addrmax
;
125 struct hlist_head fdb_head
[FDB_HASH_SIZE
];
128 #define VXLAN_F_LEARN 0x01
129 #define VXLAN_F_PROXY 0x02
130 #define VXLAN_F_RSC 0x04
131 #define VXLAN_F_L2MISS 0x08
132 #define VXLAN_F_L3MISS 0x10
134 /* salt for hash table */
135 static u32 vxlan_salt __read_mostly
;
137 static inline struct hlist_head
*vni_head(struct net
*net
, u32 id
)
139 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
141 return &vn
->vni_list
[hash_32(id
, VNI_HASH_BITS
)];
144 /* Look up VNI in a per net namespace table */
145 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, u32 id
)
147 struct vxlan_dev
*vxlan
;
149 hlist_for_each_entry_rcu(vxlan
, vni_head(net
, id
), hlist
) {
150 if (vxlan
->default_dst
.remote_vni
== id
)
157 /* Fill in neighbour message in skbuff. */
158 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
159 const struct vxlan_fdb
*fdb
,
160 u32 portid
, u32 seq
, int type
, unsigned int flags
,
161 const struct vxlan_rdst
*rdst
)
163 unsigned long now
= jiffies
;
164 struct nda_cacheinfo ci
;
165 struct nlmsghdr
*nlh
;
167 bool send_ip
, send_eth
;
169 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
173 ndm
= nlmsg_data(nlh
);
174 memset(ndm
, 0, sizeof(*ndm
));
176 send_eth
= send_ip
= true;
178 if (type
== RTM_GETNEIGH
) {
179 ndm
->ndm_family
= AF_INET
;
180 send_ip
= rdst
->remote_ip
!= htonl(INADDR_ANY
);
181 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
183 ndm
->ndm_family
= AF_BRIDGE
;
184 ndm
->ndm_state
= fdb
->state
;
185 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
186 ndm
->ndm_flags
= fdb
->flags
;
187 ndm
->ndm_type
= NDA_DST
;
189 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
190 goto nla_put_failure
;
192 if (send_ip
&& nla_put_be32(skb
, NDA_DST
, rdst
->remote_ip
))
193 goto nla_put_failure
;
195 if (rdst
->remote_port
&& rdst
->remote_port
!= htons(vxlan_port
) &&
196 nla_put_be16(skb
, NDA_PORT
, rdst
->remote_port
))
197 goto nla_put_failure
;
198 if (rdst
->remote_vni
!= vxlan
->default_dst
.remote_vni
&&
199 nla_put_be32(skb
, NDA_VNI
, rdst
->remote_vni
))
200 goto nla_put_failure
;
201 if (rdst
->remote_ifindex
&&
202 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
203 goto nla_put_failure
;
205 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
206 ci
.ndm_confirmed
= 0;
207 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
210 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
211 goto nla_put_failure
;
213 return nlmsg_end(skb
, nlh
);
216 nlmsg_cancel(skb
, nlh
);
220 static inline size_t vxlan_nlmsg_size(void)
222 return NLMSG_ALIGN(sizeof(struct ndmsg
))
223 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
224 + nla_total_size(sizeof(__be32
)) /* NDA_DST */
225 + nla_total_size(sizeof(__be16
)) /* NDA_PORT */
226 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
227 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
228 + nla_total_size(sizeof(struct nda_cacheinfo
));
231 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
,
232 const struct vxlan_fdb
*fdb
, int type
)
234 struct net
*net
= dev_net(vxlan
->dev
);
238 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
242 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0, &fdb
->remote
);
244 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
245 WARN_ON(err
== -EMSGSIZE
);
250 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
254 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
257 static void vxlan_ip_miss(struct net_device
*dev
, __be32 ipa
)
259 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
262 memset(&f
, 0, sizeof f
);
264 f
.remote
.remote_ip
= ipa
; /* goes to NDA_DST */
265 f
.remote
.remote_vni
= VXLAN_N_VID
;
267 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
270 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
274 memset(&f
, 0, sizeof f
);
276 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
278 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
281 /* Hash Ethernet address */
282 static u32
eth_hash(const unsigned char *addr
)
284 u64 value
= get_unaligned((u64
*)addr
);
286 /* only want 6 bytes */
292 return hash_64(value
, FDB_HASH_BITS
);
295 /* Hash chain to use given mac address */
296 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
299 return &vxlan
->fdb_head
[eth_hash(mac
)];
302 /* Look up Ethernet address in forwarding table */
303 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
307 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
);
310 hlist_for_each_entry_rcu(f
, head
, hlist
) {
311 if (compare_ether_addr(mac
, f
->eth_addr
) == 0)
318 /* Add/update destinations for multicast */
319 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
320 __be32 ip
, __be16 port
, __u32 vni
, __u32 ifindex
)
322 struct vxlan_rdst
*rd_prev
, *rd
;
325 for (rd
= &f
->remote
; rd
; rd
= rd
->remote_next
) {
326 if (rd
->remote_ip
== ip
&&
327 rd
->remote_port
== port
&&
328 rd
->remote_vni
== vni
&&
329 rd
->remote_ifindex
== ifindex
)
333 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
337 rd
->remote_port
= port
;
338 rd
->remote_vni
= vni
;
339 rd
->remote_ifindex
= ifindex
;
340 rd
->remote_next
= NULL
;
341 rd_prev
->remote_next
= rd
;
345 /* Add new entry to forwarding table -- assumes lock held */
346 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
347 const u8
*mac
, __be32 ip
,
348 __u16 state
, __u16 flags
,
349 __be16 port
, __u32 vni
, __u32 ifindex
,
355 f
= vxlan_find_mac(vxlan
, mac
);
357 if (flags
& NLM_F_EXCL
) {
358 netdev_dbg(vxlan
->dev
,
359 "lost race to create %pM\n", mac
);
362 if (f
->state
!= state
) {
364 f
->updated
= jiffies
;
367 if (f
->flags
!= ndm_flags
) {
368 f
->flags
= ndm_flags
;
369 f
->updated
= jiffies
;
372 if ((flags
& NLM_F_APPEND
) &&
373 is_multicast_ether_addr(f
->eth_addr
)) {
374 int rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
);
381 if (!(flags
& NLM_F_CREATE
))
384 if (vxlan
->addrmax
&& vxlan
->addrcnt
>= vxlan
->addrmax
)
387 netdev_dbg(vxlan
->dev
, "add %pM -> %pI4\n", mac
, &ip
);
388 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
393 f
->remote
.remote_ip
= ip
;
394 f
->remote
.remote_port
= port
;
395 f
->remote
.remote_vni
= vni
;
396 f
->remote
.remote_ifindex
= ifindex
;
397 f
->remote
.remote_next
= NULL
;
399 f
->flags
= ndm_flags
;
400 f
->updated
= f
->used
= jiffies
;
401 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
404 hlist_add_head_rcu(&f
->hlist
,
405 vxlan_fdb_head(vxlan
, mac
));
409 vxlan_fdb_notify(vxlan
, f
, RTM_NEWNEIGH
);
414 static void vxlan_fdb_free(struct rcu_head
*head
)
416 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
418 while (f
->remote
.remote_next
) {
419 struct vxlan_rdst
*rd
= f
->remote
.remote_next
;
421 f
->remote
.remote_next
= rd
->remote_next
;
427 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
429 netdev_dbg(vxlan
->dev
,
430 "delete %pM\n", f
->eth_addr
);
433 vxlan_fdb_notify(vxlan
, f
, RTM_DELNEIGH
);
435 hlist_del_rcu(&f
->hlist
);
436 call_rcu(&f
->rcu
, vxlan_fdb_free
);
439 /* Add static entry (via netlink) */
440 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
441 struct net_device
*dev
,
442 const unsigned char *addr
, u16 flags
)
444 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
445 struct net
*net
= dev_net(vxlan
->dev
);
451 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
452 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
457 if (tb
[NDA_DST
] == NULL
)
460 if (nla_len(tb
[NDA_DST
]) != sizeof(__be32
))
461 return -EAFNOSUPPORT
;
463 ip
= nla_get_be32(tb
[NDA_DST
]);
466 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
468 port
= nla_get_be16(tb
[NDA_PORT
]);
470 port
= htons(vxlan_port
);
473 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
475 vni
= nla_get_u32(tb
[NDA_VNI
]);
477 vni
= vxlan
->default_dst
.remote_vni
;
479 if (tb
[NDA_IFINDEX
]) {
480 struct net_device
*tdev
;
482 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
484 ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
485 tdev
= dev_get_by_index(net
, ifindex
);
487 return -EADDRNOTAVAIL
;
492 spin_lock_bh(&vxlan
->hash_lock
);
493 err
= vxlan_fdb_create(vxlan
, addr
, ip
, ndm
->ndm_state
, flags
,
494 port
, vni
, ifindex
, ndm
->ndm_flags
);
495 spin_unlock_bh(&vxlan
->hash_lock
);
500 /* Delete entry (via netlink) */
501 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
502 struct net_device
*dev
,
503 const unsigned char *addr
)
505 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
509 spin_lock_bh(&vxlan
->hash_lock
);
510 f
= vxlan_find_mac(vxlan
, addr
);
512 vxlan_fdb_destroy(vxlan
, f
);
515 spin_unlock_bh(&vxlan
->hash_lock
);
520 /* Dump forwarding table */
521 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
522 struct net_device
*dev
, int idx
)
524 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
527 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
531 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
532 struct vxlan_rdst
*rd
;
533 for (rd
= &f
->remote
; rd
; rd
= rd
->remote_next
) {
534 if (idx
< cb
->args
[0])
537 err
= vxlan_fdb_info(skb
, vxlan
, f
,
538 NETLINK_CB(cb
->skb
).portid
,
553 /* Watch incoming packets to learn mapping between Ethernet address
554 * and Tunnel endpoint.
556 static void vxlan_snoop(struct net_device
*dev
,
557 __be32 src_ip
, const u8
*src_mac
)
559 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
563 f
= vxlan_find_mac(vxlan
, src_mac
);
566 if (likely(f
->remote
.remote_ip
== src_ip
))
571 "%pM migrated from %pI4 to %pI4\n",
572 src_mac
, &f
->remote
.remote_ip
, &src_ip
);
574 f
->remote
.remote_ip
= src_ip
;
575 f
->updated
= jiffies
;
577 /* learned new entry */
578 spin_lock(&vxlan
->hash_lock
);
579 err
= vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
581 NLM_F_EXCL
|NLM_F_CREATE
,
583 vxlan
->default_dst
.remote_vni
,
585 spin_unlock(&vxlan
->hash_lock
);
590 /* See if multicast group is already in use by other ID */
591 static bool vxlan_group_used(struct vxlan_net
*vn
,
592 const struct vxlan_dev
*this)
594 const struct vxlan_dev
*vxlan
;
597 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
598 hlist_for_each_entry(vxlan
, &vn
->vni_list
[h
], hlist
) {
602 if (!netif_running(vxlan
->dev
))
605 if (vxlan
->default_dst
.remote_ip
== this->default_dst
.remote_ip
)
612 /* kernel equivalent to IP_ADD_MEMBERSHIP */
613 static int vxlan_join_group(struct net_device
*dev
)
615 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
616 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
617 struct sock
*sk
= vn
->sock
->sk
;
618 struct ip_mreqn mreq
= {
619 .imr_multiaddr
.s_addr
= vxlan
->default_dst
.remote_ip
,
620 .imr_ifindex
= vxlan
->default_dst
.remote_ifindex
,
624 /* Already a member of group */
625 if (vxlan_group_used(vn
, vxlan
))
628 /* Need to drop RTNL to call multicast join */
631 err
= ip_mc_join_group(sk
, &mreq
);
639 /* kernel equivalent to IP_DROP_MEMBERSHIP */
640 static int vxlan_leave_group(struct net_device
*dev
)
642 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
643 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
645 struct sock
*sk
= vn
->sock
->sk
;
646 struct ip_mreqn mreq
= {
647 .imr_multiaddr
.s_addr
= vxlan
->default_dst
.remote_ip
,
648 .imr_ifindex
= vxlan
->default_dst
.remote_ifindex
,
651 /* Only leave group when last vxlan is done. */
652 if (vxlan_group_used(vn
, vxlan
))
655 /* Need to drop RTNL to call multicast leave */
658 err
= ip_mc_leave_group(sk
, &mreq
);
665 /* Callback from net/ipv4/udp.c to receive packets */
666 static int vxlan_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
669 struct vxlanhdr
*vxh
;
670 struct vxlan_dev
*vxlan
;
671 struct pcpu_tstats
*stats
;
675 /* pop off outer UDP header */
676 __skb_pull(skb
, sizeof(struct udphdr
));
678 /* Need Vxlan and inner Ethernet header to be present */
679 if (!pskb_may_pull(skb
, sizeof(struct vxlanhdr
)))
682 /* Drop packets with reserved bits set */
683 vxh
= (struct vxlanhdr
*) skb
->data
;
684 if (vxh
->vx_flags
!= htonl(VXLAN_FLAGS
) ||
685 (vxh
->vx_vni
& htonl(0xff))) {
686 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
687 ntohl(vxh
->vx_flags
), ntohl(vxh
->vx_vni
));
691 __skb_pull(skb
, sizeof(struct vxlanhdr
));
693 /* Is this VNI defined? */
694 vni
= ntohl(vxh
->vx_vni
) >> 8;
695 vxlan
= vxlan_find_vni(sock_net(sk
), vni
);
697 netdev_dbg(skb
->dev
, "unknown vni %d\n", vni
);
701 if (!pskb_may_pull(skb
, ETH_HLEN
)) {
702 vxlan
->dev
->stats
.rx_length_errors
++;
703 vxlan
->dev
->stats
.rx_errors
++;
707 skb_reset_mac_header(skb
);
709 /* Re-examine inner Ethernet packet */
711 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
713 /* Ignore packet loops (and multicast echo) */
714 if (compare_ether_addr(eth_hdr(skb
)->h_source
,
715 vxlan
->dev
->dev_addr
) == 0)
718 if (vxlan
->flags
& VXLAN_F_LEARN
)
719 vxlan_snoop(skb
->dev
, oip
->saddr
, eth_hdr(skb
)->h_source
);
721 __skb_tunnel_rx(skb
, vxlan
->dev
);
722 skb_reset_network_header(skb
);
724 /* If the NIC driver gave us an encapsulated packet with
725 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
726 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
727 * for us. Otherwise force the upper layers to verify it.
729 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
|| !skb
->encapsulation
||
730 !(vxlan
->dev
->features
& NETIF_F_RXCSUM
))
731 skb
->ip_summed
= CHECKSUM_NONE
;
733 skb
->encapsulation
= 0;
735 err
= IP_ECN_decapsulate(oip
, skb
);
738 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
739 &oip
->saddr
, oip
->tos
);
741 ++vxlan
->dev
->stats
.rx_frame_errors
;
742 ++vxlan
->dev
->stats
.rx_errors
;
747 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
748 u64_stats_update_begin(&stats
->syncp
);
750 stats
->rx_bytes
+= skb
->len
;
751 u64_stats_update_end(&stats
->syncp
);
757 /* Put UDP header back */
758 __skb_push(skb
, sizeof(struct udphdr
));
762 /* Consume bad packet */
767 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
769 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
775 if (dev
->flags
& IFF_NOARP
)
778 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
779 dev
->stats
.tx_dropped
++;
784 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
785 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
786 parp
->ar_pro
!= htons(ETH_P_IP
) ||
787 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
788 parp
->ar_hln
!= dev
->addr_len
||
791 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
793 arpptr
+= dev
->addr_len
; /* sha */
794 memcpy(&sip
, arpptr
, sizeof(sip
));
795 arpptr
+= sizeof(sip
);
796 arpptr
+= dev
->addr_len
; /* tha */
797 memcpy(&tip
, arpptr
, sizeof(tip
));
799 if (ipv4_is_loopback(tip
) ||
800 ipv4_is_multicast(tip
))
803 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
807 struct sk_buff
*reply
;
809 if (!(n
->nud_state
& NUD_CONNECTED
)) {
814 f
= vxlan_find_mac(vxlan
, n
->ha
);
815 if (f
&& f
->remote
.remote_ip
== htonl(INADDR_ANY
)) {
816 /* bridge-local neighbor */
821 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
826 skb_reset_mac_header(reply
);
827 __skb_pull(reply
, skb_network_offset(reply
));
828 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
829 reply
->pkt_type
= PACKET_HOST
;
831 if (netif_rx_ni(reply
) == NET_RX_DROP
)
832 dev
->stats
.rx_dropped
++;
833 } else if (vxlan
->flags
& VXLAN_F_L3MISS
)
834 vxlan_ip_miss(dev
, tip
);
840 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
842 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
846 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
850 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
852 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
855 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
864 diff
= compare_ether_addr(eth_hdr(skb
)->h_dest
, n
->ha
) != 0;
866 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
868 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
872 } else if (vxlan
->flags
& VXLAN_F_L3MISS
)
873 vxlan_ip_miss(dev
, pip
->daddr
);
877 static void vxlan_sock_free(struct sk_buff
*skb
)
882 /* On transmit, associate with the tunnel socket */
883 static void vxlan_set_owner(struct net_device
*dev
, struct sk_buff
*skb
)
885 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
886 struct sock
*sk
= vn
->sock
->sk
;
891 skb
->destructor
= vxlan_sock_free
;
894 /* Compute source port for outgoing packet
895 * first choice to use L4 flow hash since it will spread
896 * better and maybe available from hardware
897 * secondary choice is to use jhash on the Ethernet header
899 static __be16
vxlan_src_port(const struct vxlan_dev
*vxlan
, struct sk_buff
*skb
)
901 unsigned int range
= (vxlan
->port_max
- vxlan
->port_min
) + 1;
904 hash
= skb_get_rxhash(skb
);
906 hash
= jhash(skb
->data
, 2 * ETH_ALEN
,
907 (__force u32
) skb
->protocol
);
909 return htons((((u64
) hash
* range
) >> 32) + vxlan
->port_min
);
912 static int handle_offloads(struct sk_buff
*skb
)
914 if (skb_is_gso(skb
)) {
915 int err
= skb_unclone(skb
, GFP_ATOMIC
);
919 skb_shinfo(skb
)->gso_type
|= (SKB_GSO_UDP_TUNNEL
| SKB_GSO_UDP
);
920 } else if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
921 skb
->ip_summed
= CHECKSUM_NONE
;
926 /* Bypass encapsulation if the destination is local */
927 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
928 struct vxlan_dev
*dst_vxlan
)
930 struct pcpu_tstats
*tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
931 struct pcpu_tstats
*rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
933 skb
->pkt_type
= PACKET_HOST
;
934 skb
->encapsulation
= 0;
935 skb
->dev
= dst_vxlan
->dev
;
936 __skb_pull(skb
, skb_network_offset(skb
));
938 if (dst_vxlan
->flags
& VXLAN_F_LEARN
)
939 vxlan_snoop(skb
->dev
, htonl(INADDR_LOOPBACK
),
940 eth_hdr(skb
)->h_source
);
942 u64_stats_update_begin(&tx_stats
->syncp
);
943 tx_stats
->tx_packets
++;
944 tx_stats
->tx_bytes
+= skb
->len
;
945 u64_stats_update_end(&tx_stats
->syncp
);
947 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
948 u64_stats_update_begin(&rx_stats
->syncp
);
949 rx_stats
->rx_packets
++;
950 rx_stats
->rx_bytes
+= skb
->len
;
951 u64_stats_update_end(&rx_stats
->syncp
);
953 skb
->dev
->stats
.rx_dropped
++;
957 static netdev_tx_t
vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
958 struct vxlan_rdst
*rdst
, bool did_rsc
)
960 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
962 const struct iphdr
*old_iph
;
964 struct vxlanhdr
*vxh
;
968 __be16 src_port
, dst_port
;
973 dst_port
= rdst
->remote_port
? rdst
->remote_port
: htons(vxlan_port
);
974 vni
= rdst
->remote_vni
;
975 dst
= rdst
->remote_ip
;
979 /* short-circuited back to local bridge */
980 vxlan_encap_bypass(skb
, vxlan
, vxlan
);
986 if (!skb
->encapsulation
) {
987 skb_reset_inner_headers(skb
);
988 skb
->encapsulation
= 1;
991 /* Need space for new headers (invalidates iph ptr) */
992 if (skb_cow_head(skb
, VXLAN_HEADROOM
))
995 old_iph
= ip_hdr(skb
);
998 if (!ttl
&& IN_MULTICAST(ntohl(dst
)))
1003 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
1005 src_port
= vxlan_src_port(vxlan
, skb
);
1007 memset(&fl4
, 0, sizeof(fl4
));
1008 fl4
.flowi4_oif
= rdst
->remote_ifindex
;
1009 fl4
.flowi4_tos
= RT_TOS(tos
);
1011 fl4
.saddr
= vxlan
->saddr
;
1013 rt
= ip_route_output_key(dev_net(dev
), &fl4
);
1015 netdev_dbg(dev
, "no route to %pI4\n", &dst
);
1016 dev
->stats
.tx_carrier_errors
++;
1020 if (rt
->dst
.dev
== dev
) {
1021 netdev_dbg(dev
, "circular route to %pI4\n", &dst
);
1023 dev
->stats
.collisions
++;
1027 /* Bypass encapsulation if the destination is local */
1028 if (rt
->rt_flags
& RTCF_LOCAL
&&
1029 !(rt
->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
1030 struct vxlan_dev
*dst_vxlan
;
1033 dst_vxlan
= vxlan_find_vni(dev_net(dev
), vni
);
1036 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
1037 return NETDEV_TX_OK
;
1040 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1041 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1044 skb_dst_set(skb
, &rt
->dst
);
1046 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1047 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1048 vxh
->vx_vni
= htonl(vni
<< 8);
1050 __skb_push(skb
, sizeof(*uh
));
1051 skb_reset_transport_header(skb
);
1054 uh
->dest
= dst_port
;
1055 uh
->source
= src_port
;
1057 uh
->len
= htons(skb
->len
);
1060 __skb_push(skb
, sizeof(*iph
));
1061 skb_reset_network_header(skb
);
1064 iph
->ihl
= sizeof(struct iphdr
) >> 2;
1066 iph
->protocol
= IPPROTO_UDP
;
1067 iph
->tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
1069 iph
->saddr
= fl4
.saddr
;
1070 iph
->ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
1071 tunnel_ip_select_ident(skb
, old_iph
, &rt
->dst
);
1075 vxlan_set_owner(dev
, skb
);
1077 if (handle_offloads(skb
))
1080 iptunnel_xmit(skb
, dev
);
1081 return NETDEV_TX_OK
;
1084 dev
->stats
.tx_dropped
++;
1088 dev
->stats
.tx_errors
++;
1091 return NETDEV_TX_OK
;
1094 /* Transmit local packets over Vxlan
1096 * Outer IP header inherits ECN and DF from inner header.
1097 * Outer UDP destination is the VXLAN assigned port.
1098 * source port is based on hash of flow
1100 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1102 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1104 bool did_rsc
= false;
1105 struct vxlan_rdst
*rdst0
, *rdst
;
1106 struct vxlan_fdb
*f
;
1109 skb_reset_mac_header(skb
);
1112 if ((vxlan
->flags
& VXLAN_F_PROXY
) && ntohs(eth
->h_proto
) == ETH_P_ARP
)
1113 return arp_reduce(dev
, skb
);
1115 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1118 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->flags
& VXLAN_F_RSC
) &&
1119 ntohs(eth
->h_proto
) == ETH_P_IP
) {
1120 did_rsc
= route_shortcircuit(dev
, skb
);
1122 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1126 rdst0
= &vxlan
->default_dst
;
1128 if (rdst0
->remote_ip
== htonl(INADDR_ANY
) &&
1129 (vxlan
->flags
& VXLAN_F_L2MISS
) &&
1130 !is_multicast_ether_addr(eth
->h_dest
))
1131 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
1137 /* if there are multiple destinations, send copies */
1138 for (rdst
= rdst0
->remote_next
; rdst
; rdst
= rdst
->remote_next
) {
1139 struct sk_buff
*skb1
;
1141 skb1
= skb_clone(skb
, GFP_ATOMIC
);
1142 rc1
= vxlan_xmit_one(skb1
, dev
, rdst
, did_rsc
);
1143 if (rc
== NETDEV_TX_OK
)
1147 rc1
= vxlan_xmit_one(skb
, dev
, rdst0
, did_rsc
);
1148 if (rc
== NETDEV_TX_OK
)
1153 /* Walk the forwarding table and purge stale entries */
1154 static void vxlan_cleanup(unsigned long arg
)
1156 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
1157 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
1160 if (!netif_running(vxlan
->dev
))
1163 spin_lock_bh(&vxlan
->hash_lock
);
1164 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1165 struct hlist_node
*p
, *n
;
1166 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1168 = container_of(p
, struct vxlan_fdb
, hlist
);
1169 unsigned long timeout
;
1171 if (f
->state
& NUD_PERMANENT
)
1174 timeout
= f
->used
+ vxlan
->age_interval
* HZ
;
1175 if (time_before_eq(timeout
, jiffies
)) {
1176 netdev_dbg(vxlan
->dev
,
1177 "garbage collect %pM\n",
1179 f
->state
= NUD_STALE
;
1180 vxlan_fdb_destroy(vxlan
, f
);
1181 } else if (time_before(timeout
, next_timer
))
1182 next_timer
= timeout
;
1185 spin_unlock_bh(&vxlan
->hash_lock
);
1187 mod_timer(&vxlan
->age_timer
, next_timer
);
1190 /* Setup stats when device is created */
1191 static int vxlan_init(struct net_device
*dev
)
1193 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
1200 /* Start ageing timer and join group when device is brought up */
1201 static int vxlan_open(struct net_device
*dev
)
1203 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1206 if (IN_MULTICAST(ntohl(vxlan
->default_dst
.remote_ip
))) {
1207 err
= vxlan_join_group(dev
);
1212 if (vxlan
->age_interval
)
1213 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
1218 /* Purge the forwarding table */
1219 static void vxlan_flush(struct vxlan_dev
*vxlan
)
1223 spin_lock_bh(&vxlan
->hash_lock
);
1224 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1225 struct hlist_node
*p
, *n
;
1226 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1228 = container_of(p
, struct vxlan_fdb
, hlist
);
1229 vxlan_fdb_destroy(vxlan
, f
);
1232 spin_unlock_bh(&vxlan
->hash_lock
);
1235 /* Cleanup timer and forwarding table on shutdown */
1236 static int vxlan_stop(struct net_device
*dev
)
1238 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1240 if (IN_MULTICAST(ntohl(vxlan
->default_dst
.remote_ip
)))
1241 vxlan_leave_group(dev
);
1243 del_timer_sync(&vxlan
->age_timer
);
1250 /* Stub, nothing needs to be done. */
1251 static void vxlan_set_multicast_list(struct net_device
*dev
)
1255 static const struct net_device_ops vxlan_netdev_ops
= {
1256 .ndo_init
= vxlan_init
,
1257 .ndo_open
= vxlan_open
,
1258 .ndo_stop
= vxlan_stop
,
1259 .ndo_start_xmit
= vxlan_xmit
,
1260 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1261 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
1262 .ndo_change_mtu
= eth_change_mtu
,
1263 .ndo_validate_addr
= eth_validate_addr
,
1264 .ndo_set_mac_address
= eth_mac_addr
,
1265 .ndo_fdb_add
= vxlan_fdb_add
,
1266 .ndo_fdb_del
= vxlan_fdb_delete
,
1267 .ndo_fdb_dump
= vxlan_fdb_dump
,
1270 /* Info for udev, that this is a virtual tunnel endpoint */
1271 static struct device_type vxlan_type
= {
1275 static void vxlan_free(struct net_device
*dev
)
1277 free_percpu(dev
->tstats
);
1281 /* Initialize the device structure. */
1282 static void vxlan_setup(struct net_device
*dev
)
1284 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1288 eth_hw_addr_random(dev
);
1290 dev
->hard_header_len
= ETH_HLEN
+ VXLAN_HEADROOM
;
1292 dev
->netdev_ops
= &vxlan_netdev_ops
;
1293 dev
->destructor
= vxlan_free
;
1294 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
1296 dev
->tx_queue_len
= 0;
1297 dev
->features
|= NETIF_F_LLTX
;
1298 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1299 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
1300 dev
->features
|= NETIF_F_RXCSUM
;
1301 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
1303 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1304 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1305 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1306 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
1308 spin_lock_init(&vxlan
->hash_lock
);
1310 init_timer_deferrable(&vxlan
->age_timer
);
1311 vxlan
->age_timer
.function
= vxlan_cleanup
;
1312 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
1314 inet_get_local_port_range(&low
, &high
);
1315 vxlan
->port_min
= low
;
1316 vxlan
->port_max
= high
;
1320 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
1321 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
1324 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
1325 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
1326 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
1327 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
1328 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
1329 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
1330 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
1331 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
1332 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
1333 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
1334 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
1335 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
1336 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
1337 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
1338 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
1341 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1343 if (tb
[IFLA_ADDRESS
]) {
1344 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
1345 pr_debug("invalid link address (not ethernet)\n");
1349 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
1350 pr_debug("invalid all zero ethernet address\n");
1351 return -EADDRNOTAVAIL
;
1358 if (data
[IFLA_VXLAN_ID
]) {
1359 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
1360 if (id
>= VXLAN_VID_MASK
)
1364 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
1365 const struct ifla_vxlan_port_range
*p
1366 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
1368 if (ntohs(p
->high
) < ntohs(p
->low
)) {
1369 pr_debug("port range %u .. %u not valid\n",
1370 ntohs(p
->low
), ntohs(p
->high
));
1378 static void vxlan_get_drvinfo(struct net_device
*netdev
,
1379 struct ethtool_drvinfo
*drvinfo
)
1381 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
1382 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
1385 static const struct ethtool_ops vxlan_ethtool_ops
= {
1386 .get_drvinfo
= vxlan_get_drvinfo
,
1387 .get_link
= ethtool_op_get_link
,
1390 static int vxlan_newlink(struct net
*net
, struct net_device
*dev
,
1391 struct nlattr
*tb
[], struct nlattr
*data
[])
1393 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1394 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
1398 if (!data
[IFLA_VXLAN_ID
])
1401 vni
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
1402 if (vxlan_find_vni(net
, vni
)) {
1403 pr_info("duplicate VNI %u\n", vni
);
1406 dst
->remote_vni
= vni
;
1408 if (data
[IFLA_VXLAN_GROUP
])
1409 dst
->remote_ip
= nla_get_be32(data
[IFLA_VXLAN_GROUP
]);
1411 if (data
[IFLA_VXLAN_LOCAL
])
1412 vxlan
->saddr
= nla_get_be32(data
[IFLA_VXLAN_LOCAL
]);
1414 if (data
[IFLA_VXLAN_LINK
] &&
1415 (dst
->remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]))) {
1416 struct net_device
*lowerdev
1417 = __dev_get_by_index(net
, dst
->remote_ifindex
);
1420 pr_info("ifindex %d does not exist\n", dst
->remote_ifindex
);
1425 dev
->mtu
= lowerdev
->mtu
- VXLAN_HEADROOM
;
1427 /* update header length based on lower device */
1428 dev
->hard_header_len
= lowerdev
->hard_header_len
+
1432 if (data
[IFLA_VXLAN_TOS
])
1433 vxlan
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
1435 if (data
[IFLA_VXLAN_TTL
])
1436 vxlan
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
1438 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
1439 vxlan
->flags
|= VXLAN_F_LEARN
;
1441 if (data
[IFLA_VXLAN_AGEING
])
1442 vxlan
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
1444 vxlan
->age_interval
= FDB_AGE_DEFAULT
;
1446 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
1447 vxlan
->flags
|= VXLAN_F_PROXY
;
1449 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
1450 vxlan
->flags
|= VXLAN_F_RSC
;
1452 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
1453 vxlan
->flags
|= VXLAN_F_L2MISS
;
1455 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
1456 vxlan
->flags
|= VXLAN_F_L3MISS
;
1458 if (data
[IFLA_VXLAN_LIMIT
])
1459 vxlan
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
1461 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
1462 const struct ifla_vxlan_port_range
*p
1463 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
1464 vxlan
->port_min
= ntohs(p
->low
);
1465 vxlan
->port_max
= ntohs(p
->high
);
1468 SET_ETHTOOL_OPS(dev
, &vxlan_ethtool_ops
);
1470 err
= register_netdevice(dev
);
1472 hlist_add_head_rcu(&vxlan
->hlist
, vni_head(net
, dst
->remote_vni
));
1477 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
1479 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1481 hlist_del_rcu(&vxlan
->hlist
);
1483 unregister_netdevice_queue(dev
, head
);
1486 static size_t vxlan_get_size(const struct net_device
*dev
)
1489 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
1490 nla_total_size(sizeof(__be32
)) +/* IFLA_VXLAN_GROUP */
1491 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
1492 nla_total_size(sizeof(__be32
))+ /* IFLA_VXLAN_LOCAL */
1493 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
1494 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
1495 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
1496 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
1497 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
1498 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
1499 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
1500 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
1501 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
1502 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
1506 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1508 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1509 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
1510 struct ifla_vxlan_port_range ports
= {
1511 .low
= htons(vxlan
->port_min
),
1512 .high
= htons(vxlan
->port_max
),
1515 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, dst
->remote_vni
))
1516 goto nla_put_failure
;
1518 if (dst
->remote_ip
&& nla_put_be32(skb
, IFLA_VXLAN_GROUP
, dst
->remote_ip
))
1519 goto nla_put_failure
;
1521 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
1522 goto nla_put_failure
;
1524 if (vxlan
->saddr
&& nla_put_be32(skb
, IFLA_VXLAN_LOCAL
, vxlan
->saddr
))
1525 goto nla_put_failure
;
1527 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->ttl
) ||
1528 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->tos
) ||
1529 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
1530 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
1531 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
1532 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
1533 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
1534 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
1535 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
1536 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
1537 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
1538 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->age_interval
) ||
1539 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->addrmax
))
1540 goto nla_put_failure
;
1542 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
1543 goto nla_put_failure
;
1551 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
1553 .maxtype
= IFLA_VXLAN_MAX
,
1554 .policy
= vxlan_policy
,
1555 .priv_size
= sizeof(struct vxlan_dev
),
1556 .setup
= vxlan_setup
,
1557 .validate
= vxlan_validate
,
1558 .newlink
= vxlan_newlink
,
1559 .dellink
= vxlan_dellink
,
1560 .get_size
= vxlan_get_size
,
1561 .fill_info
= vxlan_fill_info
,
1564 static __net_init
int vxlan_init_net(struct net
*net
)
1566 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
1568 struct sockaddr_in vxlan_addr
= {
1569 .sin_family
= AF_INET
,
1570 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
1575 /* Create UDP socket for encapsulation receive. */
1576 rc
= sock_create_kern(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &vn
->sock
);
1578 pr_debug("UDP socket create failed\n");
1581 /* Put in proper namespace */
1583 sk_change_net(sk
, net
);
1585 vxlan_addr
.sin_port
= htons(vxlan_port
);
1587 rc
= kernel_bind(vn
->sock
, (struct sockaddr
*) &vxlan_addr
,
1588 sizeof(vxlan_addr
));
1590 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1591 &vxlan_addr
.sin_addr
, ntohs(vxlan_addr
.sin_port
), rc
);
1592 sk_release_kernel(sk
);
1597 /* Disable multicast loopback */
1598 inet_sk(sk
)->mc_loop
= 0;
1600 /* Mark socket as an encapsulation socket. */
1601 udp_sk(sk
)->encap_type
= 1;
1602 udp_sk(sk
)->encap_rcv
= vxlan_udp_encap_recv
;
1605 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
1606 INIT_HLIST_HEAD(&vn
->vni_list
[h
]);
1611 static __net_exit
void vxlan_exit_net(struct net
*net
)
1613 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
1614 struct vxlan_dev
*vxlan
;
1618 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
1619 hlist_for_each_entry(vxlan
, &vn
->vni_list
[h
], hlist
)
1620 dev_close(vxlan
->dev
);
1624 sk_release_kernel(vn
->sock
->sk
);
1629 static struct pernet_operations vxlan_net_ops
= {
1630 .init
= vxlan_init_net
,
1631 .exit
= vxlan_exit_net
,
1632 .id
= &vxlan_net_id
,
1633 .size
= sizeof(struct vxlan_net
),
1636 static int __init
vxlan_init_module(void)
1640 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
1642 rc
= register_pernet_device(&vxlan_net_ops
);
1646 rc
= rtnl_link_register(&vxlan_link_ops
);
1653 unregister_pernet_device(&vxlan_net_ops
);
1657 module_init(vxlan_init_module
);
1659 static void __exit
vxlan_cleanup_module(void)
1661 rtnl_link_unregister(&vxlan_link_ops
);
1662 unregister_pernet_device(&vxlan_net_ops
);
1665 module_exit(vxlan_cleanup_module
);
1667 MODULE_LICENSE("GPL");
1668 MODULE_VERSION(VXLAN_VERSION
);
1669 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
1670 MODULE_ALIAS_RTNL_LINK("vxlan");