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