blob: a7fd9a089a35297df3b34bc405adb96caf1147fa [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
4 * Copyright (c) 2012 Vyatta Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * TODO
11 * - use IANA UDP port number (when defined)
12 * - IPv6 (not in RFC)
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/kernel.h>
18#include <linux/types.h>
19#include <linux/module.h>
20#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/skbuff.h>
23#include <linux/rculist.h>
24#include <linux/netdevice.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/udp.h>
28#include <linux/igmp.h>
29#include <linux/etherdevice.h>
30#include <linux/if_ether.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000031#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000032#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000033#include <net/arp.h>
34#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000035#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000036#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000037#include <net/icmp.h>
38#include <net/udp.h>
39#include <net/rtnetlink.h>
40#include <net/route.h>
41#include <net/dsfield.h>
42#include <net/inet_ecn.h>
43#include <net/net_namespace.h>
44#include <net/netns/generic.h>
45
46#define VXLAN_VERSION "0.1"
47
48#define VNI_HASH_BITS 10
49#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
50#define FDB_HASH_BITS 8
51#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
52#define FDB_AGE_DEFAULT 300 /* 5 min */
53#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
54
55#define VXLAN_N_VID (1u << 24)
56#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000057/* IP header + UDP + VXLAN + Ethernet header */
58#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
stephen hemmingerd3428942012-10-01 12:32:35 +000059
60#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
61
62/* VXLAN protocol header */
63struct vxlanhdr {
64 __be32 vx_flags;
65 __be32 vx_vni;
66};
67
68/* UDP port for VXLAN traffic. */
69static unsigned int vxlan_port __read_mostly = 8472;
70module_param_named(udp_port, vxlan_port, uint, 0444);
71MODULE_PARM_DESC(udp_port, "Destination UDP port");
72
73static bool log_ecn_error = true;
74module_param(log_ecn_error, bool, 0644);
75MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
76
77/* per-net private data for this module */
78static unsigned int vxlan_net_id;
79struct vxlan_net {
80 struct socket *sock; /* UDP encap socket */
81 struct hlist_head vni_list[VNI_HASH_SIZE];
82};
83
David Stevens66817122013-03-15 04:35:51 +000084struct vxlan_rdst {
85 struct rcu_head rcu;
86 __be32 remote_ip;
87 __be16 remote_port;
88 u32 remote_vni;
89 u32 remote_ifindex;
90 struct vxlan_rdst *remote_next;
91};
92
stephen hemmingerd3428942012-10-01 12:32:35 +000093/* Forwarding table entry */
94struct vxlan_fdb {
95 struct hlist_node hlist; /* linked list of entries */
96 struct rcu_head rcu;
97 unsigned long updated; /* jiffies */
98 unsigned long used;
David Stevens66817122013-03-15 04:35:51 +000099 struct vxlan_rdst remote;
stephen hemmingerd3428942012-10-01 12:32:35 +0000100 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000101 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000102 u8 eth_addr[ETH_ALEN];
103};
104
stephen hemmingerd3428942012-10-01 12:32:35 +0000105/* Pseudo network device */
106struct vxlan_dev {
107 struct hlist_node hlist;
108 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000109 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000110 __be32 saddr; /* source address */
stephen hemminger05f47d62012-10-09 20:35:50 +0000111 __u16 port_min; /* source port range */
112 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000113 __u8 tos; /* TOS override */
114 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000115 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000116
117 unsigned long age_interval;
118 struct timer_list age_timer;
119 spinlock_t hash_lock;
120 unsigned int addrcnt;
121 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000122
123 struct hlist_head fdb_head[FDB_HASH_SIZE];
124};
125
David Stevense4f67ad2012-11-20 02:50:14 +0000126#define VXLAN_F_LEARN 0x01
127#define VXLAN_F_PROXY 0x02
128#define VXLAN_F_RSC 0x04
129#define VXLAN_F_L2MISS 0x08
130#define VXLAN_F_L3MISS 0x10
131
stephen hemmingerd3428942012-10-01 12:32:35 +0000132/* salt for hash table */
133static u32 vxlan_salt __read_mostly;
134
135static inline struct hlist_head *vni_head(struct net *net, u32 id)
136{
137 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
138
139 return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
140}
141
142/* Look up VNI in a per net namespace table */
143static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
144{
145 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000146
Sasha Levinb67bfe02013-02-27 17:06:00 -0800147 hlist_for_each_entry_rcu(vxlan, vni_head(net, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000148 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000149 return vxlan;
150 }
151
152 return NULL;
153}
154
155/* Fill in neighbour message in skbuff. */
156static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
157 const struct vxlan_fdb *fdb,
David Stevens66817122013-03-15 04:35:51 +0000158 u32 portid, u32 seq, int type, unsigned int flags,
159 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000160{
161 unsigned long now = jiffies;
162 struct nda_cacheinfo ci;
163 struct nlmsghdr *nlh;
164 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000165 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000166
167 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
168 if (nlh == NULL)
169 return -EMSGSIZE;
170
171 ndm = nlmsg_data(nlh);
172 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000173
174 send_eth = send_ip = true;
175
176 if (type == RTM_GETNEIGH) {
177 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000178 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000179 send_eth = !is_zero_ether_addr(fdb->eth_addr);
180 } else
181 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000182 ndm->ndm_state = fdb->state;
183 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000184 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000185 ndm->ndm_type = NDA_DST;
186
David Stevense4f67ad2012-11-20 02:50:14 +0000187 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000188 goto nla_put_failure;
189
David Stevens66817122013-03-15 04:35:51 +0000190 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
191 goto nla_put_failure;
192
193 if (rdst->remote_port && rdst->remote_port != vxlan_port &&
194 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
195 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000196 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
David Stevens66817122013-03-15 04:35:51 +0000197 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
198 goto nla_put_failure;
199 if (rdst->remote_ifindex &&
200 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000201 goto nla_put_failure;
202
203 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
204 ci.ndm_confirmed = 0;
205 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
206 ci.ndm_refcnt = 0;
207
208 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
209 goto nla_put_failure;
210
211 return nlmsg_end(skb, nlh);
212
213nla_put_failure:
214 nlmsg_cancel(skb, nlh);
215 return -EMSGSIZE;
216}
217
218static inline size_t vxlan_nlmsg_size(void)
219{
220 return NLMSG_ALIGN(sizeof(struct ndmsg))
221 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
222 + nla_total_size(sizeof(__be32)) /* NDA_DST */
David Stevens66817122013-03-15 04:35:51 +0000223 + nla_total_size(sizeof(__be32)) /* NDA_PORT */
224 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
225 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000226 + nla_total_size(sizeof(struct nda_cacheinfo));
227}
228
229static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
230 const struct vxlan_fdb *fdb, int type)
231{
232 struct net *net = dev_net(vxlan->dev);
233 struct sk_buff *skb;
234 int err = -ENOBUFS;
235
236 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
237 if (skb == NULL)
238 goto errout;
239
David Stevens66817122013-03-15 04:35:51 +0000240 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
stephen hemmingerd3428942012-10-01 12:32:35 +0000241 if (err < 0) {
242 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
243 WARN_ON(err == -EMSGSIZE);
244 kfree_skb(skb);
245 goto errout;
246 }
247
248 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
249 return;
250errout:
251 if (err < 0)
252 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
253}
254
David Stevense4f67ad2012-11-20 02:50:14 +0000255static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
256{
257 struct vxlan_dev *vxlan = netdev_priv(dev);
258 struct vxlan_fdb f;
259
260 memset(&f, 0, sizeof f);
261 f.state = NUD_STALE;
David Stevens66817122013-03-15 04:35:51 +0000262 f.remote.remote_ip = ipa; /* goes to NDA_DST */
263 f.remote.remote_vni = VXLAN_N_VID;
David Stevense4f67ad2012-11-20 02:50:14 +0000264
265 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
266}
267
268static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
269{
270 struct vxlan_fdb f;
271
272 memset(&f, 0, sizeof f);
273 f.state = NUD_STALE;
274 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
275
276 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
277}
278
stephen hemmingerd3428942012-10-01 12:32:35 +0000279/* Hash Ethernet address */
280static u32 eth_hash(const unsigned char *addr)
281{
282 u64 value = get_unaligned((u64 *)addr);
283
284 /* only want 6 bytes */
285#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000286 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000287#else
288 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000289#endif
290 return hash_64(value, FDB_HASH_BITS);
291}
292
293/* Hash chain to use given mac address */
294static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
295 const u8 *mac)
296{
297 return &vxlan->fdb_head[eth_hash(mac)];
298}
299
300/* Look up Ethernet address in forwarding table */
301static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
302 const u8 *mac)
303
304{
305 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
306 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000307
Sasha Levinb67bfe02013-02-27 17:06:00 -0800308 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000309 if (compare_ether_addr(mac, f->eth_addr) == 0)
310 return f;
311 }
312
313 return NULL;
314}
315
David Stevens66817122013-03-15 04:35:51 +0000316/* Add/update destinations for multicast */
317static int vxlan_fdb_append(struct vxlan_fdb *f,
318 __be32 ip, __u32 port, __u32 vni, __u32 ifindex)
319{
320 struct vxlan_rdst *rd_prev, *rd;
321
322 rd_prev = NULL;
323 for (rd = &f->remote; rd; rd = rd->remote_next) {
324 if (rd->remote_ip == ip &&
325 rd->remote_port == port &&
326 rd->remote_vni == vni &&
327 rd->remote_ifindex == ifindex)
328 return 0;
329 rd_prev = rd;
330 }
331 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
332 if (rd == NULL)
333 return -ENOBUFS;
334 rd->remote_ip = ip;
335 rd->remote_port = port;
336 rd->remote_vni = vni;
337 rd->remote_ifindex = ifindex;
338 rd->remote_next = NULL;
339 rd_prev->remote_next = rd;
340 return 1;
341}
342
stephen hemmingerd3428942012-10-01 12:32:35 +0000343/* Add new entry to forwarding table -- assumes lock held */
344static int vxlan_fdb_create(struct vxlan_dev *vxlan,
345 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000346 __u16 state, __u16 flags,
David Stevensae884082013-04-19 00:36:26 +0000347 __u32 port, __u32 vni, __u32 ifindex,
348 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000349{
350 struct vxlan_fdb *f;
351 int notify = 0;
352
353 f = vxlan_find_mac(vxlan, mac);
354 if (f) {
355 if (flags & NLM_F_EXCL) {
356 netdev_dbg(vxlan->dev,
357 "lost race to create %pM\n", mac);
358 return -EEXIST;
359 }
360 if (f->state != state) {
361 f->state = state;
362 f->updated = jiffies;
363 notify = 1;
364 }
David Stevensae884082013-04-19 00:36:26 +0000365 if (f->flags != ndm_flags) {
366 f->flags = ndm_flags;
367 f->updated = jiffies;
368 notify = 1;
369 }
David Stevens66817122013-03-15 04:35:51 +0000370 if ((flags & NLM_F_APPEND) &&
371 is_multicast_ether_addr(f->eth_addr)) {
372 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
373
374 if (rc < 0)
375 return rc;
376 notify |= rc;
377 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000378 } else {
379 if (!(flags & NLM_F_CREATE))
380 return -ENOENT;
381
382 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
383 return -ENOSPC;
384
385 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
386 f = kmalloc(sizeof(*f), GFP_ATOMIC);
387 if (!f)
388 return -ENOMEM;
389
390 notify = 1;
David Stevens66817122013-03-15 04:35:51 +0000391 f->remote.remote_ip = ip;
392 f->remote.remote_port = port;
393 f->remote.remote_vni = vni;
394 f->remote.remote_ifindex = ifindex;
395 f->remote.remote_next = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000396 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000397 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000398 f->updated = f->used = jiffies;
399 memcpy(f->eth_addr, mac, ETH_ALEN);
400
401 ++vxlan->addrcnt;
402 hlist_add_head_rcu(&f->hlist,
403 vxlan_fdb_head(vxlan, mac));
404 }
405
406 if (notify)
407 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
408
409 return 0;
410}
411
Wei Yongjun6706c822013-04-11 19:00:35 +0000412static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000413{
414 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
415
416 while (f->remote.remote_next) {
417 struct vxlan_rdst *rd = f->remote.remote_next;
418
419 f->remote.remote_next = rd->remote_next;
420 kfree(rd);
421 }
422 kfree(f);
423}
424
stephen hemmingerd3428942012-10-01 12:32:35 +0000425static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
426{
427 netdev_dbg(vxlan->dev,
428 "delete %pM\n", f->eth_addr);
429
430 --vxlan->addrcnt;
431 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
432
433 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000434 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000435}
436
437/* Add static entry (via netlink) */
438static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
439 struct net_device *dev,
440 const unsigned char *addr, u16 flags)
441{
442 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens66817122013-03-15 04:35:51 +0000443 struct net *net = dev_net(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000444 __be32 ip;
David Stevens66817122013-03-15 04:35:51 +0000445 u32 port, vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000446 int err;
447
448 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
449 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
450 ndm->ndm_state);
451 return -EINVAL;
452 }
453
454 if (tb[NDA_DST] == NULL)
455 return -EINVAL;
456
457 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
458 return -EAFNOSUPPORT;
459
460 ip = nla_get_be32(tb[NDA_DST]);
461
David Stevens66817122013-03-15 04:35:51 +0000462 if (tb[NDA_PORT]) {
463 if (nla_len(tb[NDA_PORT]) != sizeof(u32))
464 return -EINVAL;
465 port = nla_get_u32(tb[NDA_PORT]);
466 } else
467 port = vxlan_port;
468
469 if (tb[NDA_VNI]) {
470 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
471 return -EINVAL;
472 vni = nla_get_u32(tb[NDA_VNI]);
473 } else
Atzm Watanabec7995c42013-04-16 02:50:52 +0000474 vni = vxlan->default_dst.remote_vni;
David Stevens66817122013-03-15 04:35:51 +0000475
476 if (tb[NDA_IFINDEX]) {
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000477 struct net_device *tdev;
David Stevens66817122013-03-15 04:35:51 +0000478
479 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
480 return -EINVAL;
481 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000482 tdev = dev_get_by_index(net, ifindex);
483 if (!tdev)
David Stevens66817122013-03-15 04:35:51 +0000484 return -EADDRNOTAVAIL;
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000485 dev_put(tdev);
David Stevens66817122013-03-15 04:35:51 +0000486 } else
487 ifindex = 0;
488
stephen hemmingerd3428942012-10-01 12:32:35 +0000489 spin_lock_bh(&vxlan->hash_lock);
David Stevens66817122013-03-15 04:35:51 +0000490 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags, port,
David Stevensae884082013-04-19 00:36:26 +0000491 vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000492 spin_unlock_bh(&vxlan->hash_lock);
493
494 return err;
495}
496
497/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000498static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
499 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000500 const unsigned char *addr)
501{
502 struct vxlan_dev *vxlan = netdev_priv(dev);
503 struct vxlan_fdb *f;
504 int err = -ENOENT;
505
506 spin_lock_bh(&vxlan->hash_lock);
507 f = vxlan_find_mac(vxlan, addr);
508 if (f) {
509 vxlan_fdb_destroy(vxlan, f);
510 err = 0;
511 }
512 spin_unlock_bh(&vxlan->hash_lock);
513
514 return err;
515}
516
517/* Dump forwarding table */
518static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
519 struct net_device *dev, int idx)
520{
521 struct vxlan_dev *vxlan = netdev_priv(dev);
522 unsigned int h;
523
524 for (h = 0; h < FDB_HASH_SIZE; ++h) {
525 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000526 int err;
527
Sasha Levinb67bfe02013-02-27 17:06:00 -0800528 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000529 struct vxlan_rdst *rd;
530 for (rd = &f->remote; rd; rd = rd->remote_next) {
531 if (idx < cb->args[0])
532 goto skip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000533
David Stevens66817122013-03-15 04:35:51 +0000534 err = vxlan_fdb_info(skb, vxlan, f,
535 NETLINK_CB(cb->skb).portid,
536 cb->nlh->nlmsg_seq,
537 RTM_NEWNEIGH,
538 NLM_F_MULTI, rd);
539 if (err < 0)
540 break;
stephen hemmingerd3428942012-10-01 12:32:35 +0000541skip:
David Stevens66817122013-03-15 04:35:51 +0000542 ++idx;
543 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000544 }
545 }
546
547 return idx;
548}
549
550/* Watch incoming packets to learn mapping between Ethernet address
551 * and Tunnel endpoint.
552 */
553static void vxlan_snoop(struct net_device *dev,
554 __be32 src_ip, const u8 *src_mac)
555{
556 struct vxlan_dev *vxlan = netdev_priv(dev);
557 struct vxlan_fdb *f;
558 int err;
559
560 f = vxlan_find_mac(vxlan, src_mac);
561 if (likely(f)) {
562 f->used = jiffies;
David Stevens66817122013-03-15 04:35:51 +0000563 if (likely(f->remote.remote_ip == src_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +0000564 return;
565
566 if (net_ratelimit())
567 netdev_info(dev,
568 "%pM migrated from %pI4 to %pI4\n",
David Stevens66817122013-03-15 04:35:51 +0000569 src_mac, &f->remote.remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000570
David Stevens66817122013-03-15 04:35:51 +0000571 f->remote.remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000572 f->updated = jiffies;
573 } else {
574 /* learned new entry */
575 spin_lock(&vxlan->hash_lock);
576 err = vxlan_fdb_create(vxlan, src_mac, src_ip,
577 NUD_REACHABLE,
David Stevens66817122013-03-15 04:35:51 +0000578 NLM_F_EXCL|NLM_F_CREATE,
David Stevensae884082013-04-19 00:36:26 +0000579 vxlan_port,
580 vxlan->default_dst.remote_vni,
581 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000582 spin_unlock(&vxlan->hash_lock);
583 }
584}
585
586
587/* See if multicast group is already in use by other ID */
588static bool vxlan_group_used(struct vxlan_net *vn,
589 const struct vxlan_dev *this)
590{
591 const struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000592 unsigned h;
593
594 for (h = 0; h < VNI_HASH_SIZE; ++h)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800595 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000596 if (vxlan == this)
597 continue;
598
599 if (!netif_running(vxlan->dev))
600 continue;
601
Atzm Watanabec7995c42013-04-16 02:50:52 +0000602 if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000603 return true;
604 }
605
606 return false;
607}
608
609/* kernel equivalent to IP_ADD_MEMBERSHIP */
610static int vxlan_join_group(struct net_device *dev)
611{
612 struct vxlan_dev *vxlan = netdev_priv(dev);
613 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
614 struct sock *sk = vn->sock->sk;
615 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000616 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
617 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000618 };
619 int err;
620
621 /* Already a member of group */
622 if (vxlan_group_used(vn, vxlan))
623 return 0;
624
625 /* Need to drop RTNL to call multicast join */
626 rtnl_unlock();
627 lock_sock(sk);
628 err = ip_mc_join_group(sk, &mreq);
629 release_sock(sk);
630 rtnl_lock();
631
632 return err;
633}
634
635
636/* kernel equivalent to IP_DROP_MEMBERSHIP */
637static int vxlan_leave_group(struct net_device *dev)
638{
639 struct vxlan_dev *vxlan = netdev_priv(dev);
640 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
641 int err = 0;
642 struct sock *sk = vn->sock->sk;
643 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000644 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
645 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000646 };
647
648 /* Only leave group when last vxlan is done. */
649 if (vxlan_group_used(vn, vxlan))
650 return 0;
651
652 /* Need to drop RTNL to call multicast leave */
653 rtnl_unlock();
654 lock_sock(sk);
655 err = ip_mc_leave_group(sk, &mreq);
656 release_sock(sk);
657 rtnl_lock();
658
659 return err;
660}
661
662/* Callback from net/ipv4/udp.c to receive packets */
663static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
664{
665 struct iphdr *oip;
666 struct vxlanhdr *vxh;
667 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000668 struct pcpu_tstats *stats;
stephen hemmingerd3428942012-10-01 12:32:35 +0000669 __u32 vni;
670 int err;
671
672 /* pop off outer UDP header */
673 __skb_pull(skb, sizeof(struct udphdr));
674
675 /* Need Vxlan and inner Ethernet header to be present */
676 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
677 goto error;
678
679 /* Drop packets with reserved bits set */
680 vxh = (struct vxlanhdr *) skb->data;
681 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
682 (vxh->vx_vni & htonl(0xff))) {
683 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
684 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
685 goto error;
686 }
687
688 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000689
690 /* Is this VNI defined? */
691 vni = ntohl(vxh->vx_vni) >> 8;
692 vxlan = vxlan_find_vni(sock_net(sk), vni);
693 if (!vxlan) {
694 netdev_dbg(skb->dev, "unknown vni %d\n", vni);
695 goto drop;
696 }
697
698 if (!pskb_may_pull(skb, ETH_HLEN)) {
699 vxlan->dev->stats.rx_length_errors++;
700 vxlan->dev->stats.rx_errors++;
701 goto drop;
702 }
703
David Stevense4f67ad2012-11-20 02:50:14 +0000704 skb_reset_mac_header(skb);
705
stephen hemmingerd3428942012-10-01 12:32:35 +0000706 /* Re-examine inner Ethernet packet */
707 oip = ip_hdr(skb);
708 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000709
710 /* Ignore packet loops (and multicast echo) */
711 if (compare_ether_addr(eth_hdr(skb)->h_source,
712 vxlan->dev->dev_addr) == 0)
713 goto drop;
714
David Stevense4f67ad2012-11-20 02:50:14 +0000715 if (vxlan->flags & VXLAN_F_LEARN)
stephen hemmingerd3428942012-10-01 12:32:35 +0000716 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
717
718 __skb_tunnel_rx(skb, vxlan->dev);
719 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000720
721 /* If the NIC driver gave us an encapsulated packet with
722 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
723 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
724 * for us. Otherwise force the upper layers to verify it.
725 */
726 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
727 !(vxlan->dev->features & NETIF_F_RXCSUM))
728 skb->ip_summed = CHECKSUM_NONE;
729
730 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000731
732 err = IP_ECN_decapsulate(oip, skb);
733 if (unlikely(err)) {
734 if (log_ecn_error)
735 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
736 &oip->saddr, oip->tos);
737 if (err > 1) {
738 ++vxlan->dev->stats.rx_frame_errors;
739 ++vxlan->dev->stats.rx_errors;
740 goto drop;
741 }
742 }
743
Pravin B Shelare8171042013-03-25 14:49:46 +0000744 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000745 u64_stats_update_begin(&stats->syncp);
746 stats->rx_packets++;
747 stats->rx_bytes += skb->len;
748 u64_stats_update_end(&stats->syncp);
749
750 netif_rx(skb);
751
752 return 0;
753error:
754 /* Put UDP header back */
755 __skb_push(skb, sizeof(struct udphdr));
756
757 return 1;
758drop:
759 /* Consume bad packet */
760 kfree_skb(skb);
761 return 0;
762}
763
David Stevense4f67ad2012-11-20 02:50:14 +0000764static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
765{
766 struct vxlan_dev *vxlan = netdev_priv(dev);
767 struct arphdr *parp;
768 u8 *arpptr, *sha;
769 __be32 sip, tip;
770 struct neighbour *n;
771
772 if (dev->flags & IFF_NOARP)
773 goto out;
774
775 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
776 dev->stats.tx_dropped++;
777 goto out;
778 }
779 parp = arp_hdr(skb);
780
781 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
782 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
783 parp->ar_pro != htons(ETH_P_IP) ||
784 parp->ar_op != htons(ARPOP_REQUEST) ||
785 parp->ar_hln != dev->addr_len ||
786 parp->ar_pln != 4)
787 goto out;
788 arpptr = (u8 *)parp + sizeof(struct arphdr);
789 sha = arpptr;
790 arpptr += dev->addr_len; /* sha */
791 memcpy(&sip, arpptr, sizeof(sip));
792 arpptr += sizeof(sip);
793 arpptr += dev->addr_len; /* tha */
794 memcpy(&tip, arpptr, sizeof(tip));
795
796 if (ipv4_is_loopback(tip) ||
797 ipv4_is_multicast(tip))
798 goto out;
799
800 n = neigh_lookup(&arp_tbl, &tip, dev);
801
802 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000803 struct vxlan_fdb *f;
804 struct sk_buff *reply;
805
806 if (!(n->nud_state & NUD_CONNECTED)) {
807 neigh_release(n);
808 goto out;
809 }
810
811 f = vxlan_find_mac(vxlan, n->ha);
David Stevens66817122013-03-15 04:35:51 +0000812 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000813 /* bridge-local neighbor */
814 neigh_release(n);
815 goto out;
816 }
817
818 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
819 n->ha, sha);
820
821 neigh_release(n);
822
823 skb_reset_mac_header(reply);
824 __skb_pull(reply, skb_network_offset(reply));
825 reply->ip_summed = CHECKSUM_UNNECESSARY;
826 reply->pkt_type = PACKET_HOST;
827
828 if (netif_rx_ni(reply) == NET_RX_DROP)
829 dev->stats.rx_dropped++;
830 } else if (vxlan->flags & VXLAN_F_L3MISS)
831 vxlan_ip_miss(dev, tip);
832out:
833 consume_skb(skb);
834 return NETDEV_TX_OK;
835}
836
837static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
838{
839 struct vxlan_dev *vxlan = netdev_priv(dev);
840 struct neighbour *n;
841 struct iphdr *pip;
842
843 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
844 return false;
845
846 n = NULL;
847 switch (ntohs(eth_hdr(skb)->h_proto)) {
848 case ETH_P_IP:
849 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
850 return false;
851 pip = ip_hdr(skb);
852 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
853 break;
854 default:
855 return false;
856 }
857
858 if (n) {
859 bool diff;
860
861 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
862 if (diff) {
863 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
864 dev->addr_len);
865 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
866 }
867 neigh_release(n);
868 return diff;
869 } else if (vxlan->flags & VXLAN_F_L3MISS)
870 vxlan_ip_miss(dev, pip->daddr);
871 return false;
872}
873
stephen hemminger1cad8712012-10-09 20:35:49 +0000874static void vxlan_sock_free(struct sk_buff *skb)
875{
876 sock_put(skb->sk);
877}
878
879/* On transmit, associate with the tunnel socket */
880static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
881{
882 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
883 struct sock *sk = vn->sock->sk;
884
885 skb_orphan(skb);
886 sock_hold(sk);
887 skb->sk = sk;
888 skb->destructor = vxlan_sock_free;
889}
890
stephen hemminger05f47d62012-10-09 20:35:50 +0000891/* Compute source port for outgoing packet
892 * first choice to use L4 flow hash since it will spread
893 * better and maybe available from hardware
894 * secondary choice is to use jhash on the Ethernet header
895 */
896static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
897{
898 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
899 u32 hash;
900
901 hash = skb_get_rxhash(skb);
902 if (!hash)
903 hash = jhash(skb->data, 2 * ETH_ALEN,
904 (__force u32) skb->protocol);
905
906 return (((u64) hash * range) >> 32) + vxlan->port_min;
907}
908
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000909static int handle_offloads(struct sk_buff *skb)
910{
911 if (skb_is_gso(skb)) {
912 int err = skb_unclone(skb, GFP_ATOMIC);
913 if (unlikely(err))
914 return err;
915
916 skb_shinfo(skb)->gso_type |= (SKB_GSO_UDP_TUNNEL | SKB_GSO_UDP);
917 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
918 skb->ip_summed = CHECKSUM_NONE;
919
920 return 0;
921}
922
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000923/* Bypass encapsulation if the destination is local */
924static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
925 struct vxlan_dev *dst_vxlan)
926{
927 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
928 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
929
930 skb->pkt_type = PACKET_HOST;
931 skb->encapsulation = 0;
932 skb->dev = dst_vxlan->dev;
933 __skb_pull(skb, skb_network_offset(skb));
934
935 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +0000936 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
937 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000938
939 u64_stats_update_begin(&tx_stats->syncp);
940 tx_stats->tx_packets++;
941 tx_stats->tx_bytes += skb->len;
942 u64_stats_update_end(&tx_stats->syncp);
943
944 if (netif_rx(skb) == NET_RX_SUCCESS) {
945 u64_stats_update_begin(&rx_stats->syncp);
946 rx_stats->rx_packets++;
947 rx_stats->rx_bytes += skb->len;
948 u64_stats_update_end(&rx_stats->syncp);
949 } else {
950 skb->dev->stats.rx_dropped++;
951 }
952}
953
David Stevens66817122013-03-15 04:35:51 +0000954static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
955 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +0000956{
957 struct vxlan_dev *vxlan = netdev_priv(dev);
958 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +0000959 const struct iphdr *old_iph;
960 struct iphdr *iph;
961 struct vxlanhdr *vxh;
962 struct udphdr *uh;
963 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +0000964 __be32 dst;
David Stevens66817122013-03-15 04:35:51 +0000965 __u16 src_port, dst_port;
966 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +0000967 __be16 df = 0;
968 __u8 tos, ttl;
stephen hemmingerd3428942012-10-01 12:32:35 +0000969
David Stevens66817122013-03-15 04:35:51 +0000970 dst_port = rdst->remote_port ? rdst->remote_port : vxlan_port;
971 vni = rdst->remote_vni;
972 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +0000973
974 if (!dst) {
975 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +0000976 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000977 vxlan_encap_bypass(skb, vxlan, vxlan);
David Stevense4f67ad2012-11-20 02:50:14 +0000978 return NETDEV_TX_OK;
979 }
stephen hemmingeref59feb2012-10-09 20:35:46 +0000980 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +0000981 }
stephen hemmingeref59feb2012-10-09 20:35:46 +0000982
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +0000983 if (!skb->encapsulation) {
984 skb_reset_inner_headers(skb);
985 skb->encapsulation = 1;
986 }
987
stephen hemmingerd3428942012-10-01 12:32:35 +0000988 /* Need space for new headers (invalidates iph ptr) */
989 if (skb_cow_head(skb, VXLAN_HEADROOM))
990 goto drop;
991
stephen hemmingerd3428942012-10-01 12:32:35 +0000992 old_iph = ip_hdr(skb);
993
stephen hemmingerd3428942012-10-01 12:32:35 +0000994 ttl = vxlan->ttl;
995 if (!ttl && IN_MULTICAST(ntohl(dst)))
996 ttl = 1;
997
998 tos = vxlan->tos;
999 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001000 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001001
stephen hemminger05f47d62012-10-09 20:35:50 +00001002 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001003
stephen hemmingerca78f182012-10-09 20:35:48 +00001004 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001005 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001006 fl4.flowi4_tos = RT_TOS(tos);
1007 fl4.daddr = dst;
1008 fl4.saddr = vxlan->saddr;
1009
1010 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001011 if (IS_ERR(rt)) {
1012 netdev_dbg(dev, "no route to %pI4\n", &dst);
1013 dev->stats.tx_carrier_errors++;
1014 goto tx_error;
1015 }
1016
1017 if (rt->dst.dev == dev) {
1018 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1019 ip_rt_put(rt);
1020 dev->stats.collisions++;
1021 goto tx_error;
1022 }
1023
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001024 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001025 if (rt->rt_flags & RTCF_LOCAL &&
1026 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001027 struct vxlan_dev *dst_vxlan;
1028
1029 ip_rt_put(rt);
1030 dst_vxlan = vxlan_find_vni(dev_net(dev), vni);
1031 if (!dst_vxlan)
1032 goto tx_error;
1033 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1034 return NETDEV_TX_OK;
1035 }
1036
stephen hemmingerd3428942012-10-01 12:32:35 +00001037 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1038 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1039 IPSKB_REROUTED);
1040 skb_dst_drop(skb);
1041 skb_dst_set(skb, &rt->dst);
1042
1043 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1044 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001045 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001046
1047 __skb_push(skb, sizeof(*uh));
1048 skb_reset_transport_header(skb);
1049 uh = udp_hdr(skb);
1050
David Stevens66817122013-03-15 04:35:51 +00001051 uh->dest = htons(dst_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001052 uh->source = htons(src_port);
stephen hemmingerd3428942012-10-01 12:32:35 +00001053
1054 uh->len = htons(skb->len);
1055 uh->check = 0;
1056
1057 __skb_push(skb, sizeof(*iph));
1058 skb_reset_network_header(skb);
1059 iph = ip_hdr(skb);
1060 iph->version = 4;
1061 iph->ihl = sizeof(struct iphdr) >> 2;
1062 iph->frag_off = df;
1063 iph->protocol = IPPROTO_UDP;
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001064 iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
stephen hemmingerca78f182012-10-09 20:35:48 +00001065 iph->daddr = dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001066 iph->saddr = fl4.saddr;
1067 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar8dc98eb2013-02-22 07:30:40 +00001068 tunnel_ip_select_ident(skb, old_iph, &rt->dst);
stephen hemmingerd3428942012-10-01 12:32:35 +00001069
Zang MingJie88c4c062013-03-04 06:07:34 +00001070 nf_reset(skb);
1071
stephen hemminger1cad8712012-10-09 20:35:49 +00001072 vxlan_set_owner(dev, skb);
1073
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001074 if (handle_offloads(skb))
1075 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001076
Cong Wang6aed0c82013-03-09 16:38:39 +00001077 iptunnel_xmit(skb, dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001078 return NETDEV_TX_OK;
1079
1080drop:
1081 dev->stats.tx_dropped++;
1082 goto tx_free;
1083
1084tx_error:
1085 dev->stats.tx_errors++;
1086tx_free:
1087 dev_kfree_skb(skb);
1088 return NETDEV_TX_OK;
1089}
1090
David Stevens66817122013-03-15 04:35:51 +00001091/* Transmit local packets over Vxlan
1092 *
1093 * Outer IP header inherits ECN and DF from inner header.
1094 * Outer UDP destination is the VXLAN assigned port.
1095 * source port is based on hash of flow
1096 */
1097static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1098{
1099 struct vxlan_dev *vxlan = netdev_priv(dev);
1100 struct ethhdr *eth;
1101 bool did_rsc = false;
Atzm Watanabec7995c42013-04-16 02:50:52 +00001102 struct vxlan_rdst *rdst0, *rdst;
David Stevens66817122013-03-15 04:35:51 +00001103 struct vxlan_fdb *f;
1104 int rc1, rc;
1105
1106 skb_reset_mac_header(skb);
1107 eth = eth_hdr(skb);
1108
1109 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1110 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001111
1112 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001113 did_rsc = false;
1114
1115 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1116 ntohs(eth->h_proto) == ETH_P_IP) {
1117 did_rsc = route_shortcircuit(dev, skb);
1118 if (did_rsc)
1119 f = vxlan_find_mac(vxlan, eth->h_dest);
1120 }
1121
David Stevens66817122013-03-15 04:35:51 +00001122 if (f == NULL) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001123 rdst0 = &vxlan->default_dst;
David Stevens66817122013-03-15 04:35:51 +00001124
Atzm Watanabec7995c42013-04-16 02:50:52 +00001125 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
David Stevens66817122013-03-15 04:35:51 +00001126 (vxlan->flags & VXLAN_F_L2MISS) &&
1127 !is_multicast_ether_addr(eth->h_dest))
1128 vxlan_fdb_miss(vxlan, eth->h_dest);
1129 } else
1130 rdst0 = &f->remote;
1131
1132 rc = NETDEV_TX_OK;
1133
1134 /* if there are multiple destinations, send copies */
1135 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1136 struct sk_buff *skb1;
1137
1138 skb1 = skb_clone(skb, GFP_ATOMIC);
1139 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1140 if (rc == NETDEV_TX_OK)
1141 rc = rc1;
1142 }
1143
1144 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1145 if (rc == NETDEV_TX_OK)
1146 rc = rc1;
1147 return rc;
1148}
1149
stephen hemmingerd3428942012-10-01 12:32:35 +00001150/* Walk the forwarding table and purge stale entries */
1151static void vxlan_cleanup(unsigned long arg)
1152{
1153 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1154 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1155 unsigned int h;
1156
1157 if (!netif_running(vxlan->dev))
1158 return;
1159
1160 spin_lock_bh(&vxlan->hash_lock);
1161 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1162 struct hlist_node *p, *n;
1163 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1164 struct vxlan_fdb *f
1165 = container_of(p, struct vxlan_fdb, hlist);
1166 unsigned long timeout;
1167
stephen hemminger3c172862012-10-26 06:24:34 +00001168 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001169 continue;
1170
1171 timeout = f->used + vxlan->age_interval * HZ;
1172 if (time_before_eq(timeout, jiffies)) {
1173 netdev_dbg(vxlan->dev,
1174 "garbage collect %pM\n",
1175 f->eth_addr);
1176 f->state = NUD_STALE;
1177 vxlan_fdb_destroy(vxlan, f);
1178 } else if (time_before(timeout, next_timer))
1179 next_timer = timeout;
1180 }
1181 }
1182 spin_unlock_bh(&vxlan->hash_lock);
1183
1184 mod_timer(&vxlan->age_timer, next_timer);
1185}
1186
1187/* Setup stats when device is created */
1188static int vxlan_init(struct net_device *dev)
1189{
Pravin B Shelare8171042013-03-25 14:49:46 +00001190 dev->tstats = alloc_percpu(struct pcpu_tstats);
1191 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001192 return -ENOMEM;
1193
1194 return 0;
1195}
1196
1197/* Start ageing timer and join group when device is brought up */
1198static int vxlan_open(struct net_device *dev)
1199{
1200 struct vxlan_dev *vxlan = netdev_priv(dev);
1201 int err;
1202
Atzm Watanabec7995c42013-04-16 02:50:52 +00001203 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
stephen hemmingerd3428942012-10-01 12:32:35 +00001204 err = vxlan_join_group(dev);
1205 if (err)
1206 return err;
1207 }
1208
1209 if (vxlan->age_interval)
1210 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1211
1212 return 0;
1213}
1214
1215/* Purge the forwarding table */
1216static void vxlan_flush(struct vxlan_dev *vxlan)
1217{
1218 unsigned h;
1219
1220 spin_lock_bh(&vxlan->hash_lock);
1221 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1222 struct hlist_node *p, *n;
1223 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1224 struct vxlan_fdb *f
1225 = container_of(p, struct vxlan_fdb, hlist);
1226 vxlan_fdb_destroy(vxlan, f);
1227 }
1228 }
1229 spin_unlock_bh(&vxlan->hash_lock);
1230}
1231
1232/* Cleanup timer and forwarding table on shutdown */
1233static int vxlan_stop(struct net_device *dev)
1234{
1235 struct vxlan_dev *vxlan = netdev_priv(dev);
1236
Atzm Watanabec7995c42013-04-16 02:50:52 +00001237 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001238 vxlan_leave_group(dev);
1239
1240 del_timer_sync(&vxlan->age_timer);
1241
1242 vxlan_flush(vxlan);
1243
1244 return 0;
1245}
1246
stephen hemmingerd3428942012-10-01 12:32:35 +00001247/* Stub, nothing needs to be done. */
1248static void vxlan_set_multicast_list(struct net_device *dev)
1249{
1250}
1251
1252static const struct net_device_ops vxlan_netdev_ops = {
1253 .ndo_init = vxlan_init,
1254 .ndo_open = vxlan_open,
1255 .ndo_stop = vxlan_stop,
1256 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001257 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001258 .ndo_set_rx_mode = vxlan_set_multicast_list,
1259 .ndo_change_mtu = eth_change_mtu,
1260 .ndo_validate_addr = eth_validate_addr,
1261 .ndo_set_mac_address = eth_mac_addr,
1262 .ndo_fdb_add = vxlan_fdb_add,
1263 .ndo_fdb_del = vxlan_fdb_delete,
1264 .ndo_fdb_dump = vxlan_fdb_dump,
1265};
1266
1267/* Info for udev, that this is a virtual tunnel endpoint */
1268static struct device_type vxlan_type = {
1269 .name = "vxlan",
1270};
1271
1272static void vxlan_free(struct net_device *dev)
1273{
Pravin B Shelare8171042013-03-25 14:49:46 +00001274 free_percpu(dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001275 free_netdev(dev);
1276}
1277
1278/* Initialize the device structure. */
1279static void vxlan_setup(struct net_device *dev)
1280{
1281 struct vxlan_dev *vxlan = netdev_priv(dev);
1282 unsigned h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001283 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001284
1285 eth_hw_addr_random(dev);
1286 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001287 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001288
1289 dev->netdev_ops = &vxlan_netdev_ops;
1290 dev->destructor = vxlan_free;
1291 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1292
1293 dev->tx_queue_len = 0;
1294 dev->features |= NETIF_F_LLTX;
1295 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001296 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001297 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001298 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001299
1300 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001301 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001302 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001303 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001304
1305 spin_lock_init(&vxlan->hash_lock);
1306
1307 init_timer_deferrable(&vxlan->age_timer);
1308 vxlan->age_timer.function = vxlan_cleanup;
1309 vxlan->age_timer.data = (unsigned long) vxlan;
1310
stephen hemminger05f47d62012-10-09 20:35:50 +00001311 inet_get_local_port_range(&low, &high);
1312 vxlan->port_min = low;
1313 vxlan->port_max = high;
1314
stephen hemmingerd3428942012-10-01 12:32:35 +00001315 vxlan->dev = dev;
1316
1317 for (h = 0; h < FDB_HASH_SIZE; ++h)
1318 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1319}
1320
1321static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1322 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
Atzm Watanabec7995c42013-04-16 02:50:52 +00001323 [IFLA_VXLAN_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001324 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1325 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1326 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1327 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1328 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1329 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1330 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001331 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001332 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1333 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1334 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1335 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001336};
1337
1338static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1339{
1340 if (tb[IFLA_ADDRESS]) {
1341 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1342 pr_debug("invalid link address (not ethernet)\n");
1343 return -EINVAL;
1344 }
1345
1346 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1347 pr_debug("invalid all zero ethernet address\n");
1348 return -EADDRNOTAVAIL;
1349 }
1350 }
1351
1352 if (!data)
1353 return -EINVAL;
1354
1355 if (data[IFLA_VXLAN_ID]) {
1356 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1357 if (id >= VXLAN_VID_MASK)
1358 return -ERANGE;
1359 }
1360
stephen hemminger05f47d62012-10-09 20:35:50 +00001361 if (data[IFLA_VXLAN_PORT_RANGE]) {
1362 const struct ifla_vxlan_port_range *p
1363 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1364
1365 if (ntohs(p->high) < ntohs(p->low)) {
1366 pr_debug("port range %u .. %u not valid\n",
1367 ntohs(p->low), ntohs(p->high));
1368 return -EINVAL;
1369 }
1370 }
1371
stephen hemmingerd3428942012-10-01 12:32:35 +00001372 return 0;
1373}
1374
Yan Burman1b13c972013-01-29 23:43:07 +00001375static void vxlan_get_drvinfo(struct net_device *netdev,
1376 struct ethtool_drvinfo *drvinfo)
1377{
1378 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1379 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1380}
1381
1382static const struct ethtool_ops vxlan_ethtool_ops = {
1383 .get_drvinfo = vxlan_get_drvinfo,
1384 .get_link = ethtool_op_get_link,
1385};
1386
stephen hemmingerd3428942012-10-01 12:32:35 +00001387static int vxlan_newlink(struct net *net, struct net_device *dev,
1388 struct nlattr *tb[], struct nlattr *data[])
1389{
1390 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001391 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001392 __u32 vni;
1393 int err;
1394
1395 if (!data[IFLA_VXLAN_ID])
1396 return -EINVAL;
1397
1398 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1399 if (vxlan_find_vni(net, vni)) {
1400 pr_info("duplicate VNI %u\n", vni);
1401 return -EEXIST;
1402 }
Atzm Watanabec7995c42013-04-16 02:50:52 +00001403 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001404
Atzm Watanabec7995c42013-04-16 02:50:52 +00001405 if (data[IFLA_VXLAN_REMOTE])
1406 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_REMOTE]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001407
1408 if (data[IFLA_VXLAN_LOCAL])
1409 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1410
stephen hemminger34e02aa2012-10-09 20:35:53 +00001411 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001412 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001413 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001414 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001415
stephen hemminger34e02aa2012-10-09 20:35:53 +00001416 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001417 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001418 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001419 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001420
1421 if (!tb[IFLA_MTU])
1422 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001423
1424 /* update header length based on lower device */
1425 dev->hard_header_len = lowerdev->hard_header_len +
1426 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001427 }
1428
1429 if (data[IFLA_VXLAN_TOS])
1430 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1431
Vincent Bernatafb97182012-10-30 10:27:16 +00001432 if (data[IFLA_VXLAN_TTL])
1433 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1434
stephen hemmingerd3428942012-10-01 12:32:35 +00001435 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001436 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001437
1438 if (data[IFLA_VXLAN_AGEING])
1439 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1440 else
1441 vxlan->age_interval = FDB_AGE_DEFAULT;
1442
David Stevense4f67ad2012-11-20 02:50:14 +00001443 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1444 vxlan->flags |= VXLAN_F_PROXY;
1445
1446 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1447 vxlan->flags |= VXLAN_F_RSC;
1448
1449 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1450 vxlan->flags |= VXLAN_F_L2MISS;
1451
1452 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1453 vxlan->flags |= VXLAN_F_L3MISS;
1454
stephen hemmingerd3428942012-10-01 12:32:35 +00001455 if (data[IFLA_VXLAN_LIMIT])
1456 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1457
stephen hemminger05f47d62012-10-09 20:35:50 +00001458 if (data[IFLA_VXLAN_PORT_RANGE]) {
1459 const struct ifla_vxlan_port_range *p
1460 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1461 vxlan->port_min = ntohs(p->low);
1462 vxlan->port_max = ntohs(p->high);
1463 }
1464
Yan Burman1b13c972013-01-29 23:43:07 +00001465 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1466
stephen hemmingerd3428942012-10-01 12:32:35 +00001467 err = register_netdevice(dev);
1468 if (!err)
Atzm Watanabec7995c42013-04-16 02:50:52 +00001469 hlist_add_head_rcu(&vxlan->hlist, vni_head(net, dst->remote_vni));
stephen hemmingerd3428942012-10-01 12:32:35 +00001470
1471 return err;
1472}
1473
1474static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1475{
1476 struct vxlan_dev *vxlan = netdev_priv(dev);
1477
1478 hlist_del_rcu(&vxlan->hlist);
1479
1480 unregister_netdevice_queue(dev, head);
1481}
1482
1483static size_t vxlan_get_size(const struct net_device *dev)
1484{
1485
1486 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Atzm Watanabec7995c42013-04-16 02:50:52 +00001487 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_REMOTE */
stephen hemmingerd3428942012-10-01 12:32:35 +00001488 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1489 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1490 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1491 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1492 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001493 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1494 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1495 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1496 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001497 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1498 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001499 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemmingerd3428942012-10-01 12:32:35 +00001500 0;
1501}
1502
1503static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1504{
1505 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001506 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001507 struct ifla_vxlan_port_range ports = {
1508 .low = htons(vxlan->port_min),
1509 .high = htons(vxlan->port_max),
1510 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001511
Atzm Watanabec7995c42013-04-16 02:50:52 +00001512 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001513 goto nla_put_failure;
1514
Atzm Watanabec7995c42013-04-16 02:50:52 +00001515 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_REMOTE, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001516 goto nla_put_failure;
1517
Atzm Watanabec7995c42013-04-16 02:50:52 +00001518 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001519 goto nla_put_failure;
1520
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001521 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001522 goto nla_put_failure;
1523
1524 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1525 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001526 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1527 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1528 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1529 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1530 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1531 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1532 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1533 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1534 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001535 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1536 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
1537 goto nla_put_failure;
1538
stephen hemminger05f47d62012-10-09 20:35:50 +00001539 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1540 goto nla_put_failure;
1541
stephen hemmingerd3428942012-10-01 12:32:35 +00001542 return 0;
1543
1544nla_put_failure:
1545 return -EMSGSIZE;
1546}
1547
1548static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1549 .kind = "vxlan",
1550 .maxtype = IFLA_VXLAN_MAX,
1551 .policy = vxlan_policy,
1552 .priv_size = sizeof(struct vxlan_dev),
1553 .setup = vxlan_setup,
1554 .validate = vxlan_validate,
1555 .newlink = vxlan_newlink,
1556 .dellink = vxlan_dellink,
1557 .get_size = vxlan_get_size,
1558 .fill_info = vxlan_fill_info,
1559};
1560
1561static __net_init int vxlan_init_net(struct net *net)
1562{
1563 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1564 struct sock *sk;
1565 struct sockaddr_in vxlan_addr = {
1566 .sin_family = AF_INET,
1567 .sin_addr.s_addr = htonl(INADDR_ANY),
1568 };
1569 int rc;
1570 unsigned h;
1571
1572 /* Create UDP socket for encapsulation receive. */
1573 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
1574 if (rc < 0) {
1575 pr_debug("UDP socket create failed\n");
1576 return rc;
1577 }
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001578 /* Put in proper namespace */
1579 sk = vn->sock->sk;
1580 sk_change_net(sk, net);
stephen hemmingerd3428942012-10-01 12:32:35 +00001581
1582 vxlan_addr.sin_port = htons(vxlan_port);
1583
1584 rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
1585 sizeof(vxlan_addr));
1586 if (rc < 0) {
1587 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1588 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001589 sk_release_kernel(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001590 vn->sock = NULL;
1591 return rc;
1592 }
1593
1594 /* Disable multicast loopback */
stephen hemmingerd3428942012-10-01 12:32:35 +00001595 inet_sk(sk)->mc_loop = 0;
1596
1597 /* Mark socket as an encapsulation socket. */
1598 udp_sk(sk)->encap_type = 1;
1599 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1600 udp_encap_enable();
1601
1602 for (h = 0; h < VNI_HASH_SIZE; ++h)
1603 INIT_HLIST_HEAD(&vn->vni_list[h]);
1604
1605 return 0;
1606}
1607
1608static __net_exit void vxlan_exit_net(struct net *net)
1609{
1610 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001611 struct vxlan_dev *vxlan;
1612 unsigned h;
1613
1614 rtnl_lock();
1615 for (h = 0; h < VNI_HASH_SIZE; ++h)
1616 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist)
1617 dev_close(vxlan->dev);
1618 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001619
1620 if (vn->sock) {
stephen hemmingerbfe1b9b2012-10-01 18:49:21 +00001621 sk_release_kernel(vn->sock->sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001622 vn->sock = NULL;
1623 }
1624}
1625
1626static struct pernet_operations vxlan_net_ops = {
1627 .init = vxlan_init_net,
1628 .exit = vxlan_exit_net,
1629 .id = &vxlan_net_id,
1630 .size = sizeof(struct vxlan_net),
1631};
1632
1633static int __init vxlan_init_module(void)
1634{
1635 int rc;
1636
1637 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1638
1639 rc = register_pernet_device(&vxlan_net_ops);
1640 if (rc)
1641 goto out1;
1642
1643 rc = rtnl_link_register(&vxlan_link_ops);
1644 if (rc)
1645 goto out2;
1646
1647 return 0;
1648
1649out2:
1650 unregister_pernet_device(&vxlan_net_ops);
1651out1:
1652 return rc;
1653}
1654module_init(vxlan_init_module);
1655
1656static void __exit vxlan_cleanup_module(void)
1657{
1658 rtnl_link_unregister(&vxlan_link_ops);
1659 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00001660 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001661}
1662module_exit(vxlan_cleanup_module);
1663
1664MODULE_LICENSE("GPL");
1665MODULE_VERSION(VXLAN_VERSION);
1666MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
1667MODULE_ALIAS_RTNL_LINK("vxlan");