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