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