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