vlan: rename __vlan_put_tag to vlan_insert_tag_set_proto
[deliverable/linux.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/types.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/rculist.h>
20 #include <linux/netdevice.h>
21 #include <linux/in.h>
22 #include <linux/ip.h>
23 #include <linux/udp.h>
24 #include <linux/igmp.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/hash.h>
29 #include <linux/ethtool.h>
30 #include <net/arp.h>
31 #include <net/ndisc.h>
32 #include <net/ip.h>
33 #include <net/ip_tunnels.h>
34 #include <net/icmp.h>
35 #include <net/udp.h>
36 #include <net/udp_tunnel.h>
37 #include <net/rtnetlink.h>
38 #include <net/route.h>
39 #include <net/dsfield.h>
40 #include <net/inet_ecn.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <net/vxlan.h>
44 #include <net/protocol.h>
45 #include <net/udp_tunnel.h>
46 #if IS_ENABLED(CONFIG_IPV6)
47 #include <net/ipv6.h>
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
50 #include <net/ip6_checksum.h>
51 #endif
52
53 #define VXLAN_VERSION "0.1"
54
55 #define PORT_HASH_BITS 8
56 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
57 #define VNI_HASH_BITS 10
58 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
59 #define FDB_HASH_BITS 8
60 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
61 #define FDB_AGE_DEFAULT 300 /* 5 min */
62 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
63
64 #define VXLAN_N_VID (1u << 24)
65 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
66 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
67
68 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
69
70 /* VXLAN protocol header */
71 struct vxlanhdr {
72 __be32 vx_flags;
73 __be32 vx_vni;
74 };
75
76 /* UDP port for VXLAN traffic.
77 * The IANA assigned port is 4789, but the Linux default is 8472
78 * for compatibility with early adopters.
79 */
80 static unsigned short vxlan_port __read_mostly = 8472;
81 module_param_named(udp_port, vxlan_port, ushort, 0444);
82 MODULE_PARM_DESC(udp_port, "Destination UDP port");
83
84 static bool log_ecn_error = true;
85 module_param(log_ecn_error, bool, 0644);
86 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
87
88 static int vxlan_net_id;
89
90 static const u8 all_zeros_mac[ETH_ALEN];
91
92 /* per-network namespace private data for this module */
93 struct vxlan_net {
94 struct list_head vxlan_list;
95 struct hlist_head sock_list[PORT_HASH_SIZE];
96 spinlock_t sock_lock;
97 };
98
99 union vxlan_addr {
100 struct sockaddr_in sin;
101 struct sockaddr_in6 sin6;
102 struct sockaddr sa;
103 };
104
105 struct vxlan_rdst {
106 union vxlan_addr remote_ip;
107 __be16 remote_port;
108 u32 remote_vni;
109 u32 remote_ifindex;
110 struct list_head list;
111 struct rcu_head rcu;
112 };
113
114 /* Forwarding table entry */
115 struct vxlan_fdb {
116 struct hlist_node hlist; /* linked list of entries */
117 struct rcu_head rcu;
118 unsigned long updated; /* jiffies */
119 unsigned long used;
120 struct list_head remotes;
121 u16 state; /* see ndm_state */
122 u8 flags; /* see ndm_flags */
123 u8 eth_addr[ETH_ALEN];
124 };
125
126 /* Pseudo network device */
127 struct vxlan_dev {
128 struct hlist_node hlist; /* vni hash table */
129 struct list_head next; /* vxlan's per namespace list */
130 struct vxlan_sock *vn_sock; /* listening socket */
131 struct net_device *dev;
132 struct net *net; /* netns for packet i/o */
133 struct vxlan_rdst default_dst; /* default destination */
134 union vxlan_addr saddr; /* source address */
135 __be16 dst_port;
136 __u16 port_min; /* source port range */
137 __u16 port_max;
138 __u8 tos; /* TOS override */
139 __u8 ttl;
140 u32 flags; /* VXLAN_F_* in vxlan.h */
141
142 struct work_struct sock_work;
143 struct work_struct igmp_join;
144 struct work_struct igmp_leave;
145
146 unsigned long age_interval;
147 struct timer_list age_timer;
148 spinlock_t hash_lock;
149 unsigned int addrcnt;
150 unsigned int addrmax;
151
152 struct hlist_head fdb_head[FDB_HASH_SIZE];
153 };
154
155 /* salt for hash table */
156 static u32 vxlan_salt __read_mostly;
157 static struct workqueue_struct *vxlan_wq;
158
159 static void vxlan_sock_work(struct work_struct *work);
160
161 #if IS_ENABLED(CONFIG_IPV6)
162 static inline
163 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
164 {
165 if (a->sa.sa_family != b->sa.sa_family)
166 return false;
167 if (a->sa.sa_family == AF_INET6)
168 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
169 else
170 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
171 }
172
173 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
174 {
175 if (ipa->sa.sa_family == AF_INET6)
176 return ipv6_addr_any(&ipa->sin6.sin6_addr);
177 else
178 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
179 }
180
181 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
182 {
183 if (ipa->sa.sa_family == AF_INET6)
184 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
185 else
186 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
187 }
188
189 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
190 {
191 if (nla_len(nla) >= sizeof(struct in6_addr)) {
192 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
193 ip->sa.sa_family = AF_INET6;
194 return 0;
195 } else if (nla_len(nla) >= sizeof(__be32)) {
196 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
197 ip->sa.sa_family = AF_INET;
198 return 0;
199 } else {
200 return -EAFNOSUPPORT;
201 }
202 }
203
204 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
205 const union vxlan_addr *ip)
206 {
207 if (ip->sa.sa_family == AF_INET6)
208 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
209 else
210 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
211 }
212
213 #else /* !CONFIG_IPV6 */
214
215 static inline
216 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
217 {
218 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
219 }
220
221 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
222 {
223 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
224 }
225
226 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
227 {
228 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
229 }
230
231 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
232 {
233 if (nla_len(nla) >= sizeof(struct in6_addr)) {
234 return -EAFNOSUPPORT;
235 } else if (nla_len(nla) >= sizeof(__be32)) {
236 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
237 ip->sa.sa_family = AF_INET;
238 return 0;
239 } else {
240 return -EAFNOSUPPORT;
241 }
242 }
243
244 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
245 const union vxlan_addr *ip)
246 {
247 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
248 }
249 #endif
250
251 /* Virtual Network hash table head */
252 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
253 {
254 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
255 }
256
257 /* Socket hash table head */
258 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
259 {
260 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
261
262 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
263 }
264
265 /* First remote destination for a forwarding entry.
266 * Guaranteed to be non-NULL because remotes are never deleted.
267 */
268 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
269 {
270 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
271 }
272
273 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
274 {
275 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
276 }
277
278 /* Find VXLAN socket based on network namespace, address family and UDP port */
279 static struct vxlan_sock *vxlan_find_sock(struct net *net,
280 sa_family_t family, __be16 port)
281 {
282 struct vxlan_sock *vs;
283
284 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
285 if (inet_sk(vs->sock->sk)->inet_sport == port &&
286 inet_sk(vs->sock->sk)->sk.sk_family == family)
287 return vs;
288 }
289 return NULL;
290 }
291
292 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
293 {
294 struct vxlan_dev *vxlan;
295
296 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
297 if (vxlan->default_dst.remote_vni == id)
298 return vxlan;
299 }
300
301 return NULL;
302 }
303
304 /* Look up VNI in a per net namespace table */
305 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
306 sa_family_t family, __be16 port)
307 {
308 struct vxlan_sock *vs;
309
310 vs = vxlan_find_sock(net, family, port);
311 if (!vs)
312 return NULL;
313
314 return vxlan_vs_find_vni(vs, id);
315 }
316
317 /* Fill in neighbour message in skbuff. */
318 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
319 const struct vxlan_fdb *fdb,
320 u32 portid, u32 seq, int type, unsigned int flags,
321 const struct vxlan_rdst *rdst)
322 {
323 unsigned long now = jiffies;
324 struct nda_cacheinfo ci;
325 struct nlmsghdr *nlh;
326 struct ndmsg *ndm;
327 bool send_ip, send_eth;
328
329 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
330 if (nlh == NULL)
331 return -EMSGSIZE;
332
333 ndm = nlmsg_data(nlh);
334 memset(ndm, 0, sizeof(*ndm));
335
336 send_eth = send_ip = true;
337
338 if (type == RTM_GETNEIGH) {
339 ndm->ndm_family = AF_INET;
340 send_ip = !vxlan_addr_any(&rdst->remote_ip);
341 send_eth = !is_zero_ether_addr(fdb->eth_addr);
342 } else
343 ndm->ndm_family = AF_BRIDGE;
344 ndm->ndm_state = fdb->state;
345 ndm->ndm_ifindex = vxlan->dev->ifindex;
346 ndm->ndm_flags = fdb->flags;
347 ndm->ndm_type = RTN_UNICAST;
348
349 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
350 goto nla_put_failure;
351
352 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
353 goto nla_put_failure;
354
355 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
356 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
357 goto nla_put_failure;
358 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
359 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
360 goto nla_put_failure;
361 if (rdst->remote_ifindex &&
362 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
363 goto nla_put_failure;
364
365 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
366 ci.ndm_confirmed = 0;
367 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
368 ci.ndm_refcnt = 0;
369
370 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
371 goto nla_put_failure;
372
373 return nlmsg_end(skb, nlh);
374
375 nla_put_failure:
376 nlmsg_cancel(skb, nlh);
377 return -EMSGSIZE;
378 }
379
380 static inline size_t vxlan_nlmsg_size(void)
381 {
382 return NLMSG_ALIGN(sizeof(struct ndmsg))
383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
388 + nla_total_size(sizeof(struct nda_cacheinfo));
389 }
390
391 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
392 struct vxlan_rdst *rd, int type)
393 {
394 struct net *net = dev_net(vxlan->dev);
395 struct sk_buff *skb;
396 int err = -ENOBUFS;
397
398 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
399 if (skb == NULL)
400 goto errout;
401
402 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
403 if (err < 0) {
404 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
405 WARN_ON(err == -EMSGSIZE);
406 kfree_skb(skb);
407 goto errout;
408 }
409
410 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
411 return;
412 errout:
413 if (err < 0)
414 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
415 }
416
417 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
418 {
419 struct vxlan_dev *vxlan = netdev_priv(dev);
420 struct vxlan_fdb f = {
421 .state = NUD_STALE,
422 };
423 struct vxlan_rdst remote = {
424 .remote_ip = *ipa, /* goes to NDA_DST */
425 .remote_vni = VXLAN_N_VID,
426 };
427
428 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
429 }
430
431 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
432 {
433 struct vxlan_fdb f = {
434 .state = NUD_STALE,
435 };
436 struct vxlan_rdst remote = { };
437
438 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
439
440 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
441 }
442
443 /* Hash Ethernet address */
444 static u32 eth_hash(const unsigned char *addr)
445 {
446 u64 value = get_unaligned((u64 *)addr);
447
448 /* only want 6 bytes */
449 #ifdef __BIG_ENDIAN
450 value >>= 16;
451 #else
452 value <<= 16;
453 #endif
454 return hash_64(value, FDB_HASH_BITS);
455 }
456
457 /* Hash chain to use given mac address */
458 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
459 const u8 *mac)
460 {
461 return &vxlan->fdb_head[eth_hash(mac)];
462 }
463
464 /* Look up Ethernet address in forwarding table */
465 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
466 const u8 *mac)
467 {
468 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
469 struct vxlan_fdb *f;
470
471 hlist_for_each_entry_rcu(f, head, hlist) {
472 if (ether_addr_equal(mac, f->eth_addr))
473 return f;
474 }
475
476 return NULL;
477 }
478
479 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
480 const u8 *mac)
481 {
482 struct vxlan_fdb *f;
483
484 f = __vxlan_find_mac(vxlan, mac);
485 if (f)
486 f->used = jiffies;
487
488 return f;
489 }
490
491 /* caller should hold vxlan->hash_lock */
492 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
493 union vxlan_addr *ip, __be16 port,
494 __u32 vni, __u32 ifindex)
495 {
496 struct vxlan_rdst *rd;
497
498 list_for_each_entry(rd, &f->remotes, list) {
499 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
500 rd->remote_port == port &&
501 rd->remote_vni == vni &&
502 rd->remote_ifindex == ifindex)
503 return rd;
504 }
505
506 return NULL;
507 }
508
509 /* Replace destination of unicast mac */
510 static int vxlan_fdb_replace(struct vxlan_fdb *f,
511 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
512 {
513 struct vxlan_rdst *rd;
514
515 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
516 if (rd)
517 return 0;
518
519 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
520 if (!rd)
521 return 0;
522 rd->remote_ip = *ip;
523 rd->remote_port = port;
524 rd->remote_vni = vni;
525 rd->remote_ifindex = ifindex;
526 return 1;
527 }
528
529 /* Add/update destinations for multicast */
530 static int vxlan_fdb_append(struct vxlan_fdb *f,
531 union vxlan_addr *ip, __be16 port, __u32 vni,
532 __u32 ifindex, struct vxlan_rdst **rdp)
533 {
534 struct vxlan_rdst *rd;
535
536 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
537 if (rd)
538 return 0;
539
540 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
541 if (rd == NULL)
542 return -ENOBUFS;
543 rd->remote_ip = *ip;
544 rd->remote_port = port;
545 rd->remote_vni = vni;
546 rd->remote_ifindex = ifindex;
547
548 list_add_tail_rcu(&rd->list, &f->remotes);
549
550 *rdp = rd;
551 return 1;
552 }
553
554 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, struct sk_buff *skb)
555 {
556 struct sk_buff *p, **pp = NULL;
557 struct vxlanhdr *vh, *vh2;
558 struct ethhdr *eh, *eh2;
559 unsigned int hlen, off_vx, off_eth;
560 const struct packet_offload *ptype;
561 __be16 type;
562 int flush = 1;
563
564 off_vx = skb_gro_offset(skb);
565 hlen = off_vx + sizeof(*vh);
566 vh = skb_gro_header_fast(skb, off_vx);
567 if (skb_gro_header_hard(skb, hlen)) {
568 vh = skb_gro_header_slow(skb, hlen, off_vx);
569 if (unlikely(!vh))
570 goto out;
571 }
572 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
573 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
574
575 off_eth = skb_gro_offset(skb);
576 hlen = off_eth + sizeof(*eh);
577 eh = skb_gro_header_fast(skb, off_eth);
578 if (skb_gro_header_hard(skb, hlen)) {
579 eh = skb_gro_header_slow(skb, hlen, off_eth);
580 if (unlikely(!eh))
581 goto out;
582 }
583
584 flush = 0;
585
586 for (p = *head; p; p = p->next) {
587 if (!NAPI_GRO_CB(p)->same_flow)
588 continue;
589
590 vh2 = (struct vxlanhdr *)(p->data + off_vx);
591 eh2 = (struct ethhdr *)(p->data + off_eth);
592 if (vh->vx_vni != vh2->vx_vni || compare_ether_header(eh, eh2)) {
593 NAPI_GRO_CB(p)->same_flow = 0;
594 continue;
595 }
596 }
597
598 type = eh->h_proto;
599
600 rcu_read_lock();
601 ptype = gro_find_receive_by_type(type);
602 if (ptype == NULL) {
603 flush = 1;
604 goto out_unlock;
605 }
606
607 skb_gro_pull(skb, sizeof(*eh)); /* pull inner eth header */
608 skb_gro_postpull_rcsum(skb, eh, sizeof(*eh));
609 pp = ptype->callbacks.gro_receive(head, skb);
610
611 out_unlock:
612 rcu_read_unlock();
613 out:
614 NAPI_GRO_CB(skb)->flush |= flush;
615
616 return pp;
617 }
618
619 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff)
620 {
621 struct ethhdr *eh;
622 struct packet_offload *ptype;
623 __be16 type;
624 int vxlan_len = sizeof(struct vxlanhdr) + sizeof(struct ethhdr);
625 int err = -ENOSYS;
626
627 udp_tunnel_gro_complete(skb, nhoff);
628
629 eh = (struct ethhdr *)(skb->data + nhoff + sizeof(struct vxlanhdr));
630 type = eh->h_proto;
631
632 rcu_read_lock();
633 ptype = gro_find_complete_by_type(type);
634 if (ptype != NULL)
635 err = ptype->callbacks.gro_complete(skb, nhoff + vxlan_len);
636
637 rcu_read_unlock();
638 return err;
639 }
640
641 /* Notify netdevs that UDP port started listening */
642 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
643 {
644 struct net_device *dev;
645 struct sock *sk = vs->sock->sk;
646 struct net *net = sock_net(sk);
647 sa_family_t sa_family = sk->sk_family;
648 __be16 port = inet_sk(sk)->inet_sport;
649 int err;
650
651 if (sa_family == AF_INET) {
652 err = udp_add_offload(&vs->udp_offloads);
653 if (err)
654 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
655 }
656
657 rcu_read_lock();
658 for_each_netdev_rcu(net, dev) {
659 if (dev->netdev_ops->ndo_add_vxlan_port)
660 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
661 port);
662 }
663 rcu_read_unlock();
664 }
665
666 /* Notify netdevs that UDP port is no more listening */
667 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
668 {
669 struct net_device *dev;
670 struct sock *sk = vs->sock->sk;
671 struct net *net = sock_net(sk);
672 sa_family_t sa_family = sk->sk_family;
673 __be16 port = inet_sk(sk)->inet_sport;
674
675 rcu_read_lock();
676 for_each_netdev_rcu(net, dev) {
677 if (dev->netdev_ops->ndo_del_vxlan_port)
678 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
679 port);
680 }
681 rcu_read_unlock();
682
683 if (sa_family == AF_INET)
684 udp_del_offload(&vs->udp_offloads);
685 }
686
687 /* Add new entry to forwarding table -- assumes lock held */
688 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
689 const u8 *mac, union vxlan_addr *ip,
690 __u16 state, __u16 flags,
691 __be16 port, __u32 vni, __u32 ifindex,
692 __u8 ndm_flags)
693 {
694 struct vxlan_rdst *rd = NULL;
695 struct vxlan_fdb *f;
696 int notify = 0;
697
698 f = __vxlan_find_mac(vxlan, mac);
699 if (f) {
700 if (flags & NLM_F_EXCL) {
701 netdev_dbg(vxlan->dev,
702 "lost race to create %pM\n", mac);
703 return -EEXIST;
704 }
705 if (f->state != state) {
706 f->state = state;
707 f->updated = jiffies;
708 notify = 1;
709 }
710 if (f->flags != ndm_flags) {
711 f->flags = ndm_flags;
712 f->updated = jiffies;
713 notify = 1;
714 }
715 if ((flags & NLM_F_REPLACE)) {
716 /* Only change unicasts */
717 if (!(is_multicast_ether_addr(f->eth_addr) ||
718 is_zero_ether_addr(f->eth_addr))) {
719 int rc = vxlan_fdb_replace(f, ip, port, vni,
720 ifindex);
721
722 if (rc < 0)
723 return rc;
724 notify |= rc;
725 } else
726 return -EOPNOTSUPP;
727 }
728 if ((flags & NLM_F_APPEND) &&
729 (is_multicast_ether_addr(f->eth_addr) ||
730 is_zero_ether_addr(f->eth_addr))) {
731 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
732 &rd);
733
734 if (rc < 0)
735 return rc;
736 notify |= rc;
737 }
738 } else {
739 if (!(flags & NLM_F_CREATE))
740 return -ENOENT;
741
742 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
743 return -ENOSPC;
744
745 /* Disallow replace to add a multicast entry */
746 if ((flags & NLM_F_REPLACE) &&
747 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
748 return -EOPNOTSUPP;
749
750 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
751 f = kmalloc(sizeof(*f), GFP_ATOMIC);
752 if (!f)
753 return -ENOMEM;
754
755 notify = 1;
756 f->state = state;
757 f->flags = ndm_flags;
758 f->updated = f->used = jiffies;
759 INIT_LIST_HEAD(&f->remotes);
760 memcpy(f->eth_addr, mac, ETH_ALEN);
761
762 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
763
764 ++vxlan->addrcnt;
765 hlist_add_head_rcu(&f->hlist,
766 vxlan_fdb_head(vxlan, mac));
767 }
768
769 if (notify) {
770 if (rd == NULL)
771 rd = first_remote_rtnl(f);
772 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
773 }
774
775 return 0;
776 }
777
778 static void vxlan_fdb_free(struct rcu_head *head)
779 {
780 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
781 struct vxlan_rdst *rd, *nd;
782
783 list_for_each_entry_safe(rd, nd, &f->remotes, list)
784 kfree(rd);
785 kfree(f);
786 }
787
788 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
789 {
790 netdev_dbg(vxlan->dev,
791 "delete %pM\n", f->eth_addr);
792
793 --vxlan->addrcnt;
794 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
795
796 hlist_del_rcu(&f->hlist);
797 call_rcu(&f->rcu, vxlan_fdb_free);
798 }
799
800 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
801 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
802 {
803 struct net *net = dev_net(vxlan->dev);
804 int err;
805
806 if (tb[NDA_DST]) {
807 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
808 if (err)
809 return err;
810 } else {
811 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
812 if (remote->sa.sa_family == AF_INET) {
813 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
814 ip->sa.sa_family = AF_INET;
815 #if IS_ENABLED(CONFIG_IPV6)
816 } else {
817 ip->sin6.sin6_addr = in6addr_any;
818 ip->sa.sa_family = AF_INET6;
819 #endif
820 }
821 }
822
823 if (tb[NDA_PORT]) {
824 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
825 return -EINVAL;
826 *port = nla_get_be16(tb[NDA_PORT]);
827 } else {
828 *port = vxlan->dst_port;
829 }
830
831 if (tb[NDA_VNI]) {
832 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
833 return -EINVAL;
834 *vni = nla_get_u32(tb[NDA_VNI]);
835 } else {
836 *vni = vxlan->default_dst.remote_vni;
837 }
838
839 if (tb[NDA_IFINDEX]) {
840 struct net_device *tdev;
841
842 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
843 return -EINVAL;
844 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
845 tdev = __dev_get_by_index(net, *ifindex);
846 if (!tdev)
847 return -EADDRNOTAVAIL;
848 } else {
849 *ifindex = 0;
850 }
851
852 return 0;
853 }
854
855 /* Add static entry (via netlink) */
856 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
857 struct net_device *dev,
858 const unsigned char *addr, u16 flags)
859 {
860 struct vxlan_dev *vxlan = netdev_priv(dev);
861 /* struct net *net = dev_net(vxlan->dev); */
862 union vxlan_addr ip;
863 __be16 port;
864 u32 vni, ifindex;
865 int err;
866
867 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
868 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
869 ndm->ndm_state);
870 return -EINVAL;
871 }
872
873 if (tb[NDA_DST] == NULL)
874 return -EINVAL;
875
876 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
877 if (err)
878 return err;
879
880 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
881 return -EAFNOSUPPORT;
882
883 spin_lock_bh(&vxlan->hash_lock);
884 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
885 port, vni, ifindex, ndm->ndm_flags);
886 spin_unlock_bh(&vxlan->hash_lock);
887
888 return err;
889 }
890
891 /* Delete entry (via netlink) */
892 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
893 struct net_device *dev,
894 const unsigned char *addr)
895 {
896 struct vxlan_dev *vxlan = netdev_priv(dev);
897 struct vxlan_fdb *f;
898 struct vxlan_rdst *rd = NULL;
899 union vxlan_addr ip;
900 __be16 port;
901 u32 vni, ifindex;
902 int err;
903
904 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
905 if (err)
906 return err;
907
908 err = -ENOENT;
909
910 spin_lock_bh(&vxlan->hash_lock);
911 f = vxlan_find_mac(vxlan, addr);
912 if (!f)
913 goto out;
914
915 if (!vxlan_addr_any(&ip)) {
916 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
917 if (!rd)
918 goto out;
919 }
920
921 err = 0;
922
923 /* remove a destination if it's not the only one on the list,
924 * otherwise destroy the fdb entry
925 */
926 if (rd && !list_is_singular(&f->remotes)) {
927 list_del_rcu(&rd->list);
928 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
929 kfree_rcu(rd, rcu);
930 goto out;
931 }
932
933 vxlan_fdb_destroy(vxlan, f);
934
935 out:
936 spin_unlock_bh(&vxlan->hash_lock);
937
938 return err;
939 }
940
941 /* Dump forwarding table */
942 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
943 struct net_device *dev,
944 struct net_device *filter_dev, int idx)
945 {
946 struct vxlan_dev *vxlan = netdev_priv(dev);
947 unsigned int h;
948
949 for (h = 0; h < FDB_HASH_SIZE; ++h) {
950 struct vxlan_fdb *f;
951 int err;
952
953 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
954 struct vxlan_rdst *rd;
955
956 if (idx < cb->args[0])
957 goto skip;
958
959 list_for_each_entry_rcu(rd, &f->remotes, list) {
960 err = vxlan_fdb_info(skb, vxlan, f,
961 NETLINK_CB(cb->skb).portid,
962 cb->nlh->nlmsg_seq,
963 RTM_NEWNEIGH,
964 NLM_F_MULTI, rd);
965 if (err < 0)
966 goto out;
967 }
968 skip:
969 ++idx;
970 }
971 }
972 out:
973 return idx;
974 }
975
976 /* Watch incoming packets to learn mapping between Ethernet address
977 * and Tunnel endpoint.
978 * Return true if packet is bogus and should be droppped.
979 */
980 static bool vxlan_snoop(struct net_device *dev,
981 union vxlan_addr *src_ip, const u8 *src_mac)
982 {
983 struct vxlan_dev *vxlan = netdev_priv(dev);
984 struct vxlan_fdb *f;
985
986 f = vxlan_find_mac(vxlan, src_mac);
987 if (likely(f)) {
988 struct vxlan_rdst *rdst = first_remote_rcu(f);
989
990 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
991 return false;
992
993 /* Don't migrate static entries, drop packets */
994 if (f->state & NUD_NOARP)
995 return true;
996
997 if (net_ratelimit())
998 netdev_info(dev,
999 "%pM migrated from %pIS to %pIS\n",
1000 src_mac, &rdst->remote_ip, &src_ip);
1001
1002 rdst->remote_ip = *src_ip;
1003 f->updated = jiffies;
1004 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
1005 } else {
1006 /* learned new entry */
1007 spin_lock(&vxlan->hash_lock);
1008
1009 /* close off race between vxlan_flush and incoming packets */
1010 if (netif_running(dev))
1011 vxlan_fdb_create(vxlan, src_mac, src_ip,
1012 NUD_REACHABLE,
1013 NLM_F_EXCL|NLM_F_CREATE,
1014 vxlan->dst_port,
1015 vxlan->default_dst.remote_vni,
1016 0, NTF_SELF);
1017 spin_unlock(&vxlan->hash_lock);
1018 }
1019
1020 return false;
1021 }
1022
1023 /* See if multicast group is already in use by other ID */
1024 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1025 {
1026 struct vxlan_dev *vxlan;
1027
1028 /* The vxlan_sock is only used by dev, leaving group has
1029 * no effect on other vxlan devices.
1030 */
1031 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1032 return false;
1033
1034 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1035 if (!netif_running(vxlan->dev) || vxlan == dev)
1036 continue;
1037
1038 if (vxlan->vn_sock != dev->vn_sock)
1039 continue;
1040
1041 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1042 &dev->default_dst.remote_ip))
1043 continue;
1044
1045 if (vxlan->default_dst.remote_ifindex !=
1046 dev->default_dst.remote_ifindex)
1047 continue;
1048
1049 return true;
1050 }
1051
1052 return false;
1053 }
1054
1055 static void vxlan_sock_hold(struct vxlan_sock *vs)
1056 {
1057 atomic_inc(&vs->refcnt);
1058 }
1059
1060 void vxlan_sock_release(struct vxlan_sock *vs)
1061 {
1062 struct sock *sk = vs->sock->sk;
1063 struct net *net = sock_net(sk);
1064 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1065
1066 if (!atomic_dec_and_test(&vs->refcnt))
1067 return;
1068
1069 spin_lock(&vn->sock_lock);
1070 hlist_del_rcu(&vs->hlist);
1071 vxlan_notify_del_rx_port(vs);
1072 spin_unlock(&vn->sock_lock);
1073
1074 queue_work(vxlan_wq, &vs->del_work);
1075 }
1076 EXPORT_SYMBOL_GPL(vxlan_sock_release);
1077
1078 /* Callback to update multicast group membership when first VNI on
1079 * multicast asddress is brought up
1080 * Done as workqueue because ip_mc_join_group acquires RTNL.
1081 */
1082 static void vxlan_igmp_join(struct work_struct *work)
1083 {
1084 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
1085 struct vxlan_sock *vs = vxlan->vn_sock;
1086 struct sock *sk = vs->sock->sk;
1087 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1088 int ifindex = vxlan->default_dst.remote_ifindex;
1089
1090 lock_sock(sk);
1091 if (ip->sa.sa_family == AF_INET) {
1092 struct ip_mreqn mreq = {
1093 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1094 .imr_ifindex = ifindex,
1095 };
1096
1097 ip_mc_join_group(sk, &mreq);
1098 #if IS_ENABLED(CONFIG_IPV6)
1099 } else {
1100 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1101 &ip->sin6.sin6_addr);
1102 #endif
1103 }
1104 release_sock(sk);
1105
1106 vxlan_sock_release(vs);
1107 dev_put(vxlan->dev);
1108 }
1109
1110 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1111 static void vxlan_igmp_leave(struct work_struct *work)
1112 {
1113 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
1114 struct vxlan_sock *vs = vxlan->vn_sock;
1115 struct sock *sk = vs->sock->sk;
1116 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1117 int ifindex = vxlan->default_dst.remote_ifindex;
1118
1119 lock_sock(sk);
1120 if (ip->sa.sa_family == AF_INET) {
1121 struct ip_mreqn mreq = {
1122 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1123 .imr_ifindex = ifindex,
1124 };
1125
1126 ip_mc_leave_group(sk, &mreq);
1127 #if IS_ENABLED(CONFIG_IPV6)
1128 } else {
1129 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1130 &ip->sin6.sin6_addr);
1131 #endif
1132 }
1133
1134 release_sock(sk);
1135
1136 vxlan_sock_release(vs);
1137 dev_put(vxlan->dev);
1138 }
1139
1140 /* Callback from net/ipv4/udp.c to receive packets */
1141 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1142 {
1143 struct vxlan_sock *vs;
1144 struct vxlanhdr *vxh;
1145
1146 /* Need Vxlan and inner Ethernet header to be present */
1147 if (!pskb_may_pull(skb, VXLAN_HLEN))
1148 goto error;
1149
1150 /* Return packets with reserved bits set */
1151 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1152 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1153 (vxh->vx_vni & htonl(0xff))) {
1154 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1155 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1156 goto error;
1157 }
1158
1159 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1160 goto drop;
1161
1162 vs = rcu_dereference_sk_user_data(sk);
1163 if (!vs)
1164 goto drop;
1165
1166 vs->rcv(vs, skb, vxh->vx_vni);
1167 return 0;
1168
1169 drop:
1170 /* Consume bad packet */
1171 kfree_skb(skb);
1172 return 0;
1173
1174 error:
1175 /* Return non vxlan pkt */
1176 return 1;
1177 }
1178
1179 static void vxlan_rcv(struct vxlan_sock *vs,
1180 struct sk_buff *skb, __be32 vx_vni)
1181 {
1182 struct iphdr *oip = NULL;
1183 struct ipv6hdr *oip6 = NULL;
1184 struct vxlan_dev *vxlan;
1185 struct pcpu_sw_netstats *stats;
1186 union vxlan_addr saddr;
1187 __u32 vni;
1188 int err = 0;
1189 union vxlan_addr *remote_ip;
1190
1191 vni = ntohl(vx_vni) >> 8;
1192 /* Is this VNI defined? */
1193 vxlan = vxlan_vs_find_vni(vs, vni);
1194 if (!vxlan)
1195 goto drop;
1196
1197 remote_ip = &vxlan->default_dst.remote_ip;
1198 skb_reset_mac_header(skb);
1199 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
1200 skb->protocol = eth_type_trans(skb, vxlan->dev);
1201 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1202
1203 /* Ignore packet loops (and multicast echo) */
1204 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1205 goto drop;
1206
1207 /* Re-examine inner Ethernet packet */
1208 if (remote_ip->sa.sa_family == AF_INET) {
1209 oip = ip_hdr(skb);
1210 saddr.sin.sin_addr.s_addr = oip->saddr;
1211 saddr.sa.sa_family = AF_INET;
1212 #if IS_ENABLED(CONFIG_IPV6)
1213 } else {
1214 oip6 = ipv6_hdr(skb);
1215 saddr.sin6.sin6_addr = oip6->saddr;
1216 saddr.sa.sa_family = AF_INET6;
1217 #endif
1218 }
1219
1220 if ((vxlan->flags & VXLAN_F_LEARN) &&
1221 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1222 goto drop;
1223
1224 skb_reset_network_header(skb);
1225
1226 if (oip6)
1227 err = IP6_ECN_decapsulate(oip6, skb);
1228 if (oip)
1229 err = IP_ECN_decapsulate(oip, skb);
1230
1231 if (unlikely(err)) {
1232 if (log_ecn_error) {
1233 if (oip6)
1234 net_info_ratelimited("non-ECT from %pI6\n",
1235 &oip6->saddr);
1236 if (oip)
1237 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1238 &oip->saddr, oip->tos);
1239 }
1240 if (err > 1) {
1241 ++vxlan->dev->stats.rx_frame_errors;
1242 ++vxlan->dev->stats.rx_errors;
1243 goto drop;
1244 }
1245 }
1246
1247 stats = this_cpu_ptr(vxlan->dev->tstats);
1248 u64_stats_update_begin(&stats->syncp);
1249 stats->rx_packets++;
1250 stats->rx_bytes += skb->len;
1251 u64_stats_update_end(&stats->syncp);
1252
1253 netif_rx(skb);
1254
1255 return;
1256 drop:
1257 /* Consume bad packet */
1258 kfree_skb(skb);
1259 }
1260
1261 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1262 {
1263 struct vxlan_dev *vxlan = netdev_priv(dev);
1264 struct arphdr *parp;
1265 u8 *arpptr, *sha;
1266 __be32 sip, tip;
1267 struct neighbour *n;
1268
1269 if (dev->flags & IFF_NOARP)
1270 goto out;
1271
1272 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1273 dev->stats.tx_dropped++;
1274 goto out;
1275 }
1276 parp = arp_hdr(skb);
1277
1278 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1279 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1280 parp->ar_pro != htons(ETH_P_IP) ||
1281 parp->ar_op != htons(ARPOP_REQUEST) ||
1282 parp->ar_hln != dev->addr_len ||
1283 parp->ar_pln != 4)
1284 goto out;
1285 arpptr = (u8 *)parp + sizeof(struct arphdr);
1286 sha = arpptr;
1287 arpptr += dev->addr_len; /* sha */
1288 memcpy(&sip, arpptr, sizeof(sip));
1289 arpptr += sizeof(sip);
1290 arpptr += dev->addr_len; /* tha */
1291 memcpy(&tip, arpptr, sizeof(tip));
1292
1293 if (ipv4_is_loopback(tip) ||
1294 ipv4_is_multicast(tip))
1295 goto out;
1296
1297 n = neigh_lookup(&arp_tbl, &tip, dev);
1298
1299 if (n) {
1300 struct vxlan_fdb *f;
1301 struct sk_buff *reply;
1302
1303 if (!(n->nud_state & NUD_CONNECTED)) {
1304 neigh_release(n);
1305 goto out;
1306 }
1307
1308 f = vxlan_find_mac(vxlan, n->ha);
1309 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1310 /* bridge-local neighbor */
1311 neigh_release(n);
1312 goto out;
1313 }
1314
1315 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1316 n->ha, sha);
1317
1318 neigh_release(n);
1319
1320 if (reply == NULL)
1321 goto out;
1322
1323 skb_reset_mac_header(reply);
1324 __skb_pull(reply, skb_network_offset(reply));
1325 reply->ip_summed = CHECKSUM_UNNECESSARY;
1326 reply->pkt_type = PACKET_HOST;
1327
1328 if (netif_rx_ni(reply) == NET_RX_DROP)
1329 dev->stats.rx_dropped++;
1330 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1331 union vxlan_addr ipa = {
1332 .sin.sin_addr.s_addr = tip,
1333 .sin.sin_family = AF_INET,
1334 };
1335
1336 vxlan_ip_miss(dev, &ipa);
1337 }
1338 out:
1339 consume_skb(skb);
1340 return NETDEV_TX_OK;
1341 }
1342
1343 #if IS_ENABLED(CONFIG_IPV6)
1344 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1345 struct neighbour *n, bool isrouter)
1346 {
1347 struct net_device *dev = request->dev;
1348 struct sk_buff *reply;
1349 struct nd_msg *ns, *na;
1350 struct ipv6hdr *pip6;
1351 u8 *daddr;
1352 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1353 int ns_olen;
1354 int i, len;
1355
1356 if (dev == NULL)
1357 return NULL;
1358
1359 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1360 sizeof(*na) + na_olen + dev->needed_tailroom;
1361 reply = alloc_skb(len, GFP_ATOMIC);
1362 if (reply == NULL)
1363 return NULL;
1364
1365 reply->protocol = htons(ETH_P_IPV6);
1366 reply->dev = dev;
1367 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1368 skb_push(reply, sizeof(struct ethhdr));
1369 skb_set_mac_header(reply, 0);
1370
1371 ns = (struct nd_msg *)skb_transport_header(request);
1372
1373 daddr = eth_hdr(request)->h_source;
1374 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1375 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1376 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1377 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1378 break;
1379 }
1380 }
1381
1382 /* Ethernet header */
1383 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1384 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1385 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1386 reply->protocol = htons(ETH_P_IPV6);
1387
1388 skb_pull(reply, sizeof(struct ethhdr));
1389 skb_set_network_header(reply, 0);
1390 skb_put(reply, sizeof(struct ipv6hdr));
1391
1392 /* IPv6 header */
1393
1394 pip6 = ipv6_hdr(reply);
1395 memset(pip6, 0, sizeof(struct ipv6hdr));
1396 pip6->version = 6;
1397 pip6->priority = ipv6_hdr(request)->priority;
1398 pip6->nexthdr = IPPROTO_ICMPV6;
1399 pip6->hop_limit = 255;
1400 pip6->daddr = ipv6_hdr(request)->saddr;
1401 pip6->saddr = *(struct in6_addr *)n->primary_key;
1402
1403 skb_pull(reply, sizeof(struct ipv6hdr));
1404 skb_set_transport_header(reply, 0);
1405
1406 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1407
1408 /* Neighbor Advertisement */
1409 memset(na, 0, sizeof(*na)+na_olen);
1410 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1411 na->icmph.icmp6_router = isrouter;
1412 na->icmph.icmp6_override = 1;
1413 na->icmph.icmp6_solicited = 1;
1414 na->target = ns->target;
1415 ether_addr_copy(&na->opt[2], n->ha);
1416 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1417 na->opt[1] = na_olen >> 3;
1418
1419 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1420 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1421 csum_partial(na, sizeof(*na)+na_olen, 0));
1422
1423 pip6->payload_len = htons(sizeof(*na)+na_olen);
1424
1425 skb_push(reply, sizeof(struct ipv6hdr));
1426
1427 reply->ip_summed = CHECKSUM_UNNECESSARY;
1428
1429 return reply;
1430 }
1431
1432 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1433 {
1434 struct vxlan_dev *vxlan = netdev_priv(dev);
1435 struct nd_msg *msg;
1436 const struct ipv6hdr *iphdr;
1437 const struct in6_addr *saddr, *daddr;
1438 struct neighbour *n;
1439 struct inet6_dev *in6_dev;
1440
1441 in6_dev = __in6_dev_get(dev);
1442 if (!in6_dev)
1443 goto out;
1444
1445 iphdr = ipv6_hdr(skb);
1446 saddr = &iphdr->saddr;
1447 daddr = &iphdr->daddr;
1448
1449 msg = (struct nd_msg *)skb_transport_header(skb);
1450 if (msg->icmph.icmp6_code != 0 ||
1451 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1452 goto out;
1453
1454 if (ipv6_addr_loopback(daddr) ||
1455 ipv6_addr_is_multicast(&msg->target))
1456 goto out;
1457
1458 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1459
1460 if (n) {
1461 struct vxlan_fdb *f;
1462 struct sk_buff *reply;
1463
1464 if (!(n->nud_state & NUD_CONNECTED)) {
1465 neigh_release(n);
1466 goto out;
1467 }
1468
1469 f = vxlan_find_mac(vxlan, n->ha);
1470 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1471 /* bridge-local neighbor */
1472 neigh_release(n);
1473 goto out;
1474 }
1475
1476 reply = vxlan_na_create(skb, n,
1477 !!(f ? f->flags & NTF_ROUTER : 0));
1478
1479 neigh_release(n);
1480
1481 if (reply == NULL)
1482 goto out;
1483
1484 if (netif_rx_ni(reply) == NET_RX_DROP)
1485 dev->stats.rx_dropped++;
1486
1487 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1488 union vxlan_addr ipa = {
1489 .sin6.sin6_addr = msg->target,
1490 .sin6.sin6_family = AF_INET6,
1491 };
1492
1493 vxlan_ip_miss(dev, &ipa);
1494 }
1495
1496 out:
1497 consume_skb(skb);
1498 return NETDEV_TX_OK;
1499 }
1500 #endif
1501
1502 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1503 {
1504 struct vxlan_dev *vxlan = netdev_priv(dev);
1505 struct neighbour *n;
1506
1507 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1508 return false;
1509
1510 n = NULL;
1511 switch (ntohs(eth_hdr(skb)->h_proto)) {
1512 case ETH_P_IP:
1513 {
1514 struct iphdr *pip;
1515
1516 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1517 return false;
1518 pip = ip_hdr(skb);
1519 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1520 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1521 union vxlan_addr ipa = {
1522 .sin.sin_addr.s_addr = pip->daddr,
1523 .sin.sin_family = AF_INET,
1524 };
1525
1526 vxlan_ip_miss(dev, &ipa);
1527 return false;
1528 }
1529
1530 break;
1531 }
1532 #if IS_ENABLED(CONFIG_IPV6)
1533 case ETH_P_IPV6:
1534 {
1535 struct ipv6hdr *pip6;
1536
1537 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1538 return false;
1539 pip6 = ipv6_hdr(skb);
1540 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1541 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1542 union vxlan_addr ipa = {
1543 .sin6.sin6_addr = pip6->daddr,
1544 .sin6.sin6_family = AF_INET6,
1545 };
1546
1547 vxlan_ip_miss(dev, &ipa);
1548 return false;
1549 }
1550
1551 break;
1552 }
1553 #endif
1554 default:
1555 return false;
1556 }
1557
1558 if (n) {
1559 bool diff;
1560
1561 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1562 if (diff) {
1563 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1564 dev->addr_len);
1565 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1566 }
1567 neigh_release(n);
1568 return diff;
1569 }
1570
1571 return false;
1572 }
1573
1574 #if IS_ENABLED(CONFIG_IPV6)
1575 static int vxlan6_xmit_skb(struct vxlan_sock *vs,
1576 struct dst_entry *dst, struct sk_buff *skb,
1577 struct net_device *dev, struct in6_addr *saddr,
1578 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1579 __be16 src_port, __be16 dst_port, __be32 vni,
1580 bool xnet)
1581 {
1582 struct vxlanhdr *vxh;
1583 int min_headroom;
1584 int err;
1585 bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
1586
1587 skb = udp_tunnel_handle_offloads(skb, udp_sum);
1588 if (IS_ERR(skb))
1589 return -EINVAL;
1590
1591 skb_scrub_packet(skb, xnet);
1592
1593 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1594 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1595 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1596
1597 /* Need space for new headers (invalidates iph ptr) */
1598 err = skb_cow_head(skb, min_headroom);
1599 if (unlikely(err))
1600 return err;
1601
1602 if (vlan_tx_tag_present(skb)) {
1603 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
1604 vlan_tx_tag_get(skb));
1605 if (WARN_ON(!skb))
1606 return -ENOMEM;
1607
1608 skb->vlan_tci = 0;
1609 }
1610
1611 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1612 vxh->vx_flags = htonl(VXLAN_FLAGS);
1613 vxh->vx_vni = vni;
1614
1615 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1616
1617 udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
1618 ttl, src_port, dst_port);
1619 return 0;
1620 }
1621 #endif
1622
1623 int vxlan_xmit_skb(struct vxlan_sock *vs,
1624 struct rtable *rt, struct sk_buff *skb,
1625 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1626 __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)
1627 {
1628 struct vxlanhdr *vxh;
1629 int min_headroom;
1630 int err;
1631 bool udp_sum = !vs->sock->sk->sk_no_check_tx;
1632
1633 skb = udp_tunnel_handle_offloads(skb, udp_sum);
1634 if (IS_ERR(skb))
1635 return -EINVAL;
1636
1637 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1638 + VXLAN_HLEN + sizeof(struct iphdr)
1639 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1640
1641 /* Need space for new headers (invalidates iph ptr) */
1642 err = skb_cow_head(skb, min_headroom);
1643 if (unlikely(err))
1644 return err;
1645
1646 if (vlan_tx_tag_present(skb)) {
1647 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
1648 vlan_tx_tag_get(skb));
1649 if (WARN_ON(!skb))
1650 return -ENOMEM;
1651
1652 skb->vlan_tci = 0;
1653 }
1654
1655 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1656 vxh->vx_flags = htonl(VXLAN_FLAGS);
1657 vxh->vx_vni = vni;
1658
1659 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1660
1661 return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
1662 ttl, df, src_port, dst_port, xnet);
1663 }
1664 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1665
1666 /* Bypass encapsulation if the destination is local */
1667 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1668 struct vxlan_dev *dst_vxlan)
1669 {
1670 struct pcpu_sw_netstats *tx_stats, *rx_stats;
1671 union vxlan_addr loopback;
1672 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1673 struct net_device *dev = skb->dev;
1674 int len = skb->len;
1675
1676 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1677 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1678 skb->pkt_type = PACKET_HOST;
1679 skb->encapsulation = 0;
1680 skb->dev = dst_vxlan->dev;
1681 __skb_pull(skb, skb_network_offset(skb));
1682
1683 if (remote_ip->sa.sa_family == AF_INET) {
1684 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1685 loopback.sa.sa_family = AF_INET;
1686 #if IS_ENABLED(CONFIG_IPV6)
1687 } else {
1688 loopback.sin6.sin6_addr = in6addr_loopback;
1689 loopback.sa.sa_family = AF_INET6;
1690 #endif
1691 }
1692
1693 if (dst_vxlan->flags & VXLAN_F_LEARN)
1694 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1695
1696 u64_stats_update_begin(&tx_stats->syncp);
1697 tx_stats->tx_packets++;
1698 tx_stats->tx_bytes += len;
1699 u64_stats_update_end(&tx_stats->syncp);
1700
1701 if (netif_rx(skb) == NET_RX_SUCCESS) {
1702 u64_stats_update_begin(&rx_stats->syncp);
1703 rx_stats->rx_packets++;
1704 rx_stats->rx_bytes += len;
1705 u64_stats_update_end(&rx_stats->syncp);
1706 } else {
1707 dev->stats.rx_dropped++;
1708 }
1709 }
1710
1711 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1712 struct vxlan_rdst *rdst, bool did_rsc)
1713 {
1714 struct vxlan_dev *vxlan = netdev_priv(dev);
1715 struct rtable *rt = NULL;
1716 const struct iphdr *old_iph;
1717 struct flowi4 fl4;
1718 union vxlan_addr *dst;
1719 __be16 src_port = 0, dst_port;
1720 u32 vni;
1721 __be16 df = 0;
1722 __u8 tos, ttl;
1723 int err;
1724
1725 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1726 vni = rdst->remote_vni;
1727 dst = &rdst->remote_ip;
1728
1729 if (vxlan_addr_any(dst)) {
1730 if (did_rsc) {
1731 /* short-circuited back to local bridge */
1732 vxlan_encap_bypass(skb, vxlan, vxlan);
1733 return;
1734 }
1735 goto drop;
1736 }
1737
1738 old_iph = ip_hdr(skb);
1739
1740 ttl = vxlan->ttl;
1741 if (!ttl && vxlan_addr_multicast(dst))
1742 ttl = 1;
1743
1744 tos = vxlan->tos;
1745 if (tos == 1)
1746 tos = ip_tunnel_get_dsfield(old_iph, skb);
1747
1748 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1749 vxlan->port_max, true);
1750
1751 if (dst->sa.sa_family == AF_INET) {
1752 memset(&fl4, 0, sizeof(fl4));
1753 fl4.flowi4_oif = rdst->remote_ifindex;
1754 fl4.flowi4_tos = RT_TOS(tos);
1755 fl4.daddr = dst->sin.sin_addr.s_addr;
1756 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1757
1758 rt = ip_route_output_key(vxlan->net, &fl4);
1759 if (IS_ERR(rt)) {
1760 netdev_dbg(dev, "no route to %pI4\n",
1761 &dst->sin.sin_addr.s_addr);
1762 dev->stats.tx_carrier_errors++;
1763 goto tx_error;
1764 }
1765
1766 if (rt->dst.dev == dev) {
1767 netdev_dbg(dev, "circular route to %pI4\n",
1768 &dst->sin.sin_addr.s_addr);
1769 dev->stats.collisions++;
1770 goto rt_tx_error;
1771 }
1772
1773 /* Bypass encapsulation if the destination is local */
1774 if (rt->rt_flags & RTCF_LOCAL &&
1775 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1776 struct vxlan_dev *dst_vxlan;
1777
1778 ip_rt_put(rt);
1779 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1780 dst->sa.sa_family, dst_port);
1781 if (!dst_vxlan)
1782 goto tx_error;
1783 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1784 return;
1785 }
1786
1787 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1788 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1789
1790 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1791 fl4.saddr, dst->sin.sin_addr.s_addr,
1792 tos, ttl, df, src_port, dst_port,
1793 htonl(vni << 8),
1794 !net_eq(vxlan->net, dev_net(vxlan->dev)));
1795
1796 if (err < 0)
1797 goto rt_tx_error;
1798 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1799 #if IS_ENABLED(CONFIG_IPV6)
1800 } else {
1801 struct sock *sk = vxlan->vn_sock->sock->sk;
1802 struct dst_entry *ndst;
1803 struct flowi6 fl6;
1804 u32 flags;
1805
1806 memset(&fl6, 0, sizeof(fl6));
1807 fl6.flowi6_oif = rdst->remote_ifindex;
1808 fl6.daddr = dst->sin6.sin6_addr;
1809 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1810 fl6.flowi6_proto = IPPROTO_UDP;
1811
1812 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1813 netdev_dbg(dev, "no route to %pI6\n",
1814 &dst->sin6.sin6_addr);
1815 dev->stats.tx_carrier_errors++;
1816 goto tx_error;
1817 }
1818
1819 if (ndst->dev == dev) {
1820 netdev_dbg(dev, "circular route to %pI6\n",
1821 &dst->sin6.sin6_addr);
1822 dst_release(ndst);
1823 dev->stats.collisions++;
1824 goto tx_error;
1825 }
1826
1827 /* Bypass encapsulation if the destination is local */
1828 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1829 if (flags & RTF_LOCAL &&
1830 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1831 struct vxlan_dev *dst_vxlan;
1832
1833 dst_release(ndst);
1834 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1835 dst->sa.sa_family, dst_port);
1836 if (!dst_vxlan)
1837 goto tx_error;
1838 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1839 return;
1840 }
1841
1842 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1843
1844 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
1845 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1846 src_port, dst_port, htonl(vni << 8),
1847 !net_eq(vxlan->net, dev_net(vxlan->dev)));
1848 #endif
1849 }
1850
1851 return;
1852
1853 drop:
1854 dev->stats.tx_dropped++;
1855 goto tx_free;
1856
1857 rt_tx_error:
1858 ip_rt_put(rt);
1859 tx_error:
1860 dev->stats.tx_errors++;
1861 tx_free:
1862 dev_kfree_skb(skb);
1863 }
1864
1865 /* Transmit local packets over Vxlan
1866 *
1867 * Outer IP header inherits ECN and DF from inner header.
1868 * Outer UDP destination is the VXLAN assigned port.
1869 * source port is based on hash of flow
1870 */
1871 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1872 {
1873 struct vxlan_dev *vxlan = netdev_priv(dev);
1874 struct ethhdr *eth;
1875 bool did_rsc = false;
1876 struct vxlan_rdst *rdst, *fdst = NULL;
1877 struct vxlan_fdb *f;
1878
1879 skb_reset_mac_header(skb);
1880 eth = eth_hdr(skb);
1881
1882 if ((vxlan->flags & VXLAN_F_PROXY)) {
1883 if (ntohs(eth->h_proto) == ETH_P_ARP)
1884 return arp_reduce(dev, skb);
1885 #if IS_ENABLED(CONFIG_IPV6)
1886 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
1887 pskb_may_pull(skb, sizeof(struct ipv6hdr)
1888 + sizeof(struct nd_msg)) &&
1889 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1890 struct nd_msg *msg;
1891
1892 msg = (struct nd_msg *)skb_transport_header(skb);
1893 if (msg->icmph.icmp6_code == 0 &&
1894 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1895 return neigh_reduce(dev, skb);
1896 }
1897 eth = eth_hdr(skb);
1898 #endif
1899 }
1900
1901 f = vxlan_find_mac(vxlan, eth->h_dest);
1902 did_rsc = false;
1903
1904 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1905 (ntohs(eth->h_proto) == ETH_P_IP ||
1906 ntohs(eth->h_proto) == ETH_P_IPV6)) {
1907 did_rsc = route_shortcircuit(dev, skb);
1908 if (did_rsc)
1909 f = vxlan_find_mac(vxlan, eth->h_dest);
1910 }
1911
1912 if (f == NULL) {
1913 f = vxlan_find_mac(vxlan, all_zeros_mac);
1914 if (f == NULL) {
1915 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1916 !is_multicast_ether_addr(eth->h_dest))
1917 vxlan_fdb_miss(vxlan, eth->h_dest);
1918
1919 dev->stats.tx_dropped++;
1920 kfree_skb(skb);
1921 return NETDEV_TX_OK;
1922 }
1923 }
1924
1925 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1926 struct sk_buff *skb1;
1927
1928 if (!fdst) {
1929 fdst = rdst;
1930 continue;
1931 }
1932 skb1 = skb_clone(skb, GFP_ATOMIC);
1933 if (skb1)
1934 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1935 }
1936
1937 if (fdst)
1938 vxlan_xmit_one(skb, dev, fdst, did_rsc);
1939 else
1940 kfree_skb(skb);
1941 return NETDEV_TX_OK;
1942 }
1943
1944 /* Walk the forwarding table and purge stale entries */
1945 static void vxlan_cleanup(unsigned long arg)
1946 {
1947 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1948 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1949 unsigned int h;
1950
1951 if (!netif_running(vxlan->dev))
1952 return;
1953
1954 spin_lock_bh(&vxlan->hash_lock);
1955 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1956 struct hlist_node *p, *n;
1957 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1958 struct vxlan_fdb *f
1959 = container_of(p, struct vxlan_fdb, hlist);
1960 unsigned long timeout;
1961
1962 if (f->state & NUD_PERMANENT)
1963 continue;
1964
1965 timeout = f->used + vxlan->age_interval * HZ;
1966 if (time_before_eq(timeout, jiffies)) {
1967 netdev_dbg(vxlan->dev,
1968 "garbage collect %pM\n",
1969 f->eth_addr);
1970 f->state = NUD_STALE;
1971 vxlan_fdb_destroy(vxlan, f);
1972 } else if (time_before(timeout, next_timer))
1973 next_timer = timeout;
1974 }
1975 }
1976 spin_unlock_bh(&vxlan->hash_lock);
1977
1978 mod_timer(&vxlan->age_timer, next_timer);
1979 }
1980
1981 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1982 {
1983 __u32 vni = vxlan->default_dst.remote_vni;
1984
1985 vxlan->vn_sock = vs;
1986 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1987 }
1988
1989 /* Setup stats when device is created */
1990 static int vxlan_init(struct net_device *dev)
1991 {
1992 struct vxlan_dev *vxlan = netdev_priv(dev);
1993 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1994 struct vxlan_sock *vs;
1995 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
1996
1997 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1998 if (!dev->tstats)
1999 return -ENOMEM;
2000
2001 spin_lock(&vn->sock_lock);
2002 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2003 vxlan->dst_port);
2004 if (vs) {
2005 /* If we have a socket with same port already, reuse it */
2006 atomic_inc(&vs->refcnt);
2007 vxlan_vs_add_dev(vs, vxlan);
2008 } else {
2009 /* otherwise make new socket outside of RTNL */
2010 dev_hold(dev);
2011 queue_work(vxlan_wq, &vxlan->sock_work);
2012 }
2013 spin_unlock(&vn->sock_lock);
2014
2015 return 0;
2016 }
2017
2018 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2019 {
2020 struct vxlan_fdb *f;
2021
2022 spin_lock_bh(&vxlan->hash_lock);
2023 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2024 if (f)
2025 vxlan_fdb_destroy(vxlan, f);
2026 spin_unlock_bh(&vxlan->hash_lock);
2027 }
2028
2029 static void vxlan_uninit(struct net_device *dev)
2030 {
2031 struct vxlan_dev *vxlan = netdev_priv(dev);
2032 struct vxlan_sock *vs = vxlan->vn_sock;
2033
2034 vxlan_fdb_delete_default(vxlan);
2035
2036 if (vs)
2037 vxlan_sock_release(vs);
2038 free_percpu(dev->tstats);
2039 }
2040
2041 /* Start ageing timer and join group when device is brought up */
2042 static int vxlan_open(struct net_device *dev)
2043 {
2044 struct vxlan_dev *vxlan = netdev_priv(dev);
2045 struct vxlan_sock *vs = vxlan->vn_sock;
2046
2047 /* socket hasn't been created */
2048 if (!vs)
2049 return -ENOTCONN;
2050
2051 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2052 vxlan_sock_hold(vs);
2053 dev_hold(dev);
2054 queue_work(vxlan_wq, &vxlan->igmp_join);
2055 }
2056
2057 if (vxlan->age_interval)
2058 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2059
2060 return 0;
2061 }
2062
2063 /* Purge the forwarding table */
2064 static void vxlan_flush(struct vxlan_dev *vxlan)
2065 {
2066 unsigned int h;
2067
2068 spin_lock_bh(&vxlan->hash_lock);
2069 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2070 struct hlist_node *p, *n;
2071 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2072 struct vxlan_fdb *f
2073 = container_of(p, struct vxlan_fdb, hlist);
2074 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2075 if (!is_zero_ether_addr(f->eth_addr))
2076 vxlan_fdb_destroy(vxlan, f);
2077 }
2078 }
2079 spin_unlock_bh(&vxlan->hash_lock);
2080 }
2081
2082 /* Cleanup timer and forwarding table on shutdown */
2083 static int vxlan_stop(struct net_device *dev)
2084 {
2085 struct vxlan_dev *vxlan = netdev_priv(dev);
2086 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2087 struct vxlan_sock *vs = vxlan->vn_sock;
2088
2089 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2090 !vxlan_group_used(vn, vxlan)) {
2091 vxlan_sock_hold(vs);
2092 dev_hold(dev);
2093 queue_work(vxlan_wq, &vxlan->igmp_leave);
2094 }
2095
2096 del_timer_sync(&vxlan->age_timer);
2097
2098 vxlan_flush(vxlan);
2099
2100 return 0;
2101 }
2102
2103 /* Stub, nothing needs to be done. */
2104 static void vxlan_set_multicast_list(struct net_device *dev)
2105 {
2106 }
2107
2108 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2109 {
2110 struct vxlan_dev *vxlan = netdev_priv(dev);
2111 struct vxlan_rdst *dst = &vxlan->default_dst;
2112 struct net_device *lowerdev;
2113 int max_mtu;
2114
2115 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
2116 if (lowerdev == NULL)
2117 return eth_change_mtu(dev, new_mtu);
2118
2119 if (dst->remote_ip.sa.sa_family == AF_INET6)
2120 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2121 else
2122 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2123
2124 if (new_mtu < 68 || new_mtu > max_mtu)
2125 return -EINVAL;
2126
2127 dev->mtu = new_mtu;
2128 return 0;
2129 }
2130
2131 static const struct net_device_ops vxlan_netdev_ops = {
2132 .ndo_init = vxlan_init,
2133 .ndo_uninit = vxlan_uninit,
2134 .ndo_open = vxlan_open,
2135 .ndo_stop = vxlan_stop,
2136 .ndo_start_xmit = vxlan_xmit,
2137 .ndo_get_stats64 = ip_tunnel_get_stats64,
2138 .ndo_set_rx_mode = vxlan_set_multicast_list,
2139 .ndo_change_mtu = vxlan_change_mtu,
2140 .ndo_validate_addr = eth_validate_addr,
2141 .ndo_set_mac_address = eth_mac_addr,
2142 .ndo_fdb_add = vxlan_fdb_add,
2143 .ndo_fdb_del = vxlan_fdb_delete,
2144 .ndo_fdb_dump = vxlan_fdb_dump,
2145 };
2146
2147 /* Info for udev, that this is a virtual tunnel endpoint */
2148 static struct device_type vxlan_type = {
2149 .name = "vxlan",
2150 };
2151
2152 /* Calls the ndo_add_vxlan_port of the caller in order to
2153 * supply the listening VXLAN udp ports. Callers are expected
2154 * to implement the ndo_add_vxlan_port.
2155 */
2156 void vxlan_get_rx_port(struct net_device *dev)
2157 {
2158 struct vxlan_sock *vs;
2159 struct net *net = dev_net(dev);
2160 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2161 sa_family_t sa_family;
2162 __be16 port;
2163 unsigned int i;
2164
2165 spin_lock(&vn->sock_lock);
2166 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2167 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2168 port = inet_sk(vs->sock->sk)->inet_sport;
2169 sa_family = vs->sock->sk->sk_family;
2170 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2171 port);
2172 }
2173 }
2174 spin_unlock(&vn->sock_lock);
2175 }
2176 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2177
2178 /* Initialize the device structure. */
2179 static void vxlan_setup(struct net_device *dev)
2180 {
2181 struct vxlan_dev *vxlan = netdev_priv(dev);
2182 unsigned int h;
2183
2184 eth_hw_addr_random(dev);
2185 ether_setup(dev);
2186 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2187 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
2188 else
2189 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
2190
2191 dev->netdev_ops = &vxlan_netdev_ops;
2192 dev->destructor = free_netdev;
2193 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2194
2195 dev->tx_queue_len = 0;
2196 dev->features |= NETIF_F_LLTX;
2197 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2198 dev->features |= NETIF_F_RXCSUM;
2199 dev->features |= NETIF_F_GSO_SOFTWARE;
2200
2201 dev->vlan_features = dev->features;
2202 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2203 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2204 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2205 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2206 netif_keep_dst(dev);
2207 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2208
2209 INIT_LIST_HEAD(&vxlan->next);
2210 spin_lock_init(&vxlan->hash_lock);
2211 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2212 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
2213 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
2214
2215 init_timer_deferrable(&vxlan->age_timer);
2216 vxlan->age_timer.function = vxlan_cleanup;
2217 vxlan->age_timer.data = (unsigned long) vxlan;
2218
2219 vxlan->dst_port = htons(vxlan_port);
2220
2221 vxlan->dev = dev;
2222
2223 for (h = 0; h < FDB_HASH_SIZE; ++h)
2224 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2225 }
2226
2227 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2228 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2229 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2230 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2231 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2232 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2233 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2234 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2235 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2236 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2237 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2238 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2239 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2240 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2241 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2242 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2243 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2244 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2245 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2246 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2247 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
2248 };
2249
2250 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2251 {
2252 if (tb[IFLA_ADDRESS]) {
2253 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2254 pr_debug("invalid link address (not ethernet)\n");
2255 return -EINVAL;
2256 }
2257
2258 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2259 pr_debug("invalid all zero ethernet address\n");
2260 return -EADDRNOTAVAIL;
2261 }
2262 }
2263
2264 if (!data)
2265 return -EINVAL;
2266
2267 if (data[IFLA_VXLAN_ID]) {
2268 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2269 if (id >= VXLAN_VID_MASK)
2270 return -ERANGE;
2271 }
2272
2273 if (data[IFLA_VXLAN_PORT_RANGE]) {
2274 const struct ifla_vxlan_port_range *p
2275 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2276
2277 if (ntohs(p->high) < ntohs(p->low)) {
2278 pr_debug("port range %u .. %u not valid\n",
2279 ntohs(p->low), ntohs(p->high));
2280 return -EINVAL;
2281 }
2282 }
2283
2284 return 0;
2285 }
2286
2287 static void vxlan_get_drvinfo(struct net_device *netdev,
2288 struct ethtool_drvinfo *drvinfo)
2289 {
2290 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2291 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2292 }
2293
2294 static const struct ethtool_ops vxlan_ethtool_ops = {
2295 .get_drvinfo = vxlan_get_drvinfo,
2296 .get_link = ethtool_op_get_link,
2297 };
2298
2299 static void vxlan_del_work(struct work_struct *work)
2300 {
2301 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2302 udp_tunnel_sock_release(vs->sock);
2303 kfree_rcu(vs, rcu);
2304 }
2305
2306 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2307 __be16 port, u32 flags)
2308 {
2309 struct socket *sock;
2310 struct udp_port_cfg udp_conf;
2311 int err;
2312
2313 memset(&udp_conf, 0, sizeof(udp_conf));
2314
2315 if (ipv6) {
2316 udp_conf.family = AF_INET6;
2317 udp_conf.use_udp6_tx_checksums =
2318 !!(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2319 udp_conf.use_udp6_rx_checksums =
2320 !!(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2321 } else {
2322 udp_conf.family = AF_INET;
2323 udp_conf.local_ip.s_addr = INADDR_ANY;
2324 udp_conf.use_udp_checksums =
2325 !!(flags & VXLAN_F_UDP_CSUM);
2326 }
2327
2328 udp_conf.local_udp_port = port;
2329
2330 /* Open UDP socket */
2331 err = udp_sock_create(net, &udp_conf, &sock);
2332 if (err < 0)
2333 return ERR_PTR(err);
2334
2335 return sock;
2336 }
2337
2338 /* Create new listen socket if needed */
2339 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2340 vxlan_rcv_t *rcv, void *data,
2341 u32 flags)
2342 {
2343 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2344 struct vxlan_sock *vs;
2345 struct socket *sock;
2346 unsigned int h;
2347 bool ipv6 = !!(flags & VXLAN_F_IPV6);
2348 struct udp_tunnel_sock_cfg tunnel_cfg;
2349
2350 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2351 if (!vs)
2352 return ERR_PTR(-ENOMEM);
2353
2354 for (h = 0; h < VNI_HASH_SIZE; ++h)
2355 INIT_HLIST_HEAD(&vs->vni_list[h]);
2356
2357 INIT_WORK(&vs->del_work, vxlan_del_work);
2358
2359 sock = vxlan_create_sock(net, ipv6, port, flags);
2360 if (IS_ERR(sock)) {
2361 kfree(vs);
2362 return ERR_CAST(sock);
2363 }
2364
2365 vs->sock = sock;
2366 atomic_set(&vs->refcnt, 1);
2367 vs->rcv = rcv;
2368 vs->data = data;
2369
2370 /* Initialize the vxlan udp offloads structure */
2371 vs->udp_offloads.port = port;
2372 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2373 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2374
2375 spin_lock(&vn->sock_lock);
2376 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2377 vxlan_notify_add_rx_port(vs);
2378 spin_unlock(&vn->sock_lock);
2379
2380 /* Mark socket as an encapsulation socket. */
2381 tunnel_cfg.sk_user_data = vs;
2382 tunnel_cfg.encap_type = 1;
2383 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2384 tunnel_cfg.encap_destroy = NULL;
2385
2386 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2387
2388 return vs;
2389 }
2390
2391 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2392 vxlan_rcv_t *rcv, void *data,
2393 bool no_share, u32 flags)
2394 {
2395 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2396 struct vxlan_sock *vs;
2397 bool ipv6 = flags & VXLAN_F_IPV6;
2398
2399 vs = vxlan_socket_create(net, port, rcv, data, flags);
2400 if (!IS_ERR(vs))
2401 return vs;
2402
2403 if (no_share) /* Return error if sharing is not allowed. */
2404 return vs;
2405
2406 spin_lock(&vn->sock_lock);
2407 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port);
2408 if (vs) {
2409 if (vs->rcv == rcv)
2410 atomic_inc(&vs->refcnt);
2411 else
2412 vs = ERR_PTR(-EBUSY);
2413 }
2414 spin_unlock(&vn->sock_lock);
2415
2416 if (!vs)
2417 vs = ERR_PTR(-EINVAL);
2418
2419 return vs;
2420 }
2421 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2422
2423 /* Scheduled at device creation to bind to a socket */
2424 static void vxlan_sock_work(struct work_struct *work)
2425 {
2426 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2427 struct net *net = vxlan->net;
2428 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2429 __be16 port = vxlan->dst_port;
2430 struct vxlan_sock *nvs;
2431
2432 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
2433 spin_lock(&vn->sock_lock);
2434 if (!IS_ERR(nvs))
2435 vxlan_vs_add_dev(nvs, vxlan);
2436 spin_unlock(&vn->sock_lock);
2437
2438 dev_put(vxlan->dev);
2439 }
2440
2441 static int vxlan_newlink(struct net *net, struct net_device *dev,
2442 struct nlattr *tb[], struct nlattr *data[])
2443 {
2444 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2445 struct vxlan_dev *vxlan = netdev_priv(dev);
2446 struct vxlan_rdst *dst = &vxlan->default_dst;
2447 __u32 vni;
2448 int err;
2449 bool use_ipv6 = false;
2450
2451 if (!data[IFLA_VXLAN_ID])
2452 return -EINVAL;
2453
2454 vxlan->net = dev_net(dev);
2455
2456 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2457 dst->remote_vni = vni;
2458
2459 /* Unless IPv6 is explicitly requested, assume IPv4 */
2460 dst->remote_ip.sa.sa_family = AF_INET;
2461 if (data[IFLA_VXLAN_GROUP]) {
2462 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2463 } else if (data[IFLA_VXLAN_GROUP6]) {
2464 if (!IS_ENABLED(CONFIG_IPV6))
2465 return -EPFNOSUPPORT;
2466
2467 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2468 sizeof(struct in6_addr));
2469 dst->remote_ip.sa.sa_family = AF_INET6;
2470 use_ipv6 = true;
2471 }
2472
2473 if (data[IFLA_VXLAN_LOCAL]) {
2474 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2475 vxlan->saddr.sa.sa_family = AF_INET;
2476 } else if (data[IFLA_VXLAN_LOCAL6]) {
2477 if (!IS_ENABLED(CONFIG_IPV6))
2478 return -EPFNOSUPPORT;
2479
2480 /* TODO: respect scope id */
2481 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2482 sizeof(struct in6_addr));
2483 vxlan->saddr.sa.sa_family = AF_INET6;
2484 use_ipv6 = true;
2485 }
2486
2487 if (data[IFLA_VXLAN_LINK] &&
2488 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
2489 struct net_device *lowerdev
2490 = __dev_get_by_index(net, dst->remote_ifindex);
2491
2492 if (!lowerdev) {
2493 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2494 return -ENODEV;
2495 }
2496
2497 #if IS_ENABLED(CONFIG_IPV6)
2498 if (use_ipv6) {
2499 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2500 if (idev && idev->cnf.disable_ipv6) {
2501 pr_info("IPv6 is disabled via sysctl\n");
2502 return -EPERM;
2503 }
2504 vxlan->flags |= VXLAN_F_IPV6;
2505 }
2506 #endif
2507
2508 if (!tb[IFLA_MTU])
2509 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2510
2511 dev->needed_headroom = lowerdev->hard_header_len +
2512 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2513 } else if (use_ipv6)
2514 vxlan->flags |= VXLAN_F_IPV6;
2515
2516 if (data[IFLA_VXLAN_TOS])
2517 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2518
2519 if (data[IFLA_VXLAN_TTL])
2520 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2521
2522 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2523 vxlan->flags |= VXLAN_F_LEARN;
2524
2525 if (data[IFLA_VXLAN_AGEING])
2526 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2527 else
2528 vxlan->age_interval = FDB_AGE_DEFAULT;
2529
2530 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2531 vxlan->flags |= VXLAN_F_PROXY;
2532
2533 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2534 vxlan->flags |= VXLAN_F_RSC;
2535
2536 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2537 vxlan->flags |= VXLAN_F_L2MISS;
2538
2539 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2540 vxlan->flags |= VXLAN_F_L3MISS;
2541
2542 if (data[IFLA_VXLAN_LIMIT])
2543 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2544
2545 if (data[IFLA_VXLAN_PORT_RANGE]) {
2546 const struct ifla_vxlan_port_range *p
2547 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2548 vxlan->port_min = ntohs(p->low);
2549 vxlan->port_max = ntohs(p->high);
2550 }
2551
2552 if (data[IFLA_VXLAN_PORT])
2553 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2554
2555 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2556 vxlan->flags |= VXLAN_F_UDP_CSUM;
2557
2558 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2559 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2560 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2561
2562 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2563 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2564 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2565
2566 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
2567 vxlan->dst_port)) {
2568 pr_info("duplicate VNI %u\n", vni);
2569 return -EEXIST;
2570 }
2571
2572 dev->ethtool_ops = &vxlan_ethtool_ops;
2573
2574 /* create an fdb entry for a valid default destination */
2575 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2576 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2577 &vxlan->default_dst.remote_ip,
2578 NUD_REACHABLE|NUD_PERMANENT,
2579 NLM_F_EXCL|NLM_F_CREATE,
2580 vxlan->dst_port,
2581 vxlan->default_dst.remote_vni,
2582 vxlan->default_dst.remote_ifindex,
2583 NTF_SELF);
2584 if (err)
2585 return err;
2586 }
2587
2588 err = register_netdevice(dev);
2589 if (err) {
2590 vxlan_fdb_delete_default(vxlan);
2591 return err;
2592 }
2593
2594 list_add(&vxlan->next, &vn->vxlan_list);
2595
2596 return 0;
2597 }
2598
2599 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2600 {
2601 struct vxlan_dev *vxlan = netdev_priv(dev);
2602 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2603
2604 spin_lock(&vn->sock_lock);
2605 if (!hlist_unhashed(&vxlan->hlist))
2606 hlist_del_rcu(&vxlan->hlist);
2607 spin_unlock(&vn->sock_lock);
2608
2609 list_del(&vxlan->next);
2610 unregister_netdevice_queue(dev, head);
2611 }
2612
2613 static size_t vxlan_get_size(const struct net_device *dev)
2614 {
2615
2616 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
2617 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2618 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2619 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2620 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2621 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2622 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
2623 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2624 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2625 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2626 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
2627 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2628 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2629 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2630 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2631 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2632 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2633 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2634 0;
2635 }
2636
2637 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2638 {
2639 const struct vxlan_dev *vxlan = netdev_priv(dev);
2640 const struct vxlan_rdst *dst = &vxlan->default_dst;
2641 struct ifla_vxlan_port_range ports = {
2642 .low = htons(vxlan->port_min),
2643 .high = htons(vxlan->port_max),
2644 };
2645
2646 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2647 goto nla_put_failure;
2648
2649 if (!vxlan_addr_any(&dst->remote_ip)) {
2650 if (dst->remote_ip.sa.sa_family == AF_INET) {
2651 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2652 dst->remote_ip.sin.sin_addr.s_addr))
2653 goto nla_put_failure;
2654 #if IS_ENABLED(CONFIG_IPV6)
2655 } else {
2656 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2657 &dst->remote_ip.sin6.sin6_addr))
2658 goto nla_put_failure;
2659 #endif
2660 }
2661 }
2662
2663 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2664 goto nla_put_failure;
2665
2666 if (!vxlan_addr_any(&vxlan->saddr)) {
2667 if (vxlan->saddr.sa.sa_family == AF_INET) {
2668 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2669 vxlan->saddr.sin.sin_addr.s_addr))
2670 goto nla_put_failure;
2671 #if IS_ENABLED(CONFIG_IPV6)
2672 } else {
2673 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2674 &vxlan->saddr.sin6.sin6_addr))
2675 goto nla_put_failure;
2676 #endif
2677 }
2678 }
2679
2680 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2681 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
2682 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2683 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2684 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2685 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2686 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2687 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2688 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2689 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2690 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2691 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
2692 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2693 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2694 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2695 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2696 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2697 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2698 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2699 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)))
2700 goto nla_put_failure;
2701
2702 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2703 goto nla_put_failure;
2704
2705 return 0;
2706
2707 nla_put_failure:
2708 return -EMSGSIZE;
2709 }
2710
2711 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2712 .kind = "vxlan",
2713 .maxtype = IFLA_VXLAN_MAX,
2714 .policy = vxlan_policy,
2715 .priv_size = sizeof(struct vxlan_dev),
2716 .setup = vxlan_setup,
2717 .validate = vxlan_validate,
2718 .newlink = vxlan_newlink,
2719 .dellink = vxlan_dellink,
2720 .get_size = vxlan_get_size,
2721 .fill_info = vxlan_fill_info,
2722 };
2723
2724 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2725 struct net_device *dev)
2726 {
2727 struct vxlan_dev *vxlan, *next;
2728 LIST_HEAD(list_kill);
2729
2730 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2731 struct vxlan_rdst *dst = &vxlan->default_dst;
2732
2733 /* In case we created vxlan device with carrier
2734 * and we loose the carrier due to module unload
2735 * we also need to remove vxlan device. In other
2736 * cases, it's not necessary and remote_ifindex
2737 * is 0 here, so no matches.
2738 */
2739 if (dst->remote_ifindex == dev->ifindex)
2740 vxlan_dellink(vxlan->dev, &list_kill);
2741 }
2742
2743 unregister_netdevice_many(&list_kill);
2744 }
2745
2746 static int vxlan_lowerdev_event(struct notifier_block *unused,
2747 unsigned long event, void *ptr)
2748 {
2749 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2750 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2751
2752 if (event == NETDEV_UNREGISTER)
2753 vxlan_handle_lowerdev_unregister(vn, dev);
2754
2755 return NOTIFY_DONE;
2756 }
2757
2758 static struct notifier_block vxlan_notifier_block __read_mostly = {
2759 .notifier_call = vxlan_lowerdev_event,
2760 };
2761
2762 static __net_init int vxlan_init_net(struct net *net)
2763 {
2764 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2765 unsigned int h;
2766
2767 INIT_LIST_HEAD(&vn->vxlan_list);
2768 spin_lock_init(&vn->sock_lock);
2769
2770 for (h = 0; h < PORT_HASH_SIZE; ++h)
2771 INIT_HLIST_HEAD(&vn->sock_list[h]);
2772
2773 return 0;
2774 }
2775
2776 static void __net_exit vxlan_exit_net(struct net *net)
2777 {
2778 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2779 struct vxlan_dev *vxlan, *next;
2780 struct net_device *dev, *aux;
2781 LIST_HEAD(list);
2782
2783 rtnl_lock();
2784 for_each_netdev_safe(net, dev, aux)
2785 if (dev->rtnl_link_ops == &vxlan_link_ops)
2786 unregister_netdevice_queue(dev, &list);
2787
2788 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2789 /* If vxlan->dev is in the same netns, it has already been added
2790 * to the list by the previous loop.
2791 */
2792 if (!net_eq(dev_net(vxlan->dev), net))
2793 unregister_netdevice_queue(dev, &list);
2794 }
2795
2796 unregister_netdevice_many(&list);
2797 rtnl_unlock();
2798 }
2799
2800 static struct pernet_operations vxlan_net_ops = {
2801 .init = vxlan_init_net,
2802 .exit = vxlan_exit_net,
2803 .id = &vxlan_net_id,
2804 .size = sizeof(struct vxlan_net),
2805 };
2806
2807 static int __init vxlan_init_module(void)
2808 {
2809 int rc;
2810
2811 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2812 if (!vxlan_wq)
2813 return -ENOMEM;
2814
2815 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2816
2817 rc = register_pernet_subsys(&vxlan_net_ops);
2818 if (rc)
2819 goto out1;
2820
2821 rc = register_netdevice_notifier(&vxlan_notifier_block);
2822 if (rc)
2823 goto out2;
2824
2825 rc = rtnl_link_register(&vxlan_link_ops);
2826 if (rc)
2827 goto out3;
2828
2829 return 0;
2830 out3:
2831 unregister_netdevice_notifier(&vxlan_notifier_block);
2832 out2:
2833 unregister_pernet_subsys(&vxlan_net_ops);
2834 out1:
2835 destroy_workqueue(vxlan_wq);
2836 return rc;
2837 }
2838 late_initcall(vxlan_init_module);
2839
2840 static void __exit vxlan_cleanup_module(void)
2841 {
2842 rtnl_link_unregister(&vxlan_link_ops);
2843 unregister_netdevice_notifier(&vxlan_notifier_block);
2844 destroy_workqueue(vxlan_wq);
2845 unregister_pernet_subsys(&vxlan_net_ops);
2846 /* rcu_barrier() is called by netns */
2847 }
2848 module_exit(vxlan_cleanup_module);
2849
2850 MODULE_LICENSE("GPL");
2851 MODULE_VERSION(VXLAN_VERSION);
2852 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2853 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
2854 MODULE_ALIAS_RTNL_LINK("vxlan");
This page took 0.104518 seconds and 5 git commands to generate.