VXLAN: Use UDP Tunnel segmention.
[linux-2.6/btrfs-unstable.git] / drivers / net / vxlan.c
blobf057ec00bba39c71769fcb24a112bd3860fa3bfe
1 /*
2 * VXLAN: Virtual eXtensible Local Area Network
4 * Copyright (c) 2012 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.
10 * TODO
11 * - use IANA UDP port number (when defined)
12 * - IPv6 (not in RFC)
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/skbuff.h>
23 #include <linux/rculist.h>
24 #include <linux/netdevice.h>
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/udp.h>
28 #include <linux/igmp.h>
29 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/hash.h>
32 #include <linux/ethtool.h>
33 #include <net/arp.h>
34 #include <net/ndisc.h>
35 #include <net/ip.h>
36 #include <net/ipip.h>
37 #include <net/icmp.h>
38 #include <net/udp.h>
39 #include <net/rtnetlink.h>
40 #include <net/route.h>
41 #include <net/dsfield.h>
42 #include <net/inet_ecn.h>
43 #include <net/net_namespace.h>
44 #include <net/netns/generic.h>
46 #define VXLAN_VERSION "0.1"
48 #define VNI_HASH_BITS 10
49 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
50 #define FDB_HASH_BITS 8
51 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
52 #define FDB_AGE_DEFAULT 300 /* 5 min */
53 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
55 #define VXLAN_N_VID (1u << 24)
56 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
57 /* IP header + UDP + VXLAN + Ethernet header */
58 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
60 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
62 /* VXLAN protocol header */
63 struct vxlanhdr {
64 __be32 vx_flags;
65 __be32 vx_vni;
68 /* UDP port for VXLAN traffic. */
69 static unsigned int vxlan_port __read_mostly = 8472;
70 module_param_named(udp_port, vxlan_port, uint, 0444);
71 MODULE_PARM_DESC(udp_port, "Destination UDP port");
73 static bool log_ecn_error = true;
74 module_param(log_ecn_error, bool, 0644);
75 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
77 /* per-net private data for this module */
78 static unsigned int vxlan_net_id;
79 struct vxlan_net {
80 struct socket *sock; /* UDP encap socket */
81 struct hlist_head vni_list[VNI_HASH_SIZE];
84 /* Forwarding table entry */
85 struct vxlan_fdb {
86 struct hlist_node hlist; /* linked list of entries */
87 struct rcu_head rcu;
88 unsigned long updated; /* jiffies */
89 unsigned long used;
90 __be32 remote_ip;
91 u16 state; /* see ndm_state */
92 u8 eth_addr[ETH_ALEN];
95 /* Per-cpu network traffic stats */
96 struct vxlan_stats {
97 u64 rx_packets;
98 u64 rx_bytes;
99 u64 tx_packets;
100 u64 tx_bytes;
101 struct u64_stats_sync syncp;
104 /* Pseudo network device */
105 struct vxlan_dev {
106 struct hlist_node hlist;
107 struct net_device *dev;
108 struct vxlan_stats __percpu *stats;
109 __u32 vni; /* virtual network id */
110 __be32 gaddr; /* multicast group */
111 __be32 saddr; /* source address */
112 unsigned int link; /* link to multicast over */
113 __u16 port_min; /* source port range */
114 __u16 port_max;
115 __u8 tos; /* TOS override */
116 __u8 ttl;
117 u32 flags; /* VXLAN_F_* below */
119 unsigned long age_interval;
120 struct timer_list age_timer;
121 spinlock_t hash_lock;
122 unsigned int addrcnt;
123 unsigned int addrmax;
125 struct hlist_head fdb_head[FDB_HASH_SIZE];
128 #define VXLAN_F_LEARN 0x01
129 #define VXLAN_F_PROXY 0x02
130 #define VXLAN_F_RSC 0x04
131 #define VXLAN_F_L2MISS 0x08
132 #define VXLAN_F_L3MISS 0x10
134 /* salt for hash table */
135 static u32 vxlan_salt __read_mostly;
137 static inline struct hlist_head *vni_head(struct net *net, u32 id)
139 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
141 return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
144 /* Look up VNI in a per net namespace table */
145 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
147 struct vxlan_dev *vxlan;
149 hlist_for_each_entry_rcu(vxlan, vni_head(net, id), hlist) {
150 if (vxlan->vni == id)
151 return vxlan;
154 return NULL;
157 /* Fill in neighbour message in skbuff. */
158 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
159 const struct vxlan_fdb *fdb,
160 u32 portid, u32 seq, int type, unsigned int flags)
162 unsigned long now = jiffies;
163 struct nda_cacheinfo ci;
164 struct nlmsghdr *nlh;
165 struct ndmsg *ndm;
166 bool send_ip, send_eth;
168 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
169 if (nlh == NULL)
170 return -EMSGSIZE;
172 ndm = nlmsg_data(nlh);
173 memset(ndm, 0, sizeof(*ndm));
175 send_eth = send_ip = true;
177 if (type == RTM_GETNEIGH) {
178 ndm->ndm_family = AF_INET;
179 send_ip = fdb->remote_ip != 0;
180 send_eth = !is_zero_ether_addr(fdb->eth_addr);
181 } else
182 ndm->ndm_family = AF_BRIDGE;
183 ndm->ndm_state = fdb->state;
184 ndm->ndm_ifindex = vxlan->dev->ifindex;
185 ndm->ndm_flags = NTF_SELF;
186 ndm->ndm_type = NDA_DST;
188 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
189 goto nla_put_failure;
191 if (send_ip && nla_put_be32(skb, NDA_DST, fdb->remote_ip))
192 goto nla_put_failure;
194 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
195 ci.ndm_confirmed = 0;
196 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
197 ci.ndm_refcnt = 0;
199 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
200 goto nla_put_failure;
202 return nlmsg_end(skb, nlh);
204 nla_put_failure:
205 nlmsg_cancel(skb, nlh);
206 return -EMSGSIZE;
209 static inline size_t vxlan_nlmsg_size(void)
211 return NLMSG_ALIGN(sizeof(struct ndmsg))
212 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
213 + nla_total_size(sizeof(__be32)) /* NDA_DST */
214 + nla_total_size(sizeof(struct nda_cacheinfo));
217 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
218 const struct vxlan_fdb *fdb, int type)
220 struct net *net = dev_net(vxlan->dev);
221 struct sk_buff *skb;
222 int err = -ENOBUFS;
224 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
225 if (skb == NULL)
226 goto errout;
228 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0);
229 if (err < 0) {
230 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
231 WARN_ON(err == -EMSGSIZE);
232 kfree_skb(skb);
233 goto errout;
236 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
237 return;
238 errout:
239 if (err < 0)
240 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
243 static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
245 struct vxlan_dev *vxlan = netdev_priv(dev);
246 struct vxlan_fdb f;
248 memset(&f, 0, sizeof f);
249 f.state = NUD_STALE;
250 f.remote_ip = ipa; /* goes to NDA_DST */
252 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
255 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
257 struct vxlan_fdb f;
259 memset(&f, 0, sizeof f);
260 f.state = NUD_STALE;
261 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
263 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
266 /* Hash Ethernet address */
267 static u32 eth_hash(const unsigned char *addr)
269 u64 value = get_unaligned((u64 *)addr);
271 /* only want 6 bytes */
272 #ifdef __BIG_ENDIAN
273 value >>= 16;
274 #else
275 value <<= 16;
276 #endif
277 return hash_64(value, FDB_HASH_BITS);
280 /* Hash chain to use given mac address */
281 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
282 const u8 *mac)
284 return &vxlan->fdb_head[eth_hash(mac)];
287 /* Look up Ethernet address in forwarding table */
288 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
289 const u8 *mac)
292 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
293 struct vxlan_fdb *f;
295 hlist_for_each_entry_rcu(f, head, hlist) {
296 if (compare_ether_addr(mac, f->eth_addr) == 0)
297 return f;
300 return NULL;
303 /* Add new entry to forwarding table -- assumes lock held */
304 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
305 const u8 *mac, __be32 ip,
306 __u16 state, __u16 flags)
308 struct vxlan_fdb *f;
309 int notify = 0;
311 f = vxlan_find_mac(vxlan, mac);
312 if (f) {
313 if (flags & NLM_F_EXCL) {
314 netdev_dbg(vxlan->dev,
315 "lost race to create %pM\n", mac);
316 return -EEXIST;
318 if (f->state != state) {
319 f->state = state;
320 f->updated = jiffies;
321 notify = 1;
323 } else {
324 if (!(flags & NLM_F_CREATE))
325 return -ENOENT;
327 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
328 return -ENOSPC;
330 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
331 f = kmalloc(sizeof(*f), GFP_ATOMIC);
332 if (!f)
333 return -ENOMEM;
335 notify = 1;
336 f->remote_ip = ip;
337 f->state = state;
338 f->updated = f->used = jiffies;
339 memcpy(f->eth_addr, mac, ETH_ALEN);
341 ++vxlan->addrcnt;
342 hlist_add_head_rcu(&f->hlist,
343 vxlan_fdb_head(vxlan, mac));
346 if (notify)
347 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
349 return 0;
352 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
354 netdev_dbg(vxlan->dev,
355 "delete %pM\n", f->eth_addr);
357 --vxlan->addrcnt;
358 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
360 hlist_del_rcu(&f->hlist);
361 kfree_rcu(f, rcu);
364 /* Add static entry (via netlink) */
365 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
366 struct net_device *dev,
367 const unsigned char *addr, u16 flags)
369 struct vxlan_dev *vxlan = netdev_priv(dev);
370 __be32 ip;
371 int err;
373 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
374 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
375 ndm->ndm_state);
376 return -EINVAL;
379 if (tb[NDA_DST] == NULL)
380 return -EINVAL;
382 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
383 return -EAFNOSUPPORT;
385 ip = nla_get_be32(tb[NDA_DST]);
387 spin_lock_bh(&vxlan->hash_lock);
388 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags);
389 spin_unlock_bh(&vxlan->hash_lock);
391 return err;
394 /* Delete entry (via netlink) */
395 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
396 struct net_device *dev,
397 const unsigned char *addr)
399 struct vxlan_dev *vxlan = netdev_priv(dev);
400 struct vxlan_fdb *f;
401 int err = -ENOENT;
403 spin_lock_bh(&vxlan->hash_lock);
404 f = vxlan_find_mac(vxlan, addr);
405 if (f) {
406 vxlan_fdb_destroy(vxlan, f);
407 err = 0;
409 spin_unlock_bh(&vxlan->hash_lock);
411 return err;
414 /* Dump forwarding table */
415 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
416 struct net_device *dev, int idx)
418 struct vxlan_dev *vxlan = netdev_priv(dev);
419 unsigned int h;
421 for (h = 0; h < FDB_HASH_SIZE; ++h) {
422 struct vxlan_fdb *f;
423 int err;
425 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
426 if (idx < cb->args[0])
427 goto skip;
429 err = vxlan_fdb_info(skb, vxlan, f,
430 NETLINK_CB(cb->skb).portid,
431 cb->nlh->nlmsg_seq,
432 RTM_NEWNEIGH,
433 NLM_F_MULTI);
434 if (err < 0)
435 break;
436 skip:
437 ++idx;
441 return idx;
444 /* Watch incoming packets to learn mapping between Ethernet address
445 * and Tunnel endpoint.
447 static void vxlan_snoop(struct net_device *dev,
448 __be32 src_ip, const u8 *src_mac)
450 struct vxlan_dev *vxlan = netdev_priv(dev);
451 struct vxlan_fdb *f;
452 int err;
454 f = vxlan_find_mac(vxlan, src_mac);
455 if (likely(f)) {
456 f->used = jiffies;
457 if (likely(f->remote_ip == src_ip))
458 return;
460 if (net_ratelimit())
461 netdev_info(dev,
462 "%pM migrated from %pI4 to %pI4\n",
463 src_mac, &f->remote_ip, &src_ip);
465 f->remote_ip = src_ip;
466 f->updated = jiffies;
467 } else {
468 /* learned new entry */
469 spin_lock(&vxlan->hash_lock);
470 err = vxlan_fdb_create(vxlan, src_mac, src_ip,
471 NUD_REACHABLE,
472 NLM_F_EXCL|NLM_F_CREATE);
473 spin_unlock(&vxlan->hash_lock);
478 /* See if multicast group is already in use by other ID */
479 static bool vxlan_group_used(struct vxlan_net *vn,
480 const struct vxlan_dev *this)
482 const struct vxlan_dev *vxlan;
483 unsigned h;
485 for (h = 0; h < VNI_HASH_SIZE; ++h)
486 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) {
487 if (vxlan == this)
488 continue;
490 if (!netif_running(vxlan->dev))
491 continue;
493 if (vxlan->gaddr == this->gaddr)
494 return true;
497 return false;
500 /* kernel equivalent to IP_ADD_MEMBERSHIP */
501 static int vxlan_join_group(struct net_device *dev)
503 struct vxlan_dev *vxlan = netdev_priv(dev);
504 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
505 struct sock *sk = vn->sock->sk;
506 struct ip_mreqn mreq = {
507 .imr_multiaddr.s_addr = vxlan->gaddr,
508 .imr_ifindex = vxlan->link,
510 int err;
512 /* Already a member of group */
513 if (vxlan_group_used(vn, vxlan))
514 return 0;
516 /* Need to drop RTNL to call multicast join */
517 rtnl_unlock();
518 lock_sock(sk);
519 err = ip_mc_join_group(sk, &mreq);
520 release_sock(sk);
521 rtnl_lock();
523 return err;
527 /* kernel equivalent to IP_DROP_MEMBERSHIP */
528 static int vxlan_leave_group(struct net_device *dev)
530 struct vxlan_dev *vxlan = netdev_priv(dev);
531 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
532 int err = 0;
533 struct sock *sk = vn->sock->sk;
534 struct ip_mreqn mreq = {
535 .imr_multiaddr.s_addr = vxlan->gaddr,
536 .imr_ifindex = vxlan->link,
539 /* Only leave group when last vxlan is done. */
540 if (vxlan_group_used(vn, vxlan))
541 return 0;
543 /* Need to drop RTNL to call multicast leave */
544 rtnl_unlock();
545 lock_sock(sk);
546 err = ip_mc_leave_group(sk, &mreq);
547 release_sock(sk);
548 rtnl_lock();
550 return err;
553 /* Callback from net/ipv4/udp.c to receive packets */
554 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
556 struct iphdr *oip;
557 struct vxlanhdr *vxh;
558 struct vxlan_dev *vxlan;
559 struct vxlan_stats *stats;
560 __u32 vni;
561 int err;
563 /* pop off outer UDP header */
564 __skb_pull(skb, sizeof(struct udphdr));
566 /* Need Vxlan and inner Ethernet header to be present */
567 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
568 goto error;
570 /* Drop packets with reserved bits set */
571 vxh = (struct vxlanhdr *) skb->data;
572 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
573 (vxh->vx_vni & htonl(0xff))) {
574 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
575 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
576 goto error;
579 __skb_pull(skb, sizeof(struct vxlanhdr));
581 /* Is this VNI defined? */
582 vni = ntohl(vxh->vx_vni) >> 8;
583 vxlan = vxlan_find_vni(sock_net(sk), vni);
584 if (!vxlan) {
585 netdev_dbg(skb->dev, "unknown vni %d\n", vni);
586 goto drop;
589 if (!pskb_may_pull(skb, ETH_HLEN)) {
590 vxlan->dev->stats.rx_length_errors++;
591 vxlan->dev->stats.rx_errors++;
592 goto drop;
595 skb_reset_mac_header(skb);
597 /* Re-examine inner Ethernet packet */
598 oip = ip_hdr(skb);
599 skb->protocol = eth_type_trans(skb, vxlan->dev);
601 /* Ignore packet loops (and multicast echo) */
602 if (compare_ether_addr(eth_hdr(skb)->h_source,
603 vxlan->dev->dev_addr) == 0)
604 goto drop;
606 if (vxlan->flags & VXLAN_F_LEARN)
607 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
609 __skb_tunnel_rx(skb, vxlan->dev);
610 skb_reset_network_header(skb);
612 /* If the NIC driver gave us an encapsulated packet with
613 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
614 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
615 * for us. Otherwise force the upper layers to verify it.
617 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
618 !(vxlan->dev->features & NETIF_F_RXCSUM))
619 skb->ip_summed = CHECKSUM_NONE;
621 skb->encapsulation = 0;
623 err = IP_ECN_decapsulate(oip, skb);
624 if (unlikely(err)) {
625 if (log_ecn_error)
626 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
627 &oip->saddr, oip->tos);
628 if (err > 1) {
629 ++vxlan->dev->stats.rx_frame_errors;
630 ++vxlan->dev->stats.rx_errors;
631 goto drop;
635 stats = this_cpu_ptr(vxlan->stats);
636 u64_stats_update_begin(&stats->syncp);
637 stats->rx_packets++;
638 stats->rx_bytes += skb->len;
639 u64_stats_update_end(&stats->syncp);
641 netif_rx(skb);
643 return 0;
644 error:
645 /* Put UDP header back */
646 __skb_push(skb, sizeof(struct udphdr));
648 return 1;
649 drop:
650 /* Consume bad packet */
651 kfree_skb(skb);
652 return 0;
655 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
657 struct vxlan_dev *vxlan = netdev_priv(dev);
658 struct arphdr *parp;
659 u8 *arpptr, *sha;
660 __be32 sip, tip;
661 struct neighbour *n;
663 if (dev->flags & IFF_NOARP)
664 goto out;
666 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
667 dev->stats.tx_dropped++;
668 goto out;
670 parp = arp_hdr(skb);
672 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
673 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
674 parp->ar_pro != htons(ETH_P_IP) ||
675 parp->ar_op != htons(ARPOP_REQUEST) ||
676 parp->ar_hln != dev->addr_len ||
677 parp->ar_pln != 4)
678 goto out;
679 arpptr = (u8 *)parp + sizeof(struct arphdr);
680 sha = arpptr;
681 arpptr += dev->addr_len; /* sha */
682 memcpy(&sip, arpptr, sizeof(sip));
683 arpptr += sizeof(sip);
684 arpptr += dev->addr_len; /* tha */
685 memcpy(&tip, arpptr, sizeof(tip));
687 if (ipv4_is_loopback(tip) ||
688 ipv4_is_multicast(tip))
689 goto out;
691 n = neigh_lookup(&arp_tbl, &tip, dev);
693 if (n) {
694 struct vxlan_dev *vxlan = netdev_priv(dev);
695 struct vxlan_fdb *f;
696 struct sk_buff *reply;
698 if (!(n->nud_state & NUD_CONNECTED)) {
699 neigh_release(n);
700 goto out;
703 f = vxlan_find_mac(vxlan, n->ha);
704 if (f && f->remote_ip == 0) {
705 /* bridge-local neighbor */
706 neigh_release(n);
707 goto out;
710 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
711 n->ha, sha);
713 neigh_release(n);
715 skb_reset_mac_header(reply);
716 __skb_pull(reply, skb_network_offset(reply));
717 reply->ip_summed = CHECKSUM_UNNECESSARY;
718 reply->pkt_type = PACKET_HOST;
720 if (netif_rx_ni(reply) == NET_RX_DROP)
721 dev->stats.rx_dropped++;
722 } else if (vxlan->flags & VXLAN_F_L3MISS)
723 vxlan_ip_miss(dev, tip);
724 out:
725 consume_skb(skb);
726 return NETDEV_TX_OK;
729 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
731 struct vxlan_dev *vxlan = netdev_priv(dev);
732 struct neighbour *n;
733 struct iphdr *pip;
735 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
736 return false;
738 n = NULL;
739 switch (ntohs(eth_hdr(skb)->h_proto)) {
740 case ETH_P_IP:
741 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
742 return false;
743 pip = ip_hdr(skb);
744 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
745 break;
746 default:
747 return false;
750 if (n) {
751 bool diff;
753 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
754 if (diff) {
755 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
756 dev->addr_len);
757 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
759 neigh_release(n);
760 return diff;
761 } else if (vxlan->flags & VXLAN_F_L3MISS)
762 vxlan_ip_miss(dev, pip->daddr);
763 return false;
766 /* Extract dsfield from inner protocol */
767 static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
768 const struct sk_buff *skb)
770 if (skb->protocol == htons(ETH_P_IP))
771 return iph->tos;
772 else if (skb->protocol == htons(ETH_P_IPV6))
773 return ipv6_get_dsfield((const struct ipv6hdr *)iph);
774 else
775 return 0;
778 /* Propogate ECN bits out */
779 static inline u8 vxlan_ecn_encap(u8 tos,
780 const struct iphdr *iph,
781 const struct sk_buff *skb)
783 u8 inner = vxlan_get_dsfield(iph, skb);
785 return INET_ECN_encapsulate(tos, inner);
788 static void vxlan_sock_free(struct sk_buff *skb)
790 sock_put(skb->sk);
793 /* On transmit, associate with the tunnel socket */
794 static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
796 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
797 struct sock *sk = vn->sock->sk;
799 skb_orphan(skb);
800 sock_hold(sk);
801 skb->sk = sk;
802 skb->destructor = vxlan_sock_free;
805 /* Compute source port for outgoing packet
806 * first choice to use L4 flow hash since it will spread
807 * better and maybe available from hardware
808 * secondary choice is to use jhash on the Ethernet header
810 static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
812 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
813 u32 hash;
815 hash = skb_get_rxhash(skb);
816 if (!hash)
817 hash = jhash(skb->data, 2 * ETH_ALEN,
818 (__force u32) skb->protocol);
820 return (((u64) hash * range) >> 32) + vxlan->port_min;
823 static int handle_offloads(struct sk_buff *skb)
825 if (skb_is_gso(skb)) {
826 int err = skb_unclone(skb, GFP_ATOMIC);
827 if (unlikely(err))
828 return err;
830 skb_shinfo(skb)->gso_type |= (SKB_GSO_UDP_TUNNEL | SKB_GSO_UDP);
831 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
832 skb->ip_summed = CHECKSUM_NONE;
834 return 0;
837 /* Transmit local packets over Vxlan
839 * Outer IP header inherits ECN and DF from inner header.
840 * Outer UDP destination is the VXLAN assigned port.
841 * source port is based on hash of flow
843 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
845 struct vxlan_dev *vxlan = netdev_priv(dev);
846 struct rtable *rt;
847 const struct iphdr *old_iph;
848 struct ethhdr *eth;
849 struct iphdr *iph;
850 struct vxlanhdr *vxh;
851 struct udphdr *uh;
852 struct flowi4 fl4;
853 unsigned int pkt_len = skb->len;
854 __be32 dst;
855 __u16 src_port;
856 __be16 df = 0;
857 __u8 tos, ttl;
858 int err;
859 bool did_rsc = false;
860 const struct vxlan_fdb *f;
862 skb_reset_mac_header(skb);
863 eth = eth_hdr(skb);
865 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
866 return arp_reduce(dev, skb);
867 else if ((vxlan->flags&VXLAN_F_RSC) && ntohs(eth->h_proto) == ETH_P_IP)
868 did_rsc = route_shortcircuit(dev, skb);
870 f = vxlan_find_mac(vxlan, eth->h_dest);
871 if (f == NULL) {
872 did_rsc = false;
873 dst = vxlan->gaddr;
874 if (!dst && (vxlan->flags & VXLAN_F_L2MISS) &&
875 !is_multicast_ether_addr(eth->h_dest))
876 vxlan_fdb_miss(vxlan, eth->h_dest);
877 } else
878 dst = f->remote_ip;
880 if (!dst) {
881 if (did_rsc) {
882 __skb_pull(skb, skb_network_offset(skb));
883 skb->ip_summed = CHECKSUM_NONE;
884 skb->pkt_type = PACKET_HOST;
886 /* short-circuited back to local bridge */
887 if (netif_rx(skb) == NET_RX_SUCCESS) {
888 struct vxlan_stats *stats =
889 this_cpu_ptr(vxlan->stats);
891 u64_stats_update_begin(&stats->syncp);
892 stats->tx_packets++;
893 stats->tx_bytes += pkt_len;
894 u64_stats_update_end(&stats->syncp);
895 } else {
896 dev->stats.tx_errors++;
897 dev->stats.tx_aborted_errors++;
899 return NETDEV_TX_OK;
901 goto drop;
904 if (!skb->encapsulation) {
905 skb_reset_inner_headers(skb);
906 skb->encapsulation = 1;
909 /* Need space for new headers (invalidates iph ptr) */
910 if (skb_cow_head(skb, VXLAN_HEADROOM))
911 goto drop;
913 old_iph = ip_hdr(skb);
915 ttl = vxlan->ttl;
916 if (!ttl && IN_MULTICAST(ntohl(dst)))
917 ttl = 1;
919 tos = vxlan->tos;
920 if (tos == 1)
921 tos = vxlan_get_dsfield(old_iph, skb);
923 src_port = vxlan_src_port(vxlan, skb);
925 memset(&fl4, 0, sizeof(fl4));
926 fl4.flowi4_oif = vxlan->link;
927 fl4.flowi4_tos = RT_TOS(tos);
928 fl4.daddr = dst;
929 fl4.saddr = vxlan->saddr;
931 rt = ip_route_output_key(dev_net(dev), &fl4);
932 if (IS_ERR(rt)) {
933 netdev_dbg(dev, "no route to %pI4\n", &dst);
934 dev->stats.tx_carrier_errors++;
935 goto tx_error;
938 if (rt->dst.dev == dev) {
939 netdev_dbg(dev, "circular route to %pI4\n", &dst);
940 ip_rt_put(rt);
941 dev->stats.collisions++;
942 goto tx_error;
945 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
946 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
947 IPSKB_REROUTED);
948 skb_dst_drop(skb);
949 skb_dst_set(skb, &rt->dst);
951 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
952 vxh->vx_flags = htonl(VXLAN_FLAGS);
953 vxh->vx_vni = htonl(vxlan->vni << 8);
955 __skb_push(skb, sizeof(*uh));
956 skb_reset_transport_header(skb);
957 uh = udp_hdr(skb);
959 uh->dest = htons(vxlan_port);
960 uh->source = htons(src_port);
962 uh->len = htons(skb->len);
963 uh->check = 0;
965 __skb_push(skb, sizeof(*iph));
966 skb_reset_network_header(skb);
967 iph = ip_hdr(skb);
968 iph->version = 4;
969 iph->ihl = sizeof(struct iphdr) >> 2;
970 iph->frag_off = df;
971 iph->protocol = IPPROTO_UDP;
972 iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
973 iph->daddr = dst;
974 iph->saddr = fl4.saddr;
975 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
976 tunnel_ip_select_ident(skb, old_iph, &rt->dst);
978 vxlan_set_owner(dev, skb);
980 if (handle_offloads(skb))
981 goto drop;
983 err = ip_local_out(skb);
984 if (likely(net_xmit_eval(err) == 0)) {
985 struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
987 u64_stats_update_begin(&stats->syncp);
988 stats->tx_packets++;
989 stats->tx_bytes += pkt_len;
990 u64_stats_update_end(&stats->syncp);
991 } else {
992 dev->stats.tx_errors++;
993 dev->stats.tx_aborted_errors++;
995 return NETDEV_TX_OK;
997 drop:
998 dev->stats.tx_dropped++;
999 goto tx_free;
1001 tx_error:
1002 dev->stats.tx_errors++;
1003 tx_free:
1004 dev_kfree_skb(skb);
1005 return NETDEV_TX_OK;
1008 /* Walk the forwarding table and purge stale entries */
1009 static void vxlan_cleanup(unsigned long arg)
1011 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1012 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1013 unsigned int h;
1015 if (!netif_running(vxlan->dev))
1016 return;
1018 spin_lock_bh(&vxlan->hash_lock);
1019 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1020 struct hlist_node *p, *n;
1021 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1022 struct vxlan_fdb *f
1023 = container_of(p, struct vxlan_fdb, hlist);
1024 unsigned long timeout;
1026 if (f->state & NUD_PERMANENT)
1027 continue;
1029 timeout = f->used + vxlan->age_interval * HZ;
1030 if (time_before_eq(timeout, jiffies)) {
1031 netdev_dbg(vxlan->dev,
1032 "garbage collect %pM\n",
1033 f->eth_addr);
1034 f->state = NUD_STALE;
1035 vxlan_fdb_destroy(vxlan, f);
1036 } else if (time_before(timeout, next_timer))
1037 next_timer = timeout;
1040 spin_unlock_bh(&vxlan->hash_lock);
1042 mod_timer(&vxlan->age_timer, next_timer);
1045 /* Setup stats when device is created */
1046 static int vxlan_init(struct net_device *dev)
1048 struct vxlan_dev *vxlan = netdev_priv(dev);
1050 vxlan->stats = alloc_percpu(struct vxlan_stats);
1051 if (!vxlan->stats)
1052 return -ENOMEM;
1054 return 0;
1057 /* Start ageing timer and join group when device is brought up */
1058 static int vxlan_open(struct net_device *dev)
1060 struct vxlan_dev *vxlan = netdev_priv(dev);
1061 int err;
1063 if (vxlan->gaddr) {
1064 err = vxlan_join_group(dev);
1065 if (err)
1066 return err;
1069 if (vxlan->age_interval)
1070 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1072 return 0;
1075 /* Purge the forwarding table */
1076 static void vxlan_flush(struct vxlan_dev *vxlan)
1078 unsigned h;
1080 spin_lock_bh(&vxlan->hash_lock);
1081 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1082 struct hlist_node *p, *n;
1083 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1084 struct vxlan_fdb *f
1085 = container_of(p, struct vxlan_fdb, hlist);
1086 vxlan_fdb_destroy(vxlan, f);
1089 spin_unlock_bh(&vxlan->hash_lock);
1092 /* Cleanup timer and forwarding table on shutdown */
1093 static int vxlan_stop(struct net_device *dev)
1095 struct vxlan_dev *vxlan = netdev_priv(dev);
1097 if (vxlan->gaddr)
1098 vxlan_leave_group(dev);
1100 del_timer_sync(&vxlan->age_timer);
1102 vxlan_flush(vxlan);
1104 return 0;
1107 /* Merge per-cpu statistics */
1108 static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
1109 struct rtnl_link_stats64 *stats)
1111 struct vxlan_dev *vxlan = netdev_priv(dev);
1112 struct vxlan_stats tmp, sum = { 0 };
1113 unsigned int cpu;
1115 for_each_possible_cpu(cpu) {
1116 unsigned int start;
1117 const struct vxlan_stats *stats
1118 = per_cpu_ptr(vxlan->stats, cpu);
1120 do {
1121 start = u64_stats_fetch_begin_bh(&stats->syncp);
1122 memcpy(&tmp, stats, sizeof(tmp));
1123 } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
1125 sum.tx_bytes += tmp.tx_bytes;
1126 sum.tx_packets += tmp.tx_packets;
1127 sum.rx_bytes += tmp.rx_bytes;
1128 sum.rx_packets += tmp.rx_packets;
1131 stats->tx_bytes = sum.tx_bytes;
1132 stats->tx_packets = sum.tx_packets;
1133 stats->rx_bytes = sum.rx_bytes;
1134 stats->rx_packets = sum.rx_packets;
1136 stats->multicast = dev->stats.multicast;
1137 stats->rx_length_errors = dev->stats.rx_length_errors;
1138 stats->rx_frame_errors = dev->stats.rx_frame_errors;
1139 stats->rx_errors = dev->stats.rx_errors;
1141 stats->tx_dropped = dev->stats.tx_dropped;
1142 stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
1143 stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
1144 stats->collisions = dev->stats.collisions;
1145 stats->tx_errors = dev->stats.tx_errors;
1147 return stats;
1150 /* Stub, nothing needs to be done. */
1151 static void vxlan_set_multicast_list(struct net_device *dev)
1155 static const struct net_device_ops vxlan_netdev_ops = {
1156 .ndo_init = vxlan_init,
1157 .ndo_open = vxlan_open,
1158 .ndo_stop = vxlan_stop,
1159 .ndo_start_xmit = vxlan_xmit,
1160 .ndo_get_stats64 = vxlan_stats64,
1161 .ndo_set_rx_mode = vxlan_set_multicast_list,
1162 .ndo_change_mtu = eth_change_mtu,
1163 .ndo_validate_addr = eth_validate_addr,
1164 .ndo_set_mac_address = eth_mac_addr,
1165 .ndo_fdb_add = vxlan_fdb_add,
1166 .ndo_fdb_del = vxlan_fdb_delete,
1167 .ndo_fdb_dump = vxlan_fdb_dump,
1170 /* Info for udev, that this is a virtual tunnel endpoint */
1171 static struct device_type vxlan_type = {
1172 .name = "vxlan",
1175 static void vxlan_free(struct net_device *dev)
1177 struct vxlan_dev *vxlan = netdev_priv(dev);
1179 free_percpu(vxlan->stats);
1180 free_netdev(dev);
1183 /* Initialize the device structure. */
1184 static void vxlan_setup(struct net_device *dev)
1186 struct vxlan_dev *vxlan = netdev_priv(dev);
1187 unsigned h;
1188 int low, high;
1190 eth_hw_addr_random(dev);
1191 ether_setup(dev);
1192 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1194 dev->netdev_ops = &vxlan_netdev_ops;
1195 dev->destructor = vxlan_free;
1196 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1198 dev->tx_queue_len = 0;
1199 dev->features |= NETIF_F_LLTX;
1200 dev->features |= NETIF_F_NETNS_LOCAL;
1201 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1202 dev->features |= NETIF_F_RXCSUM;
1203 dev->features |= NETIF_F_GSO_SOFTWARE;
1205 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1206 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1207 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1208 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1210 spin_lock_init(&vxlan->hash_lock);
1212 init_timer_deferrable(&vxlan->age_timer);
1213 vxlan->age_timer.function = vxlan_cleanup;
1214 vxlan->age_timer.data = (unsigned long) vxlan;
1216 inet_get_local_port_range(&low, &high);
1217 vxlan->port_min = low;
1218 vxlan->port_max = high;
1220 vxlan->dev = dev;
1222 for (h = 0; h < FDB_HASH_SIZE; ++h)
1223 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1226 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1227 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1228 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1229 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1230 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1231 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1232 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1233 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1234 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1235 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
1236 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
1237 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1238 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1239 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1240 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
1243 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1245 if (tb[IFLA_ADDRESS]) {
1246 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1247 pr_debug("invalid link address (not ethernet)\n");
1248 return -EINVAL;
1251 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1252 pr_debug("invalid all zero ethernet address\n");
1253 return -EADDRNOTAVAIL;
1257 if (!data)
1258 return -EINVAL;
1260 if (data[IFLA_VXLAN_ID]) {
1261 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1262 if (id >= VXLAN_VID_MASK)
1263 return -ERANGE;
1266 if (data[IFLA_VXLAN_GROUP]) {
1267 __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1268 if (!IN_MULTICAST(ntohl(gaddr))) {
1269 pr_debug("group address is not IPv4 multicast\n");
1270 return -EADDRNOTAVAIL;
1274 if (data[IFLA_VXLAN_PORT_RANGE]) {
1275 const struct ifla_vxlan_port_range *p
1276 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1278 if (ntohs(p->high) < ntohs(p->low)) {
1279 pr_debug("port range %u .. %u not valid\n",
1280 ntohs(p->low), ntohs(p->high));
1281 return -EINVAL;
1285 return 0;
1288 static void vxlan_get_drvinfo(struct net_device *netdev,
1289 struct ethtool_drvinfo *drvinfo)
1291 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1292 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1295 static const struct ethtool_ops vxlan_ethtool_ops = {
1296 .get_drvinfo = vxlan_get_drvinfo,
1297 .get_link = ethtool_op_get_link,
1300 static int vxlan_newlink(struct net *net, struct net_device *dev,
1301 struct nlattr *tb[], struct nlattr *data[])
1303 struct vxlan_dev *vxlan = netdev_priv(dev);
1304 __u32 vni;
1305 int err;
1307 if (!data[IFLA_VXLAN_ID])
1308 return -EINVAL;
1310 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1311 if (vxlan_find_vni(net, vni)) {
1312 pr_info("duplicate VNI %u\n", vni);
1313 return -EEXIST;
1315 vxlan->vni = vni;
1317 if (data[IFLA_VXLAN_GROUP])
1318 vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1320 if (data[IFLA_VXLAN_LOCAL])
1321 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1323 if (data[IFLA_VXLAN_LINK] &&
1324 (vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1325 struct net_device *lowerdev
1326 = __dev_get_by_index(net, vxlan->link);
1328 if (!lowerdev) {
1329 pr_info("ifindex %d does not exist\n", vxlan->link);
1330 return -ENODEV;
1333 if (!tb[IFLA_MTU])
1334 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1336 /* update header length based on lower device */
1337 dev->hard_header_len = lowerdev->hard_header_len +
1338 VXLAN_HEADROOM;
1341 if (data[IFLA_VXLAN_TOS])
1342 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1344 if (data[IFLA_VXLAN_TTL])
1345 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1347 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
1348 vxlan->flags |= VXLAN_F_LEARN;
1350 if (data[IFLA_VXLAN_AGEING])
1351 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1352 else
1353 vxlan->age_interval = FDB_AGE_DEFAULT;
1355 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1356 vxlan->flags |= VXLAN_F_PROXY;
1358 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1359 vxlan->flags |= VXLAN_F_RSC;
1361 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1362 vxlan->flags |= VXLAN_F_L2MISS;
1364 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1365 vxlan->flags |= VXLAN_F_L3MISS;
1367 if (data[IFLA_VXLAN_LIMIT])
1368 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1370 if (data[IFLA_VXLAN_PORT_RANGE]) {
1371 const struct ifla_vxlan_port_range *p
1372 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1373 vxlan->port_min = ntohs(p->low);
1374 vxlan->port_max = ntohs(p->high);
1377 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1379 err = register_netdevice(dev);
1380 if (!err)
1381 hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
1383 return err;
1386 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1388 struct vxlan_dev *vxlan = netdev_priv(dev);
1390 hlist_del_rcu(&vxlan->hlist);
1392 unregister_netdevice_queue(dev, head);
1395 static size_t vxlan_get_size(const struct net_device *dev)
1398 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1399 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
1400 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1401 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1402 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1403 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1404 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
1405 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1406 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1407 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1408 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
1409 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1410 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
1411 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
1415 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1417 const struct vxlan_dev *vxlan = netdev_priv(dev);
1418 struct ifla_vxlan_port_range ports = {
1419 .low = htons(vxlan->port_min),
1420 .high = htons(vxlan->port_max),
1423 if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
1424 goto nla_put_failure;
1426 if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
1427 goto nla_put_failure;
1429 if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
1430 goto nla_put_failure;
1432 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
1433 goto nla_put_failure;
1435 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1436 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
1437 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1438 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1439 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1440 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1441 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1442 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1443 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1444 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1445 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
1446 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1447 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
1448 goto nla_put_failure;
1450 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1451 goto nla_put_failure;
1453 return 0;
1455 nla_put_failure:
1456 return -EMSGSIZE;
1459 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1460 .kind = "vxlan",
1461 .maxtype = IFLA_VXLAN_MAX,
1462 .policy = vxlan_policy,
1463 .priv_size = sizeof(struct vxlan_dev),
1464 .setup = vxlan_setup,
1465 .validate = vxlan_validate,
1466 .newlink = vxlan_newlink,
1467 .dellink = vxlan_dellink,
1468 .get_size = vxlan_get_size,
1469 .fill_info = vxlan_fill_info,
1472 static __net_init int vxlan_init_net(struct net *net)
1474 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1475 struct sock *sk;
1476 struct sockaddr_in vxlan_addr = {
1477 .sin_family = AF_INET,
1478 .sin_addr.s_addr = htonl(INADDR_ANY),
1480 int rc;
1481 unsigned h;
1483 /* Create UDP socket for encapsulation receive. */
1484 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
1485 if (rc < 0) {
1486 pr_debug("UDP socket create failed\n");
1487 return rc;
1489 /* Put in proper namespace */
1490 sk = vn->sock->sk;
1491 sk_change_net(sk, net);
1493 vxlan_addr.sin_port = htons(vxlan_port);
1495 rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
1496 sizeof(vxlan_addr));
1497 if (rc < 0) {
1498 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1499 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1500 sk_release_kernel(sk);
1501 vn->sock = NULL;
1502 return rc;
1505 /* Disable multicast loopback */
1506 inet_sk(sk)->mc_loop = 0;
1508 /* Mark socket as an encapsulation socket. */
1509 udp_sk(sk)->encap_type = 1;
1510 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1511 udp_encap_enable();
1513 for (h = 0; h < VNI_HASH_SIZE; ++h)
1514 INIT_HLIST_HEAD(&vn->vni_list[h]);
1516 return 0;
1519 static __net_exit void vxlan_exit_net(struct net *net)
1521 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1523 if (vn->sock) {
1524 sk_release_kernel(vn->sock->sk);
1525 vn->sock = NULL;
1529 static struct pernet_operations vxlan_net_ops = {
1530 .init = vxlan_init_net,
1531 .exit = vxlan_exit_net,
1532 .id = &vxlan_net_id,
1533 .size = sizeof(struct vxlan_net),
1536 static int __init vxlan_init_module(void)
1538 int rc;
1540 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1542 rc = register_pernet_device(&vxlan_net_ops);
1543 if (rc)
1544 goto out1;
1546 rc = rtnl_link_register(&vxlan_link_ops);
1547 if (rc)
1548 goto out2;
1550 return 0;
1552 out2:
1553 unregister_pernet_device(&vxlan_net_ops);
1554 out1:
1555 return rc;
1557 module_init(vxlan_init_module);
1559 static void __exit vxlan_cleanup_module(void)
1561 rtnl_link_unregister(&vxlan_link_ops);
1562 unregister_pernet_device(&vxlan_net_ops);
1564 module_exit(vxlan_cleanup_module);
1566 MODULE_LICENSE("GPL");
1567 MODULE_VERSION(VXLAN_VERSION);
1568 MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
1569 MODULE_ALIAS_RTNL_LINK("vxlan");