]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/net/vxlan.c
acpi: apei: read ack upon ghes record consumption
[mirror_ubuntu-zesty-kernel.git] / drivers / net / vxlan.c
CommitLineData
d342894c 1/*
eb5ce439 2 * VXLAN: Virtual eXtensible Local Area Network
d342894c 3 *
3b8df3c6 4 * Copyright (c) 2012-2013 Vyatta Inc.
d342894c 5 *
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.
d342894c 9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
d342894c 14#include <linux/module.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
d342894c 17#include <linux/udp.h>
18#include <linux/igmp.h>
e05202e7 19#include <linux/inetdevice.h>
d342894c 20#include <linux/if_ether.h>
1b13c97f 21#include <linux/ethtool.h>
e4f67add
DS
22#include <net/arp.h>
23#include <net/ndisc.h>
d342894c 24#include <net/ip.h>
25#include <net/icmp.h>
d342894c 26#include <net/rtnetlink.h>
d342894c 27#include <net/inet_ecn.h>
28#include <net/net_namespace.h>
29#include <net/netns/generic.h>
012a5729 30#include <net/vxlan.h>
40d29af0 31
e4c7ed41 32#if IS_ENABLED(CONFIG_IPV6)
e4c7ed41 33#include <net/ip6_tunnel.h>
660d98ca 34#include <net/ip6_checksum.h>
e4c7ed41 35#endif
d342894c 36
37#define VXLAN_VERSION "0.1"
38
553675fb 39#define PORT_HASH_BITS 8
40#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
d342894c 41#define FDB_AGE_DEFAULT 300 /* 5 min */
42#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
43
23c578bf 44/* UDP port for VXLAN traffic.
45 * The IANA assigned port is 4789, but the Linux default is 8472
234f5b73 46 * for compatibility with early adopters.
23c578bf 47 */
9daaa397
SH
48static unsigned short vxlan_port __read_mostly = 8472;
49module_param_named(udp_port, vxlan_port, ushort, 0444);
d342894c 50MODULE_PARM_DESC(udp_port, "Destination UDP port");
51
52static bool log_ecn_error = true;
53module_param(log_ecn_error, bool, 0644);
54MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
55
c7d03a00 56static unsigned int vxlan_net_id;
0dfbdf41 57static struct rtnl_link_ops vxlan_link_ops;
553675fb 58
7256eac1 59static const u8 all_zeros_mac[ETH_ALEN + 2];
afbd8bae 60
205f356d 61static int vxlan_sock_add(struct vxlan_dev *vxlan);
614732ea 62
553675fb 63/* per-network namespace private data for this module */
64struct vxlan_net {
65 struct list_head vxlan_list;
66 struct hlist_head sock_list[PORT_HASH_SIZE];
1c51a915 67 spinlock_t sock_lock;
553675fb 68};
69
d342894c 70/* Forwarding table entry */
71struct vxlan_fdb {
72 struct hlist_node hlist; /* linked list of entries */
73 struct rcu_head rcu;
74 unsigned long updated; /* jiffies */
75 unsigned long used;
3e61aa8f 76 struct list_head remotes;
7177a3b0 77 u8 eth_addr[ETH_ALEN];
d342894c 78 u16 state; /* see ndm_state */
ae884082 79 u8 flags; /* see ndm_flags */
d342894c 80};
81
d342894c 82/* salt for hash table */
83static u32 vxlan_salt __read_mostly;
84
ee122c79
TG
85static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
86{
e7030878
TG
87 return vs->flags & VXLAN_F_COLLECT_METADATA ||
88 ip_tunnel_collect_metadata();
ee122c79
TG
89}
90
e05202e7
JV
91static struct ip_fan_map *vxlan_fan_find_map(struct vxlan_dev *vxlan, __be32 daddr)
92{
93 struct ip_fan_map *fan_map;
94
95 rcu_read_lock();
96 list_for_each_entry_rcu(fan_map, &vxlan->fan.fan_maps, list) {
97 if (fan_map->overlay ==
98 (daddr & inet_make_mask(fan_map->overlay_prefix))) {
99 rcu_read_unlock();
100 return fan_map;
101 }
102 }
103 rcu_read_unlock();
104
105 return NULL;
106}
107
108static void vxlan_fan_flush_map(struct vxlan_dev *vxlan)
109{
110 struct ip_fan_map *fan_map;
111
112 list_for_each_entry_rcu(fan_map, &vxlan->fan.fan_maps, list) {
113 list_del_rcu(&fan_map->list);
114 kfree_rcu(fan_map, rcu);
115 }
116}
117
118static int vxlan_fan_del_map(struct vxlan_dev *vxlan, __be32 overlay)
119{
120 struct ip_fan_map *fan_map;
121
122 fan_map = vxlan_fan_find_map(vxlan, overlay);
123 if (!fan_map)
124 return -ENOENT;
125
126 list_del_rcu(&fan_map->list);
127 kfree_rcu(fan_map, rcu);
128
129 return 0;
130}
131
132static int vxlan_fan_add_map(struct vxlan_dev *vxlan, struct ifla_fan_map *map)
133{
134 __be32 overlay_mask, underlay_mask;
135 struct ip_fan_map *fan_map;
136
137 overlay_mask = inet_make_mask(map->overlay_prefix);
138 underlay_mask = inet_make_mask(map->underlay_prefix);
139
140 netdev_dbg(vxlan->dev, "vfam: map: o %x/%d u %x/%d om %x um %x\n",
141 map->overlay, map->overlay_prefix,
142 map->underlay, map->underlay_prefix,
143 overlay_mask, underlay_mask);
144
145 if ((map->overlay & ~overlay_mask) || (map->underlay & ~underlay_mask))
146 return -EINVAL;
147
148 if (!(map->overlay & overlay_mask) && (map->underlay & underlay_mask))
149 return -EINVAL;
150
151 /* Special case: overlay 0 and underlay 0: flush all mappings */
152 if (!map->overlay && !map->underlay) {
153 vxlan_fan_flush_map(vxlan);
154 return 0;
155 }
156
157 /* Special case: overlay set and underlay 0: clear map for overlay */
158 if (!map->underlay)
159 return vxlan_fan_del_map(vxlan, map->overlay);
160
161 if (vxlan_fan_find_map(vxlan, map->overlay))
162 return -EEXIST;
163
164 fan_map = kmalloc(sizeof(*fan_map), GFP_KERNEL);
165 fan_map->underlay = map->underlay;
166 fan_map->overlay = map->overlay;
167 fan_map->underlay_prefix = map->underlay_prefix;
168 fan_map->overlay_mask = ntohl(overlay_mask);
169 fan_map->overlay_prefix = map->overlay_prefix;
170
171 list_add_tail_rcu(&fan_map->list, &vxlan->fan.fan_maps);
172
173 return 0;
174}
175
176static int vxlan_parse_fan_map(struct nlattr *data[], struct vxlan_dev *vxlan)
177{
178 struct ifla_fan_map *map;
179 struct nlattr *attr;
180 int rem, rv;
181
182 nla_for_each_nested(attr, data[IFLA_IPTUN_FAN_MAP], rem) {
183 map = nla_data(attr);
184 rv = vxlan_fan_add_map(vxlan, map);
185 if (rv)
186 return rv;
187 }
188
189 return 0;
190}
191
192static int vxlan_fan_build_rdst(struct vxlan_dev *vxlan, struct sk_buff *skb,
193 struct vxlan_rdst *fan_rdst)
194{
195 struct ip_fan_map *f_map;
196 union vxlan_addr *va;
197 u32 daddr, underlay;
198 struct arphdr *arp;
199 void *arp_ptr;
200 struct ethhdr *eth;
201 struct iphdr *iph;
202
203 eth = eth_hdr(skb);
204 switch (eth->h_proto) {
205 case htons(ETH_P_IP):
206 iph = ip_hdr(skb);
207 if (!iph)
208 return -EINVAL;
209 daddr = iph->daddr;
210 break;
211 case htons(ETH_P_ARP):
212 arp = arp_hdr(skb);
213 if (!arp)
214 return -EINVAL;
215 arp_ptr = arp + 1;
216 netdev_dbg(vxlan->dev,
217 "vfbr: arp sha %pM sip %pI4 tha %pM tip %pI4\n",
218 arp_ptr, arp_ptr + skb->dev->addr_len,
219 arp_ptr + skb->dev->addr_len + 4,
220 arp_ptr + (skb->dev->addr_len * 2) + 4);
221 arp_ptr += (skb->dev->addr_len * 2) + 4;
222 memcpy(&daddr, arp_ptr, 4);
223 break;
224 default:
225 netdev_dbg(vxlan->dev, "vfbr: unknown eth p %x\n", eth->h_proto);
226 return -EINVAL;
227 }
228
229 f_map = vxlan_fan_find_map(vxlan, daddr);
230 if (!f_map)
231 return -EINVAL;
232
233 daddr = ntohl(daddr);
234 underlay = ntohl(f_map->underlay);
235 if (!underlay)
236 return -EINVAL;
237
238 memset(fan_rdst, 0, sizeof(*fan_rdst));
239 va = &fan_rdst->remote_ip;
240 va->sa.sa_family = AF_INET;
241 fan_rdst->remote_vni = vxlan->default_dst.remote_vni;
242 va->sin.sin_addr.s_addr = htonl(underlay |
243 ((daddr & ~f_map->overlay_mask) >>
244 (32 - f_map->overlay_prefix -
245 (32 - f_map->underlay_prefix))));
246 netdev_dbg(vxlan->dev, "vfbr: daddr %x ul %x dst %x\n",
247 daddr, underlay, va->sin.sin_addr.s_addr);
248
249 return 0;
250}
251
e4c7ed41
CW
252#if IS_ENABLED(CONFIG_IPV6)
253static inline
254bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
255{
f0ef3126
JB
256 if (a->sa.sa_family != b->sa.sa_family)
257 return false;
258 if (a->sa.sa_family == AF_INET6)
259 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
260 else
261 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
e4c7ed41
CW
262}
263
264static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
265{
f0ef3126
JB
266 if (ipa->sa.sa_family == AF_INET6)
267 return ipv6_addr_any(&ipa->sin6.sin6_addr);
268 else
269 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
e4c7ed41
CW
270}
271
272static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
273{
f0ef3126
JB
274 if (ipa->sa.sa_family == AF_INET6)
275 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
276 else
277 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
e4c7ed41
CW
278}
279
280static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
281{
f0ef3126 282 if (nla_len(nla) >= sizeof(struct in6_addr)) {
67b61f6c 283 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
f0ef3126
JB
284 ip->sa.sa_family = AF_INET6;
285 return 0;
286 } else if (nla_len(nla) >= sizeof(__be32)) {
67b61f6c 287 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
f0ef3126
JB
288 ip->sa.sa_family = AF_INET;
289 return 0;
290 } else {
291 return -EAFNOSUPPORT;
292 }
e4c7ed41
CW
293}
294
295static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
f0ef3126 296 const union vxlan_addr *ip)
e4c7ed41 297{
f0ef3126 298 if (ip->sa.sa_family == AF_INET6)
930345ea 299 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
f0ef3126 300 else
930345ea 301 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
e4c7ed41
CW
302}
303
304#else /* !CONFIG_IPV6 */
305
306static inline
307bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
308{
f0ef3126 309 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
e4c7ed41
CW
310}
311
312static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
313{
f0ef3126 314 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
e4c7ed41
CW
315}
316
317static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
318{
f0ef3126 319 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
e4c7ed41
CW
320}
321
322static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
323{
f0ef3126
JB
324 if (nla_len(nla) >= sizeof(struct in6_addr)) {
325 return -EAFNOSUPPORT;
326 } else if (nla_len(nla) >= sizeof(__be32)) {
67b61f6c 327 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
f0ef3126
JB
328 ip->sa.sa_family = AF_INET;
329 return 0;
330 } else {
331 return -EAFNOSUPPORT;
332 }
e4c7ed41
CW
333}
334
335static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
f0ef3126 336 const union vxlan_addr *ip)
e4c7ed41 337{
930345ea 338 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
e4c7ed41
CW
339}
340#endif
341
553675fb 342/* Virtual Network hash table head */
54bfd872 343static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
553675fb 344{
54bfd872 345 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
553675fb 346}
347
348/* Socket hash table head */
349static inline struct hlist_head *vs_head(struct net *net, __be16 port)
d342894c 350{
351 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
352
553675fb 353 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
354}
355
3e61aa8f
SH
356/* First remote destination for a forwarding entry.
357 * Guaranteed to be non-NULL because remotes are never deleted.
358 */
5ca5461c 359static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
3e61aa8f 360{
5ca5461c 361 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
362}
363
364static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
365{
366 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
3e61aa8f
SH
367}
368
ac5132d1
TG
369/* Find VXLAN socket based on network namespace, address family and UDP port
370 * and enabled unshareable flags.
371 */
372static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
373 __be16 port, u32 flags)
553675fb 374{
375 struct vxlan_sock *vs;
af33c1ad
TH
376
377 flags &= VXLAN_F_RCV_FLAGS;
553675fb 378
379 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
19ca9fc1 380 if (inet_sk(vs->sock->sk)->inet_sport == port &&
705cc62f 381 vxlan_get_sk_family(vs) == family &&
af33c1ad 382 vs->flags == flags)
553675fb 383 return vs;
384 }
385 return NULL;
d342894c 386}
387
54bfd872 388static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni)
d342894c 389{
390 struct vxlan_dev *vxlan;
d342894c 391
b9167b2e
JB
392 /* For flow based devices, map all packets to VNI 0 */
393 if (vs->flags & VXLAN_F_COLLECT_METADATA)
394 vni = 0;
395
54bfd872
JB
396 hlist_for_each_entry_rcu(vxlan, vni_head(vs, vni), hlist) {
397 if (vxlan->default_dst.remote_vni == vni)
d342894c 398 return vxlan;
399 }
400
401 return NULL;
402}
403
5cfccc5a 404/* Look up VNI in a per net namespace table */
54bfd872 405static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni,
ac5132d1
TG
406 sa_family_t family, __be16 port,
407 u32 flags)
5cfccc5a
PS
408{
409 struct vxlan_sock *vs;
410
ac5132d1 411 vs = vxlan_find_sock(net, family, port, flags);
5cfccc5a
PS
412 if (!vs)
413 return NULL;
414
54bfd872 415 return vxlan_vs_find_vni(vs, vni);
5cfccc5a
PS
416}
417
d342894c 418/* Fill in neighbour message in skbuff. */
419static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
234f5b73
SH
420 const struct vxlan_fdb *fdb,
421 u32 portid, u32 seq, int type, unsigned int flags,
422 const struct vxlan_rdst *rdst)
d342894c 423{
424 unsigned long now = jiffies;
425 struct nda_cacheinfo ci;
426 struct nlmsghdr *nlh;
427 struct ndmsg *ndm;
e4f67add 428 bool send_ip, send_eth;
d342894c 429
430 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
431 if (nlh == NULL)
432 return -EMSGSIZE;
433
434 ndm = nlmsg_data(nlh);
435 memset(ndm, 0, sizeof(*ndm));
e4f67add
DS
436
437 send_eth = send_ip = true;
438
439 if (type == RTM_GETNEIGH) {
440 ndm->ndm_family = AF_INET;
e4c7ed41 441 send_ip = !vxlan_addr_any(&rdst->remote_ip);
e4f67add
DS
442 send_eth = !is_zero_ether_addr(fdb->eth_addr);
443 } else
444 ndm->ndm_family = AF_BRIDGE;
d342894c 445 ndm->ndm_state = fdb->state;
446 ndm->ndm_ifindex = vxlan->dev->ifindex;
ae884082 447 ndm->ndm_flags = fdb->flags;
545469f7 448 ndm->ndm_type = RTN_UNICAST;
d342894c 449
193523bf 450 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
4967082b 451 nla_put_s32(skb, NDA_LINK_NETNSID,
38f507f1 452 peernet2id(dev_net(vxlan->dev), vxlan->net)))
193523bf
ND
453 goto nla_put_failure;
454
e4f67add 455 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
d342894c 456 goto nla_put_failure;
457
e4c7ed41 458 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
6681712d
DS
459 goto nla_put_failure;
460
0dfbdf41 461 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
6681712d
DS
462 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
463 goto nla_put_failure;
c7995c43 464 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
54bfd872 465 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
6681712d
DS
466 goto nla_put_failure;
467 if (rdst->remote_ifindex &&
468 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
d342894c 469 goto nla_put_failure;
470
471 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
472 ci.ndm_confirmed = 0;
473 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
474 ci.ndm_refcnt = 0;
475
476 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
477 goto nla_put_failure;
478
053c095a
JB
479 nlmsg_end(skb, nlh);
480 return 0;
d342894c 481
482nla_put_failure:
483 nlmsg_cancel(skb, nlh);
484 return -EMSGSIZE;
485}
486
487static inline size_t vxlan_nlmsg_size(void)
488{
489 return NLMSG_ALIGN(sizeof(struct ndmsg))
490 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
e4c7ed41 491 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
73cf3317 492 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
6681712d
DS
493 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
494 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
4967082b 495 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
d342894c 496 + nla_total_size(sizeof(struct nda_cacheinfo));
497}
498
9e4b93f9
ND
499static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
500 struct vxlan_rdst *rd, int type)
d342894c 501{
502 struct net *net = dev_net(vxlan->dev);
503 struct sk_buff *skb;
504 int err = -ENOBUFS;
505
506 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
507 if (skb == NULL)
508 goto errout;
509
9e4b93f9 510 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
d342894c 511 if (err < 0) {
512 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
513 WARN_ON(err == -EMSGSIZE);
514 kfree_skb(skb);
515 goto errout;
516 }
517
518 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
519 return;
520errout:
521 if (err < 0)
522 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
523}
524
e4c7ed41 525static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
e4f67add
DS
526{
527 struct vxlan_dev *vxlan = netdev_priv(dev);
bb3fd687
SH
528 struct vxlan_fdb f = {
529 .state = NUD_STALE,
530 };
531 struct vxlan_rdst remote = {
e4c7ed41 532 .remote_ip = *ipa, /* goes to NDA_DST */
54bfd872 533 .remote_vni = cpu_to_be32(VXLAN_N_VID),
bb3fd687 534 };
3e61aa8f 535
9e4b93f9 536 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
e4f67add
DS
537}
538
539static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
540{
bb3fd687
SH
541 struct vxlan_fdb f = {
542 .state = NUD_STALE,
543 };
9e4b93f9 544 struct vxlan_rdst remote = { };
e4f67add 545
e4f67add
DS
546 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
547
9e4b93f9 548 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
e4f67add
DS
549}
550
d342894c 551/* Hash Ethernet address */
552static u32 eth_hash(const unsigned char *addr)
553{
554 u64 value = get_unaligned((u64 *)addr);
555
556 /* only want 6 bytes */
557#ifdef __BIG_ENDIAN
d342894c 558 value >>= 16;
321fb991 559#else
560 value <<= 16;
d342894c 561#endif
562 return hash_64(value, FDB_HASH_BITS);
563}
564
565/* Hash chain to use given mac address */
566static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
567 const u8 *mac)
568{
569 return &vxlan->fdb_head[eth_hash(mac)];
570}
571
572/* Look up Ethernet address in forwarding table */
014be2c8 573static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
d342894c 574 const u8 *mac)
d342894c 575{
576 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
577 struct vxlan_fdb *f;
d342894c 578
b67bfe0d 579 hlist_for_each_entry_rcu(f, head, hlist) {
7367d0b5 580 if (ether_addr_equal(mac, f->eth_addr))
d342894c 581 return f;
582 }
583
584 return NULL;
585}
586
014be2c8
SS
587static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
588 const u8 *mac)
589{
590 struct vxlan_fdb *f;
591
592 f = __vxlan_find_mac(vxlan, mac);
593 if (f)
594 f->used = jiffies;
595
596 return f;
597}
598
a5e7c10a
MR
599/* caller should hold vxlan->hash_lock */
600static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
e4c7ed41 601 union vxlan_addr *ip, __be16 port,
54bfd872 602 __be32 vni, __u32 ifindex)
6681712d 603{
3e61aa8f 604 struct vxlan_rdst *rd;
6681712d 605
3e61aa8f 606 list_for_each_entry(rd, &f->remotes, list) {
e4c7ed41 607 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
6681712d
DS
608 rd->remote_port == port &&
609 rd->remote_vni == vni &&
610 rd->remote_ifindex == ifindex)
a5e7c10a 611 return rd;
6681712d 612 }
3e61aa8f 613
a5e7c10a
MR
614 return NULL;
615}
616
906dc186
TR
617/* Replace destination of unicast mac */
618static int vxlan_fdb_replace(struct vxlan_fdb *f,
54bfd872
JB
619 union vxlan_addr *ip, __be16 port, __be32 vni,
620 __u32 ifindex)
906dc186
TR
621{
622 struct vxlan_rdst *rd;
623
624 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
625 if (rd)
626 return 0;
627
628 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
629 if (!rd)
630 return 0;
0c1d70af
PA
631
632 dst_cache_reset(&rd->dst_cache);
e4c7ed41 633 rd->remote_ip = *ip;
906dc186
TR
634 rd->remote_port = port;
635 rd->remote_vni = vni;
636 rd->remote_ifindex = ifindex;
637 return 1;
638}
639
a5e7c10a
MR
640/* Add/update destinations for multicast */
641static int vxlan_fdb_append(struct vxlan_fdb *f,
54bfd872 642 union vxlan_addr *ip, __be16 port, __be32 vni,
9e4b93f9 643 __u32 ifindex, struct vxlan_rdst **rdp)
a5e7c10a
MR
644{
645 struct vxlan_rdst *rd;
646
647 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
648 if (rd)
649 return 0;
650
6681712d
DS
651 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
652 if (rd == NULL)
653 return -ENOBUFS;
0c1d70af
PA
654
655 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
656 kfree(rd);
657 return -ENOBUFS;
658 }
659
e4c7ed41 660 rd->remote_ip = *ip;
6681712d
DS
661 rd->remote_port = port;
662 rd->remote_vni = vni;
663 rd->remote_ifindex = ifindex;
3e61aa8f
SH
664
665 list_add_tail_rcu(&rd->list, &f->remotes);
666
9e4b93f9 667 *rdp = rd;
6681712d
DS
668 return 1;
669}
670
dfd8645e
TH
671static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
672 unsigned int off,
673 struct vxlanhdr *vh, size_t hdrlen,
54bfd872
JB
674 __be32 vni_field,
675 struct gro_remcsum *grc,
0ace2ca8 676 bool nopartial)
dfd8645e 677{
b7fe10e5 678 size_t start, offset;
dfd8645e
TH
679
680 if (skb->remcsum_offload)
b7fe10e5 681 return vh;
dfd8645e
TH
682
683 if (!NAPI_GRO_CB(skb)->csum_valid)
684 return NULL;
685
54bfd872
JB
686 start = vxlan_rco_start(vni_field);
687 offset = start + vxlan_rco_offset(vni_field);
dfd8645e 688
b7fe10e5
TH
689 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
690 start, offset, grc, nopartial);
dfd8645e
TH
691
692 skb->remcsum_offload = 1;
693
694 return vh;
695}
696
5602c48c
TH
697static struct sk_buff **vxlan_gro_receive(struct sock *sk,
698 struct sk_buff **head,
699 struct sk_buff *skb)
dc01e7d3
OG
700{
701 struct sk_buff *p, **pp = NULL;
702 struct vxlanhdr *vh, *vh2;
9b174d88 703 unsigned int hlen, off_vx;
dc01e7d3 704 int flush = 1;
5602c48c 705 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
54bfd872 706 __be32 flags;
26c4f7da
TH
707 struct gro_remcsum grc;
708
709 skb_gro_remcsum_init(&grc);
dc01e7d3
OG
710
711 off_vx = skb_gro_offset(skb);
712 hlen = off_vx + sizeof(*vh);
713 vh = skb_gro_header_fast(skb, off_vx);
714 if (skb_gro_header_hard(skb, hlen)) {
715 vh = skb_gro_header_slow(skb, hlen, off_vx);
716 if (unlikely(!vh))
717 goto out;
718 }
dc01e7d3 719
dfd8645e
TH
720 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
721
54bfd872 722 flags = vh->vx_flags;
dfd8645e
TH
723
724 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
725 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
54bfd872 726 vh->vx_vni, &grc,
0ace2ca8
TH
727 !!(vs->flags &
728 VXLAN_F_REMCSUM_NOPARTIAL));
dfd8645e
TH
729
730 if (!vh)
731 goto out;
732 }
733
b7fe10e5
TH
734 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
735
dc01e7d3
OG
736 for (p = *head; p; p = p->next) {
737 if (!NAPI_GRO_CB(p)->same_flow)
738 continue;
739
740 vh2 = (struct vxlanhdr *)(p->data + off_vx);
3511494c
TG
741 if (vh->vx_flags != vh2->vx_flags ||
742 vh->vx_vni != vh2->vx_vni) {
dc01e7d3
OG
743 NAPI_GRO_CB(p)->same_flow = 0;
744 continue;
745 }
dc01e7d3
OG
746 }
747
fcd91dd4 748 pp = call_gro_receive(eth_gro_receive, head, skb);
c194cf93 749 flush = 0;
dc01e7d3 750
dc01e7d3 751out:
26c4f7da 752 skb_gro_remcsum_cleanup(skb, &grc);
dc01e7d3
OG
753 NAPI_GRO_CB(skb)->flush |= flush;
754
755 return pp;
756}
757
5602c48c 758static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
dc01e7d3 759{
229740c6
JR
760 /* Sets 'skb->inner_mac_header' since we are always called with
761 * 'skb->encapsulation' set.
762 */
9b174d88 763 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
dc01e7d3
OG
764}
765
d342894c 766/* Add new entry to forwarding table -- assumes lock held */
767static int vxlan_fdb_create(struct vxlan_dev *vxlan,
e4c7ed41 768 const u8 *mac, union vxlan_addr *ip,
6681712d 769 __u16 state, __u16 flags,
54bfd872 770 __be16 port, __be32 vni, __u32 ifindex,
ae884082 771 __u8 ndm_flags)
d342894c 772{
9e4b93f9 773 struct vxlan_rdst *rd = NULL;
d342894c 774 struct vxlan_fdb *f;
775 int notify = 0;
17b46365 776 int rc;
d342894c 777
014be2c8 778 f = __vxlan_find_mac(vxlan, mac);
d342894c 779 if (f) {
780 if (flags & NLM_F_EXCL) {
781 netdev_dbg(vxlan->dev,
782 "lost race to create %pM\n", mac);
783 return -EEXIST;
784 }
785 if (f->state != state) {
786 f->state = state;
787 f->updated = jiffies;
788 notify = 1;
789 }
ae884082
DS
790 if (f->flags != ndm_flags) {
791 f->flags = ndm_flags;
792 f->updated = jiffies;
793 notify = 1;
794 }
906dc186
TR
795 if ((flags & NLM_F_REPLACE)) {
796 /* Only change unicasts */
797 if (!(is_multicast_ether_addr(f->eth_addr) ||
798 is_zero_ether_addr(f->eth_addr))) {
60840429 799 notify |= vxlan_fdb_replace(f, ip, port, vni,
906dc186 800 ifindex);
906dc186
TR
801 } else
802 return -EOPNOTSUPP;
803 }
6681712d 804 if ((flags & NLM_F_APPEND) &&
58e4c767
MR
805 (is_multicast_ether_addr(f->eth_addr) ||
806 is_zero_ether_addr(f->eth_addr))) {
17b46365 807 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
6681712d
DS
808
809 if (rc < 0)
810 return rc;
811 notify |= rc;
812 }
d342894c 813 } else {
814 if (!(flags & NLM_F_CREATE))
815 return -ENOENT;
816
0dfbdf41
TG
817 if (vxlan->cfg.addrmax &&
818 vxlan->addrcnt >= vxlan->cfg.addrmax)
d342894c 819 return -ENOSPC;
820
906dc186
TR
821 /* Disallow replace to add a multicast entry */
822 if ((flags & NLM_F_REPLACE) &&
823 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
824 return -EOPNOTSUPP;
825
e4c7ed41 826 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
d342894c 827 f = kmalloc(sizeof(*f), GFP_ATOMIC);
828 if (!f)
829 return -ENOMEM;
830
831 notify = 1;
d342894c 832 f->state = state;
ae884082 833 f->flags = ndm_flags;
d342894c 834 f->updated = f->used = jiffies;
3e61aa8f 835 INIT_LIST_HEAD(&f->remotes);
d342894c 836 memcpy(f->eth_addr, mac, ETH_ALEN);
837
17b46365
HY
838 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
839 if (rc < 0) {
840 kfree(f);
841 return rc;
842 }
3e61aa8f 843
d342894c 844 ++vxlan->addrcnt;
845 hlist_add_head_rcu(&f->hlist,
846 vxlan_fdb_head(vxlan, mac));
847 }
848
9e4b93f9
ND
849 if (notify) {
850 if (rd == NULL)
851 rd = first_remote_rtnl(f);
852 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
853 }
d342894c 854
855 return 0;
856}
857
6706c82e 858static void vxlan_fdb_free(struct rcu_head *head)
6681712d
DS
859{
860 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
3e61aa8f 861 struct vxlan_rdst *rd, *nd;
6681712d 862
0c1d70af
PA
863 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
864 dst_cache_destroy(&rd->dst_cache);
6681712d 865 kfree(rd);
0c1d70af 866 }
6681712d
DS
867 kfree(f);
868}
869
d342894c 870static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
871{
872 netdev_dbg(vxlan->dev,
873 "delete %pM\n", f->eth_addr);
874
875 --vxlan->addrcnt;
9e4b93f9 876 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
d342894c 877
878 hlist_del_rcu(&f->hlist);
6681712d 879 call_rcu(&f->rcu, vxlan_fdb_free);
d342894c 880}
881
f0b074be 882static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
54bfd872
JB
883 union vxlan_addr *ip, __be16 *port, __be32 *vni,
884 u32 *ifindex)
d342894c 885{
6681712d 886 struct net *net = dev_net(vxlan->dev);
e4c7ed41 887 int err;
d342894c 888
f0b074be 889 if (tb[NDA_DST]) {
e4c7ed41
CW
890 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
891 if (err)
892 return err;
f0b074be 893 } else {
e4c7ed41
CW
894 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
895 if (remote->sa.sa_family == AF_INET) {
896 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
897 ip->sa.sa_family = AF_INET;
898#if IS_ENABLED(CONFIG_IPV6)
899 } else {
900 ip->sin6.sin6_addr = in6addr_any;
901 ip->sa.sa_family = AF_INET6;
902#endif
903 }
f0b074be 904 }
d342894c 905
6681712d 906 if (tb[NDA_PORT]) {
73cf3317 907 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
6681712d 908 return -EINVAL;
f0b074be
MR
909 *port = nla_get_be16(tb[NDA_PORT]);
910 } else {
0dfbdf41 911 *port = vxlan->cfg.dst_port;
f0b074be 912 }
6681712d
DS
913
914 if (tb[NDA_VNI]) {
915 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
916 return -EINVAL;
54bfd872 917 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
f0b074be
MR
918 } else {
919 *vni = vxlan->default_dst.remote_vni;
920 }
6681712d
DS
921
922 if (tb[NDA_IFINDEX]) {
5abb0029 923 struct net_device *tdev;
6681712d
DS
924
925 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
926 return -EINVAL;
f0b074be 927 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
73763949 928 tdev = __dev_get_by_index(net, *ifindex);
5abb0029 929 if (!tdev)
6681712d 930 return -EADDRNOTAVAIL;
f0b074be
MR
931 } else {
932 *ifindex = 0;
933 }
934
935 return 0;
936}
937
938/* Add static entry (via netlink) */
939static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
940 struct net_device *dev,
f6f6424b 941 const unsigned char *addr, u16 vid, u16 flags)
f0b074be
MR
942{
943 struct vxlan_dev *vxlan = netdev_priv(dev);
944 /* struct net *net = dev_net(vxlan->dev); */
e4c7ed41 945 union vxlan_addr ip;
f0b074be 946 __be16 port;
54bfd872
JB
947 __be32 vni;
948 u32 ifindex;
f0b074be
MR
949 int err;
950
951 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
952 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
953 ndm->ndm_state);
954 return -EINVAL;
955 }
956
957 if (tb[NDA_DST] == NULL)
958 return -EINVAL;
959
960 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
961 if (err)
962 return err;
6681712d 963
5933a7bb
MR
964 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
965 return -EAFNOSUPPORT;
966
d342894c 967 spin_lock_bh(&vxlan->hash_lock);
e4c7ed41 968 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
73cf3317 969 port, vni, ifindex, ndm->ndm_flags);
d342894c 970 spin_unlock_bh(&vxlan->hash_lock);
971
972 return err;
973}
974
975/* Delete entry (via netlink) */
1690be63
VY
976static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
977 struct net_device *dev,
f6f6424b 978 const unsigned char *addr, u16 vid)
d342894c 979{
980 struct vxlan_dev *vxlan = netdev_priv(dev);
981 struct vxlan_fdb *f;
bc7892ba 982 struct vxlan_rdst *rd = NULL;
e4c7ed41 983 union vxlan_addr ip;
bc7892ba 984 __be16 port;
54bfd872
JB
985 __be32 vni;
986 u32 ifindex;
bc7892ba
MR
987 int err;
988
989 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
990 if (err)
991 return err;
992
993 err = -ENOENT;
d342894c 994
995 spin_lock_bh(&vxlan->hash_lock);
996 f = vxlan_find_mac(vxlan, addr);
bc7892ba
MR
997 if (!f)
998 goto out;
999
e4c7ed41
CW
1000 if (!vxlan_addr_any(&ip)) {
1001 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
bc7892ba
MR
1002 if (!rd)
1003 goto out;
1004 }
1005
1006 err = 0;
1007
1008 /* remove a destination if it's not the only one on the list,
1009 * otherwise destroy the fdb entry
1010 */
1011 if (rd && !list_is_singular(&f->remotes)) {
1012 list_del_rcu(&rd->list);
9e4b93f9 1013 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
dcdc7a59 1014 kfree_rcu(rd, rcu);
bc7892ba 1015 goto out;
d342894c 1016 }
bc7892ba
MR
1017
1018 vxlan_fdb_destroy(vxlan, f);
1019
1020out:
d342894c 1021 spin_unlock_bh(&vxlan->hash_lock);
1022
1023 return err;
1024}
1025
1026/* Dump forwarding table */
1027static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
5d5eacb3 1028 struct net_device *dev,
d297653d 1029 struct net_device *filter_dev, int *idx)
d342894c 1030{
1031 struct vxlan_dev *vxlan = netdev_priv(dev);
1032 unsigned int h;
d297653d 1033 int err = 0;
d342894c 1034
1035 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1036 struct vxlan_fdb *f;
d342894c 1037
b67bfe0d 1038 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
6681712d 1039 struct vxlan_rdst *rd;
6681712d 1040
3e61aa8f 1041 list_for_each_entry_rcu(rd, &f->remotes, list) {
d297653d 1042 if (*idx < cb->args[2])
07a51cd3
AW
1043 goto skip;
1044
6681712d
DS
1045 err = vxlan_fdb_info(skb, vxlan, f,
1046 NETLINK_CB(cb->skb).portid,
1047 cb->nlh->nlmsg_seq,
1048 RTM_NEWNEIGH,
1049 NLM_F_MULTI, rd);
d297653d 1050 if (err < 0)
3e61aa8f 1051 goto out;
3e61aa8f 1052skip:
d297653d 1053 *idx += 1;
07a51cd3 1054 }
d342894c 1055 }
1056 }
3e61aa8f 1057out:
d297653d 1058 return err;
d342894c 1059}
1060
1061/* Watch incoming packets to learn mapping between Ethernet address
1062 * and Tunnel endpoint.
c4b49512 1063 * Return true if packet is bogus and should be dropped.
d342894c 1064 */
26a41ae6 1065static bool vxlan_snoop(struct net_device *dev,
e4c7ed41 1066 union vxlan_addr *src_ip, const u8 *src_mac)
d342894c 1067{
1068 struct vxlan_dev *vxlan = netdev_priv(dev);
1069 struct vxlan_fdb *f;
d342894c 1070
1071 f = vxlan_find_mac(vxlan, src_mac);
1072 if (likely(f)) {
5ca5461c 1073 struct vxlan_rdst *rdst = first_remote_rcu(f);
3e61aa8f 1074
e4c7ed41 1075 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
26a41ae6 1076 return false;
1077
1078 /* Don't migrate static entries, drop packets */
eb064c3b 1079 if (f->state & NUD_NOARP)
26a41ae6 1080 return true;
d342894c 1081
1082 if (net_ratelimit())
1083 netdev_info(dev,
e4c7ed41 1084 "%pM migrated from %pIS to %pIS\n",
a4870f79 1085 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
d342894c 1086
e4c7ed41 1087 rdst->remote_ip = *src_ip;
d342894c 1088 f->updated = jiffies;
9e4b93f9 1089 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
d342894c 1090 } else {
1091 /* learned new entry */
1092 spin_lock(&vxlan->hash_lock);
3bf74b1a 1093
1094 /* close off race between vxlan_flush and incoming packets */
1095 if (netif_running(dev))
1096 vxlan_fdb_create(vxlan, src_mac, src_ip,
1097 NUD_REACHABLE,
1098 NLM_F_EXCL|NLM_F_CREATE,
0dfbdf41 1099 vxlan->cfg.dst_port,
3bf74b1a 1100 vxlan->default_dst.remote_vni,
1101 0, NTF_SELF);
d342894c 1102 spin_unlock(&vxlan->hash_lock);
1103 }
26a41ae6 1104
1105 return false;
d342894c 1106}
1107
d342894c 1108/* See if multicast group is already in use by other ID */
95ab0991 1109static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
d342894c 1110{
553675fb 1111 struct vxlan_dev *vxlan;
c6fcc4fc 1112 struct vxlan_sock *sock4;
4053ab1b
AB
1113#if IS_ENABLED(CONFIG_IPV6)
1114 struct vxlan_sock *sock6;
1115#endif
b1be00a6 1116 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
d342894c 1117
c6fcc4fc 1118 sock4 = rtnl_dereference(dev->vn4_sock);
1119
95ab0991
G
1120 /* The vxlan_sock is only used by dev, leaving group has
1121 * no effect on other vxlan devices.
1122 */
c6fcc4fc 1123 if (family == AF_INET && sock4 && atomic_read(&sock4->refcnt) == 1)
95ab0991 1124 return false;
b1be00a6 1125#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 1126 sock6 = rtnl_dereference(dev->vn6_sock);
1127 if (family == AF_INET6 && sock6 && atomic_read(&sock6->refcnt) == 1)
b1be00a6
JB
1128 return false;
1129#endif
95ab0991 1130
553675fb 1131 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
95ab0991 1132 if (!netif_running(vxlan->dev) || vxlan == dev)
553675fb 1133 continue;
d342894c 1134
c6fcc4fc 1135 if (family == AF_INET &&
1136 rtnl_dereference(vxlan->vn4_sock) != sock4)
95ab0991 1137 continue;
b1be00a6 1138#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 1139 if (family == AF_INET6 &&
1140 rtnl_dereference(vxlan->vn6_sock) != sock6)
b1be00a6
JB
1141 continue;
1142#endif
95ab0991
G
1143
1144 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1145 &dev->default_dst.remote_ip))
1146 continue;
1147
1148 if (vxlan->default_dst.remote_ifindex !=
1149 dev->default_dst.remote_ifindex)
1150 continue;
1151
1152 return true;
553675fb 1153 }
d342894c 1154
1155 return false;
1156}
1157
544a773a 1158static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
7c47cedf 1159{
b1be00a6 1160 struct vxlan_net *vn;
012a5729 1161
b1be00a6 1162 if (!vs)
544a773a 1163 return false;
7c47cedf 1164 if (!atomic_dec_and_test(&vs->refcnt))
544a773a 1165 return false;
d342894c 1166
b1be00a6 1167 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1c51a915 1168 spin_lock(&vn->sock_lock);
7c47cedf 1169 hlist_del_rcu(&vs->hlist);
e7b3db5e 1170 udp_tunnel_notify_del_rx_port(vs->sock,
b9adcd69
AD
1171 (vs->flags & VXLAN_F_GPE) ?
1172 UDP_TUNNEL_TYPE_VXLAN_GPE :
e7b3db5e 1173 UDP_TUNNEL_TYPE_VXLAN);
1c51a915
SH
1174 spin_unlock(&vn->sock_lock);
1175
544a773a 1176 return true;
d342894c 1177}
1178
b1be00a6
JB
1179static void vxlan_sock_release(struct vxlan_dev *vxlan)
1180{
c6fcc4fc 1181 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
b1be00a6 1182#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 1183 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1184
1185 rcu_assign_pointer(vxlan->vn6_sock, NULL);
544a773a
HFS
1186#endif
1187
c6fcc4fc 1188 rcu_assign_pointer(vxlan->vn4_sock, NULL);
544a773a
HFS
1189 synchronize_net();
1190
c6fcc4fc 1191 if (__vxlan_sock_release_prep(sock4)) {
1192 udp_tunnel_sock_release(sock4->sock);
1193 kfree(sock4);
544a773a
HFS
1194 }
1195
1196#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 1197 if (__vxlan_sock_release_prep(sock6)) {
1198 udp_tunnel_sock_release(sock6->sock);
1199 kfree(sock6);
544a773a 1200 }
b1be00a6
JB
1201#endif
1202}
1203
56ef9c90 1204/* Update multicast group membership when first VNI on
c4b49512 1205 * multicast address is brought up
7c47cedf 1206 */
56ef9c90 1207static int vxlan_igmp_join(struct vxlan_dev *vxlan)
d342894c 1208{
b1be00a6 1209 struct sock *sk;
e4c7ed41
CW
1210 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1211 int ifindex = vxlan->default_dst.remote_ifindex;
149d7549 1212 int ret = -EINVAL;
d342894c 1213
e4c7ed41 1214 if (ip->sa.sa_family == AF_INET) {
c6fcc4fc 1215 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
e4c7ed41
CW
1216 struct ip_mreqn mreq = {
1217 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1218 .imr_ifindex = ifindex,
1219 };
1220
c6fcc4fc 1221 sk = sock4->sock->sk;
b1be00a6 1222 lock_sock(sk);
56ef9c90 1223 ret = ip_mc_join_group(sk, &mreq);
b1be00a6 1224 release_sock(sk);
e4c7ed41
CW
1225#if IS_ENABLED(CONFIG_IPV6)
1226 } else {
c6fcc4fc 1227 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1228
1229 sk = sock6->sock->sk;
b1be00a6 1230 lock_sock(sk);
56ef9c90
MRL
1231 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1232 &ip->sin6.sin6_addr);
b1be00a6 1233 release_sock(sk);
e4c7ed41
CW
1234#endif
1235 }
3fc2de2f 1236
56ef9c90 1237 return ret;
3fc2de2f 1238}
1239
1240/* Inverse of vxlan_igmp_join when last VNI is brought down */
56ef9c90 1241static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
3fc2de2f 1242{
b1be00a6 1243 struct sock *sk;
e4c7ed41
CW
1244 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1245 int ifindex = vxlan->default_dst.remote_ifindex;
149d7549 1246 int ret = -EINVAL;
3fc2de2f 1247
e4c7ed41 1248 if (ip->sa.sa_family == AF_INET) {
c6fcc4fc 1249 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
e4c7ed41
CW
1250 struct ip_mreqn mreq = {
1251 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1252 .imr_ifindex = ifindex,
1253 };
1254
c6fcc4fc 1255 sk = sock4->sock->sk;
b1be00a6 1256 lock_sock(sk);
56ef9c90 1257 ret = ip_mc_leave_group(sk, &mreq);
b1be00a6 1258 release_sock(sk);
e4c7ed41
CW
1259#if IS_ENABLED(CONFIG_IPV6)
1260 } else {
c6fcc4fc 1261 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1262
1263 sk = sock6->sock->sk;
b1be00a6 1264 lock_sock(sk);
56ef9c90
MRL
1265 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1266 &ip->sin6.sin6_addr);
b1be00a6 1267 release_sock(sk);
e4c7ed41
CW
1268#endif
1269 }
d342894c 1270
56ef9c90 1271 return ret;
d342894c 1272}
1273
f14ecebb
JB
1274static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1275 struct sk_buff *skb, u32 vxflags)
dfd8645e 1276{
7d34fa75 1277 size_t start, offset;
dfd8645e 1278
f14ecebb
JB
1279 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1280 goto out;
b7fe10e5 1281
f14ecebb
JB
1282 start = vxlan_rco_start(unparsed->vx_vni);
1283 offset = start + vxlan_rco_offset(unparsed->vx_vni);
dfd8645e 1284
7d34fa75 1285 if (!pskb_may_pull(skb, offset + sizeof(u16)))
be5cfeab 1286 return false;
dfd8645e 1287
be5cfeab
JB
1288 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1289 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
f14ecebb
JB
1290out:
1291 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1292 unparsed->vx_vni &= VXLAN_VNI_MASK;
be5cfeab 1293 return true;
dfd8645e
TH
1294}
1295
f14ecebb 1296static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
64f87d36 1297 struct sk_buff *skb, u32 vxflags,
10a5af23 1298 struct vxlan_metadata *md)
3288af08 1299{
f14ecebb 1300 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
10a5af23 1301 struct metadata_dst *tun_dst;
f14ecebb
JB
1302
1303 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1304 goto out;
3288af08 1305
3288af08
JB
1306 md->gbp = ntohs(gbp->policy_id);
1307
10a5af23 1308 tun_dst = (struct metadata_dst *)skb_dst(skb);
810813c4 1309 if (tun_dst) {
3288af08 1310 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
810813c4
DM
1311 tun_dst->u.tun_info.options_len = sizeof(*md);
1312 }
3288af08
JB
1313 if (gbp->dont_learn)
1314 md->gbp |= VXLAN_GBP_DONT_LEARN;
1315
1316 if (gbp->policy_applied)
1317 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
f14ecebb 1318
64f87d36
JB
1319 /* In flow-based mode, GBP is carried in dst_metadata */
1320 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1321 skb->mark = md->gbp;
f14ecebb
JB
1322out:
1323 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
3288af08
JB
1324}
1325
e1e5314d 1326static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
61618eea 1327 __be16 *protocol,
e1e5314d
JB
1328 struct sk_buff *skb, u32 vxflags)
1329{
1330 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1331
1332 /* Need to have Next Protocol set for interfaces in GPE mode. */
1333 if (!gpe->np_applied)
1334 return false;
1335 /* "The initial version is 0. If a receiver does not support the
1336 * version indicated it MUST drop the packet.
1337 */
1338 if (gpe->version != 0)
1339 return false;
1340 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1341 * processing MUST occur." However, we don't implement OAM
1342 * processing, thus drop the packet.
1343 */
1344 if (gpe->oam_flag)
1345 return false;
1346
1347 switch (gpe->next_protocol) {
1348 case VXLAN_GPE_NP_IPV4:
1349 *protocol = htons(ETH_P_IP);
1350 break;
1351 case VXLAN_GPE_NP_IPV6:
1352 *protocol = htons(ETH_P_IPV6);
1353 break;
1354 case VXLAN_GPE_NP_ETHERNET:
1355 *protocol = htons(ETH_P_TEB);
1356 break;
1357 default:
1358 return false;
1359 }
1360
1361 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1362 return true;
1363}
1364
1ab016e2
JB
1365static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1366 struct vxlan_sock *vs,
1367 struct sk_buff *skb)
614732ea 1368{
614732ea 1369 union vxlan_addr saddr;
614732ea 1370
614732ea 1371 skb_reset_mac_header(skb);
614732ea
TG
1372 skb->protocol = eth_type_trans(skb, vxlan->dev);
1373 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1374
1375 /* Ignore packet loops (and multicast echo) */
1376 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1ab016e2 1377 return false;
614732ea 1378
760c6805 1379 /* Get address from the outer IP header */
ce212d0f 1380 if (vxlan_get_sk_family(vs) == AF_INET) {
1ab016e2 1381 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
614732ea
TG
1382 saddr.sa.sa_family = AF_INET;
1383#if IS_ENABLED(CONFIG_IPV6)
1384 } else {
1ab016e2 1385 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
614732ea
TG
1386 saddr.sa.sa_family = AF_INET6;
1387#endif
1388 }
1389
1ab016e2
JB
1390 if ((vxlan->flags & VXLAN_F_LEARN) &&
1391 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1392 return false;
1393
1394 return true;
1395}
1396
760c6805
JB
1397static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1398 struct sk_buff *skb)
1399{
1400 int err = 0;
1401
1402 if (vxlan_get_sk_family(vs) == AF_INET)
1403 err = IP_ECN_decapsulate(oiph, skb);
1404#if IS_ENABLED(CONFIG_IPV6)
1405 else
1406 err = IP6_ECN_decapsulate(oiph, skb);
1407#endif
1408
1409 if (unlikely(err) && log_ecn_error) {
1410 if (vxlan_get_sk_family(vs) == AF_INET)
1411 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1412 &((struct iphdr *)oiph)->saddr,
1413 ((struct iphdr *)oiph)->tos);
1414 else
1415 net_info_ratelimited("non-ECT from %pI6\n",
1416 &((struct ipv6hdr *)oiph)->saddr);
1417 }
1418 return err <= 1;
1419}
1420
d342894c 1421/* Callback from net/ipv4/udp.c to receive packets */
f2d1968e 1422static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
d342894c 1423{
f2d1968e 1424 struct pcpu_sw_netstats *stats;
c9e78efb 1425 struct vxlan_dev *vxlan;
5cfccc5a 1426 struct vxlan_sock *vs;
f14ecebb 1427 struct vxlanhdr unparsed;
ee122c79
TG
1428 struct vxlan_metadata _md;
1429 struct vxlan_metadata *md = &_md;
61618eea 1430 __be16 protocol = htons(ETH_P_TEB);
e1e5314d 1431 bool raw_proto = false;
f2d1968e 1432 void *oiph;
d342894c 1433
e1e5314d 1434 /* Need UDP and VXLAN header to be present */
7ce04758 1435 if (!pskb_may_pull(skb, VXLAN_HLEN))
e5aed006 1436 goto drop;
d342894c 1437
f14ecebb 1438 unparsed = *vxlan_hdr(skb);
288b01c8
JB
1439 /* VNI flag always required to be set */
1440 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1441 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1442 ntohl(vxlan_hdr(skb)->vx_flags),
1443 ntohl(vxlan_hdr(skb)->vx_vni));
1444 /* Return non vxlan pkt */
e5aed006 1445 goto drop;
d342894c 1446 }
288b01c8
JB
1447 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1448 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
d342894c 1449
559835ea 1450 vs = rcu_dereference_sk_user_data(sk);
5cfccc5a 1451 if (!vs)
d342894c 1452 goto drop;
d342894c 1453
c9e78efb
JB
1454 vxlan = vxlan_vs_find_vni(vs, vxlan_vni(vxlan_hdr(skb)->vx_vni));
1455 if (!vxlan)
1456 goto drop;
1457
e1e5314d
JB
1458 /* For backwards compatibility, only allow reserved fields to be
1459 * used by VXLAN extensions if explicitly requested.
1460 */
1461 if (vs->flags & VXLAN_F_GPE) {
1462 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1463 goto drop;
1464 raw_proto = true;
1465 }
1466
1467 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1468 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1469 goto drop;
c9e78efb 1470
ee122c79 1471 if (vxlan_collect_metadata(vs)) {
07dabf20 1472 __be32 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
10a5af23 1473 struct metadata_dst *tun_dst;
07dabf20 1474
c29a70d2 1475 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
d817f432 1476 key32_to_tunnel_id(vni), sizeof(*md));
c29a70d2 1477
ee122c79
TG
1478 if (!tun_dst)
1479 goto drop;
1480
0f1b7354 1481 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
10a5af23
JB
1482
1483 skb_dst_set(skb, (struct dst_entry *)tun_dst);
ee122c79
TG
1484 } else {
1485 memset(md, 0, sizeof(*md));
1486 }
1487
f14ecebb
JB
1488 if (vs->flags & VXLAN_F_REMCSUM_RX)
1489 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1490 goto drop;
1491 if (vs->flags & VXLAN_F_GBP)
10a5af23 1492 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
e1e5314d
JB
1493 /* Note that GBP and GPE can never be active together. This is
1494 * ensured in vxlan_dev_configure.
1495 */
3511494c 1496
f14ecebb 1497 if (unparsed.vx_flags || unparsed.vx_vni) {
3bf39475
TH
1498 /* If there are any unprocessed flags remaining treat
1499 * this as a malformed packet. This behavior diverges from
1500 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1501 * in reserved fields are to be ignored. The approach here
c4b49512 1502 * maintains compatibility with previous stack code, and also
3bf39475
TH
1503 * is more robust and provides a little more security in
1504 * adding extensions to VXLAN.
1505 */
288b01c8 1506 goto drop;
3bf39475
TH
1507 }
1508
e1e5314d
JB
1509 if (!raw_proto) {
1510 if (!vxlan_set_mac(vxlan, vs, skb))
1511 goto drop;
1512 } else {
8be0cfa4 1513 skb_reset_mac_header(skb);
e1e5314d
JB
1514 skb->dev = vxlan->dev;
1515 skb->pkt_type = PACKET_HOST;
1516 }
f2d1968e 1517
f2d1968e
JB
1518 oiph = skb_network_header(skb);
1519 skb_reset_network_header(skb);
1520
1521 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1522 ++vxlan->dev->stats.rx_frame_errors;
1523 ++vxlan->dev->stats.rx_errors;
1524 goto drop;
1525 }
1526
1527 stats = this_cpu_ptr(vxlan->dev->tstats);
1528 u64_stats_update_begin(&stats->syncp);
1529 stats->rx_packets++;
1530 stats->rx_bytes += skb->len;
1531 u64_stats_update_end(&stats->syncp);
1532
1533 gro_cells_receive(&vxlan->gro_cells, skb);
5cfccc5a
PS
1534 return 0;
1535
1536drop:
288b01c8
JB
1537 /* Consume bad packet */
1538 kfree_skb(skb);
1539 return 0;
5cfccc5a
PS
1540}
1541
e4f67add
DS
1542static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1543{
1544 struct vxlan_dev *vxlan = netdev_priv(dev);
1545 struct arphdr *parp;
1546 u8 *arpptr, *sha;
1547 __be32 sip, tip;
1548 struct neighbour *n;
1549
1550 if (dev->flags & IFF_NOARP)
1551 goto out;
1552
1553 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1554 dev->stats.tx_dropped++;
1555 goto out;
1556 }
1557 parp = arp_hdr(skb);
1558
1559 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1560 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1561 parp->ar_pro != htons(ETH_P_IP) ||
1562 parp->ar_op != htons(ARPOP_REQUEST) ||
1563 parp->ar_hln != dev->addr_len ||
1564 parp->ar_pln != 4)
1565 goto out;
1566 arpptr = (u8 *)parp + sizeof(struct arphdr);
1567 sha = arpptr;
1568 arpptr += dev->addr_len; /* sha */
1569 memcpy(&sip, arpptr, sizeof(sip));
1570 arpptr += sizeof(sip);
1571 arpptr += dev->addr_len; /* tha */
1572 memcpy(&tip, arpptr, sizeof(tip));
1573
1574 if (ipv4_is_loopback(tip) ||
1575 ipv4_is_multicast(tip))
1576 goto out;
1577
1578 n = neigh_lookup(&arp_tbl, &tip, dev);
1579
1580 if (n) {
e4f67add
DS
1581 struct vxlan_fdb *f;
1582 struct sk_buff *reply;
1583
1584 if (!(n->nud_state & NUD_CONNECTED)) {
1585 neigh_release(n);
1586 goto out;
1587 }
1588
1589 f = vxlan_find_mac(vxlan, n->ha);
e4c7ed41 1590 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
e4f67add
DS
1591 /* bridge-local neighbor */
1592 neigh_release(n);
1593 goto out;
1594 }
1595
1596 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1597 n->ha, sha);
1598
1599 neigh_release(n);
1600
7346135d
DS
1601 if (reply == NULL)
1602 goto out;
1603
e4f67add
DS
1604 skb_reset_mac_header(reply);
1605 __skb_pull(reply, skb_network_offset(reply));
1606 reply->ip_summed = CHECKSUM_UNNECESSARY;
1607 reply->pkt_type = PACKET_HOST;
1608
1609 if (netif_rx_ni(reply) == NET_RX_DROP)
1610 dev->stats.rx_dropped++;
e4c7ed41
CW
1611 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1612 union vxlan_addr ipa = {
1613 .sin.sin_addr.s_addr = tip,
a45e92a5 1614 .sin.sin_family = AF_INET,
e4c7ed41
CW
1615 };
1616
1617 vxlan_ip_miss(dev, &ipa);
1618 }
e4f67add
DS
1619out:
1620 consume_skb(skb);
1621 return NETDEV_TX_OK;
1622}
1623
f564f45c 1624#if IS_ENABLED(CONFIG_IPV6)
4b29dba9
DS
1625static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1626 struct neighbour *n, bool isrouter)
1627{
1628 struct net_device *dev = request->dev;
1629 struct sk_buff *reply;
1630 struct nd_msg *ns, *na;
1631 struct ipv6hdr *pip6;
1632 u8 *daddr;
1633 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1634 int ns_olen;
1635 int i, len;
1636
1637 if (dev == NULL)
1638 return NULL;
1639
1640 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1641 sizeof(*na) + na_olen + dev->needed_tailroom;
1642 reply = alloc_skb(len, GFP_ATOMIC);
1643 if (reply == NULL)
1644 return NULL;
1645
1646 reply->protocol = htons(ETH_P_IPV6);
1647 reply->dev = dev;
1648 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1649 skb_push(reply, sizeof(struct ethhdr));
6297b91c 1650 skb_reset_mac_header(reply);
4b29dba9
DS
1651
1652 ns = (struct nd_msg *)skb_transport_header(request);
1653
1654 daddr = eth_hdr(request)->h_source;
1655 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1656 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1657 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1658 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1659 break;
1660 }
1661 }
1662
1663 /* Ethernet header */
1664 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1665 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1666 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1667 reply->protocol = htons(ETH_P_IPV6);
1668
1669 skb_pull(reply, sizeof(struct ethhdr));
6297b91c 1670 skb_reset_network_header(reply);
4b29dba9
DS
1671 skb_put(reply, sizeof(struct ipv6hdr));
1672
1673 /* IPv6 header */
1674
1675 pip6 = ipv6_hdr(reply);
1676 memset(pip6, 0, sizeof(struct ipv6hdr));
1677 pip6->version = 6;
1678 pip6->priority = ipv6_hdr(request)->priority;
1679 pip6->nexthdr = IPPROTO_ICMPV6;
1680 pip6->hop_limit = 255;
1681 pip6->daddr = ipv6_hdr(request)->saddr;
1682 pip6->saddr = *(struct in6_addr *)n->primary_key;
1683
1684 skb_pull(reply, sizeof(struct ipv6hdr));
6297b91c 1685 skb_reset_transport_header(reply);
4b29dba9
DS
1686
1687 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1688
1689 /* Neighbor Advertisement */
1690 memset(na, 0, sizeof(*na)+na_olen);
1691 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1692 na->icmph.icmp6_router = isrouter;
1693 na->icmph.icmp6_override = 1;
1694 na->icmph.icmp6_solicited = 1;
1695 na->target = ns->target;
1696 ether_addr_copy(&na->opt[2], n->ha);
1697 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1698 na->opt[1] = na_olen >> 3;
1699
1700 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1701 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1702 csum_partial(na, sizeof(*na)+na_olen, 0));
1703
1704 pip6->payload_len = htons(sizeof(*na)+na_olen);
1705
1706 skb_push(reply, sizeof(struct ipv6hdr));
1707
1708 reply->ip_summed = CHECKSUM_UNNECESSARY;
1709
1710 return reply;
1711}
1712
f564f45c
CW
1713static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1714{
1715 struct vxlan_dev *vxlan = netdev_priv(dev);
4b29dba9 1716 struct nd_msg *msg;
f564f45c
CW
1717 const struct ipv6hdr *iphdr;
1718 const struct in6_addr *saddr, *daddr;
4b29dba9
DS
1719 struct neighbour *n;
1720 struct inet6_dev *in6_dev;
f564f45c
CW
1721
1722 in6_dev = __in6_dev_get(dev);
1723 if (!in6_dev)
1724 goto out;
1725
f564f45c
CW
1726 iphdr = ipv6_hdr(skb);
1727 saddr = &iphdr->saddr;
1728 daddr = &iphdr->daddr;
1729
f564f45c
CW
1730 msg = (struct nd_msg *)skb_transport_header(skb);
1731 if (msg->icmph.icmp6_code != 0 ||
1732 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1733 goto out;
1734
4b29dba9
DS
1735 if (ipv6_addr_loopback(daddr) ||
1736 ipv6_addr_is_multicast(&msg->target))
1737 goto out;
1738
1739 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
f564f45c
CW
1740
1741 if (n) {
1742 struct vxlan_fdb *f;
4b29dba9 1743 struct sk_buff *reply;
f564f45c
CW
1744
1745 if (!(n->nud_state & NUD_CONNECTED)) {
1746 neigh_release(n);
1747 goto out;
1748 }
1749
1750 f = vxlan_find_mac(vxlan, n->ha);
1751 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1752 /* bridge-local neighbor */
1753 neigh_release(n);
1754 goto out;
1755 }
1756
4b29dba9
DS
1757 reply = vxlan_na_create(skb, n,
1758 !!(f ? f->flags & NTF_ROUTER : 0));
1759
f564f45c 1760 neigh_release(n);
4b29dba9
DS
1761
1762 if (reply == NULL)
1763 goto out;
1764
1765 if (netif_rx_ni(reply) == NET_RX_DROP)
1766 dev->stats.rx_dropped++;
1767
f564f45c 1768 } else if (vxlan->flags & VXLAN_F_L3MISS) {
4b29dba9
DS
1769 union vxlan_addr ipa = {
1770 .sin6.sin6_addr = msg->target,
a45e92a5 1771 .sin6.sin6_family = AF_INET6,
4b29dba9
DS
1772 };
1773
f564f45c
CW
1774 vxlan_ip_miss(dev, &ipa);
1775 }
1776
1777out:
1778 consume_skb(skb);
1779 return NETDEV_TX_OK;
1780}
1781#endif
1782
e4f67add
DS
1783static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1784{
1785 struct vxlan_dev *vxlan = netdev_priv(dev);
1786 struct neighbour *n;
e4f67add
DS
1787
1788 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1789 return false;
1790
1791 n = NULL;
1792 switch (ntohs(eth_hdr(skb)->h_proto)) {
1793 case ETH_P_IP:
e15a00aa
CW
1794 {
1795 struct iphdr *pip;
1796
e4f67add
DS
1797 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1798 return false;
1799 pip = ip_hdr(skb);
1800 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
e4c7ed41
CW
1801 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1802 union vxlan_addr ipa = {
1803 .sin.sin_addr.s_addr = pip->daddr,
a45e92a5 1804 .sin.sin_family = AF_INET,
e4c7ed41
CW
1805 };
1806
1807 vxlan_ip_miss(dev, &ipa);
1808 return false;
1809 }
1810
e4f67add 1811 break;
e15a00aa
CW
1812 }
1813#if IS_ENABLED(CONFIG_IPV6)
1814 case ETH_P_IPV6:
1815 {
1816 struct ipv6hdr *pip6;
1817
1818 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1819 return false;
1820 pip6 = ipv6_hdr(skb);
1821 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1822 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1823 union vxlan_addr ipa = {
1824 .sin6.sin6_addr = pip6->daddr,
a45e92a5 1825 .sin6.sin6_family = AF_INET6,
e15a00aa
CW
1826 };
1827
1828 vxlan_ip_miss(dev, &ipa);
1829 return false;
1830 }
1831
1832 break;
1833 }
1834#endif
e4f67add
DS
1835 default:
1836 return false;
1837 }
1838
1839 if (n) {
1840 bool diff;
1841
7367d0b5 1842 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
e4f67add
DS
1843 if (diff) {
1844 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1845 dev->addr_len);
1846 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1847 }
1848 neigh_release(n);
1849 return diff;
e4c7ed41
CW
1850 }
1851
e4f67add
DS
1852 return false;
1853}
1854
af33c1ad 1855static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
3511494c
TG
1856 struct vxlan_metadata *md)
1857{
1858 struct vxlanhdr_gbp *gbp;
1859
db79a621
TG
1860 if (!md->gbp)
1861 return;
1862
3511494c 1863 gbp = (struct vxlanhdr_gbp *)vxh;
54bfd872 1864 vxh->vx_flags |= VXLAN_HF_GBP;
3511494c
TG
1865
1866 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1867 gbp->dont_learn = 1;
1868
1869 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1870 gbp->policy_applied = 1;
1871
1872 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1873}
1874
e1e5314d
JB
1875static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1876 __be16 protocol)
1877{
1878 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1879
1880 gpe->np_applied = 1;
1881
1882 switch (protocol) {
1883 case htons(ETH_P_IP):
1884 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1885 return 0;
1886 case htons(ETH_P_IPV6):
1887 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1888 return 0;
1889 case htons(ETH_P_TEB):
1890 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1891 return 0;
1892 }
1893 return -EPFNOSUPPORT;
1894}
1895
f491e56d
JB
1896static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1897 int iphdr_len, __be32 vni,
1898 struct vxlan_metadata *md, u32 vxflags,
b4ed5cad 1899 bool udp_sum)
e4c7ed41 1900{
e4c7ed41 1901 struct vxlanhdr *vxh;
e4c7ed41
CW
1902 int min_headroom;
1903 int err;
dfd8645e 1904 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
e1e5314d 1905 __be16 inner_protocol = htons(ETH_P_TEB);
dfd8645e 1906
af33c1ad 1907 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
dfd8645e
TH
1908 skb->ip_summed == CHECKSUM_PARTIAL) {
1909 int csum_start = skb_checksum_start_offset(skb);
1910
1911 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1912 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1913 (skb->csum_offset == offsetof(struct udphdr, check) ||
b5708501 1914 skb->csum_offset == offsetof(struct tcphdr, check)))
dfd8645e 1915 type |= SKB_GSO_TUNNEL_REMCSUM;
dfd8645e 1916 }
e4c7ed41 1917
e4c7ed41 1918 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
4a4f86cc 1919 + VXLAN_HLEN + iphdr_len;
649c5b8b
PS
1920
1921 /* Need space for new headers (invalidates iph ptr) */
1922 err = skb_cow_head(skb, min_headroom);
e1e5314d 1923 if (unlikely(err))
c46b7897 1924 return err;
649c5b8b 1925
aed069df
AD
1926 err = iptunnel_handle_offloads(skb, type);
1927 if (err)
c46b7897 1928 return err;
b736a623 1929
49560532 1930 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
54bfd872
JB
1931 vxh->vx_flags = VXLAN_HF_VNI;
1932 vxh->vx_vni = vxlan_vni_field(vni);
49560532 1933
dfd8645e 1934 if (type & SKB_GSO_TUNNEL_REMCSUM) {
54bfd872 1935 unsigned int start;
dfd8645e 1936
54bfd872
JB
1937 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1938 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1939 vxh->vx_flags |= VXLAN_HF_RCO;
dfd8645e
TH
1940
1941 if (!skb_is_gso(skb)) {
1942 skb->ip_summed = CHECKSUM_NONE;
1943 skb->encapsulation = 0;
1944 }
1945 }
1946
af33c1ad
TH
1947 if (vxflags & VXLAN_F_GBP)
1948 vxlan_build_gbp_hdr(vxh, vxflags, md);
e1e5314d
JB
1949 if (vxflags & VXLAN_F_GPE) {
1950 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1951 if (err < 0)
c46b7897 1952 return err;
e1e5314d
JB
1953 inner_protocol = skb->protocol;
1954 }
3511494c 1955
e1e5314d 1956 skb_set_inner_protocol(skb, inner_protocol);
039f5062 1957 return 0;
49560532 1958}
49560532 1959
655c3de1 1960static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
1961 struct vxlan_sock *sock4,
1a8496ba 1962 struct sk_buff *skb, int oif, u8 tos,
4ecb1d83 1963 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
0c1d70af 1964 struct dst_cache *dst_cache,
db3c6139 1965 const struct ip_tunnel_info *info)
1a8496ba 1966{
db3c6139 1967 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
1a8496ba
JB
1968 struct rtable *rt = NULL;
1969 struct flowi4 fl4;
1970
655c3de1 1971 if (!sock4)
1972 return ERR_PTR(-EIO);
1973
db3c6139
DB
1974 if (tos && !info)
1975 use_cache = false;
1976 if (use_cache) {
0c1d70af
PA
1977 rt = dst_cache_get_ip4(dst_cache, saddr);
1978 if (rt)
1979 return rt;
1980 }
1981
1a8496ba
JB
1982 memset(&fl4, 0, sizeof(fl4));
1983 fl4.flowi4_oif = oif;
1984 fl4.flowi4_tos = RT_TOS(tos);
1985 fl4.flowi4_mark = skb->mark;
1986 fl4.flowi4_proto = IPPROTO_UDP;
1987 fl4.daddr = daddr;
272d96a5 1988 fl4.saddr = *saddr;
4ecb1d83
MP
1989 fl4.fl4_dport = dport;
1990 fl4.fl4_sport = sport;
1a8496ba
JB
1991
1992 rt = ip_route_output_key(vxlan->net, &fl4);
655c3de1 1993 if (likely(!IS_ERR(rt))) {
1994 if (rt->dst.dev == dev) {
1995 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
1996 ip_rt_put(rt);
1997 return ERR_PTR(-ELOOP);
1998 }
1999
1a8496ba 2000 *saddr = fl4.saddr;
0c1d70af
PA
2001 if (use_cache)
2002 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
655c3de1 2003 } else {
2004 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2005 return ERR_PTR(-ENETUNREACH);
0c1d70af 2006 }
1a8496ba
JB
2007 return rt;
2008}
2009
e5d4b29f
JB
2010#if IS_ENABLED(CONFIG_IPV6)
2011static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
655c3de1 2012 struct net_device *dev,
03dc52a8 2013 struct vxlan_sock *sock6,
1400615d 2014 struct sk_buff *skb, int oif, u8 tos,
e7f70af1 2015 __be32 label,
e5d4b29f 2016 const struct in6_addr *daddr,
0c1d70af 2017 struct in6_addr *saddr,
4ecb1d83 2018 __be16 dport, __be16 sport,
db3c6139
DB
2019 struct dst_cache *dst_cache,
2020 const struct ip_tunnel_info *info)
e5d4b29f 2021{
db3c6139 2022 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
e5d4b29f
JB
2023 struct dst_entry *ndst;
2024 struct flowi6 fl6;
2025 int err;
2026
c6fcc4fc 2027 if (!sock6)
2028 return ERR_PTR(-EIO);
2029
1400615d
DB
2030 if (tos && !info)
2031 use_cache = false;
db3c6139 2032 if (use_cache) {
0c1d70af
PA
2033 ndst = dst_cache_get_ip6(dst_cache, saddr);
2034 if (ndst)
2035 return ndst;
2036 }
2037
e5d4b29f
JB
2038 memset(&fl6, 0, sizeof(fl6));
2039 fl6.flowi6_oif = oif;
2040 fl6.daddr = *daddr;
272d96a5 2041 fl6.saddr = *saddr;
eaa93bf4 2042 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
e5d4b29f
JB
2043 fl6.flowi6_mark = skb->mark;
2044 fl6.flowi6_proto = IPPROTO_UDP;
4ecb1d83
MP
2045 fl6.fl6_dport = dport;
2046 fl6.fl6_sport = sport;
e5d4b29f
JB
2047
2048 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
c6fcc4fc 2049 sock6->sock->sk,
e5d4b29f 2050 &ndst, &fl6);
655c3de1 2051 if (unlikely(err < 0)) {
2052 netdev_dbg(dev, "no route to %pI6\n", daddr);
2053 return ERR_PTR(-ENETUNREACH);
2054 }
2055
2056 if (unlikely(ndst->dev == dev)) {
2057 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2058 dst_release(ndst);
2059 return ERR_PTR(-ELOOP);
2060 }
e5d4b29f
JB
2061
2062 *saddr = fl6.saddr;
db3c6139 2063 if (use_cache)
0c1d70af 2064 dst_cache_set_ip6(dst_cache, ndst, saddr);
e5d4b29f
JB
2065 return ndst;
2066}
2067#endif
2068
9dcc71e1
SS
2069/* Bypass encapsulation if the destination is local */
2070static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2071 struct vxlan_dev *dst_vxlan)
2072{
8f84985f 2073 struct pcpu_sw_netstats *tx_stats, *rx_stats;
e4c7ed41
CW
2074 union vxlan_addr loopback;
2075 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
ce6502a8
LR
2076 struct net_device *dev = skb->dev;
2077 int len = skb->len;
9dcc71e1 2078
8f84985f
LR
2079 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2080 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
9dcc71e1
SS
2081 skb->pkt_type = PACKET_HOST;
2082 skb->encapsulation = 0;
2083 skb->dev = dst_vxlan->dev;
2084 __skb_pull(skb, skb_network_offset(skb));
2085
e4c7ed41
CW
2086 if (remote_ip->sa.sa_family == AF_INET) {
2087 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2088 loopback.sa.sa_family = AF_INET;
2089#if IS_ENABLED(CONFIG_IPV6)
2090 } else {
2091 loopback.sin6.sin6_addr = in6addr_loopback;
2092 loopback.sa.sa_family = AF_INET6;
2093#endif
2094 }
2095
9dcc71e1 2096 if (dst_vxlan->flags & VXLAN_F_LEARN)
e4c7ed41 2097 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
9dcc71e1
SS
2098
2099 u64_stats_update_begin(&tx_stats->syncp);
2100 tx_stats->tx_packets++;
ce6502a8 2101 tx_stats->tx_bytes += len;
9dcc71e1
SS
2102 u64_stats_update_end(&tx_stats->syncp);
2103
2104 if (netif_rx(skb) == NET_RX_SUCCESS) {
2105 u64_stats_update_begin(&rx_stats->syncp);
2106 rx_stats->rx_packets++;
ce6502a8 2107 rx_stats->rx_bytes += len;
9dcc71e1
SS
2108 u64_stats_update_end(&rx_stats->syncp);
2109 } else {
ce6502a8 2110 dev->stats.rx_dropped++;
9dcc71e1
SS
2111 }
2112}
2113
fee1fad7 2114static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2115 struct vxlan_dev *vxlan, union vxlan_addr *daddr,
2116 __be32 dst_port, __be32 vni, struct dst_entry *dst,
2117 u32 rt_flags)
2118{
2119#if IS_ENABLED(CONFIG_IPV6)
2120 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2121 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2122 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2123 */
2124 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2125#endif
2126 /* Bypass encapsulation if the destination is local */
2127 if (rt_flags & RTCF_LOCAL &&
2128 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2129 struct vxlan_dev *dst_vxlan;
2130
2131 dst_release(dst);
2132 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2133 daddr->sa.sa_family, dst_port,
2134 vxlan->flags);
2135 if (!dst_vxlan) {
2136 dev->stats.tx_errors++;
2137 kfree_skb(skb);
2138
2139 return -ENOENT;
2140 }
2141 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2142 return 1;
2143 }
2144
2145 return 0;
2146}
2147
4ad16930
SH
2148static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2149 struct vxlan_rdst *rdst, bool did_rsc)
d342894c 2150{
d71785ff 2151 struct dst_cache *dst_cache;
3093fbe7 2152 struct ip_tunnel_info *info;
d342894c 2153 struct vxlan_dev *vxlan = netdev_priv(dev);
0770b53b 2154 const struct iphdr *old_iph = ip_hdr(skb);
e4c7ed41 2155 union vxlan_addr *dst;
272d96a5 2156 union vxlan_addr remote_ip, local_ip;
ee122c79
TG
2157 struct vxlan_metadata _md;
2158 struct vxlan_metadata *md = &_md;
e4c7ed41 2159 __be16 src_port = 0, dst_port;
655c3de1 2160 struct dst_entry *ndst = NULL;
e7f70af1 2161 __be32 vni, label;
d342894c 2162 __u8 tos, ttl;
0e6fbc5b 2163 int err;
ee122c79 2164 u32 flags = vxlan->flags;
b4ed5cad 2165 bool udp_sum = false;
f491e56d 2166 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
d342894c 2167
61adedf3 2168 info = skb_tunnel_info(skb);
3093fbe7 2169
ee122c79 2170 if (rdst) {
0770b53b 2171 dst = &rdst->remote_ip;
2172 if (vxlan_addr_any(dst)) {
2173 if (did_rsc) {
2174 /* short-circuited back to local bridge */
2175 vxlan_encap_bypass(skb, vxlan, vxlan);
2176 return;
2177 }
2178 goto drop;
2179 }
2180
0dfbdf41 2181 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
ee122c79 2182 vni = rdst->remote_vni;
b64c9908 2183 local_ip = vxlan->cfg.saddr;
d71785ff 2184 dst_cache = &rdst->dst_cache;
0770b53b 2185 md->gbp = skb->mark;
2186 ttl = vxlan->cfg.ttl;
2187 if (!ttl && vxlan_addr_multicast(dst))
2188 ttl = 1;
2189
2190 tos = vxlan->cfg.tos;
2191 if (tos == 1)
2192 tos = ip_tunnel_get_dsfield(old_iph, skb);
2193
2194 if (dst->sa.sa_family == AF_INET)
2195 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2196 else
2197 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2198 label = vxlan->cfg.label;
ee122c79
TG
2199 } else {
2200 if (!info) {
2201 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2202 dev->name);
2203 goto drop;
2204 }
b1be00a6 2205 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
272d96a5 2206 if (remote_ip.sa.sa_family == AF_INET) {
a725e514 2207 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
272d96a5 2208 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2209 } else {
a725e514 2210 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
272d96a5 2211 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2212 }
ee122c79 2213 dst = &remote_ip;
0770b53b 2214 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2215 vni = tunnel_id_to_key32(info->key.tun_id);
d71785ff 2216 dst_cache = &info->dst_cache;
0770b53b 2217 if (info->options_len)
2218 md = ip_tunnel_info_opts(info);
a725e514
JB
2219 ttl = info->key.ttl;
2220 tos = info->key.tos;
e7f70af1 2221 label = info->key.label;
b4ed5cad 2222 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
a725e514 2223 }
0770b53b 2224 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2225 vxlan->cfg.port_max, true);
a725e514 2226
e3256838 2227 rcu_read_lock();
e4c7ed41 2228 if (dst->sa.sa_family == AF_INET) {
c6fcc4fc 2229 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
c46b7897 2230 struct rtable *rt;
0770b53b 2231 __be16 df = 0;
c6fcc4fc 2232
655c3de1 2233 rt = vxlan_get_route(vxlan, dev, sock4, skb,
1a8496ba 2234 rdst ? rdst->remote_ifindex : 0, tos,
272d96a5 2235 dst->sin.sin_addr.s_addr,
b64c9908 2236 &local_ip.sin.sin_addr.s_addr,
4ecb1d83 2237 dst_port, src_port,
d71785ff 2238 dst_cache, info);
8ebd115b
DM
2239 if (IS_ERR(rt)) {
2240 err = PTR_ERR(rt);
c46b7897 2241 goto tx_error;
8ebd115b 2242 }
e4c7ed41 2243
e05202e7
JV
2244 if (fan_has_map(&vxlan->fan) && rt->rt_flags & RTCF_LOCAL) {
2245 netdev_dbg(dev, "discard fan to localhost %pI4\n",
2246 &dst->sin.sin_addr.s_addr);
2247 ip_rt_put(rt);
2248 goto tx_free;
2249 }
2250
e4c7ed41 2251 /* Bypass encapsulation if the destination is local */
fee1fad7 2252 if (!info) {
2253 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2254 dst_port, vni, &rt->dst,
2255 rt->rt_flags);
2256 if (err)
e3256838 2257 goto out_unlock;
fee1fad7 2258 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
6ceb31ca 2259 df = htons(IP_DF);
fee1fad7 2260 }
6ceb31ca 2261
c46b7897 2262 ndst = &rt->dst;
e4c7ed41
CW
2263 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2264 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
c46b7897 2265 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
54bfd872 2266 vni, md, flags, udp_sum);
f491e56d 2267 if (err < 0)
c46b7897 2268 goto tx_error;
f491e56d 2269
b64c9908 2270 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
f491e56d
JB
2271 dst->sin.sin_addr.s_addr, tos, ttl, df,
2272 src_port, dst_port, xnet, !udp_sum);
e4c7ed41
CW
2273#if IS_ENABLED(CONFIG_IPV6)
2274 } else {
c6fcc4fc 2275 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
e4c7ed41 2276
655c3de1 2277 ndst = vxlan6_get_route(vxlan, dev, sock6, skb,
1400615d 2278 rdst ? rdst->remote_ifindex : 0, tos,
272d96a5 2279 label, &dst->sin6.sin6_addr,
b64c9908 2280 &local_ip.sin6.sin6_addr,
4ecb1d83 2281 dst_port, src_port,
db3c6139 2282 dst_cache, info);
e5d4b29f 2283 if (IS_ERR(ndst)) {
8ebd115b 2284 err = PTR_ERR(ndst);
c46b7897 2285 ndst = NULL;
9dcc71e1 2286 goto tx_error;
e4c7ed41 2287 }
655c3de1 2288
fee1fad7 2289 if (!info) {
2290 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
49560532 2291
fee1fad7 2292 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2293 dst_port, vni, ndst,
2294 rt6i_flags);
2295 if (err)
e3256838 2296 goto out_unlock;
fee1fad7 2297 }
35e2d115 2298
1400615d 2299 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
e4c7ed41 2300 ttl = ttl ? : ip6_dst_hoplimit(ndst);
f491e56d
JB
2301 skb_scrub_packet(skb, xnet);
2302 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
54bfd872 2303 vni, md, flags, udp_sum);
c46b7897 2304 if (err < 0)
2305 goto tx_error;
2306
0770b53b 2307 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
b64c9908 2308 &local_ip.sin6.sin6_addr,
272d96a5 2309 &dst->sin6.sin6_addr, tos, ttl,
e7f70af1 2310 label, src_port, dst_port, !udp_sum);
e4c7ed41
CW
2311#endif
2312 }
e3256838
JK
2313out_unlock:
2314 rcu_read_unlock();
4ad16930 2315 return;
d342894c 2316
2317drop:
2318 dev->stats.tx_dropped++;
c46b7897 2319 dev_kfree_skb(skb);
2320 return;
d342894c 2321
2322tx_error:
e3256838 2323 rcu_read_unlock();
655c3de1 2324 if (err == -ELOOP)
2325 dev->stats.collisions++;
2326 else if (err == -ENETUNREACH)
2327 dev->stats.tx_carrier_errors++;
c46b7897 2328 dst_release(ndst);
d342894c 2329 dev->stats.tx_errors++;
e05202e7 2330tx_free:
c46b7897 2331 kfree_skb(skb);
d342894c 2332}
2333
6681712d
DS
2334/* Transmit local packets over Vxlan
2335 *
2336 * Outer IP header inherits ECN and DF from inner header.
2337 * Outer UDP destination is the VXLAN assigned port.
2338 * source port is based on hash of flow
2339 */
2340static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2341{
2342 struct vxlan_dev *vxlan = netdev_priv(dev);
3093fbe7 2343 const struct ip_tunnel_info *info;
6681712d
DS
2344 struct ethhdr *eth;
2345 bool did_rsc = false;
8f646c92 2346 struct vxlan_rdst *rdst, *fdst = NULL;
6681712d 2347 struct vxlan_fdb *f;
6681712d 2348
61adedf3 2349 info = skb_tunnel_info(skb);
3093fbe7 2350
6681712d 2351 skb_reset_mac_header(skb);
6681712d 2352
47e5d1b0
JB
2353 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
2354 if (info && info->mode & IP_TUNNEL_INFO_TX)
2355 vxlan_xmit_one(skb, dev, NULL, false);
2356 else
2357 kfree_skb(skb);
2358 return NETDEV_TX_OK;
2359 }
2360
2361 if (vxlan->flags & VXLAN_F_PROXY) {
2362 eth = eth_hdr(skb);
f564f45c
CW
2363 if (ntohs(eth->h_proto) == ETH_P_ARP)
2364 return arp_reduce(dev, skb);
2365#if IS_ENABLED(CONFIG_IPV6)
2366 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
91269e39
LR
2367 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2368 + sizeof(struct nd_msg)) &&
f564f45c
CW
2369 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2370 struct nd_msg *msg;
2371
2372 msg = (struct nd_msg *)skb_transport_header(skb);
2373 if (msg->icmph.icmp6_code == 0 &&
2374 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2375 return neigh_reduce(dev, skb);
2376 }
2377#endif
2378 }
6681712d 2379
e05202e7
JV
2380 if (fan_has_map(&vxlan->fan)) {
2381 struct vxlan_rdst fan_rdst;
2382
2383 netdev_dbg(vxlan->dev, "vxlan_xmit p %x d %pM\n",
2384 eth->h_proto, eth->h_dest);
2385 if (vxlan_fan_build_rdst(vxlan, skb, &fan_rdst)) {
2386 dev->stats.tx_dropped++;
2387 kfree_skb(skb);
2388 return NETDEV_TX_OK;
2389 }
2390 vxlan_xmit_one(skb, dev, &fan_rdst, 0);
2391 return NETDEV_TX_OK;
2392 }
2393
47e5d1b0 2394 eth = eth_hdr(skb);
e05202e7 2395
6681712d 2396 f = vxlan_find_mac(vxlan, eth->h_dest);
ae884082
DS
2397 did_rsc = false;
2398
2399 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
e15a00aa
CW
2400 (ntohs(eth->h_proto) == ETH_P_IP ||
2401 ntohs(eth->h_proto) == ETH_P_IPV6)) {
ae884082
DS
2402 did_rsc = route_shortcircuit(dev, skb);
2403 if (did_rsc)
2404 f = vxlan_find_mac(vxlan, eth->h_dest);
2405 }
2406
6681712d 2407 if (f == NULL) {
afbd8bae
MR
2408 f = vxlan_find_mac(vxlan, all_zeros_mac);
2409 if (f == NULL) {
2410 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2411 !is_multicast_ether_addr(eth->h_dest))
2412 vxlan_fdb_miss(vxlan, eth->h_dest);
2413
2414 dev->stats.tx_dropped++;
8f646c92 2415 kfree_skb(skb);
afbd8bae
MR
2416 return NETDEV_TX_OK;
2417 }
2418 }
6681712d 2419
afbd8bae
MR
2420 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2421 struct sk_buff *skb1;
6681712d 2422
8f646c92
ED
2423 if (!fdst) {
2424 fdst = rdst;
2425 continue;
2426 }
afbd8bae
MR
2427 skb1 = skb_clone(skb, GFP_ATOMIC);
2428 if (skb1)
2429 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
6681712d
DS
2430 }
2431
8f646c92
ED
2432 if (fdst)
2433 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2434 else
2435 kfree_skb(skb);
4ad16930 2436 return NETDEV_TX_OK;
6681712d
DS
2437}
2438
d342894c 2439/* Walk the forwarding table and purge stale entries */
2440static void vxlan_cleanup(unsigned long arg)
2441{
2442 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2443 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2444 unsigned int h;
2445
2446 if (!netif_running(vxlan->dev))
2447 return;
2448
d342894c 2449 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2450 struct hlist_node *p, *n;
14e1d0fa
SD
2451
2452 spin_lock_bh(&vxlan->hash_lock);
d342894c 2453 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2454 struct vxlan_fdb *f
2455 = container_of(p, struct vxlan_fdb, hlist);
2456 unsigned long timeout;
2457
efb5f68f 2458 if (f->state & (NUD_PERMANENT | NUD_NOARP))
d342894c 2459 continue;
2460
0dfbdf41 2461 timeout = f->used + vxlan->cfg.age_interval * HZ;
d342894c 2462 if (time_before_eq(timeout, jiffies)) {
2463 netdev_dbg(vxlan->dev,
2464 "garbage collect %pM\n",
2465 f->eth_addr);
2466 f->state = NUD_STALE;
2467 vxlan_fdb_destroy(vxlan, f);
2468 } else if (time_before(timeout, next_timer))
2469 next_timer = timeout;
2470 }
14e1d0fa 2471 spin_unlock_bh(&vxlan->hash_lock);
d342894c 2472 }
d342894c 2473
2474 mod_timer(&vxlan->age_timer, next_timer);
2475}
2476
9c2e24e1
PS
2477static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2478{
56ef9c90 2479 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
54bfd872 2480 __be32 vni = vxlan->default_dst.remote_vni;
9c2e24e1 2481
56ef9c90 2482 spin_lock(&vn->sock_lock);
9c2e24e1 2483 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
56ef9c90 2484 spin_unlock(&vn->sock_lock);
9c2e24e1
PS
2485}
2486
d342894c 2487/* Setup stats when device is created */
2488static int vxlan_init(struct net_device *dev)
2489{
1c213bd2 2490 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
e8171045 2491 if (!dev->tstats)
d342894c 2492 return -ENOMEM;
2493
2494 return 0;
2495}
2496
ba609e9b 2497static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
afbd8bae
MR
2498{
2499 struct vxlan_fdb *f;
2500
2501 spin_lock_bh(&vxlan->hash_lock);
2502 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2503 if (f)
2504 vxlan_fdb_destroy(vxlan, f);
2505 spin_unlock_bh(&vxlan->hash_lock);
2506}
2507
ebf4063e
SH
2508static void vxlan_uninit(struct net_device *dev)
2509{
2510 struct vxlan_dev *vxlan = netdev_priv(dev);
ebf4063e 2511
ba609e9b 2512 vxlan_fdb_delete_default(vxlan);
afbd8bae 2513
ebf4063e
SH
2514 free_percpu(dev->tstats);
2515}
2516
d342894c 2517/* Start ageing timer and join group when device is brought up */
2518static int vxlan_open(struct net_device *dev)
2519{
2520 struct vxlan_dev *vxlan = netdev_priv(dev);
205f356d 2521 int ret;
56ef9c90 2522
205f356d
JB
2523 ret = vxlan_sock_add(vxlan);
2524 if (ret < 0)
2525 return ret;
d342894c 2526
79d4a94f 2527 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
56ef9c90 2528 ret = vxlan_igmp_join(vxlan);
bef0057b
MRL
2529 if (ret == -EADDRINUSE)
2530 ret = 0;
56ef9c90 2531 if (ret) {
205f356d 2532 vxlan_sock_release(vxlan);
56ef9c90
MRL
2533 return ret;
2534 }
d342894c 2535 }
2536
0dfbdf41 2537 if (vxlan->cfg.age_interval)
d342894c 2538 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2539
56ef9c90 2540 return ret;
d342894c 2541}
2542
2543/* Purge the forwarding table */
8b3f9337 2544static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
d342894c 2545{
31fec5aa 2546 unsigned int h;
d342894c 2547
2548 spin_lock_bh(&vxlan->hash_lock);
2549 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2550 struct hlist_node *p, *n;
2551 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2552 struct vxlan_fdb *f
2553 = container_of(p, struct vxlan_fdb, hlist);
8b3f9337
RP
2554 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2555 continue;
afbd8bae
MR
2556 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2557 if (!is_zero_ether_addr(f->eth_addr))
2558 vxlan_fdb_destroy(vxlan, f);
d342894c 2559 }
2560 }
2561 spin_unlock_bh(&vxlan->hash_lock);
2562}
2563
2564/* Cleanup timer and forwarding table on shutdown */
2565static int vxlan_stop(struct net_device *dev)
2566{
2567 struct vxlan_dev *vxlan = netdev_priv(dev);
f01ec1c0 2568 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
56ef9c90 2569 int ret = 0;
d342894c 2570
24c0e683 2571 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
f13b1689 2572 !vxlan_group_used(vn, vxlan))
56ef9c90 2573 ret = vxlan_igmp_leave(vxlan);
d342894c 2574
2575 del_timer_sync(&vxlan->age_timer);
2576
8b3f9337 2577 vxlan_flush(vxlan, false);
205f356d 2578 vxlan_sock_release(vxlan);
d342894c 2579
56ef9c90 2580 return ret;
d342894c 2581}
2582
d342894c 2583/* Stub, nothing needs to be done. */
2584static void vxlan_set_multicast_list(struct net_device *dev)
2585{
2586}
2587
91572088 2588static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
345010b5 2589{
91572088
JW
2590 struct vxlan_dev *vxlan = netdev_priv(dev);
2591 struct vxlan_rdst *dst = &vxlan->default_dst;
2592 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2593 dst->remote_ifindex);
2594 bool use_ipv6 = false;
345010b5
DB
2595
2596 if (dst->remote_ip.sa.sa_family == AF_INET6)
91572088 2597 use_ipv6 = true;
345010b5 2598
91572088
JW
2599 /* This check is different than dev->max_mtu, because it looks at
2600 * the lowerdev->mtu, rather than the static dev->max_mtu
2601 */
2602 if (lowerdev) {
2603 int max_mtu = lowerdev->mtu -
2604 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2605 if (new_mtu > max_mtu)
72564b59 2606 return -EINVAL;
72564b59
DW
2607 }
2608
345010b5
DB
2609 dev->mtu = new_mtu;
2610 return 0;
2611}
2612
fc4099f1
PS
2613static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2614{
2615 struct vxlan_dev *vxlan = netdev_priv(dev);
2616 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2617 __be16 sport, dport;
2618
2619 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2620 vxlan->cfg.port_max, true);
2621 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2622
239e944f 2623 if (ip_tunnel_info_af(info) == AF_INET) {
c6fcc4fc 2624 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
1a8496ba
JB
2625 struct rtable *rt;
2626
655c3de1 2627 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
1a8496ba 2628 info->key.u.ipv4.dst,
22f0708a
PA
2629 &info->key.u.ipv4.src, dport, sport,
2630 &info->dst_cache, info);
1a8496ba
JB
2631 if (IS_ERR(rt))
2632 return PTR_ERR(rt);
2633 ip_rt_put(rt);
239e944f
JB
2634 } else {
2635#if IS_ENABLED(CONFIG_IPV6)
03dc52a8 2636 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
239e944f
JB
2637 struct dst_entry *ndst;
2638
655c3de1 2639 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
e7f70af1 2640 info->key.label, &info->key.u.ipv6.dst,
22f0708a
PA
2641 &info->key.u.ipv6.src, dport, sport,
2642 &info->dst_cache, info);
239e944f
JB
2643 if (IS_ERR(ndst))
2644 return PTR_ERR(ndst);
2645 dst_release(ndst);
239e944f
JB
2646#else /* !CONFIG_IPV6 */
2647 return -EPFNOSUPPORT;
2648#endif
2649 }
1a8496ba
JB
2650 info->key.tp_src = sport;
2651 info->key.tp_dst = dport;
239e944f 2652 return 0;
fc4099f1
PS
2653}
2654
0c867c9b 2655static const struct net_device_ops vxlan_netdev_ether_ops = {
d342894c 2656 .ndo_init = vxlan_init,
ebf4063e 2657 .ndo_uninit = vxlan_uninit,
d342894c 2658 .ndo_open = vxlan_open,
2659 .ndo_stop = vxlan_stop,
2660 .ndo_start_xmit = vxlan_xmit,
e8171045 2661 .ndo_get_stats64 = ip_tunnel_get_stats64,
d342894c 2662 .ndo_set_rx_mode = vxlan_set_multicast_list,
345010b5 2663 .ndo_change_mtu = vxlan_change_mtu,
d342894c 2664 .ndo_validate_addr = eth_validate_addr,
2665 .ndo_set_mac_address = eth_mac_addr,
2666 .ndo_fdb_add = vxlan_fdb_add,
2667 .ndo_fdb_del = vxlan_fdb_delete,
2668 .ndo_fdb_dump = vxlan_fdb_dump,
fc4099f1 2669 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
d342894c 2670};
2671
e1e5314d
JB
2672static const struct net_device_ops vxlan_netdev_raw_ops = {
2673 .ndo_init = vxlan_init,
2674 .ndo_uninit = vxlan_uninit,
2675 .ndo_open = vxlan_open,
2676 .ndo_stop = vxlan_stop,
2677 .ndo_start_xmit = vxlan_xmit,
2678 .ndo_get_stats64 = ip_tunnel_get_stats64,
2679 .ndo_change_mtu = vxlan_change_mtu,
2680 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2681};
2682
d342894c 2683/* Info for udev, that this is a virtual tunnel endpoint */
2684static struct device_type vxlan_type = {
2685 .name = "vxlan",
2686};
2687
e5de25dc 2688/* Calls the ndo_udp_tunnel_add of the caller in order to
35e42379 2689 * supply the listening VXLAN udp ports. Callers are expected
e5de25dc 2690 * to implement the ndo_udp_tunnel_add.
53cf5275 2691 */
b7aade15 2692static void vxlan_push_rx_ports(struct net_device *dev)
53cf5275
JG
2693{
2694 struct vxlan_sock *vs;
2695 struct net *net = dev_net(dev);
2696 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
35e42379 2697 unsigned int i;
53cf5275
JG
2698
2699 spin_lock(&vn->sock_lock);
2700 for (i = 0; i < PORT_HASH_SIZE; ++i) {
e7b3db5e
AD
2701 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2702 udp_tunnel_push_rx_port(dev, vs->sock,
b9adcd69
AD
2703 (vs->flags & VXLAN_F_GPE) ?
2704 UDP_TUNNEL_TYPE_VXLAN_GPE :
e7b3db5e 2705 UDP_TUNNEL_TYPE_VXLAN);
53cf5275
JG
2706 }
2707 spin_unlock(&vn->sock_lock);
2708}
53cf5275 2709
d342894c 2710/* Initialize the device structure. */
2711static void vxlan_setup(struct net_device *dev)
2712{
2713 struct vxlan_dev *vxlan = netdev_priv(dev);
31fec5aa 2714 unsigned int h;
d342894c 2715
65226ef8
JB
2716 eth_hw_addr_random(dev);
2717 ether_setup(dev);
2718
ebf4063e 2719 dev->destructor = free_netdev;
d342894c 2720 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2721
d342894c 2722 dev->features |= NETIF_F_LLTX;
d6727fe3 2723 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
0afb1666 2724 dev->features |= NETIF_F_RXCSUM;
05c0db08 2725 dev->features |= NETIF_F_GSO_SOFTWARE;
0afb1666 2726
1eaa8178 2727 dev->vlan_features = dev->features;
0afb1666 2728 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
05c0db08 2729 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
02875878 2730 netif_keep_dst(dev);
0c867c9b 2731 dev->priv_flags |= IFF_NO_QUEUE;
d342894c 2732
553675fb 2733 INIT_LIST_HEAD(&vxlan->next);
d342894c 2734 spin_lock_init(&vxlan->hash_lock);
2735
2736 init_timer_deferrable(&vxlan->age_timer);
2737 vxlan->age_timer.function = vxlan_cleanup;
2738 vxlan->age_timer.data = (unsigned long) vxlan;
2739
0dfbdf41 2740 vxlan->cfg.dst_port = htons(vxlan_port);
05f47d69 2741
d342894c 2742 vxlan->dev = dev;
2743
58ce31cc
TH
2744 gro_cells_init(&vxlan->gro_cells, dev);
2745
d342894c 2746 for (h = 0; h < FDB_HASH_SIZE; ++h)
2747 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
e05202e7
JV
2748
2749 INIT_LIST_HEAD(&vxlan->fan.fan_maps);
d342894c 2750}
2751
0c867c9b
JB
2752static void vxlan_ether_setup(struct net_device *dev)
2753{
0c867c9b
JB
2754 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2755 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2756 dev->netdev_ops = &vxlan_netdev_ether_ops;
2757}
2758
e1e5314d
JB
2759static void vxlan_raw_setup(struct net_device *dev)
2760{
65226ef8 2761 dev->header_ops = NULL;
e1e5314d
JB
2762 dev->type = ARPHRD_NONE;
2763 dev->hard_header_len = 0;
2764 dev->addr_len = 0;
e1e5314d
JB
2765 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2766 dev->netdev_ops = &vxlan_netdev_raw_ops;
2767}
2768
d342894c 2769static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2770 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
5d174dd8 2771 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
e4c7ed41 2772 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
d342894c 2773 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2774 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
e4c7ed41 2775 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
d342894c 2776 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2777 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
e7f70af1 2778 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
d342894c 2779 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2780 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2781 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
05f47d69 2782 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
e4f67add
DS
2783 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2784 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2785 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2786 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
f8a9b1bc 2787 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
823aa873 2788 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
5c91ae08
TH
2789 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2790 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2791 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
dfd8645e
TH
2792 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2793 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3511494c 2794 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
e1e5314d 2795 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
0ace2ca8 2796 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
d342894c 2797};
2798
2799static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2800{
2801 if (tb[IFLA_ADDRESS]) {
2802 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2803 pr_debug("invalid link address (not ethernet)\n");
2804 return -EINVAL;
2805 }
2806
2807 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2808 pr_debug("invalid all zero ethernet address\n");
2809 return -EADDRNOTAVAIL;
2810 }
2811 }
2812
2813 if (!data)
2814 return -EINVAL;
2815
2816 if (data[IFLA_VXLAN_ID]) {
2817 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
747c5462 2818 if (id >= VXLAN_N_VID)
d342894c 2819 return -ERANGE;
2820 }
2821
05f47d69 2822 if (data[IFLA_VXLAN_PORT_RANGE]) {
2823 const struct ifla_vxlan_port_range *p
2824 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2825
2826 if (ntohs(p->high) < ntohs(p->low)) {
2827 pr_debug("port range %u .. %u not valid\n",
2828 ntohs(p->low), ntohs(p->high));
2829 return -EINVAL;
2830 }
2831 }
2832
d342894c 2833 return 0;
2834}
2835
1b13c97f
YB
2836static void vxlan_get_drvinfo(struct net_device *netdev,
2837 struct ethtool_drvinfo *drvinfo)
2838{
2839 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2840 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2841}
2842
2843static const struct ethtool_ops vxlan_ethtool_ops = {
2844 .get_drvinfo = vxlan_get_drvinfo,
2845 .get_link = ethtool_op_get_link,
2846};
2847
3ee64f39
TH
2848static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2849 __be16 port, u32 flags)
553675fb 2850{
e4c7ed41 2851 struct socket *sock;
3ee64f39
TH
2852 struct udp_port_cfg udp_conf;
2853 int err;
e4c7ed41 2854
3ee64f39 2855 memset(&udp_conf, 0, sizeof(udp_conf));
e4c7ed41 2856
3ee64f39
TH
2857 if (ipv6) {
2858 udp_conf.family = AF_INET6;
3ee64f39 2859 udp_conf.use_udp6_rx_checksums =
3dc2b6a8 2860 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
a43a9ef6 2861 udp_conf.ipv6_v6only = 1;
3ee64f39
TH
2862 } else {
2863 udp_conf.family = AF_INET;
e4c7ed41 2864 }
e4c7ed41 2865
3ee64f39 2866 udp_conf.local_udp_port = port;
553675fb 2867
3ee64f39
TH
2868 /* Open UDP socket */
2869 err = udp_sock_create(net, &udp_conf, &sock);
2870 if (err < 0)
2871 return ERR_PTR(err);
e4c7ed41 2872
39deb2c7 2873 return sock;
e4c7ed41
CW
2874}
2875
2876/* Create new listen socket if needed */
b1be00a6
JB
2877static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2878 __be16 port, u32 flags)
e4c7ed41
CW
2879{
2880 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2881 struct vxlan_sock *vs;
2882 struct socket *sock;
e4c7ed41 2883 unsigned int h;
acbf74a7 2884 struct udp_tunnel_sock_cfg tunnel_cfg;
e4c7ed41 2885
dc01e7d3 2886 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
e4c7ed41
CW
2887 if (!vs)
2888 return ERR_PTR(-ENOMEM);
2889
2890 for (h = 0; h < VNI_HASH_SIZE; ++h)
2891 INIT_HLIST_HEAD(&vs->vni_list[h]);
2892
3ee64f39 2893 sock = vxlan_create_sock(net, ipv6, port, flags);
39deb2c7 2894 if (IS_ERR(sock)) {
56ef9c90
MRL
2895 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2896 PTR_ERR(sock));
553675fb 2897 kfree(vs);
e50fddc8 2898 return ERR_CAST(sock);
553675fb 2899 }
e4c7ed41
CW
2900
2901 vs->sock = sock;
9c2e24e1 2902 atomic_set(&vs->refcnt, 1);
af33c1ad 2903 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
553675fb 2904
9c2e24e1
PS
2905 spin_lock(&vn->sock_lock);
2906 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
e7b3db5e 2907 udp_tunnel_notify_add_rx_port(sock,
b9adcd69
AD
2908 (vs->flags & VXLAN_F_GPE) ?
2909 UDP_TUNNEL_TYPE_VXLAN_GPE :
e7b3db5e 2910 UDP_TUNNEL_TYPE_VXLAN);
9c2e24e1 2911 spin_unlock(&vn->sock_lock);
553675fb 2912
2913 /* Mark socket as an encapsulation socket. */
5602c48c 2914 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
acbf74a7
AZ
2915 tunnel_cfg.sk_user_data = vs;
2916 tunnel_cfg.encap_type = 1;
f2d1968e 2917 tunnel_cfg.encap_rcv = vxlan_rcv;
acbf74a7 2918 tunnel_cfg.encap_destroy = NULL;
5602c48c
TH
2919 tunnel_cfg.gro_receive = vxlan_gro_receive;
2920 tunnel_cfg.gro_complete = vxlan_gro_complete;
acbf74a7
AZ
2921
2922 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
e4c7ed41 2923
9c2e24e1
PS
2924 return vs;
2925}
2926
b1be00a6 2927static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
9c2e24e1 2928{
205f356d
JB
2929 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2930 struct vxlan_sock *vs = NULL;
9c2e24e1 2931
205f356d 2932 if (!vxlan->cfg.no_share) {
56ef9c90 2933 spin_lock(&vn->sock_lock);
205f356d
JB
2934 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2935 vxlan->cfg.dst_port, vxlan->flags);
2936 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
56ef9c90 2937 spin_unlock(&vn->sock_lock);
205f356d 2938 return -EBUSY;
56ef9c90
MRL
2939 }
2940 spin_unlock(&vn->sock_lock);
2941 }
205f356d 2942 if (!vs)
b1be00a6
JB
2943 vs = vxlan_socket_create(vxlan->net, ipv6,
2944 vxlan->cfg.dst_port, vxlan->flags);
205f356d
JB
2945 if (IS_ERR(vs))
2946 return PTR_ERR(vs);
b1be00a6
JB
2947#if IS_ENABLED(CONFIG_IPV6)
2948 if (ipv6)
c6fcc4fc 2949 rcu_assign_pointer(vxlan->vn6_sock, vs);
b1be00a6
JB
2950 else
2951#endif
c6fcc4fc 2952 rcu_assign_pointer(vxlan->vn4_sock, vs);
205f356d
JB
2953 vxlan_vs_add_dev(vs, vxlan);
2954 return 0;
553675fb 2955}
2956
b1be00a6
JB
2957static int vxlan_sock_add(struct vxlan_dev *vxlan)
2958{
2959 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2960 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
2961 int ret = 0;
2962
c6fcc4fc 2963 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
b1be00a6 2964#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 2965 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
b1be00a6
JB
2966 if (ipv6 || metadata)
2967 ret = __vxlan_sock_add(vxlan, true);
2968#endif
2969 if (!ret && (!ipv6 || metadata))
2970 ret = __vxlan_sock_add(vxlan, false);
2971 if (ret < 0)
2972 vxlan_sock_release(vxlan);
2973 return ret;
2974}
2975
0dfbdf41
TG
2976static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2977 struct vxlan_config *conf)
d342894c 2978{
33564bbb 2979 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
07b9b37c 2980 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
c7995c43 2981 struct vxlan_rdst *dst = &vxlan->default_dst;
b1be00a6 2982 unsigned short needed_headroom = ETH_HLEN;
d342894c 2983 int err;
e4c7ed41 2984 bool use_ipv6 = false;
0dfbdf41 2985 __be16 default_port = vxlan->cfg.dst_port;
7e059158 2986 struct net_device *lowerdev = NULL;
d342894c 2987
e1e5314d 2988 if (conf->flags & VXLAN_F_GPE) {
e1e5314d
JB
2989 /* For now, allow GPE only together with COLLECT_METADATA.
2990 * This can be relaxed later; in such case, the other side
2991 * of the PtP link will have to be provided.
2992 */
3555621d
JB
2993 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2994 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2995 pr_info("unsupported combination of extensions\n");
e1e5314d 2996 return -EINVAL;
3555621d 2997 }
e1e5314d
JB
2998
2999 vxlan_raw_setup(dev);
3000 } else {
3001 vxlan_ether_setup(dev);
3002 }
0c867c9b 3003
91572088
JW
3004 /* MTU range: 68 - 65535 */
3005 dev->min_mtu = ETH_MIN_MTU;
3006 dev->max_mtu = ETH_MAX_MTU;
3007
33564bbb 3008 vxlan->net = src_net;
f01ec1c0 3009
0dfbdf41 3010 dst->remote_vni = conf->vni;
d342894c 3011
0dfbdf41 3012 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
d342894c 3013
0dfbdf41
TG
3014 /* Unless IPv6 is explicitly requested, assume IPv4 */
3015 if (!dst->remote_ip.sa.sa_family)
3016 dst->remote_ip.sa.sa_family = AF_INET;
e4c7ed41 3017
0dfbdf41 3018 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
057ba29b
JB
3019 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
3020 if (!IS_ENABLED(CONFIG_IPV6))
3021 return -EPFNOSUPPORT;
e4c7ed41 3022 use_ipv6 = true;
b1be00a6 3023 vxlan->flags |= VXLAN_F_IPV6;
057ba29b 3024 }
d342894c 3025
e7f70af1
DB
3026 if (conf->label && !use_ipv6) {
3027 pr_info("label only supported in use with IPv6\n");
3028 return -EINVAL;
3029 }
3030
0dfbdf41 3031 if (conf->remote_ifindex) {
7e059158 3032 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
0dfbdf41 3033 dst->remote_ifindex = conf->remote_ifindex;
34e02aa1 3034
3035 if (!lowerdev) {
c7995c43 3036 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
34e02aa1 3037 return -ENODEV;
3038 }
d342894c 3039
e4c7ed41
CW
3040#if IS_ENABLED(CONFIG_IPV6)
3041 if (use_ipv6) {
3042 struct inet6_dev *idev = __in6_dev_get(lowerdev);
3043 if (idev && idev->cnf.disable_ipv6) {
3044 pr_info("IPv6 is disabled via sysctl\n");
3045 return -EPERM;
3046 }
e4c7ed41
CW
3047 }
3048#endif
3049
0dfbdf41 3050 if (!conf->mtu)
91572088
JW
3051 dev->mtu = lowerdev->mtu -
3052 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
1ba56fb4 3053
b1be00a6 3054 needed_headroom = lowerdev->hard_header_len;
9b4cdd51
JB
3055 } else if (vxlan_addr_multicast(&dst->remote_ip)) {
3056 pr_info("multicast destination requires interface to be specified\n");
3057 return -EINVAL;
9dc2ad10 3058 }
d342894c 3059
7e059158 3060 if (conf->mtu) {
91572088
JW
3061 int max_mtu = ETH_MAX_MTU;
3062
3063 if (lowerdev)
3064 max_mtu = lowerdev->mtu;
3065
3066 max_mtu -= (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
3067
3068 if (conf->mtu < dev->min_mtu || conf->mtu > dev->max_mtu)
3069 return -EINVAL;
3070
3071 dev->mtu = conf->mtu;
3072
3073 if (conf->mtu > max_mtu)
3074 dev->mtu = max_mtu;
7e059158
DW
3075 }
3076
b1be00a6
JB
3077 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3078 needed_headroom += VXLAN6_HEADROOM;
3079 else
3080 needed_headroom += VXLAN_HEADROOM;
3081 dev->needed_headroom = needed_headroom;
3082
0dfbdf41 3083 memcpy(&vxlan->cfg, conf, sizeof(*conf));
e1e5314d
JB
3084 if (!vxlan->cfg.dst_port) {
3085 if (conf->flags & VXLAN_F_GPE)
d5ff72d9 3086 vxlan->cfg.dst_port = htons(4790); /* IANA VXLAN-GPE port */
e1e5314d
JB
3087 else
3088 vxlan->cfg.dst_port = default_port;
3089 }
0dfbdf41
TG
3090 vxlan->flags |= conf->flags;
3091
3092 if (!vxlan->cfg.age_interval)
3093 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
3094
07b9b37c
ND
3095 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3096 if (tmp->cfg.vni == conf->vni &&
3097 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
3098 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
3099 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
3100 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
3555621d
JB
3101 (vxlan->flags & VXLAN_F_RCV_FLAGS)) {
3102 pr_info("duplicate VNI %u\n", be32_to_cpu(conf->vni));
3103 return -EEXIST;
3104 }
07b9b37c 3105 }
0dfbdf41
TG
3106
3107 dev->ethtool_ops = &vxlan_ethtool_ops;
3108
3109 /* create an fdb entry for a valid default destination */
3110 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
3111 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3112 &vxlan->default_dst.remote_ip,
3113 NUD_REACHABLE|NUD_PERMANENT,
3114 NLM_F_EXCL|NLM_F_CREATE,
3115 vxlan->cfg.dst_port,
3116 vxlan->default_dst.remote_vni,
3117 vxlan->default_dst.remote_ifindex,
3118 NTF_SELF);
3119 if (err)
3120 return err;
3121 }
3122
3123 err = register_netdevice(dev);
3124 if (err) {
3125 vxlan_fdb_delete_default(vxlan);
3126 return err;
3127 }
3128
3129 list_add(&vxlan->next, &vn->vxlan_list);
3130
3131 return 0;
3132}
3133
0dfbdf41
TG
3134static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3135 struct nlattr *tb[], struct nlattr *data[])
3136{
e05202e7 3137 struct vxlan_dev *vxlan = netdev_priv(dev);
0dfbdf41 3138 struct vxlan_config conf;
e05202e7 3139 int err;
0dfbdf41 3140
0dfbdf41 3141 memset(&conf, 0, sizeof(conf));
e277de5f
JG
3142
3143 if (data[IFLA_VXLAN_ID])
54bfd872 3144 conf.vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
0dfbdf41
TG
3145
3146 if (data[IFLA_VXLAN_GROUP]) {
3147 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3148 } else if (data[IFLA_VXLAN_GROUP6]) {
3149 if (!IS_ENABLED(CONFIG_IPV6))
3150 return -EPFNOSUPPORT;
3151
3152 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3153 conf.remote_ip.sa.sa_family = AF_INET6;
3154 }
3155
e05202e7
JV
3156 if (data[IFLA_VXLAN_FAN_MAP]) {
3157 err = vxlan_parse_fan_map(data, vxlan);
3158 if (err)
3159 return err;
3160 }
3161
0dfbdf41
TG
3162 if (data[IFLA_VXLAN_LOCAL]) {
3163 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3164 conf.saddr.sa.sa_family = AF_INET;
3165 } else if (data[IFLA_VXLAN_LOCAL6]) {
3166 if (!IS_ENABLED(CONFIG_IPV6))
3167 return -EPFNOSUPPORT;
3168
3169 /* TODO: respect scope id */
3170 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3171 conf.saddr.sa.sa_family = AF_INET6;
3172 }
3173
3174 if (data[IFLA_VXLAN_LINK])
3175 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3176
d342894c 3177 if (data[IFLA_VXLAN_TOS])
0dfbdf41 3178 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
d342894c 3179
afb97186 3180 if (data[IFLA_VXLAN_TTL])
0dfbdf41 3181 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
afb97186 3182
e7f70af1
DB
3183 if (data[IFLA_VXLAN_LABEL])
3184 conf.label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3185 IPV6_FLOWLABEL_MASK;
3186
d342894c 3187 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
0dfbdf41 3188 conf.flags |= VXLAN_F_LEARN;
d342894c 3189
3190 if (data[IFLA_VXLAN_AGEING])
0dfbdf41 3191 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
d342894c 3192
e4f67add 3193 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
0dfbdf41 3194 conf.flags |= VXLAN_F_PROXY;
e4f67add
DS
3195
3196 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
0dfbdf41 3197 conf.flags |= VXLAN_F_RSC;
e4f67add
DS
3198
3199 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
0dfbdf41 3200 conf.flags |= VXLAN_F_L2MISS;
e4f67add
DS
3201
3202 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
0dfbdf41 3203 conf.flags |= VXLAN_F_L3MISS;
e4f67add 3204
d342894c 3205 if (data[IFLA_VXLAN_LIMIT])
0dfbdf41 3206 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
d342894c 3207
f8a9b1bc
AS
3208 if (data[IFLA_VXLAN_COLLECT_METADATA] &&
3209 nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3210 conf.flags |= VXLAN_F_COLLECT_METADATA;
3211
05f47d69 3212 if (data[IFLA_VXLAN_PORT_RANGE]) {
3213 const struct ifla_vxlan_port_range *p
3214 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
0dfbdf41
TG
3215 conf.port_min = ntohs(p->low);
3216 conf.port_max = ntohs(p->high);
05f47d69 3217 }
3218
823aa873 3219 if (data[IFLA_VXLAN_PORT])
0dfbdf41 3220 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
823aa873 3221
6ceb31ca
AD
3222 if (data[IFLA_VXLAN_UDP_CSUM] &&
3223 !nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3224 conf.flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
359a0ea9
TH
3225
3226 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
3227 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
0dfbdf41 3228 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
359a0ea9
TH
3229
3230 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
3231 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
0dfbdf41 3232 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
359a0ea9 3233
dfd8645e
TH
3234 if (data[IFLA_VXLAN_REMCSUM_TX] &&
3235 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
0dfbdf41 3236 conf.flags |= VXLAN_F_REMCSUM_TX;
dfd8645e
TH
3237
3238 if (data[IFLA_VXLAN_REMCSUM_RX] &&
3239 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
0dfbdf41 3240 conf.flags |= VXLAN_F_REMCSUM_RX;
dfd8645e 3241
3511494c 3242 if (data[IFLA_VXLAN_GBP])
0dfbdf41 3243 conf.flags |= VXLAN_F_GBP;
3511494c 3244
e1e5314d
JB
3245 if (data[IFLA_VXLAN_GPE])
3246 conf.flags |= VXLAN_F_GPE;
3247
0ace2ca8 3248 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
0dfbdf41 3249 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
0ace2ca8 3250
ce577668
CH
3251 if (tb[IFLA_MTU])
3252 conf.mtu = nla_get_u32(tb[IFLA_MTU]);
3253
3555621d 3254 return vxlan_dev_configure(src_net, dev, &conf);
d342894c 3255}
3256
3257static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3258{
3259 struct vxlan_dev *vxlan = netdev_priv(dev);
f01ec1c0 3260 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
d342894c 3261
8b3f9337
RP
3262 vxlan_flush(vxlan, true);
3263
fe5c3561 3264 spin_lock(&vn->sock_lock);
9c2e24e1
PS
3265 if (!hlist_unhashed(&vxlan->hlist))
3266 hlist_del_rcu(&vxlan->hlist);
fe5c3561 3267 spin_unlock(&vn->sock_lock);
3268
58ce31cc 3269 gro_cells_destroy(&vxlan->gro_cells);
553675fb 3270 list_del(&vxlan->next);
d342894c 3271 unregister_netdevice_queue(dev, head);
3272}
3273
3274static size_t vxlan_get_size(const struct net_device *dev)
3275{
3276
3277 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
e4c7ed41 3278 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
d342894c 3279 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
e4c7ed41 3280 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
d342894c 3281 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3282 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
e7f70af1 3283 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
d342894c 3284 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
e4f67add
DS
3285 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3286 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3287 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3288 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
da8b43c0 3289 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
d342894c 3290 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3291 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
05f47d69 3292 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
359a0ea9
TH
3293 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3294 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3295 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3296 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
dfd8645e
TH
3297 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3298 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
e05202e7 3299 nla_total_size(sizeof(struct ip_fan_map) * 256) +
d342894c 3300 0;
3301}
3302
3303static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3304{
3305 const struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 3306 const struct vxlan_rdst *dst = &vxlan->default_dst;
05f47d69 3307 struct ifla_vxlan_port_range ports = {
0dfbdf41
TG
3308 .low = htons(vxlan->cfg.port_min),
3309 .high = htons(vxlan->cfg.port_max),
05f47d69 3310 };
d342894c 3311
54bfd872 3312 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
d342894c 3313 goto nla_put_failure;
3314
e4c7ed41
CW
3315 if (!vxlan_addr_any(&dst->remote_ip)) {
3316 if (dst->remote_ip.sa.sa_family == AF_INET) {
930345ea
JB
3317 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3318 dst->remote_ip.sin.sin_addr.s_addr))
e4c7ed41
CW
3319 goto nla_put_failure;
3320#if IS_ENABLED(CONFIG_IPV6)
3321 } else {
930345ea
JB
3322 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3323 &dst->remote_ip.sin6.sin6_addr))
e4c7ed41
CW
3324 goto nla_put_failure;
3325#endif
3326 }
3327 }
d342894c 3328
c7995c43 3329 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
d342894c 3330 goto nla_put_failure;
3331
0dfbdf41
TG
3332 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3333 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
930345ea 3334 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
0dfbdf41 3335 vxlan->cfg.saddr.sin.sin_addr.s_addr))
e4c7ed41
CW
3336 goto nla_put_failure;
3337#if IS_ENABLED(CONFIG_IPV6)
3338 } else {
930345ea 3339 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
0dfbdf41 3340 &vxlan->cfg.saddr.sin6.sin6_addr))
e4c7ed41
CW
3341 goto nla_put_failure;
3342#endif
3343 }
3344 }
d342894c 3345
e05202e7
JV
3346 if (fan_has_map(&vxlan->fan)) {
3347 struct nlattr *fan_nest;
3348 struct ip_fan_map *fan_map;
3349
3350 fan_nest = nla_nest_start(skb, IFLA_VXLAN_FAN_MAP);
3351 if (!fan_nest)
3352 goto nla_put_failure;
3353 list_for_each_entry_rcu(fan_map, &vxlan->fan.fan_maps, list) {
3354 struct ifla_fan_map map;
3355
3356 map.underlay = fan_map->underlay;
3357 map.underlay_prefix = fan_map->underlay_prefix;
3358 map.overlay = fan_map->overlay;
3359 map.overlay_prefix = fan_map->overlay_prefix;
3360 if (nla_put(skb, IFLA_FAN_MAPPING, sizeof(map), &map))
3361 goto nla_put_failure;
3362 }
3363 nla_nest_end(skb, fan_nest);
3364 }
3365
0dfbdf41
TG
3366 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3367 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
e7f70af1 3368 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
e4f67add
DS
3369 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3370 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3371 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3372 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3373 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3374 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3375 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3376 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3377 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
da8b43c0
AS
3378 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3379 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
0dfbdf41
TG
3380 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3381 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3382 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
359a0ea9 3383 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
6ceb31ca 3384 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
359a0ea9
TH
3385 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3386 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3387 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
dfd8645e
TH
3388 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3389 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3390 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3391 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3392 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
d342894c 3393 goto nla_put_failure;
3394
05f47d69 3395 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3396 goto nla_put_failure;
3397
3511494c
TG
3398 if (vxlan->flags & VXLAN_F_GBP &&
3399 nla_put_flag(skb, IFLA_VXLAN_GBP))
3400 goto nla_put_failure;
3401
e1e5314d
JB
3402 if (vxlan->flags & VXLAN_F_GPE &&
3403 nla_put_flag(skb, IFLA_VXLAN_GPE))
3404 goto nla_put_failure;
3405
0ace2ca8
TH
3406 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3407 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3408 goto nla_put_failure;
3409
d342894c 3410 return 0;
3411
3412nla_put_failure:
3413 return -EMSGSIZE;
3414}
3415
1728d4fa
ND
3416static struct net *vxlan_get_link_net(const struct net_device *dev)
3417{
3418 struct vxlan_dev *vxlan = netdev_priv(dev);
3419
3420 return vxlan->net;
3421}
3422
d342894c 3423static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3424 .kind = "vxlan",
3425 .maxtype = IFLA_VXLAN_MAX,
3426 .policy = vxlan_policy,
3427 .priv_size = sizeof(struct vxlan_dev),
3428 .setup = vxlan_setup,
3429 .validate = vxlan_validate,
3430 .newlink = vxlan_newlink,
3431 .dellink = vxlan_dellink,
3432 .get_size = vxlan_get_size,
3433 .fill_info = vxlan_fill_info,
1728d4fa 3434 .get_link_net = vxlan_get_link_net,
d342894c 3435};
3436
cf5da330
ND
3437struct net_device *vxlan_dev_create(struct net *net, const char *name,
3438 u8 name_assign_type,
3439 struct vxlan_config *conf)
3440{
3441 struct nlattr *tb[IFLA_MAX + 1];
3442 struct net_device *dev;
3443 int err;
3444
3445 memset(&tb, 0, sizeof(tb));
3446
3447 dev = rtnl_create_link(net, name, name_assign_type,
3448 &vxlan_link_ops, tb);
3449 if (IS_ERR(dev))
3450 return dev;
3451
3452 err = vxlan_dev_configure(net, dev, conf);
3453 if (err < 0) {
3454 free_netdev(dev);
3455 return ERR_PTR(err);
3456 }
3457
3458 err = rtnl_configure_link(dev, NULL);
3459 if (err < 0) {
3460 LIST_HEAD(list_kill);
3461
3462 vxlan_dellink(dev, &list_kill);
3463 unregister_netdevice_many(&list_kill);
3464 return ERR_PTR(err);
3465 }
3466
3467 return dev;
3468}
3469EXPORT_SYMBOL_GPL(vxlan_dev_create);
3470
acaf4e70
DB
3471static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3472 struct net_device *dev)
3473{
3474 struct vxlan_dev *vxlan, *next;
3475 LIST_HEAD(list_kill);
3476
3477 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3478 struct vxlan_rdst *dst = &vxlan->default_dst;
3479
3480 /* In case we created vxlan device with carrier
3481 * and we loose the carrier due to module unload
3482 * we also need to remove vxlan device. In other
3483 * cases, it's not necessary and remote_ifindex
3484 * is 0 here, so no matches.
3485 */
3486 if (dst->remote_ifindex == dev->ifindex)
3487 vxlan_dellink(vxlan->dev, &list_kill);
3488 }
3489
3490 unregister_netdevice_many(&list_kill);
3491}
3492
b7aade15
HFS
3493static int vxlan_netdevice_event(struct notifier_block *unused,
3494 unsigned long event, void *ptr)
acaf4e70
DB
3495{
3496 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
783c1463 3497 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
acaf4e70 3498
783c1463 3499 if (event == NETDEV_UNREGISTER)
acaf4e70 3500 vxlan_handle_lowerdev_unregister(vn, dev);
7c46a640 3501 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
b7aade15 3502 vxlan_push_rx_ports(dev);
acaf4e70
DB
3503
3504 return NOTIFY_DONE;
3505}
3506
3507static struct notifier_block vxlan_notifier_block __read_mostly = {
b7aade15 3508 .notifier_call = vxlan_netdevice_event,
acaf4e70
DB
3509};
3510
d342894c 3511static __net_init int vxlan_init_net(struct net *net)
3512{
3513 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
31fec5aa 3514 unsigned int h;
d342894c 3515
553675fb 3516 INIT_LIST_HEAD(&vn->vxlan_list);
1c51a915 3517 spin_lock_init(&vn->sock_lock);
d342894c 3518
553675fb 3519 for (h = 0; h < PORT_HASH_SIZE; ++h)
3520 INIT_HLIST_HEAD(&vn->sock_list[h]);
d342894c 3521
3522 return 0;
3523}
3524
e05202e7
JV
3525#ifdef CONFIG_SYSCTL
3526static struct ctl_table_header *vxlan_fan_header;
3527static unsigned int vxlan_fan_version = 4;
3528
3529static struct ctl_table vxlan_fan_sysctls[] = {
3530 {
3531 .procname = "vxlan",
3532 .data = &vxlan_fan_version,
3533 .maxlen = sizeof(vxlan_fan_version),
3534 .mode = 0444,
3535 .proc_handler = proc_dointvec,
3536 },
3537 {},
3538};
3539#endif /* CONFIG_SYSCTL */
3540
f01ec1c0
ND
3541static void __net_exit vxlan_exit_net(struct net *net)
3542{
3543 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3544 struct vxlan_dev *vxlan, *next;
3545 struct net_device *dev, *aux;
3546 LIST_HEAD(list);
3547
3548 rtnl_lock();
3549 for_each_netdev_safe(net, dev, aux)
3550 if (dev->rtnl_link_ops == &vxlan_link_ops)
3551 unregister_netdevice_queue(dev, &list);
3552
3553 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3554 /* If vxlan->dev is in the same netns, it has already been added
3555 * to the list by the previous loop.
3556 */
58ce31cc
TH
3557 if (!net_eq(dev_net(vxlan->dev), net)) {
3558 gro_cells_destroy(&vxlan->gro_cells);
13c3ed6a 3559 unregister_netdevice_queue(vxlan->dev, &list);
58ce31cc 3560 }
f01ec1c0
ND
3561 }
3562
3563 unregister_netdevice_many(&list);
3564 rtnl_unlock();
3565}
3566
d342894c 3567static struct pernet_operations vxlan_net_ops = {
3568 .init = vxlan_init_net,
f01ec1c0 3569 .exit = vxlan_exit_net,
d342894c 3570 .id = &vxlan_net_id,
3571 .size = sizeof(struct vxlan_net),
3572};
3573
3574static int __init vxlan_init_module(void)
3575{
3576 int rc;
3577
3578 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3579
783c1463 3580 rc = register_pernet_subsys(&vxlan_net_ops);
d342894c 3581 if (rc)
3582 goto out1;
3583
acaf4e70 3584 rc = register_netdevice_notifier(&vxlan_notifier_block);
d342894c 3585 if (rc)
3586 goto out2;
3587
acaf4e70
DB
3588 rc = rtnl_link_register(&vxlan_link_ops);
3589 if (rc)
3590 goto out3;
d342894c 3591
e05202e7
JV
3592#ifdef CONFIG_SYSCTL
3593 vxlan_fan_header = register_net_sysctl(&init_net, "net/fan",
3594 vxlan_fan_sysctls);
3595 if (!vxlan_fan_header) {
3596 rc = -ENOMEM;
3597 goto sysctl_failed;
3598 }
3599#endif /* CONFIG_SYSCTL */
3600
acaf4e70 3601 return 0;
e05202e7
JV
3602#ifdef CONFIG_SYSCTL
3603sysctl_failed:
3604 rtnl_link_unregister(&vxlan_link_ops);
3605#endif /* CONFIG_SYSCTL */
acaf4e70
DB
3606out3:
3607 unregister_netdevice_notifier(&vxlan_notifier_block);
d342894c 3608out2:
783c1463 3609 unregister_pernet_subsys(&vxlan_net_ops);
d342894c 3610out1:
3611 return rc;
3612}
7332a13b 3613late_initcall(vxlan_init_module);
d342894c 3614
3615static void __exit vxlan_cleanup_module(void)
3616{
e05202e7
JV
3617#ifdef CONFIG_SYSCTL
3618 unregister_net_sysctl_table(vxlan_fan_header);
3619#endif /* CONFIG_SYSCTL */
b7153984 3620 rtnl_link_unregister(&vxlan_link_ops);
acaf4e70 3621 unregister_netdevice_notifier(&vxlan_notifier_block);
783c1463
DB
3622 unregister_pernet_subsys(&vxlan_net_ops);
3623 /* rcu_barrier() is called by netns */
d342894c 3624}
3625module_exit(vxlan_cleanup_module);
3626
3627MODULE_LICENSE("GPL");
3628MODULE_VERSION(VXLAN_VERSION);
3b8df3c6 3629MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
ead5139a 3630MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
d342894c 3631MODULE_ALIAS_RTNL_LINK("vxlan");