blob: 80d359c6310b0ef9a6725a00e0c23d68599bb081 [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 {
100 struct rcu_head rcu;
101 __be32 remote_ip;
102 __be16 remote_port;
103 u32 remote_vni;
104 u32 remote_ifindex;
105 struct vxlan_rdst *remote_next;
106};
107
stephen hemmingerd3428942012-10-01 12:32:35 +0000108/* Forwarding table entry */
109struct vxlan_fdb {
110 struct hlist_node hlist; /* linked list of entries */
111 struct rcu_head rcu;
112 unsigned long updated; /* jiffies */
113 unsigned long used;
David Stevens66817122013-03-15 04:35:51 +0000114 struct vxlan_rdst remote;
stephen hemmingerd3428942012-10-01 12:32:35 +0000115 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000116 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000117 u8 eth_addr[ETH_ALEN];
118};
119
stephen hemmingerd3428942012-10-01 12:32:35 +0000120/* Pseudo network device */
121struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000122 struct hlist_node hlist; /* vni hash table */
123 struct list_head next; /* vxlan's per namespace list */
124 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000125 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000126 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000127 __be32 saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000128 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000129 __u16 port_min; /* source port range */
130 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000131 __u8 tos; /* TOS override */
132 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000133 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000134
135 unsigned long age_interval;
136 struct timer_list age_timer;
137 spinlock_t hash_lock;
138 unsigned int addrcnt;
139 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000140
141 struct hlist_head fdb_head[FDB_HASH_SIZE];
142};
143
David Stevense4f67ad2012-11-20 02:50:14 +0000144#define VXLAN_F_LEARN 0x01
145#define VXLAN_F_PROXY 0x02
146#define VXLAN_F_RSC 0x04
147#define VXLAN_F_L2MISS 0x08
148#define VXLAN_F_L3MISS 0x10
149
stephen hemmingerd3428942012-10-01 12:32:35 +0000150/* salt for hash table */
151static u32 vxlan_salt __read_mostly;
152
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 */
343static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
344 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
David Stevens66817122013-03-15 04:35:51 +0000358/* Add/update destinations for multicast */
359static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000360 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000361{
362 struct vxlan_rdst *rd_prev, *rd;
363
364 rd_prev = NULL;
365 for (rd = &f->remote; rd; rd = rd->remote_next) {
366 if (rd->remote_ip == ip &&
367 rd->remote_port == port &&
368 rd->remote_vni == vni &&
369 rd->remote_ifindex == ifindex)
370 return 0;
371 rd_prev = rd;
372 }
373 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
374 if (rd == NULL)
375 return -ENOBUFS;
376 rd->remote_ip = ip;
377 rd->remote_port = port;
378 rd->remote_vni = vni;
379 rd->remote_ifindex = ifindex;
380 rd->remote_next = NULL;
381 rd_prev->remote_next = rd;
382 return 1;
383}
384
stephen hemmingerd3428942012-10-01 12:32:35 +0000385/* Add new entry to forwarding table -- assumes lock held */
386static int vxlan_fdb_create(struct vxlan_dev *vxlan,
387 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000388 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000389 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000390 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000391{
392 struct vxlan_fdb *f;
393 int notify = 0;
394
395 f = vxlan_find_mac(vxlan, mac);
396 if (f) {
397 if (flags & NLM_F_EXCL) {
398 netdev_dbg(vxlan->dev,
399 "lost race to create %pM\n", mac);
400 return -EEXIST;
401 }
402 if (f->state != state) {
403 f->state = state;
404 f->updated = jiffies;
405 notify = 1;
406 }
David Stevensae884082013-04-19 00:36:26 +0000407 if (f->flags != ndm_flags) {
408 f->flags = ndm_flags;
409 f->updated = jiffies;
410 notify = 1;
411 }
David Stevens66817122013-03-15 04:35:51 +0000412 if ((flags & NLM_F_APPEND) &&
413 is_multicast_ether_addr(f->eth_addr)) {
414 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
415
416 if (rc < 0)
417 return rc;
418 notify |= rc;
419 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000420 } else {
421 if (!(flags & NLM_F_CREATE))
422 return -ENOENT;
423
424 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
425 return -ENOSPC;
426
427 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
428 f = kmalloc(sizeof(*f), GFP_ATOMIC);
429 if (!f)
430 return -ENOMEM;
431
432 notify = 1;
David Stevens66817122013-03-15 04:35:51 +0000433 f->remote.remote_ip = ip;
434 f->remote.remote_port = port;
435 f->remote.remote_vni = vni;
436 f->remote.remote_ifindex = ifindex;
437 f->remote.remote_next = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000438 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000439 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000440 f->updated = f->used = jiffies;
441 memcpy(f->eth_addr, mac, ETH_ALEN);
442
443 ++vxlan->addrcnt;
444 hlist_add_head_rcu(&f->hlist,
445 vxlan_fdb_head(vxlan, mac));
446 }
447
448 if (notify)
449 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
450
451 return 0;
452}
453
Wei Yongjun6706c822013-04-11 19:00:35 +0000454static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000455{
456 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
457
458 while (f->remote.remote_next) {
459 struct vxlan_rdst *rd = f->remote.remote_next;
460
461 f->remote.remote_next = rd->remote_next;
462 kfree(rd);
463 }
464 kfree(f);
465}
466
stephen hemmingerd3428942012-10-01 12:32:35 +0000467static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
468{
469 netdev_dbg(vxlan->dev,
470 "delete %pM\n", f->eth_addr);
471
472 --vxlan->addrcnt;
473 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
474
475 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000476 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000477}
478
479/* Add static entry (via netlink) */
480static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
481 struct net_device *dev,
482 const unsigned char *addr, u16 flags)
483{
484 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens66817122013-03-15 04:35:51 +0000485 struct net *net = dev_net(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000486 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000487 __be16 port;
488 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000489 int err;
490
491 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
492 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
493 ndm->ndm_state);
494 return -EINVAL;
495 }
496
497 if (tb[NDA_DST] == NULL)
498 return -EINVAL;
499
500 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
501 return -EAFNOSUPPORT;
502
503 ip = nla_get_be32(tb[NDA_DST]);
504
David Stevens66817122013-03-15 04:35:51 +0000505 if (tb[NDA_PORT]) {
stephen hemminger73cf3312013-04-27 11:31:54 +0000506 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
David Stevens66817122013-03-15 04:35:51 +0000507 return -EINVAL;
stephen hemminger73cf3312013-04-27 11:31:54 +0000508 port = nla_get_be16(tb[NDA_PORT]);
David Stevens66817122013-03-15 04:35:51 +0000509 } else
stephen hemminger823aa872013-04-27 11:31:57 +0000510 port = vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +0000511
512 if (tb[NDA_VNI]) {
513 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
514 return -EINVAL;
515 vni = nla_get_u32(tb[NDA_VNI]);
516 } else
Atzm Watanabec7995c42013-04-16 02:50:52 +0000517 vni = vxlan->default_dst.remote_vni;
David Stevens66817122013-03-15 04:35:51 +0000518
519 if (tb[NDA_IFINDEX]) {
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000520 struct net_device *tdev;
David Stevens66817122013-03-15 04:35:51 +0000521
522 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
523 return -EINVAL;
524 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000525 tdev = dev_get_by_index(net, ifindex);
526 if (!tdev)
David Stevens66817122013-03-15 04:35:51 +0000527 return -EADDRNOTAVAIL;
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000528 dev_put(tdev);
David Stevens66817122013-03-15 04:35:51 +0000529 } else
530 ifindex = 0;
531
stephen hemmingerd3428942012-10-01 12:32:35 +0000532 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000533 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
534 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000535 spin_unlock_bh(&vxlan->hash_lock);
536
537 return err;
538}
539
540/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000541static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
542 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000543 const unsigned char *addr)
544{
545 struct vxlan_dev *vxlan = netdev_priv(dev);
546 struct vxlan_fdb *f;
547 int err = -ENOENT;
548
549 spin_lock_bh(&vxlan->hash_lock);
550 f = vxlan_find_mac(vxlan, addr);
551 if (f) {
552 vxlan_fdb_destroy(vxlan, f);
553 err = 0;
554 }
555 spin_unlock_bh(&vxlan->hash_lock);
556
557 return err;
558}
559
560/* Dump forwarding table */
561static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
562 struct net_device *dev, int idx)
563{
564 struct vxlan_dev *vxlan = netdev_priv(dev);
565 unsigned int h;
566
567 for (h = 0; h < FDB_HASH_SIZE; ++h) {
568 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000569 int err;
570
Sasha Levinb67bfe02013-02-27 17:06:00 -0800571 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000572 struct vxlan_rdst *rd;
573 for (rd = &f->remote; rd; rd = rd->remote_next) {
574 if (idx < cb->args[0])
575 goto skip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000576
David Stevens66817122013-03-15 04:35:51 +0000577 err = vxlan_fdb_info(skb, vxlan, f,
578 NETLINK_CB(cb->skb).portid,
579 cb->nlh->nlmsg_seq,
580 RTM_NEWNEIGH,
581 NLM_F_MULTI, rd);
582 if (err < 0)
583 break;
stephen hemmingerd3428942012-10-01 12:32:35 +0000584skip:
David Stevens66817122013-03-15 04:35:51 +0000585 ++idx;
586 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000587 }
588 }
589
590 return idx;
591}
592
593/* Watch incoming packets to learn mapping between Ethernet address
594 * and Tunnel endpoint.
595 */
596static void vxlan_snoop(struct net_device *dev,
597 __be32 src_ip, const u8 *src_mac)
598{
599 struct vxlan_dev *vxlan = netdev_priv(dev);
600 struct vxlan_fdb *f;
601 int err;
602
603 f = vxlan_find_mac(vxlan, src_mac);
604 if (likely(f)) {
605 f->used = jiffies;
David Stevens66817122013-03-15 04:35:51 +0000606 if (likely(f->remote.remote_ip == src_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +0000607 return;
608
609 if (net_ratelimit())
610 netdev_info(dev,
611 "%pM migrated from %pI4 to %pI4\n",
David Stevens66817122013-03-15 04:35:51 +0000612 src_mac, &f->remote.remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000613
David Stevens66817122013-03-15 04:35:51 +0000614 f->remote.remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000615 f->updated = jiffies;
616 } else {
617 /* learned new entry */
618 spin_lock(&vxlan->hash_lock);
619 err = vxlan_fdb_create(vxlan, src_mac, src_ip,
620 NUD_REACHABLE,
David Stevens66817122013-03-15 04:35:51 +0000621 NLM_F_EXCL|NLM_F_CREATE,
stephen hemminger823aa872013-04-27 11:31:57 +0000622 vxlan->dst_port,
David Stevensae884082013-04-19 00:36:26 +0000623 vxlan->default_dst.remote_vni,
624 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000625 spin_unlock(&vxlan->hash_lock);
626 }
627}
628
629
630/* See if multicast group is already in use by other ID */
631static bool vxlan_group_used(struct vxlan_net *vn,
632 const struct vxlan_dev *this)
633{
stephen hemminger553675f2013-05-16 11:35:20 +0000634 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000635
stephen hemminger553675f2013-05-16 11:35:20 +0000636 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
637 if (vxlan == this)
638 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000639
stephen hemminger553675f2013-05-16 11:35:20 +0000640 if (!netif_running(vxlan->dev))
641 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000642
stephen hemminger553675f2013-05-16 11:35:20 +0000643 if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
644 return true;
645 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000646
647 return false;
648}
649
650/* kernel equivalent to IP_ADD_MEMBERSHIP */
651static int vxlan_join_group(struct net_device *dev)
652{
653 struct vxlan_dev *vxlan = netdev_priv(dev);
654 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemminger553675f2013-05-16 11:35:20 +0000655 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000656 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000657 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
658 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000659 };
660 int err;
661
662 /* Already a member of group */
663 if (vxlan_group_used(vn, vxlan))
664 return 0;
665
666 /* Need to drop RTNL to call multicast join */
667 rtnl_unlock();
668 lock_sock(sk);
669 err = ip_mc_join_group(sk, &mreq);
670 release_sock(sk);
671 rtnl_lock();
672
673 return err;
674}
675
676
677/* kernel equivalent to IP_DROP_MEMBERSHIP */
678static int vxlan_leave_group(struct net_device *dev)
679{
680 struct vxlan_dev *vxlan = netdev_priv(dev);
681 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
682 int err = 0;
stephen hemminger553675f2013-05-16 11:35:20 +0000683 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000684 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000685 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
686 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000687 };
688
689 /* Only leave group when last vxlan is done. */
690 if (vxlan_group_used(vn, vxlan))
691 return 0;
692
693 /* Need to drop RTNL to call multicast leave */
694 rtnl_unlock();
695 lock_sock(sk);
696 err = ip_mc_leave_group(sk, &mreq);
697 release_sock(sk);
698 rtnl_lock();
699
700 return err;
701}
702
703/* Callback from net/ipv4/udp.c to receive packets */
704static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
705{
706 struct iphdr *oip;
707 struct vxlanhdr *vxh;
708 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000709 struct pcpu_tstats *stats;
stephen hemminger553675f2013-05-16 11:35:20 +0000710 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000711 __u32 vni;
712 int err;
713
714 /* pop off outer UDP header */
715 __skb_pull(skb, sizeof(struct udphdr));
716
717 /* Need Vxlan and inner Ethernet header to be present */
718 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
719 goto error;
720
721 /* Drop packets with reserved bits set */
722 vxh = (struct vxlanhdr *) skb->data;
723 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
724 (vxh->vx_vni & htonl(0xff))) {
725 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
726 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
727 goto error;
728 }
729
730 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000731
732 /* Is this VNI defined? */
733 vni = ntohl(vxh->vx_vni) >> 8;
stephen hemminger553675f2013-05-16 11:35:20 +0000734 port = inet_sk(sk)->inet_sport;
735 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000736 if (!vxlan) {
stephen hemminger553675f2013-05-16 11:35:20 +0000737 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
738 vni, ntohs(port));
stephen hemmingerd3428942012-10-01 12:32:35 +0000739 goto drop;
740 }
741
742 if (!pskb_may_pull(skb, ETH_HLEN)) {
743 vxlan->dev->stats.rx_length_errors++;
744 vxlan->dev->stats.rx_errors++;
745 goto drop;
746 }
747
David Stevense4f67ad2012-11-20 02:50:14 +0000748 skb_reset_mac_header(skb);
749
stephen hemmingerd3428942012-10-01 12:32:35 +0000750 /* Re-examine inner Ethernet packet */
751 oip = ip_hdr(skb);
752 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000753
754 /* Ignore packet loops (and multicast echo) */
755 if (compare_ether_addr(eth_hdr(skb)->h_source,
756 vxlan->dev->dev_addr) == 0)
757 goto drop;
758
David Stevense4f67ad2012-11-20 02:50:14 +0000759 if (vxlan->flags & VXLAN_F_LEARN)
stephen hemmingerd3428942012-10-01 12:32:35 +0000760 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
761
762 __skb_tunnel_rx(skb, vxlan->dev);
763 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000764
765 /* If the NIC driver gave us an encapsulated packet with
766 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
767 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
768 * for us. Otherwise force the upper layers to verify it.
769 */
770 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
771 !(vxlan->dev->features & NETIF_F_RXCSUM))
772 skb->ip_summed = CHECKSUM_NONE;
773
774 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000775
776 err = IP_ECN_decapsulate(oip, skb);
777 if (unlikely(err)) {
778 if (log_ecn_error)
779 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
780 &oip->saddr, oip->tos);
781 if (err > 1) {
782 ++vxlan->dev->stats.rx_frame_errors;
783 ++vxlan->dev->stats.rx_errors;
784 goto drop;
785 }
786 }
787
Pravin B Shelare8171042013-03-25 14:49:46 +0000788 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000789 u64_stats_update_begin(&stats->syncp);
790 stats->rx_packets++;
791 stats->rx_bytes += skb->len;
792 u64_stats_update_end(&stats->syncp);
793
794 netif_rx(skb);
795
796 return 0;
797error:
798 /* Put UDP header back */
799 __skb_push(skb, sizeof(struct udphdr));
800
801 return 1;
802drop:
803 /* Consume bad packet */
804 kfree_skb(skb);
805 return 0;
806}
807
David Stevense4f67ad2012-11-20 02:50:14 +0000808static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
809{
810 struct vxlan_dev *vxlan = netdev_priv(dev);
811 struct arphdr *parp;
812 u8 *arpptr, *sha;
813 __be32 sip, tip;
814 struct neighbour *n;
815
816 if (dev->flags & IFF_NOARP)
817 goto out;
818
819 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
820 dev->stats.tx_dropped++;
821 goto out;
822 }
823 parp = arp_hdr(skb);
824
825 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
826 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
827 parp->ar_pro != htons(ETH_P_IP) ||
828 parp->ar_op != htons(ARPOP_REQUEST) ||
829 parp->ar_hln != dev->addr_len ||
830 parp->ar_pln != 4)
831 goto out;
832 arpptr = (u8 *)parp + sizeof(struct arphdr);
833 sha = arpptr;
834 arpptr += dev->addr_len; /* sha */
835 memcpy(&sip, arpptr, sizeof(sip));
836 arpptr += sizeof(sip);
837 arpptr += dev->addr_len; /* tha */
838 memcpy(&tip, arpptr, sizeof(tip));
839
840 if (ipv4_is_loopback(tip) ||
841 ipv4_is_multicast(tip))
842 goto out;
843
844 n = neigh_lookup(&arp_tbl, &tip, dev);
845
846 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000847 struct vxlan_fdb *f;
848 struct sk_buff *reply;
849
850 if (!(n->nud_state & NUD_CONNECTED)) {
851 neigh_release(n);
852 goto out;
853 }
854
855 f = vxlan_find_mac(vxlan, n->ha);
David Stevens66817122013-03-15 04:35:51 +0000856 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000857 /* bridge-local neighbor */
858 neigh_release(n);
859 goto out;
860 }
861
862 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
863 n->ha, sha);
864
865 neigh_release(n);
866
867 skb_reset_mac_header(reply);
868 __skb_pull(reply, skb_network_offset(reply));
869 reply->ip_summed = CHECKSUM_UNNECESSARY;
870 reply->pkt_type = PACKET_HOST;
871
872 if (netif_rx_ni(reply) == NET_RX_DROP)
873 dev->stats.rx_dropped++;
874 } else if (vxlan->flags & VXLAN_F_L3MISS)
875 vxlan_ip_miss(dev, tip);
876out:
877 consume_skb(skb);
878 return NETDEV_TX_OK;
879}
880
881static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
882{
883 struct vxlan_dev *vxlan = netdev_priv(dev);
884 struct neighbour *n;
885 struct iphdr *pip;
886
887 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
888 return false;
889
890 n = NULL;
891 switch (ntohs(eth_hdr(skb)->h_proto)) {
892 case ETH_P_IP:
893 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
894 return false;
895 pip = ip_hdr(skb);
896 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
897 break;
898 default:
899 return false;
900 }
901
902 if (n) {
903 bool diff;
904
905 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
906 if (diff) {
907 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
908 dev->addr_len);
909 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
910 }
911 neigh_release(n);
912 return diff;
913 } else if (vxlan->flags & VXLAN_F_L3MISS)
914 vxlan_ip_miss(dev, pip->daddr);
915 return false;
916}
917
stephen hemminger553675f2013-05-16 11:35:20 +0000918static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +0000919{
920 sock_put(skb->sk);
921}
922
923/* On transmit, associate with the tunnel socket */
924static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
925{
stephen hemminger553675f2013-05-16 11:35:20 +0000926 struct vxlan_dev *vxlan = netdev_priv(dev);
927 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +0000928
929 skb_orphan(skb);
930 sock_hold(sk);
931 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +0000932 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +0000933}
934
stephen hemminger05f47d62012-10-09 20:35:50 +0000935/* Compute source port for outgoing packet
936 * first choice to use L4 flow hash since it will spread
937 * better and maybe available from hardware
938 * secondary choice is to use jhash on the Ethernet header
939 */
stephen hemminger7d836a72013-04-27 11:31:56 +0000940static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +0000941{
942 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
943 u32 hash;
944
945 hash = skb_get_rxhash(skb);
946 if (!hash)
947 hash = jhash(skb->data, 2 * ETH_ALEN,
948 (__force u32) skb->protocol);
949
stephen hemminger7d836a72013-04-27 11:31:56 +0000950 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +0000951}
952
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000953static int handle_offloads(struct sk_buff *skb)
954{
955 if (skb_is_gso(skb)) {
956 int err = skb_unclone(skb, GFP_ATOMIC);
957 if (unlikely(err))
958 return err;
959
Dmitry Kravkovf6ace502013-04-28 08:16:01 +0000960 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000961 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
962 skb->ip_summed = CHECKSUM_NONE;
963
964 return 0;
965}
966
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000967/* Bypass encapsulation if the destination is local */
968static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
969 struct vxlan_dev *dst_vxlan)
970{
971 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
972 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
973
974 skb->pkt_type = PACKET_HOST;
975 skb->encapsulation = 0;
976 skb->dev = dst_vxlan->dev;
977 __skb_pull(skb, skb_network_offset(skb));
978
979 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +0000980 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
981 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000982
983 u64_stats_update_begin(&tx_stats->syncp);
984 tx_stats->tx_packets++;
985 tx_stats->tx_bytes += skb->len;
986 u64_stats_update_end(&tx_stats->syncp);
987
988 if (netif_rx(skb) == NET_RX_SUCCESS) {
989 u64_stats_update_begin(&rx_stats->syncp);
990 rx_stats->rx_packets++;
991 rx_stats->rx_bytes += skb->len;
992 u64_stats_update_end(&rx_stats->syncp);
993 } else {
994 skb->dev->stats.rx_dropped++;
995 }
996}
997
David Stevens66817122013-03-15 04:35:51 +0000998static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
999 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001000{
1001 struct vxlan_dev *vxlan = netdev_priv(dev);
1002 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001003 const struct iphdr *old_iph;
1004 struct iphdr *iph;
1005 struct vxlanhdr *vxh;
1006 struct udphdr *uh;
1007 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001008 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001009 __be16 src_port, dst_port;
David Stevens66817122013-03-15 04:35:51 +00001010 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001011 __be16 df = 0;
1012 __u8 tos, ttl;
stephen hemmingerd3428942012-10-01 12:32:35 +00001013
stephen hemminger823aa872013-04-27 11:31:57 +00001014 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001015 vni = rdst->remote_vni;
1016 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001017
1018 if (!dst) {
1019 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001020 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001021 vxlan_encap_bypass(skb, vxlan, vxlan);
David Stevense4f67ad2012-11-20 02:50:14 +00001022 return NETDEV_TX_OK;
1023 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001024 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001025 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001026
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001027 if (!skb->encapsulation) {
1028 skb_reset_inner_headers(skb);
1029 skb->encapsulation = 1;
1030 }
1031
stephen hemmingerd3428942012-10-01 12:32:35 +00001032 /* Need space for new headers (invalidates iph ptr) */
1033 if (skb_cow_head(skb, VXLAN_HEADROOM))
1034 goto drop;
1035
stephen hemmingerd3428942012-10-01 12:32:35 +00001036 old_iph = ip_hdr(skb);
1037
stephen hemmingerd3428942012-10-01 12:32:35 +00001038 ttl = vxlan->ttl;
1039 if (!ttl && IN_MULTICAST(ntohl(dst)))
1040 ttl = 1;
1041
1042 tos = vxlan->tos;
1043 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001044 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001045
stephen hemminger05f47d62012-10-09 20:35:50 +00001046 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001047
stephen hemmingerca78f182012-10-09 20:35:48 +00001048 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001049 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001050 fl4.flowi4_tos = RT_TOS(tos);
1051 fl4.daddr = dst;
1052 fl4.saddr = vxlan->saddr;
1053
1054 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001055 if (IS_ERR(rt)) {
1056 netdev_dbg(dev, "no route to %pI4\n", &dst);
1057 dev->stats.tx_carrier_errors++;
1058 goto tx_error;
1059 }
1060
1061 if (rt->dst.dev == dev) {
1062 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1063 ip_rt_put(rt);
1064 dev->stats.collisions++;
1065 goto tx_error;
1066 }
1067
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001068 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001069 if (rt->rt_flags & RTCF_LOCAL &&
1070 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001071 struct vxlan_dev *dst_vxlan;
1072
1073 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001074 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001075 if (!dst_vxlan)
1076 goto tx_error;
1077 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1078 return NETDEV_TX_OK;
1079 }
1080
stephen hemmingerd3428942012-10-01 12:32:35 +00001081 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1082 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1083 IPSKB_REROUTED);
1084 skb_dst_drop(skb);
1085 skb_dst_set(skb, &rt->dst);
1086
1087 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1088 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001089 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001090
1091 __skb_push(skb, sizeof(*uh));
1092 skb_reset_transport_header(skb);
1093 uh = udp_hdr(skb);
1094
stephen hemminger73cf3312013-04-27 11:31:54 +00001095 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001096 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001097
1098 uh->len = htons(skb->len);
1099 uh->check = 0;
1100
1101 __skb_push(skb, sizeof(*iph));
1102 skb_reset_network_header(skb);
1103 iph = ip_hdr(skb);
1104 iph->version = 4;
1105 iph->ihl = sizeof(struct iphdr) >> 2;
1106 iph->frag_off = df;
1107 iph->protocol = IPPROTO_UDP;
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001108 iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
stephen hemmingerca78f182012-10-09 20:35:48 +00001109 iph->daddr = dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001110 iph->saddr = fl4.saddr;
1111 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar8dc98eb2013-02-22 07:30:40 +00001112 tunnel_ip_select_ident(skb, old_iph, &rt->dst);
stephen hemmingerd3428942012-10-01 12:32:35 +00001113
Zang MingJie88c4c062013-03-04 06:07:34 +00001114 nf_reset(skb);
1115
stephen hemminger1cad8712012-10-09 20:35:49 +00001116 vxlan_set_owner(dev, skb);
1117
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001118 if (handle_offloads(skb))
1119 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001120
Cong Wang6aed0c82013-03-09 16:38:39 +00001121 iptunnel_xmit(skb, dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001122 return NETDEV_TX_OK;
1123
1124drop:
1125 dev->stats.tx_dropped++;
1126 goto tx_free;
1127
1128tx_error:
1129 dev->stats.tx_errors++;
1130tx_free:
1131 dev_kfree_skb(skb);
1132 return NETDEV_TX_OK;
1133}
1134
David Stevens66817122013-03-15 04:35:51 +00001135/* Transmit local packets over Vxlan
1136 *
1137 * Outer IP header inherits ECN and DF from inner header.
1138 * Outer UDP destination is the VXLAN assigned port.
1139 * source port is based on hash of flow
1140 */
1141static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1142{
1143 struct vxlan_dev *vxlan = netdev_priv(dev);
1144 struct ethhdr *eth;
1145 bool did_rsc = false;
Atzm Watanabec7995c42013-04-16 02:50:52 +00001146 struct vxlan_rdst *rdst0, *rdst;
David Stevens66817122013-03-15 04:35:51 +00001147 struct vxlan_fdb *f;
1148 int rc1, rc;
1149
1150 skb_reset_mac_header(skb);
1151 eth = eth_hdr(skb);
1152
1153 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1154 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001155
1156 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001157 did_rsc = false;
1158
1159 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1160 ntohs(eth->h_proto) == ETH_P_IP) {
1161 did_rsc = route_shortcircuit(dev, skb);
1162 if (did_rsc)
1163 f = vxlan_find_mac(vxlan, eth->h_dest);
1164 }
1165
David Stevens66817122013-03-15 04:35:51 +00001166 if (f == NULL) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001167 rdst0 = &vxlan->default_dst;
David Stevens66817122013-03-15 04:35:51 +00001168
Atzm Watanabec7995c42013-04-16 02:50:52 +00001169 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
David Stevens66817122013-03-15 04:35:51 +00001170 (vxlan->flags & VXLAN_F_L2MISS) &&
1171 !is_multicast_ether_addr(eth->h_dest))
1172 vxlan_fdb_miss(vxlan, eth->h_dest);
1173 } else
1174 rdst0 = &f->remote;
1175
1176 rc = NETDEV_TX_OK;
1177
1178 /* if there are multiple destinations, send copies */
1179 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1180 struct sk_buff *skb1;
1181
1182 skb1 = skb_clone(skb, GFP_ATOMIC);
1183 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1184 if (rc == NETDEV_TX_OK)
1185 rc = rc1;
1186 }
1187
1188 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1189 if (rc == NETDEV_TX_OK)
1190 rc = rc1;
1191 return rc;
1192}
1193
stephen hemmingerd3428942012-10-01 12:32:35 +00001194/* Walk the forwarding table and purge stale entries */
1195static void vxlan_cleanup(unsigned long arg)
1196{
1197 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1198 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1199 unsigned int h;
1200
1201 if (!netif_running(vxlan->dev))
1202 return;
1203
1204 spin_lock_bh(&vxlan->hash_lock);
1205 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1206 struct hlist_node *p, *n;
1207 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1208 struct vxlan_fdb *f
1209 = container_of(p, struct vxlan_fdb, hlist);
1210 unsigned long timeout;
1211
stephen hemminger3c172862012-10-26 06:24:34 +00001212 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001213 continue;
1214
1215 timeout = f->used + vxlan->age_interval * HZ;
1216 if (time_before_eq(timeout, jiffies)) {
1217 netdev_dbg(vxlan->dev,
1218 "garbage collect %pM\n",
1219 f->eth_addr);
1220 f->state = NUD_STALE;
1221 vxlan_fdb_destroy(vxlan, f);
1222 } else if (time_before(timeout, next_timer))
1223 next_timer = timeout;
1224 }
1225 }
1226 spin_unlock_bh(&vxlan->hash_lock);
1227
1228 mod_timer(&vxlan->age_timer, next_timer);
1229}
1230
1231/* Setup stats when device is created */
1232static int vxlan_init(struct net_device *dev)
1233{
Pravin B Shelare8171042013-03-25 14:49:46 +00001234 dev->tstats = alloc_percpu(struct pcpu_tstats);
1235 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001236 return -ENOMEM;
1237
1238 return 0;
1239}
1240
1241/* Start ageing timer and join group when device is brought up */
1242static int vxlan_open(struct net_device *dev)
1243{
1244 struct vxlan_dev *vxlan = netdev_priv(dev);
1245 int err;
1246
Atzm Watanabec7995c42013-04-16 02:50:52 +00001247 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
stephen hemmingerd3428942012-10-01 12:32:35 +00001248 err = vxlan_join_group(dev);
1249 if (err)
1250 return err;
1251 }
1252
1253 if (vxlan->age_interval)
1254 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1255
1256 return 0;
1257}
1258
1259/* Purge the forwarding table */
1260static void vxlan_flush(struct vxlan_dev *vxlan)
1261{
1262 unsigned h;
1263
1264 spin_lock_bh(&vxlan->hash_lock);
1265 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1266 struct hlist_node *p, *n;
1267 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1268 struct vxlan_fdb *f
1269 = container_of(p, struct vxlan_fdb, hlist);
1270 vxlan_fdb_destroy(vxlan, f);
1271 }
1272 }
1273 spin_unlock_bh(&vxlan->hash_lock);
1274}
1275
1276/* Cleanup timer and forwarding table on shutdown */
1277static int vxlan_stop(struct net_device *dev)
1278{
1279 struct vxlan_dev *vxlan = netdev_priv(dev);
1280
Atzm Watanabec7995c42013-04-16 02:50:52 +00001281 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001282 vxlan_leave_group(dev);
1283
1284 del_timer_sync(&vxlan->age_timer);
1285
1286 vxlan_flush(vxlan);
1287
1288 return 0;
1289}
1290
stephen hemmingerd3428942012-10-01 12:32:35 +00001291/* Stub, nothing needs to be done. */
1292static void vxlan_set_multicast_list(struct net_device *dev)
1293{
1294}
1295
1296static const struct net_device_ops vxlan_netdev_ops = {
1297 .ndo_init = vxlan_init,
1298 .ndo_open = vxlan_open,
1299 .ndo_stop = vxlan_stop,
1300 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001301 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001302 .ndo_set_rx_mode = vxlan_set_multicast_list,
1303 .ndo_change_mtu = eth_change_mtu,
1304 .ndo_validate_addr = eth_validate_addr,
1305 .ndo_set_mac_address = eth_mac_addr,
1306 .ndo_fdb_add = vxlan_fdb_add,
1307 .ndo_fdb_del = vxlan_fdb_delete,
1308 .ndo_fdb_dump = vxlan_fdb_dump,
1309};
1310
1311/* Info for udev, that this is a virtual tunnel endpoint */
1312static struct device_type vxlan_type = {
1313 .name = "vxlan",
1314};
1315
1316static void vxlan_free(struct net_device *dev)
1317{
Pravin B Shelare8171042013-03-25 14:49:46 +00001318 free_percpu(dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001319 free_netdev(dev);
1320}
1321
1322/* Initialize the device structure. */
1323static void vxlan_setup(struct net_device *dev)
1324{
1325 struct vxlan_dev *vxlan = netdev_priv(dev);
1326 unsigned h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001327 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001328
1329 eth_hw_addr_random(dev);
1330 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001331 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001332
1333 dev->netdev_ops = &vxlan_netdev_ops;
1334 dev->destructor = vxlan_free;
1335 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1336
1337 dev->tx_queue_len = 0;
1338 dev->features |= NETIF_F_LLTX;
1339 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001340 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001341 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001342 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001343
1344 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001345 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001346 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001347 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001348
stephen hemminger553675f2013-05-16 11:35:20 +00001349 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001350 spin_lock_init(&vxlan->hash_lock);
1351
1352 init_timer_deferrable(&vxlan->age_timer);
1353 vxlan->age_timer.function = vxlan_cleanup;
1354 vxlan->age_timer.data = (unsigned long) vxlan;
1355
stephen hemminger05f47d62012-10-09 20:35:50 +00001356 inet_get_local_port_range(&low, &high);
1357 vxlan->port_min = low;
1358 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001359 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001360
stephen hemmingerd3428942012-10-01 12:32:35 +00001361 vxlan->dev = dev;
1362
1363 for (h = 0; h < FDB_HASH_SIZE; ++h)
1364 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1365}
1366
1367static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1368 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001369 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001370 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1371 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1372 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1373 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1374 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1375 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1376 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001377 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001378 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1379 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1380 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1381 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001382 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001383};
1384
1385static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1386{
1387 if (tb[IFLA_ADDRESS]) {
1388 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1389 pr_debug("invalid link address (not ethernet)\n");
1390 return -EINVAL;
1391 }
1392
1393 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1394 pr_debug("invalid all zero ethernet address\n");
1395 return -EADDRNOTAVAIL;
1396 }
1397 }
1398
1399 if (!data)
1400 return -EINVAL;
1401
1402 if (data[IFLA_VXLAN_ID]) {
1403 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1404 if (id >= VXLAN_VID_MASK)
1405 return -ERANGE;
1406 }
1407
stephen hemminger05f47d62012-10-09 20:35:50 +00001408 if (data[IFLA_VXLAN_PORT_RANGE]) {
1409 const struct ifla_vxlan_port_range *p
1410 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1411
1412 if (ntohs(p->high) < ntohs(p->low)) {
1413 pr_debug("port range %u .. %u not valid\n",
1414 ntohs(p->low), ntohs(p->high));
1415 return -EINVAL;
1416 }
1417 }
1418
stephen hemmingerd3428942012-10-01 12:32:35 +00001419 return 0;
1420}
1421
Yan Burman1b13c972013-01-29 23:43:07 +00001422static void vxlan_get_drvinfo(struct net_device *netdev,
1423 struct ethtool_drvinfo *drvinfo)
1424{
1425 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1426 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1427}
1428
1429static const struct ethtool_ops vxlan_ethtool_ops = {
1430 .get_drvinfo = vxlan_get_drvinfo,
1431 .get_link = ethtool_op_get_link,
1432};
1433
stephen hemminger553675f2013-05-16 11:35:20 +00001434static void vxlan_del_work(struct work_struct *work)
1435{
1436 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1437
1438 sk_release_kernel(vs->sock->sk);
1439 kfree_rcu(vs, rcu);
1440}
1441
1442/* Create new listen socket if needed */
1443static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1444{
1445 struct vxlan_sock *vs;
1446 struct sock *sk;
1447 struct sockaddr_in vxlan_addr = {
1448 .sin_family = AF_INET,
1449 .sin_addr.s_addr = htonl(INADDR_ANY),
1450 };
1451 int rc;
1452 unsigned h;
1453
1454 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1455 if (!vs)
1456 return ERR_PTR(-ENOMEM);
1457
1458 for (h = 0; h < VNI_HASH_SIZE; ++h)
1459 INIT_HLIST_HEAD(&vs->vni_list[h]);
1460
1461 INIT_WORK(&vs->del_work, vxlan_del_work);
1462
1463 /* Create UDP socket for encapsulation receive. */
1464 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1465 if (rc < 0) {
1466 pr_debug("UDP socket create failed\n");
1467 kfree(vs);
1468 return ERR_PTR(rc);
1469 }
1470
1471 /* Put in proper namespace */
1472 sk = vs->sock->sk;
1473 sk_change_net(sk, net);
1474
1475 vxlan_addr.sin_port = port;
1476
1477 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1478 sizeof(vxlan_addr));
1479 if (rc < 0) {
1480 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1481 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1482 sk_release_kernel(sk);
1483 kfree(vs);
1484 return ERR_PTR(rc);
1485 }
1486
1487 /* Disable multicast loopback */
1488 inet_sk(sk)->mc_loop = 0;
1489
1490 /* Mark socket as an encapsulation socket. */
1491 udp_sk(sk)->encap_type = 1;
1492 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1493 udp_encap_enable();
1494
1495 vs->refcnt = 1;
1496 return vs;
1497}
1498
stephen hemmingerd3428942012-10-01 12:32:35 +00001499static int vxlan_newlink(struct net *net, struct net_device *dev,
1500 struct nlattr *tb[], struct nlattr *data[])
1501{
stephen hemminger553675f2013-05-16 11:35:20 +00001502 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001503 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001504 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger553675f2013-05-16 11:35:20 +00001505 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001506 __u32 vni;
1507 int err;
1508
1509 if (!data[IFLA_VXLAN_ID])
1510 return -EINVAL;
1511
1512 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001513 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001514
stephen hemminger5d174dd2013-04-27 11:31:55 +00001515 if (data[IFLA_VXLAN_GROUP])
1516 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001517
1518 if (data[IFLA_VXLAN_LOCAL])
1519 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1520
stephen hemminger34e02aa2012-10-09 20:35:53 +00001521 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001522 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001523 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001524 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001525
stephen hemminger34e02aa2012-10-09 20:35:53 +00001526 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001527 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001528 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001529 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001530
1531 if (!tb[IFLA_MTU])
1532 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001533
1534 /* update header length based on lower device */
1535 dev->hard_header_len = lowerdev->hard_header_len +
1536 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001537 }
1538
1539 if (data[IFLA_VXLAN_TOS])
1540 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1541
Vincent Bernatafb97182012-10-30 10:27:16 +00001542 if (data[IFLA_VXLAN_TTL])
1543 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1544
stephen hemmingerd3428942012-10-01 12:32:35 +00001545 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001546 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001547
1548 if (data[IFLA_VXLAN_AGEING])
1549 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1550 else
1551 vxlan->age_interval = FDB_AGE_DEFAULT;
1552
David Stevense4f67ad2012-11-20 02:50:14 +00001553 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1554 vxlan->flags |= VXLAN_F_PROXY;
1555
1556 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1557 vxlan->flags |= VXLAN_F_RSC;
1558
1559 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1560 vxlan->flags |= VXLAN_F_L2MISS;
1561
1562 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1563 vxlan->flags |= VXLAN_F_L3MISS;
1564
stephen hemmingerd3428942012-10-01 12:32:35 +00001565 if (data[IFLA_VXLAN_LIMIT])
1566 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1567
stephen hemminger05f47d62012-10-09 20:35:50 +00001568 if (data[IFLA_VXLAN_PORT_RANGE]) {
1569 const struct ifla_vxlan_port_range *p
1570 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1571 vxlan->port_min = ntohs(p->low);
1572 vxlan->port_max = ntohs(p->high);
1573 }
1574
stephen hemminger823aa872013-04-27 11:31:57 +00001575 if (data[IFLA_VXLAN_PORT])
1576 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1577
stephen hemminger553675f2013-05-16 11:35:20 +00001578 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1579 pr_info("duplicate VNI %u\n", vni);
1580 return -EEXIST;
1581 }
1582
1583 vs = vxlan_find_port(net, vxlan->dst_port);
1584 if (vs)
1585 ++vs->refcnt;
1586 else {
1587 /* Drop lock because socket create acquires RTNL lock */
1588 rtnl_unlock();
1589 vs = vxlan_socket_create(net, vxlan->dst_port);
1590 rtnl_lock();
1591 if (IS_ERR(vs))
1592 return PTR_ERR(vs);
1593
1594 hlist_add_head_rcu(&vs->hlist, vs_head(net, vxlan->dst_port));
1595 }
1596 vxlan->vn_sock = vs;
1597
Yan Burman1b13c972013-01-29 23:43:07 +00001598 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1599
stephen hemmingerd3428942012-10-01 12:32:35 +00001600 err = register_netdevice(dev);
stephen hemminger553675f2013-05-16 11:35:20 +00001601 if (err) {
1602 if (--vs->refcnt == 0) {
1603 rtnl_unlock();
1604 sk_release_kernel(vs->sock->sk);
1605 kfree(vs);
1606 rtnl_lock();
1607 }
1608 return err;
1609 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001610
stephen hemminger553675f2013-05-16 11:35:20 +00001611 list_add(&vxlan->next, &vn->vxlan_list);
1612 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1613
1614 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001615}
1616
1617static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1618{
1619 struct vxlan_dev *vxlan = netdev_priv(dev);
stephen hemminger553675f2013-05-16 11:35:20 +00001620 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001621
1622 hlist_del_rcu(&vxlan->hlist);
stephen hemminger553675f2013-05-16 11:35:20 +00001623 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001624 unregister_netdevice_queue(dev, head);
stephen hemminger553675f2013-05-16 11:35:20 +00001625
1626 if (--vs->refcnt == 0) {
1627 hlist_del_rcu(&vs->hlist);
1628 schedule_work(&vs->del_work);
1629 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001630}
1631
1632static size_t vxlan_get_size(const struct net_device *dev)
1633{
1634
1635 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001636 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001637 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1638 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1639 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1640 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1641 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001642 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1643 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1644 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1645 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001646 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1647 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001648 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001649 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001650 0;
1651}
1652
1653static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1654{
1655 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001656 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001657 struct ifla_vxlan_port_range ports = {
1658 .low = htons(vxlan->port_min),
1659 .high = htons(vxlan->port_max),
1660 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001661
Atzm Watanabec7995c42013-04-16 02:50:52 +00001662 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001663 goto nla_put_failure;
1664
stephen hemminger5d174dd2013-04-27 11:31:55 +00001665 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001666 goto nla_put_failure;
1667
Atzm Watanabec7995c42013-04-16 02:50:52 +00001668 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001669 goto nla_put_failure;
1670
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001671 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001672 goto nla_put_failure;
1673
1674 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1675 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001676 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1677 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1678 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1679 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1680 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1681 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1682 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1683 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1684 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001685 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001686 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1687 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001688 goto nla_put_failure;
1689
stephen hemminger05f47d62012-10-09 20:35:50 +00001690 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1691 goto nla_put_failure;
1692
stephen hemmingerd3428942012-10-01 12:32:35 +00001693 return 0;
1694
1695nla_put_failure:
1696 return -EMSGSIZE;
1697}
1698
1699static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1700 .kind = "vxlan",
1701 .maxtype = IFLA_VXLAN_MAX,
1702 .policy = vxlan_policy,
1703 .priv_size = sizeof(struct vxlan_dev),
1704 .setup = vxlan_setup,
1705 .validate = vxlan_validate,
1706 .newlink = vxlan_newlink,
1707 .dellink = vxlan_dellink,
1708 .get_size = vxlan_get_size,
1709 .fill_info = vxlan_fill_info,
1710};
1711
1712static __net_init int vxlan_init_net(struct net *net)
1713{
1714 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001715 unsigned h;
1716
stephen hemminger553675f2013-05-16 11:35:20 +00001717 INIT_LIST_HEAD(&vn->vxlan_list);
stephen hemmingerd3428942012-10-01 12:32:35 +00001718
stephen hemminger553675f2013-05-16 11:35:20 +00001719 for (h = 0; h < PORT_HASH_SIZE; ++h)
1720 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001721
1722 return 0;
1723}
1724
1725static __net_exit void vxlan_exit_net(struct net *net)
1726{
1727 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001728 struct vxlan_dev *vxlan;
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001729
1730 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001731 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1732 dev_close(vxlan->dev);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001733 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001734}
1735
1736static struct pernet_operations vxlan_net_ops = {
1737 .init = vxlan_init_net,
1738 .exit = vxlan_exit_net,
1739 .id = &vxlan_net_id,
1740 .size = sizeof(struct vxlan_net),
1741};
1742
1743static int __init vxlan_init_module(void)
1744{
1745 int rc;
1746
1747 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1748
1749 rc = register_pernet_device(&vxlan_net_ops);
1750 if (rc)
1751 goto out1;
1752
1753 rc = rtnl_link_register(&vxlan_link_ops);
1754 if (rc)
1755 goto out2;
1756
1757 return 0;
1758
1759out2:
1760 unregister_pernet_device(&vxlan_net_ops);
1761out1:
1762 return rc;
1763}
1764module_init(vxlan_init_module);
1765
1766static void __exit vxlan_cleanup_module(void)
1767{
1768 rtnl_link_unregister(&vxlan_link_ops);
1769 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00001770 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001771}
1772module_exit(vxlan_cleanup_module);
1773
1774MODULE_LICENSE("GPL");
1775MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00001776MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00001777MODULE_ALIAS_RTNL_LINK("vxlan");