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