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>
23 #include <linux/udp.h>
24 #include <linux/igmp.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/hash.h>
29 #include <linux/ethtool.h>
31 #include <net/ndisc.h>
33 #include <net/ip_tunnels.h>
36 #include <net/rtnetlink.h>
37 #include <net/route.h>
38 #include <net/dsfield.h>
39 #include <net/inet_ecn.h>
40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h>
42 #include <net/vxlan.h>
43 #if IS_ENABLED(CONFIG_IPV6)
45 #include <net/addrconf.h>
46 #include <net/ip6_tunnel.h>
47 #include <net/ip6_checksum.h>
50 #define VXLAN_VERSION "0.1"
52 #define PORT_HASH_BITS 8
53 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
54 #define VNI_HASH_BITS 10
55 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
56 #define FDB_HASH_BITS 8
57 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
58 #define FDB_AGE_DEFAULT 300 /* 5 min */
59 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
61 #define VXLAN_N_VID (1u << 24)
62 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
63 /* IP header + UDP + VXLAN + Ethernet header */
64 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
65 /* IPv6 header + UDP + VXLAN + Ethernet header */
66 #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
67 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
69 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
71 /* VXLAN protocol header */
77 /* UDP port for VXLAN traffic.
78 * The IANA assigned port is 4789, but the Linux default is 8472
79 * for compatibility with early adopters.
81 static unsigned short vxlan_port __read_mostly
= 8472;
82 module_param_named(udp_port
, vxlan_port
, ushort
, 0444);
83 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
85 static bool log_ecn_error
= true;
86 module_param(log_ecn_error
, bool, 0644);
87 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
89 static int vxlan_net_id
;
91 static const u8 all_zeros_mac
[ETH_ALEN
];
93 /* per-network namespace private data for this module */
95 struct list_head vxlan_list
;
96 struct hlist_head sock_list
[PORT_HASH_SIZE
];
101 struct sockaddr_in sin
;
102 struct sockaddr_in6 sin6
;
107 union vxlan_addr remote_ip
;
111 struct list_head list
;
115 /* Forwarding table entry */
117 struct hlist_node hlist
; /* linked list of entries */
119 unsigned long updated
; /* jiffies */
121 struct list_head remotes
;
122 u16 state
; /* see ndm_state */
123 u8 flags
; /* see ndm_flags */
124 u8 eth_addr
[ETH_ALEN
];
127 /* Pseudo network device */
129 struct hlist_node hlist
; /* vni hash table */
130 struct list_head next
; /* vxlan's per namespace list */
131 struct vxlan_sock
*vn_sock
; /* listening socket */
132 struct net_device
*dev
;
133 struct vxlan_rdst default_dst
; /* default destination */
134 union vxlan_addr saddr
; /* source address */
136 __u16 port_min
; /* source port range */
138 __u8 tos
; /* TOS override */
140 u32 flags
; /* VXLAN_F_* below */
142 struct work_struct sock_work
;
143 struct work_struct igmp_join
;
144 struct work_struct igmp_leave
;
146 unsigned long age_interval
;
147 struct timer_list age_timer
;
148 spinlock_t hash_lock
;
149 unsigned int addrcnt
;
150 unsigned int addrmax
;
152 struct hlist_head fdb_head
[FDB_HASH_SIZE
];
155 #define VXLAN_F_LEARN 0x01
156 #define VXLAN_F_PROXY 0x02
157 #define VXLAN_F_RSC 0x04
158 #define VXLAN_F_L2MISS 0x08
159 #define VXLAN_F_L3MISS 0x10
160 #define VXLAN_F_IPV6 0x20 /* internal flag */
162 /* salt for hash table */
163 static u32 vxlan_salt __read_mostly
;
164 static struct workqueue_struct
*vxlan_wq
;
166 static void vxlan_sock_work(struct work_struct
*work
);
168 #if IS_ENABLED(CONFIG_IPV6)
170 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
172 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
174 if (a
->sa
.sa_family
== AF_INET6
)
175 return ipv6_addr_equal(&a
->sin6
.sin6_addr
, &b
->sin6
.sin6_addr
);
177 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
180 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
182 if (ipa
->sa
.sa_family
== AF_INET6
)
183 return ipv6_addr_any(&ipa
->sin6
.sin6_addr
);
185 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
188 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
190 if (ipa
->sa
.sa_family
== AF_INET6
)
191 return ipv6_addr_is_multicast(&ipa
->sin6
.sin6_addr
);
193 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
196 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
198 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
199 nla_memcpy(&ip
->sin6
.sin6_addr
, nla
, sizeof(struct in6_addr
));
200 ip
->sa
.sa_family
= AF_INET6
;
202 } else if (nla_len(nla
) >= sizeof(__be32
)) {
203 ip
->sin
.sin_addr
.s_addr
= nla_get_be32(nla
);
204 ip
->sa
.sa_family
= AF_INET
;
207 return -EAFNOSUPPORT
;
211 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
212 const union vxlan_addr
*ip
)
214 if (ip
->sa
.sa_family
== AF_INET6
)
215 return nla_put(skb
, attr
, sizeof(struct in6_addr
), &ip
->sin6
.sin6_addr
);
217 return nla_put_be32(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
220 #else /* !CONFIG_IPV6 */
223 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
225 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
228 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
230 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
233 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
235 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
238 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
240 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
241 return -EAFNOSUPPORT
;
242 } else if (nla_len(nla
) >= sizeof(__be32
)) {
243 ip
->sin
.sin_addr
.s_addr
= nla_get_be32(nla
);
244 ip
->sa
.sa_family
= AF_INET
;
247 return -EAFNOSUPPORT
;
251 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
252 const union vxlan_addr
*ip
)
254 return nla_put_be32(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
258 /* Virtual Network hash table head */
259 static inline struct hlist_head
*vni_head(struct vxlan_sock
*vs
, u32 id
)
261 return &vs
->vni_list
[hash_32(id
, VNI_HASH_BITS
)];
264 /* Socket hash table head */
265 static inline struct hlist_head
*vs_head(struct net
*net
, __be16 port
)
267 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
269 return &vn
->sock_list
[hash_32(ntohs(port
), PORT_HASH_BITS
)];
272 /* First remote destination for a forwarding entry.
273 * Guaranteed to be non-NULL because remotes are never deleted.
275 static inline struct vxlan_rdst
*first_remote_rcu(struct vxlan_fdb
*fdb
)
277 return list_entry_rcu(fdb
->remotes
.next
, struct vxlan_rdst
, list
);
280 static inline struct vxlan_rdst
*first_remote_rtnl(struct vxlan_fdb
*fdb
)
282 return list_first_entry(&fdb
->remotes
, struct vxlan_rdst
, list
);
285 /* Find VXLAN socket based on network namespace, address family and UDP port */
286 static struct vxlan_sock
*vxlan_find_sock(struct net
*net
,
287 sa_family_t family
, __be16 port
)
289 struct vxlan_sock
*vs
;
291 hlist_for_each_entry_rcu(vs
, vs_head(net
, port
), hlist
) {
292 if (inet_sk(vs
->sock
->sk
)->inet_sport
== port
&&
293 inet_sk(vs
->sock
->sk
)->sk
.sk_family
== family
)
299 static struct vxlan_dev
*vxlan_vs_find_vni(struct vxlan_sock
*vs
, u32 id
)
301 struct vxlan_dev
*vxlan
;
303 hlist_for_each_entry_rcu(vxlan
, vni_head(vs
, id
), hlist
) {
304 if (vxlan
->default_dst
.remote_vni
== id
)
311 /* Look up VNI in a per net namespace table */
312 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, u32 id
,
313 sa_family_t family
, __be16 port
)
315 struct vxlan_sock
*vs
;
317 vs
= vxlan_find_sock(net
, family
, port
);
321 return vxlan_vs_find_vni(vs
, id
);
324 /* Fill in neighbour message in skbuff. */
325 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
326 const struct vxlan_fdb
*fdb
,
327 u32 portid
, u32 seq
, int type
, unsigned int flags
,
328 const struct vxlan_rdst
*rdst
)
330 unsigned long now
= jiffies
;
331 struct nda_cacheinfo ci
;
332 struct nlmsghdr
*nlh
;
334 bool send_ip
, send_eth
;
336 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
340 ndm
= nlmsg_data(nlh
);
341 memset(ndm
, 0, sizeof(*ndm
));
343 send_eth
= send_ip
= true;
345 if (type
== RTM_GETNEIGH
) {
346 ndm
->ndm_family
= AF_INET
;
347 send_ip
= !vxlan_addr_any(&rdst
->remote_ip
);
348 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
350 ndm
->ndm_family
= AF_BRIDGE
;
351 ndm
->ndm_state
= fdb
->state
;
352 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
353 ndm
->ndm_flags
= fdb
->flags
;
354 ndm
->ndm_type
= NDA_DST
;
356 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
357 goto nla_put_failure
;
359 if (send_ip
&& vxlan_nla_put_addr(skb
, NDA_DST
, &rdst
->remote_ip
))
360 goto nla_put_failure
;
362 if (rdst
->remote_port
&& rdst
->remote_port
!= vxlan
->dst_port
&&
363 nla_put_be16(skb
, NDA_PORT
, rdst
->remote_port
))
364 goto nla_put_failure
;
365 if (rdst
->remote_vni
!= vxlan
->default_dst
.remote_vni
&&
366 nla_put_u32(skb
, NDA_VNI
, rdst
->remote_vni
))
367 goto nla_put_failure
;
368 if (rdst
->remote_ifindex
&&
369 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
370 goto nla_put_failure
;
372 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
373 ci
.ndm_confirmed
= 0;
374 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
377 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
378 goto nla_put_failure
;
380 return nlmsg_end(skb
, nlh
);
383 nlmsg_cancel(skb
, nlh
);
387 static inline size_t vxlan_nlmsg_size(void)
389 return NLMSG_ALIGN(sizeof(struct ndmsg
))
390 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
391 + nla_total_size(sizeof(struct in6_addr
)) /* NDA_DST */
392 + nla_total_size(sizeof(__be16
)) /* NDA_PORT */
393 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
394 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
395 + nla_total_size(sizeof(struct nda_cacheinfo
));
398 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
,
399 struct vxlan_fdb
*fdb
, int type
)
401 struct net
*net
= dev_net(vxlan
->dev
);
405 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
409 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0,
410 first_remote_rtnl(fdb
));
412 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
413 WARN_ON(err
== -EMSGSIZE
);
418 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
422 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
425 static void vxlan_ip_miss(struct net_device
*dev
, union vxlan_addr
*ipa
)
427 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
428 struct vxlan_fdb f
= {
431 struct vxlan_rdst remote
= {
432 .remote_ip
= *ipa
, /* goes to NDA_DST */
433 .remote_vni
= VXLAN_N_VID
,
436 INIT_LIST_HEAD(&f
.remotes
);
437 list_add_rcu(&remote
.list
, &f
.remotes
);
439 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
442 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
444 struct vxlan_fdb f
= {
448 INIT_LIST_HEAD(&f
.remotes
);
449 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
451 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
454 /* Hash Ethernet address */
455 static u32
eth_hash(const unsigned char *addr
)
457 u64 value
= get_unaligned((u64
*)addr
);
459 /* only want 6 bytes */
465 return hash_64(value
, FDB_HASH_BITS
);
468 /* Hash chain to use given mac address */
469 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
472 return &vxlan
->fdb_head
[eth_hash(mac
)];
475 /* Look up Ethernet address in forwarding table */
476 static struct vxlan_fdb
*__vxlan_find_mac(struct vxlan_dev
*vxlan
,
480 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
);
483 hlist_for_each_entry_rcu(f
, head
, hlist
) {
484 if (ether_addr_equal(mac
, f
->eth_addr
))
491 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
496 f
= __vxlan_find_mac(vxlan
, mac
);
503 /* caller should hold vxlan->hash_lock */
504 static struct vxlan_rdst
*vxlan_fdb_find_rdst(struct vxlan_fdb
*f
,
505 union vxlan_addr
*ip
, __be16 port
,
506 __u32 vni
, __u32 ifindex
)
508 struct vxlan_rdst
*rd
;
510 list_for_each_entry(rd
, &f
->remotes
, list
) {
511 if (vxlan_addr_equal(&rd
->remote_ip
, ip
) &&
512 rd
->remote_port
== port
&&
513 rd
->remote_vni
== vni
&&
514 rd
->remote_ifindex
== ifindex
)
521 /* Replace destination of unicast mac */
522 static int vxlan_fdb_replace(struct vxlan_fdb
*f
,
523 union vxlan_addr
*ip
, __be16 port
, __u32 vni
, __u32 ifindex
)
525 struct vxlan_rdst
*rd
;
527 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
531 rd
= list_first_entry_or_null(&f
->remotes
, struct vxlan_rdst
, list
);
535 rd
->remote_port
= port
;
536 rd
->remote_vni
= vni
;
537 rd
->remote_ifindex
= ifindex
;
541 /* Add/update destinations for multicast */
542 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
543 union vxlan_addr
*ip
, __be16 port
, __u32 vni
, __u32 ifindex
)
545 struct vxlan_rdst
*rd
;
547 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
551 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
555 rd
->remote_port
= port
;
556 rd
->remote_vni
= vni
;
557 rd
->remote_ifindex
= ifindex
;
559 list_add_tail_rcu(&rd
->list
, &f
->remotes
);
564 /* Notify netdevs that UDP port started listening */
565 static void vxlan_notify_add_rx_port(struct sock
*sk
)
567 struct net_device
*dev
;
568 struct net
*net
= sock_net(sk
);
569 sa_family_t sa_family
= sk
->sk_family
;
570 __be16 port
= inet_sk(sk
)->inet_sport
;
573 for_each_netdev_rcu(net
, dev
) {
574 if (dev
->netdev_ops
->ndo_add_vxlan_port
)
575 dev
->netdev_ops
->ndo_add_vxlan_port(dev
, sa_family
,
581 /* Notify netdevs that UDP port is no more listening */
582 static void vxlan_notify_del_rx_port(struct sock
*sk
)
584 struct net_device
*dev
;
585 struct net
*net
= sock_net(sk
);
586 sa_family_t sa_family
= sk
->sk_family
;
587 __be16 port
= inet_sk(sk
)->inet_sport
;
590 for_each_netdev_rcu(net
, dev
) {
591 if (dev
->netdev_ops
->ndo_del_vxlan_port
)
592 dev
->netdev_ops
->ndo_del_vxlan_port(dev
, sa_family
,
598 /* Add new entry to forwarding table -- assumes lock held */
599 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
600 const u8
*mac
, union vxlan_addr
*ip
,
601 __u16 state
, __u16 flags
,
602 __be16 port
, __u32 vni
, __u32 ifindex
,
608 f
= __vxlan_find_mac(vxlan
, mac
);
610 if (flags
& NLM_F_EXCL
) {
611 netdev_dbg(vxlan
->dev
,
612 "lost race to create %pM\n", mac
);
615 if (f
->state
!= state
) {
617 f
->updated
= jiffies
;
620 if (f
->flags
!= ndm_flags
) {
621 f
->flags
= ndm_flags
;
622 f
->updated
= jiffies
;
625 if ((flags
& NLM_F_REPLACE
)) {
626 /* Only change unicasts */
627 if (!(is_multicast_ether_addr(f
->eth_addr
) ||
628 is_zero_ether_addr(f
->eth_addr
))) {
629 int rc
= vxlan_fdb_replace(f
, ip
, port
, vni
,
638 if ((flags
& NLM_F_APPEND
) &&
639 (is_multicast_ether_addr(f
->eth_addr
) ||
640 is_zero_ether_addr(f
->eth_addr
))) {
641 int rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
);
648 if (!(flags
& NLM_F_CREATE
))
651 if (vxlan
->addrmax
&& vxlan
->addrcnt
>= vxlan
->addrmax
)
654 /* Disallow replace to add a multicast entry */
655 if ((flags
& NLM_F_REPLACE
) &&
656 (is_multicast_ether_addr(mac
) || is_zero_ether_addr(mac
)))
659 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
660 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
666 f
->flags
= ndm_flags
;
667 f
->updated
= f
->used
= jiffies
;
668 INIT_LIST_HEAD(&f
->remotes
);
669 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
671 vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
);
674 hlist_add_head_rcu(&f
->hlist
,
675 vxlan_fdb_head(vxlan
, mac
));
679 vxlan_fdb_notify(vxlan
, f
, RTM_NEWNEIGH
);
684 static void vxlan_fdb_free(struct rcu_head
*head
)
686 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
687 struct vxlan_rdst
*rd
, *nd
;
689 list_for_each_entry_safe(rd
, nd
, &f
->remotes
, list
)
694 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
696 netdev_dbg(vxlan
->dev
,
697 "delete %pM\n", f
->eth_addr
);
700 vxlan_fdb_notify(vxlan
, f
, RTM_DELNEIGH
);
702 hlist_del_rcu(&f
->hlist
);
703 call_rcu(&f
->rcu
, vxlan_fdb_free
);
706 static int vxlan_fdb_parse(struct nlattr
*tb
[], struct vxlan_dev
*vxlan
,
707 union vxlan_addr
*ip
, __be16
*port
, u32
*vni
, u32
*ifindex
)
709 struct net
*net
= dev_net(vxlan
->dev
);
713 err
= vxlan_nla_get_addr(ip
, tb
[NDA_DST
]);
717 union vxlan_addr
*remote
= &vxlan
->default_dst
.remote_ip
;
718 if (remote
->sa
.sa_family
== AF_INET
) {
719 ip
->sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
720 ip
->sa
.sa_family
= AF_INET
;
721 #if IS_ENABLED(CONFIG_IPV6)
723 ip
->sin6
.sin6_addr
= in6addr_any
;
724 ip
->sa
.sa_family
= AF_INET6
;
730 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
732 *port
= nla_get_be16(tb
[NDA_PORT
]);
734 *port
= vxlan
->dst_port
;
738 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
740 *vni
= nla_get_u32(tb
[NDA_VNI
]);
742 *vni
= vxlan
->default_dst
.remote_vni
;
745 if (tb
[NDA_IFINDEX
]) {
746 struct net_device
*tdev
;
748 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
750 *ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
751 tdev
= dev_get_by_index(net
, *ifindex
);
753 return -EADDRNOTAVAIL
;
762 /* Add static entry (via netlink) */
763 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
764 struct net_device
*dev
,
765 const unsigned char *addr
, u16 flags
)
767 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
768 /* struct net *net = dev_net(vxlan->dev); */
774 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
775 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
780 if (tb
[NDA_DST
] == NULL
)
783 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
787 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
!= ip
.sa
.sa_family
)
788 return -EAFNOSUPPORT
;
790 spin_lock_bh(&vxlan
->hash_lock
);
791 err
= vxlan_fdb_create(vxlan
, addr
, &ip
, ndm
->ndm_state
, flags
,
792 port
, vni
, ifindex
, ndm
->ndm_flags
);
793 spin_unlock_bh(&vxlan
->hash_lock
);
798 /* Delete entry (via netlink) */
799 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
800 struct net_device
*dev
,
801 const unsigned char *addr
)
803 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
805 struct vxlan_rdst
*rd
= NULL
;
811 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
817 spin_lock_bh(&vxlan
->hash_lock
);
818 f
= vxlan_find_mac(vxlan
, addr
);
822 if (!vxlan_addr_any(&ip
)) {
823 rd
= vxlan_fdb_find_rdst(f
, &ip
, port
, vni
, ifindex
);
830 /* remove a destination if it's not the only one on the list,
831 * otherwise destroy the fdb entry
833 if (rd
&& !list_is_singular(&f
->remotes
)) {
834 list_del_rcu(&rd
->list
);
839 vxlan_fdb_destroy(vxlan
, f
);
842 spin_unlock_bh(&vxlan
->hash_lock
);
847 /* Dump forwarding table */
848 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
849 struct net_device
*dev
, int idx
)
851 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
854 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
858 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
859 struct vxlan_rdst
*rd
;
861 if (idx
< cb
->args
[0])
864 list_for_each_entry_rcu(rd
, &f
->remotes
, list
) {
865 err
= vxlan_fdb_info(skb
, vxlan
, f
,
866 NETLINK_CB(cb
->skb
).portid
,
881 /* Watch incoming packets to learn mapping between Ethernet address
882 * and Tunnel endpoint.
883 * Return true if packet is bogus and should be droppped.
885 static bool vxlan_snoop(struct net_device
*dev
,
886 union vxlan_addr
*src_ip
, const u8
*src_mac
)
888 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
891 f
= vxlan_find_mac(vxlan
, src_mac
);
893 struct vxlan_rdst
*rdst
= first_remote_rcu(f
);
895 if (likely(vxlan_addr_equal(&rdst
->remote_ip
, src_ip
)))
898 /* Don't migrate static entries, drop packets */
899 if (f
->state
& NUD_NOARP
)
904 "%pM migrated from %pIS to %pIS\n",
905 src_mac
, &rdst
->remote_ip
, &src_ip
);
907 rdst
->remote_ip
= *src_ip
;
908 f
->updated
= jiffies
;
909 vxlan_fdb_notify(vxlan
, f
, RTM_NEWNEIGH
);
911 /* learned new entry */
912 spin_lock(&vxlan
->hash_lock
);
914 /* close off race between vxlan_flush and incoming packets */
915 if (netif_running(dev
))
916 vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
918 NLM_F_EXCL
|NLM_F_CREATE
,
920 vxlan
->default_dst
.remote_vni
,
922 spin_unlock(&vxlan
->hash_lock
);
928 /* See if multicast group is already in use by other ID */
929 static bool vxlan_group_used(struct vxlan_net
*vn
, union vxlan_addr
*remote_ip
)
931 struct vxlan_dev
*vxlan
;
933 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
) {
934 if (!netif_running(vxlan
->dev
))
937 if (vxlan_addr_equal(&vxlan
->default_dst
.remote_ip
,
945 static void vxlan_sock_hold(struct vxlan_sock
*vs
)
947 atomic_inc(&vs
->refcnt
);
950 void vxlan_sock_release(struct vxlan_sock
*vs
)
952 struct sock
*sk
= vs
->sock
->sk
;
953 struct net
*net
= sock_net(sk
);
954 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
956 if (!atomic_dec_and_test(&vs
->refcnt
))
959 spin_lock(&vn
->sock_lock
);
960 hlist_del_rcu(&vs
->hlist
);
961 rcu_assign_sk_user_data(vs
->sock
->sk
, NULL
);
962 vxlan_notify_del_rx_port(sk
);
963 spin_unlock(&vn
->sock_lock
);
965 queue_work(vxlan_wq
, &vs
->del_work
);
967 EXPORT_SYMBOL_GPL(vxlan_sock_release
);
969 /* Callback to update multicast group membership when first VNI on
970 * multicast asddress is brought up
971 * Done as workqueue because ip_mc_join_group acquires RTNL.
973 static void vxlan_igmp_join(struct work_struct
*work
)
975 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, igmp_join
);
976 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
977 struct sock
*sk
= vs
->sock
->sk
;
978 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
979 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
982 if (ip
->sa
.sa_family
== AF_INET
) {
983 struct ip_mreqn mreq
= {
984 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
985 .imr_ifindex
= ifindex
,
988 ip_mc_join_group(sk
, &mreq
);
989 #if IS_ENABLED(CONFIG_IPV6)
991 ipv6_stub
->ipv6_sock_mc_join(sk
, ifindex
,
992 &ip
->sin6
.sin6_addr
);
997 vxlan_sock_release(vs
);
1001 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1002 static void vxlan_igmp_leave(struct work_struct
*work
)
1004 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, igmp_leave
);
1005 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
1006 struct sock
*sk
= vs
->sock
->sk
;
1007 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1008 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1011 if (ip
->sa
.sa_family
== AF_INET
) {
1012 struct ip_mreqn mreq
= {
1013 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1014 .imr_ifindex
= ifindex
,
1017 ip_mc_leave_group(sk
, &mreq
);
1018 #if IS_ENABLED(CONFIG_IPV6)
1020 ipv6_stub
->ipv6_sock_mc_drop(sk
, ifindex
,
1021 &ip
->sin6
.sin6_addr
);
1027 vxlan_sock_release(vs
);
1028 dev_put(vxlan
->dev
);
1031 /* Callback from net/ipv4/udp.c to receive packets */
1032 static int vxlan_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
1034 struct vxlan_sock
*vs
;
1035 struct vxlanhdr
*vxh
;
1038 /* Need Vxlan and inner Ethernet header to be present */
1039 if (!pskb_may_pull(skb
, VXLAN_HLEN
))
1042 /* Return packets with reserved bits set */
1043 vxh
= (struct vxlanhdr
*)(udp_hdr(skb
) + 1);
1044 if (vxh
->vx_flags
!= htonl(VXLAN_FLAGS
) ||
1045 (vxh
->vx_vni
& htonl(0xff))) {
1046 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
1047 ntohl(vxh
->vx_flags
), ntohl(vxh
->vx_vni
));
1051 if (iptunnel_pull_header(skb
, VXLAN_HLEN
, htons(ETH_P_TEB
)))
1054 port
= inet_sk(sk
)->inet_sport
;
1056 vs
= rcu_dereference_sk_user_data(sk
);
1060 /* If the NIC driver gave us an encapsulated packet
1061 * with the encapsulation mark, the device checksummed it
1062 * for us. Otherwise force the upper layers to verify it.
1064 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
|| !skb
->encapsulation
)
1065 skb
->ip_summed
= CHECKSUM_NONE
;
1067 skb
->encapsulation
= 0;
1069 vs
->rcv(vs
, skb
, vxh
->vx_vni
);
1073 /* Consume bad packet */
1078 /* Return non vxlan pkt */
1082 static void vxlan_rcv(struct vxlan_sock
*vs
,
1083 struct sk_buff
*skb
, __be32 vx_vni
)
1085 struct iphdr
*oip
= NULL
;
1086 struct ipv6hdr
*oip6
= NULL
;
1087 struct vxlan_dev
*vxlan
;
1088 struct pcpu_tstats
*stats
;
1089 union vxlan_addr saddr
;
1092 union vxlan_addr
*remote_ip
;
1094 vni
= ntohl(vx_vni
) >> 8;
1095 /* Is this VNI defined? */
1096 vxlan
= vxlan_vs_find_vni(vs
, vni
);
1100 remote_ip
= &vxlan
->default_dst
.remote_ip
;
1101 skb_reset_mac_header(skb
);
1102 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1104 /* Ignore packet loops (and multicast echo) */
1105 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1108 /* Re-examine inner Ethernet packet */
1109 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1111 saddr
.sin
.sin_addr
.s_addr
= oip
->saddr
;
1112 saddr
.sa
.sa_family
= AF_INET
;
1113 #if IS_ENABLED(CONFIG_IPV6)
1115 oip6
= ipv6_hdr(skb
);
1116 saddr
.sin6
.sin6_addr
= oip6
->saddr
;
1117 saddr
.sa
.sa_family
= AF_INET6
;
1121 if ((vxlan
->flags
& VXLAN_F_LEARN
) &&
1122 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
))
1125 skb_reset_network_header(skb
);
1128 err
= IP6_ECN_decapsulate(oip6
, skb
);
1130 err
= IP_ECN_decapsulate(oip
, skb
);
1132 if (unlikely(err
)) {
1133 if (log_ecn_error
) {
1135 net_info_ratelimited("non-ECT from %pI6\n",
1138 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1139 &oip
->saddr
, oip
->tos
);
1142 ++vxlan
->dev
->stats
.rx_frame_errors
;
1143 ++vxlan
->dev
->stats
.rx_errors
;
1148 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1149 u64_stats_update_begin(&stats
->syncp
);
1150 stats
->rx_packets
++;
1151 stats
->rx_bytes
+= skb
->len
;
1152 u64_stats_update_end(&stats
->syncp
);
1158 /* Consume bad packet */
1162 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1164 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1165 struct arphdr
*parp
;
1168 struct neighbour
*n
;
1170 if (dev
->flags
& IFF_NOARP
)
1173 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1174 dev
->stats
.tx_dropped
++;
1177 parp
= arp_hdr(skb
);
1179 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1180 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1181 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1182 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1183 parp
->ar_hln
!= dev
->addr_len
||
1186 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1188 arpptr
+= dev
->addr_len
; /* sha */
1189 memcpy(&sip
, arpptr
, sizeof(sip
));
1190 arpptr
+= sizeof(sip
);
1191 arpptr
+= dev
->addr_len
; /* tha */
1192 memcpy(&tip
, arpptr
, sizeof(tip
));
1194 if (ipv4_is_loopback(tip
) ||
1195 ipv4_is_multicast(tip
))
1198 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1201 struct vxlan_fdb
*f
;
1202 struct sk_buff
*reply
;
1204 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1209 f
= vxlan_find_mac(vxlan
, n
->ha
);
1210 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1211 /* bridge-local neighbor */
1216 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1224 skb_reset_mac_header(reply
);
1225 __skb_pull(reply
, skb_network_offset(reply
));
1226 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1227 reply
->pkt_type
= PACKET_HOST
;
1229 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1230 dev
->stats
.rx_dropped
++;
1231 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1232 union vxlan_addr ipa
= {
1233 .sin
.sin_addr
.s_addr
= tip
,
1234 .sin
.sin_family
= AF_INET
,
1237 vxlan_ip_miss(dev
, &ipa
);
1241 return NETDEV_TX_OK
;
1244 #if IS_ENABLED(CONFIG_IPV6)
1246 static struct sk_buff
*vxlan_na_create(struct sk_buff
*request
,
1247 struct neighbour
*n
, bool isrouter
)
1249 struct net_device
*dev
= request
->dev
;
1250 struct sk_buff
*reply
;
1251 struct nd_msg
*ns
, *na
;
1252 struct ipv6hdr
*pip6
;
1254 int na_olen
= 8; /* opt hdr + ETH_ALEN for target */
1261 len
= LL_RESERVED_SPACE(dev
) + sizeof(struct ipv6hdr
) +
1262 sizeof(*na
) + na_olen
+ dev
->needed_tailroom
;
1263 reply
= alloc_skb(len
, GFP_ATOMIC
);
1267 reply
->protocol
= htons(ETH_P_IPV6
);
1269 skb_reserve(reply
, LL_RESERVED_SPACE(request
->dev
));
1270 skb_push(reply
, sizeof(struct ethhdr
));
1271 skb_set_mac_header(reply
, 0);
1273 ns
= (struct nd_msg
*)skb_transport_header(request
);
1275 daddr
= eth_hdr(request
)->h_source
;
1276 ns_olen
= request
->len
- skb_transport_offset(request
) - sizeof(*ns
);
1277 for (i
= 0; i
< ns_olen
-1; i
+= (ns
->opt
[i
+1]<<3)) {
1278 if (ns
->opt
[i
] == ND_OPT_SOURCE_LL_ADDR
) {
1279 daddr
= ns
->opt
+ i
+ sizeof(struct nd_opt_hdr
);
1284 /* Ethernet header */
1285 memcpy(eth_hdr(reply
)->h_dest
, daddr
, ETH_ALEN
);
1286 memcpy(eth_hdr(reply
)->h_source
, n
->ha
, ETH_ALEN
);
1287 eth_hdr(reply
)->h_proto
= htons(ETH_P_IPV6
);
1288 reply
->protocol
= htons(ETH_P_IPV6
);
1290 skb_pull(reply
, sizeof(struct ethhdr
));
1291 skb_set_network_header(reply
, 0);
1292 skb_put(reply
, sizeof(struct ipv6hdr
));
1296 pip6
= ipv6_hdr(reply
);
1297 memset(pip6
, 0, sizeof(struct ipv6hdr
));
1299 pip6
->priority
= ipv6_hdr(request
)->priority
;
1300 pip6
->nexthdr
= IPPROTO_ICMPV6
;
1301 pip6
->hop_limit
= 255;
1302 pip6
->daddr
= ipv6_hdr(request
)->saddr
;
1303 pip6
->saddr
= *(struct in6_addr
*)n
->primary_key
;
1305 skb_pull(reply
, sizeof(struct ipv6hdr
));
1306 skb_set_transport_header(reply
, 0);
1308 na
= (struct nd_msg
*)skb_put(reply
, sizeof(*na
) + na_olen
);
1310 /* Neighbor Advertisement */
1311 memset(na
, 0, sizeof(*na
)+na_olen
);
1312 na
->icmph
.icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
1313 na
->icmph
.icmp6_router
= isrouter
;
1314 na
->icmph
.icmp6_override
= 1;
1315 na
->icmph
.icmp6_solicited
= 1;
1316 na
->target
= ns
->target
;
1317 memcpy(&na
->opt
[2], n
->ha
, ETH_ALEN
);
1318 na
->opt
[0] = ND_OPT_TARGET_LL_ADDR
;
1319 na
->opt
[1] = na_olen
>> 3;
1321 na
->icmph
.icmp6_cksum
= csum_ipv6_magic(&pip6
->saddr
,
1322 &pip6
->daddr
, sizeof(*na
)+na_olen
, IPPROTO_ICMPV6
,
1323 csum_partial(na
, sizeof(*na
)+na_olen
, 0));
1325 pip6
->payload_len
= htons(sizeof(*na
)+na_olen
);
1327 skb_push(reply
, sizeof(struct ipv6hdr
));
1329 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1334 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1336 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1338 const struct ipv6hdr
*iphdr
;
1339 const struct in6_addr
*saddr
, *daddr
;
1340 struct neighbour
*n
;
1341 struct inet6_dev
*in6_dev
;
1343 in6_dev
= __in6_dev_get(dev
);
1347 iphdr
= ipv6_hdr(skb
);
1348 saddr
= &iphdr
->saddr
;
1349 daddr
= &iphdr
->daddr
;
1351 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1352 if (msg
->icmph
.icmp6_code
!= 0 ||
1353 msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
1356 if (ipv6_addr_loopback(daddr
) ||
1357 ipv6_addr_is_multicast(&msg
->target
))
1360 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1363 struct vxlan_fdb
*f
;
1364 struct sk_buff
*reply
;
1366 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1371 f
= vxlan_find_mac(vxlan
, n
->ha
);
1372 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1373 /* bridge-local neighbor */
1378 reply
= vxlan_na_create(skb
, n
,
1379 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1386 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1387 dev
->stats
.rx_dropped
++;
1389 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1390 union vxlan_addr ipa
= {
1391 .sin6
.sin6_addr
= msg
->target
,
1392 .sin6
.sin6_family
= AF_INET6
,
1395 vxlan_ip_miss(dev
, &ipa
);
1400 return NETDEV_TX_OK
;
1404 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1406 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1407 struct neighbour
*n
;
1409 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1413 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1418 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1421 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1422 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1423 union vxlan_addr ipa
= {
1424 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1425 .sin
.sin_family
= AF_INET
,
1428 vxlan_ip_miss(dev
, &ipa
);
1434 #if IS_ENABLED(CONFIG_IPV6)
1437 struct ipv6hdr
*pip6
;
1439 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1441 pip6
= ipv6_hdr(skb
);
1442 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1443 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1444 union vxlan_addr ipa
= {
1445 .sin6
.sin6_addr
= pip6
->daddr
,
1446 .sin6
.sin6_family
= AF_INET6
,
1449 vxlan_ip_miss(dev
, &ipa
);
1463 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1465 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1467 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1476 static void vxlan_sock_put(struct sk_buff
*skb
)
1481 /* On transmit, associate with the tunnel socket */
1482 static void vxlan_set_owner(struct sock
*sk
, struct sk_buff
*skb
)
1487 skb
->destructor
= vxlan_sock_put
;
1490 /* Compute source port for outgoing packet
1491 * first choice to use L4 flow hash since it will spread
1492 * better and maybe available from hardware
1493 * secondary choice is to use jhash on the Ethernet header
1495 __be16
vxlan_src_port(__u16 port_min
, __u16 port_max
, struct sk_buff
*skb
)
1497 unsigned int range
= (port_max
- port_min
) + 1;
1500 hash
= skb_get_rxhash(skb
);
1502 hash
= jhash(skb
->data
, 2 * ETH_ALEN
,
1503 (__force u32
) skb
->protocol
);
1505 return htons((((u64
) hash
* range
) >> 32) + port_min
);
1507 EXPORT_SYMBOL_GPL(vxlan_src_port
);
1509 static int handle_offloads(struct sk_buff
*skb
)
1511 if (skb_is_gso(skb
)) {
1512 int err
= skb_unclone(skb
, GFP_ATOMIC
);
1516 skb_shinfo(skb
)->gso_type
|= SKB_GSO_UDP_TUNNEL
;
1517 } else if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
1518 skb
->ip_summed
= CHECKSUM_NONE
;
1523 #if IS_ENABLED(CONFIG_IPV6)
1524 static int vxlan6_xmit_skb(struct vxlan_sock
*vs
,
1525 struct dst_entry
*dst
, struct sk_buff
*skb
,
1526 struct net_device
*dev
, struct in6_addr
*saddr
,
1527 struct in6_addr
*daddr
, __u8 prio
, __u8 ttl
,
1528 __be16 src_port
, __be16 dst_port
, __be32 vni
)
1530 struct ipv6hdr
*ip6h
;
1531 struct vxlanhdr
*vxh
;
1536 if (!skb
->encapsulation
) {
1537 skb_reset_inner_headers(skb
);
1538 skb
->encapsulation
= 1;
1541 skb_scrub_packet(skb
, false);
1543 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1544 + VXLAN_HLEN
+ sizeof(struct ipv6hdr
)
1545 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
1547 /* Need space for new headers (invalidates iph ptr) */
1548 err
= skb_cow_head(skb
, min_headroom
);
1552 if (vlan_tx_tag_present(skb
)) {
1553 if (WARN_ON(!__vlan_put_tag(skb
,
1555 vlan_tx_tag_get(skb
))))
1561 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1562 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1565 __skb_push(skb
, sizeof(*uh
));
1566 skb_reset_transport_header(skb
);
1569 uh
->dest
= dst_port
;
1570 uh
->source
= src_port
;
1572 uh
->len
= htons(skb
->len
);
1575 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1576 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1578 skb_dst_set(skb
, dst
);
1580 if (!skb_is_gso(skb
) && !(dst
->dev
->features
& NETIF_F_IPV6_CSUM
)) {
1581 __wsum csum
= skb_checksum(skb
, 0, skb
->len
, 0);
1582 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1583 uh
->check
= csum_ipv6_magic(saddr
, daddr
, skb
->len
,
1586 uh
->check
= CSUM_MANGLED_0
;
1588 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1589 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
1590 skb
->csum_offset
= offsetof(struct udphdr
, check
);
1591 uh
->check
= ~csum_ipv6_magic(saddr
, daddr
,
1592 skb
->len
, IPPROTO_UDP
, 0);
1595 __skb_push(skb
, sizeof(*ip6h
));
1596 skb_reset_network_header(skb
);
1597 ip6h
= ipv6_hdr(skb
);
1599 ip6h
->priority
= prio
;
1600 ip6h
->flow_lbl
[0] = 0;
1601 ip6h
->flow_lbl
[1] = 0;
1602 ip6h
->flow_lbl
[2] = 0;
1603 ip6h
->payload_len
= htons(skb
->len
);
1604 ip6h
->nexthdr
= IPPROTO_UDP
;
1605 ip6h
->hop_limit
= ttl
;
1606 ip6h
->daddr
= *daddr
;
1607 ip6h
->saddr
= *saddr
;
1609 vxlan_set_owner(vs
->sock
->sk
, skb
);
1611 err
= handle_offloads(skb
);
1615 ip6tunnel_xmit(skb
, dev
);
1620 int vxlan_xmit_skb(struct vxlan_sock
*vs
,
1621 struct rtable
*rt
, struct sk_buff
*skb
,
1622 __be32 src
, __be32 dst
, __u8 tos
, __u8 ttl
, __be16 df
,
1623 __be16 src_port
, __be16 dst_port
, __be32 vni
)
1625 struct vxlanhdr
*vxh
;
1630 if (!skb
->encapsulation
) {
1631 skb_reset_inner_headers(skb
);
1632 skb
->encapsulation
= 1;
1635 min_headroom
= LL_RESERVED_SPACE(rt
->dst
.dev
) + rt
->dst
.header_len
1636 + VXLAN_HLEN
+ sizeof(struct iphdr
)
1637 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
1639 /* Need space for new headers (invalidates iph ptr) */
1640 err
= skb_cow_head(skb
, min_headroom
);
1644 if (vlan_tx_tag_present(skb
)) {
1645 if (WARN_ON(!__vlan_put_tag(skb
,
1647 vlan_tx_tag_get(skb
))))
1653 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1654 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1657 __skb_push(skb
, sizeof(*uh
));
1658 skb_reset_transport_header(skb
);
1661 uh
->dest
= dst_port
;
1662 uh
->source
= src_port
;
1664 uh
->len
= htons(skb
->len
);
1667 vxlan_set_owner(vs
->sock
->sk
, skb
);
1669 err
= handle_offloads(skb
);
1673 return iptunnel_xmit(rt
, skb
, src
, dst
, IPPROTO_UDP
, tos
, ttl
, df
,
1676 EXPORT_SYMBOL_GPL(vxlan_xmit_skb
);
1678 /* Bypass encapsulation if the destination is local */
1679 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
1680 struct vxlan_dev
*dst_vxlan
)
1682 struct pcpu_tstats
*tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
1683 struct pcpu_tstats
*rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
1684 union vxlan_addr loopback
;
1685 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
1686 struct net_device
*dev
= skb
->dev
;
1689 skb
->pkt_type
= PACKET_HOST
;
1690 skb
->encapsulation
= 0;
1691 skb
->dev
= dst_vxlan
->dev
;
1692 __skb_pull(skb
, skb_network_offset(skb
));
1694 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1695 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1696 loopback
.sa
.sa_family
= AF_INET
;
1697 #if IS_ENABLED(CONFIG_IPV6)
1699 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
1700 loopback
.sa
.sa_family
= AF_INET6
;
1704 if (dst_vxlan
->flags
& VXLAN_F_LEARN
)
1705 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
);
1707 u64_stats_update_begin(&tx_stats
->syncp
);
1708 tx_stats
->tx_packets
++;
1709 tx_stats
->tx_bytes
+= len
;
1710 u64_stats_update_end(&tx_stats
->syncp
);
1712 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
1713 u64_stats_update_begin(&rx_stats
->syncp
);
1714 rx_stats
->rx_packets
++;
1715 rx_stats
->rx_bytes
+= len
;
1716 u64_stats_update_end(&rx_stats
->syncp
);
1718 dev
->stats
.rx_dropped
++;
1722 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
1723 struct vxlan_rdst
*rdst
, bool did_rsc
)
1725 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1726 struct rtable
*rt
= NULL
;
1727 const struct iphdr
*old_iph
;
1729 union vxlan_addr
*dst
;
1730 __be16 src_port
= 0, dst_port
;
1736 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->dst_port
;
1737 vni
= rdst
->remote_vni
;
1738 dst
= &rdst
->remote_ip
;
1740 if (vxlan_addr_any(dst
)) {
1742 /* short-circuited back to local bridge */
1743 vxlan_encap_bypass(skb
, vxlan
, vxlan
);
1749 old_iph
= ip_hdr(skb
);
1752 if (!ttl
&& vxlan_addr_multicast(dst
))
1757 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
1759 src_port
= vxlan_src_port(vxlan
->port_min
, vxlan
->port_max
, skb
);
1761 if (dst
->sa
.sa_family
== AF_INET
) {
1762 memset(&fl4
, 0, sizeof(fl4
));
1763 fl4
.flowi4_oif
= rdst
->remote_ifindex
;
1764 fl4
.flowi4_tos
= RT_TOS(tos
);
1765 fl4
.daddr
= dst
->sin
.sin_addr
.s_addr
;
1766 fl4
.saddr
= vxlan
->saddr
.sin
.sin_addr
.s_addr
;
1768 rt
= ip_route_output_key(dev_net(dev
), &fl4
);
1770 netdev_dbg(dev
, "no route to %pI4\n",
1771 &dst
->sin
.sin_addr
.s_addr
);
1772 dev
->stats
.tx_carrier_errors
++;
1776 if (rt
->dst
.dev
== dev
) {
1777 netdev_dbg(dev
, "circular route to %pI4\n",
1778 &dst
->sin
.sin_addr
.s_addr
);
1779 dev
->stats
.collisions
++;
1783 /* Bypass encapsulation if the destination is local */
1784 if (rt
->rt_flags
& RTCF_LOCAL
&&
1785 !(rt
->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
1786 struct vxlan_dev
*dst_vxlan
;
1789 dst_vxlan
= vxlan_find_vni(dev_net(dev
), vni
,
1790 dst
->sa
.sa_family
, dst_port
);
1793 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
1797 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
1798 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
1800 err
= vxlan_xmit_skb(vxlan
->vn_sock
, rt
, skb
,
1801 fl4
.saddr
, dst
->sin
.sin_addr
.s_addr
,
1802 tos
, ttl
, df
, src_port
, dst_port
,
1807 iptunnel_xmit_stats(err
, &dev
->stats
, dev
->tstats
);
1808 #if IS_ENABLED(CONFIG_IPV6)
1810 struct sock
*sk
= vxlan
->vn_sock
->sock
->sk
;
1811 struct dst_entry
*ndst
;
1815 memset(&fl6
, 0, sizeof(fl6
));
1816 fl6
.flowi6_oif
= rdst
->remote_ifindex
;
1817 fl6
.daddr
= dst
->sin6
.sin6_addr
;
1818 fl6
.saddr
= vxlan
->saddr
.sin6
.sin6_addr
;
1819 fl6
.flowi6_proto
= IPPROTO_UDP
;
1821 if (ipv6_stub
->ipv6_dst_lookup(sk
, &ndst
, &fl6
)) {
1822 netdev_dbg(dev
, "no route to %pI6\n",
1823 &dst
->sin6
.sin6_addr
);
1824 dev
->stats
.tx_carrier_errors
++;
1828 if (ndst
->dev
== dev
) {
1829 netdev_dbg(dev
, "circular route to %pI6\n",
1830 &dst
->sin6
.sin6_addr
);
1832 dev
->stats
.collisions
++;
1836 /* Bypass encapsulation if the destination is local */
1837 flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
1838 if (flags
& RTF_LOCAL
&&
1839 !(flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
1840 struct vxlan_dev
*dst_vxlan
;
1843 dst_vxlan
= vxlan_find_vni(dev_net(dev
), vni
,
1844 dst
->sa
.sa_family
, dst_port
);
1847 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
1851 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
1853 err
= vxlan6_xmit_skb(vxlan
->vn_sock
, ndst
, skb
,
1854 dev
, &fl6
.saddr
, &fl6
.daddr
, 0, ttl
,
1855 src_port
, dst_port
, htonl(vni
<< 8));
1862 dev
->stats
.tx_dropped
++;
1868 dev
->stats
.tx_errors
++;
1873 /* Transmit local packets over Vxlan
1875 * Outer IP header inherits ECN and DF from inner header.
1876 * Outer UDP destination is the VXLAN assigned port.
1877 * source port is based on hash of flow
1879 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1881 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1883 bool did_rsc
= false;
1884 struct vxlan_rdst
*rdst
;
1885 struct vxlan_fdb
*f
;
1887 skb_reset_mac_header(skb
);
1890 if ((vxlan
->flags
& VXLAN_F_PROXY
)) {
1891 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
1892 return arp_reduce(dev
, skb
);
1893 #if IS_ENABLED(CONFIG_IPV6)
1894 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
&&
1895 pskb_may_pull(skb
, sizeof(struct ipv6hdr
)
1896 + sizeof(struct nd_msg
)) &&
1897 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
1900 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1901 if (msg
->icmph
.icmp6_code
== 0 &&
1902 msg
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
)
1903 return neigh_reduce(dev
, skb
);
1909 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1912 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->flags
& VXLAN_F_RSC
) &&
1913 (ntohs(eth
->h_proto
) == ETH_P_IP
||
1914 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
1915 did_rsc
= route_shortcircuit(dev
, skb
);
1917 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1921 f
= vxlan_find_mac(vxlan
, all_zeros_mac
);
1923 if ((vxlan
->flags
& VXLAN_F_L2MISS
) &&
1924 !is_multicast_ether_addr(eth
->h_dest
))
1925 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
1927 dev
->stats
.tx_dropped
++;
1929 return NETDEV_TX_OK
;
1933 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
1934 struct sk_buff
*skb1
;
1936 skb1
= skb_clone(skb
, GFP_ATOMIC
);
1938 vxlan_xmit_one(skb1
, dev
, rdst
, did_rsc
);
1942 return NETDEV_TX_OK
;
1945 /* Walk the forwarding table and purge stale entries */
1946 static void vxlan_cleanup(unsigned long arg
)
1948 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
1949 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
1952 if (!netif_running(vxlan
->dev
))
1955 spin_lock_bh(&vxlan
->hash_lock
);
1956 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1957 struct hlist_node
*p
, *n
;
1958 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1960 = container_of(p
, struct vxlan_fdb
, hlist
);
1961 unsigned long timeout
;
1963 if (f
->state
& NUD_PERMANENT
)
1966 timeout
= f
->used
+ vxlan
->age_interval
* HZ
;
1967 if (time_before_eq(timeout
, jiffies
)) {
1968 netdev_dbg(vxlan
->dev
,
1969 "garbage collect %pM\n",
1971 f
->state
= NUD_STALE
;
1972 vxlan_fdb_destroy(vxlan
, f
);
1973 } else if (time_before(timeout
, next_timer
))
1974 next_timer
= timeout
;
1977 spin_unlock_bh(&vxlan
->hash_lock
);
1979 mod_timer(&vxlan
->age_timer
, next_timer
);
1982 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
)
1984 __u32 vni
= vxlan
->default_dst
.remote_vni
;
1986 vxlan
->vn_sock
= vs
;
1987 hlist_add_head_rcu(&vxlan
->hlist
, vni_head(vs
, vni
));
1990 /* Setup stats when device is created */
1991 static int vxlan_init(struct net_device
*dev
)
1993 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1994 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
1995 bool ipv6
= vxlan
->flags
& VXLAN_F_IPV6
;
1996 struct vxlan_sock
*vs
;
1998 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
2002 spin_lock(&vn
->sock_lock
);
2003 vs
= vxlan_find_sock(dev_net(dev
), ipv6
? AF_INET6
: AF_INET
,
2005 if (vs
&& atomic_add_unless(&vs
->refcnt
, 1, 0)) {
2006 /* If we have a socket with same port already, reuse it */
2007 vxlan_vs_add_dev(vs
, vxlan
);
2009 /* otherwise make new socket outside of RTNL */
2011 queue_work(vxlan_wq
, &vxlan
->sock_work
);
2013 spin_unlock(&vn
->sock_lock
);
2018 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
)
2020 struct vxlan_fdb
*f
;
2022 spin_lock_bh(&vxlan
->hash_lock
);
2023 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
);
2025 vxlan_fdb_destroy(vxlan
, f
);
2026 spin_unlock_bh(&vxlan
->hash_lock
);
2029 static void vxlan_uninit(struct net_device
*dev
)
2031 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2032 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
2034 vxlan_fdb_delete_default(vxlan
);
2037 vxlan_sock_release(vs
);
2038 free_percpu(dev
->tstats
);
2041 /* Start ageing timer and join group when device is brought up */
2042 static int vxlan_open(struct net_device
*dev
)
2044 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
2045 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2046 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
2048 /* socket hasn't been created */
2052 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2053 vxlan_group_used(vn
, &vxlan
->default_dst
.remote_ip
)) {
2054 vxlan_sock_hold(vs
);
2056 queue_work(vxlan_wq
, &vxlan
->igmp_join
);
2059 if (vxlan
->age_interval
)
2060 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2065 /* Purge the forwarding table */
2066 static void vxlan_flush(struct vxlan_dev
*vxlan
)
2070 spin_lock_bh(&vxlan
->hash_lock
);
2071 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2072 struct hlist_node
*p
, *n
;
2073 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2075 = container_of(p
, struct vxlan_fdb
, hlist
);
2076 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2077 if (!is_zero_ether_addr(f
->eth_addr
))
2078 vxlan_fdb_destroy(vxlan
, f
);
2081 spin_unlock_bh(&vxlan
->hash_lock
);
2084 /* Cleanup timer and forwarding table on shutdown */
2085 static int vxlan_stop(struct net_device
*dev
)
2087 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
2088 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2089 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
2091 if (vs
&& vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2092 ! vxlan_group_used(vn
, &vxlan
->default_dst
.remote_ip
)) {
2093 vxlan_sock_hold(vs
);
2095 queue_work(vxlan_wq
, &vxlan
->igmp_leave
);
2098 del_timer_sync(&vxlan
->age_timer
);
2105 /* Stub, nothing needs to be done. */
2106 static void vxlan_set_multicast_list(struct net_device
*dev
)
2110 static const struct net_device_ops vxlan_netdev_ops
= {
2111 .ndo_init
= vxlan_init
,
2112 .ndo_uninit
= vxlan_uninit
,
2113 .ndo_open
= vxlan_open
,
2114 .ndo_stop
= vxlan_stop
,
2115 .ndo_start_xmit
= vxlan_xmit
,
2116 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2117 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2118 .ndo_change_mtu
= eth_change_mtu
,
2119 .ndo_validate_addr
= eth_validate_addr
,
2120 .ndo_set_mac_address
= eth_mac_addr
,
2121 .ndo_fdb_add
= vxlan_fdb_add
,
2122 .ndo_fdb_del
= vxlan_fdb_delete
,
2123 .ndo_fdb_dump
= vxlan_fdb_dump
,
2126 /* Info for udev, that this is a virtual tunnel endpoint */
2127 static struct device_type vxlan_type
= {
2131 /* Calls the ndo_add_vxlan_port of the caller in order to
2132 * supply the listening VXLAN udp ports. Callers are expected
2133 * to implement the ndo_add_vxlan_port.
2135 void vxlan_get_rx_port(struct net_device
*dev
)
2137 struct vxlan_sock
*vs
;
2138 struct net
*net
= dev_net(dev
);
2139 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2140 sa_family_t sa_family
;
2144 spin_lock(&vn
->sock_lock
);
2145 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2146 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
) {
2147 port
= inet_sk(vs
->sock
->sk
)->inet_sport
;
2148 sa_family
= vs
->sock
->sk
->sk_family
;
2149 dev
->netdev_ops
->ndo_add_vxlan_port(dev
, sa_family
,
2153 spin_unlock(&vn
->sock_lock
);
2155 EXPORT_SYMBOL_GPL(vxlan_get_rx_port
);
2157 /* Initialize the device structure. */
2158 static void vxlan_setup(struct net_device
*dev
)
2160 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2164 eth_hw_addr_random(dev
);
2166 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
== AF_INET6
)
2167 dev
->needed_headroom
= ETH_HLEN
+ VXLAN6_HEADROOM
;
2169 dev
->needed_headroom
= ETH_HLEN
+ VXLAN_HEADROOM
;
2171 dev
->netdev_ops
= &vxlan_netdev_ops
;
2172 dev
->destructor
= free_netdev
;
2173 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2175 dev
->tx_queue_len
= 0;
2176 dev
->features
|= NETIF_F_LLTX
;
2177 dev
->features
|= NETIF_F_NETNS_LOCAL
;
2178 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2179 dev
->features
|= NETIF_F_RXCSUM
;
2180 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2182 dev
->vlan_features
= dev
->features
;
2183 dev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2184 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2185 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2186 dev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2187 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
2188 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2190 INIT_LIST_HEAD(&vxlan
->next
);
2191 spin_lock_init(&vxlan
->hash_lock
);
2192 INIT_WORK(&vxlan
->igmp_join
, vxlan_igmp_join
);
2193 INIT_WORK(&vxlan
->igmp_leave
, vxlan_igmp_leave
);
2194 INIT_WORK(&vxlan
->sock_work
, vxlan_sock_work
);
2196 init_timer_deferrable(&vxlan
->age_timer
);
2197 vxlan
->age_timer
.function
= vxlan_cleanup
;
2198 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2200 inet_get_local_port_range(&low
, &high
);
2201 vxlan
->port_min
= low
;
2202 vxlan
->port_max
= high
;
2203 vxlan
->dst_port
= htons(vxlan_port
);
2207 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2208 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2211 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2212 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2213 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2214 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2215 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2216 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2217 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2218 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2219 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2220 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2221 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2222 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2223 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2224 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2225 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2226 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2227 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2228 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2231 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
2233 if (tb
[IFLA_ADDRESS
]) {
2234 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2235 pr_debug("invalid link address (not ethernet)\n");
2239 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2240 pr_debug("invalid all zero ethernet address\n");
2241 return -EADDRNOTAVAIL
;
2248 if (data
[IFLA_VXLAN_ID
]) {
2249 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2250 if (id
>= VXLAN_VID_MASK
)
2254 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2255 const struct ifla_vxlan_port_range
*p
2256 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2258 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2259 pr_debug("port range %u .. %u not valid\n",
2260 ntohs(p
->low
), ntohs(p
->high
));
2268 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2269 struct ethtool_drvinfo
*drvinfo
)
2271 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2272 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2275 static const struct ethtool_ops vxlan_ethtool_ops
= {
2276 .get_drvinfo
= vxlan_get_drvinfo
,
2277 .get_link
= ethtool_op_get_link
,
2280 static void vxlan_del_work(struct work_struct
*work
)
2282 struct vxlan_sock
*vs
= container_of(work
, struct vxlan_sock
, del_work
);
2284 sk_release_kernel(vs
->sock
->sk
);
2288 #if IS_ENABLED(CONFIG_IPV6)
2289 /* Create UDP socket for encapsulation receive. AF_INET6 socket
2290 * could be used for both IPv4 and IPv6 communications, but
2291 * users may set bindv6only=1.
2293 static int create_v6_sock(struct net
*net
, __be16 port
, struct socket
**psock
)
2296 struct socket
*sock
;
2297 struct sockaddr_in6 vxlan_addr
= {
2298 .sin6_family
= AF_INET6
,
2303 rc
= sock_create_kern(AF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
2305 pr_debug("UDPv6 socket create failed\n");
2309 /* Put in proper namespace */
2311 sk_change_net(sk
, net
);
2313 kernel_setsockopt(sock
, SOL_IPV6
, IPV6_V6ONLY
,
2314 (char *)&val
, sizeof(val
));
2315 rc
= kernel_bind(sock
, (struct sockaddr
*)&vxlan_addr
,
2316 sizeof(struct sockaddr_in6
));
2318 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2319 &vxlan_addr
.sin6_addr
, ntohs(vxlan_addr
.sin6_port
), rc
);
2320 sk_release_kernel(sk
);
2323 /* At this point, IPv6 module should have been loaded in
2324 * sock_create_kern().
2329 /* Disable multicast loopback */
2330 inet_sk(sk
)->mc_loop
= 0;
2336 static int create_v6_sock(struct net
*net
, __be16 port
, struct socket
**psock
)
2338 return -EPFNOSUPPORT
;
2342 static int create_v4_sock(struct net
*net
, __be16 port
, struct socket
**psock
)
2345 struct socket
*sock
;
2346 struct sockaddr_in vxlan_addr
= {
2347 .sin_family
= AF_INET
,
2348 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
2353 /* Create UDP socket for encapsulation receive. */
2354 rc
= sock_create_kern(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
2356 pr_debug("UDP socket create failed\n");
2360 /* Put in proper namespace */
2362 sk_change_net(sk
, net
);
2364 rc
= kernel_bind(sock
, (struct sockaddr
*) &vxlan_addr
,
2365 sizeof(vxlan_addr
));
2367 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2368 &vxlan_addr
.sin_addr
, ntohs(vxlan_addr
.sin_port
), rc
);
2369 sk_release_kernel(sk
);
2374 /* Disable multicast loopback */
2375 inet_sk(sk
)->mc_loop
= 0;
2379 /* Create new listen socket if needed */
2380 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, __be16 port
,
2381 vxlan_rcv_t
*rcv
, void *data
, bool ipv6
)
2383 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2384 struct vxlan_sock
*vs
;
2385 struct socket
*sock
;
2390 vs
= kmalloc(sizeof(*vs
), GFP_KERNEL
);
2392 return ERR_PTR(-ENOMEM
);
2394 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
2395 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
2397 INIT_WORK(&vs
->del_work
, vxlan_del_work
);
2400 rc
= create_v6_sock(net
, port
, &sock
);
2402 rc
= create_v4_sock(net
, port
, &sock
);
2410 atomic_set(&vs
->refcnt
, 1);
2413 rcu_assign_sk_user_data(vs
->sock
->sk
, vs
);
2415 spin_lock(&vn
->sock_lock
);
2416 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
2417 vxlan_notify_add_rx_port(sk
);
2418 spin_unlock(&vn
->sock_lock
);
2420 /* Mark socket as an encapsulation socket. */
2421 udp_sk(sk
)->encap_type
= 1;
2422 udp_sk(sk
)->encap_rcv
= vxlan_udp_encap_recv
;
2423 #if IS_ENABLED(CONFIG_IPV6)
2425 ipv6_stub
->udpv6_encap_enable();
2433 struct vxlan_sock
*vxlan_sock_add(struct net
*net
, __be16 port
,
2434 vxlan_rcv_t
*rcv
, void *data
,
2435 bool no_share
, bool ipv6
)
2437 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2438 struct vxlan_sock
*vs
;
2440 vs
= vxlan_socket_create(net
, port
, rcv
, data
, ipv6
);
2444 if (no_share
) /* Return error if sharing is not allowed. */
2447 spin_lock(&vn
->sock_lock
);
2448 vs
= vxlan_find_sock(net
, ipv6
? AF_INET6
: AF_INET
, port
);
2449 if (vs
&& ((vs
->rcv
!= rcv
) ||
2450 !atomic_add_unless(&vs
->refcnt
, 1, 0)))
2451 vs
= ERR_PTR(-EBUSY
);
2452 spin_unlock(&vn
->sock_lock
);
2455 vs
= ERR_PTR(-EINVAL
);
2459 EXPORT_SYMBOL_GPL(vxlan_sock_add
);
2461 /* Scheduled at device creation to bind to a socket */
2462 static void vxlan_sock_work(struct work_struct
*work
)
2464 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, sock_work
);
2465 struct net
*net
= dev_net(vxlan
->dev
);
2466 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2467 __be16 port
= vxlan
->dst_port
;
2468 struct vxlan_sock
*nvs
;
2470 nvs
= vxlan_sock_add(net
, port
, vxlan_rcv
, NULL
, false, vxlan
->flags
& VXLAN_F_IPV6
);
2471 spin_lock(&vn
->sock_lock
);
2473 vxlan_vs_add_dev(nvs
, vxlan
);
2474 spin_unlock(&vn
->sock_lock
);
2476 dev_put(vxlan
->dev
);
2479 static int vxlan_newlink(struct net
*net
, struct net_device
*dev
,
2480 struct nlattr
*tb
[], struct nlattr
*data
[])
2482 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2483 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2484 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2487 bool use_ipv6
= false;
2489 if (!data
[IFLA_VXLAN_ID
])
2492 vni
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2493 dst
->remote_vni
= vni
;
2495 /* Unless IPv6 is explicitly requested, assume IPv4 */
2496 dst
->remote_ip
.sa
.sa_family
= AF_INET
;
2497 if (data
[IFLA_VXLAN_GROUP
]) {
2498 dst
->remote_ip
.sin
.sin_addr
.s_addr
= nla_get_be32(data
[IFLA_VXLAN_GROUP
]);
2499 } else if (data
[IFLA_VXLAN_GROUP6
]) {
2500 if (!IS_ENABLED(CONFIG_IPV6
))
2501 return -EPFNOSUPPORT
;
2503 nla_memcpy(&dst
->remote_ip
.sin6
.sin6_addr
, data
[IFLA_VXLAN_GROUP6
],
2504 sizeof(struct in6_addr
));
2505 dst
->remote_ip
.sa
.sa_family
= AF_INET6
;
2509 if (data
[IFLA_VXLAN_LOCAL
]) {
2510 vxlan
->saddr
.sin
.sin_addr
.s_addr
= nla_get_be32(data
[IFLA_VXLAN_LOCAL
]);
2511 vxlan
->saddr
.sa
.sa_family
= AF_INET
;
2512 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
2513 if (!IS_ENABLED(CONFIG_IPV6
))
2514 return -EPFNOSUPPORT
;
2516 /* TODO: respect scope id */
2517 nla_memcpy(&vxlan
->saddr
.sin6
.sin6_addr
, data
[IFLA_VXLAN_LOCAL6
],
2518 sizeof(struct in6_addr
));
2519 vxlan
->saddr
.sa
.sa_family
= AF_INET6
;
2523 if (data
[IFLA_VXLAN_LINK
] &&
2524 (dst
->remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]))) {
2525 struct net_device
*lowerdev
2526 = __dev_get_by_index(net
, dst
->remote_ifindex
);
2529 pr_info("ifindex %d does not exist\n", dst
->remote_ifindex
);
2533 #if IS_ENABLED(CONFIG_IPV6)
2535 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
2536 if (idev
&& idev
->cnf
.disable_ipv6
) {
2537 pr_info("IPv6 is disabled via sysctl\n");
2540 vxlan
->flags
|= VXLAN_F_IPV6
;
2545 dev
->mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2547 dev
->needed_headroom
= lowerdev
->hard_header_len
+
2548 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2551 if (data
[IFLA_VXLAN_TOS
])
2552 vxlan
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
2554 if (data
[IFLA_VXLAN_TTL
])
2555 vxlan
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
2557 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
2558 vxlan
->flags
|= VXLAN_F_LEARN
;
2560 if (data
[IFLA_VXLAN_AGEING
])
2561 vxlan
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
2563 vxlan
->age_interval
= FDB_AGE_DEFAULT
;
2565 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
2566 vxlan
->flags
|= VXLAN_F_PROXY
;
2568 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
2569 vxlan
->flags
|= VXLAN_F_RSC
;
2571 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
2572 vxlan
->flags
|= VXLAN_F_L2MISS
;
2574 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
2575 vxlan
->flags
|= VXLAN_F_L3MISS
;
2577 if (data
[IFLA_VXLAN_LIMIT
])
2578 vxlan
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
2580 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2581 const struct ifla_vxlan_port_range
*p
2582 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2583 vxlan
->port_min
= ntohs(p
->low
);
2584 vxlan
->port_max
= ntohs(p
->high
);
2587 if (data
[IFLA_VXLAN_PORT
])
2588 vxlan
->dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
2590 if (vxlan_find_vni(net
, vni
, use_ipv6
? AF_INET6
: AF_INET
,
2592 pr_info("duplicate VNI %u\n", vni
);
2596 SET_ETHTOOL_OPS(dev
, &vxlan_ethtool_ops
);
2598 /* create an fdb entry for a valid default destination */
2599 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
2600 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
2601 &vxlan
->default_dst
.remote_ip
,
2602 NUD_REACHABLE
|NUD_PERMANENT
,
2603 NLM_F_EXCL
|NLM_F_CREATE
,
2605 vxlan
->default_dst
.remote_vni
,
2606 vxlan
->default_dst
.remote_ifindex
,
2612 err
= register_netdevice(dev
);
2614 vxlan_fdb_delete_default(vxlan
);
2618 list_add(&vxlan
->next
, &vn
->vxlan_list
);
2623 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
2625 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
2626 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2628 spin_lock(&vn
->sock_lock
);
2629 if (!hlist_unhashed(&vxlan
->hlist
))
2630 hlist_del_rcu(&vxlan
->hlist
);
2631 spin_unlock(&vn
->sock_lock
);
2633 list_del(&vxlan
->next
);
2634 unregister_netdevice_queue(dev
, head
);
2637 static size_t vxlan_get_size(const struct net_device
*dev
)
2640 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
2641 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
2642 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
2643 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
2644 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
2645 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
2646 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
2647 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
2648 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
2649 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
2650 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
2651 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
2652 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
2653 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
2654 nla_total_size(sizeof(__be16
))+ /* IFLA_VXLAN_PORT */
2658 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
2660 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2661 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2662 struct ifla_vxlan_port_range ports
= {
2663 .low
= htons(vxlan
->port_min
),
2664 .high
= htons(vxlan
->port_max
),
2667 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, dst
->remote_vni
))
2668 goto nla_put_failure
;
2670 if (!vxlan_addr_any(&dst
->remote_ip
)) {
2671 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
2672 if (nla_put_be32(skb
, IFLA_VXLAN_GROUP
,
2673 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
2674 goto nla_put_failure
;
2675 #if IS_ENABLED(CONFIG_IPV6)
2677 if (nla_put(skb
, IFLA_VXLAN_GROUP6
, sizeof(struct in6_addr
),
2678 &dst
->remote_ip
.sin6
.sin6_addr
))
2679 goto nla_put_failure
;
2684 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
2685 goto nla_put_failure
;
2687 if (!vxlan_addr_any(&vxlan
->saddr
)) {
2688 if (vxlan
->saddr
.sa
.sa_family
== AF_INET
) {
2689 if (nla_put_be32(skb
, IFLA_VXLAN_LOCAL
,
2690 vxlan
->saddr
.sin
.sin_addr
.s_addr
))
2691 goto nla_put_failure
;
2692 #if IS_ENABLED(CONFIG_IPV6)
2694 if (nla_put(skb
, IFLA_VXLAN_LOCAL6
, sizeof(struct in6_addr
),
2695 &vxlan
->saddr
.sin6
.sin6_addr
))
2696 goto nla_put_failure
;
2701 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->ttl
) ||
2702 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->tos
) ||
2703 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
2704 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
2705 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
2706 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
2707 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
2708 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
2709 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
2710 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
2711 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
2712 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->age_interval
) ||
2713 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->addrmax
) ||
2714 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->dst_port
))
2715 goto nla_put_failure
;
2717 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
2718 goto nla_put_failure
;
2726 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
2728 .maxtype
= IFLA_VXLAN_MAX
,
2729 .policy
= vxlan_policy
,
2730 .priv_size
= sizeof(struct vxlan_dev
),
2731 .setup
= vxlan_setup
,
2732 .validate
= vxlan_validate
,
2733 .newlink
= vxlan_newlink
,
2734 .dellink
= vxlan_dellink
,
2735 .get_size
= vxlan_get_size
,
2736 .fill_info
= vxlan_fill_info
,
2739 static __net_init
int vxlan_init_net(struct net
*net
)
2741 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2744 INIT_LIST_HEAD(&vn
->vxlan_list
);
2745 spin_lock_init(&vn
->sock_lock
);
2747 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
2748 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
2753 static __net_exit
void vxlan_exit_net(struct net
*net
)
2755 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2756 struct vxlan_dev
*vxlan
;
2760 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
)
2761 unregister_netdevice_queue(vxlan
->dev
, &list
);
2762 unregister_netdevice_many(&list
);
2766 static struct pernet_operations vxlan_net_ops
= {
2767 .init
= vxlan_init_net
,
2768 .exit
= vxlan_exit_net
,
2769 .id
= &vxlan_net_id
,
2770 .size
= sizeof(struct vxlan_net
),
2773 static int __init
vxlan_init_module(void)
2777 vxlan_wq
= alloc_workqueue("vxlan", 0, 0);
2781 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
2783 rc
= register_pernet_device(&vxlan_net_ops
);
2787 rc
= rtnl_link_register(&vxlan_link_ops
);
2794 unregister_pernet_device(&vxlan_net_ops
);
2796 destroy_workqueue(vxlan_wq
);
2799 late_initcall(vxlan_init_module
);
2801 static void __exit
vxlan_cleanup_module(void)
2803 rtnl_link_unregister(&vxlan_link_ops
);
2804 destroy_workqueue(vxlan_wq
);
2805 unregister_pernet_device(&vxlan_net_ops
);
2808 module_exit(vxlan_cleanup_module
);
2810 MODULE_LICENSE("GPL");
2811 MODULE_VERSION(VXLAN_VERSION
);
2812 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2813 MODULE_ALIAS_RTNL_LINK("vxlan");