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