usb: renesas_usbhs: disable TX IRQ before starting TX DMAC transfer
[linux/fpc-iii.git] / drivers / net / vxlan.c
blob74dccfa00a5ca340abf3d8cc53bf944f748c890e
1 /*
2 * VXLAN: Virtual eXtensible Local Area Network
4 * Copyright (c) 2012-2013 Vyatta Inc.
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 */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/rtnetlink.h>
37 #include <net/route.h>
38 #include <net/dsfield.h>
39 #include <net/inet_ecn.h>
40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h>
42 #include <net/vxlan.h>
43 #include <net/protocol.h>
44 #if IS_ENABLED(CONFIG_IPV6)
45 #include <net/ipv6.h>
46 #include <net/addrconf.h>
47 #include <net/ip6_tunnel.h>
48 #include <net/ip6_checksum.h>
49 #endif
51 #define VXLAN_VERSION "0.1"
53 #define PORT_HASH_BITS 8
54 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
55 #define VNI_HASH_BITS 10
56 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
57 #define FDB_HASH_BITS 8
58 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
59 #define FDB_AGE_DEFAULT 300 /* 5 min */
60 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
62 #define VXLAN_N_VID (1u << 24)
63 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
64 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
66 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
68 /* VXLAN protocol header */
69 struct vxlanhdr {
70 __be32 vx_flags;
71 __be32 vx_vni;
74 /* UDP port for VXLAN traffic.
75 * The IANA assigned port is 4789, but the Linux default is 8472
76 * for compatibility with early adopters.
78 static unsigned short vxlan_port __read_mostly = 8472;
79 module_param_named(udp_port, vxlan_port, ushort, 0444);
80 MODULE_PARM_DESC(udp_port, "Destination UDP port");
82 static bool log_ecn_error = true;
83 module_param(log_ecn_error, bool, 0644);
84 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
86 static int vxlan_net_id;
88 static const u8 all_zeros_mac[ETH_ALEN];
90 /* per-network namespace private data for this module */
91 struct vxlan_net {
92 struct list_head vxlan_list;
93 struct hlist_head sock_list[PORT_HASH_SIZE];
94 spinlock_t sock_lock;
97 union vxlan_addr {
98 struct sockaddr_in sin;
99 struct sockaddr_in6 sin6;
100 struct sockaddr sa;
103 struct vxlan_rdst {
104 union vxlan_addr remote_ip;
105 __be16 remote_port;
106 u32 remote_vni;
107 u32 remote_ifindex;
108 struct list_head list;
109 struct rcu_head rcu;
112 /* Forwarding table entry */
113 struct vxlan_fdb {
114 struct hlist_node hlist; /* linked list of entries */
115 struct rcu_head rcu;
116 unsigned long updated; /* jiffies */
117 unsigned long used;
118 struct list_head remotes;
119 u16 state; /* see ndm_state */
120 u8 flags; /* see ndm_flags */
121 u8 eth_addr[ETH_ALEN];
124 /* Pseudo network device */
125 struct vxlan_dev {
126 struct hlist_node hlist; /* vni hash table */
127 struct list_head next; /* vxlan's per namespace list */
128 struct vxlan_sock *vn_sock; /* listening socket */
129 struct net_device *dev;
130 struct net *net; /* netns for packet i/o */
131 struct vxlan_rdst default_dst; /* default destination */
132 union vxlan_addr saddr; /* source address */
133 __be16 dst_port;
134 __u16 port_min; /* source port range */
135 __u16 port_max;
136 __u8 tos; /* TOS override */
137 __u8 ttl;
138 u32 flags; /* VXLAN_F_* in vxlan.h */
140 struct work_struct sock_work;
141 struct work_struct igmp_join;
142 struct work_struct igmp_leave;
144 unsigned long age_interval;
145 struct timer_list age_timer;
146 spinlock_t hash_lock;
147 unsigned int addrcnt;
148 unsigned int addrmax;
150 struct hlist_head fdb_head[FDB_HASH_SIZE];
153 /* salt for hash table */
154 static u32 vxlan_salt __read_mostly;
155 static struct workqueue_struct *vxlan_wq;
157 static void vxlan_sock_work(struct work_struct *work);
159 #if IS_ENABLED(CONFIG_IPV6)
160 static inline
161 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
163 if (a->sa.sa_family != b->sa.sa_family)
164 return false;
165 if (a->sa.sa_family == AF_INET6)
166 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
167 else
168 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
171 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
173 if (ipa->sa.sa_family == AF_INET6)
174 return ipv6_addr_any(&ipa->sin6.sin6_addr);
175 else
176 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
179 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
181 if (ipa->sa.sa_family == AF_INET6)
182 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
183 else
184 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
187 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
189 if (nla_len(nla) >= sizeof(struct in6_addr)) {
190 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
191 ip->sa.sa_family = AF_INET6;
192 return 0;
193 } else if (nla_len(nla) >= sizeof(__be32)) {
194 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
195 ip->sa.sa_family = AF_INET;
196 return 0;
197 } else {
198 return -EAFNOSUPPORT;
202 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
203 const union vxlan_addr *ip)
205 if (ip->sa.sa_family == AF_INET6)
206 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
207 else
208 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
211 #else /* !CONFIG_IPV6 */
213 static inline
214 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
216 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
219 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
221 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
224 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
226 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
229 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
231 if (nla_len(nla) >= sizeof(struct in6_addr)) {
232 return -EAFNOSUPPORT;
233 } else if (nla_len(nla) >= sizeof(__be32)) {
234 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
235 ip->sa.sa_family = AF_INET;
236 return 0;
237 } else {
238 return -EAFNOSUPPORT;
242 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
243 const union vxlan_addr *ip)
245 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
247 #endif
249 /* Virtual Network hash table head */
250 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
252 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
255 /* Socket hash table head */
256 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
258 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
260 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
263 /* First remote destination for a forwarding entry.
264 * Guaranteed to be non-NULL because remotes are never deleted.
266 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
268 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
271 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
273 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
276 /* Find VXLAN socket based on network namespace, address family and UDP port */
277 static struct vxlan_sock *vxlan_find_sock(struct net *net,
278 sa_family_t family, __be16 port)
280 struct vxlan_sock *vs;
282 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
283 if (inet_sk(vs->sock->sk)->inet_sport == port &&
284 inet_sk(vs->sock->sk)->sk.sk_family == family)
285 return vs;
287 return NULL;
290 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
292 struct vxlan_dev *vxlan;
294 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
295 if (vxlan->default_dst.remote_vni == id)
296 return vxlan;
299 return NULL;
302 /* Look up VNI in a per net namespace table */
303 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
304 sa_family_t family, __be16 port)
306 struct vxlan_sock *vs;
308 vs = vxlan_find_sock(net, family, port);
309 if (!vs)
310 return NULL;
312 return vxlan_vs_find_vni(vs, id);
315 /* Fill in neighbour message in skbuff. */
316 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
317 const struct vxlan_fdb *fdb,
318 u32 portid, u32 seq, int type, unsigned int flags,
319 const struct vxlan_rdst *rdst)
321 unsigned long now = jiffies;
322 struct nda_cacheinfo ci;
323 struct nlmsghdr *nlh;
324 struct ndmsg *ndm;
325 bool send_ip, send_eth;
327 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
328 if (nlh == NULL)
329 return -EMSGSIZE;
331 ndm = nlmsg_data(nlh);
332 memset(ndm, 0, sizeof(*ndm));
334 send_eth = send_ip = true;
336 if (type == RTM_GETNEIGH) {
337 ndm->ndm_family = AF_INET;
338 send_ip = !vxlan_addr_any(&rdst->remote_ip);
339 send_eth = !is_zero_ether_addr(fdb->eth_addr);
340 } else
341 ndm->ndm_family = AF_BRIDGE;
342 ndm->ndm_state = fdb->state;
343 ndm->ndm_ifindex = vxlan->dev->ifindex;
344 ndm->ndm_flags = fdb->flags;
345 ndm->ndm_type = RTN_UNICAST;
347 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
348 goto nla_put_failure;
350 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
351 goto nla_put_failure;
353 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
354 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
355 goto nla_put_failure;
356 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
357 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
358 goto nla_put_failure;
359 if (rdst->remote_ifindex &&
360 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
361 goto nla_put_failure;
363 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
364 ci.ndm_confirmed = 0;
365 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
366 ci.ndm_refcnt = 0;
368 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
369 goto nla_put_failure;
371 return nlmsg_end(skb, nlh);
373 nla_put_failure:
374 nlmsg_cancel(skb, nlh);
375 return -EMSGSIZE;
378 static inline size_t vxlan_nlmsg_size(void)
380 return NLMSG_ALIGN(sizeof(struct ndmsg))
381 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
382 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
383 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
384 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
385 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
386 + nla_total_size(sizeof(struct nda_cacheinfo));
389 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
390 struct vxlan_rdst *rd, int type)
392 struct net *net = dev_net(vxlan->dev);
393 struct sk_buff *skb;
394 int err = -ENOBUFS;
396 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
397 if (skb == NULL)
398 goto errout;
400 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
401 if (err < 0) {
402 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
403 WARN_ON(err == -EMSGSIZE);
404 kfree_skb(skb);
405 goto errout;
408 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
409 return;
410 errout:
411 if (err < 0)
412 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
415 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
417 struct vxlan_dev *vxlan = netdev_priv(dev);
418 struct vxlan_fdb f = {
419 .state = NUD_STALE,
421 struct vxlan_rdst remote = {
422 .remote_ip = *ipa, /* goes to NDA_DST */
423 .remote_vni = VXLAN_N_VID,
426 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
429 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
431 struct vxlan_fdb f = {
432 .state = NUD_STALE,
434 struct vxlan_rdst remote = { };
436 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
438 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
441 /* Hash Ethernet address */
442 static u32 eth_hash(const unsigned char *addr)
444 u64 value = get_unaligned((u64 *)addr);
446 /* only want 6 bytes */
447 #ifdef __BIG_ENDIAN
448 value >>= 16;
449 #else
450 value <<= 16;
451 #endif
452 return hash_64(value, FDB_HASH_BITS);
455 /* Hash chain to use given mac address */
456 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
457 const u8 *mac)
459 return &vxlan->fdb_head[eth_hash(mac)];
462 /* Look up Ethernet address in forwarding table */
463 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
464 const u8 *mac)
466 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
467 struct vxlan_fdb *f;
469 hlist_for_each_entry_rcu(f, head, hlist) {
470 if (ether_addr_equal(mac, f->eth_addr))
471 return f;
474 return NULL;
477 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
478 const u8 *mac)
480 struct vxlan_fdb *f;
482 f = __vxlan_find_mac(vxlan, mac);
483 if (f)
484 f->used = jiffies;
486 return f;
489 /* caller should hold vxlan->hash_lock */
490 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
491 union vxlan_addr *ip, __be16 port,
492 __u32 vni, __u32 ifindex)
494 struct vxlan_rdst *rd;
496 list_for_each_entry(rd, &f->remotes, list) {
497 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
498 rd->remote_port == port &&
499 rd->remote_vni == vni &&
500 rd->remote_ifindex == ifindex)
501 return rd;
504 return NULL;
507 /* Replace destination of unicast mac */
508 static int vxlan_fdb_replace(struct vxlan_fdb *f,
509 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
511 struct vxlan_rdst *rd;
513 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
514 if (rd)
515 return 0;
517 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
518 if (!rd)
519 return 0;
520 rd->remote_ip = *ip;
521 rd->remote_port = port;
522 rd->remote_vni = vni;
523 rd->remote_ifindex = ifindex;
524 return 1;
527 /* Add/update destinations for multicast */
528 static int vxlan_fdb_append(struct vxlan_fdb *f,
529 union vxlan_addr *ip, __be16 port, __u32 vni,
530 __u32 ifindex, struct vxlan_rdst **rdp)
532 struct vxlan_rdst *rd;
534 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
535 if (rd)
536 return 0;
538 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
539 if (rd == NULL)
540 return -ENOBUFS;
541 rd->remote_ip = *ip;
542 rd->remote_port = port;
543 rd->remote_vni = vni;
544 rd->remote_ifindex = ifindex;
546 list_add_tail_rcu(&rd->list, &f->remotes);
548 *rdp = rd;
549 return 1;
552 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, struct sk_buff *skb)
554 struct sk_buff *p, **pp = NULL;
555 struct vxlanhdr *vh, *vh2;
556 struct ethhdr *eh, *eh2;
557 unsigned int hlen, off_vx, off_eth;
558 const struct packet_offload *ptype;
559 __be16 type;
560 int flush = 1;
562 off_vx = skb_gro_offset(skb);
563 hlen = off_vx + sizeof(*vh);
564 vh = skb_gro_header_fast(skb, off_vx);
565 if (skb_gro_header_hard(skb, hlen)) {
566 vh = skb_gro_header_slow(skb, hlen, off_vx);
567 if (unlikely(!vh))
568 goto out;
570 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
571 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
573 off_eth = skb_gro_offset(skb);
574 hlen = off_eth + sizeof(*eh);
575 eh = skb_gro_header_fast(skb, off_eth);
576 if (skb_gro_header_hard(skb, hlen)) {
577 eh = skb_gro_header_slow(skb, hlen, off_eth);
578 if (unlikely(!eh))
579 goto out;
582 flush = 0;
584 for (p = *head; p; p = p->next) {
585 if (!NAPI_GRO_CB(p)->same_flow)
586 continue;
588 vh2 = (struct vxlanhdr *)(p->data + off_vx);
589 eh2 = (struct ethhdr *)(p->data + off_eth);
590 if (vh->vx_vni != vh2->vx_vni || compare_ether_header(eh, eh2)) {
591 NAPI_GRO_CB(p)->same_flow = 0;
592 continue;
596 type = eh->h_proto;
598 rcu_read_lock();
599 ptype = gro_find_receive_by_type(type);
600 if (ptype == NULL) {
601 flush = 1;
602 goto out_unlock;
605 skb_gro_pull(skb, sizeof(*eh)); /* pull inner eth header */
606 skb_gro_postpull_rcsum(skb, eh, sizeof(*eh));
607 pp = ptype->callbacks.gro_receive(head, skb);
609 out_unlock:
610 rcu_read_unlock();
611 out:
612 NAPI_GRO_CB(skb)->flush |= flush;
614 return pp;
617 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff)
619 struct ethhdr *eh;
620 struct packet_offload *ptype;
621 struct udphdr *uh;
622 __be16 type;
623 int vxlan_len = sizeof(struct vxlanhdr) + sizeof(struct ethhdr);
624 int err = -ENOSYS;
626 uh = (struct udphdr *)(skb->data + nhoff - sizeof(struct udphdr));
627 skb_shinfo(skb)->gso_type |= uh->check ?
628 SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
630 eh = (struct ethhdr *)(skb->data + nhoff + sizeof(struct vxlanhdr));
631 type = eh->h_proto;
633 rcu_read_lock();
634 ptype = gro_find_complete_by_type(type);
635 if (ptype != NULL)
636 err = ptype->callbacks.gro_complete(skb, nhoff + vxlan_len);
638 rcu_read_unlock();
639 return err;
642 /* Notify netdevs that UDP port started listening */
643 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
645 struct net_device *dev;
646 struct sock *sk = vs->sock->sk;
647 struct net *net = sock_net(sk);
648 sa_family_t sa_family = sk->sk_family;
649 __be16 port = inet_sk(sk)->inet_sport;
650 int err;
652 if (sa_family == AF_INET) {
653 err = udp_add_offload(&vs->udp_offloads);
654 if (err)
655 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
658 rcu_read_lock();
659 for_each_netdev_rcu(net, dev) {
660 if (dev->netdev_ops->ndo_add_vxlan_port)
661 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
662 port);
664 rcu_read_unlock();
667 /* Notify netdevs that UDP port is no more listening */
668 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
670 struct net_device *dev;
671 struct sock *sk = vs->sock->sk;
672 struct net *net = sock_net(sk);
673 sa_family_t sa_family = sk->sk_family;
674 __be16 port = inet_sk(sk)->inet_sport;
676 rcu_read_lock();
677 for_each_netdev_rcu(net, dev) {
678 if (dev->netdev_ops->ndo_del_vxlan_port)
679 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
680 port);
682 rcu_read_unlock();
684 if (sa_family == AF_INET)
685 udp_del_offload(&vs->udp_offloads);
688 /* Add new entry to forwarding table -- assumes lock held */
689 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
690 const u8 *mac, union vxlan_addr *ip,
691 __u16 state, __u16 flags,
692 __be16 port, __u32 vni, __u32 ifindex,
693 __u8 ndm_flags)
695 struct vxlan_rdst *rd = NULL;
696 struct vxlan_fdb *f;
697 int notify = 0;
699 f = __vxlan_find_mac(vxlan, mac);
700 if (f) {
701 if (flags & NLM_F_EXCL) {
702 netdev_dbg(vxlan->dev,
703 "lost race to create %pM\n", mac);
704 return -EEXIST;
706 if (f->state != state) {
707 f->state = state;
708 f->updated = jiffies;
709 notify = 1;
711 if (f->flags != ndm_flags) {
712 f->flags = ndm_flags;
713 f->updated = jiffies;
714 notify = 1;
716 if ((flags & NLM_F_REPLACE)) {
717 /* Only change unicasts */
718 if (!(is_multicast_ether_addr(f->eth_addr) ||
719 is_zero_ether_addr(f->eth_addr))) {
720 int rc = vxlan_fdb_replace(f, ip, port, vni,
721 ifindex);
723 if (rc < 0)
724 return rc;
725 notify |= rc;
726 } else
727 return -EOPNOTSUPP;
729 if ((flags & NLM_F_APPEND) &&
730 (is_multicast_ether_addr(f->eth_addr) ||
731 is_zero_ether_addr(f->eth_addr))) {
732 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
733 &rd);
735 if (rc < 0)
736 return rc;
737 notify |= rc;
739 } else {
740 if (!(flags & NLM_F_CREATE))
741 return -ENOENT;
743 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
744 return -ENOSPC;
746 /* Disallow replace to add a multicast entry */
747 if ((flags & NLM_F_REPLACE) &&
748 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
749 return -EOPNOTSUPP;
751 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
752 f = kmalloc(sizeof(*f), GFP_ATOMIC);
753 if (!f)
754 return -ENOMEM;
756 notify = 1;
757 f->state = state;
758 f->flags = ndm_flags;
759 f->updated = f->used = jiffies;
760 INIT_LIST_HEAD(&f->remotes);
761 memcpy(f->eth_addr, mac, ETH_ALEN);
763 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
765 ++vxlan->addrcnt;
766 hlist_add_head_rcu(&f->hlist,
767 vxlan_fdb_head(vxlan, mac));
770 if (notify) {
771 if (rd == NULL)
772 rd = first_remote_rtnl(f);
773 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
776 return 0;
779 static void vxlan_fdb_free(struct rcu_head *head)
781 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
782 struct vxlan_rdst *rd, *nd;
784 list_for_each_entry_safe(rd, nd, &f->remotes, list)
785 kfree(rd);
786 kfree(f);
789 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
791 netdev_dbg(vxlan->dev,
792 "delete %pM\n", f->eth_addr);
794 --vxlan->addrcnt;
795 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
797 hlist_del_rcu(&f->hlist);
798 call_rcu(&f->rcu, vxlan_fdb_free);
801 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
802 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
804 struct net *net = dev_net(vxlan->dev);
805 int err;
807 if (tb[NDA_DST]) {
808 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
809 if (err)
810 return err;
811 } else {
812 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
813 if (remote->sa.sa_family == AF_INET) {
814 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
815 ip->sa.sa_family = AF_INET;
816 #if IS_ENABLED(CONFIG_IPV6)
817 } else {
818 ip->sin6.sin6_addr = in6addr_any;
819 ip->sa.sa_family = AF_INET6;
820 #endif
824 if (tb[NDA_PORT]) {
825 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
826 return -EINVAL;
827 *port = nla_get_be16(tb[NDA_PORT]);
828 } else {
829 *port = vxlan->dst_port;
832 if (tb[NDA_VNI]) {
833 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
834 return -EINVAL;
835 *vni = nla_get_u32(tb[NDA_VNI]);
836 } else {
837 *vni = vxlan->default_dst.remote_vni;
840 if (tb[NDA_IFINDEX]) {
841 struct net_device *tdev;
843 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
844 return -EINVAL;
845 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
846 tdev = __dev_get_by_index(net, *ifindex);
847 if (!tdev)
848 return -EADDRNOTAVAIL;
849 } else {
850 *ifindex = 0;
853 return 0;
856 /* Add static entry (via netlink) */
857 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
858 struct net_device *dev,
859 const unsigned char *addr, u16 flags)
861 struct vxlan_dev *vxlan = netdev_priv(dev);
862 /* struct net *net = dev_net(vxlan->dev); */
863 union vxlan_addr ip;
864 __be16 port;
865 u32 vni, ifindex;
866 int err;
868 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
869 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
870 ndm->ndm_state);
871 return -EINVAL;
874 if (tb[NDA_DST] == NULL)
875 return -EINVAL;
877 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
878 if (err)
879 return err;
881 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
882 return -EAFNOSUPPORT;
884 spin_lock_bh(&vxlan->hash_lock);
885 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
886 port, vni, ifindex, ndm->ndm_flags);
887 spin_unlock_bh(&vxlan->hash_lock);
889 return err;
892 /* Delete entry (via netlink) */
893 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
894 struct net_device *dev,
895 const unsigned char *addr)
897 struct vxlan_dev *vxlan = netdev_priv(dev);
898 struct vxlan_fdb *f;
899 struct vxlan_rdst *rd = NULL;
900 union vxlan_addr ip;
901 __be16 port;
902 u32 vni, ifindex;
903 int err;
905 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
906 if (err)
907 return err;
909 err = -ENOENT;
911 spin_lock_bh(&vxlan->hash_lock);
912 f = vxlan_find_mac(vxlan, addr);
913 if (!f)
914 goto out;
916 if (!vxlan_addr_any(&ip)) {
917 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
918 if (!rd)
919 goto out;
922 err = 0;
924 /* remove a destination if it's not the only one on the list,
925 * otherwise destroy the fdb entry
927 if (rd && !list_is_singular(&f->remotes)) {
928 list_del_rcu(&rd->list);
929 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
930 kfree_rcu(rd, rcu);
931 goto out;
934 vxlan_fdb_destroy(vxlan, f);
936 out:
937 spin_unlock_bh(&vxlan->hash_lock);
939 return err;
942 /* Dump forwarding table */
943 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
944 struct net_device *dev, int idx)
946 struct vxlan_dev *vxlan = netdev_priv(dev);
947 unsigned int h;
949 for (h = 0; h < FDB_HASH_SIZE; ++h) {
950 struct vxlan_fdb *f;
951 int err;
953 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
954 struct vxlan_rdst *rd;
956 if (idx < cb->args[0])
957 goto skip;
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;
968 skip:
969 ++idx;
972 out:
973 return idx;
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.
980 static bool vxlan_snoop(struct net_device *dev,
981 union vxlan_addr *src_ip, const u8 *src_mac)
983 struct vxlan_dev *vxlan = netdev_priv(dev);
984 struct vxlan_fdb *f;
986 f = vxlan_find_mac(vxlan, src_mac);
987 if (likely(f)) {
988 struct vxlan_rdst *rdst = first_remote_rcu(f);
990 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
991 return false;
993 /* Don't migrate static entries, drop packets */
994 if (f->state & NUD_NOARP)
995 return true;
997 if (net_ratelimit())
998 netdev_info(dev,
999 "%pM migrated from %pIS to %pIS\n",
1000 src_mac, &rdst->remote_ip, &src_ip);
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);
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);
1020 return false;
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)
1026 struct vxlan_dev *vxlan;
1028 /* The vxlan_sock is only used by dev, leaving group has
1029 * no effect on other vxlan devices.
1031 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1032 return false;
1034 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1035 if (!netif_running(vxlan->dev) || vxlan == dev)
1036 continue;
1038 if (vxlan->vn_sock != dev->vn_sock)
1039 continue;
1041 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1042 &dev->default_dst.remote_ip))
1043 continue;
1045 if (vxlan->default_dst.remote_ifindex !=
1046 dev->default_dst.remote_ifindex)
1047 continue;
1049 return true;
1052 return false;
1055 static void vxlan_sock_hold(struct vxlan_sock *vs)
1057 atomic_inc(&vs->refcnt);
1060 void vxlan_sock_release(struct vxlan_sock *vs)
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);
1066 if (!atomic_dec_and_test(&vs->refcnt))
1067 return;
1069 spin_lock(&vn->sock_lock);
1070 hlist_del_rcu(&vs->hlist);
1071 rcu_assign_sk_user_data(vs->sock->sk, NULL);
1072 vxlan_notify_del_rx_port(vs);
1073 spin_unlock(&vn->sock_lock);
1075 queue_work(vxlan_wq, &vs->del_work);
1077 EXPORT_SYMBOL_GPL(vxlan_sock_release);
1079 /* Callback to update multicast group membership when first VNI on
1080 * multicast asddress is brought up
1081 * Done as workqueue because ip_mc_join_group acquires RTNL.
1083 static void vxlan_igmp_join(struct work_struct *work)
1085 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
1086 struct vxlan_sock *vs = vxlan->vn_sock;
1087 struct sock *sk = vs->sock->sk;
1088 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1089 int ifindex = vxlan->default_dst.remote_ifindex;
1091 lock_sock(sk);
1092 if (ip->sa.sa_family == AF_INET) {
1093 struct ip_mreqn mreq = {
1094 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1095 .imr_ifindex = ifindex,
1098 ip_mc_join_group(sk, &mreq);
1099 #if IS_ENABLED(CONFIG_IPV6)
1100 } else {
1101 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1102 &ip->sin6.sin6_addr);
1103 #endif
1105 release_sock(sk);
1107 vxlan_sock_release(vs);
1108 dev_put(vxlan->dev);
1111 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1112 static void vxlan_igmp_leave(struct work_struct *work)
1114 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
1115 struct vxlan_sock *vs = vxlan->vn_sock;
1116 struct sock *sk = vs->sock->sk;
1117 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1118 int ifindex = vxlan->default_dst.remote_ifindex;
1120 lock_sock(sk);
1121 if (ip->sa.sa_family == AF_INET) {
1122 struct ip_mreqn mreq = {
1123 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1124 .imr_ifindex = ifindex,
1127 ip_mc_leave_group(sk, &mreq);
1128 #if IS_ENABLED(CONFIG_IPV6)
1129 } else {
1130 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1131 &ip->sin6.sin6_addr);
1132 #endif
1135 release_sock(sk);
1137 vxlan_sock_release(vs);
1138 dev_put(vxlan->dev);
1141 /* Callback from net/ipv4/udp.c to receive packets */
1142 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1144 struct vxlan_sock *vs;
1145 struct vxlanhdr *vxh;
1147 /* Need Vxlan and inner Ethernet header to be present */
1148 if (!pskb_may_pull(skb, VXLAN_HLEN))
1149 goto error;
1151 /* Return packets with reserved bits set */
1152 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1153 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1154 (vxh->vx_vni & htonl(0xff))) {
1155 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1156 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1157 goto error;
1160 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1161 goto drop;
1163 vs = rcu_dereference_sk_user_data(sk);
1164 if (!vs)
1165 goto drop;
1167 skb_pop_rcv_encapsulation(skb);
1169 vs->rcv(vs, skb, vxh->vx_vni);
1170 return 0;
1172 drop:
1173 /* Consume bad packet */
1174 kfree_skb(skb);
1175 return 0;
1177 error:
1178 /* Return non vxlan pkt */
1179 return 1;
1182 static void vxlan_rcv(struct vxlan_sock *vs,
1183 struct sk_buff *skb, __be32 vx_vni)
1185 struct iphdr *oip = NULL;
1186 struct ipv6hdr *oip6 = NULL;
1187 struct vxlan_dev *vxlan;
1188 struct pcpu_sw_netstats *stats;
1189 union vxlan_addr saddr;
1190 __u32 vni;
1191 int err = 0;
1192 union vxlan_addr *remote_ip;
1194 vni = ntohl(vx_vni) >> 8;
1195 /* Is this VNI defined? */
1196 vxlan = vxlan_vs_find_vni(vs, vni);
1197 if (!vxlan)
1198 goto drop;
1200 remote_ip = &vxlan->default_dst.remote_ip;
1201 skb_reset_mac_header(skb);
1202 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
1203 skb->protocol = eth_type_trans(skb, vxlan->dev);
1204 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1206 /* Ignore packet loops (and multicast echo) */
1207 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1208 goto drop;
1210 /* Re-examine inner Ethernet packet */
1211 if (remote_ip->sa.sa_family == AF_INET) {
1212 oip = ip_hdr(skb);
1213 saddr.sin.sin_addr.s_addr = oip->saddr;
1214 saddr.sa.sa_family = AF_INET;
1215 #if IS_ENABLED(CONFIG_IPV6)
1216 } else {
1217 oip6 = ipv6_hdr(skb);
1218 saddr.sin6.sin6_addr = oip6->saddr;
1219 saddr.sa.sa_family = AF_INET6;
1220 #endif
1223 if ((vxlan->flags & VXLAN_F_LEARN) &&
1224 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1225 goto drop;
1227 skb_reset_network_header(skb);
1229 if (oip6)
1230 err = IP6_ECN_decapsulate(oip6, skb);
1231 if (oip)
1232 err = IP_ECN_decapsulate(oip, skb);
1234 if (unlikely(err)) {
1235 if (log_ecn_error) {
1236 if (oip6)
1237 net_info_ratelimited("non-ECT from %pI6\n",
1238 &oip6->saddr);
1239 if (oip)
1240 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1241 &oip->saddr, oip->tos);
1243 if (err > 1) {
1244 ++vxlan->dev->stats.rx_frame_errors;
1245 ++vxlan->dev->stats.rx_errors;
1246 goto drop;
1250 stats = this_cpu_ptr(vxlan->dev->tstats);
1251 u64_stats_update_begin(&stats->syncp);
1252 stats->rx_packets++;
1253 stats->rx_bytes += skb->len;
1254 u64_stats_update_end(&stats->syncp);
1256 netif_rx(skb);
1258 return;
1259 drop:
1260 /* Consume bad packet */
1261 kfree_skb(skb);
1264 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1266 struct vxlan_dev *vxlan = netdev_priv(dev);
1267 struct arphdr *parp;
1268 u8 *arpptr, *sha;
1269 __be32 sip, tip;
1270 struct neighbour *n;
1272 if (dev->flags & IFF_NOARP)
1273 goto out;
1275 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1276 dev->stats.tx_dropped++;
1277 goto out;
1279 parp = arp_hdr(skb);
1281 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1282 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1283 parp->ar_pro != htons(ETH_P_IP) ||
1284 parp->ar_op != htons(ARPOP_REQUEST) ||
1285 parp->ar_hln != dev->addr_len ||
1286 parp->ar_pln != 4)
1287 goto out;
1288 arpptr = (u8 *)parp + sizeof(struct arphdr);
1289 sha = arpptr;
1290 arpptr += dev->addr_len; /* sha */
1291 memcpy(&sip, arpptr, sizeof(sip));
1292 arpptr += sizeof(sip);
1293 arpptr += dev->addr_len; /* tha */
1294 memcpy(&tip, arpptr, sizeof(tip));
1296 if (ipv4_is_loopback(tip) ||
1297 ipv4_is_multicast(tip))
1298 goto out;
1300 n = neigh_lookup(&arp_tbl, &tip, dev);
1302 if (n) {
1303 struct vxlan_fdb *f;
1304 struct sk_buff *reply;
1306 if (!(n->nud_state & NUD_CONNECTED)) {
1307 neigh_release(n);
1308 goto out;
1311 f = vxlan_find_mac(vxlan, n->ha);
1312 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1313 /* bridge-local neighbor */
1314 neigh_release(n);
1315 goto out;
1318 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1319 n->ha, sha);
1321 neigh_release(n);
1323 if (reply == NULL)
1324 goto out;
1326 skb_reset_mac_header(reply);
1327 __skb_pull(reply, skb_network_offset(reply));
1328 reply->ip_summed = CHECKSUM_UNNECESSARY;
1329 reply->pkt_type = PACKET_HOST;
1331 if (netif_rx_ni(reply) == NET_RX_DROP)
1332 dev->stats.rx_dropped++;
1333 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1334 union vxlan_addr ipa = {
1335 .sin.sin_addr.s_addr = tip,
1336 .sin.sin_family = AF_INET,
1339 vxlan_ip_miss(dev, &ipa);
1341 out:
1342 consume_skb(skb);
1343 return NETDEV_TX_OK;
1346 #if IS_ENABLED(CONFIG_IPV6)
1348 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1349 struct neighbour *n, bool isrouter)
1351 struct net_device *dev = request->dev;
1352 struct sk_buff *reply;
1353 struct nd_msg *ns, *na;
1354 struct ipv6hdr *pip6;
1355 u8 *daddr;
1356 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1357 int ns_olen;
1358 int i, len;
1360 if (dev == NULL)
1361 return NULL;
1363 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1364 sizeof(*na) + na_olen + dev->needed_tailroom;
1365 reply = alloc_skb(len, GFP_ATOMIC);
1366 if (reply == NULL)
1367 return NULL;
1369 reply->protocol = htons(ETH_P_IPV6);
1370 reply->dev = dev;
1371 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1372 skb_push(reply, sizeof(struct ethhdr));
1373 skb_set_mac_header(reply, 0);
1375 ns = (struct nd_msg *)skb_transport_header(request);
1377 daddr = eth_hdr(request)->h_source;
1378 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1379 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1380 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1381 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1382 break;
1386 /* Ethernet header */
1387 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1388 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1389 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1390 reply->protocol = htons(ETH_P_IPV6);
1392 skb_pull(reply, sizeof(struct ethhdr));
1393 skb_set_network_header(reply, 0);
1394 skb_put(reply, sizeof(struct ipv6hdr));
1396 /* IPv6 header */
1398 pip6 = ipv6_hdr(reply);
1399 memset(pip6, 0, sizeof(struct ipv6hdr));
1400 pip6->version = 6;
1401 pip6->priority = ipv6_hdr(request)->priority;
1402 pip6->nexthdr = IPPROTO_ICMPV6;
1403 pip6->hop_limit = 255;
1404 pip6->daddr = ipv6_hdr(request)->saddr;
1405 pip6->saddr = *(struct in6_addr *)n->primary_key;
1407 skb_pull(reply, sizeof(struct ipv6hdr));
1408 skb_set_transport_header(reply, 0);
1410 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1412 /* Neighbor Advertisement */
1413 memset(na, 0, sizeof(*na)+na_olen);
1414 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1415 na->icmph.icmp6_router = isrouter;
1416 na->icmph.icmp6_override = 1;
1417 na->icmph.icmp6_solicited = 1;
1418 na->target = ns->target;
1419 ether_addr_copy(&na->opt[2], n->ha);
1420 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1421 na->opt[1] = na_olen >> 3;
1423 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1424 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1425 csum_partial(na, sizeof(*na)+na_olen, 0));
1427 pip6->payload_len = htons(sizeof(*na)+na_olen);
1429 skb_push(reply, sizeof(struct ipv6hdr));
1431 reply->ip_summed = CHECKSUM_UNNECESSARY;
1433 return reply;
1436 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1438 struct vxlan_dev *vxlan = netdev_priv(dev);
1439 struct nd_msg *msg;
1440 const struct ipv6hdr *iphdr;
1441 const struct in6_addr *saddr, *daddr;
1442 struct neighbour *n;
1443 struct inet6_dev *in6_dev;
1445 in6_dev = __in6_dev_get(dev);
1446 if (!in6_dev)
1447 goto out;
1449 iphdr = ipv6_hdr(skb);
1450 saddr = &iphdr->saddr;
1451 daddr = &iphdr->daddr;
1453 msg = (struct nd_msg *)skb_transport_header(skb);
1454 if (msg->icmph.icmp6_code != 0 ||
1455 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1456 goto out;
1458 if (ipv6_addr_loopback(daddr) ||
1459 ipv6_addr_is_multicast(&msg->target))
1460 goto out;
1462 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1464 if (n) {
1465 struct vxlan_fdb *f;
1466 struct sk_buff *reply;
1468 if (!(n->nud_state & NUD_CONNECTED)) {
1469 neigh_release(n);
1470 goto out;
1473 f = vxlan_find_mac(vxlan, n->ha);
1474 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1475 /* bridge-local neighbor */
1476 neigh_release(n);
1477 goto out;
1480 reply = vxlan_na_create(skb, n,
1481 !!(f ? f->flags & NTF_ROUTER : 0));
1483 neigh_release(n);
1485 if (reply == NULL)
1486 goto out;
1488 if (netif_rx_ni(reply) == NET_RX_DROP)
1489 dev->stats.rx_dropped++;
1491 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1492 union vxlan_addr ipa = {
1493 .sin6.sin6_addr = msg->target,
1494 .sin6.sin6_family = AF_INET6,
1497 vxlan_ip_miss(dev, &ipa);
1500 out:
1501 consume_skb(skb);
1502 return NETDEV_TX_OK;
1504 #endif
1506 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1508 struct vxlan_dev *vxlan = netdev_priv(dev);
1509 struct neighbour *n;
1511 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1512 return false;
1514 n = NULL;
1515 switch (ntohs(eth_hdr(skb)->h_proto)) {
1516 case ETH_P_IP:
1518 struct iphdr *pip;
1520 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1521 return false;
1522 pip = ip_hdr(skb);
1523 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1524 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1525 union vxlan_addr ipa = {
1526 .sin.sin_addr.s_addr = pip->daddr,
1527 .sin.sin_family = AF_INET,
1530 vxlan_ip_miss(dev, &ipa);
1531 return false;
1534 break;
1536 #if IS_ENABLED(CONFIG_IPV6)
1537 case ETH_P_IPV6:
1539 struct ipv6hdr *pip6;
1541 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1542 return false;
1543 pip6 = ipv6_hdr(skb);
1544 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1545 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1546 union vxlan_addr ipa = {
1547 .sin6.sin6_addr = pip6->daddr,
1548 .sin6.sin6_family = AF_INET6,
1551 vxlan_ip_miss(dev, &ipa);
1552 return false;
1555 break;
1557 #endif
1558 default:
1559 return false;
1562 if (n) {
1563 bool diff;
1565 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1566 if (diff) {
1567 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1568 dev->addr_len);
1569 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1571 neigh_release(n);
1572 return diff;
1575 return false;
1578 /* Compute source port for outgoing packet
1579 * first choice to use L4 flow hash since it will spread
1580 * better and maybe available from hardware
1581 * secondary choice is to use jhash on the Ethernet header
1583 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
1585 unsigned int range = (port_max - port_min) + 1;
1586 u32 hash;
1588 hash = skb_get_hash(skb);
1589 if (!hash)
1590 hash = jhash(skb->data, 2 * ETH_ALEN,
1591 (__force u32) skb->protocol);
1593 return htons((((u64) hash * range) >> 32) + port_min);
1595 EXPORT_SYMBOL_GPL(vxlan_src_port);
1597 static inline struct sk_buff *vxlan_handle_offloads(struct sk_buff *skb,
1598 bool udp_csum)
1600 int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1601 return iptunnel_handle_offloads(skb, udp_csum, type);
1604 #if IS_ENABLED(CONFIG_IPV6)
1605 static int vxlan6_xmit_skb(struct vxlan_sock *vs,
1606 struct dst_entry *dst, struct sk_buff *skb,
1607 struct net_device *dev, struct in6_addr *saddr,
1608 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1609 __be16 src_port, __be16 dst_port, __be32 vni,
1610 bool xnet)
1612 struct ipv6hdr *ip6h;
1613 struct vxlanhdr *vxh;
1614 struct udphdr *uh;
1615 int min_headroom;
1616 int err;
1618 skb = vxlan_handle_offloads(skb, !udp_get_no_check6_tx(vs->sock->sk));
1619 if (IS_ERR(skb))
1620 return -EINVAL;
1622 skb_scrub_packet(skb, xnet);
1624 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1625 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1626 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1628 /* Need space for new headers (invalidates iph ptr) */
1629 err = skb_cow_head(skb, min_headroom);
1630 if (unlikely(err))
1631 return err;
1633 if (vlan_tx_tag_present(skb)) {
1634 if (WARN_ON(!__vlan_put_tag(skb,
1635 skb->vlan_proto,
1636 vlan_tx_tag_get(skb))))
1637 return -ENOMEM;
1639 skb->vlan_tci = 0;
1642 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1643 vxh->vx_flags = htonl(VXLAN_FLAGS);
1644 vxh->vx_vni = vni;
1646 __skb_push(skb, sizeof(*uh));
1647 skb_reset_transport_header(skb);
1648 uh = udp_hdr(skb);
1650 uh->dest = dst_port;
1651 uh->source = src_port;
1653 uh->len = htons(skb->len);
1655 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1656 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1657 IPSKB_REROUTED);
1658 skb_dst_set(skb, dst);
1660 udp6_set_csum(udp_get_no_check6_tx(vs->sock->sk), skb,
1661 saddr, daddr, skb->len);
1663 __skb_push(skb, sizeof(*ip6h));
1664 skb_reset_network_header(skb);
1665 ip6h = ipv6_hdr(skb);
1666 ip6h->version = 6;
1667 ip6h->priority = prio;
1668 ip6h->flow_lbl[0] = 0;
1669 ip6h->flow_lbl[1] = 0;
1670 ip6h->flow_lbl[2] = 0;
1671 ip6h->payload_len = htons(skb->len);
1672 ip6h->nexthdr = IPPROTO_UDP;
1673 ip6h->hop_limit = ttl;
1674 ip6h->daddr = *daddr;
1675 ip6h->saddr = *saddr;
1677 ip6tunnel_xmit(skb, dev);
1678 return 0;
1680 #endif
1682 int vxlan_xmit_skb(struct vxlan_sock *vs,
1683 struct rtable *rt, struct sk_buff *skb,
1684 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1685 __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)
1687 struct vxlanhdr *vxh;
1688 struct udphdr *uh;
1689 int min_headroom;
1690 int err;
1692 skb = vxlan_handle_offloads(skb, !vs->sock->sk->sk_no_check_tx);
1693 if (IS_ERR(skb))
1694 return -EINVAL;
1696 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1697 + VXLAN_HLEN + sizeof(struct iphdr)
1698 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1700 /* Need space for new headers (invalidates iph ptr) */
1701 err = skb_cow_head(skb, min_headroom);
1702 if (unlikely(err))
1703 return err;
1705 if (vlan_tx_tag_present(skb)) {
1706 if (WARN_ON(!__vlan_put_tag(skb,
1707 skb->vlan_proto,
1708 vlan_tx_tag_get(skb))))
1709 return -ENOMEM;
1711 skb->vlan_tci = 0;
1714 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1715 vxh->vx_flags = htonl(VXLAN_FLAGS);
1716 vxh->vx_vni = vni;
1718 __skb_push(skb, sizeof(*uh));
1719 skb_reset_transport_header(skb);
1720 uh = udp_hdr(skb);
1722 uh->dest = dst_port;
1723 uh->source = src_port;
1725 uh->len = htons(skb->len);
1727 udp_set_csum(vs->sock->sk->sk_no_check_tx, skb,
1728 src, dst, skb->len);
1730 return iptunnel_xmit(vs->sock->sk, rt, skb, src, dst, IPPROTO_UDP,
1731 tos, ttl, df, xnet);
1733 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1735 /* Bypass encapsulation if the destination is local */
1736 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1737 struct vxlan_dev *dst_vxlan)
1739 struct pcpu_sw_netstats *tx_stats, *rx_stats;
1740 union vxlan_addr loopback;
1741 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1742 struct net_device *dev = skb->dev;
1743 int len = skb->len;
1745 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1746 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1747 skb->pkt_type = PACKET_HOST;
1748 skb->encapsulation = 0;
1749 skb->dev = dst_vxlan->dev;
1750 __skb_pull(skb, skb_network_offset(skb));
1752 if (remote_ip->sa.sa_family == AF_INET) {
1753 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1754 loopback.sa.sa_family = AF_INET;
1755 #if IS_ENABLED(CONFIG_IPV6)
1756 } else {
1757 loopback.sin6.sin6_addr = in6addr_loopback;
1758 loopback.sa.sa_family = AF_INET6;
1759 #endif
1762 if (dst_vxlan->flags & VXLAN_F_LEARN)
1763 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1765 u64_stats_update_begin(&tx_stats->syncp);
1766 tx_stats->tx_packets++;
1767 tx_stats->tx_bytes += len;
1768 u64_stats_update_end(&tx_stats->syncp);
1770 if (netif_rx(skb) == NET_RX_SUCCESS) {
1771 u64_stats_update_begin(&rx_stats->syncp);
1772 rx_stats->rx_packets++;
1773 rx_stats->rx_bytes += len;
1774 u64_stats_update_end(&rx_stats->syncp);
1775 } else {
1776 dev->stats.rx_dropped++;
1780 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1781 struct vxlan_rdst *rdst, bool did_rsc)
1783 struct vxlan_dev *vxlan = netdev_priv(dev);
1784 struct rtable *rt = NULL;
1785 const struct iphdr *old_iph;
1786 struct flowi4 fl4;
1787 union vxlan_addr *dst;
1788 __be16 src_port = 0, dst_port;
1789 u32 vni;
1790 __be16 df = 0;
1791 __u8 tos, ttl;
1792 int err;
1794 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1795 vni = rdst->remote_vni;
1796 dst = &rdst->remote_ip;
1798 if (vxlan_addr_any(dst)) {
1799 if (did_rsc) {
1800 /* short-circuited back to local bridge */
1801 vxlan_encap_bypass(skb, vxlan, vxlan);
1802 return;
1804 goto drop;
1807 old_iph = ip_hdr(skb);
1809 ttl = vxlan->ttl;
1810 if (!ttl && vxlan_addr_multicast(dst))
1811 ttl = 1;
1813 tos = vxlan->tos;
1814 if (tos == 1)
1815 tos = ip_tunnel_get_dsfield(old_iph, skb);
1817 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
1819 if (dst->sa.sa_family == AF_INET) {
1820 memset(&fl4, 0, sizeof(fl4));
1821 fl4.flowi4_oif = rdst->remote_ifindex;
1822 fl4.flowi4_tos = RT_TOS(tos);
1823 fl4.daddr = dst->sin.sin_addr.s_addr;
1824 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1826 rt = ip_route_output_key(vxlan->net, &fl4);
1827 if (IS_ERR(rt)) {
1828 netdev_dbg(dev, "no route to %pI4\n",
1829 &dst->sin.sin_addr.s_addr);
1830 dev->stats.tx_carrier_errors++;
1831 goto tx_error;
1834 if (rt->dst.dev == dev) {
1835 netdev_dbg(dev, "circular route to %pI4\n",
1836 &dst->sin.sin_addr.s_addr);
1837 dev->stats.collisions++;
1838 goto rt_tx_error;
1841 /* Bypass encapsulation if the destination is local */
1842 if (rt->rt_flags & RTCF_LOCAL &&
1843 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1844 struct vxlan_dev *dst_vxlan;
1846 ip_rt_put(rt);
1847 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1848 dst->sa.sa_family, dst_port);
1849 if (!dst_vxlan)
1850 goto tx_error;
1851 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1852 return;
1855 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1856 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1858 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1859 fl4.saddr, dst->sin.sin_addr.s_addr,
1860 tos, ttl, df, src_port, dst_port,
1861 htonl(vni << 8),
1862 !net_eq(vxlan->net, dev_net(vxlan->dev)));
1864 if (err < 0)
1865 goto rt_tx_error;
1866 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1867 #if IS_ENABLED(CONFIG_IPV6)
1868 } else {
1869 struct sock *sk = vxlan->vn_sock->sock->sk;
1870 struct dst_entry *ndst;
1871 struct flowi6 fl6;
1872 u32 flags;
1874 memset(&fl6, 0, sizeof(fl6));
1875 fl6.flowi6_oif = rdst->remote_ifindex;
1876 fl6.daddr = dst->sin6.sin6_addr;
1877 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1878 fl6.flowi6_proto = IPPROTO_UDP;
1880 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1881 netdev_dbg(dev, "no route to %pI6\n",
1882 &dst->sin6.sin6_addr);
1883 dev->stats.tx_carrier_errors++;
1884 goto tx_error;
1887 if (ndst->dev == dev) {
1888 netdev_dbg(dev, "circular route to %pI6\n",
1889 &dst->sin6.sin6_addr);
1890 dst_release(ndst);
1891 dev->stats.collisions++;
1892 goto tx_error;
1895 /* Bypass encapsulation if the destination is local */
1896 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1897 if (flags & RTF_LOCAL &&
1898 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1899 struct vxlan_dev *dst_vxlan;
1901 dst_release(ndst);
1902 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1903 dst->sa.sa_family, dst_port);
1904 if (!dst_vxlan)
1905 goto tx_error;
1906 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1907 return;
1910 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1912 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
1913 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1914 src_port, dst_port, htonl(vni << 8),
1915 !net_eq(vxlan->net, dev_net(vxlan->dev)));
1916 #endif
1919 return;
1921 drop:
1922 dev->stats.tx_dropped++;
1923 goto tx_free;
1925 rt_tx_error:
1926 ip_rt_put(rt);
1927 tx_error:
1928 dev->stats.tx_errors++;
1929 tx_free:
1930 dev_kfree_skb(skb);
1933 /* Transmit local packets over Vxlan
1935 * Outer IP header inherits ECN and DF from inner header.
1936 * Outer UDP destination is the VXLAN assigned port.
1937 * source port is based on hash of flow
1939 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1941 struct vxlan_dev *vxlan = netdev_priv(dev);
1942 struct ethhdr *eth;
1943 bool did_rsc = false;
1944 struct vxlan_rdst *rdst, *fdst = NULL;
1945 struct vxlan_fdb *f;
1947 skb_reset_mac_header(skb);
1948 eth = eth_hdr(skb);
1950 if ((vxlan->flags & VXLAN_F_PROXY)) {
1951 if (ntohs(eth->h_proto) == ETH_P_ARP)
1952 return arp_reduce(dev, skb);
1953 #if IS_ENABLED(CONFIG_IPV6)
1954 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
1955 pskb_may_pull(skb, sizeof(struct ipv6hdr)
1956 + sizeof(struct nd_msg)) &&
1957 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1958 struct nd_msg *msg;
1960 msg = (struct nd_msg *)skb_transport_header(skb);
1961 if (msg->icmph.icmp6_code == 0 &&
1962 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1963 return neigh_reduce(dev, skb);
1965 eth = eth_hdr(skb);
1966 #endif
1969 f = vxlan_find_mac(vxlan, eth->h_dest);
1970 did_rsc = false;
1972 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1973 (ntohs(eth->h_proto) == ETH_P_IP ||
1974 ntohs(eth->h_proto) == ETH_P_IPV6)) {
1975 did_rsc = route_shortcircuit(dev, skb);
1976 if (did_rsc)
1977 f = vxlan_find_mac(vxlan, eth->h_dest);
1980 if (f == NULL) {
1981 f = vxlan_find_mac(vxlan, all_zeros_mac);
1982 if (f == NULL) {
1983 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1984 !is_multicast_ether_addr(eth->h_dest))
1985 vxlan_fdb_miss(vxlan, eth->h_dest);
1987 dev->stats.tx_dropped++;
1988 kfree_skb(skb);
1989 return NETDEV_TX_OK;
1993 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1994 struct sk_buff *skb1;
1996 if (!fdst) {
1997 fdst = rdst;
1998 continue;
2000 skb1 = skb_clone(skb, GFP_ATOMIC);
2001 if (skb1)
2002 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2005 if (fdst)
2006 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2007 else
2008 kfree_skb(skb);
2009 return NETDEV_TX_OK;
2012 /* Walk the forwarding table and purge stale entries */
2013 static void vxlan_cleanup(unsigned long arg)
2015 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2016 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2017 unsigned int h;
2019 if (!netif_running(vxlan->dev))
2020 return;
2022 spin_lock_bh(&vxlan->hash_lock);
2023 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2024 struct hlist_node *p, *n;
2025 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2026 struct vxlan_fdb *f
2027 = container_of(p, struct vxlan_fdb, hlist);
2028 unsigned long timeout;
2030 if (f->state & NUD_PERMANENT)
2031 continue;
2033 timeout = f->used + vxlan->age_interval * HZ;
2034 if (time_before_eq(timeout, jiffies)) {
2035 netdev_dbg(vxlan->dev,
2036 "garbage collect %pM\n",
2037 f->eth_addr);
2038 f->state = NUD_STALE;
2039 vxlan_fdb_destroy(vxlan, f);
2040 } else if (time_before(timeout, next_timer))
2041 next_timer = timeout;
2044 spin_unlock_bh(&vxlan->hash_lock);
2046 mod_timer(&vxlan->age_timer, next_timer);
2049 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2051 __u32 vni = vxlan->default_dst.remote_vni;
2053 vxlan->vn_sock = vs;
2054 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2057 /* Setup stats when device is created */
2058 static int vxlan_init(struct net_device *dev)
2060 struct vxlan_dev *vxlan = netdev_priv(dev);
2061 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2062 struct vxlan_sock *vs;
2063 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2065 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2066 if (!dev->tstats)
2067 return -ENOMEM;
2069 spin_lock(&vn->sock_lock);
2070 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2071 vxlan->dst_port);
2072 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
2073 /* If we have a socket with same port already, reuse it */
2074 vxlan_vs_add_dev(vs, vxlan);
2075 } else {
2076 /* otherwise make new socket outside of RTNL */
2077 dev_hold(dev);
2078 queue_work(vxlan_wq, &vxlan->sock_work);
2080 spin_unlock(&vn->sock_lock);
2082 return 0;
2085 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2087 struct vxlan_fdb *f;
2089 spin_lock_bh(&vxlan->hash_lock);
2090 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2091 if (f)
2092 vxlan_fdb_destroy(vxlan, f);
2093 spin_unlock_bh(&vxlan->hash_lock);
2096 static void vxlan_uninit(struct net_device *dev)
2098 struct vxlan_dev *vxlan = netdev_priv(dev);
2099 struct vxlan_sock *vs = vxlan->vn_sock;
2101 vxlan_fdb_delete_default(vxlan);
2103 if (vs)
2104 vxlan_sock_release(vs);
2105 free_percpu(dev->tstats);
2108 /* Start ageing timer and join group when device is brought up */
2109 static int vxlan_open(struct net_device *dev)
2111 struct vxlan_dev *vxlan = netdev_priv(dev);
2112 struct vxlan_sock *vs = vxlan->vn_sock;
2114 /* socket hasn't been created */
2115 if (!vs)
2116 return -ENOTCONN;
2118 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2119 vxlan_sock_hold(vs);
2120 dev_hold(dev);
2121 queue_work(vxlan_wq, &vxlan->igmp_join);
2124 if (vxlan->age_interval)
2125 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2127 return 0;
2130 /* Purge the forwarding table */
2131 static void vxlan_flush(struct vxlan_dev *vxlan)
2133 unsigned int h;
2135 spin_lock_bh(&vxlan->hash_lock);
2136 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2137 struct hlist_node *p, *n;
2138 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2139 struct vxlan_fdb *f
2140 = container_of(p, struct vxlan_fdb, hlist);
2141 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2142 if (!is_zero_ether_addr(f->eth_addr))
2143 vxlan_fdb_destroy(vxlan, f);
2146 spin_unlock_bh(&vxlan->hash_lock);
2149 /* Cleanup timer and forwarding table on shutdown */
2150 static int vxlan_stop(struct net_device *dev)
2152 struct vxlan_dev *vxlan = netdev_priv(dev);
2153 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2154 struct vxlan_sock *vs = vxlan->vn_sock;
2156 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2157 !vxlan_group_used(vn, vxlan)) {
2158 vxlan_sock_hold(vs);
2159 dev_hold(dev);
2160 queue_work(vxlan_wq, &vxlan->igmp_leave);
2163 del_timer_sync(&vxlan->age_timer);
2165 vxlan_flush(vxlan);
2167 return 0;
2170 /* Stub, nothing needs to be done. */
2171 static void vxlan_set_multicast_list(struct net_device *dev)
2175 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2177 struct vxlan_dev *vxlan = netdev_priv(dev);
2178 struct vxlan_rdst *dst = &vxlan->default_dst;
2179 struct net_device *lowerdev;
2180 int max_mtu;
2182 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
2183 if (lowerdev == NULL)
2184 return eth_change_mtu(dev, new_mtu);
2186 if (dst->remote_ip.sa.sa_family == AF_INET6)
2187 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2188 else
2189 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2191 if (new_mtu < 68 || new_mtu > max_mtu)
2192 return -EINVAL;
2194 dev->mtu = new_mtu;
2195 return 0;
2198 static const struct net_device_ops vxlan_netdev_ops = {
2199 .ndo_init = vxlan_init,
2200 .ndo_uninit = vxlan_uninit,
2201 .ndo_open = vxlan_open,
2202 .ndo_stop = vxlan_stop,
2203 .ndo_start_xmit = vxlan_xmit,
2204 .ndo_get_stats64 = ip_tunnel_get_stats64,
2205 .ndo_set_rx_mode = vxlan_set_multicast_list,
2206 .ndo_change_mtu = vxlan_change_mtu,
2207 .ndo_validate_addr = eth_validate_addr,
2208 .ndo_set_mac_address = eth_mac_addr,
2209 .ndo_fdb_add = vxlan_fdb_add,
2210 .ndo_fdb_del = vxlan_fdb_delete,
2211 .ndo_fdb_dump = vxlan_fdb_dump,
2214 /* Info for udev, that this is a virtual tunnel endpoint */
2215 static struct device_type vxlan_type = {
2216 .name = "vxlan",
2219 /* Calls the ndo_add_vxlan_port of the caller in order to
2220 * supply the listening VXLAN udp ports. Callers are expected
2221 * to implement the ndo_add_vxlan_port.
2223 void vxlan_get_rx_port(struct net_device *dev)
2225 struct vxlan_sock *vs;
2226 struct net *net = dev_net(dev);
2227 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2228 sa_family_t sa_family;
2229 __be16 port;
2230 unsigned int i;
2232 spin_lock(&vn->sock_lock);
2233 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2234 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2235 port = inet_sk(vs->sock->sk)->inet_sport;
2236 sa_family = vs->sock->sk->sk_family;
2237 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2238 port);
2241 spin_unlock(&vn->sock_lock);
2243 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2245 /* Initialize the device structure. */
2246 static void vxlan_setup(struct net_device *dev)
2248 struct vxlan_dev *vxlan = netdev_priv(dev);
2249 unsigned int h;
2250 int low, high;
2252 eth_hw_addr_random(dev);
2253 ether_setup(dev);
2255 dev->netdev_ops = &vxlan_netdev_ops;
2256 dev->destructor = free_netdev;
2257 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2259 dev->tx_queue_len = 0;
2260 dev->features |= NETIF_F_LLTX;
2261 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2262 dev->features |= NETIF_F_RXCSUM;
2263 dev->features |= NETIF_F_GSO_SOFTWARE;
2265 dev->vlan_features = dev->features;
2266 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2267 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2268 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2269 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2270 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
2271 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2273 INIT_LIST_HEAD(&vxlan->next);
2274 spin_lock_init(&vxlan->hash_lock);
2275 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2276 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
2277 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
2279 init_timer_deferrable(&vxlan->age_timer);
2280 vxlan->age_timer.function = vxlan_cleanup;
2281 vxlan->age_timer.data = (unsigned long) vxlan;
2283 inet_get_local_port_range(dev_net(dev), &low, &high);
2284 vxlan->port_min = low;
2285 vxlan->port_max = high;
2286 vxlan->dst_port = htons(vxlan_port);
2288 vxlan->dev = dev;
2290 for (h = 0; h < FDB_HASH_SIZE; ++h)
2291 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2294 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2295 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2296 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2297 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2298 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2299 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2300 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2301 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2302 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2303 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2304 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2305 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2306 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2307 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2308 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2309 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2310 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2311 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2314 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2316 if (tb[IFLA_ADDRESS]) {
2317 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2318 pr_debug("invalid link address (not ethernet)\n");
2319 return -EINVAL;
2322 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2323 pr_debug("invalid all zero ethernet address\n");
2324 return -EADDRNOTAVAIL;
2328 if (!data)
2329 return -EINVAL;
2331 if (data[IFLA_VXLAN_ID]) {
2332 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2333 if (id >= VXLAN_VID_MASK)
2334 return -ERANGE;
2337 if (data[IFLA_VXLAN_PORT_RANGE]) {
2338 const struct ifla_vxlan_port_range *p
2339 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2341 if (ntohs(p->high) < ntohs(p->low)) {
2342 pr_debug("port range %u .. %u not valid\n",
2343 ntohs(p->low), ntohs(p->high));
2344 return -EINVAL;
2348 return 0;
2351 static void vxlan_get_drvinfo(struct net_device *netdev,
2352 struct ethtool_drvinfo *drvinfo)
2354 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2355 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2358 static const struct ethtool_ops vxlan_ethtool_ops = {
2359 .get_drvinfo = vxlan_get_drvinfo,
2360 .get_link = ethtool_op_get_link,
2363 static void vxlan_del_work(struct work_struct *work)
2365 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2367 sk_release_kernel(vs->sock->sk);
2368 kfree_rcu(vs, rcu);
2371 #if IS_ENABLED(CONFIG_IPV6)
2372 /* Create UDP socket for encapsulation receive. AF_INET6 socket
2373 * could be used for both IPv4 and IPv6 communications, but
2374 * users may set bindv6only=1.
2376 static struct socket *create_v6_sock(struct net *net, __be16 port, u32 flags)
2378 struct sock *sk;
2379 struct socket *sock;
2380 struct sockaddr_in6 vxlan_addr = {
2381 .sin6_family = AF_INET6,
2382 .sin6_port = port,
2384 int rc, val = 1;
2386 rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
2387 if (rc < 0) {
2388 pr_debug("UDPv6 socket create failed\n");
2389 return ERR_PTR(rc);
2392 /* Put in proper namespace */
2393 sk = sock->sk;
2394 sk_change_net(sk, net);
2396 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
2397 (char *)&val, sizeof(val));
2398 rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
2399 sizeof(struct sockaddr_in6));
2400 if (rc < 0) {
2401 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2402 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
2403 sk_release_kernel(sk);
2404 return ERR_PTR(rc);
2406 /* At this point, IPv6 module should have been loaded in
2407 * sock_create_kern().
2409 BUG_ON(!ipv6_stub);
2411 /* Disable multicast loopback */
2412 inet_sk(sk)->mc_loop = 0;
2414 if (flags & VXLAN_F_UDP_ZERO_CSUM6_TX)
2415 udp_set_no_check6_tx(sk, true);
2417 if (flags & VXLAN_F_UDP_ZERO_CSUM6_RX)
2418 udp_set_no_check6_rx(sk, true);
2420 return sock;
2423 #else
2425 static struct socket *create_v6_sock(struct net *net, __be16 port, u32 flags)
2427 return ERR_PTR(-EPFNOSUPPORT);
2429 #endif
2431 static struct socket *create_v4_sock(struct net *net, __be16 port, u32 flags)
2433 struct sock *sk;
2434 struct socket *sock;
2435 struct sockaddr_in vxlan_addr = {
2436 .sin_family = AF_INET,
2437 .sin_addr.s_addr = htonl(INADDR_ANY),
2438 .sin_port = port,
2440 int rc;
2442 /* Create UDP socket for encapsulation receive. */
2443 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
2444 if (rc < 0) {
2445 pr_debug("UDP socket create failed\n");
2446 return ERR_PTR(rc);
2449 /* Put in proper namespace */
2450 sk = sock->sk;
2451 sk_change_net(sk, net);
2453 rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
2454 sizeof(vxlan_addr));
2455 if (rc < 0) {
2456 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2457 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
2458 sk_release_kernel(sk);
2459 return ERR_PTR(rc);
2462 /* Disable multicast loopback */
2463 inet_sk(sk)->mc_loop = 0;
2465 if (!(flags & VXLAN_F_UDP_CSUM))
2466 sock->sk->sk_no_check_tx = 1;
2468 return sock;
2471 /* Create new listen socket if needed */
2472 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2473 vxlan_rcv_t *rcv, void *data,
2474 u32 flags)
2476 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2477 struct vxlan_sock *vs;
2478 struct socket *sock;
2479 struct sock *sk;
2480 unsigned int h;
2481 bool ipv6 = !!(flags & VXLAN_F_IPV6);
2483 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2484 if (!vs)
2485 return ERR_PTR(-ENOMEM);
2487 for (h = 0; h < VNI_HASH_SIZE; ++h)
2488 INIT_HLIST_HEAD(&vs->vni_list[h]);
2490 INIT_WORK(&vs->del_work, vxlan_del_work);
2492 if (ipv6)
2493 sock = create_v6_sock(net, port, flags);
2494 else
2495 sock = create_v4_sock(net, port, flags);
2496 if (IS_ERR(sock)) {
2497 kfree(vs);
2498 return ERR_CAST(sock);
2501 vs->sock = sock;
2502 sk = sock->sk;
2503 atomic_set(&vs->refcnt, 1);
2504 vs->rcv = rcv;
2505 vs->data = data;
2506 rcu_assign_sk_user_data(vs->sock->sk, vs);
2508 /* Initialize the vxlan udp offloads structure */
2509 vs->udp_offloads.port = port;
2510 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2511 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2513 spin_lock(&vn->sock_lock);
2514 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2515 vxlan_notify_add_rx_port(vs);
2516 spin_unlock(&vn->sock_lock);
2518 /* Mark socket as an encapsulation socket. */
2519 udp_sk(sk)->encap_type = 1;
2520 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
2521 #if IS_ENABLED(CONFIG_IPV6)
2522 if (ipv6)
2523 ipv6_stub->udpv6_encap_enable();
2524 else
2525 #endif
2526 udp_encap_enable();
2528 return vs;
2531 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2532 vxlan_rcv_t *rcv, void *data,
2533 bool no_share, u32 flags)
2535 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2536 struct vxlan_sock *vs;
2537 bool ipv6 = flags & VXLAN_F_IPV6;
2539 vs = vxlan_socket_create(net, port, rcv, data, flags);
2540 if (!IS_ERR(vs))
2541 return vs;
2543 if (no_share) /* Return error if sharing is not allowed. */
2544 return vs;
2546 spin_lock(&vn->sock_lock);
2547 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port);
2548 if (vs && ((vs->rcv != rcv) ||
2549 !atomic_add_unless(&vs->refcnt, 1, 0)))
2550 vs = ERR_PTR(-EBUSY);
2551 spin_unlock(&vn->sock_lock);
2553 if (!vs)
2554 vs = ERR_PTR(-EINVAL);
2556 return vs;
2558 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2560 /* Scheduled at device creation to bind to a socket */
2561 static void vxlan_sock_work(struct work_struct *work)
2563 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2564 struct net *net = vxlan->net;
2565 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2566 __be16 port = vxlan->dst_port;
2567 struct vxlan_sock *nvs;
2569 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
2570 spin_lock(&vn->sock_lock);
2571 if (!IS_ERR(nvs))
2572 vxlan_vs_add_dev(nvs, vxlan);
2573 spin_unlock(&vn->sock_lock);
2575 dev_put(vxlan->dev);
2578 static int vxlan_newlink(struct net *net, struct net_device *dev,
2579 struct nlattr *tb[], struct nlattr *data[])
2581 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2582 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
2583 struct vxlan_rdst *dst = &vxlan->default_dst;
2584 __u32 vni;
2585 int err;
2586 bool use_ipv6 = false;
2588 if (!data[IFLA_VXLAN_ID])
2589 return -EINVAL;
2591 vxlan->net = dev_net(dev);
2593 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2594 dst->remote_vni = vni;
2596 /* Unless IPv6 is explicitly requested, assume IPv4 */
2597 dst->remote_ip.sa.sa_family = AF_INET;
2598 if (data[IFLA_VXLAN_GROUP]) {
2599 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2600 } else if (data[IFLA_VXLAN_GROUP6]) {
2601 if (!IS_ENABLED(CONFIG_IPV6))
2602 return -EPFNOSUPPORT;
2604 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2605 sizeof(struct in6_addr));
2606 dst->remote_ip.sa.sa_family = AF_INET6;
2607 use_ipv6 = true;
2610 if (data[IFLA_VXLAN_LOCAL]) {
2611 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2612 vxlan->saddr.sa.sa_family = AF_INET;
2613 } else if (data[IFLA_VXLAN_LOCAL6]) {
2614 if (!IS_ENABLED(CONFIG_IPV6))
2615 return -EPFNOSUPPORT;
2617 /* TODO: respect scope id */
2618 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2619 sizeof(struct in6_addr));
2620 vxlan->saddr.sa.sa_family = AF_INET6;
2621 use_ipv6 = true;
2624 if (data[IFLA_VXLAN_LINK] &&
2625 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
2626 struct net_device *lowerdev
2627 = __dev_get_by_index(net, dst->remote_ifindex);
2629 if (!lowerdev) {
2630 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2631 return -ENODEV;
2634 #if IS_ENABLED(CONFIG_IPV6)
2635 if (use_ipv6) {
2636 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2637 if (idev && idev->cnf.disable_ipv6) {
2638 pr_info("IPv6 is disabled via sysctl\n");
2639 return -EPERM;
2641 vxlan->flags |= VXLAN_F_IPV6;
2643 #endif
2645 if (!tb[IFLA_MTU])
2646 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2648 dev->needed_headroom = lowerdev->hard_header_len +
2649 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2650 } else if (use_ipv6) {
2651 vxlan->flags |= VXLAN_F_IPV6;
2652 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
2653 } else {
2654 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
2657 if (data[IFLA_VXLAN_TOS])
2658 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2660 if (data[IFLA_VXLAN_TTL])
2661 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2663 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2664 vxlan->flags |= VXLAN_F_LEARN;
2666 if (data[IFLA_VXLAN_AGEING])
2667 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2668 else
2669 vxlan->age_interval = FDB_AGE_DEFAULT;
2671 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2672 vxlan->flags |= VXLAN_F_PROXY;
2674 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2675 vxlan->flags |= VXLAN_F_RSC;
2677 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2678 vxlan->flags |= VXLAN_F_L2MISS;
2680 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2681 vxlan->flags |= VXLAN_F_L3MISS;
2683 if (data[IFLA_VXLAN_LIMIT])
2684 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2686 if (data[IFLA_VXLAN_PORT_RANGE]) {
2687 const struct ifla_vxlan_port_range *p
2688 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2689 vxlan->port_min = ntohs(p->low);
2690 vxlan->port_max = ntohs(p->high);
2693 if (data[IFLA_VXLAN_PORT])
2694 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2696 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2697 vxlan->flags |= VXLAN_F_UDP_CSUM;
2699 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2700 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2701 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2703 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2704 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2705 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2707 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2708 if (tmp->default_dst.remote_vni == vni &&
2709 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2710 tmp->saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2711 tmp->dst_port == vxlan->dst_port &&
2712 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
2713 (vxlan->flags & VXLAN_F_RCV_FLAGS))
2714 return -EEXIST;
2717 dev->ethtool_ops = &vxlan_ethtool_ops;
2719 /* create an fdb entry for a valid default destination */
2720 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2721 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2722 &vxlan->default_dst.remote_ip,
2723 NUD_REACHABLE|NUD_PERMANENT,
2724 NLM_F_EXCL|NLM_F_CREATE,
2725 vxlan->dst_port,
2726 vxlan->default_dst.remote_vni,
2727 vxlan->default_dst.remote_ifindex,
2728 NTF_SELF);
2729 if (err)
2730 return err;
2733 err = register_netdevice(dev);
2734 if (err) {
2735 vxlan_fdb_delete_default(vxlan);
2736 return err;
2739 list_add(&vxlan->next, &vn->vxlan_list);
2741 return 0;
2744 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2746 struct vxlan_dev *vxlan = netdev_priv(dev);
2747 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2749 spin_lock(&vn->sock_lock);
2750 if (!hlist_unhashed(&vxlan->hlist))
2751 hlist_del_rcu(&vxlan->hlist);
2752 spin_unlock(&vn->sock_lock);
2754 list_del(&vxlan->next);
2755 unregister_netdevice_queue(dev, head);
2758 static size_t vxlan_get_size(const struct net_device *dev)
2761 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
2762 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2763 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2764 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2765 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2766 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2767 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
2768 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2769 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2770 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2771 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
2772 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2773 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2774 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2775 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2776 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2777 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2778 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2782 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2784 const struct vxlan_dev *vxlan = netdev_priv(dev);
2785 const struct vxlan_rdst *dst = &vxlan->default_dst;
2786 struct ifla_vxlan_port_range ports = {
2787 .low = htons(vxlan->port_min),
2788 .high = htons(vxlan->port_max),
2791 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2792 goto nla_put_failure;
2794 if (!vxlan_addr_any(&dst->remote_ip)) {
2795 if (dst->remote_ip.sa.sa_family == AF_INET) {
2796 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2797 dst->remote_ip.sin.sin_addr.s_addr))
2798 goto nla_put_failure;
2799 #if IS_ENABLED(CONFIG_IPV6)
2800 } else {
2801 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2802 &dst->remote_ip.sin6.sin6_addr))
2803 goto nla_put_failure;
2804 #endif
2808 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2809 goto nla_put_failure;
2811 if (!vxlan_addr_any(&vxlan->saddr)) {
2812 if (vxlan->saddr.sa.sa_family == AF_INET) {
2813 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2814 vxlan->saddr.sin.sin_addr.s_addr))
2815 goto nla_put_failure;
2816 #if IS_ENABLED(CONFIG_IPV6)
2817 } else {
2818 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2819 &vxlan->saddr.sin6.sin6_addr))
2820 goto nla_put_failure;
2821 #endif
2825 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2826 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
2827 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2828 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2829 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2830 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2831 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2832 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2833 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2834 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2835 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2836 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
2837 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2838 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2839 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2840 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2841 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2842 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2843 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2844 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)))
2845 goto nla_put_failure;
2847 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2848 goto nla_put_failure;
2850 return 0;
2852 nla_put_failure:
2853 return -EMSGSIZE;
2856 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2857 .kind = "vxlan",
2858 .maxtype = IFLA_VXLAN_MAX,
2859 .policy = vxlan_policy,
2860 .priv_size = sizeof(struct vxlan_dev),
2861 .setup = vxlan_setup,
2862 .validate = vxlan_validate,
2863 .newlink = vxlan_newlink,
2864 .dellink = vxlan_dellink,
2865 .get_size = vxlan_get_size,
2866 .fill_info = vxlan_fill_info,
2869 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2870 struct net_device *dev)
2872 struct vxlan_dev *vxlan, *next;
2873 LIST_HEAD(list_kill);
2875 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2876 struct vxlan_rdst *dst = &vxlan->default_dst;
2878 /* In case we created vxlan device with carrier
2879 * and we loose the carrier due to module unload
2880 * we also need to remove vxlan device. In other
2881 * cases, it's not necessary and remote_ifindex
2882 * is 0 here, so no matches.
2884 if (dst->remote_ifindex == dev->ifindex)
2885 vxlan_dellink(vxlan->dev, &list_kill);
2888 unregister_netdevice_many(&list_kill);
2891 static int vxlan_lowerdev_event(struct notifier_block *unused,
2892 unsigned long event, void *ptr)
2894 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2895 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2897 if (event == NETDEV_UNREGISTER)
2898 vxlan_handle_lowerdev_unregister(vn, dev);
2900 return NOTIFY_DONE;
2903 static struct notifier_block vxlan_notifier_block __read_mostly = {
2904 .notifier_call = vxlan_lowerdev_event,
2907 static __net_init int vxlan_init_net(struct net *net)
2909 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2910 unsigned int h;
2912 INIT_LIST_HEAD(&vn->vxlan_list);
2913 spin_lock_init(&vn->sock_lock);
2915 for (h = 0; h < PORT_HASH_SIZE; ++h)
2916 INIT_HLIST_HEAD(&vn->sock_list[h]);
2918 return 0;
2921 static void __net_exit vxlan_exit_net(struct net *net)
2923 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2924 struct vxlan_dev *vxlan, *next;
2925 struct net_device *dev, *aux;
2926 LIST_HEAD(list);
2928 rtnl_lock();
2929 for_each_netdev_safe(net, dev, aux)
2930 if (dev->rtnl_link_ops == &vxlan_link_ops)
2931 unregister_netdevice_queue(dev, &list);
2933 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2934 /* If vxlan->dev is in the same netns, it has already been added
2935 * to the list by the previous loop.
2937 if (!net_eq(dev_net(vxlan->dev), net))
2938 unregister_netdevice_queue(dev, &list);
2941 unregister_netdevice_many(&list);
2942 rtnl_unlock();
2945 static struct pernet_operations vxlan_net_ops = {
2946 .init = vxlan_init_net,
2947 .exit = vxlan_exit_net,
2948 .id = &vxlan_net_id,
2949 .size = sizeof(struct vxlan_net),
2952 static int __init vxlan_init_module(void)
2954 int rc;
2956 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2957 if (!vxlan_wq)
2958 return -ENOMEM;
2960 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2962 rc = register_pernet_subsys(&vxlan_net_ops);
2963 if (rc)
2964 goto out1;
2966 rc = register_netdevice_notifier(&vxlan_notifier_block);
2967 if (rc)
2968 goto out2;
2970 rc = rtnl_link_register(&vxlan_link_ops);
2971 if (rc)
2972 goto out3;
2974 return 0;
2975 out3:
2976 unregister_netdevice_notifier(&vxlan_notifier_block);
2977 out2:
2978 unregister_pernet_subsys(&vxlan_net_ops);
2979 out1:
2980 destroy_workqueue(vxlan_wq);
2981 return rc;
2983 late_initcall(vxlan_init_module);
2985 static void __exit vxlan_cleanup_module(void)
2987 rtnl_link_unregister(&vxlan_link_ops);
2988 unregister_netdevice_notifier(&vxlan_notifier_block);
2989 destroy_workqueue(vxlan_wq);
2990 unregister_pernet_subsys(&vxlan_net_ops);
2991 /* rcu_barrier() is called by netns */
2993 module_exit(vxlan_cleanup_module);
2995 MODULE_LICENSE("GPL");
2996 MODULE_VERSION(VXLAN_VERSION);
2997 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2998 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
2999 MODULE_ALIAS_RTNL_LINK("vxlan");