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 static void vxlan_vs_del_dev(struct vxlan_dev
*vxlan
);
64 /* per-network namespace private data for this module */
66 struct list_head vxlan_list
;
67 struct hlist_head sock_list
[PORT_HASH_SIZE
];
71 /* Forwarding table entry */
73 struct hlist_node hlist
; /* linked list of entries */
75 unsigned long updated
; /* jiffies */
77 struct list_head remotes
;
78 u8 eth_addr
[ETH_ALEN
];
79 u16 state
; /* see ndm_state */
81 u8 flags
; /* see ndm_flags */
84 /* salt for hash table */
85 static u32 vxlan_salt __read_mostly
;
87 static inline bool vxlan_collect_metadata(struct vxlan_sock
*vs
)
89 return vs
->flags
& VXLAN_F_COLLECT_METADATA
||
90 ip_tunnel_collect_metadata();
93 #if IS_ENABLED(CONFIG_IPV6)
95 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
97 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
99 if (a
->sa
.sa_family
== AF_INET6
)
100 return ipv6_addr_equal(&a
->sin6
.sin6_addr
, &b
->sin6
.sin6_addr
);
102 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
105 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
107 if (ipa
->sa
.sa_family
== AF_INET6
)
108 return ipv6_addr_any(&ipa
->sin6
.sin6_addr
);
110 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
113 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
115 if (ipa
->sa
.sa_family
== AF_INET6
)
116 return ipv6_addr_is_multicast(&ipa
->sin6
.sin6_addr
);
118 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
121 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
123 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
124 ip
->sin6
.sin6_addr
= nla_get_in6_addr(nla
);
125 ip
->sa
.sa_family
= AF_INET6
;
127 } else if (nla_len(nla
) >= sizeof(__be32
)) {
128 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
129 ip
->sa
.sa_family
= AF_INET
;
132 return -EAFNOSUPPORT
;
136 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
137 const union vxlan_addr
*ip
)
139 if (ip
->sa
.sa_family
== AF_INET6
)
140 return nla_put_in6_addr(skb
, attr
, &ip
->sin6
.sin6_addr
);
142 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
145 #else /* !CONFIG_IPV6 */
148 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
150 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
153 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
155 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
158 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
160 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
163 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
165 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
166 return -EAFNOSUPPORT
;
167 } else if (nla_len(nla
) >= sizeof(__be32
)) {
168 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
169 ip
->sa
.sa_family
= AF_INET
;
172 return -EAFNOSUPPORT
;
176 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
177 const union vxlan_addr
*ip
)
179 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
183 /* Virtual Network hash table head */
184 static inline struct hlist_head
*vni_head(struct vxlan_sock
*vs
, __be32 vni
)
186 return &vs
->vni_list
[hash_32((__force u32
)vni
, VNI_HASH_BITS
)];
189 /* Socket hash table head */
190 static inline struct hlist_head
*vs_head(struct net
*net
, __be16 port
)
192 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
194 return &vn
->sock_list
[hash_32(ntohs(port
), PORT_HASH_BITS
)];
197 /* First remote destination for a forwarding entry.
198 * Guaranteed to be non-NULL because remotes are never deleted.
200 static inline struct vxlan_rdst
*first_remote_rcu(struct vxlan_fdb
*fdb
)
202 return list_entry_rcu(fdb
->remotes
.next
, struct vxlan_rdst
, list
);
205 static inline struct vxlan_rdst
*first_remote_rtnl(struct vxlan_fdb
*fdb
)
207 return list_first_entry(&fdb
->remotes
, struct vxlan_rdst
, list
);
210 /* Find VXLAN socket based on network namespace, address family and UDP port
211 * and enabled unshareable flags.
213 static struct vxlan_sock
*vxlan_find_sock(struct net
*net
, sa_family_t family
,
214 __be16 port
, u32 flags
)
216 struct vxlan_sock
*vs
;
218 flags
&= VXLAN_F_RCV_FLAGS
;
220 hlist_for_each_entry_rcu(vs
, vs_head(net
, port
), hlist
) {
221 if (inet_sk(vs
->sock
->sk
)->inet_sport
== port
&&
222 vxlan_get_sk_family(vs
) == family
&&
229 static struct vxlan_dev
*vxlan_vs_find_vni(struct vxlan_sock
*vs
, int ifindex
,
232 struct vxlan_dev_node
*node
;
234 /* For flow based devices, map all packets to VNI 0 */
235 if (vs
->flags
& VXLAN_F_COLLECT_METADATA
)
238 hlist_for_each_entry_rcu(node
, vni_head(vs
, vni
), hlist
) {
239 if (node
->vxlan
->default_dst
.remote_vni
!= vni
)
242 if (IS_ENABLED(CONFIG_IPV6
)) {
243 const struct vxlan_config
*cfg
= &node
->vxlan
->cfg
;
245 if ((cfg
->flags
& VXLAN_F_IPV6_LINKLOCAL
) &&
246 cfg
->remote_ifindex
!= ifindex
)
256 /* Look up VNI in a per net namespace table */
257 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, int ifindex
,
258 __be32 vni
, sa_family_t family
,
259 __be16 port
, u32 flags
)
261 struct vxlan_sock
*vs
;
263 vs
= vxlan_find_sock(net
, family
, port
, flags
);
267 return vxlan_vs_find_vni(vs
, ifindex
, vni
);
270 /* Fill in neighbour message in skbuff. */
271 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
272 const struct vxlan_fdb
*fdb
,
273 u32 portid
, u32 seq
, int type
, unsigned int flags
,
274 const struct vxlan_rdst
*rdst
)
276 unsigned long now
= jiffies
;
277 struct nda_cacheinfo ci
;
278 struct nlmsghdr
*nlh
;
280 bool send_ip
, send_eth
;
282 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
286 ndm
= nlmsg_data(nlh
);
287 memset(ndm
, 0, sizeof(*ndm
));
289 send_eth
= send_ip
= true;
291 if (type
== RTM_GETNEIGH
) {
292 send_ip
= !vxlan_addr_any(&rdst
->remote_ip
);
293 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
294 ndm
->ndm_family
= send_ip
? rdst
->remote_ip
.sa
.sa_family
: AF_INET
;
296 ndm
->ndm_family
= AF_BRIDGE
;
297 ndm
->ndm_state
= fdb
->state
;
298 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
299 ndm
->ndm_flags
= fdb
->flags
;
300 ndm
->ndm_type
= RTN_UNICAST
;
302 if (!net_eq(dev_net(vxlan
->dev
), vxlan
->net
) &&
303 nla_put_s32(skb
, NDA_LINK_NETNSID
,
304 peernet2id(dev_net(vxlan
->dev
), vxlan
->net
)))
305 goto nla_put_failure
;
307 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
308 goto nla_put_failure
;
310 if (send_ip
&& vxlan_nla_put_addr(skb
, NDA_DST
, &rdst
->remote_ip
))
311 goto nla_put_failure
;
313 if (rdst
->remote_port
&& rdst
->remote_port
!= vxlan
->cfg
.dst_port
&&
314 nla_put_be16(skb
, NDA_PORT
, rdst
->remote_port
))
315 goto nla_put_failure
;
316 if (rdst
->remote_vni
!= vxlan
->default_dst
.remote_vni
&&
317 nla_put_u32(skb
, NDA_VNI
, be32_to_cpu(rdst
->remote_vni
)))
318 goto nla_put_failure
;
319 if ((vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) && fdb
->vni
&&
320 nla_put_u32(skb
, NDA_SRC_VNI
,
321 be32_to_cpu(fdb
->vni
)))
322 goto nla_put_failure
;
323 if (rdst
->remote_ifindex
&&
324 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
325 goto nla_put_failure
;
327 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
328 ci
.ndm_confirmed
= 0;
329 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
332 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
333 goto nla_put_failure
;
339 nlmsg_cancel(skb
, nlh
);
343 static inline size_t vxlan_nlmsg_size(void)
345 return NLMSG_ALIGN(sizeof(struct ndmsg
))
346 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
347 + nla_total_size(sizeof(struct in6_addr
)) /* NDA_DST */
348 + nla_total_size(sizeof(__be16
)) /* NDA_PORT */
349 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
350 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
351 + nla_total_size(sizeof(__s32
)) /* NDA_LINK_NETNSID */
352 + nla_total_size(sizeof(struct nda_cacheinfo
));
355 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*fdb
,
356 struct vxlan_rdst
*rd
, int type
)
358 struct net
*net
= dev_net(vxlan
->dev
);
362 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
366 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0, rd
);
368 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
369 WARN_ON(err
== -EMSGSIZE
);
374 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
378 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
381 static void vxlan_ip_miss(struct net_device
*dev
, union vxlan_addr
*ipa
)
383 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
384 struct vxlan_fdb f
= {
387 struct vxlan_rdst remote
= {
388 .remote_ip
= *ipa
, /* goes to NDA_DST */
389 .remote_vni
= cpu_to_be32(VXLAN_N_VID
),
392 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
395 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
397 struct vxlan_fdb f
= {
400 struct vxlan_rdst remote
= { };
402 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
404 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
407 /* Hash Ethernet address */
408 static u32
eth_hash(const unsigned char *addr
)
410 u64 value
= get_unaligned((u64
*)addr
);
412 /* only want 6 bytes */
418 return hash_64(value
, FDB_HASH_BITS
);
421 static u32
eth_vni_hash(const unsigned char *addr
, __be32 vni
)
423 /* use 1 byte of OUI and 3 bytes of NIC */
424 u32 key
= get_unaligned((u32
*)(addr
+ 2));
426 return jhash_2words(key
, vni
, vxlan_salt
) & (FDB_HASH_SIZE
- 1);
429 /* Hash chain to use given mac address */
430 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
431 const u8
*mac
, __be32 vni
)
433 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
)
434 return &vxlan
->fdb_head
[eth_vni_hash(mac
, vni
)];
436 return &vxlan
->fdb_head
[eth_hash(mac
)];
439 /* Look up Ethernet address in forwarding table */
440 static struct vxlan_fdb
*__vxlan_find_mac(struct vxlan_dev
*vxlan
,
441 const u8
*mac
, __be32 vni
)
443 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
, vni
);
446 hlist_for_each_entry_rcu(f
, head
, hlist
) {
447 if (ether_addr_equal(mac
, f
->eth_addr
)) {
448 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) {
460 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
461 const u8
*mac
, __be32 vni
)
465 f
= __vxlan_find_mac(vxlan
, mac
, vni
);
472 /* caller should hold vxlan->hash_lock */
473 static struct vxlan_rdst
*vxlan_fdb_find_rdst(struct vxlan_fdb
*f
,
474 union vxlan_addr
*ip
, __be16 port
,
475 __be32 vni
, __u32 ifindex
)
477 struct vxlan_rdst
*rd
;
479 list_for_each_entry(rd
, &f
->remotes
, list
) {
480 if (vxlan_addr_equal(&rd
->remote_ip
, ip
) &&
481 rd
->remote_port
== port
&&
482 rd
->remote_vni
== vni
&&
483 rd
->remote_ifindex
== ifindex
)
490 /* Replace destination of unicast mac */
491 static int vxlan_fdb_replace(struct vxlan_fdb
*f
,
492 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
495 struct vxlan_rdst
*rd
;
497 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
501 rd
= list_first_entry_or_null(&f
->remotes
, struct vxlan_rdst
, list
);
505 dst_cache_reset(&rd
->dst_cache
);
507 rd
->remote_port
= port
;
508 rd
->remote_vni
= vni
;
509 rd
->remote_ifindex
= ifindex
;
513 /* Add/update destinations for multicast */
514 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
515 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
516 __u32 ifindex
, struct vxlan_rdst
**rdp
)
518 struct vxlan_rdst
*rd
;
520 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
524 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
528 if (dst_cache_init(&rd
->dst_cache
, GFP_ATOMIC
)) {
534 rd
->remote_port
= port
;
535 rd
->remote_vni
= vni
;
536 rd
->remote_ifindex
= ifindex
;
538 list_add_tail_rcu(&rd
->list
, &f
->remotes
);
544 static struct vxlanhdr
*vxlan_gro_remcsum(struct sk_buff
*skb
,
546 struct vxlanhdr
*vh
, size_t hdrlen
,
548 struct gro_remcsum
*grc
,
551 size_t start
, offset
;
553 if (skb
->remcsum_offload
)
556 if (!NAPI_GRO_CB(skb
)->csum_valid
)
559 start
= vxlan_rco_start(vni_field
);
560 offset
= start
+ vxlan_rco_offset(vni_field
);
562 vh
= skb_gro_remcsum_process(skb
, (void *)vh
, off
, hdrlen
,
563 start
, offset
, grc
, nopartial
);
565 skb
->remcsum_offload
= 1;
570 static struct sk_buff
**vxlan_gro_receive(struct sock
*sk
,
571 struct sk_buff
**head
,
574 struct sk_buff
*p
, **pp
= NULL
;
575 struct vxlanhdr
*vh
, *vh2
;
576 unsigned int hlen
, off_vx
;
578 struct vxlan_sock
*vs
= rcu_dereference_sk_user_data(sk
);
580 struct gro_remcsum grc
;
582 skb_gro_remcsum_init(&grc
);
584 off_vx
= skb_gro_offset(skb
);
585 hlen
= off_vx
+ sizeof(*vh
);
586 vh
= skb_gro_header_fast(skb
, off_vx
);
587 if (skb_gro_header_hard(skb
, hlen
)) {
588 vh
= skb_gro_header_slow(skb
, hlen
, off_vx
);
593 skb_gro_postpull_rcsum(skb
, vh
, sizeof(struct vxlanhdr
));
595 flags
= vh
->vx_flags
;
597 if ((flags
& VXLAN_HF_RCO
) && (vs
->flags
& VXLAN_F_REMCSUM_RX
)) {
598 vh
= vxlan_gro_remcsum(skb
, off_vx
, vh
, sizeof(struct vxlanhdr
),
601 VXLAN_F_REMCSUM_NOPARTIAL
));
607 skb_gro_pull(skb
, sizeof(struct vxlanhdr
)); /* pull vxlan header */
609 for (p
= *head
; p
; p
= p
->next
) {
610 if (!NAPI_GRO_CB(p
)->same_flow
)
613 vh2
= (struct vxlanhdr
*)(p
->data
+ off_vx
);
614 if (vh
->vx_flags
!= vh2
->vx_flags
||
615 vh
->vx_vni
!= vh2
->vx_vni
) {
616 NAPI_GRO_CB(p
)->same_flow
= 0;
621 pp
= call_gro_receive(eth_gro_receive
, head
, skb
);
625 skb_gro_remcsum_cleanup(skb
, &grc
);
626 NAPI_GRO_CB(skb
)->flush
|= flush
;
631 static int vxlan_gro_complete(struct sock
*sk
, struct sk_buff
*skb
, int nhoff
)
633 /* Sets 'skb->inner_mac_header' since we are always called with
634 * 'skb->encapsulation' set.
636 return eth_gro_complete(skb
, nhoff
+ sizeof(struct vxlanhdr
));
639 /* Add new entry to forwarding table -- assumes lock held */
640 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
641 const u8
*mac
, union vxlan_addr
*ip
,
642 __u16 state
, __u16 flags
,
643 __be16 port
, __be32 src_vni
, __be32 vni
,
644 __u32 ifindex
, __u8 ndm_flags
)
646 struct vxlan_rdst
*rd
= NULL
;
651 f
= __vxlan_find_mac(vxlan
, mac
, src_vni
);
653 if (flags
& NLM_F_EXCL
) {
654 netdev_dbg(vxlan
->dev
,
655 "lost race to create %pM\n", mac
);
658 if (f
->state
!= state
) {
660 f
->updated
= jiffies
;
663 if (f
->flags
!= ndm_flags
) {
664 f
->flags
= ndm_flags
;
665 f
->updated
= jiffies
;
668 if ((flags
& NLM_F_REPLACE
)) {
669 /* Only change unicasts */
670 if (!(is_multicast_ether_addr(f
->eth_addr
) ||
671 is_zero_ether_addr(f
->eth_addr
))) {
672 notify
|= vxlan_fdb_replace(f
, ip
, port
, vni
,
677 if ((flags
& NLM_F_APPEND
) &&
678 (is_multicast_ether_addr(f
->eth_addr
) ||
679 is_zero_ether_addr(f
->eth_addr
))) {
680 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
687 if (!(flags
& NLM_F_CREATE
))
690 if (vxlan
->cfg
.addrmax
&&
691 vxlan
->addrcnt
>= vxlan
->cfg
.addrmax
)
694 /* Disallow replace to add a multicast entry */
695 if ((flags
& NLM_F_REPLACE
) &&
696 (is_multicast_ether_addr(mac
) || is_zero_ether_addr(mac
)))
699 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
700 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
706 f
->flags
= ndm_flags
;
707 f
->updated
= f
->used
= jiffies
;
709 INIT_LIST_HEAD(&f
->remotes
);
710 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
712 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
719 hlist_add_head_rcu(&f
->hlist
,
720 vxlan_fdb_head(vxlan
, mac
, src_vni
));
725 rd
= first_remote_rtnl(f
);
726 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_NEWNEIGH
);
732 static void vxlan_fdb_free(struct rcu_head
*head
)
734 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
735 struct vxlan_rdst
*rd
, *nd
;
737 list_for_each_entry_safe(rd
, nd
, &f
->remotes
, list
) {
738 dst_cache_destroy(&rd
->dst_cache
);
744 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
746 netdev_dbg(vxlan
->dev
,
747 "delete %pM\n", f
->eth_addr
);
750 vxlan_fdb_notify(vxlan
, f
, first_remote_rtnl(f
), RTM_DELNEIGH
);
752 hlist_del_rcu(&f
->hlist
);
753 call_rcu(&f
->rcu
, vxlan_fdb_free
);
756 static void vxlan_dst_free(struct rcu_head
*head
)
758 struct vxlan_rdst
*rd
= container_of(head
, struct vxlan_rdst
, rcu
);
760 dst_cache_destroy(&rd
->dst_cache
);
764 static void vxlan_fdb_dst_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
,
765 struct vxlan_rdst
*rd
)
767 list_del_rcu(&rd
->list
);
768 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_DELNEIGH
);
769 call_rcu(&rd
->rcu
, vxlan_dst_free
);
772 static int vxlan_fdb_parse(struct nlattr
*tb
[], struct vxlan_dev
*vxlan
,
773 union vxlan_addr
*ip
, __be16
*port
, __be32
*src_vni
,
774 __be32
*vni
, u32
*ifindex
)
776 struct net
*net
= dev_net(vxlan
->dev
);
780 err
= vxlan_nla_get_addr(ip
, tb
[NDA_DST
]);
784 union vxlan_addr
*remote
= &vxlan
->default_dst
.remote_ip
;
785 if (remote
->sa
.sa_family
== AF_INET
) {
786 ip
->sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
787 ip
->sa
.sa_family
= AF_INET
;
788 #if IS_ENABLED(CONFIG_IPV6)
790 ip
->sin6
.sin6_addr
= in6addr_any
;
791 ip
->sa
.sa_family
= AF_INET6
;
797 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
799 *port
= nla_get_be16(tb
[NDA_PORT
]);
801 *port
= vxlan
->cfg
.dst_port
;
805 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
807 *vni
= cpu_to_be32(nla_get_u32(tb
[NDA_VNI
]));
809 *vni
= vxlan
->default_dst
.remote_vni
;
812 if (tb
[NDA_SRC_VNI
]) {
813 if (nla_len(tb
[NDA_SRC_VNI
]) != sizeof(u32
))
815 *src_vni
= cpu_to_be32(nla_get_u32(tb
[NDA_SRC_VNI
]));
817 *src_vni
= vxlan
->default_dst
.remote_vni
;
820 if (tb
[NDA_IFINDEX
]) {
821 struct net_device
*tdev
;
823 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
825 *ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
826 tdev
= __dev_get_by_index(net
, *ifindex
);
828 return -EADDRNOTAVAIL
;
836 /* Add static entry (via netlink) */
837 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
838 struct net_device
*dev
,
839 const unsigned char *addr
, u16 vid
, u16 flags
)
841 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
842 /* struct net *net = dev_net(vxlan->dev); */
849 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
850 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
855 if (tb
[NDA_DST
] == NULL
)
858 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &src_vni
, &vni
, &ifindex
);
862 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
!= ip
.sa
.sa_family
)
863 return -EAFNOSUPPORT
;
865 spin_lock_bh(&vxlan
->hash_lock
);
866 err
= vxlan_fdb_create(vxlan
, addr
, &ip
, ndm
->ndm_state
, flags
,
867 port
, src_vni
, vni
, ifindex
, ndm
->ndm_flags
);
868 spin_unlock_bh(&vxlan
->hash_lock
);
873 static int __vxlan_fdb_delete(struct vxlan_dev
*vxlan
,
874 const unsigned char *addr
, union vxlan_addr ip
,
875 __be16 port
, __be32 src_vni
, u32 vni
, u32 ifindex
,
879 struct vxlan_rdst
*rd
= NULL
;
882 f
= vxlan_find_mac(vxlan
, addr
, src_vni
);
886 if (!vxlan_addr_any(&ip
)) {
887 rd
= vxlan_fdb_find_rdst(f
, &ip
, port
, vni
, ifindex
);
892 /* remove a destination if it's not the only one on the list,
893 * otherwise destroy the fdb entry
895 if (rd
&& !list_is_singular(&f
->remotes
)) {
896 vxlan_fdb_dst_destroy(vxlan
, f
, rd
);
900 vxlan_fdb_destroy(vxlan
, f
);
906 /* Delete entry (via netlink) */
907 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
908 struct net_device
*dev
,
909 const unsigned char *addr
, u16 vid
)
911 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
918 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &src_vni
, &vni
, &ifindex
);
922 spin_lock_bh(&vxlan
->hash_lock
);
923 err
= __vxlan_fdb_delete(vxlan
, addr
, ip
, port
, src_vni
, vni
, ifindex
,
925 spin_unlock_bh(&vxlan
->hash_lock
);
930 /* Dump forwarding table */
931 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
932 struct net_device
*dev
,
933 struct net_device
*filter_dev
, int *idx
)
935 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
939 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
942 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
943 struct vxlan_rdst
*rd
;
945 list_for_each_entry_rcu(rd
, &f
->remotes
, list
) {
946 if (*idx
< cb
->args
[2])
949 err
= vxlan_fdb_info(skb
, vxlan
, f
,
950 NETLINK_CB(cb
->skb
).portid
,
965 /* Watch incoming packets to learn mapping between Ethernet address
966 * and Tunnel endpoint.
967 * Return true if packet is bogus and should be dropped.
969 static bool vxlan_snoop(struct net_device
*dev
,
970 union vxlan_addr
*src_ip
, const u8
*src_mac
,
971 u32 src_ifindex
, __be32 vni
)
973 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
977 #if IS_ENABLED(CONFIG_IPV6)
978 if (src_ip
->sa
.sa_family
== AF_INET6
&&
979 (ipv6_addr_type(&src_ip
->sin6
.sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
980 ifindex
= src_ifindex
;
983 f
= vxlan_find_mac(vxlan
, src_mac
, vni
);
985 struct vxlan_rdst
*rdst
= first_remote_rcu(f
);
987 if (likely(vxlan_addr_equal(&rdst
->remote_ip
, src_ip
) &&
988 rdst
->remote_ifindex
== ifindex
))
991 /* Don't migrate static entries, drop packets */
992 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
997 "%pM migrated from %pIS to %pIS\n",
998 src_mac
, &rdst
->remote_ip
.sa
, &src_ip
->sa
);
1000 rdst
->remote_ip
= *src_ip
;
1001 f
->updated
= jiffies
;
1002 vxlan_fdb_notify(vxlan
, f
, rdst
, RTM_NEWNEIGH
);
1004 /* learned new entry */
1005 spin_lock(&vxlan
->hash_lock
);
1007 /* close off race between vxlan_flush and incoming packets */
1008 if (netif_running(dev
))
1009 vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
1011 NLM_F_EXCL
|NLM_F_CREATE
,
1012 vxlan
->cfg
.dst_port
,
1014 vxlan
->default_dst
.remote_vni
,
1016 spin_unlock(&vxlan
->hash_lock
);
1022 /* See if multicast group is already in use by other ID */
1023 static bool vxlan_group_used(struct vxlan_net
*vn
, struct vxlan_dev
*dev
)
1025 struct vxlan_dev
*vxlan
;
1026 struct vxlan_sock
*sock4
;
1027 #if IS_ENABLED(CONFIG_IPV6)
1028 struct vxlan_sock
*sock6
;
1030 unsigned short family
= dev
->default_dst
.remote_ip
.sa
.sa_family
;
1032 sock4
= rtnl_dereference(dev
->vn4_sock
);
1034 /* The vxlan_sock is only used by dev, leaving group has
1035 * no effect on other vxlan devices.
1037 if (family
== AF_INET
&& sock4
&& refcount_read(&sock4
->refcnt
) == 1)
1039 #if IS_ENABLED(CONFIG_IPV6)
1040 sock6
= rtnl_dereference(dev
->vn6_sock
);
1041 if (family
== AF_INET6
&& sock6
&& refcount_read(&sock6
->refcnt
) == 1)
1045 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
) {
1046 if (!netif_running(vxlan
->dev
) || vxlan
== dev
)
1049 if (family
== AF_INET
&&
1050 rtnl_dereference(vxlan
->vn4_sock
) != sock4
)
1052 #if IS_ENABLED(CONFIG_IPV6)
1053 if (family
== AF_INET6
&&
1054 rtnl_dereference(vxlan
->vn6_sock
) != sock6
)
1058 if (!vxlan_addr_equal(&vxlan
->default_dst
.remote_ip
,
1059 &dev
->default_dst
.remote_ip
))
1062 if (vxlan
->default_dst
.remote_ifindex
!=
1063 dev
->default_dst
.remote_ifindex
)
1072 static bool __vxlan_sock_release_prep(struct vxlan_sock
*vs
)
1074 struct vxlan_net
*vn
;
1078 if (!refcount_dec_and_test(&vs
->refcnt
))
1081 vn
= net_generic(sock_net(vs
->sock
->sk
), vxlan_net_id
);
1082 spin_lock(&vn
->sock_lock
);
1083 hlist_del_rcu(&vs
->hlist
);
1084 udp_tunnel_notify_del_rx_port(vs
->sock
,
1085 (vs
->flags
& VXLAN_F_GPE
) ?
1086 UDP_TUNNEL_TYPE_VXLAN_GPE
:
1087 UDP_TUNNEL_TYPE_VXLAN
);
1088 spin_unlock(&vn
->sock_lock
);
1093 static void vxlan_sock_release(struct vxlan_dev
*vxlan
)
1095 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1096 #if IS_ENABLED(CONFIG_IPV6)
1097 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1099 RCU_INIT_POINTER(vxlan
->vn6_sock
, NULL
);
1102 RCU_INIT_POINTER(vxlan
->vn4_sock
, NULL
);
1105 vxlan_vs_del_dev(vxlan
);
1107 if (__vxlan_sock_release_prep(sock4
)) {
1108 udp_tunnel_sock_release(sock4
->sock
);
1112 #if IS_ENABLED(CONFIG_IPV6)
1113 if (__vxlan_sock_release_prep(sock6
)) {
1114 udp_tunnel_sock_release(sock6
->sock
);
1120 /* Update multicast group membership when first VNI on
1121 * multicast address is brought up
1123 static int vxlan_igmp_join(struct vxlan_dev
*vxlan
)
1126 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1127 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1130 if (ip
->sa
.sa_family
== AF_INET
) {
1131 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1132 struct ip_mreqn mreq
= {
1133 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1134 .imr_ifindex
= ifindex
,
1137 sk
= sock4
->sock
->sk
;
1139 ret
= ip_mc_join_group(sk
, &mreq
);
1141 #if IS_ENABLED(CONFIG_IPV6)
1143 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1145 sk
= sock6
->sock
->sk
;
1147 ret
= ipv6_stub
->ipv6_sock_mc_join(sk
, ifindex
,
1148 &ip
->sin6
.sin6_addr
);
1156 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1157 static int vxlan_igmp_leave(struct vxlan_dev
*vxlan
)
1160 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1161 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1164 if (ip
->sa
.sa_family
== AF_INET
) {
1165 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1166 struct ip_mreqn mreq
= {
1167 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1168 .imr_ifindex
= ifindex
,
1171 sk
= sock4
->sock
->sk
;
1173 ret
= ip_mc_leave_group(sk
, &mreq
);
1175 #if IS_ENABLED(CONFIG_IPV6)
1177 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1179 sk
= sock6
->sock
->sk
;
1181 ret
= ipv6_stub
->ipv6_sock_mc_drop(sk
, ifindex
,
1182 &ip
->sin6
.sin6_addr
);
1190 static bool vxlan_remcsum(struct vxlanhdr
*unparsed
,
1191 struct sk_buff
*skb
, u32 vxflags
)
1193 size_t start
, offset
;
1195 if (!(unparsed
->vx_flags
& VXLAN_HF_RCO
) || skb
->remcsum_offload
)
1198 start
= vxlan_rco_start(unparsed
->vx_vni
);
1199 offset
= start
+ vxlan_rco_offset(unparsed
->vx_vni
);
1201 if (!pskb_may_pull(skb
, offset
+ sizeof(u16
)))
1204 skb_remcsum_process(skb
, (void *)(vxlan_hdr(skb
) + 1), start
, offset
,
1205 !!(vxflags
& VXLAN_F_REMCSUM_NOPARTIAL
));
1207 unparsed
->vx_flags
&= ~VXLAN_HF_RCO
;
1208 unparsed
->vx_vni
&= VXLAN_VNI_MASK
;
1212 static void vxlan_parse_gbp_hdr(struct vxlanhdr
*unparsed
,
1213 struct sk_buff
*skb
, u32 vxflags
,
1214 struct vxlan_metadata
*md
)
1216 struct vxlanhdr_gbp
*gbp
= (struct vxlanhdr_gbp
*)unparsed
;
1217 struct metadata_dst
*tun_dst
;
1219 if (!(unparsed
->vx_flags
& VXLAN_HF_GBP
))
1222 md
->gbp
= ntohs(gbp
->policy_id
);
1224 tun_dst
= (struct metadata_dst
*)skb_dst(skb
);
1226 tun_dst
->u
.tun_info
.key
.tun_flags
|= TUNNEL_VXLAN_OPT
;
1227 tun_dst
->u
.tun_info
.options_len
= sizeof(*md
);
1229 if (gbp
->dont_learn
)
1230 md
->gbp
|= VXLAN_GBP_DONT_LEARN
;
1232 if (gbp
->policy_applied
)
1233 md
->gbp
|= VXLAN_GBP_POLICY_APPLIED
;
1235 /* In flow-based mode, GBP is carried in dst_metadata */
1236 if (!(vxflags
& VXLAN_F_COLLECT_METADATA
))
1237 skb
->mark
= md
->gbp
;
1239 unparsed
->vx_flags
&= ~VXLAN_GBP_USED_BITS
;
1242 static bool vxlan_parse_gpe_hdr(struct vxlanhdr
*unparsed
,
1244 struct sk_buff
*skb
, u32 vxflags
)
1246 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)unparsed
;
1248 /* Need to have Next Protocol set for interfaces in GPE mode. */
1249 if (!gpe
->np_applied
)
1251 /* "The initial version is 0. If a receiver does not support the
1252 * version indicated it MUST drop the packet.
1254 if (gpe
->version
!= 0)
1256 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1257 * processing MUST occur." However, we don't implement OAM
1258 * processing, thus drop the packet.
1263 switch (gpe
->next_protocol
) {
1264 case VXLAN_GPE_NP_IPV4
:
1265 *protocol
= htons(ETH_P_IP
);
1267 case VXLAN_GPE_NP_IPV6
:
1268 *protocol
= htons(ETH_P_IPV6
);
1270 case VXLAN_GPE_NP_ETHERNET
:
1271 *protocol
= htons(ETH_P_TEB
);
1277 unparsed
->vx_flags
&= ~VXLAN_GPE_USED_BITS
;
1281 static bool vxlan_set_mac(struct vxlan_dev
*vxlan
,
1282 struct vxlan_sock
*vs
,
1283 struct sk_buff
*skb
, __be32 vni
)
1285 union vxlan_addr saddr
;
1286 u32 ifindex
= skb
->dev
->ifindex
;
1288 skb_reset_mac_header(skb
);
1289 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1290 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
1292 /* Ignore packet loops (and multicast echo) */
1293 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1296 /* Get address from the outer IP header */
1297 if (vxlan_get_sk_family(vs
) == AF_INET
) {
1298 saddr
.sin
.sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
1299 saddr
.sa
.sa_family
= AF_INET
;
1300 #if IS_ENABLED(CONFIG_IPV6)
1302 saddr
.sin6
.sin6_addr
= ipv6_hdr(skb
)->saddr
;
1303 saddr
.sa
.sa_family
= AF_INET6
;
1307 if ((vxlan
->cfg
.flags
& VXLAN_F_LEARN
) &&
1308 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
, ifindex
, vni
))
1314 static bool vxlan_ecn_decapsulate(struct vxlan_sock
*vs
, void *oiph
,
1315 struct sk_buff
*skb
)
1319 if (vxlan_get_sk_family(vs
) == AF_INET
)
1320 err
= IP_ECN_decapsulate(oiph
, skb
);
1321 #if IS_ENABLED(CONFIG_IPV6)
1323 err
= IP6_ECN_decapsulate(oiph
, skb
);
1326 if (unlikely(err
) && log_ecn_error
) {
1327 if (vxlan_get_sk_family(vs
) == AF_INET
)
1328 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1329 &((struct iphdr
*)oiph
)->saddr
,
1330 ((struct iphdr
*)oiph
)->tos
);
1332 net_info_ratelimited("non-ECT from %pI6\n",
1333 &((struct ipv6hdr
*)oiph
)->saddr
);
1338 /* Callback from net/ipv4/udp.c to receive packets */
1339 static int vxlan_rcv(struct sock
*sk
, struct sk_buff
*skb
)
1341 struct pcpu_sw_netstats
*stats
;
1342 struct vxlan_dev
*vxlan
;
1343 struct vxlan_sock
*vs
;
1344 struct vxlanhdr unparsed
;
1345 struct vxlan_metadata _md
;
1346 struct vxlan_metadata
*md
= &_md
;
1347 __be16 protocol
= htons(ETH_P_TEB
);
1348 bool raw_proto
= false;
1352 /* Need UDP and VXLAN header to be present */
1353 if (!pskb_may_pull(skb
, VXLAN_HLEN
))
1356 unparsed
= *vxlan_hdr(skb
);
1357 /* VNI flag always required to be set */
1358 if (!(unparsed
.vx_flags
& VXLAN_HF_VNI
)) {
1359 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
1360 ntohl(vxlan_hdr(skb
)->vx_flags
),
1361 ntohl(vxlan_hdr(skb
)->vx_vni
));
1362 /* Return non vxlan pkt */
1365 unparsed
.vx_flags
&= ~VXLAN_HF_VNI
;
1366 unparsed
.vx_vni
&= ~VXLAN_VNI_MASK
;
1368 vs
= rcu_dereference_sk_user_data(sk
);
1372 vni
= vxlan_vni(vxlan_hdr(skb
)->vx_vni
);
1374 vxlan
= vxlan_vs_find_vni(vs
, skb
->dev
->ifindex
, vni
);
1378 /* For backwards compatibility, only allow reserved fields to be
1379 * used by VXLAN extensions if explicitly requested.
1381 if (vs
->flags
& VXLAN_F_GPE
) {
1382 if (!vxlan_parse_gpe_hdr(&unparsed
, &protocol
, skb
, vs
->flags
))
1387 if (__iptunnel_pull_header(skb
, VXLAN_HLEN
, protocol
, raw_proto
,
1388 !net_eq(vxlan
->net
, dev_net(vxlan
->dev
))))
1391 if (vxlan_collect_metadata(vs
)) {
1392 struct metadata_dst
*tun_dst
;
1394 tun_dst
= udp_tun_rx_dst(skb
, vxlan_get_sk_family(vs
), TUNNEL_KEY
,
1395 key32_to_tunnel_id(vni
), sizeof(*md
));
1400 md
= ip_tunnel_info_opts(&tun_dst
->u
.tun_info
);
1402 skb_dst_set(skb
, (struct dst_entry
*)tun_dst
);
1404 memset(md
, 0, sizeof(*md
));
1407 if (vs
->flags
& VXLAN_F_REMCSUM_RX
)
1408 if (!vxlan_remcsum(&unparsed
, skb
, vs
->flags
))
1410 if (vs
->flags
& VXLAN_F_GBP
)
1411 vxlan_parse_gbp_hdr(&unparsed
, skb
, vs
->flags
, md
);
1412 /* Note that GBP and GPE can never be active together. This is
1413 * ensured in vxlan_dev_configure.
1416 if (unparsed
.vx_flags
|| unparsed
.vx_vni
) {
1417 /* If there are any unprocessed flags remaining treat
1418 * this as a malformed packet. This behavior diverges from
1419 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1420 * in reserved fields are to be ignored. The approach here
1421 * maintains compatibility with previous stack code, and also
1422 * is more robust and provides a little more security in
1423 * adding extensions to VXLAN.
1429 if (!vxlan_set_mac(vxlan
, vs
, skb
, vni
))
1432 skb_reset_mac_header(skb
);
1433 skb
->dev
= vxlan
->dev
;
1434 skb
->pkt_type
= PACKET_HOST
;
1437 oiph
= skb_network_header(skb
);
1438 skb_reset_network_header(skb
);
1440 if (!vxlan_ecn_decapsulate(vs
, oiph
, skb
)) {
1441 ++vxlan
->dev
->stats
.rx_frame_errors
;
1442 ++vxlan
->dev
->stats
.rx_errors
;
1446 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1447 u64_stats_update_begin(&stats
->syncp
);
1448 stats
->rx_packets
++;
1449 stats
->rx_bytes
+= skb
->len
;
1450 u64_stats_update_end(&stats
->syncp
);
1452 gro_cells_receive(&vxlan
->gro_cells
, skb
);
1456 /* Consume bad packet */
1461 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
, __be32 vni
)
1463 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1464 struct arphdr
*parp
;
1467 struct neighbour
*n
;
1469 if (dev
->flags
& IFF_NOARP
)
1472 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1473 dev
->stats
.tx_dropped
++;
1476 parp
= arp_hdr(skb
);
1478 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1479 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1480 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1481 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1482 parp
->ar_hln
!= dev
->addr_len
||
1485 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1487 arpptr
+= dev
->addr_len
; /* sha */
1488 memcpy(&sip
, arpptr
, sizeof(sip
));
1489 arpptr
+= sizeof(sip
);
1490 arpptr
+= dev
->addr_len
; /* tha */
1491 memcpy(&tip
, arpptr
, sizeof(tip
));
1493 if (ipv4_is_loopback(tip
) ||
1494 ipv4_is_multicast(tip
))
1497 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1500 struct vxlan_fdb
*f
;
1501 struct sk_buff
*reply
;
1503 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1508 f
= vxlan_find_mac(vxlan
, n
->ha
, vni
);
1509 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1510 /* bridge-local neighbor */
1515 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1523 skb_reset_mac_header(reply
);
1524 __skb_pull(reply
, skb_network_offset(reply
));
1525 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1526 reply
->pkt_type
= PACKET_HOST
;
1528 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1529 dev
->stats
.rx_dropped
++;
1530 } else if (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
) {
1531 union vxlan_addr ipa
= {
1532 .sin
.sin_addr
.s_addr
= tip
,
1533 .sin
.sin_family
= AF_INET
,
1536 vxlan_ip_miss(dev
, &ipa
);
1540 return NETDEV_TX_OK
;
1543 #if IS_ENABLED(CONFIG_IPV6)
1544 static struct sk_buff
*vxlan_na_create(struct sk_buff
*request
,
1545 struct neighbour
*n
, bool isrouter
)
1547 struct net_device
*dev
= request
->dev
;
1548 struct sk_buff
*reply
;
1549 struct nd_msg
*ns
, *na
;
1550 struct ipv6hdr
*pip6
;
1552 int na_olen
= 8; /* opt hdr + ETH_ALEN for target */
1556 if (dev
== NULL
|| !pskb_may_pull(request
, request
->len
))
1559 len
= LL_RESERVED_SPACE(dev
) + sizeof(struct ipv6hdr
) +
1560 sizeof(*na
) + na_olen
+ dev
->needed_tailroom
;
1561 reply
= alloc_skb(len
, GFP_ATOMIC
);
1565 reply
->protocol
= htons(ETH_P_IPV6
);
1567 skb_reserve(reply
, LL_RESERVED_SPACE(request
->dev
));
1568 skb_push(reply
, sizeof(struct ethhdr
));
1569 skb_reset_mac_header(reply
);
1571 ns
= (struct nd_msg
*)(ipv6_hdr(request
) + 1);
1573 daddr
= eth_hdr(request
)->h_source
;
1574 ns_olen
= request
->len
- skb_network_offset(request
) -
1575 sizeof(struct ipv6hdr
) - sizeof(*ns
);
1576 for (i
= 0; i
< ns_olen
-1; i
+= (ns
->opt
[i
+1]<<3)) {
1577 if (ns
->opt
[i
] == ND_OPT_SOURCE_LL_ADDR
) {
1578 daddr
= ns
->opt
+ i
+ sizeof(struct nd_opt_hdr
);
1583 /* Ethernet header */
1584 ether_addr_copy(eth_hdr(reply
)->h_dest
, daddr
);
1585 ether_addr_copy(eth_hdr(reply
)->h_source
, n
->ha
);
1586 eth_hdr(reply
)->h_proto
= htons(ETH_P_IPV6
);
1587 reply
->protocol
= htons(ETH_P_IPV6
);
1589 skb_pull(reply
, sizeof(struct ethhdr
));
1590 skb_reset_network_header(reply
);
1591 skb_put(reply
, sizeof(struct ipv6hdr
));
1595 pip6
= ipv6_hdr(reply
);
1596 memset(pip6
, 0, sizeof(struct ipv6hdr
));
1598 pip6
->priority
= ipv6_hdr(request
)->priority
;
1599 pip6
->nexthdr
= IPPROTO_ICMPV6
;
1600 pip6
->hop_limit
= 255;
1601 pip6
->daddr
= ipv6_hdr(request
)->saddr
;
1602 pip6
->saddr
= *(struct in6_addr
*)n
->primary_key
;
1604 skb_pull(reply
, sizeof(struct ipv6hdr
));
1605 skb_reset_transport_header(reply
);
1607 /* Neighbor Advertisement */
1608 na
= skb_put_zero(reply
, sizeof(*na
) + na_olen
);
1609 na
->icmph
.icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
1610 na
->icmph
.icmp6_router
= isrouter
;
1611 na
->icmph
.icmp6_override
= 1;
1612 na
->icmph
.icmp6_solicited
= 1;
1613 na
->target
= ns
->target
;
1614 ether_addr_copy(&na
->opt
[2], n
->ha
);
1615 na
->opt
[0] = ND_OPT_TARGET_LL_ADDR
;
1616 na
->opt
[1] = na_olen
>> 3;
1618 na
->icmph
.icmp6_cksum
= csum_ipv6_magic(&pip6
->saddr
,
1619 &pip6
->daddr
, sizeof(*na
)+na_olen
, IPPROTO_ICMPV6
,
1620 csum_partial(na
, sizeof(*na
)+na_olen
, 0));
1622 pip6
->payload_len
= htons(sizeof(*na
)+na_olen
);
1624 skb_push(reply
, sizeof(struct ipv6hdr
));
1626 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1631 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
, __be32 vni
)
1633 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1635 const struct ipv6hdr
*iphdr
;
1636 const struct in6_addr
*daddr
;
1637 struct neighbour
*n
;
1638 struct inet6_dev
*in6_dev
;
1640 in6_dev
= __in6_dev_get(dev
);
1644 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
) + sizeof(struct nd_msg
)))
1647 iphdr
= ipv6_hdr(skb
);
1648 daddr
= &iphdr
->daddr
;
1650 msg
= (struct nd_msg
*)(iphdr
+ 1);
1651 if (msg
->icmph
.icmp6_code
!= 0 ||
1652 msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
1655 if (ipv6_addr_loopback(daddr
) ||
1656 ipv6_addr_is_multicast(&msg
->target
))
1659 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1662 struct vxlan_fdb
*f
;
1663 struct sk_buff
*reply
;
1665 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1670 f
= vxlan_find_mac(vxlan
, n
->ha
, vni
);
1671 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1672 /* bridge-local neighbor */
1677 reply
= vxlan_na_create(skb
, n
,
1678 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1685 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1686 dev
->stats
.rx_dropped
++;
1688 } else if (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
) {
1689 union vxlan_addr ipa
= {
1690 .sin6
.sin6_addr
= msg
->target
,
1691 .sin6
.sin6_family
= AF_INET6
,
1694 vxlan_ip_miss(dev
, &ipa
);
1699 return NETDEV_TX_OK
;
1703 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1705 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1706 struct neighbour
*n
;
1708 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1712 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1717 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1720 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1721 if (!n
&& (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) {
1722 union vxlan_addr ipa
= {
1723 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1724 .sin
.sin_family
= AF_INET
,
1727 vxlan_ip_miss(dev
, &ipa
);
1733 #if IS_ENABLED(CONFIG_IPV6)
1736 struct ipv6hdr
*pip6
;
1738 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1740 pip6
= ipv6_hdr(skb
);
1741 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1742 if (!n
&& (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) {
1743 union vxlan_addr ipa
= {
1744 .sin6
.sin6_addr
= pip6
->daddr
,
1745 .sin6
.sin6_family
= AF_INET6
,
1748 vxlan_ip_miss(dev
, &ipa
);
1762 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1764 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1766 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1775 static void vxlan_build_gbp_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1776 struct vxlan_metadata
*md
)
1778 struct vxlanhdr_gbp
*gbp
;
1783 gbp
= (struct vxlanhdr_gbp
*)vxh
;
1784 vxh
->vx_flags
|= VXLAN_HF_GBP
;
1786 if (md
->gbp
& VXLAN_GBP_DONT_LEARN
)
1787 gbp
->dont_learn
= 1;
1789 if (md
->gbp
& VXLAN_GBP_POLICY_APPLIED
)
1790 gbp
->policy_applied
= 1;
1792 gbp
->policy_id
= htons(md
->gbp
& VXLAN_GBP_ID_MASK
);
1795 static int vxlan_build_gpe_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1798 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)vxh
;
1800 gpe
->np_applied
= 1;
1803 case htons(ETH_P_IP
):
1804 gpe
->next_protocol
= VXLAN_GPE_NP_IPV4
;
1806 case htons(ETH_P_IPV6
):
1807 gpe
->next_protocol
= VXLAN_GPE_NP_IPV6
;
1809 case htons(ETH_P_TEB
):
1810 gpe
->next_protocol
= VXLAN_GPE_NP_ETHERNET
;
1813 return -EPFNOSUPPORT
;
1816 static int vxlan_build_skb(struct sk_buff
*skb
, struct dst_entry
*dst
,
1817 int iphdr_len
, __be32 vni
,
1818 struct vxlan_metadata
*md
, u32 vxflags
,
1821 struct vxlanhdr
*vxh
;
1824 int type
= udp_sum
? SKB_GSO_UDP_TUNNEL_CSUM
: SKB_GSO_UDP_TUNNEL
;
1825 __be16 inner_protocol
= htons(ETH_P_TEB
);
1827 if ((vxflags
& VXLAN_F_REMCSUM_TX
) &&
1828 skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1829 int csum_start
= skb_checksum_start_offset(skb
);
1831 if (csum_start
<= VXLAN_MAX_REMCSUM_START
&&
1832 !(csum_start
& VXLAN_RCO_SHIFT_MASK
) &&
1833 (skb
->csum_offset
== offsetof(struct udphdr
, check
) ||
1834 skb
->csum_offset
== offsetof(struct tcphdr
, check
)))
1835 type
|= SKB_GSO_TUNNEL_REMCSUM
;
1838 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1839 + VXLAN_HLEN
+ iphdr_len
;
1841 /* Need space for new headers (invalidates iph ptr) */
1842 err
= skb_cow_head(skb
, min_headroom
);
1846 err
= iptunnel_handle_offloads(skb
, type
);
1850 vxh
= __skb_push(skb
, sizeof(*vxh
));
1851 vxh
->vx_flags
= VXLAN_HF_VNI
;
1852 vxh
->vx_vni
= vxlan_vni_field(vni
);
1854 if (type
& SKB_GSO_TUNNEL_REMCSUM
) {
1857 start
= skb_checksum_start_offset(skb
) - sizeof(struct vxlanhdr
);
1858 vxh
->vx_vni
|= vxlan_compute_rco(start
, skb
->csum_offset
);
1859 vxh
->vx_flags
|= VXLAN_HF_RCO
;
1861 if (!skb_is_gso(skb
)) {
1862 skb
->ip_summed
= CHECKSUM_NONE
;
1863 skb
->encapsulation
= 0;
1867 if (vxflags
& VXLAN_F_GBP
)
1868 vxlan_build_gbp_hdr(vxh
, vxflags
, md
);
1869 if (vxflags
& VXLAN_F_GPE
) {
1870 err
= vxlan_build_gpe_hdr(vxh
, vxflags
, skb
->protocol
);
1873 inner_protocol
= skb
->protocol
;
1876 skb_set_inner_protocol(skb
, inner_protocol
);
1880 static struct rtable
*vxlan_get_route(struct vxlan_dev
*vxlan
, struct net_device
*dev
,
1881 struct vxlan_sock
*sock4
,
1882 struct sk_buff
*skb
, int oif
, u8 tos
,
1883 __be32 daddr
, __be32
*saddr
, __be16 dport
, __be16 sport
,
1884 struct dst_cache
*dst_cache
,
1885 const struct ip_tunnel_info
*info
)
1887 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
1888 struct rtable
*rt
= NULL
;
1892 return ERR_PTR(-EIO
);
1897 rt
= dst_cache_get_ip4(dst_cache
, saddr
);
1902 memset(&fl4
, 0, sizeof(fl4
));
1903 fl4
.flowi4_oif
= oif
;
1904 fl4
.flowi4_tos
= RT_TOS(tos
);
1905 fl4
.flowi4_mark
= skb
->mark
;
1906 fl4
.flowi4_proto
= IPPROTO_UDP
;
1909 fl4
.fl4_dport
= dport
;
1910 fl4
.fl4_sport
= sport
;
1912 rt
= ip_route_output_key(vxlan
->net
, &fl4
);
1913 if (likely(!IS_ERR(rt
))) {
1914 if (rt
->dst
.dev
== dev
) {
1915 netdev_dbg(dev
, "circular route to %pI4\n", &daddr
);
1917 return ERR_PTR(-ELOOP
);
1922 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
.saddr
);
1924 netdev_dbg(dev
, "no route to %pI4\n", &daddr
);
1925 return ERR_PTR(-ENETUNREACH
);
1930 #if IS_ENABLED(CONFIG_IPV6)
1931 static struct dst_entry
*vxlan6_get_route(struct vxlan_dev
*vxlan
,
1932 struct net_device
*dev
,
1933 struct vxlan_sock
*sock6
,
1934 struct sk_buff
*skb
, int oif
, u8 tos
,
1936 const struct in6_addr
*daddr
,
1937 struct in6_addr
*saddr
,
1938 __be16 dport
, __be16 sport
,
1939 struct dst_cache
*dst_cache
,
1940 const struct ip_tunnel_info
*info
)
1942 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
1943 struct dst_entry
*ndst
;
1948 return ERR_PTR(-EIO
);
1953 ndst
= dst_cache_get_ip6(dst_cache
, saddr
);
1958 memset(&fl6
, 0, sizeof(fl6
));
1959 fl6
.flowi6_oif
= oif
;
1962 fl6
.flowlabel
= ip6_make_flowinfo(RT_TOS(tos
), label
);
1963 fl6
.flowi6_mark
= skb
->mark
;
1964 fl6
.flowi6_proto
= IPPROTO_UDP
;
1965 fl6
.fl6_dport
= dport
;
1966 fl6
.fl6_sport
= sport
;
1968 err
= ipv6_stub
->ipv6_dst_lookup(vxlan
->net
,
1971 if (unlikely(err
< 0)) {
1972 netdev_dbg(dev
, "no route to %pI6\n", daddr
);
1973 return ERR_PTR(-ENETUNREACH
);
1976 if (unlikely(ndst
->dev
== dev
)) {
1977 netdev_dbg(dev
, "circular route to %pI6\n", daddr
);
1979 return ERR_PTR(-ELOOP
);
1984 dst_cache_set_ip6(dst_cache
, ndst
, saddr
);
1989 /* Bypass encapsulation if the destination is local */
1990 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
1991 struct vxlan_dev
*dst_vxlan
, __be32 vni
)
1993 struct pcpu_sw_netstats
*tx_stats
, *rx_stats
;
1994 union vxlan_addr loopback
;
1995 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
1996 struct net_device
*dev
= skb
->dev
;
1999 tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
2000 rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
2001 skb
->pkt_type
= PACKET_HOST
;
2002 skb
->encapsulation
= 0;
2003 skb
->dev
= dst_vxlan
->dev
;
2004 __skb_pull(skb
, skb_network_offset(skb
));
2006 if (remote_ip
->sa
.sa_family
== AF_INET
) {
2007 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2008 loopback
.sa
.sa_family
= AF_INET
;
2009 #if IS_ENABLED(CONFIG_IPV6)
2011 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
2012 loopback
.sa
.sa_family
= AF_INET6
;
2016 if (dst_vxlan
->cfg
.flags
& VXLAN_F_LEARN
)
2017 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
, 0,
2020 u64_stats_update_begin(&tx_stats
->syncp
);
2021 tx_stats
->tx_packets
++;
2022 tx_stats
->tx_bytes
+= len
;
2023 u64_stats_update_end(&tx_stats
->syncp
);
2025 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
2026 u64_stats_update_begin(&rx_stats
->syncp
);
2027 rx_stats
->rx_packets
++;
2028 rx_stats
->rx_bytes
+= len
;
2029 u64_stats_update_end(&rx_stats
->syncp
);
2031 dev
->stats
.rx_dropped
++;
2035 static int encap_bypass_if_local(struct sk_buff
*skb
, struct net_device
*dev
,
2036 struct vxlan_dev
*vxlan
,
2037 union vxlan_addr
*daddr
,
2038 __be16 dst_port
, int dst_ifindex
, __be32 vni
,
2039 struct dst_entry
*dst
,
2042 #if IS_ENABLED(CONFIG_IPV6)
2043 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2044 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2045 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2047 BUILD_BUG_ON(RTCF_LOCAL
!= RTF_LOCAL
);
2049 /* Bypass encapsulation if the destination is local */
2050 if (rt_flags
& RTCF_LOCAL
&&
2051 !(rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
2052 struct vxlan_dev
*dst_vxlan
;
2055 dst_vxlan
= vxlan_find_vni(vxlan
->net
, dst_ifindex
, vni
,
2056 daddr
->sa
.sa_family
, dst_port
,
2059 dev
->stats
.tx_errors
++;
2064 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
, vni
);
2071 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
2072 __be32 default_vni
, struct vxlan_rdst
*rdst
,
2075 struct dst_cache
*dst_cache
;
2076 struct ip_tunnel_info
*info
;
2077 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2078 const struct iphdr
*old_iph
= ip_hdr(skb
);
2079 union vxlan_addr
*dst
;
2080 union vxlan_addr remote_ip
, local_ip
;
2081 struct vxlan_metadata _md
;
2082 struct vxlan_metadata
*md
= &_md
;
2083 __be16 src_port
= 0, dst_port
;
2084 struct dst_entry
*ndst
= NULL
;
2089 u32 flags
= vxlan
->cfg
.flags
;
2090 bool udp_sum
= false;
2091 bool xnet
= !net_eq(vxlan
->net
, dev_net(vxlan
->dev
));
2093 info
= skb_tunnel_info(skb
);
2096 dst
= &rdst
->remote_ip
;
2097 if (vxlan_addr_any(dst
)) {
2099 /* short-circuited back to local bridge */
2100 vxlan_encap_bypass(skb
, vxlan
, vxlan
, default_vni
);
2106 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->cfg
.dst_port
;
2107 vni
= (rdst
->remote_vni
) ? : default_vni
;
2108 ifindex
= rdst
->remote_ifindex
;
2109 local_ip
= vxlan
->cfg
.saddr
;
2110 dst_cache
= &rdst
->dst_cache
;
2111 md
->gbp
= skb
->mark
;
2112 ttl
= vxlan
->cfg
.ttl
;
2113 if (!ttl
&& vxlan_addr_multicast(dst
))
2116 tos
= vxlan
->cfg
.tos
;
2118 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
2120 if (dst
->sa
.sa_family
== AF_INET
)
2121 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM_TX
);
2123 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
);
2124 label
= vxlan
->cfg
.label
;
2127 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2131 remote_ip
.sa
.sa_family
= ip_tunnel_info_af(info
);
2132 if (remote_ip
.sa
.sa_family
== AF_INET
) {
2133 remote_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.dst
;
2134 local_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.src
;
2136 remote_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.dst
;
2137 local_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.src
;
2140 dst_port
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2141 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2143 dst_cache
= &info
->dst_cache
;
2144 if (info
->options_len
)
2145 md
= ip_tunnel_info_opts(info
);
2146 ttl
= info
->key
.ttl
;
2147 tos
= info
->key
.tos
;
2148 label
= info
->key
.label
;
2149 udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
2151 src_port
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2152 vxlan
->cfg
.port_max
, true);
2155 if (dst
->sa
.sa_family
== AF_INET
) {
2156 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2160 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
, ifindex
, tos
,
2161 dst
->sin
.sin_addr
.s_addr
,
2162 &local_ip
.sin
.sin_addr
.s_addr
,
2170 /* Bypass encapsulation if the destination is local */
2172 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2173 dst_port
, ifindex
, vni
,
2174 &rt
->dst
, rt
->rt_flags
);
2177 } else if (info
->key
.tun_flags
& TUNNEL_DONT_FRAGMENT
) {
2182 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2183 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
2184 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct iphdr
),
2185 vni
, md
, flags
, udp_sum
);
2189 udp_tunnel_xmit_skb(rt
, sock4
->sock
->sk
, skb
, local_ip
.sin
.sin_addr
.s_addr
,
2190 dst
->sin
.sin_addr
.s_addr
, tos
, ttl
, df
,
2191 src_port
, dst_port
, xnet
, !udp_sum
);
2192 #if IS_ENABLED(CONFIG_IPV6)
2194 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2196 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
, ifindex
, tos
,
2197 label
, &dst
->sin6
.sin6_addr
,
2198 &local_ip
.sin6
.sin6_addr
,
2202 err
= PTR_ERR(ndst
);
2208 u32 rt6i_flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
2210 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2211 dst_port
, ifindex
, vni
,
2217 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2218 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
2219 skb_scrub_packet(skb
, xnet
);
2220 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct ipv6hdr
),
2221 vni
, md
, flags
, udp_sum
);
2225 udp_tunnel6_xmit_skb(ndst
, sock6
->sock
->sk
, skb
, dev
,
2226 &local_ip
.sin6
.sin6_addr
,
2227 &dst
->sin6
.sin6_addr
, tos
, ttl
,
2228 label
, src_port
, dst_port
, !udp_sum
);
2236 dev
->stats
.tx_dropped
++;
2243 dev
->stats
.collisions
++;
2244 else if (err
== -ENETUNREACH
)
2245 dev
->stats
.tx_carrier_errors
++;
2247 dev
->stats
.tx_errors
++;
2251 /* Transmit local packets over Vxlan
2253 * Outer IP header inherits ECN and DF from inner header.
2254 * Outer UDP destination is the VXLAN assigned port.
2255 * source port is based on hash of flow
2257 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
2259 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2260 const struct ip_tunnel_info
*info
;
2262 bool did_rsc
= false;
2263 struct vxlan_rdst
*rdst
, *fdst
= NULL
;
2264 struct vxlan_fdb
*f
;
2267 info
= skb_tunnel_info(skb
);
2269 skb_reset_mac_header(skb
);
2271 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) {
2272 if (info
&& info
->mode
& IP_TUNNEL_INFO_BRIDGE
&&
2273 info
->mode
& IP_TUNNEL_INFO_TX
) {
2274 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2276 if (info
&& info
->mode
& IP_TUNNEL_INFO_TX
)
2277 vxlan_xmit_one(skb
, dev
, vni
, NULL
, false);
2280 return NETDEV_TX_OK
;
2284 if (vxlan
->cfg
.flags
& VXLAN_F_PROXY
) {
2286 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
2287 return arp_reduce(dev
, skb
, vni
);
2288 #if IS_ENABLED(CONFIG_IPV6)
2289 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
) {
2290 struct ipv6hdr
*hdr
, _hdr
;
2291 if ((hdr
= skb_header_pointer(skb
,
2292 skb_network_offset(skb
),
2293 sizeof(_hdr
), &_hdr
)) &&
2294 hdr
->nexthdr
== IPPROTO_ICMPV6
)
2295 return neigh_reduce(dev
, skb
, vni
);
2301 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2304 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->cfg
.flags
& VXLAN_F_RSC
) &&
2305 (ntohs(eth
->h_proto
) == ETH_P_IP
||
2306 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
2307 did_rsc
= route_shortcircuit(dev
, skb
);
2309 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2313 f
= vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2315 if ((vxlan
->cfg
.flags
& VXLAN_F_L2MISS
) &&
2316 !is_multicast_ether_addr(eth
->h_dest
))
2317 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
2319 dev
->stats
.tx_dropped
++;
2321 return NETDEV_TX_OK
;
2325 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
2326 struct sk_buff
*skb1
;
2332 skb1
= skb_clone(skb
, GFP_ATOMIC
);
2334 vxlan_xmit_one(skb1
, dev
, vni
, rdst
, did_rsc
);
2338 vxlan_xmit_one(skb
, dev
, vni
, fdst
, did_rsc
);
2341 return NETDEV_TX_OK
;
2344 /* Walk the forwarding table and purge stale entries */
2345 static void vxlan_cleanup(unsigned long arg
)
2347 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
2348 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
2351 if (!netif_running(vxlan
->dev
))
2354 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2355 struct hlist_node
*p
, *n
;
2357 spin_lock_bh(&vxlan
->hash_lock
);
2358 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2360 = container_of(p
, struct vxlan_fdb
, hlist
);
2361 unsigned long timeout
;
2363 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
2366 if (f
->flags
& NTF_EXT_LEARNED
)
2369 timeout
= f
->used
+ vxlan
->cfg
.age_interval
* HZ
;
2370 if (time_before_eq(timeout
, jiffies
)) {
2371 netdev_dbg(vxlan
->dev
,
2372 "garbage collect %pM\n",
2374 f
->state
= NUD_STALE
;
2375 vxlan_fdb_destroy(vxlan
, f
);
2376 } else if (time_before(timeout
, next_timer
))
2377 next_timer
= timeout
;
2379 spin_unlock_bh(&vxlan
->hash_lock
);
2382 mod_timer(&vxlan
->age_timer
, next_timer
);
2385 static void vxlan_vs_del_dev(struct vxlan_dev
*vxlan
)
2387 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2389 spin_lock(&vn
->sock_lock
);
2390 hlist_del_init_rcu(&vxlan
->hlist4
.hlist
);
2391 #if IS_ENABLED(CONFIG_IPV6)
2392 hlist_del_init_rcu(&vxlan
->hlist6
.hlist
);
2394 spin_unlock(&vn
->sock_lock
);
2397 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
,
2398 struct vxlan_dev_node
*node
)
2400 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2401 __be32 vni
= vxlan
->default_dst
.remote_vni
;
2403 node
->vxlan
= vxlan
;
2404 spin_lock(&vn
->sock_lock
);
2405 hlist_add_head_rcu(&node
->hlist
, vni_head(vs
, vni
));
2406 spin_unlock(&vn
->sock_lock
);
2409 /* Setup stats when device is created */
2410 static int vxlan_init(struct net_device
*dev
)
2412 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
2419 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
, __be32 vni
)
2421 struct vxlan_fdb
*f
;
2423 spin_lock_bh(&vxlan
->hash_lock
);
2424 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2426 vxlan_fdb_destroy(vxlan
, f
);
2427 spin_unlock_bh(&vxlan
->hash_lock
);
2430 static void vxlan_uninit(struct net_device
*dev
)
2432 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2434 vxlan_fdb_delete_default(vxlan
, vxlan
->cfg
.vni
);
2436 free_percpu(dev
->tstats
);
2439 /* Start ageing timer and join group when device is brought up */
2440 static int vxlan_open(struct net_device
*dev
)
2442 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2445 ret
= vxlan_sock_add(vxlan
);
2449 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
)) {
2450 ret
= vxlan_igmp_join(vxlan
);
2451 if (ret
== -EADDRINUSE
)
2454 vxlan_sock_release(vxlan
);
2459 if (vxlan
->cfg
.age_interval
)
2460 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2465 /* Purge the forwarding table */
2466 static void vxlan_flush(struct vxlan_dev
*vxlan
, bool do_all
)
2470 spin_lock_bh(&vxlan
->hash_lock
);
2471 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2472 struct hlist_node
*p
, *n
;
2473 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2475 = container_of(p
, struct vxlan_fdb
, hlist
);
2476 if (!do_all
&& (f
->state
& (NUD_PERMANENT
| NUD_NOARP
)))
2478 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2479 if (!is_zero_ether_addr(f
->eth_addr
))
2480 vxlan_fdb_destroy(vxlan
, f
);
2483 spin_unlock_bh(&vxlan
->hash_lock
);
2486 /* Cleanup timer and forwarding table on shutdown */
2487 static int vxlan_stop(struct net_device
*dev
)
2489 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2490 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2493 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2494 !vxlan_group_used(vn
, vxlan
))
2495 ret
= vxlan_igmp_leave(vxlan
);
2497 del_timer_sync(&vxlan
->age_timer
);
2499 vxlan_flush(vxlan
, false);
2500 vxlan_sock_release(vxlan
);
2505 /* Stub, nothing needs to be done. */
2506 static void vxlan_set_multicast_list(struct net_device
*dev
)
2510 static int vxlan_change_mtu(struct net_device
*dev
, int new_mtu
)
2512 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2513 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2514 struct net_device
*lowerdev
= __dev_get_by_index(vxlan
->net
,
2515 dst
->remote_ifindex
);
2516 bool use_ipv6
= !!(vxlan
->cfg
.flags
& VXLAN_F_IPV6
);
2518 /* This check is different than dev->max_mtu, because it looks at
2519 * the lowerdev->mtu, rather than the static dev->max_mtu
2522 int max_mtu
= lowerdev
->mtu
-
2523 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2524 if (new_mtu
> max_mtu
)
2532 static int vxlan_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
2534 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2535 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
2536 __be16 sport
, dport
;
2538 sport
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2539 vxlan
->cfg
.port_max
, true);
2540 dport
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2542 if (ip_tunnel_info_af(info
) == AF_INET
) {
2543 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2546 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
, 0, info
->key
.tos
,
2547 info
->key
.u
.ipv4
.dst
,
2548 &info
->key
.u
.ipv4
.src
, dport
, sport
,
2549 &info
->dst_cache
, info
);
2554 #if IS_ENABLED(CONFIG_IPV6)
2555 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2556 struct dst_entry
*ndst
;
2558 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
, 0, info
->key
.tos
,
2559 info
->key
.label
, &info
->key
.u
.ipv6
.dst
,
2560 &info
->key
.u
.ipv6
.src
, dport
, sport
,
2561 &info
->dst_cache
, info
);
2563 return PTR_ERR(ndst
);
2565 #else /* !CONFIG_IPV6 */
2566 return -EPFNOSUPPORT
;
2569 info
->key
.tp_src
= sport
;
2570 info
->key
.tp_dst
= dport
;
2574 static const struct net_device_ops vxlan_netdev_ether_ops
= {
2575 .ndo_init
= vxlan_init
,
2576 .ndo_uninit
= vxlan_uninit
,
2577 .ndo_open
= vxlan_open
,
2578 .ndo_stop
= vxlan_stop
,
2579 .ndo_start_xmit
= vxlan_xmit
,
2580 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2581 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2582 .ndo_change_mtu
= vxlan_change_mtu
,
2583 .ndo_validate_addr
= eth_validate_addr
,
2584 .ndo_set_mac_address
= eth_mac_addr
,
2585 .ndo_fdb_add
= vxlan_fdb_add
,
2586 .ndo_fdb_del
= vxlan_fdb_delete
,
2587 .ndo_fdb_dump
= vxlan_fdb_dump
,
2588 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2591 static const struct net_device_ops vxlan_netdev_raw_ops
= {
2592 .ndo_init
= vxlan_init
,
2593 .ndo_uninit
= vxlan_uninit
,
2594 .ndo_open
= vxlan_open
,
2595 .ndo_stop
= vxlan_stop
,
2596 .ndo_start_xmit
= vxlan_xmit
,
2597 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2598 .ndo_change_mtu
= vxlan_change_mtu
,
2599 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2602 /* Info for udev, that this is a virtual tunnel endpoint */
2603 static struct device_type vxlan_type
= {
2607 /* Calls the ndo_udp_tunnel_add of the caller in order to
2608 * supply the listening VXLAN udp ports. Callers are expected
2609 * to implement the ndo_udp_tunnel_add.
2611 static void vxlan_offload_rx_ports(struct net_device
*dev
, bool push
)
2613 struct vxlan_sock
*vs
;
2614 struct net
*net
= dev_net(dev
);
2615 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2618 spin_lock(&vn
->sock_lock
);
2619 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2620 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
) {
2621 unsigned short type
;
2623 if (vs
->flags
& VXLAN_F_GPE
)
2624 type
= UDP_TUNNEL_TYPE_VXLAN_GPE
;
2626 type
= UDP_TUNNEL_TYPE_VXLAN
;
2629 udp_tunnel_push_rx_port(dev
, vs
->sock
, type
);
2631 udp_tunnel_drop_rx_port(dev
, vs
->sock
, type
);
2634 spin_unlock(&vn
->sock_lock
);
2637 /* Initialize the device structure. */
2638 static void vxlan_setup(struct net_device
*dev
)
2640 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2643 eth_hw_addr_random(dev
);
2646 dev
->needs_free_netdev
= true;
2647 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2649 dev
->features
|= NETIF_F_LLTX
;
2650 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2651 dev
->features
|= NETIF_F_RXCSUM
;
2652 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2654 dev
->vlan_features
= dev
->features
;
2655 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2656 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2657 netif_keep_dst(dev
);
2658 dev
->priv_flags
|= IFF_NO_QUEUE
;
2660 /* MTU range: 68 - 65535 */
2661 dev
->min_mtu
= ETH_MIN_MTU
;
2662 dev
->max_mtu
= ETH_MAX_MTU
;
2664 INIT_LIST_HEAD(&vxlan
->next
);
2665 spin_lock_init(&vxlan
->hash_lock
);
2667 init_timer_deferrable(&vxlan
->age_timer
);
2668 vxlan
->age_timer
.function
= vxlan_cleanup
;
2669 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2673 gro_cells_init(&vxlan
->gro_cells
, dev
);
2675 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2676 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2679 static void vxlan_ether_setup(struct net_device
*dev
)
2681 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
2682 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2683 dev
->netdev_ops
= &vxlan_netdev_ether_ops
;
2686 static void vxlan_raw_setup(struct net_device
*dev
)
2688 dev
->header_ops
= NULL
;
2689 dev
->type
= ARPHRD_NONE
;
2690 dev
->hard_header_len
= 0;
2692 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
2693 dev
->netdev_ops
= &vxlan_netdev_raw_ops
;
2696 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2697 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2698 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2699 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2700 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2701 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2702 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2703 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2704 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2705 [IFLA_VXLAN_LABEL
] = { .type
= NLA_U32
},
2706 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2707 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2708 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2709 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2710 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2711 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2712 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2713 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2714 [IFLA_VXLAN_COLLECT_METADATA
] = { .type
= NLA_U8
},
2715 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2716 [IFLA_VXLAN_UDP_CSUM
] = { .type
= NLA_U8
},
2717 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
2718 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
2719 [IFLA_VXLAN_REMCSUM_TX
] = { .type
= NLA_U8
},
2720 [IFLA_VXLAN_REMCSUM_RX
] = { .type
= NLA_U8
},
2721 [IFLA_VXLAN_GBP
] = { .type
= NLA_FLAG
, },
2722 [IFLA_VXLAN_GPE
] = { .type
= NLA_FLAG
, },
2723 [IFLA_VXLAN_REMCSUM_NOPARTIAL
] = { .type
= NLA_FLAG
},
2726 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
2727 struct netlink_ext_ack
*extack
)
2729 if (tb
[IFLA_ADDRESS
]) {
2730 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2731 pr_debug("invalid link address (not ethernet)\n");
2735 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2736 pr_debug("invalid all zero ethernet address\n");
2737 return -EADDRNOTAVAIL
;
2742 u32 mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2744 if (mtu
< ETH_MIN_MTU
|| mtu
> ETH_MAX_MTU
)
2751 if (data
[IFLA_VXLAN_ID
]) {
2752 u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2754 if (id
>= VXLAN_N_VID
)
2758 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2759 const struct ifla_vxlan_port_range
*p
2760 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2762 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2763 pr_debug("port range %u .. %u not valid\n",
2764 ntohs(p
->low
), ntohs(p
->high
));
2772 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2773 struct ethtool_drvinfo
*drvinfo
)
2775 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2776 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2779 static const struct ethtool_ops vxlan_ethtool_ops
= {
2780 .get_drvinfo
= vxlan_get_drvinfo
,
2781 .get_link
= ethtool_op_get_link
,
2784 static struct socket
*vxlan_create_sock(struct net
*net
, bool ipv6
,
2785 __be16 port
, u32 flags
)
2787 struct socket
*sock
;
2788 struct udp_port_cfg udp_conf
;
2791 memset(&udp_conf
, 0, sizeof(udp_conf
));
2794 udp_conf
.family
= AF_INET6
;
2795 udp_conf
.use_udp6_rx_checksums
=
2796 !(flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
);
2797 udp_conf
.ipv6_v6only
= 1;
2799 udp_conf
.family
= AF_INET
;
2802 udp_conf
.local_udp_port
= port
;
2804 /* Open UDP socket */
2805 err
= udp_sock_create(net
, &udp_conf
, &sock
);
2807 return ERR_PTR(err
);
2812 /* Create new listen socket if needed */
2813 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, bool ipv6
,
2814 __be16 port
, u32 flags
)
2816 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2817 struct vxlan_sock
*vs
;
2818 struct socket
*sock
;
2820 struct udp_tunnel_sock_cfg tunnel_cfg
;
2822 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
);
2824 return ERR_PTR(-ENOMEM
);
2826 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
2827 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
2829 sock
= vxlan_create_sock(net
, ipv6
, port
, flags
);
2832 return ERR_CAST(sock
);
2836 refcount_set(&vs
->refcnt
, 1);
2837 vs
->flags
= (flags
& VXLAN_F_RCV_FLAGS
);
2839 spin_lock(&vn
->sock_lock
);
2840 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
2841 udp_tunnel_notify_add_rx_port(sock
,
2842 (vs
->flags
& VXLAN_F_GPE
) ?
2843 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2844 UDP_TUNNEL_TYPE_VXLAN
);
2845 spin_unlock(&vn
->sock_lock
);
2847 /* Mark socket as an encapsulation socket. */
2848 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
2849 tunnel_cfg
.sk_user_data
= vs
;
2850 tunnel_cfg
.encap_type
= 1;
2851 tunnel_cfg
.encap_rcv
= vxlan_rcv
;
2852 tunnel_cfg
.encap_destroy
= NULL
;
2853 tunnel_cfg
.gro_receive
= vxlan_gro_receive
;
2854 tunnel_cfg
.gro_complete
= vxlan_gro_complete
;
2856 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
2861 static int __vxlan_sock_add(struct vxlan_dev
*vxlan
, bool ipv6
)
2863 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2864 struct vxlan_sock
*vs
= NULL
;
2865 struct vxlan_dev_node
*node
;
2867 if (!vxlan
->cfg
.no_share
) {
2868 spin_lock(&vn
->sock_lock
);
2869 vs
= vxlan_find_sock(vxlan
->net
, ipv6
? AF_INET6
: AF_INET
,
2870 vxlan
->cfg
.dst_port
, vxlan
->cfg
.flags
);
2871 if (vs
&& !refcount_inc_not_zero(&vs
->refcnt
)) {
2872 spin_unlock(&vn
->sock_lock
);
2875 spin_unlock(&vn
->sock_lock
);
2878 vs
= vxlan_socket_create(vxlan
->net
, ipv6
,
2879 vxlan
->cfg
.dst_port
, vxlan
->cfg
.flags
);
2882 #if IS_ENABLED(CONFIG_IPV6)
2884 rcu_assign_pointer(vxlan
->vn6_sock
, vs
);
2885 node
= &vxlan
->hlist6
;
2889 rcu_assign_pointer(vxlan
->vn4_sock
, vs
);
2890 node
= &vxlan
->hlist4
;
2892 vxlan_vs_add_dev(vs
, vxlan
, node
);
2896 static int vxlan_sock_add(struct vxlan_dev
*vxlan
)
2898 bool metadata
= vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
;
2899 bool ipv6
= vxlan
->cfg
.flags
& VXLAN_F_IPV6
|| metadata
;
2900 bool ipv4
= !ipv6
|| metadata
;
2903 RCU_INIT_POINTER(vxlan
->vn4_sock
, NULL
);
2904 #if IS_ENABLED(CONFIG_IPV6)
2905 RCU_INIT_POINTER(vxlan
->vn6_sock
, NULL
);
2907 ret
= __vxlan_sock_add(vxlan
, true);
2908 if (ret
< 0 && ret
!= -EAFNOSUPPORT
)
2913 ret
= __vxlan_sock_add(vxlan
, false);
2915 vxlan_sock_release(vxlan
);
2919 static int vxlan_config_validate(struct net
*src_net
, struct vxlan_config
*conf
,
2920 struct net_device
**lower
,
2921 struct vxlan_dev
*old
)
2923 struct vxlan_net
*vn
= net_generic(src_net
, vxlan_net_id
);
2924 struct vxlan_dev
*tmp
;
2925 bool use_ipv6
= false;
2927 if (conf
->flags
& VXLAN_F_GPE
) {
2928 /* For now, allow GPE only together with
2929 * COLLECT_METADATA. This can be relaxed later; in such
2930 * case, the other side of the PtP link will have to be
2933 if ((conf
->flags
& ~VXLAN_F_ALLOWED_GPE
) ||
2934 !(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
2939 if (!conf
->remote_ip
.sa
.sa_family
&& !conf
->saddr
.sa
.sa_family
) {
2940 /* Unless IPv6 is explicitly requested, assume IPv4 */
2941 conf
->remote_ip
.sa
.sa_family
= AF_INET
;
2942 conf
->saddr
.sa
.sa_family
= AF_INET
;
2943 } else if (!conf
->remote_ip
.sa
.sa_family
) {
2944 conf
->remote_ip
.sa
.sa_family
= conf
->saddr
.sa
.sa_family
;
2945 } else if (!conf
->saddr
.sa
.sa_family
) {
2946 conf
->saddr
.sa
.sa_family
= conf
->remote_ip
.sa
.sa_family
;
2949 if (conf
->saddr
.sa
.sa_family
!= conf
->remote_ip
.sa
.sa_family
)
2952 if (vxlan_addr_multicast(&conf
->saddr
))
2955 if (conf
->saddr
.sa
.sa_family
== AF_INET6
) {
2956 if (!IS_ENABLED(CONFIG_IPV6
))
2957 return -EPFNOSUPPORT
;
2959 conf
->flags
|= VXLAN_F_IPV6
;
2961 if (!(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
2963 ipv6_addr_type(&conf
->saddr
.sin6
.sin6_addr
);
2965 ipv6_addr_type(&conf
->remote_ip
.sin6
.sin6_addr
);
2967 if (local_type
& IPV6_ADDR_LINKLOCAL
) {
2968 if (!(remote_type
& IPV6_ADDR_LINKLOCAL
) &&
2969 (remote_type
!= IPV6_ADDR_ANY
))
2972 conf
->flags
|= VXLAN_F_IPV6_LINKLOCAL
;
2975 (IPV6_ADDR_UNICAST
| IPV6_ADDR_LINKLOCAL
))
2978 conf
->flags
&= ~VXLAN_F_IPV6_LINKLOCAL
;
2983 if (conf
->label
&& !use_ipv6
)
2986 if (conf
->remote_ifindex
) {
2987 struct net_device
*lowerdev
;
2989 lowerdev
= __dev_get_by_index(src_net
, conf
->remote_ifindex
);
2993 #if IS_ENABLED(CONFIG_IPV6)
2995 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
2996 if (idev
&& idev
->cnf
.disable_ipv6
)
3003 if (vxlan_addr_multicast(&conf
->remote_ip
))
3006 #if IS_ENABLED(CONFIG_IPV6)
3007 if (conf
->flags
& VXLAN_F_IPV6_LINKLOCAL
)
3014 if (!conf
->dst_port
) {
3015 if (conf
->flags
& VXLAN_F_GPE
)
3016 conf
->dst_port
= htons(4790); /* IANA VXLAN-GPE port */
3018 conf
->dst_port
= htons(vxlan_port
);
3021 if (!conf
->age_interval
)
3022 conf
->age_interval
= FDB_AGE_DEFAULT
;
3024 list_for_each_entry(tmp
, &vn
->vxlan_list
, next
) {
3028 if (tmp
->cfg
.vni
!= conf
->vni
)
3030 if (tmp
->cfg
.dst_port
!= conf
->dst_port
)
3032 if ((tmp
->cfg
.flags
& (VXLAN_F_RCV_FLAGS
| VXLAN_F_IPV6
)) !=
3033 (conf
->flags
& (VXLAN_F_RCV_FLAGS
| VXLAN_F_IPV6
)))
3036 if ((conf
->flags
& VXLAN_F_IPV6_LINKLOCAL
) &&
3037 tmp
->cfg
.remote_ifindex
!= conf
->remote_ifindex
)
3046 static void vxlan_config_apply(struct net_device
*dev
,
3047 struct vxlan_config
*conf
,
3048 struct net_device
*lowerdev
,
3049 struct net
*src_net
,
3052 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3053 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3054 unsigned short needed_headroom
= ETH_HLEN
;
3055 bool use_ipv6
= !!(conf
->flags
& VXLAN_F_IPV6
);
3056 int max_mtu
= ETH_MAX_MTU
;
3059 if (conf
->flags
& VXLAN_F_GPE
)
3060 vxlan_raw_setup(dev
);
3062 vxlan_ether_setup(dev
);
3065 dev
->mtu
= conf
->mtu
;
3067 vxlan
->net
= src_net
;
3070 dst
->remote_vni
= conf
->vni
;
3072 memcpy(&dst
->remote_ip
, &conf
->remote_ip
, sizeof(conf
->remote_ip
));
3075 dst
->remote_ifindex
= conf
->remote_ifindex
;
3077 dev
->gso_max_size
= lowerdev
->gso_max_size
;
3078 dev
->gso_max_segs
= lowerdev
->gso_max_segs
;
3080 needed_headroom
= lowerdev
->hard_header_len
;
3082 max_mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
:
3086 if (dev
->mtu
> max_mtu
)
3089 if (use_ipv6
|| conf
->flags
& VXLAN_F_COLLECT_METADATA
)
3090 needed_headroom
+= VXLAN6_HEADROOM
;
3092 needed_headroom
+= VXLAN_HEADROOM
;
3093 dev
->needed_headroom
= needed_headroom
;
3095 memcpy(&vxlan
->cfg
, conf
, sizeof(*conf
));
3098 static int vxlan_dev_configure(struct net
*src_net
, struct net_device
*dev
,
3099 struct vxlan_config
*conf
,
3102 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3103 struct net_device
*lowerdev
;
3106 ret
= vxlan_config_validate(src_net
, conf
, &lowerdev
, vxlan
);
3110 vxlan_config_apply(dev
, conf
, lowerdev
, src_net
, changelink
);
3115 static int __vxlan_dev_create(struct net
*net
, struct net_device
*dev
,
3116 struct vxlan_config
*conf
)
3118 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3119 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3122 err
= vxlan_dev_configure(net
, dev
, conf
, false);
3126 dev
->ethtool_ops
= &vxlan_ethtool_ops
;
3128 /* create an fdb entry for a valid default destination */
3129 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
3130 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
3131 &vxlan
->default_dst
.remote_ip
,
3132 NUD_REACHABLE
| NUD_PERMANENT
,
3133 NLM_F_EXCL
| NLM_F_CREATE
,
3134 vxlan
->cfg
.dst_port
,
3135 vxlan
->default_dst
.remote_vni
,
3136 vxlan
->default_dst
.remote_vni
,
3137 vxlan
->default_dst
.remote_ifindex
,
3143 err
= register_netdevice(dev
);
3145 vxlan_fdb_delete_default(vxlan
, vxlan
->default_dst
.remote_vni
);
3149 list_add(&vxlan
->next
, &vn
->vxlan_list
);
3153 static int vxlan_nl2conf(struct nlattr
*tb
[], struct nlattr
*data
[],
3154 struct net_device
*dev
, struct vxlan_config
*conf
,
3157 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3159 memset(conf
, 0, sizeof(*conf
));
3161 /* if changelink operation, start with old existing cfg */
3163 memcpy(conf
, &vxlan
->cfg
, sizeof(*conf
));
3165 if (data
[IFLA_VXLAN_ID
]) {
3166 __be32 vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3168 if (changelink
&& (vni
!= conf
->vni
))
3170 conf
->vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3173 if (data
[IFLA_VXLAN_GROUP
]) {
3174 if (changelink
&& (conf
->remote_ip
.sa
.sa_family
!= AF_INET
))
3177 conf
->remote_ip
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_GROUP
]);
3178 conf
->remote_ip
.sa
.sa_family
= AF_INET
;
3179 } else if (data
[IFLA_VXLAN_GROUP6
]) {
3180 if (!IS_ENABLED(CONFIG_IPV6
))
3181 return -EPFNOSUPPORT
;
3183 if (changelink
&& (conf
->remote_ip
.sa
.sa_family
!= AF_INET6
))
3186 conf
->remote_ip
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_GROUP6
]);
3187 conf
->remote_ip
.sa
.sa_family
= AF_INET6
;
3190 if (data
[IFLA_VXLAN_LOCAL
]) {
3191 if (changelink
&& (conf
->saddr
.sa
.sa_family
!= AF_INET
))
3194 conf
->saddr
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_LOCAL
]);
3195 conf
->saddr
.sa
.sa_family
= AF_INET
;
3196 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
3197 if (!IS_ENABLED(CONFIG_IPV6
))
3198 return -EPFNOSUPPORT
;
3200 if (changelink
&& (conf
->saddr
.sa
.sa_family
!= AF_INET6
))
3203 /* TODO: respect scope id */
3204 conf
->saddr
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_LOCAL6
]);
3205 conf
->saddr
.sa
.sa_family
= AF_INET6
;
3208 if (data
[IFLA_VXLAN_LINK
])
3209 conf
->remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]);
3211 if (data
[IFLA_VXLAN_TOS
])
3212 conf
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
3214 if (data
[IFLA_VXLAN_TTL
])
3215 conf
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
3217 if (data
[IFLA_VXLAN_LABEL
])
3218 conf
->label
= nla_get_be32(data
[IFLA_VXLAN_LABEL
]) &
3219 IPV6_FLOWLABEL_MASK
;
3221 if (data
[IFLA_VXLAN_LEARNING
]) {
3222 if (nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
3223 conf
->flags
|= VXLAN_F_LEARN
;
3225 conf
->flags
&= ~VXLAN_F_LEARN
;
3226 } else if (!changelink
) {
3227 /* default to learn on a new device */
3228 conf
->flags
|= VXLAN_F_LEARN
;
3231 if (data
[IFLA_VXLAN_AGEING
]) {
3234 conf
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
3237 if (data
[IFLA_VXLAN_PROXY
]) {
3240 if (nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
3241 conf
->flags
|= VXLAN_F_PROXY
;
3244 if (data
[IFLA_VXLAN_RSC
]) {
3247 if (nla_get_u8(data
[IFLA_VXLAN_RSC
]))
3248 conf
->flags
|= VXLAN_F_RSC
;
3251 if (data
[IFLA_VXLAN_L2MISS
]) {
3254 if (nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
3255 conf
->flags
|= VXLAN_F_L2MISS
;
3258 if (data
[IFLA_VXLAN_L3MISS
]) {
3261 if (nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
3262 conf
->flags
|= VXLAN_F_L3MISS
;
3265 if (data
[IFLA_VXLAN_LIMIT
]) {
3268 conf
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
3271 if (data
[IFLA_VXLAN_COLLECT_METADATA
]) {
3274 if (nla_get_u8(data
[IFLA_VXLAN_COLLECT_METADATA
]))
3275 conf
->flags
|= VXLAN_F_COLLECT_METADATA
;
3278 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
3280 const struct ifla_vxlan_port_range
*p
3281 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
3282 conf
->port_min
= ntohs(p
->low
);
3283 conf
->port_max
= ntohs(p
->high
);
3289 if (data
[IFLA_VXLAN_PORT
]) {
3292 conf
->dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
3295 if (data
[IFLA_VXLAN_UDP_CSUM
]) {
3298 if (!nla_get_u8(data
[IFLA_VXLAN_UDP_CSUM
]))
3299 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM_TX
;
3302 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]) {
3305 if (nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
3306 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_TX
;
3309 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]) {
3312 if (nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
3313 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_RX
;
3316 if (data
[IFLA_VXLAN_REMCSUM_TX
]) {
3319 if (nla_get_u8(data
[IFLA_VXLAN_REMCSUM_TX
]))
3320 conf
->flags
|= VXLAN_F_REMCSUM_TX
;
3323 if (data
[IFLA_VXLAN_REMCSUM_RX
]) {
3326 if (nla_get_u8(data
[IFLA_VXLAN_REMCSUM_RX
]))
3327 conf
->flags
|= VXLAN_F_REMCSUM_RX
;
3330 if (data
[IFLA_VXLAN_GBP
]) {
3333 conf
->flags
|= VXLAN_F_GBP
;
3336 if (data
[IFLA_VXLAN_GPE
]) {
3339 conf
->flags
|= VXLAN_F_GPE
;
3342 if (data
[IFLA_VXLAN_REMCSUM_NOPARTIAL
]) {
3345 conf
->flags
|= VXLAN_F_REMCSUM_NOPARTIAL
;
3351 conf
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3357 static int vxlan_newlink(struct net
*src_net
, struct net_device
*dev
,
3358 struct nlattr
*tb
[], struct nlattr
*data
[],
3359 struct netlink_ext_ack
*extack
)
3361 struct vxlan_config conf
;
3364 err
= vxlan_nl2conf(tb
, data
, dev
, &conf
, false);
3368 return __vxlan_dev_create(src_net
, dev
, &conf
);
3371 static int vxlan_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
3372 struct nlattr
*data
[],
3373 struct netlink_ext_ack
*extack
)
3375 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3376 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3377 struct vxlan_rdst old_dst
;
3378 struct vxlan_config conf
;
3381 err
= vxlan_nl2conf(tb
, data
,
3386 memcpy(&old_dst
, dst
, sizeof(struct vxlan_rdst
));
3388 err
= vxlan_dev_configure(vxlan
->net
, dev
, &conf
, true);
3392 /* handle default dst entry */
3393 if (!vxlan_addr_equal(&dst
->remote_ip
, &old_dst
.remote_ip
)) {
3394 spin_lock_bh(&vxlan
->hash_lock
);
3395 if (!vxlan_addr_any(&old_dst
.remote_ip
))
3396 __vxlan_fdb_delete(vxlan
, all_zeros_mac
,
3398 vxlan
->cfg
.dst_port
,
3401 old_dst
.remote_ifindex
, 0);
3403 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3404 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
3406 NUD_REACHABLE
| NUD_PERMANENT
,
3407 NLM_F_CREATE
| NLM_F_APPEND
,
3408 vxlan
->cfg
.dst_port
,
3411 dst
->remote_ifindex
,
3414 spin_unlock_bh(&vxlan
->hash_lock
);
3418 spin_unlock_bh(&vxlan
->hash_lock
);
3424 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
3426 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3428 vxlan_flush(vxlan
, true);
3430 gro_cells_destroy(&vxlan
->gro_cells
);
3431 list_del(&vxlan
->next
);
3432 unregister_netdevice_queue(dev
, head
);
3435 static size_t vxlan_get_size(const struct net_device
*dev
)
3438 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
3439 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
3440 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
3441 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
3442 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
3443 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
3444 nla_total_size(sizeof(__be32
)) + /* IFLA_VXLAN_LABEL */
3445 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
3446 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
3447 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
3448 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
3449 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
3450 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_COLLECT_METADATA */
3451 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
3452 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
3453 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
3454 nla_total_size(sizeof(__be16
)) + /* IFLA_VXLAN_PORT */
3455 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_CSUM */
3456 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3457 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3458 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_TX */
3459 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_RX */
3463 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
3465 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3466 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3467 struct ifla_vxlan_port_range ports
= {
3468 .low
= htons(vxlan
->cfg
.port_min
),
3469 .high
= htons(vxlan
->cfg
.port_max
),
3472 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, be32_to_cpu(dst
->remote_vni
)))
3473 goto nla_put_failure
;
3475 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3476 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
3477 if (nla_put_in_addr(skb
, IFLA_VXLAN_GROUP
,
3478 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
3479 goto nla_put_failure
;
3480 #if IS_ENABLED(CONFIG_IPV6)
3482 if (nla_put_in6_addr(skb
, IFLA_VXLAN_GROUP6
,
3483 &dst
->remote_ip
.sin6
.sin6_addr
))
3484 goto nla_put_failure
;
3489 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
3490 goto nla_put_failure
;
3492 if (!vxlan_addr_any(&vxlan
->cfg
.saddr
)) {
3493 if (vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET
) {
3494 if (nla_put_in_addr(skb
, IFLA_VXLAN_LOCAL
,
3495 vxlan
->cfg
.saddr
.sin
.sin_addr
.s_addr
))
3496 goto nla_put_failure
;
3497 #if IS_ENABLED(CONFIG_IPV6)
3499 if (nla_put_in6_addr(skb
, IFLA_VXLAN_LOCAL6
,
3500 &vxlan
->cfg
.saddr
.sin6
.sin6_addr
))
3501 goto nla_put_failure
;
3506 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->cfg
.ttl
) ||
3507 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->cfg
.tos
) ||
3508 nla_put_be32(skb
, IFLA_VXLAN_LABEL
, vxlan
->cfg
.label
) ||
3509 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
3510 !!(vxlan
->cfg
.flags
& VXLAN_F_LEARN
)) ||
3511 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
3512 !!(vxlan
->cfg
.flags
& VXLAN_F_PROXY
)) ||
3513 nla_put_u8(skb
, IFLA_VXLAN_RSC
,
3514 !!(vxlan
->cfg
.flags
& VXLAN_F_RSC
)) ||
3515 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
3516 !!(vxlan
->cfg
.flags
& VXLAN_F_L2MISS
)) ||
3517 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
3518 !!(vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) ||
3519 nla_put_u8(skb
, IFLA_VXLAN_COLLECT_METADATA
,
3520 !!(vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
)) ||
3521 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->cfg
.age_interval
) ||
3522 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->cfg
.addrmax
) ||
3523 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->cfg
.dst_port
) ||
3524 nla_put_u8(skb
, IFLA_VXLAN_UDP_CSUM
,
3525 !(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM_TX
)) ||
3526 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_TX
,
3527 !!(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
)) ||
3528 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_RX
,
3529 !!(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
)) ||
3530 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_TX
,
3531 !!(vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_TX
)) ||
3532 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_RX
,
3533 !!(vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_RX
)))
3534 goto nla_put_failure
;
3536 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
3537 goto nla_put_failure
;
3539 if (vxlan
->cfg
.flags
& VXLAN_F_GBP
&&
3540 nla_put_flag(skb
, IFLA_VXLAN_GBP
))
3541 goto nla_put_failure
;
3543 if (vxlan
->cfg
.flags
& VXLAN_F_GPE
&&
3544 nla_put_flag(skb
, IFLA_VXLAN_GPE
))
3545 goto nla_put_failure
;
3547 if (vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_NOPARTIAL
&&
3548 nla_put_flag(skb
, IFLA_VXLAN_REMCSUM_NOPARTIAL
))
3549 goto nla_put_failure
;
3557 static struct net
*vxlan_get_link_net(const struct net_device
*dev
)
3559 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3564 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
3566 .maxtype
= IFLA_VXLAN_MAX
,
3567 .policy
= vxlan_policy
,
3568 .priv_size
= sizeof(struct vxlan_dev
),
3569 .setup
= vxlan_setup
,
3570 .validate
= vxlan_validate
,
3571 .newlink
= vxlan_newlink
,
3572 .changelink
= vxlan_changelink
,
3573 .dellink
= vxlan_dellink
,
3574 .get_size
= vxlan_get_size
,
3575 .fill_info
= vxlan_fill_info
,
3576 .get_link_net
= vxlan_get_link_net
,
3579 struct net_device
*vxlan_dev_create(struct net
*net
, const char *name
,
3580 u8 name_assign_type
,
3581 struct vxlan_config
*conf
)
3583 struct nlattr
*tb
[IFLA_MAX
+ 1];
3584 struct net_device
*dev
;
3587 memset(&tb
, 0, sizeof(tb
));
3589 dev
= rtnl_create_link(net
, name
, name_assign_type
,
3590 &vxlan_link_ops
, tb
);
3594 err
= __vxlan_dev_create(net
, dev
, conf
);
3597 return ERR_PTR(err
);
3600 err
= rtnl_configure_link(dev
, NULL
);
3602 LIST_HEAD(list_kill
);
3604 vxlan_dellink(dev
, &list_kill
);
3605 unregister_netdevice_many(&list_kill
);
3606 return ERR_PTR(err
);
3611 EXPORT_SYMBOL_GPL(vxlan_dev_create
);
3613 static void vxlan_handle_lowerdev_unregister(struct vxlan_net
*vn
,
3614 struct net_device
*dev
)
3616 struct vxlan_dev
*vxlan
, *next
;
3617 LIST_HEAD(list_kill
);
3619 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3620 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3622 /* In case we created vxlan device with carrier
3623 * and we loose the carrier due to module unload
3624 * we also need to remove vxlan device. In other
3625 * cases, it's not necessary and remote_ifindex
3626 * is 0 here, so no matches.
3628 if (dst
->remote_ifindex
== dev
->ifindex
)
3629 vxlan_dellink(vxlan
->dev
, &list_kill
);
3632 unregister_netdevice_many(&list_kill
);
3635 static int vxlan_netdevice_event(struct notifier_block
*unused
,
3636 unsigned long event
, void *ptr
)
3638 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3639 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
3641 if (event
== NETDEV_UNREGISTER
) {
3642 vxlan_offload_rx_ports(dev
, false);
3643 vxlan_handle_lowerdev_unregister(vn
, dev
);
3644 } else if (event
== NETDEV_REGISTER
) {
3645 vxlan_offload_rx_ports(dev
, true);
3646 } else if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
||
3647 event
== NETDEV_UDP_TUNNEL_DROP_INFO
) {
3648 vxlan_offload_rx_ports(dev
, event
== NETDEV_UDP_TUNNEL_PUSH_INFO
);
3654 static struct notifier_block vxlan_notifier_block __read_mostly
= {
3655 .notifier_call
= vxlan_netdevice_event
,
3658 static __net_init
int vxlan_init_net(struct net
*net
)
3660 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3663 INIT_LIST_HEAD(&vn
->vxlan_list
);
3664 spin_lock_init(&vn
->sock_lock
);
3666 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
3667 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
3672 static void __net_exit
vxlan_exit_net(struct net
*net
)
3674 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3675 struct vxlan_dev
*vxlan
, *next
;
3676 struct net_device
*dev
, *aux
;
3680 for_each_netdev_safe(net
, dev
, aux
)
3681 if (dev
->rtnl_link_ops
== &vxlan_link_ops
)
3682 unregister_netdevice_queue(dev
, &list
);
3684 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3685 /* If vxlan->dev is in the same netns, it has already been added
3686 * to the list by the previous loop.
3688 if (!net_eq(dev_net(vxlan
->dev
), net
)) {
3689 gro_cells_destroy(&vxlan
->gro_cells
);
3690 unregister_netdevice_queue(vxlan
->dev
, &list
);
3694 unregister_netdevice_many(&list
);
3698 static struct pernet_operations vxlan_net_ops
= {
3699 .init
= vxlan_init_net
,
3700 .exit
= vxlan_exit_net
,
3701 .id
= &vxlan_net_id
,
3702 .size
= sizeof(struct vxlan_net
),
3705 static int __init
vxlan_init_module(void)
3709 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
3711 rc
= register_pernet_subsys(&vxlan_net_ops
);
3715 rc
= register_netdevice_notifier(&vxlan_notifier_block
);
3719 rc
= rtnl_link_register(&vxlan_link_ops
);
3725 unregister_netdevice_notifier(&vxlan_notifier_block
);
3727 unregister_pernet_subsys(&vxlan_net_ops
);
3731 late_initcall(vxlan_init_module
);
3733 static void __exit
vxlan_cleanup_module(void)
3735 rtnl_link_unregister(&vxlan_link_ops
);
3736 unregister_netdevice_notifier(&vxlan_notifier_block
);
3737 unregister_pernet_subsys(&vxlan_net_ops
);
3738 /* rcu_barrier() is called by netns */
3740 module_exit(vxlan_cleanup_module
);
3742 MODULE_LICENSE("GPL");
3743 MODULE_VERSION(VXLAN_VERSION
);
3744 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3745 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3746 MODULE_ALIAS_RTNL_LINK("vxlan");