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