blob: 3b21aca0c0c249c6a6e3392d78b47e03fd7d9006 [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>
Pravin B Shelar1eaa8172013-08-19 11:23:29 -070030#include <linux/if_vlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000031#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000032#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000033#include <net/arp.h>
34#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000035#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000036#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000037#include <net/icmp.h>
38#include <net/udp.h>
39#include <net/rtnetlink.h>
40#include <net/route.h>
41#include <net/dsfield.h>
42#include <net/inet_ecn.h>
43#include <net/net_namespace.h>
44#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070045#include <net/vxlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000046
47#define VXLAN_VERSION "0.1"
48
stephen hemminger553675f2013-05-16 11:35:20 +000049#define PORT_HASH_BITS 8
50#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000051#define VNI_HASH_BITS 10
52#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
53#define FDB_HASH_BITS 8
54#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
55#define FDB_AGE_DEFAULT 300 /* 5 min */
56#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
57
58#define VXLAN_N_VID (1u << 24)
59#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000060/* IP header + UDP + VXLAN + Ethernet header */
61#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
Pravin B Shelar7ce04752013-08-19 11:22:54 -070062#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
stephen hemmingerd3428942012-10-01 12:32:35 +000063
64#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
65
66/* VXLAN protocol header */
67struct vxlanhdr {
68 __be32 vx_flags;
69 __be32 vx_vni;
70};
71
stephen hemminger23c578b2013-04-27 11:31:53 +000072/* UDP port for VXLAN traffic.
73 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070074 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000075 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070076static unsigned short vxlan_port __read_mostly = 8472;
77module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000078MODULE_PARM_DESC(udp_port, "Destination UDP port");
79
80static bool log_ecn_error = true;
81module_param(log_ecn_error, bool, 0644);
82MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
83
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070084static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000085
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030086static const u8 all_zeros_mac[ETH_ALEN];
87
stephen hemminger553675f2013-05-16 11:35:20 +000088/* per-network namespace private data for this module */
89struct vxlan_net {
90 struct list_head vxlan_list;
91 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070092 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000093};
94
David Stevens66817122013-03-15 04:35:51 +000095struct vxlan_rdst {
David Stevens66817122013-03-15 04:35:51 +000096 __be32 remote_ip;
97 __be16 remote_port;
98 u32 remote_vni;
99 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700100 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300101 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000102};
103
stephen hemmingerd3428942012-10-01 12:32:35 +0000104/* Forwarding table entry */
105struct vxlan_fdb {
106 struct hlist_node hlist; /* linked list of entries */
107 struct rcu_head rcu;
108 unsigned long updated; /* jiffies */
109 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700110 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000111 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000112 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000113 u8 eth_addr[ETH_ALEN];
114};
115
stephen hemmingerd3428942012-10-01 12:32:35 +0000116/* Pseudo network device */
117struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000118 struct hlist_node hlist; /* vni hash table */
119 struct list_head next; /* vxlan's per namespace list */
120 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000121 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000122 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000123 __be32 saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000124 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000125 __u16 port_min; /* source port range */
126 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000127 __u8 tos; /* TOS override */
128 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000129 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000130
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700131 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700132 struct work_struct igmp_join;
133 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700134
stephen hemmingerd3428942012-10-01 12:32:35 +0000135 unsigned long age_interval;
136 struct timer_list age_timer;
137 spinlock_t hash_lock;
138 unsigned int addrcnt;
139 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000140
141 struct hlist_head fdb_head[FDB_HASH_SIZE];
142};
143
David Stevense4f67ad2012-11-20 02:50:14 +0000144#define VXLAN_F_LEARN 0x01
145#define VXLAN_F_PROXY 0x02
146#define VXLAN_F_RSC 0x04
147#define VXLAN_F_L2MISS 0x08
148#define VXLAN_F_L3MISS 0x10
149
stephen hemmingerd3428942012-10-01 12:32:35 +0000150/* salt for hash table */
151static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700152static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000153
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700154static void vxlan_sock_work(struct work_struct *work);
155
stephen hemminger553675f2013-05-16 11:35:20 +0000156/* Virtual Network hash table head */
157static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
158{
159 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
160}
161
162/* Socket hash table head */
163static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000164{
165 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
166
stephen hemminger553675f2013-05-16 11:35:20 +0000167 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
168}
169
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700170/* First remote destination for a forwarding entry.
171 * Guaranteed to be non-NULL because remotes are never deleted.
172 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700173static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700174{
stephen hemminger5ca54612013-08-04 17:17:39 -0700175 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
176}
177
178static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
179{
180 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700181}
182
stephen hemminger553675f2013-05-16 11:35:20 +0000183/* Find VXLAN socket based on network namespace and UDP port */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -0700184static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000185{
186 struct vxlan_sock *vs;
187
188 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
189 if (inet_sk(vs->sock->sk)->inet_sport == port)
190 return vs;
191 }
192 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000193}
194
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700195static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000196{
197 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000198
stephen hemminger553675f2013-05-16 11:35:20 +0000199 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000200 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000201 return vxlan;
202 }
203
204 return NULL;
205}
206
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700207/* Look up VNI in a per net namespace table */
208static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
209{
210 struct vxlan_sock *vs;
211
212 vs = vxlan_find_sock(net, port);
213 if (!vs)
214 return NULL;
215
216 return vxlan_vs_find_vni(vs, id);
217}
218
stephen hemmingerd3428942012-10-01 12:32:35 +0000219/* Fill in neighbour message in skbuff. */
220static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700221 const struct vxlan_fdb *fdb,
222 u32 portid, u32 seq, int type, unsigned int flags,
223 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000224{
225 unsigned long now = jiffies;
226 struct nda_cacheinfo ci;
227 struct nlmsghdr *nlh;
228 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000229 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000230
231 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
232 if (nlh == NULL)
233 return -EMSGSIZE;
234
235 ndm = nlmsg_data(nlh);
236 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000237
238 send_eth = send_ip = true;
239
240 if (type == RTM_GETNEIGH) {
241 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000242 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000243 send_eth = !is_zero_ether_addr(fdb->eth_addr);
244 } else
245 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000246 ndm->ndm_state = fdb->state;
247 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000248 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000249 ndm->ndm_type = NDA_DST;
250
David Stevense4f67ad2012-11-20 02:50:14 +0000251 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000252 goto nla_put_failure;
253
David Stevens66817122013-03-15 04:35:51 +0000254 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
255 goto nla_put_failure;
256
stephen hemminger823aa872013-04-27 11:31:57 +0000257 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000258 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
259 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000260 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700261 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000262 goto nla_put_failure;
263 if (rdst->remote_ifindex &&
264 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000265 goto nla_put_failure;
266
267 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
268 ci.ndm_confirmed = 0;
269 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
270 ci.ndm_refcnt = 0;
271
272 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
273 goto nla_put_failure;
274
275 return nlmsg_end(skb, nlh);
276
277nla_put_failure:
278 nlmsg_cancel(skb, nlh);
279 return -EMSGSIZE;
280}
281
282static inline size_t vxlan_nlmsg_size(void)
283{
284 return NLMSG_ALIGN(sizeof(struct ndmsg))
285 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
286 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000287 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000288 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
289 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000290 + nla_total_size(sizeof(struct nda_cacheinfo));
291}
292
293static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700294 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000295{
296 struct net *net = dev_net(vxlan->dev);
297 struct sk_buff *skb;
298 int err = -ENOBUFS;
299
300 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
301 if (skb == NULL)
302 goto errout;
303
stephen hemminger5ca54612013-08-04 17:17:39 -0700304 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
305 first_remote_rtnl(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000306 if (err < 0) {
307 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
308 WARN_ON(err == -EMSGSIZE);
309 kfree_skb(skb);
310 goto errout;
311 }
312
313 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
314 return;
315errout:
316 if (err < 0)
317 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
318}
319
David Stevense4f67ad2012-11-20 02:50:14 +0000320static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
321{
322 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700323 struct vxlan_fdb f = {
324 .state = NUD_STALE,
325 };
326 struct vxlan_rdst remote = {
327 .remote_ip = ipa, /* goes to NDA_DST */
328 .remote_vni = VXLAN_N_VID,
329 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700330
331 INIT_LIST_HEAD(&f.remotes);
332 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000333
334 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
335}
336
337static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
338{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700339 struct vxlan_fdb f = {
340 .state = NUD_STALE,
341 };
David Stevense4f67ad2012-11-20 02:50:14 +0000342
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700343 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000344 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
345
346 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
347}
348
stephen hemmingerd3428942012-10-01 12:32:35 +0000349/* Hash Ethernet address */
350static u32 eth_hash(const unsigned char *addr)
351{
352 u64 value = get_unaligned((u64 *)addr);
353
354 /* only want 6 bytes */
355#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000356 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000357#else
358 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000359#endif
360 return hash_64(value, FDB_HASH_BITS);
361}
362
363/* Hash chain to use given mac address */
364static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
365 const u8 *mac)
366{
367 return &vxlan->fdb_head[eth_hash(mac)];
368}
369
370/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000371static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000372 const u8 *mac)
373
374{
375 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
376 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000377
Sasha Levinb67bfe02013-02-27 17:06:00 -0800378 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000379 if (compare_ether_addr(mac, f->eth_addr) == 0)
380 return f;
381 }
382
383 return NULL;
384}
385
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000386static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
387 const u8 *mac)
388{
389 struct vxlan_fdb *f;
390
391 f = __vxlan_find_mac(vxlan, mac);
392 if (f)
393 f->used = jiffies;
394
395 return f;
396}
397
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300398/* caller should hold vxlan->hash_lock */
399static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
400 __be32 ip, __be16 port,
401 __u32 vni, __u32 ifindex)
402{
403 struct vxlan_rdst *rd;
404
405 list_for_each_entry(rd, &f->remotes, list) {
406 if (rd->remote_ip == ip &&
407 rd->remote_port == port &&
408 rd->remote_vni == vni &&
409 rd->remote_ifindex == ifindex)
410 return rd;
411 }
412
413 return NULL;
414}
415
Thomas Richter906dc182013-07-19 17:20:07 +0200416/* Replace destination of unicast mac */
417static int vxlan_fdb_replace(struct vxlan_fdb *f,
418 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
419{
420 struct vxlan_rdst *rd;
421
422 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
423 if (rd)
424 return 0;
425
426 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
427 if (!rd)
428 return 0;
429 rd->remote_ip = ip;
430 rd->remote_port = port;
431 rd->remote_vni = vni;
432 rd->remote_ifindex = ifindex;
433 return 1;
434}
435
David Stevens66817122013-03-15 04:35:51 +0000436/* Add/update destinations for multicast */
437static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000438 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000439{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700440 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000441
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300442 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
443 if (rd)
444 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700445
David Stevens66817122013-03-15 04:35:51 +0000446 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
447 if (rd == NULL)
448 return -ENOBUFS;
449 rd->remote_ip = ip;
450 rd->remote_port = port;
451 rd->remote_vni = vni;
452 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700453
454 list_add_tail_rcu(&rd->list, &f->remotes);
455
David Stevens66817122013-03-15 04:35:51 +0000456 return 1;
457}
458
stephen hemmingerd3428942012-10-01 12:32:35 +0000459/* Add new entry to forwarding table -- assumes lock held */
460static int vxlan_fdb_create(struct vxlan_dev *vxlan,
461 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000462 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000463 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000464 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000465{
466 struct vxlan_fdb *f;
467 int notify = 0;
468
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000469 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000470 if (f) {
471 if (flags & NLM_F_EXCL) {
472 netdev_dbg(vxlan->dev,
473 "lost race to create %pM\n", mac);
474 return -EEXIST;
475 }
476 if (f->state != state) {
477 f->state = state;
478 f->updated = jiffies;
479 notify = 1;
480 }
David Stevensae884082013-04-19 00:36:26 +0000481 if (f->flags != ndm_flags) {
482 f->flags = ndm_flags;
483 f->updated = jiffies;
484 notify = 1;
485 }
Thomas Richter906dc182013-07-19 17:20:07 +0200486 if ((flags & NLM_F_REPLACE)) {
487 /* Only change unicasts */
488 if (!(is_multicast_ether_addr(f->eth_addr) ||
489 is_zero_ether_addr(f->eth_addr))) {
490 int rc = vxlan_fdb_replace(f, ip, port, vni,
491 ifindex);
492
493 if (rc < 0)
494 return rc;
495 notify |= rc;
496 } else
497 return -EOPNOTSUPP;
498 }
David Stevens66817122013-03-15 04:35:51 +0000499 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300500 (is_multicast_ether_addr(f->eth_addr) ||
501 is_zero_ether_addr(f->eth_addr))) {
David Stevens66817122013-03-15 04:35:51 +0000502 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
503
504 if (rc < 0)
505 return rc;
506 notify |= rc;
507 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000508 } else {
509 if (!(flags & NLM_F_CREATE))
510 return -ENOENT;
511
512 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
513 return -ENOSPC;
514
Thomas Richter906dc182013-07-19 17:20:07 +0200515 /* Disallow replace to add a multicast entry */
516 if ((flags & NLM_F_REPLACE) &&
517 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
518 return -EOPNOTSUPP;
519
stephen hemmingerd3428942012-10-01 12:32:35 +0000520 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
521 f = kmalloc(sizeof(*f), GFP_ATOMIC);
522 if (!f)
523 return -ENOMEM;
524
525 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000526 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000527 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000528 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700529 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000530 memcpy(f->eth_addr, mac, ETH_ALEN);
531
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700532 vxlan_fdb_append(f, ip, port, vni, ifindex);
533
stephen hemmingerd3428942012-10-01 12:32:35 +0000534 ++vxlan->addrcnt;
535 hlist_add_head_rcu(&f->hlist,
536 vxlan_fdb_head(vxlan, mac));
537 }
538
539 if (notify)
540 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
541
542 return 0;
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);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800684 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300685 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 hemminger5ca54612013-08-04 17:17:39 -0700742 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700743
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
stephen hemmingerd3428942012-10-01 12:32:35 +0000777/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700778static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000779{
stephen hemminger553675f2013-05-16 11:35:20 +0000780 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000781
stephen hemminger553675f2013-05-16 11:35:20 +0000782 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000783 if (!netif_running(vxlan->dev))
784 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000785
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700786 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000787 return true;
788 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000789
790 return false;
791}
792
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700793static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000794{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700795 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000796}
797
Pravin B Shelar012a5722013-08-19 11:23:07 -0700798void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000799{
Pravin B Shelar012a5722013-08-19 11:23:07 -0700800 struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
801
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700802 if (!atomic_dec_and_test(&vs->refcnt))
803 return;
804
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700805 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700806 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700807 spin_unlock(&vn->sock_lock);
808
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700809 queue_work(vxlan_wq, &vs->del_work);
810}
Pravin B Shelar012a5722013-08-19 11:23:07 -0700811EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700812
stephen hemminger3fc2de22013-07-18 08:40:15 -0700813/* Callback to update multicast group membership when first VNI on
814 * multicast asddress is brought up
815 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700816 */
stephen hemminger3fc2de22013-07-18 08:40:15 -0700817static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700818{
stephen hemminger3fc2de22013-07-18 08:40:15 -0700819 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700820 struct vxlan_sock *vs = vxlan->vn_sock;
821 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000822 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000823 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
824 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000825 };
826
stephen hemmingerd3428942012-10-01 12:32:35 +0000827 lock_sock(sk);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700828 ip_mc_join_group(sk, &mreq);
829 release_sock(sk);
830
Pravin B Shelar012a5722013-08-19 11:23:07 -0700831 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700832 dev_put(vxlan->dev);
833}
834
835/* Inverse of vxlan_igmp_join when last VNI is brought down */
836static void vxlan_igmp_leave(struct work_struct *work)
837{
838 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700839 struct vxlan_sock *vs = vxlan->vn_sock;
840 struct sock *sk = vs->sock->sk;
841 struct ip_mreqn mreq = {
842 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
843 .imr_ifindex = vxlan->default_dst.remote_ifindex,
844 };
845
846 lock_sock(sk);
847 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000848 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000849
Pravin B Shelar012a5722013-08-19 11:23:07 -0700850 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700851 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000852}
853
854/* Callback from net/ipv4/udp.c to receive packets */
855static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
856{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700857 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000858 struct vxlanhdr *vxh;
stephen hemminger553675f2013-05-16 11:35:20 +0000859 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000860
stephen hemmingerd3428942012-10-01 12:32:35 +0000861 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700862 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +0000863 goto error;
864
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700865 /* Return packets with reserved bits set */
866 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +0000867 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
868 (vxh->vx_vni & htonl(0xff))) {
869 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
870 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
871 goto error;
872 }
873
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700874 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +0000875 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000876
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700877 port = inet_sk(sk)->inet_sport;
878
879 vs = vxlan_find_sock(sock_net(sk), port);
880 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000881 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700882
883 vs->rcv(vs, skb, vxh->vx_vni);
884 return 0;
885
886drop:
887 /* Consume bad packet */
888 kfree_skb(skb);
889 return 0;
890
891error:
892 /* Return non vxlan pkt */
893 return 1;
894}
895
896static void vxlan_rcv(struct vxlan_sock *vs,
897 struct sk_buff *skb, __be32 vx_vni)
898{
899 struct iphdr *oip;
900 struct vxlan_dev *vxlan;
901 struct pcpu_tstats *stats;
902 __u32 vni;
903 int err;
904
905 vni = ntohl(vx_vni) >> 8;
906 /* Is this VNI defined? */
907 vxlan = vxlan_vs_find_vni(vs, vni);
908 if (!vxlan)
909 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000910
David Stevense4f67ad2012-11-20 02:50:14 +0000911 skb_reset_mac_header(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +0000912 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000913
914 /* Ignore packet loops (and multicast echo) */
915 if (compare_ether_addr(eth_hdr(skb)->h_source,
916 vxlan->dev->dev_addr) == 0)
917 goto drop;
918
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700919 /* Re-examine inner Ethernet packet */
920 oip = ip_hdr(skb);
stephen hemminger26a41ae62013-06-17 12:09:58 -0700921 if ((vxlan->flags & VXLAN_F_LEARN) &&
922 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
923 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000924
stephen hemmingerd3428942012-10-01 12:32:35 +0000925 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000926
927 /* If the NIC driver gave us an encapsulated packet with
928 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
929 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
930 * for us. Otherwise force the upper layers to verify it.
931 */
932 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
933 !(vxlan->dev->features & NETIF_F_RXCSUM))
934 skb->ip_summed = CHECKSUM_NONE;
935
936 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000937
938 err = IP_ECN_decapsulate(oip, skb);
939 if (unlikely(err)) {
940 if (log_ecn_error)
941 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
942 &oip->saddr, oip->tos);
943 if (err > 1) {
944 ++vxlan->dev->stats.rx_frame_errors;
945 ++vxlan->dev->stats.rx_errors;
946 goto drop;
947 }
948 }
949
Pravin B Shelare8171042013-03-25 14:49:46 +0000950 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000951 u64_stats_update_begin(&stats->syncp);
952 stats->rx_packets++;
953 stats->rx_bytes += skb->len;
954 u64_stats_update_end(&stats->syncp);
955
956 netif_rx(skb);
957
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700958 return;
stephen hemmingerd3428942012-10-01 12:32:35 +0000959drop:
960 /* Consume bad packet */
961 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +0000962}
963
David Stevense4f67ad2012-11-20 02:50:14 +0000964static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
965{
966 struct vxlan_dev *vxlan = netdev_priv(dev);
967 struct arphdr *parp;
968 u8 *arpptr, *sha;
969 __be32 sip, tip;
970 struct neighbour *n;
971
972 if (dev->flags & IFF_NOARP)
973 goto out;
974
975 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
976 dev->stats.tx_dropped++;
977 goto out;
978 }
979 parp = arp_hdr(skb);
980
981 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
982 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
983 parp->ar_pro != htons(ETH_P_IP) ||
984 parp->ar_op != htons(ARPOP_REQUEST) ||
985 parp->ar_hln != dev->addr_len ||
986 parp->ar_pln != 4)
987 goto out;
988 arpptr = (u8 *)parp + sizeof(struct arphdr);
989 sha = arpptr;
990 arpptr += dev->addr_len; /* sha */
991 memcpy(&sip, arpptr, sizeof(sip));
992 arpptr += sizeof(sip);
993 arpptr += dev->addr_len; /* tha */
994 memcpy(&tip, arpptr, sizeof(tip));
995
996 if (ipv4_is_loopback(tip) ||
997 ipv4_is_multicast(tip))
998 goto out;
999
1000 n = neigh_lookup(&arp_tbl, &tip, dev);
1001
1002 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001003 struct vxlan_fdb *f;
1004 struct sk_buff *reply;
1005
1006 if (!(n->nud_state & NUD_CONNECTED)) {
1007 neigh_release(n);
1008 goto out;
1009 }
1010
1011 f = vxlan_find_mac(vxlan, n->ha);
stephen hemminger5ca54612013-08-04 17:17:39 -07001012 if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001013 /* bridge-local neighbor */
1014 neigh_release(n);
1015 goto out;
1016 }
1017
1018 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1019 n->ha, sha);
1020
1021 neigh_release(n);
1022
1023 skb_reset_mac_header(reply);
1024 __skb_pull(reply, skb_network_offset(reply));
1025 reply->ip_summed = CHECKSUM_UNNECESSARY;
1026 reply->pkt_type = PACKET_HOST;
1027
1028 if (netif_rx_ni(reply) == NET_RX_DROP)
1029 dev->stats.rx_dropped++;
1030 } else if (vxlan->flags & VXLAN_F_L3MISS)
1031 vxlan_ip_miss(dev, tip);
1032out:
1033 consume_skb(skb);
1034 return NETDEV_TX_OK;
1035}
1036
1037static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1038{
1039 struct vxlan_dev *vxlan = netdev_priv(dev);
1040 struct neighbour *n;
1041 struct iphdr *pip;
1042
1043 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1044 return false;
1045
1046 n = NULL;
1047 switch (ntohs(eth_hdr(skb)->h_proto)) {
1048 case ETH_P_IP:
1049 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1050 return false;
1051 pip = ip_hdr(skb);
1052 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1053 break;
1054 default:
1055 return false;
1056 }
1057
1058 if (n) {
1059 bool diff;
1060
1061 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1062 if (diff) {
1063 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1064 dev->addr_len);
1065 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1066 }
1067 neigh_release(n);
1068 return diff;
1069 } else if (vxlan->flags & VXLAN_F_L3MISS)
1070 vxlan_ip_miss(dev, pip->daddr);
1071 return false;
1072}
1073
stephen hemminger553675f2013-05-16 11:35:20 +00001074static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001075{
1076 sock_put(skb->sk);
1077}
1078
1079/* On transmit, associate with the tunnel socket */
Pravin B Shelar49560532013-08-19 11:23:17 -07001080static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001081{
stephen hemminger1cad8712012-10-09 20:35:49 +00001082 skb_orphan(skb);
1083 sock_hold(sk);
1084 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001085 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001086}
1087
stephen hemminger05f47d62012-10-09 20:35:50 +00001088/* Compute source port for outgoing packet
1089 * first choice to use L4 flow hash since it will spread
1090 * better and maybe available from hardware
1091 * secondary choice is to use jhash on the Ethernet header
1092 */
Pravin B Shelar49560532013-08-19 11:23:17 -07001093__be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001094{
Pravin B Shelar49560532013-08-19 11:23:17 -07001095 unsigned int range = (port_max - port_min) + 1;
stephen hemminger05f47d62012-10-09 20:35:50 +00001096 u32 hash;
1097
1098 hash = skb_get_rxhash(skb);
1099 if (!hash)
1100 hash = jhash(skb->data, 2 * ETH_ALEN,
1101 (__force u32) skb->protocol);
1102
Pravin B Shelar49560532013-08-19 11:23:17 -07001103 return htons((((u64) hash * range) >> 32) + port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001104}
Pravin B Shelar49560532013-08-19 11:23:17 -07001105EXPORT_SYMBOL_GPL(vxlan_src_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001106
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001107static int handle_offloads(struct sk_buff *skb)
1108{
1109 if (skb_is_gso(skb)) {
1110 int err = skb_unclone(skb, GFP_ATOMIC);
1111 if (unlikely(err))
1112 return err;
1113
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001114 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001115 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1116 skb->ip_summed = CHECKSUM_NONE;
1117
1118 return 0;
1119}
1120
Pravin B Shelar49560532013-08-19 11:23:17 -07001121int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
1122 struct rtable *rt, struct sk_buff *skb,
1123 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1124 __be16 src_port, __be16 dst_port, __be32 vni)
1125{
1126 struct vxlanhdr *vxh;
1127 struct udphdr *uh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001128 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001129 int err;
1130
1131 if (!skb->encapsulation) {
1132 skb_reset_inner_headers(skb);
1133 skb->encapsulation = 1;
1134 }
1135
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001136 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001137 + VXLAN_HLEN + sizeof(struct iphdr)
1138 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001139
1140 /* Need space for new headers (invalidates iph ptr) */
1141 err = skb_cow_head(skb, min_headroom);
1142 if (unlikely(err))
1143 return err;
1144
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001145 if (vlan_tx_tag_present(skb)) {
1146 if (WARN_ON(!__vlan_put_tag(skb,
1147 skb->vlan_proto,
1148 vlan_tx_tag_get(skb))))
1149 return -ENOMEM;
1150
1151 skb->vlan_tci = 0;
1152 }
1153
Pravin B Shelar49560532013-08-19 11:23:17 -07001154 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1155 vxh->vx_flags = htonl(VXLAN_FLAGS);
1156 vxh->vx_vni = vni;
1157
1158 __skb_push(skb, sizeof(*uh));
1159 skb_reset_transport_header(skb);
1160 uh = udp_hdr(skb);
1161
1162 uh->dest = dst_port;
1163 uh->source = src_port;
1164
1165 uh->len = htons(skb->len);
1166 uh->check = 0;
1167
1168 vxlan_set_owner(vs->sock->sk, skb);
1169
1170 err = handle_offloads(skb);
1171 if (err)
1172 return err;
1173
1174 return iptunnel_xmit(net, rt, skb, src, dst,
1175 IPPROTO_UDP, tos, ttl, df);
1176}
1177EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1178
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001179/* Bypass encapsulation if the destination is local */
1180static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1181 struct vxlan_dev *dst_vxlan)
1182{
1183 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1184 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1185
1186 skb->pkt_type = PACKET_HOST;
1187 skb->encapsulation = 0;
1188 skb->dev = dst_vxlan->dev;
1189 __skb_pull(skb, skb_network_offset(skb));
1190
1191 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001192 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1193 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001194
1195 u64_stats_update_begin(&tx_stats->syncp);
1196 tx_stats->tx_packets++;
1197 tx_stats->tx_bytes += skb->len;
1198 u64_stats_update_end(&tx_stats->syncp);
1199
1200 if (netif_rx(skb) == NET_RX_SUCCESS) {
1201 u64_stats_update_begin(&rx_stats->syncp);
1202 rx_stats->rx_packets++;
1203 rx_stats->rx_bytes += skb->len;
1204 u64_stats_update_end(&rx_stats->syncp);
1205 } else {
1206 skb->dev->stats.rx_dropped++;
1207 }
1208}
1209
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001210static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1211 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001212{
1213 struct vxlan_dev *vxlan = netdev_priv(dev);
1214 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001215 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001216 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001217 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001218 __be16 src_port, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001219 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001220 __be16 df = 0;
1221 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001222 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001223
stephen hemminger823aa872013-04-27 11:31:57 +00001224 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001225 vni = rdst->remote_vni;
1226 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001227
1228 if (!dst) {
1229 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001230 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001231 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001232 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001233 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001234 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001235 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001236
stephen hemmingerd3428942012-10-01 12:32:35 +00001237 old_iph = ip_hdr(skb);
1238
stephen hemmingerd3428942012-10-01 12:32:35 +00001239 ttl = vxlan->ttl;
1240 if (!ttl && IN_MULTICAST(ntohl(dst)))
1241 ttl = 1;
1242
1243 tos = vxlan->tos;
1244 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001245 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001246
Pravin B Shelar49560532013-08-19 11:23:17 -07001247 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001248
stephen hemmingerca78f182012-10-09 20:35:48 +00001249 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001250 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001251 fl4.flowi4_tos = RT_TOS(tos);
1252 fl4.daddr = dst;
1253 fl4.saddr = vxlan->saddr;
1254
1255 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001256 if (IS_ERR(rt)) {
1257 netdev_dbg(dev, "no route to %pI4\n", &dst);
1258 dev->stats.tx_carrier_errors++;
1259 goto tx_error;
1260 }
1261
1262 if (rt->dst.dev == dev) {
1263 netdev_dbg(dev, "circular route to %pI4\n", &dst);
stephen hemmingerd3428942012-10-01 12:32:35 +00001264 dev->stats.collisions++;
Pravin B Shelar49560532013-08-19 11:23:17 -07001265 goto rt_tx_error;
stephen hemmingerd3428942012-10-01 12:32:35 +00001266 }
1267
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001268 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001269 if (rt->rt_flags & RTCF_LOCAL &&
1270 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001271 struct vxlan_dev *dst_vxlan;
1272
1273 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001274 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001275 if (!dst_vxlan)
1276 goto tx_error;
1277 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001278 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001279 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001280
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001281 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1282 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1283
Pravin B Shelar49560532013-08-19 11:23:17 -07001284 err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb,
1285 fl4.saddr, dst, tos, ttl, df,
1286 src_port, dst_port, htonl(vni << 8));
1287
1288 if (err < 0)
1289 goto rt_tx_error;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001290 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1291
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001292 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001293
1294drop:
1295 dev->stats.tx_dropped++;
1296 goto tx_free;
1297
Pravin B Shelar49560532013-08-19 11:23:17 -07001298rt_tx_error:
1299 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001300tx_error:
1301 dev->stats.tx_errors++;
1302tx_free:
1303 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001304}
1305
David Stevens66817122013-03-15 04:35:51 +00001306/* Transmit local packets over Vxlan
1307 *
1308 * Outer IP header inherits ECN and DF from inner header.
1309 * Outer UDP destination is the VXLAN assigned port.
1310 * source port is based on hash of flow
1311 */
1312static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1313{
1314 struct vxlan_dev *vxlan = netdev_priv(dev);
1315 struct ethhdr *eth;
1316 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001317 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001318 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001319
1320 skb_reset_mac_header(skb);
1321 eth = eth_hdr(skb);
1322
1323 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1324 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001325
1326 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001327 did_rsc = false;
1328
1329 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1330 ntohs(eth->h_proto) == ETH_P_IP) {
1331 did_rsc = route_shortcircuit(dev, skb);
1332 if (did_rsc)
1333 f = vxlan_find_mac(vxlan, eth->h_dest);
1334 }
1335
David Stevens66817122013-03-15 04:35:51 +00001336 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001337 f = vxlan_find_mac(vxlan, all_zeros_mac);
1338 if (f == NULL) {
1339 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1340 !is_multicast_ether_addr(eth->h_dest))
1341 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001342
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001343 dev->stats.tx_dropped++;
1344 dev_kfree_skb(skb);
1345 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001346 }
David Stevens66817122013-03-15 04:35:51 +00001347 }
1348
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001349 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1350 struct sk_buff *skb1;
1351
1352 skb1 = skb_clone(skb, GFP_ATOMIC);
1353 if (skb1)
1354 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1355 }
1356
1357 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001358 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001359}
1360
stephen hemmingerd3428942012-10-01 12:32:35 +00001361/* Walk the forwarding table and purge stale entries */
1362static void vxlan_cleanup(unsigned long arg)
1363{
1364 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1365 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1366 unsigned int h;
1367
1368 if (!netif_running(vxlan->dev))
1369 return;
1370
1371 spin_lock_bh(&vxlan->hash_lock);
1372 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1373 struct hlist_node *p, *n;
1374 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1375 struct vxlan_fdb *f
1376 = container_of(p, struct vxlan_fdb, hlist);
1377 unsigned long timeout;
1378
stephen hemminger3c172862012-10-26 06:24:34 +00001379 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001380 continue;
1381
1382 timeout = f->used + vxlan->age_interval * HZ;
1383 if (time_before_eq(timeout, jiffies)) {
1384 netdev_dbg(vxlan->dev,
1385 "garbage collect %pM\n",
1386 f->eth_addr);
1387 f->state = NUD_STALE;
1388 vxlan_fdb_destroy(vxlan, f);
1389 } else if (time_before(timeout, next_timer))
1390 next_timer = timeout;
1391 }
1392 }
1393 spin_unlock_bh(&vxlan->hash_lock);
1394
1395 mod_timer(&vxlan->age_timer, next_timer);
1396}
1397
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001398static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1399{
1400 __u32 vni = vxlan->default_dst.remote_vni;
1401
1402 vxlan->vn_sock = vs;
1403 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1404}
1405
stephen hemmingerd3428942012-10-01 12:32:35 +00001406/* Setup stats when device is created */
1407static int vxlan_init(struct net_device *dev)
1408{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001409 struct vxlan_dev *vxlan = netdev_priv(dev);
1410 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1411 struct vxlan_sock *vs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001412
Pravin B Shelare8171042013-03-25 14:49:46 +00001413 dev->tstats = alloc_percpu(struct pcpu_tstats);
1414 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001415 return -ENOMEM;
1416
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001417 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001418 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001419 if (vs) {
1420 /* If we have a socket with same port already, reuse it */
1421 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001422 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001423 } else {
1424 /* otherwise make new socket outside of RTNL */
1425 dev_hold(dev);
1426 queue_work(vxlan_wq, &vxlan->sock_work);
1427 }
1428 spin_unlock(&vn->sock_lock);
1429
stephen hemmingerd3428942012-10-01 12:32:35 +00001430 return 0;
1431}
1432
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001433static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001434{
1435 struct vxlan_fdb *f;
1436
1437 spin_lock_bh(&vxlan->hash_lock);
1438 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1439 if (f)
1440 vxlan_fdb_destroy(vxlan, f);
1441 spin_unlock_bh(&vxlan->hash_lock);
1442}
1443
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001444static void vxlan_uninit(struct net_device *dev)
1445{
1446 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001447 struct vxlan_sock *vs = vxlan->vn_sock;
1448
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001449 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001450
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001451 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07001452 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001453 free_percpu(dev->tstats);
1454}
1455
stephen hemmingerd3428942012-10-01 12:32:35 +00001456/* Start ageing timer and join group when device is brought up */
1457static int vxlan_open(struct net_device *dev)
1458{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001459 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001460 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001461 struct vxlan_sock *vs = vxlan->vn_sock;
1462
1463 /* socket hasn't been created */
1464 if (!vs)
1465 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001466
stephen hemminger3fc2de22013-07-18 08:40:15 -07001467 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
Cong Wang614334d2013-08-07 16:35:45 +08001468 vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001469 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001470 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001471 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00001472 }
1473
1474 if (vxlan->age_interval)
1475 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1476
1477 return 0;
1478}
1479
1480/* Purge the forwarding table */
1481static void vxlan_flush(struct vxlan_dev *vxlan)
1482{
Cong Wang31fec5a2013-05-27 22:35:52 +00001483 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001484
1485 spin_lock_bh(&vxlan->hash_lock);
1486 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1487 struct hlist_node *p, *n;
1488 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1489 struct vxlan_fdb *f
1490 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001491 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1492 if (!is_zero_ether_addr(f->eth_addr))
1493 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001494 }
1495 }
1496 spin_unlock_bh(&vxlan->hash_lock);
1497}
1498
1499/* Cleanup timer and forwarding table on shutdown */
1500static int vxlan_stop(struct net_device *dev)
1501{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001502 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001503 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001504 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001505
stephen hemminger3fc2de22013-07-18 08:40:15 -07001506 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1507 ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001508 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001509 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001510 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001511 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001512
1513 del_timer_sync(&vxlan->age_timer);
1514
1515 vxlan_flush(vxlan);
1516
1517 return 0;
1518}
1519
stephen hemmingerd3428942012-10-01 12:32:35 +00001520/* Stub, nothing needs to be done. */
1521static void vxlan_set_multicast_list(struct net_device *dev)
1522{
1523}
1524
1525static const struct net_device_ops vxlan_netdev_ops = {
1526 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001527 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001528 .ndo_open = vxlan_open,
1529 .ndo_stop = vxlan_stop,
1530 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001531 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001532 .ndo_set_rx_mode = vxlan_set_multicast_list,
1533 .ndo_change_mtu = eth_change_mtu,
1534 .ndo_validate_addr = eth_validate_addr,
1535 .ndo_set_mac_address = eth_mac_addr,
1536 .ndo_fdb_add = vxlan_fdb_add,
1537 .ndo_fdb_del = vxlan_fdb_delete,
1538 .ndo_fdb_dump = vxlan_fdb_dump,
1539};
1540
1541/* Info for udev, that this is a virtual tunnel endpoint */
1542static struct device_type vxlan_type = {
1543 .name = "vxlan",
1544};
1545
stephen hemmingerd3428942012-10-01 12:32:35 +00001546/* Initialize the device structure. */
1547static void vxlan_setup(struct net_device *dev)
1548{
1549 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001550 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001551 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001552
1553 eth_hw_addr_random(dev);
1554 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001555 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001556
1557 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001558 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001559 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1560
1561 dev->tx_queue_len = 0;
1562 dev->features |= NETIF_F_LLTX;
1563 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001564 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001565 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001566 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001567
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001568 dev->vlan_features = dev->features;
1569 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001570 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001571 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001572 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
stephen hemmingerd3428942012-10-01 12:32:35 +00001573 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001574 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001575
stephen hemminger553675f2013-05-16 11:35:20 +00001576 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001577 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001578 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1579 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001580 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001581
1582 init_timer_deferrable(&vxlan->age_timer);
1583 vxlan->age_timer.function = vxlan_cleanup;
1584 vxlan->age_timer.data = (unsigned long) vxlan;
1585
stephen hemminger05f47d62012-10-09 20:35:50 +00001586 inet_get_local_port_range(&low, &high);
1587 vxlan->port_min = low;
1588 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001589 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001590
stephen hemmingerd3428942012-10-01 12:32:35 +00001591 vxlan->dev = dev;
1592
1593 for (h = 0; h < FDB_HASH_SIZE; ++h)
1594 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1595}
1596
1597static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1598 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001599 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001600 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1601 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1602 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1603 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1604 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1605 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1606 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001607 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001608 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1609 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1610 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1611 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001612 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001613};
1614
1615static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1616{
1617 if (tb[IFLA_ADDRESS]) {
1618 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1619 pr_debug("invalid link address (not ethernet)\n");
1620 return -EINVAL;
1621 }
1622
1623 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1624 pr_debug("invalid all zero ethernet address\n");
1625 return -EADDRNOTAVAIL;
1626 }
1627 }
1628
1629 if (!data)
1630 return -EINVAL;
1631
1632 if (data[IFLA_VXLAN_ID]) {
1633 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1634 if (id >= VXLAN_VID_MASK)
1635 return -ERANGE;
1636 }
1637
stephen hemminger05f47d62012-10-09 20:35:50 +00001638 if (data[IFLA_VXLAN_PORT_RANGE]) {
1639 const struct ifla_vxlan_port_range *p
1640 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1641
1642 if (ntohs(p->high) < ntohs(p->low)) {
1643 pr_debug("port range %u .. %u not valid\n",
1644 ntohs(p->low), ntohs(p->high));
1645 return -EINVAL;
1646 }
1647 }
1648
stephen hemmingerd3428942012-10-01 12:32:35 +00001649 return 0;
1650}
1651
Yan Burman1b13c972013-01-29 23:43:07 +00001652static void vxlan_get_drvinfo(struct net_device *netdev,
1653 struct ethtool_drvinfo *drvinfo)
1654{
1655 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1656 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1657}
1658
1659static const struct ethtool_ops vxlan_ethtool_ops = {
1660 .get_drvinfo = vxlan_get_drvinfo,
1661 .get_link = ethtool_op_get_link,
1662};
1663
stephen hemminger553675f2013-05-16 11:35:20 +00001664static void vxlan_del_work(struct work_struct *work)
1665{
1666 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1667
1668 sk_release_kernel(vs->sock->sk);
1669 kfree_rcu(vs, rcu);
1670}
1671
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001672static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Pravin B Shelar012a5722013-08-19 11:23:07 -07001673 vxlan_rcv_t *rcv, void *data)
stephen hemminger553675f2013-05-16 11:35:20 +00001674{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001675 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemminger553675f2013-05-16 11:35:20 +00001676 struct vxlan_sock *vs;
1677 struct sock *sk;
1678 struct sockaddr_in vxlan_addr = {
1679 .sin_family = AF_INET,
1680 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07001681 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00001682 };
1683 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001684 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001685
1686 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001687 if (!vs) {
1688 pr_debug("memory alocation failure\n");
stephen hemminger553675f2013-05-16 11:35:20 +00001689 return ERR_PTR(-ENOMEM);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001690 }
stephen hemminger553675f2013-05-16 11:35:20 +00001691
1692 for (h = 0; h < VNI_HASH_SIZE; ++h)
1693 INIT_HLIST_HEAD(&vs->vni_list[h]);
1694
1695 INIT_WORK(&vs->del_work, vxlan_del_work);
1696
1697 /* Create UDP socket for encapsulation receive. */
1698 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1699 if (rc < 0) {
1700 pr_debug("UDP socket create failed\n");
1701 kfree(vs);
1702 return ERR_PTR(rc);
1703 }
1704
1705 /* Put in proper namespace */
1706 sk = vs->sock->sk;
1707 sk_change_net(sk, net);
1708
stephen hemminger553675f2013-05-16 11:35:20 +00001709 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1710 sizeof(vxlan_addr));
1711 if (rc < 0) {
1712 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1713 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1714 sk_release_kernel(sk);
1715 kfree(vs);
1716 return ERR_PTR(rc);
1717 }
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001718 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001719 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001720 vs->data = data;
stephen hemminger553675f2013-05-16 11:35:20 +00001721
1722 /* Disable multicast loopback */
1723 inet_sk(sk)->mc_loop = 0;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001724 spin_lock(&vn->sock_lock);
1725 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1726 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00001727
1728 /* Mark socket as an encapsulation socket. */
1729 udp_sk(sk)->encap_type = 1;
1730 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1731 udp_encap_enable();
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001732 return vs;
1733}
stephen hemminger553675f2013-05-16 11:35:20 +00001734
Pravin B Shelar012a5722013-08-19 11:23:07 -07001735struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
1736 vxlan_rcv_t *rcv, void *data,
1737 bool no_share)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001738{
1739 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1740 struct vxlan_sock *vs;
1741
Pravin B Shelar012a5722013-08-19 11:23:07 -07001742 vs = vxlan_socket_create(net, port, rcv, data);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001743 if (!IS_ERR(vs))
1744 return vs;
1745
Pravin B Shelar012a5722013-08-19 11:23:07 -07001746 if (no_share) /* Return error if sharing is not allowed. */
1747 return vs;
1748
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001749 spin_lock(&vn->sock_lock);
1750 vs = vxlan_find_sock(net, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001751 if (vs) {
1752 if (vs->rcv == rcv)
1753 atomic_inc(&vs->refcnt);
1754 else
1755 vs = ERR_PTR(-EBUSY);
1756 }
1757 spin_unlock(&vn->sock_lock);
1758
1759 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001760 vs = ERR_PTR(-EINVAL);
1761
stephen hemminger553675f2013-05-16 11:35:20 +00001762 return vs;
1763}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001764EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00001765
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001766/* Scheduled at device creation to bind to a socket */
1767static void vxlan_sock_work(struct work_struct *work)
1768{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001769 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
1770 struct net *net = dev_net(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001771 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001772 __be16 port = vxlan->dst_port;
1773 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001774
Pravin B Shelar012a5722013-08-19 11:23:07 -07001775 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001776 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001777 if (!IS_ERR(nvs))
1778 vxlan_vs_add_dev(nvs, vxlan);
1779 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001780
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001781 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001782}
1783
stephen hemmingerd3428942012-10-01 12:32:35 +00001784static int vxlan_newlink(struct net *net, struct net_device *dev,
1785 struct nlattr *tb[], struct nlattr *data[])
1786{
stephen hemminger553675f2013-05-16 11:35:20 +00001787 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001788 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001789 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001790 __u32 vni;
1791 int err;
1792
1793 if (!data[IFLA_VXLAN_ID])
1794 return -EINVAL;
1795
1796 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001797 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001798
stephen hemminger5d174dd2013-04-27 11:31:55 +00001799 if (data[IFLA_VXLAN_GROUP])
1800 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001801
1802 if (data[IFLA_VXLAN_LOCAL])
1803 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1804
stephen hemminger34e02aa2012-10-09 20:35:53 +00001805 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001806 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001807 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001808 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001809
stephen hemminger34e02aa2012-10-09 20:35:53 +00001810 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001811 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001812 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001813 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001814
1815 if (!tb[IFLA_MTU])
1816 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001817
1818 /* update header length based on lower device */
1819 dev->hard_header_len = lowerdev->hard_header_len +
1820 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001821 }
1822
1823 if (data[IFLA_VXLAN_TOS])
1824 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1825
Vincent Bernatafb97182012-10-30 10:27:16 +00001826 if (data[IFLA_VXLAN_TTL])
1827 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1828
stephen hemmingerd3428942012-10-01 12:32:35 +00001829 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001830 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001831
1832 if (data[IFLA_VXLAN_AGEING])
1833 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1834 else
1835 vxlan->age_interval = FDB_AGE_DEFAULT;
1836
David Stevense4f67ad2012-11-20 02:50:14 +00001837 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1838 vxlan->flags |= VXLAN_F_PROXY;
1839
1840 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1841 vxlan->flags |= VXLAN_F_RSC;
1842
1843 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1844 vxlan->flags |= VXLAN_F_L2MISS;
1845
1846 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1847 vxlan->flags |= VXLAN_F_L3MISS;
1848
stephen hemmingerd3428942012-10-01 12:32:35 +00001849 if (data[IFLA_VXLAN_LIMIT])
1850 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1851
stephen hemminger05f47d62012-10-09 20:35:50 +00001852 if (data[IFLA_VXLAN_PORT_RANGE]) {
1853 const struct ifla_vxlan_port_range *p
1854 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1855 vxlan->port_min = ntohs(p->low);
1856 vxlan->port_max = ntohs(p->high);
1857 }
1858
stephen hemminger823aa872013-04-27 11:31:57 +00001859 if (data[IFLA_VXLAN_PORT])
1860 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1861
stephen hemminger553675f2013-05-16 11:35:20 +00001862 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1863 pr_info("duplicate VNI %u\n", vni);
1864 return -EEXIST;
1865 }
1866
Yan Burman1b13c972013-01-29 23:43:07 +00001867 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1868
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001869 /* create an fdb entry for default destination */
1870 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1871 vxlan->default_dst.remote_ip,
1872 NUD_REACHABLE|NUD_PERMANENT,
1873 NLM_F_EXCL|NLM_F_CREATE,
1874 vxlan->dst_port, vxlan->default_dst.remote_vni,
1875 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001876 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001877 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001878
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001879 err = register_netdevice(dev);
1880 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001881 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001882 return err;
1883 }
1884
stephen hemminger553675f2013-05-16 11:35:20 +00001885 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001886
1887 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001888}
1889
1890static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1891{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001892 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001893 struct vxlan_dev *vxlan = netdev_priv(dev);
1894
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001895 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001896 if (!hlist_unhashed(&vxlan->hlist))
1897 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001898 spin_unlock(&vn->sock_lock);
1899
stephen hemminger553675f2013-05-16 11:35:20 +00001900 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001901 unregister_netdevice_queue(dev, head);
1902}
1903
1904static size_t vxlan_get_size(const struct net_device *dev)
1905{
1906
1907 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001908 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001909 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1910 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1911 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1912 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1913 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001914 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1915 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1916 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1917 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001918 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1919 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001920 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001921 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001922 0;
1923}
1924
1925static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1926{
1927 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001928 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001929 struct ifla_vxlan_port_range ports = {
1930 .low = htons(vxlan->port_min),
1931 .high = htons(vxlan->port_max),
1932 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001933
Atzm Watanabec7995c42013-04-16 02:50:52 +00001934 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001935 goto nla_put_failure;
1936
stephen hemminger5d174dd2013-04-27 11:31:55 +00001937 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001938 goto nla_put_failure;
1939
Atzm Watanabec7995c42013-04-16 02:50:52 +00001940 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001941 goto nla_put_failure;
1942
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001943 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001944 goto nla_put_failure;
1945
1946 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1947 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001948 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1949 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1950 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1951 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1952 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1953 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1954 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1955 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1956 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001957 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001958 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1959 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001960 goto nla_put_failure;
1961
stephen hemminger05f47d62012-10-09 20:35:50 +00001962 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1963 goto nla_put_failure;
1964
stephen hemmingerd3428942012-10-01 12:32:35 +00001965 return 0;
1966
1967nla_put_failure:
1968 return -EMSGSIZE;
1969}
1970
1971static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1972 .kind = "vxlan",
1973 .maxtype = IFLA_VXLAN_MAX,
1974 .policy = vxlan_policy,
1975 .priv_size = sizeof(struct vxlan_dev),
1976 .setup = vxlan_setup,
1977 .validate = vxlan_validate,
1978 .newlink = vxlan_newlink,
1979 .dellink = vxlan_dellink,
1980 .get_size = vxlan_get_size,
1981 .fill_info = vxlan_fill_info,
1982};
1983
1984static __net_init int vxlan_init_net(struct net *net)
1985{
1986 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001987 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001988
stephen hemminger553675f2013-05-16 11:35:20 +00001989 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001990 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001991
stephen hemminger553675f2013-05-16 11:35:20 +00001992 for (h = 0; h < PORT_HASH_SIZE; ++h)
1993 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001994
1995 return 0;
1996}
1997
1998static __net_exit void vxlan_exit_net(struct net *net)
1999{
2000 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002001 struct vxlan_dev *vxlan;
stephen hemminger372675a2013-07-18 08:38:26 -07002002 LIST_HEAD(list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002003
2004 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00002005 list_for_each_entry(vxlan, &vn->vxlan_list, next)
stephen hemminger372675a2013-07-18 08:38:26 -07002006 unregister_netdevice_queue(vxlan->dev, &list);
2007 unregister_netdevice_many(&list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002008 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00002009}
2010
2011static struct pernet_operations vxlan_net_ops = {
2012 .init = vxlan_init_net,
2013 .exit = vxlan_exit_net,
2014 .id = &vxlan_net_id,
2015 .size = sizeof(struct vxlan_net),
2016};
2017
2018static int __init vxlan_init_module(void)
2019{
2020 int rc;
2021
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002022 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2023 if (!vxlan_wq)
2024 return -ENOMEM;
2025
stephen hemmingerd3428942012-10-01 12:32:35 +00002026 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2027
2028 rc = register_pernet_device(&vxlan_net_ops);
2029 if (rc)
2030 goto out1;
2031
2032 rc = rtnl_link_register(&vxlan_link_ops);
2033 if (rc)
2034 goto out2;
2035
2036 return 0;
2037
2038out2:
2039 unregister_pernet_device(&vxlan_net_ops);
2040out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002041 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002042 return rc;
2043}
Cong Wang7332a132013-05-27 22:35:53 +00002044late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002045
2046static void __exit vxlan_cleanup_module(void)
2047{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002048 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002049 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07002050 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00002051 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00002052}
2053module_exit(vxlan_cleanup_module);
2054
2055MODULE_LICENSE("GPL");
2056MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002057MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00002058MODULE_ALIAS_RTNL_LINK("vxlan");