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 static void vxlan_rcv(struct vxlan_sock
*vs
,
1079 struct sk_buff
*skb
, __be32 vx_vni
)
1081 struct iphdr
*oip
= NULL
;
1082 struct ipv6hdr
*oip6
= NULL
;
1083 struct vxlan_dev
*vxlan
;
1084 struct pcpu_tstats
*stats
;
1085 union vxlan_addr saddr
;
1088 union vxlan_addr
*remote_ip
;
1090 vni
= ntohl(vx_vni
) >> 8;
1091 /* Is this VNI defined? */
1092 vxlan
= vxlan_vs_find_vni(vs
, vni
);
1096 remote_ip
= &vxlan
->default_dst
.remote_ip
;
1097 skb_reset_mac_header(skb
);
1098 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1100 /* Ignore packet loops (and multicast echo) */
1101 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1104 /* Re-examine inner Ethernet packet */
1105 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1107 saddr
.sin
.sin_addr
.s_addr
= oip
->saddr
;
1108 saddr
.sa
.sa_family
= AF_INET
;
1109 #if IS_ENABLED(CONFIG_IPV6)
1111 oip6
= ipv6_hdr(skb
);
1112 saddr
.sin6
.sin6_addr
= oip6
->saddr
;
1113 saddr
.sa
.sa_family
= AF_INET6
;
1117 if ((vxlan
->flags
& VXLAN_F_LEARN
) &&
1118 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
))
1121 skb_reset_network_header(skb
);
1124 err
= IP6_ECN_decapsulate(oip6
, skb
);
1126 err
= IP_ECN_decapsulate(oip
, skb
);
1128 if (unlikely(err
)) {
1129 if (log_ecn_error
) {
1131 net_info_ratelimited("non-ECT from %pI6\n",
1134 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1135 &oip
->saddr
, oip
->tos
);
1138 ++vxlan
->dev
->stats
.rx_frame_errors
;
1139 ++vxlan
->dev
->stats
.rx_errors
;
1144 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1145 u64_stats_update_begin(&stats
->syncp
);
1146 stats
->rx_packets
++;
1147 stats
->rx_bytes
+= skb
->len
;
1148 u64_stats_update_end(&stats
->syncp
);
1154 /* Consume bad packet */
1158 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1160 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1161 struct arphdr
*parp
;
1164 struct neighbour
*n
;
1166 if (dev
->flags
& IFF_NOARP
)
1169 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1170 dev
->stats
.tx_dropped
++;
1173 parp
= arp_hdr(skb
);
1175 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1176 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1177 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1178 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1179 parp
->ar_hln
!= dev
->addr_len
||
1182 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1184 arpptr
+= dev
->addr_len
; /* sha */
1185 memcpy(&sip
, arpptr
, sizeof(sip
));
1186 arpptr
+= sizeof(sip
);
1187 arpptr
+= dev
->addr_len
; /* tha */
1188 memcpy(&tip
, arpptr
, sizeof(tip
));
1190 if (ipv4_is_loopback(tip
) ||
1191 ipv4_is_multicast(tip
))
1194 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1197 struct vxlan_fdb
*f
;
1198 struct sk_buff
*reply
;
1200 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1205 f
= vxlan_find_mac(vxlan
, n
->ha
);
1206 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1207 /* bridge-local neighbor */
1212 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1220 skb_reset_mac_header(reply
);
1221 __skb_pull(reply
, skb_network_offset(reply
));
1222 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1223 reply
->pkt_type
= PACKET_HOST
;
1225 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1226 dev
->stats
.rx_dropped
++;
1227 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1228 union vxlan_addr ipa
= {
1229 .sin
.sin_addr
.s_addr
= tip
,
1230 .sin
.sin_family
= AF_INET
,
1233 vxlan_ip_miss(dev
, &ipa
);
1237 return NETDEV_TX_OK
;
1240 #if IS_ENABLED(CONFIG_IPV6)
1242 static struct sk_buff
*vxlan_na_create(struct sk_buff
*request
,
1243 struct neighbour
*n
, bool isrouter
)
1245 struct net_device
*dev
= request
->dev
;
1246 struct sk_buff
*reply
;
1247 struct nd_msg
*ns
, *na
;
1248 struct ipv6hdr
*pip6
;
1250 int na_olen
= 8; /* opt hdr + ETH_ALEN for target */
1257 len
= LL_RESERVED_SPACE(dev
) + sizeof(struct ipv6hdr
) +
1258 sizeof(*na
) + na_olen
+ dev
->needed_tailroom
;
1259 reply
= alloc_skb(len
, GFP_ATOMIC
);
1263 reply
->protocol
= htons(ETH_P_IPV6
);
1265 skb_reserve(reply
, LL_RESERVED_SPACE(request
->dev
));
1266 skb_push(reply
, sizeof(struct ethhdr
));
1267 skb_set_mac_header(reply
, 0);
1269 ns
= (struct nd_msg
*)skb_transport_header(request
);
1271 daddr
= eth_hdr(request
)->h_source
;
1272 ns_olen
= request
->len
- skb_transport_offset(request
) - sizeof(*ns
);
1273 for (i
= 0; i
< ns_olen
-1; i
+= (ns
->opt
[i
+1]<<3)) {
1274 if (ns
->opt
[i
] == ND_OPT_SOURCE_LL_ADDR
) {
1275 daddr
= ns
->opt
+ i
+ sizeof(struct nd_opt_hdr
);
1280 /* Ethernet header */
1281 memcpy(eth_hdr(reply
)->h_dest
, daddr
, ETH_ALEN
);
1282 memcpy(eth_hdr(reply
)->h_source
, n
->ha
, ETH_ALEN
);
1283 eth_hdr(reply
)->h_proto
= htons(ETH_P_IPV6
);
1284 reply
->protocol
= htons(ETH_P_IPV6
);
1286 skb_pull(reply
, sizeof(struct ethhdr
));
1287 skb_set_network_header(reply
, 0);
1288 skb_put(reply
, sizeof(struct ipv6hdr
));
1292 pip6
= ipv6_hdr(reply
);
1293 memset(pip6
, 0, sizeof(struct ipv6hdr
));
1295 pip6
->priority
= ipv6_hdr(request
)->priority
;
1296 pip6
->nexthdr
= IPPROTO_ICMPV6
;
1297 pip6
->hop_limit
= 255;
1298 pip6
->daddr
= ipv6_hdr(request
)->saddr
;
1299 pip6
->saddr
= *(struct in6_addr
*)n
->primary_key
;
1301 skb_pull(reply
, sizeof(struct ipv6hdr
));
1302 skb_set_transport_header(reply
, 0);
1304 na
= (struct nd_msg
*)skb_put(reply
, sizeof(*na
) + na_olen
);
1306 /* Neighbor Advertisement */
1307 memset(na
, 0, sizeof(*na
)+na_olen
);
1308 na
->icmph
.icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
1309 na
->icmph
.icmp6_router
= isrouter
;
1310 na
->icmph
.icmp6_override
= 1;
1311 na
->icmph
.icmp6_solicited
= 1;
1312 na
->target
= ns
->target
;
1313 memcpy(&na
->opt
[2], n
->ha
, ETH_ALEN
);
1314 na
->opt
[0] = ND_OPT_TARGET_LL_ADDR
;
1315 na
->opt
[1] = na_olen
>> 3;
1317 na
->icmph
.icmp6_cksum
= csum_ipv6_magic(&pip6
->saddr
,
1318 &pip6
->daddr
, sizeof(*na
)+na_olen
, IPPROTO_ICMPV6
,
1319 csum_partial(na
, sizeof(*na
)+na_olen
, 0));
1321 pip6
->payload_len
= htons(sizeof(*na
)+na_olen
);
1323 skb_push(reply
, sizeof(struct ipv6hdr
));
1325 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1330 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1332 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1334 const struct ipv6hdr
*iphdr
;
1335 const struct in6_addr
*saddr
, *daddr
;
1336 struct neighbour
*n
;
1337 struct inet6_dev
*in6_dev
;
1339 in6_dev
= __in6_dev_get(dev
);
1343 iphdr
= ipv6_hdr(skb
);
1344 saddr
= &iphdr
->saddr
;
1345 daddr
= &iphdr
->daddr
;
1347 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1348 if (msg
->icmph
.icmp6_code
!= 0 ||
1349 msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
1352 if (ipv6_addr_loopback(daddr
) ||
1353 ipv6_addr_is_multicast(&msg
->target
))
1356 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1359 struct vxlan_fdb
*f
;
1360 struct sk_buff
*reply
;
1362 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1367 f
= vxlan_find_mac(vxlan
, n
->ha
);
1368 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1369 /* bridge-local neighbor */
1374 reply
= vxlan_na_create(skb
, n
,
1375 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1382 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1383 dev
->stats
.rx_dropped
++;
1385 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1386 union vxlan_addr ipa
= {
1387 .sin6
.sin6_addr
= msg
->target
,
1388 .sin6
.sin6_family
= AF_INET6
,
1391 vxlan_ip_miss(dev
, &ipa
);
1396 return NETDEV_TX_OK
;
1400 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1402 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1403 struct neighbour
*n
;
1405 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1409 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1414 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1417 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1418 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1419 union vxlan_addr ipa
= {
1420 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1421 .sin
.sin_family
= AF_INET
,
1424 vxlan_ip_miss(dev
, &ipa
);
1430 #if IS_ENABLED(CONFIG_IPV6)
1433 struct ipv6hdr
*pip6
;
1435 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1437 pip6
= ipv6_hdr(skb
);
1438 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1439 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1440 union vxlan_addr ipa
= {
1441 .sin6
.sin6_addr
= pip6
->daddr
,
1442 .sin6
.sin6_family
= AF_INET6
,
1445 vxlan_ip_miss(dev
, &ipa
);
1459 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1461 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1463 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1472 static void vxlan_sock_put(struct sk_buff
*skb
)
1477 /* On transmit, associate with the tunnel socket */
1478 static void vxlan_set_owner(struct sock
*sk
, struct sk_buff
*skb
)
1483 skb
->destructor
= vxlan_sock_put
;
1486 /* Compute source port for outgoing packet
1487 * first choice to use L4 flow hash since it will spread
1488 * better and maybe available from hardware
1489 * secondary choice is to use jhash on the Ethernet header
1491 __be16
vxlan_src_port(__u16 port_min
, __u16 port_max
, struct sk_buff
*skb
)
1493 unsigned int range
= (port_max
- port_min
) + 1;
1496 hash
= skb_get_rxhash(skb
);
1498 hash
= jhash(skb
->data
, 2 * ETH_ALEN
,
1499 (__force u32
) skb
->protocol
);
1501 return htons((((u64
) hash
* range
) >> 32) + port_min
);
1503 EXPORT_SYMBOL_GPL(vxlan_src_port
);
1505 static int handle_offloads(struct sk_buff
*skb
)
1507 if (skb_is_gso(skb
)) {
1508 int err
= skb_unclone(skb
, GFP_ATOMIC
);
1512 skb_shinfo(skb
)->gso_type
|= SKB_GSO_UDP_TUNNEL
;
1513 } else if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
1514 skb
->ip_summed
= CHECKSUM_NONE
;
1519 #if IS_ENABLED(CONFIG_IPV6)
1520 static int vxlan6_xmit_skb(struct vxlan_sock
*vs
,
1521 struct dst_entry
*dst
, struct sk_buff
*skb
,
1522 struct net_device
*dev
, struct in6_addr
*saddr
,
1523 struct in6_addr
*daddr
, __u8 prio
, __u8 ttl
,
1524 __be16 src_port
, __be16 dst_port
, __be32 vni
)
1526 struct ipv6hdr
*ip6h
;
1527 struct vxlanhdr
*vxh
;
1532 if (!skb
->encapsulation
) {
1533 skb_reset_inner_headers(skb
);
1534 skb
->encapsulation
= 1;
1537 skb_scrub_packet(skb
, false);
1539 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1540 + VXLAN_HLEN
+ sizeof(struct ipv6hdr
)
1541 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
1543 /* Need space for new headers (invalidates iph ptr) */
1544 err
= skb_cow_head(skb
, min_headroom
);
1548 if (vlan_tx_tag_present(skb
)) {
1549 if (WARN_ON(!__vlan_put_tag(skb
,
1551 vlan_tx_tag_get(skb
))))
1557 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1558 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1561 __skb_push(skb
, sizeof(*uh
));
1562 skb_reset_transport_header(skb
);
1565 uh
->dest
= dst_port
;
1566 uh
->source
= src_port
;
1568 uh
->len
= htons(skb
->len
);
1571 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1572 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1574 skb_dst_set(skb
, dst
);
1576 if (!skb_is_gso(skb
) && !(dst
->dev
->features
& NETIF_F_IPV6_CSUM
)) {
1577 __wsum csum
= skb_checksum(skb
, 0, skb
->len
, 0);
1578 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1579 uh
->check
= csum_ipv6_magic(saddr
, daddr
, skb
->len
,
1582 uh
->check
= CSUM_MANGLED_0
;
1584 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1585 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
1586 skb
->csum_offset
= offsetof(struct udphdr
, check
);
1587 uh
->check
= ~csum_ipv6_magic(saddr
, daddr
,
1588 skb
->len
, IPPROTO_UDP
, 0);
1591 __skb_push(skb
, sizeof(*ip6h
));
1592 skb_reset_network_header(skb
);
1593 ip6h
= ipv6_hdr(skb
);
1595 ip6h
->priority
= prio
;
1596 ip6h
->flow_lbl
[0] = 0;
1597 ip6h
->flow_lbl
[1] = 0;
1598 ip6h
->flow_lbl
[2] = 0;
1599 ip6h
->payload_len
= htons(skb
->len
);
1600 ip6h
->nexthdr
= IPPROTO_UDP
;
1601 ip6h
->hop_limit
= ttl
;
1602 ip6h
->daddr
= *daddr
;
1603 ip6h
->saddr
= *saddr
;
1605 vxlan_set_owner(vs
->sock
->sk
, skb
);
1607 err
= handle_offloads(skb
);
1611 ip6tunnel_xmit(skb
, dev
);
1616 int vxlan_xmit_skb(struct vxlan_sock
*vs
,
1617 struct rtable
*rt
, struct sk_buff
*skb
,
1618 __be32 src
, __be32 dst
, __u8 tos
, __u8 ttl
, __be16 df
,
1619 __be16 src_port
, __be16 dst_port
, __be32 vni
)
1621 struct vxlanhdr
*vxh
;
1626 if (!skb
->encapsulation
) {
1627 skb_reset_inner_headers(skb
);
1628 skb
->encapsulation
= 1;
1631 min_headroom
= LL_RESERVED_SPACE(rt
->dst
.dev
) + rt
->dst
.header_len
1632 + VXLAN_HLEN
+ sizeof(struct iphdr
)
1633 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
1635 /* Need space for new headers (invalidates iph ptr) */
1636 err
= skb_cow_head(skb
, min_headroom
);
1640 if (vlan_tx_tag_present(skb
)) {
1641 if (WARN_ON(!__vlan_put_tag(skb
,
1643 vlan_tx_tag_get(skb
))))
1649 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1650 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1653 __skb_push(skb
, sizeof(*uh
));
1654 skb_reset_transport_header(skb
);
1657 uh
->dest
= dst_port
;
1658 uh
->source
= src_port
;
1660 uh
->len
= htons(skb
->len
);
1663 vxlan_set_owner(vs
->sock
->sk
, skb
);
1665 err
= handle_offloads(skb
);
1669 return iptunnel_xmit(rt
, skb
, src
, dst
, IPPROTO_UDP
, tos
, ttl
, df
,
1672 EXPORT_SYMBOL_GPL(vxlan_xmit_skb
);
1674 /* Bypass encapsulation if the destination is local */
1675 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
1676 struct vxlan_dev
*dst_vxlan
)
1678 struct pcpu_tstats
*tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
1679 struct pcpu_tstats
*rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
1680 union vxlan_addr loopback
;
1681 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
1682 struct net_device
*dev
= skb
->dev
;
1685 skb
->pkt_type
= PACKET_HOST
;
1686 skb
->encapsulation
= 0;
1687 skb
->dev
= dst_vxlan
->dev
;
1688 __skb_pull(skb
, skb_network_offset(skb
));
1690 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1691 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1692 loopback
.sa
.sa_family
= AF_INET
;
1693 #if IS_ENABLED(CONFIG_IPV6)
1695 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
1696 loopback
.sa
.sa_family
= AF_INET6
;
1700 if (dst_vxlan
->flags
& VXLAN_F_LEARN
)
1701 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
);
1703 u64_stats_update_begin(&tx_stats
->syncp
);
1704 tx_stats
->tx_packets
++;
1705 tx_stats
->tx_bytes
+= len
;
1706 u64_stats_update_end(&tx_stats
->syncp
);
1708 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
1709 u64_stats_update_begin(&rx_stats
->syncp
);
1710 rx_stats
->rx_packets
++;
1711 rx_stats
->rx_bytes
+= len
;
1712 u64_stats_update_end(&rx_stats
->syncp
);
1714 dev
->stats
.rx_dropped
++;
1718 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
1719 struct vxlan_rdst
*rdst
, bool did_rsc
)
1721 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1722 struct rtable
*rt
= NULL
;
1723 const struct iphdr
*old_iph
;
1725 union vxlan_addr
*dst
;
1726 __be16 src_port
= 0, dst_port
;
1732 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->dst_port
;
1733 vni
= rdst
->remote_vni
;
1734 dst
= &rdst
->remote_ip
;
1736 if (vxlan_addr_any(dst
)) {
1738 /* short-circuited back to local bridge */
1739 vxlan_encap_bypass(skb
, vxlan
, vxlan
);
1745 old_iph
= ip_hdr(skb
);
1748 if (!ttl
&& vxlan_addr_multicast(dst
))
1753 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
1755 src_port
= vxlan_src_port(vxlan
->port_min
, vxlan
->port_max
, skb
);
1757 if (dst
->sa
.sa_family
== AF_INET
) {
1758 memset(&fl4
, 0, sizeof(fl4
));
1759 fl4
.flowi4_oif
= rdst
->remote_ifindex
;
1760 fl4
.flowi4_tos
= RT_TOS(tos
);
1761 fl4
.daddr
= dst
->sin
.sin_addr
.s_addr
;
1762 fl4
.saddr
= vxlan
->saddr
.sin
.sin_addr
.s_addr
;
1764 rt
= ip_route_output_key(dev_net(dev
), &fl4
);
1766 netdev_dbg(dev
, "no route to %pI4\n",
1767 &dst
->sin
.sin_addr
.s_addr
);
1768 dev
->stats
.tx_carrier_errors
++;
1772 if (rt
->dst
.dev
== dev
) {
1773 netdev_dbg(dev
, "circular route to %pI4\n",
1774 &dst
->sin
.sin_addr
.s_addr
);
1775 dev
->stats
.collisions
++;
1779 /* Bypass encapsulation if the destination is local */
1780 if (rt
->rt_flags
& RTCF_LOCAL
&&
1781 !(rt
->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
1782 struct vxlan_dev
*dst_vxlan
;
1785 dst_vxlan
= vxlan_find_vni(dev_net(dev
), vni
,
1786 dst
->sa
.sa_family
, dst_port
);
1789 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
1793 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
1794 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
1796 err
= vxlan_xmit_skb(vxlan
->vn_sock
, rt
, skb
,
1797 fl4
.saddr
, dst
->sin
.sin_addr
.s_addr
,
1798 tos
, ttl
, df
, src_port
, dst_port
,
1803 iptunnel_xmit_stats(err
, &dev
->stats
, dev
->tstats
);
1804 #if IS_ENABLED(CONFIG_IPV6)
1806 struct sock
*sk
= vxlan
->vn_sock
->sock
->sk
;
1807 struct dst_entry
*ndst
;
1811 memset(&fl6
, 0, sizeof(fl6
));
1812 fl6
.flowi6_oif
= rdst
->remote_ifindex
;
1813 fl6
.daddr
= dst
->sin6
.sin6_addr
;
1814 fl6
.saddr
= vxlan
->saddr
.sin6
.sin6_addr
;
1815 fl6
.flowi6_proto
= IPPROTO_UDP
;
1817 if (ipv6_stub
->ipv6_dst_lookup(sk
, &ndst
, &fl6
)) {
1818 netdev_dbg(dev
, "no route to %pI6\n",
1819 &dst
->sin6
.sin6_addr
);
1820 dev
->stats
.tx_carrier_errors
++;
1824 if (ndst
->dev
== dev
) {
1825 netdev_dbg(dev
, "circular route to %pI6\n",
1826 &dst
->sin6
.sin6_addr
);
1828 dev
->stats
.collisions
++;
1832 /* Bypass encapsulation if the destination is local */
1833 flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
1834 if (flags
& RTF_LOCAL
&&
1835 !(flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
1836 struct vxlan_dev
*dst_vxlan
;
1839 dst_vxlan
= vxlan_find_vni(dev_net(dev
), vni
,
1840 dst
->sa
.sa_family
, dst_port
);
1843 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
1847 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
1849 err
= vxlan6_xmit_skb(vxlan
->vn_sock
, ndst
, skb
,
1850 dev
, &fl6
.saddr
, &fl6
.daddr
, 0, ttl
,
1851 src_port
, dst_port
, htonl(vni
<< 8));
1858 dev
->stats
.tx_dropped
++;
1864 dev
->stats
.tx_errors
++;
1869 /* Transmit local packets over Vxlan
1871 * Outer IP header inherits ECN and DF from inner header.
1872 * Outer UDP destination is the VXLAN assigned port.
1873 * source port is based on hash of flow
1875 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1877 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1879 bool did_rsc
= false;
1880 struct vxlan_rdst
*rdst
;
1881 struct vxlan_fdb
*f
;
1883 skb_reset_mac_header(skb
);
1886 if ((vxlan
->flags
& VXLAN_F_PROXY
)) {
1887 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
1888 return arp_reduce(dev
, skb
);
1889 #if IS_ENABLED(CONFIG_IPV6)
1890 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
&&
1891 pskb_may_pull(skb
, sizeof(struct ipv6hdr
)
1892 + sizeof(struct nd_msg
)) &&
1893 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
1896 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1897 if (msg
->icmph
.icmp6_code
== 0 &&
1898 msg
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
)
1899 return neigh_reduce(dev
, skb
);
1905 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1908 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->flags
& VXLAN_F_RSC
) &&
1909 (ntohs(eth
->h_proto
) == ETH_P_IP
||
1910 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
1911 did_rsc
= route_shortcircuit(dev
, skb
);
1913 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1917 f
= vxlan_find_mac(vxlan
, all_zeros_mac
);
1919 if ((vxlan
->flags
& VXLAN_F_L2MISS
) &&
1920 !is_multicast_ether_addr(eth
->h_dest
))
1921 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
1923 dev
->stats
.tx_dropped
++;
1925 return NETDEV_TX_OK
;
1929 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
1930 struct sk_buff
*skb1
;
1932 skb1
= skb_clone(skb
, GFP_ATOMIC
);
1934 vxlan_xmit_one(skb1
, dev
, rdst
, did_rsc
);
1938 return NETDEV_TX_OK
;
1941 /* Walk the forwarding table and purge stale entries */
1942 static void vxlan_cleanup(unsigned long arg
)
1944 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
1945 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
1948 if (!netif_running(vxlan
->dev
))
1951 spin_lock_bh(&vxlan
->hash_lock
);
1952 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1953 struct hlist_node
*p
, *n
;
1954 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1956 = container_of(p
, struct vxlan_fdb
, hlist
);
1957 unsigned long timeout
;
1959 if (f
->state
& NUD_PERMANENT
)
1962 timeout
= f
->used
+ vxlan
->age_interval
* HZ
;
1963 if (time_before_eq(timeout
, jiffies
)) {
1964 netdev_dbg(vxlan
->dev
,
1965 "garbage collect %pM\n",
1967 f
->state
= NUD_STALE
;
1968 vxlan_fdb_destroy(vxlan
, f
);
1969 } else if (time_before(timeout
, next_timer
))
1970 next_timer
= timeout
;
1973 spin_unlock_bh(&vxlan
->hash_lock
);
1975 mod_timer(&vxlan
->age_timer
, next_timer
);
1978 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
)
1980 __u32 vni
= vxlan
->default_dst
.remote_vni
;
1982 vxlan
->vn_sock
= vs
;
1983 hlist_add_head_rcu(&vxlan
->hlist
, vni_head(vs
, vni
));
1986 /* Setup stats when device is created */
1987 static int vxlan_init(struct net_device
*dev
)
1989 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1990 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
1991 bool ipv6
= vxlan
->flags
& VXLAN_F_IPV6
;
1992 struct vxlan_sock
*vs
;
1994 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
1998 spin_lock(&vn
->sock_lock
);
1999 vs
= vxlan_find_sock(dev_net(dev
), ipv6
? AF_INET6
: AF_INET
,
2001 if (vs
&& atomic_add_unless(&vs
->refcnt
, 1, 0)) {
2002 /* If we have a socket with same port already, reuse it */
2003 vxlan_vs_add_dev(vs
, vxlan
);
2005 /* otherwise make new socket outside of RTNL */
2007 queue_work(vxlan_wq
, &vxlan
->sock_work
);
2009 spin_unlock(&vn
->sock_lock
);
2014 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
)
2016 struct vxlan_fdb
*f
;
2018 spin_lock_bh(&vxlan
->hash_lock
);
2019 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
);
2021 vxlan_fdb_destroy(vxlan
, f
);
2022 spin_unlock_bh(&vxlan
->hash_lock
);
2025 static void vxlan_uninit(struct net_device
*dev
)
2027 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2028 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
2030 vxlan_fdb_delete_default(vxlan
);
2033 vxlan_sock_release(vs
);
2034 free_percpu(dev
->tstats
);
2037 /* Start ageing timer and join group when device is brought up */
2038 static int vxlan_open(struct net_device
*dev
)
2040 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
2041 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2042 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
2044 /* socket hasn't been created */
2048 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2049 vxlan_group_used(vn
, &vxlan
->default_dst
.remote_ip
)) {
2050 vxlan_sock_hold(vs
);
2052 queue_work(vxlan_wq
, &vxlan
->igmp_join
);
2055 if (vxlan
->age_interval
)
2056 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2061 /* Purge the forwarding table */
2062 static void vxlan_flush(struct vxlan_dev
*vxlan
)
2066 spin_lock_bh(&vxlan
->hash_lock
);
2067 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2068 struct hlist_node
*p
, *n
;
2069 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2071 = container_of(p
, struct vxlan_fdb
, hlist
);
2072 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2073 if (!is_zero_ether_addr(f
->eth_addr
))
2074 vxlan_fdb_destroy(vxlan
, f
);
2077 spin_unlock_bh(&vxlan
->hash_lock
);
2080 /* Cleanup timer and forwarding table on shutdown */
2081 static int vxlan_stop(struct net_device
*dev
)
2083 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
2084 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2085 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
2087 if (vs
&& vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2088 ! vxlan_group_used(vn
, &vxlan
->default_dst
.remote_ip
)) {
2089 vxlan_sock_hold(vs
);
2091 queue_work(vxlan_wq
, &vxlan
->igmp_leave
);
2094 del_timer_sync(&vxlan
->age_timer
);
2101 /* Stub, nothing needs to be done. */
2102 static void vxlan_set_multicast_list(struct net_device
*dev
)
2106 static const struct net_device_ops vxlan_netdev_ops
= {
2107 .ndo_init
= vxlan_init
,
2108 .ndo_uninit
= vxlan_uninit
,
2109 .ndo_open
= vxlan_open
,
2110 .ndo_stop
= vxlan_stop
,
2111 .ndo_start_xmit
= vxlan_xmit
,
2112 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2113 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2114 .ndo_change_mtu
= eth_change_mtu
,
2115 .ndo_validate_addr
= eth_validate_addr
,
2116 .ndo_set_mac_address
= eth_mac_addr
,
2117 .ndo_fdb_add
= vxlan_fdb_add
,
2118 .ndo_fdb_del
= vxlan_fdb_delete
,
2119 .ndo_fdb_dump
= vxlan_fdb_dump
,
2122 /* Info for udev, that this is a virtual tunnel endpoint */
2123 static struct device_type vxlan_type
= {
2127 /* Calls the ndo_add_vxlan_port of the caller in order to
2128 * supply the listening VXLAN udp ports. Callers are expected
2129 * to implement the ndo_add_vxlan_port.
2131 void vxlan_get_rx_port(struct net_device
*dev
)
2133 struct vxlan_sock
*vs
;
2134 struct net
*net
= dev_net(dev
);
2135 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2136 sa_family_t sa_family
;
2140 spin_lock(&vn
->sock_lock
);
2141 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2142 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
) {
2143 port
= inet_sk(vs
->sock
->sk
)->inet_sport
;
2144 sa_family
= vs
->sock
->sk
->sk_family
;
2145 dev
->netdev_ops
->ndo_add_vxlan_port(dev
, sa_family
,
2149 spin_unlock(&vn
->sock_lock
);
2151 EXPORT_SYMBOL_GPL(vxlan_get_rx_port
);
2153 /* Initialize the device structure. */
2154 static void vxlan_setup(struct net_device
*dev
)
2156 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2160 eth_hw_addr_random(dev
);
2162 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
== AF_INET6
)
2163 dev
->needed_headroom
= ETH_HLEN
+ VXLAN6_HEADROOM
;
2165 dev
->needed_headroom
= ETH_HLEN
+ VXLAN_HEADROOM
;
2167 dev
->netdev_ops
= &vxlan_netdev_ops
;
2168 dev
->destructor
= free_netdev
;
2169 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2171 dev
->tx_queue_len
= 0;
2172 dev
->features
|= NETIF_F_LLTX
;
2173 dev
->features
|= NETIF_F_NETNS_LOCAL
;
2174 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2175 dev
->features
|= NETIF_F_RXCSUM
;
2176 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2178 dev
->vlan_features
= dev
->features
;
2179 dev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2180 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2181 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2182 dev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2183 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
2184 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2186 INIT_LIST_HEAD(&vxlan
->next
);
2187 spin_lock_init(&vxlan
->hash_lock
);
2188 INIT_WORK(&vxlan
->igmp_join
, vxlan_igmp_join
);
2189 INIT_WORK(&vxlan
->igmp_leave
, vxlan_igmp_leave
);
2190 INIT_WORK(&vxlan
->sock_work
, vxlan_sock_work
);
2192 init_timer_deferrable(&vxlan
->age_timer
);
2193 vxlan
->age_timer
.function
= vxlan_cleanup
;
2194 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2196 inet_get_local_port_range(&low
, &high
);
2197 vxlan
->port_min
= low
;
2198 vxlan
->port_max
= high
;
2199 vxlan
->dst_port
= htons(vxlan_port
);
2203 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2204 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2207 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2208 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2209 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2210 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2211 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2212 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2213 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2214 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2215 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2216 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2217 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2218 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2219 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2220 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2221 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2222 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2223 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2224 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2227 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
2229 if (tb
[IFLA_ADDRESS
]) {
2230 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2231 pr_debug("invalid link address (not ethernet)\n");
2235 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2236 pr_debug("invalid all zero ethernet address\n");
2237 return -EADDRNOTAVAIL
;
2244 if (data
[IFLA_VXLAN_ID
]) {
2245 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2246 if (id
>= VXLAN_VID_MASK
)
2250 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2251 const struct ifla_vxlan_port_range
*p
2252 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2254 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2255 pr_debug("port range %u .. %u not valid\n",
2256 ntohs(p
->low
), ntohs(p
->high
));
2264 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2265 struct ethtool_drvinfo
*drvinfo
)
2267 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2268 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2271 static const struct ethtool_ops vxlan_ethtool_ops
= {
2272 .get_drvinfo
= vxlan_get_drvinfo
,
2273 .get_link
= ethtool_op_get_link
,
2276 static void vxlan_del_work(struct work_struct
*work
)
2278 struct vxlan_sock
*vs
= container_of(work
, struct vxlan_sock
, del_work
);
2280 sk_release_kernel(vs
->sock
->sk
);
2284 #if IS_ENABLED(CONFIG_IPV6)
2285 /* Create UDP socket for encapsulation receive. AF_INET6 socket
2286 * could be used for both IPv4 and IPv6 communications, but
2287 * users may set bindv6only=1.
2289 static int create_v6_sock(struct net
*net
, __be16 port
, struct socket
**psock
)
2292 struct socket
*sock
;
2293 struct sockaddr_in6 vxlan_addr
= {
2294 .sin6_family
= AF_INET6
,
2299 rc
= sock_create_kern(AF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
2301 pr_debug("UDPv6 socket create failed\n");
2305 /* Put in proper namespace */
2307 sk_change_net(sk
, net
);
2309 kernel_setsockopt(sock
, SOL_IPV6
, IPV6_V6ONLY
,
2310 (char *)&val
, sizeof(val
));
2311 rc
= kernel_bind(sock
, (struct sockaddr
*)&vxlan_addr
,
2312 sizeof(struct sockaddr_in6
));
2314 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2315 &vxlan_addr
.sin6_addr
, ntohs(vxlan_addr
.sin6_port
), rc
);
2316 sk_release_kernel(sk
);
2319 /* At this point, IPv6 module should have been loaded in
2320 * sock_create_kern().
2325 /* Disable multicast loopback */
2326 inet_sk(sk
)->mc_loop
= 0;
2332 static int create_v6_sock(struct net
*net
, __be16 port
, struct socket
**psock
)
2334 return -EPFNOSUPPORT
;
2338 static int create_v4_sock(struct net
*net
, __be16 port
, struct socket
**psock
)
2341 struct socket
*sock
;
2342 struct sockaddr_in vxlan_addr
= {
2343 .sin_family
= AF_INET
,
2344 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
2349 /* Create UDP socket for encapsulation receive. */
2350 rc
= sock_create_kern(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
2352 pr_debug("UDP socket create failed\n");
2356 /* Put in proper namespace */
2358 sk_change_net(sk
, net
);
2360 rc
= kernel_bind(sock
, (struct sockaddr
*) &vxlan_addr
,
2361 sizeof(vxlan_addr
));
2363 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2364 &vxlan_addr
.sin_addr
, ntohs(vxlan_addr
.sin_port
), rc
);
2365 sk_release_kernel(sk
);
2370 /* Disable multicast loopback */
2371 inet_sk(sk
)->mc_loop
= 0;
2375 /* Create new listen socket if needed */
2376 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, __be16 port
,
2377 vxlan_rcv_t
*rcv
, void *data
, bool ipv6
)
2379 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2380 struct vxlan_sock
*vs
;
2381 struct socket
*sock
;
2386 vs
= kmalloc(sizeof(*vs
), GFP_KERNEL
);
2388 return ERR_PTR(-ENOMEM
);
2390 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
2391 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
2393 INIT_WORK(&vs
->del_work
, vxlan_del_work
);
2396 rc
= create_v6_sock(net
, port
, &sock
);
2398 rc
= create_v4_sock(net
, port
, &sock
);
2406 atomic_set(&vs
->refcnt
, 1);
2409 rcu_assign_sk_user_data(vs
->sock
->sk
, vs
);
2411 spin_lock(&vn
->sock_lock
);
2412 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
2413 vxlan_notify_add_rx_port(sk
);
2414 spin_unlock(&vn
->sock_lock
);
2416 /* Mark socket as an encapsulation socket. */
2417 udp_sk(sk
)->encap_type
= 1;
2418 udp_sk(sk
)->encap_rcv
= vxlan_udp_encap_recv
;
2419 #if IS_ENABLED(CONFIG_IPV6)
2421 ipv6_stub
->udpv6_encap_enable();
2429 struct vxlan_sock
*vxlan_sock_add(struct net
*net
, __be16 port
,
2430 vxlan_rcv_t
*rcv
, void *data
,
2431 bool no_share
, bool ipv6
)
2433 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2434 struct vxlan_sock
*vs
;
2436 vs
= vxlan_socket_create(net
, port
, rcv
, data
, ipv6
);
2440 if (no_share
) /* Return error if sharing is not allowed. */
2443 spin_lock(&vn
->sock_lock
);
2444 vs
= vxlan_find_sock(net
, ipv6
? AF_INET6
: AF_INET
, port
);
2445 if (vs
&& ((vs
->rcv
!= rcv
) ||
2446 !atomic_add_unless(&vs
->refcnt
, 1, 0)))
2447 vs
= ERR_PTR(-EBUSY
);
2448 spin_unlock(&vn
->sock_lock
);
2451 vs
= ERR_PTR(-EINVAL
);
2455 EXPORT_SYMBOL_GPL(vxlan_sock_add
);
2457 /* Scheduled at device creation to bind to a socket */
2458 static void vxlan_sock_work(struct work_struct
*work
)
2460 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, sock_work
);
2461 struct net
*net
= dev_net(vxlan
->dev
);
2462 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2463 __be16 port
= vxlan
->dst_port
;
2464 struct vxlan_sock
*nvs
;
2466 nvs
= vxlan_sock_add(net
, port
, vxlan_rcv
, NULL
, false, vxlan
->flags
& VXLAN_F_IPV6
);
2467 spin_lock(&vn
->sock_lock
);
2469 vxlan_vs_add_dev(nvs
, vxlan
);
2470 spin_unlock(&vn
->sock_lock
);
2472 dev_put(vxlan
->dev
);
2475 static int vxlan_newlink(struct net
*net
, struct net_device
*dev
,
2476 struct nlattr
*tb
[], struct nlattr
*data
[])
2478 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2479 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2480 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2483 bool use_ipv6
= false;
2485 if (!data
[IFLA_VXLAN_ID
])
2488 vni
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2489 dst
->remote_vni
= vni
;
2491 /* Unless IPv6 is explicitly requested, assume IPv4 */
2492 dst
->remote_ip
.sa
.sa_family
= AF_INET
;
2493 if (data
[IFLA_VXLAN_GROUP
]) {
2494 dst
->remote_ip
.sin
.sin_addr
.s_addr
= nla_get_be32(data
[IFLA_VXLAN_GROUP
]);
2495 } else if (data
[IFLA_VXLAN_GROUP6
]) {
2496 if (!IS_ENABLED(CONFIG_IPV6
))
2497 return -EPFNOSUPPORT
;
2499 nla_memcpy(&dst
->remote_ip
.sin6
.sin6_addr
, data
[IFLA_VXLAN_GROUP6
],
2500 sizeof(struct in6_addr
));
2501 dst
->remote_ip
.sa
.sa_family
= AF_INET6
;
2505 if (data
[IFLA_VXLAN_LOCAL
]) {
2506 vxlan
->saddr
.sin
.sin_addr
.s_addr
= nla_get_be32(data
[IFLA_VXLAN_LOCAL
]);
2507 vxlan
->saddr
.sa
.sa_family
= AF_INET
;
2508 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
2509 if (!IS_ENABLED(CONFIG_IPV6
))
2510 return -EPFNOSUPPORT
;
2512 /* TODO: respect scope id */
2513 nla_memcpy(&vxlan
->saddr
.sin6
.sin6_addr
, data
[IFLA_VXLAN_LOCAL6
],
2514 sizeof(struct in6_addr
));
2515 vxlan
->saddr
.sa
.sa_family
= AF_INET6
;
2519 if (data
[IFLA_VXLAN_LINK
] &&
2520 (dst
->remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]))) {
2521 struct net_device
*lowerdev
2522 = __dev_get_by_index(net
, dst
->remote_ifindex
);
2525 pr_info("ifindex %d does not exist\n", dst
->remote_ifindex
);
2529 #if IS_ENABLED(CONFIG_IPV6)
2531 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
2532 if (idev
&& idev
->cnf
.disable_ipv6
) {
2533 pr_info("IPv6 is disabled via sysctl\n");
2536 vxlan
->flags
|= VXLAN_F_IPV6
;
2541 dev
->mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2543 dev
->needed_headroom
= lowerdev
->hard_header_len
+
2544 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2547 if (data
[IFLA_VXLAN_TOS
])
2548 vxlan
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
2550 if (data
[IFLA_VXLAN_TTL
])
2551 vxlan
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
2553 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
2554 vxlan
->flags
|= VXLAN_F_LEARN
;
2556 if (data
[IFLA_VXLAN_AGEING
])
2557 vxlan
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
2559 vxlan
->age_interval
= FDB_AGE_DEFAULT
;
2561 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
2562 vxlan
->flags
|= VXLAN_F_PROXY
;
2564 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
2565 vxlan
->flags
|= VXLAN_F_RSC
;
2567 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
2568 vxlan
->flags
|= VXLAN_F_L2MISS
;
2570 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
2571 vxlan
->flags
|= VXLAN_F_L3MISS
;
2573 if (data
[IFLA_VXLAN_LIMIT
])
2574 vxlan
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
2576 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2577 const struct ifla_vxlan_port_range
*p
2578 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2579 vxlan
->port_min
= ntohs(p
->low
);
2580 vxlan
->port_max
= ntohs(p
->high
);
2583 if (data
[IFLA_VXLAN_PORT
])
2584 vxlan
->dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
2586 if (vxlan_find_vni(net
, vni
, use_ipv6
? AF_INET6
: AF_INET
,
2588 pr_info("duplicate VNI %u\n", vni
);
2592 SET_ETHTOOL_OPS(dev
, &vxlan_ethtool_ops
);
2594 /* create an fdb entry for a valid default destination */
2595 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
2596 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
2597 &vxlan
->default_dst
.remote_ip
,
2598 NUD_REACHABLE
|NUD_PERMANENT
,
2599 NLM_F_EXCL
|NLM_F_CREATE
,
2601 vxlan
->default_dst
.remote_vni
,
2602 vxlan
->default_dst
.remote_ifindex
,
2608 err
= register_netdevice(dev
);
2610 vxlan_fdb_delete_default(vxlan
);
2614 list_add(&vxlan
->next
, &vn
->vxlan_list
);
2619 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
2621 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
2622 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2624 spin_lock(&vn
->sock_lock
);
2625 if (!hlist_unhashed(&vxlan
->hlist
))
2626 hlist_del_rcu(&vxlan
->hlist
);
2627 spin_unlock(&vn
->sock_lock
);
2629 list_del(&vxlan
->next
);
2630 unregister_netdevice_queue(dev
, head
);
2633 static size_t vxlan_get_size(const struct net_device
*dev
)
2636 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
2637 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
2638 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
2639 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
2640 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
2641 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
2642 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
2643 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
2644 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
2645 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
2646 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
2647 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
2648 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
2649 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
2650 nla_total_size(sizeof(__be16
))+ /* IFLA_VXLAN_PORT */
2654 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
2656 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2657 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2658 struct ifla_vxlan_port_range ports
= {
2659 .low
= htons(vxlan
->port_min
),
2660 .high
= htons(vxlan
->port_max
),
2663 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, dst
->remote_vni
))
2664 goto nla_put_failure
;
2666 if (!vxlan_addr_any(&dst
->remote_ip
)) {
2667 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
2668 if (nla_put_be32(skb
, IFLA_VXLAN_GROUP
,
2669 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
2670 goto nla_put_failure
;
2671 #if IS_ENABLED(CONFIG_IPV6)
2673 if (nla_put(skb
, IFLA_VXLAN_GROUP6
, sizeof(struct in6_addr
),
2674 &dst
->remote_ip
.sin6
.sin6_addr
))
2675 goto nla_put_failure
;
2680 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
2681 goto nla_put_failure
;
2683 if (!vxlan_addr_any(&vxlan
->saddr
)) {
2684 if (vxlan
->saddr
.sa
.sa_family
== AF_INET
) {
2685 if (nla_put_be32(skb
, IFLA_VXLAN_LOCAL
,
2686 vxlan
->saddr
.sin
.sin_addr
.s_addr
))
2687 goto nla_put_failure
;
2688 #if IS_ENABLED(CONFIG_IPV6)
2690 if (nla_put(skb
, IFLA_VXLAN_LOCAL6
, sizeof(struct in6_addr
),
2691 &vxlan
->saddr
.sin6
.sin6_addr
))
2692 goto nla_put_failure
;
2697 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->ttl
) ||
2698 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->tos
) ||
2699 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
2700 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
2701 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
2702 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
2703 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
2704 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
2705 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
2706 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
2707 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
2708 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->age_interval
) ||
2709 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->addrmax
) ||
2710 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->dst_port
))
2711 goto nla_put_failure
;
2713 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
2714 goto nla_put_failure
;
2722 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
2724 .maxtype
= IFLA_VXLAN_MAX
,
2725 .policy
= vxlan_policy
,
2726 .priv_size
= sizeof(struct vxlan_dev
),
2727 .setup
= vxlan_setup
,
2728 .validate
= vxlan_validate
,
2729 .newlink
= vxlan_newlink
,
2730 .dellink
= vxlan_dellink
,
2731 .get_size
= vxlan_get_size
,
2732 .fill_info
= vxlan_fill_info
,
2735 static __net_init
int vxlan_init_net(struct net
*net
)
2737 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2740 INIT_LIST_HEAD(&vn
->vxlan_list
);
2741 spin_lock_init(&vn
->sock_lock
);
2743 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
2744 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
2749 static __net_exit
void vxlan_exit_net(struct net
*net
)
2751 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2752 struct vxlan_dev
*vxlan
;
2756 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
)
2757 unregister_netdevice_queue(vxlan
->dev
, &list
);
2758 unregister_netdevice_many(&list
);
2762 static struct pernet_operations vxlan_net_ops
= {
2763 .init
= vxlan_init_net
,
2764 .exit
= vxlan_exit_net
,
2765 .id
= &vxlan_net_id
,
2766 .size
= sizeof(struct vxlan_net
),
2769 static int __init
vxlan_init_module(void)
2773 vxlan_wq
= alloc_workqueue("vxlan", 0, 0);
2777 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
2779 rc
= register_pernet_device(&vxlan_net_ops
);
2783 rc
= rtnl_link_register(&vxlan_link_ops
);
2790 unregister_pernet_device(&vxlan_net_ops
);
2792 destroy_workqueue(vxlan_wq
);
2795 late_initcall(vxlan_init_module
);
2797 static void __exit
vxlan_cleanup_module(void)
2799 rtnl_link_unregister(&vxlan_link_ops
);
2800 destroy_workqueue(vxlan_wq
);
2801 unregister_pernet_device(&vxlan_net_ops
);
2804 module_exit(vxlan_cleanup_module
);
2806 MODULE_LICENSE("GPL");
2807 MODULE_VERSION(VXLAN_VERSION
);
2808 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2809 MODULE_ALIAS_RTNL_LINK("vxlan");