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