blob: f89a58bb3f267b79ac90066645679075fbc775ce [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 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
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;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -070088 atomic_t refcnt;
stephen hemminger553675f2013-05-16 11:35:20 +000089 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];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070097 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000098};
99
David Stevens66817122013-03-15 04:35:51 +0000100struct vxlan_rdst {
David Stevens66817122013-03-15 04:35:51 +0000101 __be32 remote_ip;
102 __be16 remote_port;
103 u32 remote_vni;
104 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700105 struct list_head list;
David Stevens66817122013-03-15 04:35:51 +0000106};
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;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700114 struct list_head remotes;
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
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700135 struct work_struct sock_work;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700136 struct work_struct igmp_work;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700137
stephen hemmingerd3428942012-10-01 12:32:35 +0000138 unsigned long age_interval;
139 struct timer_list age_timer;
140 spinlock_t hash_lock;
141 unsigned int addrcnt;
142 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000143
144 struct hlist_head fdb_head[FDB_HASH_SIZE];
145};
146
David Stevense4f67ad2012-11-20 02:50:14 +0000147#define VXLAN_F_LEARN 0x01
148#define VXLAN_F_PROXY 0x02
149#define VXLAN_F_RSC 0x04
150#define VXLAN_F_L2MISS 0x08
151#define VXLAN_F_L3MISS 0x10
152
stephen hemmingerd3428942012-10-01 12:32:35 +0000153/* salt for hash table */
154static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700155static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000156
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700157static void vxlan_sock_work(struct work_struct *work);
158
stephen hemminger553675f2013-05-16 11:35:20 +0000159/* Virtual Network hash table head */
160static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
161{
162 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
163}
164
165/* Socket hash table head */
166static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000167{
168 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
169
stephen hemminger553675f2013-05-16 11:35:20 +0000170 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
171}
172
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700173/* First remote destination for a forwarding entry.
174 * Guaranteed to be non-NULL because remotes are never deleted.
175 */
176static inline struct vxlan_rdst *first_remote(struct vxlan_fdb *fdb)
177{
178 return list_first_or_null_rcu(&fdb->remotes, struct vxlan_rdst, list);
179}
180
stephen hemminger553675f2013-05-16 11:35:20 +0000181/* Find VXLAN socket based on network namespace and UDP port */
182static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
183{
184 struct vxlan_sock *vs;
185
186 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
187 if (inet_sk(vs->sock->sk)->inet_sport == port)
188 return vs;
189 }
190 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000191}
192
193/* Look up VNI in a per net namespace table */
stephen hemminger553675f2013-05-16 11:35:20 +0000194static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000195{
stephen hemminger553675f2013-05-16 11:35:20 +0000196 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000197 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000198
stephen hemminger553675f2013-05-16 11:35:20 +0000199 vs = vxlan_find_port(net, port);
200 if (!vs)
201 return NULL;
202
203 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000204 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000205 return vxlan;
206 }
207
208 return NULL;
209}
210
211/* Fill in neighbour message in skbuff. */
212static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
213 const struct vxlan_fdb *fdb,
David Stevens66817122013-03-15 04:35:51 +0000214 u32 portid, u32 seq, int type, unsigned int flags,
215 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000216{
217 unsigned long now = jiffies;
218 struct nda_cacheinfo ci;
219 struct nlmsghdr *nlh;
220 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000221 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000222
223 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
224 if (nlh == NULL)
225 return -EMSGSIZE;
226
227 ndm = nlmsg_data(nlh);
228 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000229
230 send_eth = send_ip = true;
231
232 if (type == RTM_GETNEIGH) {
233 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000234 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000235 send_eth = !is_zero_ether_addr(fdb->eth_addr);
236 } else
237 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000238 ndm->ndm_state = fdb->state;
239 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000240 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000241 ndm->ndm_type = NDA_DST;
242
David Stevense4f67ad2012-11-20 02:50:14 +0000243 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000244 goto nla_put_failure;
245
David Stevens66817122013-03-15 04:35:51 +0000246 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
247 goto nla_put_failure;
248
stephen hemminger823aa872013-04-27 11:31:57 +0000249 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000250 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
251 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000252 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
David Stevens66817122013-03-15 04:35:51 +0000253 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
254 goto nla_put_failure;
255 if (rdst->remote_ifindex &&
256 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000257 goto nla_put_failure;
258
259 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
260 ci.ndm_confirmed = 0;
261 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
262 ci.ndm_refcnt = 0;
263
264 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
265 goto nla_put_failure;
266
267 return nlmsg_end(skb, nlh);
268
269nla_put_failure:
270 nlmsg_cancel(skb, nlh);
271 return -EMSGSIZE;
272}
273
274static inline size_t vxlan_nlmsg_size(void)
275{
276 return NLMSG_ALIGN(sizeof(struct ndmsg))
277 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
278 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000279 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000280 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
281 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000282 + nla_total_size(sizeof(struct nda_cacheinfo));
283}
284
285static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700286 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000287{
288 struct net *net = dev_net(vxlan->dev);
289 struct sk_buff *skb;
290 int err = -ENOBUFS;
291
292 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
293 if (skb == NULL)
294 goto errout;
295
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700296 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, first_remote(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000297 if (err < 0) {
298 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
299 WARN_ON(err == -EMSGSIZE);
300 kfree_skb(skb);
301 goto errout;
302 }
303
304 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
305 return;
306errout:
307 if (err < 0)
308 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
309}
310
David Stevense4f67ad2012-11-20 02:50:14 +0000311static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
312{
313 struct vxlan_dev *vxlan = netdev_priv(dev);
314 struct vxlan_fdb f;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700315 struct vxlan_rdst remote;
David Stevense4f67ad2012-11-20 02:50:14 +0000316
317 memset(&f, 0, sizeof f);
318 f.state = NUD_STALE;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700319
320 remote.remote_ip = ipa; /* goes to NDA_DST */
321 remote.remote_vni = VXLAN_N_VID;
322
323 INIT_LIST_HEAD(&f.remotes);
324 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000325
326 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
327}
328
329static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
330{
331 struct vxlan_fdb f;
332
333 memset(&f, 0, sizeof f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700334 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000335 f.state = NUD_STALE;
336 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
337
338 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
339}
340
stephen hemmingerd3428942012-10-01 12:32:35 +0000341/* Hash Ethernet address */
342static u32 eth_hash(const unsigned char *addr)
343{
344 u64 value = get_unaligned((u64 *)addr);
345
346 /* only want 6 bytes */
347#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000348 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000349#else
350 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000351#endif
352 return hash_64(value, FDB_HASH_BITS);
353}
354
355/* Hash chain to use given mac address */
356static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
357 const u8 *mac)
358{
359 return &vxlan->fdb_head[eth_hash(mac)];
360}
361
362/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000363static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000364 const u8 *mac)
365
366{
367 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
368 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000369
Sasha Levinb67bfe02013-02-27 17:06:00 -0800370 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000371 if (compare_ether_addr(mac, f->eth_addr) == 0)
372 return f;
373 }
374
375 return NULL;
376}
377
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000378static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
379 const u8 *mac)
380{
381 struct vxlan_fdb *f;
382
383 f = __vxlan_find_mac(vxlan, mac);
384 if (f)
385 f->used = jiffies;
386
387 return f;
388}
389
David Stevens66817122013-03-15 04:35:51 +0000390/* Add/update destinations for multicast */
391static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000392 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000393{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700394 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000395
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700396 /* protected by vxlan->hash_lock */
397 list_for_each_entry(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000398 if (rd->remote_ip == ip &&
399 rd->remote_port == port &&
400 rd->remote_vni == vni &&
401 rd->remote_ifindex == ifindex)
402 return 0;
David Stevens66817122013-03-15 04:35:51 +0000403 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700404
David Stevens66817122013-03-15 04:35:51 +0000405 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
406 if (rd == NULL)
407 return -ENOBUFS;
408 rd->remote_ip = ip;
409 rd->remote_port = port;
410 rd->remote_vni = vni;
411 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700412
413 list_add_tail_rcu(&rd->list, &f->remotes);
414
David Stevens66817122013-03-15 04:35:51 +0000415 return 1;
416}
417
stephen hemmingerd3428942012-10-01 12:32:35 +0000418/* Add new entry to forwarding table -- assumes lock held */
419static int vxlan_fdb_create(struct vxlan_dev *vxlan,
420 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000421 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000422 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000423 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000424{
425 struct vxlan_fdb *f;
426 int notify = 0;
427
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000428 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000429 if (f) {
430 if (flags & NLM_F_EXCL) {
431 netdev_dbg(vxlan->dev,
432 "lost race to create %pM\n", mac);
433 return -EEXIST;
434 }
435 if (f->state != state) {
436 f->state = state;
437 f->updated = jiffies;
438 notify = 1;
439 }
David Stevensae884082013-04-19 00:36:26 +0000440 if (f->flags != ndm_flags) {
441 f->flags = ndm_flags;
442 f->updated = jiffies;
443 notify = 1;
444 }
David Stevens66817122013-03-15 04:35:51 +0000445 if ((flags & NLM_F_APPEND) &&
446 is_multicast_ether_addr(f->eth_addr)) {
447 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
448
449 if (rc < 0)
450 return rc;
451 notify |= rc;
452 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000453 } else {
454 if (!(flags & NLM_F_CREATE))
455 return -ENOENT;
456
457 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
458 return -ENOSPC;
459
460 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
461 f = kmalloc(sizeof(*f), GFP_ATOMIC);
462 if (!f)
463 return -ENOMEM;
464
465 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000466 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000467 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000468 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700469 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000470 memcpy(f->eth_addr, mac, ETH_ALEN);
471
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700472 vxlan_fdb_append(f, ip, port, vni, ifindex);
473
stephen hemmingerd3428942012-10-01 12:32:35 +0000474 ++vxlan->addrcnt;
475 hlist_add_head_rcu(&f->hlist,
476 vxlan_fdb_head(vxlan, mac));
477 }
478
479 if (notify)
480 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
481
482 return 0;
483}
484
Wei Yongjun6706c822013-04-11 19:00:35 +0000485static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000486{
487 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700488 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000489
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700490 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000491 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000492 kfree(f);
493}
494
stephen hemmingerd3428942012-10-01 12:32:35 +0000495static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
496{
497 netdev_dbg(vxlan->dev,
498 "delete %pM\n", f->eth_addr);
499
500 --vxlan->addrcnt;
501 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
502
503 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000504 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000505}
506
507/* Add static entry (via netlink) */
508static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
509 struct net_device *dev,
510 const unsigned char *addr, u16 flags)
511{
512 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens66817122013-03-15 04:35:51 +0000513 struct net *net = dev_net(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000514 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000515 __be16 port;
516 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000517 int err;
518
519 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
520 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
521 ndm->ndm_state);
522 return -EINVAL;
523 }
524
525 if (tb[NDA_DST] == NULL)
526 return -EINVAL;
527
528 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
529 return -EAFNOSUPPORT;
530
531 ip = nla_get_be32(tb[NDA_DST]);
532
David Stevens66817122013-03-15 04:35:51 +0000533 if (tb[NDA_PORT]) {
stephen hemminger73cf3312013-04-27 11:31:54 +0000534 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
David Stevens66817122013-03-15 04:35:51 +0000535 return -EINVAL;
stephen hemminger73cf3312013-04-27 11:31:54 +0000536 port = nla_get_be16(tb[NDA_PORT]);
David Stevens66817122013-03-15 04:35:51 +0000537 } else
stephen hemminger823aa872013-04-27 11:31:57 +0000538 port = vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +0000539
540 if (tb[NDA_VNI]) {
541 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
542 return -EINVAL;
543 vni = nla_get_u32(tb[NDA_VNI]);
544 } else
Atzm Watanabec7995c42013-04-16 02:50:52 +0000545 vni = vxlan->default_dst.remote_vni;
David Stevens66817122013-03-15 04:35:51 +0000546
547 if (tb[NDA_IFINDEX]) {
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000548 struct net_device *tdev;
David Stevens66817122013-03-15 04:35:51 +0000549
550 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
551 return -EINVAL;
552 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000553 tdev = dev_get_by_index(net, ifindex);
554 if (!tdev)
David Stevens66817122013-03-15 04:35:51 +0000555 return -EADDRNOTAVAIL;
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000556 dev_put(tdev);
David Stevens66817122013-03-15 04:35:51 +0000557 } else
558 ifindex = 0;
559
stephen hemmingerd3428942012-10-01 12:32:35 +0000560 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000561 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
562 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000563 spin_unlock_bh(&vxlan->hash_lock);
564
565 return err;
566}
567
568/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000569static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
570 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000571 const unsigned char *addr)
572{
573 struct vxlan_dev *vxlan = netdev_priv(dev);
574 struct vxlan_fdb *f;
575 int err = -ENOENT;
576
577 spin_lock_bh(&vxlan->hash_lock);
578 f = vxlan_find_mac(vxlan, addr);
579 if (f) {
580 vxlan_fdb_destroy(vxlan, f);
581 err = 0;
582 }
583 spin_unlock_bh(&vxlan->hash_lock);
584
585 return err;
586}
587
588/* Dump forwarding table */
589static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
590 struct net_device *dev, int idx)
591{
592 struct vxlan_dev *vxlan = netdev_priv(dev);
593 unsigned int h;
594
595 for (h = 0; h < FDB_HASH_SIZE; ++h) {
596 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000597 int err;
598
Sasha Levinb67bfe02013-02-27 17:06:00 -0800599 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000600 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000601
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700602 if (idx < cb->args[0])
603 goto skip;
604
605 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000606 err = vxlan_fdb_info(skb, vxlan, f,
607 NETLINK_CB(cb->skb).portid,
608 cb->nlh->nlmsg_seq,
609 RTM_NEWNEIGH,
610 NLM_F_MULTI, rd);
611 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700612 goto out;
David Stevens66817122013-03-15 04:35:51 +0000613 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700614skip:
615 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000616 }
617 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700618out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000619 return idx;
620}
621
622/* Watch incoming packets to learn mapping between Ethernet address
623 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700624 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000625 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700626static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000627 __be32 src_ip, const u8 *src_mac)
628{
629 struct vxlan_dev *vxlan = netdev_priv(dev);
630 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000631
632 f = vxlan_find_mac(vxlan, src_mac);
633 if (likely(f)) {
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700634 struct vxlan_rdst *rdst = first_remote(f);
635
636 if (likely(rdst->remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700637 return false;
638
639 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700640 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700641 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000642
643 if (net_ratelimit())
644 netdev_info(dev,
645 "%pM migrated from %pI4 to %pI4\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700646 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000647
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700648 rdst->remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000649 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700650 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000651 } else {
652 /* learned new entry */
653 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700654
655 /* close off race between vxlan_flush and incoming packets */
656 if (netif_running(dev))
657 vxlan_fdb_create(vxlan, src_mac, src_ip,
658 NUD_REACHABLE,
659 NLM_F_EXCL|NLM_F_CREATE,
660 vxlan->dst_port,
661 vxlan->default_dst.remote_vni,
662 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000663 spin_unlock(&vxlan->hash_lock);
664 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700665
666 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000667}
668
669
670/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700671static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000672{
stephen hemminger553675f2013-05-16 11:35:20 +0000673 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000674
stephen hemminger553675f2013-05-16 11:35:20 +0000675 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000676 if (!netif_running(vxlan->dev))
677 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000678
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700679 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000680 return true;
681 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000682
683 return false;
684}
685
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700686static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000687{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700688 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000689}
690
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700691static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000692{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700693 if (!atomic_dec_and_test(&vs->refcnt))
694 return;
695
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700696 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700697 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700698 spin_unlock(&vn->sock_lock);
699
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700700 queue_work(vxlan_wq, &vs->del_work);
701}
702
703/* Callback to update multicast group membership.
704 * Scheduled when vxlan goes up/down.
705 */
706static void vxlan_igmp_work(struct work_struct *work)
707{
708 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_work);
709 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
710 struct vxlan_sock *vs = vxlan->vn_sock;
711 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000712 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000713 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
714 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000715 };
716
stephen hemmingerd3428942012-10-01 12:32:35 +0000717 lock_sock(sk);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700718 if (vxlan_group_used(vn, vxlan->default_dst.remote_ip))
719 ip_mc_join_group(sk, &mreq);
720 else
721 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000722 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000723
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700724 vxlan_sock_release(vn, vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700725 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000726}
727
728/* Callback from net/ipv4/udp.c to receive packets */
729static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
730{
731 struct iphdr *oip;
732 struct vxlanhdr *vxh;
733 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000734 struct pcpu_tstats *stats;
stephen hemminger553675f2013-05-16 11:35:20 +0000735 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000736 __u32 vni;
737 int err;
738
739 /* pop off outer UDP header */
740 __skb_pull(skb, sizeof(struct udphdr));
741
742 /* Need Vxlan and inner Ethernet header to be present */
743 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
744 goto error;
745
746 /* Drop packets with reserved bits set */
747 vxh = (struct vxlanhdr *) skb->data;
748 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
749 (vxh->vx_vni & htonl(0xff))) {
750 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
751 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
752 goto error;
753 }
754
755 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000756
757 /* Is this VNI defined? */
758 vni = ntohl(vxh->vx_vni) >> 8;
stephen hemminger553675f2013-05-16 11:35:20 +0000759 port = inet_sk(sk)->inet_sport;
760 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000761 if (!vxlan) {
stephen hemminger553675f2013-05-16 11:35:20 +0000762 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
763 vni, ntohs(port));
stephen hemmingerd3428942012-10-01 12:32:35 +0000764 goto drop;
765 }
766
767 if (!pskb_may_pull(skb, ETH_HLEN)) {
768 vxlan->dev->stats.rx_length_errors++;
769 vxlan->dev->stats.rx_errors++;
770 goto drop;
771 }
772
David Stevense4f67ad2012-11-20 02:50:14 +0000773 skb_reset_mac_header(skb);
774
stephen hemmingerd3428942012-10-01 12:32:35 +0000775 /* Re-examine inner Ethernet packet */
776 oip = ip_hdr(skb);
777 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000778
779 /* Ignore packet loops (and multicast echo) */
780 if (compare_ether_addr(eth_hdr(skb)->h_source,
781 vxlan->dev->dev_addr) == 0)
782 goto drop;
783
stephen hemminger26a41ae62013-06-17 12:09:58 -0700784 if ((vxlan->flags & VXLAN_F_LEARN) &&
785 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
786 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000787
788 __skb_tunnel_rx(skb, vxlan->dev);
789 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000790
791 /* If the NIC driver gave us an encapsulated packet with
792 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
793 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
794 * for us. Otherwise force the upper layers to verify it.
795 */
796 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
797 !(vxlan->dev->features & NETIF_F_RXCSUM))
798 skb->ip_summed = CHECKSUM_NONE;
799
800 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000801
802 err = IP_ECN_decapsulate(oip, skb);
803 if (unlikely(err)) {
804 if (log_ecn_error)
805 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
806 &oip->saddr, oip->tos);
807 if (err > 1) {
808 ++vxlan->dev->stats.rx_frame_errors;
809 ++vxlan->dev->stats.rx_errors;
810 goto drop;
811 }
812 }
813
Pravin B Shelare8171042013-03-25 14:49:46 +0000814 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000815 u64_stats_update_begin(&stats->syncp);
816 stats->rx_packets++;
817 stats->rx_bytes += skb->len;
818 u64_stats_update_end(&stats->syncp);
819
820 netif_rx(skb);
821
822 return 0;
823error:
824 /* Put UDP header back */
825 __skb_push(skb, sizeof(struct udphdr));
826
827 return 1;
828drop:
829 /* Consume bad packet */
830 kfree_skb(skb);
831 return 0;
832}
833
David Stevense4f67ad2012-11-20 02:50:14 +0000834static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
835{
836 struct vxlan_dev *vxlan = netdev_priv(dev);
837 struct arphdr *parp;
838 u8 *arpptr, *sha;
839 __be32 sip, tip;
840 struct neighbour *n;
841
842 if (dev->flags & IFF_NOARP)
843 goto out;
844
845 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
846 dev->stats.tx_dropped++;
847 goto out;
848 }
849 parp = arp_hdr(skb);
850
851 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
852 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
853 parp->ar_pro != htons(ETH_P_IP) ||
854 parp->ar_op != htons(ARPOP_REQUEST) ||
855 parp->ar_hln != dev->addr_len ||
856 parp->ar_pln != 4)
857 goto out;
858 arpptr = (u8 *)parp + sizeof(struct arphdr);
859 sha = arpptr;
860 arpptr += dev->addr_len; /* sha */
861 memcpy(&sip, arpptr, sizeof(sip));
862 arpptr += sizeof(sip);
863 arpptr += dev->addr_len; /* tha */
864 memcpy(&tip, arpptr, sizeof(tip));
865
866 if (ipv4_is_loopback(tip) ||
867 ipv4_is_multicast(tip))
868 goto out;
869
870 n = neigh_lookup(&arp_tbl, &tip, dev);
871
872 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000873 struct vxlan_fdb *f;
874 struct sk_buff *reply;
875
876 if (!(n->nud_state & NUD_CONNECTED)) {
877 neigh_release(n);
878 goto out;
879 }
880
881 f = vxlan_find_mac(vxlan, n->ha);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700882 if (f && first_remote(f)->remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000883 /* bridge-local neighbor */
884 neigh_release(n);
885 goto out;
886 }
887
888 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
889 n->ha, sha);
890
891 neigh_release(n);
892
893 skb_reset_mac_header(reply);
894 __skb_pull(reply, skb_network_offset(reply));
895 reply->ip_summed = CHECKSUM_UNNECESSARY;
896 reply->pkt_type = PACKET_HOST;
897
898 if (netif_rx_ni(reply) == NET_RX_DROP)
899 dev->stats.rx_dropped++;
900 } else if (vxlan->flags & VXLAN_F_L3MISS)
901 vxlan_ip_miss(dev, tip);
902out:
903 consume_skb(skb);
904 return NETDEV_TX_OK;
905}
906
907static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
908{
909 struct vxlan_dev *vxlan = netdev_priv(dev);
910 struct neighbour *n;
911 struct iphdr *pip;
912
913 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
914 return false;
915
916 n = NULL;
917 switch (ntohs(eth_hdr(skb)->h_proto)) {
918 case ETH_P_IP:
919 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
920 return false;
921 pip = ip_hdr(skb);
922 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
923 break;
924 default:
925 return false;
926 }
927
928 if (n) {
929 bool diff;
930
931 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
932 if (diff) {
933 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
934 dev->addr_len);
935 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
936 }
937 neigh_release(n);
938 return diff;
939 } else if (vxlan->flags & VXLAN_F_L3MISS)
940 vxlan_ip_miss(dev, pip->daddr);
941 return false;
942}
943
stephen hemminger553675f2013-05-16 11:35:20 +0000944static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +0000945{
946 sock_put(skb->sk);
947}
948
949/* On transmit, associate with the tunnel socket */
950static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
951{
stephen hemminger553675f2013-05-16 11:35:20 +0000952 struct vxlan_dev *vxlan = netdev_priv(dev);
953 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +0000954
955 skb_orphan(skb);
956 sock_hold(sk);
957 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +0000958 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +0000959}
960
stephen hemminger05f47d62012-10-09 20:35:50 +0000961/* Compute source port for outgoing packet
962 * first choice to use L4 flow hash since it will spread
963 * better and maybe available from hardware
964 * secondary choice is to use jhash on the Ethernet header
965 */
stephen hemminger7d836a72013-04-27 11:31:56 +0000966static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +0000967{
968 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
969 u32 hash;
970
971 hash = skb_get_rxhash(skb);
972 if (!hash)
973 hash = jhash(skb->data, 2 * ETH_ALEN,
974 (__force u32) skb->protocol);
975
stephen hemminger7d836a72013-04-27 11:31:56 +0000976 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +0000977}
978
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000979static int handle_offloads(struct sk_buff *skb)
980{
981 if (skb_is_gso(skb)) {
982 int err = skb_unclone(skb, GFP_ATOMIC);
983 if (unlikely(err))
984 return err;
985
Dmitry Kravkovf6ace502013-04-28 08:16:01 +0000986 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000987 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
988 skb->ip_summed = CHECKSUM_NONE;
989
990 return 0;
991}
992
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000993/* Bypass encapsulation if the destination is local */
994static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
995 struct vxlan_dev *dst_vxlan)
996{
997 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
998 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
999
1000 skb->pkt_type = PACKET_HOST;
1001 skb->encapsulation = 0;
1002 skb->dev = dst_vxlan->dev;
1003 __skb_pull(skb, skb_network_offset(skb));
1004
1005 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001006 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1007 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001008
1009 u64_stats_update_begin(&tx_stats->syncp);
1010 tx_stats->tx_packets++;
1011 tx_stats->tx_bytes += skb->len;
1012 u64_stats_update_end(&tx_stats->syncp);
1013
1014 if (netif_rx(skb) == NET_RX_SUCCESS) {
1015 u64_stats_update_begin(&rx_stats->syncp);
1016 rx_stats->rx_packets++;
1017 rx_stats->rx_bytes += skb->len;
1018 u64_stats_update_end(&rx_stats->syncp);
1019 } else {
1020 skb->dev->stats.rx_dropped++;
1021 }
1022}
1023
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001024static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1025 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001026{
1027 struct vxlan_dev *vxlan = netdev_priv(dev);
1028 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001029 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001030 struct vxlanhdr *vxh;
1031 struct udphdr *uh;
1032 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001033 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001034 __be16 src_port, dst_port;
David Stevens66817122013-03-15 04:35:51 +00001035 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001036 __be16 df = 0;
1037 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001038 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001039
stephen hemminger823aa872013-04-27 11:31:57 +00001040 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001041 vni = rdst->remote_vni;
1042 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001043
1044 if (!dst) {
1045 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001046 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001047 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001048 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001049 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001050 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001051 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001052
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001053 if (!skb->encapsulation) {
1054 skb_reset_inner_headers(skb);
1055 skb->encapsulation = 1;
1056 }
1057
stephen hemmingerd3428942012-10-01 12:32:35 +00001058 /* Need space for new headers (invalidates iph ptr) */
1059 if (skb_cow_head(skb, VXLAN_HEADROOM))
1060 goto drop;
1061
stephen hemmingerd3428942012-10-01 12:32:35 +00001062 old_iph = ip_hdr(skb);
1063
stephen hemmingerd3428942012-10-01 12:32:35 +00001064 ttl = vxlan->ttl;
1065 if (!ttl && IN_MULTICAST(ntohl(dst)))
1066 ttl = 1;
1067
1068 tos = vxlan->tos;
1069 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001070 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001071
stephen hemminger05f47d62012-10-09 20:35:50 +00001072 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001073
stephen hemmingerca78f182012-10-09 20:35:48 +00001074 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001075 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001076 fl4.flowi4_tos = RT_TOS(tos);
1077 fl4.daddr = dst;
1078 fl4.saddr = vxlan->saddr;
1079
1080 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001081 if (IS_ERR(rt)) {
1082 netdev_dbg(dev, "no route to %pI4\n", &dst);
1083 dev->stats.tx_carrier_errors++;
1084 goto tx_error;
1085 }
1086
1087 if (rt->dst.dev == dev) {
1088 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1089 ip_rt_put(rt);
1090 dev->stats.collisions++;
1091 goto tx_error;
1092 }
1093
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001094 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001095 if (rt->rt_flags & RTCF_LOCAL &&
1096 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001097 struct vxlan_dev *dst_vxlan;
1098
1099 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001100 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001101 if (!dst_vxlan)
1102 goto tx_error;
1103 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001104 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001105 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001106 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1107 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001108 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001109
1110 __skb_push(skb, sizeof(*uh));
1111 skb_reset_transport_header(skb);
1112 uh = udp_hdr(skb);
1113
stephen hemminger73cf3312013-04-27 11:31:54 +00001114 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001115 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001116
1117 uh->len = htons(skb->len);
1118 uh->check = 0;
1119
stephen hemminger1cad8712012-10-09 20:35:49 +00001120 vxlan_set_owner(dev, skb);
1121
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001122 if (handle_offloads(skb))
1123 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001124
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001125 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1126 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1127
1128 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1129 IPPROTO_UDP, tos, ttl, df);
1130 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1131
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001132 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001133
1134drop:
1135 dev->stats.tx_dropped++;
1136 goto tx_free;
1137
1138tx_error:
1139 dev->stats.tx_errors++;
1140tx_free:
1141 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001142}
1143
David Stevens66817122013-03-15 04:35:51 +00001144/* Transmit local packets over Vxlan
1145 *
1146 * Outer IP header inherits ECN and DF from inner header.
1147 * Outer UDP destination is the VXLAN assigned port.
1148 * source port is based on hash of flow
1149 */
1150static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1151{
1152 struct vxlan_dev *vxlan = netdev_priv(dev);
1153 struct ethhdr *eth;
1154 bool did_rsc = false;
Atzm Watanabec7995c42013-04-16 02:50:52 +00001155 struct vxlan_rdst *rdst0, *rdst;
David Stevens66817122013-03-15 04:35:51 +00001156 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001157
1158 skb_reset_mac_header(skb);
1159 eth = eth_hdr(skb);
1160
1161 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1162 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001163
1164 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001165 did_rsc = false;
1166
1167 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1168 ntohs(eth->h_proto) == ETH_P_IP) {
1169 did_rsc = route_shortcircuit(dev, skb);
1170 if (did_rsc)
1171 f = vxlan_find_mac(vxlan, eth->h_dest);
1172 }
1173
David Stevens66817122013-03-15 04:35:51 +00001174 if (f == NULL) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001175 rdst0 = &vxlan->default_dst;
David Stevens66817122013-03-15 04:35:51 +00001176
Atzm Watanabec7995c42013-04-16 02:50:52 +00001177 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
David Stevens66817122013-03-15 04:35:51 +00001178 (vxlan->flags & VXLAN_F_L2MISS) &&
1179 !is_multicast_ether_addr(eth->h_dest))
1180 vxlan_fdb_miss(vxlan, eth->h_dest);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001181 } else {
1182 rdst = rdst0 = first_remote(f);
David Stevens66817122013-03-15 04:35:51 +00001183
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001184 /* if there are multiple destinations, send copies */
1185 list_for_each_entry_continue_rcu(rdst, &f->remotes, list) {
1186 struct sk_buff *skb1;
David Stevens66817122013-03-15 04:35:51 +00001187
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001188 skb1 = skb_clone(skb, GFP_ATOMIC);
1189 if (skb1)
1190 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1191 }
David Stevens66817122013-03-15 04:35:51 +00001192 }
1193
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001194 vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1195 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001196}
1197
stephen hemmingerd3428942012-10-01 12:32:35 +00001198/* Walk the forwarding table and purge stale entries */
1199static void vxlan_cleanup(unsigned long arg)
1200{
1201 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1202 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1203 unsigned int h;
1204
1205 if (!netif_running(vxlan->dev))
1206 return;
1207
1208 spin_lock_bh(&vxlan->hash_lock);
1209 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1210 struct hlist_node *p, *n;
1211 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1212 struct vxlan_fdb *f
1213 = container_of(p, struct vxlan_fdb, hlist);
1214 unsigned long timeout;
1215
stephen hemminger3c172862012-10-26 06:24:34 +00001216 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001217 continue;
1218
1219 timeout = f->used + vxlan->age_interval * HZ;
1220 if (time_before_eq(timeout, jiffies)) {
1221 netdev_dbg(vxlan->dev,
1222 "garbage collect %pM\n",
1223 f->eth_addr);
1224 f->state = NUD_STALE;
1225 vxlan_fdb_destroy(vxlan, f);
1226 } else if (time_before(timeout, next_timer))
1227 next_timer = timeout;
1228 }
1229 }
1230 spin_unlock_bh(&vxlan->hash_lock);
1231
1232 mod_timer(&vxlan->age_timer, next_timer);
1233}
1234
1235/* Setup stats when device is created */
1236static int vxlan_init(struct net_device *dev)
1237{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001238 struct vxlan_dev *vxlan = netdev_priv(dev);
1239 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1240 struct vxlan_sock *vs;
1241 __u32 vni = vxlan->default_dst.remote_vni;
1242
Pravin B Shelare8171042013-03-25 14:49:46 +00001243 dev->tstats = alloc_percpu(struct pcpu_tstats);
1244 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001245 return -ENOMEM;
1246
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001247 spin_lock(&vn->sock_lock);
1248 vs = vxlan_find_port(dev_net(dev), vxlan->dst_port);
1249 if (vs) {
1250 /* If we have a socket with same port already, reuse it */
1251 atomic_inc(&vs->refcnt);
1252 vxlan->vn_sock = vs;
1253 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1254 } else {
1255 /* otherwise make new socket outside of RTNL */
1256 dev_hold(dev);
1257 queue_work(vxlan_wq, &vxlan->sock_work);
1258 }
1259 spin_unlock(&vn->sock_lock);
1260
stephen hemmingerd3428942012-10-01 12:32:35 +00001261 return 0;
1262}
1263
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001264static void vxlan_uninit(struct net_device *dev)
1265{
1266 struct vxlan_dev *vxlan = netdev_priv(dev);
1267 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1268 struct vxlan_sock *vs = vxlan->vn_sock;
1269
1270 if (vs)
1271 vxlan_sock_release(vn, vs);
1272 free_percpu(dev->tstats);
1273}
1274
stephen hemmingerd3428942012-10-01 12:32:35 +00001275/* Start ageing timer and join group when device is brought up */
1276static int vxlan_open(struct net_device *dev)
1277{
1278 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001279 struct vxlan_sock *vs = vxlan->vn_sock;
1280
1281 /* socket hasn't been created */
1282 if (!vs)
1283 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001284
Atzm Watanabec7995c42013-04-16 02:50:52 +00001285 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001286 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001287 dev_hold(dev);
1288 queue_work(vxlan_wq, &vxlan->igmp_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001289 }
1290
1291 if (vxlan->age_interval)
1292 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1293
1294 return 0;
1295}
1296
1297/* Purge the forwarding table */
1298static void vxlan_flush(struct vxlan_dev *vxlan)
1299{
Cong Wang31fec5a2013-05-27 22:35:52 +00001300 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001301
1302 spin_lock_bh(&vxlan->hash_lock);
1303 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1304 struct hlist_node *p, *n;
1305 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1306 struct vxlan_fdb *f
1307 = container_of(p, struct vxlan_fdb, hlist);
1308 vxlan_fdb_destroy(vxlan, f);
1309 }
1310 }
1311 spin_unlock_bh(&vxlan->hash_lock);
1312}
1313
1314/* Cleanup timer and forwarding table on shutdown */
1315static int vxlan_stop(struct net_device *dev)
1316{
1317 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001318 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001319
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001320 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1321 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001322 dev_hold(dev);
1323 queue_work(vxlan_wq, &vxlan->igmp_work);
1324 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001325
1326 del_timer_sync(&vxlan->age_timer);
1327
1328 vxlan_flush(vxlan);
1329
1330 return 0;
1331}
1332
stephen hemmingerd3428942012-10-01 12:32:35 +00001333/* Stub, nothing needs to be done. */
1334static void vxlan_set_multicast_list(struct net_device *dev)
1335{
1336}
1337
1338static const struct net_device_ops vxlan_netdev_ops = {
1339 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001340 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001341 .ndo_open = vxlan_open,
1342 .ndo_stop = vxlan_stop,
1343 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001344 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001345 .ndo_set_rx_mode = vxlan_set_multicast_list,
1346 .ndo_change_mtu = eth_change_mtu,
1347 .ndo_validate_addr = eth_validate_addr,
1348 .ndo_set_mac_address = eth_mac_addr,
1349 .ndo_fdb_add = vxlan_fdb_add,
1350 .ndo_fdb_del = vxlan_fdb_delete,
1351 .ndo_fdb_dump = vxlan_fdb_dump,
1352};
1353
1354/* Info for udev, that this is a virtual tunnel endpoint */
1355static struct device_type vxlan_type = {
1356 .name = "vxlan",
1357};
1358
stephen hemmingerd3428942012-10-01 12:32:35 +00001359/* Initialize the device structure. */
1360static void vxlan_setup(struct net_device *dev)
1361{
1362 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001363 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001364 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001365
1366 eth_hw_addr_random(dev);
1367 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001368 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001369
1370 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001371 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001372 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1373
1374 dev->tx_queue_len = 0;
1375 dev->features |= NETIF_F_LLTX;
1376 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001377 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001378 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001379 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001380
1381 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001382 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001383 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001384 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001385
stephen hemminger553675f2013-05-16 11:35:20 +00001386 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001387 spin_lock_init(&vxlan->hash_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001388 INIT_WORK(&vxlan->igmp_work, vxlan_igmp_work);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001389 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001390
1391 init_timer_deferrable(&vxlan->age_timer);
1392 vxlan->age_timer.function = vxlan_cleanup;
1393 vxlan->age_timer.data = (unsigned long) vxlan;
1394
stephen hemminger05f47d62012-10-09 20:35:50 +00001395 inet_get_local_port_range(&low, &high);
1396 vxlan->port_min = low;
1397 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001398 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001399
stephen hemmingerd3428942012-10-01 12:32:35 +00001400 vxlan->dev = dev;
1401
1402 for (h = 0; h < FDB_HASH_SIZE; ++h)
1403 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1404}
1405
1406static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1407 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001408 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001409 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1410 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1411 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1412 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1413 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1414 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1415 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001416 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001417 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1418 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1419 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1420 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001421 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001422};
1423
1424static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1425{
1426 if (tb[IFLA_ADDRESS]) {
1427 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1428 pr_debug("invalid link address (not ethernet)\n");
1429 return -EINVAL;
1430 }
1431
1432 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1433 pr_debug("invalid all zero ethernet address\n");
1434 return -EADDRNOTAVAIL;
1435 }
1436 }
1437
1438 if (!data)
1439 return -EINVAL;
1440
1441 if (data[IFLA_VXLAN_ID]) {
1442 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1443 if (id >= VXLAN_VID_MASK)
1444 return -ERANGE;
1445 }
1446
stephen hemminger05f47d62012-10-09 20:35:50 +00001447 if (data[IFLA_VXLAN_PORT_RANGE]) {
1448 const struct ifla_vxlan_port_range *p
1449 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1450
1451 if (ntohs(p->high) < ntohs(p->low)) {
1452 pr_debug("port range %u .. %u not valid\n",
1453 ntohs(p->low), ntohs(p->high));
1454 return -EINVAL;
1455 }
1456 }
1457
stephen hemmingerd3428942012-10-01 12:32:35 +00001458 return 0;
1459}
1460
Yan Burman1b13c972013-01-29 23:43:07 +00001461static void vxlan_get_drvinfo(struct net_device *netdev,
1462 struct ethtool_drvinfo *drvinfo)
1463{
1464 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1465 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1466}
1467
1468static const struct ethtool_ops vxlan_ethtool_ops = {
1469 .get_drvinfo = vxlan_get_drvinfo,
1470 .get_link = ethtool_op_get_link,
1471};
1472
stephen hemminger553675f2013-05-16 11:35:20 +00001473static void vxlan_del_work(struct work_struct *work)
1474{
1475 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1476
1477 sk_release_kernel(vs->sock->sk);
1478 kfree_rcu(vs, rcu);
1479}
1480
stephen hemminger553675f2013-05-16 11:35:20 +00001481static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1482{
1483 struct vxlan_sock *vs;
1484 struct sock *sk;
1485 struct sockaddr_in vxlan_addr = {
1486 .sin_family = AF_INET,
1487 .sin_addr.s_addr = htonl(INADDR_ANY),
1488 };
1489 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001490 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001491
1492 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1493 if (!vs)
1494 return ERR_PTR(-ENOMEM);
1495
1496 for (h = 0; h < VNI_HASH_SIZE; ++h)
1497 INIT_HLIST_HEAD(&vs->vni_list[h]);
1498
1499 INIT_WORK(&vs->del_work, vxlan_del_work);
1500
1501 /* Create UDP socket for encapsulation receive. */
1502 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1503 if (rc < 0) {
1504 pr_debug("UDP socket create failed\n");
1505 kfree(vs);
1506 return ERR_PTR(rc);
1507 }
1508
1509 /* Put in proper namespace */
1510 sk = vs->sock->sk;
1511 sk_change_net(sk, net);
1512
1513 vxlan_addr.sin_port = port;
1514
1515 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1516 sizeof(vxlan_addr));
1517 if (rc < 0) {
1518 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1519 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1520 sk_release_kernel(sk);
1521 kfree(vs);
1522 return ERR_PTR(rc);
1523 }
1524
1525 /* Disable multicast loopback */
1526 inet_sk(sk)->mc_loop = 0;
1527
1528 /* Mark socket as an encapsulation socket. */
1529 udp_sk(sk)->encap_type = 1;
1530 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1531 udp_encap_enable();
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001532 atomic_set(&vs->refcnt, 1);
stephen hemminger553675f2013-05-16 11:35:20 +00001533
stephen hemminger553675f2013-05-16 11:35:20 +00001534 return vs;
1535}
1536
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001537/* Scheduled at device creation to bind to a socket */
1538static void vxlan_sock_work(struct work_struct *work)
1539{
1540 struct vxlan_dev *vxlan
1541 = container_of(work, struct vxlan_dev, sock_work);
1542 struct net_device *dev = vxlan->dev;
1543 struct net *net = dev_net(dev);
1544 __u32 vni = vxlan->default_dst.remote_vni;
1545 __be16 port = vxlan->dst_port;
1546 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1547 struct vxlan_sock *nvs, *ovs;
1548
1549 nvs = vxlan_socket_create(net, port);
1550 if (IS_ERR(nvs)) {
1551 netdev_err(vxlan->dev, "Can not create UDP socket, %ld\n",
1552 PTR_ERR(nvs));
1553 goto out;
1554 }
1555
1556 spin_lock(&vn->sock_lock);
1557 /* Look again to see if can reuse socket */
1558 ovs = vxlan_find_port(net, port);
1559 if (ovs) {
1560 atomic_inc(&ovs->refcnt);
1561 vxlan->vn_sock = ovs;
1562 hlist_add_head_rcu(&vxlan->hlist, vni_head(ovs, vni));
1563 spin_unlock(&vn->sock_lock);
1564
1565 sk_release_kernel(nvs->sock->sk);
1566 kfree(nvs);
1567 } else {
1568 vxlan->vn_sock = nvs;
1569 hlist_add_head_rcu(&nvs->hlist, vs_head(net, port));
1570 hlist_add_head_rcu(&vxlan->hlist, vni_head(nvs, vni));
1571 spin_unlock(&vn->sock_lock);
1572 }
1573out:
1574 dev_put(dev);
1575}
1576
stephen hemmingerd3428942012-10-01 12:32:35 +00001577static int vxlan_newlink(struct net *net, struct net_device *dev,
1578 struct nlattr *tb[], struct nlattr *data[])
1579{
stephen hemminger553675f2013-05-16 11:35:20 +00001580 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001581 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001582 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001583 __u32 vni;
1584 int err;
1585
1586 if (!data[IFLA_VXLAN_ID])
1587 return -EINVAL;
1588
1589 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001590 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001591
stephen hemminger5d174dd2013-04-27 11:31:55 +00001592 if (data[IFLA_VXLAN_GROUP])
1593 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001594
1595 if (data[IFLA_VXLAN_LOCAL])
1596 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1597
stephen hemminger34e02aa2012-10-09 20:35:53 +00001598 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001599 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001600 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001601 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001602
stephen hemminger34e02aa2012-10-09 20:35:53 +00001603 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001604 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001605 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001606 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001607
1608 if (!tb[IFLA_MTU])
1609 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001610
1611 /* update header length based on lower device */
1612 dev->hard_header_len = lowerdev->hard_header_len +
1613 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001614 }
1615
1616 if (data[IFLA_VXLAN_TOS])
1617 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1618
Vincent Bernatafb97182012-10-30 10:27:16 +00001619 if (data[IFLA_VXLAN_TTL])
1620 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1621
stephen hemmingerd3428942012-10-01 12:32:35 +00001622 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001623 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001624
1625 if (data[IFLA_VXLAN_AGEING])
1626 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1627 else
1628 vxlan->age_interval = FDB_AGE_DEFAULT;
1629
David Stevense4f67ad2012-11-20 02:50:14 +00001630 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1631 vxlan->flags |= VXLAN_F_PROXY;
1632
1633 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1634 vxlan->flags |= VXLAN_F_RSC;
1635
1636 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1637 vxlan->flags |= VXLAN_F_L2MISS;
1638
1639 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1640 vxlan->flags |= VXLAN_F_L3MISS;
1641
stephen hemmingerd3428942012-10-01 12:32:35 +00001642 if (data[IFLA_VXLAN_LIMIT])
1643 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1644
stephen hemminger05f47d62012-10-09 20:35:50 +00001645 if (data[IFLA_VXLAN_PORT_RANGE]) {
1646 const struct ifla_vxlan_port_range *p
1647 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1648 vxlan->port_min = ntohs(p->low);
1649 vxlan->port_max = ntohs(p->high);
1650 }
1651
stephen hemminger823aa872013-04-27 11:31:57 +00001652 if (data[IFLA_VXLAN_PORT])
1653 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1654
stephen hemminger553675f2013-05-16 11:35:20 +00001655 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1656 pr_info("duplicate VNI %u\n", vni);
1657 return -EEXIST;
1658 }
1659
Yan Burman1b13c972013-01-29 23:43:07 +00001660 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1661
stephen hemmingerd3428942012-10-01 12:32:35 +00001662 err = register_netdevice(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001663 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001664 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001665
stephen hemminger553675f2013-05-16 11:35:20 +00001666 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001667
1668 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001669}
1670
1671static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1672{
1673 struct vxlan_dev *vxlan = netdev_priv(dev);
1674
1675 hlist_del_rcu(&vxlan->hlist);
stephen hemminger553675f2013-05-16 11:35:20 +00001676 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001677 unregister_netdevice_queue(dev, head);
1678}
1679
1680static size_t vxlan_get_size(const struct net_device *dev)
1681{
1682
1683 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001684 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001685 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1686 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1687 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1688 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1689 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001690 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1691 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1692 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1693 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001694 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1695 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001696 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001697 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001698 0;
1699}
1700
1701static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1702{
1703 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001704 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001705 struct ifla_vxlan_port_range ports = {
1706 .low = htons(vxlan->port_min),
1707 .high = htons(vxlan->port_max),
1708 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001709
Atzm Watanabec7995c42013-04-16 02:50:52 +00001710 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001711 goto nla_put_failure;
1712
stephen hemminger5d174dd2013-04-27 11:31:55 +00001713 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001714 goto nla_put_failure;
1715
Atzm Watanabec7995c42013-04-16 02:50:52 +00001716 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001717 goto nla_put_failure;
1718
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001719 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001720 goto nla_put_failure;
1721
1722 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1723 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001724 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1725 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1726 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1727 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1728 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1729 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1730 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1731 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1732 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001733 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001734 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1735 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001736 goto nla_put_failure;
1737
stephen hemminger05f47d62012-10-09 20:35:50 +00001738 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1739 goto nla_put_failure;
1740
stephen hemmingerd3428942012-10-01 12:32:35 +00001741 return 0;
1742
1743nla_put_failure:
1744 return -EMSGSIZE;
1745}
1746
1747static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1748 .kind = "vxlan",
1749 .maxtype = IFLA_VXLAN_MAX,
1750 .policy = vxlan_policy,
1751 .priv_size = sizeof(struct vxlan_dev),
1752 .setup = vxlan_setup,
1753 .validate = vxlan_validate,
1754 .newlink = vxlan_newlink,
1755 .dellink = vxlan_dellink,
1756 .get_size = vxlan_get_size,
1757 .fill_info = vxlan_fill_info,
1758};
1759
1760static __net_init int vxlan_init_net(struct net *net)
1761{
1762 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001763 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001764
stephen hemminger553675f2013-05-16 11:35:20 +00001765 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001766 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001767
stephen hemminger553675f2013-05-16 11:35:20 +00001768 for (h = 0; h < PORT_HASH_SIZE; ++h)
1769 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001770
1771 return 0;
1772}
1773
1774static __net_exit void vxlan_exit_net(struct net *net)
1775{
1776 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001777 struct vxlan_dev *vxlan;
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001778
1779 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001780 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1781 dev_close(vxlan->dev);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001782 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001783}
1784
1785static struct pernet_operations vxlan_net_ops = {
1786 .init = vxlan_init_net,
1787 .exit = vxlan_exit_net,
1788 .id = &vxlan_net_id,
1789 .size = sizeof(struct vxlan_net),
1790};
1791
1792static int __init vxlan_init_module(void)
1793{
1794 int rc;
1795
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001796 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1797 if (!vxlan_wq)
1798 return -ENOMEM;
1799
stephen hemmingerd3428942012-10-01 12:32:35 +00001800 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1801
1802 rc = register_pernet_device(&vxlan_net_ops);
1803 if (rc)
1804 goto out1;
1805
1806 rc = rtnl_link_register(&vxlan_link_ops);
1807 if (rc)
1808 goto out2;
1809
1810 return 0;
1811
1812out2:
1813 unregister_pernet_device(&vxlan_net_ops);
1814out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001815 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00001816 return rc;
1817}
Cong Wang7332a132013-05-27 22:35:53 +00001818late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00001819
1820static void __exit vxlan_cleanup_module(void)
1821{
stephen hemmingerd3428942012-10-01 12:32:35 +00001822 unregister_pernet_device(&vxlan_net_ops);
Stephen Hemmingerb7153982013-06-17 14:16:09 -07001823 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001824 destroy_workqueue(vxlan_wq);
David Stevens66817122013-03-15 04:35:51 +00001825 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001826}
1827module_exit(vxlan_cleanup_module);
1828
1829MODULE_LICENSE("GPL");
1830MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00001831MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00001832MODULE_ALIAS_RTNL_LINK("vxlan");