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