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