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