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
);
1797 const struct in6_addr
*daddr
;
1798 const struct ipv6hdr
*iphdr
;
1799 struct inet6_dev
*in6_dev
;
1800 struct neighbour
*n
;
1803 in6_dev
= __in6_dev_get(dev
);
1807 iphdr
= ipv6_hdr(skb
);
1808 daddr
= &iphdr
->daddr
;
1809 msg
= (struct nd_msg
*)(iphdr
+ 1);
1811 if (ipv6_addr_loopback(daddr
) ||
1812 ipv6_addr_is_multicast(&msg
->target
))
1815 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1818 struct vxlan_fdb
*f
;
1819 struct sk_buff
*reply
;
1821 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1826 f
= vxlan_find_mac(vxlan
, n
->ha
, vni
);
1827 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1828 /* bridge-local neighbor */
1833 reply
= vxlan_na_create(skb
, n
,
1834 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1841 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1842 dev
->stats
.rx_dropped
++;
1844 } else if (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
) {
1845 union vxlan_addr ipa
= {
1846 .sin6
.sin6_addr
= msg
->target
,
1847 .sin6
.sin6_family
= AF_INET6
,
1850 vxlan_ip_miss(dev
, &ipa
);
1855 return NETDEV_TX_OK
;
1859 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1861 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1862 struct neighbour
*n
;
1864 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1868 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1873 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1876 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1877 if (!n
&& (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) {
1878 union vxlan_addr ipa
= {
1879 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1880 .sin
.sin_family
= AF_INET
,
1883 vxlan_ip_miss(dev
, &ipa
);
1889 #if IS_ENABLED(CONFIG_IPV6)
1892 struct ipv6hdr
*pip6
;
1894 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1896 pip6
= ipv6_hdr(skb
);
1897 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1898 if (!n
&& (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) {
1899 union vxlan_addr ipa
= {
1900 .sin6
.sin6_addr
= pip6
->daddr
,
1901 .sin6
.sin6_family
= AF_INET6
,
1904 vxlan_ip_miss(dev
, &ipa
);
1918 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1920 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1922 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1931 static void vxlan_build_gbp_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1932 struct vxlan_metadata
*md
)
1934 struct vxlanhdr_gbp
*gbp
;
1939 gbp
= (struct vxlanhdr_gbp
*)vxh
;
1940 vxh
->vx_flags
|= VXLAN_HF_GBP
;
1942 if (md
->gbp
& VXLAN_GBP_DONT_LEARN
)
1943 gbp
->dont_learn
= 1;
1945 if (md
->gbp
& VXLAN_GBP_POLICY_APPLIED
)
1946 gbp
->policy_applied
= 1;
1948 gbp
->policy_id
= htons(md
->gbp
& VXLAN_GBP_ID_MASK
);
1951 static int vxlan_build_gpe_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1954 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)vxh
;
1956 gpe
->np_applied
= 1;
1959 case htons(ETH_P_IP
):
1960 gpe
->next_protocol
= VXLAN_GPE_NP_IPV4
;
1962 case htons(ETH_P_IPV6
):
1963 gpe
->next_protocol
= VXLAN_GPE_NP_IPV6
;
1965 case htons(ETH_P_TEB
):
1966 gpe
->next_protocol
= VXLAN_GPE_NP_ETHERNET
;
1969 return -EPFNOSUPPORT
;
1972 static int vxlan_build_skb(struct sk_buff
*skb
, struct dst_entry
*dst
,
1973 int iphdr_len
, __be32 vni
,
1974 struct vxlan_metadata
*md
, u32 vxflags
,
1977 struct vxlanhdr
*vxh
;
1980 int type
= udp_sum
? SKB_GSO_UDP_TUNNEL_CSUM
: SKB_GSO_UDP_TUNNEL
;
1981 __be16 inner_protocol
= htons(ETH_P_TEB
);
1983 if ((vxflags
& VXLAN_F_REMCSUM_TX
) &&
1984 skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1985 int csum_start
= skb_checksum_start_offset(skb
);
1987 if (csum_start
<= VXLAN_MAX_REMCSUM_START
&&
1988 !(csum_start
& VXLAN_RCO_SHIFT_MASK
) &&
1989 (skb
->csum_offset
== offsetof(struct udphdr
, check
) ||
1990 skb
->csum_offset
== offsetof(struct tcphdr
, check
)))
1991 type
|= SKB_GSO_TUNNEL_REMCSUM
;
1994 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1995 + VXLAN_HLEN
+ iphdr_len
;
1997 /* Need space for new headers (invalidates iph ptr) */
1998 err
= skb_cow_head(skb
, min_headroom
);
2002 err
= iptunnel_handle_offloads(skb
, type
);
2006 vxh
= __skb_push(skb
, sizeof(*vxh
));
2007 vxh
->vx_flags
= VXLAN_HF_VNI
;
2008 vxh
->vx_vni
= vxlan_vni_field(vni
);
2010 if (type
& SKB_GSO_TUNNEL_REMCSUM
) {
2013 start
= skb_checksum_start_offset(skb
) - sizeof(struct vxlanhdr
);
2014 vxh
->vx_vni
|= vxlan_compute_rco(start
, skb
->csum_offset
);
2015 vxh
->vx_flags
|= VXLAN_HF_RCO
;
2017 if (!skb_is_gso(skb
)) {
2018 skb
->ip_summed
= CHECKSUM_NONE
;
2019 skb
->encapsulation
= 0;
2023 if (vxflags
& VXLAN_F_GBP
)
2024 vxlan_build_gbp_hdr(vxh
, vxflags
, md
);
2025 if (vxflags
& VXLAN_F_GPE
) {
2026 err
= vxlan_build_gpe_hdr(vxh
, vxflags
, skb
->protocol
);
2029 inner_protocol
= skb
->protocol
;
2032 skb_set_inner_protocol(skb
, inner_protocol
);
2036 static struct rtable
*vxlan_get_route(struct vxlan_dev
*vxlan
, struct net_device
*dev
,
2037 struct vxlan_sock
*sock4
,
2038 struct sk_buff
*skb
, int oif
, u8 tos
,
2039 __be32 daddr
, __be32
*saddr
, __be16 dport
, __be16 sport
,
2040 struct dst_cache
*dst_cache
,
2041 const struct ip_tunnel_info
*info
)
2043 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
2044 struct rtable
*rt
= NULL
;
2048 return ERR_PTR(-EIO
);
2053 rt
= dst_cache_get_ip4(dst_cache
, saddr
);
2058 memset(&fl4
, 0, sizeof(fl4
));
2059 fl4
.flowi4_oif
= oif
;
2060 fl4
.flowi4_tos
= RT_TOS(tos
);
2061 fl4
.flowi4_mark
= skb
->mark
;
2062 fl4
.flowi4_proto
= IPPROTO_UDP
;
2065 fl4
.fl4_dport
= dport
;
2066 fl4
.fl4_sport
= sport
;
2068 rt
= ip_route_output_key(vxlan
->net
, &fl4
);
2069 if (likely(!IS_ERR(rt
))) {
2070 if (rt
->dst
.dev
== dev
) {
2071 netdev_dbg(dev
, "circular route to %pI4\n", &daddr
);
2073 return ERR_PTR(-ELOOP
);
2078 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
.saddr
);
2080 netdev_dbg(dev
, "no route to %pI4\n", &daddr
);
2081 return ERR_PTR(-ENETUNREACH
);
2086 #if IS_ENABLED(CONFIG_IPV6)
2087 static struct dst_entry
*vxlan6_get_route(struct vxlan_dev
*vxlan
,
2088 struct net_device
*dev
,
2089 struct vxlan_sock
*sock6
,
2090 struct sk_buff
*skb
, int oif
, u8 tos
,
2092 const struct in6_addr
*daddr
,
2093 struct in6_addr
*saddr
,
2094 __be16 dport
, __be16 sport
,
2095 struct dst_cache
*dst_cache
,
2096 const struct ip_tunnel_info
*info
)
2098 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
2099 struct dst_entry
*ndst
;
2104 return ERR_PTR(-EIO
);
2109 ndst
= dst_cache_get_ip6(dst_cache
, saddr
);
2114 memset(&fl6
, 0, sizeof(fl6
));
2115 fl6
.flowi6_oif
= oif
;
2118 fl6
.flowlabel
= ip6_make_flowinfo(RT_TOS(tos
), label
);
2119 fl6
.flowi6_mark
= skb
->mark
;
2120 fl6
.flowi6_proto
= IPPROTO_UDP
;
2121 fl6
.fl6_dport
= dport
;
2122 fl6
.fl6_sport
= sport
;
2124 err
= ipv6_stub
->ipv6_dst_lookup(vxlan
->net
,
2127 if (unlikely(err
< 0)) {
2128 netdev_dbg(dev
, "no route to %pI6\n", daddr
);
2129 return ERR_PTR(-ENETUNREACH
);
2132 if (unlikely(ndst
->dev
== dev
)) {
2133 netdev_dbg(dev
, "circular route to %pI6\n", daddr
);
2135 return ERR_PTR(-ELOOP
);
2140 dst_cache_set_ip6(dst_cache
, ndst
, saddr
);
2145 /* Bypass encapsulation if the destination is local */
2146 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
2147 struct vxlan_dev
*dst_vxlan
, __be32 vni
)
2149 struct pcpu_sw_netstats
*tx_stats
, *rx_stats
;
2150 union vxlan_addr loopback
;
2151 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
2152 struct net_device
*dev
= skb
->dev
;
2155 tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
2156 rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
2157 skb
->pkt_type
= PACKET_HOST
;
2158 skb
->encapsulation
= 0;
2159 skb
->dev
= dst_vxlan
->dev
;
2160 __skb_pull(skb
, skb_network_offset(skb
));
2162 if (remote_ip
->sa
.sa_family
== AF_INET
) {
2163 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2164 loopback
.sa
.sa_family
= AF_INET
;
2165 #if IS_ENABLED(CONFIG_IPV6)
2167 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
2168 loopback
.sa
.sa_family
= AF_INET6
;
2172 if (dst_vxlan
->cfg
.flags
& VXLAN_F_LEARN
)
2173 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
, 0,
2176 u64_stats_update_begin(&tx_stats
->syncp
);
2177 tx_stats
->tx_packets
++;
2178 tx_stats
->tx_bytes
+= len
;
2179 u64_stats_update_end(&tx_stats
->syncp
);
2181 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
2182 u64_stats_update_begin(&rx_stats
->syncp
);
2183 rx_stats
->rx_packets
++;
2184 rx_stats
->rx_bytes
+= len
;
2185 u64_stats_update_end(&rx_stats
->syncp
);
2187 dev
->stats
.rx_dropped
++;
2191 static int encap_bypass_if_local(struct sk_buff
*skb
, struct net_device
*dev
,
2192 struct vxlan_dev
*vxlan
,
2193 union vxlan_addr
*daddr
,
2194 __be16 dst_port
, int dst_ifindex
, __be32 vni
,
2195 struct dst_entry
*dst
,
2198 #if IS_ENABLED(CONFIG_IPV6)
2199 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2200 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2201 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2203 BUILD_BUG_ON(RTCF_LOCAL
!= RTF_LOCAL
);
2205 /* Bypass encapsulation if the destination is local */
2206 if (rt_flags
& RTCF_LOCAL
&&
2207 !(rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
2208 struct vxlan_dev
*dst_vxlan
;
2211 dst_vxlan
= vxlan_find_vni(vxlan
->net
, dst_ifindex
, vni
,
2212 daddr
->sa
.sa_family
, dst_port
,
2215 dev
->stats
.tx_errors
++;
2220 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
, vni
);
2227 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
2228 __be32 default_vni
, struct vxlan_rdst
*rdst
,
2231 struct dst_cache
*dst_cache
;
2232 struct ip_tunnel_info
*info
;
2233 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2234 const struct iphdr
*old_iph
= ip_hdr(skb
);
2235 union vxlan_addr
*dst
;
2236 union vxlan_addr remote_ip
, local_ip
;
2237 struct vxlan_metadata _md
;
2238 struct vxlan_metadata
*md
= &_md
;
2239 __be16 src_port
= 0, dst_port
;
2240 struct dst_entry
*ndst
= NULL
;
2245 u32 flags
= vxlan
->cfg
.flags
;
2246 bool udp_sum
= false;
2247 bool xnet
= !net_eq(vxlan
->net
, dev_net(vxlan
->dev
));
2249 info
= skb_tunnel_info(skb
);
2252 dst
= &rdst
->remote_ip
;
2253 if (vxlan_addr_any(dst
)) {
2255 /* short-circuited back to local bridge */
2256 vxlan_encap_bypass(skb
, vxlan
, vxlan
, default_vni
);
2262 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->cfg
.dst_port
;
2263 vni
= (rdst
->remote_vni
) ? : default_vni
;
2264 ifindex
= rdst
->remote_ifindex
;
2265 local_ip
= vxlan
->cfg
.saddr
;
2266 dst_cache
= &rdst
->dst_cache
;
2267 md
->gbp
= skb
->mark
;
2268 ttl
= vxlan
->cfg
.ttl
;
2269 if (!ttl
&& vxlan_addr_multicast(dst
))
2272 tos
= vxlan
->cfg
.tos
;
2274 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
2276 if (dst
->sa
.sa_family
== AF_INET
)
2277 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM_TX
);
2279 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
);
2280 label
= vxlan
->cfg
.label
;
2283 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2287 remote_ip
.sa
.sa_family
= ip_tunnel_info_af(info
);
2288 if (remote_ip
.sa
.sa_family
== AF_INET
) {
2289 remote_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.dst
;
2290 local_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.src
;
2292 remote_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.dst
;
2293 local_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.src
;
2296 dst_port
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2297 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2299 dst_cache
= &info
->dst_cache
;
2300 if (info
->options_len
)
2301 md
= ip_tunnel_info_opts(info
);
2302 ttl
= info
->key
.ttl
;
2303 tos
= info
->key
.tos
;
2304 label
= info
->key
.label
;
2305 udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
2307 src_port
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2308 vxlan
->cfg
.port_max
, true);
2311 if (dst
->sa
.sa_family
== AF_INET
) {
2312 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2316 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
, ifindex
, tos
,
2317 dst
->sin
.sin_addr
.s_addr
,
2318 &local_ip
.sin
.sin_addr
.s_addr
,
2326 if (fan_has_map(&vxlan
->fan
) && rt
->rt_flags
& RTCF_LOCAL
) {
2327 netdev_dbg(dev
, "discard fan to localhost %pI4\n",
2328 &dst
->sin
.sin_addr
.s_addr
);
2333 /* Bypass encapsulation if the destination is local */
2335 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2336 dst_port
, ifindex
, vni
,
2337 &rt
->dst
, rt
->rt_flags
);
2340 } else if (info
->key
.tun_flags
& TUNNEL_DONT_FRAGMENT
) {
2345 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2346 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
2347 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct iphdr
),
2348 vni
, md
, flags
, udp_sum
);
2352 udp_tunnel_xmit_skb(rt
, sock4
->sock
->sk
, skb
, local_ip
.sin
.sin_addr
.s_addr
,
2353 dst
->sin
.sin_addr
.s_addr
, tos
, ttl
, df
,
2354 src_port
, dst_port
, xnet
, !udp_sum
);
2355 #if IS_ENABLED(CONFIG_IPV6)
2357 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2359 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
, ifindex
, tos
,
2360 label
, &dst
->sin6
.sin6_addr
,
2361 &local_ip
.sin6
.sin6_addr
,
2365 err
= PTR_ERR(ndst
);
2371 u32 rt6i_flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
2373 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2374 dst_port
, ifindex
, vni
,
2380 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2381 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
2382 skb_scrub_packet(skb
, xnet
);
2383 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct ipv6hdr
),
2384 vni
, md
, flags
, udp_sum
);
2388 udp_tunnel6_xmit_skb(ndst
, sock6
->sock
->sk
, skb
, dev
,
2389 &local_ip
.sin6
.sin6_addr
,
2390 &dst
->sin6
.sin6_addr
, tos
, ttl
,
2391 label
, src_port
, dst_port
, !udp_sum
);
2399 dev
->stats
.tx_dropped
++;
2406 dev
->stats
.collisions
++;
2407 else if (err
== -ENETUNREACH
)
2408 dev
->stats
.tx_carrier_errors
++;
2410 dev
->stats
.tx_errors
++;
2415 /* Transmit local packets over Vxlan
2417 * Outer IP header inherits ECN and DF from inner header.
2418 * Outer UDP destination is the VXLAN assigned port.
2419 * source port is based on hash of flow
2421 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
2423 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2424 struct vxlan_rdst
*rdst
, *fdst
= NULL
;
2425 const struct ip_tunnel_info
*info
;
2426 bool did_rsc
= false;
2427 struct vxlan_fdb
*f
;
2431 info
= skb_tunnel_info(skb
);
2433 skb_reset_mac_header(skb
);
2435 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) {
2436 if (info
&& info
->mode
& IP_TUNNEL_INFO_BRIDGE
&&
2437 info
->mode
& IP_TUNNEL_INFO_TX
) {
2438 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2440 if (info
&& info
->mode
& IP_TUNNEL_INFO_TX
)
2441 vxlan_xmit_one(skb
, dev
, vni
, NULL
, false);
2444 return NETDEV_TX_OK
;
2448 if (vxlan
->cfg
.flags
& VXLAN_F_PROXY
) {
2450 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
2451 return arp_reduce(dev
, skb
, vni
);
2452 #if IS_ENABLED(CONFIG_IPV6)
2453 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
&&
2454 pskb_may_pull(skb
, sizeof(struct ipv6hdr
) +
2455 sizeof(struct nd_msg
)) &&
2456 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
2457 struct nd_msg
*m
= (struct nd_msg
*)(ipv6_hdr(skb
) + 1);
2459 if (m
->icmph
.icmp6_code
== 0 &&
2460 m
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
)
2461 return neigh_reduce(dev
, skb
, vni
);
2466 if (fan_has_map(&vxlan
->fan
)) {
2467 struct vxlan_rdst fan_rdst
;
2469 netdev_dbg(vxlan
->dev
, "vxlan_xmit p %x d %pM\n",
2470 eth
->h_proto
, eth
->h_dest
);
2471 if (vxlan_fan_build_rdst(vxlan
, skb
, &fan_rdst
)) {
2472 dev
->stats
.tx_dropped
++;
2474 return NETDEV_TX_OK
;
2476 vxlan_xmit_one(skb
, dev
, vni
, &fan_rdst
, 0);
2477 return NETDEV_TX_OK
;
2481 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2484 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->cfg
.flags
& VXLAN_F_RSC
) &&
2485 (ntohs(eth
->h_proto
) == ETH_P_IP
||
2486 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
2487 did_rsc
= route_shortcircuit(dev
, skb
);
2489 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2493 f
= vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2495 if ((vxlan
->cfg
.flags
& VXLAN_F_L2MISS
) &&
2496 !is_multicast_ether_addr(eth
->h_dest
))
2497 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
2499 dev
->stats
.tx_dropped
++;
2501 return NETDEV_TX_OK
;
2505 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
2506 struct sk_buff
*skb1
;
2512 skb1
= skb_clone(skb
, GFP_ATOMIC
);
2514 vxlan_xmit_one(skb1
, dev
, vni
, rdst
, did_rsc
);
2518 vxlan_xmit_one(skb
, dev
, vni
, fdst
, did_rsc
);
2521 return NETDEV_TX_OK
;
2524 /* Walk the forwarding table and purge stale entries */
2525 static void vxlan_cleanup(unsigned long arg
)
2527 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
2528 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
2531 if (!netif_running(vxlan
->dev
))
2534 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2535 struct hlist_node
*p
, *n
;
2537 spin_lock_bh(&vxlan
->hash_lock
);
2538 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2540 = container_of(p
, struct vxlan_fdb
, hlist
);
2541 unsigned long timeout
;
2543 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
2546 if (f
->flags
& NTF_EXT_LEARNED
)
2549 timeout
= f
->used
+ vxlan
->cfg
.age_interval
* HZ
;
2550 if (time_before_eq(timeout
, jiffies
)) {
2551 netdev_dbg(vxlan
->dev
,
2552 "garbage collect %pM\n",
2554 f
->state
= NUD_STALE
;
2555 vxlan_fdb_destroy(vxlan
, f
);
2556 } else if (time_before(timeout
, next_timer
))
2557 next_timer
= timeout
;
2559 spin_unlock_bh(&vxlan
->hash_lock
);
2562 mod_timer(&vxlan
->age_timer
, next_timer
);
2565 static void vxlan_vs_del_dev(struct vxlan_dev
*vxlan
)
2567 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2569 spin_lock(&vn
->sock_lock
);
2570 hlist_del_init_rcu(&vxlan
->hlist4
.hlist
);
2571 #if IS_ENABLED(CONFIG_IPV6)
2572 hlist_del_init_rcu(&vxlan
->hlist6
.hlist
);
2574 spin_unlock(&vn
->sock_lock
);
2577 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
,
2578 struct vxlan_dev_node
*node
)
2580 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2581 __be32 vni
= vxlan
->default_dst
.remote_vni
;
2583 node
->vxlan
= vxlan
;
2584 spin_lock(&vn
->sock_lock
);
2585 hlist_add_head_rcu(&node
->hlist
, vni_head(vs
, vni
));
2586 spin_unlock(&vn
->sock_lock
);
2589 /* Setup stats when device is created */
2590 static int vxlan_init(struct net_device
*dev
)
2592 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
2599 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
, __be32 vni
)
2601 struct vxlan_fdb
*f
;
2603 spin_lock_bh(&vxlan
->hash_lock
);
2604 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2606 vxlan_fdb_destroy(vxlan
, f
);
2607 spin_unlock_bh(&vxlan
->hash_lock
);
2610 static void vxlan_uninit(struct net_device
*dev
)
2612 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2614 vxlan_fdb_delete_default(vxlan
, vxlan
->cfg
.vni
);
2616 free_percpu(dev
->tstats
);
2619 /* Start ageing timer and join group when device is brought up */
2620 static int vxlan_open(struct net_device
*dev
)
2622 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2625 ret
= vxlan_sock_add(vxlan
);
2629 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
)) {
2630 ret
= vxlan_igmp_join(vxlan
);
2631 if (ret
== -EADDRINUSE
)
2634 vxlan_sock_release(vxlan
);
2639 if (vxlan
->cfg
.age_interval
)
2640 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2645 /* Purge the forwarding table */
2646 static void vxlan_flush(struct vxlan_dev
*vxlan
, bool do_all
)
2650 spin_lock_bh(&vxlan
->hash_lock
);
2651 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2652 struct hlist_node
*p
, *n
;
2653 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2655 = container_of(p
, struct vxlan_fdb
, hlist
);
2656 if (!do_all
&& (f
->state
& (NUD_PERMANENT
| NUD_NOARP
)))
2658 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2659 if (!is_zero_ether_addr(f
->eth_addr
))
2660 vxlan_fdb_destroy(vxlan
, f
);
2663 spin_unlock_bh(&vxlan
->hash_lock
);
2666 /* Cleanup timer and forwarding table on shutdown */
2667 static int vxlan_stop(struct net_device
*dev
)
2669 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2670 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2673 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2674 !vxlan_group_used(vn
, vxlan
))
2675 ret
= vxlan_igmp_leave(vxlan
);
2677 del_timer_sync(&vxlan
->age_timer
);
2679 vxlan_flush(vxlan
, false);
2680 vxlan_sock_release(vxlan
);
2685 /* Stub, nothing needs to be done. */
2686 static void vxlan_set_multicast_list(struct net_device
*dev
)
2690 static int vxlan_change_mtu(struct net_device
*dev
, int new_mtu
)
2692 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2693 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2694 struct net_device
*lowerdev
= __dev_get_by_index(vxlan
->net
,
2695 dst
->remote_ifindex
);
2696 bool use_ipv6
= !!(vxlan
->cfg
.flags
& VXLAN_F_IPV6
);
2698 /* This check is different than dev->max_mtu, because it looks at
2699 * the lowerdev->mtu, rather than the static dev->max_mtu
2702 int max_mtu
= lowerdev
->mtu
-
2703 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2704 if (new_mtu
> max_mtu
)
2712 static int vxlan_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
2714 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2715 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
2716 __be16 sport
, dport
;
2718 sport
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2719 vxlan
->cfg
.port_max
, true);
2720 dport
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2722 if (ip_tunnel_info_af(info
) == AF_INET
) {
2723 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2726 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
, 0, info
->key
.tos
,
2727 info
->key
.u
.ipv4
.dst
,
2728 &info
->key
.u
.ipv4
.src
, dport
, sport
,
2729 &info
->dst_cache
, info
);
2734 #if IS_ENABLED(CONFIG_IPV6)
2735 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2736 struct dst_entry
*ndst
;
2738 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
, 0, info
->key
.tos
,
2739 info
->key
.label
, &info
->key
.u
.ipv6
.dst
,
2740 &info
->key
.u
.ipv6
.src
, dport
, sport
,
2741 &info
->dst_cache
, info
);
2743 return PTR_ERR(ndst
);
2745 #else /* !CONFIG_IPV6 */
2746 return -EPFNOSUPPORT
;
2749 info
->key
.tp_src
= sport
;
2750 info
->key
.tp_dst
= dport
;
2754 static const struct net_device_ops vxlan_netdev_ether_ops
= {
2755 .ndo_init
= vxlan_init
,
2756 .ndo_uninit
= vxlan_uninit
,
2757 .ndo_open
= vxlan_open
,
2758 .ndo_stop
= vxlan_stop
,
2759 .ndo_start_xmit
= vxlan_xmit
,
2760 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2761 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2762 .ndo_change_mtu
= vxlan_change_mtu
,
2763 .ndo_validate_addr
= eth_validate_addr
,
2764 .ndo_set_mac_address
= eth_mac_addr
,
2765 .ndo_fdb_add
= vxlan_fdb_add
,
2766 .ndo_fdb_del
= vxlan_fdb_delete
,
2767 .ndo_fdb_dump
= vxlan_fdb_dump
,
2768 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2771 static const struct net_device_ops vxlan_netdev_raw_ops
= {
2772 .ndo_init
= vxlan_init
,
2773 .ndo_uninit
= vxlan_uninit
,
2774 .ndo_open
= vxlan_open
,
2775 .ndo_stop
= vxlan_stop
,
2776 .ndo_start_xmit
= vxlan_xmit
,
2777 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2778 .ndo_change_mtu
= vxlan_change_mtu
,
2779 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2782 /* Info for udev, that this is a virtual tunnel endpoint */
2783 static struct device_type vxlan_type
= {
2787 /* Calls the ndo_udp_tunnel_add of the caller in order to
2788 * supply the listening VXLAN udp ports. Callers are expected
2789 * to implement the ndo_udp_tunnel_add.
2791 static void vxlan_push_rx_ports(struct net_device
*dev
)
2793 struct vxlan_sock
*vs
;
2794 struct net
*net
= dev_net(dev
);
2795 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2798 spin_lock(&vn
->sock_lock
);
2799 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2800 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
)
2801 udp_tunnel_push_rx_port(dev
, vs
->sock
,
2802 (vs
->flags
& VXLAN_F_GPE
) ?
2803 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2804 UDP_TUNNEL_TYPE_VXLAN
);
2806 spin_unlock(&vn
->sock_lock
);
2809 /* Initialize the device structure. */
2810 static void vxlan_setup(struct net_device
*dev
)
2812 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2815 eth_hw_addr_random(dev
);
2818 dev
->needs_free_netdev
= true;
2819 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2821 dev
->features
|= NETIF_F_LLTX
;
2822 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2823 dev
->features
|= NETIF_F_RXCSUM
;
2824 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2826 dev
->vlan_features
= dev
->features
;
2827 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2828 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2829 netif_keep_dst(dev
);
2830 dev
->priv_flags
|= IFF_NO_QUEUE
;
2832 /* MTU range: 68 - 65535 */
2833 dev
->min_mtu
= ETH_MIN_MTU
;
2834 dev
->max_mtu
= ETH_MAX_MTU
;
2836 INIT_LIST_HEAD(&vxlan
->next
);
2837 spin_lock_init(&vxlan
->hash_lock
);
2839 init_timer_deferrable(&vxlan
->age_timer
);
2840 vxlan
->age_timer
.function
= vxlan_cleanup
;
2841 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2845 gro_cells_init(&vxlan
->gro_cells
, dev
);
2847 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2848 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2850 INIT_LIST_HEAD(&vxlan
->fan
.fan_maps
);
2853 static void vxlan_ether_setup(struct net_device
*dev
)
2855 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
2856 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2857 dev
->netdev_ops
= &vxlan_netdev_ether_ops
;
2860 static void vxlan_raw_setup(struct net_device
*dev
)
2862 dev
->header_ops
= NULL
;
2863 dev
->type
= ARPHRD_NONE
;
2864 dev
->hard_header_len
= 0;
2866 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
2867 dev
->netdev_ops
= &vxlan_netdev_raw_ops
;
2870 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2871 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2872 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2873 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2874 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2875 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2876 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2877 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2878 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2879 [IFLA_VXLAN_LABEL
] = { .type
= NLA_U32
},
2880 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2881 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2882 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2883 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2884 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2885 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2886 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2887 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2888 [IFLA_VXLAN_COLLECT_METADATA
] = { .type
= NLA_U8
},
2889 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2890 [IFLA_VXLAN_UDP_CSUM
] = { .type
= NLA_U8
},
2891 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
2892 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
2893 [IFLA_VXLAN_REMCSUM_TX
] = { .type
= NLA_U8
},
2894 [IFLA_VXLAN_REMCSUM_RX
] = { .type
= NLA_U8
},
2895 [IFLA_VXLAN_GBP
] = { .type
= NLA_FLAG
, },
2896 [IFLA_VXLAN_GPE
] = { .type
= NLA_FLAG
, },
2897 [IFLA_VXLAN_REMCSUM_NOPARTIAL
] = { .type
= NLA_FLAG
},
2900 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
2901 struct netlink_ext_ack
*extack
)
2903 if (tb
[IFLA_ADDRESS
]) {
2904 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2905 pr_debug("invalid link address (not ethernet)\n");
2909 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2910 pr_debug("invalid all zero ethernet address\n");
2911 return -EADDRNOTAVAIL
;
2916 u32 mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2918 if (mtu
< ETH_MIN_MTU
|| mtu
> ETH_MAX_MTU
)
2925 if (data
[IFLA_VXLAN_ID
]) {
2926 u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2928 if (id
>= VXLAN_N_VID
)
2932 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2933 const struct ifla_vxlan_port_range
*p
2934 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2936 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2937 pr_debug("port range %u .. %u not valid\n",
2938 ntohs(p
->low
), ntohs(p
->high
));
2946 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2947 struct ethtool_drvinfo
*drvinfo
)
2949 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2950 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2953 static const struct ethtool_ops vxlan_ethtool_ops
= {
2954 .get_drvinfo
= vxlan_get_drvinfo
,
2955 .get_link
= ethtool_op_get_link
,
2958 static struct socket
*vxlan_create_sock(struct net
*net
, bool ipv6
,
2959 __be16 port
, u32 flags
)
2961 struct socket
*sock
;
2962 struct udp_port_cfg udp_conf
;
2965 memset(&udp_conf
, 0, sizeof(udp_conf
));
2968 udp_conf
.family
= AF_INET6
;
2969 udp_conf
.use_udp6_rx_checksums
=
2970 !(flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
);
2971 udp_conf
.ipv6_v6only
= 1;
2973 udp_conf
.family
= AF_INET
;
2976 udp_conf
.local_udp_port
= port
;
2978 /* Open UDP socket */
2979 err
= udp_sock_create(net
, &udp_conf
, &sock
);
2981 return ERR_PTR(err
);
2986 /* Create new listen socket if needed */
2987 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, bool ipv6
,
2988 __be16 port
, u32 flags
)
2990 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2991 struct vxlan_sock
*vs
;
2992 struct socket
*sock
;
2994 struct udp_tunnel_sock_cfg tunnel_cfg
;
2996 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
);
2998 return ERR_PTR(-ENOMEM
);
3000 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
3001 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
3003 sock
= vxlan_create_sock(net
, ipv6
, port
, flags
);
3006 return ERR_CAST(sock
);
3010 refcount_set(&vs
->refcnt
, 1);
3011 vs
->flags
= (flags
& VXLAN_F_RCV_FLAGS
);
3013 spin_lock(&vn
->sock_lock
);
3014 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
3015 udp_tunnel_notify_add_rx_port(sock
,
3016 (vs
->flags
& VXLAN_F_GPE
) ?
3017 UDP_TUNNEL_TYPE_VXLAN_GPE
:
3018 UDP_TUNNEL_TYPE_VXLAN
);
3019 spin_unlock(&vn
->sock_lock
);
3021 /* Mark socket as an encapsulation socket. */
3022 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
3023 tunnel_cfg
.sk_user_data
= vs
;
3024 tunnel_cfg
.encap_type
= 1;
3025 tunnel_cfg
.encap_rcv
= vxlan_rcv
;
3026 tunnel_cfg
.encap_destroy
= NULL
;
3027 tunnel_cfg
.gro_receive
= vxlan_gro_receive
;
3028 tunnel_cfg
.gro_complete
= vxlan_gro_complete
;
3030 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
3035 static int __vxlan_sock_add(struct vxlan_dev
*vxlan
, bool ipv6
)
3037 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
3038 struct vxlan_sock
*vs
= NULL
;
3039 struct vxlan_dev_node
*node
;
3041 if (!vxlan
->cfg
.no_share
) {
3042 spin_lock(&vn
->sock_lock
);
3043 vs
= vxlan_find_sock(vxlan
->net
, ipv6
? AF_INET6
: AF_INET
,
3044 vxlan
->cfg
.dst_port
, vxlan
->cfg
.flags
);
3045 if (vs
&& !refcount_inc_not_zero(&vs
->refcnt
)) {
3046 spin_unlock(&vn
->sock_lock
);
3049 spin_unlock(&vn
->sock_lock
);
3052 vs
= vxlan_socket_create(vxlan
->net
, ipv6
,
3053 vxlan
->cfg
.dst_port
, vxlan
->cfg
.flags
);
3056 #if IS_ENABLED(CONFIG_IPV6)
3058 rcu_assign_pointer(vxlan
->vn6_sock
, vs
);
3059 node
= &vxlan
->hlist6
;
3063 rcu_assign_pointer(vxlan
->vn4_sock
, vs
);
3064 node
= &vxlan
->hlist4
;
3066 vxlan_vs_add_dev(vs
, vxlan
, node
);
3070 static int vxlan_sock_add(struct vxlan_dev
*vxlan
)
3072 bool metadata
= vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
;
3073 bool ipv6
= vxlan
->cfg
.flags
& VXLAN_F_IPV6
|| metadata
;
3074 bool ipv4
= !ipv6
|| metadata
;
3077 RCU_INIT_POINTER(vxlan
->vn4_sock
, NULL
);
3078 #if IS_ENABLED(CONFIG_IPV6)
3079 RCU_INIT_POINTER(vxlan
->vn6_sock
, NULL
);
3081 ret
= __vxlan_sock_add(vxlan
, true);
3082 if (ret
< 0 && ret
!= -EAFNOSUPPORT
)
3087 ret
= __vxlan_sock_add(vxlan
, false);
3089 vxlan_sock_release(vxlan
);
3093 static int vxlan_config_validate(struct net
*src_net
, struct vxlan_config
*conf
,
3094 struct net_device
**lower
,
3095 struct vxlan_dev
*old
)
3097 struct vxlan_net
*vn
= net_generic(src_net
, vxlan_net_id
);
3098 struct vxlan_dev
*tmp
;
3099 bool use_ipv6
= false;
3101 if (conf
->flags
& VXLAN_F_GPE
) {
3102 /* For now, allow GPE only together with
3103 * COLLECT_METADATA. This can be relaxed later; in such
3104 * case, the other side of the PtP link will have to be
3107 if ((conf
->flags
& ~VXLAN_F_ALLOWED_GPE
) ||
3108 !(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
3113 if (!conf
->remote_ip
.sa
.sa_family
&& !conf
->saddr
.sa
.sa_family
) {
3114 /* Unless IPv6 is explicitly requested, assume IPv4 */
3115 conf
->remote_ip
.sa
.sa_family
= AF_INET
;
3116 conf
->saddr
.sa
.sa_family
= AF_INET
;
3117 } else if (!conf
->remote_ip
.sa
.sa_family
) {
3118 conf
->remote_ip
.sa
.sa_family
= conf
->saddr
.sa
.sa_family
;
3119 } else if (!conf
->saddr
.sa
.sa_family
) {
3120 conf
->saddr
.sa
.sa_family
= conf
->remote_ip
.sa
.sa_family
;
3123 if (conf
->saddr
.sa
.sa_family
!= conf
->remote_ip
.sa
.sa_family
)
3126 if (vxlan_addr_multicast(&conf
->saddr
))
3129 if (conf
->saddr
.sa
.sa_family
== AF_INET6
) {
3130 if (!IS_ENABLED(CONFIG_IPV6
))
3131 return -EPFNOSUPPORT
;
3133 conf
->flags
|= VXLAN_F_IPV6
;
3135 if (!(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
3137 ipv6_addr_type(&conf
->saddr
.sin6
.sin6_addr
);
3139 ipv6_addr_type(&conf
->remote_ip
.sin6
.sin6_addr
);
3141 if (local_type
& IPV6_ADDR_LINKLOCAL
) {
3142 if (!(remote_type
& IPV6_ADDR_LINKLOCAL
) &&
3143 (remote_type
!= IPV6_ADDR_ANY
))
3146 conf
->flags
|= VXLAN_F_IPV6_LINKLOCAL
;
3149 (IPV6_ADDR_UNICAST
| IPV6_ADDR_LINKLOCAL
))
3152 conf
->flags
&= ~VXLAN_F_IPV6_LINKLOCAL
;
3157 if (conf
->label
&& !use_ipv6
)
3160 if (conf
->remote_ifindex
) {
3161 struct net_device
*lowerdev
;
3163 lowerdev
= __dev_get_by_index(src_net
, conf
->remote_ifindex
);
3167 #if IS_ENABLED(CONFIG_IPV6)
3169 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
3170 if (idev
&& idev
->cnf
.disable_ipv6
)
3177 if (vxlan_addr_multicast(&conf
->remote_ip
))
3180 #if IS_ENABLED(CONFIG_IPV6)
3181 if (conf
->flags
& VXLAN_F_IPV6_LINKLOCAL
)
3188 if (!conf
->dst_port
) {
3189 if (conf
->flags
& VXLAN_F_GPE
)
3190 conf
->dst_port
= htons(4790); /* IANA VXLAN-GPE port */
3192 conf
->dst_port
= htons(vxlan_port
);
3195 if (!conf
->age_interval
)
3196 conf
->age_interval
= FDB_AGE_DEFAULT
;
3198 list_for_each_entry(tmp
, &vn
->vxlan_list
, next
) {
3202 if (tmp
->cfg
.vni
!= conf
->vni
)
3204 if (tmp
->cfg
.dst_port
!= conf
->dst_port
)
3206 if ((tmp
->cfg
.flags
& (VXLAN_F_RCV_FLAGS
| VXLAN_F_IPV6
)) !=
3207 (conf
->flags
& (VXLAN_F_RCV_FLAGS
| VXLAN_F_IPV6
)))
3210 if ((conf
->flags
& VXLAN_F_IPV6_LINKLOCAL
) &&
3211 tmp
->cfg
.remote_ifindex
!= conf
->remote_ifindex
)
3220 static void vxlan_config_apply(struct net_device
*dev
,
3221 struct vxlan_config
*conf
,
3222 struct net_device
*lowerdev
,
3223 struct net
*src_net
,
3226 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3227 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3228 unsigned short needed_headroom
= ETH_HLEN
;
3229 bool use_ipv6
= !!(conf
->flags
& VXLAN_F_IPV6
);
3230 int max_mtu
= ETH_MAX_MTU
;
3233 if (conf
->flags
& VXLAN_F_GPE
)
3234 vxlan_raw_setup(dev
);
3236 vxlan_ether_setup(dev
);
3239 dev
->mtu
= conf
->mtu
;
3241 vxlan
->net
= src_net
;
3244 dst
->remote_vni
= conf
->vni
;
3246 memcpy(&dst
->remote_ip
, &conf
->remote_ip
, sizeof(conf
->remote_ip
));
3249 dst
->remote_ifindex
= conf
->remote_ifindex
;
3251 dev
->gso_max_size
= lowerdev
->gso_max_size
;
3252 dev
->gso_max_segs
= lowerdev
->gso_max_segs
;
3254 needed_headroom
= lowerdev
->hard_header_len
;
3256 max_mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
:
3260 if (dev
->mtu
> max_mtu
)
3263 if (use_ipv6
|| conf
->flags
& VXLAN_F_COLLECT_METADATA
)
3264 needed_headroom
+= VXLAN6_HEADROOM
;
3266 needed_headroom
+= VXLAN_HEADROOM
;
3267 dev
->needed_headroom
= needed_headroom
;
3269 memcpy(&vxlan
->cfg
, conf
, sizeof(*conf
));
3272 static int vxlan_dev_configure(struct net
*src_net
, struct net_device
*dev
,
3273 struct vxlan_config
*conf
,
3276 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3277 struct net_device
*lowerdev
;
3280 ret
= vxlan_config_validate(src_net
, conf
, &lowerdev
, vxlan
);
3284 vxlan_config_apply(dev
, conf
, lowerdev
, src_net
, changelink
);
3289 static int __vxlan_dev_create(struct net
*net
, struct net_device
*dev
,
3290 struct vxlan_config
*conf
)
3292 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3293 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3296 err
= vxlan_dev_configure(net
, dev
, conf
, false);
3300 dev
->ethtool_ops
= &vxlan_ethtool_ops
;
3302 /* create an fdb entry for a valid default destination */
3303 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
3304 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
3305 &vxlan
->default_dst
.remote_ip
,
3306 NUD_REACHABLE
| NUD_PERMANENT
,
3307 NLM_F_EXCL
| NLM_F_CREATE
,
3308 vxlan
->cfg
.dst_port
,
3309 vxlan
->default_dst
.remote_vni
,
3310 vxlan
->default_dst
.remote_vni
,
3311 vxlan
->default_dst
.remote_ifindex
,
3317 err
= register_netdevice(dev
);
3319 vxlan_fdb_delete_default(vxlan
, vxlan
->default_dst
.remote_vni
);
3323 list_add(&vxlan
->next
, &vn
->vxlan_list
);
3327 static int vxlan_nl2conf(struct nlattr
*tb
[], struct nlattr
*data
[],
3328 struct net_device
*dev
, struct vxlan_config
*conf
,
3331 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3334 memset(conf
, 0, sizeof(*conf
));
3336 /* if changelink operation, start with old existing cfg */
3338 memcpy(conf
, &vxlan
->cfg
, sizeof(*conf
));
3340 if (data
[IFLA_VXLAN_ID
]) {
3341 __be32 vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3343 if (changelink
&& (vni
!= conf
->vni
))
3345 conf
->vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3348 if (data
[IFLA_VXLAN_GROUP
]) {
3349 if (changelink
&& (conf
->remote_ip
.sa
.sa_family
!= AF_INET
))
3352 conf
->remote_ip
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_GROUP
]);
3353 conf
->remote_ip
.sa
.sa_family
= AF_INET
;
3354 } else if (data
[IFLA_VXLAN_GROUP6
]) {
3355 if (!IS_ENABLED(CONFIG_IPV6
))
3356 return -EPFNOSUPPORT
;
3358 if (changelink
&& (conf
->remote_ip
.sa
.sa_family
!= AF_INET6
))
3361 conf
->remote_ip
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_GROUP6
]);
3362 conf
->remote_ip
.sa
.sa_family
= AF_INET6
;
3365 if (data
[IFLA_VXLAN_FAN_MAP
]) {
3366 err
= vxlan_parse_fan_map(data
, vxlan
);
3371 if (data
[IFLA_VXLAN_LOCAL
]) {
3372 if (changelink
&& (conf
->saddr
.sa
.sa_family
!= AF_INET
))
3375 conf
->saddr
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_LOCAL
]);
3376 conf
->saddr
.sa
.sa_family
= AF_INET
;
3377 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
3378 if (!IS_ENABLED(CONFIG_IPV6
))
3379 return -EPFNOSUPPORT
;
3381 if (changelink
&& (conf
->saddr
.sa
.sa_family
!= AF_INET6
))
3384 /* TODO: respect scope id */
3385 conf
->saddr
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_LOCAL6
]);
3386 conf
->saddr
.sa
.sa_family
= AF_INET6
;
3389 if (data
[IFLA_VXLAN_LINK
])
3390 conf
->remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]);
3392 if (data
[IFLA_VXLAN_TOS
])
3393 conf
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
3395 if (data
[IFLA_VXLAN_TTL
])
3396 conf
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
3398 if (data
[IFLA_VXLAN_LABEL
])
3399 conf
->label
= nla_get_be32(data
[IFLA_VXLAN_LABEL
]) &
3400 IPV6_FLOWLABEL_MASK
;
3402 if (data
[IFLA_VXLAN_LEARNING
]) {
3403 if (nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
3404 conf
->flags
|= VXLAN_F_LEARN
;
3406 conf
->flags
&= ~VXLAN_F_LEARN
;
3407 } else if (!changelink
) {
3408 /* default to learn on a new device */
3409 conf
->flags
|= VXLAN_F_LEARN
;
3412 if (data
[IFLA_VXLAN_AGEING
]) {
3415 conf
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
3418 if (data
[IFLA_VXLAN_PROXY
]) {
3421 if (nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
3422 conf
->flags
|= VXLAN_F_PROXY
;
3425 if (data
[IFLA_VXLAN_RSC
]) {
3428 if (nla_get_u8(data
[IFLA_VXLAN_RSC
]))
3429 conf
->flags
|= VXLAN_F_RSC
;
3432 if (data
[IFLA_VXLAN_L2MISS
]) {
3435 if (nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
3436 conf
->flags
|= VXLAN_F_L2MISS
;
3439 if (data
[IFLA_VXLAN_L3MISS
]) {
3442 if (nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
3443 conf
->flags
|= VXLAN_F_L3MISS
;
3446 if (data
[IFLA_VXLAN_LIMIT
]) {
3449 conf
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
3452 if (data
[IFLA_VXLAN_COLLECT_METADATA
]) {
3455 if (nla_get_u8(data
[IFLA_VXLAN_COLLECT_METADATA
]))
3456 conf
->flags
|= VXLAN_F_COLLECT_METADATA
;
3459 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
3461 const struct ifla_vxlan_port_range
*p
3462 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
3463 conf
->port_min
= ntohs(p
->low
);
3464 conf
->port_max
= ntohs(p
->high
);
3470 if (data
[IFLA_VXLAN_PORT
]) {
3473 conf
->dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
3476 if (data
[IFLA_VXLAN_UDP_CSUM
]) {
3479 if (!nla_get_u8(data
[IFLA_VXLAN_UDP_CSUM
]))
3480 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM_TX
;
3483 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]) {
3486 if (nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
3487 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_TX
;
3490 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]) {
3493 if (nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
3494 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_RX
;
3497 if (data
[IFLA_VXLAN_REMCSUM_TX
]) {
3500 if (nla_get_u8(data
[IFLA_VXLAN_REMCSUM_TX
]))
3501 conf
->flags
|= VXLAN_F_REMCSUM_TX
;
3504 if (data
[IFLA_VXLAN_REMCSUM_RX
]) {
3507 if (nla_get_u8(data
[IFLA_VXLAN_REMCSUM_RX
]))
3508 conf
->flags
|= VXLAN_F_REMCSUM_RX
;
3511 if (data
[IFLA_VXLAN_GBP
]) {
3514 conf
->flags
|= VXLAN_F_GBP
;
3517 if (data
[IFLA_VXLAN_GPE
]) {
3520 conf
->flags
|= VXLAN_F_GPE
;
3523 if (data
[IFLA_VXLAN_REMCSUM_NOPARTIAL
]) {
3526 conf
->flags
|= VXLAN_F_REMCSUM_NOPARTIAL
;
3532 conf
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3538 static int vxlan_newlink(struct net
*src_net
, struct net_device
*dev
,
3539 struct nlattr
*tb
[], struct nlattr
*data
[],
3540 struct netlink_ext_ack
*extack
)
3542 struct vxlan_config conf
;
3545 err
= vxlan_nl2conf(tb
, data
, dev
, &conf
, false);
3549 return __vxlan_dev_create(src_net
, dev
, &conf
);
3552 static int vxlan_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
3553 struct nlattr
*data
[],
3554 struct netlink_ext_ack
*extack
)
3556 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3557 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3558 struct vxlan_rdst old_dst
;
3559 struct vxlan_config conf
;
3562 err
= vxlan_nl2conf(tb
, data
,
3567 memcpy(&old_dst
, dst
, sizeof(struct vxlan_rdst
));
3569 err
= vxlan_dev_configure(vxlan
->net
, dev
, &conf
, true);
3573 /* handle default dst entry */
3574 if (!vxlan_addr_equal(&dst
->remote_ip
, &old_dst
.remote_ip
)) {
3575 spin_lock_bh(&vxlan
->hash_lock
);
3576 if (!vxlan_addr_any(&old_dst
.remote_ip
))
3577 __vxlan_fdb_delete(vxlan
, all_zeros_mac
,
3579 vxlan
->cfg
.dst_port
,
3582 old_dst
.remote_ifindex
, 0);
3584 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3585 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
3587 NUD_REACHABLE
| NUD_PERMANENT
,
3588 NLM_F_CREATE
| NLM_F_APPEND
,
3589 vxlan
->cfg
.dst_port
,
3592 dst
->remote_ifindex
,
3595 spin_unlock_bh(&vxlan
->hash_lock
);
3599 spin_unlock_bh(&vxlan
->hash_lock
);
3605 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
3607 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3609 vxlan_flush(vxlan
, true);
3611 gro_cells_destroy(&vxlan
->gro_cells
);
3612 list_del(&vxlan
->next
);
3613 unregister_netdevice_queue(dev
, head
);
3616 static size_t vxlan_get_size(const struct net_device
*dev
)
3619 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
3620 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
3621 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
3622 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
3623 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
3624 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
3625 nla_total_size(sizeof(__be32
)) + /* IFLA_VXLAN_LABEL */
3626 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
3627 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
3628 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
3629 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
3630 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
3631 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_COLLECT_METADATA */
3632 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
3633 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
3634 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
3635 nla_total_size(sizeof(__be16
)) + /* IFLA_VXLAN_PORT */
3636 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_CSUM */
3637 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3638 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3639 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_TX */
3640 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_RX */
3641 nla_total_size(sizeof(struct ip_fan_map
) * 256) +
3645 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
3647 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3648 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3649 struct ifla_vxlan_port_range ports
= {
3650 .low
= htons(vxlan
->cfg
.port_min
),
3651 .high
= htons(vxlan
->cfg
.port_max
),
3654 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, be32_to_cpu(dst
->remote_vni
)))
3655 goto nla_put_failure
;
3657 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3658 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
3659 if (nla_put_in_addr(skb
, IFLA_VXLAN_GROUP
,
3660 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
3661 goto nla_put_failure
;
3662 #if IS_ENABLED(CONFIG_IPV6)
3664 if (nla_put_in6_addr(skb
, IFLA_VXLAN_GROUP6
,
3665 &dst
->remote_ip
.sin6
.sin6_addr
))
3666 goto nla_put_failure
;
3671 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
3672 goto nla_put_failure
;
3674 if (!vxlan_addr_any(&vxlan
->cfg
.saddr
)) {
3675 if (vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET
) {
3676 if (nla_put_in_addr(skb
, IFLA_VXLAN_LOCAL
,
3677 vxlan
->cfg
.saddr
.sin
.sin_addr
.s_addr
))
3678 goto nla_put_failure
;
3679 #if IS_ENABLED(CONFIG_IPV6)
3681 if (nla_put_in6_addr(skb
, IFLA_VXLAN_LOCAL6
,
3682 &vxlan
->cfg
.saddr
.sin6
.sin6_addr
))
3683 goto nla_put_failure
;
3688 if (fan_has_map(&vxlan
->fan
)) {
3689 struct nlattr
*fan_nest
;
3690 struct ip_fan_map
*fan_map
;
3692 fan_nest
= nla_nest_start(skb
, IFLA_VXLAN_FAN_MAP
);
3694 goto nla_put_failure
;
3695 list_for_each_entry_rcu(fan_map
, &vxlan
->fan
.fan_maps
, list
) {
3696 struct ifla_fan_map map
;
3698 map
.underlay
= fan_map
->underlay
;
3699 map
.underlay_prefix
= fan_map
->underlay_prefix
;
3700 map
.overlay
= fan_map
->overlay
;
3701 map
.overlay_prefix
= fan_map
->overlay_prefix
;
3702 if (nla_put(skb
, IFLA_FAN_MAPPING
, sizeof(map
), &map
))
3703 goto nla_put_failure
;
3705 nla_nest_end(skb
, fan_nest
);
3708 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->cfg
.ttl
) ||
3709 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->cfg
.tos
) ||
3710 nla_put_be32(skb
, IFLA_VXLAN_LABEL
, vxlan
->cfg
.label
) ||
3711 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
3712 !!(vxlan
->cfg
.flags
& VXLAN_F_LEARN
)) ||
3713 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
3714 !!(vxlan
->cfg
.flags
& VXLAN_F_PROXY
)) ||
3715 nla_put_u8(skb
, IFLA_VXLAN_RSC
,
3716 !!(vxlan
->cfg
.flags
& VXLAN_F_RSC
)) ||
3717 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
3718 !!(vxlan
->cfg
.flags
& VXLAN_F_L2MISS
)) ||
3719 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
3720 !!(vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) ||
3721 nla_put_u8(skb
, IFLA_VXLAN_COLLECT_METADATA
,
3722 !!(vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
)) ||
3723 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->cfg
.age_interval
) ||
3724 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->cfg
.addrmax
) ||
3725 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->cfg
.dst_port
) ||
3726 nla_put_u8(skb
, IFLA_VXLAN_UDP_CSUM
,
3727 !(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM_TX
)) ||
3728 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_TX
,
3729 !!(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
)) ||
3730 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_RX
,
3731 !!(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
)) ||
3732 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_TX
,
3733 !!(vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_TX
)) ||
3734 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_RX
,
3735 !!(vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_RX
)))
3736 goto nla_put_failure
;
3738 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
3739 goto nla_put_failure
;
3741 if (vxlan
->cfg
.flags
& VXLAN_F_GBP
&&
3742 nla_put_flag(skb
, IFLA_VXLAN_GBP
))
3743 goto nla_put_failure
;
3745 if (vxlan
->cfg
.flags
& VXLAN_F_GPE
&&
3746 nla_put_flag(skb
, IFLA_VXLAN_GPE
))
3747 goto nla_put_failure
;
3749 if (vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_NOPARTIAL
&&
3750 nla_put_flag(skb
, IFLA_VXLAN_REMCSUM_NOPARTIAL
))
3751 goto nla_put_failure
;
3759 static struct net
*vxlan_get_link_net(const struct net_device
*dev
)
3761 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3766 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
3768 .maxtype
= IFLA_VXLAN_MAX
,
3769 .policy
= vxlan_policy
,
3770 .priv_size
= sizeof(struct vxlan_dev
),
3771 .setup
= vxlan_setup
,
3772 .validate
= vxlan_validate
,
3773 .newlink
= vxlan_newlink
,
3774 .changelink
= vxlan_changelink
,
3775 .dellink
= vxlan_dellink
,
3776 .get_size
= vxlan_get_size
,
3777 .fill_info
= vxlan_fill_info
,
3778 .get_link_net
= vxlan_get_link_net
,
3781 struct net_device
*vxlan_dev_create(struct net
*net
, const char *name
,
3782 u8 name_assign_type
,
3783 struct vxlan_config
*conf
)
3785 struct nlattr
*tb
[IFLA_MAX
+ 1];
3786 struct net_device
*dev
;
3789 memset(&tb
, 0, sizeof(tb
));
3791 dev
= rtnl_create_link(net
, name
, name_assign_type
,
3792 &vxlan_link_ops
, tb
);
3796 err
= __vxlan_dev_create(net
, dev
, conf
);
3799 return ERR_PTR(err
);
3802 err
= rtnl_configure_link(dev
, NULL
);
3804 LIST_HEAD(list_kill
);
3806 vxlan_dellink(dev
, &list_kill
);
3807 unregister_netdevice_many(&list_kill
);
3808 return ERR_PTR(err
);
3813 EXPORT_SYMBOL_GPL(vxlan_dev_create
);
3815 static void vxlan_handle_lowerdev_unregister(struct vxlan_net
*vn
,
3816 struct net_device
*dev
)
3818 struct vxlan_dev
*vxlan
, *next
;
3819 LIST_HEAD(list_kill
);
3821 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3822 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3824 /* In case we created vxlan device with carrier
3825 * and we loose the carrier due to module unload
3826 * we also need to remove vxlan device. In other
3827 * cases, it's not necessary and remote_ifindex
3828 * is 0 here, so no matches.
3830 if (dst
->remote_ifindex
== dev
->ifindex
)
3831 vxlan_dellink(vxlan
->dev
, &list_kill
);
3834 unregister_netdevice_many(&list_kill
);
3837 static int vxlan_netdevice_event(struct notifier_block
*unused
,
3838 unsigned long event
, void *ptr
)
3840 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3841 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
3843 if (event
== NETDEV_UNREGISTER
)
3844 vxlan_handle_lowerdev_unregister(vn
, dev
);
3845 else if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
)
3846 vxlan_push_rx_ports(dev
);
3851 static struct notifier_block vxlan_notifier_block __read_mostly
= {
3852 .notifier_call
= vxlan_netdevice_event
,
3855 static __net_init
int vxlan_init_net(struct net
*net
)
3857 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3860 INIT_LIST_HEAD(&vn
->vxlan_list
);
3861 spin_lock_init(&vn
->sock_lock
);
3863 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
3864 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
3869 #ifdef CONFIG_SYSCTL
3870 static struct ctl_table_header
*vxlan_fan_header
;
3871 static unsigned int vxlan_fan_version
= 4;
3873 static struct ctl_table vxlan_fan_sysctls
[] = {
3875 .procname
= "vxlan",
3876 .data
= &vxlan_fan_version
,
3877 .maxlen
= sizeof(vxlan_fan_version
),
3879 .proc_handler
= proc_dointvec
,
3883 #endif /* CONFIG_SYSCTL */
3885 static void __net_exit
vxlan_exit_net(struct net
*net
)
3887 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3888 struct vxlan_dev
*vxlan
, *next
;
3889 struct net_device
*dev
, *aux
;
3893 for_each_netdev_safe(net
, dev
, aux
)
3894 if (dev
->rtnl_link_ops
== &vxlan_link_ops
)
3895 unregister_netdevice_queue(dev
, &list
);
3897 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3898 /* If vxlan->dev is in the same netns, it has already been added
3899 * to the list by the previous loop.
3901 if (!net_eq(dev_net(vxlan
->dev
), net
)) {
3902 gro_cells_destroy(&vxlan
->gro_cells
);
3903 unregister_netdevice_queue(vxlan
->dev
, &list
);
3907 unregister_netdevice_many(&list
);
3911 static struct pernet_operations vxlan_net_ops
= {
3912 .init
= vxlan_init_net
,
3913 .exit
= vxlan_exit_net
,
3914 .id
= &vxlan_net_id
,
3915 .size
= sizeof(struct vxlan_net
),
3918 static int __init
vxlan_init_module(void)
3922 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
3924 rc
= register_pernet_subsys(&vxlan_net_ops
);
3928 rc
= register_netdevice_notifier(&vxlan_notifier_block
);
3932 rc
= rtnl_link_register(&vxlan_link_ops
);
3936 #ifdef CONFIG_SYSCTL
3937 vxlan_fan_header
= register_net_sysctl(&init_net
, "net/fan",
3939 if (!vxlan_fan_header
) {
3943 #endif /* CONFIG_SYSCTL */
3946 #ifdef CONFIG_SYSCTL
3948 rtnl_link_unregister(&vxlan_link_ops
);
3949 #endif /* CONFIG_SYSCTL */
3951 unregister_netdevice_notifier(&vxlan_notifier_block
);
3953 unregister_pernet_subsys(&vxlan_net_ops
);
3957 late_initcall(vxlan_init_module
);
3959 static void __exit
vxlan_cleanup_module(void)
3961 #ifdef CONFIG_SYSCTL
3962 unregister_net_sysctl_table(vxlan_fan_header
);
3963 #endif /* CONFIG_SYSCTL */
3964 rtnl_link_unregister(&vxlan_link_ops
);
3965 unregister_netdevice_notifier(&vxlan_notifier_block
);
3966 unregister_pernet_subsys(&vxlan_net_ops
);
3967 /* rcu_barrier() is called by netns */
3969 module_exit(vxlan_cleanup_module
);
3971 MODULE_LICENSE("GPL");
3972 MODULE_VERSION(VXLAN_VERSION
);
3973 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3974 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3975 MODULE_ALIAS_RTNL_LINK("vxlan");