blob: 236c445b1930b7e86e760edc205fa2c539d47a33 [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)
Pravin B Shelar7ce04752013-08-19 11:22:54 -070060#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
stephen hemmingerd3428942012-10-01 12:32:35 +000061
62#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
63
64/* VXLAN protocol header */
65struct vxlanhdr {
66 __be32 vx_flags;
67 __be32 vx_vni;
68};
69
stephen hemminger23c578b2013-04-27 11:31:53 +000070/* UDP port for VXLAN traffic.
71 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070072 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000073 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070074static unsigned short vxlan_port __read_mostly = 8472;
75module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000076MODULE_PARM_DESC(udp_port, "Destination UDP port");
77
78static bool log_ecn_error = true;
79module_param(log_ecn_error, bool, 0644);
80MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
81
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070082static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000083
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030084static const u8 all_zeros_mac[ETH_ALEN];
85
Pravin B Shelar5cfccc52013-08-19 11:23:02 -070086struct vxlan_sock;
87typedef void (vxlan_rcv_t)(struct vxlan_sock *vh, struct sk_buff *skb, __be32 key);
88
stephen hemminger553675f2013-05-16 11:35:20 +000089/* per UDP socket information */
90struct vxlan_sock {
Pravin B Shelar5cfccc52013-08-19 11:23:02 -070091 vxlan_rcv_t *rcv;
stephen hemminger553675f2013-05-16 11:35:20 +000092 struct hlist_node hlist;
93 struct rcu_head rcu;
94 struct work_struct del_work;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -070095 atomic_t refcnt;
stephen hemminger553675f2013-05-16 11:35:20 +000096 struct socket *sock;
stephen hemmingerd3428942012-10-01 12:32:35 +000097 struct hlist_head vni_list[VNI_HASH_SIZE];
98};
99
stephen hemminger553675f2013-05-16 11:35:20 +0000100/* per-network namespace private data for this module */
101struct vxlan_net {
102 struct list_head vxlan_list;
103 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700104 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +0000105};
106
David Stevens66817122013-03-15 04:35:51 +0000107struct vxlan_rdst {
David Stevens66817122013-03-15 04:35:51 +0000108 __be32 remote_ip;
109 __be16 remote_port;
110 u32 remote_vni;
111 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700112 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300113 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000114};
115
stephen hemmingerd3428942012-10-01 12:32:35 +0000116/* Forwarding table entry */
117struct vxlan_fdb {
118 struct hlist_node hlist; /* linked list of entries */
119 struct rcu_head rcu;
120 unsigned long updated; /* jiffies */
121 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700122 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000123 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000124 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000125 u8 eth_addr[ETH_ALEN];
126};
127
stephen hemmingerd3428942012-10-01 12:32:35 +0000128/* Pseudo network device */
129struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000130 struct hlist_node hlist; /* vni hash table */
131 struct list_head next; /* vxlan's per namespace list */
132 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000133 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000134 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000135 __be32 saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000136 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000137 __u16 port_min; /* source port range */
138 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000139 __u8 tos; /* TOS override */
140 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000141 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000142
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700143 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700144 struct work_struct igmp_join;
145 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700146
stephen hemmingerd3428942012-10-01 12:32:35 +0000147 unsigned long age_interval;
148 struct timer_list age_timer;
149 spinlock_t hash_lock;
150 unsigned int addrcnt;
151 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000152
153 struct hlist_head fdb_head[FDB_HASH_SIZE];
154};
155
David Stevense4f67ad2012-11-20 02:50:14 +0000156#define VXLAN_F_LEARN 0x01
157#define VXLAN_F_PROXY 0x02
158#define VXLAN_F_RSC 0x04
159#define VXLAN_F_L2MISS 0x08
160#define VXLAN_F_L3MISS 0x10
161
stephen hemmingerd3428942012-10-01 12:32:35 +0000162/* salt for hash table */
163static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700164static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000165
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700166static void vxlan_sock_work(struct work_struct *work);
167
stephen hemminger553675f2013-05-16 11:35:20 +0000168/* Virtual Network hash table head */
169static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
170{
171 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
172}
173
174/* Socket hash table head */
175static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000176{
177 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
178
stephen hemminger553675f2013-05-16 11:35:20 +0000179 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
180}
181
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700182/* First remote destination for a forwarding entry.
183 * Guaranteed to be non-NULL because remotes are never deleted.
184 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700185static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700186{
stephen hemminger5ca54612013-08-04 17:17:39 -0700187 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
188}
189
190static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
191{
192 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700193}
194
stephen hemminger553675f2013-05-16 11:35:20 +0000195/* Find VXLAN socket based on network namespace and UDP port */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -0700196static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000197{
198 struct vxlan_sock *vs;
199
200 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
201 if (inet_sk(vs->sock->sk)->inet_sport == port)
202 return vs;
203 }
204 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000205}
206
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700207static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000208{
209 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000210
stephen hemminger553675f2013-05-16 11:35:20 +0000211 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000212 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000213 return vxlan;
214 }
215
216 return NULL;
217}
218
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700219/* Look up VNI in a per net namespace table */
220static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
221{
222 struct vxlan_sock *vs;
223
224 vs = vxlan_find_sock(net, port);
225 if (!vs)
226 return NULL;
227
228 return vxlan_vs_find_vni(vs, id);
229}
230
stephen hemmingerd3428942012-10-01 12:32:35 +0000231/* Fill in neighbour message in skbuff. */
232static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700233 const struct vxlan_fdb *fdb,
234 u32 portid, u32 seq, int type, unsigned int flags,
235 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000236{
237 unsigned long now = jiffies;
238 struct nda_cacheinfo ci;
239 struct nlmsghdr *nlh;
240 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000241 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000242
243 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
244 if (nlh == NULL)
245 return -EMSGSIZE;
246
247 ndm = nlmsg_data(nlh);
248 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000249
250 send_eth = send_ip = true;
251
252 if (type == RTM_GETNEIGH) {
253 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000254 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000255 send_eth = !is_zero_ether_addr(fdb->eth_addr);
256 } else
257 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000258 ndm->ndm_state = fdb->state;
259 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000260 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000261 ndm->ndm_type = NDA_DST;
262
David Stevense4f67ad2012-11-20 02:50:14 +0000263 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000264 goto nla_put_failure;
265
David Stevens66817122013-03-15 04:35:51 +0000266 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
267 goto nla_put_failure;
268
stephen hemminger823aa872013-04-27 11:31:57 +0000269 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000270 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
271 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000272 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700273 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000274 goto nla_put_failure;
275 if (rdst->remote_ifindex &&
276 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000277 goto nla_put_failure;
278
279 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
280 ci.ndm_confirmed = 0;
281 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
282 ci.ndm_refcnt = 0;
283
284 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
285 goto nla_put_failure;
286
287 return nlmsg_end(skb, nlh);
288
289nla_put_failure:
290 nlmsg_cancel(skb, nlh);
291 return -EMSGSIZE;
292}
293
294static inline size_t vxlan_nlmsg_size(void)
295{
296 return NLMSG_ALIGN(sizeof(struct ndmsg))
297 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
298 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000299 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000300 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
301 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000302 + nla_total_size(sizeof(struct nda_cacheinfo));
303}
304
305static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700306 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000307{
308 struct net *net = dev_net(vxlan->dev);
309 struct sk_buff *skb;
310 int err = -ENOBUFS;
311
312 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
313 if (skb == NULL)
314 goto errout;
315
stephen hemminger5ca54612013-08-04 17:17:39 -0700316 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
317 first_remote_rtnl(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000318 if (err < 0) {
319 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
320 WARN_ON(err == -EMSGSIZE);
321 kfree_skb(skb);
322 goto errout;
323 }
324
325 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
326 return;
327errout:
328 if (err < 0)
329 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
330}
331
David Stevense4f67ad2012-11-20 02:50:14 +0000332static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
333{
334 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700335 struct vxlan_fdb f = {
336 .state = NUD_STALE,
337 };
338 struct vxlan_rdst remote = {
339 .remote_ip = ipa, /* goes to NDA_DST */
340 .remote_vni = VXLAN_N_VID,
341 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700342
343 INIT_LIST_HEAD(&f.remotes);
344 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000345
346 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
347}
348
349static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
350{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700351 struct vxlan_fdb f = {
352 .state = NUD_STALE,
353 };
David Stevense4f67ad2012-11-20 02:50:14 +0000354
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700355 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000356 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
357
358 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
359}
360
stephen hemmingerd3428942012-10-01 12:32:35 +0000361/* Hash Ethernet address */
362static u32 eth_hash(const unsigned char *addr)
363{
364 u64 value = get_unaligned((u64 *)addr);
365
366 /* only want 6 bytes */
367#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000368 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000369#else
370 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000371#endif
372 return hash_64(value, FDB_HASH_BITS);
373}
374
375/* Hash chain to use given mac address */
376static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
377 const u8 *mac)
378{
379 return &vxlan->fdb_head[eth_hash(mac)];
380}
381
382/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000383static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000384 const u8 *mac)
385
386{
387 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
388 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000389
Sasha Levinb67bfe02013-02-27 17:06:00 -0800390 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000391 if (compare_ether_addr(mac, f->eth_addr) == 0)
392 return f;
393 }
394
395 return NULL;
396}
397
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000398static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
399 const u8 *mac)
400{
401 struct vxlan_fdb *f;
402
403 f = __vxlan_find_mac(vxlan, mac);
404 if (f)
405 f->used = jiffies;
406
407 return f;
408}
409
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300410/* caller should hold vxlan->hash_lock */
411static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
412 __be32 ip, __be16 port,
413 __u32 vni, __u32 ifindex)
414{
415 struct vxlan_rdst *rd;
416
417 list_for_each_entry(rd, &f->remotes, list) {
418 if (rd->remote_ip == ip &&
419 rd->remote_port == port &&
420 rd->remote_vni == vni &&
421 rd->remote_ifindex == ifindex)
422 return rd;
423 }
424
425 return NULL;
426}
427
Thomas Richter906dc182013-07-19 17:20:07 +0200428/* Replace destination of unicast mac */
429static int vxlan_fdb_replace(struct vxlan_fdb *f,
430 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
431{
432 struct vxlan_rdst *rd;
433
434 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
435 if (rd)
436 return 0;
437
438 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
439 if (!rd)
440 return 0;
441 rd->remote_ip = ip;
442 rd->remote_port = port;
443 rd->remote_vni = vni;
444 rd->remote_ifindex = ifindex;
445 return 1;
446}
447
David Stevens66817122013-03-15 04:35:51 +0000448/* Add/update destinations for multicast */
449static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000450 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000451{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700452 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000453
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300454 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
455 if (rd)
456 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700457
David Stevens66817122013-03-15 04:35:51 +0000458 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
459 if (rd == NULL)
460 return -ENOBUFS;
461 rd->remote_ip = ip;
462 rd->remote_port = port;
463 rd->remote_vni = vni;
464 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700465
466 list_add_tail_rcu(&rd->list, &f->remotes);
467
David Stevens66817122013-03-15 04:35:51 +0000468 return 1;
469}
470
stephen hemmingerd3428942012-10-01 12:32:35 +0000471/* Add new entry to forwarding table -- assumes lock held */
472static int vxlan_fdb_create(struct vxlan_dev *vxlan,
473 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000474 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000475 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000476 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000477{
478 struct vxlan_fdb *f;
479 int notify = 0;
480
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000481 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000482 if (f) {
483 if (flags & NLM_F_EXCL) {
484 netdev_dbg(vxlan->dev,
485 "lost race to create %pM\n", mac);
486 return -EEXIST;
487 }
488 if (f->state != state) {
489 f->state = state;
490 f->updated = jiffies;
491 notify = 1;
492 }
David Stevensae884082013-04-19 00:36:26 +0000493 if (f->flags != ndm_flags) {
494 f->flags = ndm_flags;
495 f->updated = jiffies;
496 notify = 1;
497 }
Thomas Richter906dc182013-07-19 17:20:07 +0200498 if ((flags & NLM_F_REPLACE)) {
499 /* Only change unicasts */
500 if (!(is_multicast_ether_addr(f->eth_addr) ||
501 is_zero_ether_addr(f->eth_addr))) {
502 int rc = vxlan_fdb_replace(f, ip, port, vni,
503 ifindex);
504
505 if (rc < 0)
506 return rc;
507 notify |= rc;
508 } else
509 return -EOPNOTSUPP;
510 }
David Stevens66817122013-03-15 04:35:51 +0000511 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300512 (is_multicast_ether_addr(f->eth_addr) ||
513 is_zero_ether_addr(f->eth_addr))) {
David Stevens66817122013-03-15 04:35:51 +0000514 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
515
516 if (rc < 0)
517 return rc;
518 notify |= rc;
519 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000520 } else {
521 if (!(flags & NLM_F_CREATE))
522 return -ENOENT;
523
524 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
525 return -ENOSPC;
526
Thomas Richter906dc182013-07-19 17:20:07 +0200527 /* Disallow replace to add a multicast entry */
528 if ((flags & NLM_F_REPLACE) &&
529 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
530 return -EOPNOTSUPP;
531
stephen hemmingerd3428942012-10-01 12:32:35 +0000532 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
533 f = kmalloc(sizeof(*f), GFP_ATOMIC);
534 if (!f)
535 return -ENOMEM;
536
537 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000538 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000539 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000540 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700541 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000542 memcpy(f->eth_addr, mac, ETH_ALEN);
543
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700544 vxlan_fdb_append(f, ip, port, vni, ifindex);
545
stephen hemmingerd3428942012-10-01 12:32:35 +0000546 ++vxlan->addrcnt;
547 hlist_add_head_rcu(&f->hlist,
548 vxlan_fdb_head(vxlan, mac));
549 }
550
551 if (notify)
552 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
553
554 return 0;
555}
556
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300557static void vxlan_fdb_free_rdst(struct rcu_head *head)
558{
559 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
560 kfree(rd);
561}
562
Wei Yongjun6706c822013-04-11 19:00:35 +0000563static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000564{
565 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700566 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000567
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700568 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000569 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000570 kfree(f);
571}
572
stephen hemmingerd3428942012-10-01 12:32:35 +0000573static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
574{
575 netdev_dbg(vxlan->dev,
576 "delete %pM\n", f->eth_addr);
577
578 --vxlan->addrcnt;
579 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
580
581 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000582 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000583}
584
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300585static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
586 __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
587{
588 struct net *net = dev_net(vxlan->dev);
589
590 if (tb[NDA_DST]) {
591 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
592 return -EAFNOSUPPORT;
593
594 *ip = nla_get_be32(tb[NDA_DST]);
595 } else {
596 *ip = htonl(INADDR_ANY);
597 }
598
599 if (tb[NDA_PORT]) {
600 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
601 return -EINVAL;
602 *port = nla_get_be16(tb[NDA_PORT]);
603 } else {
604 *port = vxlan->dst_port;
605 }
606
607 if (tb[NDA_VNI]) {
608 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
609 return -EINVAL;
610 *vni = nla_get_u32(tb[NDA_VNI]);
611 } else {
612 *vni = vxlan->default_dst.remote_vni;
613 }
614
615 if (tb[NDA_IFINDEX]) {
616 struct net_device *tdev;
617
618 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
619 return -EINVAL;
620 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
621 tdev = dev_get_by_index(net, *ifindex);
622 if (!tdev)
623 return -EADDRNOTAVAIL;
624 dev_put(tdev);
625 } else {
626 *ifindex = 0;
627 }
628
629 return 0;
630}
631
stephen hemmingerd3428942012-10-01 12:32:35 +0000632/* Add static entry (via netlink) */
633static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
634 struct net_device *dev,
635 const unsigned char *addr, u16 flags)
636{
637 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300638 /* struct net *net = dev_net(vxlan->dev); */
stephen hemmingerd3428942012-10-01 12:32:35 +0000639 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000640 __be16 port;
641 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000642 int err;
643
644 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
645 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
646 ndm->ndm_state);
647 return -EINVAL;
648 }
649
650 if (tb[NDA_DST] == NULL)
651 return -EINVAL;
652
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300653 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
654 if (err)
655 return err;
David Stevens66817122013-03-15 04:35:51 +0000656
stephen hemmingerd3428942012-10-01 12:32:35 +0000657 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000658 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
659 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000660 spin_unlock_bh(&vxlan->hash_lock);
661
662 return err;
663}
664
665/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000666static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
667 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000668 const unsigned char *addr)
669{
670 struct vxlan_dev *vxlan = netdev_priv(dev);
671 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300672 struct vxlan_rdst *rd = NULL;
673 __be32 ip;
674 __be16 port;
675 u32 vni, ifindex;
676 int err;
677
678 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
679 if (err)
680 return err;
681
682 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000683
684 spin_lock_bh(&vxlan->hash_lock);
685 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300686 if (!f)
687 goto out;
688
689 if (ip != htonl(INADDR_ANY)) {
690 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
691 if (!rd)
692 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000693 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300694
695 err = 0;
696
697 /* remove a destination if it's not the only one on the list,
698 * otherwise destroy the fdb entry
699 */
700 if (rd && !list_is_singular(&f->remotes)) {
701 list_del_rcu(&rd->list);
702 call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
703 goto out;
704 }
705
706 vxlan_fdb_destroy(vxlan, f);
707
708out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000709 spin_unlock_bh(&vxlan->hash_lock);
710
711 return err;
712}
713
714/* Dump forwarding table */
715static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
716 struct net_device *dev, int idx)
717{
718 struct vxlan_dev *vxlan = netdev_priv(dev);
719 unsigned int h;
720
721 for (h = 0; h < FDB_HASH_SIZE; ++h) {
722 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000723 int err;
724
Sasha Levinb67bfe02013-02-27 17:06:00 -0800725 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000726 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000727
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700728 if (idx < cb->args[0])
729 goto skip;
730
731 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000732 err = vxlan_fdb_info(skb, vxlan, f,
733 NETLINK_CB(cb->skb).portid,
734 cb->nlh->nlmsg_seq,
735 RTM_NEWNEIGH,
736 NLM_F_MULTI, rd);
737 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700738 goto out;
David Stevens66817122013-03-15 04:35:51 +0000739 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700740skip:
741 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000742 }
743 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700744out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000745 return idx;
746}
747
748/* Watch incoming packets to learn mapping between Ethernet address
749 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700750 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000751 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700752static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000753 __be32 src_ip, const u8 *src_mac)
754{
755 struct vxlan_dev *vxlan = netdev_priv(dev);
756 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000757
758 f = vxlan_find_mac(vxlan, src_mac);
759 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700760 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700761
762 if (likely(rdst->remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700763 return false;
764
765 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700766 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700767 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000768
769 if (net_ratelimit())
770 netdev_info(dev,
771 "%pM migrated from %pI4 to %pI4\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700772 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000773
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700774 rdst->remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000775 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700776 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000777 } else {
778 /* learned new entry */
779 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700780
781 /* close off race between vxlan_flush and incoming packets */
782 if (netif_running(dev))
783 vxlan_fdb_create(vxlan, src_mac, src_ip,
784 NUD_REACHABLE,
785 NLM_F_EXCL|NLM_F_CREATE,
786 vxlan->dst_port,
787 vxlan->default_dst.remote_vni,
788 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000789 spin_unlock(&vxlan->hash_lock);
790 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700791
792 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000793}
794
stephen hemmingerd3428942012-10-01 12:32:35 +0000795/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700796static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000797{
stephen hemminger553675f2013-05-16 11:35:20 +0000798 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000799
stephen hemminger553675f2013-05-16 11:35:20 +0000800 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000801 if (!netif_running(vxlan->dev))
802 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000803
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700804 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000805 return true;
806 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000807
808 return false;
809}
810
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700811static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000812{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700813 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000814}
815
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700816static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000817{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700818 if (!atomic_dec_and_test(&vs->refcnt))
819 return;
820
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700821 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700822 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700823 spin_unlock(&vn->sock_lock);
824
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700825 queue_work(vxlan_wq, &vs->del_work);
826}
827
stephen hemminger3fc2de22013-07-18 08:40:15 -0700828/* Callback to update multicast group membership when first VNI on
829 * multicast asddress is brought up
830 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700831 */
stephen hemminger3fc2de22013-07-18 08:40:15 -0700832static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700833{
stephen hemminger3fc2de22013-07-18 08:40:15 -0700834 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700835 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
836 struct vxlan_sock *vs = vxlan->vn_sock;
837 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000838 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000839 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
840 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000841 };
842
stephen hemmingerd3428942012-10-01 12:32:35 +0000843 lock_sock(sk);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700844 ip_mc_join_group(sk, &mreq);
845 release_sock(sk);
846
847 vxlan_sock_release(vn, vs);
848 dev_put(vxlan->dev);
849}
850
851/* Inverse of vxlan_igmp_join when last VNI is brought down */
852static void vxlan_igmp_leave(struct work_struct *work)
853{
854 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
855 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
856 struct vxlan_sock *vs = vxlan->vn_sock;
857 struct sock *sk = vs->sock->sk;
858 struct ip_mreqn mreq = {
859 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
860 .imr_ifindex = vxlan->default_dst.remote_ifindex,
861 };
862
863 lock_sock(sk);
864 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000865 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000866
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700867 vxlan_sock_release(vn, vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700868 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000869}
870
871/* Callback from net/ipv4/udp.c to receive packets */
872static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
873{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700874 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000875 struct vxlanhdr *vxh;
stephen hemminger553675f2013-05-16 11:35:20 +0000876 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000877
stephen hemmingerd3428942012-10-01 12:32:35 +0000878 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700879 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +0000880 goto error;
881
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700882 /* Return packets with reserved bits set */
883 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +0000884 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
885 (vxh->vx_vni & htonl(0xff))) {
886 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
887 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
888 goto error;
889 }
890
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700891 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +0000892 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000893
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700894 port = inet_sk(sk)->inet_sport;
895
896 vs = vxlan_find_sock(sock_net(sk), port);
897 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000898 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700899
900 vs->rcv(vs, skb, vxh->vx_vni);
901 return 0;
902
903drop:
904 /* Consume bad packet */
905 kfree_skb(skb);
906 return 0;
907
908error:
909 /* Return non vxlan pkt */
910 return 1;
911}
912
913static void vxlan_rcv(struct vxlan_sock *vs,
914 struct sk_buff *skb, __be32 vx_vni)
915{
916 struct iphdr *oip;
917 struct vxlan_dev *vxlan;
918 struct pcpu_tstats *stats;
919 __u32 vni;
920 int err;
921
922 vni = ntohl(vx_vni) >> 8;
923 /* Is this VNI defined? */
924 vxlan = vxlan_vs_find_vni(vs, vni);
925 if (!vxlan)
926 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000927
David Stevense4f67ad2012-11-20 02:50:14 +0000928 skb_reset_mac_header(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +0000929 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000930
931 /* Ignore packet loops (and multicast echo) */
932 if (compare_ether_addr(eth_hdr(skb)->h_source,
933 vxlan->dev->dev_addr) == 0)
934 goto drop;
935
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700936 /* Re-examine inner Ethernet packet */
937 oip = ip_hdr(skb);
stephen hemminger26a41ae62013-06-17 12:09:58 -0700938 if ((vxlan->flags & VXLAN_F_LEARN) &&
939 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
940 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000941
stephen hemmingerd3428942012-10-01 12:32:35 +0000942 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000943
944 /* If the NIC driver gave us an encapsulated packet with
945 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
946 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
947 * for us. Otherwise force the upper layers to verify it.
948 */
949 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
950 !(vxlan->dev->features & NETIF_F_RXCSUM))
951 skb->ip_summed = CHECKSUM_NONE;
952
953 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000954
955 err = IP_ECN_decapsulate(oip, skb);
956 if (unlikely(err)) {
957 if (log_ecn_error)
958 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
959 &oip->saddr, oip->tos);
960 if (err > 1) {
961 ++vxlan->dev->stats.rx_frame_errors;
962 ++vxlan->dev->stats.rx_errors;
963 goto drop;
964 }
965 }
966
Pravin B Shelare8171042013-03-25 14:49:46 +0000967 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000968 u64_stats_update_begin(&stats->syncp);
969 stats->rx_packets++;
970 stats->rx_bytes += skb->len;
971 u64_stats_update_end(&stats->syncp);
972
973 netif_rx(skb);
974
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700975 return;
stephen hemmingerd3428942012-10-01 12:32:35 +0000976drop:
977 /* Consume bad packet */
978 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +0000979}
980
David Stevense4f67ad2012-11-20 02:50:14 +0000981static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
982{
983 struct vxlan_dev *vxlan = netdev_priv(dev);
984 struct arphdr *parp;
985 u8 *arpptr, *sha;
986 __be32 sip, tip;
987 struct neighbour *n;
988
989 if (dev->flags & IFF_NOARP)
990 goto out;
991
992 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
993 dev->stats.tx_dropped++;
994 goto out;
995 }
996 parp = arp_hdr(skb);
997
998 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
999 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1000 parp->ar_pro != htons(ETH_P_IP) ||
1001 parp->ar_op != htons(ARPOP_REQUEST) ||
1002 parp->ar_hln != dev->addr_len ||
1003 parp->ar_pln != 4)
1004 goto out;
1005 arpptr = (u8 *)parp + sizeof(struct arphdr);
1006 sha = arpptr;
1007 arpptr += dev->addr_len; /* sha */
1008 memcpy(&sip, arpptr, sizeof(sip));
1009 arpptr += sizeof(sip);
1010 arpptr += dev->addr_len; /* tha */
1011 memcpy(&tip, arpptr, sizeof(tip));
1012
1013 if (ipv4_is_loopback(tip) ||
1014 ipv4_is_multicast(tip))
1015 goto out;
1016
1017 n = neigh_lookup(&arp_tbl, &tip, dev);
1018
1019 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001020 struct vxlan_fdb *f;
1021 struct sk_buff *reply;
1022
1023 if (!(n->nud_state & NUD_CONNECTED)) {
1024 neigh_release(n);
1025 goto out;
1026 }
1027
1028 f = vxlan_find_mac(vxlan, n->ha);
stephen hemminger5ca54612013-08-04 17:17:39 -07001029 if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001030 /* bridge-local neighbor */
1031 neigh_release(n);
1032 goto out;
1033 }
1034
1035 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1036 n->ha, sha);
1037
1038 neigh_release(n);
1039
1040 skb_reset_mac_header(reply);
1041 __skb_pull(reply, skb_network_offset(reply));
1042 reply->ip_summed = CHECKSUM_UNNECESSARY;
1043 reply->pkt_type = PACKET_HOST;
1044
1045 if (netif_rx_ni(reply) == NET_RX_DROP)
1046 dev->stats.rx_dropped++;
1047 } else if (vxlan->flags & VXLAN_F_L3MISS)
1048 vxlan_ip_miss(dev, tip);
1049out:
1050 consume_skb(skb);
1051 return NETDEV_TX_OK;
1052}
1053
1054static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1055{
1056 struct vxlan_dev *vxlan = netdev_priv(dev);
1057 struct neighbour *n;
1058 struct iphdr *pip;
1059
1060 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1061 return false;
1062
1063 n = NULL;
1064 switch (ntohs(eth_hdr(skb)->h_proto)) {
1065 case ETH_P_IP:
1066 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1067 return false;
1068 pip = ip_hdr(skb);
1069 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1070 break;
1071 default:
1072 return false;
1073 }
1074
1075 if (n) {
1076 bool diff;
1077
1078 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1079 if (diff) {
1080 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1081 dev->addr_len);
1082 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1083 }
1084 neigh_release(n);
1085 return diff;
1086 } else if (vxlan->flags & VXLAN_F_L3MISS)
1087 vxlan_ip_miss(dev, pip->daddr);
1088 return false;
1089}
1090
stephen hemminger553675f2013-05-16 11:35:20 +00001091static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001092{
1093 sock_put(skb->sk);
1094}
1095
1096/* On transmit, associate with the tunnel socket */
1097static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1098{
stephen hemminger553675f2013-05-16 11:35:20 +00001099 struct vxlan_dev *vxlan = netdev_priv(dev);
1100 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +00001101
1102 skb_orphan(skb);
1103 sock_hold(sk);
1104 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001105 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001106}
1107
stephen hemminger05f47d62012-10-09 20:35:50 +00001108/* Compute source port for outgoing packet
1109 * first choice to use L4 flow hash since it will spread
1110 * better and maybe available from hardware
1111 * secondary choice is to use jhash on the Ethernet header
1112 */
stephen hemminger7d836a72013-04-27 11:31:56 +00001113static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001114{
1115 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1116 u32 hash;
1117
1118 hash = skb_get_rxhash(skb);
1119 if (!hash)
1120 hash = jhash(skb->data, 2 * ETH_ALEN,
1121 (__force u32) skb->protocol);
1122
stephen hemminger7d836a72013-04-27 11:31:56 +00001123 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001124}
1125
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001126static int handle_offloads(struct sk_buff *skb)
1127{
1128 if (skb_is_gso(skb)) {
1129 int err = skb_unclone(skb, GFP_ATOMIC);
1130 if (unlikely(err))
1131 return err;
1132
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001133 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001134 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1135 skb->ip_summed = CHECKSUM_NONE;
1136
1137 return 0;
1138}
1139
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001140/* Bypass encapsulation if the destination is local */
1141static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1142 struct vxlan_dev *dst_vxlan)
1143{
1144 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1145 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1146
1147 skb->pkt_type = PACKET_HOST;
1148 skb->encapsulation = 0;
1149 skb->dev = dst_vxlan->dev;
1150 __skb_pull(skb, skb_network_offset(skb));
1151
1152 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001153 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1154 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001155
1156 u64_stats_update_begin(&tx_stats->syncp);
1157 tx_stats->tx_packets++;
1158 tx_stats->tx_bytes += skb->len;
1159 u64_stats_update_end(&tx_stats->syncp);
1160
1161 if (netif_rx(skb) == NET_RX_SUCCESS) {
1162 u64_stats_update_begin(&rx_stats->syncp);
1163 rx_stats->rx_packets++;
1164 rx_stats->rx_bytes += skb->len;
1165 u64_stats_update_end(&rx_stats->syncp);
1166 } else {
1167 skb->dev->stats.rx_dropped++;
1168 }
1169}
1170
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001171static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1172 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001173{
1174 struct vxlan_dev *vxlan = netdev_priv(dev);
1175 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001176 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001177 struct vxlanhdr *vxh;
1178 struct udphdr *uh;
1179 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001180 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001181 __be16 src_port, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001182 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001183 __be16 df = 0;
1184 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001185 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001186
stephen hemminger823aa872013-04-27 11:31:57 +00001187 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001188 vni = rdst->remote_vni;
1189 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001190
1191 if (!dst) {
1192 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001193 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001194 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001195 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001196 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001197 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001198 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001199
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001200 if (!skb->encapsulation) {
1201 skb_reset_inner_headers(skb);
1202 skb->encapsulation = 1;
1203 }
1204
stephen hemmingerd3428942012-10-01 12:32:35 +00001205 /* Need space for new headers (invalidates iph ptr) */
1206 if (skb_cow_head(skb, VXLAN_HEADROOM))
1207 goto drop;
1208
stephen hemmingerd3428942012-10-01 12:32:35 +00001209 old_iph = ip_hdr(skb);
1210
stephen hemmingerd3428942012-10-01 12:32:35 +00001211 ttl = vxlan->ttl;
1212 if (!ttl && IN_MULTICAST(ntohl(dst)))
1213 ttl = 1;
1214
1215 tos = vxlan->tos;
1216 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001217 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001218
stephen hemminger05f47d62012-10-09 20:35:50 +00001219 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001220
stephen hemmingerca78f182012-10-09 20:35:48 +00001221 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001222 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001223 fl4.flowi4_tos = RT_TOS(tos);
1224 fl4.daddr = dst;
1225 fl4.saddr = vxlan->saddr;
1226
1227 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001228 if (IS_ERR(rt)) {
1229 netdev_dbg(dev, "no route to %pI4\n", &dst);
1230 dev->stats.tx_carrier_errors++;
1231 goto tx_error;
1232 }
1233
1234 if (rt->dst.dev == dev) {
1235 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1236 ip_rt_put(rt);
1237 dev->stats.collisions++;
1238 goto tx_error;
1239 }
1240
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001241 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001242 if (rt->rt_flags & RTCF_LOCAL &&
1243 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001244 struct vxlan_dev *dst_vxlan;
1245
1246 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001247 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001248 if (!dst_vxlan)
1249 goto tx_error;
1250 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001251 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001252 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001253 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1254 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001255 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001256
1257 __skb_push(skb, sizeof(*uh));
1258 skb_reset_transport_header(skb);
1259 uh = udp_hdr(skb);
1260
stephen hemminger73cf3312013-04-27 11:31:54 +00001261 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001262 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001263
1264 uh->len = htons(skb->len);
1265 uh->check = 0;
1266
stephen hemminger1cad8712012-10-09 20:35:49 +00001267 vxlan_set_owner(dev, skb);
1268
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001269 if (handle_offloads(skb))
1270 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001271
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001272 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1273 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1274
1275 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1276 IPPROTO_UDP, tos, ttl, df);
1277 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1278
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001279 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001280
1281drop:
1282 dev->stats.tx_dropped++;
1283 goto tx_free;
1284
1285tx_error:
1286 dev->stats.tx_errors++;
1287tx_free:
1288 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001289}
1290
David Stevens66817122013-03-15 04:35:51 +00001291/* Transmit local packets over Vxlan
1292 *
1293 * Outer IP header inherits ECN and DF from inner header.
1294 * Outer UDP destination is the VXLAN assigned port.
1295 * source port is based on hash of flow
1296 */
1297static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1298{
1299 struct vxlan_dev *vxlan = netdev_priv(dev);
1300 struct ethhdr *eth;
1301 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001302 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001303 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001304
1305 skb_reset_mac_header(skb);
1306 eth = eth_hdr(skb);
1307
1308 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1309 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001310
1311 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001312 did_rsc = false;
1313
1314 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1315 ntohs(eth->h_proto) == ETH_P_IP) {
1316 did_rsc = route_shortcircuit(dev, skb);
1317 if (did_rsc)
1318 f = vxlan_find_mac(vxlan, eth->h_dest);
1319 }
1320
David Stevens66817122013-03-15 04:35:51 +00001321 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001322 f = vxlan_find_mac(vxlan, all_zeros_mac);
1323 if (f == NULL) {
1324 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1325 !is_multicast_ether_addr(eth->h_dest))
1326 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001327
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001328 dev->stats.tx_dropped++;
1329 dev_kfree_skb(skb);
1330 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001331 }
David Stevens66817122013-03-15 04:35:51 +00001332 }
1333
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001334 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1335 struct sk_buff *skb1;
1336
1337 skb1 = skb_clone(skb, GFP_ATOMIC);
1338 if (skb1)
1339 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1340 }
1341
1342 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001343 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001344}
1345
stephen hemmingerd3428942012-10-01 12:32:35 +00001346/* Walk the forwarding table and purge stale entries */
1347static void vxlan_cleanup(unsigned long arg)
1348{
1349 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1350 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1351 unsigned int h;
1352
1353 if (!netif_running(vxlan->dev))
1354 return;
1355
1356 spin_lock_bh(&vxlan->hash_lock);
1357 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1358 struct hlist_node *p, *n;
1359 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1360 struct vxlan_fdb *f
1361 = container_of(p, struct vxlan_fdb, hlist);
1362 unsigned long timeout;
1363
stephen hemminger3c172862012-10-26 06:24:34 +00001364 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001365 continue;
1366
1367 timeout = f->used + vxlan->age_interval * HZ;
1368 if (time_before_eq(timeout, jiffies)) {
1369 netdev_dbg(vxlan->dev,
1370 "garbage collect %pM\n",
1371 f->eth_addr);
1372 f->state = NUD_STALE;
1373 vxlan_fdb_destroy(vxlan, f);
1374 } else if (time_before(timeout, next_timer))
1375 next_timer = timeout;
1376 }
1377 }
1378 spin_unlock_bh(&vxlan->hash_lock);
1379
1380 mod_timer(&vxlan->age_timer, next_timer);
1381}
1382
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001383static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1384{
1385 __u32 vni = vxlan->default_dst.remote_vni;
1386
1387 vxlan->vn_sock = vs;
1388 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1389}
1390
stephen hemmingerd3428942012-10-01 12:32:35 +00001391/* Setup stats when device is created */
1392static int vxlan_init(struct net_device *dev)
1393{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001394 struct vxlan_dev *vxlan = netdev_priv(dev);
1395 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1396 struct vxlan_sock *vs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001397
Pravin B Shelare8171042013-03-25 14:49:46 +00001398 dev->tstats = alloc_percpu(struct pcpu_tstats);
1399 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001400 return -ENOMEM;
1401
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001402 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001403 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001404 if (vs) {
1405 /* If we have a socket with same port already, reuse it */
1406 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001407 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001408 } else {
1409 /* otherwise make new socket outside of RTNL */
1410 dev_hold(dev);
1411 queue_work(vxlan_wq, &vxlan->sock_work);
1412 }
1413 spin_unlock(&vn->sock_lock);
1414
stephen hemmingerd3428942012-10-01 12:32:35 +00001415 return 0;
1416}
1417
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001418static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001419{
1420 struct vxlan_fdb *f;
1421
1422 spin_lock_bh(&vxlan->hash_lock);
1423 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1424 if (f)
1425 vxlan_fdb_destroy(vxlan, f);
1426 spin_unlock_bh(&vxlan->hash_lock);
1427}
1428
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001429static void vxlan_uninit(struct net_device *dev)
1430{
1431 struct vxlan_dev *vxlan = netdev_priv(dev);
1432 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1433 struct vxlan_sock *vs = vxlan->vn_sock;
1434
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001435 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001436
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001437 if (vs)
1438 vxlan_sock_release(vn, vs);
1439 free_percpu(dev->tstats);
1440}
1441
stephen hemmingerd3428942012-10-01 12:32:35 +00001442/* Start ageing timer and join group when device is brought up */
1443static int vxlan_open(struct net_device *dev)
1444{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001445 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001446 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001447 struct vxlan_sock *vs = vxlan->vn_sock;
1448
1449 /* socket hasn't been created */
1450 if (!vs)
1451 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001452
stephen hemminger3fc2de22013-07-18 08:40:15 -07001453 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
Cong Wang614334d2013-08-07 16:35:45 +08001454 vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001455 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001456 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001457 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00001458 }
1459
1460 if (vxlan->age_interval)
1461 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1462
1463 return 0;
1464}
1465
1466/* Purge the forwarding table */
1467static void vxlan_flush(struct vxlan_dev *vxlan)
1468{
Cong Wang31fec5a2013-05-27 22:35:52 +00001469 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001470
1471 spin_lock_bh(&vxlan->hash_lock);
1472 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1473 struct hlist_node *p, *n;
1474 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1475 struct vxlan_fdb *f
1476 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001477 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1478 if (!is_zero_ether_addr(f->eth_addr))
1479 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001480 }
1481 }
1482 spin_unlock_bh(&vxlan->hash_lock);
1483}
1484
1485/* Cleanup timer and forwarding table on shutdown */
1486static int vxlan_stop(struct net_device *dev)
1487{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001488 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001489 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001490 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001491
stephen hemminger3fc2de22013-07-18 08:40:15 -07001492 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1493 ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001494 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001495 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001496 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001497 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001498
1499 del_timer_sync(&vxlan->age_timer);
1500
1501 vxlan_flush(vxlan);
1502
1503 return 0;
1504}
1505
stephen hemmingerd3428942012-10-01 12:32:35 +00001506/* Stub, nothing needs to be done. */
1507static void vxlan_set_multicast_list(struct net_device *dev)
1508{
1509}
1510
1511static const struct net_device_ops vxlan_netdev_ops = {
1512 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001513 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001514 .ndo_open = vxlan_open,
1515 .ndo_stop = vxlan_stop,
1516 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001517 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001518 .ndo_set_rx_mode = vxlan_set_multicast_list,
1519 .ndo_change_mtu = eth_change_mtu,
1520 .ndo_validate_addr = eth_validate_addr,
1521 .ndo_set_mac_address = eth_mac_addr,
1522 .ndo_fdb_add = vxlan_fdb_add,
1523 .ndo_fdb_del = vxlan_fdb_delete,
1524 .ndo_fdb_dump = vxlan_fdb_dump,
1525};
1526
1527/* Info for udev, that this is a virtual tunnel endpoint */
1528static struct device_type vxlan_type = {
1529 .name = "vxlan",
1530};
1531
stephen hemmingerd3428942012-10-01 12:32:35 +00001532/* Initialize the device structure. */
1533static void vxlan_setup(struct net_device *dev)
1534{
1535 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001536 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001537 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001538
1539 eth_hw_addr_random(dev);
1540 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001541 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001542
1543 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001544 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001545 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1546
1547 dev->tx_queue_len = 0;
1548 dev->features |= NETIF_F_LLTX;
1549 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001550 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001551 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001552 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001553
1554 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001555 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001556 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001557 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001558
stephen hemminger553675f2013-05-16 11:35:20 +00001559 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001560 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001561 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1562 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001563 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001564
1565 init_timer_deferrable(&vxlan->age_timer);
1566 vxlan->age_timer.function = vxlan_cleanup;
1567 vxlan->age_timer.data = (unsigned long) vxlan;
1568
stephen hemminger05f47d62012-10-09 20:35:50 +00001569 inet_get_local_port_range(&low, &high);
1570 vxlan->port_min = low;
1571 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001572 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001573
stephen hemmingerd3428942012-10-01 12:32:35 +00001574 vxlan->dev = dev;
1575
1576 for (h = 0; h < FDB_HASH_SIZE; ++h)
1577 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1578}
1579
1580static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1581 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001582 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001583 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1584 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1585 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1586 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1587 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1588 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1589 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001590 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001591 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1592 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1593 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1594 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001595 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001596};
1597
1598static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1599{
1600 if (tb[IFLA_ADDRESS]) {
1601 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1602 pr_debug("invalid link address (not ethernet)\n");
1603 return -EINVAL;
1604 }
1605
1606 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1607 pr_debug("invalid all zero ethernet address\n");
1608 return -EADDRNOTAVAIL;
1609 }
1610 }
1611
1612 if (!data)
1613 return -EINVAL;
1614
1615 if (data[IFLA_VXLAN_ID]) {
1616 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1617 if (id >= VXLAN_VID_MASK)
1618 return -ERANGE;
1619 }
1620
stephen hemminger05f47d62012-10-09 20:35:50 +00001621 if (data[IFLA_VXLAN_PORT_RANGE]) {
1622 const struct ifla_vxlan_port_range *p
1623 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1624
1625 if (ntohs(p->high) < ntohs(p->low)) {
1626 pr_debug("port range %u .. %u not valid\n",
1627 ntohs(p->low), ntohs(p->high));
1628 return -EINVAL;
1629 }
1630 }
1631
stephen hemmingerd3428942012-10-01 12:32:35 +00001632 return 0;
1633}
1634
Yan Burman1b13c972013-01-29 23:43:07 +00001635static void vxlan_get_drvinfo(struct net_device *netdev,
1636 struct ethtool_drvinfo *drvinfo)
1637{
1638 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1639 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1640}
1641
1642static const struct ethtool_ops vxlan_ethtool_ops = {
1643 .get_drvinfo = vxlan_get_drvinfo,
1644 .get_link = ethtool_op_get_link,
1645};
1646
stephen hemminger553675f2013-05-16 11:35:20 +00001647static void vxlan_del_work(struct work_struct *work)
1648{
1649 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1650
1651 sk_release_kernel(vs->sock->sk);
1652 kfree_rcu(vs, rcu);
1653}
1654
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001655static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
1656 vxlan_rcv_t *rcv)
stephen hemminger553675f2013-05-16 11:35:20 +00001657{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001658 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemminger553675f2013-05-16 11:35:20 +00001659 struct vxlan_sock *vs;
1660 struct sock *sk;
1661 struct sockaddr_in vxlan_addr = {
1662 .sin_family = AF_INET,
1663 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07001664 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00001665 };
1666 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001667 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001668
1669 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001670 if (!vs) {
1671 pr_debug("memory alocation failure\n");
stephen hemminger553675f2013-05-16 11:35:20 +00001672 return ERR_PTR(-ENOMEM);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001673 }
stephen hemminger553675f2013-05-16 11:35:20 +00001674
1675 for (h = 0; h < VNI_HASH_SIZE; ++h)
1676 INIT_HLIST_HEAD(&vs->vni_list[h]);
1677
1678 INIT_WORK(&vs->del_work, vxlan_del_work);
1679
1680 /* Create UDP socket for encapsulation receive. */
1681 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1682 if (rc < 0) {
1683 pr_debug("UDP socket create failed\n");
1684 kfree(vs);
1685 return ERR_PTR(rc);
1686 }
1687
1688 /* Put in proper namespace */
1689 sk = vs->sock->sk;
1690 sk_change_net(sk, net);
1691
stephen hemminger553675f2013-05-16 11:35:20 +00001692 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1693 sizeof(vxlan_addr));
1694 if (rc < 0) {
1695 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1696 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1697 sk_release_kernel(sk);
1698 kfree(vs);
1699 return ERR_PTR(rc);
1700 }
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001701 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001702 vs->rcv = rcv;
stephen hemminger553675f2013-05-16 11:35:20 +00001703
1704 /* Disable multicast loopback */
1705 inet_sk(sk)->mc_loop = 0;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001706 spin_lock(&vn->sock_lock);
1707 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1708 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00001709
1710 /* Mark socket as an encapsulation socket. */
1711 udp_sk(sk)->encap_type = 1;
1712 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1713 udp_encap_enable();
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001714 return vs;
1715}
stephen hemminger553675f2013-05-16 11:35:20 +00001716
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001717static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
1718 vxlan_rcv_t *rcv)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001719{
1720 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1721 struct vxlan_sock *vs;
1722
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001723 vs = vxlan_socket_create(net, port, rcv);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001724 if (!IS_ERR(vs))
1725 return vs;
1726
1727 spin_lock(&vn->sock_lock);
1728 vs = vxlan_find_sock(net, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001729 if (vs) {
1730 if (vs->rcv == rcv)
1731 atomic_inc(&vs->refcnt);
1732 else
1733 vs = ERR_PTR(-EBUSY);
1734 }
1735 spin_unlock(&vn->sock_lock);
1736
1737 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001738 vs = ERR_PTR(-EINVAL);
1739
stephen hemminger553675f2013-05-16 11:35:20 +00001740 return vs;
1741}
1742
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001743/* Scheduled at device creation to bind to a socket */
1744static void vxlan_sock_work(struct work_struct *work)
1745{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001746 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
1747 struct net *net = dev_net(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001748 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001749 __be16 port = vxlan->dst_port;
1750 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001751
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001752 nvs = vxlan_sock_add(net, port, vxlan_rcv);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001753 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001754 if (!IS_ERR(nvs))
1755 vxlan_vs_add_dev(nvs, vxlan);
1756 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001757
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001758 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001759}
1760
stephen hemmingerd3428942012-10-01 12:32:35 +00001761static int vxlan_newlink(struct net *net, struct net_device *dev,
1762 struct nlattr *tb[], struct nlattr *data[])
1763{
stephen hemminger553675f2013-05-16 11:35:20 +00001764 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001765 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001766 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001767 __u32 vni;
1768 int err;
1769
1770 if (!data[IFLA_VXLAN_ID])
1771 return -EINVAL;
1772
1773 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001774 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001775
stephen hemminger5d174dd2013-04-27 11:31:55 +00001776 if (data[IFLA_VXLAN_GROUP])
1777 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001778
1779 if (data[IFLA_VXLAN_LOCAL])
1780 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1781
stephen hemminger34e02aa2012-10-09 20:35:53 +00001782 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001783 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001784 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001785 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001786
stephen hemminger34e02aa2012-10-09 20:35:53 +00001787 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001788 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001789 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001790 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001791
1792 if (!tb[IFLA_MTU])
1793 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001794
1795 /* update header length based on lower device */
1796 dev->hard_header_len = lowerdev->hard_header_len +
1797 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001798 }
1799
1800 if (data[IFLA_VXLAN_TOS])
1801 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1802
Vincent Bernatafb97182012-10-30 10:27:16 +00001803 if (data[IFLA_VXLAN_TTL])
1804 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1805
stephen hemmingerd3428942012-10-01 12:32:35 +00001806 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001807 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001808
1809 if (data[IFLA_VXLAN_AGEING])
1810 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1811 else
1812 vxlan->age_interval = FDB_AGE_DEFAULT;
1813
David Stevense4f67ad2012-11-20 02:50:14 +00001814 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1815 vxlan->flags |= VXLAN_F_PROXY;
1816
1817 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1818 vxlan->flags |= VXLAN_F_RSC;
1819
1820 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1821 vxlan->flags |= VXLAN_F_L2MISS;
1822
1823 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1824 vxlan->flags |= VXLAN_F_L3MISS;
1825
stephen hemmingerd3428942012-10-01 12:32:35 +00001826 if (data[IFLA_VXLAN_LIMIT])
1827 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1828
stephen hemminger05f47d62012-10-09 20:35:50 +00001829 if (data[IFLA_VXLAN_PORT_RANGE]) {
1830 const struct ifla_vxlan_port_range *p
1831 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1832 vxlan->port_min = ntohs(p->low);
1833 vxlan->port_max = ntohs(p->high);
1834 }
1835
stephen hemminger823aa872013-04-27 11:31:57 +00001836 if (data[IFLA_VXLAN_PORT])
1837 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1838
stephen hemminger553675f2013-05-16 11:35:20 +00001839 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1840 pr_info("duplicate VNI %u\n", vni);
1841 return -EEXIST;
1842 }
1843
Yan Burman1b13c972013-01-29 23:43:07 +00001844 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1845
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001846 /* create an fdb entry for default destination */
1847 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1848 vxlan->default_dst.remote_ip,
1849 NUD_REACHABLE|NUD_PERMANENT,
1850 NLM_F_EXCL|NLM_F_CREATE,
1851 vxlan->dst_port, vxlan->default_dst.remote_vni,
1852 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001853 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001854 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001855
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001856 err = register_netdevice(dev);
1857 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001858 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001859 return err;
1860 }
1861
stephen hemminger553675f2013-05-16 11:35:20 +00001862 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001863
1864 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001865}
1866
1867static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1868{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001869 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001870 struct vxlan_dev *vxlan = netdev_priv(dev);
1871
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001872 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001873 if (!hlist_unhashed(&vxlan->hlist))
1874 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001875 spin_unlock(&vn->sock_lock);
1876
stephen hemminger553675f2013-05-16 11:35:20 +00001877 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001878 unregister_netdevice_queue(dev, head);
1879}
1880
1881static size_t vxlan_get_size(const struct net_device *dev)
1882{
1883
1884 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001885 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001886 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1887 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1888 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1889 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1890 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001891 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1892 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1893 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1894 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001895 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1896 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001897 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001898 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001899 0;
1900}
1901
1902static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1903{
1904 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001905 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001906 struct ifla_vxlan_port_range ports = {
1907 .low = htons(vxlan->port_min),
1908 .high = htons(vxlan->port_max),
1909 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001910
Atzm Watanabec7995c42013-04-16 02:50:52 +00001911 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001912 goto nla_put_failure;
1913
stephen hemminger5d174dd2013-04-27 11:31:55 +00001914 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001915 goto nla_put_failure;
1916
Atzm Watanabec7995c42013-04-16 02:50:52 +00001917 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001918 goto nla_put_failure;
1919
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001920 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001921 goto nla_put_failure;
1922
1923 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1924 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001925 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1926 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1927 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1928 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1929 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1930 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1931 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1932 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1933 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001934 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001935 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1936 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001937 goto nla_put_failure;
1938
stephen hemminger05f47d62012-10-09 20:35:50 +00001939 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1940 goto nla_put_failure;
1941
stephen hemmingerd3428942012-10-01 12:32:35 +00001942 return 0;
1943
1944nla_put_failure:
1945 return -EMSGSIZE;
1946}
1947
1948static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1949 .kind = "vxlan",
1950 .maxtype = IFLA_VXLAN_MAX,
1951 .policy = vxlan_policy,
1952 .priv_size = sizeof(struct vxlan_dev),
1953 .setup = vxlan_setup,
1954 .validate = vxlan_validate,
1955 .newlink = vxlan_newlink,
1956 .dellink = vxlan_dellink,
1957 .get_size = vxlan_get_size,
1958 .fill_info = vxlan_fill_info,
1959};
1960
1961static __net_init int vxlan_init_net(struct net *net)
1962{
1963 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001964 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001965
stephen hemminger553675f2013-05-16 11:35:20 +00001966 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001967 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001968
stephen hemminger553675f2013-05-16 11:35:20 +00001969 for (h = 0; h < PORT_HASH_SIZE; ++h)
1970 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001971
1972 return 0;
1973}
1974
1975static __net_exit void vxlan_exit_net(struct net *net)
1976{
1977 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001978 struct vxlan_dev *vxlan;
stephen hemminger372675a2013-07-18 08:38:26 -07001979 LIST_HEAD(list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001980
1981 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001982 list_for_each_entry(vxlan, &vn->vxlan_list, next)
stephen hemminger372675a2013-07-18 08:38:26 -07001983 unregister_netdevice_queue(vxlan->dev, &list);
1984 unregister_netdevice_many(&list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001985 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001986}
1987
1988static struct pernet_operations vxlan_net_ops = {
1989 .init = vxlan_init_net,
1990 .exit = vxlan_exit_net,
1991 .id = &vxlan_net_id,
1992 .size = sizeof(struct vxlan_net),
1993};
1994
1995static int __init vxlan_init_module(void)
1996{
1997 int rc;
1998
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001999 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2000 if (!vxlan_wq)
2001 return -ENOMEM;
2002
stephen hemmingerd3428942012-10-01 12:32:35 +00002003 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2004
2005 rc = register_pernet_device(&vxlan_net_ops);
2006 if (rc)
2007 goto out1;
2008
2009 rc = rtnl_link_register(&vxlan_link_ops);
2010 if (rc)
2011 goto out2;
2012
2013 return 0;
2014
2015out2:
2016 unregister_pernet_device(&vxlan_net_ops);
2017out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002018 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002019 return rc;
2020}
Cong Wang7332a132013-05-27 22:35:53 +00002021late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002022
2023static void __exit vxlan_cleanup_module(void)
2024{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002025 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002026 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07002027 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00002028 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00002029}
2030module_exit(vxlan_cleanup_module);
2031
2032MODULE_LICENSE("GPL");
2033MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002034MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00002035MODULE_ALIAS_RTNL_LINK("vxlan");