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