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>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <net/ip6_tunnel.h>
33 #include <net/ip6_checksum.h>
36 #define VXLAN_VERSION "0.1"
38 #define PORT_HASH_BITS 8
39 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
40 #define FDB_AGE_DEFAULT 300 /* 5 min */
41 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
43 /* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
45 * for compatibility with early adopters.
47 static unsigned short vxlan_port __read_mostly
= 8472;
48 module_param_named(udp_port
, vxlan_port
, ushort
, 0444);
49 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
51 static bool log_ecn_error
= true;
52 module_param(log_ecn_error
, bool, 0644);
53 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
55 static unsigned int vxlan_net_id
;
56 static struct rtnl_link_ops vxlan_link_ops
;
58 static const u8 all_zeros_mac
[ETH_ALEN
+ 2];
60 static int vxlan_sock_add(struct vxlan_dev
*vxlan
);
62 /* per-network namespace private data for this module */
64 struct list_head vxlan_list
;
65 struct hlist_head sock_list
[PORT_HASH_SIZE
];
69 /* Forwarding table entry */
71 struct hlist_node hlist
; /* linked list of entries */
73 unsigned long updated
; /* jiffies */
75 struct list_head remotes
;
76 u8 eth_addr
[ETH_ALEN
];
77 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(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 ((vxlan
->flags
& VXLAN_F_COLLECT_METADATA
) && fdb
->vni
&&
307 nla_put_u32(skb
, NDA_SRC_VNI
,
308 be32_to_cpu(fdb
->vni
)))
309 goto nla_put_failure
;
310 if (rdst
->remote_ifindex
&&
311 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
312 goto nla_put_failure
;
314 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
315 ci
.ndm_confirmed
= 0;
316 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
319 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
320 goto nla_put_failure
;
326 nlmsg_cancel(skb
, nlh
);
330 static inline size_t vxlan_nlmsg_size(void)
332 return NLMSG_ALIGN(sizeof(struct ndmsg
))
333 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
334 + nla_total_size(sizeof(struct in6_addr
)) /* NDA_DST */
335 + nla_total_size(sizeof(__be16
)) /* NDA_PORT */
336 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
337 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
338 + nla_total_size(sizeof(__s32
)) /* NDA_LINK_NETNSID */
339 + nla_total_size(sizeof(struct nda_cacheinfo
));
342 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*fdb
,
343 struct vxlan_rdst
*rd
, int type
)
345 struct net
*net
= dev_net(vxlan
->dev
);
349 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
353 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0, rd
);
355 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
356 WARN_ON(err
== -EMSGSIZE
);
361 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
365 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
368 static void vxlan_ip_miss(struct net_device
*dev
, union vxlan_addr
*ipa
)
370 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
371 struct vxlan_fdb f
= {
374 struct vxlan_rdst remote
= {
375 .remote_ip
= *ipa
, /* goes to NDA_DST */
376 .remote_vni
= cpu_to_be32(VXLAN_N_VID
),
379 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
382 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
384 struct vxlan_fdb f
= {
387 struct vxlan_rdst remote
= { };
389 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
391 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
394 /* Hash Ethernet address */
395 static u32
eth_hash(const unsigned char *addr
)
397 u64 value
= get_unaligned((u64
*)addr
);
399 /* only want 6 bytes */
405 return hash_64(value
, FDB_HASH_BITS
);
408 static u32
eth_vni_hash(const unsigned char *addr
, __be32 vni
)
410 /* use 1 byte of OUI and 3 bytes of NIC */
411 u32 key
= get_unaligned((u32
*)(addr
+ 2));
413 return jhash_2words(key
, vni
, vxlan_salt
) & (FDB_HASH_SIZE
- 1);
416 /* Hash chain to use given mac address */
417 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
418 const u8
*mac
, __be32 vni
)
420 if (vxlan
->flags
& VXLAN_F_COLLECT_METADATA
)
421 return &vxlan
->fdb_head
[eth_vni_hash(mac
, vni
)];
423 return &vxlan
->fdb_head
[eth_hash(mac
)];
426 /* Look up Ethernet address in forwarding table */
427 static struct vxlan_fdb
*__vxlan_find_mac(struct vxlan_dev
*vxlan
,
428 const u8
*mac
, __be32 vni
)
430 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
, vni
);
433 hlist_for_each_entry_rcu(f
, head
, hlist
) {
434 if (ether_addr_equal(mac
, f
->eth_addr
)) {
435 if (vxlan
->flags
& VXLAN_F_COLLECT_METADATA
) {
447 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
448 const u8
*mac
, __be32 vni
)
452 f
= __vxlan_find_mac(vxlan
, mac
, vni
);
459 /* caller should hold vxlan->hash_lock */
460 static struct vxlan_rdst
*vxlan_fdb_find_rdst(struct vxlan_fdb
*f
,
461 union vxlan_addr
*ip
, __be16 port
,
462 __be32 vni
, __u32 ifindex
)
464 struct vxlan_rdst
*rd
;
466 list_for_each_entry(rd
, &f
->remotes
, list
) {
467 if (vxlan_addr_equal(&rd
->remote_ip
, ip
) &&
468 rd
->remote_port
== port
&&
469 rd
->remote_vni
== vni
&&
470 rd
->remote_ifindex
== ifindex
)
477 /* Replace destination of unicast mac */
478 static int vxlan_fdb_replace(struct vxlan_fdb
*f
,
479 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
482 struct vxlan_rdst
*rd
;
484 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
488 rd
= list_first_entry_or_null(&f
->remotes
, struct vxlan_rdst
, list
);
492 dst_cache_reset(&rd
->dst_cache
);
494 rd
->remote_port
= port
;
495 rd
->remote_vni
= vni
;
496 rd
->remote_ifindex
= ifindex
;
500 /* Add/update destinations for multicast */
501 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
502 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
503 __u32 ifindex
, struct vxlan_rdst
**rdp
)
505 struct vxlan_rdst
*rd
;
507 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
511 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
515 if (dst_cache_init(&rd
->dst_cache
, GFP_ATOMIC
)) {
521 rd
->remote_port
= port
;
522 rd
->remote_vni
= vni
;
523 rd
->remote_ifindex
= ifindex
;
525 list_add_tail_rcu(&rd
->list
, &f
->remotes
);
531 static struct vxlanhdr
*vxlan_gro_remcsum(struct sk_buff
*skb
,
533 struct vxlanhdr
*vh
, size_t hdrlen
,
535 struct gro_remcsum
*grc
,
538 size_t start
, offset
;
540 if (skb
->remcsum_offload
)
543 if (!NAPI_GRO_CB(skb
)->csum_valid
)
546 start
= vxlan_rco_start(vni_field
);
547 offset
= start
+ vxlan_rco_offset(vni_field
);
549 vh
= skb_gro_remcsum_process(skb
, (void *)vh
, off
, hdrlen
,
550 start
, offset
, grc
, nopartial
);
552 skb
->remcsum_offload
= 1;
557 static struct sk_buff
**vxlan_gro_receive(struct sock
*sk
,
558 struct sk_buff
**head
,
561 struct sk_buff
*p
, **pp
= NULL
;
562 struct vxlanhdr
*vh
, *vh2
;
563 unsigned int hlen
, off_vx
;
565 struct vxlan_sock
*vs
= rcu_dereference_sk_user_data(sk
);
567 struct gro_remcsum grc
;
569 skb_gro_remcsum_init(&grc
);
571 off_vx
= skb_gro_offset(skb
);
572 hlen
= off_vx
+ sizeof(*vh
);
573 vh
= skb_gro_header_fast(skb
, off_vx
);
574 if (skb_gro_header_hard(skb
, hlen
)) {
575 vh
= skb_gro_header_slow(skb
, hlen
, off_vx
);
580 skb_gro_postpull_rcsum(skb
, vh
, sizeof(struct vxlanhdr
));
582 flags
= vh
->vx_flags
;
584 if ((flags
& VXLAN_HF_RCO
) && (vs
->flags
& VXLAN_F_REMCSUM_RX
)) {
585 vh
= vxlan_gro_remcsum(skb
, off_vx
, vh
, sizeof(struct vxlanhdr
),
588 VXLAN_F_REMCSUM_NOPARTIAL
));
594 skb_gro_pull(skb
, sizeof(struct vxlanhdr
)); /* pull vxlan header */
596 for (p
= *head
; p
; p
= p
->next
) {
597 if (!NAPI_GRO_CB(p
)->same_flow
)
600 vh2
= (struct vxlanhdr
*)(p
->data
+ off_vx
);
601 if (vh
->vx_flags
!= vh2
->vx_flags
||
602 vh
->vx_vni
!= vh2
->vx_vni
) {
603 NAPI_GRO_CB(p
)->same_flow
= 0;
608 pp
= call_gro_receive(eth_gro_receive
, head
, skb
);
612 skb_gro_remcsum_cleanup(skb
, &grc
);
613 NAPI_GRO_CB(skb
)->flush
|= flush
;
618 static int vxlan_gro_complete(struct sock
*sk
, struct sk_buff
*skb
, int nhoff
)
620 /* Sets 'skb->inner_mac_header' since we are always called with
621 * 'skb->encapsulation' set.
623 return eth_gro_complete(skb
, nhoff
+ sizeof(struct vxlanhdr
));
626 /* Add new entry to forwarding table -- assumes lock held */
627 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
628 const u8
*mac
, union vxlan_addr
*ip
,
629 __u16 state
, __u16 flags
,
630 __be16 port
, __be32 src_vni
, __be32 vni
,
631 __u32 ifindex
, __u8 ndm_flags
)
633 struct vxlan_rdst
*rd
= NULL
;
638 f
= __vxlan_find_mac(vxlan
, mac
, src_vni
);
640 if (flags
& NLM_F_EXCL
) {
641 netdev_dbg(vxlan
->dev
,
642 "lost race to create %pM\n", mac
);
645 if (f
->state
!= state
) {
647 f
->updated
= jiffies
;
650 if (f
->flags
!= ndm_flags
) {
651 f
->flags
= ndm_flags
;
652 f
->updated
= jiffies
;
655 if ((flags
& NLM_F_REPLACE
)) {
656 /* Only change unicasts */
657 if (!(is_multicast_ether_addr(f
->eth_addr
) ||
658 is_zero_ether_addr(f
->eth_addr
))) {
659 notify
|= vxlan_fdb_replace(f
, ip
, port
, vni
,
664 if ((flags
& NLM_F_APPEND
) &&
665 (is_multicast_ether_addr(f
->eth_addr
) ||
666 is_zero_ether_addr(f
->eth_addr
))) {
667 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
674 if (!(flags
& NLM_F_CREATE
))
677 if (vxlan
->cfg
.addrmax
&&
678 vxlan
->addrcnt
>= vxlan
->cfg
.addrmax
)
681 /* Disallow replace to add a multicast entry */
682 if ((flags
& NLM_F_REPLACE
) &&
683 (is_multicast_ether_addr(mac
) || is_zero_ether_addr(mac
)))
686 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
687 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
693 f
->flags
= ndm_flags
;
694 f
->updated
= f
->used
= jiffies
;
696 INIT_LIST_HEAD(&f
->remotes
);
697 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
699 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
706 hlist_add_head_rcu(&f
->hlist
,
707 vxlan_fdb_head(vxlan
, mac
, src_vni
));
712 rd
= first_remote_rtnl(f
);
713 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_NEWNEIGH
);
719 static void vxlan_fdb_free(struct rcu_head
*head
)
721 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
722 struct vxlan_rdst
*rd
, *nd
;
724 list_for_each_entry_safe(rd
, nd
, &f
->remotes
, list
) {
725 dst_cache_destroy(&rd
->dst_cache
);
731 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
733 netdev_dbg(vxlan
->dev
,
734 "delete %pM\n", f
->eth_addr
);
737 vxlan_fdb_notify(vxlan
, f
, first_remote_rtnl(f
), RTM_DELNEIGH
);
739 hlist_del_rcu(&f
->hlist
);
740 call_rcu(&f
->rcu
, vxlan_fdb_free
);
743 static int vxlan_fdb_parse(struct nlattr
*tb
[], struct vxlan_dev
*vxlan
,
744 union vxlan_addr
*ip
, __be16
*port
, __be32
*src_vni
,
745 __be32
*vni
, u32
*ifindex
)
747 struct net
*net
= dev_net(vxlan
->dev
);
751 err
= vxlan_nla_get_addr(ip
, tb
[NDA_DST
]);
755 union vxlan_addr
*remote
= &vxlan
->default_dst
.remote_ip
;
756 if (remote
->sa
.sa_family
== AF_INET
) {
757 ip
->sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
758 ip
->sa
.sa_family
= AF_INET
;
759 #if IS_ENABLED(CONFIG_IPV6)
761 ip
->sin6
.sin6_addr
= in6addr_any
;
762 ip
->sa
.sa_family
= AF_INET6
;
768 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
770 *port
= nla_get_be16(tb
[NDA_PORT
]);
772 *port
= vxlan
->cfg
.dst_port
;
776 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
778 *vni
= cpu_to_be32(nla_get_u32(tb
[NDA_VNI
]));
780 *vni
= vxlan
->default_dst
.remote_vni
;
783 if (tb
[NDA_SRC_VNI
]) {
784 if (nla_len(tb
[NDA_SRC_VNI
]) != sizeof(u32
))
786 *src_vni
= cpu_to_be32(nla_get_u32(tb
[NDA_SRC_VNI
]));
788 *src_vni
= vxlan
->default_dst
.remote_vni
;
791 if (tb
[NDA_IFINDEX
]) {
792 struct net_device
*tdev
;
794 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
796 *ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
797 tdev
= __dev_get_by_index(net
, *ifindex
);
799 return -EADDRNOTAVAIL
;
807 /* Add static entry (via netlink) */
808 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
809 struct net_device
*dev
,
810 const unsigned char *addr
, u16 vid
, u16 flags
)
812 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
813 /* struct net *net = dev_net(vxlan->dev); */
820 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
821 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
826 if (tb
[NDA_DST
] == NULL
)
829 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &src_vni
, &vni
, &ifindex
);
833 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
!= ip
.sa
.sa_family
)
834 return -EAFNOSUPPORT
;
836 spin_lock_bh(&vxlan
->hash_lock
);
837 err
= vxlan_fdb_create(vxlan
, addr
, &ip
, ndm
->ndm_state
, flags
,
838 port
, src_vni
, vni
, ifindex
, ndm
->ndm_flags
);
839 spin_unlock_bh(&vxlan
->hash_lock
);
844 static int __vxlan_fdb_delete(struct vxlan_dev
*vxlan
,
845 const unsigned char *addr
, union vxlan_addr ip
,
846 __be16 port
, __be32 src_vni
, u32 vni
, u32 ifindex
,
850 struct vxlan_rdst
*rd
= NULL
;
853 f
= vxlan_find_mac(vxlan
, addr
, src_vni
);
857 if (!vxlan_addr_any(&ip
)) {
858 rd
= vxlan_fdb_find_rdst(f
, &ip
, port
, vni
, ifindex
);
863 /* remove a destination if it's not the only one on the list,
864 * otherwise destroy the fdb entry
866 if (rd
&& !list_is_singular(&f
->remotes
)) {
867 list_del_rcu(&rd
->list
);
868 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_DELNEIGH
);
873 vxlan_fdb_destroy(vxlan
, f
);
879 /* Delete entry (via netlink) */
880 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
881 struct net_device
*dev
,
882 const unsigned char *addr
, u16 vid
)
884 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
891 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &src_vni
, &vni
, &ifindex
);
895 spin_lock_bh(&vxlan
->hash_lock
);
896 err
= __vxlan_fdb_delete(vxlan
, addr
, ip
, port
, src_vni
, vni
, ifindex
,
898 spin_unlock_bh(&vxlan
->hash_lock
);
903 /* Dump forwarding table */
904 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
905 struct net_device
*dev
,
906 struct net_device
*filter_dev
, int *idx
)
908 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
912 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
915 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
916 struct vxlan_rdst
*rd
;
918 list_for_each_entry_rcu(rd
, &f
->remotes
, list
) {
919 if (*idx
< cb
->args
[2])
922 err
= vxlan_fdb_info(skb
, vxlan
, f
,
923 NETLINK_CB(cb
->skb
).portid
,
938 /* Watch incoming packets to learn mapping between Ethernet address
939 * and Tunnel endpoint.
940 * Return true if packet is bogus and should be dropped.
942 static bool vxlan_snoop(struct net_device
*dev
,
943 union vxlan_addr
*src_ip
, const u8
*src_mac
,
946 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
949 f
= vxlan_find_mac(vxlan
, src_mac
, vni
);
951 struct vxlan_rdst
*rdst
= first_remote_rcu(f
);
953 if (likely(vxlan_addr_equal(&rdst
->remote_ip
, src_ip
)))
956 /* Don't migrate static entries, drop packets */
957 if (f
->state
& NUD_NOARP
)
962 "%pM migrated from %pIS to %pIS\n",
963 src_mac
, &rdst
->remote_ip
.sa
, &src_ip
->sa
);
965 rdst
->remote_ip
= *src_ip
;
966 f
->updated
= jiffies
;
967 vxlan_fdb_notify(vxlan
, f
, rdst
, RTM_NEWNEIGH
);
969 /* learned new entry */
970 spin_lock(&vxlan
->hash_lock
);
972 /* close off race between vxlan_flush and incoming packets */
973 if (netif_running(dev
))
974 vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
976 NLM_F_EXCL
|NLM_F_CREATE
,
979 vxlan
->default_dst
.remote_vni
,
981 spin_unlock(&vxlan
->hash_lock
);
987 /* See if multicast group is already in use by other ID */
988 static bool vxlan_group_used(struct vxlan_net
*vn
, struct vxlan_dev
*dev
)
990 struct vxlan_dev
*vxlan
;
991 struct vxlan_sock
*sock4
;
992 #if IS_ENABLED(CONFIG_IPV6)
993 struct vxlan_sock
*sock6
;
995 unsigned short family
= dev
->default_dst
.remote_ip
.sa
.sa_family
;
997 sock4
= rtnl_dereference(dev
->vn4_sock
);
999 /* The vxlan_sock is only used by dev, leaving group has
1000 * no effect on other vxlan devices.
1002 if (family
== AF_INET
&& sock4
&& atomic_read(&sock4
->refcnt
) == 1)
1004 #if IS_ENABLED(CONFIG_IPV6)
1005 sock6
= rtnl_dereference(dev
->vn6_sock
);
1006 if (family
== AF_INET6
&& sock6
&& atomic_read(&sock6
->refcnt
) == 1)
1010 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
) {
1011 if (!netif_running(vxlan
->dev
) || vxlan
== dev
)
1014 if (family
== AF_INET
&&
1015 rtnl_dereference(vxlan
->vn4_sock
) != sock4
)
1017 #if IS_ENABLED(CONFIG_IPV6)
1018 if (family
== AF_INET6
&&
1019 rtnl_dereference(vxlan
->vn6_sock
) != sock6
)
1023 if (!vxlan_addr_equal(&vxlan
->default_dst
.remote_ip
,
1024 &dev
->default_dst
.remote_ip
))
1027 if (vxlan
->default_dst
.remote_ifindex
!=
1028 dev
->default_dst
.remote_ifindex
)
1037 static bool __vxlan_sock_release_prep(struct vxlan_sock
*vs
)
1039 struct vxlan_net
*vn
;
1043 if (!atomic_dec_and_test(&vs
->refcnt
))
1046 vn
= net_generic(sock_net(vs
->sock
->sk
), vxlan_net_id
);
1047 spin_lock(&vn
->sock_lock
);
1048 hlist_del_rcu(&vs
->hlist
);
1049 udp_tunnel_notify_del_rx_port(vs
->sock
,
1050 (vs
->flags
& VXLAN_F_GPE
) ?
1051 UDP_TUNNEL_TYPE_VXLAN_GPE
:
1052 UDP_TUNNEL_TYPE_VXLAN
);
1053 spin_unlock(&vn
->sock_lock
);
1058 static void vxlan_sock_release(struct vxlan_dev
*vxlan
)
1060 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1061 #if IS_ENABLED(CONFIG_IPV6)
1062 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1064 rcu_assign_pointer(vxlan
->vn6_sock
, NULL
);
1067 rcu_assign_pointer(vxlan
->vn4_sock
, NULL
);
1070 if (__vxlan_sock_release_prep(sock4
)) {
1071 udp_tunnel_sock_release(sock4
->sock
);
1075 #if IS_ENABLED(CONFIG_IPV6)
1076 if (__vxlan_sock_release_prep(sock6
)) {
1077 udp_tunnel_sock_release(sock6
->sock
);
1083 /* Update multicast group membership when first VNI on
1084 * multicast address is brought up
1086 static int vxlan_igmp_join(struct vxlan_dev
*vxlan
)
1089 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1090 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1093 if (ip
->sa
.sa_family
== AF_INET
) {
1094 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1095 struct ip_mreqn mreq
= {
1096 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1097 .imr_ifindex
= ifindex
,
1100 sk
= sock4
->sock
->sk
;
1102 ret
= ip_mc_join_group(sk
, &mreq
);
1104 #if IS_ENABLED(CONFIG_IPV6)
1106 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1108 sk
= sock6
->sock
->sk
;
1110 ret
= ipv6_stub
->ipv6_sock_mc_join(sk
, ifindex
,
1111 &ip
->sin6
.sin6_addr
);
1119 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1120 static int vxlan_igmp_leave(struct vxlan_dev
*vxlan
)
1123 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1124 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1127 if (ip
->sa
.sa_family
== AF_INET
) {
1128 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1129 struct ip_mreqn mreq
= {
1130 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1131 .imr_ifindex
= ifindex
,
1134 sk
= sock4
->sock
->sk
;
1136 ret
= ip_mc_leave_group(sk
, &mreq
);
1138 #if IS_ENABLED(CONFIG_IPV6)
1140 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1142 sk
= sock6
->sock
->sk
;
1144 ret
= ipv6_stub
->ipv6_sock_mc_drop(sk
, ifindex
,
1145 &ip
->sin6
.sin6_addr
);
1153 static bool vxlan_remcsum(struct vxlanhdr
*unparsed
,
1154 struct sk_buff
*skb
, u32 vxflags
)
1156 size_t start
, offset
;
1158 if (!(unparsed
->vx_flags
& VXLAN_HF_RCO
) || skb
->remcsum_offload
)
1161 start
= vxlan_rco_start(unparsed
->vx_vni
);
1162 offset
= start
+ vxlan_rco_offset(unparsed
->vx_vni
);
1164 if (!pskb_may_pull(skb
, offset
+ sizeof(u16
)))
1167 skb_remcsum_process(skb
, (void *)(vxlan_hdr(skb
) + 1), start
, offset
,
1168 !!(vxflags
& VXLAN_F_REMCSUM_NOPARTIAL
));
1170 unparsed
->vx_flags
&= ~VXLAN_HF_RCO
;
1171 unparsed
->vx_vni
&= VXLAN_VNI_MASK
;
1175 static void vxlan_parse_gbp_hdr(struct vxlanhdr
*unparsed
,
1176 struct sk_buff
*skb
, u32 vxflags
,
1177 struct vxlan_metadata
*md
)
1179 struct vxlanhdr_gbp
*gbp
= (struct vxlanhdr_gbp
*)unparsed
;
1180 struct metadata_dst
*tun_dst
;
1182 if (!(unparsed
->vx_flags
& VXLAN_HF_GBP
))
1185 md
->gbp
= ntohs(gbp
->policy_id
);
1187 tun_dst
= (struct metadata_dst
*)skb_dst(skb
);
1189 tun_dst
->u
.tun_info
.key
.tun_flags
|= TUNNEL_VXLAN_OPT
;
1190 tun_dst
->u
.tun_info
.options_len
= sizeof(*md
);
1192 if (gbp
->dont_learn
)
1193 md
->gbp
|= VXLAN_GBP_DONT_LEARN
;
1195 if (gbp
->policy_applied
)
1196 md
->gbp
|= VXLAN_GBP_POLICY_APPLIED
;
1198 /* In flow-based mode, GBP is carried in dst_metadata */
1199 if (!(vxflags
& VXLAN_F_COLLECT_METADATA
))
1200 skb
->mark
= md
->gbp
;
1202 unparsed
->vx_flags
&= ~VXLAN_GBP_USED_BITS
;
1205 static bool vxlan_parse_gpe_hdr(struct vxlanhdr
*unparsed
,
1207 struct sk_buff
*skb
, u32 vxflags
)
1209 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)unparsed
;
1211 /* Need to have Next Protocol set for interfaces in GPE mode. */
1212 if (!gpe
->np_applied
)
1214 /* "The initial version is 0. If a receiver does not support the
1215 * version indicated it MUST drop the packet.
1217 if (gpe
->version
!= 0)
1219 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1220 * processing MUST occur." However, we don't implement OAM
1221 * processing, thus drop the packet.
1226 switch (gpe
->next_protocol
) {
1227 case VXLAN_GPE_NP_IPV4
:
1228 *protocol
= htons(ETH_P_IP
);
1230 case VXLAN_GPE_NP_IPV6
:
1231 *protocol
= htons(ETH_P_IPV6
);
1233 case VXLAN_GPE_NP_ETHERNET
:
1234 *protocol
= htons(ETH_P_TEB
);
1240 unparsed
->vx_flags
&= ~VXLAN_GPE_USED_BITS
;
1244 static bool vxlan_set_mac(struct vxlan_dev
*vxlan
,
1245 struct vxlan_sock
*vs
,
1246 struct sk_buff
*skb
, __be32 vni
)
1248 union vxlan_addr saddr
;
1250 skb_reset_mac_header(skb
);
1251 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1252 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
1254 /* Ignore packet loops (and multicast echo) */
1255 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1258 /* Get address from the outer IP header */
1259 if (vxlan_get_sk_family(vs
) == AF_INET
) {
1260 saddr
.sin
.sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
1261 saddr
.sa
.sa_family
= AF_INET
;
1262 #if IS_ENABLED(CONFIG_IPV6)
1264 saddr
.sin6
.sin6_addr
= ipv6_hdr(skb
)->saddr
;
1265 saddr
.sa
.sa_family
= AF_INET6
;
1269 if ((vxlan
->flags
& VXLAN_F_LEARN
) &&
1270 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
, vni
))
1276 static bool vxlan_ecn_decapsulate(struct vxlan_sock
*vs
, void *oiph
,
1277 struct sk_buff
*skb
)
1281 if (vxlan_get_sk_family(vs
) == AF_INET
)
1282 err
= IP_ECN_decapsulate(oiph
, skb
);
1283 #if IS_ENABLED(CONFIG_IPV6)
1285 err
= IP6_ECN_decapsulate(oiph
, skb
);
1288 if (unlikely(err
) && log_ecn_error
) {
1289 if (vxlan_get_sk_family(vs
) == AF_INET
)
1290 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1291 &((struct iphdr
*)oiph
)->saddr
,
1292 ((struct iphdr
*)oiph
)->tos
);
1294 net_info_ratelimited("non-ECT from %pI6\n",
1295 &((struct ipv6hdr
*)oiph
)->saddr
);
1300 /* Callback from net/ipv4/udp.c to receive packets */
1301 static int vxlan_rcv(struct sock
*sk
, struct sk_buff
*skb
)
1303 struct pcpu_sw_netstats
*stats
;
1304 struct vxlan_dev
*vxlan
;
1305 struct vxlan_sock
*vs
;
1306 struct vxlanhdr unparsed
;
1307 struct vxlan_metadata _md
;
1308 struct vxlan_metadata
*md
= &_md
;
1309 __be16 protocol
= htons(ETH_P_TEB
);
1310 bool raw_proto
= false;
1314 /* Need UDP and VXLAN header to be present */
1315 if (!pskb_may_pull(skb
, VXLAN_HLEN
))
1318 unparsed
= *vxlan_hdr(skb
);
1319 /* VNI flag always required to be set */
1320 if (!(unparsed
.vx_flags
& VXLAN_HF_VNI
)) {
1321 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
1322 ntohl(vxlan_hdr(skb
)->vx_flags
),
1323 ntohl(vxlan_hdr(skb
)->vx_vni
));
1324 /* Return non vxlan pkt */
1327 unparsed
.vx_flags
&= ~VXLAN_HF_VNI
;
1328 unparsed
.vx_vni
&= ~VXLAN_VNI_MASK
;
1330 vs
= rcu_dereference_sk_user_data(sk
);
1334 vni
= vxlan_vni(vxlan_hdr(skb
)->vx_vni
);
1336 vxlan
= vxlan_vs_find_vni(vs
, vni
);
1340 /* For backwards compatibility, only allow reserved fields to be
1341 * used by VXLAN extensions if explicitly requested.
1343 if (vs
->flags
& VXLAN_F_GPE
) {
1344 if (!vxlan_parse_gpe_hdr(&unparsed
, &protocol
, skb
, vs
->flags
))
1349 if (__iptunnel_pull_header(skb
, VXLAN_HLEN
, protocol
, raw_proto
,
1350 !net_eq(vxlan
->net
, dev_net(vxlan
->dev
))))
1353 if (vxlan_collect_metadata(vs
)) {
1354 struct metadata_dst
*tun_dst
;
1356 tun_dst
= udp_tun_rx_dst(skb
, vxlan_get_sk_family(vs
), TUNNEL_KEY
,
1357 key32_to_tunnel_id(vni
), sizeof(*md
));
1362 md
= ip_tunnel_info_opts(&tun_dst
->u
.tun_info
);
1364 skb_dst_set(skb
, (struct dst_entry
*)tun_dst
);
1366 memset(md
, 0, sizeof(*md
));
1369 if (vs
->flags
& VXLAN_F_REMCSUM_RX
)
1370 if (!vxlan_remcsum(&unparsed
, skb
, vs
->flags
))
1372 if (vs
->flags
& VXLAN_F_GBP
)
1373 vxlan_parse_gbp_hdr(&unparsed
, skb
, vs
->flags
, md
);
1374 /* Note that GBP and GPE can never be active together. This is
1375 * ensured in vxlan_dev_configure.
1378 if (unparsed
.vx_flags
|| unparsed
.vx_vni
) {
1379 /* If there are any unprocessed flags remaining treat
1380 * this as a malformed packet. This behavior diverges from
1381 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1382 * in reserved fields are to be ignored. The approach here
1383 * maintains compatibility with previous stack code, and also
1384 * is more robust and provides a little more security in
1385 * adding extensions to VXLAN.
1391 if (!vxlan_set_mac(vxlan
, vs
, skb
, vni
))
1394 skb_reset_mac_header(skb
);
1395 skb
->dev
= vxlan
->dev
;
1396 skb
->pkt_type
= PACKET_HOST
;
1399 oiph
= skb_network_header(skb
);
1400 skb_reset_network_header(skb
);
1402 if (!vxlan_ecn_decapsulate(vs
, oiph
, skb
)) {
1403 ++vxlan
->dev
->stats
.rx_frame_errors
;
1404 ++vxlan
->dev
->stats
.rx_errors
;
1408 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1409 u64_stats_update_begin(&stats
->syncp
);
1410 stats
->rx_packets
++;
1411 stats
->rx_bytes
+= skb
->len
;
1412 u64_stats_update_end(&stats
->syncp
);
1414 gro_cells_receive(&vxlan
->gro_cells
, skb
);
1418 /* Consume bad packet */
1423 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
, __be32 vni
)
1425 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1426 struct arphdr
*parp
;
1429 struct neighbour
*n
;
1431 if (dev
->flags
& IFF_NOARP
)
1434 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1435 dev
->stats
.tx_dropped
++;
1438 parp
= arp_hdr(skb
);
1440 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1441 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1442 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1443 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1444 parp
->ar_hln
!= dev
->addr_len
||
1447 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1449 arpptr
+= dev
->addr_len
; /* sha */
1450 memcpy(&sip
, arpptr
, sizeof(sip
));
1451 arpptr
+= sizeof(sip
);
1452 arpptr
+= dev
->addr_len
; /* tha */
1453 memcpy(&tip
, arpptr
, sizeof(tip
));
1455 if (ipv4_is_loopback(tip
) ||
1456 ipv4_is_multicast(tip
))
1459 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1462 struct vxlan_fdb
*f
;
1463 struct sk_buff
*reply
;
1465 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1470 f
= vxlan_find_mac(vxlan
, n
->ha
, vni
);
1471 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1472 /* bridge-local neighbor */
1477 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1485 skb_reset_mac_header(reply
);
1486 __skb_pull(reply
, skb_network_offset(reply
));
1487 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1488 reply
->pkt_type
= PACKET_HOST
;
1490 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1491 dev
->stats
.rx_dropped
++;
1492 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1493 union vxlan_addr ipa
= {
1494 .sin
.sin_addr
.s_addr
= tip
,
1495 .sin
.sin_family
= AF_INET
,
1498 vxlan_ip_miss(dev
, &ipa
);
1502 return NETDEV_TX_OK
;
1505 #if IS_ENABLED(CONFIG_IPV6)
1506 static struct sk_buff
*vxlan_na_create(struct sk_buff
*request
,
1507 struct neighbour
*n
, bool isrouter
)
1509 struct net_device
*dev
= request
->dev
;
1510 struct sk_buff
*reply
;
1511 struct nd_msg
*ns
, *na
;
1512 struct ipv6hdr
*pip6
;
1514 int na_olen
= 8; /* opt hdr + ETH_ALEN for target */
1521 len
= LL_RESERVED_SPACE(dev
) + sizeof(struct ipv6hdr
) +
1522 sizeof(*na
) + na_olen
+ dev
->needed_tailroom
;
1523 reply
= alloc_skb(len
, GFP_ATOMIC
);
1527 reply
->protocol
= htons(ETH_P_IPV6
);
1529 skb_reserve(reply
, LL_RESERVED_SPACE(request
->dev
));
1530 skb_push(reply
, sizeof(struct ethhdr
));
1531 skb_reset_mac_header(reply
);
1533 ns
= (struct nd_msg
*)skb_transport_header(request
);
1535 daddr
= eth_hdr(request
)->h_source
;
1536 ns_olen
= request
->len
- skb_transport_offset(request
) - sizeof(*ns
);
1537 for (i
= 0; i
< ns_olen
-1; i
+= (ns
->opt
[i
+1]<<3)) {
1538 if (ns
->opt
[i
] == ND_OPT_SOURCE_LL_ADDR
) {
1539 daddr
= ns
->opt
+ i
+ sizeof(struct nd_opt_hdr
);
1544 /* Ethernet header */
1545 ether_addr_copy(eth_hdr(reply
)->h_dest
, daddr
);
1546 ether_addr_copy(eth_hdr(reply
)->h_source
, n
->ha
);
1547 eth_hdr(reply
)->h_proto
= htons(ETH_P_IPV6
);
1548 reply
->protocol
= htons(ETH_P_IPV6
);
1550 skb_pull(reply
, sizeof(struct ethhdr
));
1551 skb_reset_network_header(reply
);
1552 skb_put(reply
, sizeof(struct ipv6hdr
));
1556 pip6
= ipv6_hdr(reply
);
1557 memset(pip6
, 0, sizeof(struct ipv6hdr
));
1559 pip6
->priority
= ipv6_hdr(request
)->priority
;
1560 pip6
->nexthdr
= IPPROTO_ICMPV6
;
1561 pip6
->hop_limit
= 255;
1562 pip6
->daddr
= ipv6_hdr(request
)->saddr
;
1563 pip6
->saddr
= *(struct in6_addr
*)n
->primary_key
;
1565 skb_pull(reply
, sizeof(struct ipv6hdr
));
1566 skb_reset_transport_header(reply
);
1568 na
= (struct nd_msg
*)skb_put(reply
, sizeof(*na
) + na_olen
);
1570 /* Neighbor Advertisement */
1571 memset(na
, 0, sizeof(*na
)+na_olen
);
1572 na
->icmph
.icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
1573 na
->icmph
.icmp6_router
= isrouter
;
1574 na
->icmph
.icmp6_override
= 1;
1575 na
->icmph
.icmp6_solicited
= 1;
1576 na
->target
= ns
->target
;
1577 ether_addr_copy(&na
->opt
[2], n
->ha
);
1578 na
->opt
[0] = ND_OPT_TARGET_LL_ADDR
;
1579 na
->opt
[1] = na_olen
>> 3;
1581 na
->icmph
.icmp6_cksum
= csum_ipv6_magic(&pip6
->saddr
,
1582 &pip6
->daddr
, sizeof(*na
)+na_olen
, IPPROTO_ICMPV6
,
1583 csum_partial(na
, sizeof(*na
)+na_olen
, 0));
1585 pip6
->payload_len
= htons(sizeof(*na
)+na_olen
);
1587 skb_push(reply
, sizeof(struct ipv6hdr
));
1589 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1594 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
, __be32 vni
)
1596 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1598 const struct ipv6hdr
*iphdr
;
1599 const struct in6_addr
*saddr
, *daddr
;
1600 struct neighbour
*n
;
1601 struct inet6_dev
*in6_dev
;
1603 in6_dev
= __in6_dev_get(dev
);
1607 iphdr
= ipv6_hdr(skb
);
1608 saddr
= &iphdr
->saddr
;
1609 daddr
= &iphdr
->daddr
;
1611 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1612 if (msg
->icmph
.icmp6_code
!= 0 ||
1613 msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
1616 if (ipv6_addr_loopback(daddr
) ||
1617 ipv6_addr_is_multicast(&msg
->target
))
1620 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1623 struct vxlan_fdb
*f
;
1624 struct sk_buff
*reply
;
1626 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1631 f
= vxlan_find_mac(vxlan
, n
->ha
, vni
);
1632 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1633 /* bridge-local neighbor */
1638 reply
= vxlan_na_create(skb
, n
,
1639 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1646 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1647 dev
->stats
.rx_dropped
++;
1649 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1650 union vxlan_addr ipa
= {
1651 .sin6
.sin6_addr
= msg
->target
,
1652 .sin6
.sin6_family
= AF_INET6
,
1655 vxlan_ip_miss(dev
, &ipa
);
1660 return NETDEV_TX_OK
;
1664 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1666 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1667 struct neighbour
*n
;
1669 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1673 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1678 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1681 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1682 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1683 union vxlan_addr ipa
= {
1684 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1685 .sin
.sin_family
= AF_INET
,
1688 vxlan_ip_miss(dev
, &ipa
);
1694 #if IS_ENABLED(CONFIG_IPV6)
1697 struct ipv6hdr
*pip6
;
1699 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1701 pip6
= ipv6_hdr(skb
);
1702 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1703 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1704 union vxlan_addr ipa
= {
1705 .sin6
.sin6_addr
= pip6
->daddr
,
1706 .sin6
.sin6_family
= AF_INET6
,
1709 vxlan_ip_miss(dev
, &ipa
);
1723 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1725 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1727 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1736 static void vxlan_build_gbp_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1737 struct vxlan_metadata
*md
)
1739 struct vxlanhdr_gbp
*gbp
;
1744 gbp
= (struct vxlanhdr_gbp
*)vxh
;
1745 vxh
->vx_flags
|= VXLAN_HF_GBP
;
1747 if (md
->gbp
& VXLAN_GBP_DONT_LEARN
)
1748 gbp
->dont_learn
= 1;
1750 if (md
->gbp
& VXLAN_GBP_POLICY_APPLIED
)
1751 gbp
->policy_applied
= 1;
1753 gbp
->policy_id
= htons(md
->gbp
& VXLAN_GBP_ID_MASK
);
1756 static int vxlan_build_gpe_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1759 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)vxh
;
1761 gpe
->np_applied
= 1;
1764 case htons(ETH_P_IP
):
1765 gpe
->next_protocol
= VXLAN_GPE_NP_IPV4
;
1767 case htons(ETH_P_IPV6
):
1768 gpe
->next_protocol
= VXLAN_GPE_NP_IPV6
;
1770 case htons(ETH_P_TEB
):
1771 gpe
->next_protocol
= VXLAN_GPE_NP_ETHERNET
;
1774 return -EPFNOSUPPORT
;
1777 static int vxlan_build_skb(struct sk_buff
*skb
, struct dst_entry
*dst
,
1778 int iphdr_len
, __be32 vni
,
1779 struct vxlan_metadata
*md
, u32 vxflags
,
1782 struct vxlanhdr
*vxh
;
1785 int type
= udp_sum
? SKB_GSO_UDP_TUNNEL_CSUM
: SKB_GSO_UDP_TUNNEL
;
1786 __be16 inner_protocol
= htons(ETH_P_TEB
);
1788 if ((vxflags
& VXLAN_F_REMCSUM_TX
) &&
1789 skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1790 int csum_start
= skb_checksum_start_offset(skb
);
1792 if (csum_start
<= VXLAN_MAX_REMCSUM_START
&&
1793 !(csum_start
& VXLAN_RCO_SHIFT_MASK
) &&
1794 (skb
->csum_offset
== offsetof(struct udphdr
, check
) ||
1795 skb
->csum_offset
== offsetof(struct tcphdr
, check
)))
1796 type
|= SKB_GSO_TUNNEL_REMCSUM
;
1799 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1800 + VXLAN_HLEN
+ iphdr_len
;
1802 /* Need space for new headers (invalidates iph ptr) */
1803 err
= skb_cow_head(skb
, min_headroom
);
1807 err
= iptunnel_handle_offloads(skb
, type
);
1811 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1812 vxh
->vx_flags
= VXLAN_HF_VNI
;
1813 vxh
->vx_vni
= vxlan_vni_field(vni
);
1815 if (type
& SKB_GSO_TUNNEL_REMCSUM
) {
1818 start
= skb_checksum_start_offset(skb
) - sizeof(struct vxlanhdr
);
1819 vxh
->vx_vni
|= vxlan_compute_rco(start
, skb
->csum_offset
);
1820 vxh
->vx_flags
|= VXLAN_HF_RCO
;
1822 if (!skb_is_gso(skb
)) {
1823 skb
->ip_summed
= CHECKSUM_NONE
;
1824 skb
->encapsulation
= 0;
1828 if (vxflags
& VXLAN_F_GBP
)
1829 vxlan_build_gbp_hdr(vxh
, vxflags
, md
);
1830 if (vxflags
& VXLAN_F_GPE
) {
1831 err
= vxlan_build_gpe_hdr(vxh
, vxflags
, skb
->protocol
);
1834 inner_protocol
= skb
->protocol
;
1837 skb_set_inner_protocol(skb
, inner_protocol
);
1841 static struct rtable
*vxlan_get_route(struct vxlan_dev
*vxlan
, struct net_device
*dev
,
1842 struct vxlan_sock
*sock4
,
1843 struct sk_buff
*skb
, int oif
, u8 tos
,
1844 __be32 daddr
, __be32
*saddr
, __be16 dport
, __be16 sport
,
1845 struct dst_cache
*dst_cache
,
1846 const struct ip_tunnel_info
*info
)
1848 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
1849 struct rtable
*rt
= NULL
;
1853 return ERR_PTR(-EIO
);
1858 rt
= dst_cache_get_ip4(dst_cache
, saddr
);
1863 memset(&fl4
, 0, sizeof(fl4
));
1864 fl4
.flowi4_oif
= oif
;
1865 fl4
.flowi4_tos
= RT_TOS(tos
);
1866 fl4
.flowi4_mark
= skb
->mark
;
1867 fl4
.flowi4_proto
= IPPROTO_UDP
;
1870 fl4
.fl4_dport
= dport
;
1871 fl4
.fl4_sport
= sport
;
1873 rt
= ip_route_output_key(vxlan
->net
, &fl4
);
1874 if (likely(!IS_ERR(rt
))) {
1875 if (rt
->dst
.dev
== dev
) {
1876 netdev_dbg(dev
, "circular route to %pI4\n", &daddr
);
1878 return ERR_PTR(-ELOOP
);
1883 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
.saddr
);
1885 netdev_dbg(dev
, "no route to %pI4\n", &daddr
);
1886 return ERR_PTR(-ENETUNREACH
);
1891 #if IS_ENABLED(CONFIG_IPV6)
1892 static struct dst_entry
*vxlan6_get_route(struct vxlan_dev
*vxlan
,
1893 struct net_device
*dev
,
1894 struct vxlan_sock
*sock6
,
1895 struct sk_buff
*skb
, int oif
, u8 tos
,
1897 const struct in6_addr
*daddr
,
1898 struct in6_addr
*saddr
,
1899 __be16 dport
, __be16 sport
,
1900 struct dst_cache
*dst_cache
,
1901 const struct ip_tunnel_info
*info
)
1903 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
1904 struct dst_entry
*ndst
;
1909 return ERR_PTR(-EIO
);
1914 ndst
= dst_cache_get_ip6(dst_cache
, saddr
);
1919 memset(&fl6
, 0, sizeof(fl6
));
1920 fl6
.flowi6_oif
= oif
;
1923 fl6
.flowlabel
= ip6_make_flowinfo(RT_TOS(tos
), label
);
1924 fl6
.flowi6_mark
= skb
->mark
;
1925 fl6
.flowi6_proto
= IPPROTO_UDP
;
1926 fl6
.fl6_dport
= dport
;
1927 fl6
.fl6_sport
= sport
;
1929 err
= ipv6_stub
->ipv6_dst_lookup(vxlan
->net
,
1932 if (unlikely(err
< 0)) {
1933 netdev_dbg(dev
, "no route to %pI6\n", daddr
);
1934 return ERR_PTR(-ENETUNREACH
);
1937 if (unlikely(ndst
->dev
== dev
)) {
1938 netdev_dbg(dev
, "circular route to %pI6\n", daddr
);
1940 return ERR_PTR(-ELOOP
);
1945 dst_cache_set_ip6(dst_cache
, ndst
, saddr
);
1950 /* Bypass encapsulation if the destination is local */
1951 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
1952 struct vxlan_dev
*dst_vxlan
, __be32 vni
)
1954 struct pcpu_sw_netstats
*tx_stats
, *rx_stats
;
1955 union vxlan_addr loopback
;
1956 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
1957 struct net_device
*dev
= skb
->dev
;
1960 tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
1961 rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
1962 skb
->pkt_type
= PACKET_HOST
;
1963 skb
->encapsulation
= 0;
1964 skb
->dev
= dst_vxlan
->dev
;
1965 __skb_pull(skb
, skb_network_offset(skb
));
1967 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1968 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1969 loopback
.sa
.sa_family
= AF_INET
;
1970 #if IS_ENABLED(CONFIG_IPV6)
1972 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
1973 loopback
.sa
.sa_family
= AF_INET6
;
1977 if (dst_vxlan
->flags
& VXLAN_F_LEARN
)
1978 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
, vni
);
1980 u64_stats_update_begin(&tx_stats
->syncp
);
1981 tx_stats
->tx_packets
++;
1982 tx_stats
->tx_bytes
+= len
;
1983 u64_stats_update_end(&tx_stats
->syncp
);
1985 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
1986 u64_stats_update_begin(&rx_stats
->syncp
);
1987 rx_stats
->rx_packets
++;
1988 rx_stats
->rx_bytes
+= len
;
1989 u64_stats_update_end(&rx_stats
->syncp
);
1991 dev
->stats
.rx_dropped
++;
1995 static int encap_bypass_if_local(struct sk_buff
*skb
, struct net_device
*dev
,
1996 struct vxlan_dev
*vxlan
, union vxlan_addr
*daddr
,
1997 __be16 dst_port
, __be32 vni
, struct dst_entry
*dst
,
2000 #if IS_ENABLED(CONFIG_IPV6)
2001 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2002 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2003 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2005 BUILD_BUG_ON(RTCF_LOCAL
!= RTF_LOCAL
);
2007 /* Bypass encapsulation if the destination is local */
2008 if (rt_flags
& RTCF_LOCAL
&&
2009 !(rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
2010 struct vxlan_dev
*dst_vxlan
;
2013 dst_vxlan
= vxlan_find_vni(vxlan
->net
, vni
,
2014 daddr
->sa
.sa_family
, dst_port
,
2017 dev
->stats
.tx_errors
++;
2022 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
, vni
);
2029 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
2030 __be32 default_vni
, struct vxlan_rdst
*rdst
,
2033 struct dst_cache
*dst_cache
;
2034 struct ip_tunnel_info
*info
;
2035 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2036 const struct iphdr
*old_iph
= ip_hdr(skb
);
2037 union vxlan_addr
*dst
;
2038 union vxlan_addr remote_ip
, local_ip
;
2039 union vxlan_addr
*src
;
2040 struct vxlan_metadata _md
;
2041 struct vxlan_metadata
*md
= &_md
;
2042 __be16 src_port
= 0, dst_port
;
2043 struct dst_entry
*ndst
= NULL
;
2047 u32 flags
= vxlan
->flags
;
2048 bool udp_sum
= false;
2049 bool xnet
= !net_eq(vxlan
->net
, dev_net(vxlan
->dev
));
2051 info
= skb_tunnel_info(skb
);
2054 dst
= &rdst
->remote_ip
;
2055 if (vxlan_addr_any(dst
)) {
2057 /* short-circuited back to local bridge */
2058 vxlan_encap_bypass(skb
, vxlan
, vxlan
, default_vni
);
2064 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->cfg
.dst_port
;
2065 vni
= (rdst
->remote_vni
) ? : default_vni
;
2066 src
= &vxlan
->cfg
.saddr
;
2067 dst_cache
= &rdst
->dst_cache
;
2068 md
->gbp
= skb
->mark
;
2069 ttl
= vxlan
->cfg
.ttl
;
2070 if (!ttl
&& vxlan_addr_multicast(dst
))
2073 tos
= vxlan
->cfg
.tos
;
2075 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
2077 if (dst
->sa
.sa_family
== AF_INET
)
2078 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM_TX
);
2080 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
);
2081 label
= vxlan
->cfg
.label
;
2084 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2088 remote_ip
.sa
.sa_family
= ip_tunnel_info_af(info
);
2089 if (remote_ip
.sa
.sa_family
== AF_INET
) {
2090 remote_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.dst
;
2091 local_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.src
;
2093 remote_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.dst
;
2094 local_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.src
;
2097 dst_port
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2098 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2100 dst_cache
= &info
->dst_cache
;
2101 if (info
->options_len
)
2102 md
= ip_tunnel_info_opts(info
);
2103 ttl
= info
->key
.ttl
;
2104 tos
= info
->key
.tos
;
2105 label
= info
->key
.label
;
2106 udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
2108 src_port
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2109 vxlan
->cfg
.port_max
, true);
2111 if (dst
->sa
.sa_family
== AF_INET
) {
2112 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2116 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
,
2117 rdst
? rdst
->remote_ifindex
: 0, tos
,
2118 dst
->sin
.sin_addr
.s_addr
,
2119 &src
->sin
.sin_addr
.s_addr
,
2127 /* Bypass encapsulation if the destination is local */
2129 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2130 dst_port
, vni
, &rt
->dst
,
2134 } else if (info
->key
.tun_flags
& TUNNEL_DONT_FRAGMENT
) {
2139 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2140 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
2141 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct iphdr
),
2142 vni
, md
, flags
, udp_sum
);
2146 udp_tunnel_xmit_skb(rt
, sock4
->sock
->sk
, skb
, src
->sin
.sin_addr
.s_addr
,
2147 dst
->sin
.sin_addr
.s_addr
, tos
, ttl
, df
,
2148 src_port
, dst_port
, xnet
, !udp_sum
);
2149 #if IS_ENABLED(CONFIG_IPV6)
2151 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2153 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
,
2154 rdst
? rdst
->remote_ifindex
: 0, tos
,
2155 label
, &dst
->sin6
.sin6_addr
,
2156 &src
->sin6
.sin6_addr
,
2160 err
= PTR_ERR(ndst
);
2166 u32 rt6i_flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
2168 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2169 dst_port
, vni
, ndst
,
2175 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2176 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
2177 skb_scrub_packet(skb
, xnet
);
2178 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct ipv6hdr
),
2179 vni
, md
, flags
, udp_sum
);
2183 udp_tunnel6_xmit_skb(ndst
, sock6
->sock
->sk
, skb
, dev
,
2184 &src
->sin6
.sin6_addr
,
2185 &dst
->sin6
.sin6_addr
, tos
, ttl
,
2186 label
, src_port
, dst_port
, !udp_sum
);
2192 dev
->stats
.tx_dropped
++;
2198 dev
->stats
.collisions
++;
2199 else if (err
== -ENETUNREACH
)
2200 dev
->stats
.tx_carrier_errors
++;
2202 dev
->stats
.tx_errors
++;
2206 /* Transmit local packets over Vxlan
2208 * Outer IP header inherits ECN and DF from inner header.
2209 * Outer UDP destination is the VXLAN assigned port.
2210 * source port is based on hash of flow
2212 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
2214 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2215 const struct ip_tunnel_info
*info
;
2217 bool did_rsc
= false;
2218 struct vxlan_rdst
*rdst
, *fdst
= NULL
;
2219 struct vxlan_fdb
*f
;
2222 info
= skb_tunnel_info(skb
);
2224 skb_reset_mac_header(skb
);
2226 if (vxlan
->flags
& VXLAN_F_COLLECT_METADATA
) {
2227 if (info
&& info
->mode
& IP_TUNNEL_INFO_BRIDGE
&&
2228 info
->mode
& IP_TUNNEL_INFO_TX
) {
2229 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2231 if (info
&& info
->mode
& IP_TUNNEL_INFO_TX
)
2232 vxlan_xmit_one(skb
, dev
, vni
, NULL
, false);
2235 return NETDEV_TX_OK
;
2239 if (vxlan
->flags
& VXLAN_F_PROXY
) {
2241 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
2242 return arp_reduce(dev
, skb
, vni
);
2243 #if IS_ENABLED(CONFIG_IPV6)
2244 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
&&
2245 pskb_may_pull(skb
, sizeof(struct ipv6hdr
)
2246 + sizeof(struct nd_msg
)) &&
2247 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
2250 msg
= (struct nd_msg
*)skb_transport_header(skb
);
2251 if (msg
->icmph
.icmp6_code
== 0 &&
2252 msg
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
)
2253 return neigh_reduce(dev
, skb
, vni
);
2259 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2262 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->flags
& VXLAN_F_RSC
) &&
2263 (ntohs(eth
->h_proto
) == ETH_P_IP
||
2264 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
2265 did_rsc
= route_shortcircuit(dev
, skb
);
2267 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2271 f
= vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2273 if ((vxlan
->flags
& VXLAN_F_L2MISS
) &&
2274 !is_multicast_ether_addr(eth
->h_dest
))
2275 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
2277 dev
->stats
.tx_dropped
++;
2279 return NETDEV_TX_OK
;
2283 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
2284 struct sk_buff
*skb1
;
2290 skb1
= skb_clone(skb
, GFP_ATOMIC
);
2292 vxlan_xmit_one(skb1
, dev
, vni
, rdst
, did_rsc
);
2296 vxlan_xmit_one(skb
, dev
, vni
, fdst
, did_rsc
);
2299 return NETDEV_TX_OK
;
2302 /* Walk the forwarding table and purge stale entries */
2303 static void vxlan_cleanup(unsigned long arg
)
2305 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
2306 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
2309 if (!netif_running(vxlan
->dev
))
2312 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2313 struct hlist_node
*p
, *n
;
2315 spin_lock_bh(&vxlan
->hash_lock
);
2316 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2318 = container_of(p
, struct vxlan_fdb
, hlist
);
2319 unsigned long timeout
;
2321 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
2324 timeout
= f
->used
+ vxlan
->cfg
.age_interval
* HZ
;
2325 if (time_before_eq(timeout
, jiffies
)) {
2326 netdev_dbg(vxlan
->dev
,
2327 "garbage collect %pM\n",
2329 f
->state
= NUD_STALE
;
2330 vxlan_fdb_destroy(vxlan
, f
);
2331 } else if (time_before(timeout
, next_timer
))
2332 next_timer
= timeout
;
2334 spin_unlock_bh(&vxlan
->hash_lock
);
2337 mod_timer(&vxlan
->age_timer
, next_timer
);
2340 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
)
2342 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2343 __be32 vni
= vxlan
->default_dst
.remote_vni
;
2345 spin_lock(&vn
->sock_lock
);
2346 hlist_add_head_rcu(&vxlan
->hlist
, vni_head(vs
, vni
));
2347 spin_unlock(&vn
->sock_lock
);
2350 /* Setup stats when device is created */
2351 static int vxlan_init(struct net_device
*dev
)
2353 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
2360 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
, __be32 vni
)
2362 struct vxlan_fdb
*f
;
2364 spin_lock_bh(&vxlan
->hash_lock
);
2365 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2367 vxlan_fdb_destroy(vxlan
, f
);
2368 spin_unlock_bh(&vxlan
->hash_lock
);
2371 static void vxlan_uninit(struct net_device
*dev
)
2373 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2375 vxlan_fdb_delete_default(vxlan
, vxlan
->cfg
.vni
);
2377 free_percpu(dev
->tstats
);
2380 /* Start ageing timer and join group when device is brought up */
2381 static int vxlan_open(struct net_device
*dev
)
2383 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2386 ret
= vxlan_sock_add(vxlan
);
2390 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
)) {
2391 ret
= vxlan_igmp_join(vxlan
);
2392 if (ret
== -EADDRINUSE
)
2395 vxlan_sock_release(vxlan
);
2400 if (vxlan
->cfg
.age_interval
)
2401 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2406 /* Purge the forwarding table */
2407 static void vxlan_flush(struct vxlan_dev
*vxlan
, bool do_all
)
2411 spin_lock_bh(&vxlan
->hash_lock
);
2412 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2413 struct hlist_node
*p
, *n
;
2414 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2416 = container_of(p
, struct vxlan_fdb
, hlist
);
2417 if (!do_all
&& (f
->state
& (NUD_PERMANENT
| NUD_NOARP
)))
2419 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2420 if (!is_zero_ether_addr(f
->eth_addr
))
2421 vxlan_fdb_destroy(vxlan
, f
);
2424 spin_unlock_bh(&vxlan
->hash_lock
);
2427 /* Cleanup timer and forwarding table on shutdown */
2428 static int vxlan_stop(struct net_device
*dev
)
2430 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2431 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2434 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2435 !vxlan_group_used(vn
, vxlan
))
2436 ret
= vxlan_igmp_leave(vxlan
);
2438 del_timer_sync(&vxlan
->age_timer
);
2440 vxlan_flush(vxlan
, false);
2441 vxlan_sock_release(vxlan
);
2446 /* Stub, nothing needs to be done. */
2447 static void vxlan_set_multicast_list(struct net_device
*dev
)
2451 static int vxlan_change_mtu(struct net_device
*dev
, int new_mtu
)
2453 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2454 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2455 struct net_device
*lowerdev
= __dev_get_by_index(vxlan
->net
,
2456 dst
->remote_ifindex
);
2457 bool use_ipv6
= false;
2459 if (dst
->remote_ip
.sa
.sa_family
== AF_INET6
)
2462 /* This check is different than dev->max_mtu, because it looks at
2463 * the lowerdev->mtu, rather than the static dev->max_mtu
2466 int max_mtu
= lowerdev
->mtu
-
2467 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2468 if (new_mtu
> max_mtu
)
2476 static int vxlan_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
2478 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2479 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
2480 __be16 sport
, dport
;
2482 sport
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2483 vxlan
->cfg
.port_max
, true);
2484 dport
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2486 if (ip_tunnel_info_af(info
) == AF_INET
) {
2487 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2490 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
, 0, info
->key
.tos
,
2491 info
->key
.u
.ipv4
.dst
,
2492 &info
->key
.u
.ipv4
.src
, dport
, sport
, NULL
, info
);
2497 #if IS_ENABLED(CONFIG_IPV6)
2498 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2499 struct dst_entry
*ndst
;
2501 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
, 0, info
->key
.tos
,
2502 info
->key
.label
, &info
->key
.u
.ipv6
.dst
,
2503 &info
->key
.u
.ipv6
.src
, dport
, sport
, NULL
, info
);
2505 return PTR_ERR(ndst
);
2507 #else /* !CONFIG_IPV6 */
2508 return -EPFNOSUPPORT
;
2511 info
->key
.tp_src
= sport
;
2512 info
->key
.tp_dst
= dport
;
2516 static const struct net_device_ops vxlan_netdev_ether_ops
= {
2517 .ndo_init
= vxlan_init
,
2518 .ndo_uninit
= vxlan_uninit
,
2519 .ndo_open
= vxlan_open
,
2520 .ndo_stop
= vxlan_stop
,
2521 .ndo_start_xmit
= vxlan_xmit
,
2522 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2523 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2524 .ndo_change_mtu
= vxlan_change_mtu
,
2525 .ndo_validate_addr
= eth_validate_addr
,
2526 .ndo_set_mac_address
= eth_mac_addr
,
2527 .ndo_fdb_add
= vxlan_fdb_add
,
2528 .ndo_fdb_del
= vxlan_fdb_delete
,
2529 .ndo_fdb_dump
= vxlan_fdb_dump
,
2530 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2533 static const struct net_device_ops vxlan_netdev_raw_ops
= {
2534 .ndo_init
= vxlan_init
,
2535 .ndo_uninit
= vxlan_uninit
,
2536 .ndo_open
= vxlan_open
,
2537 .ndo_stop
= vxlan_stop
,
2538 .ndo_start_xmit
= vxlan_xmit
,
2539 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2540 .ndo_change_mtu
= vxlan_change_mtu
,
2541 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2544 /* Info for udev, that this is a virtual tunnel endpoint */
2545 static struct device_type vxlan_type
= {
2549 /* Calls the ndo_udp_tunnel_add of the caller in order to
2550 * supply the listening VXLAN udp ports. Callers are expected
2551 * to implement the ndo_udp_tunnel_add.
2553 static void vxlan_push_rx_ports(struct net_device
*dev
)
2555 struct vxlan_sock
*vs
;
2556 struct net
*net
= dev_net(dev
);
2557 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2560 spin_lock(&vn
->sock_lock
);
2561 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2562 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
)
2563 udp_tunnel_push_rx_port(dev
, vs
->sock
,
2564 (vs
->flags
& VXLAN_F_GPE
) ?
2565 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2566 UDP_TUNNEL_TYPE_VXLAN
);
2568 spin_unlock(&vn
->sock_lock
);
2571 /* Initialize the device structure. */
2572 static void vxlan_setup(struct net_device
*dev
)
2574 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2577 eth_hw_addr_random(dev
);
2580 dev
->destructor
= free_netdev
;
2581 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2583 dev
->features
|= NETIF_F_LLTX
;
2584 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2585 dev
->features
|= NETIF_F_RXCSUM
;
2586 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2588 dev
->vlan_features
= dev
->features
;
2589 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2590 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2591 netif_keep_dst(dev
);
2592 dev
->priv_flags
|= IFF_NO_QUEUE
;
2594 INIT_LIST_HEAD(&vxlan
->next
);
2595 spin_lock_init(&vxlan
->hash_lock
);
2597 init_timer_deferrable(&vxlan
->age_timer
);
2598 vxlan
->age_timer
.function
= vxlan_cleanup
;
2599 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2601 vxlan
->cfg
.dst_port
= htons(vxlan_port
);
2605 gro_cells_init(&vxlan
->gro_cells
, dev
);
2607 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2608 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2611 static void vxlan_ether_setup(struct net_device
*dev
)
2613 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
2614 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2615 dev
->netdev_ops
= &vxlan_netdev_ether_ops
;
2618 static void vxlan_raw_setup(struct net_device
*dev
)
2620 dev
->header_ops
= NULL
;
2621 dev
->type
= ARPHRD_NONE
;
2622 dev
->hard_header_len
= 0;
2624 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
2625 dev
->netdev_ops
= &vxlan_netdev_raw_ops
;
2628 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2629 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2630 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2631 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2632 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2633 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2634 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2635 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2636 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2637 [IFLA_VXLAN_LABEL
] = { .type
= NLA_U32
},
2638 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2639 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2640 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2641 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2642 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2643 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2644 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2645 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2646 [IFLA_VXLAN_COLLECT_METADATA
] = { .type
= NLA_U8
},
2647 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2648 [IFLA_VXLAN_UDP_CSUM
] = { .type
= NLA_U8
},
2649 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
2650 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
2651 [IFLA_VXLAN_REMCSUM_TX
] = { .type
= NLA_U8
},
2652 [IFLA_VXLAN_REMCSUM_RX
] = { .type
= NLA_U8
},
2653 [IFLA_VXLAN_GBP
] = { .type
= NLA_FLAG
, },
2654 [IFLA_VXLAN_GPE
] = { .type
= NLA_FLAG
, },
2655 [IFLA_VXLAN_REMCSUM_NOPARTIAL
] = { .type
= NLA_FLAG
},
2658 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
2660 if (tb
[IFLA_ADDRESS
]) {
2661 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2662 pr_debug("invalid link address (not ethernet)\n");
2666 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2667 pr_debug("invalid all zero ethernet address\n");
2668 return -EADDRNOTAVAIL
;
2675 if (data
[IFLA_VXLAN_ID
]) {
2676 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2677 if (id
>= VXLAN_VID_MASK
)
2681 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2682 const struct ifla_vxlan_port_range
*p
2683 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2685 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2686 pr_debug("port range %u .. %u not valid\n",
2687 ntohs(p
->low
), ntohs(p
->high
));
2695 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2696 struct ethtool_drvinfo
*drvinfo
)
2698 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2699 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2702 static const struct ethtool_ops vxlan_ethtool_ops
= {
2703 .get_drvinfo
= vxlan_get_drvinfo
,
2704 .get_link
= ethtool_op_get_link
,
2707 static struct socket
*vxlan_create_sock(struct net
*net
, bool ipv6
,
2708 __be16 port
, u32 flags
)
2710 struct socket
*sock
;
2711 struct udp_port_cfg udp_conf
;
2714 memset(&udp_conf
, 0, sizeof(udp_conf
));
2717 udp_conf
.family
= AF_INET6
;
2718 udp_conf
.use_udp6_rx_checksums
=
2719 !(flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
);
2720 udp_conf
.ipv6_v6only
= 1;
2722 udp_conf
.family
= AF_INET
;
2725 udp_conf
.local_udp_port
= port
;
2727 /* Open UDP socket */
2728 err
= udp_sock_create(net
, &udp_conf
, &sock
);
2730 return ERR_PTR(err
);
2735 /* Create new listen socket if needed */
2736 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, bool ipv6
,
2737 __be16 port
, u32 flags
)
2739 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2740 struct vxlan_sock
*vs
;
2741 struct socket
*sock
;
2743 struct udp_tunnel_sock_cfg tunnel_cfg
;
2745 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
);
2747 return ERR_PTR(-ENOMEM
);
2749 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
2750 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
2752 sock
= vxlan_create_sock(net
, ipv6
, port
, flags
);
2754 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port
),
2757 return ERR_CAST(sock
);
2761 atomic_set(&vs
->refcnt
, 1);
2762 vs
->flags
= (flags
& VXLAN_F_RCV_FLAGS
);
2764 spin_lock(&vn
->sock_lock
);
2765 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
2766 udp_tunnel_notify_add_rx_port(sock
,
2767 (vs
->flags
& VXLAN_F_GPE
) ?
2768 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2769 UDP_TUNNEL_TYPE_VXLAN
);
2770 spin_unlock(&vn
->sock_lock
);
2772 /* Mark socket as an encapsulation socket. */
2773 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
2774 tunnel_cfg
.sk_user_data
= vs
;
2775 tunnel_cfg
.encap_type
= 1;
2776 tunnel_cfg
.encap_rcv
= vxlan_rcv
;
2777 tunnel_cfg
.encap_destroy
= NULL
;
2778 tunnel_cfg
.gro_receive
= vxlan_gro_receive
;
2779 tunnel_cfg
.gro_complete
= vxlan_gro_complete
;
2781 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
2786 static int __vxlan_sock_add(struct vxlan_dev
*vxlan
, bool ipv6
)
2788 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2789 struct vxlan_sock
*vs
= NULL
;
2791 if (!vxlan
->cfg
.no_share
) {
2792 spin_lock(&vn
->sock_lock
);
2793 vs
= vxlan_find_sock(vxlan
->net
, ipv6
? AF_INET6
: AF_INET
,
2794 vxlan
->cfg
.dst_port
, vxlan
->flags
);
2795 if (vs
&& !atomic_add_unless(&vs
->refcnt
, 1, 0)) {
2796 spin_unlock(&vn
->sock_lock
);
2799 spin_unlock(&vn
->sock_lock
);
2802 vs
= vxlan_socket_create(vxlan
->net
, ipv6
,
2803 vxlan
->cfg
.dst_port
, vxlan
->flags
);
2806 #if IS_ENABLED(CONFIG_IPV6)
2808 rcu_assign_pointer(vxlan
->vn6_sock
, vs
);
2811 rcu_assign_pointer(vxlan
->vn4_sock
, vs
);
2812 vxlan_vs_add_dev(vs
, vxlan
);
2816 static int vxlan_sock_add(struct vxlan_dev
*vxlan
)
2818 bool ipv6
= vxlan
->flags
& VXLAN_F_IPV6
;
2819 bool metadata
= vxlan
->flags
& VXLAN_F_COLLECT_METADATA
;
2822 RCU_INIT_POINTER(vxlan
->vn4_sock
, NULL
);
2823 #if IS_ENABLED(CONFIG_IPV6)
2824 RCU_INIT_POINTER(vxlan
->vn6_sock
, NULL
);
2825 if (ipv6
|| metadata
)
2826 ret
= __vxlan_sock_add(vxlan
, true);
2828 if (!ret
&& (!ipv6
|| metadata
))
2829 ret
= __vxlan_sock_add(vxlan
, false);
2831 vxlan_sock_release(vxlan
);
2835 static int vxlan_dev_configure(struct net
*src_net
, struct net_device
*dev
,
2836 struct vxlan_config
*conf
)
2838 struct vxlan_net
*vn
= net_generic(src_net
, vxlan_net_id
);
2839 struct vxlan_dev
*vxlan
= netdev_priv(dev
), *tmp
;
2840 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2841 unsigned short needed_headroom
= ETH_HLEN
;
2843 bool use_ipv6
= false;
2844 __be16 default_port
= vxlan
->cfg
.dst_port
;
2845 struct net_device
*lowerdev
= NULL
;
2847 if (conf
->flags
& VXLAN_F_GPE
) {
2848 /* For now, allow GPE only together with COLLECT_METADATA.
2849 * This can be relaxed later; in such case, the other side
2850 * of the PtP link will have to be provided.
2852 if ((conf
->flags
& ~VXLAN_F_ALLOWED_GPE
) ||
2853 !(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
2854 pr_info("unsupported combination of extensions\n");
2858 vxlan_raw_setup(dev
);
2860 vxlan_ether_setup(dev
);
2863 /* MTU range: 68 - 65535 */
2864 dev
->min_mtu
= ETH_MIN_MTU
;
2865 dev
->max_mtu
= ETH_MAX_MTU
;
2867 vxlan
->net
= src_net
;
2869 dst
->remote_vni
= conf
->vni
;
2871 memcpy(&dst
->remote_ip
, &conf
->remote_ip
, sizeof(conf
->remote_ip
));
2873 /* Unless IPv6 is explicitly requested, assume IPv4 */
2874 if (!dst
->remote_ip
.sa
.sa_family
)
2875 dst
->remote_ip
.sa
.sa_family
= AF_INET
;
2877 if (dst
->remote_ip
.sa
.sa_family
== AF_INET6
||
2878 vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET6
) {
2879 if (!IS_ENABLED(CONFIG_IPV6
))
2880 return -EPFNOSUPPORT
;
2882 vxlan
->flags
|= VXLAN_F_IPV6
;
2885 if (conf
->label
&& !use_ipv6
) {
2886 pr_info("label only supported in use with IPv6\n");
2890 if (conf
->remote_ifindex
) {
2891 lowerdev
= __dev_get_by_index(src_net
, conf
->remote_ifindex
);
2892 dst
->remote_ifindex
= conf
->remote_ifindex
;
2895 pr_info("ifindex %d does not exist\n", dst
->remote_ifindex
);
2899 #if IS_ENABLED(CONFIG_IPV6)
2901 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
2902 if (idev
&& idev
->cnf
.disable_ipv6
) {
2903 pr_info("IPv6 is disabled via sysctl\n");
2910 dev
->mtu
= lowerdev
->mtu
-
2911 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2913 needed_headroom
= lowerdev
->hard_header_len
;
2914 } else if (vxlan_addr_multicast(&dst
->remote_ip
)) {
2915 pr_info("multicast destination requires interface to be specified\n");
2920 int max_mtu
= ETH_MAX_MTU
;
2923 max_mtu
= lowerdev
->mtu
;
2925 max_mtu
-= (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2927 if (conf
->mtu
< dev
->min_mtu
|| conf
->mtu
> dev
->max_mtu
)
2930 dev
->mtu
= conf
->mtu
;
2932 if (conf
->mtu
> max_mtu
)
2936 if (use_ipv6
|| conf
->flags
& VXLAN_F_COLLECT_METADATA
)
2937 needed_headroom
+= VXLAN6_HEADROOM
;
2939 needed_headroom
+= VXLAN_HEADROOM
;
2940 dev
->needed_headroom
= needed_headroom
;
2942 memcpy(&vxlan
->cfg
, conf
, sizeof(*conf
));
2943 if (!vxlan
->cfg
.dst_port
) {
2944 if (conf
->flags
& VXLAN_F_GPE
)
2945 vxlan
->cfg
.dst_port
= htons(4790); /* IANA VXLAN-GPE port */
2947 vxlan
->cfg
.dst_port
= default_port
;
2949 vxlan
->flags
|= conf
->flags
;
2951 if (!vxlan
->cfg
.age_interval
)
2952 vxlan
->cfg
.age_interval
= FDB_AGE_DEFAULT
;
2954 list_for_each_entry(tmp
, &vn
->vxlan_list
, next
) {
2955 if (tmp
->cfg
.vni
== conf
->vni
&&
2956 (tmp
->default_dst
.remote_ip
.sa
.sa_family
== AF_INET6
||
2957 tmp
->cfg
.saddr
.sa
.sa_family
== AF_INET6
) == use_ipv6
&&
2958 tmp
->cfg
.dst_port
== vxlan
->cfg
.dst_port
&&
2959 (tmp
->flags
& VXLAN_F_RCV_FLAGS
) ==
2960 (vxlan
->flags
& VXLAN_F_RCV_FLAGS
)) {
2961 pr_info("duplicate VNI %u\n", be32_to_cpu(conf
->vni
));
2966 dev
->ethtool_ops
= &vxlan_ethtool_ops
;
2968 /* create an fdb entry for a valid default destination */
2969 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
2970 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
2971 &vxlan
->default_dst
.remote_ip
,
2972 NUD_REACHABLE
|NUD_PERMANENT
,
2973 NLM_F_EXCL
|NLM_F_CREATE
,
2974 vxlan
->cfg
.dst_port
,
2975 vxlan
->default_dst
.remote_vni
,
2976 vxlan
->default_dst
.remote_vni
,
2977 vxlan
->default_dst
.remote_ifindex
,
2983 err
= register_netdevice(dev
);
2985 vxlan_fdb_delete_default(vxlan
, vxlan
->cfg
.vni
);
2989 list_add(&vxlan
->next
, &vn
->vxlan_list
);
2994 static int vxlan_newlink(struct net
*src_net
, struct net_device
*dev
,
2995 struct nlattr
*tb
[], struct nlattr
*data
[])
2997 struct vxlan_config conf
;
2999 memset(&conf
, 0, sizeof(conf
));
3001 if (data
[IFLA_VXLAN_ID
])
3002 conf
.vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3004 if (data
[IFLA_VXLAN_GROUP
]) {
3005 conf
.remote_ip
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_GROUP
]);
3006 } else if (data
[IFLA_VXLAN_GROUP6
]) {
3007 if (!IS_ENABLED(CONFIG_IPV6
))
3008 return -EPFNOSUPPORT
;
3010 conf
.remote_ip
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_GROUP6
]);
3011 conf
.remote_ip
.sa
.sa_family
= AF_INET6
;
3014 if (data
[IFLA_VXLAN_LOCAL
]) {
3015 conf
.saddr
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_LOCAL
]);
3016 conf
.saddr
.sa
.sa_family
= AF_INET
;
3017 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
3018 if (!IS_ENABLED(CONFIG_IPV6
))
3019 return -EPFNOSUPPORT
;
3021 /* TODO: respect scope id */
3022 conf
.saddr
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_LOCAL6
]);
3023 conf
.saddr
.sa
.sa_family
= AF_INET6
;
3026 if (data
[IFLA_VXLAN_LINK
])
3027 conf
.remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]);
3029 if (data
[IFLA_VXLAN_TOS
])
3030 conf
.tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
3032 if (data
[IFLA_VXLAN_TTL
])
3033 conf
.ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
3035 if (data
[IFLA_VXLAN_LABEL
])
3036 conf
.label
= nla_get_be32(data
[IFLA_VXLAN_LABEL
]) &
3037 IPV6_FLOWLABEL_MASK
;
3039 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
3040 conf
.flags
|= VXLAN_F_LEARN
;
3042 if (data
[IFLA_VXLAN_AGEING
])
3043 conf
.age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
3045 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
3046 conf
.flags
|= VXLAN_F_PROXY
;
3048 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
3049 conf
.flags
|= VXLAN_F_RSC
;
3051 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
3052 conf
.flags
|= VXLAN_F_L2MISS
;
3054 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
3055 conf
.flags
|= VXLAN_F_L3MISS
;
3057 if (data
[IFLA_VXLAN_LIMIT
])
3058 conf
.addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
3060 if (data
[IFLA_VXLAN_COLLECT_METADATA
] &&
3061 nla_get_u8(data
[IFLA_VXLAN_COLLECT_METADATA
]))
3062 conf
.flags
|= VXLAN_F_COLLECT_METADATA
;
3064 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
3065 const struct ifla_vxlan_port_range
*p
3066 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
3067 conf
.port_min
= ntohs(p
->low
);
3068 conf
.port_max
= ntohs(p
->high
);
3071 if (data
[IFLA_VXLAN_PORT
])
3072 conf
.dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
3074 if (data
[IFLA_VXLAN_UDP_CSUM
] &&
3075 !nla_get_u8(data
[IFLA_VXLAN_UDP_CSUM
]))
3076 conf
.flags
|= VXLAN_F_UDP_ZERO_CSUM_TX
;
3078 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] &&
3079 nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
3080 conf
.flags
|= VXLAN_F_UDP_ZERO_CSUM6_TX
;
3082 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] &&
3083 nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
3084 conf
.flags
|= VXLAN_F_UDP_ZERO_CSUM6_RX
;
3086 if (data
[IFLA_VXLAN_REMCSUM_TX
] &&
3087 nla_get_u8(data
[IFLA_VXLAN_REMCSUM_TX
]))
3088 conf
.flags
|= VXLAN_F_REMCSUM_TX
;
3090 if (data
[IFLA_VXLAN_REMCSUM_RX
] &&
3091 nla_get_u8(data
[IFLA_VXLAN_REMCSUM_RX
]))
3092 conf
.flags
|= VXLAN_F_REMCSUM_RX
;
3094 if (data
[IFLA_VXLAN_GBP
])
3095 conf
.flags
|= VXLAN_F_GBP
;
3097 if (data
[IFLA_VXLAN_GPE
])
3098 conf
.flags
|= VXLAN_F_GPE
;
3100 if (data
[IFLA_VXLAN_REMCSUM_NOPARTIAL
])
3101 conf
.flags
|= VXLAN_F_REMCSUM_NOPARTIAL
;
3104 conf
.mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3106 return vxlan_dev_configure(src_net
, dev
, &conf
);
3109 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
3111 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3112 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
3114 vxlan_flush(vxlan
, true);
3116 spin_lock(&vn
->sock_lock
);
3117 if (!hlist_unhashed(&vxlan
->hlist
))
3118 hlist_del_rcu(&vxlan
->hlist
);
3119 spin_unlock(&vn
->sock_lock
);
3121 gro_cells_destroy(&vxlan
->gro_cells
);
3122 list_del(&vxlan
->next
);
3123 unregister_netdevice_queue(dev
, head
);
3126 static size_t vxlan_get_size(const struct net_device
*dev
)
3129 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
3130 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
3131 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
3132 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
3133 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
3134 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
3135 nla_total_size(sizeof(__be32
)) + /* IFLA_VXLAN_LABEL */
3136 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
3137 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
3138 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
3139 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
3140 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
3141 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_COLLECT_METADATA */
3142 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
3143 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
3144 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
3145 nla_total_size(sizeof(__be16
)) + /* IFLA_VXLAN_PORT */
3146 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_CSUM */
3147 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3148 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3149 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_TX */
3150 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_RX */
3154 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
3156 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3157 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3158 struct ifla_vxlan_port_range ports
= {
3159 .low
= htons(vxlan
->cfg
.port_min
),
3160 .high
= htons(vxlan
->cfg
.port_max
),
3163 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, be32_to_cpu(dst
->remote_vni
)))
3164 goto nla_put_failure
;
3166 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3167 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
3168 if (nla_put_in_addr(skb
, IFLA_VXLAN_GROUP
,
3169 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
3170 goto nla_put_failure
;
3171 #if IS_ENABLED(CONFIG_IPV6)
3173 if (nla_put_in6_addr(skb
, IFLA_VXLAN_GROUP6
,
3174 &dst
->remote_ip
.sin6
.sin6_addr
))
3175 goto nla_put_failure
;
3180 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
3181 goto nla_put_failure
;
3183 if (!vxlan_addr_any(&vxlan
->cfg
.saddr
)) {
3184 if (vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET
) {
3185 if (nla_put_in_addr(skb
, IFLA_VXLAN_LOCAL
,
3186 vxlan
->cfg
.saddr
.sin
.sin_addr
.s_addr
))
3187 goto nla_put_failure
;
3188 #if IS_ENABLED(CONFIG_IPV6)
3190 if (nla_put_in6_addr(skb
, IFLA_VXLAN_LOCAL6
,
3191 &vxlan
->cfg
.saddr
.sin6
.sin6_addr
))
3192 goto nla_put_failure
;
3197 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->cfg
.ttl
) ||
3198 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->cfg
.tos
) ||
3199 nla_put_be32(skb
, IFLA_VXLAN_LABEL
, vxlan
->cfg
.label
) ||
3200 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
3201 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
3202 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
3203 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
3204 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
3205 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
3206 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
3207 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
3208 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
3209 nla_put_u8(skb
, IFLA_VXLAN_COLLECT_METADATA
,
3210 !!(vxlan
->flags
& VXLAN_F_COLLECT_METADATA
)) ||
3211 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->cfg
.age_interval
) ||
3212 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->cfg
.addrmax
) ||
3213 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->cfg
.dst_port
) ||
3214 nla_put_u8(skb
, IFLA_VXLAN_UDP_CSUM
,
3215 !(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM_TX
)) ||
3216 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_TX
,
3217 !!(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
)) ||
3218 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_RX
,
3219 !!(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
)) ||
3220 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_TX
,
3221 !!(vxlan
->flags
& VXLAN_F_REMCSUM_TX
)) ||
3222 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_RX
,
3223 !!(vxlan
->flags
& VXLAN_F_REMCSUM_RX
)))
3224 goto nla_put_failure
;
3226 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
3227 goto nla_put_failure
;
3229 if (vxlan
->flags
& VXLAN_F_GBP
&&
3230 nla_put_flag(skb
, IFLA_VXLAN_GBP
))
3231 goto nla_put_failure
;
3233 if (vxlan
->flags
& VXLAN_F_GPE
&&
3234 nla_put_flag(skb
, IFLA_VXLAN_GPE
))
3235 goto nla_put_failure
;
3237 if (vxlan
->flags
& VXLAN_F_REMCSUM_NOPARTIAL
&&
3238 nla_put_flag(skb
, IFLA_VXLAN_REMCSUM_NOPARTIAL
))
3239 goto nla_put_failure
;
3247 static struct net
*vxlan_get_link_net(const struct net_device
*dev
)
3249 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3254 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
3256 .maxtype
= IFLA_VXLAN_MAX
,
3257 .policy
= vxlan_policy
,
3258 .priv_size
= sizeof(struct vxlan_dev
),
3259 .setup
= vxlan_setup
,
3260 .validate
= vxlan_validate
,
3261 .newlink
= vxlan_newlink
,
3262 .dellink
= vxlan_dellink
,
3263 .get_size
= vxlan_get_size
,
3264 .fill_info
= vxlan_fill_info
,
3265 .get_link_net
= vxlan_get_link_net
,
3268 struct net_device
*vxlan_dev_create(struct net
*net
, const char *name
,
3269 u8 name_assign_type
,
3270 struct vxlan_config
*conf
)
3272 struct nlattr
*tb
[IFLA_MAX
+ 1];
3273 struct net_device
*dev
;
3276 memset(&tb
, 0, sizeof(tb
));
3278 dev
= rtnl_create_link(net
, name
, name_assign_type
,
3279 &vxlan_link_ops
, tb
);
3283 err
= vxlan_dev_configure(net
, dev
, conf
);
3286 return ERR_PTR(err
);
3289 err
= rtnl_configure_link(dev
, NULL
);
3291 LIST_HEAD(list_kill
);
3293 vxlan_dellink(dev
, &list_kill
);
3294 unregister_netdevice_many(&list_kill
);
3295 return ERR_PTR(err
);
3300 EXPORT_SYMBOL_GPL(vxlan_dev_create
);
3302 static void vxlan_handle_lowerdev_unregister(struct vxlan_net
*vn
,
3303 struct net_device
*dev
)
3305 struct vxlan_dev
*vxlan
, *next
;
3306 LIST_HEAD(list_kill
);
3308 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3309 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3311 /* In case we created vxlan device with carrier
3312 * and we loose the carrier due to module unload
3313 * we also need to remove vxlan device. In other
3314 * cases, it's not necessary and remote_ifindex
3315 * is 0 here, so no matches.
3317 if (dst
->remote_ifindex
== dev
->ifindex
)
3318 vxlan_dellink(vxlan
->dev
, &list_kill
);
3321 unregister_netdevice_many(&list_kill
);
3324 static int vxlan_netdevice_event(struct notifier_block
*unused
,
3325 unsigned long event
, void *ptr
)
3327 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3328 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
3330 if (event
== NETDEV_UNREGISTER
)
3331 vxlan_handle_lowerdev_unregister(vn
, dev
);
3332 else if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
)
3333 vxlan_push_rx_ports(dev
);
3338 static struct notifier_block vxlan_notifier_block __read_mostly
= {
3339 .notifier_call
= vxlan_netdevice_event
,
3342 static __net_init
int vxlan_init_net(struct net
*net
)
3344 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3347 INIT_LIST_HEAD(&vn
->vxlan_list
);
3348 spin_lock_init(&vn
->sock_lock
);
3350 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
3351 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
3356 static void __net_exit
vxlan_exit_net(struct net
*net
)
3358 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3359 struct vxlan_dev
*vxlan
, *next
;
3360 struct net_device
*dev
, *aux
;
3364 for_each_netdev_safe(net
, dev
, aux
)
3365 if (dev
->rtnl_link_ops
== &vxlan_link_ops
)
3366 unregister_netdevice_queue(dev
, &list
);
3368 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3369 /* If vxlan->dev is in the same netns, it has already been added
3370 * to the list by the previous loop.
3372 if (!net_eq(dev_net(vxlan
->dev
), net
)) {
3373 gro_cells_destroy(&vxlan
->gro_cells
);
3374 unregister_netdevice_queue(vxlan
->dev
, &list
);
3378 unregister_netdevice_many(&list
);
3382 static struct pernet_operations vxlan_net_ops
= {
3383 .init
= vxlan_init_net
,
3384 .exit
= vxlan_exit_net
,
3385 .id
= &vxlan_net_id
,
3386 .size
= sizeof(struct vxlan_net
),
3389 static int __init
vxlan_init_module(void)
3393 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
3395 rc
= register_pernet_subsys(&vxlan_net_ops
);
3399 rc
= register_netdevice_notifier(&vxlan_notifier_block
);
3403 rc
= rtnl_link_register(&vxlan_link_ops
);
3409 unregister_netdevice_notifier(&vxlan_notifier_block
);
3411 unregister_pernet_subsys(&vxlan_net_ops
);
3415 late_initcall(vxlan_init_module
);
3417 static void __exit
vxlan_cleanup_module(void)
3419 rtnl_link_unregister(&vxlan_link_ops
);
3420 unregister_netdevice_notifier(&vxlan_notifier_block
);
3421 unregister_pernet_subsys(&vxlan_net_ops
);
3422 /* rcu_barrier() is called by netns */
3424 module_exit(vxlan_cleanup_module
);
3426 MODULE_LICENSE("GPL");
3427 MODULE_VERSION(VXLAN_VERSION
);
3428 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3429 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3430 MODULE_ALIAS_RTNL_LINK("vxlan");