]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/vxlan.c
UBUNTU: [Config] CONFIG_SND_SOC_MAX98927=m
[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 struct nd_msg *msg;
1798 const struct ipv6hdr *iphdr;
1799 const struct in6_addr *daddr;
1800 struct neighbour *n;
1801 struct inet6_dev *in6_dev;
1802
1803 in6_dev = __in6_dev_get(dev);
1804 if (!in6_dev)
1805 goto out;
1806
1807 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
1808 goto out;
1809
1810 iphdr = ipv6_hdr(skb);
1811 daddr = &iphdr->daddr;
1812
1813 msg = (struct nd_msg *)(iphdr + 1);
1814 if (msg->icmph.icmp6_code != 0 ||
1815 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1816 goto out;
1817
1818 if (ipv6_addr_loopback(daddr) ||
1819 ipv6_addr_is_multicast(&msg->target))
1820 goto out;
1821
1822 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1823
1824 if (n) {
1825 struct vxlan_fdb *f;
1826 struct sk_buff *reply;
1827
1828 if (!(n->nud_state & NUD_CONNECTED)) {
1829 neigh_release(n);
1830 goto out;
1831 }
1832
1833 f = vxlan_find_mac(vxlan, n->ha, vni);
1834 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1835 /* bridge-local neighbor */
1836 neigh_release(n);
1837 goto out;
1838 }
1839
1840 reply = vxlan_na_create(skb, n,
1841 !!(f ? f->flags & NTF_ROUTER : 0));
1842
1843 neigh_release(n);
1844
1845 if (reply == NULL)
1846 goto out;
1847
1848 if (netif_rx_ni(reply) == NET_RX_DROP)
1849 dev->stats.rx_dropped++;
1850
1851 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
1852 union vxlan_addr ipa = {
1853 .sin6.sin6_addr = msg->target,
1854 .sin6.sin6_family = AF_INET6,
1855 };
1856
1857 vxlan_ip_miss(dev, &ipa);
1858 }
1859
1860 out:
1861 consume_skb(skb);
1862 return NETDEV_TX_OK;
1863 }
1864 #endif
1865
1866 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1867 {
1868 struct vxlan_dev *vxlan = netdev_priv(dev);
1869 struct neighbour *n;
1870
1871 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1872 return false;
1873
1874 n = NULL;
1875 switch (ntohs(eth_hdr(skb)->h_proto)) {
1876 case ETH_P_IP:
1877 {
1878 struct iphdr *pip;
1879
1880 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1881 return false;
1882 pip = ip_hdr(skb);
1883 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1884 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
1885 union vxlan_addr ipa = {
1886 .sin.sin_addr.s_addr = pip->daddr,
1887 .sin.sin_family = AF_INET,
1888 };
1889
1890 vxlan_ip_miss(dev, &ipa);
1891 return false;
1892 }
1893
1894 break;
1895 }
1896 #if IS_ENABLED(CONFIG_IPV6)
1897 case ETH_P_IPV6:
1898 {
1899 struct ipv6hdr *pip6;
1900
1901 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1902 return false;
1903 pip6 = ipv6_hdr(skb);
1904 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1905 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
1906 union vxlan_addr ipa = {
1907 .sin6.sin6_addr = pip6->daddr,
1908 .sin6.sin6_family = AF_INET6,
1909 };
1910
1911 vxlan_ip_miss(dev, &ipa);
1912 return false;
1913 }
1914
1915 break;
1916 }
1917 #endif
1918 default:
1919 return false;
1920 }
1921
1922 if (n) {
1923 bool diff;
1924
1925 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1926 if (diff) {
1927 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1928 dev->addr_len);
1929 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1930 }
1931 neigh_release(n);
1932 return diff;
1933 }
1934
1935 return false;
1936 }
1937
1938 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1939 struct vxlan_metadata *md)
1940 {
1941 struct vxlanhdr_gbp *gbp;
1942
1943 if (!md->gbp)
1944 return;
1945
1946 gbp = (struct vxlanhdr_gbp *)vxh;
1947 vxh->vx_flags |= VXLAN_HF_GBP;
1948
1949 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1950 gbp->dont_learn = 1;
1951
1952 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1953 gbp->policy_applied = 1;
1954
1955 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1956 }
1957
1958 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1959 __be16 protocol)
1960 {
1961 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1962
1963 gpe->np_applied = 1;
1964
1965 switch (protocol) {
1966 case htons(ETH_P_IP):
1967 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1968 return 0;
1969 case htons(ETH_P_IPV6):
1970 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1971 return 0;
1972 case htons(ETH_P_TEB):
1973 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1974 return 0;
1975 }
1976 return -EPFNOSUPPORT;
1977 }
1978
1979 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1980 int iphdr_len, __be32 vni,
1981 struct vxlan_metadata *md, u32 vxflags,
1982 bool udp_sum)
1983 {
1984 struct vxlanhdr *vxh;
1985 int min_headroom;
1986 int err;
1987 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1988 __be16 inner_protocol = htons(ETH_P_TEB);
1989
1990 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1991 skb->ip_summed == CHECKSUM_PARTIAL) {
1992 int csum_start = skb_checksum_start_offset(skb);
1993
1994 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1995 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1996 (skb->csum_offset == offsetof(struct udphdr, check) ||
1997 skb->csum_offset == offsetof(struct tcphdr, check)))
1998 type |= SKB_GSO_TUNNEL_REMCSUM;
1999 }
2000
2001 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
2002 + VXLAN_HLEN + iphdr_len;
2003
2004 /* Need space for new headers (invalidates iph ptr) */
2005 err = skb_cow_head(skb, min_headroom);
2006 if (unlikely(err))
2007 return err;
2008
2009 err = iptunnel_handle_offloads(skb, type);
2010 if (err)
2011 return err;
2012
2013 vxh = __skb_push(skb, sizeof(*vxh));
2014 vxh->vx_flags = VXLAN_HF_VNI;
2015 vxh->vx_vni = vxlan_vni_field(vni);
2016
2017 if (type & SKB_GSO_TUNNEL_REMCSUM) {
2018 unsigned int start;
2019
2020 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2021 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2022 vxh->vx_flags |= VXLAN_HF_RCO;
2023
2024 if (!skb_is_gso(skb)) {
2025 skb->ip_summed = CHECKSUM_NONE;
2026 skb->encapsulation = 0;
2027 }
2028 }
2029
2030 if (vxflags & VXLAN_F_GBP)
2031 vxlan_build_gbp_hdr(vxh, vxflags, md);
2032 if (vxflags & VXLAN_F_GPE) {
2033 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2034 if (err < 0)
2035 return err;
2036 inner_protocol = skb->protocol;
2037 }
2038
2039 skb_set_inner_protocol(skb, inner_protocol);
2040 return 0;
2041 }
2042
2043 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2044 struct vxlan_sock *sock4,
2045 struct sk_buff *skb, int oif, u8 tos,
2046 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
2047 struct dst_cache *dst_cache,
2048 const struct ip_tunnel_info *info)
2049 {
2050 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2051 struct rtable *rt = NULL;
2052 struct flowi4 fl4;
2053
2054 if (!sock4)
2055 return ERR_PTR(-EIO);
2056
2057 if (tos && !info)
2058 use_cache = false;
2059 if (use_cache) {
2060 rt = dst_cache_get_ip4(dst_cache, saddr);
2061 if (rt)
2062 return rt;
2063 }
2064
2065 memset(&fl4, 0, sizeof(fl4));
2066 fl4.flowi4_oif = oif;
2067 fl4.flowi4_tos = RT_TOS(tos);
2068 fl4.flowi4_mark = skb->mark;
2069 fl4.flowi4_proto = IPPROTO_UDP;
2070 fl4.daddr = daddr;
2071 fl4.saddr = *saddr;
2072 fl4.fl4_dport = dport;
2073 fl4.fl4_sport = sport;
2074
2075 rt = ip_route_output_key(vxlan->net, &fl4);
2076 if (likely(!IS_ERR(rt))) {
2077 if (rt->dst.dev == dev) {
2078 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2079 ip_rt_put(rt);
2080 return ERR_PTR(-ELOOP);
2081 }
2082
2083 *saddr = fl4.saddr;
2084 if (use_cache)
2085 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2086 } else {
2087 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2088 return ERR_PTR(-ENETUNREACH);
2089 }
2090 return rt;
2091 }
2092
2093 #if IS_ENABLED(CONFIG_IPV6)
2094 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
2095 struct net_device *dev,
2096 struct vxlan_sock *sock6,
2097 struct sk_buff *skb, int oif, u8 tos,
2098 __be32 label,
2099 const struct in6_addr *daddr,
2100 struct in6_addr *saddr,
2101 __be16 dport, __be16 sport,
2102 struct dst_cache *dst_cache,
2103 const struct ip_tunnel_info *info)
2104 {
2105 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2106 struct dst_entry *ndst;
2107 struct flowi6 fl6;
2108 int err;
2109
2110 if (!sock6)
2111 return ERR_PTR(-EIO);
2112
2113 if (tos && !info)
2114 use_cache = false;
2115 if (use_cache) {
2116 ndst = dst_cache_get_ip6(dst_cache, saddr);
2117 if (ndst)
2118 return ndst;
2119 }
2120
2121 memset(&fl6, 0, sizeof(fl6));
2122 fl6.flowi6_oif = oif;
2123 fl6.daddr = *daddr;
2124 fl6.saddr = *saddr;
2125 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
2126 fl6.flowi6_mark = skb->mark;
2127 fl6.flowi6_proto = IPPROTO_UDP;
2128 fl6.fl6_dport = dport;
2129 fl6.fl6_sport = sport;
2130
2131 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
2132 sock6->sock->sk,
2133 &ndst, &fl6);
2134 if (unlikely(err < 0)) {
2135 netdev_dbg(dev, "no route to %pI6\n", daddr);
2136 return ERR_PTR(-ENETUNREACH);
2137 }
2138
2139 if (unlikely(ndst->dev == dev)) {
2140 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2141 dst_release(ndst);
2142 return ERR_PTR(-ELOOP);
2143 }
2144
2145 *saddr = fl6.saddr;
2146 if (use_cache)
2147 dst_cache_set_ip6(dst_cache, ndst, saddr);
2148 return ndst;
2149 }
2150 #endif
2151
2152 /* Bypass encapsulation if the destination is local */
2153 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2154 struct vxlan_dev *dst_vxlan, __be32 vni)
2155 {
2156 struct pcpu_sw_netstats *tx_stats, *rx_stats;
2157 union vxlan_addr loopback;
2158 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
2159 struct net_device *dev = skb->dev;
2160 int len = skb->len;
2161
2162 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2163 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
2164 skb->pkt_type = PACKET_HOST;
2165 skb->encapsulation = 0;
2166 skb->dev = dst_vxlan->dev;
2167 __skb_pull(skb, skb_network_offset(skb));
2168
2169 if (remote_ip->sa.sa_family == AF_INET) {
2170 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2171 loopback.sa.sa_family = AF_INET;
2172 #if IS_ENABLED(CONFIG_IPV6)
2173 } else {
2174 loopback.sin6.sin6_addr = in6addr_loopback;
2175 loopback.sa.sa_family = AF_INET6;
2176 #endif
2177 }
2178
2179 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
2180 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0,
2181 vni);
2182
2183 u64_stats_update_begin(&tx_stats->syncp);
2184 tx_stats->tx_packets++;
2185 tx_stats->tx_bytes += len;
2186 u64_stats_update_end(&tx_stats->syncp);
2187
2188 if (netif_rx(skb) == NET_RX_SUCCESS) {
2189 u64_stats_update_begin(&rx_stats->syncp);
2190 rx_stats->rx_packets++;
2191 rx_stats->rx_bytes += len;
2192 u64_stats_update_end(&rx_stats->syncp);
2193 } else {
2194 dev->stats.rx_dropped++;
2195 }
2196 }
2197
2198 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2199 struct vxlan_dev *vxlan,
2200 union vxlan_addr *daddr,
2201 __be16 dst_port, int dst_ifindex, __be32 vni,
2202 struct dst_entry *dst,
2203 u32 rt_flags)
2204 {
2205 #if IS_ENABLED(CONFIG_IPV6)
2206 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2207 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2208 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2209 */
2210 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2211 #endif
2212 /* Bypass encapsulation if the destination is local */
2213 if (rt_flags & RTCF_LOCAL &&
2214 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2215 struct vxlan_dev *dst_vxlan;
2216
2217 dst_release(dst);
2218 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2219 daddr->sa.sa_family, dst_port,
2220 vxlan->cfg.flags);
2221 if (!dst_vxlan) {
2222 dev->stats.tx_errors++;
2223 kfree_skb(skb);
2224
2225 return -ENOENT;
2226 }
2227 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
2228 return 1;
2229 }
2230
2231 return 0;
2232 }
2233
2234 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2235 __be32 default_vni, struct vxlan_rdst *rdst,
2236 bool did_rsc)
2237 {
2238 struct dst_cache *dst_cache;
2239 struct ip_tunnel_info *info;
2240 struct vxlan_dev *vxlan = netdev_priv(dev);
2241 const struct iphdr *old_iph = ip_hdr(skb);
2242 union vxlan_addr *dst;
2243 union vxlan_addr remote_ip, local_ip;
2244 struct vxlan_metadata _md;
2245 struct vxlan_metadata *md = &_md;
2246 __be16 src_port = 0, dst_port;
2247 struct dst_entry *ndst = NULL;
2248 __be32 vni, label;
2249 __u8 tos, ttl;
2250 int ifindex;
2251 int err;
2252 u32 flags = vxlan->cfg.flags;
2253 bool udp_sum = false;
2254 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2255
2256 info = skb_tunnel_info(skb);
2257
2258 if (rdst) {
2259 dst = &rdst->remote_ip;
2260 if (vxlan_addr_any(dst)) {
2261 if (did_rsc) {
2262 /* short-circuited back to local bridge */
2263 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
2264 return;
2265 }
2266 goto drop;
2267 }
2268
2269 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2270 vni = (rdst->remote_vni) ? : default_vni;
2271 ifindex = rdst->remote_ifindex;
2272 local_ip = vxlan->cfg.saddr;
2273 dst_cache = &rdst->dst_cache;
2274 md->gbp = skb->mark;
2275 ttl = vxlan->cfg.ttl;
2276 if (!ttl && vxlan_addr_multicast(dst))
2277 ttl = 1;
2278
2279 tos = vxlan->cfg.tos;
2280 if (tos == 1)
2281 tos = ip_tunnel_get_dsfield(old_iph, skb);
2282
2283 if (dst->sa.sa_family == AF_INET)
2284 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2285 else
2286 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2287 label = vxlan->cfg.label;
2288 } else {
2289 if (!info) {
2290 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2291 dev->name);
2292 goto drop;
2293 }
2294 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2295 if (remote_ip.sa.sa_family == AF_INET) {
2296 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2297 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2298 } else {
2299 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2300 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2301 }
2302 dst = &remote_ip;
2303 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2304 vni = tunnel_id_to_key32(info->key.tun_id);
2305 ifindex = 0;
2306 dst_cache = &info->dst_cache;
2307 if (info->options_len)
2308 md = ip_tunnel_info_opts(info);
2309 ttl = info->key.ttl;
2310 tos = info->key.tos;
2311 label = info->key.label;
2312 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2313 }
2314 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2315 vxlan->cfg.port_max, true);
2316
2317 rcu_read_lock();
2318 if (dst->sa.sa_family == AF_INET) {
2319 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2320 struct rtable *rt;
2321 __be16 df = 0;
2322
2323 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
2324 dst->sin.sin_addr.s_addr,
2325 &local_ip.sin.sin_addr.s_addr,
2326 dst_port, src_port,
2327 dst_cache, info);
2328 if (IS_ERR(rt)) {
2329 err = PTR_ERR(rt);
2330 goto tx_error;
2331 }
2332
2333 if (fan_has_map(&vxlan->fan) && rt->rt_flags & RTCF_LOCAL) {
2334 netdev_dbg(dev, "discard fan to localhost %pI4\n",
2335 &dst->sin.sin_addr.s_addr);
2336 ip_rt_put(rt);
2337 goto tx_free;
2338 }
2339
2340 /* Bypass encapsulation if the destination is local */
2341 if (!info) {
2342 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2343 dst_port, ifindex, vni,
2344 &rt->dst, rt->rt_flags);
2345 if (err)
2346 goto out_unlock;
2347 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2348 df = htons(IP_DF);
2349 }
2350
2351 ndst = &rt->dst;
2352 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2353 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2354 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2355 vni, md, flags, udp_sum);
2356 if (err < 0)
2357 goto tx_error;
2358
2359 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2360 dst->sin.sin_addr.s_addr, tos, ttl, df,
2361 src_port, dst_port, xnet, !udp_sum);
2362 #if IS_ENABLED(CONFIG_IPV6)
2363 } else {
2364 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2365
2366 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
2367 label, &dst->sin6.sin6_addr,
2368 &local_ip.sin6.sin6_addr,
2369 dst_port, src_port,
2370 dst_cache, info);
2371 if (IS_ERR(ndst)) {
2372 err = PTR_ERR(ndst);
2373 ndst = NULL;
2374 goto tx_error;
2375 }
2376
2377 if (!info) {
2378 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2379
2380 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2381 dst_port, ifindex, vni,
2382 ndst, rt6i_flags);
2383 if (err)
2384 goto out_unlock;
2385 }
2386
2387 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2388 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2389 skb_scrub_packet(skb, xnet);
2390 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2391 vni, md, flags, udp_sum);
2392 if (err < 0)
2393 goto tx_error;
2394
2395 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2396 &local_ip.sin6.sin6_addr,
2397 &dst->sin6.sin6_addr, tos, ttl,
2398 label, src_port, dst_port, !udp_sum);
2399 #endif
2400 }
2401 out_unlock:
2402 rcu_read_unlock();
2403 return;
2404
2405 drop:
2406 dev->stats.tx_dropped++;
2407 dev_kfree_skb(skb);
2408 return;
2409
2410 tx_error:
2411 rcu_read_unlock();
2412 if (err == -ELOOP)
2413 dev->stats.collisions++;
2414 else if (err == -ENETUNREACH)
2415 dev->stats.tx_carrier_errors++;
2416 dst_release(ndst);
2417 dev->stats.tx_errors++;
2418 tx_free:
2419 kfree_skb(skb);
2420 }
2421
2422 /* Transmit local packets over Vxlan
2423 *
2424 * Outer IP header inherits ECN and DF from inner header.
2425 * Outer UDP destination is the VXLAN assigned port.
2426 * source port is based on hash of flow
2427 */
2428 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2429 {
2430 struct vxlan_dev *vxlan = netdev_priv(dev);
2431 const struct ip_tunnel_info *info;
2432 struct ethhdr *eth;
2433 bool did_rsc = false;
2434 struct vxlan_rdst *rdst, *fdst = NULL;
2435 struct vxlan_fdb *f;
2436 __be32 vni = 0;
2437
2438 info = skb_tunnel_info(skb);
2439
2440 skb_reset_mac_header(skb);
2441
2442 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
2443 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2444 info->mode & IP_TUNNEL_INFO_TX) {
2445 vni = tunnel_id_to_key32(info->key.tun_id);
2446 } else {
2447 if (info && info->mode & IP_TUNNEL_INFO_TX)
2448 vxlan_xmit_one(skb, dev, vni, NULL, false);
2449 else
2450 kfree_skb(skb);
2451 return NETDEV_TX_OK;
2452 }
2453 }
2454
2455 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
2456 eth = eth_hdr(skb);
2457 if (ntohs(eth->h_proto) == ETH_P_ARP)
2458 return arp_reduce(dev, skb, vni);
2459 #if IS_ENABLED(CONFIG_IPV6)
2460 else if (ntohs(eth->h_proto) == ETH_P_IPV6) {
2461 struct ipv6hdr *hdr, _hdr;
2462 if ((hdr = skb_header_pointer(skb,
2463 skb_network_offset(skb),
2464 sizeof(_hdr), &_hdr)) &&
2465 hdr->nexthdr == IPPROTO_ICMPV6)
2466 return neigh_reduce(dev, skb, vni);
2467 }
2468 #endif
2469 }
2470
2471 if (fan_has_map(&vxlan->fan)) {
2472 struct vxlan_rdst fan_rdst;
2473
2474 netdev_dbg(vxlan->dev, "vxlan_xmit p %x d %pM\n",
2475 eth->h_proto, eth->h_dest);
2476 if (vxlan_fan_build_rdst(vxlan, skb, &fan_rdst)) {
2477 dev->stats.tx_dropped++;
2478 kfree_skb(skb);
2479 return NETDEV_TX_OK;
2480 }
2481 vxlan_xmit_one(skb, dev, vni, &fan_rdst, 0);
2482 return NETDEV_TX_OK;
2483 }
2484
2485 eth = eth_hdr(skb);
2486 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2487 did_rsc = false;
2488
2489 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
2490 (ntohs(eth->h_proto) == ETH_P_IP ||
2491 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2492 did_rsc = route_shortcircuit(dev, skb);
2493 if (did_rsc)
2494 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2495 }
2496
2497 if (f == NULL) {
2498 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2499 if (f == NULL) {
2500 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
2501 !is_multicast_ether_addr(eth->h_dest))
2502 vxlan_fdb_miss(vxlan, eth->h_dest);
2503
2504 dev->stats.tx_dropped++;
2505 kfree_skb(skb);
2506 return NETDEV_TX_OK;
2507 }
2508 }
2509
2510 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2511 struct sk_buff *skb1;
2512
2513 if (!fdst) {
2514 fdst = rdst;
2515 continue;
2516 }
2517 skb1 = skb_clone(skb, GFP_ATOMIC);
2518 if (skb1)
2519 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2520 }
2521
2522 if (fdst)
2523 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2524 else
2525 kfree_skb(skb);
2526 return NETDEV_TX_OK;
2527 }
2528
2529 /* Walk the forwarding table and purge stale entries */
2530 static void vxlan_cleanup(unsigned long arg)
2531 {
2532 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2533 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2534 unsigned int h;
2535
2536 if (!netif_running(vxlan->dev))
2537 return;
2538
2539 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2540 struct hlist_node *p, *n;
2541
2542 spin_lock_bh(&vxlan->hash_lock);
2543 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2544 struct vxlan_fdb *f
2545 = container_of(p, struct vxlan_fdb, hlist);
2546 unsigned long timeout;
2547
2548 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2549 continue;
2550
2551 if (f->flags & NTF_EXT_LEARNED)
2552 continue;
2553
2554 timeout = f->used + vxlan->cfg.age_interval * HZ;
2555 if (time_before_eq(timeout, jiffies)) {
2556 netdev_dbg(vxlan->dev,
2557 "garbage collect %pM\n",
2558 f->eth_addr);
2559 f->state = NUD_STALE;
2560 vxlan_fdb_destroy(vxlan, f);
2561 } else if (time_before(timeout, next_timer))
2562 next_timer = timeout;
2563 }
2564 spin_unlock_bh(&vxlan->hash_lock);
2565 }
2566
2567 mod_timer(&vxlan->age_timer, next_timer);
2568 }
2569
2570 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2571 {
2572 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2573
2574 spin_lock(&vn->sock_lock);
2575 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2576 #if IS_ENABLED(CONFIG_IPV6)
2577 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2578 #endif
2579 spin_unlock(&vn->sock_lock);
2580 }
2581
2582 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2583 struct vxlan_dev_node *node)
2584 {
2585 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2586 __be32 vni = vxlan->default_dst.remote_vni;
2587
2588 node->vxlan = vxlan;
2589 spin_lock(&vn->sock_lock);
2590 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
2591 spin_unlock(&vn->sock_lock);
2592 }
2593
2594 /* Setup stats when device is created */
2595 static int vxlan_init(struct net_device *dev)
2596 {
2597 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2598 if (!dev->tstats)
2599 return -ENOMEM;
2600
2601 return 0;
2602 }
2603
2604 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
2605 {
2606 struct vxlan_fdb *f;
2607
2608 spin_lock_bh(&vxlan->hash_lock);
2609 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
2610 if (f)
2611 vxlan_fdb_destroy(vxlan, f);
2612 spin_unlock_bh(&vxlan->hash_lock);
2613 }
2614
2615 static void vxlan_uninit(struct net_device *dev)
2616 {
2617 struct vxlan_dev *vxlan = netdev_priv(dev);
2618
2619 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
2620
2621 free_percpu(dev->tstats);
2622 }
2623
2624 /* Start ageing timer and join group when device is brought up */
2625 static int vxlan_open(struct net_device *dev)
2626 {
2627 struct vxlan_dev *vxlan = netdev_priv(dev);
2628 int ret;
2629
2630 ret = vxlan_sock_add(vxlan);
2631 if (ret < 0)
2632 return ret;
2633
2634 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2635 ret = vxlan_igmp_join(vxlan);
2636 if (ret == -EADDRINUSE)
2637 ret = 0;
2638 if (ret) {
2639 vxlan_sock_release(vxlan);
2640 return ret;
2641 }
2642 }
2643
2644 if (vxlan->cfg.age_interval)
2645 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2646
2647 return ret;
2648 }
2649
2650 /* Purge the forwarding table */
2651 static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
2652 {
2653 unsigned int h;
2654
2655 spin_lock_bh(&vxlan->hash_lock);
2656 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2657 struct hlist_node *p, *n;
2658 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2659 struct vxlan_fdb *f
2660 = container_of(p, struct vxlan_fdb, hlist);
2661 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2662 continue;
2663 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2664 if (!is_zero_ether_addr(f->eth_addr))
2665 vxlan_fdb_destroy(vxlan, f);
2666 }
2667 }
2668 spin_unlock_bh(&vxlan->hash_lock);
2669 }
2670
2671 /* Cleanup timer and forwarding table on shutdown */
2672 static int vxlan_stop(struct net_device *dev)
2673 {
2674 struct vxlan_dev *vxlan = netdev_priv(dev);
2675 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2676 int ret = 0;
2677
2678 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2679 !vxlan_group_used(vn, vxlan))
2680 ret = vxlan_igmp_leave(vxlan);
2681
2682 del_timer_sync(&vxlan->age_timer);
2683
2684 vxlan_flush(vxlan, false);
2685 vxlan_sock_release(vxlan);
2686
2687 return ret;
2688 }
2689
2690 /* Stub, nothing needs to be done. */
2691 static void vxlan_set_multicast_list(struct net_device *dev)
2692 {
2693 }
2694
2695 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2696 {
2697 struct vxlan_dev *vxlan = netdev_priv(dev);
2698 struct vxlan_rdst *dst = &vxlan->default_dst;
2699 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2700 dst->remote_ifindex);
2701 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
2702
2703 /* This check is different than dev->max_mtu, because it looks at
2704 * the lowerdev->mtu, rather than the static dev->max_mtu
2705 */
2706 if (lowerdev) {
2707 int max_mtu = lowerdev->mtu -
2708 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2709 if (new_mtu > max_mtu)
2710 return -EINVAL;
2711 }
2712
2713 dev->mtu = new_mtu;
2714 return 0;
2715 }
2716
2717 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2718 {
2719 struct vxlan_dev *vxlan = netdev_priv(dev);
2720 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2721 __be16 sport, dport;
2722
2723 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2724 vxlan->cfg.port_max, true);
2725 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2726
2727 if (ip_tunnel_info_af(info) == AF_INET) {
2728 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2729 struct rtable *rt;
2730
2731 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
2732 info->key.u.ipv4.dst,
2733 &info->key.u.ipv4.src, dport, sport,
2734 &info->dst_cache, info);
2735 if (IS_ERR(rt))
2736 return PTR_ERR(rt);
2737 ip_rt_put(rt);
2738 } else {
2739 #if IS_ENABLED(CONFIG_IPV6)
2740 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2741 struct dst_entry *ndst;
2742
2743 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
2744 info->key.label, &info->key.u.ipv6.dst,
2745 &info->key.u.ipv6.src, dport, sport,
2746 &info->dst_cache, info);
2747 if (IS_ERR(ndst))
2748 return PTR_ERR(ndst);
2749 dst_release(ndst);
2750 #else /* !CONFIG_IPV6 */
2751 return -EPFNOSUPPORT;
2752 #endif
2753 }
2754 info->key.tp_src = sport;
2755 info->key.tp_dst = dport;
2756 return 0;
2757 }
2758
2759 static const struct net_device_ops vxlan_netdev_ether_ops = {
2760 .ndo_init = vxlan_init,
2761 .ndo_uninit = vxlan_uninit,
2762 .ndo_open = vxlan_open,
2763 .ndo_stop = vxlan_stop,
2764 .ndo_start_xmit = vxlan_xmit,
2765 .ndo_get_stats64 = ip_tunnel_get_stats64,
2766 .ndo_set_rx_mode = vxlan_set_multicast_list,
2767 .ndo_change_mtu = vxlan_change_mtu,
2768 .ndo_validate_addr = eth_validate_addr,
2769 .ndo_set_mac_address = eth_mac_addr,
2770 .ndo_fdb_add = vxlan_fdb_add,
2771 .ndo_fdb_del = vxlan_fdb_delete,
2772 .ndo_fdb_dump = vxlan_fdb_dump,
2773 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2774 };
2775
2776 static const struct net_device_ops vxlan_netdev_raw_ops = {
2777 .ndo_init = vxlan_init,
2778 .ndo_uninit = vxlan_uninit,
2779 .ndo_open = vxlan_open,
2780 .ndo_stop = vxlan_stop,
2781 .ndo_start_xmit = vxlan_xmit,
2782 .ndo_get_stats64 = ip_tunnel_get_stats64,
2783 .ndo_change_mtu = vxlan_change_mtu,
2784 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2785 };
2786
2787 /* Info for udev, that this is a virtual tunnel endpoint */
2788 static struct device_type vxlan_type = {
2789 .name = "vxlan",
2790 };
2791
2792 /* Calls the ndo_udp_tunnel_add of the caller in order to
2793 * supply the listening VXLAN udp ports. Callers are expected
2794 * to implement the ndo_udp_tunnel_add.
2795 */
2796 static void vxlan_push_rx_ports(struct net_device *dev)
2797 {
2798 struct vxlan_sock *vs;
2799 struct net *net = dev_net(dev);
2800 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2801 unsigned int i;
2802
2803 spin_lock(&vn->sock_lock);
2804 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2805 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2806 udp_tunnel_push_rx_port(dev, vs->sock,
2807 (vs->flags & VXLAN_F_GPE) ?
2808 UDP_TUNNEL_TYPE_VXLAN_GPE :
2809 UDP_TUNNEL_TYPE_VXLAN);
2810 }
2811 spin_unlock(&vn->sock_lock);
2812 }
2813
2814 /* Initialize the device structure. */
2815 static void vxlan_setup(struct net_device *dev)
2816 {
2817 struct vxlan_dev *vxlan = netdev_priv(dev);
2818 unsigned int h;
2819
2820 eth_hw_addr_random(dev);
2821 ether_setup(dev);
2822
2823 dev->needs_free_netdev = true;
2824 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2825
2826 dev->features |= NETIF_F_LLTX;
2827 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2828 dev->features |= NETIF_F_RXCSUM;
2829 dev->features |= NETIF_F_GSO_SOFTWARE;
2830
2831 dev->vlan_features = dev->features;
2832 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2833 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2834 netif_keep_dst(dev);
2835 dev->priv_flags |= IFF_NO_QUEUE;
2836
2837 /* MTU range: 68 - 65535 */
2838 dev->min_mtu = ETH_MIN_MTU;
2839 dev->max_mtu = ETH_MAX_MTU;
2840
2841 INIT_LIST_HEAD(&vxlan->next);
2842 spin_lock_init(&vxlan->hash_lock);
2843
2844 init_timer_deferrable(&vxlan->age_timer);
2845 vxlan->age_timer.function = vxlan_cleanup;
2846 vxlan->age_timer.data = (unsigned long) vxlan;
2847
2848 vxlan->dev = dev;
2849
2850 gro_cells_init(&vxlan->gro_cells, dev);
2851
2852 for (h = 0; h < FDB_HASH_SIZE; ++h)
2853 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2854
2855 INIT_LIST_HEAD(&vxlan->fan.fan_maps);
2856 }
2857
2858 static void vxlan_ether_setup(struct net_device *dev)
2859 {
2860 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2861 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2862 dev->netdev_ops = &vxlan_netdev_ether_ops;
2863 }
2864
2865 static void vxlan_raw_setup(struct net_device *dev)
2866 {
2867 dev->header_ops = NULL;
2868 dev->type = ARPHRD_NONE;
2869 dev->hard_header_len = 0;
2870 dev->addr_len = 0;
2871 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2872 dev->netdev_ops = &vxlan_netdev_raw_ops;
2873 }
2874
2875 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2876 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2877 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2878 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2879 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2880 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2881 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2882 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2883 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2884 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
2885 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2886 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2887 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2888 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2889 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2890 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2891 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2892 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2893 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
2894 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2895 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2896 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2897 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
2898 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2899 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2900 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
2901 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
2902 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
2903 };
2904
2905 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
2906 struct netlink_ext_ack *extack)
2907 {
2908 if (tb[IFLA_ADDRESS]) {
2909 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2910 pr_debug("invalid link address (not ethernet)\n");
2911 return -EINVAL;
2912 }
2913
2914 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2915 pr_debug("invalid all zero ethernet address\n");
2916 return -EADDRNOTAVAIL;
2917 }
2918 }
2919
2920 if (tb[IFLA_MTU]) {
2921 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
2922
2923 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU)
2924 return -EINVAL;
2925 }
2926
2927 if (!data)
2928 return -EINVAL;
2929
2930 if (data[IFLA_VXLAN_ID]) {
2931 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2932
2933 if (id >= VXLAN_N_VID)
2934 return -ERANGE;
2935 }
2936
2937 if (data[IFLA_VXLAN_PORT_RANGE]) {
2938 const struct ifla_vxlan_port_range *p
2939 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2940
2941 if (ntohs(p->high) < ntohs(p->low)) {
2942 pr_debug("port range %u .. %u not valid\n",
2943 ntohs(p->low), ntohs(p->high));
2944 return -EINVAL;
2945 }
2946 }
2947
2948 return 0;
2949 }
2950
2951 static void vxlan_get_drvinfo(struct net_device *netdev,
2952 struct ethtool_drvinfo *drvinfo)
2953 {
2954 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2955 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2956 }
2957
2958 static const struct ethtool_ops vxlan_ethtool_ops = {
2959 .get_drvinfo = vxlan_get_drvinfo,
2960 .get_link = ethtool_op_get_link,
2961 };
2962
2963 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2964 __be16 port, u32 flags)
2965 {
2966 struct socket *sock;
2967 struct udp_port_cfg udp_conf;
2968 int err;
2969
2970 memset(&udp_conf, 0, sizeof(udp_conf));
2971
2972 if (ipv6) {
2973 udp_conf.family = AF_INET6;
2974 udp_conf.use_udp6_rx_checksums =
2975 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2976 udp_conf.ipv6_v6only = 1;
2977 } else {
2978 udp_conf.family = AF_INET;
2979 }
2980
2981 udp_conf.local_udp_port = port;
2982
2983 /* Open UDP socket */
2984 err = udp_sock_create(net, &udp_conf, &sock);
2985 if (err < 0)
2986 return ERR_PTR(err);
2987
2988 return sock;
2989 }
2990
2991 /* Create new listen socket if needed */
2992 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2993 __be16 port, u32 flags)
2994 {
2995 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2996 struct vxlan_sock *vs;
2997 struct socket *sock;
2998 unsigned int h;
2999 struct udp_tunnel_sock_cfg tunnel_cfg;
3000
3001 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
3002 if (!vs)
3003 return ERR_PTR(-ENOMEM);
3004
3005 for (h = 0; h < VNI_HASH_SIZE; ++h)
3006 INIT_HLIST_HEAD(&vs->vni_list[h]);
3007
3008 sock = vxlan_create_sock(net, ipv6, port, flags);
3009 if (IS_ERR(sock)) {
3010 kfree(vs);
3011 return ERR_CAST(sock);
3012 }
3013
3014 vs->sock = sock;
3015 refcount_set(&vs->refcnt, 1);
3016 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
3017
3018 spin_lock(&vn->sock_lock);
3019 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
3020 udp_tunnel_notify_add_rx_port(sock,
3021 (vs->flags & VXLAN_F_GPE) ?
3022 UDP_TUNNEL_TYPE_VXLAN_GPE :
3023 UDP_TUNNEL_TYPE_VXLAN);
3024 spin_unlock(&vn->sock_lock);
3025
3026 /* Mark socket as an encapsulation socket. */
3027 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
3028 tunnel_cfg.sk_user_data = vs;
3029 tunnel_cfg.encap_type = 1;
3030 tunnel_cfg.encap_rcv = vxlan_rcv;
3031 tunnel_cfg.encap_destroy = NULL;
3032 tunnel_cfg.gro_receive = vxlan_gro_receive;
3033 tunnel_cfg.gro_complete = vxlan_gro_complete;
3034
3035 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
3036
3037 return vs;
3038 }
3039
3040 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
3041 {
3042 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3043 struct vxlan_sock *vs = NULL;
3044 struct vxlan_dev_node *node;
3045
3046 if (!vxlan->cfg.no_share) {
3047 spin_lock(&vn->sock_lock);
3048 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
3049 vxlan->cfg.dst_port, vxlan->cfg.flags);
3050 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
3051 spin_unlock(&vn->sock_lock);
3052 return -EBUSY;
3053 }
3054 spin_unlock(&vn->sock_lock);
3055 }
3056 if (!vs)
3057 vs = vxlan_socket_create(vxlan->net, ipv6,
3058 vxlan->cfg.dst_port, vxlan->cfg.flags);
3059 if (IS_ERR(vs))
3060 return PTR_ERR(vs);
3061 #if IS_ENABLED(CONFIG_IPV6)
3062 if (ipv6) {
3063 rcu_assign_pointer(vxlan->vn6_sock, vs);
3064 node = &vxlan->hlist6;
3065 } else
3066 #endif
3067 {
3068 rcu_assign_pointer(vxlan->vn4_sock, vs);
3069 node = &vxlan->hlist4;
3070 }
3071 vxlan_vs_add_dev(vs, vxlan, node);
3072 return 0;
3073 }
3074
3075 static int vxlan_sock_add(struct vxlan_dev *vxlan)
3076 {
3077 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3078 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
3079 bool ipv4 = !ipv6 || metadata;
3080 int ret = 0;
3081
3082 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
3083 #if IS_ENABLED(CONFIG_IPV6)
3084 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
3085 if (ipv6) {
3086 ret = __vxlan_sock_add(vxlan, true);
3087 if (ret < 0 && ret != -EAFNOSUPPORT)
3088 ipv4 = false;
3089 }
3090 #endif
3091 if (ipv4)
3092 ret = __vxlan_sock_add(vxlan, false);
3093 if (ret < 0)
3094 vxlan_sock_release(vxlan);
3095 return ret;
3096 }
3097
3098 static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3099 struct net_device **lower,
3100 struct vxlan_dev *old)
3101 {
3102 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
3103 struct vxlan_dev *tmp;
3104 bool use_ipv6 = false;
3105
3106 if (conf->flags & VXLAN_F_GPE) {
3107 /* For now, allow GPE only together with
3108 * COLLECT_METADATA. This can be relaxed later; in such
3109 * case, the other side of the PtP link will have to be
3110 * provided.
3111 */
3112 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3113 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3114 return -EINVAL;
3115 }
3116 }
3117
3118 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3119 /* Unless IPv6 is explicitly requested, assume IPv4 */
3120 conf->remote_ip.sa.sa_family = AF_INET;
3121 conf->saddr.sa.sa_family = AF_INET;
3122 } else if (!conf->remote_ip.sa.sa_family) {
3123 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3124 } else if (!conf->saddr.sa.sa_family) {
3125 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3126 }
3127
3128 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family)
3129 return -EINVAL;
3130
3131 if (vxlan_addr_multicast(&conf->saddr))
3132 return -EINVAL;
3133
3134 if (conf->saddr.sa.sa_family == AF_INET6) {
3135 if (!IS_ENABLED(CONFIG_IPV6))
3136 return -EPFNOSUPPORT;
3137 use_ipv6 = true;
3138 conf->flags |= VXLAN_F_IPV6;
3139
3140 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3141 int local_type =
3142 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3143 int remote_type =
3144 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3145
3146 if (local_type & IPV6_ADDR_LINKLOCAL) {
3147 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
3148 (remote_type != IPV6_ADDR_ANY))
3149 return -EINVAL;
3150
3151 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3152 } else {
3153 if (remote_type ==
3154 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL))
3155 return -EINVAL;
3156
3157 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3158 }
3159 }
3160 }
3161
3162 if (conf->label && !use_ipv6)
3163 return -EINVAL;
3164
3165 if (conf->remote_ifindex) {
3166 struct net_device *lowerdev;
3167
3168 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
3169 if (!lowerdev)
3170 return -ENODEV;
3171
3172 #if IS_ENABLED(CONFIG_IPV6)
3173 if (use_ipv6) {
3174 struct inet6_dev *idev = __in6_dev_get(lowerdev);
3175 if (idev && idev->cnf.disable_ipv6)
3176 return -EPERM;
3177 }
3178 #endif
3179
3180 *lower = lowerdev;
3181 } else {
3182 if (vxlan_addr_multicast(&conf->remote_ip))
3183 return -EINVAL;
3184
3185 #if IS_ENABLED(CONFIG_IPV6)
3186 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL)
3187 return -EINVAL;
3188 #endif
3189
3190 *lower = NULL;
3191 }
3192
3193 if (!conf->dst_port) {
3194 if (conf->flags & VXLAN_F_GPE)
3195 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3196 else
3197 conf->dst_port = htons(vxlan_port);
3198 }
3199
3200 if (!conf->age_interval)
3201 conf->age_interval = FDB_AGE_DEFAULT;
3202
3203 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3204 if (tmp == old)
3205 continue;
3206
3207 if (tmp->cfg.vni != conf->vni)
3208 continue;
3209 if (tmp->cfg.dst_port != conf->dst_port)
3210 continue;
3211 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3212 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3213 continue;
3214
3215 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3216 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3217 continue;
3218
3219 return -EEXIST;
3220 }
3221
3222 return 0;
3223 }
3224
3225 static void vxlan_config_apply(struct net_device *dev,
3226 struct vxlan_config *conf,
3227 struct net_device *lowerdev,
3228 struct net *src_net,
3229 bool changelink)
3230 {
3231 struct vxlan_dev *vxlan = netdev_priv(dev);
3232 struct vxlan_rdst *dst = &vxlan->default_dst;
3233 unsigned short needed_headroom = ETH_HLEN;
3234 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3235 int max_mtu = ETH_MAX_MTU;
3236
3237 if (!changelink) {
3238 if (conf->flags & VXLAN_F_GPE)
3239 vxlan_raw_setup(dev);
3240 else
3241 vxlan_ether_setup(dev);
3242
3243 if (conf->mtu)
3244 dev->mtu = conf->mtu;
3245
3246 vxlan->net = src_net;
3247 }
3248
3249 dst->remote_vni = conf->vni;
3250
3251 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
3252
3253 if (lowerdev) {
3254 dst->remote_ifindex = conf->remote_ifindex;
3255
3256 dev->gso_max_size = lowerdev->gso_max_size;
3257 dev->gso_max_segs = lowerdev->gso_max_segs;
3258
3259 needed_headroom = lowerdev->hard_header_len;
3260
3261 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3262 VXLAN_HEADROOM);
3263 }
3264
3265 if (dev->mtu > max_mtu)
3266 dev->mtu = max_mtu;
3267
3268 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3269 needed_headroom += VXLAN6_HEADROOM;
3270 else
3271 needed_headroom += VXLAN_HEADROOM;
3272 dev->needed_headroom = needed_headroom;
3273
3274 memcpy(&vxlan->cfg, conf, sizeof(*conf));
3275 }
3276
3277 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3278 struct vxlan_config *conf,
3279 bool changelink)
3280 {
3281 struct vxlan_dev *vxlan = netdev_priv(dev);
3282 struct net_device *lowerdev;
3283 int ret;
3284
3285 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan);
3286 if (ret)
3287 return ret;
3288
3289 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
3290
3291 return 0;
3292 }
3293
3294 static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3295 struct vxlan_config *conf)
3296 {
3297 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3298 struct vxlan_dev *vxlan = netdev_priv(dev);
3299 int err;
3300
3301 err = vxlan_dev_configure(net, dev, conf, false);
3302 if (err)
3303 return err;
3304
3305 dev->ethtool_ops = &vxlan_ethtool_ops;
3306
3307 /* create an fdb entry for a valid default destination */
3308 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
3309 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3310 &vxlan->default_dst.remote_ip,
3311 NUD_REACHABLE | NUD_PERMANENT,
3312 NLM_F_EXCL | NLM_F_CREATE,
3313 vxlan->cfg.dst_port,
3314 vxlan->default_dst.remote_vni,
3315 vxlan->default_dst.remote_vni,
3316 vxlan->default_dst.remote_ifindex,
3317 NTF_SELF);
3318 if (err)
3319 return err;
3320 }
3321
3322 err = register_netdevice(dev);
3323 if (err) {
3324 vxlan_fdb_delete_default(vxlan, vxlan->default_dst.remote_vni);
3325 return err;
3326 }
3327
3328 list_add(&vxlan->next, &vn->vxlan_list);
3329 return 0;
3330 }
3331
3332 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3333 struct net_device *dev, struct vxlan_config *conf,
3334 bool changelink)
3335 {
3336 struct vxlan_dev *vxlan = netdev_priv(dev);
3337 int err;
3338
3339 memset(conf, 0, sizeof(*conf));
3340
3341 /* if changelink operation, start with old existing cfg */
3342 if (changelink)
3343 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3344
3345 if (data[IFLA_VXLAN_ID]) {
3346 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3347
3348 if (changelink && (vni != conf->vni))
3349 return -EOPNOTSUPP;
3350 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3351 }
3352
3353 if (data[IFLA_VXLAN_GROUP]) {
3354 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET))
3355 return -EOPNOTSUPP;
3356
3357 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3358 conf->remote_ip.sa.sa_family = AF_INET;
3359 } else if (data[IFLA_VXLAN_GROUP6]) {
3360 if (!IS_ENABLED(CONFIG_IPV6))
3361 return -EPFNOSUPPORT;
3362
3363 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6))
3364 return -EOPNOTSUPP;
3365
3366 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3367 conf->remote_ip.sa.sa_family = AF_INET6;
3368 }
3369
3370 if (data[IFLA_VXLAN_FAN_MAP]) {
3371 err = vxlan_parse_fan_map(data, vxlan);
3372 if (err)
3373 return err;
3374 }
3375
3376 if (data[IFLA_VXLAN_LOCAL]) {
3377 if (changelink && (conf->saddr.sa.sa_family != AF_INET))
3378 return -EOPNOTSUPP;
3379
3380 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3381 conf->saddr.sa.sa_family = AF_INET;
3382 } else if (data[IFLA_VXLAN_LOCAL6]) {
3383 if (!IS_ENABLED(CONFIG_IPV6))
3384 return -EPFNOSUPPORT;
3385
3386 if (changelink && (conf->saddr.sa.sa_family != AF_INET6))
3387 return -EOPNOTSUPP;
3388
3389 /* TODO: respect scope id */
3390 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3391 conf->saddr.sa.sa_family = AF_INET6;
3392 }
3393
3394 if (data[IFLA_VXLAN_LINK])
3395 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3396
3397 if (data[IFLA_VXLAN_TOS])
3398 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3399
3400 if (data[IFLA_VXLAN_TTL])
3401 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3402
3403 if (data[IFLA_VXLAN_LABEL])
3404 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3405 IPV6_FLOWLABEL_MASK;
3406
3407 if (data[IFLA_VXLAN_LEARNING]) {
3408 if (nla_get_u8(data[IFLA_VXLAN_LEARNING]))
3409 conf->flags |= VXLAN_F_LEARN;
3410 else
3411 conf->flags &= ~VXLAN_F_LEARN;
3412 } else if (!changelink) {
3413 /* default to learn on a new device */
3414 conf->flags |= VXLAN_F_LEARN;
3415 }
3416
3417 if (data[IFLA_VXLAN_AGEING]) {
3418 if (changelink)
3419 return -EOPNOTSUPP;
3420 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3421 }
3422
3423 if (data[IFLA_VXLAN_PROXY]) {
3424 if (changelink)
3425 return -EOPNOTSUPP;
3426 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3427 conf->flags |= VXLAN_F_PROXY;
3428 }
3429
3430 if (data[IFLA_VXLAN_RSC]) {
3431 if (changelink)
3432 return -EOPNOTSUPP;
3433 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3434 conf->flags |= VXLAN_F_RSC;
3435 }
3436
3437 if (data[IFLA_VXLAN_L2MISS]) {
3438 if (changelink)
3439 return -EOPNOTSUPP;
3440 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3441 conf->flags |= VXLAN_F_L2MISS;
3442 }
3443
3444 if (data[IFLA_VXLAN_L3MISS]) {
3445 if (changelink)
3446 return -EOPNOTSUPP;
3447 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3448 conf->flags |= VXLAN_F_L3MISS;
3449 }
3450
3451 if (data[IFLA_VXLAN_LIMIT]) {
3452 if (changelink)
3453 return -EOPNOTSUPP;
3454 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3455 }
3456
3457 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3458 if (changelink)
3459 return -EOPNOTSUPP;
3460 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3461 conf->flags |= VXLAN_F_COLLECT_METADATA;
3462 }
3463
3464 if (data[IFLA_VXLAN_PORT_RANGE]) {
3465 if (!changelink) {
3466 const struct ifla_vxlan_port_range *p
3467 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3468 conf->port_min = ntohs(p->low);
3469 conf->port_max = ntohs(p->high);
3470 } else {
3471 return -EOPNOTSUPP;
3472 }
3473 }
3474
3475 if (data[IFLA_VXLAN_PORT]) {
3476 if (changelink)
3477 return -EOPNOTSUPP;
3478 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3479 }
3480
3481 if (data[IFLA_VXLAN_UDP_CSUM]) {
3482 if (changelink)
3483 return -EOPNOTSUPP;
3484 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3485 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3486 }
3487
3488 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3489 if (changelink)
3490 return -EOPNOTSUPP;
3491 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3492 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3493 }
3494
3495 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3496 if (changelink)
3497 return -EOPNOTSUPP;
3498 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3499 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3500 }
3501
3502 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3503 if (changelink)
3504 return -EOPNOTSUPP;
3505 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3506 conf->flags |= VXLAN_F_REMCSUM_TX;
3507 }
3508
3509 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3510 if (changelink)
3511 return -EOPNOTSUPP;
3512 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3513 conf->flags |= VXLAN_F_REMCSUM_RX;
3514 }
3515
3516 if (data[IFLA_VXLAN_GBP]) {
3517 if (changelink)
3518 return -EOPNOTSUPP;
3519 conf->flags |= VXLAN_F_GBP;
3520 }
3521
3522 if (data[IFLA_VXLAN_GPE]) {
3523 if (changelink)
3524 return -EOPNOTSUPP;
3525 conf->flags |= VXLAN_F_GPE;
3526 }
3527
3528 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3529 if (changelink)
3530 return -EOPNOTSUPP;
3531 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3532 }
3533
3534 if (tb[IFLA_MTU]) {
3535 if (changelink)
3536 return -EOPNOTSUPP;
3537 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3538 }
3539
3540 return 0;
3541 }
3542
3543 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3544 struct nlattr *tb[], struct nlattr *data[],
3545 struct netlink_ext_ack *extack)
3546 {
3547 struct vxlan_config conf;
3548 int err;
3549
3550 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3551 if (err)
3552 return err;
3553
3554 return __vxlan_dev_create(src_net, dev, &conf);
3555 }
3556
3557 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
3558 struct nlattr *data[],
3559 struct netlink_ext_ack *extack)
3560 {
3561 struct vxlan_dev *vxlan = netdev_priv(dev);
3562 struct vxlan_rdst *dst = &vxlan->default_dst;
3563 struct vxlan_rdst old_dst;
3564 struct vxlan_config conf;
3565 int err;
3566
3567 err = vxlan_nl2conf(tb, data,
3568 dev, &conf, true);
3569 if (err)
3570 return err;
3571
3572 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
3573
3574 err = vxlan_dev_configure(vxlan->net, dev, &conf, true);
3575 if (err)
3576 return err;
3577
3578 /* handle default dst entry */
3579 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3580 spin_lock_bh(&vxlan->hash_lock);
3581 if (!vxlan_addr_any(&old_dst.remote_ip))
3582 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3583 old_dst.remote_ip,
3584 vxlan->cfg.dst_port,
3585 old_dst.remote_vni,
3586 old_dst.remote_vni,
3587 old_dst.remote_ifindex, 0);
3588
3589 if (!vxlan_addr_any(&dst->remote_ip)) {
3590 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3591 &dst->remote_ip,
3592 NUD_REACHABLE | NUD_PERMANENT,
3593 NLM_F_CREATE | NLM_F_APPEND,
3594 vxlan->cfg.dst_port,
3595 dst->remote_vni,
3596 dst->remote_vni,
3597 dst->remote_ifindex,
3598 NTF_SELF);
3599 if (err) {
3600 spin_unlock_bh(&vxlan->hash_lock);
3601 return err;
3602 }
3603 }
3604 spin_unlock_bh(&vxlan->hash_lock);
3605 }
3606
3607 return 0;
3608 }
3609
3610 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3611 {
3612 struct vxlan_dev *vxlan = netdev_priv(dev);
3613
3614 vxlan_flush(vxlan, true);
3615
3616 gro_cells_destroy(&vxlan->gro_cells);
3617 list_del(&vxlan->next);
3618 unregister_netdevice_queue(dev, head);
3619 }
3620
3621 static size_t vxlan_get_size(const struct net_device *dev)
3622 {
3623
3624 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
3625 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
3626 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
3627 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
3628 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3629 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
3630 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
3631 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
3632 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3633 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3634 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3635 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
3636 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
3637 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3638 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
3639 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
3640 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3641 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3642 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3643 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3644 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3645 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
3646 nla_total_size(sizeof(struct ip_fan_map) * 256) +
3647 0;
3648 }
3649
3650 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3651 {
3652 const struct vxlan_dev *vxlan = netdev_priv(dev);
3653 const struct vxlan_rdst *dst = &vxlan->default_dst;
3654 struct ifla_vxlan_port_range ports = {
3655 .low = htons(vxlan->cfg.port_min),
3656 .high = htons(vxlan->cfg.port_max),
3657 };
3658
3659 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
3660 goto nla_put_failure;
3661
3662 if (!vxlan_addr_any(&dst->remote_ip)) {
3663 if (dst->remote_ip.sa.sa_family == AF_INET) {
3664 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3665 dst->remote_ip.sin.sin_addr.s_addr))
3666 goto nla_put_failure;
3667 #if IS_ENABLED(CONFIG_IPV6)
3668 } else {
3669 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3670 &dst->remote_ip.sin6.sin6_addr))
3671 goto nla_put_failure;
3672 #endif
3673 }
3674 }
3675
3676 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
3677 goto nla_put_failure;
3678
3679 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3680 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
3681 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
3682 vxlan->cfg.saddr.sin.sin_addr.s_addr))
3683 goto nla_put_failure;
3684 #if IS_ENABLED(CONFIG_IPV6)
3685 } else {
3686 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
3687 &vxlan->cfg.saddr.sin6.sin6_addr))
3688 goto nla_put_failure;
3689 #endif
3690 }
3691 }
3692
3693 if (fan_has_map(&vxlan->fan)) {
3694 struct nlattr *fan_nest;
3695 struct ip_fan_map *fan_map;
3696
3697 fan_nest = nla_nest_start(skb, IFLA_VXLAN_FAN_MAP);
3698 if (!fan_nest)
3699 goto nla_put_failure;
3700 list_for_each_entry_rcu(fan_map, &vxlan->fan.fan_maps, list) {
3701 struct ifla_fan_map map;
3702
3703 map.underlay = fan_map->underlay;
3704 map.underlay_prefix = fan_map->underlay_prefix;
3705 map.overlay = fan_map->overlay;
3706 map.overlay_prefix = fan_map->overlay_prefix;
3707 if (nla_put(skb, IFLA_FAN_MAPPING, sizeof(map), &map))
3708 goto nla_put_failure;
3709 }
3710 nla_nest_end(skb, fan_nest);
3711 }
3712
3713 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3714 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
3715 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
3716 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3717 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
3718 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3719 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
3720 nla_put_u8(skb, IFLA_VXLAN_RSC,
3721 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
3722 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3723 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
3724 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3725 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
3726 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3727 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
3728 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3729 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3730 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
3731 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
3732 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
3733 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3734 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3735 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
3736 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3737 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3738 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
3739 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3740 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
3741 goto nla_put_failure;
3742
3743 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3744 goto nla_put_failure;
3745
3746 if (vxlan->cfg.flags & VXLAN_F_GBP &&
3747 nla_put_flag(skb, IFLA_VXLAN_GBP))
3748 goto nla_put_failure;
3749
3750 if (vxlan->cfg.flags & VXLAN_F_GPE &&
3751 nla_put_flag(skb, IFLA_VXLAN_GPE))
3752 goto nla_put_failure;
3753
3754 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3755 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3756 goto nla_put_failure;
3757
3758 return 0;
3759
3760 nla_put_failure:
3761 return -EMSGSIZE;
3762 }
3763
3764 static struct net *vxlan_get_link_net(const struct net_device *dev)
3765 {
3766 struct vxlan_dev *vxlan = netdev_priv(dev);
3767
3768 return vxlan->net;
3769 }
3770
3771 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3772 .kind = "vxlan",
3773 .maxtype = IFLA_VXLAN_MAX,
3774 .policy = vxlan_policy,
3775 .priv_size = sizeof(struct vxlan_dev),
3776 .setup = vxlan_setup,
3777 .validate = vxlan_validate,
3778 .newlink = vxlan_newlink,
3779 .changelink = vxlan_changelink,
3780 .dellink = vxlan_dellink,
3781 .get_size = vxlan_get_size,
3782 .fill_info = vxlan_fill_info,
3783 .get_link_net = vxlan_get_link_net,
3784 };
3785
3786 struct net_device *vxlan_dev_create(struct net *net, const char *name,
3787 u8 name_assign_type,
3788 struct vxlan_config *conf)
3789 {
3790 struct nlattr *tb[IFLA_MAX + 1];
3791 struct net_device *dev;
3792 int err;
3793
3794 memset(&tb, 0, sizeof(tb));
3795
3796 dev = rtnl_create_link(net, name, name_assign_type,
3797 &vxlan_link_ops, tb);
3798 if (IS_ERR(dev))
3799 return dev;
3800
3801 err = __vxlan_dev_create(net, dev, conf);
3802 if (err < 0) {
3803 free_netdev(dev);
3804 return ERR_PTR(err);
3805 }
3806
3807 err = rtnl_configure_link(dev, NULL);
3808 if (err < 0) {
3809 LIST_HEAD(list_kill);
3810
3811 vxlan_dellink(dev, &list_kill);
3812 unregister_netdevice_many(&list_kill);
3813 return ERR_PTR(err);
3814 }
3815
3816 return dev;
3817 }
3818 EXPORT_SYMBOL_GPL(vxlan_dev_create);
3819
3820 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3821 struct net_device *dev)
3822 {
3823 struct vxlan_dev *vxlan, *next;
3824 LIST_HEAD(list_kill);
3825
3826 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3827 struct vxlan_rdst *dst = &vxlan->default_dst;
3828
3829 /* In case we created vxlan device with carrier
3830 * and we loose the carrier due to module unload
3831 * we also need to remove vxlan device. In other
3832 * cases, it's not necessary and remote_ifindex
3833 * is 0 here, so no matches.
3834 */
3835 if (dst->remote_ifindex == dev->ifindex)
3836 vxlan_dellink(vxlan->dev, &list_kill);
3837 }
3838
3839 unregister_netdevice_many(&list_kill);
3840 }
3841
3842 static int vxlan_netdevice_event(struct notifier_block *unused,
3843 unsigned long event, void *ptr)
3844 {
3845 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3846 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
3847
3848 if (event == NETDEV_UNREGISTER)
3849 vxlan_handle_lowerdev_unregister(vn, dev);
3850 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
3851 vxlan_push_rx_ports(dev);
3852
3853 return NOTIFY_DONE;
3854 }
3855
3856 static struct notifier_block vxlan_notifier_block __read_mostly = {
3857 .notifier_call = vxlan_netdevice_event,
3858 };
3859
3860 static __net_init int vxlan_init_net(struct net *net)
3861 {
3862 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3863 unsigned int h;
3864
3865 INIT_LIST_HEAD(&vn->vxlan_list);
3866 spin_lock_init(&vn->sock_lock);
3867
3868 for (h = 0; h < PORT_HASH_SIZE; ++h)
3869 INIT_HLIST_HEAD(&vn->sock_list[h]);
3870
3871 return 0;
3872 }
3873
3874 #ifdef CONFIG_SYSCTL
3875 static struct ctl_table_header *vxlan_fan_header;
3876 static unsigned int vxlan_fan_version = 4;
3877
3878 static struct ctl_table vxlan_fan_sysctls[] = {
3879 {
3880 .procname = "vxlan",
3881 .data = &vxlan_fan_version,
3882 .maxlen = sizeof(vxlan_fan_version),
3883 .mode = 0444,
3884 .proc_handler = proc_dointvec,
3885 },
3886 {},
3887 };
3888 #endif /* CONFIG_SYSCTL */
3889
3890 static void __net_exit vxlan_exit_net(struct net *net)
3891 {
3892 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3893 struct vxlan_dev *vxlan, *next;
3894 struct net_device *dev, *aux;
3895 LIST_HEAD(list);
3896
3897 rtnl_lock();
3898 for_each_netdev_safe(net, dev, aux)
3899 if (dev->rtnl_link_ops == &vxlan_link_ops)
3900 unregister_netdevice_queue(dev, &list);
3901
3902 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3903 /* If vxlan->dev is in the same netns, it has already been added
3904 * to the list by the previous loop.
3905 */
3906 if (!net_eq(dev_net(vxlan->dev), net)) {
3907 gro_cells_destroy(&vxlan->gro_cells);
3908 unregister_netdevice_queue(vxlan->dev, &list);
3909 }
3910 }
3911
3912 unregister_netdevice_many(&list);
3913 rtnl_unlock();
3914 }
3915
3916 static struct pernet_operations vxlan_net_ops = {
3917 .init = vxlan_init_net,
3918 .exit = vxlan_exit_net,
3919 .id = &vxlan_net_id,
3920 .size = sizeof(struct vxlan_net),
3921 };
3922
3923 static int __init vxlan_init_module(void)
3924 {
3925 int rc;
3926
3927 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3928
3929 rc = register_pernet_subsys(&vxlan_net_ops);
3930 if (rc)
3931 goto out1;
3932
3933 rc = register_netdevice_notifier(&vxlan_notifier_block);
3934 if (rc)
3935 goto out2;
3936
3937 rc = rtnl_link_register(&vxlan_link_ops);
3938 if (rc)
3939 goto out3;
3940
3941 #ifdef CONFIG_SYSCTL
3942 vxlan_fan_header = register_net_sysctl(&init_net, "net/fan",
3943 vxlan_fan_sysctls);
3944 if (!vxlan_fan_header) {
3945 rc = -ENOMEM;
3946 goto sysctl_failed;
3947 }
3948 #endif /* CONFIG_SYSCTL */
3949
3950 return 0;
3951 #ifdef CONFIG_SYSCTL
3952 sysctl_failed:
3953 rtnl_link_unregister(&vxlan_link_ops);
3954 #endif /* CONFIG_SYSCTL */
3955 out3:
3956 unregister_netdevice_notifier(&vxlan_notifier_block);
3957 out2:
3958 unregister_pernet_subsys(&vxlan_net_ops);
3959 out1:
3960 return rc;
3961 }
3962 late_initcall(vxlan_init_module);
3963
3964 static void __exit vxlan_cleanup_module(void)
3965 {
3966 #ifdef CONFIG_SYSCTL
3967 unregister_net_sysctl_table(vxlan_fan_header);
3968 #endif /* CONFIG_SYSCTL */
3969 rtnl_link_unregister(&vxlan_link_ops);
3970 unregister_netdevice_notifier(&vxlan_notifier_block);
3971 unregister_pernet_subsys(&vxlan_net_ops);
3972 /* rcu_barrier() is called by netns */
3973 }
3974 module_exit(vxlan_cleanup_module);
3975
3976 MODULE_LICENSE("GPL");
3977 MODULE_VERSION(VXLAN_VERSION);
3978 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3979 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3980 MODULE_ALIAS_RTNL_LINK("vxlan");