blob: a14df1ce99ffb7defc3504392a0254d833bf5670 [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>
32#include <net/ip.h>
33#include <net/icmp.h>
34#include <net/udp.h>
35#include <net/rtnetlink.h>
36#include <net/route.h>
37#include <net/dsfield.h>
38#include <net/inet_ecn.h>
39#include <net/net_namespace.h>
40#include <net/netns/generic.h>
41
42#define VXLAN_VERSION "0.1"
43
44#define VNI_HASH_BITS 10
45#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
46#define FDB_HASH_BITS 8
47#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
48#define FDB_AGE_DEFAULT 300 /* 5 min */
49#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
50
51#define VXLAN_N_VID (1u << 24)
52#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000053/* IP header + UDP + VXLAN + Ethernet header */
54#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
stephen hemmingerd3428942012-10-01 12:32:35 +000055
56#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
57
58/* VXLAN protocol header */
59struct vxlanhdr {
60 __be32 vx_flags;
61 __be32 vx_vni;
62};
63
64/* UDP port for VXLAN traffic. */
65static unsigned int vxlan_port __read_mostly = 8472;
66module_param_named(udp_port, vxlan_port, uint, 0444);
67MODULE_PARM_DESC(udp_port, "Destination UDP port");
68
69static bool log_ecn_error = true;
70module_param(log_ecn_error, bool, 0644);
71MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
72
73/* per-net private data for this module */
74static unsigned int vxlan_net_id;
75struct vxlan_net {
76 struct socket *sock; /* UDP encap socket */
77 struct hlist_head vni_list[VNI_HASH_SIZE];
78};
79
80/* Forwarding table entry */
81struct vxlan_fdb {
82 struct hlist_node hlist; /* linked list of entries */
83 struct rcu_head rcu;
84 unsigned long updated; /* jiffies */
85 unsigned long used;
86 __be32 remote_ip;
87 u16 state; /* see ndm_state */
88 u8 eth_addr[ETH_ALEN];
89};
90
91/* Per-cpu network traffic stats */
92struct vxlan_stats {
93 u64 rx_packets;
94 u64 rx_bytes;
95 u64 tx_packets;
96 u64 tx_bytes;
97 struct u64_stats_sync syncp;
98};
99
100/* Pseudo network device */
101struct vxlan_dev {
102 struct hlist_node hlist;
103 struct net_device *dev;
104 struct vxlan_stats __percpu *stats;
105 __u32 vni; /* virtual network id */
106 __be32 gaddr; /* multicast group */
107 __be32 saddr; /* source address */
108 unsigned int link; /* link to multicast over */
stephen hemminger05f47d62012-10-09 20:35:50 +0000109 __u16 port_min; /* source port range */
110 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000111 __u8 tos; /* TOS override */
112 __u8 ttl;
113 bool learn;
114
115 unsigned long age_interval;
116 struct timer_list age_timer;
117 spinlock_t hash_lock;
118 unsigned int addrcnt;
119 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000120
121 struct hlist_head fdb_head[FDB_HASH_SIZE];
122};
123
124/* salt for hash table */
125static u32 vxlan_salt __read_mostly;
126
127static inline struct hlist_head *vni_head(struct net *net, u32 id)
128{
129 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
130
131 return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
132}
133
134/* Look up VNI in a per net namespace table */
135static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
136{
137 struct vxlan_dev *vxlan;
138 struct hlist_node *node;
139
140 hlist_for_each_entry_rcu(vxlan, node, vni_head(net, id), hlist) {
141 if (vxlan->vni == id)
142 return vxlan;
143 }
144
145 return NULL;
146}
147
148/* Fill in neighbour message in skbuff. */
149static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
150 const struct vxlan_fdb *fdb,
151 u32 portid, u32 seq, int type, unsigned int flags)
152{
153 unsigned long now = jiffies;
154 struct nda_cacheinfo ci;
155 struct nlmsghdr *nlh;
156 struct ndmsg *ndm;
157
158 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
159 if (nlh == NULL)
160 return -EMSGSIZE;
161
162 ndm = nlmsg_data(nlh);
163 memset(ndm, 0, sizeof(*ndm));
164 ndm->ndm_family = AF_BRIDGE;
165 ndm->ndm_state = fdb->state;
166 ndm->ndm_ifindex = vxlan->dev->ifindex;
167 ndm->ndm_flags = NTF_SELF;
168 ndm->ndm_type = NDA_DST;
169
170 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
171 goto nla_put_failure;
172
173 if (nla_put_be32(skb, NDA_DST, fdb->remote_ip))
174 goto nla_put_failure;
175
176 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
177 ci.ndm_confirmed = 0;
178 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
179 ci.ndm_refcnt = 0;
180
181 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
182 goto nla_put_failure;
183
184 return nlmsg_end(skb, nlh);
185
186nla_put_failure:
187 nlmsg_cancel(skb, nlh);
188 return -EMSGSIZE;
189}
190
191static inline size_t vxlan_nlmsg_size(void)
192{
193 return NLMSG_ALIGN(sizeof(struct ndmsg))
194 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
195 + nla_total_size(sizeof(__be32)) /* NDA_DST */
196 + nla_total_size(sizeof(struct nda_cacheinfo));
197}
198
199static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
200 const struct vxlan_fdb *fdb, int type)
201{
202 struct net *net = dev_net(vxlan->dev);
203 struct sk_buff *skb;
204 int err = -ENOBUFS;
205
206 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
207 if (skb == NULL)
208 goto errout;
209
210 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0);
211 if (err < 0) {
212 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
213 WARN_ON(err == -EMSGSIZE);
214 kfree_skb(skb);
215 goto errout;
216 }
217
218 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
219 return;
220errout:
221 if (err < 0)
222 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
223}
224
225/* Hash Ethernet address */
226static u32 eth_hash(const unsigned char *addr)
227{
228 u64 value = get_unaligned((u64 *)addr);
229
230 /* only want 6 bytes */
231#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000232 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000233#else
234 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000235#endif
236 return hash_64(value, FDB_HASH_BITS);
237}
238
239/* Hash chain to use given mac address */
240static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
241 const u8 *mac)
242{
243 return &vxlan->fdb_head[eth_hash(mac)];
244}
245
246/* Look up Ethernet address in forwarding table */
247static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
248 const u8 *mac)
249
250{
251 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
252 struct vxlan_fdb *f;
253 struct hlist_node *node;
254
255 hlist_for_each_entry_rcu(f, node, head, hlist) {
256 if (compare_ether_addr(mac, f->eth_addr) == 0)
257 return f;
258 }
259
260 return NULL;
261}
262
263/* Add new entry to forwarding table -- assumes lock held */
264static int vxlan_fdb_create(struct vxlan_dev *vxlan,
265 const u8 *mac, __be32 ip,
266 __u16 state, __u16 flags)
267{
268 struct vxlan_fdb *f;
269 int notify = 0;
270
271 f = vxlan_find_mac(vxlan, mac);
272 if (f) {
273 if (flags & NLM_F_EXCL) {
274 netdev_dbg(vxlan->dev,
275 "lost race to create %pM\n", mac);
276 return -EEXIST;
277 }
278 if (f->state != state) {
279 f->state = state;
280 f->updated = jiffies;
281 notify = 1;
282 }
283 } else {
284 if (!(flags & NLM_F_CREATE))
285 return -ENOENT;
286
287 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
288 return -ENOSPC;
289
290 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
291 f = kmalloc(sizeof(*f), GFP_ATOMIC);
292 if (!f)
293 return -ENOMEM;
294
295 notify = 1;
296 f->remote_ip = ip;
297 f->state = state;
298 f->updated = f->used = jiffies;
299 memcpy(f->eth_addr, mac, ETH_ALEN);
300
301 ++vxlan->addrcnt;
302 hlist_add_head_rcu(&f->hlist,
303 vxlan_fdb_head(vxlan, mac));
304 }
305
306 if (notify)
307 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
308
309 return 0;
310}
311
312static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
313{
314 netdev_dbg(vxlan->dev,
315 "delete %pM\n", f->eth_addr);
316
317 --vxlan->addrcnt;
318 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
319
320 hlist_del_rcu(&f->hlist);
321 kfree_rcu(f, rcu);
322}
323
324/* Add static entry (via netlink) */
325static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
326 struct net_device *dev,
327 const unsigned char *addr, u16 flags)
328{
329 struct vxlan_dev *vxlan = netdev_priv(dev);
330 __be32 ip;
331 int err;
332
333 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
334 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
335 ndm->ndm_state);
336 return -EINVAL;
337 }
338
339 if (tb[NDA_DST] == NULL)
340 return -EINVAL;
341
342 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
343 return -EAFNOSUPPORT;
344
345 ip = nla_get_be32(tb[NDA_DST]);
346
347 spin_lock_bh(&vxlan->hash_lock);
348 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags);
349 spin_unlock_bh(&vxlan->hash_lock);
350
351 return err;
352}
353
354/* Delete entry (via netlink) */
355static int vxlan_fdb_delete(struct ndmsg *ndm, struct net_device *dev,
356 const unsigned char *addr)
357{
358 struct vxlan_dev *vxlan = netdev_priv(dev);
359 struct vxlan_fdb *f;
360 int err = -ENOENT;
361
362 spin_lock_bh(&vxlan->hash_lock);
363 f = vxlan_find_mac(vxlan, addr);
364 if (f) {
365 vxlan_fdb_destroy(vxlan, f);
366 err = 0;
367 }
368 spin_unlock_bh(&vxlan->hash_lock);
369
370 return err;
371}
372
373/* Dump forwarding table */
374static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
375 struct net_device *dev, int idx)
376{
377 struct vxlan_dev *vxlan = netdev_priv(dev);
378 unsigned int h;
379
380 for (h = 0; h < FDB_HASH_SIZE; ++h) {
381 struct vxlan_fdb *f;
382 struct hlist_node *n;
383 int err;
384
385 hlist_for_each_entry_rcu(f, n, &vxlan->fdb_head[h], hlist) {
386 if (idx < cb->args[0])
387 goto skip;
388
389 err = vxlan_fdb_info(skb, vxlan, f,
390 NETLINK_CB(cb->skb).portid,
391 cb->nlh->nlmsg_seq,
392 RTM_NEWNEIGH,
393 NLM_F_MULTI);
394 if (err < 0)
395 break;
396skip:
397 ++idx;
398 }
399 }
400
401 return idx;
402}
403
404/* Watch incoming packets to learn mapping between Ethernet address
405 * and Tunnel endpoint.
406 */
407static void vxlan_snoop(struct net_device *dev,
408 __be32 src_ip, const u8 *src_mac)
409{
410 struct vxlan_dev *vxlan = netdev_priv(dev);
411 struct vxlan_fdb *f;
412 int err;
413
414 f = vxlan_find_mac(vxlan, src_mac);
415 if (likely(f)) {
416 f->used = jiffies;
417 if (likely(f->remote_ip == src_ip))
418 return;
419
420 if (net_ratelimit())
421 netdev_info(dev,
422 "%pM migrated from %pI4 to %pI4\n",
423 src_mac, &f->remote_ip, &src_ip);
424
425 f->remote_ip = src_ip;
426 f->updated = jiffies;
427 } else {
428 /* learned new entry */
429 spin_lock(&vxlan->hash_lock);
430 err = vxlan_fdb_create(vxlan, src_mac, src_ip,
431 NUD_REACHABLE,
432 NLM_F_EXCL|NLM_F_CREATE);
433 spin_unlock(&vxlan->hash_lock);
434 }
435}
436
437
438/* See if multicast group is already in use by other ID */
439static bool vxlan_group_used(struct vxlan_net *vn,
440 const struct vxlan_dev *this)
441{
442 const struct vxlan_dev *vxlan;
443 struct hlist_node *node;
444 unsigned h;
445
446 for (h = 0; h < VNI_HASH_SIZE; ++h)
447 hlist_for_each_entry(vxlan, node, &vn->vni_list[h], hlist) {
448 if (vxlan == this)
449 continue;
450
451 if (!netif_running(vxlan->dev))
452 continue;
453
454 if (vxlan->gaddr == this->gaddr)
455 return true;
456 }
457
458 return false;
459}
460
461/* kernel equivalent to IP_ADD_MEMBERSHIP */
462static int vxlan_join_group(struct net_device *dev)
463{
464 struct vxlan_dev *vxlan = netdev_priv(dev);
465 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
466 struct sock *sk = vn->sock->sk;
467 struct ip_mreqn mreq = {
468 .imr_multiaddr.s_addr = vxlan->gaddr,
469 };
470 int err;
471
472 /* Already a member of group */
473 if (vxlan_group_used(vn, vxlan))
474 return 0;
475
476 /* Need to drop RTNL to call multicast join */
477 rtnl_unlock();
478 lock_sock(sk);
479 err = ip_mc_join_group(sk, &mreq);
480 release_sock(sk);
481 rtnl_lock();
482
483 return err;
484}
485
486
487/* kernel equivalent to IP_DROP_MEMBERSHIP */
488static int vxlan_leave_group(struct net_device *dev)
489{
490 struct vxlan_dev *vxlan = netdev_priv(dev);
491 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
492 int err = 0;
493 struct sock *sk = vn->sock->sk;
494 struct ip_mreqn mreq = {
495 .imr_multiaddr.s_addr = vxlan->gaddr,
496 };
497
498 /* Only leave group when last vxlan is done. */
499 if (vxlan_group_used(vn, vxlan))
500 return 0;
501
502 /* Need to drop RTNL to call multicast leave */
503 rtnl_unlock();
504 lock_sock(sk);
505 err = ip_mc_leave_group(sk, &mreq);
506 release_sock(sk);
507 rtnl_lock();
508
509 return err;
510}
511
512/* Callback from net/ipv4/udp.c to receive packets */
513static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
514{
515 struct iphdr *oip;
516 struct vxlanhdr *vxh;
517 struct vxlan_dev *vxlan;
518 struct vxlan_stats *stats;
519 __u32 vni;
520 int err;
521
522 /* pop off outer UDP header */
523 __skb_pull(skb, sizeof(struct udphdr));
524
525 /* Need Vxlan and inner Ethernet header to be present */
526 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
527 goto error;
528
529 /* Drop packets with reserved bits set */
530 vxh = (struct vxlanhdr *) skb->data;
531 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
532 (vxh->vx_vni & htonl(0xff))) {
533 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
534 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
535 goto error;
536 }
537
538 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000539
540 /* Is this VNI defined? */
541 vni = ntohl(vxh->vx_vni) >> 8;
542 vxlan = vxlan_find_vni(sock_net(sk), vni);
543 if (!vxlan) {
544 netdev_dbg(skb->dev, "unknown vni %d\n", vni);
545 goto drop;
546 }
547
548 if (!pskb_may_pull(skb, ETH_HLEN)) {
549 vxlan->dev->stats.rx_length_errors++;
550 vxlan->dev->stats.rx_errors++;
551 goto drop;
552 }
553
554 /* Re-examine inner Ethernet packet */
555 oip = ip_hdr(skb);
556 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000557
558 /* Ignore packet loops (and multicast echo) */
559 if (compare_ether_addr(eth_hdr(skb)->h_source,
560 vxlan->dev->dev_addr) == 0)
561 goto drop;
562
563 if (vxlan->learn)
564 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
565
566 __skb_tunnel_rx(skb, vxlan->dev);
567 skb_reset_network_header(skb);
stephen hemmingerd97c00a2012-10-09 20:35:52 +0000568 skb->ip_summed = CHECKSUM_NONE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000569
570 err = IP_ECN_decapsulate(oip, skb);
571 if (unlikely(err)) {
572 if (log_ecn_error)
573 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
574 &oip->saddr, oip->tos);
575 if (err > 1) {
576 ++vxlan->dev->stats.rx_frame_errors;
577 ++vxlan->dev->stats.rx_errors;
578 goto drop;
579 }
580 }
581
582 stats = this_cpu_ptr(vxlan->stats);
583 u64_stats_update_begin(&stats->syncp);
584 stats->rx_packets++;
585 stats->rx_bytes += skb->len;
586 u64_stats_update_end(&stats->syncp);
587
588 netif_rx(skb);
589
590 return 0;
591error:
592 /* Put UDP header back */
593 __skb_push(skb, sizeof(struct udphdr));
594
595 return 1;
596drop:
597 /* Consume bad packet */
598 kfree_skb(skb);
599 return 0;
600}
601
602/* Extract dsfield from inner protocol */
603static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
604 const struct sk_buff *skb)
605{
606 if (skb->protocol == htons(ETH_P_IP))
607 return iph->tos;
608 else if (skb->protocol == htons(ETH_P_IPV6))
609 return ipv6_get_dsfield((const struct ipv6hdr *)iph);
610 else
611 return 0;
612}
613
614/* Propogate ECN bits out */
615static inline u8 vxlan_ecn_encap(u8 tos,
616 const struct iphdr *iph,
617 const struct sk_buff *skb)
618{
619 u8 inner = vxlan_get_dsfield(iph, skb);
620
621 return INET_ECN_encapsulate(tos, inner);
622}
623
stephen hemmingeref59feb2012-10-09 20:35:46 +0000624static __be32 vxlan_find_dst(struct vxlan_dev *vxlan, struct sk_buff *skb)
625{
626 const struct ethhdr *eth = (struct ethhdr *) skb->data;
627 const struct vxlan_fdb *f;
628
629 if (is_multicast_ether_addr(eth->h_dest))
630 return vxlan->gaddr;
631
632 f = vxlan_find_mac(vxlan, eth->h_dest);
633 if (f)
634 return f->remote_ip;
635 else
636 return vxlan->gaddr;
637
638}
639
stephen hemminger1cad8712012-10-09 20:35:49 +0000640static void vxlan_sock_free(struct sk_buff *skb)
641{
642 sock_put(skb->sk);
643}
644
645/* On transmit, associate with the tunnel socket */
646static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
647{
648 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
649 struct sock *sk = vn->sock->sk;
650
651 skb_orphan(skb);
652 sock_hold(sk);
653 skb->sk = sk;
654 skb->destructor = vxlan_sock_free;
655}
656
stephen hemminger05f47d62012-10-09 20:35:50 +0000657/* Compute source port for outgoing packet
658 * first choice to use L4 flow hash since it will spread
659 * better and maybe available from hardware
660 * secondary choice is to use jhash on the Ethernet header
661 */
662static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
663{
664 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
665 u32 hash;
666
667 hash = skb_get_rxhash(skb);
668 if (!hash)
669 hash = jhash(skb->data, 2 * ETH_ALEN,
670 (__force u32) skb->protocol);
671
672 return (((u64) hash * range) >> 32) + vxlan->port_min;
673}
674
stephen hemmingerd3428942012-10-01 12:32:35 +0000675/* Transmit local packets over Vxlan
676 *
677 * Outer IP header inherits ECN and DF from inner header.
678 * Outer UDP destination is the VXLAN assigned port.
stephen hemminger05f47d62012-10-09 20:35:50 +0000679 * source port is based on hash of flow
stephen hemmingerd3428942012-10-01 12:32:35 +0000680 */
681static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
682{
683 struct vxlan_dev *vxlan = netdev_priv(dev);
684 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +0000685 const struct iphdr *old_iph;
686 struct iphdr *iph;
687 struct vxlanhdr *vxh;
688 struct udphdr *uh;
689 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +0000690 unsigned int pkt_len = skb->len;
stephen hemmingerd3428942012-10-01 12:32:35 +0000691 __be32 dst;
stephen hemminger05f47d62012-10-09 20:35:50 +0000692 __u16 src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000693 __be16 df = 0;
694 __u8 tos, ttl;
695 int err;
696
stephen hemmingeref59feb2012-10-09 20:35:46 +0000697 dst = vxlan_find_dst(vxlan, skb);
698 if (!dst)
699 goto drop;
700
stephen hemmingerd3428942012-10-01 12:32:35 +0000701 /* Need space for new headers (invalidates iph ptr) */
702 if (skb_cow_head(skb, VXLAN_HEADROOM))
703 goto drop;
704
stephen hemmingerd3428942012-10-01 12:32:35 +0000705 old_iph = ip_hdr(skb);
706
stephen hemmingerd3428942012-10-01 12:32:35 +0000707 ttl = vxlan->ttl;
708 if (!ttl && IN_MULTICAST(ntohl(dst)))
709 ttl = 1;
710
711 tos = vxlan->tos;
712 if (tos == 1)
713 tos = vxlan_get_dsfield(old_iph, skb);
714
stephen hemminger05f47d62012-10-09 20:35:50 +0000715 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +0000716
stephen hemmingerca78f182012-10-09 20:35:48 +0000717 memset(&fl4, 0, sizeof(fl4));
718 fl4.flowi4_oif = vxlan->link;
719 fl4.flowi4_tos = RT_TOS(tos);
720 fl4.daddr = dst;
721 fl4.saddr = vxlan->saddr;
722
723 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +0000724 if (IS_ERR(rt)) {
725 netdev_dbg(dev, "no route to %pI4\n", &dst);
726 dev->stats.tx_carrier_errors++;
727 goto tx_error;
728 }
729
730 if (rt->dst.dev == dev) {
731 netdev_dbg(dev, "circular route to %pI4\n", &dst);
732 ip_rt_put(rt);
733 dev->stats.collisions++;
734 goto tx_error;
735 }
736
737 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
738 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
739 IPSKB_REROUTED);
740 skb_dst_drop(skb);
741 skb_dst_set(skb, &rt->dst);
742
743 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
744 vxh->vx_flags = htonl(VXLAN_FLAGS);
745 vxh->vx_vni = htonl(vxlan->vni << 8);
746
747 __skb_push(skb, sizeof(*uh));
748 skb_reset_transport_header(skb);
749 uh = udp_hdr(skb);
750
751 uh->dest = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +0000752 uh->source = htons(src_port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000753
754 uh->len = htons(skb->len);
755 uh->check = 0;
756
757 __skb_push(skb, sizeof(*iph));
758 skb_reset_network_header(skb);
759 iph = ip_hdr(skb);
760 iph->version = 4;
761 iph->ihl = sizeof(struct iphdr) >> 2;
762 iph->frag_off = df;
763 iph->protocol = IPPROTO_UDP;
764 iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
stephen hemmingerca78f182012-10-09 20:35:48 +0000765 iph->daddr = dst;
stephen hemmingerd3428942012-10-01 12:32:35 +0000766 iph->saddr = fl4.saddr;
767 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
768
stephen hemminger1cad8712012-10-09 20:35:49 +0000769 vxlan_set_owner(dev, skb);
770
Amerigo Wangaa0010f2012-11-11 21:52:33 +0000771 /* See iptunnel_xmit() */
stephen hemmingerd3428942012-10-01 12:32:35 +0000772 skb->ip_summed = CHECKSUM_NONE;
773 ip_select_ident(iph, &rt->dst, NULL);
774
775 err = ip_local_out(skb);
776 if (likely(net_xmit_eval(err) == 0)) {
777 struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
778
779 u64_stats_update_begin(&stats->syncp);
780 stats->tx_packets++;
781 stats->tx_bytes += pkt_len;
782 u64_stats_update_end(&stats->syncp);
783 } else {
784 dev->stats.tx_errors++;
785 dev->stats.tx_aborted_errors++;
786 }
787 return NETDEV_TX_OK;
788
789drop:
790 dev->stats.tx_dropped++;
791 goto tx_free;
792
793tx_error:
794 dev->stats.tx_errors++;
795tx_free:
796 dev_kfree_skb(skb);
797 return NETDEV_TX_OK;
798}
799
800/* Walk the forwarding table and purge stale entries */
801static void vxlan_cleanup(unsigned long arg)
802{
803 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
804 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
805 unsigned int h;
806
807 if (!netif_running(vxlan->dev))
808 return;
809
810 spin_lock_bh(&vxlan->hash_lock);
811 for (h = 0; h < FDB_HASH_SIZE; ++h) {
812 struct hlist_node *p, *n;
813 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
814 struct vxlan_fdb *f
815 = container_of(p, struct vxlan_fdb, hlist);
816 unsigned long timeout;
817
stephen hemminger3c172862012-10-26 06:24:34 +0000818 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +0000819 continue;
820
821 timeout = f->used + vxlan->age_interval * HZ;
822 if (time_before_eq(timeout, jiffies)) {
823 netdev_dbg(vxlan->dev,
824 "garbage collect %pM\n",
825 f->eth_addr);
826 f->state = NUD_STALE;
827 vxlan_fdb_destroy(vxlan, f);
828 } else if (time_before(timeout, next_timer))
829 next_timer = timeout;
830 }
831 }
832 spin_unlock_bh(&vxlan->hash_lock);
833
834 mod_timer(&vxlan->age_timer, next_timer);
835}
836
837/* Setup stats when device is created */
838static int vxlan_init(struct net_device *dev)
839{
840 struct vxlan_dev *vxlan = netdev_priv(dev);
841
842 vxlan->stats = alloc_percpu(struct vxlan_stats);
843 if (!vxlan->stats)
844 return -ENOMEM;
845
846 return 0;
847}
848
849/* Start ageing timer and join group when device is brought up */
850static int vxlan_open(struct net_device *dev)
851{
852 struct vxlan_dev *vxlan = netdev_priv(dev);
853 int err;
854
855 if (vxlan->gaddr) {
856 err = vxlan_join_group(dev);
857 if (err)
858 return err;
859 }
860
861 if (vxlan->age_interval)
862 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
863
864 return 0;
865}
866
867/* Purge the forwarding table */
868static void vxlan_flush(struct vxlan_dev *vxlan)
869{
870 unsigned h;
871
872 spin_lock_bh(&vxlan->hash_lock);
873 for (h = 0; h < FDB_HASH_SIZE; ++h) {
874 struct hlist_node *p, *n;
875 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
876 struct vxlan_fdb *f
877 = container_of(p, struct vxlan_fdb, hlist);
878 vxlan_fdb_destroy(vxlan, f);
879 }
880 }
881 spin_unlock_bh(&vxlan->hash_lock);
882}
883
884/* Cleanup timer and forwarding table on shutdown */
885static int vxlan_stop(struct net_device *dev)
886{
887 struct vxlan_dev *vxlan = netdev_priv(dev);
888
889 if (vxlan->gaddr)
890 vxlan_leave_group(dev);
891
892 del_timer_sync(&vxlan->age_timer);
893
894 vxlan_flush(vxlan);
895
896 return 0;
897}
898
899/* Merge per-cpu statistics */
900static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
901 struct rtnl_link_stats64 *stats)
902{
903 struct vxlan_dev *vxlan = netdev_priv(dev);
904 struct vxlan_stats tmp, sum = { 0 };
905 unsigned int cpu;
906
907 for_each_possible_cpu(cpu) {
908 unsigned int start;
909 const struct vxlan_stats *stats
910 = per_cpu_ptr(vxlan->stats, cpu);
911
912 do {
913 start = u64_stats_fetch_begin_bh(&stats->syncp);
914 memcpy(&tmp, stats, sizeof(tmp));
915 } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
916
917 sum.tx_bytes += tmp.tx_bytes;
918 sum.tx_packets += tmp.tx_packets;
919 sum.rx_bytes += tmp.rx_bytes;
920 sum.rx_packets += tmp.rx_packets;
921 }
922
923 stats->tx_bytes = sum.tx_bytes;
924 stats->tx_packets = sum.tx_packets;
925 stats->rx_bytes = sum.rx_bytes;
926 stats->rx_packets = sum.rx_packets;
927
928 stats->multicast = dev->stats.multicast;
929 stats->rx_length_errors = dev->stats.rx_length_errors;
930 stats->rx_frame_errors = dev->stats.rx_frame_errors;
931 stats->rx_errors = dev->stats.rx_errors;
932
933 stats->tx_dropped = dev->stats.tx_dropped;
934 stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
935 stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
936 stats->collisions = dev->stats.collisions;
937 stats->tx_errors = dev->stats.tx_errors;
938
939 return stats;
940}
941
942/* Stub, nothing needs to be done. */
943static void vxlan_set_multicast_list(struct net_device *dev)
944{
945}
946
947static const struct net_device_ops vxlan_netdev_ops = {
948 .ndo_init = vxlan_init,
949 .ndo_open = vxlan_open,
950 .ndo_stop = vxlan_stop,
951 .ndo_start_xmit = vxlan_xmit,
952 .ndo_get_stats64 = vxlan_stats64,
953 .ndo_set_rx_mode = vxlan_set_multicast_list,
954 .ndo_change_mtu = eth_change_mtu,
955 .ndo_validate_addr = eth_validate_addr,
956 .ndo_set_mac_address = eth_mac_addr,
957 .ndo_fdb_add = vxlan_fdb_add,
958 .ndo_fdb_del = vxlan_fdb_delete,
959 .ndo_fdb_dump = vxlan_fdb_dump,
960};
961
962/* Info for udev, that this is a virtual tunnel endpoint */
963static struct device_type vxlan_type = {
964 .name = "vxlan",
965};
966
967static void vxlan_free(struct net_device *dev)
968{
969 struct vxlan_dev *vxlan = netdev_priv(dev);
970
971 free_percpu(vxlan->stats);
972 free_netdev(dev);
973}
974
975/* Initialize the device structure. */
976static void vxlan_setup(struct net_device *dev)
977{
978 struct vxlan_dev *vxlan = netdev_priv(dev);
979 unsigned h;
stephen hemminger05f47d62012-10-09 20:35:50 +0000980 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +0000981
982 eth_hw_addr_random(dev);
983 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +0000984 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +0000985
986 dev->netdev_ops = &vxlan_netdev_ops;
987 dev->destructor = vxlan_free;
988 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
989
990 dev->tx_queue_len = 0;
991 dev->features |= NETIF_F_LLTX;
992 dev->features |= NETIF_F_NETNS_LOCAL;
993 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
994
995 spin_lock_init(&vxlan->hash_lock);
996
997 init_timer_deferrable(&vxlan->age_timer);
998 vxlan->age_timer.function = vxlan_cleanup;
999 vxlan->age_timer.data = (unsigned long) vxlan;
1000
stephen hemminger05f47d62012-10-09 20:35:50 +00001001 inet_get_local_port_range(&low, &high);
1002 vxlan->port_min = low;
1003 vxlan->port_max = high;
1004
stephen hemmingerd3428942012-10-01 12:32:35 +00001005 vxlan->dev = dev;
1006
1007 for (h = 0; h < FDB_HASH_SIZE; ++h)
1008 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1009}
1010
1011static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1012 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1013 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1014 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1015 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1016 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1017 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1018 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1019 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1020 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001021 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001022};
1023
1024static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1025{
1026 if (tb[IFLA_ADDRESS]) {
1027 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1028 pr_debug("invalid link address (not ethernet)\n");
1029 return -EINVAL;
1030 }
1031
1032 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1033 pr_debug("invalid all zero ethernet address\n");
1034 return -EADDRNOTAVAIL;
1035 }
1036 }
1037
1038 if (!data)
1039 return -EINVAL;
1040
1041 if (data[IFLA_VXLAN_ID]) {
1042 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1043 if (id >= VXLAN_VID_MASK)
1044 return -ERANGE;
1045 }
1046
1047 if (data[IFLA_VXLAN_GROUP]) {
1048 __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1049 if (!IN_MULTICAST(ntohl(gaddr))) {
1050 pr_debug("group address is not IPv4 multicast\n");
1051 return -EADDRNOTAVAIL;
1052 }
1053 }
stephen hemminger05f47d62012-10-09 20:35:50 +00001054
1055 if (data[IFLA_VXLAN_PORT_RANGE]) {
1056 const struct ifla_vxlan_port_range *p
1057 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1058
1059 if (ntohs(p->high) < ntohs(p->low)) {
1060 pr_debug("port range %u .. %u not valid\n",
1061 ntohs(p->low), ntohs(p->high));
1062 return -EINVAL;
1063 }
1064 }
1065
stephen hemmingerd3428942012-10-01 12:32:35 +00001066 return 0;
1067}
1068
1069static int vxlan_newlink(struct net *net, struct net_device *dev,
1070 struct nlattr *tb[], struct nlattr *data[])
1071{
1072 struct vxlan_dev *vxlan = netdev_priv(dev);
1073 __u32 vni;
1074 int err;
1075
1076 if (!data[IFLA_VXLAN_ID])
1077 return -EINVAL;
1078
1079 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1080 if (vxlan_find_vni(net, vni)) {
1081 pr_info("duplicate VNI %u\n", vni);
1082 return -EEXIST;
1083 }
1084 vxlan->vni = vni;
1085
1086 if (data[IFLA_VXLAN_GROUP])
1087 vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1088
1089 if (data[IFLA_VXLAN_LOCAL])
1090 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1091
stephen hemminger34e02aa2012-10-09 20:35:53 +00001092 if (data[IFLA_VXLAN_LINK] &&
1093 (vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1094 struct net_device *lowerdev
1095 = __dev_get_by_index(net, vxlan->link);
stephen hemmingerd3428942012-10-01 12:32:35 +00001096
stephen hemminger34e02aa2012-10-09 20:35:53 +00001097 if (!lowerdev) {
1098 pr_info("ifindex %d does not exist\n", vxlan->link);
1099 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001100 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001101
1102 if (!tb[IFLA_MTU])
1103 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001104
1105 /* update header length based on lower device */
1106 dev->hard_header_len = lowerdev->hard_header_len +
1107 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001108 }
1109
1110 if (data[IFLA_VXLAN_TOS])
1111 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1112
Vincent Bernatafb97182012-10-30 10:27:16 +00001113 if (data[IFLA_VXLAN_TTL])
1114 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1115
stephen hemmingerd3428942012-10-01 12:32:35 +00001116 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
1117 vxlan->learn = true;
1118
1119 if (data[IFLA_VXLAN_AGEING])
1120 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1121 else
1122 vxlan->age_interval = FDB_AGE_DEFAULT;
1123
1124 if (data[IFLA_VXLAN_LIMIT])
1125 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1126
stephen hemminger05f47d62012-10-09 20:35:50 +00001127 if (data[IFLA_VXLAN_PORT_RANGE]) {
1128 const struct ifla_vxlan_port_range *p
1129 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1130 vxlan->port_min = ntohs(p->low);
1131 vxlan->port_max = ntohs(p->high);
1132 }
1133
stephen hemmingerd3428942012-10-01 12:32:35 +00001134 err = register_netdevice(dev);
1135 if (!err)
1136 hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
1137
1138 return err;
1139}
1140
1141static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1142{
1143 struct vxlan_dev *vxlan = netdev_priv(dev);
1144
1145 hlist_del_rcu(&vxlan->hlist);
1146
1147 unregister_netdevice_queue(dev, head);
1148}
1149
1150static size_t vxlan_get_size(const struct net_device *dev)
1151{
1152
1153 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1154 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
1155 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1156 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1157 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1158 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1159 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
1160 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1161 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001162 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemmingerd3428942012-10-01 12:32:35 +00001163 0;
1164}
1165
1166static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1167{
1168 const struct vxlan_dev *vxlan = netdev_priv(dev);
stephen hemminger05f47d62012-10-09 20:35:50 +00001169 struct ifla_vxlan_port_range ports = {
1170 .low = htons(vxlan->port_min),
1171 .high = htons(vxlan->port_max),
1172 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001173
1174 if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
1175 goto nla_put_failure;
1176
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001177 if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001178 goto nla_put_failure;
1179
1180 if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
1181 goto nla_put_failure;
1182
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001183 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001184 goto nla_put_failure;
1185
1186 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1187 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
1188 nla_put_u8(skb, IFLA_VXLAN_LEARNING, vxlan->learn) ||
1189 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1190 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
1191 goto nla_put_failure;
1192
stephen hemminger05f47d62012-10-09 20:35:50 +00001193 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1194 goto nla_put_failure;
1195
stephen hemmingerd3428942012-10-01 12:32:35 +00001196 return 0;
1197
1198nla_put_failure:
1199 return -EMSGSIZE;
1200}
1201
1202static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1203 .kind = "vxlan",
1204 .maxtype = IFLA_VXLAN_MAX,
1205 .policy = vxlan_policy,
1206 .priv_size = sizeof(struct vxlan_dev),
1207 .setup = vxlan_setup,
1208 .validate = vxlan_validate,
1209 .newlink = vxlan_newlink,
1210 .dellink = vxlan_dellink,
1211 .get_size = vxlan_get_size,
1212 .fill_info = vxlan_fill_info,
1213};
1214
1215static __net_init int vxlan_init_net(struct net *net)
1216{
1217 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1218 struct sock *sk;
1219 struct sockaddr_in vxlan_addr = {
1220 .sin_family = AF_INET,
1221 .sin_addr.s_addr = htonl(INADDR_ANY),
1222 };
1223 int rc;
1224 unsigned h;
1225
1226 /* Create UDP socket for encapsulation receive. */
1227 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
1228 if (rc < 0) {
1229 pr_debug("UDP socket create failed\n");
1230 return rc;
1231 }
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001232 /* Put in proper namespace */
1233 sk = vn->sock->sk;
1234 sk_change_net(sk, net);
stephen hemmingerd3428942012-10-01 12:32:35 +00001235
1236 vxlan_addr.sin_port = htons(vxlan_port);
1237
1238 rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
1239 sizeof(vxlan_addr));
1240 if (rc < 0) {
1241 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1242 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001243 sk_release_kernel(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001244 vn->sock = NULL;
1245 return rc;
1246 }
1247
1248 /* Disable multicast loopback */
stephen hemmingerd3428942012-10-01 12:32:35 +00001249 inet_sk(sk)->mc_loop = 0;
1250
1251 /* Mark socket as an encapsulation socket. */
1252 udp_sk(sk)->encap_type = 1;
1253 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1254 udp_encap_enable();
1255
1256 for (h = 0; h < VNI_HASH_SIZE; ++h)
1257 INIT_HLIST_HEAD(&vn->vni_list[h]);
1258
1259 return 0;
1260}
1261
1262static __net_exit void vxlan_exit_net(struct net *net)
1263{
1264 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1265
1266 if (vn->sock) {
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001267 sk_release_kernel(vn->sock->sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001268 vn->sock = NULL;
1269 }
1270}
1271
1272static struct pernet_operations vxlan_net_ops = {
1273 .init = vxlan_init_net,
1274 .exit = vxlan_exit_net,
1275 .id = &vxlan_net_id,
1276 .size = sizeof(struct vxlan_net),
1277};
1278
1279static int __init vxlan_init_module(void)
1280{
1281 int rc;
1282
1283 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1284
1285 rc = register_pernet_device(&vxlan_net_ops);
1286 if (rc)
1287 goto out1;
1288
1289 rc = rtnl_link_register(&vxlan_link_ops);
1290 if (rc)
1291 goto out2;
1292
1293 return 0;
1294
1295out2:
1296 unregister_pernet_device(&vxlan_net_ops);
1297out1:
1298 return rc;
1299}
1300module_init(vxlan_init_module);
1301
1302static void __exit vxlan_cleanup_module(void)
1303{
1304 rtnl_link_unregister(&vxlan_link_ops);
1305 unregister_pernet_device(&vxlan_net_ops);
1306}
1307module_exit(vxlan_cleanup_module);
1308
1309MODULE_LICENSE("GPL");
1310MODULE_VERSION(VXLAN_VERSION);
1311MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
1312MODULE_ALIAS_RTNL_LINK("vxlan");