blob: 72485b9b90054408c14511b6f01b8734b0672a43 [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
4 * Copyright (c) 2012 Vyatta Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * TODO
11 * - use IANA UDP port number (when defined)
12 * - IPv6 (not in RFC)
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
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>
stephen hemmingerd3428942012-10-01 12:32:35 +000031#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000032#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000033#include <net/arp.h>
34#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000035#include <net/ip.h>
36#include <net/icmp.h>
37#include <net/udp.h>
38#include <net/rtnetlink.h>
39#include <net/route.h>
40#include <net/dsfield.h>
41#include <net/inet_ecn.h>
42#include <net/net_namespace.h>
43#include <net/netns/generic.h>
44
45#define VXLAN_VERSION "0.1"
46
47#define VNI_HASH_BITS 10
48#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
49#define FDB_HASH_BITS 8
50#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
51#define FDB_AGE_DEFAULT 300 /* 5 min */
52#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
53
54#define VXLAN_N_VID (1u << 24)
55#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000056/* IP header + UDP + VXLAN + Ethernet header */
57#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
stephen hemmingerd3428942012-10-01 12:32:35 +000058
59#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
60
61/* VXLAN protocol header */
62struct vxlanhdr {
63 __be32 vx_flags;
64 __be32 vx_vni;
65};
66
67/* UDP port for VXLAN traffic. */
68static unsigned int vxlan_port __read_mostly = 8472;
69module_param_named(udp_port, vxlan_port, uint, 0444);
70MODULE_PARM_DESC(udp_port, "Destination UDP port");
71
72static bool log_ecn_error = true;
73module_param(log_ecn_error, bool, 0644);
74MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
75
76/* per-net private data for this module */
77static unsigned int vxlan_net_id;
78struct vxlan_net {
79 struct socket *sock; /* UDP encap socket */
80 struct hlist_head vni_list[VNI_HASH_SIZE];
81};
82
83/* Forwarding table entry */
84struct vxlan_fdb {
85 struct hlist_node hlist; /* linked list of entries */
86 struct rcu_head rcu;
87 unsigned long updated; /* jiffies */
88 unsigned long used;
89 __be32 remote_ip;
90 u16 state; /* see ndm_state */
91 u8 eth_addr[ETH_ALEN];
92};
93
94/* Per-cpu network traffic stats */
95struct vxlan_stats {
96 u64 rx_packets;
97 u64 rx_bytes;
98 u64 tx_packets;
99 u64 tx_bytes;
100 struct u64_stats_sync syncp;
101};
102
103/* Pseudo network device */
104struct vxlan_dev {
105 struct hlist_node hlist;
106 struct net_device *dev;
107 struct vxlan_stats __percpu *stats;
108 __u32 vni; /* virtual network id */
109 __be32 gaddr; /* multicast group */
110 __be32 saddr; /* source address */
111 unsigned int link; /* link to multicast over */
stephen hemminger05f47d62012-10-09 20:35:50 +0000112 __u16 port_min; /* source port range */
113 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000114 __u8 tos; /* TOS override */
115 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000116 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000117
118 unsigned long age_interval;
119 struct timer_list age_timer;
120 spinlock_t hash_lock;
121 unsigned int addrcnt;
122 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000123
124 struct hlist_head fdb_head[FDB_HASH_SIZE];
125};
126
David Stevense4f67ad2012-11-20 02:50:14 +0000127#define VXLAN_F_LEARN 0x01
128#define VXLAN_F_PROXY 0x02
129#define VXLAN_F_RSC 0x04
130#define VXLAN_F_L2MISS 0x08
131#define VXLAN_F_L3MISS 0x10
132
stephen hemmingerd3428942012-10-01 12:32:35 +0000133/* salt for hash table */
134static u32 vxlan_salt __read_mostly;
135
136static inline struct hlist_head *vni_head(struct net *net, u32 id)
137{
138 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
139
140 return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
141}
142
143/* Look up VNI in a per net namespace table */
144static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
145{
146 struct vxlan_dev *vxlan;
147 struct hlist_node *node;
148
149 hlist_for_each_entry_rcu(vxlan, node, vni_head(net, id), hlist) {
150 if (vxlan->vni == id)
151 return vxlan;
152 }
153
154 return NULL;
155}
156
157/* Fill in neighbour message in skbuff. */
158static 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)
161{
162 unsigned long now = jiffies;
163 struct nda_cacheinfo ci;
164 struct nlmsghdr *nlh;
165 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000166 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000167
168 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
169 if (nlh == NULL)
170 return -EMSGSIZE;
171
172 ndm = nlmsg_data(nlh);
173 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000174
175 send_eth = send_ip = true;
176
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;
stephen hemmingerd3428942012-10-01 12:32:35 +0000183 ndm->ndm_state = fdb->state;
184 ndm->ndm_ifindex = vxlan->dev->ifindex;
185 ndm->ndm_flags = NTF_SELF;
186 ndm->ndm_type = NDA_DST;
187
David Stevense4f67ad2012-11-20 02:50:14 +0000188 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000189 goto nla_put_failure;
190
David Stevense4f67ad2012-11-20 02:50:14 +0000191 if (send_ip && nla_put_be32(skb, NDA_DST, fdb->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +0000192 goto nla_put_failure;
193
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;
198
199 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
200 goto nla_put_failure;
201
202 return nlmsg_end(skb, nlh);
203
204nla_put_failure:
205 nlmsg_cancel(skb, nlh);
206 return -EMSGSIZE;
207}
208
209static inline size_t vxlan_nlmsg_size(void)
210{
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));
215}
216
217static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
218 const struct vxlan_fdb *fdb, int type)
219{
220 struct net *net = dev_net(vxlan->dev);
221 struct sk_buff *skb;
222 int err = -ENOBUFS;
223
224 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
225 if (skb == NULL)
226 goto errout;
227
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;
234 }
235
236 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
237 return;
238errout:
239 if (err < 0)
240 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
241}
242
David Stevense4f67ad2012-11-20 02:50:14 +0000243static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
244{
245 struct vxlan_dev *vxlan = netdev_priv(dev);
246 struct vxlan_fdb f;
247
248 memset(&f, 0, sizeof f);
249 f.state = NUD_STALE;
250 f.remote_ip = ipa; /* goes to NDA_DST */
251
252 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
253}
254
255static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
256{
257 struct vxlan_fdb f;
258
259 memset(&f, 0, sizeof f);
260 f.state = NUD_STALE;
261 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
262
263 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
264}
265
stephen hemmingerd3428942012-10-01 12:32:35 +0000266/* Hash Ethernet address */
267static u32 eth_hash(const unsigned char *addr)
268{
269 u64 value = get_unaligned((u64 *)addr);
270
271 /* only want 6 bytes */
272#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000273 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000274#else
275 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000276#endif
277 return hash_64(value, FDB_HASH_BITS);
278}
279
280/* Hash chain to use given mac address */
281static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
282 const u8 *mac)
283{
284 return &vxlan->fdb_head[eth_hash(mac)];
285}
286
287/* Look up Ethernet address in forwarding table */
288static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
289 const u8 *mac)
290
291{
292 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
293 struct vxlan_fdb *f;
294 struct hlist_node *node;
295
296 hlist_for_each_entry_rcu(f, node, head, hlist) {
297 if (compare_ether_addr(mac, f->eth_addr) == 0)
298 return f;
299 }
300
301 return NULL;
302}
303
304/* Add new entry to forwarding table -- assumes lock held */
305static int vxlan_fdb_create(struct vxlan_dev *vxlan,
306 const u8 *mac, __be32 ip,
307 __u16 state, __u16 flags)
308{
309 struct vxlan_fdb *f;
310 int notify = 0;
311
312 f = vxlan_find_mac(vxlan, mac);
313 if (f) {
314 if (flags & NLM_F_EXCL) {
315 netdev_dbg(vxlan->dev,
316 "lost race to create %pM\n", mac);
317 return -EEXIST;
318 }
319 if (f->state != state) {
320 f->state = state;
321 f->updated = jiffies;
322 notify = 1;
323 }
324 } else {
325 if (!(flags & NLM_F_CREATE))
326 return -ENOENT;
327
328 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
329 return -ENOSPC;
330
331 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
332 f = kmalloc(sizeof(*f), GFP_ATOMIC);
333 if (!f)
334 return -ENOMEM;
335
336 notify = 1;
337 f->remote_ip = ip;
338 f->state = state;
339 f->updated = f->used = jiffies;
340 memcpy(f->eth_addr, mac, ETH_ALEN);
341
342 ++vxlan->addrcnt;
343 hlist_add_head_rcu(&f->hlist,
344 vxlan_fdb_head(vxlan, mac));
345 }
346
347 if (notify)
348 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
349
350 return 0;
351}
352
353static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
354{
355 netdev_dbg(vxlan->dev,
356 "delete %pM\n", f->eth_addr);
357
358 --vxlan->addrcnt;
359 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
360
361 hlist_del_rcu(&f->hlist);
362 kfree_rcu(f, rcu);
363}
364
365/* Add static entry (via netlink) */
366static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
367 struct net_device *dev,
368 const unsigned char *addr, u16 flags)
369{
370 struct vxlan_dev *vxlan = netdev_priv(dev);
371 __be32 ip;
372 int err;
373
374 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
375 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
376 ndm->ndm_state);
377 return -EINVAL;
378 }
379
380 if (tb[NDA_DST] == NULL)
381 return -EINVAL;
382
383 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
384 return -EAFNOSUPPORT;
385
386 ip = nla_get_be32(tb[NDA_DST]);
387
388 spin_lock_bh(&vxlan->hash_lock);
389 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags);
390 spin_unlock_bh(&vxlan->hash_lock);
391
392 return err;
393}
394
395/* Delete entry (via netlink) */
396static int vxlan_fdb_delete(struct ndmsg *ndm, struct net_device *dev,
397 const unsigned char *addr)
398{
399 struct vxlan_dev *vxlan = netdev_priv(dev);
400 struct vxlan_fdb *f;
401 int err = -ENOENT;
402
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;
408 }
409 spin_unlock_bh(&vxlan->hash_lock);
410
411 return err;
412}
413
414/* Dump forwarding table */
415static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
416 struct net_device *dev, int idx)
417{
418 struct vxlan_dev *vxlan = netdev_priv(dev);
419 unsigned int h;
420
421 for (h = 0; h < FDB_HASH_SIZE; ++h) {
422 struct vxlan_fdb *f;
423 struct hlist_node *n;
424 int err;
425
426 hlist_for_each_entry_rcu(f, n, &vxlan->fdb_head[h], hlist) {
427 if (idx < cb->args[0])
428 goto skip;
429
430 err = vxlan_fdb_info(skb, vxlan, f,
431 NETLINK_CB(cb->skb).portid,
432 cb->nlh->nlmsg_seq,
433 RTM_NEWNEIGH,
434 NLM_F_MULTI);
435 if (err < 0)
436 break;
437skip:
438 ++idx;
439 }
440 }
441
442 return idx;
443}
444
445/* Watch incoming packets to learn mapping between Ethernet address
446 * and Tunnel endpoint.
447 */
448static void vxlan_snoop(struct net_device *dev,
449 __be32 src_ip, const u8 *src_mac)
450{
451 struct vxlan_dev *vxlan = netdev_priv(dev);
452 struct vxlan_fdb *f;
453 int err;
454
455 f = vxlan_find_mac(vxlan, src_mac);
456 if (likely(f)) {
457 f->used = jiffies;
458 if (likely(f->remote_ip == src_ip))
459 return;
460
461 if (net_ratelimit())
462 netdev_info(dev,
463 "%pM migrated from %pI4 to %pI4\n",
464 src_mac, &f->remote_ip, &src_ip);
465
466 f->remote_ip = src_ip;
467 f->updated = jiffies;
468 } else {
469 /* learned new entry */
470 spin_lock(&vxlan->hash_lock);
471 err = vxlan_fdb_create(vxlan, src_mac, src_ip,
472 NUD_REACHABLE,
473 NLM_F_EXCL|NLM_F_CREATE);
474 spin_unlock(&vxlan->hash_lock);
475 }
476}
477
478
479/* See if multicast group is already in use by other ID */
480static bool vxlan_group_used(struct vxlan_net *vn,
481 const struct vxlan_dev *this)
482{
483 const struct vxlan_dev *vxlan;
484 struct hlist_node *node;
485 unsigned h;
486
487 for (h = 0; h < VNI_HASH_SIZE; ++h)
488 hlist_for_each_entry(vxlan, node, &vn->vni_list[h], hlist) {
489 if (vxlan == this)
490 continue;
491
492 if (!netif_running(vxlan->dev))
493 continue;
494
495 if (vxlan->gaddr == this->gaddr)
496 return true;
497 }
498
499 return false;
500}
501
502/* kernel equivalent to IP_ADD_MEMBERSHIP */
503static int vxlan_join_group(struct net_device *dev)
504{
505 struct vxlan_dev *vxlan = netdev_priv(dev);
506 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
507 struct sock *sk = vn->sock->sk;
508 struct ip_mreqn mreq = {
Yan Burmanaf9b0782012-12-20 03:36:08 +0000509 .imr_multiaddr.s_addr = vxlan->gaddr,
510 .imr_ifindex = vxlan->link,
stephen hemmingerd3428942012-10-01 12:32:35 +0000511 };
512 int err;
513
514 /* Already a member of group */
515 if (vxlan_group_used(vn, vxlan))
516 return 0;
517
518 /* Need to drop RTNL to call multicast join */
519 rtnl_unlock();
520 lock_sock(sk);
521 err = ip_mc_join_group(sk, &mreq);
522 release_sock(sk);
523 rtnl_lock();
524
525 return err;
526}
527
528
529/* kernel equivalent to IP_DROP_MEMBERSHIP */
530static int vxlan_leave_group(struct net_device *dev)
531{
532 struct vxlan_dev *vxlan = netdev_priv(dev);
533 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
534 int err = 0;
535 struct sock *sk = vn->sock->sk;
536 struct ip_mreqn mreq = {
Yan Burmanaf9b0782012-12-20 03:36:08 +0000537 .imr_multiaddr.s_addr = vxlan->gaddr,
538 .imr_ifindex = vxlan->link,
stephen hemmingerd3428942012-10-01 12:32:35 +0000539 };
540
541 /* Only leave group when last vxlan is done. */
542 if (vxlan_group_used(vn, vxlan))
543 return 0;
544
545 /* Need to drop RTNL to call multicast leave */
546 rtnl_unlock();
547 lock_sock(sk);
548 err = ip_mc_leave_group(sk, &mreq);
549 release_sock(sk);
550 rtnl_lock();
551
552 return err;
553}
554
555/* Callback from net/ipv4/udp.c to receive packets */
556static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
557{
558 struct iphdr *oip;
559 struct vxlanhdr *vxh;
560 struct vxlan_dev *vxlan;
561 struct vxlan_stats *stats;
562 __u32 vni;
563 int err;
564
565 /* pop off outer UDP header */
566 __skb_pull(skb, sizeof(struct udphdr));
567
568 /* Need Vxlan and inner Ethernet header to be present */
569 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
570 goto error;
571
572 /* Drop packets with reserved bits set */
573 vxh = (struct vxlanhdr *) skb->data;
574 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
575 (vxh->vx_vni & htonl(0xff))) {
576 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
577 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
578 goto error;
579 }
580
581 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000582
583 /* Is this VNI defined? */
584 vni = ntohl(vxh->vx_vni) >> 8;
585 vxlan = vxlan_find_vni(sock_net(sk), vni);
586 if (!vxlan) {
587 netdev_dbg(skb->dev, "unknown vni %d\n", vni);
588 goto drop;
589 }
590
591 if (!pskb_may_pull(skb, ETH_HLEN)) {
592 vxlan->dev->stats.rx_length_errors++;
593 vxlan->dev->stats.rx_errors++;
594 goto drop;
595 }
596
David Stevense4f67ad2012-11-20 02:50:14 +0000597 skb_reset_mac_header(skb);
598
stephen hemmingerd3428942012-10-01 12:32:35 +0000599 /* Re-examine inner Ethernet packet */
600 oip = ip_hdr(skb);
601 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000602
603 /* Ignore packet loops (and multicast echo) */
604 if (compare_ether_addr(eth_hdr(skb)->h_source,
605 vxlan->dev->dev_addr) == 0)
606 goto drop;
607
David Stevense4f67ad2012-11-20 02:50:14 +0000608 if (vxlan->flags & VXLAN_F_LEARN)
stephen hemmingerd3428942012-10-01 12:32:35 +0000609 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
610
611 __skb_tunnel_rx(skb, vxlan->dev);
612 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000613
614 /* If the NIC driver gave us an encapsulated packet with
615 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
616 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
617 * for us. Otherwise force the upper layers to verify it.
618 */
619 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
620 !(vxlan->dev->features & NETIF_F_RXCSUM))
621 skb->ip_summed = CHECKSUM_NONE;
622
623 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000624
625 err = IP_ECN_decapsulate(oip, skb);
626 if (unlikely(err)) {
627 if (log_ecn_error)
628 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
629 &oip->saddr, oip->tos);
630 if (err > 1) {
631 ++vxlan->dev->stats.rx_frame_errors;
632 ++vxlan->dev->stats.rx_errors;
633 goto drop;
634 }
635 }
636
637 stats = this_cpu_ptr(vxlan->stats);
638 u64_stats_update_begin(&stats->syncp);
639 stats->rx_packets++;
640 stats->rx_bytes += skb->len;
641 u64_stats_update_end(&stats->syncp);
642
643 netif_rx(skb);
644
645 return 0;
646error:
647 /* Put UDP header back */
648 __skb_push(skb, sizeof(struct udphdr));
649
650 return 1;
651drop:
652 /* Consume bad packet */
653 kfree_skb(skb);
654 return 0;
655}
656
David Stevense4f67ad2012-11-20 02:50:14 +0000657static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
658{
659 struct vxlan_dev *vxlan = netdev_priv(dev);
660 struct arphdr *parp;
661 u8 *arpptr, *sha;
662 __be32 sip, tip;
663 struct neighbour *n;
664
665 if (dev->flags & IFF_NOARP)
666 goto out;
667
668 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
669 dev->stats.tx_dropped++;
670 goto out;
671 }
672 parp = arp_hdr(skb);
673
674 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
675 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
676 parp->ar_pro != htons(ETH_P_IP) ||
677 parp->ar_op != htons(ARPOP_REQUEST) ||
678 parp->ar_hln != dev->addr_len ||
679 parp->ar_pln != 4)
680 goto out;
681 arpptr = (u8 *)parp + sizeof(struct arphdr);
682 sha = arpptr;
683 arpptr += dev->addr_len; /* sha */
684 memcpy(&sip, arpptr, sizeof(sip));
685 arpptr += sizeof(sip);
686 arpptr += dev->addr_len; /* tha */
687 memcpy(&tip, arpptr, sizeof(tip));
688
689 if (ipv4_is_loopback(tip) ||
690 ipv4_is_multicast(tip))
691 goto out;
692
693 n = neigh_lookup(&arp_tbl, &tip, dev);
694
695 if (n) {
696 struct vxlan_dev *vxlan = netdev_priv(dev);
697 struct vxlan_fdb *f;
698 struct sk_buff *reply;
699
700 if (!(n->nud_state & NUD_CONNECTED)) {
701 neigh_release(n);
702 goto out;
703 }
704
705 f = vxlan_find_mac(vxlan, n->ha);
706 if (f && f->remote_ip == 0) {
707 /* bridge-local neighbor */
708 neigh_release(n);
709 goto out;
710 }
711
712 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
713 n->ha, sha);
714
715 neigh_release(n);
716
717 skb_reset_mac_header(reply);
718 __skb_pull(reply, skb_network_offset(reply));
719 reply->ip_summed = CHECKSUM_UNNECESSARY;
720 reply->pkt_type = PACKET_HOST;
721
722 if (netif_rx_ni(reply) == NET_RX_DROP)
723 dev->stats.rx_dropped++;
724 } else if (vxlan->flags & VXLAN_F_L3MISS)
725 vxlan_ip_miss(dev, tip);
726out:
727 consume_skb(skb);
728 return NETDEV_TX_OK;
729}
730
731static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
732{
733 struct vxlan_dev *vxlan = netdev_priv(dev);
734 struct neighbour *n;
735 struct iphdr *pip;
736
737 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
738 return false;
739
740 n = NULL;
741 switch (ntohs(eth_hdr(skb)->h_proto)) {
742 case ETH_P_IP:
743 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
744 return false;
745 pip = ip_hdr(skb);
746 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
747 break;
748 default:
749 return false;
750 }
751
752 if (n) {
753 bool diff;
754
755 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
756 if (diff) {
757 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
758 dev->addr_len);
759 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
760 }
761 neigh_release(n);
762 return diff;
763 } else if (vxlan->flags & VXLAN_F_L3MISS)
764 vxlan_ip_miss(dev, pip->daddr);
765 return false;
766}
767
stephen hemmingerd3428942012-10-01 12:32:35 +0000768/* Extract dsfield from inner protocol */
769static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
770 const struct sk_buff *skb)
771{
772 if (skb->protocol == htons(ETH_P_IP))
773 return iph->tos;
774 else if (skb->protocol == htons(ETH_P_IPV6))
775 return ipv6_get_dsfield((const struct ipv6hdr *)iph);
776 else
777 return 0;
778}
779
780/* Propogate ECN bits out */
781static inline u8 vxlan_ecn_encap(u8 tos,
782 const struct iphdr *iph,
783 const struct sk_buff *skb)
784{
785 u8 inner = vxlan_get_dsfield(iph, skb);
786
787 return INET_ECN_encapsulate(tos, inner);
788}
789
stephen hemminger1cad8712012-10-09 20:35:49 +0000790static void vxlan_sock_free(struct sk_buff *skb)
791{
792 sock_put(skb->sk);
793}
794
795/* On transmit, associate with the tunnel socket */
796static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
797{
798 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
799 struct sock *sk = vn->sock->sk;
800
801 skb_orphan(skb);
802 sock_hold(sk);
803 skb->sk = sk;
804 skb->destructor = vxlan_sock_free;
805}
806
stephen hemminger05f47d62012-10-09 20:35:50 +0000807/* Compute source port for outgoing packet
808 * first choice to use L4 flow hash since it will spread
809 * better and maybe available from hardware
810 * secondary choice is to use jhash on the Ethernet header
811 */
812static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
813{
814 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
815 u32 hash;
816
817 hash = skb_get_rxhash(skb);
818 if (!hash)
819 hash = jhash(skb->data, 2 * ETH_ALEN,
820 (__force u32) skb->protocol);
821
822 return (((u64) hash * range) >> 32) + vxlan->port_min;
823}
824
stephen hemmingerd3428942012-10-01 12:32:35 +0000825/* Transmit local packets over Vxlan
826 *
827 * Outer IP header inherits ECN and DF from inner header.
828 * Outer UDP destination is the VXLAN assigned port.
stephen hemminger05f47d62012-10-09 20:35:50 +0000829 * source port is based on hash of flow
stephen hemmingerd3428942012-10-01 12:32:35 +0000830 */
831static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
832{
833 struct vxlan_dev *vxlan = netdev_priv(dev);
834 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +0000835 const struct iphdr *old_iph;
David Stevense4f67ad2012-11-20 02:50:14 +0000836 struct ethhdr *eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000837 struct iphdr *iph;
838 struct vxlanhdr *vxh;
839 struct udphdr *uh;
840 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +0000841 unsigned int pkt_len = skb->len;
stephen hemmingerd3428942012-10-01 12:32:35 +0000842 __be32 dst;
stephen hemminger05f47d62012-10-09 20:35:50 +0000843 __u16 src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000844 __be16 df = 0;
845 __u8 tos, ttl;
846 int err;
David Stevense4f67ad2012-11-20 02:50:14 +0000847 bool did_rsc = false;
848 const struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000849
David Stevense4f67ad2012-11-20 02:50:14 +0000850 skb_reset_mac_header(skb);
851 eth = eth_hdr(skb);
852
853 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
854 return arp_reduce(dev, skb);
855 else if ((vxlan->flags&VXLAN_F_RSC) && ntohs(eth->h_proto) == ETH_P_IP)
856 did_rsc = route_shortcircuit(dev, skb);
857
858 f = vxlan_find_mac(vxlan, eth->h_dest);
859 if (f == NULL) {
860 did_rsc = false;
861 dst = vxlan->gaddr;
862 if (!dst && (vxlan->flags & VXLAN_F_L2MISS) &&
863 !is_multicast_ether_addr(eth->h_dest))
864 vxlan_fdb_miss(vxlan, eth->h_dest);
865 } else
866 dst = f->remote_ip;
867
868 if (!dst) {
869 if (did_rsc) {
870 __skb_pull(skb, skb_network_offset(skb));
871 skb->ip_summed = CHECKSUM_NONE;
872 skb->pkt_type = PACKET_HOST;
873
874 /* short-circuited back to local bridge */
875 if (netif_rx(skb) == NET_RX_SUCCESS) {
876 struct vxlan_stats *stats =
877 this_cpu_ptr(vxlan->stats);
878
879 u64_stats_update_begin(&stats->syncp);
880 stats->tx_packets++;
881 stats->tx_bytes += pkt_len;
882 u64_stats_update_end(&stats->syncp);
883 } else {
884 dev->stats.tx_errors++;
885 dev->stats.tx_aborted_errors++;
886 }
887 return NETDEV_TX_OK;
888 }
stephen hemmingeref59feb2012-10-09 20:35:46 +0000889 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +0000890 }
stephen hemmingeref59feb2012-10-09 20:35:46 +0000891
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +0000892 if (!skb->encapsulation) {
893 skb_reset_inner_headers(skb);
894 skb->encapsulation = 1;
895 }
896
stephen hemmingerd3428942012-10-01 12:32:35 +0000897 /* Need space for new headers (invalidates iph ptr) */
898 if (skb_cow_head(skb, VXLAN_HEADROOM))
899 goto drop;
900
stephen hemmingerd3428942012-10-01 12:32:35 +0000901 old_iph = ip_hdr(skb);
902
stephen hemmingerd3428942012-10-01 12:32:35 +0000903 ttl = vxlan->ttl;
904 if (!ttl && IN_MULTICAST(ntohl(dst)))
905 ttl = 1;
906
907 tos = vxlan->tos;
908 if (tos == 1)
909 tos = vxlan_get_dsfield(old_iph, skb);
910
stephen hemminger05f47d62012-10-09 20:35:50 +0000911 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +0000912
stephen hemmingerca78f182012-10-09 20:35:48 +0000913 memset(&fl4, 0, sizeof(fl4));
914 fl4.flowi4_oif = vxlan->link;
915 fl4.flowi4_tos = RT_TOS(tos);
916 fl4.daddr = dst;
917 fl4.saddr = vxlan->saddr;
918
919 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +0000920 if (IS_ERR(rt)) {
921 netdev_dbg(dev, "no route to %pI4\n", &dst);
922 dev->stats.tx_carrier_errors++;
923 goto tx_error;
924 }
925
926 if (rt->dst.dev == dev) {
927 netdev_dbg(dev, "circular route to %pI4\n", &dst);
928 ip_rt_put(rt);
929 dev->stats.collisions++;
930 goto tx_error;
931 }
932
933 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
934 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
935 IPSKB_REROUTED);
936 skb_dst_drop(skb);
937 skb_dst_set(skb, &rt->dst);
938
939 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
940 vxh->vx_flags = htonl(VXLAN_FLAGS);
941 vxh->vx_vni = htonl(vxlan->vni << 8);
942
943 __skb_push(skb, sizeof(*uh));
944 skb_reset_transport_header(skb);
945 uh = udp_hdr(skb);
946
947 uh->dest = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +0000948 uh->source = htons(src_port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000949
950 uh->len = htons(skb->len);
951 uh->check = 0;
952
953 __skb_push(skb, sizeof(*iph));
954 skb_reset_network_header(skb);
955 iph = ip_hdr(skb);
956 iph->version = 4;
957 iph->ihl = sizeof(struct iphdr) >> 2;
958 iph->frag_off = df;
959 iph->protocol = IPPROTO_UDP;
960 iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
stephen hemmingerca78f182012-10-09 20:35:48 +0000961 iph->daddr = dst;
stephen hemmingerd3428942012-10-01 12:32:35 +0000962 iph->saddr = fl4.saddr;
963 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
964
stephen hemminger1cad8712012-10-09 20:35:49 +0000965 vxlan_set_owner(dev, skb);
966
Amerigo Wangaa0010f2012-11-11 21:52:33 +0000967 /* See iptunnel_xmit() */
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +0000968 if (skb->ip_summed != CHECKSUM_PARTIAL)
969 skb->ip_summed = CHECKSUM_NONE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000970 ip_select_ident(iph, &rt->dst, NULL);
971
972 err = ip_local_out(skb);
973 if (likely(net_xmit_eval(err) == 0)) {
974 struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
975
976 u64_stats_update_begin(&stats->syncp);
977 stats->tx_packets++;
978 stats->tx_bytes += pkt_len;
979 u64_stats_update_end(&stats->syncp);
980 } else {
981 dev->stats.tx_errors++;
982 dev->stats.tx_aborted_errors++;
983 }
984 return NETDEV_TX_OK;
985
986drop:
987 dev->stats.tx_dropped++;
988 goto tx_free;
989
990tx_error:
991 dev->stats.tx_errors++;
992tx_free:
993 dev_kfree_skb(skb);
994 return NETDEV_TX_OK;
995}
996
997/* Walk the forwarding table and purge stale entries */
998static void vxlan_cleanup(unsigned long arg)
999{
1000 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1001 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1002 unsigned int h;
1003
1004 if (!netif_running(vxlan->dev))
1005 return;
1006
1007 spin_lock_bh(&vxlan->hash_lock);
1008 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1009 struct hlist_node *p, *n;
1010 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1011 struct vxlan_fdb *f
1012 = container_of(p, struct vxlan_fdb, hlist);
1013 unsigned long timeout;
1014
stephen hemminger3c172862012-10-26 06:24:34 +00001015 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001016 continue;
1017
1018 timeout = f->used + vxlan->age_interval * HZ;
1019 if (time_before_eq(timeout, jiffies)) {
1020 netdev_dbg(vxlan->dev,
1021 "garbage collect %pM\n",
1022 f->eth_addr);
1023 f->state = NUD_STALE;
1024 vxlan_fdb_destroy(vxlan, f);
1025 } else if (time_before(timeout, next_timer))
1026 next_timer = timeout;
1027 }
1028 }
1029 spin_unlock_bh(&vxlan->hash_lock);
1030
1031 mod_timer(&vxlan->age_timer, next_timer);
1032}
1033
1034/* Setup stats when device is created */
1035static int vxlan_init(struct net_device *dev)
1036{
1037 struct vxlan_dev *vxlan = netdev_priv(dev);
1038
1039 vxlan->stats = alloc_percpu(struct vxlan_stats);
1040 if (!vxlan->stats)
1041 return -ENOMEM;
1042
1043 return 0;
1044}
1045
1046/* Start ageing timer and join group when device is brought up */
1047static int vxlan_open(struct net_device *dev)
1048{
1049 struct vxlan_dev *vxlan = netdev_priv(dev);
1050 int err;
1051
1052 if (vxlan->gaddr) {
1053 err = vxlan_join_group(dev);
1054 if (err)
1055 return err;
1056 }
1057
1058 if (vxlan->age_interval)
1059 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1060
1061 return 0;
1062}
1063
1064/* Purge the forwarding table */
1065static void vxlan_flush(struct vxlan_dev *vxlan)
1066{
1067 unsigned h;
1068
1069 spin_lock_bh(&vxlan->hash_lock);
1070 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1071 struct hlist_node *p, *n;
1072 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1073 struct vxlan_fdb *f
1074 = container_of(p, struct vxlan_fdb, hlist);
1075 vxlan_fdb_destroy(vxlan, f);
1076 }
1077 }
1078 spin_unlock_bh(&vxlan->hash_lock);
1079}
1080
1081/* Cleanup timer and forwarding table on shutdown */
1082static int vxlan_stop(struct net_device *dev)
1083{
1084 struct vxlan_dev *vxlan = netdev_priv(dev);
1085
1086 if (vxlan->gaddr)
1087 vxlan_leave_group(dev);
1088
1089 del_timer_sync(&vxlan->age_timer);
1090
1091 vxlan_flush(vxlan);
1092
1093 return 0;
1094}
1095
1096/* Merge per-cpu statistics */
1097static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
1098 struct rtnl_link_stats64 *stats)
1099{
1100 struct vxlan_dev *vxlan = netdev_priv(dev);
1101 struct vxlan_stats tmp, sum = { 0 };
1102 unsigned int cpu;
1103
1104 for_each_possible_cpu(cpu) {
1105 unsigned int start;
1106 const struct vxlan_stats *stats
1107 = per_cpu_ptr(vxlan->stats, cpu);
1108
1109 do {
1110 start = u64_stats_fetch_begin_bh(&stats->syncp);
1111 memcpy(&tmp, stats, sizeof(tmp));
1112 } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
1113
1114 sum.tx_bytes += tmp.tx_bytes;
1115 sum.tx_packets += tmp.tx_packets;
1116 sum.rx_bytes += tmp.rx_bytes;
1117 sum.rx_packets += tmp.rx_packets;
1118 }
1119
1120 stats->tx_bytes = sum.tx_bytes;
1121 stats->tx_packets = sum.tx_packets;
1122 stats->rx_bytes = sum.rx_bytes;
1123 stats->rx_packets = sum.rx_packets;
1124
1125 stats->multicast = dev->stats.multicast;
1126 stats->rx_length_errors = dev->stats.rx_length_errors;
1127 stats->rx_frame_errors = dev->stats.rx_frame_errors;
1128 stats->rx_errors = dev->stats.rx_errors;
1129
1130 stats->tx_dropped = dev->stats.tx_dropped;
1131 stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
1132 stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
1133 stats->collisions = dev->stats.collisions;
1134 stats->tx_errors = dev->stats.tx_errors;
1135
1136 return stats;
1137}
1138
1139/* Stub, nothing needs to be done. */
1140static void vxlan_set_multicast_list(struct net_device *dev)
1141{
1142}
1143
1144static const struct net_device_ops vxlan_netdev_ops = {
1145 .ndo_init = vxlan_init,
1146 .ndo_open = vxlan_open,
1147 .ndo_stop = vxlan_stop,
1148 .ndo_start_xmit = vxlan_xmit,
1149 .ndo_get_stats64 = vxlan_stats64,
1150 .ndo_set_rx_mode = vxlan_set_multicast_list,
1151 .ndo_change_mtu = eth_change_mtu,
1152 .ndo_validate_addr = eth_validate_addr,
1153 .ndo_set_mac_address = eth_mac_addr,
1154 .ndo_fdb_add = vxlan_fdb_add,
1155 .ndo_fdb_del = vxlan_fdb_delete,
1156 .ndo_fdb_dump = vxlan_fdb_dump,
1157};
1158
1159/* Info for udev, that this is a virtual tunnel endpoint */
1160static struct device_type vxlan_type = {
1161 .name = "vxlan",
1162};
1163
1164static void vxlan_free(struct net_device *dev)
1165{
1166 struct vxlan_dev *vxlan = netdev_priv(dev);
1167
1168 free_percpu(vxlan->stats);
1169 free_netdev(dev);
1170}
1171
1172/* Initialize the device structure. */
1173static void vxlan_setup(struct net_device *dev)
1174{
1175 struct vxlan_dev *vxlan = netdev_priv(dev);
1176 unsigned h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001177 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001178
1179 eth_hw_addr_random(dev);
1180 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001181 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001182
1183 dev->netdev_ops = &vxlan_netdev_ops;
1184 dev->destructor = vxlan_free;
1185 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1186
1187 dev->tx_queue_len = 0;
1188 dev->features |= NETIF_F_LLTX;
1189 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001190 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001191 dev->features |= NETIF_F_RXCSUM;
1192
1193 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001194 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001195 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001196
1197 spin_lock_init(&vxlan->hash_lock);
1198
1199 init_timer_deferrable(&vxlan->age_timer);
1200 vxlan->age_timer.function = vxlan_cleanup;
1201 vxlan->age_timer.data = (unsigned long) vxlan;
1202
stephen hemminger05f47d62012-10-09 20:35:50 +00001203 inet_get_local_port_range(&low, &high);
1204 vxlan->port_min = low;
1205 vxlan->port_max = high;
1206
stephen hemmingerd3428942012-10-01 12:32:35 +00001207 vxlan->dev = dev;
1208
1209 for (h = 0; h < FDB_HASH_SIZE; ++h)
1210 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1211}
1212
1213static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1214 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1215 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1216 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1217 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1218 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1219 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1220 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1221 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1222 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001223 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001224 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1225 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1226 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1227 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001228};
1229
1230static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1231{
1232 if (tb[IFLA_ADDRESS]) {
1233 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1234 pr_debug("invalid link address (not ethernet)\n");
1235 return -EINVAL;
1236 }
1237
1238 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1239 pr_debug("invalid all zero ethernet address\n");
1240 return -EADDRNOTAVAIL;
1241 }
1242 }
1243
1244 if (!data)
1245 return -EINVAL;
1246
1247 if (data[IFLA_VXLAN_ID]) {
1248 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1249 if (id >= VXLAN_VID_MASK)
1250 return -ERANGE;
1251 }
1252
1253 if (data[IFLA_VXLAN_GROUP]) {
1254 __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1255 if (!IN_MULTICAST(ntohl(gaddr))) {
1256 pr_debug("group address is not IPv4 multicast\n");
1257 return -EADDRNOTAVAIL;
1258 }
1259 }
stephen hemminger05f47d62012-10-09 20:35:50 +00001260
1261 if (data[IFLA_VXLAN_PORT_RANGE]) {
1262 const struct ifla_vxlan_port_range *p
1263 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1264
1265 if (ntohs(p->high) < ntohs(p->low)) {
1266 pr_debug("port range %u .. %u not valid\n",
1267 ntohs(p->low), ntohs(p->high));
1268 return -EINVAL;
1269 }
1270 }
1271
stephen hemmingerd3428942012-10-01 12:32:35 +00001272 return 0;
1273}
1274
Yan Burman1b13c972013-01-29 23:43:07 +00001275static void vxlan_get_drvinfo(struct net_device *netdev,
1276 struct ethtool_drvinfo *drvinfo)
1277{
1278 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1279 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1280}
1281
1282static const struct ethtool_ops vxlan_ethtool_ops = {
1283 .get_drvinfo = vxlan_get_drvinfo,
1284 .get_link = ethtool_op_get_link,
1285};
1286
stephen hemmingerd3428942012-10-01 12:32:35 +00001287static int vxlan_newlink(struct net *net, struct net_device *dev,
1288 struct nlattr *tb[], struct nlattr *data[])
1289{
1290 struct vxlan_dev *vxlan = netdev_priv(dev);
1291 __u32 vni;
1292 int err;
1293
1294 if (!data[IFLA_VXLAN_ID])
1295 return -EINVAL;
1296
1297 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1298 if (vxlan_find_vni(net, vni)) {
1299 pr_info("duplicate VNI %u\n", vni);
1300 return -EEXIST;
1301 }
1302 vxlan->vni = vni;
1303
1304 if (data[IFLA_VXLAN_GROUP])
1305 vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1306
1307 if (data[IFLA_VXLAN_LOCAL])
1308 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1309
stephen hemminger34e02aa2012-10-09 20:35:53 +00001310 if (data[IFLA_VXLAN_LINK] &&
1311 (vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1312 struct net_device *lowerdev
1313 = __dev_get_by_index(net, vxlan->link);
stephen hemmingerd3428942012-10-01 12:32:35 +00001314
stephen hemminger34e02aa2012-10-09 20:35:53 +00001315 if (!lowerdev) {
1316 pr_info("ifindex %d does not exist\n", vxlan->link);
1317 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001318 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001319
1320 if (!tb[IFLA_MTU])
1321 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001322
1323 /* update header length based on lower device */
1324 dev->hard_header_len = lowerdev->hard_header_len +
1325 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001326 }
1327
1328 if (data[IFLA_VXLAN_TOS])
1329 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1330
Vincent Bernatafb97182012-10-30 10:27:16 +00001331 if (data[IFLA_VXLAN_TTL])
1332 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1333
stephen hemmingerd3428942012-10-01 12:32:35 +00001334 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001335 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001336
1337 if (data[IFLA_VXLAN_AGEING])
1338 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1339 else
1340 vxlan->age_interval = FDB_AGE_DEFAULT;
1341
David Stevense4f67ad2012-11-20 02:50:14 +00001342 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1343 vxlan->flags |= VXLAN_F_PROXY;
1344
1345 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1346 vxlan->flags |= VXLAN_F_RSC;
1347
1348 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1349 vxlan->flags |= VXLAN_F_L2MISS;
1350
1351 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1352 vxlan->flags |= VXLAN_F_L3MISS;
1353
stephen hemmingerd3428942012-10-01 12:32:35 +00001354 if (data[IFLA_VXLAN_LIMIT])
1355 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1356
stephen hemminger05f47d62012-10-09 20:35:50 +00001357 if (data[IFLA_VXLAN_PORT_RANGE]) {
1358 const struct ifla_vxlan_port_range *p
1359 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1360 vxlan->port_min = ntohs(p->low);
1361 vxlan->port_max = ntohs(p->high);
1362 }
1363
Yan Burman1b13c972013-01-29 23:43:07 +00001364 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1365
stephen hemmingerd3428942012-10-01 12:32:35 +00001366 err = register_netdevice(dev);
1367 if (!err)
1368 hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
1369
1370 return err;
1371}
1372
1373static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1374{
1375 struct vxlan_dev *vxlan = netdev_priv(dev);
1376
1377 hlist_del_rcu(&vxlan->hlist);
1378
1379 unregister_netdevice_queue(dev, head);
1380}
1381
1382static size_t vxlan_get_size(const struct net_device *dev)
1383{
1384
1385 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1386 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
1387 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1388 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1389 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1390 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1391 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001392 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1393 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1394 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1395 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001396 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1397 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001398 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemmingerd3428942012-10-01 12:32:35 +00001399 0;
1400}
1401
1402static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1403{
1404 const struct vxlan_dev *vxlan = netdev_priv(dev);
stephen hemminger05f47d62012-10-09 20:35:50 +00001405 struct ifla_vxlan_port_range ports = {
1406 .low = htons(vxlan->port_min),
1407 .high = htons(vxlan->port_max),
1408 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001409
1410 if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
1411 goto nla_put_failure;
1412
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001413 if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001414 goto nla_put_failure;
1415
1416 if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
1417 goto nla_put_failure;
1418
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001419 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001420 goto nla_put_failure;
1421
1422 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1423 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001424 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1425 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1426 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1427 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1428 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1429 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1430 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1431 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1432 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001433 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1434 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
1435 goto nla_put_failure;
1436
stephen hemminger05f47d62012-10-09 20:35:50 +00001437 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1438 goto nla_put_failure;
1439
stephen hemmingerd3428942012-10-01 12:32:35 +00001440 return 0;
1441
1442nla_put_failure:
1443 return -EMSGSIZE;
1444}
1445
1446static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1447 .kind = "vxlan",
1448 .maxtype = IFLA_VXLAN_MAX,
1449 .policy = vxlan_policy,
1450 .priv_size = sizeof(struct vxlan_dev),
1451 .setup = vxlan_setup,
1452 .validate = vxlan_validate,
1453 .newlink = vxlan_newlink,
1454 .dellink = vxlan_dellink,
1455 .get_size = vxlan_get_size,
1456 .fill_info = vxlan_fill_info,
1457};
1458
1459static __net_init int vxlan_init_net(struct net *net)
1460{
1461 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1462 struct sock *sk;
1463 struct sockaddr_in vxlan_addr = {
1464 .sin_family = AF_INET,
1465 .sin_addr.s_addr = htonl(INADDR_ANY),
1466 };
1467 int rc;
1468 unsigned h;
1469
1470 /* Create UDP socket for encapsulation receive. */
1471 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
1472 if (rc < 0) {
1473 pr_debug("UDP socket create failed\n");
1474 return rc;
1475 }
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001476 /* Put in proper namespace */
1477 sk = vn->sock->sk;
1478 sk_change_net(sk, net);
stephen hemmingerd3428942012-10-01 12:32:35 +00001479
1480 vxlan_addr.sin_port = htons(vxlan_port);
1481
1482 rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
1483 sizeof(vxlan_addr));
1484 if (rc < 0) {
1485 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1486 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001487 sk_release_kernel(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001488 vn->sock = NULL;
1489 return rc;
1490 }
1491
1492 /* Disable multicast loopback */
stephen hemmingerd3428942012-10-01 12:32:35 +00001493 inet_sk(sk)->mc_loop = 0;
1494
1495 /* Mark socket as an encapsulation socket. */
1496 udp_sk(sk)->encap_type = 1;
1497 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1498 udp_encap_enable();
1499
1500 for (h = 0; h < VNI_HASH_SIZE; ++h)
1501 INIT_HLIST_HEAD(&vn->vni_list[h]);
1502
1503 return 0;
1504}
1505
1506static __net_exit void vxlan_exit_net(struct net *net)
1507{
1508 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1509
1510 if (vn->sock) {
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001511 sk_release_kernel(vn->sock->sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001512 vn->sock = NULL;
1513 }
1514}
1515
1516static struct pernet_operations vxlan_net_ops = {
1517 .init = vxlan_init_net,
1518 .exit = vxlan_exit_net,
1519 .id = &vxlan_net_id,
1520 .size = sizeof(struct vxlan_net),
1521};
1522
1523static int __init vxlan_init_module(void)
1524{
1525 int rc;
1526
1527 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1528
1529 rc = register_pernet_device(&vxlan_net_ops);
1530 if (rc)
1531 goto out1;
1532
1533 rc = rtnl_link_register(&vxlan_link_ops);
1534 if (rc)
1535 goto out2;
1536
1537 return 0;
1538
1539out2:
1540 unregister_pernet_device(&vxlan_net_ops);
1541out1:
1542 return rc;
1543}
1544module_init(vxlan_init_module);
1545
1546static void __exit vxlan_cleanup_module(void)
1547{
1548 rtnl_link_unregister(&vxlan_link_ops);
1549 unregister_pernet_device(&vxlan_net_ops);
1550}
1551module_exit(vxlan_cleanup_module);
1552
1553MODULE_LICENSE("GPL");
1554MODULE_VERSION(VXLAN_VERSION);
1555MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
1556MODULE_ALIAS_RTNL_LINK("vxlan");