blob: eb94bf5812cb6c59797a234c3975593e190c509c [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
stephen hemminger553675f2013-05-16 11:35:20 +000047#define PORT_HASH_BITS 8
48#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000049#define VNI_HASH_BITS 10
50#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
51#define FDB_HASH_BITS 8
52#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
53#define FDB_AGE_DEFAULT 300 /* 5 min */
54#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
55
56#define VXLAN_N_VID (1u << 24)
57#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000058/* IP header + UDP + VXLAN + Ethernet header */
59#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
stephen hemmingerd3428942012-10-01 12:32:35 +000060
61#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
62
63/* VXLAN protocol header */
64struct vxlanhdr {
65 __be32 vx_flags;
66 __be32 vx_vni;
67};
68
stephen hemminger23c578b2013-04-27 11:31:53 +000069/* UDP port for VXLAN traffic.
70 * The IANA assigned port is 4789, but the Linux default is 8472
71 * for compatability with early adopters.
72 */
stephen hemmingerd3428942012-10-01 12:32:35 +000073static unsigned int vxlan_port __read_mostly = 8472;
74module_param_named(udp_port, vxlan_port, uint, 0444);
75MODULE_PARM_DESC(udp_port, "Destination UDP port");
76
77static bool log_ecn_error = true;
78module_param(log_ecn_error, bool, 0644);
79MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
80
stephen hemmingerd3428942012-10-01 12:32:35 +000081static unsigned int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000082
83/* per UDP socket information */
84struct vxlan_sock {
85 struct hlist_node hlist;
86 struct rcu_head rcu;
87 struct work_struct del_work;
88 unsigned int refcnt;
89 struct socket *sock;
stephen hemmingerd3428942012-10-01 12:32:35 +000090 struct hlist_head vni_list[VNI_HASH_SIZE];
91};
92
stephen hemminger553675f2013-05-16 11:35:20 +000093/* per-network namespace private data for this module */
94struct vxlan_net {
95 struct list_head vxlan_list;
96 struct hlist_head sock_list[PORT_HASH_SIZE];
97};
98
David Stevens66817122013-03-15 04:35:51 +000099struct vxlan_rdst {
David Stevens66817122013-03-15 04:35:51 +0000100 __be32 remote_ip;
101 __be16 remote_port;
102 u32 remote_vni;
103 u32 remote_ifindex;
104 struct vxlan_rdst *remote_next;
105};
106
stephen hemmingerd3428942012-10-01 12:32:35 +0000107/* Forwarding table entry */
108struct vxlan_fdb {
109 struct hlist_node hlist; /* linked list of entries */
110 struct rcu_head rcu;
111 unsigned long updated; /* jiffies */
112 unsigned long used;
David Stevens66817122013-03-15 04:35:51 +0000113 struct vxlan_rdst remote;
stephen hemmingerd3428942012-10-01 12:32:35 +0000114 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000115 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000116 u8 eth_addr[ETH_ALEN];
117};
118
stephen hemmingerd3428942012-10-01 12:32:35 +0000119/* Pseudo network device */
120struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000121 struct hlist_node hlist; /* vni hash table */
122 struct list_head next; /* vxlan's per namespace list */
123 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000124 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000125 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000126 __be32 saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000127 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000128 __u16 port_min; /* source port range */
129 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000130 __u8 tos; /* TOS override */
131 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000132 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000133
134 unsigned long age_interval;
135 struct timer_list age_timer;
136 spinlock_t hash_lock;
137 unsigned int addrcnt;
138 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000139
140 struct hlist_head fdb_head[FDB_HASH_SIZE];
141};
142
David Stevense4f67ad2012-11-20 02:50:14 +0000143#define VXLAN_F_LEARN 0x01
144#define VXLAN_F_PROXY 0x02
145#define VXLAN_F_RSC 0x04
146#define VXLAN_F_L2MISS 0x08
147#define VXLAN_F_L3MISS 0x10
148
stephen hemmingerd3428942012-10-01 12:32:35 +0000149/* salt for hash table */
150static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700151static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000152
stephen hemminger553675f2013-05-16 11:35:20 +0000153/* Virtual Network hash table head */
154static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
155{
156 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
157}
158
159/* Socket hash table head */
160static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000161{
162 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
163
stephen hemminger553675f2013-05-16 11:35:20 +0000164 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
165}
166
167/* Find VXLAN socket based on network namespace and UDP port */
168static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
169{
170 struct vxlan_sock *vs;
171
172 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
173 if (inet_sk(vs->sock->sk)->inet_sport == port)
174 return vs;
175 }
176 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000177}
178
179/* Look up VNI in a per net namespace table */
stephen hemminger553675f2013-05-16 11:35:20 +0000180static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000181{
stephen hemminger553675f2013-05-16 11:35:20 +0000182 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000183 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000184
stephen hemminger553675f2013-05-16 11:35:20 +0000185 vs = vxlan_find_port(net, port);
186 if (!vs)
187 return NULL;
188
189 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000190 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000191 return vxlan;
192 }
193
194 return NULL;
195}
196
197/* Fill in neighbour message in skbuff. */
198static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
199 const struct vxlan_fdb *fdb,
David Stevens66817122013-03-15 04:35:51 +0000200 u32 portid, u32 seq, int type, unsigned int flags,
201 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000202{
203 unsigned long now = jiffies;
204 struct nda_cacheinfo ci;
205 struct nlmsghdr *nlh;
206 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000207 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000208
209 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
210 if (nlh == NULL)
211 return -EMSGSIZE;
212
213 ndm = nlmsg_data(nlh);
214 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000215
216 send_eth = send_ip = true;
217
218 if (type == RTM_GETNEIGH) {
219 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000220 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000221 send_eth = !is_zero_ether_addr(fdb->eth_addr);
222 } else
223 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000224 ndm->ndm_state = fdb->state;
225 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000226 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000227 ndm->ndm_type = NDA_DST;
228
David Stevense4f67ad2012-11-20 02:50:14 +0000229 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000230 goto nla_put_failure;
231
David Stevens66817122013-03-15 04:35:51 +0000232 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
233 goto nla_put_failure;
234
stephen hemminger823aa872013-04-27 11:31:57 +0000235 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000236 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
237 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000238 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
David Stevens66817122013-03-15 04:35:51 +0000239 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
240 goto nla_put_failure;
241 if (rdst->remote_ifindex &&
242 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000243 goto nla_put_failure;
244
245 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
246 ci.ndm_confirmed = 0;
247 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
248 ci.ndm_refcnt = 0;
249
250 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
251 goto nla_put_failure;
252
253 return nlmsg_end(skb, nlh);
254
255nla_put_failure:
256 nlmsg_cancel(skb, nlh);
257 return -EMSGSIZE;
258}
259
260static inline size_t vxlan_nlmsg_size(void)
261{
262 return NLMSG_ALIGN(sizeof(struct ndmsg))
263 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
264 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000265 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000266 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
267 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000268 + nla_total_size(sizeof(struct nda_cacheinfo));
269}
270
271static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
272 const struct vxlan_fdb *fdb, int type)
273{
274 struct net *net = dev_net(vxlan->dev);
275 struct sk_buff *skb;
276 int err = -ENOBUFS;
277
278 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
279 if (skb == NULL)
280 goto errout;
281
David Stevens66817122013-03-15 04:35:51 +0000282 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
stephen hemmingerd3428942012-10-01 12:32:35 +0000283 if (err < 0) {
284 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
285 WARN_ON(err == -EMSGSIZE);
286 kfree_skb(skb);
287 goto errout;
288 }
289
290 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
291 return;
292errout:
293 if (err < 0)
294 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
295}
296
David Stevense4f67ad2012-11-20 02:50:14 +0000297static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
298{
299 struct vxlan_dev *vxlan = netdev_priv(dev);
300 struct vxlan_fdb f;
301
302 memset(&f, 0, sizeof f);
303 f.state = NUD_STALE;
David Stevens66817122013-03-15 04:35:51 +0000304 f.remote.remote_ip = ipa; /* goes to NDA_DST */
305 f.remote.remote_vni = VXLAN_N_VID;
David Stevense4f67ad2012-11-20 02:50:14 +0000306
307 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
308}
309
310static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
311{
312 struct vxlan_fdb f;
313
314 memset(&f, 0, sizeof f);
315 f.state = NUD_STALE;
316 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
317
318 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
319}
320
stephen hemmingerd3428942012-10-01 12:32:35 +0000321/* Hash Ethernet address */
322static u32 eth_hash(const unsigned char *addr)
323{
324 u64 value = get_unaligned((u64 *)addr);
325
326 /* only want 6 bytes */
327#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000328 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000329#else
330 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000331#endif
332 return hash_64(value, FDB_HASH_BITS);
333}
334
335/* Hash chain to use given mac address */
336static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
337 const u8 *mac)
338{
339 return &vxlan->fdb_head[eth_hash(mac)];
340}
341
342/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000343static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000344 const u8 *mac)
345
346{
347 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
348 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000349
Sasha Levinb67bfe02013-02-27 17:06:00 -0800350 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000351 if (compare_ether_addr(mac, f->eth_addr) == 0)
352 return f;
353 }
354
355 return NULL;
356}
357
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000358static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
359 const u8 *mac)
360{
361 struct vxlan_fdb *f;
362
363 f = __vxlan_find_mac(vxlan, mac);
364 if (f)
365 f->used = jiffies;
366
367 return f;
368}
369
David Stevens66817122013-03-15 04:35:51 +0000370/* Add/update destinations for multicast */
371static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000372 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000373{
374 struct vxlan_rdst *rd_prev, *rd;
375
376 rd_prev = NULL;
377 for (rd = &f->remote; rd; rd = rd->remote_next) {
378 if (rd->remote_ip == ip &&
379 rd->remote_port == port &&
380 rd->remote_vni == vni &&
381 rd->remote_ifindex == ifindex)
382 return 0;
383 rd_prev = rd;
384 }
385 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
386 if (rd == NULL)
387 return -ENOBUFS;
388 rd->remote_ip = ip;
389 rd->remote_port = port;
390 rd->remote_vni = vni;
391 rd->remote_ifindex = ifindex;
392 rd->remote_next = NULL;
393 rd_prev->remote_next = rd;
394 return 1;
395}
396
stephen hemmingerd3428942012-10-01 12:32:35 +0000397/* Add new entry to forwarding table -- assumes lock held */
398static int vxlan_fdb_create(struct vxlan_dev *vxlan,
399 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000400 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000401 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000402 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000403{
404 struct vxlan_fdb *f;
405 int notify = 0;
406
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000407 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000408 if (f) {
409 if (flags & NLM_F_EXCL) {
410 netdev_dbg(vxlan->dev,
411 "lost race to create %pM\n", mac);
412 return -EEXIST;
413 }
414 if (f->state != state) {
415 f->state = state;
416 f->updated = jiffies;
417 notify = 1;
418 }
David Stevensae884082013-04-19 00:36:26 +0000419 if (f->flags != ndm_flags) {
420 f->flags = ndm_flags;
421 f->updated = jiffies;
422 notify = 1;
423 }
David Stevens66817122013-03-15 04:35:51 +0000424 if ((flags & NLM_F_APPEND) &&
425 is_multicast_ether_addr(f->eth_addr)) {
426 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
427
428 if (rc < 0)
429 return rc;
430 notify |= rc;
431 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000432 } else {
433 if (!(flags & NLM_F_CREATE))
434 return -ENOENT;
435
436 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
437 return -ENOSPC;
438
439 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
440 f = kmalloc(sizeof(*f), GFP_ATOMIC);
441 if (!f)
442 return -ENOMEM;
443
444 notify = 1;
David Stevens66817122013-03-15 04:35:51 +0000445 f->remote.remote_ip = ip;
446 f->remote.remote_port = port;
447 f->remote.remote_vni = vni;
448 f->remote.remote_ifindex = ifindex;
449 f->remote.remote_next = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000450 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000451 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000452 f->updated = f->used = jiffies;
453 memcpy(f->eth_addr, mac, ETH_ALEN);
454
455 ++vxlan->addrcnt;
456 hlist_add_head_rcu(&f->hlist,
457 vxlan_fdb_head(vxlan, mac));
458 }
459
460 if (notify)
461 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
462
463 return 0;
464}
465
Wei Yongjun6706c822013-04-11 19:00:35 +0000466static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000467{
468 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
469
470 while (f->remote.remote_next) {
471 struct vxlan_rdst *rd = f->remote.remote_next;
472
473 f->remote.remote_next = rd->remote_next;
474 kfree(rd);
475 }
476 kfree(f);
477}
478
stephen hemmingerd3428942012-10-01 12:32:35 +0000479static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
480{
481 netdev_dbg(vxlan->dev,
482 "delete %pM\n", f->eth_addr);
483
484 --vxlan->addrcnt;
485 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
486
487 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000488 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000489}
490
491/* Add static entry (via netlink) */
492static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
493 struct net_device *dev,
494 const unsigned char *addr, u16 flags)
495{
496 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens66817122013-03-15 04:35:51 +0000497 struct net *net = dev_net(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000498 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000499 __be16 port;
500 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000501 int err;
502
503 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
504 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
505 ndm->ndm_state);
506 return -EINVAL;
507 }
508
509 if (tb[NDA_DST] == NULL)
510 return -EINVAL;
511
512 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
513 return -EAFNOSUPPORT;
514
515 ip = nla_get_be32(tb[NDA_DST]);
516
David Stevens66817122013-03-15 04:35:51 +0000517 if (tb[NDA_PORT]) {
stephen hemminger73cf3312013-04-27 11:31:54 +0000518 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
David Stevens66817122013-03-15 04:35:51 +0000519 return -EINVAL;
stephen hemminger73cf3312013-04-27 11:31:54 +0000520 port = nla_get_be16(tb[NDA_PORT]);
David Stevens66817122013-03-15 04:35:51 +0000521 } else
stephen hemminger823aa872013-04-27 11:31:57 +0000522 port = vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +0000523
524 if (tb[NDA_VNI]) {
525 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
526 return -EINVAL;
527 vni = nla_get_u32(tb[NDA_VNI]);
528 } else
Atzm Watanabec7995c42013-04-16 02:50:52 +0000529 vni = vxlan->default_dst.remote_vni;
David Stevens66817122013-03-15 04:35:51 +0000530
531 if (tb[NDA_IFINDEX]) {
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000532 struct net_device *tdev;
David Stevens66817122013-03-15 04:35:51 +0000533
534 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
535 return -EINVAL;
536 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000537 tdev = dev_get_by_index(net, ifindex);
538 if (!tdev)
David Stevens66817122013-03-15 04:35:51 +0000539 return -EADDRNOTAVAIL;
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000540 dev_put(tdev);
David Stevens66817122013-03-15 04:35:51 +0000541 } else
542 ifindex = 0;
543
stephen hemmingerd3428942012-10-01 12:32:35 +0000544 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000545 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
546 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000547 spin_unlock_bh(&vxlan->hash_lock);
548
549 return err;
550}
551
552/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000553static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
554 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000555 const unsigned char *addr)
556{
557 struct vxlan_dev *vxlan = netdev_priv(dev);
558 struct vxlan_fdb *f;
559 int err = -ENOENT;
560
561 spin_lock_bh(&vxlan->hash_lock);
562 f = vxlan_find_mac(vxlan, addr);
563 if (f) {
564 vxlan_fdb_destroy(vxlan, f);
565 err = 0;
566 }
567 spin_unlock_bh(&vxlan->hash_lock);
568
569 return err;
570}
571
572/* Dump forwarding table */
573static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
574 struct net_device *dev, int idx)
575{
576 struct vxlan_dev *vxlan = netdev_priv(dev);
577 unsigned int h;
578
579 for (h = 0; h < FDB_HASH_SIZE; ++h) {
580 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000581 int err;
582
Sasha Levinb67bfe02013-02-27 17:06:00 -0800583 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000584 struct vxlan_rdst *rd;
585 for (rd = &f->remote; rd; rd = rd->remote_next) {
586 if (idx < cb->args[0])
587 goto skip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000588
David Stevens66817122013-03-15 04:35:51 +0000589 err = vxlan_fdb_info(skb, vxlan, f,
590 NETLINK_CB(cb->skb).portid,
591 cb->nlh->nlmsg_seq,
592 RTM_NEWNEIGH,
593 NLM_F_MULTI, rd);
594 if (err < 0)
595 break;
stephen hemmingerd3428942012-10-01 12:32:35 +0000596skip:
David Stevens66817122013-03-15 04:35:51 +0000597 ++idx;
598 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000599 }
600 }
601
602 return idx;
603}
604
605/* Watch incoming packets to learn mapping between Ethernet address
606 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700607 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000608 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700609static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000610 __be32 src_ip, const u8 *src_mac)
611{
612 struct vxlan_dev *vxlan = netdev_priv(dev);
613 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000614
615 f = vxlan_find_mac(vxlan, src_mac);
616 if (likely(f)) {
David Stevens66817122013-03-15 04:35:51 +0000617 if (likely(f->remote.remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700618 return false;
619
620 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700621 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700622 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000623
624 if (net_ratelimit())
625 netdev_info(dev,
626 "%pM migrated from %pI4 to %pI4\n",
David Stevens66817122013-03-15 04:35:51 +0000627 src_mac, &f->remote.remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000628
David Stevens66817122013-03-15 04:35:51 +0000629 f->remote.remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000630 f->updated = jiffies;
631 } else {
632 /* learned new entry */
633 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700634
635 /* close off race between vxlan_flush and incoming packets */
636 if (netif_running(dev))
637 vxlan_fdb_create(vxlan, src_mac, src_ip,
638 NUD_REACHABLE,
639 NLM_F_EXCL|NLM_F_CREATE,
640 vxlan->dst_port,
641 vxlan->default_dst.remote_vni,
642 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000643 spin_unlock(&vxlan->hash_lock);
644 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700645
646 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000647}
648
649
650/* See if multicast group is already in use by other ID */
651static bool vxlan_group_used(struct vxlan_net *vn,
652 const struct vxlan_dev *this)
653{
stephen hemminger553675f2013-05-16 11:35:20 +0000654 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000655
stephen hemminger553675f2013-05-16 11:35:20 +0000656 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
657 if (vxlan == this)
658 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000659
stephen hemminger553675f2013-05-16 11:35:20 +0000660 if (!netif_running(vxlan->dev))
661 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000662
stephen hemminger553675f2013-05-16 11:35:20 +0000663 if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
664 return true;
665 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000666
667 return false;
668}
669
670/* kernel equivalent to IP_ADD_MEMBERSHIP */
671static int vxlan_join_group(struct net_device *dev)
672{
673 struct vxlan_dev *vxlan = netdev_priv(dev);
674 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemminger553675f2013-05-16 11:35:20 +0000675 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000676 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000677 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
678 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000679 };
680 int err;
681
682 /* Already a member of group */
683 if (vxlan_group_used(vn, vxlan))
684 return 0;
685
686 /* Need to drop RTNL to call multicast join */
687 rtnl_unlock();
688 lock_sock(sk);
689 err = ip_mc_join_group(sk, &mreq);
690 release_sock(sk);
691 rtnl_lock();
692
693 return err;
694}
695
696
697/* kernel equivalent to IP_DROP_MEMBERSHIP */
698static int vxlan_leave_group(struct net_device *dev)
699{
700 struct vxlan_dev *vxlan = netdev_priv(dev);
701 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
702 int err = 0;
stephen hemminger553675f2013-05-16 11:35:20 +0000703 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000704 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000705 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
706 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000707 };
708
709 /* Only leave group when last vxlan is done. */
710 if (vxlan_group_used(vn, vxlan))
711 return 0;
712
713 /* Need to drop RTNL to call multicast leave */
714 rtnl_unlock();
715 lock_sock(sk);
716 err = ip_mc_leave_group(sk, &mreq);
717 release_sock(sk);
718 rtnl_lock();
719
720 return err;
721}
722
723/* Callback from net/ipv4/udp.c to receive packets */
724static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
725{
726 struct iphdr *oip;
727 struct vxlanhdr *vxh;
728 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000729 struct pcpu_tstats *stats;
stephen hemminger553675f2013-05-16 11:35:20 +0000730 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000731 __u32 vni;
732 int err;
733
734 /* pop off outer UDP header */
735 __skb_pull(skb, sizeof(struct udphdr));
736
737 /* Need Vxlan and inner Ethernet header to be present */
738 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
739 goto error;
740
741 /* Drop packets with reserved bits set */
742 vxh = (struct vxlanhdr *) skb->data;
743 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
744 (vxh->vx_vni & htonl(0xff))) {
745 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
746 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
747 goto error;
748 }
749
750 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000751
752 /* Is this VNI defined? */
753 vni = ntohl(vxh->vx_vni) >> 8;
stephen hemminger553675f2013-05-16 11:35:20 +0000754 port = inet_sk(sk)->inet_sport;
755 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000756 if (!vxlan) {
stephen hemminger553675f2013-05-16 11:35:20 +0000757 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
758 vni, ntohs(port));
stephen hemmingerd3428942012-10-01 12:32:35 +0000759 goto drop;
760 }
761
762 if (!pskb_may_pull(skb, ETH_HLEN)) {
763 vxlan->dev->stats.rx_length_errors++;
764 vxlan->dev->stats.rx_errors++;
765 goto drop;
766 }
767
David Stevense4f67ad2012-11-20 02:50:14 +0000768 skb_reset_mac_header(skb);
769
stephen hemmingerd3428942012-10-01 12:32:35 +0000770 /* Re-examine inner Ethernet packet */
771 oip = ip_hdr(skb);
772 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000773
774 /* Ignore packet loops (and multicast echo) */
775 if (compare_ether_addr(eth_hdr(skb)->h_source,
776 vxlan->dev->dev_addr) == 0)
777 goto drop;
778
stephen hemminger26a41ae62013-06-17 12:09:58 -0700779 if ((vxlan->flags & VXLAN_F_LEARN) &&
780 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
781 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000782
783 __skb_tunnel_rx(skb, vxlan->dev);
784 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000785
786 /* If the NIC driver gave us an encapsulated packet with
787 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
788 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
789 * for us. Otherwise force the upper layers to verify it.
790 */
791 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
792 !(vxlan->dev->features & NETIF_F_RXCSUM))
793 skb->ip_summed = CHECKSUM_NONE;
794
795 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000796
797 err = IP_ECN_decapsulate(oip, skb);
798 if (unlikely(err)) {
799 if (log_ecn_error)
800 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
801 &oip->saddr, oip->tos);
802 if (err > 1) {
803 ++vxlan->dev->stats.rx_frame_errors;
804 ++vxlan->dev->stats.rx_errors;
805 goto drop;
806 }
807 }
808
Pravin B Shelare8171042013-03-25 14:49:46 +0000809 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000810 u64_stats_update_begin(&stats->syncp);
811 stats->rx_packets++;
812 stats->rx_bytes += skb->len;
813 u64_stats_update_end(&stats->syncp);
814
815 netif_rx(skb);
816
817 return 0;
818error:
819 /* Put UDP header back */
820 __skb_push(skb, sizeof(struct udphdr));
821
822 return 1;
823drop:
824 /* Consume bad packet */
825 kfree_skb(skb);
826 return 0;
827}
828
David Stevense4f67ad2012-11-20 02:50:14 +0000829static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
830{
831 struct vxlan_dev *vxlan = netdev_priv(dev);
832 struct arphdr *parp;
833 u8 *arpptr, *sha;
834 __be32 sip, tip;
835 struct neighbour *n;
836
837 if (dev->flags & IFF_NOARP)
838 goto out;
839
840 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
841 dev->stats.tx_dropped++;
842 goto out;
843 }
844 parp = arp_hdr(skb);
845
846 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
847 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
848 parp->ar_pro != htons(ETH_P_IP) ||
849 parp->ar_op != htons(ARPOP_REQUEST) ||
850 parp->ar_hln != dev->addr_len ||
851 parp->ar_pln != 4)
852 goto out;
853 arpptr = (u8 *)parp + sizeof(struct arphdr);
854 sha = arpptr;
855 arpptr += dev->addr_len; /* sha */
856 memcpy(&sip, arpptr, sizeof(sip));
857 arpptr += sizeof(sip);
858 arpptr += dev->addr_len; /* tha */
859 memcpy(&tip, arpptr, sizeof(tip));
860
861 if (ipv4_is_loopback(tip) ||
862 ipv4_is_multicast(tip))
863 goto out;
864
865 n = neigh_lookup(&arp_tbl, &tip, dev);
866
867 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000868 struct vxlan_fdb *f;
869 struct sk_buff *reply;
870
871 if (!(n->nud_state & NUD_CONNECTED)) {
872 neigh_release(n);
873 goto out;
874 }
875
876 f = vxlan_find_mac(vxlan, n->ha);
David Stevens66817122013-03-15 04:35:51 +0000877 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000878 /* bridge-local neighbor */
879 neigh_release(n);
880 goto out;
881 }
882
883 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
884 n->ha, sha);
885
886 neigh_release(n);
887
888 skb_reset_mac_header(reply);
889 __skb_pull(reply, skb_network_offset(reply));
890 reply->ip_summed = CHECKSUM_UNNECESSARY;
891 reply->pkt_type = PACKET_HOST;
892
893 if (netif_rx_ni(reply) == NET_RX_DROP)
894 dev->stats.rx_dropped++;
895 } else if (vxlan->flags & VXLAN_F_L3MISS)
896 vxlan_ip_miss(dev, tip);
897out:
898 consume_skb(skb);
899 return NETDEV_TX_OK;
900}
901
902static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
903{
904 struct vxlan_dev *vxlan = netdev_priv(dev);
905 struct neighbour *n;
906 struct iphdr *pip;
907
908 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
909 return false;
910
911 n = NULL;
912 switch (ntohs(eth_hdr(skb)->h_proto)) {
913 case ETH_P_IP:
914 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
915 return false;
916 pip = ip_hdr(skb);
917 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
918 break;
919 default:
920 return false;
921 }
922
923 if (n) {
924 bool diff;
925
926 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
927 if (diff) {
928 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
929 dev->addr_len);
930 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
931 }
932 neigh_release(n);
933 return diff;
934 } else if (vxlan->flags & VXLAN_F_L3MISS)
935 vxlan_ip_miss(dev, pip->daddr);
936 return false;
937}
938
stephen hemminger553675f2013-05-16 11:35:20 +0000939static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +0000940{
941 sock_put(skb->sk);
942}
943
944/* On transmit, associate with the tunnel socket */
945static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
946{
stephen hemminger553675f2013-05-16 11:35:20 +0000947 struct vxlan_dev *vxlan = netdev_priv(dev);
948 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +0000949
950 skb_orphan(skb);
951 sock_hold(sk);
952 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +0000953 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +0000954}
955
stephen hemminger05f47d62012-10-09 20:35:50 +0000956/* Compute source port for outgoing packet
957 * first choice to use L4 flow hash since it will spread
958 * better and maybe available from hardware
959 * secondary choice is to use jhash on the Ethernet header
960 */
stephen hemminger7d836a72013-04-27 11:31:56 +0000961static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +0000962{
963 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
964 u32 hash;
965
966 hash = skb_get_rxhash(skb);
967 if (!hash)
968 hash = jhash(skb->data, 2 * ETH_ALEN,
969 (__force u32) skb->protocol);
970
stephen hemminger7d836a72013-04-27 11:31:56 +0000971 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +0000972}
973
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000974static int handle_offloads(struct sk_buff *skb)
975{
976 if (skb_is_gso(skb)) {
977 int err = skb_unclone(skb, GFP_ATOMIC);
978 if (unlikely(err))
979 return err;
980
Dmitry Kravkovf6ace502013-04-28 08:16:01 +0000981 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000982 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
983 skb->ip_summed = CHECKSUM_NONE;
984
985 return 0;
986}
987
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000988/* Bypass encapsulation if the destination is local */
989static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
990 struct vxlan_dev *dst_vxlan)
991{
992 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
993 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
994
995 skb->pkt_type = PACKET_HOST;
996 skb->encapsulation = 0;
997 skb->dev = dst_vxlan->dev;
998 __skb_pull(skb, skb_network_offset(skb));
999
1000 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001001 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1002 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001003
1004 u64_stats_update_begin(&tx_stats->syncp);
1005 tx_stats->tx_packets++;
1006 tx_stats->tx_bytes += skb->len;
1007 u64_stats_update_end(&tx_stats->syncp);
1008
1009 if (netif_rx(skb) == NET_RX_SUCCESS) {
1010 u64_stats_update_begin(&rx_stats->syncp);
1011 rx_stats->rx_packets++;
1012 rx_stats->rx_bytes += skb->len;
1013 u64_stats_update_end(&rx_stats->syncp);
1014 } else {
1015 skb->dev->stats.rx_dropped++;
1016 }
1017}
1018
David Stevens66817122013-03-15 04:35:51 +00001019static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1020 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001021{
1022 struct vxlan_dev *vxlan = netdev_priv(dev);
1023 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001024 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001025 struct vxlanhdr *vxh;
1026 struct udphdr *uh;
1027 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001028 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001029 __be16 src_port, dst_port;
David Stevens66817122013-03-15 04:35:51 +00001030 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001031 __be16 df = 0;
1032 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001033 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001034
stephen hemminger823aa872013-04-27 11:31:57 +00001035 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001036 vni = rdst->remote_vni;
1037 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001038
1039 if (!dst) {
1040 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001041 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001042 vxlan_encap_bypass(skb, vxlan, vxlan);
David Stevense4f67ad2012-11-20 02:50:14 +00001043 return NETDEV_TX_OK;
1044 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001045 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001046 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001047
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001048 if (!skb->encapsulation) {
1049 skb_reset_inner_headers(skb);
1050 skb->encapsulation = 1;
1051 }
1052
stephen hemmingerd3428942012-10-01 12:32:35 +00001053 /* Need space for new headers (invalidates iph ptr) */
1054 if (skb_cow_head(skb, VXLAN_HEADROOM))
1055 goto drop;
1056
stephen hemmingerd3428942012-10-01 12:32:35 +00001057 old_iph = ip_hdr(skb);
1058
stephen hemmingerd3428942012-10-01 12:32:35 +00001059 ttl = vxlan->ttl;
1060 if (!ttl && IN_MULTICAST(ntohl(dst)))
1061 ttl = 1;
1062
1063 tos = vxlan->tos;
1064 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001065 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001066
stephen hemminger05f47d62012-10-09 20:35:50 +00001067 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001068
stephen hemmingerca78f182012-10-09 20:35:48 +00001069 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001070 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001071 fl4.flowi4_tos = RT_TOS(tos);
1072 fl4.daddr = dst;
1073 fl4.saddr = vxlan->saddr;
1074
1075 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001076 if (IS_ERR(rt)) {
1077 netdev_dbg(dev, "no route to %pI4\n", &dst);
1078 dev->stats.tx_carrier_errors++;
1079 goto tx_error;
1080 }
1081
1082 if (rt->dst.dev == dev) {
1083 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1084 ip_rt_put(rt);
1085 dev->stats.collisions++;
1086 goto tx_error;
1087 }
1088
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001089 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001090 if (rt->rt_flags & RTCF_LOCAL &&
1091 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001092 struct vxlan_dev *dst_vxlan;
1093
1094 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001095 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001096 if (!dst_vxlan)
1097 goto tx_error;
1098 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1099 return NETDEV_TX_OK;
1100 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001101 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1102 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001103 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001104
1105 __skb_push(skb, sizeof(*uh));
1106 skb_reset_transport_header(skb);
1107 uh = udp_hdr(skb);
1108
stephen hemminger73cf3312013-04-27 11:31:54 +00001109 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001110 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001111
1112 uh->len = htons(skb->len);
1113 uh->check = 0;
1114
stephen hemminger1cad8712012-10-09 20:35:49 +00001115 vxlan_set_owner(dev, skb);
1116
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001117 if (handle_offloads(skb))
1118 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001119
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001120 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1121 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1122
1123 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1124 IPPROTO_UDP, tos, ttl, df);
1125 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1126
stephen hemmingerd3428942012-10-01 12:32:35 +00001127 return NETDEV_TX_OK;
1128
1129drop:
1130 dev->stats.tx_dropped++;
1131 goto tx_free;
1132
1133tx_error:
1134 dev->stats.tx_errors++;
1135tx_free:
1136 dev_kfree_skb(skb);
1137 return NETDEV_TX_OK;
1138}
1139
David Stevens66817122013-03-15 04:35:51 +00001140/* Transmit local packets over Vxlan
1141 *
1142 * Outer IP header inherits ECN and DF from inner header.
1143 * Outer UDP destination is the VXLAN assigned port.
1144 * source port is based on hash of flow
1145 */
1146static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1147{
1148 struct vxlan_dev *vxlan = netdev_priv(dev);
1149 struct ethhdr *eth;
1150 bool did_rsc = false;
Atzm Watanabec7995c42013-04-16 02:50:52 +00001151 struct vxlan_rdst *rdst0, *rdst;
David Stevens66817122013-03-15 04:35:51 +00001152 struct vxlan_fdb *f;
1153 int rc1, rc;
1154
1155 skb_reset_mac_header(skb);
1156 eth = eth_hdr(skb);
1157
1158 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1159 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001160
1161 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001162 did_rsc = false;
1163
1164 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1165 ntohs(eth->h_proto) == ETH_P_IP) {
1166 did_rsc = route_shortcircuit(dev, skb);
1167 if (did_rsc)
1168 f = vxlan_find_mac(vxlan, eth->h_dest);
1169 }
1170
David Stevens66817122013-03-15 04:35:51 +00001171 if (f == NULL) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001172 rdst0 = &vxlan->default_dst;
David Stevens66817122013-03-15 04:35:51 +00001173
Atzm Watanabec7995c42013-04-16 02:50:52 +00001174 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
David Stevens66817122013-03-15 04:35:51 +00001175 (vxlan->flags & VXLAN_F_L2MISS) &&
1176 !is_multicast_ether_addr(eth->h_dest))
1177 vxlan_fdb_miss(vxlan, eth->h_dest);
1178 } else
1179 rdst0 = &f->remote;
1180
1181 rc = NETDEV_TX_OK;
1182
1183 /* if there are multiple destinations, send copies */
1184 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1185 struct sk_buff *skb1;
1186
1187 skb1 = skb_clone(skb, GFP_ATOMIC);
stephen hemminger7aa27232013-06-17 12:09:59 -07001188 if (skb1) {
1189 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1190 if (rc == NETDEV_TX_OK)
1191 rc = rc1;
1192 }
David Stevens66817122013-03-15 04:35:51 +00001193 }
1194
1195 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1196 if (rc == NETDEV_TX_OK)
1197 rc = rc1;
1198 return rc;
1199}
1200
stephen hemmingerd3428942012-10-01 12:32:35 +00001201/* Walk the forwarding table and purge stale entries */
1202static void vxlan_cleanup(unsigned long arg)
1203{
1204 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1205 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1206 unsigned int h;
1207
1208 if (!netif_running(vxlan->dev))
1209 return;
1210
1211 spin_lock_bh(&vxlan->hash_lock);
1212 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1213 struct hlist_node *p, *n;
1214 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1215 struct vxlan_fdb *f
1216 = container_of(p, struct vxlan_fdb, hlist);
1217 unsigned long timeout;
1218
stephen hemminger3c172862012-10-26 06:24:34 +00001219 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001220 continue;
1221
1222 timeout = f->used + vxlan->age_interval * HZ;
1223 if (time_before_eq(timeout, jiffies)) {
1224 netdev_dbg(vxlan->dev,
1225 "garbage collect %pM\n",
1226 f->eth_addr);
1227 f->state = NUD_STALE;
1228 vxlan_fdb_destroy(vxlan, f);
1229 } else if (time_before(timeout, next_timer))
1230 next_timer = timeout;
1231 }
1232 }
1233 spin_unlock_bh(&vxlan->hash_lock);
1234
1235 mod_timer(&vxlan->age_timer, next_timer);
1236}
1237
1238/* Setup stats when device is created */
1239static int vxlan_init(struct net_device *dev)
1240{
Pravin B Shelare8171042013-03-25 14:49:46 +00001241 dev->tstats = alloc_percpu(struct pcpu_tstats);
1242 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001243 return -ENOMEM;
1244
1245 return 0;
1246}
1247
1248/* Start ageing timer and join group when device is brought up */
1249static int vxlan_open(struct net_device *dev)
1250{
1251 struct vxlan_dev *vxlan = netdev_priv(dev);
1252 int err;
1253
Atzm Watanabec7995c42013-04-16 02:50:52 +00001254 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
stephen hemmingerd3428942012-10-01 12:32:35 +00001255 err = vxlan_join_group(dev);
1256 if (err)
1257 return err;
1258 }
1259
1260 if (vxlan->age_interval)
1261 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1262
1263 return 0;
1264}
1265
1266/* Purge the forwarding table */
1267static void vxlan_flush(struct vxlan_dev *vxlan)
1268{
Cong Wang31fec5a2013-05-27 22:35:52 +00001269 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001270
1271 spin_lock_bh(&vxlan->hash_lock);
1272 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1273 struct hlist_node *p, *n;
1274 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1275 struct vxlan_fdb *f
1276 = container_of(p, struct vxlan_fdb, hlist);
1277 vxlan_fdb_destroy(vxlan, f);
1278 }
1279 }
1280 spin_unlock_bh(&vxlan->hash_lock);
1281}
1282
1283/* Cleanup timer and forwarding table on shutdown */
1284static int vxlan_stop(struct net_device *dev)
1285{
1286 struct vxlan_dev *vxlan = netdev_priv(dev);
1287
Atzm Watanabec7995c42013-04-16 02:50:52 +00001288 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001289 vxlan_leave_group(dev);
1290
1291 del_timer_sync(&vxlan->age_timer);
1292
1293 vxlan_flush(vxlan);
1294
1295 return 0;
1296}
1297
stephen hemmingerd3428942012-10-01 12:32:35 +00001298/* Stub, nothing needs to be done. */
1299static void vxlan_set_multicast_list(struct net_device *dev)
1300{
1301}
1302
1303static const struct net_device_ops vxlan_netdev_ops = {
1304 .ndo_init = vxlan_init,
1305 .ndo_open = vxlan_open,
1306 .ndo_stop = vxlan_stop,
1307 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001308 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001309 .ndo_set_rx_mode = vxlan_set_multicast_list,
1310 .ndo_change_mtu = eth_change_mtu,
1311 .ndo_validate_addr = eth_validate_addr,
1312 .ndo_set_mac_address = eth_mac_addr,
1313 .ndo_fdb_add = vxlan_fdb_add,
1314 .ndo_fdb_del = vxlan_fdb_delete,
1315 .ndo_fdb_dump = vxlan_fdb_dump,
1316};
1317
1318/* Info for udev, that this is a virtual tunnel endpoint */
1319static struct device_type vxlan_type = {
1320 .name = "vxlan",
1321};
1322
1323static void vxlan_free(struct net_device *dev)
1324{
Pravin B Shelare8171042013-03-25 14:49:46 +00001325 free_percpu(dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001326 free_netdev(dev);
1327}
1328
1329/* Initialize the device structure. */
1330static void vxlan_setup(struct net_device *dev)
1331{
1332 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001333 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001334 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001335
1336 eth_hw_addr_random(dev);
1337 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001338 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001339
1340 dev->netdev_ops = &vxlan_netdev_ops;
1341 dev->destructor = vxlan_free;
1342 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1343
1344 dev->tx_queue_len = 0;
1345 dev->features |= NETIF_F_LLTX;
1346 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001347 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001348 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001349 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001350
1351 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001352 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001353 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001354 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001355
stephen hemminger553675f2013-05-16 11:35:20 +00001356 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001357 spin_lock_init(&vxlan->hash_lock);
1358
1359 init_timer_deferrable(&vxlan->age_timer);
1360 vxlan->age_timer.function = vxlan_cleanup;
1361 vxlan->age_timer.data = (unsigned long) vxlan;
1362
stephen hemminger05f47d62012-10-09 20:35:50 +00001363 inet_get_local_port_range(&low, &high);
1364 vxlan->port_min = low;
1365 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001366 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001367
stephen hemmingerd3428942012-10-01 12:32:35 +00001368 vxlan->dev = dev;
1369
1370 for (h = 0; h < FDB_HASH_SIZE; ++h)
1371 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1372}
1373
1374static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1375 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001376 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001377 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1378 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1379 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1380 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1381 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1382 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1383 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001384 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001385 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1386 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1387 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1388 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001389 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001390};
1391
1392static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1393{
1394 if (tb[IFLA_ADDRESS]) {
1395 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1396 pr_debug("invalid link address (not ethernet)\n");
1397 return -EINVAL;
1398 }
1399
1400 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1401 pr_debug("invalid all zero ethernet address\n");
1402 return -EADDRNOTAVAIL;
1403 }
1404 }
1405
1406 if (!data)
1407 return -EINVAL;
1408
1409 if (data[IFLA_VXLAN_ID]) {
1410 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1411 if (id >= VXLAN_VID_MASK)
1412 return -ERANGE;
1413 }
1414
stephen hemminger05f47d62012-10-09 20:35:50 +00001415 if (data[IFLA_VXLAN_PORT_RANGE]) {
1416 const struct ifla_vxlan_port_range *p
1417 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1418
1419 if (ntohs(p->high) < ntohs(p->low)) {
1420 pr_debug("port range %u .. %u not valid\n",
1421 ntohs(p->low), ntohs(p->high));
1422 return -EINVAL;
1423 }
1424 }
1425
stephen hemmingerd3428942012-10-01 12:32:35 +00001426 return 0;
1427}
1428
Yan Burman1b13c972013-01-29 23:43:07 +00001429static void vxlan_get_drvinfo(struct net_device *netdev,
1430 struct ethtool_drvinfo *drvinfo)
1431{
1432 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1433 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1434}
1435
1436static const struct ethtool_ops vxlan_ethtool_ops = {
1437 .get_drvinfo = vxlan_get_drvinfo,
1438 .get_link = ethtool_op_get_link,
1439};
1440
stephen hemminger553675f2013-05-16 11:35:20 +00001441static void vxlan_del_work(struct work_struct *work)
1442{
1443 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1444
1445 sk_release_kernel(vs->sock->sk);
1446 kfree_rcu(vs, rcu);
1447}
1448
1449/* Create new listen socket if needed */
1450static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1451{
1452 struct vxlan_sock *vs;
1453 struct sock *sk;
1454 struct sockaddr_in vxlan_addr = {
1455 .sin_family = AF_INET,
1456 .sin_addr.s_addr = htonl(INADDR_ANY),
1457 };
1458 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001459 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001460
1461 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1462 if (!vs)
1463 return ERR_PTR(-ENOMEM);
1464
1465 for (h = 0; h < VNI_HASH_SIZE; ++h)
1466 INIT_HLIST_HEAD(&vs->vni_list[h]);
1467
1468 INIT_WORK(&vs->del_work, vxlan_del_work);
1469
1470 /* Create UDP socket for encapsulation receive. */
1471 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1472 if (rc < 0) {
1473 pr_debug("UDP socket create failed\n");
1474 kfree(vs);
1475 return ERR_PTR(rc);
1476 }
1477
1478 /* Put in proper namespace */
1479 sk = vs->sock->sk;
1480 sk_change_net(sk, net);
1481
1482 vxlan_addr.sin_port = port;
1483
1484 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1485 sizeof(vxlan_addr));
1486 if (rc < 0) {
1487 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1488 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1489 sk_release_kernel(sk);
1490 kfree(vs);
1491 return ERR_PTR(rc);
1492 }
1493
1494 /* Disable multicast loopback */
1495 inet_sk(sk)->mc_loop = 0;
1496
1497 /* Mark socket as an encapsulation socket. */
1498 udp_sk(sk)->encap_type = 1;
1499 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1500 udp_encap_enable();
1501
1502 vs->refcnt = 1;
1503 return vs;
1504}
1505
stephen hemmingerd3428942012-10-01 12:32:35 +00001506static int vxlan_newlink(struct net *net, struct net_device *dev,
1507 struct nlattr *tb[], struct nlattr *data[])
1508{
stephen hemminger553675f2013-05-16 11:35:20 +00001509 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001510 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001511 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger553675f2013-05-16 11:35:20 +00001512 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001513 __u32 vni;
1514 int err;
1515
1516 if (!data[IFLA_VXLAN_ID])
1517 return -EINVAL;
1518
1519 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001520 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001521
stephen hemminger5d174dd2013-04-27 11:31:55 +00001522 if (data[IFLA_VXLAN_GROUP])
1523 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001524
1525 if (data[IFLA_VXLAN_LOCAL])
1526 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1527
stephen hemminger34e02aa2012-10-09 20:35:53 +00001528 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001529 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001530 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001531 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001532
stephen hemminger34e02aa2012-10-09 20:35:53 +00001533 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001534 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001535 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001536 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001537
1538 if (!tb[IFLA_MTU])
1539 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001540
1541 /* update header length based on lower device */
1542 dev->hard_header_len = lowerdev->hard_header_len +
1543 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001544 }
1545
1546 if (data[IFLA_VXLAN_TOS])
1547 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1548
Vincent Bernatafb97182012-10-30 10:27:16 +00001549 if (data[IFLA_VXLAN_TTL])
1550 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1551
stephen hemmingerd3428942012-10-01 12:32:35 +00001552 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001553 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001554
1555 if (data[IFLA_VXLAN_AGEING])
1556 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1557 else
1558 vxlan->age_interval = FDB_AGE_DEFAULT;
1559
David Stevense4f67ad2012-11-20 02:50:14 +00001560 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1561 vxlan->flags |= VXLAN_F_PROXY;
1562
1563 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1564 vxlan->flags |= VXLAN_F_RSC;
1565
1566 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1567 vxlan->flags |= VXLAN_F_L2MISS;
1568
1569 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1570 vxlan->flags |= VXLAN_F_L3MISS;
1571
stephen hemmingerd3428942012-10-01 12:32:35 +00001572 if (data[IFLA_VXLAN_LIMIT])
1573 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1574
stephen hemminger05f47d62012-10-09 20:35:50 +00001575 if (data[IFLA_VXLAN_PORT_RANGE]) {
1576 const struct ifla_vxlan_port_range *p
1577 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1578 vxlan->port_min = ntohs(p->low);
1579 vxlan->port_max = ntohs(p->high);
1580 }
1581
stephen hemminger823aa872013-04-27 11:31:57 +00001582 if (data[IFLA_VXLAN_PORT])
1583 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1584
stephen hemminger553675f2013-05-16 11:35:20 +00001585 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1586 pr_info("duplicate VNI %u\n", vni);
1587 return -EEXIST;
1588 }
1589
1590 vs = vxlan_find_port(net, vxlan->dst_port);
1591 if (vs)
1592 ++vs->refcnt;
1593 else {
1594 /* Drop lock because socket create acquires RTNL lock */
1595 rtnl_unlock();
1596 vs = vxlan_socket_create(net, vxlan->dst_port);
1597 rtnl_lock();
1598 if (IS_ERR(vs))
1599 return PTR_ERR(vs);
1600
1601 hlist_add_head_rcu(&vs->hlist, vs_head(net, vxlan->dst_port));
1602 }
1603 vxlan->vn_sock = vs;
1604
Yan Burman1b13c972013-01-29 23:43:07 +00001605 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1606
stephen hemmingerd3428942012-10-01 12:32:35 +00001607 err = register_netdevice(dev);
stephen hemminger553675f2013-05-16 11:35:20 +00001608 if (err) {
1609 if (--vs->refcnt == 0) {
1610 rtnl_unlock();
1611 sk_release_kernel(vs->sock->sk);
1612 kfree(vs);
1613 rtnl_lock();
1614 }
1615 return err;
1616 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001617
stephen hemminger553675f2013-05-16 11:35:20 +00001618 list_add(&vxlan->next, &vn->vxlan_list);
1619 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1620
1621 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001622}
1623
1624static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1625{
1626 struct vxlan_dev *vxlan = netdev_priv(dev);
stephen hemminger553675f2013-05-16 11:35:20 +00001627 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001628
1629 hlist_del_rcu(&vxlan->hlist);
stephen hemminger553675f2013-05-16 11:35:20 +00001630 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001631 unregister_netdevice_queue(dev, head);
stephen hemminger553675f2013-05-16 11:35:20 +00001632
1633 if (--vs->refcnt == 0) {
1634 hlist_del_rcu(&vs->hlist);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001635 queue_work(vxlan_wq, &vs->del_work);
stephen hemminger553675f2013-05-16 11:35:20 +00001636 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001637}
1638
1639static size_t vxlan_get_size(const struct net_device *dev)
1640{
1641
1642 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001643 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001644 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1645 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1646 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1647 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1648 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001649 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1650 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1651 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1652 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001653 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1654 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001655 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001656 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001657 0;
1658}
1659
1660static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1661{
1662 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001663 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001664 struct ifla_vxlan_port_range ports = {
1665 .low = htons(vxlan->port_min),
1666 .high = htons(vxlan->port_max),
1667 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001668
Atzm Watanabec7995c42013-04-16 02:50:52 +00001669 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001670 goto nla_put_failure;
1671
stephen hemminger5d174dd2013-04-27 11:31:55 +00001672 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001673 goto nla_put_failure;
1674
Atzm Watanabec7995c42013-04-16 02:50:52 +00001675 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001676 goto nla_put_failure;
1677
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001678 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001679 goto nla_put_failure;
1680
1681 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1682 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001683 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1684 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1685 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1686 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1687 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1688 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1689 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1690 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1691 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001692 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001693 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1694 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001695 goto nla_put_failure;
1696
stephen hemminger05f47d62012-10-09 20:35:50 +00001697 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1698 goto nla_put_failure;
1699
stephen hemmingerd3428942012-10-01 12:32:35 +00001700 return 0;
1701
1702nla_put_failure:
1703 return -EMSGSIZE;
1704}
1705
1706static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1707 .kind = "vxlan",
1708 .maxtype = IFLA_VXLAN_MAX,
1709 .policy = vxlan_policy,
1710 .priv_size = sizeof(struct vxlan_dev),
1711 .setup = vxlan_setup,
1712 .validate = vxlan_validate,
1713 .newlink = vxlan_newlink,
1714 .dellink = vxlan_dellink,
1715 .get_size = vxlan_get_size,
1716 .fill_info = vxlan_fill_info,
1717};
1718
1719static __net_init int vxlan_init_net(struct net *net)
1720{
1721 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001722 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001723
stephen hemminger553675f2013-05-16 11:35:20 +00001724 INIT_LIST_HEAD(&vn->vxlan_list);
stephen hemmingerd3428942012-10-01 12:32:35 +00001725
stephen hemminger553675f2013-05-16 11:35:20 +00001726 for (h = 0; h < PORT_HASH_SIZE; ++h)
1727 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001728
1729 return 0;
1730}
1731
1732static __net_exit void vxlan_exit_net(struct net *net)
1733{
1734 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001735 struct vxlan_dev *vxlan;
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001736
1737 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001738 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1739 dev_close(vxlan->dev);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001740 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001741}
1742
1743static struct pernet_operations vxlan_net_ops = {
1744 .init = vxlan_init_net,
1745 .exit = vxlan_exit_net,
1746 .id = &vxlan_net_id,
1747 .size = sizeof(struct vxlan_net),
1748};
1749
1750static int __init vxlan_init_module(void)
1751{
1752 int rc;
1753
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001754 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1755 if (!vxlan_wq)
1756 return -ENOMEM;
1757
stephen hemmingerd3428942012-10-01 12:32:35 +00001758 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1759
1760 rc = register_pernet_device(&vxlan_net_ops);
1761 if (rc)
1762 goto out1;
1763
1764 rc = rtnl_link_register(&vxlan_link_ops);
1765 if (rc)
1766 goto out2;
1767
1768 return 0;
1769
1770out2:
1771 unregister_pernet_device(&vxlan_net_ops);
1772out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001773 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00001774 return rc;
1775}
Cong Wang7332a132013-05-27 22:35:53 +00001776late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00001777
1778static void __exit vxlan_cleanup_module(void)
1779{
stephen hemmingerd3428942012-10-01 12:32:35 +00001780 unregister_pernet_device(&vxlan_net_ops);
Stephen Hemmingerb7153982013-06-17 14:16:09 -07001781 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001782 destroy_workqueue(vxlan_wq);
David Stevens66817122013-03-15 04:35:51 +00001783 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001784}
1785module_exit(vxlan_cleanup_module);
1786
1787MODULE_LICENSE("GPL");
1788MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00001789MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00001790MODULE_ALIAS_RTNL_LINK("vxlan");