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