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/module.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/udp.h>
18 #include <linux/igmp.h>
19 #include <linux/if_ether.h>
20 #include <linux/ethtool.h>
22 #include <net/ndisc.h>
25 #include <net/rtnetlink.h>
26 #include <net/inet_ecn.h>
27 #include <net/net_namespace.h>
28 #include <net/netns/generic.h>
29 #include <net/vxlan.h>
30 #include <net/protocol.h>
32 #if IS_ENABLED(CONFIG_IPV6)
33 #include <net/ip6_tunnel.h>
34 #include <net/ip6_checksum.h>
37 #define VXLAN_VERSION "0.1"
39 #define PORT_HASH_BITS 8
40 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
41 #define FDB_AGE_DEFAULT 300 /* 5 min */
42 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
44 /* UDP port for VXLAN traffic.
45 * The IANA assigned port is 4789, but the Linux default is 8472
46 * for compatibility with early adopters.
48 static unsigned short vxlan_port __read_mostly
= 8472;
49 module_param_named(udp_port
, vxlan_port
, ushort
, 0444);
50 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
52 static bool log_ecn_error
= true;
53 module_param(log_ecn_error
, bool, 0644);
54 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
56 static int vxlan_net_id
;
57 static struct rtnl_link_ops vxlan_link_ops
;
59 static const u8 all_zeros_mac
[ETH_ALEN
+ 2];
61 static int vxlan_sock_add(struct vxlan_dev
*vxlan
);
63 /* per-network namespace private data for this module */
65 struct list_head vxlan_list
;
66 struct hlist_head sock_list
[PORT_HASH_SIZE
];
70 /* Forwarding table entry */
72 struct hlist_node hlist
; /* linked list of entries */
74 unsigned long updated
; /* jiffies */
76 struct list_head remotes
;
77 u8 eth_addr
[ETH_ALEN
];
78 u16 state
; /* see ndm_state */
79 u8 flags
; /* see ndm_flags */
82 /* salt for hash table */
83 static u32 vxlan_salt __read_mostly
;
85 static inline bool vxlan_collect_metadata(struct vxlan_sock
*vs
)
87 return vs
->flags
& VXLAN_F_COLLECT_METADATA
||
88 ip_tunnel_collect_metadata();
91 #if IS_ENABLED(CONFIG_IPV6)
93 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
95 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
97 if (a
->sa
.sa_family
== AF_INET6
)
98 return ipv6_addr_equal(&a
->sin6
.sin6_addr
, &b
->sin6
.sin6_addr
);
100 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
103 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
105 if (ipa
->sa
.sa_family
== AF_INET6
)
106 return ipv6_addr_any(&ipa
->sin6
.sin6_addr
);
108 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
111 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
113 if (ipa
->sa
.sa_family
== AF_INET6
)
114 return ipv6_addr_is_multicast(&ipa
->sin6
.sin6_addr
);
116 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
119 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
121 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
122 ip
->sin6
.sin6_addr
= nla_get_in6_addr(nla
);
123 ip
->sa
.sa_family
= AF_INET6
;
125 } else if (nla_len(nla
) >= sizeof(__be32
)) {
126 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
127 ip
->sa
.sa_family
= AF_INET
;
130 return -EAFNOSUPPORT
;
134 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
135 const union vxlan_addr
*ip
)
137 if (ip
->sa
.sa_family
== AF_INET6
)
138 return nla_put_in6_addr(skb
, attr
, &ip
->sin6
.sin6_addr
);
140 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
143 #else /* !CONFIG_IPV6 */
146 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
148 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
151 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
153 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
156 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
158 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
161 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
163 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
164 return -EAFNOSUPPORT
;
165 } else if (nla_len(nla
) >= sizeof(__be32
)) {
166 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
167 ip
->sa
.sa_family
= AF_INET
;
170 return -EAFNOSUPPORT
;
174 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
175 const union vxlan_addr
*ip
)
177 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
181 /* Virtual Network hash table head */
182 static inline struct hlist_head
*vni_head(struct vxlan_sock
*vs
, __be32 vni
)
184 return &vs
->vni_list
[hash_32((__force u32
)vni
, VNI_HASH_BITS
)];
187 /* Socket hash table head */
188 static inline struct hlist_head
*vs_head(struct net
*net
, __be16 port
)
190 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
192 return &vn
->sock_list
[hash_32(ntohs(port
), PORT_HASH_BITS
)];
195 /* First remote destination for a forwarding entry.
196 * Guaranteed to be non-NULL because remotes are never deleted.
198 static inline struct vxlan_rdst
*first_remote_rcu(struct vxlan_fdb
*fdb
)
200 return list_entry_rcu(fdb
->remotes
.next
, struct vxlan_rdst
, list
);
203 static inline struct vxlan_rdst
*first_remote_rtnl(struct vxlan_fdb
*fdb
)
205 return list_first_entry(&fdb
->remotes
, struct vxlan_rdst
, list
);
208 /* Find VXLAN socket based on network namespace, address family and UDP port
209 * and enabled unshareable flags.
211 static struct vxlan_sock
*vxlan_find_sock(struct net
*net
, sa_family_t family
,
212 __be16 port
, u32 flags
)
214 struct vxlan_sock
*vs
;
216 flags
&= VXLAN_F_RCV_FLAGS
;
218 hlist_for_each_entry_rcu(vs
, vs_head(net
, port
), hlist
) {
219 if (inet_sk(vs
->sock
->sk
)->inet_sport
== port
&&
220 vxlan_get_sk_family(vs
) == family
&&
227 static struct vxlan_dev
*vxlan_vs_find_vni(struct vxlan_sock
*vs
, __be32 vni
)
229 struct vxlan_dev
*vxlan
;
231 /* For flow based devices, map all packets to VNI 0 */
232 if (vs
->flags
& VXLAN_F_COLLECT_METADATA
)
235 hlist_for_each_entry_rcu(vxlan
, vni_head(vs
, vni
), hlist
) {
236 if (vxlan
->default_dst
.remote_vni
== vni
)
243 /* Look up VNI in a per net namespace table */
244 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, __be32 vni
,
245 sa_family_t family
, __be16 port
,
248 struct vxlan_sock
*vs
;
250 vs
= vxlan_find_sock(net
, family
, port
, flags
);
254 return vxlan_vs_find_vni(vs
, vni
);
257 /* Fill in neighbour message in skbuff. */
258 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
259 const struct vxlan_fdb
*fdb
,
260 u32 portid
, u32 seq
, int type
, unsigned int flags
,
261 const struct vxlan_rdst
*rdst
)
263 unsigned long now
= jiffies
;
264 struct nda_cacheinfo ci
;
265 struct nlmsghdr
*nlh
;
267 bool send_ip
, send_eth
;
269 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
273 ndm
= nlmsg_data(nlh
);
274 memset(ndm
, 0, sizeof(*ndm
));
276 send_eth
= send_ip
= true;
278 if (type
== RTM_GETNEIGH
) {
279 ndm
->ndm_family
= AF_INET
;
280 send_ip
= !vxlan_addr_any(&rdst
->remote_ip
);
281 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
283 ndm
->ndm_family
= AF_BRIDGE
;
284 ndm
->ndm_state
= fdb
->state
;
285 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
286 ndm
->ndm_flags
= fdb
->flags
;
287 ndm
->ndm_type
= RTN_UNICAST
;
289 if (!net_eq(dev_net(vxlan
->dev
), vxlan
->net
) &&
290 nla_put_s32(skb
, NDA_LINK_NETNSID
,
291 peernet2id_alloc(dev_net(vxlan
->dev
), vxlan
->net
)))
292 goto nla_put_failure
;
294 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
295 goto nla_put_failure
;
297 if (send_ip
&& vxlan_nla_put_addr(skb
, NDA_DST
, &rdst
->remote_ip
))
298 goto nla_put_failure
;
300 if (rdst
->remote_port
&& rdst
->remote_port
!= vxlan
->cfg
.dst_port
&&
301 nla_put_be16(skb
, NDA_PORT
, rdst
->remote_port
))
302 goto nla_put_failure
;
303 if (rdst
->remote_vni
!= vxlan
->default_dst
.remote_vni
&&
304 nla_put_u32(skb
, NDA_VNI
, be32_to_cpu(rdst
->remote_vni
)))
305 goto nla_put_failure
;
306 if (rdst
->remote_ifindex
&&
307 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
308 goto nla_put_failure
;
310 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
311 ci
.ndm_confirmed
= 0;
312 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
315 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
316 goto nla_put_failure
;
322 nlmsg_cancel(skb
, nlh
);
326 static inline size_t vxlan_nlmsg_size(void)
328 return NLMSG_ALIGN(sizeof(struct ndmsg
))
329 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
330 + nla_total_size(sizeof(struct in6_addr
)) /* NDA_DST */
331 + nla_total_size(sizeof(__be16
)) /* NDA_PORT */
332 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
333 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
334 + nla_total_size(sizeof(__s32
)) /* NDA_LINK_NETNSID */
335 + nla_total_size(sizeof(struct nda_cacheinfo
));
338 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*fdb
,
339 struct vxlan_rdst
*rd
, int type
)
341 struct net
*net
= dev_net(vxlan
->dev
);
345 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
349 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0, rd
);
351 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
352 WARN_ON(err
== -EMSGSIZE
);
357 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
361 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
364 static void vxlan_ip_miss(struct net_device
*dev
, union vxlan_addr
*ipa
)
366 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
367 struct vxlan_fdb f
= {
370 struct vxlan_rdst remote
= {
371 .remote_ip
= *ipa
, /* goes to NDA_DST */
372 .remote_vni
= cpu_to_be32(VXLAN_N_VID
),
375 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
378 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
380 struct vxlan_fdb f
= {
383 struct vxlan_rdst remote
= { };
385 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
387 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
390 /* Hash Ethernet address */
391 static u32
eth_hash(const unsigned char *addr
)
393 u64 value
= get_unaligned((u64
*)addr
);
395 /* only want 6 bytes */
401 return hash_64(value
, FDB_HASH_BITS
);
404 /* Hash chain to use given mac address */
405 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
408 return &vxlan
->fdb_head
[eth_hash(mac
)];
411 /* Look up Ethernet address in forwarding table */
412 static struct vxlan_fdb
*__vxlan_find_mac(struct vxlan_dev
*vxlan
,
415 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
);
418 hlist_for_each_entry_rcu(f
, head
, hlist
) {
419 if (ether_addr_equal(mac
, f
->eth_addr
))
426 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
431 f
= __vxlan_find_mac(vxlan
, mac
);
438 /* caller should hold vxlan->hash_lock */
439 static struct vxlan_rdst
*vxlan_fdb_find_rdst(struct vxlan_fdb
*f
,
440 union vxlan_addr
*ip
, __be16 port
,
441 __be32 vni
, __u32 ifindex
)
443 struct vxlan_rdst
*rd
;
445 list_for_each_entry(rd
, &f
->remotes
, list
) {
446 if (vxlan_addr_equal(&rd
->remote_ip
, ip
) &&
447 rd
->remote_port
== port
&&
448 rd
->remote_vni
== vni
&&
449 rd
->remote_ifindex
== ifindex
)
456 /* Replace destination of unicast mac */
457 static int vxlan_fdb_replace(struct vxlan_fdb
*f
,
458 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
461 struct vxlan_rdst
*rd
;
463 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
467 rd
= list_first_entry_or_null(&f
->remotes
, struct vxlan_rdst
, list
);
471 dst_cache_reset(&rd
->dst_cache
);
473 rd
->remote_port
= port
;
474 rd
->remote_vni
= vni
;
475 rd
->remote_ifindex
= ifindex
;
479 /* Add/update destinations for multicast */
480 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
481 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
482 __u32 ifindex
, struct vxlan_rdst
**rdp
)
484 struct vxlan_rdst
*rd
;
486 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
490 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
494 if (dst_cache_init(&rd
->dst_cache
, GFP_ATOMIC
)) {
500 rd
->remote_port
= port
;
501 rd
->remote_vni
= vni
;
502 rd
->remote_ifindex
= ifindex
;
504 list_add_tail_rcu(&rd
->list
, &f
->remotes
);
510 static struct vxlanhdr
*vxlan_gro_remcsum(struct sk_buff
*skb
,
512 struct vxlanhdr
*vh
, size_t hdrlen
,
514 struct gro_remcsum
*grc
,
517 size_t start
, offset
;
519 if (skb
->remcsum_offload
)
522 if (!NAPI_GRO_CB(skb
)->csum_valid
)
525 start
= vxlan_rco_start(vni_field
);
526 offset
= start
+ vxlan_rco_offset(vni_field
);
528 vh
= skb_gro_remcsum_process(skb
, (void *)vh
, off
, hdrlen
,
529 start
, offset
, grc
, nopartial
);
531 skb
->remcsum_offload
= 1;
536 static struct sk_buff
**vxlan_gro_receive(struct sock
*sk
,
537 struct sk_buff
**head
,
540 struct sk_buff
*p
, **pp
= NULL
;
541 struct vxlanhdr
*vh
, *vh2
;
542 unsigned int hlen
, off_vx
;
544 struct vxlan_sock
*vs
= rcu_dereference_sk_user_data(sk
);
546 struct gro_remcsum grc
;
548 skb_gro_remcsum_init(&grc
);
550 off_vx
= skb_gro_offset(skb
);
551 hlen
= off_vx
+ sizeof(*vh
);
552 vh
= skb_gro_header_fast(skb
, off_vx
);
553 if (skb_gro_header_hard(skb
, hlen
)) {
554 vh
= skb_gro_header_slow(skb
, hlen
, off_vx
);
559 skb_gro_postpull_rcsum(skb
, vh
, sizeof(struct vxlanhdr
));
561 flags
= vh
->vx_flags
;
563 if ((flags
& VXLAN_HF_RCO
) && (vs
->flags
& VXLAN_F_REMCSUM_RX
)) {
564 vh
= vxlan_gro_remcsum(skb
, off_vx
, vh
, sizeof(struct vxlanhdr
),
567 VXLAN_F_REMCSUM_NOPARTIAL
));
573 skb_gro_pull(skb
, sizeof(struct vxlanhdr
)); /* pull vxlan header */
575 for (p
= *head
; p
; p
= p
->next
) {
576 if (!NAPI_GRO_CB(p
)->same_flow
)
579 vh2
= (struct vxlanhdr
*)(p
->data
+ off_vx
);
580 if (vh
->vx_flags
!= vh2
->vx_flags
||
581 vh
->vx_vni
!= vh2
->vx_vni
) {
582 NAPI_GRO_CB(p
)->same_flow
= 0;
587 pp
= eth_gro_receive(head
, skb
);
591 skb_gro_remcsum_cleanup(skb
, &grc
);
592 NAPI_GRO_CB(skb
)->flush
|= flush
;
597 static int vxlan_gro_complete(struct sock
*sk
, struct sk_buff
*skb
, int nhoff
)
599 /* Sets 'skb->inner_mac_header' since we are always called with
600 * 'skb->encapsulation' set.
602 return eth_gro_complete(skb
, nhoff
+ sizeof(struct vxlanhdr
));
605 /* Add new entry to forwarding table -- assumes lock held */
606 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
607 const u8
*mac
, union vxlan_addr
*ip
,
608 __u16 state
, __u16 flags
,
609 __be16 port
, __be32 vni
, __u32 ifindex
,
612 struct vxlan_rdst
*rd
= NULL
;
616 f
= __vxlan_find_mac(vxlan
, mac
);
618 if (flags
& NLM_F_EXCL
) {
619 netdev_dbg(vxlan
->dev
,
620 "lost race to create %pM\n", mac
);
623 if (f
->state
!= state
) {
625 f
->updated
= jiffies
;
628 if (f
->flags
!= ndm_flags
) {
629 f
->flags
= ndm_flags
;
630 f
->updated
= jiffies
;
633 if ((flags
& NLM_F_REPLACE
)) {
634 /* Only change unicasts */
635 if (!(is_multicast_ether_addr(f
->eth_addr
) ||
636 is_zero_ether_addr(f
->eth_addr
))) {
637 notify
|= vxlan_fdb_replace(f
, ip
, port
, vni
,
642 if ((flags
& NLM_F_APPEND
) &&
643 (is_multicast_ether_addr(f
->eth_addr
) ||
644 is_zero_ether_addr(f
->eth_addr
))) {
645 int rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
,
653 if (!(flags
& NLM_F_CREATE
))
656 if (vxlan
->cfg
.addrmax
&&
657 vxlan
->addrcnt
>= vxlan
->cfg
.addrmax
)
660 /* Disallow replace to add a multicast entry */
661 if ((flags
& NLM_F_REPLACE
) &&
662 (is_multicast_ether_addr(mac
) || is_zero_ether_addr(mac
)))
665 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
666 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
672 f
->flags
= ndm_flags
;
673 f
->updated
= f
->used
= jiffies
;
674 INIT_LIST_HEAD(&f
->remotes
);
675 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
677 vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
680 hlist_add_head_rcu(&f
->hlist
,
681 vxlan_fdb_head(vxlan
, mac
));
686 rd
= first_remote_rtnl(f
);
687 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_NEWNEIGH
);
693 static void vxlan_fdb_free(struct rcu_head
*head
)
695 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
696 struct vxlan_rdst
*rd
, *nd
;
698 list_for_each_entry_safe(rd
, nd
, &f
->remotes
, list
) {
699 dst_cache_destroy(&rd
->dst_cache
);
705 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
707 netdev_dbg(vxlan
->dev
,
708 "delete %pM\n", f
->eth_addr
);
711 vxlan_fdb_notify(vxlan
, f
, first_remote_rtnl(f
), RTM_DELNEIGH
);
713 hlist_del_rcu(&f
->hlist
);
714 call_rcu(&f
->rcu
, vxlan_fdb_free
);
717 static int vxlan_fdb_parse(struct nlattr
*tb
[], struct vxlan_dev
*vxlan
,
718 union vxlan_addr
*ip
, __be16
*port
, __be32
*vni
,
721 struct net
*net
= dev_net(vxlan
->dev
);
725 err
= vxlan_nla_get_addr(ip
, tb
[NDA_DST
]);
729 union vxlan_addr
*remote
= &vxlan
->default_dst
.remote_ip
;
730 if (remote
->sa
.sa_family
== AF_INET
) {
731 ip
->sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
732 ip
->sa
.sa_family
= AF_INET
;
733 #if IS_ENABLED(CONFIG_IPV6)
735 ip
->sin6
.sin6_addr
= in6addr_any
;
736 ip
->sa
.sa_family
= AF_INET6
;
742 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
744 *port
= nla_get_be16(tb
[NDA_PORT
]);
746 *port
= vxlan
->cfg
.dst_port
;
750 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
752 *vni
= cpu_to_be32(nla_get_u32(tb
[NDA_VNI
]));
754 *vni
= vxlan
->default_dst
.remote_vni
;
757 if (tb
[NDA_IFINDEX
]) {
758 struct net_device
*tdev
;
760 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
762 *ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
763 tdev
= __dev_get_by_index(net
, *ifindex
);
765 return -EADDRNOTAVAIL
;
773 /* Add static entry (via netlink) */
774 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
775 struct net_device
*dev
,
776 const unsigned char *addr
, u16 vid
, u16 flags
)
778 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
779 /* struct net *net = dev_net(vxlan->dev); */
786 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
787 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
792 if (tb
[NDA_DST
] == NULL
)
795 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
799 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
!= ip
.sa
.sa_family
)
800 return -EAFNOSUPPORT
;
802 spin_lock_bh(&vxlan
->hash_lock
);
803 err
= vxlan_fdb_create(vxlan
, addr
, &ip
, ndm
->ndm_state
, flags
,
804 port
, vni
, ifindex
, ndm
->ndm_flags
);
805 spin_unlock_bh(&vxlan
->hash_lock
);
810 /* Delete entry (via netlink) */
811 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
812 struct net_device
*dev
,
813 const unsigned char *addr
, u16 vid
)
815 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
817 struct vxlan_rdst
*rd
= NULL
;
824 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
830 spin_lock_bh(&vxlan
->hash_lock
);
831 f
= vxlan_find_mac(vxlan
, addr
);
835 if (!vxlan_addr_any(&ip
)) {
836 rd
= vxlan_fdb_find_rdst(f
, &ip
, port
, vni
, ifindex
);
843 /* remove a destination if it's not the only one on the list,
844 * otherwise destroy the fdb entry
846 if (rd
&& !list_is_singular(&f
->remotes
)) {
847 list_del_rcu(&rd
->list
);
848 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_DELNEIGH
);
853 vxlan_fdb_destroy(vxlan
, f
);
856 spin_unlock_bh(&vxlan
->hash_lock
);
861 /* Dump forwarding table */
862 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
863 struct net_device
*dev
,
864 struct net_device
*filter_dev
, int idx
)
866 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
869 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
873 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
874 struct vxlan_rdst
*rd
;
876 list_for_each_entry_rcu(rd
, &f
->remotes
, list
) {
877 if (idx
< cb
->args
[0])
880 err
= vxlan_fdb_info(skb
, vxlan
, f
,
881 NETLINK_CB(cb
->skb
).portid
,
898 /* Watch incoming packets to learn mapping between Ethernet address
899 * and Tunnel endpoint.
900 * Return true if packet is bogus and should be dropped.
902 static bool vxlan_snoop(struct net_device
*dev
,
903 union vxlan_addr
*src_ip
, const u8
*src_mac
)
905 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
908 f
= vxlan_find_mac(vxlan
, src_mac
);
910 struct vxlan_rdst
*rdst
= first_remote_rcu(f
);
912 if (likely(vxlan_addr_equal(&rdst
->remote_ip
, src_ip
)))
915 /* Don't migrate static entries, drop packets */
916 if (f
->state
& NUD_NOARP
)
921 "%pM migrated from %pIS to %pIS\n",
922 src_mac
, &rdst
->remote_ip
.sa
, &src_ip
->sa
);
924 rdst
->remote_ip
= *src_ip
;
925 f
->updated
= jiffies
;
926 vxlan_fdb_notify(vxlan
, f
, rdst
, RTM_NEWNEIGH
);
928 /* learned new entry */
929 spin_lock(&vxlan
->hash_lock
);
931 /* close off race between vxlan_flush and incoming packets */
932 if (netif_running(dev
))
933 vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
935 NLM_F_EXCL
|NLM_F_CREATE
,
937 vxlan
->default_dst
.remote_vni
,
939 spin_unlock(&vxlan
->hash_lock
);
945 /* See if multicast group is already in use by other ID */
946 static bool vxlan_group_used(struct vxlan_net
*vn
, struct vxlan_dev
*dev
)
948 struct vxlan_dev
*vxlan
;
949 unsigned short family
= dev
->default_dst
.remote_ip
.sa
.sa_family
;
951 /* The vxlan_sock is only used by dev, leaving group has
952 * no effect on other vxlan devices.
954 if (family
== AF_INET
&& dev
->vn4_sock
&&
955 atomic_read(&dev
->vn4_sock
->refcnt
) == 1)
957 #if IS_ENABLED(CONFIG_IPV6)
958 if (family
== AF_INET6
&& dev
->vn6_sock
&&
959 atomic_read(&dev
->vn6_sock
->refcnt
) == 1)
963 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
) {
964 if (!netif_running(vxlan
->dev
) || vxlan
== dev
)
967 if (family
== AF_INET
&& vxlan
->vn4_sock
!= dev
->vn4_sock
)
969 #if IS_ENABLED(CONFIG_IPV6)
970 if (family
== AF_INET6
&& vxlan
->vn6_sock
!= dev
->vn6_sock
)
974 if (!vxlan_addr_equal(&vxlan
->default_dst
.remote_ip
,
975 &dev
->default_dst
.remote_ip
))
978 if (vxlan
->default_dst
.remote_ifindex
!=
979 dev
->default_dst
.remote_ifindex
)
988 static bool __vxlan_sock_release_prep(struct vxlan_sock
*vs
)
990 struct vxlan_net
*vn
;
994 if (!atomic_dec_and_test(&vs
->refcnt
))
997 vn
= net_generic(sock_net(vs
->sock
->sk
), vxlan_net_id
);
998 spin_lock(&vn
->sock_lock
);
999 hlist_del_rcu(&vs
->hlist
);
1000 udp_tunnel_notify_del_rx_port(vs
->sock
,
1001 (vs
->flags
& VXLAN_F_GPE
) ?
1002 UDP_TUNNEL_TYPE_VXLAN_GPE
:
1003 UDP_TUNNEL_TYPE_VXLAN
);
1004 spin_unlock(&vn
->sock_lock
);
1009 static void vxlan_sock_release(struct vxlan_dev
*vxlan
)
1011 bool ipv4
= __vxlan_sock_release_prep(vxlan
->vn4_sock
);
1012 #if IS_ENABLED(CONFIG_IPV6)
1013 bool ipv6
= __vxlan_sock_release_prep(vxlan
->vn6_sock
);
1019 udp_tunnel_sock_release(vxlan
->vn4_sock
->sock
);
1020 kfree(vxlan
->vn4_sock
);
1023 #if IS_ENABLED(CONFIG_IPV6)
1025 udp_tunnel_sock_release(vxlan
->vn6_sock
->sock
);
1026 kfree(vxlan
->vn6_sock
);
1031 /* Update multicast group membership when first VNI on
1032 * multicast address is brought up
1034 static int vxlan_igmp_join(struct vxlan_dev
*vxlan
)
1037 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1038 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1041 if (ip
->sa
.sa_family
== AF_INET
) {
1042 struct ip_mreqn mreq
= {
1043 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1044 .imr_ifindex
= ifindex
,
1047 sk
= vxlan
->vn4_sock
->sock
->sk
;
1049 ret
= ip_mc_join_group(sk
, &mreq
);
1051 #if IS_ENABLED(CONFIG_IPV6)
1053 sk
= vxlan
->vn6_sock
->sock
->sk
;
1055 ret
= ipv6_stub
->ipv6_sock_mc_join(sk
, ifindex
,
1056 &ip
->sin6
.sin6_addr
);
1064 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1065 static int vxlan_igmp_leave(struct vxlan_dev
*vxlan
)
1068 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1069 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1072 if (ip
->sa
.sa_family
== AF_INET
) {
1073 struct ip_mreqn mreq
= {
1074 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1075 .imr_ifindex
= ifindex
,
1078 sk
= vxlan
->vn4_sock
->sock
->sk
;
1080 ret
= ip_mc_leave_group(sk
, &mreq
);
1082 #if IS_ENABLED(CONFIG_IPV6)
1084 sk
= vxlan
->vn6_sock
->sock
->sk
;
1086 ret
= ipv6_stub
->ipv6_sock_mc_drop(sk
, ifindex
,
1087 &ip
->sin6
.sin6_addr
);
1095 static bool vxlan_remcsum(struct vxlanhdr
*unparsed
,
1096 struct sk_buff
*skb
, u32 vxflags
)
1098 size_t start
, offset
;
1100 if (!(unparsed
->vx_flags
& VXLAN_HF_RCO
) || skb
->remcsum_offload
)
1103 start
= vxlan_rco_start(unparsed
->vx_vni
);
1104 offset
= start
+ vxlan_rco_offset(unparsed
->vx_vni
);
1106 if (!pskb_may_pull(skb
, offset
+ sizeof(u16
)))
1109 skb_remcsum_process(skb
, (void *)(vxlan_hdr(skb
) + 1), start
, offset
,
1110 !!(vxflags
& VXLAN_F_REMCSUM_NOPARTIAL
));
1112 unparsed
->vx_flags
&= ~VXLAN_HF_RCO
;
1113 unparsed
->vx_vni
&= VXLAN_VNI_MASK
;
1117 static void vxlan_parse_gbp_hdr(struct vxlanhdr
*unparsed
,
1118 struct sk_buff
*skb
, u32 vxflags
,
1119 struct vxlan_metadata
*md
)
1121 struct vxlanhdr_gbp
*gbp
= (struct vxlanhdr_gbp
*)unparsed
;
1122 struct metadata_dst
*tun_dst
;
1124 if (!(unparsed
->vx_flags
& VXLAN_HF_GBP
))
1127 md
->gbp
= ntohs(gbp
->policy_id
);
1129 tun_dst
= (struct metadata_dst
*)skb_dst(skb
);
1131 tun_dst
->u
.tun_info
.key
.tun_flags
|= TUNNEL_VXLAN_OPT
;
1132 tun_dst
->u
.tun_info
.options_len
= sizeof(*md
);
1134 if (gbp
->dont_learn
)
1135 md
->gbp
|= VXLAN_GBP_DONT_LEARN
;
1137 if (gbp
->policy_applied
)
1138 md
->gbp
|= VXLAN_GBP_POLICY_APPLIED
;
1140 /* In flow-based mode, GBP is carried in dst_metadata */
1141 if (!(vxflags
& VXLAN_F_COLLECT_METADATA
))
1142 skb
->mark
= md
->gbp
;
1144 unparsed
->vx_flags
&= ~VXLAN_GBP_USED_BITS
;
1147 static bool vxlan_parse_gpe_hdr(struct vxlanhdr
*unparsed
,
1149 struct sk_buff
*skb
, u32 vxflags
)
1151 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)unparsed
;
1153 /* Need to have Next Protocol set for interfaces in GPE mode. */
1154 if (!gpe
->np_applied
)
1156 /* "The initial version is 0. If a receiver does not support the
1157 * version indicated it MUST drop the packet.
1159 if (gpe
->version
!= 0)
1161 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1162 * processing MUST occur." However, we don't implement OAM
1163 * processing, thus drop the packet.
1168 switch (gpe
->next_protocol
) {
1169 case VXLAN_GPE_NP_IPV4
:
1170 *protocol
= htons(ETH_P_IP
);
1172 case VXLAN_GPE_NP_IPV6
:
1173 *protocol
= htons(ETH_P_IPV6
);
1175 case VXLAN_GPE_NP_ETHERNET
:
1176 *protocol
= htons(ETH_P_TEB
);
1182 unparsed
->vx_flags
&= ~VXLAN_GPE_USED_BITS
;
1186 static bool vxlan_set_mac(struct vxlan_dev
*vxlan
,
1187 struct vxlan_sock
*vs
,
1188 struct sk_buff
*skb
)
1190 union vxlan_addr saddr
;
1192 skb_reset_mac_header(skb
);
1193 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1194 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
1196 /* Ignore packet loops (and multicast echo) */
1197 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1200 /* Get address from the outer IP header */
1201 if (vxlan_get_sk_family(vs
) == AF_INET
) {
1202 saddr
.sin
.sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
1203 saddr
.sa
.sa_family
= AF_INET
;
1204 #if IS_ENABLED(CONFIG_IPV6)
1206 saddr
.sin6
.sin6_addr
= ipv6_hdr(skb
)->saddr
;
1207 saddr
.sa
.sa_family
= AF_INET6
;
1211 if ((vxlan
->flags
& VXLAN_F_LEARN
) &&
1212 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
))
1218 static bool vxlan_ecn_decapsulate(struct vxlan_sock
*vs
, void *oiph
,
1219 struct sk_buff
*skb
)
1223 if (vxlan_get_sk_family(vs
) == AF_INET
)
1224 err
= IP_ECN_decapsulate(oiph
, skb
);
1225 #if IS_ENABLED(CONFIG_IPV6)
1227 err
= IP6_ECN_decapsulate(oiph
, skb
);
1230 if (unlikely(err
) && log_ecn_error
) {
1231 if (vxlan_get_sk_family(vs
) == AF_INET
)
1232 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1233 &((struct iphdr
*)oiph
)->saddr
,
1234 ((struct iphdr
*)oiph
)->tos
);
1236 net_info_ratelimited("non-ECT from %pI6\n",
1237 &((struct ipv6hdr
*)oiph
)->saddr
);
1242 /* Callback from net/ipv4/udp.c to receive packets */
1243 static int vxlan_rcv(struct sock
*sk
, struct sk_buff
*skb
)
1245 struct pcpu_sw_netstats
*stats
;
1246 struct vxlan_dev
*vxlan
;
1247 struct vxlan_sock
*vs
;
1248 struct vxlanhdr unparsed
;
1249 struct vxlan_metadata _md
;
1250 struct vxlan_metadata
*md
= &_md
;
1251 __be16 protocol
= htons(ETH_P_TEB
);
1252 bool raw_proto
= false;
1255 /* Need UDP and VXLAN header to be present */
1256 if (!pskb_may_pull(skb
, VXLAN_HLEN
))
1259 unparsed
= *vxlan_hdr(skb
);
1260 /* VNI flag always required to be set */
1261 if (!(unparsed
.vx_flags
& VXLAN_HF_VNI
)) {
1262 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
1263 ntohl(vxlan_hdr(skb
)->vx_flags
),
1264 ntohl(vxlan_hdr(skb
)->vx_vni
));
1265 /* Return non vxlan pkt */
1268 unparsed
.vx_flags
&= ~VXLAN_HF_VNI
;
1269 unparsed
.vx_vni
&= ~VXLAN_VNI_MASK
;
1271 vs
= rcu_dereference_sk_user_data(sk
);
1275 vxlan
= vxlan_vs_find_vni(vs
, vxlan_vni(vxlan_hdr(skb
)->vx_vni
));
1279 /* For backwards compatibility, only allow reserved fields to be
1280 * used by VXLAN extensions if explicitly requested.
1282 if (vs
->flags
& VXLAN_F_GPE
) {
1283 if (!vxlan_parse_gpe_hdr(&unparsed
, &protocol
, skb
, vs
->flags
))
1288 if (__iptunnel_pull_header(skb
, VXLAN_HLEN
, protocol
, raw_proto
,
1289 !net_eq(vxlan
->net
, dev_net(vxlan
->dev
))))
1292 if (vxlan_collect_metadata(vs
)) {
1293 __be32 vni
= vxlan_vni(vxlan_hdr(skb
)->vx_vni
);
1294 struct metadata_dst
*tun_dst
;
1296 tun_dst
= udp_tun_rx_dst(skb
, vxlan_get_sk_family(vs
), TUNNEL_KEY
,
1297 vxlan_vni_to_tun_id(vni
), sizeof(*md
));
1302 md
= ip_tunnel_info_opts(&tun_dst
->u
.tun_info
);
1304 skb_dst_set(skb
, (struct dst_entry
*)tun_dst
);
1306 memset(md
, 0, sizeof(*md
));
1309 if (vs
->flags
& VXLAN_F_REMCSUM_RX
)
1310 if (!vxlan_remcsum(&unparsed
, skb
, vs
->flags
))
1312 if (vs
->flags
& VXLAN_F_GBP
)
1313 vxlan_parse_gbp_hdr(&unparsed
, skb
, vs
->flags
, md
);
1314 /* Note that GBP and GPE can never be active together. This is
1315 * ensured in vxlan_dev_configure.
1318 if (unparsed
.vx_flags
|| unparsed
.vx_vni
) {
1319 /* If there are any unprocessed flags remaining treat
1320 * this as a malformed packet. This behavior diverges from
1321 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1322 * in reserved fields are to be ignored. The approach here
1323 * maintains compatibility with previous stack code, and also
1324 * is more robust and provides a little more security in
1325 * adding extensions to VXLAN.
1331 if (!vxlan_set_mac(vxlan
, vs
, skb
))
1334 skb_reset_mac_header(skb
);
1335 skb
->dev
= vxlan
->dev
;
1336 skb
->pkt_type
= PACKET_HOST
;
1339 oiph
= skb_network_header(skb
);
1340 skb_reset_network_header(skb
);
1342 if (!vxlan_ecn_decapsulate(vs
, oiph
, skb
)) {
1343 ++vxlan
->dev
->stats
.rx_frame_errors
;
1344 ++vxlan
->dev
->stats
.rx_errors
;
1348 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1349 u64_stats_update_begin(&stats
->syncp
);
1350 stats
->rx_packets
++;
1351 stats
->rx_bytes
+= skb
->len
;
1352 u64_stats_update_end(&stats
->syncp
);
1354 gro_cells_receive(&vxlan
->gro_cells
, skb
);
1358 /* Consume bad packet */
1363 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1365 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1366 struct arphdr
*parp
;
1369 struct neighbour
*n
;
1371 if (dev
->flags
& IFF_NOARP
)
1374 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1375 dev
->stats
.tx_dropped
++;
1378 parp
= arp_hdr(skb
);
1380 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1381 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1382 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1383 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1384 parp
->ar_hln
!= dev
->addr_len
||
1387 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1389 arpptr
+= dev
->addr_len
; /* sha */
1390 memcpy(&sip
, arpptr
, sizeof(sip
));
1391 arpptr
+= sizeof(sip
);
1392 arpptr
+= dev
->addr_len
; /* tha */
1393 memcpy(&tip
, arpptr
, sizeof(tip
));
1395 if (ipv4_is_loopback(tip
) ||
1396 ipv4_is_multicast(tip
))
1399 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1402 struct vxlan_fdb
*f
;
1403 struct sk_buff
*reply
;
1405 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1410 f
= vxlan_find_mac(vxlan
, n
->ha
);
1411 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1412 /* bridge-local neighbor */
1417 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1425 skb_reset_mac_header(reply
);
1426 __skb_pull(reply
, skb_network_offset(reply
));
1427 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1428 reply
->pkt_type
= PACKET_HOST
;
1430 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1431 dev
->stats
.rx_dropped
++;
1432 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1433 union vxlan_addr ipa
= {
1434 .sin
.sin_addr
.s_addr
= tip
,
1435 .sin
.sin_family
= AF_INET
,
1438 vxlan_ip_miss(dev
, &ipa
);
1442 return NETDEV_TX_OK
;
1445 #if IS_ENABLED(CONFIG_IPV6)
1446 static struct sk_buff
*vxlan_na_create(struct sk_buff
*request
,
1447 struct neighbour
*n
, bool isrouter
)
1449 struct net_device
*dev
= request
->dev
;
1450 struct sk_buff
*reply
;
1451 struct nd_msg
*ns
, *na
;
1452 struct ipv6hdr
*pip6
;
1454 int na_olen
= 8; /* opt hdr + ETH_ALEN for target */
1461 len
= LL_RESERVED_SPACE(dev
) + sizeof(struct ipv6hdr
) +
1462 sizeof(*na
) + na_olen
+ dev
->needed_tailroom
;
1463 reply
= alloc_skb(len
, GFP_ATOMIC
);
1467 reply
->protocol
= htons(ETH_P_IPV6
);
1469 skb_reserve(reply
, LL_RESERVED_SPACE(request
->dev
));
1470 skb_push(reply
, sizeof(struct ethhdr
));
1471 skb_reset_mac_header(reply
);
1473 ns
= (struct nd_msg
*)skb_transport_header(request
);
1475 daddr
= eth_hdr(request
)->h_source
;
1476 ns_olen
= request
->len
- skb_transport_offset(request
) - sizeof(*ns
);
1477 for (i
= 0; i
< ns_olen
-1; i
+= (ns
->opt
[i
+1]<<3)) {
1478 if (ns
->opt
[i
] == ND_OPT_SOURCE_LL_ADDR
) {
1479 daddr
= ns
->opt
+ i
+ sizeof(struct nd_opt_hdr
);
1484 /* Ethernet header */
1485 ether_addr_copy(eth_hdr(reply
)->h_dest
, daddr
);
1486 ether_addr_copy(eth_hdr(reply
)->h_source
, n
->ha
);
1487 eth_hdr(reply
)->h_proto
= htons(ETH_P_IPV6
);
1488 reply
->protocol
= htons(ETH_P_IPV6
);
1490 skb_pull(reply
, sizeof(struct ethhdr
));
1491 skb_reset_network_header(reply
);
1492 skb_put(reply
, sizeof(struct ipv6hdr
));
1496 pip6
= ipv6_hdr(reply
);
1497 memset(pip6
, 0, sizeof(struct ipv6hdr
));
1499 pip6
->priority
= ipv6_hdr(request
)->priority
;
1500 pip6
->nexthdr
= IPPROTO_ICMPV6
;
1501 pip6
->hop_limit
= 255;
1502 pip6
->daddr
= ipv6_hdr(request
)->saddr
;
1503 pip6
->saddr
= *(struct in6_addr
*)n
->primary_key
;
1505 skb_pull(reply
, sizeof(struct ipv6hdr
));
1506 skb_reset_transport_header(reply
);
1508 na
= (struct nd_msg
*)skb_put(reply
, sizeof(*na
) + na_olen
);
1510 /* Neighbor Advertisement */
1511 memset(na
, 0, sizeof(*na
)+na_olen
);
1512 na
->icmph
.icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
1513 na
->icmph
.icmp6_router
= isrouter
;
1514 na
->icmph
.icmp6_override
= 1;
1515 na
->icmph
.icmp6_solicited
= 1;
1516 na
->target
= ns
->target
;
1517 ether_addr_copy(&na
->opt
[2], n
->ha
);
1518 na
->opt
[0] = ND_OPT_TARGET_LL_ADDR
;
1519 na
->opt
[1] = na_olen
>> 3;
1521 na
->icmph
.icmp6_cksum
= csum_ipv6_magic(&pip6
->saddr
,
1522 &pip6
->daddr
, sizeof(*na
)+na_olen
, IPPROTO_ICMPV6
,
1523 csum_partial(na
, sizeof(*na
)+na_olen
, 0));
1525 pip6
->payload_len
= htons(sizeof(*na
)+na_olen
);
1527 skb_push(reply
, sizeof(struct ipv6hdr
));
1529 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1534 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1536 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1538 const struct ipv6hdr
*iphdr
;
1539 const struct in6_addr
*saddr
, *daddr
;
1540 struct neighbour
*n
;
1541 struct inet6_dev
*in6_dev
;
1543 in6_dev
= __in6_dev_get(dev
);
1547 iphdr
= ipv6_hdr(skb
);
1548 saddr
= &iphdr
->saddr
;
1549 daddr
= &iphdr
->daddr
;
1551 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1552 if (msg
->icmph
.icmp6_code
!= 0 ||
1553 msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
1556 if (ipv6_addr_loopback(daddr
) ||
1557 ipv6_addr_is_multicast(&msg
->target
))
1560 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1563 struct vxlan_fdb
*f
;
1564 struct sk_buff
*reply
;
1566 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1571 f
= vxlan_find_mac(vxlan
, n
->ha
);
1572 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1573 /* bridge-local neighbor */
1578 reply
= vxlan_na_create(skb
, n
,
1579 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1586 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1587 dev
->stats
.rx_dropped
++;
1589 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1590 union vxlan_addr ipa
= {
1591 .sin6
.sin6_addr
= msg
->target
,
1592 .sin6
.sin6_family
= AF_INET6
,
1595 vxlan_ip_miss(dev
, &ipa
);
1600 return NETDEV_TX_OK
;
1604 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1606 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1607 struct neighbour
*n
;
1609 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1613 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1618 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1621 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1622 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1623 union vxlan_addr ipa
= {
1624 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1625 .sin
.sin_family
= AF_INET
,
1628 vxlan_ip_miss(dev
, &ipa
);
1634 #if IS_ENABLED(CONFIG_IPV6)
1637 struct ipv6hdr
*pip6
;
1639 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1641 pip6
= ipv6_hdr(skb
);
1642 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1643 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1644 union vxlan_addr ipa
= {
1645 .sin6
.sin6_addr
= pip6
->daddr
,
1646 .sin6
.sin6_family
= AF_INET6
,
1649 vxlan_ip_miss(dev
, &ipa
);
1663 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1665 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1667 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1676 static void vxlan_build_gbp_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1677 struct vxlan_metadata
*md
)
1679 struct vxlanhdr_gbp
*gbp
;
1684 gbp
= (struct vxlanhdr_gbp
*)vxh
;
1685 vxh
->vx_flags
|= VXLAN_HF_GBP
;
1687 if (md
->gbp
& VXLAN_GBP_DONT_LEARN
)
1688 gbp
->dont_learn
= 1;
1690 if (md
->gbp
& VXLAN_GBP_POLICY_APPLIED
)
1691 gbp
->policy_applied
= 1;
1693 gbp
->policy_id
= htons(md
->gbp
& VXLAN_GBP_ID_MASK
);
1696 static int vxlan_build_gpe_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1699 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)vxh
;
1701 gpe
->np_applied
= 1;
1704 case htons(ETH_P_IP
):
1705 gpe
->next_protocol
= VXLAN_GPE_NP_IPV4
;
1707 case htons(ETH_P_IPV6
):
1708 gpe
->next_protocol
= VXLAN_GPE_NP_IPV6
;
1710 case htons(ETH_P_TEB
):
1711 gpe
->next_protocol
= VXLAN_GPE_NP_ETHERNET
;
1714 return -EPFNOSUPPORT
;
1717 static int vxlan_build_skb(struct sk_buff
*skb
, struct dst_entry
*dst
,
1718 int iphdr_len
, __be32 vni
,
1719 struct vxlan_metadata
*md
, u32 vxflags
,
1722 struct vxlanhdr
*vxh
;
1725 int type
= udp_sum
? SKB_GSO_UDP_TUNNEL_CSUM
: SKB_GSO_UDP_TUNNEL
;
1726 __be16 inner_protocol
= htons(ETH_P_TEB
);
1728 if ((vxflags
& VXLAN_F_REMCSUM_TX
) &&
1729 skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1730 int csum_start
= skb_checksum_start_offset(skb
);
1732 if (csum_start
<= VXLAN_MAX_REMCSUM_START
&&
1733 !(csum_start
& VXLAN_RCO_SHIFT_MASK
) &&
1734 (skb
->csum_offset
== offsetof(struct udphdr
, check
) ||
1735 skb
->csum_offset
== offsetof(struct tcphdr
, check
)))
1736 type
|= SKB_GSO_TUNNEL_REMCSUM
;
1739 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1740 + VXLAN_HLEN
+ iphdr_len
1741 + (skb_vlan_tag_present(skb
) ? VLAN_HLEN
: 0);
1743 /* Need space for new headers (invalidates iph ptr) */
1744 err
= skb_cow_head(skb
, min_headroom
);
1748 skb
= vlan_hwaccel_push_inside(skb
);
1752 err
= iptunnel_handle_offloads(skb
, type
);
1756 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1757 vxh
->vx_flags
= VXLAN_HF_VNI
;
1758 vxh
->vx_vni
= vxlan_vni_field(vni
);
1760 if (type
& SKB_GSO_TUNNEL_REMCSUM
) {
1763 start
= skb_checksum_start_offset(skb
) - sizeof(struct vxlanhdr
);
1764 vxh
->vx_vni
|= vxlan_compute_rco(start
, skb
->csum_offset
);
1765 vxh
->vx_flags
|= VXLAN_HF_RCO
;
1767 if (!skb_is_gso(skb
)) {
1768 skb
->ip_summed
= CHECKSUM_NONE
;
1769 skb
->encapsulation
= 0;
1773 if (vxflags
& VXLAN_F_GBP
)
1774 vxlan_build_gbp_hdr(vxh
, vxflags
, md
);
1775 if (vxflags
& VXLAN_F_GPE
) {
1776 err
= vxlan_build_gpe_hdr(vxh
, vxflags
, skb
->protocol
);
1779 inner_protocol
= skb
->protocol
;
1782 skb_set_inner_protocol(skb
, inner_protocol
);
1790 static struct rtable
*vxlan_get_route(struct vxlan_dev
*vxlan
,
1791 struct sk_buff
*skb
, int oif
, u8 tos
,
1792 __be32 daddr
, __be32
*saddr
,
1793 struct dst_cache
*dst_cache
,
1794 const struct ip_tunnel_info
*info
)
1796 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
1797 struct rtable
*rt
= NULL
;
1803 rt
= dst_cache_get_ip4(dst_cache
, saddr
);
1808 memset(&fl4
, 0, sizeof(fl4
));
1809 fl4
.flowi4_oif
= oif
;
1810 fl4
.flowi4_tos
= RT_TOS(tos
);
1811 fl4
.flowi4_mark
= skb
->mark
;
1812 fl4
.flowi4_proto
= IPPROTO_UDP
;
1816 rt
= ip_route_output_key(vxlan
->net
, &fl4
);
1820 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
.saddr
);
1825 #if IS_ENABLED(CONFIG_IPV6)
1826 static struct dst_entry
*vxlan6_get_route(struct vxlan_dev
*vxlan
,
1827 struct sk_buff
*skb
, int oif
, u8 tos
,
1829 const struct in6_addr
*daddr
,
1830 struct in6_addr
*saddr
,
1831 struct dst_cache
*dst_cache
,
1832 const struct ip_tunnel_info
*info
)
1834 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
1835 struct dst_entry
*ndst
;
1842 ndst
= dst_cache_get_ip6(dst_cache
, saddr
);
1847 memset(&fl6
, 0, sizeof(fl6
));
1848 fl6
.flowi6_oif
= oif
;
1851 fl6
.flowlabel
= ip6_make_flowinfo(RT_TOS(tos
), label
);
1852 fl6
.flowi6_mark
= skb
->mark
;
1853 fl6
.flowi6_proto
= IPPROTO_UDP
;
1855 err
= ipv6_stub
->ipv6_dst_lookup(vxlan
->net
,
1856 vxlan
->vn6_sock
->sock
->sk
,
1859 return ERR_PTR(err
);
1863 dst_cache_set_ip6(dst_cache
, ndst
, saddr
);
1868 /* Bypass encapsulation if the destination is local */
1869 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
1870 struct vxlan_dev
*dst_vxlan
)
1872 struct pcpu_sw_netstats
*tx_stats
, *rx_stats
;
1873 union vxlan_addr loopback
;
1874 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
1875 struct net_device
*dev
= skb
->dev
;
1878 tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
1879 rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
1880 skb
->pkt_type
= PACKET_HOST
;
1881 skb
->encapsulation
= 0;
1882 skb
->dev
= dst_vxlan
->dev
;
1883 __skb_pull(skb
, skb_network_offset(skb
));
1885 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1886 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1887 loopback
.sa
.sa_family
= AF_INET
;
1888 #if IS_ENABLED(CONFIG_IPV6)
1890 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
1891 loopback
.sa
.sa_family
= AF_INET6
;
1895 if (dst_vxlan
->flags
& VXLAN_F_LEARN
)
1896 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
);
1898 u64_stats_update_begin(&tx_stats
->syncp
);
1899 tx_stats
->tx_packets
++;
1900 tx_stats
->tx_bytes
+= len
;
1901 u64_stats_update_end(&tx_stats
->syncp
);
1903 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
1904 u64_stats_update_begin(&rx_stats
->syncp
);
1905 rx_stats
->rx_packets
++;
1906 rx_stats
->rx_bytes
+= len
;
1907 u64_stats_update_end(&rx_stats
->syncp
);
1909 dev
->stats
.rx_dropped
++;
1913 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
1914 struct vxlan_rdst
*rdst
, bool did_rsc
)
1916 struct dst_cache
*dst_cache
;
1917 struct ip_tunnel_info
*info
;
1918 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1920 struct rtable
*rt
= NULL
;
1921 const struct iphdr
*old_iph
;
1922 union vxlan_addr
*dst
;
1923 union vxlan_addr remote_ip
, local_ip
;
1924 union vxlan_addr
*src
;
1925 struct vxlan_metadata _md
;
1926 struct vxlan_metadata
*md
= &_md
;
1927 __be16 src_port
= 0, dst_port
;
1932 u32 flags
= vxlan
->flags
;
1933 bool udp_sum
= false;
1934 bool xnet
= !net_eq(vxlan
->net
, dev_net(vxlan
->dev
));
1936 info
= skb_tunnel_info(skb
);
1939 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->cfg
.dst_port
;
1940 vni
= rdst
->remote_vni
;
1941 dst
= &rdst
->remote_ip
;
1942 src
= &vxlan
->cfg
.saddr
;
1943 dst_cache
= &rdst
->dst_cache
;
1946 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1950 dst_port
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
1951 vni
= vxlan_tun_id_to_vni(info
->key
.tun_id
);
1952 remote_ip
.sa
.sa_family
= ip_tunnel_info_af(info
);
1953 if (remote_ip
.sa
.sa_family
== AF_INET
) {
1954 remote_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.dst
;
1955 local_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.src
;
1957 remote_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.dst
;
1958 local_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.src
;
1962 dst_cache
= &info
->dst_cache
;
1965 if (vxlan_addr_any(dst
)) {
1967 /* short-circuited back to local bridge */
1968 vxlan_encap_bypass(skb
, vxlan
, vxlan
);
1974 old_iph
= ip_hdr(skb
);
1976 ttl
= vxlan
->cfg
.ttl
;
1977 if (!ttl
&& vxlan_addr_multicast(dst
))
1980 tos
= vxlan
->cfg
.tos
;
1982 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
1984 label
= vxlan
->cfg
.label
;
1985 src_port
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
1986 vxlan
->cfg
.port_max
, true);
1989 ttl
= info
->key
.ttl
;
1990 tos
= info
->key
.tos
;
1991 label
= info
->key
.label
;
1992 udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
1994 if (info
->options_len
)
1995 md
= ip_tunnel_info_opts(info
);
1997 md
->gbp
= skb
->mark
;
2000 if (dst
->sa
.sa_family
== AF_INET
) {
2001 if (!vxlan
->vn4_sock
)
2003 sk
= vxlan
->vn4_sock
->sock
->sk
;
2005 rt
= vxlan_get_route(vxlan
, skb
,
2006 rdst
? rdst
->remote_ifindex
: 0, tos
,
2007 dst
->sin
.sin_addr
.s_addr
,
2008 &src
->sin
.sin_addr
.s_addr
,
2011 netdev_dbg(dev
, "no route to %pI4\n",
2012 &dst
->sin
.sin_addr
.s_addr
);
2013 dev
->stats
.tx_carrier_errors
++;
2017 if (rt
->dst
.dev
== dev
) {
2018 netdev_dbg(dev
, "circular route to %pI4\n",
2019 &dst
->sin
.sin_addr
.s_addr
);
2020 dev
->stats
.collisions
++;
2024 /* Bypass encapsulation if the destination is local */
2025 if (!info
&& rt
->rt_flags
& RTCF_LOCAL
&&
2026 !(rt
->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
2027 struct vxlan_dev
*dst_vxlan
;
2030 dst_vxlan
= vxlan_find_vni(vxlan
->net
, vni
,
2031 dst
->sa
.sa_family
, dst_port
,
2035 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
2040 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM_TX
);
2041 else if (info
->key
.tun_flags
& TUNNEL_DONT_FRAGMENT
)
2044 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2045 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
2046 err
= vxlan_build_skb(skb
, &rt
->dst
, sizeof(struct iphdr
),
2047 vni
, md
, flags
, udp_sum
);
2051 udp_tunnel_xmit_skb(rt
, sk
, skb
, src
->sin
.sin_addr
.s_addr
,
2052 dst
->sin
.sin_addr
.s_addr
, tos
, ttl
, df
,
2053 src_port
, dst_port
, xnet
, !udp_sum
);
2054 #if IS_ENABLED(CONFIG_IPV6)
2056 struct dst_entry
*ndst
;
2059 if (!vxlan
->vn6_sock
)
2061 sk
= vxlan
->vn6_sock
->sock
->sk
;
2063 ndst
= vxlan6_get_route(vxlan
, skb
,
2064 rdst
? rdst
->remote_ifindex
: 0, tos
,
2065 label
, &dst
->sin6
.sin6_addr
,
2066 &src
->sin6
.sin6_addr
,
2069 netdev_dbg(dev
, "no route to %pI6\n",
2070 &dst
->sin6
.sin6_addr
);
2071 dev
->stats
.tx_carrier_errors
++;
2075 if (ndst
->dev
== dev
) {
2076 netdev_dbg(dev
, "circular route to %pI6\n",
2077 &dst
->sin6
.sin6_addr
);
2079 dev
->stats
.collisions
++;
2083 /* Bypass encapsulation if the destination is local */
2084 rt6i_flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
2085 if (!info
&& rt6i_flags
& RTF_LOCAL
&&
2086 !(rt6i_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
2087 struct vxlan_dev
*dst_vxlan
;
2090 dst_vxlan
= vxlan_find_vni(vxlan
->net
, vni
,
2091 dst
->sa
.sa_family
, dst_port
,
2095 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
2100 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
);
2102 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2103 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
2104 skb_scrub_packet(skb
, xnet
);
2105 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct ipv6hdr
),
2106 vni
, md
, flags
, udp_sum
);
2111 udp_tunnel6_xmit_skb(ndst
, sk
, skb
, dev
,
2112 &src
->sin6
.sin6_addr
,
2113 &dst
->sin6
.sin6_addr
, tos
, ttl
,
2114 label
, src_port
, dst_port
, !udp_sum
);
2121 dev
->stats
.tx_dropped
++;
2125 /* skb is already freed. */
2130 dev
->stats
.tx_errors
++;
2135 /* Transmit local packets over Vxlan
2137 * Outer IP header inherits ECN and DF from inner header.
2138 * Outer UDP destination is the VXLAN assigned port.
2139 * source port is based on hash of flow
2141 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
2143 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2144 const struct ip_tunnel_info
*info
;
2146 bool did_rsc
= false;
2147 struct vxlan_rdst
*rdst
, *fdst
= NULL
;
2148 struct vxlan_fdb
*f
;
2150 info
= skb_tunnel_info(skb
);
2152 skb_reset_mac_header(skb
);
2154 if (vxlan
->flags
& VXLAN_F_COLLECT_METADATA
) {
2155 if (info
&& info
->mode
& IP_TUNNEL_INFO_TX
)
2156 vxlan_xmit_one(skb
, dev
, NULL
, false);
2159 return NETDEV_TX_OK
;
2162 if (vxlan
->flags
& VXLAN_F_PROXY
) {
2164 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
2165 return arp_reduce(dev
, skb
);
2166 #if IS_ENABLED(CONFIG_IPV6)
2167 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
&&
2168 pskb_may_pull(skb
, sizeof(struct ipv6hdr
)
2169 + sizeof(struct nd_msg
)) &&
2170 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
2173 msg
= (struct nd_msg
*)skb_transport_header(skb
);
2174 if (msg
->icmph
.icmp6_code
== 0 &&
2175 msg
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
)
2176 return neigh_reduce(dev
, skb
);
2182 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
2185 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->flags
& VXLAN_F_RSC
) &&
2186 (ntohs(eth
->h_proto
) == ETH_P_IP
||
2187 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
2188 did_rsc
= route_shortcircuit(dev
, skb
);
2190 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
2194 f
= vxlan_find_mac(vxlan
, all_zeros_mac
);
2196 if ((vxlan
->flags
& VXLAN_F_L2MISS
) &&
2197 !is_multicast_ether_addr(eth
->h_dest
))
2198 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
2200 dev
->stats
.tx_dropped
++;
2202 return NETDEV_TX_OK
;
2206 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
2207 struct sk_buff
*skb1
;
2213 skb1
= skb_clone(skb
, GFP_ATOMIC
);
2215 vxlan_xmit_one(skb1
, dev
, rdst
, did_rsc
);
2219 vxlan_xmit_one(skb
, dev
, fdst
, did_rsc
);
2222 return NETDEV_TX_OK
;
2225 /* Walk the forwarding table and purge stale entries */
2226 static void vxlan_cleanup(unsigned long arg
)
2228 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
2229 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
2232 if (!netif_running(vxlan
->dev
))
2235 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2236 struct hlist_node
*p
, *n
;
2238 spin_lock_bh(&vxlan
->hash_lock
);
2239 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2241 = container_of(p
, struct vxlan_fdb
, hlist
);
2242 unsigned long timeout
;
2244 if (f
->state
& NUD_PERMANENT
)
2247 timeout
= f
->used
+ vxlan
->cfg
.age_interval
* HZ
;
2248 if (time_before_eq(timeout
, jiffies
)) {
2249 netdev_dbg(vxlan
->dev
,
2250 "garbage collect %pM\n",
2252 f
->state
= NUD_STALE
;
2253 vxlan_fdb_destroy(vxlan
, f
);
2254 } else if (time_before(timeout
, next_timer
))
2255 next_timer
= timeout
;
2257 spin_unlock_bh(&vxlan
->hash_lock
);
2260 mod_timer(&vxlan
->age_timer
, next_timer
);
2263 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
)
2265 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2266 __be32 vni
= vxlan
->default_dst
.remote_vni
;
2268 spin_lock(&vn
->sock_lock
);
2269 hlist_add_head_rcu(&vxlan
->hlist
, vni_head(vs
, vni
));
2270 spin_unlock(&vn
->sock_lock
);
2273 /* Setup stats when device is created */
2274 static int vxlan_init(struct net_device
*dev
)
2276 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
2283 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
)
2285 struct vxlan_fdb
*f
;
2287 spin_lock_bh(&vxlan
->hash_lock
);
2288 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
);
2290 vxlan_fdb_destroy(vxlan
, f
);
2291 spin_unlock_bh(&vxlan
->hash_lock
);
2294 static void vxlan_uninit(struct net_device
*dev
)
2296 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2298 vxlan_fdb_delete_default(vxlan
);
2300 free_percpu(dev
->tstats
);
2303 /* Start ageing timer and join group when device is brought up */
2304 static int vxlan_open(struct net_device
*dev
)
2306 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2309 ret
= vxlan_sock_add(vxlan
);
2313 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
)) {
2314 ret
= vxlan_igmp_join(vxlan
);
2315 if (ret
== -EADDRINUSE
)
2318 vxlan_sock_release(vxlan
);
2323 if (vxlan
->cfg
.age_interval
)
2324 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2329 /* Purge the forwarding table */
2330 static void vxlan_flush(struct vxlan_dev
*vxlan
)
2334 spin_lock_bh(&vxlan
->hash_lock
);
2335 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2336 struct hlist_node
*p
, *n
;
2337 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2339 = container_of(p
, struct vxlan_fdb
, hlist
);
2340 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2341 if (!is_zero_ether_addr(f
->eth_addr
))
2342 vxlan_fdb_destroy(vxlan
, f
);
2345 spin_unlock_bh(&vxlan
->hash_lock
);
2348 /* Cleanup timer and forwarding table on shutdown */
2349 static int vxlan_stop(struct net_device
*dev
)
2351 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2352 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2355 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2356 !vxlan_group_used(vn
, vxlan
))
2357 ret
= vxlan_igmp_leave(vxlan
);
2359 del_timer_sync(&vxlan
->age_timer
);
2362 vxlan_sock_release(vxlan
);
2367 /* Stub, nothing needs to be done. */
2368 static void vxlan_set_multicast_list(struct net_device
*dev
)
2372 static int __vxlan_change_mtu(struct net_device
*dev
,
2373 struct net_device
*lowerdev
,
2374 struct vxlan_rdst
*dst
, int new_mtu
, bool strict
)
2376 int max_mtu
= IP_MAX_MTU
;
2379 max_mtu
= lowerdev
->mtu
;
2381 if (dst
->remote_ip
.sa
.sa_family
== AF_INET6
)
2382 max_mtu
-= VXLAN6_HEADROOM
;
2384 max_mtu
-= VXLAN_HEADROOM
;
2389 if (new_mtu
> max_mtu
) {
2400 static int vxlan_change_mtu(struct net_device
*dev
, int new_mtu
)
2402 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2403 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2404 struct net_device
*lowerdev
= __dev_get_by_index(vxlan
->net
,
2405 dst
->remote_ifindex
);
2406 return __vxlan_change_mtu(dev
, lowerdev
, dst
, new_mtu
, true);
2409 static int vxlan_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
2411 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2412 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
2413 __be16 sport
, dport
;
2415 sport
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2416 vxlan
->cfg
.port_max
, true);
2417 dport
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2419 if (ip_tunnel_info_af(info
) == AF_INET
) {
2422 if (!vxlan
->vn4_sock
)
2424 rt
= vxlan_get_route(vxlan
, skb
, 0, info
->key
.tos
,
2425 info
->key
.u
.ipv4
.dst
,
2426 &info
->key
.u
.ipv4
.src
, NULL
, info
);
2431 #if IS_ENABLED(CONFIG_IPV6)
2432 struct dst_entry
*ndst
;
2434 if (!vxlan
->vn6_sock
)
2436 ndst
= vxlan6_get_route(vxlan
, skb
, 0, info
->key
.tos
,
2437 info
->key
.label
, &info
->key
.u
.ipv6
.dst
,
2438 &info
->key
.u
.ipv6
.src
, NULL
, info
);
2440 return PTR_ERR(ndst
);
2442 #else /* !CONFIG_IPV6 */
2443 return -EPFNOSUPPORT
;
2446 info
->key
.tp_src
= sport
;
2447 info
->key
.tp_dst
= dport
;
2451 static const struct net_device_ops vxlan_netdev_ether_ops
= {
2452 .ndo_init
= vxlan_init
,
2453 .ndo_uninit
= vxlan_uninit
,
2454 .ndo_open
= vxlan_open
,
2455 .ndo_stop
= vxlan_stop
,
2456 .ndo_start_xmit
= vxlan_xmit
,
2457 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2458 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2459 .ndo_change_mtu
= vxlan_change_mtu
,
2460 .ndo_validate_addr
= eth_validate_addr
,
2461 .ndo_set_mac_address
= eth_mac_addr
,
2462 .ndo_fdb_add
= vxlan_fdb_add
,
2463 .ndo_fdb_del
= vxlan_fdb_delete
,
2464 .ndo_fdb_dump
= vxlan_fdb_dump
,
2465 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2468 static const struct net_device_ops vxlan_netdev_raw_ops
= {
2469 .ndo_init
= vxlan_init
,
2470 .ndo_uninit
= vxlan_uninit
,
2471 .ndo_open
= vxlan_open
,
2472 .ndo_stop
= vxlan_stop
,
2473 .ndo_start_xmit
= vxlan_xmit
,
2474 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2475 .ndo_change_mtu
= vxlan_change_mtu
,
2476 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2479 /* Info for udev, that this is a virtual tunnel endpoint */
2480 static struct device_type vxlan_type
= {
2484 /* Calls the ndo_udp_tunnel_add of the caller in order to
2485 * supply the listening VXLAN udp ports. Callers are expected
2486 * to implement the ndo_udp_tunnel_add.
2488 static void vxlan_push_rx_ports(struct net_device
*dev
)
2490 struct vxlan_sock
*vs
;
2491 struct net
*net
= dev_net(dev
);
2492 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2495 spin_lock(&vn
->sock_lock
);
2496 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2497 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
)
2498 udp_tunnel_push_rx_port(dev
, vs
->sock
,
2499 (vs
->flags
& VXLAN_F_GPE
) ?
2500 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2501 UDP_TUNNEL_TYPE_VXLAN
);
2503 spin_unlock(&vn
->sock_lock
);
2506 /* Initialize the device structure. */
2507 static void vxlan_setup(struct net_device
*dev
)
2509 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2512 eth_hw_addr_random(dev
);
2515 dev
->destructor
= free_netdev
;
2516 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2518 dev
->features
|= NETIF_F_LLTX
;
2519 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2520 dev
->features
|= NETIF_F_RXCSUM
;
2521 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2523 dev
->vlan_features
= dev
->features
;
2524 dev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2525 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2526 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2527 dev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2528 netif_keep_dst(dev
);
2529 dev
->priv_flags
|= IFF_NO_QUEUE
;
2531 INIT_LIST_HEAD(&vxlan
->next
);
2532 spin_lock_init(&vxlan
->hash_lock
);
2534 init_timer_deferrable(&vxlan
->age_timer
);
2535 vxlan
->age_timer
.function
= vxlan_cleanup
;
2536 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2538 vxlan
->cfg
.dst_port
= htons(vxlan_port
);
2542 gro_cells_init(&vxlan
->gro_cells
, dev
);
2544 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2545 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2548 static void vxlan_ether_setup(struct net_device
*dev
)
2550 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
2551 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2552 dev
->netdev_ops
= &vxlan_netdev_ether_ops
;
2555 static void vxlan_raw_setup(struct net_device
*dev
)
2557 dev
->header_ops
= NULL
;
2558 dev
->type
= ARPHRD_NONE
;
2559 dev
->hard_header_len
= 0;
2561 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
2562 dev
->netdev_ops
= &vxlan_netdev_raw_ops
;
2565 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2566 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2567 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2568 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2569 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2570 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2571 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2572 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2573 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2574 [IFLA_VXLAN_LABEL
] = { .type
= NLA_U32
},
2575 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2576 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2577 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2578 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2579 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2580 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2581 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2582 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2583 [IFLA_VXLAN_COLLECT_METADATA
] = { .type
= NLA_U8
},
2584 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2585 [IFLA_VXLAN_UDP_CSUM
] = { .type
= NLA_U8
},
2586 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
2587 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
2588 [IFLA_VXLAN_REMCSUM_TX
] = { .type
= NLA_U8
},
2589 [IFLA_VXLAN_REMCSUM_RX
] = { .type
= NLA_U8
},
2590 [IFLA_VXLAN_GBP
] = { .type
= NLA_FLAG
, },
2591 [IFLA_VXLAN_GPE
] = { .type
= NLA_FLAG
, },
2592 [IFLA_VXLAN_REMCSUM_NOPARTIAL
] = { .type
= NLA_FLAG
},
2595 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
2597 if (tb
[IFLA_ADDRESS
]) {
2598 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2599 pr_debug("invalid link address (not ethernet)\n");
2603 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2604 pr_debug("invalid all zero ethernet address\n");
2605 return -EADDRNOTAVAIL
;
2612 if (data
[IFLA_VXLAN_ID
]) {
2613 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2614 if (id
>= VXLAN_VID_MASK
)
2618 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2619 const struct ifla_vxlan_port_range
*p
2620 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2622 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2623 pr_debug("port range %u .. %u not valid\n",
2624 ntohs(p
->low
), ntohs(p
->high
));
2632 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2633 struct ethtool_drvinfo
*drvinfo
)
2635 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2636 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2639 static const struct ethtool_ops vxlan_ethtool_ops
= {
2640 .get_drvinfo
= vxlan_get_drvinfo
,
2641 .get_link
= ethtool_op_get_link
,
2644 static struct socket
*vxlan_create_sock(struct net
*net
, bool ipv6
,
2645 __be16 port
, u32 flags
)
2647 struct socket
*sock
;
2648 struct udp_port_cfg udp_conf
;
2651 memset(&udp_conf
, 0, sizeof(udp_conf
));
2654 udp_conf
.family
= AF_INET6
;
2655 udp_conf
.use_udp6_rx_checksums
=
2656 !(flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
);
2657 udp_conf
.ipv6_v6only
= 1;
2659 udp_conf
.family
= AF_INET
;
2662 udp_conf
.local_udp_port
= port
;
2664 /* Open UDP socket */
2665 err
= udp_sock_create(net
, &udp_conf
, &sock
);
2667 return ERR_PTR(err
);
2672 /* Create new listen socket if needed */
2673 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, bool ipv6
,
2674 __be16 port
, u32 flags
)
2676 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2677 struct vxlan_sock
*vs
;
2678 struct socket
*sock
;
2680 struct udp_tunnel_sock_cfg tunnel_cfg
;
2682 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
);
2684 return ERR_PTR(-ENOMEM
);
2686 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
2687 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
2689 sock
= vxlan_create_sock(net
, ipv6
, port
, flags
);
2691 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port
),
2694 return ERR_CAST(sock
);
2698 atomic_set(&vs
->refcnt
, 1);
2699 vs
->flags
= (flags
& VXLAN_F_RCV_FLAGS
);
2701 spin_lock(&vn
->sock_lock
);
2702 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
2703 udp_tunnel_notify_add_rx_port(sock
,
2704 (vs
->flags
& VXLAN_F_GPE
) ?
2705 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2706 UDP_TUNNEL_TYPE_VXLAN
);
2707 spin_unlock(&vn
->sock_lock
);
2709 /* Mark socket as an encapsulation socket. */
2710 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
2711 tunnel_cfg
.sk_user_data
= vs
;
2712 tunnel_cfg
.encap_type
= 1;
2713 tunnel_cfg
.encap_rcv
= vxlan_rcv
;
2714 tunnel_cfg
.encap_destroy
= NULL
;
2715 tunnel_cfg
.gro_receive
= vxlan_gro_receive
;
2716 tunnel_cfg
.gro_complete
= vxlan_gro_complete
;
2718 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
2723 static int __vxlan_sock_add(struct vxlan_dev
*vxlan
, bool ipv6
)
2725 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2726 struct vxlan_sock
*vs
= NULL
;
2728 if (!vxlan
->cfg
.no_share
) {
2729 spin_lock(&vn
->sock_lock
);
2730 vs
= vxlan_find_sock(vxlan
->net
, ipv6
? AF_INET6
: AF_INET
,
2731 vxlan
->cfg
.dst_port
, vxlan
->flags
);
2732 if (vs
&& !atomic_add_unless(&vs
->refcnt
, 1, 0)) {
2733 spin_unlock(&vn
->sock_lock
);
2736 spin_unlock(&vn
->sock_lock
);
2739 vs
= vxlan_socket_create(vxlan
->net
, ipv6
,
2740 vxlan
->cfg
.dst_port
, vxlan
->flags
);
2743 #if IS_ENABLED(CONFIG_IPV6)
2745 vxlan
->vn6_sock
= vs
;
2748 vxlan
->vn4_sock
= vs
;
2749 vxlan_vs_add_dev(vs
, vxlan
);
2753 static int vxlan_sock_add(struct vxlan_dev
*vxlan
)
2755 bool ipv6
= vxlan
->flags
& VXLAN_F_IPV6
;
2756 bool metadata
= vxlan
->flags
& VXLAN_F_COLLECT_METADATA
;
2759 vxlan
->vn4_sock
= NULL
;
2760 #if IS_ENABLED(CONFIG_IPV6)
2761 vxlan
->vn6_sock
= NULL
;
2762 if (ipv6
|| metadata
)
2763 ret
= __vxlan_sock_add(vxlan
, true);
2765 if (!ret
&& (!ipv6
|| metadata
))
2766 ret
= __vxlan_sock_add(vxlan
, false);
2768 vxlan_sock_release(vxlan
);
2772 static int vxlan_dev_configure(struct net
*src_net
, struct net_device
*dev
,
2773 struct vxlan_config
*conf
)
2775 struct vxlan_net
*vn
= net_generic(src_net
, vxlan_net_id
);
2776 struct vxlan_dev
*vxlan
= netdev_priv(dev
), *tmp
;
2777 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2778 unsigned short needed_headroom
= ETH_HLEN
;
2780 bool use_ipv6
= false;
2781 __be16 default_port
= vxlan
->cfg
.dst_port
;
2782 struct net_device
*lowerdev
= NULL
;
2784 if (conf
->flags
& VXLAN_F_GPE
) {
2785 /* For now, allow GPE only together with COLLECT_METADATA.
2786 * This can be relaxed later; in such case, the other side
2787 * of the PtP link will have to be provided.
2789 if ((conf
->flags
& ~VXLAN_F_ALLOWED_GPE
) ||
2790 !(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
2791 pr_info("unsupported combination of extensions\n");
2795 vxlan_raw_setup(dev
);
2797 vxlan_ether_setup(dev
);
2800 vxlan
->net
= src_net
;
2802 dst
->remote_vni
= conf
->vni
;
2804 memcpy(&dst
->remote_ip
, &conf
->remote_ip
, sizeof(conf
->remote_ip
));
2806 /* Unless IPv6 is explicitly requested, assume IPv4 */
2807 if (!dst
->remote_ip
.sa
.sa_family
)
2808 dst
->remote_ip
.sa
.sa_family
= AF_INET
;
2810 if (dst
->remote_ip
.sa
.sa_family
== AF_INET6
||
2811 vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET6
) {
2812 if (!IS_ENABLED(CONFIG_IPV6
))
2813 return -EPFNOSUPPORT
;
2815 vxlan
->flags
|= VXLAN_F_IPV6
;
2818 if (conf
->label
&& !use_ipv6
) {
2819 pr_info("label only supported in use with IPv6\n");
2823 if (conf
->remote_ifindex
) {
2824 lowerdev
= __dev_get_by_index(src_net
, conf
->remote_ifindex
);
2825 dst
->remote_ifindex
= conf
->remote_ifindex
;
2828 pr_info("ifindex %d does not exist\n", dst
->remote_ifindex
);
2832 #if IS_ENABLED(CONFIG_IPV6)
2834 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
2835 if (idev
&& idev
->cnf
.disable_ipv6
) {
2836 pr_info("IPv6 is disabled via sysctl\n");
2843 dev
->mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2845 needed_headroom
= lowerdev
->hard_header_len
;
2846 } else if (vxlan_addr_multicast(&dst
->remote_ip
)) {
2847 pr_info("multicast destination requires interface to be specified\n");
2852 err
= __vxlan_change_mtu(dev
, lowerdev
, dst
, conf
->mtu
, false);
2857 if (use_ipv6
|| conf
->flags
& VXLAN_F_COLLECT_METADATA
)
2858 needed_headroom
+= VXLAN6_HEADROOM
;
2860 needed_headroom
+= VXLAN_HEADROOM
;
2861 dev
->needed_headroom
= needed_headroom
;
2863 memcpy(&vxlan
->cfg
, conf
, sizeof(*conf
));
2864 if (!vxlan
->cfg
.dst_port
) {
2865 if (conf
->flags
& VXLAN_F_GPE
)
2866 vxlan
->cfg
.dst_port
= 4790; /* IANA assigned VXLAN-GPE port */
2868 vxlan
->cfg
.dst_port
= default_port
;
2870 vxlan
->flags
|= conf
->flags
;
2872 if (!vxlan
->cfg
.age_interval
)
2873 vxlan
->cfg
.age_interval
= FDB_AGE_DEFAULT
;
2875 list_for_each_entry(tmp
, &vn
->vxlan_list
, next
) {
2876 if (tmp
->cfg
.vni
== conf
->vni
&&
2877 (tmp
->default_dst
.remote_ip
.sa
.sa_family
== AF_INET6
||
2878 tmp
->cfg
.saddr
.sa
.sa_family
== AF_INET6
) == use_ipv6
&&
2879 tmp
->cfg
.dst_port
== vxlan
->cfg
.dst_port
&&
2880 (tmp
->flags
& VXLAN_F_RCV_FLAGS
) ==
2881 (vxlan
->flags
& VXLAN_F_RCV_FLAGS
)) {
2882 pr_info("duplicate VNI %u\n", be32_to_cpu(conf
->vni
));
2887 dev
->ethtool_ops
= &vxlan_ethtool_ops
;
2889 /* create an fdb entry for a valid default destination */
2890 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
2891 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
2892 &vxlan
->default_dst
.remote_ip
,
2893 NUD_REACHABLE
|NUD_PERMANENT
,
2894 NLM_F_EXCL
|NLM_F_CREATE
,
2895 vxlan
->cfg
.dst_port
,
2896 vxlan
->default_dst
.remote_vni
,
2897 vxlan
->default_dst
.remote_ifindex
,
2903 err
= register_netdevice(dev
);
2905 vxlan_fdb_delete_default(vxlan
);
2909 list_add(&vxlan
->next
, &vn
->vxlan_list
);
2914 static int vxlan_newlink(struct net
*src_net
, struct net_device
*dev
,
2915 struct nlattr
*tb
[], struct nlattr
*data
[])
2917 struct vxlan_config conf
;
2919 memset(&conf
, 0, sizeof(conf
));
2921 if (data
[IFLA_VXLAN_ID
])
2922 conf
.vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
2924 if (data
[IFLA_VXLAN_GROUP
]) {
2925 conf
.remote_ip
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_GROUP
]);
2926 } else if (data
[IFLA_VXLAN_GROUP6
]) {
2927 if (!IS_ENABLED(CONFIG_IPV6
))
2928 return -EPFNOSUPPORT
;
2930 conf
.remote_ip
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_GROUP6
]);
2931 conf
.remote_ip
.sa
.sa_family
= AF_INET6
;
2934 if (data
[IFLA_VXLAN_LOCAL
]) {
2935 conf
.saddr
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_LOCAL
]);
2936 conf
.saddr
.sa
.sa_family
= AF_INET
;
2937 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
2938 if (!IS_ENABLED(CONFIG_IPV6
))
2939 return -EPFNOSUPPORT
;
2941 /* TODO: respect scope id */
2942 conf
.saddr
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_LOCAL6
]);
2943 conf
.saddr
.sa
.sa_family
= AF_INET6
;
2946 if (data
[IFLA_VXLAN_LINK
])
2947 conf
.remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]);
2949 if (data
[IFLA_VXLAN_TOS
])
2950 conf
.tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
2952 if (data
[IFLA_VXLAN_TTL
])
2953 conf
.ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
2955 if (data
[IFLA_VXLAN_LABEL
])
2956 conf
.label
= nla_get_be32(data
[IFLA_VXLAN_LABEL
]) &
2957 IPV6_FLOWLABEL_MASK
;
2959 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
2960 conf
.flags
|= VXLAN_F_LEARN
;
2962 if (data
[IFLA_VXLAN_AGEING
])
2963 conf
.age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
2965 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
2966 conf
.flags
|= VXLAN_F_PROXY
;
2968 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
2969 conf
.flags
|= VXLAN_F_RSC
;
2971 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
2972 conf
.flags
|= VXLAN_F_L2MISS
;
2974 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
2975 conf
.flags
|= VXLAN_F_L3MISS
;
2977 if (data
[IFLA_VXLAN_LIMIT
])
2978 conf
.addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
2980 if (data
[IFLA_VXLAN_COLLECT_METADATA
] &&
2981 nla_get_u8(data
[IFLA_VXLAN_COLLECT_METADATA
]))
2982 conf
.flags
|= VXLAN_F_COLLECT_METADATA
;
2984 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2985 const struct ifla_vxlan_port_range
*p
2986 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2987 conf
.port_min
= ntohs(p
->low
);
2988 conf
.port_max
= ntohs(p
->high
);
2991 if (data
[IFLA_VXLAN_PORT
])
2992 conf
.dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
2994 if (data
[IFLA_VXLAN_UDP_CSUM
] &&
2995 !nla_get_u8(data
[IFLA_VXLAN_UDP_CSUM
]))
2996 conf
.flags
|= VXLAN_F_UDP_ZERO_CSUM_TX
;
2998 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] &&
2999 nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
3000 conf
.flags
|= VXLAN_F_UDP_ZERO_CSUM6_TX
;
3002 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] &&
3003 nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
3004 conf
.flags
|= VXLAN_F_UDP_ZERO_CSUM6_RX
;
3006 if (data
[IFLA_VXLAN_REMCSUM_TX
] &&
3007 nla_get_u8(data
[IFLA_VXLAN_REMCSUM_TX
]))
3008 conf
.flags
|= VXLAN_F_REMCSUM_TX
;
3010 if (data
[IFLA_VXLAN_REMCSUM_RX
] &&
3011 nla_get_u8(data
[IFLA_VXLAN_REMCSUM_RX
]))
3012 conf
.flags
|= VXLAN_F_REMCSUM_RX
;
3014 if (data
[IFLA_VXLAN_GBP
])
3015 conf
.flags
|= VXLAN_F_GBP
;
3017 if (data
[IFLA_VXLAN_GPE
])
3018 conf
.flags
|= VXLAN_F_GPE
;
3020 if (data
[IFLA_VXLAN_REMCSUM_NOPARTIAL
])
3021 conf
.flags
|= VXLAN_F_REMCSUM_NOPARTIAL
;
3024 conf
.mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3026 return vxlan_dev_configure(src_net
, dev
, &conf
);
3029 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
3031 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3032 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
3034 spin_lock(&vn
->sock_lock
);
3035 if (!hlist_unhashed(&vxlan
->hlist
))
3036 hlist_del_rcu(&vxlan
->hlist
);
3037 spin_unlock(&vn
->sock_lock
);
3039 gro_cells_destroy(&vxlan
->gro_cells
);
3040 list_del(&vxlan
->next
);
3041 unregister_netdevice_queue(dev
, head
);
3044 static size_t vxlan_get_size(const struct net_device
*dev
)
3047 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
3048 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
3049 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
3050 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
3051 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
3052 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
3053 nla_total_size(sizeof(__be32
)) + /* IFLA_VXLAN_LABEL */
3054 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
3055 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
3056 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
3057 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
3058 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
3059 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_COLLECT_METADATA */
3060 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
3061 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
3062 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
3063 nla_total_size(sizeof(__be16
)) + /* IFLA_VXLAN_PORT */
3064 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_CSUM */
3065 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3066 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3067 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_TX */
3068 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_RX */
3072 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
3074 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3075 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3076 struct ifla_vxlan_port_range ports
= {
3077 .low
= htons(vxlan
->cfg
.port_min
),
3078 .high
= htons(vxlan
->cfg
.port_max
),
3081 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, be32_to_cpu(dst
->remote_vni
)))
3082 goto nla_put_failure
;
3084 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3085 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
3086 if (nla_put_in_addr(skb
, IFLA_VXLAN_GROUP
,
3087 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
3088 goto nla_put_failure
;
3089 #if IS_ENABLED(CONFIG_IPV6)
3091 if (nla_put_in6_addr(skb
, IFLA_VXLAN_GROUP6
,
3092 &dst
->remote_ip
.sin6
.sin6_addr
))
3093 goto nla_put_failure
;
3098 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
3099 goto nla_put_failure
;
3101 if (!vxlan_addr_any(&vxlan
->cfg
.saddr
)) {
3102 if (vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET
) {
3103 if (nla_put_in_addr(skb
, IFLA_VXLAN_LOCAL
,
3104 vxlan
->cfg
.saddr
.sin
.sin_addr
.s_addr
))
3105 goto nla_put_failure
;
3106 #if IS_ENABLED(CONFIG_IPV6)
3108 if (nla_put_in6_addr(skb
, IFLA_VXLAN_LOCAL6
,
3109 &vxlan
->cfg
.saddr
.sin6
.sin6_addr
))
3110 goto nla_put_failure
;
3115 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->cfg
.ttl
) ||
3116 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->cfg
.tos
) ||
3117 nla_put_be32(skb
, IFLA_VXLAN_LABEL
, vxlan
->cfg
.label
) ||
3118 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
3119 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
3120 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
3121 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
3122 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
3123 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
3124 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
3125 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
3126 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
3127 nla_put_u8(skb
, IFLA_VXLAN_COLLECT_METADATA
,
3128 !!(vxlan
->flags
& VXLAN_F_COLLECT_METADATA
)) ||
3129 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->cfg
.age_interval
) ||
3130 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->cfg
.addrmax
) ||
3131 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->cfg
.dst_port
) ||
3132 nla_put_u8(skb
, IFLA_VXLAN_UDP_CSUM
,
3133 !(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM_TX
)) ||
3134 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_TX
,
3135 !!(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
)) ||
3136 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_RX
,
3137 !!(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
)) ||
3138 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_TX
,
3139 !!(vxlan
->flags
& VXLAN_F_REMCSUM_TX
)) ||
3140 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_RX
,
3141 !!(vxlan
->flags
& VXLAN_F_REMCSUM_RX
)))
3142 goto nla_put_failure
;
3144 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
3145 goto nla_put_failure
;
3147 if (vxlan
->flags
& VXLAN_F_GBP
&&
3148 nla_put_flag(skb
, IFLA_VXLAN_GBP
))
3149 goto nla_put_failure
;
3151 if (vxlan
->flags
& VXLAN_F_GPE
&&
3152 nla_put_flag(skb
, IFLA_VXLAN_GPE
))
3153 goto nla_put_failure
;
3155 if (vxlan
->flags
& VXLAN_F_REMCSUM_NOPARTIAL
&&
3156 nla_put_flag(skb
, IFLA_VXLAN_REMCSUM_NOPARTIAL
))
3157 goto nla_put_failure
;
3165 static struct net
*vxlan_get_link_net(const struct net_device
*dev
)
3167 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3172 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
3174 .maxtype
= IFLA_VXLAN_MAX
,
3175 .policy
= vxlan_policy
,
3176 .priv_size
= sizeof(struct vxlan_dev
),
3177 .setup
= vxlan_setup
,
3178 .validate
= vxlan_validate
,
3179 .newlink
= vxlan_newlink
,
3180 .dellink
= vxlan_dellink
,
3181 .get_size
= vxlan_get_size
,
3182 .fill_info
= vxlan_fill_info
,
3183 .get_link_net
= vxlan_get_link_net
,
3186 struct net_device
*vxlan_dev_create(struct net
*net
, const char *name
,
3187 u8 name_assign_type
,
3188 struct vxlan_config
*conf
)
3190 struct nlattr
*tb
[IFLA_MAX
+ 1];
3191 struct net_device
*dev
;
3194 memset(&tb
, 0, sizeof(tb
));
3196 dev
= rtnl_create_link(net
, name
, name_assign_type
,
3197 &vxlan_link_ops
, tb
);
3201 err
= vxlan_dev_configure(net
, dev
, conf
);
3204 return ERR_PTR(err
);
3207 err
= rtnl_configure_link(dev
, NULL
);
3209 LIST_HEAD(list_kill
);
3211 vxlan_dellink(dev
, &list_kill
);
3212 unregister_netdevice_many(&list_kill
);
3213 return ERR_PTR(err
);
3218 EXPORT_SYMBOL_GPL(vxlan_dev_create
);
3220 static void vxlan_handle_lowerdev_unregister(struct vxlan_net
*vn
,
3221 struct net_device
*dev
)
3223 struct vxlan_dev
*vxlan
, *next
;
3224 LIST_HEAD(list_kill
);
3226 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3227 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3229 /* In case we created vxlan device with carrier
3230 * and we loose the carrier due to module unload
3231 * we also need to remove vxlan device. In other
3232 * cases, it's not necessary and remote_ifindex
3233 * is 0 here, so no matches.
3235 if (dst
->remote_ifindex
== dev
->ifindex
)
3236 vxlan_dellink(vxlan
->dev
, &list_kill
);
3239 unregister_netdevice_many(&list_kill
);
3242 static int vxlan_netdevice_event(struct notifier_block
*unused
,
3243 unsigned long event
, void *ptr
)
3245 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3246 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
3248 if (event
== NETDEV_UNREGISTER
)
3249 vxlan_handle_lowerdev_unregister(vn
, dev
);
3250 else if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
)
3251 vxlan_push_rx_ports(dev
);
3256 static struct notifier_block vxlan_notifier_block __read_mostly
= {
3257 .notifier_call
= vxlan_netdevice_event
,
3260 static __net_init
int vxlan_init_net(struct net
*net
)
3262 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3265 INIT_LIST_HEAD(&vn
->vxlan_list
);
3266 spin_lock_init(&vn
->sock_lock
);
3268 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
3269 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
3274 static void __net_exit
vxlan_exit_net(struct net
*net
)
3276 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3277 struct vxlan_dev
*vxlan
, *next
;
3278 struct net_device
*dev
, *aux
;
3282 for_each_netdev_safe(net
, dev
, aux
)
3283 if (dev
->rtnl_link_ops
== &vxlan_link_ops
)
3284 unregister_netdevice_queue(dev
, &list
);
3286 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3287 /* If vxlan->dev is in the same netns, it has already been added
3288 * to the list by the previous loop.
3290 if (!net_eq(dev_net(vxlan
->dev
), net
)) {
3291 gro_cells_destroy(&vxlan
->gro_cells
);
3292 unregister_netdevice_queue(vxlan
->dev
, &list
);
3296 unregister_netdevice_many(&list
);
3300 static struct pernet_operations vxlan_net_ops
= {
3301 .init
= vxlan_init_net
,
3302 .exit
= vxlan_exit_net
,
3303 .id
= &vxlan_net_id
,
3304 .size
= sizeof(struct vxlan_net
),
3307 static int __init
vxlan_init_module(void)
3311 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
3313 rc
= register_pernet_subsys(&vxlan_net_ops
);
3317 rc
= register_netdevice_notifier(&vxlan_notifier_block
);
3321 rc
= rtnl_link_register(&vxlan_link_ops
);
3327 unregister_netdevice_notifier(&vxlan_notifier_block
);
3329 unregister_pernet_subsys(&vxlan_net_ops
);
3333 late_initcall(vxlan_init_module
);
3335 static void __exit
vxlan_cleanup_module(void)
3337 rtnl_link_unregister(&vxlan_link_ops
);
3338 unregister_netdevice_notifier(&vxlan_notifier_block
);
3339 unregister_pernet_subsys(&vxlan_net_ops
);
3340 /* rcu_barrier() is called by netns */
3342 module_exit(vxlan_cleanup_module
);
3344 MODULE_LICENSE("GPL");
3345 MODULE_VERSION(VXLAN_VERSION
);
3346 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3347 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3348 MODULE_ALIAS_RTNL_LINK("vxlan");