blob: f401c1a9d9c3bdf46f498e3d33534c0b9f6bfecf [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
Thomas Richter906dc182013-07-19 17:20:07 +0200410/* Replace destination of unicast mac */
411static int vxlan_fdb_replace(struct vxlan_fdb *f,
412 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
413{
414 struct vxlan_rdst *rd;
415
416 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
417 if (rd)
418 return 0;
419
420 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
421 if (!rd)
422 return 0;
423 rd->remote_ip = ip;
424 rd->remote_port = port;
425 rd->remote_vni = vni;
426 rd->remote_ifindex = ifindex;
427 return 1;
428}
429
David Stevens66817122013-03-15 04:35:51 +0000430/* Add/update destinations for multicast */
431static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000432 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000433{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700434 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000435
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300436 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
437 if (rd)
438 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700439
David Stevens66817122013-03-15 04:35:51 +0000440 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
441 if (rd == NULL)
442 return -ENOBUFS;
443 rd->remote_ip = ip;
444 rd->remote_port = port;
445 rd->remote_vni = vni;
446 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700447
448 list_add_tail_rcu(&rd->list, &f->remotes);
449
David Stevens66817122013-03-15 04:35:51 +0000450 return 1;
451}
452
stephen hemmingerd3428942012-10-01 12:32:35 +0000453/* Add new entry to forwarding table -- assumes lock held */
454static int vxlan_fdb_create(struct vxlan_dev *vxlan,
455 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000456 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000457 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000458 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000459{
460 struct vxlan_fdb *f;
461 int notify = 0;
462
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000463 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000464 if (f) {
465 if (flags & NLM_F_EXCL) {
466 netdev_dbg(vxlan->dev,
467 "lost race to create %pM\n", mac);
468 return -EEXIST;
469 }
470 if (f->state != state) {
471 f->state = state;
472 f->updated = jiffies;
473 notify = 1;
474 }
David Stevensae884082013-04-19 00:36:26 +0000475 if (f->flags != ndm_flags) {
476 f->flags = ndm_flags;
477 f->updated = jiffies;
478 notify = 1;
479 }
Thomas Richter906dc182013-07-19 17:20:07 +0200480 if ((flags & NLM_F_REPLACE)) {
481 /* Only change unicasts */
482 if (!(is_multicast_ether_addr(f->eth_addr) ||
483 is_zero_ether_addr(f->eth_addr))) {
484 int rc = vxlan_fdb_replace(f, ip, port, vni,
485 ifindex);
486
487 if (rc < 0)
488 return rc;
489 notify |= rc;
490 } else
491 return -EOPNOTSUPP;
492 }
David Stevens66817122013-03-15 04:35:51 +0000493 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300494 (is_multicast_ether_addr(f->eth_addr) ||
495 is_zero_ether_addr(f->eth_addr))) {
David Stevens66817122013-03-15 04:35:51 +0000496 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
497
498 if (rc < 0)
499 return rc;
500 notify |= rc;
501 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000502 } else {
503 if (!(flags & NLM_F_CREATE))
504 return -ENOENT;
505
506 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
507 return -ENOSPC;
508
Thomas Richter906dc182013-07-19 17:20:07 +0200509 /* Disallow replace to add a multicast entry */
510 if ((flags & NLM_F_REPLACE) &&
511 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
512 return -EOPNOTSUPP;
513
stephen hemmingerd3428942012-10-01 12:32:35 +0000514 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
515 f = kmalloc(sizeof(*f), GFP_ATOMIC);
516 if (!f)
517 return -ENOMEM;
518
519 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000520 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000521 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000522 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700523 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000524 memcpy(f->eth_addr, mac, ETH_ALEN);
525
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700526 vxlan_fdb_append(f, ip, port, vni, ifindex);
527
stephen hemmingerd3428942012-10-01 12:32:35 +0000528 ++vxlan->addrcnt;
529 hlist_add_head_rcu(&f->hlist,
530 vxlan_fdb_head(vxlan, mac));
531 }
532
533 if (notify)
534 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
535
536 return 0;
537}
538
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300539static void vxlan_fdb_free_rdst(struct rcu_head *head)
540{
541 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
542 kfree(rd);
543}
544
Wei Yongjun6706c822013-04-11 19:00:35 +0000545static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000546{
547 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700548 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000549
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700550 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000551 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000552 kfree(f);
553}
554
stephen hemmingerd3428942012-10-01 12:32:35 +0000555static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
556{
557 netdev_dbg(vxlan->dev,
558 "delete %pM\n", f->eth_addr);
559
560 --vxlan->addrcnt;
561 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
562
563 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000564 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000565}
566
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300567static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
568 __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
569{
570 struct net *net = dev_net(vxlan->dev);
571
572 if (tb[NDA_DST]) {
573 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
574 return -EAFNOSUPPORT;
575
576 *ip = nla_get_be32(tb[NDA_DST]);
577 } else {
578 *ip = htonl(INADDR_ANY);
579 }
580
581 if (tb[NDA_PORT]) {
582 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
583 return -EINVAL;
584 *port = nla_get_be16(tb[NDA_PORT]);
585 } else {
586 *port = vxlan->dst_port;
587 }
588
589 if (tb[NDA_VNI]) {
590 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
591 return -EINVAL;
592 *vni = nla_get_u32(tb[NDA_VNI]);
593 } else {
594 *vni = vxlan->default_dst.remote_vni;
595 }
596
597 if (tb[NDA_IFINDEX]) {
598 struct net_device *tdev;
599
600 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
601 return -EINVAL;
602 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
603 tdev = dev_get_by_index(net, *ifindex);
604 if (!tdev)
605 return -EADDRNOTAVAIL;
606 dev_put(tdev);
607 } else {
608 *ifindex = 0;
609 }
610
611 return 0;
612}
613
stephen hemmingerd3428942012-10-01 12:32:35 +0000614/* Add static entry (via netlink) */
615static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
616 struct net_device *dev,
617 const unsigned char *addr, u16 flags)
618{
619 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300620 /* struct net *net = dev_net(vxlan->dev); */
stephen hemmingerd3428942012-10-01 12:32:35 +0000621 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000622 __be16 port;
623 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000624 int err;
625
626 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
627 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
628 ndm->ndm_state);
629 return -EINVAL;
630 }
631
632 if (tb[NDA_DST] == NULL)
633 return -EINVAL;
634
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300635 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
636 if (err)
637 return err;
David Stevens66817122013-03-15 04:35:51 +0000638
stephen hemmingerd3428942012-10-01 12:32:35 +0000639 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000640 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
641 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000642 spin_unlock_bh(&vxlan->hash_lock);
643
644 return err;
645}
646
647/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000648static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
649 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000650 const unsigned char *addr)
651{
652 struct vxlan_dev *vxlan = netdev_priv(dev);
653 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300654 struct vxlan_rdst *rd = NULL;
655 __be32 ip;
656 __be16 port;
657 u32 vni, ifindex;
658 int err;
659
660 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
661 if (err)
662 return err;
663
664 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000665
666 spin_lock_bh(&vxlan->hash_lock);
667 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300668 if (!f)
669 goto out;
670
671 if (ip != htonl(INADDR_ANY)) {
672 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
673 if (!rd)
674 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000675 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300676
677 err = 0;
678
679 /* remove a destination if it's not the only one on the list,
680 * otherwise destroy the fdb entry
681 */
682 if (rd && !list_is_singular(&f->remotes)) {
683 list_del_rcu(&rd->list);
684 call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
685 goto out;
686 }
687
688 vxlan_fdb_destroy(vxlan, f);
689
690out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000691 spin_unlock_bh(&vxlan->hash_lock);
692
693 return err;
694}
695
696/* Dump forwarding table */
697static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
698 struct net_device *dev, int idx)
699{
700 struct vxlan_dev *vxlan = netdev_priv(dev);
701 unsigned int h;
702
703 for (h = 0; h < FDB_HASH_SIZE; ++h) {
704 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000705 int err;
706
Sasha Levinb67bfe02013-02-27 17:06:00 -0800707 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000708 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000709
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700710 if (idx < cb->args[0])
711 goto skip;
712
713 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000714 err = vxlan_fdb_info(skb, vxlan, f,
715 NETLINK_CB(cb->skb).portid,
716 cb->nlh->nlmsg_seq,
717 RTM_NEWNEIGH,
718 NLM_F_MULTI, rd);
719 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700720 goto out;
David Stevens66817122013-03-15 04:35:51 +0000721 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700722skip:
723 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000724 }
725 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700726out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000727 return idx;
728}
729
730/* Watch incoming packets to learn mapping between Ethernet address
731 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700732 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000733 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700734static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000735 __be32 src_ip, const u8 *src_mac)
736{
737 struct vxlan_dev *vxlan = netdev_priv(dev);
738 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000739
740 f = vxlan_find_mac(vxlan, src_mac);
741 if (likely(f)) {
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700742 struct vxlan_rdst *rdst = first_remote(f);
743
744 if (likely(rdst->remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700745 return false;
746
747 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700748 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700749 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000750
751 if (net_ratelimit())
752 netdev_info(dev,
753 "%pM migrated from %pI4 to %pI4\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700754 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000755
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700756 rdst->remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000757 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700758 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000759 } else {
760 /* learned new entry */
761 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700762
763 /* close off race between vxlan_flush and incoming packets */
764 if (netif_running(dev))
765 vxlan_fdb_create(vxlan, src_mac, src_ip,
766 NUD_REACHABLE,
767 NLM_F_EXCL|NLM_F_CREATE,
768 vxlan->dst_port,
769 vxlan->default_dst.remote_vni,
770 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000771 spin_unlock(&vxlan->hash_lock);
772 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700773
774 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000775}
776
777
778/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700779static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000780{
stephen hemminger553675f2013-05-16 11:35:20 +0000781 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000782
stephen hemminger553675f2013-05-16 11:35:20 +0000783 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000784 if (!netif_running(vxlan->dev))
785 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000786
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700787 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000788 return true;
789 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000790
791 return false;
792}
793
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700794static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000795{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700796 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000797}
798
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700799static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000800{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700801 if (!atomic_dec_and_test(&vs->refcnt))
802 return;
803
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700804 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700805 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700806 spin_unlock(&vn->sock_lock);
807
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700808 queue_work(vxlan_wq, &vs->del_work);
809}
810
811/* Callback to update multicast group membership.
812 * Scheduled when vxlan goes up/down.
813 */
814static void vxlan_igmp_work(struct work_struct *work)
815{
816 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_work);
817 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
818 struct vxlan_sock *vs = vxlan->vn_sock;
819 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000820 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000821 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
822 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000823 };
824
stephen hemmingerd3428942012-10-01 12:32:35 +0000825 lock_sock(sk);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700826 if (vxlan_group_used(vn, vxlan->default_dst.remote_ip))
827 ip_mc_join_group(sk, &mreq);
828 else
829 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000830 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000831
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700832 vxlan_sock_release(vn, vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700833 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000834}
835
836/* Callback from net/ipv4/udp.c to receive packets */
837static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
838{
839 struct iphdr *oip;
840 struct vxlanhdr *vxh;
841 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000842 struct pcpu_tstats *stats;
stephen hemminger553675f2013-05-16 11:35:20 +0000843 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000844 __u32 vni;
845 int err;
846
847 /* pop off outer UDP header */
848 __skb_pull(skb, sizeof(struct udphdr));
849
850 /* Need Vxlan and inner Ethernet header to be present */
851 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
852 goto error;
853
854 /* Drop packets with reserved bits set */
855 vxh = (struct vxlanhdr *) skb->data;
856 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
857 (vxh->vx_vni & htonl(0xff))) {
858 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
859 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
860 goto error;
861 }
862
863 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000864
865 /* Is this VNI defined? */
866 vni = ntohl(vxh->vx_vni) >> 8;
stephen hemminger553675f2013-05-16 11:35:20 +0000867 port = inet_sk(sk)->inet_sport;
868 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000869 if (!vxlan) {
stephen hemminger553675f2013-05-16 11:35:20 +0000870 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
871 vni, ntohs(port));
stephen hemmingerd3428942012-10-01 12:32:35 +0000872 goto drop;
873 }
874
875 if (!pskb_may_pull(skb, ETH_HLEN)) {
876 vxlan->dev->stats.rx_length_errors++;
877 vxlan->dev->stats.rx_errors++;
878 goto drop;
879 }
880
David Stevense4f67ad2012-11-20 02:50:14 +0000881 skb_reset_mac_header(skb);
882
stephen hemmingerd3428942012-10-01 12:32:35 +0000883 /* Re-examine inner Ethernet packet */
884 oip = ip_hdr(skb);
885 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000886
887 /* Ignore packet loops (and multicast echo) */
888 if (compare_ether_addr(eth_hdr(skb)->h_source,
889 vxlan->dev->dev_addr) == 0)
890 goto drop;
891
stephen hemminger26a41ae62013-06-17 12:09:58 -0700892 if ((vxlan->flags & VXLAN_F_LEARN) &&
893 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
894 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000895
896 __skb_tunnel_rx(skb, vxlan->dev);
897 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000898
899 /* If the NIC driver gave us an encapsulated packet with
900 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
901 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
902 * for us. Otherwise force the upper layers to verify it.
903 */
904 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
905 !(vxlan->dev->features & NETIF_F_RXCSUM))
906 skb->ip_summed = CHECKSUM_NONE;
907
908 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000909
910 err = IP_ECN_decapsulate(oip, skb);
911 if (unlikely(err)) {
912 if (log_ecn_error)
913 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
914 &oip->saddr, oip->tos);
915 if (err > 1) {
916 ++vxlan->dev->stats.rx_frame_errors;
917 ++vxlan->dev->stats.rx_errors;
918 goto drop;
919 }
920 }
921
Pravin B Shelare8171042013-03-25 14:49:46 +0000922 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000923 u64_stats_update_begin(&stats->syncp);
924 stats->rx_packets++;
925 stats->rx_bytes += skb->len;
926 u64_stats_update_end(&stats->syncp);
927
928 netif_rx(skb);
929
930 return 0;
931error:
932 /* Put UDP header back */
933 __skb_push(skb, sizeof(struct udphdr));
934
935 return 1;
936drop:
937 /* Consume bad packet */
938 kfree_skb(skb);
939 return 0;
940}
941
David Stevense4f67ad2012-11-20 02:50:14 +0000942static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
943{
944 struct vxlan_dev *vxlan = netdev_priv(dev);
945 struct arphdr *parp;
946 u8 *arpptr, *sha;
947 __be32 sip, tip;
948 struct neighbour *n;
949
950 if (dev->flags & IFF_NOARP)
951 goto out;
952
953 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
954 dev->stats.tx_dropped++;
955 goto out;
956 }
957 parp = arp_hdr(skb);
958
959 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
960 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
961 parp->ar_pro != htons(ETH_P_IP) ||
962 parp->ar_op != htons(ARPOP_REQUEST) ||
963 parp->ar_hln != dev->addr_len ||
964 parp->ar_pln != 4)
965 goto out;
966 arpptr = (u8 *)parp + sizeof(struct arphdr);
967 sha = arpptr;
968 arpptr += dev->addr_len; /* sha */
969 memcpy(&sip, arpptr, sizeof(sip));
970 arpptr += sizeof(sip);
971 arpptr += dev->addr_len; /* tha */
972 memcpy(&tip, arpptr, sizeof(tip));
973
974 if (ipv4_is_loopback(tip) ||
975 ipv4_is_multicast(tip))
976 goto out;
977
978 n = neigh_lookup(&arp_tbl, &tip, dev);
979
980 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000981 struct vxlan_fdb *f;
982 struct sk_buff *reply;
983
984 if (!(n->nud_state & NUD_CONNECTED)) {
985 neigh_release(n);
986 goto out;
987 }
988
989 f = vxlan_find_mac(vxlan, n->ha);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700990 if (f && first_remote(f)->remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000991 /* bridge-local neighbor */
992 neigh_release(n);
993 goto out;
994 }
995
996 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
997 n->ha, sha);
998
999 neigh_release(n);
1000
1001 skb_reset_mac_header(reply);
1002 __skb_pull(reply, skb_network_offset(reply));
1003 reply->ip_summed = CHECKSUM_UNNECESSARY;
1004 reply->pkt_type = PACKET_HOST;
1005
1006 if (netif_rx_ni(reply) == NET_RX_DROP)
1007 dev->stats.rx_dropped++;
1008 } else if (vxlan->flags & VXLAN_F_L3MISS)
1009 vxlan_ip_miss(dev, tip);
1010out:
1011 consume_skb(skb);
1012 return NETDEV_TX_OK;
1013}
1014
1015static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1016{
1017 struct vxlan_dev *vxlan = netdev_priv(dev);
1018 struct neighbour *n;
1019 struct iphdr *pip;
1020
1021 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1022 return false;
1023
1024 n = NULL;
1025 switch (ntohs(eth_hdr(skb)->h_proto)) {
1026 case ETH_P_IP:
1027 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1028 return false;
1029 pip = ip_hdr(skb);
1030 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1031 break;
1032 default:
1033 return false;
1034 }
1035
1036 if (n) {
1037 bool diff;
1038
1039 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1040 if (diff) {
1041 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1042 dev->addr_len);
1043 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1044 }
1045 neigh_release(n);
1046 return diff;
1047 } else if (vxlan->flags & VXLAN_F_L3MISS)
1048 vxlan_ip_miss(dev, pip->daddr);
1049 return false;
1050}
1051
stephen hemminger553675f2013-05-16 11:35:20 +00001052static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001053{
1054 sock_put(skb->sk);
1055}
1056
1057/* On transmit, associate with the tunnel socket */
1058static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1059{
stephen hemminger553675f2013-05-16 11:35:20 +00001060 struct vxlan_dev *vxlan = netdev_priv(dev);
1061 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +00001062
1063 skb_orphan(skb);
1064 sock_hold(sk);
1065 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001066 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001067}
1068
stephen hemminger05f47d62012-10-09 20:35:50 +00001069/* Compute source port for outgoing packet
1070 * first choice to use L4 flow hash since it will spread
1071 * better and maybe available from hardware
1072 * secondary choice is to use jhash on the Ethernet header
1073 */
stephen hemminger7d836a72013-04-27 11:31:56 +00001074static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001075{
1076 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1077 u32 hash;
1078
1079 hash = skb_get_rxhash(skb);
1080 if (!hash)
1081 hash = jhash(skb->data, 2 * ETH_ALEN,
1082 (__force u32) skb->protocol);
1083
stephen hemminger7d836a72013-04-27 11:31:56 +00001084 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001085}
1086
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001087static int handle_offloads(struct sk_buff *skb)
1088{
1089 if (skb_is_gso(skb)) {
1090 int err = skb_unclone(skb, GFP_ATOMIC);
1091 if (unlikely(err))
1092 return err;
1093
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001094 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001095 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1096 skb->ip_summed = CHECKSUM_NONE;
1097
1098 return 0;
1099}
1100
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001101/* Bypass encapsulation if the destination is local */
1102static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1103 struct vxlan_dev *dst_vxlan)
1104{
1105 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1106 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1107
1108 skb->pkt_type = PACKET_HOST;
1109 skb->encapsulation = 0;
1110 skb->dev = dst_vxlan->dev;
1111 __skb_pull(skb, skb_network_offset(skb));
1112
1113 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001114 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1115 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001116
1117 u64_stats_update_begin(&tx_stats->syncp);
1118 tx_stats->tx_packets++;
1119 tx_stats->tx_bytes += skb->len;
1120 u64_stats_update_end(&tx_stats->syncp);
1121
1122 if (netif_rx(skb) == NET_RX_SUCCESS) {
1123 u64_stats_update_begin(&rx_stats->syncp);
1124 rx_stats->rx_packets++;
1125 rx_stats->rx_bytes += skb->len;
1126 u64_stats_update_end(&rx_stats->syncp);
1127 } else {
1128 skb->dev->stats.rx_dropped++;
1129 }
1130}
1131
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001132static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1133 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001134{
1135 struct vxlan_dev *vxlan = netdev_priv(dev);
1136 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001137 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001138 struct vxlanhdr *vxh;
1139 struct udphdr *uh;
1140 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001141 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001142 __be16 src_port, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001143 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001144 __be16 df = 0;
1145 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001146 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001147
stephen hemminger823aa872013-04-27 11:31:57 +00001148 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001149 vni = rdst->remote_vni;
1150 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001151
1152 if (!dst) {
1153 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001154 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001155 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001156 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001157 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001158 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001159 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001160
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001161 if (!skb->encapsulation) {
1162 skb_reset_inner_headers(skb);
1163 skb->encapsulation = 1;
1164 }
1165
stephen hemmingerd3428942012-10-01 12:32:35 +00001166 /* Need space for new headers (invalidates iph ptr) */
1167 if (skb_cow_head(skb, VXLAN_HEADROOM))
1168 goto drop;
1169
stephen hemmingerd3428942012-10-01 12:32:35 +00001170 old_iph = ip_hdr(skb);
1171
stephen hemmingerd3428942012-10-01 12:32:35 +00001172 ttl = vxlan->ttl;
1173 if (!ttl && IN_MULTICAST(ntohl(dst)))
1174 ttl = 1;
1175
1176 tos = vxlan->tos;
1177 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001178 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001179
stephen hemminger05f47d62012-10-09 20:35:50 +00001180 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001181
stephen hemmingerca78f182012-10-09 20:35:48 +00001182 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001183 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001184 fl4.flowi4_tos = RT_TOS(tos);
1185 fl4.daddr = dst;
1186 fl4.saddr = vxlan->saddr;
1187
1188 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001189 if (IS_ERR(rt)) {
1190 netdev_dbg(dev, "no route to %pI4\n", &dst);
1191 dev->stats.tx_carrier_errors++;
1192 goto tx_error;
1193 }
1194
1195 if (rt->dst.dev == dev) {
1196 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1197 ip_rt_put(rt);
1198 dev->stats.collisions++;
1199 goto tx_error;
1200 }
1201
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001202 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001203 if (rt->rt_flags & RTCF_LOCAL &&
1204 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001205 struct vxlan_dev *dst_vxlan;
1206
1207 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001208 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001209 if (!dst_vxlan)
1210 goto tx_error;
1211 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001212 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001213 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001214 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1215 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001216 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001217
1218 __skb_push(skb, sizeof(*uh));
1219 skb_reset_transport_header(skb);
1220 uh = udp_hdr(skb);
1221
stephen hemminger73cf3312013-04-27 11:31:54 +00001222 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001223 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001224
1225 uh->len = htons(skb->len);
1226 uh->check = 0;
1227
stephen hemminger1cad8712012-10-09 20:35:49 +00001228 vxlan_set_owner(dev, skb);
1229
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001230 if (handle_offloads(skb))
1231 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001232
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001233 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1234 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1235
1236 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1237 IPPROTO_UDP, tos, ttl, df);
1238 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1239
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001240 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001241
1242drop:
1243 dev->stats.tx_dropped++;
1244 goto tx_free;
1245
1246tx_error:
1247 dev->stats.tx_errors++;
1248tx_free:
1249 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001250}
1251
David Stevens66817122013-03-15 04:35:51 +00001252/* Transmit local packets over Vxlan
1253 *
1254 * Outer IP header inherits ECN and DF from inner header.
1255 * Outer UDP destination is the VXLAN assigned port.
1256 * source port is based on hash of flow
1257 */
1258static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1259{
1260 struct vxlan_dev *vxlan = netdev_priv(dev);
1261 struct ethhdr *eth;
1262 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001263 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001264 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001265
1266 skb_reset_mac_header(skb);
1267 eth = eth_hdr(skb);
1268
1269 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1270 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001271
1272 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001273 did_rsc = false;
1274
1275 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1276 ntohs(eth->h_proto) == ETH_P_IP) {
1277 did_rsc = route_shortcircuit(dev, skb);
1278 if (did_rsc)
1279 f = vxlan_find_mac(vxlan, eth->h_dest);
1280 }
1281
David Stevens66817122013-03-15 04:35:51 +00001282 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001283 f = vxlan_find_mac(vxlan, all_zeros_mac);
1284 if (f == NULL) {
1285 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1286 !is_multicast_ether_addr(eth->h_dest))
1287 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001288
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001289 dev->stats.tx_dropped++;
1290 dev_kfree_skb(skb);
1291 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001292 }
David Stevens66817122013-03-15 04:35:51 +00001293 }
1294
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001295 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1296 struct sk_buff *skb1;
1297
1298 skb1 = skb_clone(skb, GFP_ATOMIC);
1299 if (skb1)
1300 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1301 }
1302
1303 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001304 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001305}
1306
stephen hemmingerd3428942012-10-01 12:32:35 +00001307/* Walk the forwarding table and purge stale entries */
1308static void vxlan_cleanup(unsigned long arg)
1309{
1310 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1311 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1312 unsigned int h;
1313
1314 if (!netif_running(vxlan->dev))
1315 return;
1316
1317 spin_lock_bh(&vxlan->hash_lock);
1318 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1319 struct hlist_node *p, *n;
1320 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1321 struct vxlan_fdb *f
1322 = container_of(p, struct vxlan_fdb, hlist);
1323 unsigned long timeout;
1324
stephen hemminger3c172862012-10-26 06:24:34 +00001325 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001326 continue;
1327
1328 timeout = f->used + vxlan->age_interval * HZ;
1329 if (time_before_eq(timeout, jiffies)) {
1330 netdev_dbg(vxlan->dev,
1331 "garbage collect %pM\n",
1332 f->eth_addr);
1333 f->state = NUD_STALE;
1334 vxlan_fdb_destroy(vxlan, f);
1335 } else if (time_before(timeout, next_timer))
1336 next_timer = timeout;
1337 }
1338 }
1339 spin_unlock_bh(&vxlan->hash_lock);
1340
1341 mod_timer(&vxlan->age_timer, next_timer);
1342}
1343
1344/* Setup stats when device is created */
1345static int vxlan_init(struct net_device *dev)
1346{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001347 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;
1350 __u32 vni = vxlan->default_dst.remote_vni;
1351
Pravin B Shelare8171042013-03-25 14:49:46 +00001352 dev->tstats = alloc_percpu(struct pcpu_tstats);
1353 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001354 return -ENOMEM;
1355
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001356 spin_lock(&vn->sock_lock);
1357 vs = vxlan_find_port(dev_net(dev), vxlan->dst_port);
1358 if (vs) {
1359 /* If we have a socket with same port already, reuse it */
1360 atomic_inc(&vs->refcnt);
1361 vxlan->vn_sock = vs;
1362 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1363 } else {
1364 /* otherwise make new socket outside of RTNL */
1365 dev_hold(dev);
1366 queue_work(vxlan_wq, &vxlan->sock_work);
1367 }
1368 spin_unlock(&vn->sock_lock);
1369
stephen hemmingerd3428942012-10-01 12:32:35 +00001370 return 0;
1371}
1372
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001373static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001374{
1375 struct vxlan_fdb *f;
1376
1377 spin_lock_bh(&vxlan->hash_lock);
1378 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1379 if (f)
1380 vxlan_fdb_destroy(vxlan, f);
1381 spin_unlock_bh(&vxlan->hash_lock);
1382}
1383
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001384static void vxlan_uninit(struct net_device *dev)
1385{
1386 struct vxlan_dev *vxlan = netdev_priv(dev);
1387 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1388 struct vxlan_sock *vs = vxlan->vn_sock;
1389
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001390 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001391
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001392 if (vs)
1393 vxlan_sock_release(vn, vs);
1394 free_percpu(dev->tstats);
1395}
1396
stephen hemmingerd3428942012-10-01 12:32:35 +00001397/* Start ageing timer and join group when device is brought up */
1398static int vxlan_open(struct net_device *dev)
1399{
1400 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001401 struct vxlan_sock *vs = vxlan->vn_sock;
1402
1403 /* socket hasn't been created */
1404 if (!vs)
1405 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001406
Atzm Watanabec7995c42013-04-16 02:50:52 +00001407 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001408 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001409 dev_hold(dev);
1410 queue_work(vxlan_wq, &vxlan->igmp_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001411 }
1412
1413 if (vxlan->age_interval)
1414 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1415
1416 return 0;
1417}
1418
1419/* Purge the forwarding table */
1420static void vxlan_flush(struct vxlan_dev *vxlan)
1421{
Cong Wang31fec5a2013-05-27 22:35:52 +00001422 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001423
1424 spin_lock_bh(&vxlan->hash_lock);
1425 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1426 struct hlist_node *p, *n;
1427 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1428 struct vxlan_fdb *f
1429 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001430 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1431 if (!is_zero_ether_addr(f->eth_addr))
1432 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001433 }
1434 }
1435 spin_unlock_bh(&vxlan->hash_lock);
1436}
1437
1438/* Cleanup timer and forwarding table on shutdown */
1439static int vxlan_stop(struct net_device *dev)
1440{
1441 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001442 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001443
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001444 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1445 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001446 dev_hold(dev);
1447 queue_work(vxlan_wq, &vxlan->igmp_work);
1448 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001449
1450 del_timer_sync(&vxlan->age_timer);
1451
1452 vxlan_flush(vxlan);
1453
1454 return 0;
1455}
1456
stephen hemmingerd3428942012-10-01 12:32:35 +00001457/* Stub, nothing needs to be done. */
1458static void vxlan_set_multicast_list(struct net_device *dev)
1459{
1460}
1461
1462static const struct net_device_ops vxlan_netdev_ops = {
1463 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001464 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001465 .ndo_open = vxlan_open,
1466 .ndo_stop = vxlan_stop,
1467 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001468 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001469 .ndo_set_rx_mode = vxlan_set_multicast_list,
1470 .ndo_change_mtu = eth_change_mtu,
1471 .ndo_validate_addr = eth_validate_addr,
1472 .ndo_set_mac_address = eth_mac_addr,
1473 .ndo_fdb_add = vxlan_fdb_add,
1474 .ndo_fdb_del = vxlan_fdb_delete,
1475 .ndo_fdb_dump = vxlan_fdb_dump,
1476};
1477
1478/* Info for udev, that this is a virtual tunnel endpoint */
1479static struct device_type vxlan_type = {
1480 .name = "vxlan",
1481};
1482
stephen hemmingerd3428942012-10-01 12:32:35 +00001483/* Initialize the device structure. */
1484static void vxlan_setup(struct net_device *dev)
1485{
1486 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001487 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001488 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001489
1490 eth_hw_addr_random(dev);
1491 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001492 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001493
1494 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001495 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001496 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1497
1498 dev->tx_queue_len = 0;
1499 dev->features |= NETIF_F_LLTX;
1500 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001501 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001502 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001503 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001504
1505 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001506 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001507 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001508 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001509
stephen hemminger553675f2013-05-16 11:35:20 +00001510 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001511 spin_lock_init(&vxlan->hash_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001512 INIT_WORK(&vxlan->igmp_work, vxlan_igmp_work);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001513 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001514
1515 init_timer_deferrable(&vxlan->age_timer);
1516 vxlan->age_timer.function = vxlan_cleanup;
1517 vxlan->age_timer.data = (unsigned long) vxlan;
1518
stephen hemminger05f47d62012-10-09 20:35:50 +00001519 inet_get_local_port_range(&low, &high);
1520 vxlan->port_min = low;
1521 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001522 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001523
stephen hemmingerd3428942012-10-01 12:32:35 +00001524 vxlan->dev = dev;
1525
1526 for (h = 0; h < FDB_HASH_SIZE; ++h)
1527 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1528}
1529
1530static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1531 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001532 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001533 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1534 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1535 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1536 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1537 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1538 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1539 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001540 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001541 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1542 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1543 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1544 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001545 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001546};
1547
1548static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1549{
1550 if (tb[IFLA_ADDRESS]) {
1551 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1552 pr_debug("invalid link address (not ethernet)\n");
1553 return -EINVAL;
1554 }
1555
1556 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1557 pr_debug("invalid all zero ethernet address\n");
1558 return -EADDRNOTAVAIL;
1559 }
1560 }
1561
1562 if (!data)
1563 return -EINVAL;
1564
1565 if (data[IFLA_VXLAN_ID]) {
1566 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1567 if (id >= VXLAN_VID_MASK)
1568 return -ERANGE;
1569 }
1570
stephen hemminger05f47d62012-10-09 20:35:50 +00001571 if (data[IFLA_VXLAN_PORT_RANGE]) {
1572 const struct ifla_vxlan_port_range *p
1573 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1574
1575 if (ntohs(p->high) < ntohs(p->low)) {
1576 pr_debug("port range %u .. %u not valid\n",
1577 ntohs(p->low), ntohs(p->high));
1578 return -EINVAL;
1579 }
1580 }
1581
stephen hemmingerd3428942012-10-01 12:32:35 +00001582 return 0;
1583}
1584
Yan Burman1b13c972013-01-29 23:43:07 +00001585static void vxlan_get_drvinfo(struct net_device *netdev,
1586 struct ethtool_drvinfo *drvinfo)
1587{
1588 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1589 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1590}
1591
1592static const struct ethtool_ops vxlan_ethtool_ops = {
1593 .get_drvinfo = vxlan_get_drvinfo,
1594 .get_link = ethtool_op_get_link,
1595};
1596
stephen hemminger553675f2013-05-16 11:35:20 +00001597static void vxlan_del_work(struct work_struct *work)
1598{
1599 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1600
1601 sk_release_kernel(vs->sock->sk);
1602 kfree_rcu(vs, rcu);
1603}
1604
stephen hemminger553675f2013-05-16 11:35:20 +00001605static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1606{
1607 struct vxlan_sock *vs;
1608 struct sock *sk;
1609 struct sockaddr_in vxlan_addr = {
1610 .sin_family = AF_INET,
1611 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07001612 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00001613 };
1614 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001615 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001616
1617 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1618 if (!vs)
1619 return ERR_PTR(-ENOMEM);
1620
1621 for (h = 0; h < VNI_HASH_SIZE; ++h)
1622 INIT_HLIST_HEAD(&vs->vni_list[h]);
1623
1624 INIT_WORK(&vs->del_work, vxlan_del_work);
1625
1626 /* Create UDP socket for encapsulation receive. */
1627 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1628 if (rc < 0) {
1629 pr_debug("UDP socket create failed\n");
1630 kfree(vs);
1631 return ERR_PTR(rc);
1632 }
1633
1634 /* Put in proper namespace */
1635 sk = vs->sock->sk;
1636 sk_change_net(sk, net);
1637
stephen hemminger553675f2013-05-16 11:35:20 +00001638 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1639 sizeof(vxlan_addr));
1640 if (rc < 0) {
1641 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1642 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1643 sk_release_kernel(sk);
1644 kfree(vs);
1645 return ERR_PTR(rc);
1646 }
1647
1648 /* Disable multicast loopback */
1649 inet_sk(sk)->mc_loop = 0;
1650
1651 /* Mark socket as an encapsulation socket. */
1652 udp_sk(sk)->encap_type = 1;
1653 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1654 udp_encap_enable();
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001655 atomic_set(&vs->refcnt, 1);
stephen hemminger553675f2013-05-16 11:35:20 +00001656
stephen hemminger553675f2013-05-16 11:35:20 +00001657 return vs;
1658}
1659
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001660/* Scheduled at device creation to bind to a socket */
1661static void vxlan_sock_work(struct work_struct *work)
1662{
1663 struct vxlan_dev *vxlan
1664 = container_of(work, struct vxlan_dev, sock_work);
1665 struct net_device *dev = vxlan->dev;
1666 struct net *net = dev_net(dev);
1667 __u32 vni = vxlan->default_dst.remote_vni;
1668 __be16 port = vxlan->dst_port;
1669 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1670 struct vxlan_sock *nvs, *ovs;
1671
1672 nvs = vxlan_socket_create(net, port);
1673 if (IS_ERR(nvs)) {
1674 netdev_err(vxlan->dev, "Can not create UDP socket, %ld\n",
1675 PTR_ERR(nvs));
1676 goto out;
1677 }
1678
1679 spin_lock(&vn->sock_lock);
1680 /* Look again to see if can reuse socket */
1681 ovs = vxlan_find_port(net, port);
1682 if (ovs) {
1683 atomic_inc(&ovs->refcnt);
1684 vxlan->vn_sock = ovs;
1685 hlist_add_head_rcu(&vxlan->hlist, vni_head(ovs, vni));
1686 spin_unlock(&vn->sock_lock);
1687
1688 sk_release_kernel(nvs->sock->sk);
1689 kfree(nvs);
1690 } else {
1691 vxlan->vn_sock = nvs;
1692 hlist_add_head_rcu(&nvs->hlist, vs_head(net, port));
1693 hlist_add_head_rcu(&vxlan->hlist, vni_head(nvs, vni));
1694 spin_unlock(&vn->sock_lock);
1695 }
1696out:
1697 dev_put(dev);
1698}
1699
stephen hemmingerd3428942012-10-01 12:32:35 +00001700static int vxlan_newlink(struct net *net, struct net_device *dev,
1701 struct nlattr *tb[], struct nlattr *data[])
1702{
stephen hemminger553675f2013-05-16 11:35:20 +00001703 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001704 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001705 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001706 __u32 vni;
1707 int err;
1708
1709 if (!data[IFLA_VXLAN_ID])
1710 return -EINVAL;
1711
1712 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001713 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001714
stephen hemminger5d174dd2013-04-27 11:31:55 +00001715 if (data[IFLA_VXLAN_GROUP])
1716 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001717
1718 if (data[IFLA_VXLAN_LOCAL])
1719 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1720
stephen hemminger34e02aa2012-10-09 20:35:53 +00001721 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001722 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001723 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001724 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001725
stephen hemminger34e02aa2012-10-09 20:35:53 +00001726 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001727 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001728 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001729 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001730
1731 if (!tb[IFLA_MTU])
1732 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001733
1734 /* update header length based on lower device */
1735 dev->hard_header_len = lowerdev->hard_header_len +
1736 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001737 }
1738
1739 if (data[IFLA_VXLAN_TOS])
1740 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1741
Vincent Bernatafb97182012-10-30 10:27:16 +00001742 if (data[IFLA_VXLAN_TTL])
1743 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1744
stephen hemmingerd3428942012-10-01 12:32:35 +00001745 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001746 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001747
1748 if (data[IFLA_VXLAN_AGEING])
1749 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1750 else
1751 vxlan->age_interval = FDB_AGE_DEFAULT;
1752
David Stevense4f67ad2012-11-20 02:50:14 +00001753 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1754 vxlan->flags |= VXLAN_F_PROXY;
1755
1756 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1757 vxlan->flags |= VXLAN_F_RSC;
1758
1759 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1760 vxlan->flags |= VXLAN_F_L2MISS;
1761
1762 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1763 vxlan->flags |= VXLAN_F_L3MISS;
1764
stephen hemmingerd3428942012-10-01 12:32:35 +00001765 if (data[IFLA_VXLAN_LIMIT])
1766 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1767
stephen hemminger05f47d62012-10-09 20:35:50 +00001768 if (data[IFLA_VXLAN_PORT_RANGE]) {
1769 const struct ifla_vxlan_port_range *p
1770 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1771 vxlan->port_min = ntohs(p->low);
1772 vxlan->port_max = ntohs(p->high);
1773 }
1774
stephen hemminger823aa872013-04-27 11:31:57 +00001775 if (data[IFLA_VXLAN_PORT])
1776 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1777
stephen hemminger553675f2013-05-16 11:35:20 +00001778 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1779 pr_info("duplicate VNI %u\n", vni);
1780 return -EEXIST;
1781 }
1782
Yan Burman1b13c972013-01-29 23:43:07 +00001783 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1784
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001785 /* create an fdb entry for default destination */
1786 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1787 vxlan->default_dst.remote_ip,
1788 NUD_REACHABLE|NUD_PERMANENT,
1789 NLM_F_EXCL|NLM_F_CREATE,
1790 vxlan->dst_port, vxlan->default_dst.remote_vni,
1791 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001792 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001793 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001794
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001795 err = register_netdevice(dev);
1796 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001797 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001798 return err;
1799 }
1800
stephen hemminger553675f2013-05-16 11:35:20 +00001801 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001802
1803 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001804}
1805
1806static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1807{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001808 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001809 struct vxlan_dev *vxlan = netdev_priv(dev);
1810
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001811 flush_workqueue(vxlan_wq);
1812
1813 spin_lock(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001814 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001815 spin_unlock(&vn->sock_lock);
1816
stephen hemminger553675f2013-05-16 11:35:20 +00001817 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001818 unregister_netdevice_queue(dev, head);
1819}
1820
1821static size_t vxlan_get_size(const struct net_device *dev)
1822{
1823
1824 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001825 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001826 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1827 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1828 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1829 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1830 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001831 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1832 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1833 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1834 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001835 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1836 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001837 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001838 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001839 0;
1840}
1841
1842static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1843{
1844 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001845 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001846 struct ifla_vxlan_port_range ports = {
1847 .low = htons(vxlan->port_min),
1848 .high = htons(vxlan->port_max),
1849 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001850
Atzm Watanabec7995c42013-04-16 02:50:52 +00001851 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001852 goto nla_put_failure;
1853
stephen hemminger5d174dd2013-04-27 11:31:55 +00001854 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001855 goto nla_put_failure;
1856
Atzm Watanabec7995c42013-04-16 02:50:52 +00001857 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001858 goto nla_put_failure;
1859
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001860 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001861 goto nla_put_failure;
1862
1863 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1864 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001865 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1866 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1867 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1868 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1869 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1870 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1871 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1872 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1873 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001874 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001875 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1876 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001877 goto nla_put_failure;
1878
stephen hemminger05f47d62012-10-09 20:35:50 +00001879 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1880 goto nla_put_failure;
1881
stephen hemmingerd3428942012-10-01 12:32:35 +00001882 return 0;
1883
1884nla_put_failure:
1885 return -EMSGSIZE;
1886}
1887
1888static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1889 .kind = "vxlan",
1890 .maxtype = IFLA_VXLAN_MAX,
1891 .policy = vxlan_policy,
1892 .priv_size = sizeof(struct vxlan_dev),
1893 .setup = vxlan_setup,
1894 .validate = vxlan_validate,
1895 .newlink = vxlan_newlink,
1896 .dellink = vxlan_dellink,
1897 .get_size = vxlan_get_size,
1898 .fill_info = vxlan_fill_info,
1899};
1900
1901static __net_init int vxlan_init_net(struct net *net)
1902{
1903 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001904 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001905
stephen hemminger553675f2013-05-16 11:35:20 +00001906 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001907 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001908
stephen hemminger553675f2013-05-16 11:35:20 +00001909 for (h = 0; h < PORT_HASH_SIZE; ++h)
1910 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001911
1912 return 0;
1913}
1914
1915static __net_exit void vxlan_exit_net(struct net *net)
1916{
1917 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001918 struct vxlan_dev *vxlan;
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001919
1920 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001921 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1922 dev_close(vxlan->dev);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001923 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001924}
1925
1926static struct pernet_operations vxlan_net_ops = {
1927 .init = vxlan_init_net,
1928 .exit = vxlan_exit_net,
1929 .id = &vxlan_net_id,
1930 .size = sizeof(struct vxlan_net),
1931};
1932
1933static int __init vxlan_init_module(void)
1934{
1935 int rc;
1936
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001937 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1938 if (!vxlan_wq)
1939 return -ENOMEM;
1940
stephen hemmingerd3428942012-10-01 12:32:35 +00001941 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1942
1943 rc = register_pernet_device(&vxlan_net_ops);
1944 if (rc)
1945 goto out1;
1946
1947 rc = rtnl_link_register(&vxlan_link_ops);
1948 if (rc)
1949 goto out2;
1950
1951 return 0;
1952
1953out2:
1954 unregister_pernet_device(&vxlan_net_ops);
1955out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001956 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00001957 return rc;
1958}
Cong Wang7332a132013-05-27 22:35:53 +00001959late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00001960
1961static void __exit vxlan_cleanup_module(void)
1962{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07001963 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001964 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07001965 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00001966 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001967}
1968module_exit(vxlan_cleanup_module);
1969
1970MODULE_LICENSE("GPL");
1971MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00001972MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00001973MODULE_ALIAS_RTNL_LINK("vxlan");