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/inetdevice.h>
20 #include <linux/if_ether.h>
21 #include <linux/ethtool.h>
23 #include <net/ndisc.h>
26 #include <net/rtnetlink.h>
27 #include <net/inet_ecn.h>
28 #include <net/net_namespace.h>
29 #include <net/netns/generic.h>
30 #include <net/vxlan.h>
32 #if IS_ENABLED(CONFIG_IPV6)
33 #include <net/ip6_tunnel.h>
34 #include <net/ip6_checksum.h>
37 #define VXLAN_VERSION "0.1"
39 #define PORT_HASH_BITS 8
40 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
41 #define FDB_AGE_DEFAULT 300 /* 5 min */
42 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
44 /* UDP port for VXLAN traffic.
45 * The IANA assigned port is 4789, but the Linux default is 8472
46 * for compatibility with early adopters.
48 static unsigned short vxlan_port __read_mostly
= 8472;
49 module_param_named(udp_port
, vxlan_port
, ushort
, 0444);
50 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
52 static bool log_ecn_error
= true;
53 module_param(log_ecn_error
, bool, 0644);
54 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
56 static unsigned int vxlan_net_id
;
57 static struct rtnl_link_ops vxlan_link_ops
;
59 static const u8 all_zeros_mac
[ETH_ALEN
+ 2];
61 static int vxlan_sock_add(struct vxlan_dev
*vxlan
);
63 static void vxlan_vs_del_dev(struct vxlan_dev
*vxlan
);
65 /* per-network namespace private data for this module */
67 struct list_head vxlan_list
;
68 struct hlist_head sock_list
[PORT_HASH_SIZE
];
72 /* Forwarding table entry */
74 struct hlist_node hlist
; /* linked list of entries */
76 unsigned long updated
; /* jiffies */
78 struct list_head remotes
;
79 u8 eth_addr
[ETH_ALEN
];
80 u16 state
; /* see ndm_state */
82 u8 flags
; /* see ndm_flags */
85 /* salt for hash table */
86 static u32 vxlan_salt __read_mostly
;
88 static inline bool vxlan_collect_metadata(struct vxlan_sock
*vs
)
90 return vs
->flags
& VXLAN_F_COLLECT_METADATA
||
91 ip_tunnel_collect_metadata();
94 static struct ip_fan_map
*vxlan_fan_find_map(struct vxlan_dev
*vxlan
, __be32 daddr
)
96 struct ip_fan_map
*fan_map
;
99 list_for_each_entry_rcu(fan_map
, &vxlan
->fan
.fan_maps
, list
) {
100 if (fan_map
->overlay
==
101 (daddr
& inet_make_mask(fan_map
->overlay_prefix
))) {
111 static void vxlan_fan_flush_map(struct vxlan_dev
*vxlan
)
113 struct ip_fan_map
*fan_map
;
115 list_for_each_entry_rcu(fan_map
, &vxlan
->fan
.fan_maps
, list
) {
116 list_del_rcu(&fan_map
->list
);
117 kfree_rcu(fan_map
, rcu
);
121 static int vxlan_fan_del_map(struct vxlan_dev
*vxlan
, __be32 overlay
)
123 struct ip_fan_map
*fan_map
;
125 fan_map
= vxlan_fan_find_map(vxlan
, overlay
);
129 list_del_rcu(&fan_map
->list
);
130 kfree_rcu(fan_map
, rcu
);
135 static int vxlan_fan_add_map(struct vxlan_dev
*vxlan
, struct ifla_fan_map
*map
)
137 __be32 overlay_mask
, underlay_mask
;
138 struct ip_fan_map
*fan_map
;
140 overlay_mask
= inet_make_mask(map
->overlay_prefix
);
141 underlay_mask
= inet_make_mask(map
->underlay_prefix
);
143 netdev_dbg(vxlan
->dev
, "vfam: map: o %x/%d u %x/%d om %x um %x\n",
144 map
->overlay
, map
->overlay_prefix
,
145 map
->underlay
, map
->underlay_prefix
,
146 overlay_mask
, underlay_mask
);
148 if ((map
->overlay
& ~overlay_mask
) || (map
->underlay
& ~underlay_mask
))
151 if (!(map
->overlay
& overlay_mask
) && (map
->underlay
& underlay_mask
))
154 /* Special case: overlay 0 and underlay 0: flush all mappings */
155 if (!map
->overlay
&& !map
->underlay
) {
156 vxlan_fan_flush_map(vxlan
);
160 /* Special case: overlay set and underlay 0: clear map for overlay */
162 return vxlan_fan_del_map(vxlan
, map
->overlay
);
164 if (vxlan_fan_find_map(vxlan
, map
->overlay
))
167 fan_map
= kmalloc(sizeof(*fan_map
), GFP_KERNEL
);
168 fan_map
->underlay
= map
->underlay
;
169 fan_map
->overlay
= map
->overlay
;
170 fan_map
->underlay_prefix
= map
->underlay_prefix
;
171 fan_map
->overlay_mask
= ntohl(overlay_mask
);
172 fan_map
->overlay_prefix
= map
->overlay_prefix
;
174 list_add_tail_rcu(&fan_map
->list
, &vxlan
->fan
.fan_maps
);
179 static int vxlan_parse_fan_map(struct nlattr
*data
[], struct vxlan_dev
*vxlan
)
181 struct ifla_fan_map
*map
;
185 nla_for_each_nested(attr
, data
[IFLA_IPTUN_FAN_MAP
], rem
) {
186 map
= nla_data(attr
);
187 rv
= vxlan_fan_add_map(vxlan
, map
);
195 static int vxlan_fan_build_rdst(struct vxlan_dev
*vxlan
, struct sk_buff
*skb
,
196 struct vxlan_rdst
*fan_rdst
)
198 struct ip_fan_map
*f_map
;
199 union vxlan_addr
*va
;
207 switch (eth
->h_proto
) {
208 case htons(ETH_P_IP
):
214 case htons(ETH_P_ARP
):
219 netdev_dbg(vxlan
->dev
,
220 "vfbr: arp sha %pM sip %pI4 tha %pM tip %pI4\n",
221 arp_ptr
, arp_ptr
+ skb
->dev
->addr_len
,
222 arp_ptr
+ skb
->dev
->addr_len
+ 4,
223 arp_ptr
+ (skb
->dev
->addr_len
* 2) + 4);
224 arp_ptr
+= (skb
->dev
->addr_len
* 2) + 4;
225 memcpy(&daddr
, arp_ptr
, 4);
228 netdev_dbg(vxlan
->dev
, "vfbr: unknown eth p %x\n", eth
->h_proto
);
232 f_map
= vxlan_fan_find_map(vxlan
, daddr
);
236 daddr
= ntohl(daddr
);
237 underlay
= ntohl(f_map
->underlay
);
241 memset(fan_rdst
, 0, sizeof(*fan_rdst
));
242 va
= &fan_rdst
->remote_ip
;
243 va
->sa
.sa_family
= AF_INET
;
244 fan_rdst
->remote_vni
= vxlan
->default_dst
.remote_vni
;
245 va
->sin
.sin_addr
.s_addr
= htonl(underlay
|
246 ((daddr
& ~f_map
->overlay_mask
) >>
247 (32 - f_map
->overlay_prefix
-
248 (32 - f_map
->underlay_prefix
))));
249 netdev_dbg(vxlan
->dev
, "vfbr: daddr %x ul %x dst %x\n",
250 daddr
, underlay
, va
->sin
.sin_addr
.s_addr
);
255 #if IS_ENABLED(CONFIG_IPV6)
257 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
259 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
261 if (a
->sa
.sa_family
== AF_INET6
)
262 return ipv6_addr_equal(&a
->sin6
.sin6_addr
, &b
->sin6
.sin6_addr
);
264 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
267 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
269 if (ipa
->sa
.sa_family
== AF_INET6
)
270 return ipv6_addr_any(&ipa
->sin6
.sin6_addr
);
272 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
275 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
277 if (ipa
->sa
.sa_family
== AF_INET6
)
278 return ipv6_addr_is_multicast(&ipa
->sin6
.sin6_addr
);
280 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
283 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
285 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
286 ip
->sin6
.sin6_addr
= nla_get_in6_addr(nla
);
287 ip
->sa
.sa_family
= AF_INET6
;
289 } else if (nla_len(nla
) >= sizeof(__be32
)) {
290 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
291 ip
->sa
.sa_family
= AF_INET
;
294 return -EAFNOSUPPORT
;
298 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
299 const union vxlan_addr
*ip
)
301 if (ip
->sa
.sa_family
== AF_INET6
)
302 return nla_put_in6_addr(skb
, attr
, &ip
->sin6
.sin6_addr
);
304 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
307 #else /* !CONFIG_IPV6 */
310 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
312 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
315 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
317 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
320 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
322 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
325 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
327 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
328 return -EAFNOSUPPORT
;
329 } else if (nla_len(nla
) >= sizeof(__be32
)) {
330 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
331 ip
->sa
.sa_family
= AF_INET
;
334 return -EAFNOSUPPORT
;
338 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
339 const union vxlan_addr
*ip
)
341 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
345 /* Virtual Network hash table head */
346 static inline struct hlist_head
*vni_head(struct vxlan_sock
*vs
, __be32 vni
)
348 return &vs
->vni_list
[hash_32((__force u32
)vni
, VNI_HASH_BITS
)];
351 /* Socket hash table head */
352 static inline struct hlist_head
*vs_head(struct net
*net
, __be16 port
)
354 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
356 return &vn
->sock_list
[hash_32(ntohs(port
), PORT_HASH_BITS
)];
359 /* First remote destination for a forwarding entry.
360 * Guaranteed to be non-NULL because remotes are never deleted.
362 static inline struct vxlan_rdst
*first_remote_rcu(struct vxlan_fdb
*fdb
)
364 return list_entry_rcu(fdb
->remotes
.next
, struct vxlan_rdst
, list
);
367 static inline struct vxlan_rdst
*first_remote_rtnl(struct vxlan_fdb
*fdb
)
369 return list_first_entry(&fdb
->remotes
, struct vxlan_rdst
, list
);
372 /* Find VXLAN socket based on network namespace, address family and UDP port
373 * and enabled unshareable flags.
375 static struct vxlan_sock
*vxlan_find_sock(struct net
*net
, sa_family_t family
,
376 __be16 port
, u32 flags
)
378 struct vxlan_sock
*vs
;
380 flags
&= VXLAN_F_RCV_FLAGS
;
382 hlist_for_each_entry_rcu(vs
, vs_head(net
, port
), hlist
) {
383 if (inet_sk(vs
->sock
->sk
)->inet_sport
== port
&&
384 vxlan_get_sk_family(vs
) == family
&&
391 static struct vxlan_dev
*vxlan_vs_find_vni(struct vxlan_sock
*vs
, int ifindex
,
394 struct vxlan_dev_node
*node
;
396 /* For flow based devices, map all packets to VNI 0 */
397 if (vs
->flags
& VXLAN_F_COLLECT_METADATA
)
400 hlist_for_each_entry_rcu(node
, vni_head(vs
, vni
), hlist
) {
401 if (node
->vxlan
->default_dst
.remote_vni
!= vni
)
404 if (IS_ENABLED(CONFIG_IPV6
)) {
405 const struct vxlan_config
*cfg
= &node
->vxlan
->cfg
;
407 if ((cfg
->flags
& VXLAN_F_IPV6_LINKLOCAL
) &&
408 cfg
->remote_ifindex
!= ifindex
)
418 /* Look up VNI in a per net namespace table */
419 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, int ifindex
,
420 __be32 vni
, sa_family_t family
,
421 __be16 port
, u32 flags
)
423 struct vxlan_sock
*vs
;
425 vs
= vxlan_find_sock(net
, family
, port
, flags
);
429 return vxlan_vs_find_vni(vs
, ifindex
, vni
);
432 /* Fill in neighbour message in skbuff. */
433 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
434 const struct vxlan_fdb
*fdb
,
435 u32 portid
, u32 seq
, int type
, unsigned int flags
,
436 const struct vxlan_rdst
*rdst
)
438 unsigned long now
= jiffies
;
439 struct nda_cacheinfo ci
;
440 struct nlmsghdr
*nlh
;
442 bool send_ip
, send_eth
;
444 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
448 ndm
= nlmsg_data(nlh
);
449 memset(ndm
, 0, sizeof(*ndm
));
451 send_eth
= send_ip
= true;
453 if (type
== RTM_GETNEIGH
) {
454 send_ip
= !vxlan_addr_any(&rdst
->remote_ip
);
455 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
456 ndm
->ndm_family
= send_ip
? rdst
->remote_ip
.sa
.sa_family
: AF_INET
;
458 ndm
->ndm_family
= AF_BRIDGE
;
459 ndm
->ndm_state
= fdb
->state
;
460 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
461 ndm
->ndm_flags
= fdb
->flags
;
462 ndm
->ndm_type
= RTN_UNICAST
;
464 if (!net_eq(dev_net(vxlan
->dev
), vxlan
->net
) &&
465 nla_put_s32(skb
, NDA_LINK_NETNSID
,
466 peernet2id(dev_net(vxlan
->dev
), vxlan
->net
)))
467 goto nla_put_failure
;
469 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
470 goto nla_put_failure
;
472 if (send_ip
&& vxlan_nla_put_addr(skb
, NDA_DST
, &rdst
->remote_ip
))
473 goto nla_put_failure
;
475 if (rdst
->remote_port
&& rdst
->remote_port
!= vxlan
->cfg
.dst_port
&&
476 nla_put_be16(skb
, NDA_PORT
, rdst
->remote_port
))
477 goto nla_put_failure
;
478 if (rdst
->remote_vni
!= vxlan
->default_dst
.remote_vni
&&
479 nla_put_u32(skb
, NDA_VNI
, be32_to_cpu(rdst
->remote_vni
)))
480 goto nla_put_failure
;
481 if ((vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) && fdb
->vni
&&
482 nla_put_u32(skb
, NDA_SRC_VNI
,
483 be32_to_cpu(fdb
->vni
)))
484 goto nla_put_failure
;
485 if (rdst
->remote_ifindex
&&
486 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
487 goto nla_put_failure
;
489 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
490 ci
.ndm_confirmed
= 0;
491 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
494 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
495 goto nla_put_failure
;
501 nlmsg_cancel(skb
, nlh
);
505 static inline size_t vxlan_nlmsg_size(void)
507 return NLMSG_ALIGN(sizeof(struct ndmsg
))
508 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
509 + nla_total_size(sizeof(struct in6_addr
)) /* NDA_DST */
510 + nla_total_size(sizeof(__be16
)) /* NDA_PORT */
511 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
512 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
513 + nla_total_size(sizeof(__s32
)) /* NDA_LINK_NETNSID */
514 + nla_total_size(sizeof(struct nda_cacheinfo
));
517 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*fdb
,
518 struct vxlan_rdst
*rd
, int type
)
520 struct net
*net
= dev_net(vxlan
->dev
);
524 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
528 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0, rd
);
530 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
531 WARN_ON(err
== -EMSGSIZE
);
536 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
540 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
543 static void vxlan_ip_miss(struct net_device
*dev
, union vxlan_addr
*ipa
)
545 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
546 struct vxlan_fdb f
= {
549 struct vxlan_rdst remote
= {
550 .remote_ip
= *ipa
, /* goes to NDA_DST */
551 .remote_vni
= cpu_to_be32(VXLAN_N_VID
),
554 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
557 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
559 struct vxlan_fdb f
= {
562 struct vxlan_rdst remote
= { };
564 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
566 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
569 /* Hash Ethernet address */
570 static u32
eth_hash(const unsigned char *addr
)
572 u64 value
= get_unaligned((u64
*)addr
);
574 /* only want 6 bytes */
580 return hash_64(value
, FDB_HASH_BITS
);
583 static u32
eth_vni_hash(const unsigned char *addr
, __be32 vni
)
585 /* use 1 byte of OUI and 3 bytes of NIC */
586 u32 key
= get_unaligned((u32
*)(addr
+ 2));
588 return jhash_2words(key
, vni
, vxlan_salt
) & (FDB_HASH_SIZE
- 1);
591 /* Hash chain to use given mac address */
592 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
593 const u8
*mac
, __be32 vni
)
595 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
)
596 return &vxlan
->fdb_head
[eth_vni_hash(mac
, vni
)];
598 return &vxlan
->fdb_head
[eth_hash(mac
)];
601 /* Look up Ethernet address in forwarding table */
602 static struct vxlan_fdb
*__vxlan_find_mac(struct vxlan_dev
*vxlan
,
603 const u8
*mac
, __be32 vni
)
605 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
, vni
);
608 hlist_for_each_entry_rcu(f
, head
, hlist
) {
609 if (ether_addr_equal(mac
, f
->eth_addr
)) {
610 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) {
622 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
623 const u8
*mac
, __be32 vni
)
627 f
= __vxlan_find_mac(vxlan
, mac
, vni
);
634 /* caller should hold vxlan->hash_lock */
635 static struct vxlan_rdst
*vxlan_fdb_find_rdst(struct vxlan_fdb
*f
,
636 union vxlan_addr
*ip
, __be16 port
,
637 __be32 vni
, __u32 ifindex
)
639 struct vxlan_rdst
*rd
;
641 list_for_each_entry(rd
, &f
->remotes
, list
) {
642 if (vxlan_addr_equal(&rd
->remote_ip
, ip
) &&
643 rd
->remote_port
== port
&&
644 rd
->remote_vni
== vni
&&
645 rd
->remote_ifindex
== ifindex
)
652 /* Replace destination of unicast mac */
653 static int vxlan_fdb_replace(struct vxlan_fdb
*f
,
654 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
657 struct vxlan_rdst
*rd
;
659 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
663 rd
= list_first_entry_or_null(&f
->remotes
, struct vxlan_rdst
, list
);
667 dst_cache_reset(&rd
->dst_cache
);
669 rd
->remote_port
= port
;
670 rd
->remote_vni
= vni
;
671 rd
->remote_ifindex
= ifindex
;
675 /* Add/update destinations for multicast */
676 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
677 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
678 __u32 ifindex
, struct vxlan_rdst
**rdp
)
680 struct vxlan_rdst
*rd
;
682 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
686 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
690 if (dst_cache_init(&rd
->dst_cache
, GFP_ATOMIC
)) {
696 rd
->remote_port
= port
;
697 rd
->remote_vni
= vni
;
698 rd
->remote_ifindex
= ifindex
;
700 list_add_tail_rcu(&rd
->list
, &f
->remotes
);
706 static struct vxlanhdr
*vxlan_gro_remcsum(struct sk_buff
*skb
,
708 struct vxlanhdr
*vh
, size_t hdrlen
,
710 struct gro_remcsum
*grc
,
713 size_t start
, offset
;
715 if (skb
->remcsum_offload
)
718 if (!NAPI_GRO_CB(skb
)->csum_valid
)
721 start
= vxlan_rco_start(vni_field
);
722 offset
= start
+ vxlan_rco_offset(vni_field
);
724 vh
= skb_gro_remcsum_process(skb
, (void *)vh
, off
, hdrlen
,
725 start
, offset
, grc
, nopartial
);
727 skb
->remcsum_offload
= 1;
732 static struct sk_buff
**vxlan_gro_receive(struct sock
*sk
,
733 struct sk_buff
**head
,
736 struct sk_buff
*p
, **pp
= NULL
;
737 struct vxlanhdr
*vh
, *vh2
;
738 unsigned int hlen
, off_vx
;
740 struct vxlan_sock
*vs
= rcu_dereference_sk_user_data(sk
);
742 struct gro_remcsum grc
;
744 skb_gro_remcsum_init(&grc
);
746 off_vx
= skb_gro_offset(skb
);
747 hlen
= off_vx
+ sizeof(*vh
);
748 vh
= skb_gro_header_fast(skb
, off_vx
);
749 if (skb_gro_header_hard(skb
, hlen
)) {
750 vh
= skb_gro_header_slow(skb
, hlen
, off_vx
);
755 skb_gro_postpull_rcsum(skb
, vh
, sizeof(struct vxlanhdr
));
757 flags
= vh
->vx_flags
;
759 if ((flags
& VXLAN_HF_RCO
) && (vs
->flags
& VXLAN_F_REMCSUM_RX
)) {
760 vh
= vxlan_gro_remcsum(skb
, off_vx
, vh
, sizeof(struct vxlanhdr
),
763 VXLAN_F_REMCSUM_NOPARTIAL
));
769 skb_gro_pull(skb
, sizeof(struct vxlanhdr
)); /* pull vxlan header */
771 for (p
= *head
; p
; p
= p
->next
) {
772 if (!NAPI_GRO_CB(p
)->same_flow
)
775 vh2
= (struct vxlanhdr
*)(p
->data
+ off_vx
);
776 if (vh
->vx_flags
!= vh2
->vx_flags
||
777 vh
->vx_vni
!= vh2
->vx_vni
) {
778 NAPI_GRO_CB(p
)->same_flow
= 0;
783 pp
= call_gro_receive(eth_gro_receive
, head
, skb
);
787 skb_gro_remcsum_cleanup(skb
, &grc
);
788 skb
->remcsum_offload
= 0;
789 NAPI_GRO_CB(skb
)->flush
|= flush
;
794 static int vxlan_gro_complete(struct sock
*sk
, struct sk_buff
*skb
, int nhoff
)
796 /* Sets 'skb->inner_mac_header' since we are always called with
797 * 'skb->encapsulation' set.
799 return eth_gro_complete(skb
, nhoff
+ sizeof(struct vxlanhdr
));
802 /* Add new entry to forwarding table -- assumes lock held */
803 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
804 const u8
*mac
, union vxlan_addr
*ip
,
805 __u16 state
, __u16 flags
,
806 __be16 port
, __be32 src_vni
, __be32 vni
,
807 __u32 ifindex
, __u8 ndm_flags
)
809 struct vxlan_rdst
*rd
= NULL
;
814 f
= __vxlan_find_mac(vxlan
, mac
, src_vni
);
816 if (flags
& NLM_F_EXCL
) {
817 netdev_dbg(vxlan
->dev
,
818 "lost race to create %pM\n", mac
);
821 if (f
->state
!= state
) {
823 f
->updated
= jiffies
;
826 if (f
->flags
!= ndm_flags
) {
827 f
->flags
= ndm_flags
;
828 f
->updated
= jiffies
;
831 if ((flags
& NLM_F_REPLACE
)) {
832 /* Only change unicasts */
833 if (!(is_multicast_ether_addr(f
->eth_addr
) ||
834 is_zero_ether_addr(f
->eth_addr
))) {
835 notify
|= vxlan_fdb_replace(f
, ip
, port
, vni
,
840 if ((flags
& NLM_F_APPEND
) &&
841 (is_multicast_ether_addr(f
->eth_addr
) ||
842 is_zero_ether_addr(f
->eth_addr
))) {
843 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
850 if (!(flags
& NLM_F_CREATE
))
853 if (vxlan
->cfg
.addrmax
&&
854 vxlan
->addrcnt
>= vxlan
->cfg
.addrmax
)
857 /* Disallow replace to add a multicast entry */
858 if ((flags
& NLM_F_REPLACE
) &&
859 (is_multicast_ether_addr(mac
) || is_zero_ether_addr(mac
)))
862 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
863 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
869 f
->flags
= ndm_flags
;
870 f
->updated
= f
->used
= jiffies
;
872 INIT_LIST_HEAD(&f
->remotes
);
873 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
875 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
882 hlist_add_head_rcu(&f
->hlist
,
883 vxlan_fdb_head(vxlan
, mac
, src_vni
));
888 rd
= first_remote_rtnl(f
);
889 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_NEWNEIGH
);
895 static void vxlan_fdb_free(struct rcu_head
*head
)
897 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
898 struct vxlan_rdst
*rd
, *nd
;
900 list_for_each_entry_safe(rd
, nd
, &f
->remotes
, list
) {
901 dst_cache_destroy(&rd
->dst_cache
);
907 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
909 netdev_dbg(vxlan
->dev
,
910 "delete %pM\n", f
->eth_addr
);
913 vxlan_fdb_notify(vxlan
, f
, first_remote_rtnl(f
), RTM_DELNEIGH
);
915 hlist_del_rcu(&f
->hlist
);
916 call_rcu(&f
->rcu
, vxlan_fdb_free
);
919 static void vxlan_dst_free(struct rcu_head
*head
)
921 struct vxlan_rdst
*rd
= container_of(head
, struct vxlan_rdst
, rcu
);
923 dst_cache_destroy(&rd
->dst_cache
);
927 static void vxlan_fdb_dst_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
,
928 struct vxlan_rdst
*rd
)
930 list_del_rcu(&rd
->list
);
931 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_DELNEIGH
);
932 call_rcu(&rd
->rcu
, vxlan_dst_free
);
935 static int vxlan_fdb_parse(struct nlattr
*tb
[], struct vxlan_dev
*vxlan
,
936 union vxlan_addr
*ip
, __be16
*port
, __be32
*src_vni
,
937 __be32
*vni
, u32
*ifindex
)
939 struct net
*net
= dev_net(vxlan
->dev
);
943 err
= vxlan_nla_get_addr(ip
, tb
[NDA_DST
]);
947 union vxlan_addr
*remote
= &vxlan
->default_dst
.remote_ip
;
948 if (remote
->sa
.sa_family
== AF_INET
) {
949 ip
->sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
950 ip
->sa
.sa_family
= AF_INET
;
951 #if IS_ENABLED(CONFIG_IPV6)
953 ip
->sin6
.sin6_addr
= in6addr_any
;
954 ip
->sa
.sa_family
= AF_INET6
;
960 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
962 *port
= nla_get_be16(tb
[NDA_PORT
]);
964 *port
= vxlan
->cfg
.dst_port
;
968 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
970 *vni
= cpu_to_be32(nla_get_u32(tb
[NDA_VNI
]));
972 *vni
= vxlan
->default_dst
.remote_vni
;
975 if (tb
[NDA_SRC_VNI
]) {
976 if (nla_len(tb
[NDA_SRC_VNI
]) != sizeof(u32
))
978 *src_vni
= cpu_to_be32(nla_get_u32(tb
[NDA_SRC_VNI
]));
980 *src_vni
= vxlan
->default_dst
.remote_vni
;
983 if (tb
[NDA_IFINDEX
]) {
984 struct net_device
*tdev
;
986 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
988 *ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
989 tdev
= __dev_get_by_index(net
, *ifindex
);
991 return -EADDRNOTAVAIL
;
999 /* Add static entry (via netlink) */
1000 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
1001 struct net_device
*dev
,
1002 const unsigned char *addr
, u16 vid
, u16 flags
)
1004 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1005 /* struct net *net = dev_net(vxlan->dev); */
1006 union vxlan_addr ip
;
1008 __be32 src_vni
, vni
;
1012 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
1013 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1018 if (tb
[NDA_DST
] == NULL
)
1021 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &src_vni
, &vni
, &ifindex
);
1025 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
!= ip
.sa
.sa_family
)
1026 return -EAFNOSUPPORT
;
1028 spin_lock_bh(&vxlan
->hash_lock
);
1029 err
= vxlan_fdb_create(vxlan
, addr
, &ip
, ndm
->ndm_state
, flags
,
1030 port
, src_vni
, vni
, ifindex
, ndm
->ndm_flags
);
1031 spin_unlock_bh(&vxlan
->hash_lock
);
1036 static int __vxlan_fdb_delete(struct vxlan_dev
*vxlan
,
1037 const unsigned char *addr
, union vxlan_addr ip
,
1038 __be16 port
, __be32 src_vni
, u32 vni
, u32 ifindex
,
1041 struct vxlan_fdb
*f
;
1042 struct vxlan_rdst
*rd
= NULL
;
1045 f
= vxlan_find_mac(vxlan
, addr
, src_vni
);
1049 if (!vxlan_addr_any(&ip
)) {
1050 rd
= vxlan_fdb_find_rdst(f
, &ip
, port
, vni
, ifindex
);
1055 /* remove a destination if it's not the only one on the list,
1056 * otherwise destroy the fdb entry
1058 if (rd
&& !list_is_singular(&f
->remotes
)) {
1059 vxlan_fdb_dst_destroy(vxlan
, f
, rd
);
1063 vxlan_fdb_destroy(vxlan
, f
);
1069 /* Delete entry (via netlink) */
1070 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
1071 struct net_device
*dev
,
1072 const unsigned char *addr
, u16 vid
)
1074 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1075 union vxlan_addr ip
;
1076 __be32 src_vni
, vni
;
1081 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &src_vni
, &vni
, &ifindex
);
1085 spin_lock_bh(&vxlan
->hash_lock
);
1086 err
= __vxlan_fdb_delete(vxlan
, addr
, ip
, port
, src_vni
, vni
, ifindex
,
1088 spin_unlock_bh(&vxlan
->hash_lock
);
1093 /* Dump forwarding table */
1094 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
1095 struct net_device
*dev
,
1096 struct net_device
*filter_dev
, int *idx
)
1098 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1102 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1103 struct vxlan_fdb
*f
;
1105 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
1106 struct vxlan_rdst
*rd
;
1108 list_for_each_entry_rcu(rd
, &f
->remotes
, list
) {
1109 if (*idx
< cb
->args
[2])
1112 err
= vxlan_fdb_info(skb
, vxlan
, f
,
1113 NETLINK_CB(cb
->skb
).portid
,
1128 /* Watch incoming packets to learn mapping between Ethernet address
1129 * and Tunnel endpoint.
1130 * Return true if packet is bogus and should be dropped.
1132 static bool vxlan_snoop(struct net_device
*dev
,
1133 union vxlan_addr
*src_ip
, const u8
*src_mac
,
1134 u32 src_ifindex
, __be32 vni
)
1136 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1137 struct vxlan_fdb
*f
;
1140 #if IS_ENABLED(CONFIG_IPV6)
1141 if (src_ip
->sa
.sa_family
== AF_INET6
&&
1142 (ipv6_addr_type(&src_ip
->sin6
.sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
1143 ifindex
= src_ifindex
;
1146 f
= vxlan_find_mac(vxlan
, src_mac
, vni
);
1148 struct vxlan_rdst
*rdst
= first_remote_rcu(f
);
1150 if (likely(vxlan_addr_equal(&rdst
->remote_ip
, src_ip
) &&
1151 rdst
->remote_ifindex
== ifindex
))
1154 /* Don't migrate static entries, drop packets */
1155 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
1158 if (net_ratelimit())
1160 "%pM migrated from %pIS to %pIS\n",
1161 src_mac
, &rdst
->remote_ip
.sa
, &src_ip
->sa
);
1163 rdst
->remote_ip
= *src_ip
;
1164 f
->updated
= jiffies
;
1165 vxlan_fdb_notify(vxlan
, f
, rdst
, RTM_NEWNEIGH
);
1167 /* learned new entry */
1168 spin_lock(&vxlan
->hash_lock
);
1170 /* close off race between vxlan_flush and incoming packets */
1171 if (netif_running(dev
))
1172 vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
1174 NLM_F_EXCL
|NLM_F_CREATE
,
1175 vxlan
->cfg
.dst_port
,
1177 vxlan
->default_dst
.remote_vni
,
1179 spin_unlock(&vxlan
->hash_lock
);
1185 /* See if multicast group is already in use by other ID */
1186 static bool vxlan_group_used(struct vxlan_net
*vn
, struct vxlan_dev
*dev
)
1188 struct vxlan_dev
*vxlan
;
1189 struct vxlan_sock
*sock4
;
1190 #if IS_ENABLED(CONFIG_IPV6)
1191 struct vxlan_sock
*sock6
;
1193 unsigned short family
= dev
->default_dst
.remote_ip
.sa
.sa_family
;
1195 sock4
= rtnl_dereference(dev
->vn4_sock
);
1197 /* The vxlan_sock is only used by dev, leaving group has
1198 * no effect on other vxlan devices.
1200 if (family
== AF_INET
&& sock4
&& refcount_read(&sock4
->refcnt
) == 1)
1202 #if IS_ENABLED(CONFIG_IPV6)
1203 sock6
= rtnl_dereference(dev
->vn6_sock
);
1204 if (family
== AF_INET6
&& sock6
&& refcount_read(&sock6
->refcnt
) == 1)
1208 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
) {
1209 if (!netif_running(vxlan
->dev
) || vxlan
== dev
)
1212 if (family
== AF_INET
&&
1213 rtnl_dereference(vxlan
->vn4_sock
) != sock4
)
1215 #if IS_ENABLED(CONFIG_IPV6)
1216 if (family
== AF_INET6
&&
1217 rtnl_dereference(vxlan
->vn6_sock
) != sock6
)
1221 if (!vxlan_addr_equal(&vxlan
->default_dst
.remote_ip
,
1222 &dev
->default_dst
.remote_ip
))
1225 if (vxlan
->default_dst
.remote_ifindex
!=
1226 dev
->default_dst
.remote_ifindex
)
1235 static bool __vxlan_sock_release_prep(struct vxlan_sock
*vs
)
1237 struct vxlan_net
*vn
;
1241 if (!refcount_dec_and_test(&vs
->refcnt
))
1244 vn
= net_generic(sock_net(vs
->sock
->sk
), vxlan_net_id
);
1245 spin_lock(&vn
->sock_lock
);
1246 hlist_del_rcu(&vs
->hlist
);
1247 udp_tunnel_notify_del_rx_port(vs
->sock
,
1248 (vs
->flags
& VXLAN_F_GPE
) ?
1249 UDP_TUNNEL_TYPE_VXLAN_GPE
:
1250 UDP_TUNNEL_TYPE_VXLAN
);
1251 spin_unlock(&vn
->sock_lock
);
1256 static void vxlan_sock_release(struct vxlan_dev
*vxlan
)
1258 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1259 #if IS_ENABLED(CONFIG_IPV6)
1260 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1262 RCU_INIT_POINTER(vxlan
->vn6_sock
, NULL
);
1265 RCU_INIT_POINTER(vxlan
->vn4_sock
, NULL
);
1268 vxlan_vs_del_dev(vxlan
);
1270 if (__vxlan_sock_release_prep(sock4
)) {
1271 udp_tunnel_sock_release(sock4
->sock
);
1275 #if IS_ENABLED(CONFIG_IPV6)
1276 if (__vxlan_sock_release_prep(sock6
)) {
1277 udp_tunnel_sock_release(sock6
->sock
);
1283 /* Update multicast group membership when first VNI on
1284 * multicast address is brought up
1286 static int vxlan_igmp_join(struct vxlan_dev
*vxlan
)
1289 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1290 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1293 if (ip
->sa
.sa_family
== AF_INET
) {
1294 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1295 struct ip_mreqn mreq
= {
1296 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1297 .imr_ifindex
= ifindex
,
1300 sk
= sock4
->sock
->sk
;
1302 ret
= ip_mc_join_group(sk
, &mreq
);
1304 #if IS_ENABLED(CONFIG_IPV6)
1306 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1308 sk
= sock6
->sock
->sk
;
1310 ret
= ipv6_stub
->ipv6_sock_mc_join(sk
, ifindex
,
1311 &ip
->sin6
.sin6_addr
);
1319 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1320 static int vxlan_igmp_leave(struct vxlan_dev
*vxlan
)
1323 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1324 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1327 if (ip
->sa
.sa_family
== AF_INET
) {
1328 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1329 struct ip_mreqn mreq
= {
1330 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1331 .imr_ifindex
= ifindex
,
1334 sk
= sock4
->sock
->sk
;
1336 ret
= ip_mc_leave_group(sk
, &mreq
);
1338 #if IS_ENABLED(CONFIG_IPV6)
1340 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1342 sk
= sock6
->sock
->sk
;
1344 ret
= ipv6_stub
->ipv6_sock_mc_drop(sk
, ifindex
,
1345 &ip
->sin6
.sin6_addr
);
1353 static bool vxlan_remcsum(struct vxlanhdr
*unparsed
,
1354 struct sk_buff
*skb
, u32 vxflags
)
1356 size_t start
, offset
;
1358 if (!(unparsed
->vx_flags
& VXLAN_HF_RCO
) || skb
->remcsum_offload
)
1361 start
= vxlan_rco_start(unparsed
->vx_vni
);
1362 offset
= start
+ vxlan_rco_offset(unparsed
->vx_vni
);
1364 if (!pskb_may_pull(skb
, offset
+ sizeof(u16
)))
1367 skb_remcsum_process(skb
, (void *)(vxlan_hdr(skb
) + 1), start
, offset
,
1368 !!(vxflags
& VXLAN_F_REMCSUM_NOPARTIAL
));
1370 unparsed
->vx_flags
&= ~VXLAN_HF_RCO
;
1371 unparsed
->vx_vni
&= VXLAN_VNI_MASK
;
1375 static void vxlan_parse_gbp_hdr(struct vxlanhdr
*unparsed
,
1376 struct sk_buff
*skb
, u32 vxflags
,
1377 struct vxlan_metadata
*md
)
1379 struct vxlanhdr_gbp
*gbp
= (struct vxlanhdr_gbp
*)unparsed
;
1380 struct metadata_dst
*tun_dst
;
1382 if (!(unparsed
->vx_flags
& VXLAN_HF_GBP
))
1385 md
->gbp
= ntohs(gbp
->policy_id
);
1387 tun_dst
= (struct metadata_dst
*)skb_dst(skb
);
1389 tun_dst
->u
.tun_info
.key
.tun_flags
|= TUNNEL_VXLAN_OPT
;
1390 tun_dst
->u
.tun_info
.options_len
= sizeof(*md
);
1392 if (gbp
->dont_learn
)
1393 md
->gbp
|= VXLAN_GBP_DONT_LEARN
;
1395 if (gbp
->policy_applied
)
1396 md
->gbp
|= VXLAN_GBP_POLICY_APPLIED
;
1398 /* In flow-based mode, GBP is carried in dst_metadata */
1399 if (!(vxflags
& VXLAN_F_COLLECT_METADATA
))
1400 skb
->mark
= md
->gbp
;
1402 unparsed
->vx_flags
&= ~VXLAN_GBP_USED_BITS
;
1405 static bool vxlan_parse_gpe_hdr(struct vxlanhdr
*unparsed
,
1407 struct sk_buff
*skb
, u32 vxflags
)
1409 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)unparsed
;
1411 /* Need to have Next Protocol set for interfaces in GPE mode. */
1412 if (!gpe
->np_applied
)
1414 /* "The initial version is 0. If a receiver does not support the
1415 * version indicated it MUST drop the packet.
1417 if (gpe
->version
!= 0)
1419 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1420 * processing MUST occur." However, we don't implement OAM
1421 * processing, thus drop the packet.
1426 switch (gpe
->next_protocol
) {
1427 case VXLAN_GPE_NP_IPV4
:
1428 *protocol
= htons(ETH_P_IP
);
1430 case VXLAN_GPE_NP_IPV6
:
1431 *protocol
= htons(ETH_P_IPV6
);
1433 case VXLAN_GPE_NP_ETHERNET
:
1434 *protocol
= htons(ETH_P_TEB
);
1440 unparsed
->vx_flags
&= ~VXLAN_GPE_USED_BITS
;
1444 static bool vxlan_set_mac(struct vxlan_dev
*vxlan
,
1445 struct vxlan_sock
*vs
,
1446 struct sk_buff
*skb
, __be32 vni
)
1448 union vxlan_addr saddr
;
1449 u32 ifindex
= skb
->dev
->ifindex
;
1451 skb_reset_mac_header(skb
);
1452 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1453 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
1455 /* Ignore packet loops (and multicast echo) */
1456 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1459 /* Get address from the outer IP header */
1460 if (vxlan_get_sk_family(vs
) == AF_INET
) {
1461 saddr
.sin
.sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
1462 saddr
.sa
.sa_family
= AF_INET
;
1463 #if IS_ENABLED(CONFIG_IPV6)
1465 saddr
.sin6
.sin6_addr
= ipv6_hdr(skb
)->saddr
;
1466 saddr
.sa
.sa_family
= AF_INET6
;
1470 if ((vxlan
->cfg
.flags
& VXLAN_F_LEARN
) &&
1471 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
, ifindex
, vni
))
1477 static bool vxlan_ecn_decapsulate(struct vxlan_sock
*vs
, void *oiph
,
1478 struct sk_buff
*skb
)
1482 if (vxlan_get_sk_family(vs
) == AF_INET
)
1483 err
= IP_ECN_decapsulate(oiph
, skb
);
1484 #if IS_ENABLED(CONFIG_IPV6)
1486 err
= IP6_ECN_decapsulate(oiph
, skb
);
1489 if (unlikely(err
) && log_ecn_error
) {
1490 if (vxlan_get_sk_family(vs
) == AF_INET
)
1491 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1492 &((struct iphdr
*)oiph
)->saddr
,
1493 ((struct iphdr
*)oiph
)->tos
);
1495 net_info_ratelimited("non-ECT from %pI6\n",
1496 &((struct ipv6hdr
*)oiph
)->saddr
);
1501 /* Callback from net/ipv4/udp.c to receive packets */
1502 static int vxlan_rcv(struct sock
*sk
, struct sk_buff
*skb
)
1504 struct pcpu_sw_netstats
*stats
;
1505 struct vxlan_dev
*vxlan
;
1506 struct vxlan_sock
*vs
;
1507 struct vxlanhdr unparsed
;
1508 struct vxlan_metadata _md
;
1509 struct vxlan_metadata
*md
= &_md
;
1510 __be16 protocol
= htons(ETH_P_TEB
);
1511 bool raw_proto
= false;
1515 /* Need UDP and VXLAN header to be present */
1516 if (!pskb_may_pull(skb
, VXLAN_HLEN
))
1519 unparsed
= *vxlan_hdr(skb
);
1520 /* VNI flag always required to be set */
1521 if (!(unparsed
.vx_flags
& VXLAN_HF_VNI
)) {
1522 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
1523 ntohl(vxlan_hdr(skb
)->vx_flags
),
1524 ntohl(vxlan_hdr(skb
)->vx_vni
));
1525 /* Return non vxlan pkt */
1528 unparsed
.vx_flags
&= ~VXLAN_HF_VNI
;
1529 unparsed
.vx_vni
&= ~VXLAN_VNI_MASK
;
1531 vs
= rcu_dereference_sk_user_data(sk
);
1535 vni
= vxlan_vni(vxlan_hdr(skb
)->vx_vni
);
1537 vxlan
= vxlan_vs_find_vni(vs
, skb
->dev
->ifindex
, vni
);
1541 /* For backwards compatibility, only allow reserved fields to be
1542 * used by VXLAN extensions if explicitly requested.
1544 if (vs
->flags
& VXLAN_F_GPE
) {
1545 if (!vxlan_parse_gpe_hdr(&unparsed
, &protocol
, skb
, vs
->flags
))
1550 if (__iptunnel_pull_header(skb
, VXLAN_HLEN
, protocol
, raw_proto
,
1551 !net_eq(vxlan
->net
, dev_net(vxlan
->dev
))))
1554 if (vxlan_collect_metadata(vs
)) {
1555 struct metadata_dst
*tun_dst
;
1557 tun_dst
= udp_tun_rx_dst(skb
, vxlan_get_sk_family(vs
), TUNNEL_KEY
,
1558 key32_to_tunnel_id(vni
), sizeof(*md
));
1563 md
= ip_tunnel_info_opts(&tun_dst
->u
.tun_info
);
1565 skb_dst_set(skb
, (struct dst_entry
*)tun_dst
);
1567 memset(md
, 0, sizeof(*md
));
1570 if (vs
->flags
& VXLAN_F_REMCSUM_RX
)
1571 if (!vxlan_remcsum(&unparsed
, skb
, vs
->flags
))
1573 if (vs
->flags
& VXLAN_F_GBP
)
1574 vxlan_parse_gbp_hdr(&unparsed
, skb
, vs
->flags
, md
);
1575 /* Note that GBP and GPE can never be active together. This is
1576 * ensured in vxlan_dev_configure.
1579 if (unparsed
.vx_flags
|| unparsed
.vx_vni
) {
1580 /* If there are any unprocessed flags remaining treat
1581 * this as a malformed packet. This behavior diverges from
1582 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1583 * in reserved fields are to be ignored. The approach here
1584 * maintains compatibility with previous stack code, and also
1585 * is more robust and provides a little more security in
1586 * adding extensions to VXLAN.
1592 if (!vxlan_set_mac(vxlan
, vs
, skb
, vni
))
1595 skb_reset_mac_header(skb
);
1596 skb
->dev
= vxlan
->dev
;
1597 skb
->pkt_type
= PACKET_HOST
;
1600 oiph
= skb_network_header(skb
);
1601 skb_reset_network_header(skb
);
1603 if (!vxlan_ecn_decapsulate(vs
, oiph
, skb
)) {
1604 ++vxlan
->dev
->stats
.rx_frame_errors
;
1605 ++vxlan
->dev
->stats
.rx_errors
;
1609 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1610 u64_stats_update_begin(&stats
->syncp
);
1611 stats
->rx_packets
++;
1612 stats
->rx_bytes
+= skb
->len
;
1613 u64_stats_update_end(&stats
->syncp
);
1615 gro_cells_receive(&vxlan
->gro_cells
, skb
);
1619 /* Consume bad packet */
1624 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
, __be32 vni
)
1626 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1627 struct arphdr
*parp
;
1630 struct neighbour
*n
;
1632 if (dev
->flags
& IFF_NOARP
)
1635 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1636 dev
->stats
.tx_dropped
++;
1639 parp
= arp_hdr(skb
);
1641 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1642 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1643 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1644 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1645 parp
->ar_hln
!= dev
->addr_len
||
1648 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1650 arpptr
+= dev
->addr_len
; /* sha */
1651 memcpy(&sip
, arpptr
, sizeof(sip
));
1652 arpptr
+= sizeof(sip
);
1653 arpptr
+= dev
->addr_len
; /* tha */
1654 memcpy(&tip
, arpptr
, sizeof(tip
));
1656 if (ipv4_is_loopback(tip
) ||
1657 ipv4_is_multicast(tip
))
1660 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1663 struct vxlan_fdb
*f
;
1664 struct sk_buff
*reply
;
1666 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1671 f
= vxlan_find_mac(vxlan
, n
->ha
, vni
);
1672 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1673 /* bridge-local neighbor */
1678 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1686 skb_reset_mac_header(reply
);
1687 __skb_pull(reply
, skb_network_offset(reply
));
1688 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1689 reply
->pkt_type
= PACKET_HOST
;
1691 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1692 dev
->stats
.rx_dropped
++;
1693 } else if (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
) {
1694 union vxlan_addr ipa
= {
1695 .sin
.sin_addr
.s_addr
= tip
,
1696 .sin
.sin_family
= AF_INET
,
1699 vxlan_ip_miss(dev
, &ipa
);
1703 return NETDEV_TX_OK
;
1706 #if IS_ENABLED(CONFIG_IPV6)
1707 static struct sk_buff
*vxlan_na_create(struct sk_buff
*request
,
1708 struct neighbour
*n
, bool isrouter
)
1710 struct net_device
*dev
= request
->dev
;
1711 struct sk_buff
*reply
;
1712 struct nd_msg
*ns
, *na
;
1713 struct ipv6hdr
*pip6
;
1715 int na_olen
= 8; /* opt hdr + ETH_ALEN for target */
1719 if (dev
== NULL
|| !pskb_may_pull(request
, request
->len
))
1722 len
= LL_RESERVED_SPACE(dev
) + sizeof(struct ipv6hdr
) +
1723 sizeof(*na
) + na_olen
+ dev
->needed_tailroom
;
1724 reply
= alloc_skb(len
, GFP_ATOMIC
);
1728 reply
->protocol
= htons(ETH_P_IPV6
);
1730 skb_reserve(reply
, LL_RESERVED_SPACE(request
->dev
));
1731 skb_push(reply
, sizeof(struct ethhdr
));
1732 skb_reset_mac_header(reply
);
1734 ns
= (struct nd_msg
*)(ipv6_hdr(request
) + 1);
1736 daddr
= eth_hdr(request
)->h_source
;
1737 ns_olen
= request
->len
- skb_network_offset(request
) -
1738 sizeof(struct ipv6hdr
) - sizeof(*ns
);
1739 for (i
= 0; i
< ns_olen
-1; i
+= (ns
->opt
[i
+1]<<3)) {
1740 if (ns
->opt
[i
] == ND_OPT_SOURCE_LL_ADDR
) {
1741 daddr
= ns
->opt
+ i
+ sizeof(struct nd_opt_hdr
);
1746 /* Ethernet header */
1747 ether_addr_copy(eth_hdr(reply
)->h_dest
, daddr
);
1748 ether_addr_copy(eth_hdr(reply
)->h_source
, n
->ha
);
1749 eth_hdr(reply
)->h_proto
= htons(ETH_P_IPV6
);
1750 reply
->protocol
= htons(ETH_P_IPV6
);
1752 skb_pull(reply
, sizeof(struct ethhdr
));
1753 skb_reset_network_header(reply
);
1754 skb_put(reply
, sizeof(struct ipv6hdr
));
1758 pip6
= ipv6_hdr(reply
);
1759 memset(pip6
, 0, sizeof(struct ipv6hdr
));
1761 pip6
->priority
= ipv6_hdr(request
)->priority
;
1762 pip6
->nexthdr
= IPPROTO_ICMPV6
;
1763 pip6
->hop_limit
= 255;
1764 pip6
->daddr
= ipv6_hdr(request
)->saddr
;
1765 pip6
->saddr
= *(struct in6_addr
*)n
->primary_key
;
1767 skb_pull(reply
, sizeof(struct ipv6hdr
));
1768 skb_reset_transport_header(reply
);
1770 /* Neighbor Advertisement */
1771 na
= skb_put_zero(reply
, sizeof(*na
) + na_olen
);
1772 na
->icmph
.icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
1773 na
->icmph
.icmp6_router
= isrouter
;
1774 na
->icmph
.icmp6_override
= 1;
1775 na
->icmph
.icmp6_solicited
= 1;
1776 na
->target
= ns
->target
;
1777 ether_addr_copy(&na
->opt
[2], n
->ha
);
1778 na
->opt
[0] = ND_OPT_TARGET_LL_ADDR
;
1779 na
->opt
[1] = na_olen
>> 3;
1781 na
->icmph
.icmp6_cksum
= csum_ipv6_magic(&pip6
->saddr
,
1782 &pip6
->daddr
, sizeof(*na
)+na_olen
, IPPROTO_ICMPV6
,
1783 csum_partial(na
, sizeof(*na
)+na_olen
, 0));
1785 pip6
->payload_len
= htons(sizeof(*na
)+na_olen
);
1787 skb_push(reply
, sizeof(struct ipv6hdr
));
1789 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1794 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
, __be32 vni
)
1796 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1798 const struct ipv6hdr
*iphdr
;
1799 const struct in6_addr
*daddr
;
1800 struct neighbour
*n
;
1801 struct inet6_dev
*in6_dev
;
1803 in6_dev
= __in6_dev_get(dev
);
1807 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
) + sizeof(struct nd_msg
)))
1810 iphdr
= ipv6_hdr(skb
);
1811 daddr
= &iphdr
->daddr
;
1813 msg
= (struct nd_msg
*)(iphdr
+ 1);
1814 if (msg
->icmph
.icmp6_code
!= 0 ||
1815 msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
1818 if (ipv6_addr_loopback(daddr
) ||
1819 ipv6_addr_is_multicast(&msg
->target
))
1822 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1825 struct vxlan_fdb
*f
;
1826 struct sk_buff
*reply
;
1828 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1833 f
= vxlan_find_mac(vxlan
, n
->ha
, vni
);
1834 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1835 /* bridge-local neighbor */
1840 reply
= vxlan_na_create(skb
, n
,
1841 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1848 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1849 dev
->stats
.rx_dropped
++;
1851 } else if (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
) {
1852 union vxlan_addr ipa
= {
1853 .sin6
.sin6_addr
= msg
->target
,
1854 .sin6
.sin6_family
= AF_INET6
,
1857 vxlan_ip_miss(dev
, &ipa
);
1862 return NETDEV_TX_OK
;
1866 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1868 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1869 struct neighbour
*n
;
1871 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1875 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1880 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1883 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1884 if (!n
&& (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) {
1885 union vxlan_addr ipa
= {
1886 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1887 .sin
.sin_family
= AF_INET
,
1890 vxlan_ip_miss(dev
, &ipa
);
1896 #if IS_ENABLED(CONFIG_IPV6)
1899 struct ipv6hdr
*pip6
;
1901 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1903 pip6
= ipv6_hdr(skb
);
1904 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1905 if (!n
&& (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) {
1906 union vxlan_addr ipa
= {
1907 .sin6
.sin6_addr
= pip6
->daddr
,
1908 .sin6
.sin6_family
= AF_INET6
,
1911 vxlan_ip_miss(dev
, &ipa
);
1925 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1927 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1929 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1938 static void vxlan_build_gbp_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1939 struct vxlan_metadata
*md
)
1941 struct vxlanhdr_gbp
*gbp
;
1946 gbp
= (struct vxlanhdr_gbp
*)vxh
;
1947 vxh
->vx_flags
|= VXLAN_HF_GBP
;
1949 if (md
->gbp
& VXLAN_GBP_DONT_LEARN
)
1950 gbp
->dont_learn
= 1;
1952 if (md
->gbp
& VXLAN_GBP_POLICY_APPLIED
)
1953 gbp
->policy_applied
= 1;
1955 gbp
->policy_id
= htons(md
->gbp
& VXLAN_GBP_ID_MASK
);
1958 static int vxlan_build_gpe_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1961 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)vxh
;
1963 gpe
->np_applied
= 1;
1966 case htons(ETH_P_IP
):
1967 gpe
->next_protocol
= VXLAN_GPE_NP_IPV4
;
1969 case htons(ETH_P_IPV6
):
1970 gpe
->next_protocol
= VXLAN_GPE_NP_IPV6
;
1972 case htons(ETH_P_TEB
):
1973 gpe
->next_protocol
= VXLAN_GPE_NP_ETHERNET
;
1976 return -EPFNOSUPPORT
;
1979 static int vxlan_build_skb(struct sk_buff
*skb
, struct dst_entry
*dst
,
1980 int iphdr_len
, __be32 vni
,
1981 struct vxlan_metadata
*md
, u32 vxflags
,
1984 struct vxlanhdr
*vxh
;
1987 int type
= udp_sum
? SKB_GSO_UDP_TUNNEL_CSUM
: SKB_GSO_UDP_TUNNEL
;
1988 __be16 inner_protocol
= htons(ETH_P_TEB
);
1990 if ((vxflags
& VXLAN_F_REMCSUM_TX
) &&
1991 skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1992 int csum_start
= skb_checksum_start_offset(skb
);
1994 if (csum_start
<= VXLAN_MAX_REMCSUM_START
&&
1995 !(csum_start
& VXLAN_RCO_SHIFT_MASK
) &&
1996 (skb
->csum_offset
== offsetof(struct udphdr
, check
) ||
1997 skb
->csum_offset
== offsetof(struct tcphdr
, check
)))
1998 type
|= SKB_GSO_TUNNEL_REMCSUM
;
2001 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
2002 + VXLAN_HLEN
+ iphdr_len
;
2004 /* Need space for new headers (invalidates iph ptr) */
2005 err
= skb_cow_head(skb
, min_headroom
);
2009 err
= iptunnel_handle_offloads(skb
, type
);
2013 vxh
= __skb_push(skb
, sizeof(*vxh
));
2014 vxh
->vx_flags
= VXLAN_HF_VNI
;
2015 vxh
->vx_vni
= vxlan_vni_field(vni
);
2017 if (type
& SKB_GSO_TUNNEL_REMCSUM
) {
2020 start
= skb_checksum_start_offset(skb
) - sizeof(struct vxlanhdr
);
2021 vxh
->vx_vni
|= vxlan_compute_rco(start
, skb
->csum_offset
);
2022 vxh
->vx_flags
|= VXLAN_HF_RCO
;
2024 if (!skb_is_gso(skb
)) {
2025 skb
->ip_summed
= CHECKSUM_NONE
;
2026 skb
->encapsulation
= 0;
2030 if (vxflags
& VXLAN_F_GBP
)
2031 vxlan_build_gbp_hdr(vxh
, vxflags
, md
);
2032 if (vxflags
& VXLAN_F_GPE
) {
2033 err
= vxlan_build_gpe_hdr(vxh
, vxflags
, skb
->protocol
);
2036 inner_protocol
= skb
->protocol
;
2039 skb_set_inner_protocol(skb
, inner_protocol
);
2043 static struct rtable
*vxlan_get_route(struct vxlan_dev
*vxlan
, struct net_device
*dev
,
2044 struct vxlan_sock
*sock4
,
2045 struct sk_buff
*skb
, int oif
, u8 tos
,
2046 __be32 daddr
, __be32
*saddr
, __be16 dport
, __be16 sport
,
2047 struct dst_cache
*dst_cache
,
2048 const struct ip_tunnel_info
*info
)
2050 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
2051 struct rtable
*rt
= NULL
;
2055 return ERR_PTR(-EIO
);
2060 rt
= dst_cache_get_ip4(dst_cache
, saddr
);
2065 memset(&fl4
, 0, sizeof(fl4
));
2066 fl4
.flowi4_oif
= oif
;
2067 fl4
.flowi4_tos
= RT_TOS(tos
);
2068 fl4
.flowi4_mark
= skb
->mark
;
2069 fl4
.flowi4_proto
= IPPROTO_UDP
;
2072 fl4
.fl4_dport
= dport
;
2073 fl4
.fl4_sport
= sport
;
2075 rt
= ip_route_output_key(vxlan
->net
, &fl4
);
2076 if (likely(!IS_ERR(rt
))) {
2077 if (rt
->dst
.dev
== dev
) {
2078 netdev_dbg(dev
, "circular route to %pI4\n", &daddr
);
2080 return ERR_PTR(-ELOOP
);
2085 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
.saddr
);
2087 netdev_dbg(dev
, "no route to %pI4\n", &daddr
);
2088 return ERR_PTR(-ENETUNREACH
);
2093 #if IS_ENABLED(CONFIG_IPV6)
2094 static struct dst_entry
*vxlan6_get_route(struct vxlan_dev
*vxlan
,
2095 struct net_device
*dev
,
2096 struct vxlan_sock
*sock6
,
2097 struct sk_buff
*skb
, int oif
, u8 tos
,
2099 const struct in6_addr
*daddr
,
2100 struct in6_addr
*saddr
,
2101 __be16 dport
, __be16 sport
,
2102 struct dst_cache
*dst_cache
,
2103 const struct ip_tunnel_info
*info
)
2105 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
2106 struct dst_entry
*ndst
;
2111 return ERR_PTR(-EIO
);
2116 ndst
= dst_cache_get_ip6(dst_cache
, saddr
);
2121 memset(&fl6
, 0, sizeof(fl6
));
2122 fl6
.flowi6_oif
= oif
;
2125 fl6
.flowlabel
= ip6_make_flowinfo(RT_TOS(tos
), label
);
2126 fl6
.flowi6_mark
= skb
->mark
;
2127 fl6
.flowi6_proto
= IPPROTO_UDP
;
2128 fl6
.fl6_dport
= dport
;
2129 fl6
.fl6_sport
= sport
;
2131 err
= ipv6_stub
->ipv6_dst_lookup(vxlan
->net
,
2134 if (unlikely(err
< 0)) {
2135 netdev_dbg(dev
, "no route to %pI6\n", daddr
);
2136 return ERR_PTR(-ENETUNREACH
);
2139 if (unlikely(ndst
->dev
== dev
)) {
2140 netdev_dbg(dev
, "circular route to %pI6\n", daddr
);
2142 return ERR_PTR(-ELOOP
);
2147 dst_cache_set_ip6(dst_cache
, ndst
, saddr
);
2152 /* Bypass encapsulation if the destination is local */
2153 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
2154 struct vxlan_dev
*dst_vxlan
, __be32 vni
)
2156 struct pcpu_sw_netstats
*tx_stats
, *rx_stats
;
2157 union vxlan_addr loopback
;
2158 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
2159 struct net_device
*dev
= skb
->dev
;
2162 tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
2163 rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
2164 skb
->pkt_type
= PACKET_HOST
;
2165 skb
->encapsulation
= 0;
2166 skb
->dev
= dst_vxlan
->dev
;
2167 __skb_pull(skb
, skb_network_offset(skb
));
2169 if (remote_ip
->sa
.sa_family
== AF_INET
) {
2170 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2171 loopback
.sa
.sa_family
= AF_INET
;
2172 #if IS_ENABLED(CONFIG_IPV6)
2174 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
2175 loopback
.sa
.sa_family
= AF_INET6
;
2179 if (dst_vxlan
->cfg
.flags
& VXLAN_F_LEARN
)
2180 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
, 0,
2183 u64_stats_update_begin(&tx_stats
->syncp
);
2184 tx_stats
->tx_packets
++;
2185 tx_stats
->tx_bytes
+= len
;
2186 u64_stats_update_end(&tx_stats
->syncp
);
2188 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
2189 u64_stats_update_begin(&rx_stats
->syncp
);
2190 rx_stats
->rx_packets
++;
2191 rx_stats
->rx_bytes
+= len
;
2192 u64_stats_update_end(&rx_stats
->syncp
);
2194 dev
->stats
.rx_dropped
++;
2198 static int encap_bypass_if_local(struct sk_buff
*skb
, struct net_device
*dev
,
2199 struct vxlan_dev
*vxlan
,
2200 union vxlan_addr
*daddr
,
2201 __be16 dst_port
, int dst_ifindex
, __be32 vni
,
2202 struct dst_entry
*dst
,
2205 #if IS_ENABLED(CONFIG_IPV6)
2206 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2207 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2208 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2210 BUILD_BUG_ON(RTCF_LOCAL
!= RTF_LOCAL
);
2212 /* Bypass encapsulation if the destination is local */
2213 if (rt_flags
& RTCF_LOCAL
&&
2214 !(rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
2215 struct vxlan_dev
*dst_vxlan
;
2218 dst_vxlan
= vxlan_find_vni(vxlan
->net
, dst_ifindex
, vni
,
2219 daddr
->sa
.sa_family
, dst_port
,
2222 dev
->stats
.tx_errors
++;
2227 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
, vni
);
2234 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
2235 __be32 default_vni
, struct vxlan_rdst
*rdst
,
2238 struct dst_cache
*dst_cache
;
2239 struct ip_tunnel_info
*info
;
2240 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2241 const struct iphdr
*old_iph
= ip_hdr(skb
);
2242 union vxlan_addr
*dst
;
2243 union vxlan_addr remote_ip
, local_ip
;
2244 struct vxlan_metadata _md
;
2245 struct vxlan_metadata
*md
= &_md
;
2246 __be16 src_port
= 0, dst_port
;
2247 struct dst_entry
*ndst
= NULL
;
2252 u32 flags
= vxlan
->cfg
.flags
;
2253 bool udp_sum
= false;
2254 bool xnet
= !net_eq(vxlan
->net
, dev_net(vxlan
->dev
));
2256 info
= skb_tunnel_info(skb
);
2259 dst
= &rdst
->remote_ip
;
2260 if (vxlan_addr_any(dst
)) {
2262 /* short-circuited back to local bridge */
2263 vxlan_encap_bypass(skb
, vxlan
, vxlan
, default_vni
);
2269 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->cfg
.dst_port
;
2270 vni
= (rdst
->remote_vni
) ? : default_vni
;
2271 ifindex
= rdst
->remote_ifindex
;
2272 local_ip
= vxlan
->cfg
.saddr
;
2273 dst_cache
= &rdst
->dst_cache
;
2274 md
->gbp
= skb
->mark
;
2275 ttl
= vxlan
->cfg
.ttl
;
2276 if (!ttl
&& vxlan_addr_multicast(dst
))
2279 tos
= vxlan
->cfg
.tos
;
2281 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
2283 if (dst
->sa
.sa_family
== AF_INET
)
2284 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM_TX
);
2286 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
);
2287 label
= vxlan
->cfg
.label
;
2290 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2294 remote_ip
.sa
.sa_family
= ip_tunnel_info_af(info
);
2295 if (remote_ip
.sa
.sa_family
== AF_INET
) {
2296 remote_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.dst
;
2297 local_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.src
;
2299 remote_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.dst
;
2300 local_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.src
;
2303 dst_port
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2304 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2306 dst_cache
= &info
->dst_cache
;
2307 if (info
->options_len
)
2308 md
= ip_tunnel_info_opts(info
);
2309 ttl
= info
->key
.ttl
;
2310 tos
= info
->key
.tos
;
2311 label
= info
->key
.label
;
2312 udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
2314 src_port
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2315 vxlan
->cfg
.port_max
, true);
2318 if (dst
->sa
.sa_family
== AF_INET
) {
2319 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2323 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
, ifindex
, tos
,
2324 dst
->sin
.sin_addr
.s_addr
,
2325 &local_ip
.sin
.sin_addr
.s_addr
,
2333 if (fan_has_map(&vxlan
->fan
) && rt
->rt_flags
& RTCF_LOCAL
) {
2334 netdev_dbg(dev
, "discard fan to localhost %pI4\n",
2335 &dst
->sin
.sin_addr
.s_addr
);
2340 /* Bypass encapsulation if the destination is local */
2342 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2343 dst_port
, ifindex
, vni
,
2344 &rt
->dst
, rt
->rt_flags
);
2347 } else if (info
->key
.tun_flags
& TUNNEL_DONT_FRAGMENT
) {
2352 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2353 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
2354 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct iphdr
),
2355 vni
, md
, flags
, udp_sum
);
2359 udp_tunnel_xmit_skb(rt
, sock4
->sock
->sk
, skb
, local_ip
.sin
.sin_addr
.s_addr
,
2360 dst
->sin
.sin_addr
.s_addr
, tos
, ttl
, df
,
2361 src_port
, dst_port
, xnet
, !udp_sum
);
2362 #if IS_ENABLED(CONFIG_IPV6)
2364 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2366 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
, ifindex
, tos
,
2367 label
, &dst
->sin6
.sin6_addr
,
2368 &local_ip
.sin6
.sin6_addr
,
2372 err
= PTR_ERR(ndst
);
2378 u32 rt6i_flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
2380 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2381 dst_port
, ifindex
, vni
,
2387 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2388 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
2389 skb_scrub_packet(skb
, xnet
);
2390 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct ipv6hdr
),
2391 vni
, md
, flags
, udp_sum
);
2395 udp_tunnel6_xmit_skb(ndst
, sock6
->sock
->sk
, skb
, dev
,
2396 &local_ip
.sin6
.sin6_addr
,
2397 &dst
->sin6
.sin6_addr
, tos
, ttl
,
2398 label
, src_port
, dst_port
, !udp_sum
);
2406 dev
->stats
.tx_dropped
++;
2413 dev
->stats
.collisions
++;
2414 else if (err
== -ENETUNREACH
)
2415 dev
->stats
.tx_carrier_errors
++;
2417 dev
->stats
.tx_errors
++;
2422 /* Transmit local packets over Vxlan
2424 * Outer IP header inherits ECN and DF from inner header.
2425 * Outer UDP destination is the VXLAN assigned port.
2426 * source port is based on hash of flow
2428 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
2430 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2431 const struct ip_tunnel_info
*info
;
2433 bool did_rsc
= false;
2434 struct vxlan_rdst
*rdst
, *fdst
= NULL
;
2435 struct vxlan_fdb
*f
;
2438 info
= skb_tunnel_info(skb
);
2440 skb_reset_mac_header(skb
);
2442 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) {
2443 if (info
&& info
->mode
& IP_TUNNEL_INFO_BRIDGE
&&
2444 info
->mode
& IP_TUNNEL_INFO_TX
) {
2445 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2447 if (info
&& info
->mode
& IP_TUNNEL_INFO_TX
)
2448 vxlan_xmit_one(skb
, dev
, vni
, NULL
, false);
2451 return NETDEV_TX_OK
;
2455 if (vxlan
->cfg
.flags
& VXLAN_F_PROXY
) {
2457 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
2458 return arp_reduce(dev
, skb
, vni
);
2459 #if IS_ENABLED(CONFIG_IPV6)
2460 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
) {
2461 struct ipv6hdr
*hdr
, _hdr
;
2462 if ((hdr
= skb_header_pointer(skb
,
2463 skb_network_offset(skb
),
2464 sizeof(_hdr
), &_hdr
)) &&
2465 hdr
->nexthdr
== IPPROTO_ICMPV6
)
2466 return neigh_reduce(dev
, skb
, vni
);
2471 if (fan_has_map(&vxlan
->fan
)) {
2472 struct vxlan_rdst fan_rdst
;
2474 netdev_dbg(vxlan
->dev
, "vxlan_xmit p %x d %pM\n",
2475 eth
->h_proto
, eth
->h_dest
);
2476 if (vxlan_fan_build_rdst(vxlan
, skb
, &fan_rdst
)) {
2477 dev
->stats
.tx_dropped
++;
2479 return NETDEV_TX_OK
;
2481 vxlan_xmit_one(skb
, dev
, vni
, &fan_rdst
, 0);
2482 return NETDEV_TX_OK
;
2486 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2489 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->cfg
.flags
& VXLAN_F_RSC
) &&
2490 (ntohs(eth
->h_proto
) == ETH_P_IP
||
2491 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
2492 did_rsc
= route_shortcircuit(dev
, skb
);
2494 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2498 f
= vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2500 if ((vxlan
->cfg
.flags
& VXLAN_F_L2MISS
) &&
2501 !is_multicast_ether_addr(eth
->h_dest
))
2502 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
2504 dev
->stats
.tx_dropped
++;
2506 return NETDEV_TX_OK
;
2510 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
2511 struct sk_buff
*skb1
;
2517 skb1
= skb_clone(skb
, GFP_ATOMIC
);
2519 vxlan_xmit_one(skb1
, dev
, vni
, rdst
, did_rsc
);
2523 vxlan_xmit_one(skb
, dev
, vni
, fdst
, did_rsc
);
2526 return NETDEV_TX_OK
;
2529 /* Walk the forwarding table and purge stale entries */
2530 static void vxlan_cleanup(unsigned long arg
)
2532 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
2533 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
2536 if (!netif_running(vxlan
->dev
))
2539 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2540 struct hlist_node
*p
, *n
;
2542 spin_lock_bh(&vxlan
->hash_lock
);
2543 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2545 = container_of(p
, struct vxlan_fdb
, hlist
);
2546 unsigned long timeout
;
2548 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
2551 if (f
->flags
& NTF_EXT_LEARNED
)
2554 timeout
= f
->used
+ vxlan
->cfg
.age_interval
* HZ
;
2555 if (time_before_eq(timeout
, jiffies
)) {
2556 netdev_dbg(vxlan
->dev
,
2557 "garbage collect %pM\n",
2559 f
->state
= NUD_STALE
;
2560 vxlan_fdb_destroy(vxlan
, f
);
2561 } else if (time_before(timeout
, next_timer
))
2562 next_timer
= timeout
;
2564 spin_unlock_bh(&vxlan
->hash_lock
);
2567 mod_timer(&vxlan
->age_timer
, next_timer
);
2570 static void vxlan_vs_del_dev(struct vxlan_dev
*vxlan
)
2572 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2574 spin_lock(&vn
->sock_lock
);
2575 hlist_del_init_rcu(&vxlan
->hlist4
.hlist
);
2576 #if IS_ENABLED(CONFIG_IPV6)
2577 hlist_del_init_rcu(&vxlan
->hlist6
.hlist
);
2579 spin_unlock(&vn
->sock_lock
);
2582 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
,
2583 struct vxlan_dev_node
*node
)
2585 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2586 __be32 vni
= vxlan
->default_dst
.remote_vni
;
2588 node
->vxlan
= vxlan
;
2589 spin_lock(&vn
->sock_lock
);
2590 hlist_add_head_rcu(&node
->hlist
, vni_head(vs
, vni
));
2591 spin_unlock(&vn
->sock_lock
);
2594 /* Setup stats when device is created */
2595 static int vxlan_init(struct net_device
*dev
)
2597 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
2604 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
, __be32 vni
)
2606 struct vxlan_fdb
*f
;
2608 spin_lock_bh(&vxlan
->hash_lock
);
2609 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2611 vxlan_fdb_destroy(vxlan
, f
);
2612 spin_unlock_bh(&vxlan
->hash_lock
);
2615 static void vxlan_uninit(struct net_device
*dev
)
2617 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2619 vxlan_fdb_delete_default(vxlan
, vxlan
->cfg
.vni
);
2621 free_percpu(dev
->tstats
);
2624 /* Start ageing timer and join group when device is brought up */
2625 static int vxlan_open(struct net_device
*dev
)
2627 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2630 ret
= vxlan_sock_add(vxlan
);
2634 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
)) {
2635 ret
= vxlan_igmp_join(vxlan
);
2636 if (ret
== -EADDRINUSE
)
2639 vxlan_sock_release(vxlan
);
2644 if (vxlan
->cfg
.age_interval
)
2645 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2650 /* Purge the forwarding table */
2651 static void vxlan_flush(struct vxlan_dev
*vxlan
, bool do_all
)
2655 spin_lock_bh(&vxlan
->hash_lock
);
2656 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2657 struct hlist_node
*p
, *n
;
2658 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2660 = container_of(p
, struct vxlan_fdb
, hlist
);
2661 if (!do_all
&& (f
->state
& (NUD_PERMANENT
| NUD_NOARP
)))
2663 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2664 if (!is_zero_ether_addr(f
->eth_addr
))
2665 vxlan_fdb_destroy(vxlan
, f
);
2668 spin_unlock_bh(&vxlan
->hash_lock
);
2671 /* Cleanup timer and forwarding table on shutdown */
2672 static int vxlan_stop(struct net_device
*dev
)
2674 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2675 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2678 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2679 !vxlan_group_used(vn
, vxlan
))
2680 ret
= vxlan_igmp_leave(vxlan
);
2682 del_timer_sync(&vxlan
->age_timer
);
2684 vxlan_flush(vxlan
, false);
2685 vxlan_sock_release(vxlan
);
2690 /* Stub, nothing needs to be done. */
2691 static void vxlan_set_multicast_list(struct net_device
*dev
)
2695 static int vxlan_change_mtu(struct net_device
*dev
, int new_mtu
)
2697 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2698 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2699 struct net_device
*lowerdev
= __dev_get_by_index(vxlan
->net
,
2700 dst
->remote_ifindex
);
2701 bool use_ipv6
= !!(vxlan
->cfg
.flags
& VXLAN_F_IPV6
);
2703 /* This check is different than dev->max_mtu, because it looks at
2704 * the lowerdev->mtu, rather than the static dev->max_mtu
2707 int max_mtu
= lowerdev
->mtu
-
2708 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2709 if (new_mtu
> max_mtu
)
2717 static int vxlan_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
2719 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2720 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
2721 __be16 sport
, dport
;
2723 sport
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2724 vxlan
->cfg
.port_max
, true);
2725 dport
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2727 if (ip_tunnel_info_af(info
) == AF_INET
) {
2728 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2731 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
, 0, info
->key
.tos
,
2732 info
->key
.u
.ipv4
.dst
,
2733 &info
->key
.u
.ipv4
.src
, dport
, sport
,
2734 &info
->dst_cache
, info
);
2739 #if IS_ENABLED(CONFIG_IPV6)
2740 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2741 struct dst_entry
*ndst
;
2743 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
, 0, info
->key
.tos
,
2744 info
->key
.label
, &info
->key
.u
.ipv6
.dst
,
2745 &info
->key
.u
.ipv6
.src
, dport
, sport
,
2746 &info
->dst_cache
, info
);
2748 return PTR_ERR(ndst
);
2750 #else /* !CONFIG_IPV6 */
2751 return -EPFNOSUPPORT
;
2754 info
->key
.tp_src
= sport
;
2755 info
->key
.tp_dst
= dport
;
2759 static const struct net_device_ops vxlan_netdev_ether_ops
= {
2760 .ndo_init
= vxlan_init
,
2761 .ndo_uninit
= vxlan_uninit
,
2762 .ndo_open
= vxlan_open
,
2763 .ndo_stop
= vxlan_stop
,
2764 .ndo_start_xmit
= vxlan_xmit
,
2765 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2766 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2767 .ndo_change_mtu
= vxlan_change_mtu
,
2768 .ndo_validate_addr
= eth_validate_addr
,
2769 .ndo_set_mac_address
= eth_mac_addr
,
2770 .ndo_fdb_add
= vxlan_fdb_add
,
2771 .ndo_fdb_del
= vxlan_fdb_delete
,
2772 .ndo_fdb_dump
= vxlan_fdb_dump
,
2773 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2776 static const struct net_device_ops vxlan_netdev_raw_ops
= {
2777 .ndo_init
= vxlan_init
,
2778 .ndo_uninit
= vxlan_uninit
,
2779 .ndo_open
= vxlan_open
,
2780 .ndo_stop
= vxlan_stop
,
2781 .ndo_start_xmit
= vxlan_xmit
,
2782 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2783 .ndo_change_mtu
= vxlan_change_mtu
,
2784 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2787 /* Info for udev, that this is a virtual tunnel endpoint */
2788 static struct device_type vxlan_type
= {
2792 /* Calls the ndo_udp_tunnel_add of the caller in order to
2793 * supply the listening VXLAN udp ports. Callers are expected
2794 * to implement the ndo_udp_tunnel_add.
2796 static void vxlan_push_rx_ports(struct net_device
*dev
)
2798 struct vxlan_sock
*vs
;
2799 struct net
*net
= dev_net(dev
);
2800 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2803 spin_lock(&vn
->sock_lock
);
2804 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2805 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
)
2806 udp_tunnel_push_rx_port(dev
, vs
->sock
,
2807 (vs
->flags
& VXLAN_F_GPE
) ?
2808 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2809 UDP_TUNNEL_TYPE_VXLAN
);
2811 spin_unlock(&vn
->sock_lock
);
2814 /* Initialize the device structure. */
2815 static void vxlan_setup(struct net_device
*dev
)
2817 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2820 eth_hw_addr_random(dev
);
2823 dev
->needs_free_netdev
= true;
2824 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2826 dev
->features
|= NETIF_F_LLTX
;
2827 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2828 dev
->features
|= NETIF_F_RXCSUM
;
2829 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2831 dev
->vlan_features
= dev
->features
;
2832 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2833 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2834 netif_keep_dst(dev
);
2835 dev
->priv_flags
|= IFF_NO_QUEUE
;
2837 /* MTU range: 68 - 65535 */
2838 dev
->min_mtu
= ETH_MIN_MTU
;
2839 dev
->max_mtu
= ETH_MAX_MTU
;
2841 INIT_LIST_HEAD(&vxlan
->next
);
2842 spin_lock_init(&vxlan
->hash_lock
);
2844 init_timer_deferrable(&vxlan
->age_timer
);
2845 vxlan
->age_timer
.function
= vxlan_cleanup
;
2846 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2850 gro_cells_init(&vxlan
->gro_cells
, dev
);
2852 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2853 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2855 INIT_LIST_HEAD(&vxlan
->fan
.fan_maps
);
2858 static void vxlan_ether_setup(struct net_device
*dev
)
2860 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
2861 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2862 dev
->netdev_ops
= &vxlan_netdev_ether_ops
;
2865 static void vxlan_raw_setup(struct net_device
*dev
)
2867 dev
->header_ops
= NULL
;
2868 dev
->type
= ARPHRD_NONE
;
2869 dev
->hard_header_len
= 0;
2871 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
2872 dev
->netdev_ops
= &vxlan_netdev_raw_ops
;
2875 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2876 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2877 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2878 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2879 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2880 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2881 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2882 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2883 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2884 [IFLA_VXLAN_LABEL
] = { .type
= NLA_U32
},
2885 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2886 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2887 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2888 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2889 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2890 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2891 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2892 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2893 [IFLA_VXLAN_COLLECT_METADATA
] = { .type
= NLA_U8
},
2894 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2895 [IFLA_VXLAN_UDP_CSUM
] = { .type
= NLA_U8
},
2896 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
2897 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
2898 [IFLA_VXLAN_REMCSUM_TX
] = { .type
= NLA_U8
},
2899 [IFLA_VXLAN_REMCSUM_RX
] = { .type
= NLA_U8
},
2900 [IFLA_VXLAN_GBP
] = { .type
= NLA_FLAG
, },
2901 [IFLA_VXLAN_GPE
] = { .type
= NLA_FLAG
, },
2902 [IFLA_VXLAN_REMCSUM_NOPARTIAL
] = { .type
= NLA_FLAG
},
2905 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
2906 struct netlink_ext_ack
*extack
)
2908 if (tb
[IFLA_ADDRESS
]) {
2909 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2910 pr_debug("invalid link address (not ethernet)\n");
2914 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2915 pr_debug("invalid all zero ethernet address\n");
2916 return -EADDRNOTAVAIL
;
2921 u32 mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2923 if (mtu
< ETH_MIN_MTU
|| mtu
> ETH_MAX_MTU
)
2930 if (data
[IFLA_VXLAN_ID
]) {
2931 u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2933 if (id
>= VXLAN_N_VID
)
2937 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2938 const struct ifla_vxlan_port_range
*p
2939 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2941 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2942 pr_debug("port range %u .. %u not valid\n",
2943 ntohs(p
->low
), ntohs(p
->high
));
2951 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2952 struct ethtool_drvinfo
*drvinfo
)
2954 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2955 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2958 static const struct ethtool_ops vxlan_ethtool_ops
= {
2959 .get_drvinfo
= vxlan_get_drvinfo
,
2960 .get_link
= ethtool_op_get_link
,
2963 static struct socket
*vxlan_create_sock(struct net
*net
, bool ipv6
,
2964 __be16 port
, u32 flags
)
2966 struct socket
*sock
;
2967 struct udp_port_cfg udp_conf
;
2970 memset(&udp_conf
, 0, sizeof(udp_conf
));
2973 udp_conf
.family
= AF_INET6
;
2974 udp_conf
.use_udp6_rx_checksums
=
2975 !(flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
);
2976 udp_conf
.ipv6_v6only
= 1;
2978 udp_conf
.family
= AF_INET
;
2981 udp_conf
.local_udp_port
= port
;
2983 /* Open UDP socket */
2984 err
= udp_sock_create(net
, &udp_conf
, &sock
);
2986 return ERR_PTR(err
);
2991 /* Create new listen socket if needed */
2992 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, bool ipv6
,
2993 __be16 port
, u32 flags
)
2995 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2996 struct vxlan_sock
*vs
;
2997 struct socket
*sock
;
2999 struct udp_tunnel_sock_cfg tunnel_cfg
;
3001 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
);
3003 return ERR_PTR(-ENOMEM
);
3005 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
3006 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
3008 sock
= vxlan_create_sock(net
, ipv6
, port
, flags
);
3011 return ERR_CAST(sock
);
3015 refcount_set(&vs
->refcnt
, 1);
3016 vs
->flags
= (flags
& VXLAN_F_RCV_FLAGS
);
3018 spin_lock(&vn
->sock_lock
);
3019 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
3020 udp_tunnel_notify_add_rx_port(sock
,
3021 (vs
->flags
& VXLAN_F_GPE
) ?
3022 UDP_TUNNEL_TYPE_VXLAN_GPE
:
3023 UDP_TUNNEL_TYPE_VXLAN
);
3024 spin_unlock(&vn
->sock_lock
);
3026 /* Mark socket as an encapsulation socket. */
3027 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
3028 tunnel_cfg
.sk_user_data
= vs
;
3029 tunnel_cfg
.encap_type
= 1;
3030 tunnel_cfg
.encap_rcv
= vxlan_rcv
;
3031 tunnel_cfg
.encap_destroy
= NULL
;
3032 tunnel_cfg
.gro_receive
= vxlan_gro_receive
;
3033 tunnel_cfg
.gro_complete
= vxlan_gro_complete
;
3035 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
3040 static int __vxlan_sock_add(struct vxlan_dev
*vxlan
, bool ipv6
)
3042 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
3043 struct vxlan_sock
*vs
= NULL
;
3044 struct vxlan_dev_node
*node
;
3046 if (!vxlan
->cfg
.no_share
) {
3047 spin_lock(&vn
->sock_lock
);
3048 vs
= vxlan_find_sock(vxlan
->net
, ipv6
? AF_INET6
: AF_INET
,
3049 vxlan
->cfg
.dst_port
, vxlan
->cfg
.flags
);
3050 if (vs
&& !refcount_inc_not_zero(&vs
->refcnt
)) {
3051 spin_unlock(&vn
->sock_lock
);
3054 spin_unlock(&vn
->sock_lock
);
3057 vs
= vxlan_socket_create(vxlan
->net
, ipv6
,
3058 vxlan
->cfg
.dst_port
, vxlan
->cfg
.flags
);
3061 #if IS_ENABLED(CONFIG_IPV6)
3063 rcu_assign_pointer(vxlan
->vn6_sock
, vs
);
3064 node
= &vxlan
->hlist6
;
3068 rcu_assign_pointer(vxlan
->vn4_sock
, vs
);
3069 node
= &vxlan
->hlist4
;
3071 vxlan_vs_add_dev(vs
, vxlan
, node
);
3075 static int vxlan_sock_add(struct vxlan_dev
*vxlan
)
3077 bool metadata
= vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
;
3078 bool ipv6
= vxlan
->cfg
.flags
& VXLAN_F_IPV6
|| metadata
;
3079 bool ipv4
= !ipv6
|| metadata
;
3082 RCU_INIT_POINTER(vxlan
->vn4_sock
, NULL
);
3083 #if IS_ENABLED(CONFIG_IPV6)
3084 RCU_INIT_POINTER(vxlan
->vn6_sock
, NULL
);
3086 ret
= __vxlan_sock_add(vxlan
, true);
3087 if (ret
< 0 && ret
!= -EAFNOSUPPORT
)
3092 ret
= __vxlan_sock_add(vxlan
, false);
3094 vxlan_sock_release(vxlan
);
3098 static int vxlan_config_validate(struct net
*src_net
, struct vxlan_config
*conf
,
3099 struct net_device
**lower
,
3100 struct vxlan_dev
*old
)
3102 struct vxlan_net
*vn
= net_generic(src_net
, vxlan_net_id
);
3103 struct vxlan_dev
*tmp
;
3104 bool use_ipv6
= false;
3106 if (conf
->flags
& VXLAN_F_GPE
) {
3107 /* For now, allow GPE only together with
3108 * COLLECT_METADATA. This can be relaxed later; in such
3109 * case, the other side of the PtP link will have to be
3112 if ((conf
->flags
& ~VXLAN_F_ALLOWED_GPE
) ||
3113 !(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
3118 if (!conf
->remote_ip
.sa
.sa_family
&& !conf
->saddr
.sa
.sa_family
) {
3119 /* Unless IPv6 is explicitly requested, assume IPv4 */
3120 conf
->remote_ip
.sa
.sa_family
= AF_INET
;
3121 conf
->saddr
.sa
.sa_family
= AF_INET
;
3122 } else if (!conf
->remote_ip
.sa
.sa_family
) {
3123 conf
->remote_ip
.sa
.sa_family
= conf
->saddr
.sa
.sa_family
;
3124 } else if (!conf
->saddr
.sa
.sa_family
) {
3125 conf
->saddr
.sa
.sa_family
= conf
->remote_ip
.sa
.sa_family
;
3128 if (conf
->saddr
.sa
.sa_family
!= conf
->remote_ip
.sa
.sa_family
)
3131 if (vxlan_addr_multicast(&conf
->saddr
))
3134 if (conf
->saddr
.sa
.sa_family
== AF_INET6
) {
3135 if (!IS_ENABLED(CONFIG_IPV6
))
3136 return -EPFNOSUPPORT
;
3138 conf
->flags
|= VXLAN_F_IPV6
;
3140 if (!(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
3142 ipv6_addr_type(&conf
->saddr
.sin6
.sin6_addr
);
3144 ipv6_addr_type(&conf
->remote_ip
.sin6
.sin6_addr
);
3146 if (local_type
& IPV6_ADDR_LINKLOCAL
) {
3147 if (!(remote_type
& IPV6_ADDR_LINKLOCAL
) &&
3148 (remote_type
!= IPV6_ADDR_ANY
))
3151 conf
->flags
|= VXLAN_F_IPV6_LINKLOCAL
;
3154 (IPV6_ADDR_UNICAST
| IPV6_ADDR_LINKLOCAL
))
3157 conf
->flags
&= ~VXLAN_F_IPV6_LINKLOCAL
;
3162 if (conf
->label
&& !use_ipv6
)
3165 if (conf
->remote_ifindex
) {
3166 struct net_device
*lowerdev
;
3168 lowerdev
= __dev_get_by_index(src_net
, conf
->remote_ifindex
);
3172 #if IS_ENABLED(CONFIG_IPV6)
3174 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
3175 if (idev
&& idev
->cnf
.disable_ipv6
)
3182 if (vxlan_addr_multicast(&conf
->remote_ip
))
3185 #if IS_ENABLED(CONFIG_IPV6)
3186 if (conf
->flags
& VXLAN_F_IPV6_LINKLOCAL
)
3193 if (!conf
->dst_port
) {
3194 if (conf
->flags
& VXLAN_F_GPE
)
3195 conf
->dst_port
= htons(4790); /* IANA VXLAN-GPE port */
3197 conf
->dst_port
= htons(vxlan_port
);
3200 if (!conf
->age_interval
)
3201 conf
->age_interval
= FDB_AGE_DEFAULT
;
3203 list_for_each_entry(tmp
, &vn
->vxlan_list
, next
) {
3207 if (tmp
->cfg
.vni
!= conf
->vni
)
3209 if (tmp
->cfg
.dst_port
!= conf
->dst_port
)
3211 if ((tmp
->cfg
.flags
& (VXLAN_F_RCV_FLAGS
| VXLAN_F_IPV6
)) !=
3212 (conf
->flags
& (VXLAN_F_RCV_FLAGS
| VXLAN_F_IPV6
)))
3215 if ((conf
->flags
& VXLAN_F_IPV6_LINKLOCAL
) &&
3216 tmp
->cfg
.remote_ifindex
!= conf
->remote_ifindex
)
3225 static void vxlan_config_apply(struct net_device
*dev
,
3226 struct vxlan_config
*conf
,
3227 struct net_device
*lowerdev
,
3228 struct net
*src_net
,
3231 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3232 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3233 unsigned short needed_headroom
= ETH_HLEN
;
3234 bool use_ipv6
= !!(conf
->flags
& VXLAN_F_IPV6
);
3235 int max_mtu
= ETH_MAX_MTU
;
3238 if (conf
->flags
& VXLAN_F_GPE
)
3239 vxlan_raw_setup(dev
);
3241 vxlan_ether_setup(dev
);
3244 dev
->mtu
= conf
->mtu
;
3246 vxlan
->net
= src_net
;
3249 dst
->remote_vni
= conf
->vni
;
3251 memcpy(&dst
->remote_ip
, &conf
->remote_ip
, sizeof(conf
->remote_ip
));
3254 dst
->remote_ifindex
= conf
->remote_ifindex
;
3256 dev
->gso_max_size
= lowerdev
->gso_max_size
;
3257 dev
->gso_max_segs
= lowerdev
->gso_max_segs
;
3259 needed_headroom
= lowerdev
->hard_header_len
;
3261 max_mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
:
3265 if (dev
->mtu
> max_mtu
)
3268 if (use_ipv6
|| conf
->flags
& VXLAN_F_COLLECT_METADATA
)
3269 needed_headroom
+= VXLAN6_HEADROOM
;
3271 needed_headroom
+= VXLAN_HEADROOM
;
3272 dev
->needed_headroom
= needed_headroom
;
3274 memcpy(&vxlan
->cfg
, conf
, sizeof(*conf
));
3277 static int vxlan_dev_configure(struct net
*src_net
, struct net_device
*dev
,
3278 struct vxlan_config
*conf
,
3281 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3282 struct net_device
*lowerdev
;
3285 ret
= vxlan_config_validate(src_net
, conf
, &lowerdev
, vxlan
);
3289 vxlan_config_apply(dev
, conf
, lowerdev
, src_net
, changelink
);
3294 static int __vxlan_dev_create(struct net
*net
, struct net_device
*dev
,
3295 struct vxlan_config
*conf
)
3297 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3298 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3301 err
= vxlan_dev_configure(net
, dev
, conf
, false);
3305 dev
->ethtool_ops
= &vxlan_ethtool_ops
;
3307 /* create an fdb entry for a valid default destination */
3308 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
3309 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
3310 &vxlan
->default_dst
.remote_ip
,
3311 NUD_REACHABLE
| NUD_PERMANENT
,
3312 NLM_F_EXCL
| NLM_F_CREATE
,
3313 vxlan
->cfg
.dst_port
,
3314 vxlan
->default_dst
.remote_vni
,
3315 vxlan
->default_dst
.remote_vni
,
3316 vxlan
->default_dst
.remote_ifindex
,
3322 err
= register_netdevice(dev
);
3324 vxlan_fdb_delete_default(vxlan
, vxlan
->default_dst
.remote_vni
);
3328 list_add(&vxlan
->next
, &vn
->vxlan_list
);
3332 static int vxlan_nl2conf(struct nlattr
*tb
[], struct nlattr
*data
[],
3333 struct net_device
*dev
, struct vxlan_config
*conf
,
3336 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3339 memset(conf
, 0, sizeof(*conf
));
3341 /* if changelink operation, start with old existing cfg */
3343 memcpy(conf
, &vxlan
->cfg
, sizeof(*conf
));
3345 if (data
[IFLA_VXLAN_ID
]) {
3346 __be32 vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3348 if (changelink
&& (vni
!= conf
->vni
))
3350 conf
->vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3353 if (data
[IFLA_VXLAN_GROUP
]) {
3354 if (changelink
&& (conf
->remote_ip
.sa
.sa_family
!= AF_INET
))
3357 conf
->remote_ip
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_GROUP
]);
3358 conf
->remote_ip
.sa
.sa_family
= AF_INET
;
3359 } else if (data
[IFLA_VXLAN_GROUP6
]) {
3360 if (!IS_ENABLED(CONFIG_IPV6
))
3361 return -EPFNOSUPPORT
;
3363 if (changelink
&& (conf
->remote_ip
.sa
.sa_family
!= AF_INET6
))
3366 conf
->remote_ip
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_GROUP6
]);
3367 conf
->remote_ip
.sa
.sa_family
= AF_INET6
;
3370 if (data
[IFLA_VXLAN_FAN_MAP
]) {
3371 err
= vxlan_parse_fan_map(data
, vxlan
);
3376 if (data
[IFLA_VXLAN_LOCAL
]) {
3377 if (changelink
&& (conf
->saddr
.sa
.sa_family
!= AF_INET
))
3380 conf
->saddr
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_LOCAL
]);
3381 conf
->saddr
.sa
.sa_family
= AF_INET
;
3382 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
3383 if (!IS_ENABLED(CONFIG_IPV6
))
3384 return -EPFNOSUPPORT
;
3386 if (changelink
&& (conf
->saddr
.sa
.sa_family
!= AF_INET6
))
3389 /* TODO: respect scope id */
3390 conf
->saddr
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_LOCAL6
]);
3391 conf
->saddr
.sa
.sa_family
= AF_INET6
;
3394 if (data
[IFLA_VXLAN_LINK
])
3395 conf
->remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]);
3397 if (data
[IFLA_VXLAN_TOS
])
3398 conf
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
3400 if (data
[IFLA_VXLAN_TTL
])
3401 conf
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
3403 if (data
[IFLA_VXLAN_LABEL
])
3404 conf
->label
= nla_get_be32(data
[IFLA_VXLAN_LABEL
]) &
3405 IPV6_FLOWLABEL_MASK
;
3407 if (data
[IFLA_VXLAN_LEARNING
]) {
3408 if (nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
3409 conf
->flags
|= VXLAN_F_LEARN
;
3411 conf
->flags
&= ~VXLAN_F_LEARN
;
3412 } else if (!changelink
) {
3413 /* default to learn on a new device */
3414 conf
->flags
|= VXLAN_F_LEARN
;
3417 if (data
[IFLA_VXLAN_AGEING
]) {
3420 conf
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
3423 if (data
[IFLA_VXLAN_PROXY
]) {
3426 if (nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
3427 conf
->flags
|= VXLAN_F_PROXY
;
3430 if (data
[IFLA_VXLAN_RSC
]) {
3433 if (nla_get_u8(data
[IFLA_VXLAN_RSC
]))
3434 conf
->flags
|= VXLAN_F_RSC
;
3437 if (data
[IFLA_VXLAN_L2MISS
]) {
3440 if (nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
3441 conf
->flags
|= VXLAN_F_L2MISS
;
3444 if (data
[IFLA_VXLAN_L3MISS
]) {
3447 if (nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
3448 conf
->flags
|= VXLAN_F_L3MISS
;
3451 if (data
[IFLA_VXLAN_LIMIT
]) {
3454 conf
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
3457 if (data
[IFLA_VXLAN_COLLECT_METADATA
]) {
3460 if (nla_get_u8(data
[IFLA_VXLAN_COLLECT_METADATA
]))
3461 conf
->flags
|= VXLAN_F_COLLECT_METADATA
;
3464 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
3466 const struct ifla_vxlan_port_range
*p
3467 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
3468 conf
->port_min
= ntohs(p
->low
);
3469 conf
->port_max
= ntohs(p
->high
);
3475 if (data
[IFLA_VXLAN_PORT
]) {
3478 conf
->dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
3481 if (data
[IFLA_VXLAN_UDP_CSUM
]) {
3484 if (!nla_get_u8(data
[IFLA_VXLAN_UDP_CSUM
]))
3485 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM_TX
;
3488 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]) {
3491 if (nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
3492 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_TX
;
3495 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]) {
3498 if (nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
3499 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_RX
;
3502 if (data
[IFLA_VXLAN_REMCSUM_TX
]) {
3505 if (nla_get_u8(data
[IFLA_VXLAN_REMCSUM_TX
]))
3506 conf
->flags
|= VXLAN_F_REMCSUM_TX
;
3509 if (data
[IFLA_VXLAN_REMCSUM_RX
]) {
3512 if (nla_get_u8(data
[IFLA_VXLAN_REMCSUM_RX
]))
3513 conf
->flags
|= VXLAN_F_REMCSUM_RX
;
3516 if (data
[IFLA_VXLAN_GBP
]) {
3519 conf
->flags
|= VXLAN_F_GBP
;
3522 if (data
[IFLA_VXLAN_GPE
]) {
3525 conf
->flags
|= VXLAN_F_GPE
;
3528 if (data
[IFLA_VXLAN_REMCSUM_NOPARTIAL
]) {
3531 conf
->flags
|= VXLAN_F_REMCSUM_NOPARTIAL
;
3537 conf
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3543 static int vxlan_newlink(struct net
*src_net
, struct net_device
*dev
,
3544 struct nlattr
*tb
[], struct nlattr
*data
[],
3545 struct netlink_ext_ack
*extack
)
3547 struct vxlan_config conf
;
3550 err
= vxlan_nl2conf(tb
, data
, dev
, &conf
, false);
3554 return __vxlan_dev_create(src_net
, dev
, &conf
);
3557 static int vxlan_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
3558 struct nlattr
*data
[],
3559 struct netlink_ext_ack
*extack
)
3561 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3562 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3563 struct vxlan_rdst old_dst
;
3564 struct vxlan_config conf
;
3567 err
= vxlan_nl2conf(tb
, data
,
3572 memcpy(&old_dst
, dst
, sizeof(struct vxlan_rdst
));
3574 err
= vxlan_dev_configure(vxlan
->net
, dev
, &conf
, true);
3578 /* handle default dst entry */
3579 if (!vxlan_addr_equal(&dst
->remote_ip
, &old_dst
.remote_ip
)) {
3580 spin_lock_bh(&vxlan
->hash_lock
);
3581 if (!vxlan_addr_any(&old_dst
.remote_ip
))
3582 __vxlan_fdb_delete(vxlan
, all_zeros_mac
,
3584 vxlan
->cfg
.dst_port
,
3587 old_dst
.remote_ifindex
, 0);
3589 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3590 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
3592 NUD_REACHABLE
| NUD_PERMANENT
,
3593 NLM_F_CREATE
| NLM_F_APPEND
,
3594 vxlan
->cfg
.dst_port
,
3597 dst
->remote_ifindex
,
3600 spin_unlock_bh(&vxlan
->hash_lock
);
3604 spin_unlock_bh(&vxlan
->hash_lock
);
3610 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
3612 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3614 vxlan_flush(vxlan
, true);
3616 gro_cells_destroy(&vxlan
->gro_cells
);
3617 list_del(&vxlan
->next
);
3618 unregister_netdevice_queue(dev
, head
);
3621 static size_t vxlan_get_size(const struct net_device
*dev
)
3624 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
3625 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
3626 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
3627 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
3628 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
3629 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
3630 nla_total_size(sizeof(__be32
)) + /* IFLA_VXLAN_LABEL */
3631 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
3632 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
3633 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
3634 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
3635 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
3636 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_COLLECT_METADATA */
3637 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
3638 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
3639 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
3640 nla_total_size(sizeof(__be16
)) + /* IFLA_VXLAN_PORT */
3641 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_CSUM */
3642 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3643 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3644 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_TX */
3645 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_RX */
3646 nla_total_size(sizeof(struct ip_fan_map
) * 256) +
3650 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
3652 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3653 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3654 struct ifla_vxlan_port_range ports
= {
3655 .low
= htons(vxlan
->cfg
.port_min
),
3656 .high
= htons(vxlan
->cfg
.port_max
),
3659 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, be32_to_cpu(dst
->remote_vni
)))
3660 goto nla_put_failure
;
3662 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3663 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
3664 if (nla_put_in_addr(skb
, IFLA_VXLAN_GROUP
,
3665 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
3666 goto nla_put_failure
;
3667 #if IS_ENABLED(CONFIG_IPV6)
3669 if (nla_put_in6_addr(skb
, IFLA_VXLAN_GROUP6
,
3670 &dst
->remote_ip
.sin6
.sin6_addr
))
3671 goto nla_put_failure
;
3676 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
3677 goto nla_put_failure
;
3679 if (!vxlan_addr_any(&vxlan
->cfg
.saddr
)) {
3680 if (vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET
) {
3681 if (nla_put_in_addr(skb
, IFLA_VXLAN_LOCAL
,
3682 vxlan
->cfg
.saddr
.sin
.sin_addr
.s_addr
))
3683 goto nla_put_failure
;
3684 #if IS_ENABLED(CONFIG_IPV6)
3686 if (nla_put_in6_addr(skb
, IFLA_VXLAN_LOCAL6
,
3687 &vxlan
->cfg
.saddr
.sin6
.sin6_addr
))
3688 goto nla_put_failure
;
3693 if (fan_has_map(&vxlan
->fan
)) {
3694 struct nlattr
*fan_nest
;
3695 struct ip_fan_map
*fan_map
;
3697 fan_nest
= nla_nest_start(skb
, IFLA_VXLAN_FAN_MAP
);
3699 goto nla_put_failure
;
3700 list_for_each_entry_rcu(fan_map
, &vxlan
->fan
.fan_maps
, list
) {
3701 struct ifla_fan_map map
;
3703 map
.underlay
= fan_map
->underlay
;
3704 map
.underlay_prefix
= fan_map
->underlay_prefix
;
3705 map
.overlay
= fan_map
->overlay
;
3706 map
.overlay_prefix
= fan_map
->overlay_prefix
;
3707 if (nla_put(skb
, IFLA_FAN_MAPPING
, sizeof(map
), &map
))
3708 goto nla_put_failure
;
3710 nla_nest_end(skb
, fan_nest
);
3713 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->cfg
.ttl
) ||
3714 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->cfg
.tos
) ||
3715 nla_put_be32(skb
, IFLA_VXLAN_LABEL
, vxlan
->cfg
.label
) ||
3716 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
3717 !!(vxlan
->cfg
.flags
& VXLAN_F_LEARN
)) ||
3718 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
3719 !!(vxlan
->cfg
.flags
& VXLAN_F_PROXY
)) ||
3720 nla_put_u8(skb
, IFLA_VXLAN_RSC
,
3721 !!(vxlan
->cfg
.flags
& VXLAN_F_RSC
)) ||
3722 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
3723 !!(vxlan
->cfg
.flags
& VXLAN_F_L2MISS
)) ||
3724 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
3725 !!(vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) ||
3726 nla_put_u8(skb
, IFLA_VXLAN_COLLECT_METADATA
,
3727 !!(vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
)) ||
3728 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->cfg
.age_interval
) ||
3729 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->cfg
.addrmax
) ||
3730 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->cfg
.dst_port
) ||
3731 nla_put_u8(skb
, IFLA_VXLAN_UDP_CSUM
,
3732 !(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM_TX
)) ||
3733 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_TX
,
3734 !!(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
)) ||
3735 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_RX
,
3736 !!(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
)) ||
3737 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_TX
,
3738 !!(vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_TX
)) ||
3739 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_RX
,
3740 !!(vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_RX
)))
3741 goto nla_put_failure
;
3743 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
3744 goto nla_put_failure
;
3746 if (vxlan
->cfg
.flags
& VXLAN_F_GBP
&&
3747 nla_put_flag(skb
, IFLA_VXLAN_GBP
))
3748 goto nla_put_failure
;
3750 if (vxlan
->cfg
.flags
& VXLAN_F_GPE
&&
3751 nla_put_flag(skb
, IFLA_VXLAN_GPE
))
3752 goto nla_put_failure
;
3754 if (vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_NOPARTIAL
&&
3755 nla_put_flag(skb
, IFLA_VXLAN_REMCSUM_NOPARTIAL
))
3756 goto nla_put_failure
;
3764 static struct net
*vxlan_get_link_net(const struct net_device
*dev
)
3766 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3771 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
3773 .maxtype
= IFLA_VXLAN_MAX
,
3774 .policy
= vxlan_policy
,
3775 .priv_size
= sizeof(struct vxlan_dev
),
3776 .setup
= vxlan_setup
,
3777 .validate
= vxlan_validate
,
3778 .newlink
= vxlan_newlink
,
3779 .changelink
= vxlan_changelink
,
3780 .dellink
= vxlan_dellink
,
3781 .get_size
= vxlan_get_size
,
3782 .fill_info
= vxlan_fill_info
,
3783 .get_link_net
= vxlan_get_link_net
,
3786 struct net_device
*vxlan_dev_create(struct net
*net
, const char *name
,
3787 u8 name_assign_type
,
3788 struct vxlan_config
*conf
)
3790 struct nlattr
*tb
[IFLA_MAX
+ 1];
3791 struct net_device
*dev
;
3794 memset(&tb
, 0, sizeof(tb
));
3796 dev
= rtnl_create_link(net
, name
, name_assign_type
,
3797 &vxlan_link_ops
, tb
);
3801 err
= __vxlan_dev_create(net
, dev
, conf
);
3804 return ERR_PTR(err
);
3807 err
= rtnl_configure_link(dev
, NULL
);
3809 LIST_HEAD(list_kill
);
3811 vxlan_dellink(dev
, &list_kill
);
3812 unregister_netdevice_many(&list_kill
);
3813 return ERR_PTR(err
);
3818 EXPORT_SYMBOL_GPL(vxlan_dev_create
);
3820 static void vxlan_handle_lowerdev_unregister(struct vxlan_net
*vn
,
3821 struct net_device
*dev
)
3823 struct vxlan_dev
*vxlan
, *next
;
3824 LIST_HEAD(list_kill
);
3826 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3827 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3829 /* In case we created vxlan device with carrier
3830 * and we loose the carrier due to module unload
3831 * we also need to remove vxlan device. In other
3832 * cases, it's not necessary and remote_ifindex
3833 * is 0 here, so no matches.
3835 if (dst
->remote_ifindex
== dev
->ifindex
)
3836 vxlan_dellink(vxlan
->dev
, &list_kill
);
3839 unregister_netdevice_many(&list_kill
);
3842 static int vxlan_netdevice_event(struct notifier_block
*unused
,
3843 unsigned long event
, void *ptr
)
3845 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3846 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
3848 if (event
== NETDEV_UNREGISTER
)
3849 vxlan_handle_lowerdev_unregister(vn
, dev
);
3850 else if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
)
3851 vxlan_push_rx_ports(dev
);
3856 static struct notifier_block vxlan_notifier_block __read_mostly
= {
3857 .notifier_call
= vxlan_netdevice_event
,
3860 static __net_init
int vxlan_init_net(struct net
*net
)
3862 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3865 INIT_LIST_HEAD(&vn
->vxlan_list
);
3866 spin_lock_init(&vn
->sock_lock
);
3868 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
3869 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
3874 #ifdef CONFIG_SYSCTL
3875 static struct ctl_table_header
*vxlan_fan_header
;
3876 static unsigned int vxlan_fan_version
= 4;
3878 static struct ctl_table vxlan_fan_sysctls
[] = {
3880 .procname
= "vxlan",
3881 .data
= &vxlan_fan_version
,
3882 .maxlen
= sizeof(vxlan_fan_version
),
3884 .proc_handler
= proc_dointvec
,
3888 #endif /* CONFIG_SYSCTL */
3890 static void __net_exit
vxlan_exit_net(struct net
*net
)
3892 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3893 struct vxlan_dev
*vxlan
, *next
;
3894 struct net_device
*dev
, *aux
;
3898 for_each_netdev_safe(net
, dev
, aux
)
3899 if (dev
->rtnl_link_ops
== &vxlan_link_ops
)
3900 unregister_netdevice_queue(dev
, &list
);
3902 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3903 /* If vxlan->dev is in the same netns, it has already been added
3904 * to the list by the previous loop.
3906 if (!net_eq(dev_net(vxlan
->dev
), net
)) {
3907 gro_cells_destroy(&vxlan
->gro_cells
);
3908 unregister_netdevice_queue(vxlan
->dev
, &list
);
3912 unregister_netdevice_many(&list
);
3916 static struct pernet_operations vxlan_net_ops
= {
3917 .init
= vxlan_init_net
,
3918 .exit
= vxlan_exit_net
,
3919 .id
= &vxlan_net_id
,
3920 .size
= sizeof(struct vxlan_net
),
3923 static int __init
vxlan_init_module(void)
3927 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
3929 rc
= register_pernet_subsys(&vxlan_net_ops
);
3933 rc
= register_netdevice_notifier(&vxlan_notifier_block
);
3937 rc
= rtnl_link_register(&vxlan_link_ops
);
3941 #ifdef CONFIG_SYSCTL
3942 vxlan_fan_header
= register_net_sysctl(&init_net
, "net/fan",
3944 if (!vxlan_fan_header
) {
3948 #endif /* CONFIG_SYSCTL */
3951 #ifdef CONFIG_SYSCTL
3953 rtnl_link_unregister(&vxlan_link_ops
);
3954 #endif /* CONFIG_SYSCTL */
3956 unregister_netdevice_notifier(&vxlan_notifier_block
);
3958 unregister_pernet_subsys(&vxlan_net_ops
);
3962 late_initcall(vxlan_init_module
);
3964 static void __exit
vxlan_cleanup_module(void)
3966 #ifdef CONFIG_SYSCTL
3967 unregister_net_sysctl_table(vxlan_fan_header
);
3968 #endif /* CONFIG_SYSCTL */
3969 rtnl_link_unregister(&vxlan_link_ops
);
3970 unregister_netdevice_notifier(&vxlan_notifier_block
);
3971 unregister_pernet_subsys(&vxlan_net_ops
);
3972 /* rcu_barrier() is called by netns */
3974 module_exit(vxlan_cleanup_module
);
3976 MODULE_LICENSE("GPL");
3977 MODULE_VERSION(VXLAN_VERSION
);
3978 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3979 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3980 MODULE_ALIAS_RTNL_LINK("vxlan");