blob: d8de8a1303eba465fe734ba519d364a4ba69ed83 [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 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
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
stephen hemmingerd3428942012-10-01 12:32:35 +000011 * - IPv6 (not in RFC)
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/slab.h>
21#include <linux/skbuff.h>
22#include <linux/rculist.h>
23#include <linux/netdevice.h>
24#include <linux/in.h>
25#include <linux/ip.h>
26#include <linux/udp.h>
27#include <linux/igmp.h>
28#include <linux/etherdevice.h>
29#include <linux/if_ether.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000030#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000031#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000032#include <net/arp.h>
33#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000035#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000036#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
stephen hemminger23c578b2013-04-27 11:31:53 +000067/* UDP port for VXLAN traffic.
68 * The IANA assigned port is 4789, but the Linux default is 8472
69 * for compatability with early adopters.
70 */
stephen hemmingerd3428942012-10-01 12:32:35 +000071static unsigned int vxlan_port __read_mostly = 8472;
72module_param_named(udp_port, vxlan_port, uint, 0444);
73MODULE_PARM_DESC(udp_port, "Destination UDP port");
74
75static bool log_ecn_error = true;
76module_param(log_ecn_error, bool, 0644);
77MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
78
79/* per-net private data for this module */
80static unsigned int vxlan_net_id;
81struct vxlan_net {
82 struct socket *sock; /* UDP encap socket */
83 struct hlist_head vni_list[VNI_HASH_SIZE];
84};
85
David Stevens66817122013-03-15 04:35:51 +000086struct vxlan_rdst {
87 struct rcu_head rcu;
88 __be32 remote_ip;
89 __be16 remote_port;
90 u32 remote_vni;
91 u32 remote_ifindex;
92 struct vxlan_rdst *remote_next;
93};
94
stephen hemmingerd3428942012-10-01 12:32:35 +000095/* Forwarding table entry */
96struct vxlan_fdb {
97 struct hlist_node hlist; /* linked list of entries */
98 struct rcu_head rcu;
99 unsigned long updated; /* jiffies */
100 unsigned long used;
David Stevens66817122013-03-15 04:35:51 +0000101 struct vxlan_rdst remote;
stephen hemmingerd3428942012-10-01 12:32:35 +0000102 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000103 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000104 u8 eth_addr[ETH_ALEN];
105};
106
stephen hemmingerd3428942012-10-01 12:32:35 +0000107/* Pseudo network device */
108struct vxlan_dev {
109 struct hlist_node hlist;
110 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000111 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000112 __be32 saddr; /* source address */
stephen hemminger05f47d62012-10-09 20:35:50 +0000113 __u16 port_min; /* source port range */
114 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000115 __u8 tos; /* TOS override */
116 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000117 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000118
119 unsigned long age_interval;
120 struct timer_list age_timer;
121 spinlock_t hash_lock;
122 unsigned int addrcnt;
123 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000124
125 struct hlist_head fdb_head[FDB_HASH_SIZE];
126};
127
David Stevense4f67ad2012-11-20 02:50:14 +0000128#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
133
stephen hemmingerd3428942012-10-01 12:32:35 +0000134/* salt for hash table */
135static u32 vxlan_salt __read_mostly;
136
137static inline struct hlist_head *vni_head(struct net *net, u32 id)
138{
139 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
140
141 return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
142}
143
144/* Look up VNI in a per net namespace table */
145static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
146{
147 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000148
Sasha Levinb67bfe02013-02-27 17:06:00 -0800149 hlist_for_each_entry_rcu(vxlan, vni_head(net, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000150 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000151 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,
David Stevens66817122013-03-15 04:35:51 +0000160 u32 portid, u32 seq, int type, unsigned int flags,
161 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000162{
163 unsigned long now = jiffies;
164 struct nda_cacheinfo ci;
165 struct nlmsghdr *nlh;
166 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000167 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000168
169 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
170 if (nlh == NULL)
171 return -EMSGSIZE;
172
173 ndm = nlmsg_data(nlh);
174 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000175
176 send_eth = send_ip = true;
177
178 if (type == RTM_GETNEIGH) {
179 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000180 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000181 send_eth = !is_zero_ether_addr(fdb->eth_addr);
182 } else
183 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000184 ndm->ndm_state = fdb->state;
185 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000186 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000187 ndm->ndm_type = NDA_DST;
188
David Stevense4f67ad2012-11-20 02:50:14 +0000189 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000190 goto nla_put_failure;
191
David Stevens66817122013-03-15 04:35:51 +0000192 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
193 goto nla_put_failure;
194
stephen hemminger73cf3312013-04-27 11:31:54 +0000195 if (rdst->remote_port && rdst->remote_port != htons(vxlan_port) &&
David Stevens66817122013-03-15 04:35:51 +0000196 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
197 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000198 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
David Stevens66817122013-03-15 04:35:51 +0000199 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
200 goto nla_put_failure;
201 if (rdst->remote_ifindex &&
202 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000203 goto nla_put_failure;
204
205 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
206 ci.ndm_confirmed = 0;
207 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
208 ci.ndm_refcnt = 0;
209
210 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
211 goto nla_put_failure;
212
213 return nlmsg_end(skb, nlh);
214
215nla_put_failure:
216 nlmsg_cancel(skb, nlh);
217 return -EMSGSIZE;
218}
219
220static inline size_t vxlan_nlmsg_size(void)
221{
222 return NLMSG_ALIGN(sizeof(struct ndmsg))
223 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
224 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000225 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000226 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
227 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000228 + nla_total_size(sizeof(struct nda_cacheinfo));
229}
230
231static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
232 const struct vxlan_fdb *fdb, int type)
233{
234 struct net *net = dev_net(vxlan->dev);
235 struct sk_buff *skb;
236 int err = -ENOBUFS;
237
238 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
239 if (skb == NULL)
240 goto errout;
241
David Stevens66817122013-03-15 04:35:51 +0000242 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
stephen hemmingerd3428942012-10-01 12:32:35 +0000243 if (err < 0) {
244 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
245 WARN_ON(err == -EMSGSIZE);
246 kfree_skb(skb);
247 goto errout;
248 }
249
250 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
251 return;
252errout:
253 if (err < 0)
254 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
255}
256
David Stevense4f67ad2012-11-20 02:50:14 +0000257static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
258{
259 struct vxlan_dev *vxlan = netdev_priv(dev);
260 struct vxlan_fdb f;
261
262 memset(&f, 0, sizeof f);
263 f.state = NUD_STALE;
David Stevens66817122013-03-15 04:35:51 +0000264 f.remote.remote_ip = ipa; /* goes to NDA_DST */
265 f.remote.remote_vni = VXLAN_N_VID;
David Stevense4f67ad2012-11-20 02:50:14 +0000266
267 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
268}
269
270static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
271{
272 struct vxlan_fdb f;
273
274 memset(&f, 0, sizeof f);
275 f.state = NUD_STALE;
276 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
277
278 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
279}
280
stephen hemmingerd3428942012-10-01 12:32:35 +0000281/* Hash Ethernet address */
282static u32 eth_hash(const unsigned char *addr)
283{
284 u64 value = get_unaligned((u64 *)addr);
285
286 /* only want 6 bytes */
287#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000288 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000289#else
290 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000291#endif
292 return hash_64(value, FDB_HASH_BITS);
293}
294
295/* Hash chain to use given mac address */
296static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
297 const u8 *mac)
298{
299 return &vxlan->fdb_head[eth_hash(mac)];
300}
301
302/* Look up Ethernet address in forwarding table */
303static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
304 const u8 *mac)
305
306{
307 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
308 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000309
Sasha Levinb67bfe02013-02-27 17:06:00 -0800310 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000311 if (compare_ether_addr(mac, f->eth_addr) == 0)
312 return f;
313 }
314
315 return NULL;
316}
317
David Stevens66817122013-03-15 04:35:51 +0000318/* Add/update destinations for multicast */
319static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000320 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000321{
322 struct vxlan_rdst *rd_prev, *rd;
323
324 rd_prev = NULL;
325 for (rd = &f->remote; rd; rd = rd->remote_next) {
326 if (rd->remote_ip == ip &&
327 rd->remote_port == port &&
328 rd->remote_vni == vni &&
329 rd->remote_ifindex == ifindex)
330 return 0;
331 rd_prev = rd;
332 }
333 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
334 if (rd == NULL)
335 return -ENOBUFS;
336 rd->remote_ip = ip;
337 rd->remote_port = port;
338 rd->remote_vni = vni;
339 rd->remote_ifindex = ifindex;
340 rd->remote_next = NULL;
341 rd_prev->remote_next = rd;
342 return 1;
343}
344
stephen hemmingerd3428942012-10-01 12:32:35 +0000345/* Add new entry to forwarding table -- assumes lock held */
346static int vxlan_fdb_create(struct vxlan_dev *vxlan,
347 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000348 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000349 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000350 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000351{
352 struct vxlan_fdb *f;
353 int notify = 0;
354
355 f = vxlan_find_mac(vxlan, mac);
356 if (f) {
357 if (flags & NLM_F_EXCL) {
358 netdev_dbg(vxlan->dev,
359 "lost race to create %pM\n", mac);
360 return -EEXIST;
361 }
362 if (f->state != state) {
363 f->state = state;
364 f->updated = jiffies;
365 notify = 1;
366 }
David Stevensae884082013-04-19 00:36:26 +0000367 if (f->flags != ndm_flags) {
368 f->flags = ndm_flags;
369 f->updated = jiffies;
370 notify = 1;
371 }
David Stevens66817122013-03-15 04:35:51 +0000372 if ((flags & NLM_F_APPEND) &&
373 is_multicast_ether_addr(f->eth_addr)) {
374 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
375
376 if (rc < 0)
377 return rc;
378 notify |= rc;
379 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000380 } else {
381 if (!(flags & NLM_F_CREATE))
382 return -ENOENT;
383
384 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
385 return -ENOSPC;
386
387 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
388 f = kmalloc(sizeof(*f), GFP_ATOMIC);
389 if (!f)
390 return -ENOMEM;
391
392 notify = 1;
David Stevens66817122013-03-15 04:35:51 +0000393 f->remote.remote_ip = ip;
394 f->remote.remote_port = port;
395 f->remote.remote_vni = vni;
396 f->remote.remote_ifindex = ifindex;
397 f->remote.remote_next = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000398 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000399 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000400 f->updated = f->used = jiffies;
401 memcpy(f->eth_addr, mac, ETH_ALEN);
402
403 ++vxlan->addrcnt;
404 hlist_add_head_rcu(&f->hlist,
405 vxlan_fdb_head(vxlan, mac));
406 }
407
408 if (notify)
409 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
410
411 return 0;
412}
413
Wei Yongjun6706c822013-04-11 19:00:35 +0000414static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000415{
416 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
417
418 while (f->remote.remote_next) {
419 struct vxlan_rdst *rd = f->remote.remote_next;
420
421 f->remote.remote_next = rd->remote_next;
422 kfree(rd);
423 }
424 kfree(f);
425}
426
stephen hemmingerd3428942012-10-01 12:32:35 +0000427static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
428{
429 netdev_dbg(vxlan->dev,
430 "delete %pM\n", f->eth_addr);
431
432 --vxlan->addrcnt;
433 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
434
435 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000436 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000437}
438
439/* Add static entry (via netlink) */
440static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
441 struct net_device *dev,
442 const unsigned char *addr, u16 flags)
443{
444 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens66817122013-03-15 04:35:51 +0000445 struct net *net = dev_net(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000446 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000447 __be16 port;
448 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000449 int err;
450
451 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
452 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
453 ndm->ndm_state);
454 return -EINVAL;
455 }
456
457 if (tb[NDA_DST] == NULL)
458 return -EINVAL;
459
460 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
461 return -EAFNOSUPPORT;
462
463 ip = nla_get_be32(tb[NDA_DST]);
464
David Stevens66817122013-03-15 04:35:51 +0000465 if (tb[NDA_PORT]) {
stephen hemminger73cf3312013-04-27 11:31:54 +0000466 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
David Stevens66817122013-03-15 04:35:51 +0000467 return -EINVAL;
stephen hemminger73cf3312013-04-27 11:31:54 +0000468 port = nla_get_be16(tb[NDA_PORT]);
David Stevens66817122013-03-15 04:35:51 +0000469 } else
stephen hemminger73cf3312013-04-27 11:31:54 +0000470 port = htons(vxlan_port);
David Stevens66817122013-03-15 04:35:51 +0000471
472 if (tb[NDA_VNI]) {
473 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
474 return -EINVAL;
475 vni = nla_get_u32(tb[NDA_VNI]);
476 } else
Atzm Watanabec7995c42013-04-16 02:50:52 +0000477 vni = vxlan->default_dst.remote_vni;
David Stevens66817122013-03-15 04:35:51 +0000478
479 if (tb[NDA_IFINDEX]) {
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000480 struct net_device *tdev;
David Stevens66817122013-03-15 04:35:51 +0000481
482 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
483 return -EINVAL;
484 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000485 tdev = dev_get_by_index(net, ifindex);
486 if (!tdev)
David Stevens66817122013-03-15 04:35:51 +0000487 return -EADDRNOTAVAIL;
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000488 dev_put(tdev);
David Stevens66817122013-03-15 04:35:51 +0000489 } else
490 ifindex = 0;
491
stephen hemmingerd3428942012-10-01 12:32:35 +0000492 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000493 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
494 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000495 spin_unlock_bh(&vxlan->hash_lock);
496
497 return err;
498}
499
500/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000501static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
502 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000503 const unsigned char *addr)
504{
505 struct vxlan_dev *vxlan = netdev_priv(dev);
506 struct vxlan_fdb *f;
507 int err = -ENOENT;
508
509 spin_lock_bh(&vxlan->hash_lock);
510 f = vxlan_find_mac(vxlan, addr);
511 if (f) {
512 vxlan_fdb_destroy(vxlan, f);
513 err = 0;
514 }
515 spin_unlock_bh(&vxlan->hash_lock);
516
517 return err;
518}
519
520/* Dump forwarding table */
521static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
522 struct net_device *dev, int idx)
523{
524 struct vxlan_dev *vxlan = netdev_priv(dev);
525 unsigned int h;
526
527 for (h = 0; h < FDB_HASH_SIZE; ++h) {
528 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000529 int err;
530
Sasha Levinb67bfe02013-02-27 17:06:00 -0800531 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000532 struct vxlan_rdst *rd;
533 for (rd = &f->remote; rd; rd = rd->remote_next) {
534 if (idx < cb->args[0])
535 goto skip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000536
David Stevens66817122013-03-15 04:35:51 +0000537 err = vxlan_fdb_info(skb, vxlan, f,
538 NETLINK_CB(cb->skb).portid,
539 cb->nlh->nlmsg_seq,
540 RTM_NEWNEIGH,
541 NLM_F_MULTI, rd);
542 if (err < 0)
543 break;
stephen hemmingerd3428942012-10-01 12:32:35 +0000544skip:
David Stevens66817122013-03-15 04:35:51 +0000545 ++idx;
546 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000547 }
548 }
549
550 return idx;
551}
552
553/* Watch incoming packets to learn mapping between Ethernet address
554 * and Tunnel endpoint.
555 */
556static void vxlan_snoop(struct net_device *dev,
557 __be32 src_ip, const u8 *src_mac)
558{
559 struct vxlan_dev *vxlan = netdev_priv(dev);
560 struct vxlan_fdb *f;
561 int err;
562
563 f = vxlan_find_mac(vxlan, src_mac);
564 if (likely(f)) {
565 f->used = jiffies;
David Stevens66817122013-03-15 04:35:51 +0000566 if (likely(f->remote.remote_ip == src_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +0000567 return;
568
569 if (net_ratelimit())
570 netdev_info(dev,
571 "%pM migrated from %pI4 to %pI4\n",
David Stevens66817122013-03-15 04:35:51 +0000572 src_mac, &f->remote.remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000573
David Stevens66817122013-03-15 04:35:51 +0000574 f->remote.remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000575 f->updated = jiffies;
576 } else {
577 /* learned new entry */
578 spin_lock(&vxlan->hash_lock);
579 err = vxlan_fdb_create(vxlan, src_mac, src_ip,
580 NUD_REACHABLE,
David Stevens66817122013-03-15 04:35:51 +0000581 NLM_F_EXCL|NLM_F_CREATE,
David Stevensae884082013-04-19 00:36:26 +0000582 vxlan_port,
583 vxlan->default_dst.remote_vni,
584 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000585 spin_unlock(&vxlan->hash_lock);
586 }
587}
588
589
590/* See if multicast group is already in use by other ID */
591static bool vxlan_group_used(struct vxlan_net *vn,
592 const struct vxlan_dev *this)
593{
594 const struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000595 unsigned h;
596
597 for (h = 0; h < VNI_HASH_SIZE; ++h)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800598 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000599 if (vxlan == this)
600 continue;
601
602 if (!netif_running(vxlan->dev))
603 continue;
604
Atzm Watanabec7995c42013-04-16 02:50:52 +0000605 if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000606 return true;
607 }
608
609 return false;
610}
611
612/* kernel equivalent to IP_ADD_MEMBERSHIP */
613static int vxlan_join_group(struct net_device *dev)
614{
615 struct vxlan_dev *vxlan = netdev_priv(dev);
616 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
617 struct sock *sk = vn->sock->sk;
618 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000619 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
620 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000621 };
622 int err;
623
624 /* Already a member of group */
625 if (vxlan_group_used(vn, vxlan))
626 return 0;
627
628 /* Need to drop RTNL to call multicast join */
629 rtnl_unlock();
630 lock_sock(sk);
631 err = ip_mc_join_group(sk, &mreq);
632 release_sock(sk);
633 rtnl_lock();
634
635 return err;
636}
637
638
639/* kernel equivalent to IP_DROP_MEMBERSHIP */
640static int vxlan_leave_group(struct net_device *dev)
641{
642 struct vxlan_dev *vxlan = netdev_priv(dev);
643 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
644 int err = 0;
645 struct sock *sk = vn->sock->sk;
646 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000647 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
648 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000649 };
650
651 /* Only leave group when last vxlan is done. */
652 if (vxlan_group_used(vn, vxlan))
653 return 0;
654
655 /* Need to drop RTNL to call multicast leave */
656 rtnl_unlock();
657 lock_sock(sk);
658 err = ip_mc_leave_group(sk, &mreq);
659 release_sock(sk);
660 rtnl_lock();
661
662 return err;
663}
664
665/* Callback from net/ipv4/udp.c to receive packets */
666static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
667{
668 struct iphdr *oip;
669 struct vxlanhdr *vxh;
670 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000671 struct pcpu_tstats *stats;
stephen hemmingerd3428942012-10-01 12:32:35 +0000672 __u32 vni;
673 int err;
674
675 /* pop off outer UDP header */
676 __skb_pull(skb, sizeof(struct udphdr));
677
678 /* Need Vxlan and inner Ethernet header to be present */
679 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
680 goto error;
681
682 /* Drop packets with reserved bits set */
683 vxh = (struct vxlanhdr *) skb->data;
684 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
685 (vxh->vx_vni & htonl(0xff))) {
686 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
687 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
688 goto error;
689 }
690
691 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000692
693 /* Is this VNI defined? */
694 vni = ntohl(vxh->vx_vni) >> 8;
695 vxlan = vxlan_find_vni(sock_net(sk), vni);
696 if (!vxlan) {
697 netdev_dbg(skb->dev, "unknown vni %d\n", vni);
698 goto drop;
699 }
700
701 if (!pskb_may_pull(skb, ETH_HLEN)) {
702 vxlan->dev->stats.rx_length_errors++;
703 vxlan->dev->stats.rx_errors++;
704 goto drop;
705 }
706
David Stevense4f67ad2012-11-20 02:50:14 +0000707 skb_reset_mac_header(skb);
708
stephen hemmingerd3428942012-10-01 12:32:35 +0000709 /* Re-examine inner Ethernet packet */
710 oip = ip_hdr(skb);
711 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000712
713 /* Ignore packet loops (and multicast echo) */
714 if (compare_ether_addr(eth_hdr(skb)->h_source,
715 vxlan->dev->dev_addr) == 0)
716 goto drop;
717
David Stevense4f67ad2012-11-20 02:50:14 +0000718 if (vxlan->flags & VXLAN_F_LEARN)
stephen hemmingerd3428942012-10-01 12:32:35 +0000719 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
720
721 __skb_tunnel_rx(skb, vxlan->dev);
722 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000723
724 /* If the NIC driver gave us an encapsulated packet with
725 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
726 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
727 * for us. Otherwise force the upper layers to verify it.
728 */
729 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
730 !(vxlan->dev->features & NETIF_F_RXCSUM))
731 skb->ip_summed = CHECKSUM_NONE;
732
733 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000734
735 err = IP_ECN_decapsulate(oip, skb);
736 if (unlikely(err)) {
737 if (log_ecn_error)
738 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
739 &oip->saddr, oip->tos);
740 if (err > 1) {
741 ++vxlan->dev->stats.rx_frame_errors;
742 ++vxlan->dev->stats.rx_errors;
743 goto drop;
744 }
745 }
746
Pravin B Shelare8171042013-03-25 14:49:46 +0000747 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000748 u64_stats_update_begin(&stats->syncp);
749 stats->rx_packets++;
750 stats->rx_bytes += skb->len;
751 u64_stats_update_end(&stats->syncp);
752
753 netif_rx(skb);
754
755 return 0;
756error:
757 /* Put UDP header back */
758 __skb_push(skb, sizeof(struct udphdr));
759
760 return 1;
761drop:
762 /* Consume bad packet */
763 kfree_skb(skb);
764 return 0;
765}
766
David Stevense4f67ad2012-11-20 02:50:14 +0000767static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
768{
769 struct vxlan_dev *vxlan = netdev_priv(dev);
770 struct arphdr *parp;
771 u8 *arpptr, *sha;
772 __be32 sip, tip;
773 struct neighbour *n;
774
775 if (dev->flags & IFF_NOARP)
776 goto out;
777
778 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
779 dev->stats.tx_dropped++;
780 goto out;
781 }
782 parp = arp_hdr(skb);
783
784 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
785 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
786 parp->ar_pro != htons(ETH_P_IP) ||
787 parp->ar_op != htons(ARPOP_REQUEST) ||
788 parp->ar_hln != dev->addr_len ||
789 parp->ar_pln != 4)
790 goto out;
791 arpptr = (u8 *)parp + sizeof(struct arphdr);
792 sha = arpptr;
793 arpptr += dev->addr_len; /* sha */
794 memcpy(&sip, arpptr, sizeof(sip));
795 arpptr += sizeof(sip);
796 arpptr += dev->addr_len; /* tha */
797 memcpy(&tip, arpptr, sizeof(tip));
798
799 if (ipv4_is_loopback(tip) ||
800 ipv4_is_multicast(tip))
801 goto out;
802
803 n = neigh_lookup(&arp_tbl, &tip, dev);
804
805 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000806 struct vxlan_fdb *f;
807 struct sk_buff *reply;
808
809 if (!(n->nud_state & NUD_CONNECTED)) {
810 neigh_release(n);
811 goto out;
812 }
813
814 f = vxlan_find_mac(vxlan, n->ha);
David Stevens66817122013-03-15 04:35:51 +0000815 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000816 /* bridge-local neighbor */
817 neigh_release(n);
818 goto out;
819 }
820
821 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
822 n->ha, sha);
823
824 neigh_release(n);
825
826 skb_reset_mac_header(reply);
827 __skb_pull(reply, skb_network_offset(reply));
828 reply->ip_summed = CHECKSUM_UNNECESSARY;
829 reply->pkt_type = PACKET_HOST;
830
831 if (netif_rx_ni(reply) == NET_RX_DROP)
832 dev->stats.rx_dropped++;
833 } else if (vxlan->flags & VXLAN_F_L3MISS)
834 vxlan_ip_miss(dev, tip);
835out:
836 consume_skb(skb);
837 return NETDEV_TX_OK;
838}
839
840static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
841{
842 struct vxlan_dev *vxlan = netdev_priv(dev);
843 struct neighbour *n;
844 struct iphdr *pip;
845
846 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
847 return false;
848
849 n = NULL;
850 switch (ntohs(eth_hdr(skb)->h_proto)) {
851 case ETH_P_IP:
852 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
853 return false;
854 pip = ip_hdr(skb);
855 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
856 break;
857 default:
858 return false;
859 }
860
861 if (n) {
862 bool diff;
863
864 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
865 if (diff) {
866 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
867 dev->addr_len);
868 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
869 }
870 neigh_release(n);
871 return diff;
872 } else if (vxlan->flags & VXLAN_F_L3MISS)
873 vxlan_ip_miss(dev, pip->daddr);
874 return false;
875}
876
stephen hemminger1cad8712012-10-09 20:35:49 +0000877static void vxlan_sock_free(struct sk_buff *skb)
878{
879 sock_put(skb->sk);
880}
881
882/* On transmit, associate with the tunnel socket */
883static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
884{
885 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
886 struct sock *sk = vn->sock->sk;
887
888 skb_orphan(skb);
889 sock_hold(sk);
890 skb->sk = sk;
891 skb->destructor = vxlan_sock_free;
892}
893
stephen hemminger05f47d62012-10-09 20:35:50 +0000894/* Compute source port for outgoing packet
895 * first choice to use L4 flow hash since it will spread
896 * better and maybe available from hardware
897 * secondary choice is to use jhash on the Ethernet header
898 */
899static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
900{
901 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
902 u32 hash;
903
904 hash = skb_get_rxhash(skb);
905 if (!hash)
906 hash = jhash(skb->data, 2 * ETH_ALEN,
907 (__force u32) skb->protocol);
908
909 return (((u64) hash * range) >> 32) + vxlan->port_min;
910}
911
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000912static int handle_offloads(struct sk_buff *skb)
913{
914 if (skb_is_gso(skb)) {
915 int err = skb_unclone(skb, GFP_ATOMIC);
916 if (unlikely(err))
917 return err;
918
919 skb_shinfo(skb)->gso_type |= (SKB_GSO_UDP_TUNNEL | SKB_GSO_UDP);
920 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
921 skb->ip_summed = CHECKSUM_NONE;
922
923 return 0;
924}
925
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000926/* Bypass encapsulation if the destination is local */
927static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
928 struct vxlan_dev *dst_vxlan)
929{
930 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
931 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
932
933 skb->pkt_type = PACKET_HOST;
934 skb->encapsulation = 0;
935 skb->dev = dst_vxlan->dev;
936 __skb_pull(skb, skb_network_offset(skb));
937
938 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +0000939 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
940 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000941
942 u64_stats_update_begin(&tx_stats->syncp);
943 tx_stats->tx_packets++;
944 tx_stats->tx_bytes += skb->len;
945 u64_stats_update_end(&tx_stats->syncp);
946
947 if (netif_rx(skb) == NET_RX_SUCCESS) {
948 u64_stats_update_begin(&rx_stats->syncp);
949 rx_stats->rx_packets++;
950 rx_stats->rx_bytes += skb->len;
951 u64_stats_update_end(&rx_stats->syncp);
952 } else {
953 skb->dev->stats.rx_dropped++;
954 }
955}
956
David Stevens66817122013-03-15 04:35:51 +0000957static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
958 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +0000959{
960 struct vxlan_dev *vxlan = netdev_priv(dev);
961 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +0000962 const struct iphdr *old_iph;
963 struct iphdr *iph;
964 struct vxlanhdr *vxh;
965 struct udphdr *uh;
966 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +0000967 __be32 dst;
stephen hemminger73cf3312013-04-27 11:31:54 +0000968 __u16 src_port;
969 __be16 dst_port;
David Stevens66817122013-03-15 04:35:51 +0000970 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +0000971 __be16 df = 0;
972 __u8 tos, ttl;
stephen hemmingerd3428942012-10-01 12:32:35 +0000973
stephen hemminger73cf3312013-04-27 11:31:54 +0000974 dst_port = rdst->remote_port ? rdst->remote_port : htons(vxlan_port);
David Stevens66817122013-03-15 04:35:51 +0000975 vni = rdst->remote_vni;
976 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +0000977
978 if (!dst) {
979 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +0000980 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000981 vxlan_encap_bypass(skb, vxlan, vxlan);
David Stevense4f67ad2012-11-20 02:50:14 +0000982 return NETDEV_TX_OK;
983 }
stephen hemmingeref59feb2012-10-09 20:35:46 +0000984 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +0000985 }
stephen hemmingeref59feb2012-10-09 20:35:46 +0000986
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +0000987 if (!skb->encapsulation) {
988 skb_reset_inner_headers(skb);
989 skb->encapsulation = 1;
990 }
991
stephen hemmingerd3428942012-10-01 12:32:35 +0000992 /* Need space for new headers (invalidates iph ptr) */
993 if (skb_cow_head(skb, VXLAN_HEADROOM))
994 goto drop;
995
stephen hemmingerd3428942012-10-01 12:32:35 +0000996 old_iph = ip_hdr(skb);
997
stephen hemmingerd3428942012-10-01 12:32:35 +0000998 ttl = vxlan->ttl;
999 if (!ttl && IN_MULTICAST(ntohl(dst)))
1000 ttl = 1;
1001
1002 tos = vxlan->tos;
1003 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001004 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001005
stephen hemminger05f47d62012-10-09 20:35:50 +00001006 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001007
stephen hemmingerca78f182012-10-09 20:35:48 +00001008 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001009 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001010 fl4.flowi4_tos = RT_TOS(tos);
1011 fl4.daddr = dst;
1012 fl4.saddr = vxlan->saddr;
1013
1014 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001015 if (IS_ERR(rt)) {
1016 netdev_dbg(dev, "no route to %pI4\n", &dst);
1017 dev->stats.tx_carrier_errors++;
1018 goto tx_error;
1019 }
1020
1021 if (rt->dst.dev == dev) {
1022 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1023 ip_rt_put(rt);
1024 dev->stats.collisions++;
1025 goto tx_error;
1026 }
1027
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001028 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001029 if (rt->rt_flags & RTCF_LOCAL &&
1030 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001031 struct vxlan_dev *dst_vxlan;
1032
1033 ip_rt_put(rt);
1034 dst_vxlan = vxlan_find_vni(dev_net(dev), vni);
1035 if (!dst_vxlan)
1036 goto tx_error;
1037 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1038 return NETDEV_TX_OK;
1039 }
1040
stephen hemmingerd3428942012-10-01 12:32:35 +00001041 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1042 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1043 IPSKB_REROUTED);
1044 skb_dst_drop(skb);
1045 skb_dst_set(skb, &rt->dst);
1046
1047 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1048 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001049 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001050
1051 __skb_push(skb, sizeof(*uh));
1052 skb_reset_transport_header(skb);
1053 uh = udp_hdr(skb);
1054
stephen hemminger73cf3312013-04-27 11:31:54 +00001055 uh->dest = dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +00001056 uh->source = htons(src_port);
stephen hemmingerd3428942012-10-01 12:32:35 +00001057
1058 uh->len = htons(skb->len);
1059 uh->check = 0;
1060
1061 __skb_push(skb, sizeof(*iph));
1062 skb_reset_network_header(skb);
1063 iph = ip_hdr(skb);
1064 iph->version = 4;
1065 iph->ihl = sizeof(struct iphdr) >> 2;
1066 iph->frag_off = df;
1067 iph->protocol = IPPROTO_UDP;
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001068 iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
stephen hemmingerca78f182012-10-09 20:35:48 +00001069 iph->daddr = dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001070 iph->saddr = fl4.saddr;
1071 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar8dc98eb2013-02-22 07:30:40 +00001072 tunnel_ip_select_ident(skb, old_iph, &rt->dst);
stephen hemmingerd3428942012-10-01 12:32:35 +00001073
Zang MingJie88c4c062013-03-04 06:07:34 +00001074 nf_reset(skb);
1075
stephen hemminger1cad8712012-10-09 20:35:49 +00001076 vxlan_set_owner(dev, skb);
1077
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001078 if (handle_offloads(skb))
1079 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001080
Cong Wang6aed0c82013-03-09 16:38:39 +00001081 iptunnel_xmit(skb, dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001082 return NETDEV_TX_OK;
1083
1084drop:
1085 dev->stats.tx_dropped++;
1086 goto tx_free;
1087
1088tx_error:
1089 dev->stats.tx_errors++;
1090tx_free:
1091 dev_kfree_skb(skb);
1092 return NETDEV_TX_OK;
1093}
1094
David Stevens66817122013-03-15 04:35:51 +00001095/* Transmit local packets over Vxlan
1096 *
1097 * Outer IP header inherits ECN and DF from inner header.
1098 * Outer UDP destination is the VXLAN assigned port.
1099 * source port is based on hash of flow
1100 */
1101static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1102{
1103 struct vxlan_dev *vxlan = netdev_priv(dev);
1104 struct ethhdr *eth;
1105 bool did_rsc = false;
Atzm Watanabec7995c42013-04-16 02:50:52 +00001106 struct vxlan_rdst *rdst0, *rdst;
David Stevens66817122013-03-15 04:35:51 +00001107 struct vxlan_fdb *f;
1108 int rc1, rc;
1109
1110 skb_reset_mac_header(skb);
1111 eth = eth_hdr(skb);
1112
1113 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1114 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001115
1116 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001117 did_rsc = false;
1118
1119 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1120 ntohs(eth->h_proto) == ETH_P_IP) {
1121 did_rsc = route_shortcircuit(dev, skb);
1122 if (did_rsc)
1123 f = vxlan_find_mac(vxlan, eth->h_dest);
1124 }
1125
David Stevens66817122013-03-15 04:35:51 +00001126 if (f == NULL) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001127 rdst0 = &vxlan->default_dst;
David Stevens66817122013-03-15 04:35:51 +00001128
Atzm Watanabec7995c42013-04-16 02:50:52 +00001129 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
David Stevens66817122013-03-15 04:35:51 +00001130 (vxlan->flags & VXLAN_F_L2MISS) &&
1131 !is_multicast_ether_addr(eth->h_dest))
1132 vxlan_fdb_miss(vxlan, eth->h_dest);
1133 } else
1134 rdst0 = &f->remote;
1135
1136 rc = NETDEV_TX_OK;
1137
1138 /* if there are multiple destinations, send copies */
1139 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1140 struct sk_buff *skb1;
1141
1142 skb1 = skb_clone(skb, GFP_ATOMIC);
1143 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1144 if (rc == NETDEV_TX_OK)
1145 rc = rc1;
1146 }
1147
1148 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1149 if (rc == NETDEV_TX_OK)
1150 rc = rc1;
1151 return rc;
1152}
1153
stephen hemmingerd3428942012-10-01 12:32:35 +00001154/* Walk the forwarding table and purge stale entries */
1155static void vxlan_cleanup(unsigned long arg)
1156{
1157 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1158 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1159 unsigned int h;
1160
1161 if (!netif_running(vxlan->dev))
1162 return;
1163
1164 spin_lock_bh(&vxlan->hash_lock);
1165 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1166 struct hlist_node *p, *n;
1167 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1168 struct vxlan_fdb *f
1169 = container_of(p, struct vxlan_fdb, hlist);
1170 unsigned long timeout;
1171
stephen hemminger3c172862012-10-26 06:24:34 +00001172 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001173 continue;
1174
1175 timeout = f->used + vxlan->age_interval * HZ;
1176 if (time_before_eq(timeout, jiffies)) {
1177 netdev_dbg(vxlan->dev,
1178 "garbage collect %pM\n",
1179 f->eth_addr);
1180 f->state = NUD_STALE;
1181 vxlan_fdb_destroy(vxlan, f);
1182 } else if (time_before(timeout, next_timer))
1183 next_timer = timeout;
1184 }
1185 }
1186 spin_unlock_bh(&vxlan->hash_lock);
1187
1188 mod_timer(&vxlan->age_timer, next_timer);
1189}
1190
1191/* Setup stats when device is created */
1192static int vxlan_init(struct net_device *dev)
1193{
Pravin B Shelare8171042013-03-25 14:49:46 +00001194 dev->tstats = alloc_percpu(struct pcpu_tstats);
1195 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001196 return -ENOMEM;
1197
1198 return 0;
1199}
1200
1201/* Start ageing timer and join group when device is brought up */
1202static int vxlan_open(struct net_device *dev)
1203{
1204 struct vxlan_dev *vxlan = netdev_priv(dev);
1205 int err;
1206
Atzm Watanabec7995c42013-04-16 02:50:52 +00001207 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
stephen hemmingerd3428942012-10-01 12:32:35 +00001208 err = vxlan_join_group(dev);
1209 if (err)
1210 return err;
1211 }
1212
1213 if (vxlan->age_interval)
1214 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1215
1216 return 0;
1217}
1218
1219/* Purge the forwarding table */
1220static void vxlan_flush(struct vxlan_dev *vxlan)
1221{
1222 unsigned h;
1223
1224 spin_lock_bh(&vxlan->hash_lock);
1225 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1226 struct hlist_node *p, *n;
1227 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1228 struct vxlan_fdb *f
1229 = container_of(p, struct vxlan_fdb, hlist);
1230 vxlan_fdb_destroy(vxlan, f);
1231 }
1232 }
1233 spin_unlock_bh(&vxlan->hash_lock);
1234}
1235
1236/* Cleanup timer and forwarding table on shutdown */
1237static int vxlan_stop(struct net_device *dev)
1238{
1239 struct vxlan_dev *vxlan = netdev_priv(dev);
1240
Atzm Watanabec7995c42013-04-16 02:50:52 +00001241 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001242 vxlan_leave_group(dev);
1243
1244 del_timer_sync(&vxlan->age_timer);
1245
1246 vxlan_flush(vxlan);
1247
1248 return 0;
1249}
1250
stephen hemmingerd3428942012-10-01 12:32:35 +00001251/* Stub, nothing needs to be done. */
1252static void vxlan_set_multicast_list(struct net_device *dev)
1253{
1254}
1255
1256static const struct net_device_ops vxlan_netdev_ops = {
1257 .ndo_init = vxlan_init,
1258 .ndo_open = vxlan_open,
1259 .ndo_stop = vxlan_stop,
1260 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001261 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001262 .ndo_set_rx_mode = vxlan_set_multicast_list,
1263 .ndo_change_mtu = eth_change_mtu,
1264 .ndo_validate_addr = eth_validate_addr,
1265 .ndo_set_mac_address = eth_mac_addr,
1266 .ndo_fdb_add = vxlan_fdb_add,
1267 .ndo_fdb_del = vxlan_fdb_delete,
1268 .ndo_fdb_dump = vxlan_fdb_dump,
1269};
1270
1271/* Info for udev, that this is a virtual tunnel endpoint */
1272static struct device_type vxlan_type = {
1273 .name = "vxlan",
1274};
1275
1276static void vxlan_free(struct net_device *dev)
1277{
Pravin B Shelare8171042013-03-25 14:49:46 +00001278 free_percpu(dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001279 free_netdev(dev);
1280}
1281
1282/* Initialize the device structure. */
1283static void vxlan_setup(struct net_device *dev)
1284{
1285 struct vxlan_dev *vxlan = netdev_priv(dev);
1286 unsigned h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001287 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001288
1289 eth_hw_addr_random(dev);
1290 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001291 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001292
1293 dev->netdev_ops = &vxlan_netdev_ops;
1294 dev->destructor = vxlan_free;
1295 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1296
1297 dev->tx_queue_len = 0;
1298 dev->features |= NETIF_F_LLTX;
1299 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001300 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001301 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001302 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001303
1304 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001305 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001306 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001307 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001308
1309 spin_lock_init(&vxlan->hash_lock);
1310
1311 init_timer_deferrable(&vxlan->age_timer);
1312 vxlan->age_timer.function = vxlan_cleanup;
1313 vxlan->age_timer.data = (unsigned long) vxlan;
1314
stephen hemminger05f47d62012-10-09 20:35:50 +00001315 inet_get_local_port_range(&low, &high);
1316 vxlan->port_min = low;
1317 vxlan->port_max = high;
1318
stephen hemmingerd3428942012-10-01 12:32:35 +00001319 vxlan->dev = dev;
1320
1321 for (h = 0; h < FDB_HASH_SIZE; ++h)
1322 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1323}
1324
1325static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1326 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001327 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001328 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1329 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1330 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1331 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1332 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1333 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1334 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001335 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001336 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1337 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1338 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1339 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001340};
1341
1342static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1343{
1344 if (tb[IFLA_ADDRESS]) {
1345 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1346 pr_debug("invalid link address (not ethernet)\n");
1347 return -EINVAL;
1348 }
1349
1350 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1351 pr_debug("invalid all zero ethernet address\n");
1352 return -EADDRNOTAVAIL;
1353 }
1354 }
1355
1356 if (!data)
1357 return -EINVAL;
1358
1359 if (data[IFLA_VXLAN_ID]) {
1360 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1361 if (id >= VXLAN_VID_MASK)
1362 return -ERANGE;
1363 }
1364
stephen hemminger05f47d62012-10-09 20:35:50 +00001365 if (data[IFLA_VXLAN_PORT_RANGE]) {
1366 const struct ifla_vxlan_port_range *p
1367 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1368
1369 if (ntohs(p->high) < ntohs(p->low)) {
1370 pr_debug("port range %u .. %u not valid\n",
1371 ntohs(p->low), ntohs(p->high));
1372 return -EINVAL;
1373 }
1374 }
1375
stephen hemmingerd3428942012-10-01 12:32:35 +00001376 return 0;
1377}
1378
Yan Burman1b13c972013-01-29 23:43:07 +00001379static void vxlan_get_drvinfo(struct net_device *netdev,
1380 struct ethtool_drvinfo *drvinfo)
1381{
1382 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1383 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1384}
1385
1386static const struct ethtool_ops vxlan_ethtool_ops = {
1387 .get_drvinfo = vxlan_get_drvinfo,
1388 .get_link = ethtool_op_get_link,
1389};
1390
stephen hemmingerd3428942012-10-01 12:32:35 +00001391static int vxlan_newlink(struct net *net, struct net_device *dev,
1392 struct nlattr *tb[], struct nlattr *data[])
1393{
1394 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001395 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001396 __u32 vni;
1397 int err;
1398
1399 if (!data[IFLA_VXLAN_ID])
1400 return -EINVAL;
1401
1402 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1403 if (vxlan_find_vni(net, vni)) {
1404 pr_info("duplicate VNI %u\n", vni);
1405 return -EEXIST;
1406 }
Atzm Watanabec7995c42013-04-16 02:50:52 +00001407 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001408
stephen hemminger5d174dd2013-04-27 11:31:55 +00001409 if (data[IFLA_VXLAN_GROUP])
1410 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001411
1412 if (data[IFLA_VXLAN_LOCAL])
1413 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1414
stephen hemminger34e02aa2012-10-09 20:35:53 +00001415 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001416 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001417 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001418 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001419
stephen hemminger34e02aa2012-10-09 20:35:53 +00001420 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001421 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001422 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001423 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001424
1425 if (!tb[IFLA_MTU])
1426 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001427
1428 /* update header length based on lower device */
1429 dev->hard_header_len = lowerdev->hard_header_len +
1430 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001431 }
1432
1433 if (data[IFLA_VXLAN_TOS])
1434 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1435
Vincent Bernatafb97182012-10-30 10:27:16 +00001436 if (data[IFLA_VXLAN_TTL])
1437 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1438
stephen hemmingerd3428942012-10-01 12:32:35 +00001439 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001440 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001441
1442 if (data[IFLA_VXLAN_AGEING])
1443 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1444 else
1445 vxlan->age_interval = FDB_AGE_DEFAULT;
1446
David Stevense4f67ad2012-11-20 02:50:14 +00001447 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1448 vxlan->flags |= VXLAN_F_PROXY;
1449
1450 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1451 vxlan->flags |= VXLAN_F_RSC;
1452
1453 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1454 vxlan->flags |= VXLAN_F_L2MISS;
1455
1456 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1457 vxlan->flags |= VXLAN_F_L3MISS;
1458
stephen hemmingerd3428942012-10-01 12:32:35 +00001459 if (data[IFLA_VXLAN_LIMIT])
1460 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1461
stephen hemminger05f47d62012-10-09 20:35:50 +00001462 if (data[IFLA_VXLAN_PORT_RANGE]) {
1463 const struct ifla_vxlan_port_range *p
1464 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1465 vxlan->port_min = ntohs(p->low);
1466 vxlan->port_max = ntohs(p->high);
1467 }
1468
Yan Burman1b13c972013-01-29 23:43:07 +00001469 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1470
stephen hemmingerd3428942012-10-01 12:32:35 +00001471 err = register_netdevice(dev);
1472 if (!err)
Atzm Watanabec7995c42013-04-16 02:50:52 +00001473 hlist_add_head_rcu(&vxlan->hlist, vni_head(net, dst->remote_vni));
stephen hemmingerd3428942012-10-01 12:32:35 +00001474
1475 return err;
1476}
1477
1478static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1479{
1480 struct vxlan_dev *vxlan = netdev_priv(dev);
1481
1482 hlist_del_rcu(&vxlan->hlist);
1483
1484 unregister_netdevice_queue(dev, head);
1485}
1486
1487static size_t vxlan_get_size(const struct net_device *dev)
1488{
1489
1490 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001491 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001492 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1493 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1494 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1495 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1496 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001497 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1498 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1499 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1500 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001501 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1502 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001503 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemmingerd3428942012-10-01 12:32:35 +00001504 0;
1505}
1506
1507static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1508{
1509 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001510 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001511 struct ifla_vxlan_port_range ports = {
1512 .low = htons(vxlan->port_min),
1513 .high = htons(vxlan->port_max),
1514 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001515
Atzm Watanabec7995c42013-04-16 02:50:52 +00001516 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001517 goto nla_put_failure;
1518
stephen hemminger5d174dd2013-04-27 11:31:55 +00001519 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001520 goto nla_put_failure;
1521
Atzm Watanabec7995c42013-04-16 02:50:52 +00001522 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001523 goto nla_put_failure;
1524
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001525 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001526 goto nla_put_failure;
1527
1528 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1529 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001530 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1531 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1532 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1533 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1534 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1535 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1536 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1537 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1538 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001539 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1540 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
1541 goto nla_put_failure;
1542
stephen hemminger05f47d62012-10-09 20:35:50 +00001543 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1544 goto nla_put_failure;
1545
stephen hemmingerd3428942012-10-01 12:32:35 +00001546 return 0;
1547
1548nla_put_failure:
1549 return -EMSGSIZE;
1550}
1551
1552static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1553 .kind = "vxlan",
1554 .maxtype = IFLA_VXLAN_MAX,
1555 .policy = vxlan_policy,
1556 .priv_size = sizeof(struct vxlan_dev),
1557 .setup = vxlan_setup,
1558 .validate = vxlan_validate,
1559 .newlink = vxlan_newlink,
1560 .dellink = vxlan_dellink,
1561 .get_size = vxlan_get_size,
1562 .fill_info = vxlan_fill_info,
1563};
1564
1565static __net_init int vxlan_init_net(struct net *net)
1566{
1567 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1568 struct sock *sk;
1569 struct sockaddr_in vxlan_addr = {
1570 .sin_family = AF_INET,
1571 .sin_addr.s_addr = htonl(INADDR_ANY),
1572 };
1573 int rc;
1574 unsigned h;
1575
1576 /* Create UDP socket for encapsulation receive. */
1577 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
1578 if (rc < 0) {
1579 pr_debug("UDP socket create failed\n");
1580 return rc;
1581 }
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001582 /* Put in proper namespace */
1583 sk = vn->sock->sk;
1584 sk_change_net(sk, net);
stephen hemmingerd3428942012-10-01 12:32:35 +00001585
1586 vxlan_addr.sin_port = htons(vxlan_port);
1587
1588 rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
1589 sizeof(vxlan_addr));
1590 if (rc < 0) {
1591 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1592 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001593 sk_release_kernel(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001594 vn->sock = NULL;
1595 return rc;
1596 }
1597
1598 /* Disable multicast loopback */
stephen hemmingerd3428942012-10-01 12:32:35 +00001599 inet_sk(sk)->mc_loop = 0;
1600
1601 /* Mark socket as an encapsulation socket. */
1602 udp_sk(sk)->encap_type = 1;
1603 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1604 udp_encap_enable();
1605
1606 for (h = 0; h < VNI_HASH_SIZE; ++h)
1607 INIT_HLIST_HEAD(&vn->vni_list[h]);
1608
1609 return 0;
1610}
1611
1612static __net_exit void vxlan_exit_net(struct net *net)
1613{
1614 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001615 struct vxlan_dev *vxlan;
1616 unsigned h;
1617
1618 rtnl_lock();
1619 for (h = 0; h < VNI_HASH_SIZE; ++h)
1620 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist)
1621 dev_close(vxlan->dev);
1622 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001623
1624 if (vn->sock) {
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001625 sk_release_kernel(vn->sock->sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001626 vn->sock = NULL;
1627 }
1628}
1629
1630static struct pernet_operations vxlan_net_ops = {
1631 .init = vxlan_init_net,
1632 .exit = vxlan_exit_net,
1633 .id = &vxlan_net_id,
1634 .size = sizeof(struct vxlan_net),
1635};
1636
1637static int __init vxlan_init_module(void)
1638{
1639 int rc;
1640
1641 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1642
1643 rc = register_pernet_device(&vxlan_net_ops);
1644 if (rc)
1645 goto out1;
1646
1647 rc = rtnl_link_register(&vxlan_link_ops);
1648 if (rc)
1649 goto out2;
1650
1651 return 0;
1652
1653out2:
1654 unregister_pernet_device(&vxlan_net_ops);
1655out1:
1656 return rc;
1657}
1658module_init(vxlan_init_module);
1659
1660static void __exit vxlan_cleanup_module(void)
1661{
1662 rtnl_link_unregister(&vxlan_link_ops);
1663 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00001664 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001665}
1666module_exit(vxlan_cleanup_module);
1667
1668MODULE_LICENSE("GPL");
1669MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00001670MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00001671MODULE_ALIAS_RTNL_LINK("vxlan");