2 * VXLAN: Virtual eXtensible Local Area Network
4 * Copyright (c) 2012-2013 Vyatta Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/udp.h>
18 #include <linux/igmp.h>
19 #include <linux/if_ether.h>
20 #include <linux/ethtool.h>
22 #include <net/ndisc.h>
25 #include <net/rtnetlink.h>
26 #include <net/inet_ecn.h>
27 #include <net/net_namespace.h>
28 #include <net/netns/generic.h>
29 #include <net/tun_proto.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 #if IS_ENABLED(CONFIG_IPV6)
96 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
98 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
100 if (a
->sa
.sa_family
== AF_INET6
)
101 return ipv6_addr_equal(&a
->sin6
.sin6_addr
, &b
->sin6
.sin6_addr
);
103 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
106 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
108 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
109 ip
->sin6
.sin6_addr
= nla_get_in6_addr(nla
);
110 ip
->sa
.sa_family
= AF_INET6
;
112 } else if (nla_len(nla
) >= sizeof(__be32
)) {
113 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
114 ip
->sa
.sa_family
= AF_INET
;
117 return -EAFNOSUPPORT
;
121 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
122 const union vxlan_addr
*ip
)
124 if (ip
->sa
.sa_family
== AF_INET6
)
125 return nla_put_in6_addr(skb
, attr
, &ip
->sin6
.sin6_addr
);
127 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
130 #else /* !CONFIG_IPV6 */
133 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
135 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
138 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
140 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
141 return -EAFNOSUPPORT
;
142 } else if (nla_len(nla
) >= sizeof(__be32
)) {
143 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
144 ip
->sa
.sa_family
= AF_INET
;
147 return -EAFNOSUPPORT
;
151 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
152 const union vxlan_addr
*ip
)
154 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
158 /* Virtual Network hash table head */
159 static inline struct hlist_head
*vni_head(struct vxlan_sock
*vs
, __be32 vni
)
161 return &vs
->vni_list
[hash_32((__force u32
)vni
, VNI_HASH_BITS
)];
164 /* Socket hash table head */
165 static inline struct hlist_head
*vs_head(struct net
*net
, __be16 port
)
167 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
169 return &vn
->sock_list
[hash_32(ntohs(port
), PORT_HASH_BITS
)];
172 /* First remote destination for a forwarding entry.
173 * Guaranteed to be non-NULL because remotes are never deleted.
175 static inline struct vxlan_rdst
*first_remote_rcu(struct vxlan_fdb
*fdb
)
177 return list_entry_rcu(fdb
->remotes
.next
, struct vxlan_rdst
, list
);
180 static inline struct vxlan_rdst
*first_remote_rtnl(struct vxlan_fdb
*fdb
)
182 return list_first_entry(&fdb
->remotes
, struct vxlan_rdst
, list
);
185 /* Find VXLAN socket based on network namespace, address family and UDP port
186 * and enabled unshareable flags.
188 static struct vxlan_sock
*vxlan_find_sock(struct net
*net
, sa_family_t family
,
189 __be16 port
, u32 flags
)
191 struct vxlan_sock
*vs
;
193 flags
&= VXLAN_F_RCV_FLAGS
;
195 hlist_for_each_entry_rcu(vs
, vs_head(net
, port
), hlist
) {
196 if (inet_sk(vs
->sock
->sk
)->inet_sport
== port
&&
197 vxlan_get_sk_family(vs
) == family
&&
204 static struct vxlan_dev
*vxlan_vs_find_vni(struct vxlan_sock
*vs
, int ifindex
,
207 struct vxlan_dev_node
*node
;
209 /* For flow based devices, map all packets to VNI 0 */
210 if (vs
->flags
& VXLAN_F_COLLECT_METADATA
)
213 hlist_for_each_entry_rcu(node
, vni_head(vs
, vni
), hlist
) {
214 if (node
->vxlan
->default_dst
.remote_vni
!= vni
)
217 if (IS_ENABLED(CONFIG_IPV6
)) {
218 const struct vxlan_config
*cfg
= &node
->vxlan
->cfg
;
220 if ((cfg
->flags
& VXLAN_F_IPV6_LINKLOCAL
) &&
221 cfg
->remote_ifindex
!= ifindex
)
231 /* Look up VNI in a per net namespace table */
232 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, int ifindex
,
233 __be32 vni
, sa_family_t family
,
234 __be16 port
, u32 flags
)
236 struct vxlan_sock
*vs
;
238 vs
= vxlan_find_sock(net
, family
, port
, flags
);
242 return vxlan_vs_find_vni(vs
, ifindex
, vni
);
245 /* Fill in neighbour message in skbuff. */
246 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
247 const struct vxlan_fdb
*fdb
,
248 u32 portid
, u32 seq
, int type
, unsigned int flags
,
249 const struct vxlan_rdst
*rdst
)
251 unsigned long now
= jiffies
;
252 struct nda_cacheinfo ci
;
253 struct nlmsghdr
*nlh
;
255 bool send_ip
, send_eth
;
257 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
261 ndm
= nlmsg_data(nlh
);
262 memset(ndm
, 0, sizeof(*ndm
));
264 send_eth
= send_ip
= true;
266 if (type
== RTM_GETNEIGH
) {
267 send_ip
= !vxlan_addr_any(&rdst
->remote_ip
);
268 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
269 ndm
->ndm_family
= send_ip
? rdst
->remote_ip
.sa
.sa_family
: AF_INET
;
271 ndm
->ndm_family
= AF_BRIDGE
;
272 ndm
->ndm_state
= fdb
->state
;
273 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
274 ndm
->ndm_flags
= fdb
->flags
;
276 ndm
->ndm_flags
|= NTF_OFFLOADED
;
277 ndm
->ndm_type
= RTN_UNICAST
;
279 if (!net_eq(dev_net(vxlan
->dev
), vxlan
->net
) &&
280 nla_put_s32(skb
, NDA_LINK_NETNSID
,
281 peernet2id(dev_net(vxlan
->dev
), vxlan
->net
)))
282 goto nla_put_failure
;
284 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
285 goto nla_put_failure
;
287 if (send_ip
&& vxlan_nla_put_addr(skb
, NDA_DST
, &rdst
->remote_ip
))
288 goto nla_put_failure
;
290 if (rdst
->remote_port
&& rdst
->remote_port
!= vxlan
->cfg
.dst_port
&&
291 nla_put_be16(skb
, NDA_PORT
, rdst
->remote_port
))
292 goto nla_put_failure
;
293 if (rdst
->remote_vni
!= vxlan
->default_dst
.remote_vni
&&
294 nla_put_u32(skb
, NDA_VNI
, be32_to_cpu(rdst
->remote_vni
)))
295 goto nla_put_failure
;
296 if ((vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) && fdb
->vni
&&
297 nla_put_u32(skb
, NDA_SRC_VNI
,
298 be32_to_cpu(fdb
->vni
)))
299 goto nla_put_failure
;
300 if (rdst
->remote_ifindex
&&
301 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
302 goto nla_put_failure
;
304 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
305 ci
.ndm_confirmed
= 0;
306 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
309 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
310 goto nla_put_failure
;
316 nlmsg_cancel(skb
, nlh
);
320 static inline size_t vxlan_nlmsg_size(void)
322 return NLMSG_ALIGN(sizeof(struct ndmsg
))
323 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
324 + nla_total_size(sizeof(struct in6_addr
)) /* NDA_DST */
325 + nla_total_size(sizeof(__be16
)) /* NDA_PORT */
326 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
327 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
328 + nla_total_size(sizeof(__s32
)) /* NDA_LINK_NETNSID */
329 + nla_total_size(sizeof(struct nda_cacheinfo
));
332 static void __vxlan_fdb_notify(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*fdb
,
333 struct vxlan_rdst
*rd
, int type
)
335 struct net
*net
= dev_net(vxlan
->dev
);
339 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
343 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0, rd
);
345 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
346 WARN_ON(err
== -EMSGSIZE
);
351 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
355 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
358 static void vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev
*vxlan
,
359 struct vxlan_fdb
*fdb
,
360 struct vxlan_rdst
*rd
,
363 struct switchdev_notifier_vxlan_fdb_info info
;
364 enum switchdev_notifier_type notifier_type
;
369 notifier_type
= adding
? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
370 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE
;
372 info
= (struct switchdev_notifier_vxlan_fdb_info
){
373 .remote_ip
= rd
->remote_ip
,
374 .remote_port
= rd
->remote_port
,
375 .remote_vni
= rd
->remote_vni
,
376 .remote_ifindex
= rd
->remote_ifindex
,
378 .offloaded
= rd
->offloaded
,
380 memcpy(info
.eth_addr
, fdb
->eth_addr
, ETH_ALEN
);
382 call_switchdev_notifiers(notifier_type
, vxlan
->dev
,
386 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*fdb
,
387 struct vxlan_rdst
*rd
, int type
)
391 vxlan_fdb_switchdev_call_notifiers(vxlan
, fdb
, rd
, true);
394 vxlan_fdb_switchdev_call_notifiers(vxlan
, fdb
, rd
, false);
398 __vxlan_fdb_notify(vxlan
, fdb
, rd
, type
);
401 static void vxlan_ip_miss(struct net_device
*dev
, union vxlan_addr
*ipa
)
403 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
404 struct vxlan_fdb f
= {
407 struct vxlan_rdst remote
= {
408 .remote_ip
= *ipa
, /* goes to NDA_DST */
409 .remote_vni
= cpu_to_be32(VXLAN_N_VID
),
412 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
415 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
417 struct vxlan_fdb f
= {
420 struct vxlan_rdst remote
= { };
422 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
424 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
427 /* Hash Ethernet address */
428 static u32
eth_hash(const unsigned char *addr
)
430 u64 value
= get_unaligned((u64
*)addr
);
432 /* only want 6 bytes */
438 return hash_64(value
, FDB_HASH_BITS
);
441 static u32
eth_vni_hash(const unsigned char *addr
, __be32 vni
)
443 /* use 1 byte of OUI and 3 bytes of NIC */
444 u32 key
= get_unaligned((u32
*)(addr
+ 2));
446 return jhash_2words(key
, vni
, vxlan_salt
) & (FDB_HASH_SIZE
- 1);
449 /* Hash chain to use given mac address */
450 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
451 const u8
*mac
, __be32 vni
)
453 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
)
454 return &vxlan
->fdb_head
[eth_vni_hash(mac
, vni
)];
456 return &vxlan
->fdb_head
[eth_hash(mac
)];
459 /* Look up Ethernet address in forwarding table */
460 static struct vxlan_fdb
*__vxlan_find_mac(struct vxlan_dev
*vxlan
,
461 const u8
*mac
, __be32 vni
)
463 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
, vni
);
466 hlist_for_each_entry_rcu(f
, head
, hlist
) {
467 if (ether_addr_equal(mac
, f
->eth_addr
)) {
468 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) {
480 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
481 const u8
*mac
, __be32 vni
)
485 f
= __vxlan_find_mac(vxlan
, mac
, vni
);
486 if (f
&& f
->used
!= jiffies
)
492 /* caller should hold vxlan->hash_lock */
493 static struct vxlan_rdst
*vxlan_fdb_find_rdst(struct vxlan_fdb
*f
,
494 union vxlan_addr
*ip
, __be16 port
,
495 __be32 vni
, __u32 ifindex
)
497 struct vxlan_rdst
*rd
;
499 list_for_each_entry(rd
, &f
->remotes
, list
) {
500 if (vxlan_addr_equal(&rd
->remote_ip
, ip
) &&
501 rd
->remote_port
== port
&&
502 rd
->remote_vni
== vni
&&
503 rd
->remote_ifindex
== ifindex
)
510 int vxlan_fdb_find_uc(struct net_device
*dev
, const u8
*mac
, __be32 vni
,
511 struct switchdev_notifier_vxlan_fdb_info
*fdb_info
)
513 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
514 u8 eth_addr
[ETH_ALEN
+ 2] = { 0 };
515 struct vxlan_rdst
*rdst
;
519 if (is_multicast_ether_addr(mac
) ||
520 is_zero_ether_addr(mac
))
523 ether_addr_copy(eth_addr
, mac
);
527 f
= __vxlan_find_mac(vxlan
, eth_addr
, vni
);
533 rdst
= first_remote_rcu(f
);
535 memset(fdb_info
, 0, sizeof(*fdb_info
));
536 fdb_info
->info
.dev
= dev
;
537 fdb_info
->remote_ip
= rdst
->remote_ip
;
538 fdb_info
->remote_port
= rdst
->remote_port
;
539 fdb_info
->remote_vni
= rdst
->remote_vni
;
540 fdb_info
->remote_ifindex
= rdst
->remote_ifindex
;
542 fdb_info
->offloaded
= rdst
->offloaded
;
543 ether_addr_copy(fdb_info
->eth_addr
, mac
);
549 EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc
);
551 /* Replace destination of unicast mac */
552 static int vxlan_fdb_replace(struct vxlan_fdb
*f
,
553 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
556 struct vxlan_rdst
*rd
;
558 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
562 rd
= list_first_entry_or_null(&f
->remotes
, struct vxlan_rdst
, list
);
566 dst_cache_reset(&rd
->dst_cache
);
568 rd
->remote_port
= port
;
569 rd
->remote_vni
= vni
;
570 rd
->remote_ifindex
= ifindex
;
574 /* Add/update destinations for multicast */
575 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
576 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
577 __u32 ifindex
, struct vxlan_rdst
**rdp
)
579 struct vxlan_rdst
*rd
;
581 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
585 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
589 if (dst_cache_init(&rd
->dst_cache
, GFP_ATOMIC
)) {
595 rd
->remote_port
= port
;
596 rd
->offloaded
= false;
597 rd
->remote_vni
= vni
;
598 rd
->remote_ifindex
= ifindex
;
600 list_add_tail_rcu(&rd
->list
, &f
->remotes
);
606 static struct vxlanhdr
*vxlan_gro_remcsum(struct sk_buff
*skb
,
608 struct vxlanhdr
*vh
, size_t hdrlen
,
610 struct gro_remcsum
*grc
,
613 size_t start
, offset
;
615 if (skb
->remcsum_offload
)
618 if (!NAPI_GRO_CB(skb
)->csum_valid
)
621 start
= vxlan_rco_start(vni_field
);
622 offset
= start
+ vxlan_rco_offset(vni_field
);
624 vh
= skb_gro_remcsum_process(skb
, (void *)vh
, off
, hdrlen
,
625 start
, offset
, grc
, nopartial
);
627 skb
->remcsum_offload
= 1;
632 static struct sk_buff
*vxlan_gro_receive(struct sock
*sk
,
633 struct list_head
*head
,
636 struct sk_buff
*pp
= NULL
;
638 struct vxlanhdr
*vh
, *vh2
;
639 unsigned int hlen
, off_vx
;
641 struct vxlan_sock
*vs
= rcu_dereference_sk_user_data(sk
);
643 struct gro_remcsum grc
;
645 skb_gro_remcsum_init(&grc
);
647 off_vx
= skb_gro_offset(skb
);
648 hlen
= off_vx
+ sizeof(*vh
);
649 vh
= skb_gro_header_fast(skb
, off_vx
);
650 if (skb_gro_header_hard(skb
, hlen
)) {
651 vh
= skb_gro_header_slow(skb
, hlen
, off_vx
);
656 skb_gro_postpull_rcsum(skb
, vh
, sizeof(struct vxlanhdr
));
658 flags
= vh
->vx_flags
;
660 if ((flags
& VXLAN_HF_RCO
) && (vs
->flags
& VXLAN_F_REMCSUM_RX
)) {
661 vh
= vxlan_gro_remcsum(skb
, off_vx
, vh
, sizeof(struct vxlanhdr
),
664 VXLAN_F_REMCSUM_NOPARTIAL
));
670 skb_gro_pull(skb
, sizeof(struct vxlanhdr
)); /* pull vxlan header */
672 list_for_each_entry(p
, head
, list
) {
673 if (!NAPI_GRO_CB(p
)->same_flow
)
676 vh2
= (struct vxlanhdr
*)(p
->data
+ off_vx
);
677 if (vh
->vx_flags
!= vh2
->vx_flags
||
678 vh
->vx_vni
!= vh2
->vx_vni
) {
679 NAPI_GRO_CB(p
)->same_flow
= 0;
684 pp
= call_gro_receive(eth_gro_receive
, head
, skb
);
688 skb_gro_flush_final_remcsum(skb
, pp
, flush
, &grc
);
693 static int vxlan_gro_complete(struct sock
*sk
, struct sk_buff
*skb
, int nhoff
)
695 /* Sets 'skb->inner_mac_header' since we are always called with
696 * 'skb->encapsulation' set.
698 return eth_gro_complete(skb
, nhoff
+ sizeof(struct vxlanhdr
));
701 static struct vxlan_fdb
*vxlan_fdb_alloc(struct vxlan_dev
*vxlan
,
702 const u8
*mac
, __u16 state
,
703 __be32 src_vni
, __u8 ndm_flags
)
707 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
711 f
->flags
= ndm_flags
;
712 f
->updated
= f
->used
= jiffies
;
714 INIT_LIST_HEAD(&f
->remotes
);
715 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
720 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
721 const u8
*mac
, union vxlan_addr
*ip
,
722 __u16 state
, __be16 port
, __be32 src_vni
,
723 __be32 vni
, __u32 ifindex
, __u8 ndm_flags
,
724 struct vxlan_fdb
**fdb
)
726 struct vxlan_rdst
*rd
= NULL
;
730 if (vxlan
->cfg
.addrmax
&&
731 vxlan
->addrcnt
>= vxlan
->cfg
.addrmax
)
734 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
735 f
= vxlan_fdb_alloc(vxlan
, mac
, state
, src_vni
, ndm_flags
);
739 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
746 hlist_add_head_rcu(&f
->hlist
,
747 vxlan_fdb_head(vxlan
, mac
, src_vni
));
754 /* Add new entry to forwarding table -- assumes lock held */
755 static int vxlan_fdb_update(struct vxlan_dev
*vxlan
,
756 const u8
*mac
, union vxlan_addr
*ip
,
757 __u16 state
, __u16 flags
,
758 __be16 port
, __be32 src_vni
, __be32 vni
,
759 __u32 ifindex
, __u8 ndm_flags
)
761 __u8 fdb_flags
= (ndm_flags
& ~NTF_USE
);
762 struct vxlan_rdst
*rd
= NULL
;
767 f
= __vxlan_find_mac(vxlan
, mac
, src_vni
);
769 if (flags
& NLM_F_EXCL
) {
770 netdev_dbg(vxlan
->dev
,
771 "lost race to create %pM\n", mac
);
774 if (f
->state
!= state
) {
776 f
->updated
= jiffies
;
779 if (f
->flags
!= fdb_flags
) {
780 f
->flags
= fdb_flags
;
781 f
->updated
= jiffies
;
784 if ((flags
& NLM_F_REPLACE
)) {
785 /* Only change unicasts */
786 if (!(is_multicast_ether_addr(f
->eth_addr
) ||
787 is_zero_ether_addr(f
->eth_addr
))) {
788 notify
|= vxlan_fdb_replace(f
, ip
, port
, vni
,
793 if ((flags
& NLM_F_APPEND
) &&
794 (is_multicast_ether_addr(f
->eth_addr
) ||
795 is_zero_ether_addr(f
->eth_addr
))) {
796 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
803 if (ndm_flags
& NTF_USE
)
806 if (!(flags
& NLM_F_CREATE
))
809 /* Disallow replace to add a multicast entry */
810 if ((flags
& NLM_F_REPLACE
) &&
811 (is_multicast_ether_addr(mac
) || is_zero_ether_addr(mac
)))
814 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
815 rc
= vxlan_fdb_create(vxlan
, mac
, ip
, state
, port
, src_vni
,
816 vni
, ifindex
, fdb_flags
, &f
);
824 rd
= first_remote_rtnl(f
);
825 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_NEWNEIGH
);
831 static void vxlan_fdb_free(struct rcu_head
*head
)
833 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
834 struct vxlan_rdst
*rd
, *nd
;
836 list_for_each_entry_safe(rd
, nd
, &f
->remotes
, list
) {
837 dst_cache_destroy(&rd
->dst_cache
);
843 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
,
846 struct vxlan_rdst
*rd
;
848 netdev_dbg(vxlan
->dev
,
849 "delete %pM\n", f
->eth_addr
);
853 list_for_each_entry(rd
, &f
->remotes
, list
)
854 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_DELNEIGH
);
856 hlist_del_rcu(&f
->hlist
);
857 call_rcu(&f
->rcu
, vxlan_fdb_free
);
860 static void vxlan_dst_free(struct rcu_head
*head
)
862 struct vxlan_rdst
*rd
= container_of(head
, struct vxlan_rdst
, rcu
);
864 dst_cache_destroy(&rd
->dst_cache
);
868 static void vxlan_fdb_dst_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
,
869 struct vxlan_rdst
*rd
)
871 list_del_rcu(&rd
->list
);
872 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_DELNEIGH
);
873 call_rcu(&rd
->rcu
, vxlan_dst_free
);
876 static int vxlan_fdb_parse(struct nlattr
*tb
[], struct vxlan_dev
*vxlan
,
877 union vxlan_addr
*ip
, __be16
*port
, __be32
*src_vni
,
878 __be32
*vni
, u32
*ifindex
)
880 struct net
*net
= dev_net(vxlan
->dev
);
884 err
= vxlan_nla_get_addr(ip
, tb
[NDA_DST
]);
888 union vxlan_addr
*remote
= &vxlan
->default_dst
.remote_ip
;
889 if (remote
->sa
.sa_family
== AF_INET
) {
890 ip
->sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
891 ip
->sa
.sa_family
= AF_INET
;
892 #if IS_ENABLED(CONFIG_IPV6)
894 ip
->sin6
.sin6_addr
= in6addr_any
;
895 ip
->sa
.sa_family
= AF_INET6
;
901 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
903 *port
= nla_get_be16(tb
[NDA_PORT
]);
905 *port
= vxlan
->cfg
.dst_port
;
909 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
911 *vni
= cpu_to_be32(nla_get_u32(tb
[NDA_VNI
]));
913 *vni
= vxlan
->default_dst
.remote_vni
;
916 if (tb
[NDA_SRC_VNI
]) {
917 if (nla_len(tb
[NDA_SRC_VNI
]) != sizeof(u32
))
919 *src_vni
= cpu_to_be32(nla_get_u32(tb
[NDA_SRC_VNI
]));
921 *src_vni
= vxlan
->default_dst
.remote_vni
;
924 if (tb
[NDA_IFINDEX
]) {
925 struct net_device
*tdev
;
927 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
929 *ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
930 tdev
= __dev_get_by_index(net
, *ifindex
);
932 return -EADDRNOTAVAIL
;
940 /* Add static entry (via netlink) */
941 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
942 struct net_device
*dev
,
943 const unsigned char *addr
, u16 vid
, u16 flags
)
945 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
946 /* struct net *net = dev_net(vxlan->dev); */
953 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
954 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
959 if (tb
[NDA_DST
] == NULL
)
962 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &src_vni
, &vni
, &ifindex
);
966 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
!= ip
.sa
.sa_family
)
967 return -EAFNOSUPPORT
;
969 spin_lock_bh(&vxlan
->hash_lock
);
970 err
= vxlan_fdb_update(vxlan
, addr
, &ip
, ndm
->ndm_state
, flags
,
971 port
, src_vni
, vni
, ifindex
, ndm
->ndm_flags
);
972 spin_unlock_bh(&vxlan
->hash_lock
);
977 static int __vxlan_fdb_delete(struct vxlan_dev
*vxlan
,
978 const unsigned char *addr
, union vxlan_addr ip
,
979 __be16 port
, __be32 src_vni
, __be32 vni
,
980 u32 ifindex
, u16 vid
)
983 struct vxlan_rdst
*rd
= NULL
;
986 f
= vxlan_find_mac(vxlan
, addr
, src_vni
);
990 if (!vxlan_addr_any(&ip
)) {
991 rd
= vxlan_fdb_find_rdst(f
, &ip
, port
, vni
, ifindex
);
996 /* remove a destination if it's not the only one on the list,
997 * otherwise destroy the fdb entry
999 if (rd
&& !list_is_singular(&f
->remotes
)) {
1000 vxlan_fdb_dst_destroy(vxlan
, f
, rd
);
1004 vxlan_fdb_destroy(vxlan
, f
, true);
1010 /* Delete entry (via netlink) */
1011 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
1012 struct net_device
*dev
,
1013 const unsigned char *addr
, u16 vid
)
1015 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1016 union vxlan_addr ip
;
1017 __be32 src_vni
, vni
;
1022 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &src_vni
, &vni
, &ifindex
);
1026 spin_lock_bh(&vxlan
->hash_lock
);
1027 err
= __vxlan_fdb_delete(vxlan
, addr
, ip
, port
, src_vni
, vni
, ifindex
,
1029 spin_unlock_bh(&vxlan
->hash_lock
);
1034 /* Dump forwarding table */
1035 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
1036 struct net_device
*dev
,
1037 struct net_device
*filter_dev
, int *idx
)
1039 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1043 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1044 struct vxlan_fdb
*f
;
1046 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
1047 struct vxlan_rdst
*rd
;
1049 list_for_each_entry_rcu(rd
, &f
->remotes
, list
) {
1050 if (*idx
< cb
->args
[2])
1053 err
= vxlan_fdb_info(skb
, vxlan
, f
,
1054 NETLINK_CB(cb
->skb
).portid
,
1069 /* Watch incoming packets to learn mapping between Ethernet address
1070 * and Tunnel endpoint.
1071 * Return true if packet is bogus and should be dropped.
1073 static bool vxlan_snoop(struct net_device
*dev
,
1074 union vxlan_addr
*src_ip
, const u8
*src_mac
,
1075 u32 src_ifindex
, __be32 vni
)
1077 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1078 struct vxlan_fdb
*f
;
1081 #if IS_ENABLED(CONFIG_IPV6)
1082 if (src_ip
->sa
.sa_family
== AF_INET6
&&
1083 (ipv6_addr_type(&src_ip
->sin6
.sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
1084 ifindex
= src_ifindex
;
1087 f
= vxlan_find_mac(vxlan
, src_mac
, vni
);
1089 struct vxlan_rdst
*rdst
= first_remote_rcu(f
);
1091 if (likely(vxlan_addr_equal(&rdst
->remote_ip
, src_ip
) &&
1092 rdst
->remote_ifindex
== ifindex
))
1095 /* Don't migrate static entries, drop packets */
1096 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
1099 if (net_ratelimit())
1101 "%pM migrated from %pIS to %pIS\n",
1102 src_mac
, &rdst
->remote_ip
.sa
, &src_ip
->sa
);
1104 rdst
->remote_ip
= *src_ip
;
1105 f
->updated
= jiffies
;
1106 vxlan_fdb_notify(vxlan
, f
, rdst
, RTM_NEWNEIGH
);
1108 /* learned new entry */
1109 spin_lock(&vxlan
->hash_lock
);
1111 /* close off race between vxlan_flush and incoming packets */
1112 if (netif_running(dev
))
1113 vxlan_fdb_update(vxlan
, src_mac
, src_ip
,
1115 NLM_F_EXCL
|NLM_F_CREATE
,
1116 vxlan
->cfg
.dst_port
,
1118 vxlan
->default_dst
.remote_vni
,
1120 spin_unlock(&vxlan
->hash_lock
);
1126 /* See if multicast group is already in use by other ID */
1127 static bool vxlan_group_used(struct vxlan_net
*vn
, struct vxlan_dev
*dev
)
1129 struct vxlan_dev
*vxlan
;
1130 struct vxlan_sock
*sock4
;
1131 #if IS_ENABLED(CONFIG_IPV6)
1132 struct vxlan_sock
*sock6
;
1134 unsigned short family
= dev
->default_dst
.remote_ip
.sa
.sa_family
;
1136 sock4
= rtnl_dereference(dev
->vn4_sock
);
1138 /* The vxlan_sock is only used by dev, leaving group has
1139 * no effect on other vxlan devices.
1141 if (family
== AF_INET
&& sock4
&& refcount_read(&sock4
->refcnt
) == 1)
1143 #if IS_ENABLED(CONFIG_IPV6)
1144 sock6
= rtnl_dereference(dev
->vn6_sock
);
1145 if (family
== AF_INET6
&& sock6
&& refcount_read(&sock6
->refcnt
) == 1)
1149 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
) {
1150 if (!netif_running(vxlan
->dev
) || vxlan
== dev
)
1153 if (family
== AF_INET
&&
1154 rtnl_dereference(vxlan
->vn4_sock
) != sock4
)
1156 #if IS_ENABLED(CONFIG_IPV6)
1157 if (family
== AF_INET6
&&
1158 rtnl_dereference(vxlan
->vn6_sock
) != sock6
)
1162 if (!vxlan_addr_equal(&vxlan
->default_dst
.remote_ip
,
1163 &dev
->default_dst
.remote_ip
))
1166 if (vxlan
->default_dst
.remote_ifindex
!=
1167 dev
->default_dst
.remote_ifindex
)
1176 static bool __vxlan_sock_release_prep(struct vxlan_sock
*vs
)
1178 struct vxlan_net
*vn
;
1182 if (!refcount_dec_and_test(&vs
->refcnt
))
1185 vn
= net_generic(sock_net(vs
->sock
->sk
), vxlan_net_id
);
1186 spin_lock(&vn
->sock_lock
);
1187 hlist_del_rcu(&vs
->hlist
);
1188 udp_tunnel_notify_del_rx_port(vs
->sock
,
1189 (vs
->flags
& VXLAN_F_GPE
) ?
1190 UDP_TUNNEL_TYPE_VXLAN_GPE
:
1191 UDP_TUNNEL_TYPE_VXLAN
);
1192 spin_unlock(&vn
->sock_lock
);
1197 static void vxlan_sock_release(struct vxlan_dev
*vxlan
)
1199 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1200 #if IS_ENABLED(CONFIG_IPV6)
1201 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1203 RCU_INIT_POINTER(vxlan
->vn6_sock
, NULL
);
1206 RCU_INIT_POINTER(vxlan
->vn4_sock
, NULL
);
1209 vxlan_vs_del_dev(vxlan
);
1211 if (__vxlan_sock_release_prep(sock4
)) {
1212 udp_tunnel_sock_release(sock4
->sock
);
1216 #if IS_ENABLED(CONFIG_IPV6)
1217 if (__vxlan_sock_release_prep(sock6
)) {
1218 udp_tunnel_sock_release(sock6
->sock
);
1224 /* Update multicast group membership when first VNI on
1225 * multicast address is brought up
1227 static int vxlan_igmp_join(struct vxlan_dev
*vxlan
)
1230 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1231 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1234 if (ip
->sa
.sa_family
== AF_INET
) {
1235 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1236 struct ip_mreqn mreq
= {
1237 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1238 .imr_ifindex
= ifindex
,
1241 sk
= sock4
->sock
->sk
;
1243 ret
= ip_mc_join_group(sk
, &mreq
);
1245 #if IS_ENABLED(CONFIG_IPV6)
1247 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1249 sk
= sock6
->sock
->sk
;
1251 ret
= ipv6_stub
->ipv6_sock_mc_join(sk
, ifindex
,
1252 &ip
->sin6
.sin6_addr
);
1260 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1261 static int vxlan_igmp_leave(struct vxlan_dev
*vxlan
)
1264 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1265 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1268 if (ip
->sa
.sa_family
== AF_INET
) {
1269 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1270 struct ip_mreqn mreq
= {
1271 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1272 .imr_ifindex
= ifindex
,
1275 sk
= sock4
->sock
->sk
;
1277 ret
= ip_mc_leave_group(sk
, &mreq
);
1279 #if IS_ENABLED(CONFIG_IPV6)
1281 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1283 sk
= sock6
->sock
->sk
;
1285 ret
= ipv6_stub
->ipv6_sock_mc_drop(sk
, ifindex
,
1286 &ip
->sin6
.sin6_addr
);
1294 static bool vxlan_remcsum(struct vxlanhdr
*unparsed
,
1295 struct sk_buff
*skb
, u32 vxflags
)
1297 size_t start
, offset
;
1299 if (!(unparsed
->vx_flags
& VXLAN_HF_RCO
) || skb
->remcsum_offload
)
1302 start
= vxlan_rco_start(unparsed
->vx_vni
);
1303 offset
= start
+ vxlan_rco_offset(unparsed
->vx_vni
);
1305 if (!pskb_may_pull(skb
, offset
+ sizeof(u16
)))
1308 skb_remcsum_process(skb
, (void *)(vxlan_hdr(skb
) + 1), start
, offset
,
1309 !!(vxflags
& VXLAN_F_REMCSUM_NOPARTIAL
));
1311 unparsed
->vx_flags
&= ~VXLAN_HF_RCO
;
1312 unparsed
->vx_vni
&= VXLAN_VNI_MASK
;
1316 static void vxlan_parse_gbp_hdr(struct vxlanhdr
*unparsed
,
1317 struct sk_buff
*skb
, u32 vxflags
,
1318 struct vxlan_metadata
*md
)
1320 struct vxlanhdr_gbp
*gbp
= (struct vxlanhdr_gbp
*)unparsed
;
1321 struct metadata_dst
*tun_dst
;
1323 if (!(unparsed
->vx_flags
& VXLAN_HF_GBP
))
1326 md
->gbp
= ntohs(gbp
->policy_id
);
1328 tun_dst
= (struct metadata_dst
*)skb_dst(skb
);
1330 tun_dst
->u
.tun_info
.key
.tun_flags
|= TUNNEL_VXLAN_OPT
;
1331 tun_dst
->u
.tun_info
.options_len
= sizeof(*md
);
1333 if (gbp
->dont_learn
)
1334 md
->gbp
|= VXLAN_GBP_DONT_LEARN
;
1336 if (gbp
->policy_applied
)
1337 md
->gbp
|= VXLAN_GBP_POLICY_APPLIED
;
1339 /* In flow-based mode, GBP is carried in dst_metadata */
1340 if (!(vxflags
& VXLAN_F_COLLECT_METADATA
))
1341 skb
->mark
= md
->gbp
;
1343 unparsed
->vx_flags
&= ~VXLAN_GBP_USED_BITS
;
1346 static bool vxlan_parse_gpe_hdr(struct vxlanhdr
*unparsed
,
1348 struct sk_buff
*skb
, u32 vxflags
)
1350 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)unparsed
;
1352 /* Need to have Next Protocol set for interfaces in GPE mode. */
1353 if (!gpe
->np_applied
)
1355 /* "The initial version is 0. If a receiver does not support the
1356 * version indicated it MUST drop the packet.
1358 if (gpe
->version
!= 0)
1360 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1361 * processing MUST occur." However, we don't implement OAM
1362 * processing, thus drop the packet.
1367 *protocol
= tun_p_to_eth_p(gpe
->next_protocol
);
1371 unparsed
->vx_flags
&= ~VXLAN_GPE_USED_BITS
;
1375 static bool vxlan_set_mac(struct vxlan_dev
*vxlan
,
1376 struct vxlan_sock
*vs
,
1377 struct sk_buff
*skb
, __be32 vni
)
1379 union vxlan_addr saddr
;
1380 u32 ifindex
= skb
->dev
->ifindex
;
1382 skb_reset_mac_header(skb
);
1383 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1384 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
1386 /* Ignore packet loops (and multicast echo) */
1387 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1390 /* Get address from the outer IP header */
1391 if (vxlan_get_sk_family(vs
) == AF_INET
) {
1392 saddr
.sin
.sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
1393 saddr
.sa
.sa_family
= AF_INET
;
1394 #if IS_ENABLED(CONFIG_IPV6)
1396 saddr
.sin6
.sin6_addr
= ipv6_hdr(skb
)->saddr
;
1397 saddr
.sa
.sa_family
= AF_INET6
;
1401 if ((vxlan
->cfg
.flags
& VXLAN_F_LEARN
) &&
1402 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
, ifindex
, vni
))
1408 static bool vxlan_ecn_decapsulate(struct vxlan_sock
*vs
, void *oiph
,
1409 struct sk_buff
*skb
)
1413 if (vxlan_get_sk_family(vs
) == AF_INET
)
1414 err
= IP_ECN_decapsulate(oiph
, skb
);
1415 #if IS_ENABLED(CONFIG_IPV6)
1417 err
= IP6_ECN_decapsulate(oiph
, skb
);
1420 if (unlikely(err
) && log_ecn_error
) {
1421 if (vxlan_get_sk_family(vs
) == AF_INET
)
1422 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1423 &((struct iphdr
*)oiph
)->saddr
,
1424 ((struct iphdr
*)oiph
)->tos
);
1426 net_info_ratelimited("non-ECT from %pI6\n",
1427 &((struct ipv6hdr
*)oiph
)->saddr
);
1432 /* Callback from net/ipv4/udp.c to receive packets */
1433 static int vxlan_rcv(struct sock
*sk
, struct sk_buff
*skb
)
1435 struct pcpu_sw_netstats
*stats
;
1436 struct vxlan_dev
*vxlan
;
1437 struct vxlan_sock
*vs
;
1438 struct vxlanhdr unparsed
;
1439 struct vxlan_metadata _md
;
1440 struct vxlan_metadata
*md
= &_md
;
1441 __be16 protocol
= htons(ETH_P_TEB
);
1442 bool raw_proto
= false;
1446 /* Need UDP and VXLAN header to be present */
1447 if (!pskb_may_pull(skb
, VXLAN_HLEN
))
1450 unparsed
= *vxlan_hdr(skb
);
1451 /* VNI flag always required to be set */
1452 if (!(unparsed
.vx_flags
& VXLAN_HF_VNI
)) {
1453 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
1454 ntohl(vxlan_hdr(skb
)->vx_flags
),
1455 ntohl(vxlan_hdr(skb
)->vx_vni
));
1456 /* Return non vxlan pkt */
1459 unparsed
.vx_flags
&= ~VXLAN_HF_VNI
;
1460 unparsed
.vx_vni
&= ~VXLAN_VNI_MASK
;
1462 vs
= rcu_dereference_sk_user_data(sk
);
1466 vni
= vxlan_vni(vxlan_hdr(skb
)->vx_vni
);
1468 vxlan
= vxlan_vs_find_vni(vs
, skb
->dev
->ifindex
, vni
);
1472 /* For backwards compatibility, only allow reserved fields to be
1473 * used by VXLAN extensions if explicitly requested.
1475 if (vs
->flags
& VXLAN_F_GPE
) {
1476 if (!vxlan_parse_gpe_hdr(&unparsed
, &protocol
, skb
, vs
->flags
))
1481 if (__iptunnel_pull_header(skb
, VXLAN_HLEN
, protocol
, raw_proto
,
1482 !net_eq(vxlan
->net
, dev_net(vxlan
->dev
))))
1485 if (vxlan_collect_metadata(vs
)) {
1486 struct metadata_dst
*tun_dst
;
1488 tun_dst
= udp_tun_rx_dst(skb
, vxlan_get_sk_family(vs
), TUNNEL_KEY
,
1489 key32_to_tunnel_id(vni
), sizeof(*md
));
1494 md
= ip_tunnel_info_opts(&tun_dst
->u
.tun_info
);
1496 skb_dst_set(skb
, (struct dst_entry
*)tun_dst
);
1498 memset(md
, 0, sizeof(*md
));
1501 if (vs
->flags
& VXLAN_F_REMCSUM_RX
)
1502 if (!vxlan_remcsum(&unparsed
, skb
, vs
->flags
))
1504 if (vs
->flags
& VXLAN_F_GBP
)
1505 vxlan_parse_gbp_hdr(&unparsed
, skb
, vs
->flags
, md
);
1506 /* Note that GBP and GPE can never be active together. This is
1507 * ensured in vxlan_dev_configure.
1510 if (unparsed
.vx_flags
|| unparsed
.vx_vni
) {
1511 /* If there are any unprocessed flags remaining treat
1512 * this as a malformed packet. This behavior diverges from
1513 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1514 * in reserved fields are to be ignored. The approach here
1515 * maintains compatibility with previous stack code, and also
1516 * is more robust and provides a little more security in
1517 * adding extensions to VXLAN.
1523 if (!vxlan_set_mac(vxlan
, vs
, skb
, vni
))
1526 skb_reset_mac_header(skb
);
1527 skb
->dev
= vxlan
->dev
;
1528 skb
->pkt_type
= PACKET_HOST
;
1531 oiph
= skb_network_header(skb
);
1532 skb_reset_network_header(skb
);
1534 if (!vxlan_ecn_decapsulate(vs
, oiph
, skb
)) {
1535 ++vxlan
->dev
->stats
.rx_frame_errors
;
1536 ++vxlan
->dev
->stats
.rx_errors
;
1540 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1541 u64_stats_update_begin(&stats
->syncp
);
1542 stats
->rx_packets
++;
1543 stats
->rx_bytes
+= skb
->len
;
1544 u64_stats_update_end(&stats
->syncp
);
1546 gro_cells_receive(&vxlan
->gro_cells
, skb
);
1550 /* Consume bad packet */
1555 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
, __be32 vni
)
1557 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1558 struct arphdr
*parp
;
1561 struct neighbour
*n
;
1563 if (dev
->flags
& IFF_NOARP
)
1566 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1567 dev
->stats
.tx_dropped
++;
1570 parp
= arp_hdr(skb
);
1572 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1573 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1574 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1575 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1576 parp
->ar_hln
!= dev
->addr_len
||
1579 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1581 arpptr
+= dev
->addr_len
; /* sha */
1582 memcpy(&sip
, arpptr
, sizeof(sip
));
1583 arpptr
+= sizeof(sip
);
1584 arpptr
+= dev
->addr_len
; /* tha */
1585 memcpy(&tip
, arpptr
, sizeof(tip
));
1587 if (ipv4_is_loopback(tip
) ||
1588 ipv4_is_multicast(tip
))
1591 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1594 struct vxlan_fdb
*f
;
1595 struct sk_buff
*reply
;
1597 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1602 f
= vxlan_find_mac(vxlan
, n
->ha
, vni
);
1603 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1604 /* bridge-local neighbor */
1609 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1617 skb_reset_mac_header(reply
);
1618 __skb_pull(reply
, skb_network_offset(reply
));
1619 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1620 reply
->pkt_type
= PACKET_HOST
;
1622 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1623 dev
->stats
.rx_dropped
++;
1624 } else if (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
) {
1625 union vxlan_addr ipa
= {
1626 .sin
.sin_addr
.s_addr
= tip
,
1627 .sin
.sin_family
= AF_INET
,
1630 vxlan_ip_miss(dev
, &ipa
);
1634 return NETDEV_TX_OK
;
1637 #if IS_ENABLED(CONFIG_IPV6)
1638 static struct sk_buff
*vxlan_na_create(struct sk_buff
*request
,
1639 struct neighbour
*n
, bool isrouter
)
1641 struct net_device
*dev
= request
->dev
;
1642 struct sk_buff
*reply
;
1643 struct nd_msg
*ns
, *na
;
1644 struct ipv6hdr
*pip6
;
1646 int na_olen
= 8; /* opt hdr + ETH_ALEN for target */
1650 if (dev
== NULL
|| !pskb_may_pull(request
, request
->len
))
1653 len
= LL_RESERVED_SPACE(dev
) + sizeof(struct ipv6hdr
) +
1654 sizeof(*na
) + na_olen
+ dev
->needed_tailroom
;
1655 reply
= alloc_skb(len
, GFP_ATOMIC
);
1659 reply
->protocol
= htons(ETH_P_IPV6
);
1661 skb_reserve(reply
, LL_RESERVED_SPACE(request
->dev
));
1662 skb_push(reply
, sizeof(struct ethhdr
));
1663 skb_reset_mac_header(reply
);
1665 ns
= (struct nd_msg
*)(ipv6_hdr(request
) + 1);
1667 daddr
= eth_hdr(request
)->h_source
;
1668 ns_olen
= request
->len
- skb_network_offset(request
) -
1669 sizeof(struct ipv6hdr
) - sizeof(*ns
);
1670 for (i
= 0; i
< ns_olen
-1; i
+= (ns
->opt
[i
+1]<<3)) {
1671 if (ns
->opt
[i
] == ND_OPT_SOURCE_LL_ADDR
) {
1672 daddr
= ns
->opt
+ i
+ sizeof(struct nd_opt_hdr
);
1677 /* Ethernet header */
1678 ether_addr_copy(eth_hdr(reply
)->h_dest
, daddr
);
1679 ether_addr_copy(eth_hdr(reply
)->h_source
, n
->ha
);
1680 eth_hdr(reply
)->h_proto
= htons(ETH_P_IPV6
);
1681 reply
->protocol
= htons(ETH_P_IPV6
);
1683 skb_pull(reply
, sizeof(struct ethhdr
));
1684 skb_reset_network_header(reply
);
1685 skb_put(reply
, sizeof(struct ipv6hdr
));
1689 pip6
= ipv6_hdr(reply
);
1690 memset(pip6
, 0, sizeof(struct ipv6hdr
));
1692 pip6
->priority
= ipv6_hdr(request
)->priority
;
1693 pip6
->nexthdr
= IPPROTO_ICMPV6
;
1694 pip6
->hop_limit
= 255;
1695 pip6
->daddr
= ipv6_hdr(request
)->saddr
;
1696 pip6
->saddr
= *(struct in6_addr
*)n
->primary_key
;
1698 skb_pull(reply
, sizeof(struct ipv6hdr
));
1699 skb_reset_transport_header(reply
);
1701 /* Neighbor Advertisement */
1702 na
= skb_put_zero(reply
, sizeof(*na
) + na_olen
);
1703 na
->icmph
.icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
1704 na
->icmph
.icmp6_router
= isrouter
;
1705 na
->icmph
.icmp6_override
= 1;
1706 na
->icmph
.icmp6_solicited
= 1;
1707 na
->target
= ns
->target
;
1708 ether_addr_copy(&na
->opt
[2], n
->ha
);
1709 na
->opt
[0] = ND_OPT_TARGET_LL_ADDR
;
1710 na
->opt
[1] = na_olen
>> 3;
1712 na
->icmph
.icmp6_cksum
= csum_ipv6_magic(&pip6
->saddr
,
1713 &pip6
->daddr
, sizeof(*na
)+na_olen
, IPPROTO_ICMPV6
,
1714 csum_partial(na
, sizeof(*na
)+na_olen
, 0));
1716 pip6
->payload_len
= htons(sizeof(*na
)+na_olen
);
1718 skb_push(reply
, sizeof(struct ipv6hdr
));
1720 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1725 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
, __be32 vni
)
1727 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1728 const struct in6_addr
*daddr
;
1729 const struct ipv6hdr
*iphdr
;
1730 struct inet6_dev
*in6_dev
;
1731 struct neighbour
*n
;
1734 in6_dev
= __in6_dev_get(dev
);
1738 iphdr
= ipv6_hdr(skb
);
1739 daddr
= &iphdr
->daddr
;
1740 msg
= (struct nd_msg
*)(iphdr
+ 1);
1742 if (ipv6_addr_loopback(daddr
) ||
1743 ipv6_addr_is_multicast(&msg
->target
))
1746 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1749 struct vxlan_fdb
*f
;
1750 struct sk_buff
*reply
;
1752 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1757 f
= vxlan_find_mac(vxlan
, n
->ha
, vni
);
1758 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1759 /* bridge-local neighbor */
1764 reply
= vxlan_na_create(skb
, n
,
1765 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1772 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1773 dev
->stats
.rx_dropped
++;
1775 } else if (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
) {
1776 union vxlan_addr ipa
= {
1777 .sin6
.sin6_addr
= msg
->target
,
1778 .sin6
.sin6_family
= AF_INET6
,
1781 vxlan_ip_miss(dev
, &ipa
);
1786 return NETDEV_TX_OK
;
1790 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1792 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1793 struct neighbour
*n
;
1795 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1799 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1804 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1807 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1808 if (!n
&& (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) {
1809 union vxlan_addr ipa
= {
1810 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1811 .sin
.sin_family
= AF_INET
,
1814 vxlan_ip_miss(dev
, &ipa
);
1820 #if IS_ENABLED(CONFIG_IPV6)
1823 struct ipv6hdr
*pip6
;
1825 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1827 pip6
= ipv6_hdr(skb
);
1828 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1829 if (!n
&& (vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) {
1830 union vxlan_addr ipa
= {
1831 .sin6
.sin6_addr
= pip6
->daddr
,
1832 .sin6
.sin6_family
= AF_INET6
,
1835 vxlan_ip_miss(dev
, &ipa
);
1849 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1851 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1853 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1862 static void vxlan_build_gbp_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1863 struct vxlan_metadata
*md
)
1865 struct vxlanhdr_gbp
*gbp
;
1870 gbp
= (struct vxlanhdr_gbp
*)vxh
;
1871 vxh
->vx_flags
|= VXLAN_HF_GBP
;
1873 if (md
->gbp
& VXLAN_GBP_DONT_LEARN
)
1874 gbp
->dont_learn
= 1;
1876 if (md
->gbp
& VXLAN_GBP_POLICY_APPLIED
)
1877 gbp
->policy_applied
= 1;
1879 gbp
->policy_id
= htons(md
->gbp
& VXLAN_GBP_ID_MASK
);
1882 static int vxlan_build_gpe_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1885 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)vxh
;
1887 gpe
->np_applied
= 1;
1888 gpe
->next_protocol
= tun_p_from_eth_p(protocol
);
1889 if (!gpe
->next_protocol
)
1890 return -EPFNOSUPPORT
;
1894 static int vxlan_build_skb(struct sk_buff
*skb
, struct dst_entry
*dst
,
1895 int iphdr_len
, __be32 vni
,
1896 struct vxlan_metadata
*md
, u32 vxflags
,
1899 struct vxlanhdr
*vxh
;
1902 int type
= udp_sum
? SKB_GSO_UDP_TUNNEL_CSUM
: SKB_GSO_UDP_TUNNEL
;
1903 __be16 inner_protocol
= htons(ETH_P_TEB
);
1905 if ((vxflags
& VXLAN_F_REMCSUM_TX
) &&
1906 skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1907 int csum_start
= skb_checksum_start_offset(skb
);
1909 if (csum_start
<= VXLAN_MAX_REMCSUM_START
&&
1910 !(csum_start
& VXLAN_RCO_SHIFT_MASK
) &&
1911 (skb
->csum_offset
== offsetof(struct udphdr
, check
) ||
1912 skb
->csum_offset
== offsetof(struct tcphdr
, check
)))
1913 type
|= SKB_GSO_TUNNEL_REMCSUM
;
1916 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1917 + VXLAN_HLEN
+ iphdr_len
;
1919 /* Need space for new headers (invalidates iph ptr) */
1920 err
= skb_cow_head(skb
, min_headroom
);
1924 err
= iptunnel_handle_offloads(skb
, type
);
1928 vxh
= __skb_push(skb
, sizeof(*vxh
));
1929 vxh
->vx_flags
= VXLAN_HF_VNI
;
1930 vxh
->vx_vni
= vxlan_vni_field(vni
);
1932 if (type
& SKB_GSO_TUNNEL_REMCSUM
) {
1935 start
= skb_checksum_start_offset(skb
) - sizeof(struct vxlanhdr
);
1936 vxh
->vx_vni
|= vxlan_compute_rco(start
, skb
->csum_offset
);
1937 vxh
->vx_flags
|= VXLAN_HF_RCO
;
1939 if (!skb_is_gso(skb
)) {
1940 skb
->ip_summed
= CHECKSUM_NONE
;
1941 skb
->encapsulation
= 0;
1945 if (vxflags
& VXLAN_F_GBP
)
1946 vxlan_build_gbp_hdr(vxh
, vxflags
, md
);
1947 if (vxflags
& VXLAN_F_GPE
) {
1948 err
= vxlan_build_gpe_hdr(vxh
, vxflags
, skb
->protocol
);
1951 inner_protocol
= skb
->protocol
;
1954 skb_set_inner_protocol(skb
, inner_protocol
);
1958 static struct rtable
*vxlan_get_route(struct vxlan_dev
*vxlan
, struct net_device
*dev
,
1959 struct vxlan_sock
*sock4
,
1960 struct sk_buff
*skb
, int oif
, u8 tos
,
1961 __be32 daddr
, __be32
*saddr
, __be16 dport
, __be16 sport
,
1962 struct dst_cache
*dst_cache
,
1963 const struct ip_tunnel_info
*info
)
1965 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
1966 struct rtable
*rt
= NULL
;
1970 return ERR_PTR(-EIO
);
1975 rt
= dst_cache_get_ip4(dst_cache
, saddr
);
1980 memset(&fl4
, 0, sizeof(fl4
));
1981 fl4
.flowi4_oif
= oif
;
1982 fl4
.flowi4_tos
= RT_TOS(tos
);
1983 fl4
.flowi4_mark
= skb
->mark
;
1984 fl4
.flowi4_proto
= IPPROTO_UDP
;
1987 fl4
.fl4_dport
= dport
;
1988 fl4
.fl4_sport
= sport
;
1990 rt
= ip_route_output_key(vxlan
->net
, &fl4
);
1991 if (likely(!IS_ERR(rt
))) {
1992 if (rt
->dst
.dev
== dev
) {
1993 netdev_dbg(dev
, "circular route to %pI4\n", &daddr
);
1995 return ERR_PTR(-ELOOP
);
2000 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
.saddr
);
2002 netdev_dbg(dev
, "no route to %pI4\n", &daddr
);
2003 return ERR_PTR(-ENETUNREACH
);
2008 #if IS_ENABLED(CONFIG_IPV6)
2009 static struct dst_entry
*vxlan6_get_route(struct vxlan_dev
*vxlan
,
2010 struct net_device
*dev
,
2011 struct vxlan_sock
*sock6
,
2012 struct sk_buff
*skb
, int oif
, u8 tos
,
2014 const struct in6_addr
*daddr
,
2015 struct in6_addr
*saddr
,
2016 __be16 dport
, __be16 sport
,
2017 struct dst_cache
*dst_cache
,
2018 const struct ip_tunnel_info
*info
)
2020 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
2021 struct dst_entry
*ndst
;
2026 return ERR_PTR(-EIO
);
2031 ndst
= dst_cache_get_ip6(dst_cache
, saddr
);
2036 memset(&fl6
, 0, sizeof(fl6
));
2037 fl6
.flowi6_oif
= oif
;
2040 fl6
.flowlabel
= ip6_make_flowinfo(RT_TOS(tos
), label
);
2041 fl6
.flowi6_mark
= skb
->mark
;
2042 fl6
.flowi6_proto
= IPPROTO_UDP
;
2043 fl6
.fl6_dport
= dport
;
2044 fl6
.fl6_sport
= sport
;
2046 err
= ipv6_stub
->ipv6_dst_lookup(vxlan
->net
,
2049 if (unlikely(err
< 0)) {
2050 netdev_dbg(dev
, "no route to %pI6\n", daddr
);
2051 return ERR_PTR(-ENETUNREACH
);
2054 if (unlikely(ndst
->dev
== dev
)) {
2055 netdev_dbg(dev
, "circular route to %pI6\n", daddr
);
2057 return ERR_PTR(-ELOOP
);
2062 dst_cache_set_ip6(dst_cache
, ndst
, saddr
);
2067 /* Bypass encapsulation if the destination is local */
2068 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
2069 struct vxlan_dev
*dst_vxlan
, __be32 vni
)
2071 struct pcpu_sw_netstats
*tx_stats
, *rx_stats
;
2072 union vxlan_addr loopback
;
2073 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
2074 struct net_device
*dev
= skb
->dev
;
2077 tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
2078 rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
2079 skb
->pkt_type
= PACKET_HOST
;
2080 skb
->encapsulation
= 0;
2081 skb
->dev
= dst_vxlan
->dev
;
2082 __skb_pull(skb
, skb_network_offset(skb
));
2084 if (remote_ip
->sa
.sa_family
== AF_INET
) {
2085 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2086 loopback
.sa
.sa_family
= AF_INET
;
2087 #if IS_ENABLED(CONFIG_IPV6)
2089 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
2090 loopback
.sa
.sa_family
= AF_INET6
;
2094 if (dst_vxlan
->cfg
.flags
& VXLAN_F_LEARN
)
2095 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
, 0,
2098 u64_stats_update_begin(&tx_stats
->syncp
);
2099 tx_stats
->tx_packets
++;
2100 tx_stats
->tx_bytes
+= len
;
2101 u64_stats_update_end(&tx_stats
->syncp
);
2103 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
2104 u64_stats_update_begin(&rx_stats
->syncp
);
2105 rx_stats
->rx_packets
++;
2106 rx_stats
->rx_bytes
+= len
;
2107 u64_stats_update_end(&rx_stats
->syncp
);
2109 dev
->stats
.rx_dropped
++;
2113 static int encap_bypass_if_local(struct sk_buff
*skb
, struct net_device
*dev
,
2114 struct vxlan_dev
*vxlan
,
2115 union vxlan_addr
*daddr
,
2116 __be16 dst_port
, int dst_ifindex
, __be32 vni
,
2117 struct dst_entry
*dst
,
2120 #if IS_ENABLED(CONFIG_IPV6)
2121 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2122 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2123 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2125 BUILD_BUG_ON(RTCF_LOCAL
!= RTF_LOCAL
);
2127 /* Bypass encapsulation if the destination is local */
2128 if (rt_flags
& RTCF_LOCAL
&&
2129 !(rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
2130 struct vxlan_dev
*dst_vxlan
;
2133 dst_vxlan
= vxlan_find_vni(vxlan
->net
, dst_ifindex
, vni
,
2134 daddr
->sa
.sa_family
, dst_port
,
2137 dev
->stats
.tx_errors
++;
2142 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
, vni
);
2149 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
2150 __be32 default_vni
, struct vxlan_rdst
*rdst
,
2153 struct dst_cache
*dst_cache
;
2154 struct ip_tunnel_info
*info
;
2155 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2156 const struct iphdr
*old_iph
= ip_hdr(skb
);
2157 union vxlan_addr
*dst
;
2158 union vxlan_addr remote_ip
, local_ip
;
2159 struct vxlan_metadata _md
;
2160 struct vxlan_metadata
*md
= &_md
;
2161 __be16 src_port
= 0, dst_port
;
2162 struct dst_entry
*ndst
= NULL
;
2167 u32 flags
= vxlan
->cfg
.flags
;
2168 bool udp_sum
= false;
2169 bool xnet
= !net_eq(vxlan
->net
, dev_net(vxlan
->dev
));
2171 info
= skb_tunnel_info(skb
);
2174 dst
= &rdst
->remote_ip
;
2175 if (vxlan_addr_any(dst
)) {
2177 /* short-circuited back to local bridge */
2178 vxlan_encap_bypass(skb
, vxlan
, vxlan
, default_vni
);
2184 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->cfg
.dst_port
;
2185 vni
= (rdst
->remote_vni
) ? : default_vni
;
2186 ifindex
= rdst
->remote_ifindex
;
2187 local_ip
= vxlan
->cfg
.saddr
;
2188 dst_cache
= &rdst
->dst_cache
;
2189 md
->gbp
= skb
->mark
;
2190 if (flags
& VXLAN_F_TTL_INHERIT
) {
2191 ttl
= ip_tunnel_get_ttl(old_iph
, skb
);
2193 ttl
= vxlan
->cfg
.ttl
;
2194 if (!ttl
&& vxlan_addr_multicast(dst
))
2198 tos
= vxlan
->cfg
.tos
;
2200 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
2202 if (dst
->sa
.sa_family
== AF_INET
)
2203 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM_TX
);
2205 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
);
2206 label
= vxlan
->cfg
.label
;
2209 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2213 remote_ip
.sa
.sa_family
= ip_tunnel_info_af(info
);
2214 if (remote_ip
.sa
.sa_family
== AF_INET
) {
2215 remote_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.dst
;
2216 local_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.src
;
2218 remote_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.dst
;
2219 local_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.src
;
2222 dst_port
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2223 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2225 dst_cache
= &info
->dst_cache
;
2226 if (info
->options_len
&&
2227 info
->key
.tun_flags
& TUNNEL_VXLAN_OPT
)
2228 md
= ip_tunnel_info_opts(info
);
2229 ttl
= info
->key
.ttl
;
2230 tos
= info
->key
.tos
;
2231 label
= info
->key
.label
;
2232 udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
2234 src_port
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2235 vxlan
->cfg
.port_max
, true);
2238 if (dst
->sa
.sa_family
== AF_INET
) {
2239 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2243 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
, ifindex
, tos
,
2244 dst
->sin
.sin_addr
.s_addr
,
2245 &local_ip
.sin
.sin_addr
.s_addr
,
2253 /* Bypass encapsulation if the destination is local */
2255 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2256 dst_port
, ifindex
, vni
,
2257 &rt
->dst
, rt
->rt_flags
);
2260 } else if (info
->key
.tun_flags
& TUNNEL_DONT_FRAGMENT
) {
2265 skb_tunnel_check_pmtu(skb
, ndst
, VXLAN_HEADROOM
);
2267 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2268 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
2269 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct iphdr
),
2270 vni
, md
, flags
, udp_sum
);
2274 udp_tunnel_xmit_skb(rt
, sock4
->sock
->sk
, skb
, local_ip
.sin
.sin_addr
.s_addr
,
2275 dst
->sin
.sin_addr
.s_addr
, tos
, ttl
, df
,
2276 src_port
, dst_port
, xnet
, !udp_sum
);
2277 #if IS_ENABLED(CONFIG_IPV6)
2279 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2281 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
, ifindex
, tos
,
2282 label
, &dst
->sin6
.sin6_addr
,
2283 &local_ip
.sin6
.sin6_addr
,
2287 err
= PTR_ERR(ndst
);
2293 u32 rt6i_flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
2295 err
= encap_bypass_if_local(skb
, dev
, vxlan
, dst
,
2296 dst_port
, ifindex
, vni
,
2302 skb_tunnel_check_pmtu(skb
, ndst
, VXLAN6_HEADROOM
);
2304 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2305 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
2306 skb_scrub_packet(skb
, xnet
);
2307 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct ipv6hdr
),
2308 vni
, md
, flags
, udp_sum
);
2312 udp_tunnel6_xmit_skb(ndst
, sock6
->sock
->sk
, skb
, dev
,
2313 &local_ip
.sin6
.sin6_addr
,
2314 &dst
->sin6
.sin6_addr
, tos
, ttl
,
2315 label
, src_port
, dst_port
, !udp_sum
);
2323 dev
->stats
.tx_dropped
++;
2330 dev
->stats
.collisions
++;
2331 else if (err
== -ENETUNREACH
)
2332 dev
->stats
.tx_carrier_errors
++;
2334 dev
->stats
.tx_errors
++;
2338 /* Transmit local packets over Vxlan
2340 * Outer IP header inherits ECN and DF from inner header.
2341 * Outer UDP destination is the VXLAN assigned port.
2342 * source port is based on hash of flow
2344 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
2346 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2347 struct vxlan_rdst
*rdst
, *fdst
= NULL
;
2348 const struct ip_tunnel_info
*info
;
2349 bool did_rsc
= false;
2350 struct vxlan_fdb
*f
;
2354 info
= skb_tunnel_info(skb
);
2356 skb_reset_mac_header(skb
);
2358 if (vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
) {
2359 if (info
&& info
->mode
& IP_TUNNEL_INFO_BRIDGE
&&
2360 info
->mode
& IP_TUNNEL_INFO_TX
) {
2361 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2363 if (info
&& info
->mode
& IP_TUNNEL_INFO_TX
)
2364 vxlan_xmit_one(skb
, dev
, vni
, NULL
, false);
2367 return NETDEV_TX_OK
;
2371 if (vxlan
->cfg
.flags
& VXLAN_F_PROXY
) {
2373 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
2374 return arp_reduce(dev
, skb
, vni
);
2375 #if IS_ENABLED(CONFIG_IPV6)
2376 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
&&
2377 pskb_may_pull(skb
, sizeof(struct ipv6hdr
) +
2378 sizeof(struct nd_msg
)) &&
2379 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
2380 struct nd_msg
*m
= (struct nd_msg
*)(ipv6_hdr(skb
) + 1);
2382 if (m
->icmph
.icmp6_code
== 0 &&
2383 m
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
)
2384 return neigh_reduce(dev
, skb
, vni
);
2390 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2393 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->cfg
.flags
& VXLAN_F_RSC
) &&
2394 (ntohs(eth
->h_proto
) == ETH_P_IP
||
2395 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
2396 did_rsc
= route_shortcircuit(dev
, skb
);
2398 f
= vxlan_find_mac(vxlan
, eth
->h_dest
, vni
);
2402 f
= vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2404 if ((vxlan
->cfg
.flags
& VXLAN_F_L2MISS
) &&
2405 !is_multicast_ether_addr(eth
->h_dest
))
2406 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
2408 dev
->stats
.tx_dropped
++;
2410 return NETDEV_TX_OK
;
2414 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
2415 struct sk_buff
*skb1
;
2421 skb1
= skb_clone(skb
, GFP_ATOMIC
);
2423 vxlan_xmit_one(skb1
, dev
, vni
, rdst
, did_rsc
);
2427 vxlan_xmit_one(skb
, dev
, vni
, fdst
, did_rsc
);
2430 return NETDEV_TX_OK
;
2433 /* Walk the forwarding table and purge stale entries */
2434 static void vxlan_cleanup(struct timer_list
*t
)
2436 struct vxlan_dev
*vxlan
= from_timer(vxlan
, t
, age_timer
);
2437 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
2440 if (!netif_running(vxlan
->dev
))
2443 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2444 struct hlist_node
*p
, *n
;
2446 spin_lock_bh(&vxlan
->hash_lock
);
2447 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2449 = container_of(p
, struct vxlan_fdb
, hlist
);
2450 unsigned long timeout
;
2452 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
2455 if (f
->flags
& NTF_EXT_LEARNED
)
2458 timeout
= f
->used
+ vxlan
->cfg
.age_interval
* HZ
;
2459 if (time_before_eq(timeout
, jiffies
)) {
2460 netdev_dbg(vxlan
->dev
,
2461 "garbage collect %pM\n",
2463 f
->state
= NUD_STALE
;
2464 vxlan_fdb_destroy(vxlan
, f
, true);
2465 } else if (time_before(timeout
, next_timer
))
2466 next_timer
= timeout
;
2468 spin_unlock_bh(&vxlan
->hash_lock
);
2471 mod_timer(&vxlan
->age_timer
, next_timer
);
2474 static void vxlan_vs_del_dev(struct vxlan_dev
*vxlan
)
2476 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2478 spin_lock(&vn
->sock_lock
);
2479 hlist_del_init_rcu(&vxlan
->hlist4
.hlist
);
2480 #if IS_ENABLED(CONFIG_IPV6)
2481 hlist_del_init_rcu(&vxlan
->hlist6
.hlist
);
2483 spin_unlock(&vn
->sock_lock
);
2486 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
,
2487 struct vxlan_dev_node
*node
)
2489 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2490 __be32 vni
= vxlan
->default_dst
.remote_vni
;
2492 node
->vxlan
= vxlan
;
2493 spin_lock(&vn
->sock_lock
);
2494 hlist_add_head_rcu(&node
->hlist
, vni_head(vs
, vni
));
2495 spin_unlock(&vn
->sock_lock
);
2498 /* Setup stats when device is created */
2499 static int vxlan_init(struct net_device
*dev
)
2501 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
2508 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
, __be32 vni
)
2510 struct vxlan_fdb
*f
;
2512 spin_lock_bh(&vxlan
->hash_lock
);
2513 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
, vni
);
2515 vxlan_fdb_destroy(vxlan
, f
, true);
2516 spin_unlock_bh(&vxlan
->hash_lock
);
2519 static void vxlan_uninit(struct net_device
*dev
)
2521 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2523 vxlan_fdb_delete_default(vxlan
, vxlan
->cfg
.vni
);
2525 free_percpu(dev
->tstats
);
2528 /* Start ageing timer and join group when device is brought up */
2529 static int vxlan_open(struct net_device
*dev
)
2531 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2534 ret
= vxlan_sock_add(vxlan
);
2538 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
)) {
2539 ret
= vxlan_igmp_join(vxlan
);
2540 if (ret
== -EADDRINUSE
)
2543 vxlan_sock_release(vxlan
);
2548 if (vxlan
->cfg
.age_interval
)
2549 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2554 /* Purge the forwarding table */
2555 static void vxlan_flush(struct vxlan_dev
*vxlan
, bool do_all
)
2559 spin_lock_bh(&vxlan
->hash_lock
);
2560 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2561 struct hlist_node
*p
, *n
;
2562 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2564 = container_of(p
, struct vxlan_fdb
, hlist
);
2565 if (!do_all
&& (f
->state
& (NUD_PERMANENT
| NUD_NOARP
)))
2567 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2568 if (!is_zero_ether_addr(f
->eth_addr
))
2569 vxlan_fdb_destroy(vxlan
, f
, true);
2572 spin_unlock_bh(&vxlan
->hash_lock
);
2575 /* Cleanup timer and forwarding table on shutdown */
2576 static int vxlan_stop(struct net_device
*dev
)
2578 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2579 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2582 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2583 !vxlan_group_used(vn
, vxlan
))
2584 ret
= vxlan_igmp_leave(vxlan
);
2586 del_timer_sync(&vxlan
->age_timer
);
2588 vxlan_flush(vxlan
, false);
2589 vxlan_sock_release(vxlan
);
2594 /* Stub, nothing needs to be done. */
2595 static void vxlan_set_multicast_list(struct net_device
*dev
)
2599 static int vxlan_change_mtu(struct net_device
*dev
, int new_mtu
)
2601 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2602 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2603 struct net_device
*lowerdev
= __dev_get_by_index(vxlan
->net
,
2604 dst
->remote_ifindex
);
2605 bool use_ipv6
= !!(vxlan
->cfg
.flags
& VXLAN_F_IPV6
);
2607 /* This check is different than dev->max_mtu, because it looks at
2608 * the lowerdev->mtu, rather than the static dev->max_mtu
2611 int max_mtu
= lowerdev
->mtu
-
2612 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2613 if (new_mtu
> max_mtu
)
2621 static int vxlan_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
2623 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2624 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
2625 __be16 sport
, dport
;
2627 sport
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2628 vxlan
->cfg
.port_max
, true);
2629 dport
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2631 if (ip_tunnel_info_af(info
) == AF_INET
) {
2632 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2635 rt
= vxlan_get_route(vxlan
, dev
, sock4
, skb
, 0, info
->key
.tos
,
2636 info
->key
.u
.ipv4
.dst
,
2637 &info
->key
.u
.ipv4
.src
, dport
, sport
,
2638 &info
->dst_cache
, info
);
2643 #if IS_ENABLED(CONFIG_IPV6)
2644 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2645 struct dst_entry
*ndst
;
2647 ndst
= vxlan6_get_route(vxlan
, dev
, sock6
, skb
, 0, info
->key
.tos
,
2648 info
->key
.label
, &info
->key
.u
.ipv6
.dst
,
2649 &info
->key
.u
.ipv6
.src
, dport
, sport
,
2650 &info
->dst_cache
, info
);
2652 return PTR_ERR(ndst
);
2654 #else /* !CONFIG_IPV6 */
2655 return -EPFNOSUPPORT
;
2658 info
->key
.tp_src
= sport
;
2659 info
->key
.tp_dst
= dport
;
2663 static const struct net_device_ops vxlan_netdev_ether_ops
= {
2664 .ndo_init
= vxlan_init
,
2665 .ndo_uninit
= vxlan_uninit
,
2666 .ndo_open
= vxlan_open
,
2667 .ndo_stop
= vxlan_stop
,
2668 .ndo_start_xmit
= vxlan_xmit
,
2669 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2670 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2671 .ndo_change_mtu
= vxlan_change_mtu
,
2672 .ndo_validate_addr
= eth_validate_addr
,
2673 .ndo_set_mac_address
= eth_mac_addr
,
2674 .ndo_fdb_add
= vxlan_fdb_add
,
2675 .ndo_fdb_del
= vxlan_fdb_delete
,
2676 .ndo_fdb_dump
= vxlan_fdb_dump
,
2677 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2680 static const struct net_device_ops vxlan_netdev_raw_ops
= {
2681 .ndo_init
= vxlan_init
,
2682 .ndo_uninit
= vxlan_uninit
,
2683 .ndo_open
= vxlan_open
,
2684 .ndo_stop
= vxlan_stop
,
2685 .ndo_start_xmit
= vxlan_xmit
,
2686 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2687 .ndo_change_mtu
= vxlan_change_mtu
,
2688 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2691 /* Info for udev, that this is a virtual tunnel endpoint */
2692 static struct device_type vxlan_type
= {
2696 /* Calls the ndo_udp_tunnel_add of the caller in order to
2697 * supply the listening VXLAN udp ports. Callers are expected
2698 * to implement the ndo_udp_tunnel_add.
2700 static void vxlan_offload_rx_ports(struct net_device
*dev
, bool push
)
2702 struct vxlan_sock
*vs
;
2703 struct net
*net
= dev_net(dev
);
2704 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2707 spin_lock(&vn
->sock_lock
);
2708 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2709 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
) {
2710 unsigned short type
;
2712 if (vs
->flags
& VXLAN_F_GPE
)
2713 type
= UDP_TUNNEL_TYPE_VXLAN_GPE
;
2715 type
= UDP_TUNNEL_TYPE_VXLAN
;
2718 udp_tunnel_push_rx_port(dev
, vs
->sock
, type
);
2720 udp_tunnel_drop_rx_port(dev
, vs
->sock
, type
);
2723 spin_unlock(&vn
->sock_lock
);
2726 /* Initialize the device structure. */
2727 static void vxlan_setup(struct net_device
*dev
)
2729 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2732 eth_hw_addr_random(dev
);
2735 dev
->needs_free_netdev
= true;
2736 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2738 dev
->features
|= NETIF_F_LLTX
;
2739 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2740 dev
->features
|= NETIF_F_RXCSUM
;
2741 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2743 dev
->vlan_features
= dev
->features
;
2744 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2745 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2746 netif_keep_dst(dev
);
2747 dev
->priv_flags
|= IFF_NO_QUEUE
;
2749 /* MTU range: 68 - 65535 */
2750 dev
->min_mtu
= ETH_MIN_MTU
;
2751 dev
->max_mtu
= ETH_MAX_MTU
;
2753 INIT_LIST_HEAD(&vxlan
->next
);
2754 spin_lock_init(&vxlan
->hash_lock
);
2756 timer_setup(&vxlan
->age_timer
, vxlan_cleanup
, TIMER_DEFERRABLE
);
2760 gro_cells_init(&vxlan
->gro_cells
, dev
);
2762 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2763 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2766 static void vxlan_ether_setup(struct net_device
*dev
)
2768 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
2769 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2770 dev
->netdev_ops
= &vxlan_netdev_ether_ops
;
2773 static void vxlan_raw_setup(struct net_device
*dev
)
2775 dev
->header_ops
= NULL
;
2776 dev
->type
= ARPHRD_NONE
;
2777 dev
->hard_header_len
= 0;
2779 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
2780 dev
->netdev_ops
= &vxlan_netdev_raw_ops
;
2783 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2784 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2785 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2786 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2787 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2788 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2789 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2790 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2791 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2792 [IFLA_VXLAN_LABEL
] = { .type
= NLA_U32
},
2793 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2794 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2795 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2796 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2797 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2798 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2799 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2800 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2801 [IFLA_VXLAN_COLLECT_METADATA
] = { .type
= NLA_U8
},
2802 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2803 [IFLA_VXLAN_UDP_CSUM
] = { .type
= NLA_U8
},
2804 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
2805 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
2806 [IFLA_VXLAN_REMCSUM_TX
] = { .type
= NLA_U8
},
2807 [IFLA_VXLAN_REMCSUM_RX
] = { .type
= NLA_U8
},
2808 [IFLA_VXLAN_GBP
] = { .type
= NLA_FLAG
, },
2809 [IFLA_VXLAN_GPE
] = { .type
= NLA_FLAG
, },
2810 [IFLA_VXLAN_REMCSUM_NOPARTIAL
] = { .type
= NLA_FLAG
},
2811 [IFLA_VXLAN_TTL_INHERIT
] = { .type
= NLA_FLAG
},
2814 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
2815 struct netlink_ext_ack
*extack
)
2817 if (tb
[IFLA_ADDRESS
]) {
2818 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2819 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_ADDRESS
],
2820 "Provided link layer address is not Ethernet");
2824 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2825 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_ADDRESS
],
2826 "Provided Ethernet address is not unicast");
2827 return -EADDRNOTAVAIL
;
2832 u32 mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2834 if (mtu
< ETH_MIN_MTU
|| mtu
> ETH_MAX_MTU
) {
2835 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_MTU
],
2836 "MTU must be between 68 and 65535");
2842 NL_SET_ERR_MSG(extack
,
2843 "Required attributes not provided to perform the operation");
2847 if (data
[IFLA_VXLAN_ID
]) {
2848 u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2850 if (id
>= VXLAN_N_VID
) {
2851 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_VXLAN_ID
],
2852 "VXLAN ID must be lower than 16777216");
2857 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2858 const struct ifla_vxlan_port_range
*p
2859 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2861 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2862 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_VXLAN_PORT_RANGE
],
2863 "Invalid source port range");
2871 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2872 struct ethtool_drvinfo
*drvinfo
)
2874 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2875 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2878 static const struct ethtool_ops vxlan_ethtool_ops
= {
2879 .get_drvinfo
= vxlan_get_drvinfo
,
2880 .get_link
= ethtool_op_get_link
,
2883 static struct socket
*vxlan_create_sock(struct net
*net
, bool ipv6
,
2884 __be16 port
, u32 flags
)
2886 struct socket
*sock
;
2887 struct udp_port_cfg udp_conf
;
2890 memset(&udp_conf
, 0, sizeof(udp_conf
));
2893 udp_conf
.family
= AF_INET6
;
2894 udp_conf
.use_udp6_rx_checksums
=
2895 !(flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
);
2896 udp_conf
.ipv6_v6only
= 1;
2898 udp_conf
.family
= AF_INET
;
2901 udp_conf
.local_udp_port
= port
;
2903 /* Open UDP socket */
2904 err
= udp_sock_create(net
, &udp_conf
, &sock
);
2906 return ERR_PTR(err
);
2911 /* Create new listen socket if needed */
2912 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, bool ipv6
,
2913 __be16 port
, u32 flags
)
2915 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2916 struct vxlan_sock
*vs
;
2917 struct socket
*sock
;
2919 struct udp_tunnel_sock_cfg tunnel_cfg
;
2921 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
);
2923 return ERR_PTR(-ENOMEM
);
2925 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
2926 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
2928 sock
= vxlan_create_sock(net
, ipv6
, port
, flags
);
2931 return ERR_CAST(sock
);
2935 refcount_set(&vs
->refcnt
, 1);
2936 vs
->flags
= (flags
& VXLAN_F_RCV_FLAGS
);
2938 spin_lock(&vn
->sock_lock
);
2939 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
2940 udp_tunnel_notify_add_rx_port(sock
,
2941 (vs
->flags
& VXLAN_F_GPE
) ?
2942 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2943 UDP_TUNNEL_TYPE_VXLAN
);
2944 spin_unlock(&vn
->sock_lock
);
2946 /* Mark socket as an encapsulation socket. */
2947 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
2948 tunnel_cfg
.sk_user_data
= vs
;
2949 tunnel_cfg
.encap_type
= 1;
2950 tunnel_cfg
.encap_rcv
= vxlan_rcv
;
2951 tunnel_cfg
.encap_destroy
= NULL
;
2952 tunnel_cfg
.gro_receive
= vxlan_gro_receive
;
2953 tunnel_cfg
.gro_complete
= vxlan_gro_complete
;
2955 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
2960 static int __vxlan_sock_add(struct vxlan_dev
*vxlan
, bool ipv6
)
2962 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2963 struct vxlan_sock
*vs
= NULL
;
2964 struct vxlan_dev_node
*node
;
2966 if (!vxlan
->cfg
.no_share
) {
2967 spin_lock(&vn
->sock_lock
);
2968 vs
= vxlan_find_sock(vxlan
->net
, ipv6
? AF_INET6
: AF_INET
,
2969 vxlan
->cfg
.dst_port
, vxlan
->cfg
.flags
);
2970 if (vs
&& !refcount_inc_not_zero(&vs
->refcnt
)) {
2971 spin_unlock(&vn
->sock_lock
);
2974 spin_unlock(&vn
->sock_lock
);
2977 vs
= vxlan_socket_create(vxlan
->net
, ipv6
,
2978 vxlan
->cfg
.dst_port
, vxlan
->cfg
.flags
);
2981 #if IS_ENABLED(CONFIG_IPV6)
2983 rcu_assign_pointer(vxlan
->vn6_sock
, vs
);
2984 node
= &vxlan
->hlist6
;
2988 rcu_assign_pointer(vxlan
->vn4_sock
, vs
);
2989 node
= &vxlan
->hlist4
;
2991 vxlan_vs_add_dev(vs
, vxlan
, node
);
2995 static int vxlan_sock_add(struct vxlan_dev
*vxlan
)
2997 bool metadata
= vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
;
2998 bool ipv6
= vxlan
->cfg
.flags
& VXLAN_F_IPV6
|| metadata
;
2999 bool ipv4
= !ipv6
|| metadata
;
3002 RCU_INIT_POINTER(vxlan
->vn4_sock
, NULL
);
3003 #if IS_ENABLED(CONFIG_IPV6)
3004 RCU_INIT_POINTER(vxlan
->vn6_sock
, NULL
);
3006 ret
= __vxlan_sock_add(vxlan
, true);
3007 if (ret
< 0 && ret
!= -EAFNOSUPPORT
)
3012 ret
= __vxlan_sock_add(vxlan
, false);
3014 vxlan_sock_release(vxlan
);
3018 static int vxlan_config_validate(struct net
*src_net
, struct vxlan_config
*conf
,
3019 struct net_device
**lower
,
3020 struct vxlan_dev
*old
,
3021 struct netlink_ext_ack
*extack
)
3023 struct vxlan_net
*vn
= net_generic(src_net
, vxlan_net_id
);
3024 struct vxlan_dev
*tmp
;
3025 bool use_ipv6
= false;
3027 if (conf
->flags
& VXLAN_F_GPE
) {
3028 /* For now, allow GPE only together with
3029 * COLLECT_METADATA. This can be relaxed later; in such
3030 * case, the other side of the PtP link will have to be
3033 if ((conf
->flags
& ~VXLAN_F_ALLOWED_GPE
) ||
3034 !(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
3035 NL_SET_ERR_MSG(extack
,
3036 "VXLAN GPE does not support this combination of attributes");
3041 if (!conf
->remote_ip
.sa
.sa_family
&& !conf
->saddr
.sa
.sa_family
) {
3042 /* Unless IPv6 is explicitly requested, assume IPv4 */
3043 conf
->remote_ip
.sa
.sa_family
= AF_INET
;
3044 conf
->saddr
.sa
.sa_family
= AF_INET
;
3045 } else if (!conf
->remote_ip
.sa
.sa_family
) {
3046 conf
->remote_ip
.sa
.sa_family
= conf
->saddr
.sa
.sa_family
;
3047 } else if (!conf
->saddr
.sa
.sa_family
) {
3048 conf
->saddr
.sa
.sa_family
= conf
->remote_ip
.sa
.sa_family
;
3051 if (conf
->saddr
.sa
.sa_family
!= conf
->remote_ip
.sa
.sa_family
) {
3052 NL_SET_ERR_MSG(extack
,
3053 "Local and remote address must be from the same family");
3057 if (vxlan_addr_multicast(&conf
->saddr
)) {
3058 NL_SET_ERR_MSG(extack
, "Local address cannot be multicast");
3062 if (conf
->saddr
.sa
.sa_family
== AF_INET6
) {
3063 if (!IS_ENABLED(CONFIG_IPV6
)) {
3064 NL_SET_ERR_MSG(extack
,
3065 "IPv6 support not enabled in the kernel");
3066 return -EPFNOSUPPORT
;
3069 conf
->flags
|= VXLAN_F_IPV6
;
3071 if (!(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
3073 ipv6_addr_type(&conf
->saddr
.sin6
.sin6_addr
);
3075 ipv6_addr_type(&conf
->remote_ip
.sin6
.sin6_addr
);
3077 if (local_type
& IPV6_ADDR_LINKLOCAL
) {
3078 if (!(remote_type
& IPV6_ADDR_LINKLOCAL
) &&
3079 (remote_type
!= IPV6_ADDR_ANY
)) {
3080 NL_SET_ERR_MSG(extack
,
3081 "Invalid combination of local and remote address scopes");
3085 conf
->flags
|= VXLAN_F_IPV6_LINKLOCAL
;
3088 (IPV6_ADDR_UNICAST
| IPV6_ADDR_LINKLOCAL
)) {
3089 NL_SET_ERR_MSG(extack
,
3090 "Invalid combination of local and remote address scopes");
3094 conf
->flags
&= ~VXLAN_F_IPV6_LINKLOCAL
;
3099 if (conf
->label
&& !use_ipv6
) {
3100 NL_SET_ERR_MSG(extack
,
3101 "Label attribute only applies to IPv6 VXLAN devices");
3105 if (conf
->remote_ifindex
) {
3106 struct net_device
*lowerdev
;
3108 lowerdev
= __dev_get_by_index(src_net
, conf
->remote_ifindex
);
3110 NL_SET_ERR_MSG(extack
,
3111 "Invalid local interface, device not found");
3115 #if IS_ENABLED(CONFIG_IPV6)
3117 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
3118 if (idev
&& idev
->cnf
.disable_ipv6
) {
3119 NL_SET_ERR_MSG(extack
,
3120 "IPv6 support disabled by administrator");
3128 if (vxlan_addr_multicast(&conf
->remote_ip
)) {
3129 NL_SET_ERR_MSG(extack
,
3130 "Local interface required for multicast remote destination");
3135 #if IS_ENABLED(CONFIG_IPV6)
3136 if (conf
->flags
& VXLAN_F_IPV6_LINKLOCAL
) {
3137 NL_SET_ERR_MSG(extack
,
3138 "Local interface required for link-local local/remote addresses");
3146 if (!conf
->dst_port
) {
3147 if (conf
->flags
& VXLAN_F_GPE
)
3148 conf
->dst_port
= htons(4790); /* IANA VXLAN-GPE port */
3150 conf
->dst_port
= htons(vxlan_port
);
3153 if (!conf
->age_interval
)
3154 conf
->age_interval
= FDB_AGE_DEFAULT
;
3156 list_for_each_entry(tmp
, &vn
->vxlan_list
, next
) {
3160 if (tmp
->cfg
.vni
!= conf
->vni
)
3162 if (tmp
->cfg
.dst_port
!= conf
->dst_port
)
3164 if ((tmp
->cfg
.flags
& (VXLAN_F_RCV_FLAGS
| VXLAN_F_IPV6
)) !=
3165 (conf
->flags
& (VXLAN_F_RCV_FLAGS
| VXLAN_F_IPV6
)))
3168 if ((conf
->flags
& VXLAN_F_IPV6_LINKLOCAL
) &&
3169 tmp
->cfg
.remote_ifindex
!= conf
->remote_ifindex
)
3172 NL_SET_ERR_MSG(extack
,
3173 "A VXLAN device with the specified VNI already exists");
3180 static void vxlan_config_apply(struct net_device
*dev
,
3181 struct vxlan_config
*conf
,
3182 struct net_device
*lowerdev
,
3183 struct net
*src_net
,
3186 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3187 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3188 unsigned short needed_headroom
= ETH_HLEN
;
3189 bool use_ipv6
= !!(conf
->flags
& VXLAN_F_IPV6
);
3190 int max_mtu
= ETH_MAX_MTU
;
3193 if (conf
->flags
& VXLAN_F_GPE
)
3194 vxlan_raw_setup(dev
);
3196 vxlan_ether_setup(dev
);
3199 dev
->mtu
= conf
->mtu
;
3201 vxlan
->net
= src_net
;
3204 dst
->remote_vni
= conf
->vni
;
3206 memcpy(&dst
->remote_ip
, &conf
->remote_ip
, sizeof(conf
->remote_ip
));
3209 dst
->remote_ifindex
= conf
->remote_ifindex
;
3211 dev
->gso_max_size
= lowerdev
->gso_max_size
;
3212 dev
->gso_max_segs
= lowerdev
->gso_max_segs
;
3214 needed_headroom
= lowerdev
->hard_header_len
;
3216 max_mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
:
3218 if (max_mtu
< ETH_MIN_MTU
)
3219 max_mtu
= ETH_MIN_MTU
;
3221 if (!changelink
&& !conf
->mtu
)
3225 if (dev
->mtu
> max_mtu
)
3228 if (use_ipv6
|| conf
->flags
& VXLAN_F_COLLECT_METADATA
)
3229 needed_headroom
+= VXLAN6_HEADROOM
;
3231 needed_headroom
+= VXLAN_HEADROOM
;
3232 dev
->needed_headroom
= needed_headroom
;
3234 memcpy(&vxlan
->cfg
, conf
, sizeof(*conf
));
3237 static int vxlan_dev_configure(struct net
*src_net
, struct net_device
*dev
,
3238 struct vxlan_config
*conf
, bool changelink
,
3239 struct netlink_ext_ack
*extack
)
3241 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3242 struct net_device
*lowerdev
;
3245 ret
= vxlan_config_validate(src_net
, conf
, &lowerdev
, vxlan
, extack
);
3249 vxlan_config_apply(dev
, conf
, lowerdev
, src_net
, changelink
);
3254 static int __vxlan_dev_create(struct net
*net
, struct net_device
*dev
,
3255 struct vxlan_config
*conf
,
3256 struct netlink_ext_ack
*extack
)
3258 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3259 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3260 struct vxlan_fdb
*f
= NULL
;
3263 err
= vxlan_dev_configure(net
, dev
, conf
, false, extack
);
3267 dev
->ethtool_ops
= &vxlan_ethtool_ops
;
3269 /* create an fdb entry for a valid default destination */
3270 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
3271 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
3272 &vxlan
->default_dst
.remote_ip
,
3273 NUD_REACHABLE
| NUD_PERMANENT
,
3274 vxlan
->cfg
.dst_port
,
3275 vxlan
->default_dst
.remote_vni
,
3276 vxlan
->default_dst
.remote_vni
,
3277 vxlan
->default_dst
.remote_ifindex
,
3283 err
= register_netdevice(dev
);
3287 err
= rtnl_configure_link(dev
, NULL
);
3289 unregister_netdevice(dev
);
3293 /* notify default fdb entry */
3295 vxlan_fdb_notify(vxlan
, f
, first_remote_rtnl(f
), RTM_NEWNEIGH
);
3297 list_add(&vxlan
->next
, &vn
->vxlan_list
);
3301 vxlan_fdb_destroy(vxlan
, f
, false);
3305 static int vxlan_nl2conf(struct nlattr
*tb
[], struct nlattr
*data
[],
3306 struct net_device
*dev
, struct vxlan_config
*conf
,
3309 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3311 memset(conf
, 0, sizeof(*conf
));
3313 /* if changelink operation, start with old existing cfg */
3315 memcpy(conf
, &vxlan
->cfg
, sizeof(*conf
));
3317 if (data
[IFLA_VXLAN_ID
]) {
3318 __be32 vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3320 if (changelink
&& (vni
!= conf
->vni
))
3322 conf
->vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3325 if (data
[IFLA_VXLAN_GROUP
]) {
3326 if (changelink
&& (conf
->remote_ip
.sa
.sa_family
!= AF_INET
))
3329 conf
->remote_ip
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_GROUP
]);
3330 conf
->remote_ip
.sa
.sa_family
= AF_INET
;
3331 } else if (data
[IFLA_VXLAN_GROUP6
]) {
3332 if (!IS_ENABLED(CONFIG_IPV6
))
3333 return -EPFNOSUPPORT
;
3335 if (changelink
&& (conf
->remote_ip
.sa
.sa_family
!= AF_INET6
))
3338 conf
->remote_ip
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_GROUP6
]);
3339 conf
->remote_ip
.sa
.sa_family
= AF_INET6
;
3342 if (data
[IFLA_VXLAN_LOCAL
]) {
3343 if (changelink
&& (conf
->saddr
.sa
.sa_family
!= AF_INET
))
3346 conf
->saddr
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_LOCAL
]);
3347 conf
->saddr
.sa
.sa_family
= AF_INET
;
3348 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
3349 if (!IS_ENABLED(CONFIG_IPV6
))
3350 return -EPFNOSUPPORT
;
3352 if (changelink
&& (conf
->saddr
.sa
.sa_family
!= AF_INET6
))
3355 /* TODO: respect scope id */
3356 conf
->saddr
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_LOCAL6
]);
3357 conf
->saddr
.sa
.sa_family
= AF_INET6
;
3360 if (data
[IFLA_VXLAN_LINK
])
3361 conf
->remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]);
3363 if (data
[IFLA_VXLAN_TOS
])
3364 conf
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
3366 if (data
[IFLA_VXLAN_TTL
])
3367 conf
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
3369 if (data
[IFLA_VXLAN_TTL_INHERIT
]) {
3372 conf
->flags
|= VXLAN_F_TTL_INHERIT
;
3375 if (data
[IFLA_VXLAN_LABEL
])
3376 conf
->label
= nla_get_be32(data
[IFLA_VXLAN_LABEL
]) &
3377 IPV6_FLOWLABEL_MASK
;
3379 if (data
[IFLA_VXLAN_LEARNING
]) {
3380 if (nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
3381 conf
->flags
|= VXLAN_F_LEARN
;
3383 conf
->flags
&= ~VXLAN_F_LEARN
;
3384 } else if (!changelink
) {
3385 /* default to learn on a new device */
3386 conf
->flags
|= VXLAN_F_LEARN
;
3389 if (data
[IFLA_VXLAN_AGEING
]) {
3392 conf
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
3395 if (data
[IFLA_VXLAN_PROXY
]) {
3398 if (nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
3399 conf
->flags
|= VXLAN_F_PROXY
;
3402 if (data
[IFLA_VXLAN_RSC
]) {
3405 if (nla_get_u8(data
[IFLA_VXLAN_RSC
]))
3406 conf
->flags
|= VXLAN_F_RSC
;
3409 if (data
[IFLA_VXLAN_L2MISS
]) {
3412 if (nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
3413 conf
->flags
|= VXLAN_F_L2MISS
;
3416 if (data
[IFLA_VXLAN_L3MISS
]) {
3419 if (nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
3420 conf
->flags
|= VXLAN_F_L3MISS
;
3423 if (data
[IFLA_VXLAN_LIMIT
]) {
3426 conf
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
3429 if (data
[IFLA_VXLAN_COLLECT_METADATA
]) {
3432 if (nla_get_u8(data
[IFLA_VXLAN_COLLECT_METADATA
]))
3433 conf
->flags
|= VXLAN_F_COLLECT_METADATA
;
3436 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
3438 const struct ifla_vxlan_port_range
*p
3439 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
3440 conf
->port_min
= ntohs(p
->low
);
3441 conf
->port_max
= ntohs(p
->high
);
3447 if (data
[IFLA_VXLAN_PORT
]) {
3450 conf
->dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
3453 if (data
[IFLA_VXLAN_UDP_CSUM
]) {
3456 if (!nla_get_u8(data
[IFLA_VXLAN_UDP_CSUM
]))
3457 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM_TX
;
3460 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]) {
3463 if (nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
3464 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_TX
;
3467 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]) {
3470 if (nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
3471 conf
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_RX
;
3474 if (data
[IFLA_VXLAN_REMCSUM_TX
]) {
3477 if (nla_get_u8(data
[IFLA_VXLAN_REMCSUM_TX
]))
3478 conf
->flags
|= VXLAN_F_REMCSUM_TX
;
3481 if (data
[IFLA_VXLAN_REMCSUM_RX
]) {
3484 if (nla_get_u8(data
[IFLA_VXLAN_REMCSUM_RX
]))
3485 conf
->flags
|= VXLAN_F_REMCSUM_RX
;
3488 if (data
[IFLA_VXLAN_GBP
]) {
3491 conf
->flags
|= VXLAN_F_GBP
;
3494 if (data
[IFLA_VXLAN_GPE
]) {
3497 conf
->flags
|= VXLAN_F_GPE
;
3500 if (data
[IFLA_VXLAN_REMCSUM_NOPARTIAL
]) {
3503 conf
->flags
|= VXLAN_F_REMCSUM_NOPARTIAL
;
3509 conf
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3515 static int vxlan_newlink(struct net
*src_net
, struct net_device
*dev
,
3516 struct nlattr
*tb
[], struct nlattr
*data
[],
3517 struct netlink_ext_ack
*extack
)
3519 struct vxlan_config conf
;
3522 err
= vxlan_nl2conf(tb
, data
, dev
, &conf
, false);
3526 return __vxlan_dev_create(src_net
, dev
, &conf
, extack
);
3529 static int vxlan_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
3530 struct nlattr
*data
[],
3531 struct netlink_ext_ack
*extack
)
3533 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3534 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3535 struct vxlan_rdst old_dst
;
3536 struct vxlan_config conf
;
3537 struct vxlan_fdb
*f
= NULL
;
3540 err
= vxlan_nl2conf(tb
, data
,
3545 memcpy(&old_dst
, dst
, sizeof(struct vxlan_rdst
));
3547 err
= vxlan_dev_configure(vxlan
->net
, dev
, &conf
, true, extack
);
3551 /* handle default dst entry */
3552 if (!vxlan_addr_equal(&dst
->remote_ip
, &old_dst
.remote_ip
)) {
3553 spin_lock_bh(&vxlan
->hash_lock
);
3554 if (!vxlan_addr_any(&old_dst
.remote_ip
))
3555 __vxlan_fdb_delete(vxlan
, all_zeros_mac
,
3557 vxlan
->cfg
.dst_port
,
3560 old_dst
.remote_ifindex
, 0);
3562 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3563 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
3565 NUD_REACHABLE
| NUD_PERMANENT
,
3566 vxlan
->cfg
.dst_port
,
3569 dst
->remote_ifindex
,
3572 spin_unlock_bh(&vxlan
->hash_lock
);
3575 vxlan_fdb_notify(vxlan
, f
, first_remote_rtnl(f
), RTM_NEWNEIGH
);
3577 spin_unlock_bh(&vxlan
->hash_lock
);
3583 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
3585 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3587 vxlan_flush(vxlan
, true);
3589 gro_cells_destroy(&vxlan
->gro_cells
);
3590 list_del(&vxlan
->next
);
3591 unregister_netdevice_queue(dev
, head
);
3594 static size_t vxlan_get_size(const struct net_device
*dev
)
3597 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
3598 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
3599 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
3600 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
3601 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
3602 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL_INHERIT */
3603 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
3604 nla_total_size(sizeof(__be32
)) + /* IFLA_VXLAN_LABEL */
3605 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
3606 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
3607 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
3608 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
3609 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
3610 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_COLLECT_METADATA */
3611 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
3612 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
3613 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
3614 nla_total_size(sizeof(__be16
)) + /* IFLA_VXLAN_PORT */
3615 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_CSUM */
3616 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3617 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3618 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_TX */
3619 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_RX */
3623 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
3625 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3626 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3627 struct ifla_vxlan_port_range ports
= {
3628 .low
= htons(vxlan
->cfg
.port_min
),
3629 .high
= htons(vxlan
->cfg
.port_max
),
3632 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, be32_to_cpu(dst
->remote_vni
)))
3633 goto nla_put_failure
;
3635 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3636 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
3637 if (nla_put_in_addr(skb
, IFLA_VXLAN_GROUP
,
3638 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
3639 goto nla_put_failure
;
3640 #if IS_ENABLED(CONFIG_IPV6)
3642 if (nla_put_in6_addr(skb
, IFLA_VXLAN_GROUP6
,
3643 &dst
->remote_ip
.sin6
.sin6_addr
))
3644 goto nla_put_failure
;
3649 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
3650 goto nla_put_failure
;
3652 if (!vxlan_addr_any(&vxlan
->cfg
.saddr
)) {
3653 if (vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET
) {
3654 if (nla_put_in_addr(skb
, IFLA_VXLAN_LOCAL
,
3655 vxlan
->cfg
.saddr
.sin
.sin_addr
.s_addr
))
3656 goto nla_put_failure
;
3657 #if IS_ENABLED(CONFIG_IPV6)
3659 if (nla_put_in6_addr(skb
, IFLA_VXLAN_LOCAL6
,
3660 &vxlan
->cfg
.saddr
.sin6
.sin6_addr
))
3661 goto nla_put_failure
;
3666 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->cfg
.ttl
) ||
3667 nla_put_u8(skb
, IFLA_VXLAN_TTL_INHERIT
,
3668 !!(vxlan
->cfg
.flags
& VXLAN_F_TTL_INHERIT
)) ||
3669 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->cfg
.tos
) ||
3670 nla_put_be32(skb
, IFLA_VXLAN_LABEL
, vxlan
->cfg
.label
) ||
3671 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
3672 !!(vxlan
->cfg
.flags
& VXLAN_F_LEARN
)) ||
3673 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
3674 !!(vxlan
->cfg
.flags
& VXLAN_F_PROXY
)) ||
3675 nla_put_u8(skb
, IFLA_VXLAN_RSC
,
3676 !!(vxlan
->cfg
.flags
& VXLAN_F_RSC
)) ||
3677 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
3678 !!(vxlan
->cfg
.flags
& VXLAN_F_L2MISS
)) ||
3679 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
3680 !!(vxlan
->cfg
.flags
& VXLAN_F_L3MISS
)) ||
3681 nla_put_u8(skb
, IFLA_VXLAN_COLLECT_METADATA
,
3682 !!(vxlan
->cfg
.flags
& VXLAN_F_COLLECT_METADATA
)) ||
3683 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->cfg
.age_interval
) ||
3684 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->cfg
.addrmax
) ||
3685 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->cfg
.dst_port
) ||
3686 nla_put_u8(skb
, IFLA_VXLAN_UDP_CSUM
,
3687 !(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM_TX
)) ||
3688 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_TX
,
3689 !!(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
)) ||
3690 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_RX
,
3691 !!(vxlan
->cfg
.flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
)) ||
3692 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_TX
,
3693 !!(vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_TX
)) ||
3694 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_RX
,
3695 !!(vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_RX
)))
3696 goto nla_put_failure
;
3698 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
3699 goto nla_put_failure
;
3701 if (vxlan
->cfg
.flags
& VXLAN_F_GBP
&&
3702 nla_put_flag(skb
, IFLA_VXLAN_GBP
))
3703 goto nla_put_failure
;
3705 if (vxlan
->cfg
.flags
& VXLAN_F_GPE
&&
3706 nla_put_flag(skb
, IFLA_VXLAN_GPE
))
3707 goto nla_put_failure
;
3709 if (vxlan
->cfg
.flags
& VXLAN_F_REMCSUM_NOPARTIAL
&&
3710 nla_put_flag(skb
, IFLA_VXLAN_REMCSUM_NOPARTIAL
))
3711 goto nla_put_failure
;
3719 static struct net
*vxlan_get_link_net(const struct net_device
*dev
)
3721 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3726 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
3728 .maxtype
= IFLA_VXLAN_MAX
,
3729 .policy
= vxlan_policy
,
3730 .priv_size
= sizeof(struct vxlan_dev
),
3731 .setup
= vxlan_setup
,
3732 .validate
= vxlan_validate
,
3733 .newlink
= vxlan_newlink
,
3734 .changelink
= vxlan_changelink
,
3735 .dellink
= vxlan_dellink
,
3736 .get_size
= vxlan_get_size
,
3737 .fill_info
= vxlan_fill_info
,
3738 .get_link_net
= vxlan_get_link_net
,
3741 struct net_device
*vxlan_dev_create(struct net
*net
, const char *name
,
3742 u8 name_assign_type
,
3743 struct vxlan_config
*conf
)
3745 struct nlattr
*tb
[IFLA_MAX
+ 1];
3746 struct net_device
*dev
;
3749 memset(&tb
, 0, sizeof(tb
));
3751 dev
= rtnl_create_link(net
, name
, name_assign_type
,
3752 &vxlan_link_ops
, tb
);
3756 err
= __vxlan_dev_create(net
, dev
, conf
, NULL
);
3759 return ERR_PTR(err
);
3762 err
= rtnl_configure_link(dev
, NULL
);
3764 LIST_HEAD(list_kill
);
3766 vxlan_dellink(dev
, &list_kill
);
3767 unregister_netdevice_many(&list_kill
);
3768 return ERR_PTR(err
);
3773 EXPORT_SYMBOL_GPL(vxlan_dev_create
);
3775 static void vxlan_handle_lowerdev_unregister(struct vxlan_net
*vn
,
3776 struct net_device
*dev
)
3778 struct vxlan_dev
*vxlan
, *next
;
3779 LIST_HEAD(list_kill
);
3781 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3782 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3784 /* In case we created vxlan device with carrier
3785 * and we loose the carrier due to module unload
3786 * we also need to remove vxlan device. In other
3787 * cases, it's not necessary and remote_ifindex
3788 * is 0 here, so no matches.
3790 if (dst
->remote_ifindex
== dev
->ifindex
)
3791 vxlan_dellink(vxlan
->dev
, &list_kill
);
3794 unregister_netdevice_many(&list_kill
);
3797 static int vxlan_netdevice_event(struct notifier_block
*unused
,
3798 unsigned long event
, void *ptr
)
3800 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3801 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
3803 if (event
== NETDEV_UNREGISTER
) {
3804 vxlan_offload_rx_ports(dev
, false);
3805 vxlan_handle_lowerdev_unregister(vn
, dev
);
3806 } else if (event
== NETDEV_REGISTER
) {
3807 vxlan_offload_rx_ports(dev
, true);
3808 } else if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
||
3809 event
== NETDEV_UDP_TUNNEL_DROP_INFO
) {
3810 vxlan_offload_rx_ports(dev
, event
== NETDEV_UDP_TUNNEL_PUSH_INFO
);
3816 static struct notifier_block vxlan_notifier_block __read_mostly
= {
3817 .notifier_call
= vxlan_netdevice_event
,
3821 vxlan_fdb_offloaded_set(struct net_device
*dev
,
3822 struct switchdev_notifier_vxlan_fdb_info
*fdb_info
)
3824 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3825 struct vxlan_rdst
*rdst
;
3826 struct vxlan_fdb
*f
;
3828 spin_lock_bh(&vxlan
->hash_lock
);
3830 f
= vxlan_find_mac(vxlan
, fdb_info
->eth_addr
, fdb_info
->vni
);
3834 rdst
= vxlan_fdb_find_rdst(f
, &fdb_info
->remote_ip
,
3835 fdb_info
->remote_port
,
3836 fdb_info
->remote_vni
,
3837 fdb_info
->remote_ifindex
);
3841 rdst
->offloaded
= fdb_info
->offloaded
;
3844 spin_unlock_bh(&vxlan
->hash_lock
);
3847 static int vxlan_switchdev_event(struct notifier_block
*unused
,
3848 unsigned long event
, void *ptr
)
3850 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
3853 case SWITCHDEV_VXLAN_FDB_OFFLOADED
:
3854 vxlan_fdb_offloaded_set(dev
, ptr
);
3861 static struct notifier_block vxlan_switchdev_notifier_block __read_mostly
= {
3862 .notifier_call
= vxlan_switchdev_event
,
3865 static __net_init
int vxlan_init_net(struct net
*net
)
3867 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3870 INIT_LIST_HEAD(&vn
->vxlan_list
);
3871 spin_lock_init(&vn
->sock_lock
);
3873 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
3874 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
3879 static void vxlan_destroy_tunnels(struct net
*net
, struct list_head
*head
)
3881 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3882 struct vxlan_dev
*vxlan
, *next
;
3883 struct net_device
*dev
, *aux
;
3886 for_each_netdev_safe(net
, dev
, aux
)
3887 if (dev
->rtnl_link_ops
== &vxlan_link_ops
)
3888 unregister_netdevice_queue(dev
, head
);
3890 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3891 /* If vxlan->dev is in the same netns, it has already been added
3892 * to the list by the previous loop.
3894 if (!net_eq(dev_net(vxlan
->dev
), net
)) {
3895 gro_cells_destroy(&vxlan
->gro_cells
);
3896 unregister_netdevice_queue(vxlan
->dev
, head
);
3900 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
3901 WARN_ON_ONCE(!hlist_empty(&vn
->sock_list
[h
]));
3904 static void __net_exit
vxlan_exit_batch_net(struct list_head
*net_list
)
3910 list_for_each_entry(net
, net_list
, exit_list
)
3911 vxlan_destroy_tunnels(net
, &list
);
3913 unregister_netdevice_many(&list
);
3917 static struct pernet_operations vxlan_net_ops
= {
3918 .init
= vxlan_init_net
,
3919 .exit_batch
= vxlan_exit_batch_net
,
3920 .id
= &vxlan_net_id
,
3921 .size
= sizeof(struct vxlan_net
),
3924 static int __init
vxlan_init_module(void)
3928 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
3930 rc
= register_pernet_subsys(&vxlan_net_ops
);
3934 rc
= register_netdevice_notifier(&vxlan_notifier_block
);
3938 rc
= register_switchdev_notifier(&vxlan_switchdev_notifier_block
);
3942 rc
= rtnl_link_register(&vxlan_link_ops
);
3948 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block
);
3950 unregister_netdevice_notifier(&vxlan_notifier_block
);
3952 unregister_pernet_subsys(&vxlan_net_ops
);
3956 late_initcall(vxlan_init_module
);
3958 static void __exit
vxlan_cleanup_module(void)
3960 rtnl_link_unregister(&vxlan_link_ops
);
3961 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block
);
3962 unregister_netdevice_notifier(&vxlan_notifier_block
);
3963 unregister_pernet_subsys(&vxlan_net_ops
);
3964 /* rcu_barrier() is called by netns */
3966 module_exit(vxlan_cleanup_module
);
3968 MODULE_LICENSE("GPL");
3969 MODULE_VERSION(VXLAN_VERSION
);
3970 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3971 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3972 MODULE_ALIAS_RTNL_LINK("vxlan");