blob: f4c6db419ddb3b56a9ecede0b62988c33e1d7c29 [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
Stephen Hemminger234f5b72013-06-17 14:16:41 -070071 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000072 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070073static unsigned short vxlan_port __read_mostly = 8472;
74module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000075MODULE_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
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070081static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000082
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030083static const u8 all_zeros_mac[ETH_ALEN];
84
stephen hemminger553675f2013-05-16 11:35:20 +000085/* per UDP socket information */
86struct vxlan_sock {
87 struct hlist_node hlist;
88 struct rcu_head rcu;
89 struct work_struct del_work;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -070090 atomic_t refcnt;
stephen hemminger553675f2013-05-16 11:35:20 +000091 struct socket *sock;
stephen hemmingerd3428942012-10-01 12:32:35 +000092 struct hlist_head vni_list[VNI_HASH_SIZE];
93};
94
stephen hemminger553675f2013-05-16 11:35:20 +000095/* per-network namespace private data for this module */
96struct vxlan_net {
97 struct list_head vxlan_list;
98 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070099 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +0000100};
101
David Stevens66817122013-03-15 04:35:51 +0000102struct vxlan_rdst {
David Stevens66817122013-03-15 04:35:51 +0000103 __be32 remote_ip;
104 __be16 remote_port;
105 u32 remote_vni;
106 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700107 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300108 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000109};
110
stephen hemmingerd3428942012-10-01 12:32:35 +0000111/* Forwarding table entry */
112struct vxlan_fdb {
113 struct hlist_node hlist; /* linked list of entries */
114 struct rcu_head rcu;
115 unsigned long updated; /* jiffies */
116 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700117 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000118 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000119 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000120 u8 eth_addr[ETH_ALEN];
121};
122
stephen hemmingerd3428942012-10-01 12:32:35 +0000123/* Pseudo network device */
124struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000125 struct hlist_node hlist; /* vni hash table */
126 struct list_head next; /* vxlan's per namespace list */
127 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000128 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000129 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000130 __be32 saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000131 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000132 __u16 port_min; /* source port range */
133 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000134 __u8 tos; /* TOS override */
135 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000136 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000137
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700138 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700139 struct work_struct igmp_join;
140 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700141
stephen hemmingerd3428942012-10-01 12:32:35 +0000142 unsigned long age_interval;
143 struct timer_list age_timer;
144 spinlock_t hash_lock;
145 unsigned int addrcnt;
146 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000147
148 struct hlist_head fdb_head[FDB_HASH_SIZE];
149};
150
David Stevense4f67ad2012-11-20 02:50:14 +0000151#define VXLAN_F_LEARN 0x01
152#define VXLAN_F_PROXY 0x02
153#define VXLAN_F_RSC 0x04
154#define VXLAN_F_L2MISS 0x08
155#define VXLAN_F_L3MISS 0x10
156
stephen hemmingerd3428942012-10-01 12:32:35 +0000157/* salt for hash table */
158static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700159static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000160
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700161static void vxlan_sock_work(struct work_struct *work);
162
stephen hemminger553675f2013-05-16 11:35:20 +0000163/* Virtual Network hash table head */
164static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
165{
166 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
167}
168
169/* Socket hash table head */
170static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000171{
172 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
173
stephen hemminger553675f2013-05-16 11:35:20 +0000174 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
175}
176
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700177/* First remote destination for a forwarding entry.
178 * Guaranteed to be non-NULL because remotes are never deleted.
179 */
180static inline struct vxlan_rdst *first_remote(struct vxlan_fdb *fdb)
181{
182 return list_first_or_null_rcu(&fdb->remotes, struct vxlan_rdst, list);
183}
184
stephen hemminger553675f2013-05-16 11:35:20 +0000185/* Find VXLAN socket based on network namespace and UDP port */
186static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
187{
188 struct vxlan_sock *vs;
189
190 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
191 if (inet_sk(vs->sock->sk)->inet_sport == port)
192 return vs;
193 }
194 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000195}
196
197/* Look up VNI in a per net namespace table */
stephen hemminger553675f2013-05-16 11:35:20 +0000198static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000199{
stephen hemminger553675f2013-05-16 11:35:20 +0000200 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000201 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000202
stephen hemminger553675f2013-05-16 11:35:20 +0000203 vs = vxlan_find_port(net, port);
204 if (!vs)
205 return NULL;
206
207 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000208 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000209 return vxlan;
210 }
211
212 return NULL;
213}
214
215/* Fill in neighbour message in skbuff. */
216static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700217 const struct vxlan_fdb *fdb,
218 u32 portid, u32 seq, int type, unsigned int flags,
219 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000220{
221 unsigned long now = jiffies;
222 struct nda_cacheinfo ci;
223 struct nlmsghdr *nlh;
224 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000225 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000226
227 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
228 if (nlh == NULL)
229 return -EMSGSIZE;
230
231 ndm = nlmsg_data(nlh);
232 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000233
234 send_eth = send_ip = true;
235
236 if (type == RTM_GETNEIGH) {
237 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000238 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000239 send_eth = !is_zero_ether_addr(fdb->eth_addr);
240 } else
241 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000242 ndm->ndm_state = fdb->state;
243 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000244 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000245 ndm->ndm_type = NDA_DST;
246
David Stevense4f67ad2012-11-20 02:50:14 +0000247 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000248 goto nla_put_failure;
249
David Stevens66817122013-03-15 04:35:51 +0000250 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
251 goto nla_put_failure;
252
stephen hemminger823aa872013-04-27 11:31:57 +0000253 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000254 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
255 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000256 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700257 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000258 goto nla_put_failure;
259 if (rdst->remote_ifindex &&
260 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000261 goto nla_put_failure;
262
263 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
264 ci.ndm_confirmed = 0;
265 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
266 ci.ndm_refcnt = 0;
267
268 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
269 goto nla_put_failure;
270
271 return nlmsg_end(skb, nlh);
272
273nla_put_failure:
274 nlmsg_cancel(skb, nlh);
275 return -EMSGSIZE;
276}
277
278static inline size_t vxlan_nlmsg_size(void)
279{
280 return NLMSG_ALIGN(sizeof(struct ndmsg))
281 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
282 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000283 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000284 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
285 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000286 + nla_total_size(sizeof(struct nda_cacheinfo));
287}
288
289static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700290 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000291{
292 struct net *net = dev_net(vxlan->dev);
293 struct sk_buff *skb;
294 int err = -ENOBUFS;
295
296 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
297 if (skb == NULL)
298 goto errout;
299
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700300 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, first_remote(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000301 if (err < 0) {
302 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
303 WARN_ON(err == -EMSGSIZE);
304 kfree_skb(skb);
305 goto errout;
306 }
307
308 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
309 return;
310errout:
311 if (err < 0)
312 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
313}
314
David Stevense4f67ad2012-11-20 02:50:14 +0000315static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
316{
317 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700318 struct vxlan_fdb f = {
319 .state = NUD_STALE,
320 };
321 struct vxlan_rdst remote = {
322 .remote_ip = ipa, /* goes to NDA_DST */
323 .remote_vni = VXLAN_N_VID,
324 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700325
326 INIT_LIST_HEAD(&f.remotes);
327 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000328
329 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
330}
331
332static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
333{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700334 struct vxlan_fdb f = {
335 .state = NUD_STALE,
336 };
David Stevense4f67ad2012-11-20 02:50:14 +0000337
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700338 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000339 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
340
341 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
342}
343
stephen hemmingerd3428942012-10-01 12:32:35 +0000344/* Hash Ethernet address */
345static u32 eth_hash(const unsigned char *addr)
346{
347 u64 value = get_unaligned((u64 *)addr);
348
349 /* only want 6 bytes */
350#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000351 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000352#else
353 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000354#endif
355 return hash_64(value, FDB_HASH_BITS);
356}
357
358/* Hash chain to use given mac address */
359static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
360 const u8 *mac)
361{
362 return &vxlan->fdb_head[eth_hash(mac)];
363}
364
365/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000366static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000367 const u8 *mac)
368
369{
370 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
371 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000372
Sasha Levinb67bfe02013-02-27 17:06:00 -0800373 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000374 if (compare_ether_addr(mac, f->eth_addr) == 0)
375 return f;
376 }
377
378 return NULL;
379}
380
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000381static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
382 const u8 *mac)
383{
384 struct vxlan_fdb *f;
385
386 f = __vxlan_find_mac(vxlan, mac);
387 if (f)
388 f->used = jiffies;
389
390 return f;
391}
392
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300393/* caller should hold vxlan->hash_lock */
394static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
395 __be32 ip, __be16 port,
396 __u32 vni, __u32 ifindex)
397{
398 struct vxlan_rdst *rd;
399
400 list_for_each_entry(rd, &f->remotes, list) {
401 if (rd->remote_ip == ip &&
402 rd->remote_port == port &&
403 rd->remote_vni == vni &&
404 rd->remote_ifindex == ifindex)
405 return rd;
406 }
407
408 return NULL;
409}
410
David Stevens66817122013-03-15 04:35:51 +0000411/* Add/update destinations for multicast */
412static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000413 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000414{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700415 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000416
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300417 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
418 if (rd)
419 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700420
David Stevens66817122013-03-15 04:35:51 +0000421 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
422 if (rd == NULL)
423 return -ENOBUFS;
424 rd->remote_ip = ip;
425 rd->remote_port = port;
426 rd->remote_vni = vni;
427 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700428
429 list_add_tail_rcu(&rd->list, &f->remotes);
430
David Stevens66817122013-03-15 04:35:51 +0000431 return 1;
432}
433
stephen hemmingerd3428942012-10-01 12:32:35 +0000434/* Add new entry to forwarding table -- assumes lock held */
435static int vxlan_fdb_create(struct vxlan_dev *vxlan,
436 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000437 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000438 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000439 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000440{
441 struct vxlan_fdb *f;
442 int notify = 0;
443
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000444 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000445 if (f) {
446 if (flags & NLM_F_EXCL) {
447 netdev_dbg(vxlan->dev,
448 "lost race to create %pM\n", mac);
449 return -EEXIST;
450 }
451 if (f->state != state) {
452 f->state = state;
453 f->updated = jiffies;
454 notify = 1;
455 }
David Stevensae884082013-04-19 00:36:26 +0000456 if (f->flags != ndm_flags) {
457 f->flags = ndm_flags;
458 f->updated = jiffies;
459 notify = 1;
460 }
David Stevens66817122013-03-15 04:35:51 +0000461 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300462 (is_multicast_ether_addr(f->eth_addr) ||
463 is_zero_ether_addr(f->eth_addr))) {
David Stevens66817122013-03-15 04:35:51 +0000464 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
465
466 if (rc < 0)
467 return rc;
468 notify |= rc;
469 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000470 } else {
471 if (!(flags & NLM_F_CREATE))
472 return -ENOENT;
473
474 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
475 return -ENOSPC;
476
477 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
478 f = kmalloc(sizeof(*f), GFP_ATOMIC);
479 if (!f)
480 return -ENOMEM;
481
482 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000483 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000484 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000485 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700486 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000487 memcpy(f->eth_addr, mac, ETH_ALEN);
488
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700489 vxlan_fdb_append(f, ip, port, vni, ifindex);
490
stephen hemmingerd3428942012-10-01 12:32:35 +0000491 ++vxlan->addrcnt;
492 hlist_add_head_rcu(&f->hlist,
493 vxlan_fdb_head(vxlan, mac));
494 }
495
496 if (notify)
497 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
498
499 return 0;
500}
501
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300502static void vxlan_fdb_free_rdst(struct rcu_head *head)
503{
504 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
505 kfree(rd);
506}
507
Wei Yongjun6706c822013-04-11 19:00:35 +0000508static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000509{
510 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700511 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000512
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700513 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000514 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000515 kfree(f);
516}
517
stephen hemmingerd3428942012-10-01 12:32:35 +0000518static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
519{
520 netdev_dbg(vxlan->dev,
521 "delete %pM\n", f->eth_addr);
522
523 --vxlan->addrcnt;
524 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
525
526 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000527 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000528}
529
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300530static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
531 __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
532{
533 struct net *net = dev_net(vxlan->dev);
534
535 if (tb[NDA_DST]) {
536 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
537 return -EAFNOSUPPORT;
538
539 *ip = nla_get_be32(tb[NDA_DST]);
540 } else {
541 *ip = htonl(INADDR_ANY);
542 }
543
544 if (tb[NDA_PORT]) {
545 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
546 return -EINVAL;
547 *port = nla_get_be16(tb[NDA_PORT]);
548 } else {
549 *port = vxlan->dst_port;
550 }
551
552 if (tb[NDA_VNI]) {
553 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
554 return -EINVAL;
555 *vni = nla_get_u32(tb[NDA_VNI]);
556 } else {
557 *vni = vxlan->default_dst.remote_vni;
558 }
559
560 if (tb[NDA_IFINDEX]) {
561 struct net_device *tdev;
562
563 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
564 return -EINVAL;
565 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
566 tdev = dev_get_by_index(net, *ifindex);
567 if (!tdev)
568 return -EADDRNOTAVAIL;
569 dev_put(tdev);
570 } else {
571 *ifindex = 0;
572 }
573
574 return 0;
575}
576
stephen hemmingerd3428942012-10-01 12:32:35 +0000577/* Add static entry (via netlink) */
578static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
579 struct net_device *dev,
580 const unsigned char *addr, u16 flags)
581{
582 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300583 /* struct net *net = dev_net(vxlan->dev); */
stephen hemmingerd3428942012-10-01 12:32:35 +0000584 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000585 __be16 port;
586 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000587 int err;
588
589 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
590 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
591 ndm->ndm_state);
592 return -EINVAL;
593 }
594
595 if (tb[NDA_DST] == NULL)
596 return -EINVAL;
597
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300598 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
599 if (err)
600 return err;
David Stevens66817122013-03-15 04:35:51 +0000601
stephen hemmingerd3428942012-10-01 12:32:35 +0000602 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000603 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
604 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000605 spin_unlock_bh(&vxlan->hash_lock);
606
607 return err;
608}
609
610/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000611static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
612 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000613 const unsigned char *addr)
614{
615 struct vxlan_dev *vxlan = netdev_priv(dev);
616 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300617 struct vxlan_rdst *rd = NULL;
618 __be32 ip;
619 __be16 port;
620 u32 vni, ifindex;
621 int err;
622
623 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
624 if (err)
625 return err;
626
627 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000628
629 spin_lock_bh(&vxlan->hash_lock);
630 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300631 if (!f)
632 goto out;
633
634 if (ip != htonl(INADDR_ANY)) {
635 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
636 if (!rd)
637 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000638 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300639
640 err = 0;
641
642 /* remove a destination if it's not the only one on the list,
643 * otherwise destroy the fdb entry
644 */
645 if (rd && !list_is_singular(&f->remotes)) {
646 list_del_rcu(&rd->list);
647 call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
648 goto out;
649 }
650
651 vxlan_fdb_destroy(vxlan, f);
652
653out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000654 spin_unlock_bh(&vxlan->hash_lock);
655
656 return err;
657}
658
659/* Dump forwarding table */
660static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
661 struct net_device *dev, int idx)
662{
663 struct vxlan_dev *vxlan = netdev_priv(dev);
664 unsigned int h;
665
666 for (h = 0; h < FDB_HASH_SIZE; ++h) {
667 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000668 int err;
669
Sasha Levinb67bfe02013-02-27 17:06:00 -0800670 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000671 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000672
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700673 if (idx < cb->args[0])
674 goto skip;
675
676 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000677 err = vxlan_fdb_info(skb, vxlan, f,
678 NETLINK_CB(cb->skb).portid,
679 cb->nlh->nlmsg_seq,
680 RTM_NEWNEIGH,
681 NLM_F_MULTI, rd);
682 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700683 goto out;
David Stevens66817122013-03-15 04:35:51 +0000684 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700685skip:
686 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000687 }
688 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700689out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000690 return idx;
691}
692
693/* Watch incoming packets to learn mapping between Ethernet address
694 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700695 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000696 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700697static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000698 __be32 src_ip, const u8 *src_mac)
699{
700 struct vxlan_dev *vxlan = netdev_priv(dev);
701 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000702
703 f = vxlan_find_mac(vxlan, src_mac);
704 if (likely(f)) {
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700705 struct vxlan_rdst *rdst = first_remote(f);
706
707 if (likely(rdst->remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700708 return false;
709
710 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700711 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700712 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000713
714 if (net_ratelimit())
715 netdev_info(dev,
716 "%pM migrated from %pI4 to %pI4\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700717 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000718
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700719 rdst->remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000720 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700721 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000722 } else {
723 /* learned new entry */
724 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700725
726 /* close off race between vxlan_flush and incoming packets */
727 if (netif_running(dev))
728 vxlan_fdb_create(vxlan, src_mac, src_ip,
729 NUD_REACHABLE,
730 NLM_F_EXCL|NLM_F_CREATE,
731 vxlan->dst_port,
732 vxlan->default_dst.remote_vni,
733 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000734 spin_unlock(&vxlan->hash_lock);
735 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700736
737 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000738}
739
stephen hemmingerd3428942012-10-01 12:32:35 +0000740/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700741static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000742{
stephen hemminger553675f2013-05-16 11:35:20 +0000743 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000744
stephen hemminger553675f2013-05-16 11:35:20 +0000745 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000746 if (!netif_running(vxlan->dev))
747 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000748
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700749 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000750 return true;
751 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000752
753 return false;
754}
755
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700756static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000757{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700758 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000759}
760
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700761static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000762{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700763 if (!atomic_dec_and_test(&vs->refcnt))
764 return;
765
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700766 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700767 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700768 spin_unlock(&vn->sock_lock);
769
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700770 queue_work(vxlan_wq, &vs->del_work);
771}
772
stephen hemminger3fc2de22013-07-18 08:40:15 -0700773/* Callback to update multicast group membership when first VNI on
774 * multicast asddress is brought up
775 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700776 */
stephen hemminger3fc2de22013-07-18 08:40:15 -0700777static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700778{
stephen hemminger3fc2de22013-07-18 08:40:15 -0700779 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700780 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
781 struct vxlan_sock *vs = vxlan->vn_sock;
782 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000783 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000784 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
785 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000786 };
787
stephen hemmingerd3428942012-10-01 12:32:35 +0000788 lock_sock(sk);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700789 ip_mc_join_group(sk, &mreq);
790 release_sock(sk);
791
792 vxlan_sock_release(vn, vs);
793 dev_put(vxlan->dev);
794}
795
796/* Inverse of vxlan_igmp_join when last VNI is brought down */
797static void vxlan_igmp_leave(struct work_struct *work)
798{
799 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
800 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
801 struct vxlan_sock *vs = vxlan->vn_sock;
802 struct sock *sk = vs->sock->sk;
803 struct ip_mreqn mreq = {
804 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
805 .imr_ifindex = vxlan->default_dst.remote_ifindex,
806 };
807
808 lock_sock(sk);
809 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000810 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000811
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700812 vxlan_sock_release(vn, vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700813 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000814}
815
816/* Callback from net/ipv4/udp.c to receive packets */
817static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
818{
819 struct iphdr *oip;
820 struct vxlanhdr *vxh;
821 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000822 struct pcpu_tstats *stats;
stephen hemminger553675f2013-05-16 11:35:20 +0000823 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000824 __u32 vni;
825 int err;
826
827 /* pop off outer UDP header */
828 __skb_pull(skb, sizeof(struct udphdr));
829
830 /* Need Vxlan and inner Ethernet header to be present */
831 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
832 goto error;
833
834 /* Drop packets with reserved bits set */
835 vxh = (struct vxlanhdr *) skb->data;
836 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
837 (vxh->vx_vni & htonl(0xff))) {
838 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
839 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
840 goto error;
841 }
842
843 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000844
845 /* Is this VNI defined? */
846 vni = ntohl(vxh->vx_vni) >> 8;
stephen hemminger553675f2013-05-16 11:35:20 +0000847 port = inet_sk(sk)->inet_sport;
848 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000849 if (!vxlan) {
stephen hemminger553675f2013-05-16 11:35:20 +0000850 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
851 vni, ntohs(port));
stephen hemmingerd3428942012-10-01 12:32:35 +0000852 goto drop;
853 }
854
855 if (!pskb_may_pull(skb, ETH_HLEN)) {
856 vxlan->dev->stats.rx_length_errors++;
857 vxlan->dev->stats.rx_errors++;
858 goto drop;
859 }
860
David Stevense4f67ad2012-11-20 02:50:14 +0000861 skb_reset_mac_header(skb);
862
stephen hemmingerd3428942012-10-01 12:32:35 +0000863 /* Re-examine inner Ethernet packet */
864 oip = ip_hdr(skb);
865 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000866
867 /* Ignore packet loops (and multicast echo) */
868 if (compare_ether_addr(eth_hdr(skb)->h_source,
869 vxlan->dev->dev_addr) == 0)
870 goto drop;
871
stephen hemminger26a41ae62013-06-17 12:09:58 -0700872 if ((vxlan->flags & VXLAN_F_LEARN) &&
873 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
874 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000875
876 __skb_tunnel_rx(skb, vxlan->dev);
877 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000878
879 /* If the NIC driver gave us an encapsulated packet with
880 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
881 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
882 * for us. Otherwise force the upper layers to verify it.
883 */
884 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
885 !(vxlan->dev->features & NETIF_F_RXCSUM))
886 skb->ip_summed = CHECKSUM_NONE;
887
888 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000889
890 err = IP_ECN_decapsulate(oip, skb);
891 if (unlikely(err)) {
892 if (log_ecn_error)
893 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
894 &oip->saddr, oip->tos);
895 if (err > 1) {
896 ++vxlan->dev->stats.rx_frame_errors;
897 ++vxlan->dev->stats.rx_errors;
898 goto drop;
899 }
900 }
901
Pravin B Shelare8171042013-03-25 14:49:46 +0000902 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000903 u64_stats_update_begin(&stats->syncp);
904 stats->rx_packets++;
905 stats->rx_bytes += skb->len;
906 u64_stats_update_end(&stats->syncp);
907
908 netif_rx(skb);
909
910 return 0;
911error:
912 /* Put UDP header back */
913 __skb_push(skb, sizeof(struct udphdr));
914
915 return 1;
916drop:
917 /* Consume bad packet */
918 kfree_skb(skb);
919 return 0;
920}
921
David Stevense4f67ad2012-11-20 02:50:14 +0000922static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
923{
924 struct vxlan_dev *vxlan = netdev_priv(dev);
925 struct arphdr *parp;
926 u8 *arpptr, *sha;
927 __be32 sip, tip;
928 struct neighbour *n;
929
930 if (dev->flags & IFF_NOARP)
931 goto out;
932
933 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
934 dev->stats.tx_dropped++;
935 goto out;
936 }
937 parp = arp_hdr(skb);
938
939 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
940 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
941 parp->ar_pro != htons(ETH_P_IP) ||
942 parp->ar_op != htons(ARPOP_REQUEST) ||
943 parp->ar_hln != dev->addr_len ||
944 parp->ar_pln != 4)
945 goto out;
946 arpptr = (u8 *)parp + sizeof(struct arphdr);
947 sha = arpptr;
948 arpptr += dev->addr_len; /* sha */
949 memcpy(&sip, arpptr, sizeof(sip));
950 arpptr += sizeof(sip);
951 arpptr += dev->addr_len; /* tha */
952 memcpy(&tip, arpptr, sizeof(tip));
953
954 if (ipv4_is_loopback(tip) ||
955 ipv4_is_multicast(tip))
956 goto out;
957
958 n = neigh_lookup(&arp_tbl, &tip, dev);
959
960 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000961 struct vxlan_fdb *f;
962 struct sk_buff *reply;
963
964 if (!(n->nud_state & NUD_CONNECTED)) {
965 neigh_release(n);
966 goto out;
967 }
968
969 f = vxlan_find_mac(vxlan, n->ha);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700970 if (f && first_remote(f)->remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000971 /* bridge-local neighbor */
972 neigh_release(n);
973 goto out;
974 }
975
976 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
977 n->ha, sha);
978
979 neigh_release(n);
980
981 skb_reset_mac_header(reply);
982 __skb_pull(reply, skb_network_offset(reply));
983 reply->ip_summed = CHECKSUM_UNNECESSARY;
984 reply->pkt_type = PACKET_HOST;
985
986 if (netif_rx_ni(reply) == NET_RX_DROP)
987 dev->stats.rx_dropped++;
988 } else if (vxlan->flags & VXLAN_F_L3MISS)
989 vxlan_ip_miss(dev, tip);
990out:
991 consume_skb(skb);
992 return NETDEV_TX_OK;
993}
994
995static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
996{
997 struct vxlan_dev *vxlan = netdev_priv(dev);
998 struct neighbour *n;
999 struct iphdr *pip;
1000
1001 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1002 return false;
1003
1004 n = NULL;
1005 switch (ntohs(eth_hdr(skb)->h_proto)) {
1006 case ETH_P_IP:
1007 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1008 return false;
1009 pip = ip_hdr(skb);
1010 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1011 break;
1012 default:
1013 return false;
1014 }
1015
1016 if (n) {
1017 bool diff;
1018
1019 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1020 if (diff) {
1021 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1022 dev->addr_len);
1023 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1024 }
1025 neigh_release(n);
1026 return diff;
1027 } else if (vxlan->flags & VXLAN_F_L3MISS)
1028 vxlan_ip_miss(dev, pip->daddr);
1029 return false;
1030}
1031
stephen hemminger553675f2013-05-16 11:35:20 +00001032static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001033{
1034 sock_put(skb->sk);
1035}
1036
1037/* On transmit, associate with the tunnel socket */
1038static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1039{
stephen hemminger553675f2013-05-16 11:35:20 +00001040 struct vxlan_dev *vxlan = netdev_priv(dev);
1041 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +00001042
1043 skb_orphan(skb);
1044 sock_hold(sk);
1045 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001046 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001047}
1048
stephen hemminger05f47d62012-10-09 20:35:50 +00001049/* Compute source port for outgoing packet
1050 * first choice to use L4 flow hash since it will spread
1051 * better and maybe available from hardware
1052 * secondary choice is to use jhash on the Ethernet header
1053 */
stephen hemminger7d836a72013-04-27 11:31:56 +00001054static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001055{
1056 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1057 u32 hash;
1058
1059 hash = skb_get_rxhash(skb);
1060 if (!hash)
1061 hash = jhash(skb->data, 2 * ETH_ALEN,
1062 (__force u32) skb->protocol);
1063
stephen hemminger7d836a72013-04-27 11:31:56 +00001064 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001065}
1066
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001067static int handle_offloads(struct sk_buff *skb)
1068{
1069 if (skb_is_gso(skb)) {
1070 int err = skb_unclone(skb, GFP_ATOMIC);
1071 if (unlikely(err))
1072 return err;
1073
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001074 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001075 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1076 skb->ip_summed = CHECKSUM_NONE;
1077
1078 return 0;
1079}
1080
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001081/* Bypass encapsulation if the destination is local */
1082static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1083 struct vxlan_dev *dst_vxlan)
1084{
1085 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1086 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1087
1088 skb->pkt_type = PACKET_HOST;
1089 skb->encapsulation = 0;
1090 skb->dev = dst_vxlan->dev;
1091 __skb_pull(skb, skb_network_offset(skb));
1092
1093 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001094 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1095 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001096
1097 u64_stats_update_begin(&tx_stats->syncp);
1098 tx_stats->tx_packets++;
1099 tx_stats->tx_bytes += skb->len;
1100 u64_stats_update_end(&tx_stats->syncp);
1101
1102 if (netif_rx(skb) == NET_RX_SUCCESS) {
1103 u64_stats_update_begin(&rx_stats->syncp);
1104 rx_stats->rx_packets++;
1105 rx_stats->rx_bytes += skb->len;
1106 u64_stats_update_end(&rx_stats->syncp);
1107 } else {
1108 skb->dev->stats.rx_dropped++;
1109 }
1110}
1111
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001112static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1113 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001114{
1115 struct vxlan_dev *vxlan = netdev_priv(dev);
1116 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001117 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001118 struct vxlanhdr *vxh;
1119 struct udphdr *uh;
1120 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001121 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001122 __be16 src_port, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001123 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001124 __be16 df = 0;
1125 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001126 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001127
stephen hemminger823aa872013-04-27 11:31:57 +00001128 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001129 vni = rdst->remote_vni;
1130 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001131
1132 if (!dst) {
1133 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001134 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001135 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001136 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001137 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001138 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001139 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001140
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001141 if (!skb->encapsulation) {
1142 skb_reset_inner_headers(skb);
1143 skb->encapsulation = 1;
1144 }
1145
stephen hemmingerd3428942012-10-01 12:32:35 +00001146 /* Need space for new headers (invalidates iph ptr) */
1147 if (skb_cow_head(skb, VXLAN_HEADROOM))
1148 goto drop;
1149
stephen hemmingerd3428942012-10-01 12:32:35 +00001150 old_iph = ip_hdr(skb);
1151
stephen hemmingerd3428942012-10-01 12:32:35 +00001152 ttl = vxlan->ttl;
1153 if (!ttl && IN_MULTICAST(ntohl(dst)))
1154 ttl = 1;
1155
1156 tos = vxlan->tos;
1157 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001158 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001159
stephen hemminger05f47d62012-10-09 20:35:50 +00001160 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001161
stephen hemmingerca78f182012-10-09 20:35:48 +00001162 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001163 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001164 fl4.flowi4_tos = RT_TOS(tos);
1165 fl4.daddr = dst;
1166 fl4.saddr = vxlan->saddr;
1167
1168 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001169 if (IS_ERR(rt)) {
1170 netdev_dbg(dev, "no route to %pI4\n", &dst);
1171 dev->stats.tx_carrier_errors++;
1172 goto tx_error;
1173 }
1174
1175 if (rt->dst.dev == dev) {
1176 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1177 ip_rt_put(rt);
1178 dev->stats.collisions++;
1179 goto tx_error;
1180 }
1181
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001182 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001183 if (rt->rt_flags & RTCF_LOCAL &&
1184 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001185 struct vxlan_dev *dst_vxlan;
1186
1187 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001188 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001189 if (!dst_vxlan)
1190 goto tx_error;
1191 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001192 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001193 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001194 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1195 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001196 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001197
1198 __skb_push(skb, sizeof(*uh));
1199 skb_reset_transport_header(skb);
1200 uh = udp_hdr(skb);
1201
stephen hemminger73cf3312013-04-27 11:31:54 +00001202 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001203 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001204
1205 uh->len = htons(skb->len);
1206 uh->check = 0;
1207
stephen hemminger1cad8712012-10-09 20:35:49 +00001208 vxlan_set_owner(dev, skb);
1209
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001210 if (handle_offloads(skb))
1211 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001212
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001213 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1214 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1215
1216 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1217 IPPROTO_UDP, tos, ttl, df);
1218 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1219
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001220 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001221
1222drop:
1223 dev->stats.tx_dropped++;
1224 goto tx_free;
1225
1226tx_error:
1227 dev->stats.tx_errors++;
1228tx_free:
1229 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001230}
1231
David Stevens66817122013-03-15 04:35:51 +00001232/* Transmit local packets over Vxlan
1233 *
1234 * Outer IP header inherits ECN and DF from inner header.
1235 * Outer UDP destination is the VXLAN assigned port.
1236 * source port is based on hash of flow
1237 */
1238static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1239{
1240 struct vxlan_dev *vxlan = netdev_priv(dev);
1241 struct ethhdr *eth;
1242 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001243 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001244 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001245
1246 skb_reset_mac_header(skb);
1247 eth = eth_hdr(skb);
1248
1249 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1250 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001251
1252 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001253 did_rsc = false;
1254
1255 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1256 ntohs(eth->h_proto) == ETH_P_IP) {
1257 did_rsc = route_shortcircuit(dev, skb);
1258 if (did_rsc)
1259 f = vxlan_find_mac(vxlan, eth->h_dest);
1260 }
1261
David Stevens66817122013-03-15 04:35:51 +00001262 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001263 f = vxlan_find_mac(vxlan, all_zeros_mac);
1264 if (f == NULL) {
1265 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1266 !is_multicast_ether_addr(eth->h_dest))
1267 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001268
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001269 dev->stats.tx_dropped++;
1270 dev_kfree_skb(skb);
1271 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001272 }
David Stevens66817122013-03-15 04:35:51 +00001273 }
1274
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001275 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1276 struct sk_buff *skb1;
1277
1278 skb1 = skb_clone(skb, GFP_ATOMIC);
1279 if (skb1)
1280 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1281 }
1282
1283 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001284 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001285}
1286
stephen hemmingerd3428942012-10-01 12:32:35 +00001287/* Walk the forwarding table and purge stale entries */
1288static void vxlan_cleanup(unsigned long arg)
1289{
1290 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1291 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1292 unsigned int h;
1293
1294 if (!netif_running(vxlan->dev))
1295 return;
1296
1297 spin_lock_bh(&vxlan->hash_lock);
1298 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1299 struct hlist_node *p, *n;
1300 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1301 struct vxlan_fdb *f
1302 = container_of(p, struct vxlan_fdb, hlist);
1303 unsigned long timeout;
1304
stephen hemminger3c172862012-10-26 06:24:34 +00001305 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001306 continue;
1307
1308 timeout = f->used + vxlan->age_interval * HZ;
1309 if (time_before_eq(timeout, jiffies)) {
1310 netdev_dbg(vxlan->dev,
1311 "garbage collect %pM\n",
1312 f->eth_addr);
1313 f->state = NUD_STALE;
1314 vxlan_fdb_destroy(vxlan, f);
1315 } else if (time_before(timeout, next_timer))
1316 next_timer = timeout;
1317 }
1318 }
1319 spin_unlock_bh(&vxlan->hash_lock);
1320
1321 mod_timer(&vxlan->age_timer, next_timer);
1322}
1323
1324/* Setup stats when device is created */
1325static int vxlan_init(struct net_device *dev)
1326{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001327 struct vxlan_dev *vxlan = netdev_priv(dev);
1328 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1329 struct vxlan_sock *vs;
1330 __u32 vni = vxlan->default_dst.remote_vni;
1331
Pravin B Shelare8171042013-03-25 14:49:46 +00001332 dev->tstats = alloc_percpu(struct pcpu_tstats);
1333 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001334 return -ENOMEM;
1335
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001336 spin_lock(&vn->sock_lock);
1337 vs = vxlan_find_port(dev_net(dev), vxlan->dst_port);
1338 if (vs) {
1339 /* If we have a socket with same port already, reuse it */
1340 atomic_inc(&vs->refcnt);
1341 vxlan->vn_sock = vs;
1342 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1343 } else {
1344 /* otherwise make new socket outside of RTNL */
1345 dev_hold(dev);
1346 queue_work(vxlan_wq, &vxlan->sock_work);
1347 }
1348 spin_unlock(&vn->sock_lock);
1349
stephen hemmingerd3428942012-10-01 12:32:35 +00001350 return 0;
1351}
1352
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001353static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001354{
1355 struct vxlan_fdb *f;
1356
1357 spin_lock_bh(&vxlan->hash_lock);
1358 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1359 if (f)
1360 vxlan_fdb_destroy(vxlan, f);
1361 spin_unlock_bh(&vxlan->hash_lock);
1362}
1363
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001364static void vxlan_uninit(struct net_device *dev)
1365{
1366 struct vxlan_dev *vxlan = netdev_priv(dev);
1367 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1368 struct vxlan_sock *vs = vxlan->vn_sock;
1369
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001370 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001371
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001372 if (vs)
1373 vxlan_sock_release(vn, vs);
1374 free_percpu(dev->tstats);
1375}
1376
stephen hemmingerd3428942012-10-01 12:32:35 +00001377/* Start ageing timer and join group when device is brought up */
1378static int vxlan_open(struct net_device *dev)
1379{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001380 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001381 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001382 struct vxlan_sock *vs = vxlan->vn_sock;
1383
1384 /* socket hasn't been created */
1385 if (!vs)
1386 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001387
stephen hemminger3fc2de22013-07-18 08:40:15 -07001388 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1389 ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001390 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001391 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001392 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00001393 }
1394
1395 if (vxlan->age_interval)
1396 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1397
1398 return 0;
1399}
1400
1401/* Purge the forwarding table */
1402static void vxlan_flush(struct vxlan_dev *vxlan)
1403{
Cong Wang31fec5a2013-05-27 22:35:52 +00001404 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001405
1406 spin_lock_bh(&vxlan->hash_lock);
1407 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1408 struct hlist_node *p, *n;
1409 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1410 struct vxlan_fdb *f
1411 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001412 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1413 if (!is_zero_ether_addr(f->eth_addr))
1414 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001415 }
1416 }
1417 spin_unlock_bh(&vxlan->hash_lock);
1418}
1419
1420/* Cleanup timer and forwarding table on shutdown */
1421static int vxlan_stop(struct net_device *dev)
1422{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001423 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001424 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001425 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001426
stephen hemminger3fc2de22013-07-18 08:40:15 -07001427 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1428 ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001429 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001430 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001431 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001432 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001433
1434 del_timer_sync(&vxlan->age_timer);
1435
1436 vxlan_flush(vxlan);
1437
1438 return 0;
1439}
1440
stephen hemmingerd3428942012-10-01 12:32:35 +00001441/* Stub, nothing needs to be done. */
1442static void vxlan_set_multicast_list(struct net_device *dev)
1443{
1444}
1445
1446static const struct net_device_ops vxlan_netdev_ops = {
1447 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001448 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001449 .ndo_open = vxlan_open,
1450 .ndo_stop = vxlan_stop,
1451 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001452 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001453 .ndo_set_rx_mode = vxlan_set_multicast_list,
1454 .ndo_change_mtu = eth_change_mtu,
1455 .ndo_validate_addr = eth_validate_addr,
1456 .ndo_set_mac_address = eth_mac_addr,
1457 .ndo_fdb_add = vxlan_fdb_add,
1458 .ndo_fdb_del = vxlan_fdb_delete,
1459 .ndo_fdb_dump = vxlan_fdb_dump,
1460};
1461
1462/* Info for udev, that this is a virtual tunnel endpoint */
1463static struct device_type vxlan_type = {
1464 .name = "vxlan",
1465};
1466
stephen hemmingerd3428942012-10-01 12:32:35 +00001467/* Initialize the device structure. */
1468static void vxlan_setup(struct net_device *dev)
1469{
1470 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001471 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001472 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001473
1474 eth_hw_addr_random(dev);
1475 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001476 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001477
1478 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001479 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001480 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1481
1482 dev->tx_queue_len = 0;
1483 dev->features |= NETIF_F_LLTX;
1484 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001485 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001486 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001487 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001488
1489 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001490 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001491 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001492 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001493
stephen hemminger553675f2013-05-16 11:35:20 +00001494 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001495 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001496 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1497 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001498 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001499
1500 init_timer_deferrable(&vxlan->age_timer);
1501 vxlan->age_timer.function = vxlan_cleanup;
1502 vxlan->age_timer.data = (unsigned long) vxlan;
1503
stephen hemminger05f47d62012-10-09 20:35:50 +00001504 inet_get_local_port_range(&low, &high);
1505 vxlan->port_min = low;
1506 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001507 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001508
stephen hemmingerd3428942012-10-01 12:32:35 +00001509 vxlan->dev = dev;
1510
1511 for (h = 0; h < FDB_HASH_SIZE; ++h)
1512 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1513}
1514
1515static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1516 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001517 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001518 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1519 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1520 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1521 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1522 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1523 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1524 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001525 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001526 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1527 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1528 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1529 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001530 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001531};
1532
1533static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1534{
1535 if (tb[IFLA_ADDRESS]) {
1536 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1537 pr_debug("invalid link address (not ethernet)\n");
1538 return -EINVAL;
1539 }
1540
1541 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1542 pr_debug("invalid all zero ethernet address\n");
1543 return -EADDRNOTAVAIL;
1544 }
1545 }
1546
1547 if (!data)
1548 return -EINVAL;
1549
1550 if (data[IFLA_VXLAN_ID]) {
1551 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1552 if (id >= VXLAN_VID_MASK)
1553 return -ERANGE;
1554 }
1555
stephen hemminger05f47d62012-10-09 20:35:50 +00001556 if (data[IFLA_VXLAN_PORT_RANGE]) {
1557 const struct ifla_vxlan_port_range *p
1558 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1559
1560 if (ntohs(p->high) < ntohs(p->low)) {
1561 pr_debug("port range %u .. %u not valid\n",
1562 ntohs(p->low), ntohs(p->high));
1563 return -EINVAL;
1564 }
1565 }
1566
stephen hemmingerd3428942012-10-01 12:32:35 +00001567 return 0;
1568}
1569
Yan Burman1b13c972013-01-29 23:43:07 +00001570static void vxlan_get_drvinfo(struct net_device *netdev,
1571 struct ethtool_drvinfo *drvinfo)
1572{
1573 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1574 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1575}
1576
1577static const struct ethtool_ops vxlan_ethtool_ops = {
1578 .get_drvinfo = vxlan_get_drvinfo,
1579 .get_link = ethtool_op_get_link,
1580};
1581
stephen hemminger553675f2013-05-16 11:35:20 +00001582static void vxlan_del_work(struct work_struct *work)
1583{
1584 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1585
1586 sk_release_kernel(vs->sock->sk);
1587 kfree_rcu(vs, rcu);
1588}
1589
stephen hemminger553675f2013-05-16 11:35:20 +00001590static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1591{
1592 struct vxlan_sock *vs;
1593 struct sock *sk;
1594 struct sockaddr_in vxlan_addr = {
1595 .sin_family = AF_INET,
1596 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07001597 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00001598 };
1599 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001600 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001601
1602 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1603 if (!vs)
1604 return ERR_PTR(-ENOMEM);
1605
1606 for (h = 0; h < VNI_HASH_SIZE; ++h)
1607 INIT_HLIST_HEAD(&vs->vni_list[h]);
1608
1609 INIT_WORK(&vs->del_work, vxlan_del_work);
1610
1611 /* Create UDP socket for encapsulation receive. */
1612 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1613 if (rc < 0) {
1614 pr_debug("UDP socket create failed\n");
1615 kfree(vs);
1616 return ERR_PTR(rc);
1617 }
1618
1619 /* Put in proper namespace */
1620 sk = vs->sock->sk;
1621 sk_change_net(sk, net);
1622
stephen hemminger553675f2013-05-16 11:35:20 +00001623 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1624 sizeof(vxlan_addr));
1625 if (rc < 0) {
1626 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1627 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1628 sk_release_kernel(sk);
1629 kfree(vs);
1630 return ERR_PTR(rc);
1631 }
1632
1633 /* Disable multicast loopback */
1634 inet_sk(sk)->mc_loop = 0;
1635
1636 /* Mark socket as an encapsulation socket. */
1637 udp_sk(sk)->encap_type = 1;
1638 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1639 udp_encap_enable();
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001640 atomic_set(&vs->refcnt, 1);
stephen hemminger553675f2013-05-16 11:35:20 +00001641
stephen hemminger553675f2013-05-16 11:35:20 +00001642 return vs;
1643}
1644
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001645/* Scheduled at device creation to bind to a socket */
1646static void vxlan_sock_work(struct work_struct *work)
1647{
1648 struct vxlan_dev *vxlan
1649 = container_of(work, struct vxlan_dev, sock_work);
1650 struct net_device *dev = vxlan->dev;
1651 struct net *net = dev_net(dev);
1652 __u32 vni = vxlan->default_dst.remote_vni;
1653 __be16 port = vxlan->dst_port;
1654 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1655 struct vxlan_sock *nvs, *ovs;
1656
1657 nvs = vxlan_socket_create(net, port);
1658 if (IS_ERR(nvs)) {
1659 netdev_err(vxlan->dev, "Can not create UDP socket, %ld\n",
1660 PTR_ERR(nvs));
1661 goto out;
1662 }
1663
1664 spin_lock(&vn->sock_lock);
1665 /* Look again to see if can reuse socket */
1666 ovs = vxlan_find_port(net, port);
1667 if (ovs) {
1668 atomic_inc(&ovs->refcnt);
1669 vxlan->vn_sock = ovs;
1670 hlist_add_head_rcu(&vxlan->hlist, vni_head(ovs, vni));
1671 spin_unlock(&vn->sock_lock);
1672
1673 sk_release_kernel(nvs->sock->sk);
1674 kfree(nvs);
1675 } else {
1676 vxlan->vn_sock = nvs;
1677 hlist_add_head_rcu(&nvs->hlist, vs_head(net, port));
1678 hlist_add_head_rcu(&vxlan->hlist, vni_head(nvs, vni));
1679 spin_unlock(&vn->sock_lock);
1680 }
1681out:
1682 dev_put(dev);
1683}
1684
stephen hemmingerd3428942012-10-01 12:32:35 +00001685static int vxlan_newlink(struct net *net, struct net_device *dev,
1686 struct nlattr *tb[], struct nlattr *data[])
1687{
stephen hemminger553675f2013-05-16 11:35:20 +00001688 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001689 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001690 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001691 __u32 vni;
1692 int err;
1693
1694 if (!data[IFLA_VXLAN_ID])
1695 return -EINVAL;
1696
1697 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001698 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001699
stephen hemminger5d174dd2013-04-27 11:31:55 +00001700 if (data[IFLA_VXLAN_GROUP])
1701 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001702
1703 if (data[IFLA_VXLAN_LOCAL])
1704 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1705
stephen hemminger34e02aa2012-10-09 20:35:53 +00001706 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001707 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001708 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001709 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001710
stephen hemminger34e02aa2012-10-09 20:35:53 +00001711 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001712 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001713 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001714 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001715
1716 if (!tb[IFLA_MTU])
1717 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001718
1719 /* update header length based on lower device */
1720 dev->hard_header_len = lowerdev->hard_header_len +
1721 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001722 }
1723
1724 if (data[IFLA_VXLAN_TOS])
1725 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1726
Vincent Bernatafb97182012-10-30 10:27:16 +00001727 if (data[IFLA_VXLAN_TTL])
1728 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1729
stephen hemmingerd3428942012-10-01 12:32:35 +00001730 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001731 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001732
1733 if (data[IFLA_VXLAN_AGEING])
1734 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1735 else
1736 vxlan->age_interval = FDB_AGE_DEFAULT;
1737
David Stevense4f67ad2012-11-20 02:50:14 +00001738 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1739 vxlan->flags |= VXLAN_F_PROXY;
1740
1741 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1742 vxlan->flags |= VXLAN_F_RSC;
1743
1744 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1745 vxlan->flags |= VXLAN_F_L2MISS;
1746
1747 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1748 vxlan->flags |= VXLAN_F_L3MISS;
1749
stephen hemmingerd3428942012-10-01 12:32:35 +00001750 if (data[IFLA_VXLAN_LIMIT])
1751 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1752
stephen hemminger05f47d62012-10-09 20:35:50 +00001753 if (data[IFLA_VXLAN_PORT_RANGE]) {
1754 const struct ifla_vxlan_port_range *p
1755 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1756 vxlan->port_min = ntohs(p->low);
1757 vxlan->port_max = ntohs(p->high);
1758 }
1759
stephen hemminger823aa872013-04-27 11:31:57 +00001760 if (data[IFLA_VXLAN_PORT])
1761 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1762
stephen hemminger553675f2013-05-16 11:35:20 +00001763 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1764 pr_info("duplicate VNI %u\n", vni);
1765 return -EEXIST;
1766 }
1767
Yan Burman1b13c972013-01-29 23:43:07 +00001768 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1769
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001770 /* create an fdb entry for default destination */
1771 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1772 vxlan->default_dst.remote_ip,
1773 NUD_REACHABLE|NUD_PERMANENT,
1774 NLM_F_EXCL|NLM_F_CREATE,
1775 vxlan->dst_port, vxlan->default_dst.remote_vni,
1776 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001777 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001778 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001779
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001780 err = register_netdevice(dev);
1781 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001782 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001783 return err;
1784 }
1785
stephen hemminger553675f2013-05-16 11:35:20 +00001786 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001787
1788 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001789}
1790
1791static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1792{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001793 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001794 struct vxlan_dev *vxlan = netdev_priv(dev);
1795
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001796 flush_workqueue(vxlan_wq);
1797
1798 spin_lock(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001799 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001800 spin_unlock(&vn->sock_lock);
1801
stephen hemminger553675f2013-05-16 11:35:20 +00001802 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001803 unregister_netdevice_queue(dev, head);
1804}
1805
1806static size_t vxlan_get_size(const struct net_device *dev)
1807{
1808
1809 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001810 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001811 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1812 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1813 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1814 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1815 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001816 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1817 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1818 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1819 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001820 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1821 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001822 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001823 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001824 0;
1825}
1826
1827static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1828{
1829 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001830 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001831 struct ifla_vxlan_port_range ports = {
1832 .low = htons(vxlan->port_min),
1833 .high = htons(vxlan->port_max),
1834 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001835
Atzm Watanabec7995c42013-04-16 02:50:52 +00001836 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001837 goto nla_put_failure;
1838
stephen hemminger5d174dd2013-04-27 11:31:55 +00001839 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001840 goto nla_put_failure;
1841
Atzm Watanabec7995c42013-04-16 02:50:52 +00001842 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001843 goto nla_put_failure;
1844
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001845 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001846 goto nla_put_failure;
1847
1848 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1849 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001850 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1851 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1852 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1853 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1854 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1855 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1856 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1857 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1858 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001859 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001860 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1861 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001862 goto nla_put_failure;
1863
stephen hemminger05f47d62012-10-09 20:35:50 +00001864 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1865 goto nla_put_failure;
1866
stephen hemmingerd3428942012-10-01 12:32:35 +00001867 return 0;
1868
1869nla_put_failure:
1870 return -EMSGSIZE;
1871}
1872
1873static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1874 .kind = "vxlan",
1875 .maxtype = IFLA_VXLAN_MAX,
1876 .policy = vxlan_policy,
1877 .priv_size = sizeof(struct vxlan_dev),
1878 .setup = vxlan_setup,
1879 .validate = vxlan_validate,
1880 .newlink = vxlan_newlink,
1881 .dellink = vxlan_dellink,
1882 .get_size = vxlan_get_size,
1883 .fill_info = vxlan_fill_info,
1884};
1885
1886static __net_init int vxlan_init_net(struct net *net)
1887{
1888 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001889 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001890
stephen hemminger553675f2013-05-16 11:35:20 +00001891 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001892 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001893
stephen hemminger553675f2013-05-16 11:35:20 +00001894 for (h = 0; h < PORT_HASH_SIZE; ++h)
1895 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001896
1897 return 0;
1898}
1899
1900static __net_exit void vxlan_exit_net(struct net *net)
1901{
1902 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001903 struct vxlan_dev *vxlan;
stephen hemminger372675a2013-07-18 08:38:26 -07001904 LIST_HEAD(list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001905
1906 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001907 list_for_each_entry(vxlan, &vn->vxlan_list, next)
stephen hemminger372675a2013-07-18 08:38:26 -07001908 unregister_netdevice_queue(vxlan->dev, &list);
1909 unregister_netdevice_many(&list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001910 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001911}
1912
1913static struct pernet_operations vxlan_net_ops = {
1914 .init = vxlan_init_net,
1915 .exit = vxlan_exit_net,
1916 .id = &vxlan_net_id,
1917 .size = sizeof(struct vxlan_net),
1918};
1919
1920static int __init vxlan_init_module(void)
1921{
1922 int rc;
1923
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001924 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1925 if (!vxlan_wq)
1926 return -ENOMEM;
1927
stephen hemmingerd3428942012-10-01 12:32:35 +00001928 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1929
1930 rc = register_pernet_device(&vxlan_net_ops);
1931 if (rc)
1932 goto out1;
1933
1934 rc = rtnl_link_register(&vxlan_link_ops);
1935 if (rc)
1936 goto out2;
1937
1938 return 0;
1939
1940out2:
1941 unregister_pernet_device(&vxlan_net_ops);
1942out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001943 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00001944 return rc;
1945}
Cong Wang7332a132013-05-27 22:35:53 +00001946late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00001947
1948static void __exit vxlan_cleanup_module(void)
1949{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07001950 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001951 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07001952 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00001953 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001954}
1955module_exit(vxlan_cleanup_module);
1956
1957MODULE_LICENSE("GPL");
1958MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00001959MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00001960MODULE_ALIAS_RTNL_LINK("vxlan");