blob: c1825201f9e278ad3f862e55c3b4e17535437164 [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 Hemminger7c47ced2013-06-17 14:16:10 -0700139 struct work_struct igmp_work;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700140
stephen hemmingerd3428942012-10-01 12:32:35 +0000141 unsigned long age_interval;
142 struct timer_list age_timer;
143 spinlock_t hash_lock;
144 unsigned int addrcnt;
145 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000146
147 struct hlist_head fdb_head[FDB_HASH_SIZE];
148};
149
David Stevense4f67ad2012-11-20 02:50:14 +0000150#define VXLAN_F_LEARN 0x01
151#define VXLAN_F_PROXY 0x02
152#define VXLAN_F_RSC 0x04
153#define VXLAN_F_L2MISS 0x08
154#define VXLAN_F_L3MISS 0x10
155
stephen hemmingerd3428942012-10-01 12:32:35 +0000156/* salt for hash table */
157static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700158static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000159
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700160static void vxlan_sock_work(struct work_struct *work);
161
stephen hemminger553675f2013-05-16 11:35:20 +0000162/* Virtual Network hash table head */
163static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
164{
165 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
166}
167
168/* Socket hash table head */
169static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000170{
171 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
172
stephen hemminger553675f2013-05-16 11:35:20 +0000173 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
174}
175
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700176/* First remote destination for a forwarding entry.
177 * Guaranteed to be non-NULL because remotes are never deleted.
178 */
179static inline struct vxlan_rdst *first_remote(struct vxlan_fdb *fdb)
180{
181 return list_first_or_null_rcu(&fdb->remotes, struct vxlan_rdst, list);
182}
183
stephen hemminger553675f2013-05-16 11:35:20 +0000184/* Find VXLAN socket based on network namespace and UDP port */
185static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
186{
187 struct vxlan_sock *vs;
188
189 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
190 if (inet_sk(vs->sock->sk)->inet_sport == port)
191 return vs;
192 }
193 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000194}
195
196/* Look up VNI in a per net namespace table */
stephen hemminger553675f2013-05-16 11:35:20 +0000197static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000198{
stephen hemminger553675f2013-05-16 11:35:20 +0000199 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000200 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000201
stephen hemminger553675f2013-05-16 11:35:20 +0000202 vs = vxlan_find_port(net, port);
203 if (!vs)
204 return NULL;
205
206 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000207 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000208 return vxlan;
209 }
210
211 return NULL;
212}
213
214/* Fill in neighbour message in skbuff. */
215static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700216 const struct vxlan_fdb *fdb,
217 u32 portid, u32 seq, int type, unsigned int flags,
218 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000219{
220 unsigned long now = jiffies;
221 struct nda_cacheinfo ci;
222 struct nlmsghdr *nlh;
223 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000224 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000225
226 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
227 if (nlh == NULL)
228 return -EMSGSIZE;
229
230 ndm = nlmsg_data(nlh);
231 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000232
233 send_eth = send_ip = true;
234
235 if (type == RTM_GETNEIGH) {
236 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000237 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000238 send_eth = !is_zero_ether_addr(fdb->eth_addr);
239 } else
240 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000241 ndm->ndm_state = fdb->state;
242 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000243 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000244 ndm->ndm_type = NDA_DST;
245
David Stevense4f67ad2012-11-20 02:50:14 +0000246 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000247 goto nla_put_failure;
248
David Stevens66817122013-03-15 04:35:51 +0000249 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
250 goto nla_put_failure;
251
stephen hemminger823aa872013-04-27 11:31:57 +0000252 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000253 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
254 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000255 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700256 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000257 goto nla_put_failure;
258 if (rdst->remote_ifindex &&
259 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000260 goto nla_put_failure;
261
262 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
263 ci.ndm_confirmed = 0;
264 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
265 ci.ndm_refcnt = 0;
266
267 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
268 goto nla_put_failure;
269
270 return nlmsg_end(skb, nlh);
271
272nla_put_failure:
273 nlmsg_cancel(skb, nlh);
274 return -EMSGSIZE;
275}
276
277static inline size_t vxlan_nlmsg_size(void)
278{
279 return NLMSG_ALIGN(sizeof(struct ndmsg))
280 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
281 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000282 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000283 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
284 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000285 + nla_total_size(sizeof(struct nda_cacheinfo));
286}
287
288static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700289 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000290{
291 struct net *net = dev_net(vxlan->dev);
292 struct sk_buff *skb;
293 int err = -ENOBUFS;
294
295 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
296 if (skb == NULL)
297 goto errout;
298
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700299 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, first_remote(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000300 if (err < 0) {
301 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
302 WARN_ON(err == -EMSGSIZE);
303 kfree_skb(skb);
304 goto errout;
305 }
306
307 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
308 return;
309errout:
310 if (err < 0)
311 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
312}
313
David Stevense4f67ad2012-11-20 02:50:14 +0000314static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
315{
316 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700317 struct vxlan_fdb f = {
318 .state = NUD_STALE,
319 };
320 struct vxlan_rdst remote = {
321 .remote_ip = ipa, /* goes to NDA_DST */
322 .remote_vni = VXLAN_N_VID,
323 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700324
325 INIT_LIST_HEAD(&f.remotes);
326 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000327
328 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
329}
330
331static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
332{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700333 struct vxlan_fdb f = {
334 .state = NUD_STALE,
335 };
David Stevense4f67ad2012-11-20 02:50:14 +0000336
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700337 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000338 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
339
340 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
341}
342
stephen hemmingerd3428942012-10-01 12:32:35 +0000343/* Hash Ethernet address */
344static u32 eth_hash(const unsigned char *addr)
345{
346 u64 value = get_unaligned((u64 *)addr);
347
348 /* only want 6 bytes */
349#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000350 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000351#else
352 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000353#endif
354 return hash_64(value, FDB_HASH_BITS);
355}
356
357/* Hash chain to use given mac address */
358static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
359 const u8 *mac)
360{
361 return &vxlan->fdb_head[eth_hash(mac)];
362}
363
364/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000365static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000366 const u8 *mac)
367
368{
369 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
370 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000371
Sasha Levinb67bfe02013-02-27 17:06:00 -0800372 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000373 if (compare_ether_addr(mac, f->eth_addr) == 0)
374 return f;
375 }
376
377 return NULL;
378}
379
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000380static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
381 const u8 *mac)
382{
383 struct vxlan_fdb *f;
384
385 f = __vxlan_find_mac(vxlan, mac);
386 if (f)
387 f->used = jiffies;
388
389 return f;
390}
391
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300392/* caller should hold vxlan->hash_lock */
393static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
394 __be32 ip, __be16 port,
395 __u32 vni, __u32 ifindex)
396{
397 struct vxlan_rdst *rd;
398
399 list_for_each_entry(rd, &f->remotes, list) {
400 if (rd->remote_ip == ip &&
401 rd->remote_port == port &&
402 rd->remote_vni == vni &&
403 rd->remote_ifindex == ifindex)
404 return rd;
405 }
406
407 return NULL;
408}
409
David Stevens66817122013-03-15 04:35:51 +0000410/* Add/update destinations for multicast */
411static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000412 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000413{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700414 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000415
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300416 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
417 if (rd)
418 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700419
David Stevens66817122013-03-15 04:35:51 +0000420 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
421 if (rd == NULL)
422 return -ENOBUFS;
423 rd->remote_ip = ip;
424 rd->remote_port = port;
425 rd->remote_vni = vni;
426 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700427
428 list_add_tail_rcu(&rd->list, &f->remotes);
429
David Stevens66817122013-03-15 04:35:51 +0000430 return 1;
431}
432
stephen hemmingerd3428942012-10-01 12:32:35 +0000433/* Add new entry to forwarding table -- assumes lock held */
434static int vxlan_fdb_create(struct vxlan_dev *vxlan,
435 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000436 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000437 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000438 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000439{
440 struct vxlan_fdb *f;
441 int notify = 0;
442
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000443 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000444 if (f) {
445 if (flags & NLM_F_EXCL) {
446 netdev_dbg(vxlan->dev,
447 "lost race to create %pM\n", mac);
448 return -EEXIST;
449 }
450 if (f->state != state) {
451 f->state = state;
452 f->updated = jiffies;
453 notify = 1;
454 }
David Stevensae884082013-04-19 00:36:26 +0000455 if (f->flags != ndm_flags) {
456 f->flags = ndm_flags;
457 f->updated = jiffies;
458 notify = 1;
459 }
David Stevens66817122013-03-15 04:35:51 +0000460 if ((flags & NLM_F_APPEND) &&
461 is_multicast_ether_addr(f->eth_addr)) {
462 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
463
464 if (rc < 0)
465 return rc;
466 notify |= rc;
467 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000468 } else {
469 if (!(flags & NLM_F_CREATE))
470 return -ENOENT;
471
472 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
473 return -ENOSPC;
474
475 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
476 f = kmalloc(sizeof(*f), GFP_ATOMIC);
477 if (!f)
478 return -ENOMEM;
479
480 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000481 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000482 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000483 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700484 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000485 memcpy(f->eth_addr, mac, ETH_ALEN);
486
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700487 vxlan_fdb_append(f, ip, port, vni, ifindex);
488
stephen hemmingerd3428942012-10-01 12:32:35 +0000489 ++vxlan->addrcnt;
490 hlist_add_head_rcu(&f->hlist,
491 vxlan_fdb_head(vxlan, mac));
492 }
493
494 if (notify)
495 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
496
497 return 0;
498}
499
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300500static void vxlan_fdb_free_rdst(struct rcu_head *head)
501{
502 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
503 kfree(rd);
504}
505
Wei Yongjun6706c822013-04-11 19:00:35 +0000506static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000507{
508 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700509 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000510
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700511 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000512 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000513 kfree(f);
514}
515
stephen hemmingerd3428942012-10-01 12:32:35 +0000516static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
517{
518 netdev_dbg(vxlan->dev,
519 "delete %pM\n", f->eth_addr);
520
521 --vxlan->addrcnt;
522 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
523
524 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000525 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000526}
527
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300528static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
529 __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
530{
531 struct net *net = dev_net(vxlan->dev);
532
533 if (tb[NDA_DST]) {
534 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
535 return -EAFNOSUPPORT;
536
537 *ip = nla_get_be32(tb[NDA_DST]);
538 } else {
539 *ip = htonl(INADDR_ANY);
540 }
541
542 if (tb[NDA_PORT]) {
543 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
544 return -EINVAL;
545 *port = nla_get_be16(tb[NDA_PORT]);
546 } else {
547 *port = vxlan->dst_port;
548 }
549
550 if (tb[NDA_VNI]) {
551 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
552 return -EINVAL;
553 *vni = nla_get_u32(tb[NDA_VNI]);
554 } else {
555 *vni = vxlan->default_dst.remote_vni;
556 }
557
558 if (tb[NDA_IFINDEX]) {
559 struct net_device *tdev;
560
561 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
562 return -EINVAL;
563 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
564 tdev = dev_get_by_index(net, *ifindex);
565 if (!tdev)
566 return -EADDRNOTAVAIL;
567 dev_put(tdev);
568 } else {
569 *ifindex = 0;
570 }
571
572 return 0;
573}
574
stephen hemmingerd3428942012-10-01 12:32:35 +0000575/* Add static entry (via netlink) */
576static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
577 struct net_device *dev,
578 const unsigned char *addr, u16 flags)
579{
580 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300581 /* struct net *net = dev_net(vxlan->dev); */
stephen hemmingerd3428942012-10-01 12:32:35 +0000582 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000583 __be16 port;
584 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000585 int err;
586
587 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
588 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
589 ndm->ndm_state);
590 return -EINVAL;
591 }
592
593 if (tb[NDA_DST] == NULL)
594 return -EINVAL;
595
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300596 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
597 if (err)
598 return err;
David Stevens66817122013-03-15 04:35:51 +0000599
stephen hemmingerd3428942012-10-01 12:32:35 +0000600 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000601 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
602 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000603 spin_unlock_bh(&vxlan->hash_lock);
604
605 return err;
606}
607
608/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000609static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
610 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000611 const unsigned char *addr)
612{
613 struct vxlan_dev *vxlan = netdev_priv(dev);
614 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300615 struct vxlan_rdst *rd = NULL;
616 __be32 ip;
617 __be16 port;
618 u32 vni, ifindex;
619 int err;
620
621 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
622 if (err)
623 return err;
624
625 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000626
627 spin_lock_bh(&vxlan->hash_lock);
628 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300629 if (!f)
630 goto out;
631
632 if (ip != htonl(INADDR_ANY)) {
633 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
634 if (!rd)
635 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000636 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300637
638 err = 0;
639
640 /* remove a destination if it's not the only one on the list,
641 * otherwise destroy the fdb entry
642 */
643 if (rd && !list_is_singular(&f->remotes)) {
644 list_del_rcu(&rd->list);
645 call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
646 goto out;
647 }
648
649 vxlan_fdb_destroy(vxlan, f);
650
651out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000652 spin_unlock_bh(&vxlan->hash_lock);
653
654 return err;
655}
656
657/* Dump forwarding table */
658static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
659 struct net_device *dev, int idx)
660{
661 struct vxlan_dev *vxlan = netdev_priv(dev);
662 unsigned int h;
663
664 for (h = 0; h < FDB_HASH_SIZE; ++h) {
665 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000666 int err;
667
Sasha Levinb67bfe02013-02-27 17:06:00 -0800668 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000669 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000670
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700671 if (idx < cb->args[0])
672 goto skip;
673
674 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000675 err = vxlan_fdb_info(skb, vxlan, f,
676 NETLINK_CB(cb->skb).portid,
677 cb->nlh->nlmsg_seq,
678 RTM_NEWNEIGH,
679 NLM_F_MULTI, rd);
680 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700681 goto out;
David Stevens66817122013-03-15 04:35:51 +0000682 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700683skip:
684 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000685 }
686 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700687out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000688 return idx;
689}
690
691/* Watch incoming packets to learn mapping between Ethernet address
692 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700693 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000694 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700695static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000696 __be32 src_ip, const u8 *src_mac)
697{
698 struct vxlan_dev *vxlan = netdev_priv(dev);
699 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000700
701 f = vxlan_find_mac(vxlan, src_mac);
702 if (likely(f)) {
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700703 struct vxlan_rdst *rdst = first_remote(f);
704
705 if (likely(rdst->remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700706 return false;
707
708 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700709 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700710 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000711
712 if (net_ratelimit())
713 netdev_info(dev,
714 "%pM migrated from %pI4 to %pI4\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700715 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000716
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700717 rdst->remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000718 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700719 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000720 } else {
721 /* learned new entry */
722 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700723
724 /* close off race between vxlan_flush and incoming packets */
725 if (netif_running(dev))
726 vxlan_fdb_create(vxlan, src_mac, src_ip,
727 NUD_REACHABLE,
728 NLM_F_EXCL|NLM_F_CREATE,
729 vxlan->dst_port,
730 vxlan->default_dst.remote_vni,
731 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000732 spin_unlock(&vxlan->hash_lock);
733 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700734
735 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000736}
737
738
739/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700740static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000741{
stephen hemminger553675f2013-05-16 11:35:20 +0000742 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000743
stephen hemminger553675f2013-05-16 11:35:20 +0000744 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000745 if (!netif_running(vxlan->dev))
746 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000747
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700748 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000749 return true;
750 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000751
752 return false;
753}
754
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700755static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000756{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700757 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000758}
759
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700760static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000761{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700762 if (!atomic_dec_and_test(&vs->refcnt))
763 return;
764
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700765 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700766 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700767 spin_unlock(&vn->sock_lock);
768
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700769 queue_work(vxlan_wq, &vs->del_work);
770}
771
772/* Callback to update multicast group membership.
773 * Scheduled when vxlan goes up/down.
774 */
775static void vxlan_igmp_work(struct work_struct *work)
776{
777 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_work);
778 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
779 struct vxlan_sock *vs = vxlan->vn_sock;
780 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000781 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000782 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
783 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000784 };
785
stephen hemmingerd3428942012-10-01 12:32:35 +0000786 lock_sock(sk);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700787 if (vxlan_group_used(vn, vxlan->default_dst.remote_ip))
788 ip_mc_join_group(sk, &mreq);
789 else
790 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000791 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000792
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700793 vxlan_sock_release(vn, vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700794 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000795}
796
797/* Callback from net/ipv4/udp.c to receive packets */
798static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
799{
800 struct iphdr *oip;
801 struct vxlanhdr *vxh;
802 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000803 struct pcpu_tstats *stats;
stephen hemminger553675f2013-05-16 11:35:20 +0000804 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000805 __u32 vni;
806 int err;
807
808 /* pop off outer UDP header */
809 __skb_pull(skb, sizeof(struct udphdr));
810
811 /* Need Vxlan and inner Ethernet header to be present */
812 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
813 goto error;
814
815 /* Drop packets with reserved bits set */
816 vxh = (struct vxlanhdr *) skb->data;
817 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
818 (vxh->vx_vni & htonl(0xff))) {
819 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
820 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
821 goto error;
822 }
823
824 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000825
826 /* Is this VNI defined? */
827 vni = ntohl(vxh->vx_vni) >> 8;
stephen hemminger553675f2013-05-16 11:35:20 +0000828 port = inet_sk(sk)->inet_sport;
829 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000830 if (!vxlan) {
stephen hemminger553675f2013-05-16 11:35:20 +0000831 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
832 vni, ntohs(port));
stephen hemmingerd3428942012-10-01 12:32:35 +0000833 goto drop;
834 }
835
836 if (!pskb_may_pull(skb, ETH_HLEN)) {
837 vxlan->dev->stats.rx_length_errors++;
838 vxlan->dev->stats.rx_errors++;
839 goto drop;
840 }
841
David Stevense4f67ad2012-11-20 02:50:14 +0000842 skb_reset_mac_header(skb);
843
stephen hemmingerd3428942012-10-01 12:32:35 +0000844 /* Re-examine inner Ethernet packet */
845 oip = ip_hdr(skb);
846 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000847
848 /* Ignore packet loops (and multicast echo) */
849 if (compare_ether_addr(eth_hdr(skb)->h_source,
850 vxlan->dev->dev_addr) == 0)
851 goto drop;
852
stephen hemminger26a41ae62013-06-17 12:09:58 -0700853 if ((vxlan->flags & VXLAN_F_LEARN) &&
854 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
855 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000856
857 __skb_tunnel_rx(skb, vxlan->dev);
858 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000859
860 /* If the NIC driver gave us an encapsulated packet with
861 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
862 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
863 * for us. Otherwise force the upper layers to verify it.
864 */
865 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
866 !(vxlan->dev->features & NETIF_F_RXCSUM))
867 skb->ip_summed = CHECKSUM_NONE;
868
869 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000870
871 err = IP_ECN_decapsulate(oip, skb);
872 if (unlikely(err)) {
873 if (log_ecn_error)
874 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
875 &oip->saddr, oip->tos);
876 if (err > 1) {
877 ++vxlan->dev->stats.rx_frame_errors;
878 ++vxlan->dev->stats.rx_errors;
879 goto drop;
880 }
881 }
882
Pravin B Shelare8171042013-03-25 14:49:46 +0000883 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000884 u64_stats_update_begin(&stats->syncp);
885 stats->rx_packets++;
886 stats->rx_bytes += skb->len;
887 u64_stats_update_end(&stats->syncp);
888
889 netif_rx(skb);
890
891 return 0;
892error:
893 /* Put UDP header back */
894 __skb_push(skb, sizeof(struct udphdr));
895
896 return 1;
897drop:
898 /* Consume bad packet */
899 kfree_skb(skb);
900 return 0;
901}
902
David Stevense4f67ad2012-11-20 02:50:14 +0000903static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
904{
905 struct vxlan_dev *vxlan = netdev_priv(dev);
906 struct arphdr *parp;
907 u8 *arpptr, *sha;
908 __be32 sip, tip;
909 struct neighbour *n;
910
911 if (dev->flags & IFF_NOARP)
912 goto out;
913
914 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
915 dev->stats.tx_dropped++;
916 goto out;
917 }
918 parp = arp_hdr(skb);
919
920 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
921 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
922 parp->ar_pro != htons(ETH_P_IP) ||
923 parp->ar_op != htons(ARPOP_REQUEST) ||
924 parp->ar_hln != dev->addr_len ||
925 parp->ar_pln != 4)
926 goto out;
927 arpptr = (u8 *)parp + sizeof(struct arphdr);
928 sha = arpptr;
929 arpptr += dev->addr_len; /* sha */
930 memcpy(&sip, arpptr, sizeof(sip));
931 arpptr += sizeof(sip);
932 arpptr += dev->addr_len; /* tha */
933 memcpy(&tip, arpptr, sizeof(tip));
934
935 if (ipv4_is_loopback(tip) ||
936 ipv4_is_multicast(tip))
937 goto out;
938
939 n = neigh_lookup(&arp_tbl, &tip, dev);
940
941 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000942 struct vxlan_fdb *f;
943 struct sk_buff *reply;
944
945 if (!(n->nud_state & NUD_CONNECTED)) {
946 neigh_release(n);
947 goto out;
948 }
949
950 f = vxlan_find_mac(vxlan, n->ha);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700951 if (f && first_remote(f)->remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000952 /* bridge-local neighbor */
953 neigh_release(n);
954 goto out;
955 }
956
957 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
958 n->ha, sha);
959
960 neigh_release(n);
961
962 skb_reset_mac_header(reply);
963 __skb_pull(reply, skb_network_offset(reply));
964 reply->ip_summed = CHECKSUM_UNNECESSARY;
965 reply->pkt_type = PACKET_HOST;
966
967 if (netif_rx_ni(reply) == NET_RX_DROP)
968 dev->stats.rx_dropped++;
969 } else if (vxlan->flags & VXLAN_F_L3MISS)
970 vxlan_ip_miss(dev, tip);
971out:
972 consume_skb(skb);
973 return NETDEV_TX_OK;
974}
975
976static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
977{
978 struct vxlan_dev *vxlan = netdev_priv(dev);
979 struct neighbour *n;
980 struct iphdr *pip;
981
982 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
983 return false;
984
985 n = NULL;
986 switch (ntohs(eth_hdr(skb)->h_proto)) {
987 case ETH_P_IP:
988 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
989 return false;
990 pip = ip_hdr(skb);
991 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
992 break;
993 default:
994 return false;
995 }
996
997 if (n) {
998 bool diff;
999
1000 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1001 if (diff) {
1002 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1003 dev->addr_len);
1004 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1005 }
1006 neigh_release(n);
1007 return diff;
1008 } else if (vxlan->flags & VXLAN_F_L3MISS)
1009 vxlan_ip_miss(dev, pip->daddr);
1010 return false;
1011}
1012
stephen hemminger553675f2013-05-16 11:35:20 +00001013static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001014{
1015 sock_put(skb->sk);
1016}
1017
1018/* On transmit, associate with the tunnel socket */
1019static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1020{
stephen hemminger553675f2013-05-16 11:35:20 +00001021 struct vxlan_dev *vxlan = netdev_priv(dev);
1022 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +00001023
1024 skb_orphan(skb);
1025 sock_hold(sk);
1026 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001027 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001028}
1029
stephen hemminger05f47d62012-10-09 20:35:50 +00001030/* Compute source port for outgoing packet
1031 * first choice to use L4 flow hash since it will spread
1032 * better and maybe available from hardware
1033 * secondary choice is to use jhash on the Ethernet header
1034 */
stephen hemminger7d836a72013-04-27 11:31:56 +00001035static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001036{
1037 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1038 u32 hash;
1039
1040 hash = skb_get_rxhash(skb);
1041 if (!hash)
1042 hash = jhash(skb->data, 2 * ETH_ALEN,
1043 (__force u32) skb->protocol);
1044
stephen hemminger7d836a72013-04-27 11:31:56 +00001045 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001046}
1047
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001048static int handle_offloads(struct sk_buff *skb)
1049{
1050 if (skb_is_gso(skb)) {
1051 int err = skb_unclone(skb, GFP_ATOMIC);
1052 if (unlikely(err))
1053 return err;
1054
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001055 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001056 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1057 skb->ip_summed = CHECKSUM_NONE;
1058
1059 return 0;
1060}
1061
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001062/* Bypass encapsulation if the destination is local */
1063static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1064 struct vxlan_dev *dst_vxlan)
1065{
1066 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1067 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1068
1069 skb->pkt_type = PACKET_HOST;
1070 skb->encapsulation = 0;
1071 skb->dev = dst_vxlan->dev;
1072 __skb_pull(skb, skb_network_offset(skb));
1073
1074 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001075 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1076 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001077
1078 u64_stats_update_begin(&tx_stats->syncp);
1079 tx_stats->tx_packets++;
1080 tx_stats->tx_bytes += skb->len;
1081 u64_stats_update_end(&tx_stats->syncp);
1082
1083 if (netif_rx(skb) == NET_RX_SUCCESS) {
1084 u64_stats_update_begin(&rx_stats->syncp);
1085 rx_stats->rx_packets++;
1086 rx_stats->rx_bytes += skb->len;
1087 u64_stats_update_end(&rx_stats->syncp);
1088 } else {
1089 skb->dev->stats.rx_dropped++;
1090 }
1091}
1092
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001093static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1094 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001095{
1096 struct vxlan_dev *vxlan = netdev_priv(dev);
1097 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001098 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001099 struct vxlanhdr *vxh;
1100 struct udphdr *uh;
1101 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001102 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001103 __be16 src_port, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001104 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001105 __be16 df = 0;
1106 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001107 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001108
stephen hemminger823aa872013-04-27 11:31:57 +00001109 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001110 vni = rdst->remote_vni;
1111 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001112
1113 if (!dst) {
1114 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001115 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001116 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001117 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001118 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001119 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001120 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001121
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001122 if (!skb->encapsulation) {
1123 skb_reset_inner_headers(skb);
1124 skb->encapsulation = 1;
1125 }
1126
stephen hemmingerd3428942012-10-01 12:32:35 +00001127 /* Need space for new headers (invalidates iph ptr) */
1128 if (skb_cow_head(skb, VXLAN_HEADROOM))
1129 goto drop;
1130
stephen hemmingerd3428942012-10-01 12:32:35 +00001131 old_iph = ip_hdr(skb);
1132
stephen hemmingerd3428942012-10-01 12:32:35 +00001133 ttl = vxlan->ttl;
1134 if (!ttl && IN_MULTICAST(ntohl(dst)))
1135 ttl = 1;
1136
1137 tos = vxlan->tos;
1138 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001139 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001140
stephen hemminger05f47d62012-10-09 20:35:50 +00001141 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001142
stephen hemmingerca78f182012-10-09 20:35:48 +00001143 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001144 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001145 fl4.flowi4_tos = RT_TOS(tos);
1146 fl4.daddr = dst;
1147 fl4.saddr = vxlan->saddr;
1148
1149 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001150 if (IS_ERR(rt)) {
1151 netdev_dbg(dev, "no route to %pI4\n", &dst);
1152 dev->stats.tx_carrier_errors++;
1153 goto tx_error;
1154 }
1155
1156 if (rt->dst.dev == dev) {
1157 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1158 ip_rt_put(rt);
1159 dev->stats.collisions++;
1160 goto tx_error;
1161 }
1162
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001163 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001164 if (rt->rt_flags & RTCF_LOCAL &&
1165 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001166 struct vxlan_dev *dst_vxlan;
1167
1168 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001169 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001170 if (!dst_vxlan)
1171 goto tx_error;
1172 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001173 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001174 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001175 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1176 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001177 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001178
1179 __skb_push(skb, sizeof(*uh));
1180 skb_reset_transport_header(skb);
1181 uh = udp_hdr(skb);
1182
stephen hemminger73cf3312013-04-27 11:31:54 +00001183 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001184 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001185
1186 uh->len = htons(skb->len);
1187 uh->check = 0;
1188
stephen hemminger1cad8712012-10-09 20:35:49 +00001189 vxlan_set_owner(dev, skb);
1190
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001191 if (handle_offloads(skb))
1192 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001193
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001194 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1195 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1196
1197 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1198 IPPROTO_UDP, tos, ttl, df);
1199 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1200
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001201 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001202
1203drop:
1204 dev->stats.tx_dropped++;
1205 goto tx_free;
1206
1207tx_error:
1208 dev->stats.tx_errors++;
1209tx_free:
1210 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001211}
1212
David Stevens66817122013-03-15 04:35:51 +00001213/* Transmit local packets over Vxlan
1214 *
1215 * Outer IP header inherits ECN and DF from inner header.
1216 * Outer UDP destination is the VXLAN assigned port.
1217 * source port is based on hash of flow
1218 */
1219static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1220{
1221 struct vxlan_dev *vxlan = netdev_priv(dev);
1222 struct ethhdr *eth;
1223 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001224 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001225 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001226
1227 skb_reset_mac_header(skb);
1228 eth = eth_hdr(skb);
1229
1230 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1231 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001232
1233 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001234 did_rsc = false;
1235
1236 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1237 ntohs(eth->h_proto) == ETH_P_IP) {
1238 did_rsc = route_shortcircuit(dev, skb);
1239 if (did_rsc)
1240 f = vxlan_find_mac(vxlan, eth->h_dest);
1241 }
1242
David Stevens66817122013-03-15 04:35:51 +00001243 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001244 f = vxlan_find_mac(vxlan, all_zeros_mac);
1245 if (f == NULL) {
1246 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1247 !is_multicast_ether_addr(eth->h_dest))
1248 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001249
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001250 dev->stats.tx_dropped++;
1251 dev_kfree_skb(skb);
1252 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001253 }
David Stevens66817122013-03-15 04:35:51 +00001254 }
1255
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001256 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1257 struct sk_buff *skb1;
1258
1259 skb1 = skb_clone(skb, GFP_ATOMIC);
1260 if (skb1)
1261 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1262 }
1263
1264 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001265 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001266}
1267
stephen hemmingerd3428942012-10-01 12:32:35 +00001268/* Walk the forwarding table and purge stale entries */
1269static void vxlan_cleanup(unsigned long arg)
1270{
1271 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1272 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1273 unsigned int h;
1274
1275 if (!netif_running(vxlan->dev))
1276 return;
1277
1278 spin_lock_bh(&vxlan->hash_lock);
1279 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1280 struct hlist_node *p, *n;
1281 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1282 struct vxlan_fdb *f
1283 = container_of(p, struct vxlan_fdb, hlist);
1284 unsigned long timeout;
1285
stephen hemminger3c172862012-10-26 06:24:34 +00001286 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001287 continue;
1288
1289 timeout = f->used + vxlan->age_interval * HZ;
1290 if (time_before_eq(timeout, jiffies)) {
1291 netdev_dbg(vxlan->dev,
1292 "garbage collect %pM\n",
1293 f->eth_addr);
1294 f->state = NUD_STALE;
1295 vxlan_fdb_destroy(vxlan, f);
1296 } else if (time_before(timeout, next_timer))
1297 next_timer = timeout;
1298 }
1299 }
1300 spin_unlock_bh(&vxlan->hash_lock);
1301
1302 mod_timer(&vxlan->age_timer, next_timer);
1303}
1304
1305/* Setup stats when device is created */
1306static int vxlan_init(struct net_device *dev)
1307{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001308 struct vxlan_dev *vxlan = netdev_priv(dev);
1309 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1310 struct vxlan_sock *vs;
1311 __u32 vni = vxlan->default_dst.remote_vni;
1312
Pravin B Shelare8171042013-03-25 14:49:46 +00001313 dev->tstats = alloc_percpu(struct pcpu_tstats);
1314 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001315 return -ENOMEM;
1316
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001317 spin_lock(&vn->sock_lock);
1318 vs = vxlan_find_port(dev_net(dev), vxlan->dst_port);
1319 if (vs) {
1320 /* If we have a socket with same port already, reuse it */
1321 atomic_inc(&vs->refcnt);
1322 vxlan->vn_sock = vs;
1323 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1324 } else {
1325 /* otherwise make new socket outside of RTNL */
1326 dev_hold(dev);
1327 queue_work(vxlan_wq, &vxlan->sock_work);
1328 }
1329 spin_unlock(&vn->sock_lock);
1330
stephen hemmingerd3428942012-10-01 12:32:35 +00001331 return 0;
1332}
1333
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001334static void vxlan_fdb_delete_defualt(struct vxlan_dev *vxlan)
1335{
1336 struct vxlan_fdb *f;
1337
1338 spin_lock_bh(&vxlan->hash_lock);
1339 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1340 if (f)
1341 vxlan_fdb_destroy(vxlan, f);
1342 spin_unlock_bh(&vxlan->hash_lock);
1343}
1344
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001345static void vxlan_uninit(struct net_device *dev)
1346{
1347 struct vxlan_dev *vxlan = netdev_priv(dev);
1348 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1349 struct vxlan_sock *vs = vxlan->vn_sock;
1350
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001351 vxlan_fdb_delete_defualt(vxlan);
1352
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001353 if (vs)
1354 vxlan_sock_release(vn, vs);
1355 free_percpu(dev->tstats);
1356}
1357
stephen hemmingerd3428942012-10-01 12:32:35 +00001358/* Start ageing timer and join group when device is brought up */
1359static int vxlan_open(struct net_device *dev)
1360{
1361 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001362 struct vxlan_sock *vs = vxlan->vn_sock;
1363
1364 /* socket hasn't been created */
1365 if (!vs)
1366 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001367
Atzm Watanabec7995c42013-04-16 02:50:52 +00001368 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001369 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001370 dev_hold(dev);
1371 queue_work(vxlan_wq, &vxlan->igmp_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001372 }
1373
1374 if (vxlan->age_interval)
1375 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1376
1377 return 0;
1378}
1379
1380/* Purge the forwarding table */
1381static void vxlan_flush(struct vxlan_dev *vxlan)
1382{
Cong Wang31fec5a2013-05-27 22:35:52 +00001383 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001384
1385 spin_lock_bh(&vxlan->hash_lock);
1386 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1387 struct hlist_node *p, *n;
1388 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1389 struct vxlan_fdb *f
1390 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001391 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1392 if (!is_zero_ether_addr(f->eth_addr))
1393 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001394 }
1395 }
1396 spin_unlock_bh(&vxlan->hash_lock);
1397}
1398
1399/* Cleanup timer and forwarding table on shutdown */
1400static int vxlan_stop(struct net_device *dev)
1401{
1402 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001403 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001404
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001405 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1406 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001407 dev_hold(dev);
1408 queue_work(vxlan_wq, &vxlan->igmp_work);
1409 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001410
1411 del_timer_sync(&vxlan->age_timer);
1412
1413 vxlan_flush(vxlan);
1414
1415 return 0;
1416}
1417
stephen hemmingerd3428942012-10-01 12:32:35 +00001418/* Stub, nothing needs to be done. */
1419static void vxlan_set_multicast_list(struct net_device *dev)
1420{
1421}
1422
1423static const struct net_device_ops vxlan_netdev_ops = {
1424 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001425 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001426 .ndo_open = vxlan_open,
1427 .ndo_stop = vxlan_stop,
1428 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001429 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001430 .ndo_set_rx_mode = vxlan_set_multicast_list,
1431 .ndo_change_mtu = eth_change_mtu,
1432 .ndo_validate_addr = eth_validate_addr,
1433 .ndo_set_mac_address = eth_mac_addr,
1434 .ndo_fdb_add = vxlan_fdb_add,
1435 .ndo_fdb_del = vxlan_fdb_delete,
1436 .ndo_fdb_dump = vxlan_fdb_dump,
1437};
1438
1439/* Info for udev, that this is a virtual tunnel endpoint */
1440static struct device_type vxlan_type = {
1441 .name = "vxlan",
1442};
1443
stephen hemmingerd3428942012-10-01 12:32:35 +00001444/* Initialize the device structure. */
1445static void vxlan_setup(struct net_device *dev)
1446{
1447 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001448 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001449 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001450
1451 eth_hw_addr_random(dev);
1452 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001453 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001454
1455 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001456 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001457 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1458
1459 dev->tx_queue_len = 0;
1460 dev->features |= NETIF_F_LLTX;
1461 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001462 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001463 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001464 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001465
1466 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001467 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001468 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001469 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001470
stephen hemminger553675f2013-05-16 11:35:20 +00001471 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001472 spin_lock_init(&vxlan->hash_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001473 INIT_WORK(&vxlan->igmp_work, vxlan_igmp_work);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001474 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001475
1476 init_timer_deferrable(&vxlan->age_timer);
1477 vxlan->age_timer.function = vxlan_cleanup;
1478 vxlan->age_timer.data = (unsigned long) vxlan;
1479
stephen hemminger05f47d62012-10-09 20:35:50 +00001480 inet_get_local_port_range(&low, &high);
1481 vxlan->port_min = low;
1482 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001483 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001484
stephen hemmingerd3428942012-10-01 12:32:35 +00001485 vxlan->dev = dev;
1486
1487 for (h = 0; h < FDB_HASH_SIZE; ++h)
1488 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1489}
1490
1491static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1492 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001493 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001494 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1495 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1496 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1497 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1498 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1499 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1500 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001501 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001502 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1503 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1504 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1505 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001506 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001507};
1508
1509static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1510{
1511 if (tb[IFLA_ADDRESS]) {
1512 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1513 pr_debug("invalid link address (not ethernet)\n");
1514 return -EINVAL;
1515 }
1516
1517 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1518 pr_debug("invalid all zero ethernet address\n");
1519 return -EADDRNOTAVAIL;
1520 }
1521 }
1522
1523 if (!data)
1524 return -EINVAL;
1525
1526 if (data[IFLA_VXLAN_ID]) {
1527 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1528 if (id >= VXLAN_VID_MASK)
1529 return -ERANGE;
1530 }
1531
stephen hemminger05f47d62012-10-09 20:35:50 +00001532 if (data[IFLA_VXLAN_PORT_RANGE]) {
1533 const struct ifla_vxlan_port_range *p
1534 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1535
1536 if (ntohs(p->high) < ntohs(p->low)) {
1537 pr_debug("port range %u .. %u not valid\n",
1538 ntohs(p->low), ntohs(p->high));
1539 return -EINVAL;
1540 }
1541 }
1542
stephen hemmingerd3428942012-10-01 12:32:35 +00001543 return 0;
1544}
1545
Yan Burman1b13c972013-01-29 23:43:07 +00001546static void vxlan_get_drvinfo(struct net_device *netdev,
1547 struct ethtool_drvinfo *drvinfo)
1548{
1549 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1550 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1551}
1552
1553static const struct ethtool_ops vxlan_ethtool_ops = {
1554 .get_drvinfo = vxlan_get_drvinfo,
1555 .get_link = ethtool_op_get_link,
1556};
1557
stephen hemminger553675f2013-05-16 11:35:20 +00001558static void vxlan_del_work(struct work_struct *work)
1559{
1560 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1561
1562 sk_release_kernel(vs->sock->sk);
1563 kfree_rcu(vs, rcu);
1564}
1565
stephen hemminger553675f2013-05-16 11:35:20 +00001566static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1567{
1568 struct vxlan_sock *vs;
1569 struct sock *sk;
1570 struct sockaddr_in vxlan_addr = {
1571 .sin_family = AF_INET,
1572 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07001573 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00001574 };
1575 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001576 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001577
1578 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1579 if (!vs)
1580 return ERR_PTR(-ENOMEM);
1581
1582 for (h = 0; h < VNI_HASH_SIZE; ++h)
1583 INIT_HLIST_HEAD(&vs->vni_list[h]);
1584
1585 INIT_WORK(&vs->del_work, vxlan_del_work);
1586
1587 /* Create UDP socket for encapsulation receive. */
1588 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1589 if (rc < 0) {
1590 pr_debug("UDP socket create failed\n");
1591 kfree(vs);
1592 return ERR_PTR(rc);
1593 }
1594
1595 /* Put in proper namespace */
1596 sk = vs->sock->sk;
1597 sk_change_net(sk, net);
1598
stephen hemminger553675f2013-05-16 11:35:20 +00001599 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1600 sizeof(vxlan_addr));
1601 if (rc < 0) {
1602 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1603 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1604 sk_release_kernel(sk);
1605 kfree(vs);
1606 return ERR_PTR(rc);
1607 }
1608
1609 /* Disable multicast loopback */
1610 inet_sk(sk)->mc_loop = 0;
1611
1612 /* Mark socket as an encapsulation socket. */
1613 udp_sk(sk)->encap_type = 1;
1614 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1615 udp_encap_enable();
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001616 atomic_set(&vs->refcnt, 1);
stephen hemminger553675f2013-05-16 11:35:20 +00001617
stephen hemminger553675f2013-05-16 11:35:20 +00001618 return vs;
1619}
1620
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001621/* Scheduled at device creation to bind to a socket */
1622static void vxlan_sock_work(struct work_struct *work)
1623{
1624 struct vxlan_dev *vxlan
1625 = container_of(work, struct vxlan_dev, sock_work);
1626 struct net_device *dev = vxlan->dev;
1627 struct net *net = dev_net(dev);
1628 __u32 vni = vxlan->default_dst.remote_vni;
1629 __be16 port = vxlan->dst_port;
1630 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1631 struct vxlan_sock *nvs, *ovs;
1632
1633 nvs = vxlan_socket_create(net, port);
1634 if (IS_ERR(nvs)) {
1635 netdev_err(vxlan->dev, "Can not create UDP socket, %ld\n",
1636 PTR_ERR(nvs));
1637 goto out;
1638 }
1639
1640 spin_lock(&vn->sock_lock);
1641 /* Look again to see if can reuse socket */
1642 ovs = vxlan_find_port(net, port);
1643 if (ovs) {
1644 atomic_inc(&ovs->refcnt);
1645 vxlan->vn_sock = ovs;
1646 hlist_add_head_rcu(&vxlan->hlist, vni_head(ovs, vni));
1647 spin_unlock(&vn->sock_lock);
1648
1649 sk_release_kernel(nvs->sock->sk);
1650 kfree(nvs);
1651 } else {
1652 vxlan->vn_sock = nvs;
1653 hlist_add_head_rcu(&nvs->hlist, vs_head(net, port));
1654 hlist_add_head_rcu(&vxlan->hlist, vni_head(nvs, vni));
1655 spin_unlock(&vn->sock_lock);
1656 }
1657out:
1658 dev_put(dev);
1659}
1660
stephen hemmingerd3428942012-10-01 12:32:35 +00001661static int vxlan_newlink(struct net *net, struct net_device *dev,
1662 struct nlattr *tb[], struct nlattr *data[])
1663{
stephen hemminger553675f2013-05-16 11:35:20 +00001664 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001665 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001666 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001667 __u32 vni;
1668 int err;
1669
1670 if (!data[IFLA_VXLAN_ID])
1671 return -EINVAL;
1672
1673 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001674 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001675
stephen hemminger5d174dd2013-04-27 11:31:55 +00001676 if (data[IFLA_VXLAN_GROUP])
1677 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001678
1679 if (data[IFLA_VXLAN_LOCAL])
1680 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1681
stephen hemminger34e02aa2012-10-09 20:35:53 +00001682 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001683 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001684 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001685 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001686
stephen hemminger34e02aa2012-10-09 20:35:53 +00001687 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001688 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001689 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001690 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001691
1692 if (!tb[IFLA_MTU])
1693 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001694
1695 /* update header length based on lower device */
1696 dev->hard_header_len = lowerdev->hard_header_len +
1697 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001698 }
1699
1700 if (data[IFLA_VXLAN_TOS])
1701 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1702
Vincent Bernatafb97182012-10-30 10:27:16 +00001703 if (data[IFLA_VXLAN_TTL])
1704 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1705
stephen hemmingerd3428942012-10-01 12:32:35 +00001706 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001707 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001708
1709 if (data[IFLA_VXLAN_AGEING])
1710 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1711 else
1712 vxlan->age_interval = FDB_AGE_DEFAULT;
1713
David Stevense4f67ad2012-11-20 02:50:14 +00001714 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1715 vxlan->flags |= VXLAN_F_PROXY;
1716
1717 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1718 vxlan->flags |= VXLAN_F_RSC;
1719
1720 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1721 vxlan->flags |= VXLAN_F_L2MISS;
1722
1723 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1724 vxlan->flags |= VXLAN_F_L3MISS;
1725
stephen hemmingerd3428942012-10-01 12:32:35 +00001726 if (data[IFLA_VXLAN_LIMIT])
1727 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1728
stephen hemminger05f47d62012-10-09 20:35:50 +00001729 if (data[IFLA_VXLAN_PORT_RANGE]) {
1730 const struct ifla_vxlan_port_range *p
1731 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1732 vxlan->port_min = ntohs(p->low);
1733 vxlan->port_max = ntohs(p->high);
1734 }
1735
stephen hemminger823aa872013-04-27 11:31:57 +00001736 if (data[IFLA_VXLAN_PORT])
1737 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1738
stephen hemminger553675f2013-05-16 11:35:20 +00001739 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1740 pr_info("duplicate VNI %u\n", vni);
1741 return -EEXIST;
1742 }
1743
Yan Burman1b13c972013-01-29 23:43:07 +00001744 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1745
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001746 /* create an fdb entry for default destination */
1747 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1748 vxlan->default_dst.remote_ip,
1749 NUD_REACHABLE|NUD_PERMANENT,
1750 NLM_F_EXCL|NLM_F_CREATE,
1751 vxlan->dst_port, vxlan->default_dst.remote_vni,
1752 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001753 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001754 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001755
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001756 err = register_netdevice(dev);
1757 if (err) {
1758 vxlan_fdb_delete_defualt(vxlan);
1759 return err;
1760 }
1761
stephen hemminger553675f2013-05-16 11:35:20 +00001762 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001763
1764 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001765}
1766
1767static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1768{
1769 struct vxlan_dev *vxlan = netdev_priv(dev);
1770
1771 hlist_del_rcu(&vxlan->hlist);
stephen hemminger553675f2013-05-16 11:35:20 +00001772 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001773 unregister_netdevice_queue(dev, head);
1774}
1775
1776static size_t vxlan_get_size(const struct net_device *dev)
1777{
1778
1779 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001780 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001781 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1782 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1783 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1784 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1785 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001786 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1787 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1788 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1789 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001790 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1791 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001792 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001793 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001794 0;
1795}
1796
1797static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1798{
1799 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001800 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001801 struct ifla_vxlan_port_range ports = {
1802 .low = htons(vxlan->port_min),
1803 .high = htons(vxlan->port_max),
1804 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001805
Atzm Watanabec7995c42013-04-16 02:50:52 +00001806 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001807 goto nla_put_failure;
1808
stephen hemminger5d174dd2013-04-27 11:31:55 +00001809 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001810 goto nla_put_failure;
1811
Atzm Watanabec7995c42013-04-16 02:50:52 +00001812 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001813 goto nla_put_failure;
1814
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001815 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001816 goto nla_put_failure;
1817
1818 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1819 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001820 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1821 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1822 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1823 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1824 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1825 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1826 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1827 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1828 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001829 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001830 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1831 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001832 goto nla_put_failure;
1833
stephen hemminger05f47d62012-10-09 20:35:50 +00001834 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1835 goto nla_put_failure;
1836
stephen hemmingerd3428942012-10-01 12:32:35 +00001837 return 0;
1838
1839nla_put_failure:
1840 return -EMSGSIZE;
1841}
1842
1843static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1844 .kind = "vxlan",
1845 .maxtype = IFLA_VXLAN_MAX,
1846 .policy = vxlan_policy,
1847 .priv_size = sizeof(struct vxlan_dev),
1848 .setup = vxlan_setup,
1849 .validate = vxlan_validate,
1850 .newlink = vxlan_newlink,
1851 .dellink = vxlan_dellink,
1852 .get_size = vxlan_get_size,
1853 .fill_info = vxlan_fill_info,
1854};
1855
1856static __net_init int vxlan_init_net(struct net *net)
1857{
1858 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001859 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001860
stephen hemminger553675f2013-05-16 11:35:20 +00001861 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001862 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001863
stephen hemminger553675f2013-05-16 11:35:20 +00001864 for (h = 0; h < PORT_HASH_SIZE; ++h)
1865 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001866
1867 return 0;
1868}
1869
1870static __net_exit void vxlan_exit_net(struct net *net)
1871{
1872 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001873 struct vxlan_dev *vxlan;
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001874
1875 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001876 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1877 dev_close(vxlan->dev);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001878 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001879}
1880
1881static struct pernet_operations vxlan_net_ops = {
1882 .init = vxlan_init_net,
1883 .exit = vxlan_exit_net,
1884 .id = &vxlan_net_id,
1885 .size = sizeof(struct vxlan_net),
1886};
1887
1888static int __init vxlan_init_module(void)
1889{
1890 int rc;
1891
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001892 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1893 if (!vxlan_wq)
1894 return -ENOMEM;
1895
stephen hemmingerd3428942012-10-01 12:32:35 +00001896 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1897
1898 rc = register_pernet_device(&vxlan_net_ops);
1899 if (rc)
1900 goto out1;
1901
1902 rc = rtnl_link_register(&vxlan_link_ops);
1903 if (rc)
1904 goto out2;
1905
1906 return 0;
1907
1908out2:
1909 unregister_pernet_device(&vxlan_net_ops);
1910out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001911 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00001912 return rc;
1913}
Cong Wang7332a132013-05-27 22:35:53 +00001914late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00001915
1916static void __exit vxlan_cleanup_module(void)
1917{
stephen hemmingerd3428942012-10-01 12:32:35 +00001918 unregister_pernet_device(&vxlan_net_ops);
Stephen Hemmingerb7153982013-06-17 14:16:09 -07001919 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001920 destroy_workqueue(vxlan_wq);
David Stevens66817122013-03-15 04:35:51 +00001921 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001922}
1923module_exit(vxlan_cleanup_module);
1924
1925MODULE_LICENSE("GPL");
1926MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00001927MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00001928MODULE_ALIAS_RTNL_LINK("vxlan");