blob: f3496e9953025d7d358b0dd62d8cc695b8350aeb [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>
Pravin B Shelar012a5722013-08-19 11:23:07 -070044#include <net/vxlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000045
46#define VXLAN_VERSION "0.1"
47
stephen hemminger553675f2013-05-16 11:35:20 +000048#define PORT_HASH_BITS 8
49#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000050#define VNI_HASH_BITS 10
51#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
52#define FDB_HASH_BITS 8
53#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
54#define FDB_AGE_DEFAULT 300 /* 5 min */
55#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
56
57#define VXLAN_N_VID (1u << 24)
58#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000059/* IP header + UDP + VXLAN + Ethernet header */
60#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
Pravin B Shelar7ce04752013-08-19 11:22:54 -070061#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
stephen hemmingerd3428942012-10-01 12:32:35 +000062
63#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
64
65/* VXLAN protocol header */
66struct vxlanhdr {
67 __be32 vx_flags;
68 __be32 vx_vni;
69};
70
stephen hemminger23c578b2013-04-27 11:31:53 +000071/* UDP port for VXLAN traffic.
72 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070073 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000074 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070075static unsigned short vxlan_port __read_mostly = 8472;
76module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000077MODULE_PARM_DESC(udp_port, "Destination UDP port");
78
79static bool log_ecn_error = true;
80module_param(log_ecn_error, bool, 0644);
81MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
82
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070083static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000084
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030085static const u8 all_zeros_mac[ETH_ALEN];
86
stephen hemminger553675f2013-05-16 11:35:20 +000087/* per-network namespace private data for this module */
88struct vxlan_net {
89 struct list_head vxlan_list;
90 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070091 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000092};
93
David Stevens66817122013-03-15 04:35:51 +000094struct vxlan_rdst {
David Stevens66817122013-03-15 04:35:51 +000095 __be32 remote_ip;
96 __be16 remote_port;
97 u32 remote_vni;
98 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070099 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300100 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000101};
102
stephen hemmingerd3428942012-10-01 12:32:35 +0000103/* Forwarding table entry */
104struct vxlan_fdb {
105 struct hlist_node hlist; /* linked list of entries */
106 struct rcu_head rcu;
107 unsigned long updated; /* jiffies */
108 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700109 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000110 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000111 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000112 u8 eth_addr[ETH_ALEN];
113};
114
stephen hemmingerd3428942012-10-01 12:32:35 +0000115/* Pseudo network device */
116struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000117 struct hlist_node hlist; /* vni hash table */
118 struct list_head next; /* vxlan's per namespace list */
119 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000120 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000121 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000122 __be32 saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000123 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000124 __u16 port_min; /* source port range */
125 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000126 __u8 tos; /* TOS override */
127 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000128 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000129
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700130 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700131 struct work_struct igmp_join;
132 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700133
stephen hemmingerd3428942012-10-01 12:32:35 +0000134 unsigned long age_interval;
135 struct timer_list age_timer;
136 spinlock_t hash_lock;
137 unsigned int addrcnt;
138 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000139
140 struct hlist_head fdb_head[FDB_HASH_SIZE];
141};
142
David Stevense4f67ad2012-11-20 02:50:14 +0000143#define VXLAN_F_LEARN 0x01
144#define VXLAN_F_PROXY 0x02
145#define VXLAN_F_RSC 0x04
146#define VXLAN_F_L2MISS 0x08
147#define VXLAN_F_L3MISS 0x10
148
stephen hemmingerd3428942012-10-01 12:32:35 +0000149/* salt for hash table */
150static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700151static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000152
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700153static void vxlan_sock_work(struct work_struct *work);
154
stephen hemminger553675f2013-05-16 11:35:20 +0000155/* Virtual Network hash table head */
156static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
157{
158 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
159}
160
161/* Socket hash table head */
162static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000163{
164 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
165
stephen hemminger553675f2013-05-16 11:35:20 +0000166 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
167}
168
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700169/* First remote destination for a forwarding entry.
170 * Guaranteed to be non-NULL because remotes are never deleted.
171 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700172static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700173{
stephen hemminger5ca54612013-08-04 17:17:39 -0700174 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
175}
176
177static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
178{
179 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700180}
181
stephen hemminger553675f2013-05-16 11:35:20 +0000182/* Find VXLAN socket based on network namespace and UDP port */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -0700183static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000184{
185 struct vxlan_sock *vs;
186
187 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
188 if (inet_sk(vs->sock->sk)->inet_sport == port)
189 return vs;
190 }
191 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000192}
193
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700194static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000195{
196 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000197
stephen hemminger553675f2013-05-16 11:35:20 +0000198 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000199 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000200 return vxlan;
201 }
202
203 return NULL;
204}
205
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700206/* Look up VNI in a per net namespace table */
207static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
208{
209 struct vxlan_sock *vs;
210
211 vs = vxlan_find_sock(net, port);
212 if (!vs)
213 return NULL;
214
215 return vxlan_vs_find_vni(vs, id);
216}
217
stephen hemmingerd3428942012-10-01 12:32:35 +0000218/* Fill in neighbour message in skbuff. */
219static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700220 const struct vxlan_fdb *fdb,
221 u32 portid, u32 seq, int type, unsigned int flags,
222 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000223{
224 unsigned long now = jiffies;
225 struct nda_cacheinfo ci;
226 struct nlmsghdr *nlh;
227 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000228 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000229
230 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
231 if (nlh == NULL)
232 return -EMSGSIZE;
233
234 ndm = nlmsg_data(nlh);
235 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000236
237 send_eth = send_ip = true;
238
239 if (type == RTM_GETNEIGH) {
240 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000241 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000242 send_eth = !is_zero_ether_addr(fdb->eth_addr);
243 } else
244 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000245 ndm->ndm_state = fdb->state;
246 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000247 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000248 ndm->ndm_type = NDA_DST;
249
David Stevense4f67ad2012-11-20 02:50:14 +0000250 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000251 goto nla_put_failure;
252
David Stevens66817122013-03-15 04:35:51 +0000253 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
254 goto nla_put_failure;
255
stephen hemminger823aa872013-04-27 11:31:57 +0000256 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000257 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
258 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000259 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700260 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000261 goto nla_put_failure;
262 if (rdst->remote_ifindex &&
263 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000264 goto nla_put_failure;
265
266 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
267 ci.ndm_confirmed = 0;
268 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
269 ci.ndm_refcnt = 0;
270
271 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
272 goto nla_put_failure;
273
274 return nlmsg_end(skb, nlh);
275
276nla_put_failure:
277 nlmsg_cancel(skb, nlh);
278 return -EMSGSIZE;
279}
280
281static inline size_t vxlan_nlmsg_size(void)
282{
283 return NLMSG_ALIGN(sizeof(struct ndmsg))
284 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
285 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000286 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000287 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
288 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000289 + nla_total_size(sizeof(struct nda_cacheinfo));
290}
291
292static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700293 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000294{
295 struct net *net = dev_net(vxlan->dev);
296 struct sk_buff *skb;
297 int err = -ENOBUFS;
298
299 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
300 if (skb == NULL)
301 goto errout;
302
stephen hemminger5ca54612013-08-04 17:17:39 -0700303 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
304 first_remote_rtnl(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000305 if (err < 0) {
306 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
307 WARN_ON(err == -EMSGSIZE);
308 kfree_skb(skb);
309 goto errout;
310 }
311
312 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
313 return;
314errout:
315 if (err < 0)
316 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
317}
318
David Stevense4f67ad2012-11-20 02:50:14 +0000319static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
320{
321 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700322 struct vxlan_fdb f = {
323 .state = NUD_STALE,
324 };
325 struct vxlan_rdst remote = {
326 .remote_ip = ipa, /* goes to NDA_DST */
327 .remote_vni = VXLAN_N_VID,
328 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700329
330 INIT_LIST_HEAD(&f.remotes);
331 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000332
333 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
334}
335
336static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
337{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700338 struct vxlan_fdb f = {
339 .state = NUD_STALE,
340 };
David Stevense4f67ad2012-11-20 02:50:14 +0000341
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700342 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000343 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
344
345 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
346}
347
stephen hemmingerd3428942012-10-01 12:32:35 +0000348/* Hash Ethernet address */
349static u32 eth_hash(const unsigned char *addr)
350{
351 u64 value = get_unaligned((u64 *)addr);
352
353 /* only want 6 bytes */
354#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000355 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000356#else
357 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000358#endif
359 return hash_64(value, FDB_HASH_BITS);
360}
361
362/* Hash chain to use given mac address */
363static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
364 const u8 *mac)
365{
366 return &vxlan->fdb_head[eth_hash(mac)];
367}
368
369/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000370static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000371 const u8 *mac)
372
373{
374 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
375 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000376
Sasha Levinb67bfe02013-02-27 17:06:00 -0800377 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000378 if (compare_ether_addr(mac, f->eth_addr) == 0)
379 return f;
380 }
381
382 return NULL;
383}
384
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000385static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
386 const u8 *mac)
387{
388 struct vxlan_fdb *f;
389
390 f = __vxlan_find_mac(vxlan, mac);
391 if (f)
392 f->used = jiffies;
393
394 return f;
395}
396
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300397/* caller should hold vxlan->hash_lock */
398static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
399 __be32 ip, __be16 port,
400 __u32 vni, __u32 ifindex)
401{
402 struct vxlan_rdst *rd;
403
404 list_for_each_entry(rd, &f->remotes, list) {
405 if (rd->remote_ip == ip &&
406 rd->remote_port == port &&
407 rd->remote_vni == vni &&
408 rd->remote_ifindex == ifindex)
409 return rd;
410 }
411
412 return NULL;
413}
414
Thomas Richter906dc182013-07-19 17:20:07 +0200415/* Replace destination of unicast mac */
416static int vxlan_fdb_replace(struct vxlan_fdb *f,
417 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
418{
419 struct vxlan_rdst *rd;
420
421 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
422 if (rd)
423 return 0;
424
425 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
426 if (!rd)
427 return 0;
428 rd->remote_ip = ip;
429 rd->remote_port = port;
430 rd->remote_vni = vni;
431 rd->remote_ifindex = ifindex;
432 return 1;
433}
434
David Stevens66817122013-03-15 04:35:51 +0000435/* Add/update destinations for multicast */
436static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000437 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000438{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700439 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000440
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300441 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
442 if (rd)
443 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700444
David Stevens66817122013-03-15 04:35:51 +0000445 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
446 if (rd == NULL)
447 return -ENOBUFS;
448 rd->remote_ip = ip;
449 rd->remote_port = port;
450 rd->remote_vni = vni;
451 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700452
453 list_add_tail_rcu(&rd->list, &f->remotes);
454
David Stevens66817122013-03-15 04:35:51 +0000455 return 1;
456}
457
stephen hemmingerd3428942012-10-01 12:32:35 +0000458/* Add new entry to forwarding table -- assumes lock held */
459static int vxlan_fdb_create(struct vxlan_dev *vxlan,
460 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000461 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000462 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000463 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000464{
465 struct vxlan_fdb *f;
466 int notify = 0;
467
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000468 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000469 if (f) {
470 if (flags & NLM_F_EXCL) {
471 netdev_dbg(vxlan->dev,
472 "lost race to create %pM\n", mac);
473 return -EEXIST;
474 }
475 if (f->state != state) {
476 f->state = state;
477 f->updated = jiffies;
478 notify = 1;
479 }
David Stevensae884082013-04-19 00:36:26 +0000480 if (f->flags != ndm_flags) {
481 f->flags = ndm_flags;
482 f->updated = jiffies;
483 notify = 1;
484 }
Thomas Richter906dc182013-07-19 17:20:07 +0200485 if ((flags & NLM_F_REPLACE)) {
486 /* Only change unicasts */
487 if (!(is_multicast_ether_addr(f->eth_addr) ||
488 is_zero_ether_addr(f->eth_addr))) {
489 int rc = vxlan_fdb_replace(f, ip, port, vni,
490 ifindex);
491
492 if (rc < 0)
493 return rc;
494 notify |= rc;
495 } else
496 return -EOPNOTSUPP;
497 }
David Stevens66817122013-03-15 04:35:51 +0000498 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300499 (is_multicast_ether_addr(f->eth_addr) ||
500 is_zero_ether_addr(f->eth_addr))) {
David Stevens66817122013-03-15 04:35:51 +0000501 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
502
503 if (rc < 0)
504 return rc;
505 notify |= rc;
506 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000507 } else {
508 if (!(flags & NLM_F_CREATE))
509 return -ENOENT;
510
511 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
512 return -ENOSPC;
513
Thomas Richter906dc182013-07-19 17:20:07 +0200514 /* Disallow replace to add a multicast entry */
515 if ((flags & NLM_F_REPLACE) &&
516 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
517 return -EOPNOTSUPP;
518
stephen hemmingerd3428942012-10-01 12:32:35 +0000519 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
520 f = kmalloc(sizeof(*f), GFP_ATOMIC);
521 if (!f)
522 return -ENOMEM;
523
524 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000525 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000526 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000527 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700528 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000529 memcpy(f->eth_addr, mac, ETH_ALEN);
530
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700531 vxlan_fdb_append(f, ip, port, vni, ifindex);
532
stephen hemmingerd3428942012-10-01 12:32:35 +0000533 ++vxlan->addrcnt;
534 hlist_add_head_rcu(&f->hlist,
535 vxlan_fdb_head(vxlan, mac));
536 }
537
538 if (notify)
539 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
540
541 return 0;
542}
543
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300544static void vxlan_fdb_free_rdst(struct rcu_head *head)
545{
546 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
547 kfree(rd);
548}
549
Wei Yongjun6706c822013-04-11 19:00:35 +0000550static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000551{
552 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700553 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000554
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700555 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000556 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000557 kfree(f);
558}
559
stephen hemmingerd3428942012-10-01 12:32:35 +0000560static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
561{
562 netdev_dbg(vxlan->dev,
563 "delete %pM\n", f->eth_addr);
564
565 --vxlan->addrcnt;
566 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
567
568 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000569 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000570}
571
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300572static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
573 __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
574{
575 struct net *net = dev_net(vxlan->dev);
576
577 if (tb[NDA_DST]) {
578 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
579 return -EAFNOSUPPORT;
580
581 *ip = nla_get_be32(tb[NDA_DST]);
582 } else {
583 *ip = htonl(INADDR_ANY);
584 }
585
586 if (tb[NDA_PORT]) {
587 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
588 return -EINVAL;
589 *port = nla_get_be16(tb[NDA_PORT]);
590 } else {
591 *port = vxlan->dst_port;
592 }
593
594 if (tb[NDA_VNI]) {
595 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
596 return -EINVAL;
597 *vni = nla_get_u32(tb[NDA_VNI]);
598 } else {
599 *vni = vxlan->default_dst.remote_vni;
600 }
601
602 if (tb[NDA_IFINDEX]) {
603 struct net_device *tdev;
604
605 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
606 return -EINVAL;
607 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
608 tdev = dev_get_by_index(net, *ifindex);
609 if (!tdev)
610 return -EADDRNOTAVAIL;
611 dev_put(tdev);
612 } else {
613 *ifindex = 0;
614 }
615
616 return 0;
617}
618
stephen hemmingerd3428942012-10-01 12:32:35 +0000619/* Add static entry (via netlink) */
620static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
621 struct net_device *dev,
622 const unsigned char *addr, u16 flags)
623{
624 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300625 /* struct net *net = dev_net(vxlan->dev); */
stephen hemmingerd3428942012-10-01 12:32:35 +0000626 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000627 __be16 port;
628 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000629 int err;
630
631 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
632 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
633 ndm->ndm_state);
634 return -EINVAL;
635 }
636
637 if (tb[NDA_DST] == NULL)
638 return -EINVAL;
639
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300640 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
641 if (err)
642 return err;
David Stevens66817122013-03-15 04:35:51 +0000643
stephen hemmingerd3428942012-10-01 12:32:35 +0000644 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000645 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
646 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000647 spin_unlock_bh(&vxlan->hash_lock);
648
649 return err;
650}
651
652/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000653static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
654 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000655 const unsigned char *addr)
656{
657 struct vxlan_dev *vxlan = netdev_priv(dev);
658 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300659 struct vxlan_rdst *rd = NULL;
660 __be32 ip;
661 __be16 port;
662 u32 vni, ifindex;
663 int err;
664
665 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
666 if (err)
667 return err;
668
669 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000670
671 spin_lock_bh(&vxlan->hash_lock);
672 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300673 if (!f)
674 goto out;
675
676 if (ip != htonl(INADDR_ANY)) {
677 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
678 if (!rd)
679 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000680 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300681
682 err = 0;
683
684 /* remove a destination if it's not the only one on the list,
685 * otherwise destroy the fdb entry
686 */
687 if (rd && !list_is_singular(&f->remotes)) {
688 list_del_rcu(&rd->list);
689 call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
690 goto out;
691 }
692
693 vxlan_fdb_destroy(vxlan, f);
694
695out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000696 spin_unlock_bh(&vxlan->hash_lock);
697
698 return err;
699}
700
701/* Dump forwarding table */
702static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
703 struct net_device *dev, int idx)
704{
705 struct vxlan_dev *vxlan = netdev_priv(dev);
706 unsigned int h;
707
708 for (h = 0; h < FDB_HASH_SIZE; ++h) {
709 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000710 int err;
711
Sasha Levinb67bfe02013-02-27 17:06:00 -0800712 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000713 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000714
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700715 if (idx < cb->args[0])
716 goto skip;
717
718 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000719 err = vxlan_fdb_info(skb, vxlan, f,
720 NETLINK_CB(cb->skb).portid,
721 cb->nlh->nlmsg_seq,
722 RTM_NEWNEIGH,
723 NLM_F_MULTI, rd);
724 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700725 goto out;
David Stevens66817122013-03-15 04:35:51 +0000726 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700727skip:
728 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000729 }
730 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700731out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000732 return idx;
733}
734
735/* Watch incoming packets to learn mapping between Ethernet address
736 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700737 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000738 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700739static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000740 __be32 src_ip, const u8 *src_mac)
741{
742 struct vxlan_dev *vxlan = netdev_priv(dev);
743 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000744
745 f = vxlan_find_mac(vxlan, src_mac);
746 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700747 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700748
749 if (likely(rdst->remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700750 return false;
751
752 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700753 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700754 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000755
756 if (net_ratelimit())
757 netdev_info(dev,
758 "%pM migrated from %pI4 to %pI4\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700759 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000760
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700761 rdst->remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000762 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700763 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000764 } else {
765 /* learned new entry */
766 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700767
768 /* close off race between vxlan_flush and incoming packets */
769 if (netif_running(dev))
770 vxlan_fdb_create(vxlan, src_mac, src_ip,
771 NUD_REACHABLE,
772 NLM_F_EXCL|NLM_F_CREATE,
773 vxlan->dst_port,
774 vxlan->default_dst.remote_vni,
775 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000776 spin_unlock(&vxlan->hash_lock);
777 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700778
779 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000780}
781
stephen hemmingerd3428942012-10-01 12:32:35 +0000782/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700783static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000784{
stephen hemminger553675f2013-05-16 11:35:20 +0000785 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000786
stephen hemminger553675f2013-05-16 11:35:20 +0000787 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000788 if (!netif_running(vxlan->dev))
789 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000790
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700791 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000792 return true;
793 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000794
795 return false;
796}
797
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700798static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000799{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700800 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000801}
802
Pravin B Shelar012a5722013-08-19 11:23:07 -0700803void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000804{
Pravin B Shelar012a5722013-08-19 11:23:07 -0700805 struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
806
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700807 if (!atomic_dec_and_test(&vs->refcnt))
808 return;
809
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700810 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700811 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700812 spin_unlock(&vn->sock_lock);
813
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700814 queue_work(vxlan_wq, &vs->del_work);
815}
Pravin B Shelar012a5722013-08-19 11:23:07 -0700816EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700817
stephen hemminger3fc2de22013-07-18 08:40:15 -0700818/* Callback to update multicast group membership when first VNI on
819 * multicast asddress is brought up
820 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700821 */
stephen hemminger3fc2de22013-07-18 08:40:15 -0700822static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700823{
stephen hemminger3fc2de22013-07-18 08:40:15 -0700824 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700825 struct vxlan_sock *vs = vxlan->vn_sock;
826 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000827 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000828 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
829 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000830 };
831
stephen hemmingerd3428942012-10-01 12:32:35 +0000832 lock_sock(sk);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700833 ip_mc_join_group(sk, &mreq);
834 release_sock(sk);
835
Pravin B Shelar012a5722013-08-19 11:23:07 -0700836 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700837 dev_put(vxlan->dev);
838}
839
840/* Inverse of vxlan_igmp_join when last VNI is brought down */
841static void vxlan_igmp_leave(struct work_struct *work)
842{
843 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700844 struct vxlan_sock *vs = vxlan->vn_sock;
845 struct sock *sk = vs->sock->sk;
846 struct ip_mreqn mreq = {
847 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
848 .imr_ifindex = vxlan->default_dst.remote_ifindex,
849 };
850
851 lock_sock(sk);
852 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000853 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000854
Pravin B Shelar012a5722013-08-19 11:23:07 -0700855 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700856 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000857}
858
859/* Callback from net/ipv4/udp.c to receive packets */
860static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
861{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700862 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000863 struct vxlanhdr *vxh;
stephen hemminger553675f2013-05-16 11:35:20 +0000864 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000865
stephen hemmingerd3428942012-10-01 12:32:35 +0000866 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700867 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +0000868 goto error;
869
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700870 /* Return packets with reserved bits set */
871 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +0000872 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
873 (vxh->vx_vni & htonl(0xff))) {
874 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
875 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
876 goto error;
877 }
878
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700879 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +0000880 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000881
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700882 port = inet_sk(sk)->inet_sport;
883
884 vs = vxlan_find_sock(sock_net(sk), port);
885 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000886 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700887
888 vs->rcv(vs, skb, vxh->vx_vni);
889 return 0;
890
891drop:
892 /* Consume bad packet */
893 kfree_skb(skb);
894 return 0;
895
896error:
897 /* Return non vxlan pkt */
898 return 1;
899}
900
901static void vxlan_rcv(struct vxlan_sock *vs,
902 struct sk_buff *skb, __be32 vx_vni)
903{
904 struct iphdr *oip;
905 struct vxlan_dev *vxlan;
906 struct pcpu_tstats *stats;
907 __u32 vni;
908 int err;
909
910 vni = ntohl(vx_vni) >> 8;
911 /* Is this VNI defined? */
912 vxlan = vxlan_vs_find_vni(vs, vni);
913 if (!vxlan)
914 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000915
David Stevense4f67ad2012-11-20 02:50:14 +0000916 skb_reset_mac_header(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +0000917 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000918
919 /* Ignore packet loops (and multicast echo) */
920 if (compare_ether_addr(eth_hdr(skb)->h_source,
921 vxlan->dev->dev_addr) == 0)
922 goto drop;
923
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700924 /* Re-examine inner Ethernet packet */
925 oip = ip_hdr(skb);
stephen hemminger26a41ae62013-06-17 12:09:58 -0700926 if ((vxlan->flags & VXLAN_F_LEARN) &&
927 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
928 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000929
stephen hemmingerd3428942012-10-01 12:32:35 +0000930 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000931
932 /* If the NIC driver gave us an encapsulated packet with
933 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
934 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
935 * for us. Otherwise force the upper layers to verify it.
936 */
937 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
938 !(vxlan->dev->features & NETIF_F_RXCSUM))
939 skb->ip_summed = CHECKSUM_NONE;
940
941 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000942
943 err = IP_ECN_decapsulate(oip, skb);
944 if (unlikely(err)) {
945 if (log_ecn_error)
946 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
947 &oip->saddr, oip->tos);
948 if (err > 1) {
949 ++vxlan->dev->stats.rx_frame_errors;
950 ++vxlan->dev->stats.rx_errors;
951 goto drop;
952 }
953 }
954
Pravin B Shelare8171042013-03-25 14:49:46 +0000955 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000956 u64_stats_update_begin(&stats->syncp);
957 stats->rx_packets++;
958 stats->rx_bytes += skb->len;
959 u64_stats_update_end(&stats->syncp);
960
961 netif_rx(skb);
962
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700963 return;
stephen hemmingerd3428942012-10-01 12:32:35 +0000964drop:
965 /* Consume bad packet */
966 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +0000967}
968
David Stevense4f67ad2012-11-20 02:50:14 +0000969static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
970{
971 struct vxlan_dev *vxlan = netdev_priv(dev);
972 struct arphdr *parp;
973 u8 *arpptr, *sha;
974 __be32 sip, tip;
975 struct neighbour *n;
976
977 if (dev->flags & IFF_NOARP)
978 goto out;
979
980 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
981 dev->stats.tx_dropped++;
982 goto out;
983 }
984 parp = arp_hdr(skb);
985
986 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
987 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
988 parp->ar_pro != htons(ETH_P_IP) ||
989 parp->ar_op != htons(ARPOP_REQUEST) ||
990 parp->ar_hln != dev->addr_len ||
991 parp->ar_pln != 4)
992 goto out;
993 arpptr = (u8 *)parp + sizeof(struct arphdr);
994 sha = arpptr;
995 arpptr += dev->addr_len; /* sha */
996 memcpy(&sip, arpptr, sizeof(sip));
997 arpptr += sizeof(sip);
998 arpptr += dev->addr_len; /* tha */
999 memcpy(&tip, arpptr, sizeof(tip));
1000
1001 if (ipv4_is_loopback(tip) ||
1002 ipv4_is_multicast(tip))
1003 goto out;
1004
1005 n = neigh_lookup(&arp_tbl, &tip, dev);
1006
1007 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001008 struct vxlan_fdb *f;
1009 struct sk_buff *reply;
1010
1011 if (!(n->nud_state & NUD_CONNECTED)) {
1012 neigh_release(n);
1013 goto out;
1014 }
1015
1016 f = vxlan_find_mac(vxlan, n->ha);
stephen hemminger5ca54612013-08-04 17:17:39 -07001017 if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001018 /* bridge-local neighbor */
1019 neigh_release(n);
1020 goto out;
1021 }
1022
1023 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1024 n->ha, sha);
1025
1026 neigh_release(n);
1027
1028 skb_reset_mac_header(reply);
1029 __skb_pull(reply, skb_network_offset(reply));
1030 reply->ip_summed = CHECKSUM_UNNECESSARY;
1031 reply->pkt_type = PACKET_HOST;
1032
1033 if (netif_rx_ni(reply) == NET_RX_DROP)
1034 dev->stats.rx_dropped++;
1035 } else if (vxlan->flags & VXLAN_F_L3MISS)
1036 vxlan_ip_miss(dev, tip);
1037out:
1038 consume_skb(skb);
1039 return NETDEV_TX_OK;
1040}
1041
1042static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1043{
1044 struct vxlan_dev *vxlan = netdev_priv(dev);
1045 struct neighbour *n;
1046 struct iphdr *pip;
1047
1048 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1049 return false;
1050
1051 n = NULL;
1052 switch (ntohs(eth_hdr(skb)->h_proto)) {
1053 case ETH_P_IP:
1054 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1055 return false;
1056 pip = ip_hdr(skb);
1057 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1058 break;
1059 default:
1060 return false;
1061 }
1062
1063 if (n) {
1064 bool diff;
1065
1066 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1067 if (diff) {
1068 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1069 dev->addr_len);
1070 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1071 }
1072 neigh_release(n);
1073 return diff;
1074 } else if (vxlan->flags & VXLAN_F_L3MISS)
1075 vxlan_ip_miss(dev, pip->daddr);
1076 return false;
1077}
1078
stephen hemminger553675f2013-05-16 11:35:20 +00001079static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001080{
1081 sock_put(skb->sk);
1082}
1083
1084/* On transmit, associate with the tunnel socket */
Pravin B Shelar49560532013-08-19 11:23:17 -07001085static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001086{
stephen hemminger1cad8712012-10-09 20:35:49 +00001087 skb_orphan(skb);
1088 sock_hold(sk);
1089 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001090 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001091}
1092
stephen hemminger05f47d62012-10-09 20:35:50 +00001093/* Compute source port for outgoing packet
1094 * first choice to use L4 flow hash since it will spread
1095 * better and maybe available from hardware
1096 * secondary choice is to use jhash on the Ethernet header
1097 */
Pravin B Shelar49560532013-08-19 11:23:17 -07001098__be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001099{
Pravin B Shelar49560532013-08-19 11:23:17 -07001100 unsigned int range = (port_max - port_min) + 1;
stephen hemminger05f47d62012-10-09 20:35:50 +00001101 u32 hash;
1102
1103 hash = skb_get_rxhash(skb);
1104 if (!hash)
1105 hash = jhash(skb->data, 2 * ETH_ALEN,
1106 (__force u32) skb->protocol);
1107
Pravin B Shelar49560532013-08-19 11:23:17 -07001108 return htons((((u64) hash * range) >> 32) + port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001109}
Pravin B Shelar49560532013-08-19 11:23:17 -07001110EXPORT_SYMBOL_GPL(vxlan_src_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001111
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001112static int handle_offloads(struct sk_buff *skb)
1113{
1114 if (skb_is_gso(skb)) {
1115 int err = skb_unclone(skb, GFP_ATOMIC);
1116 if (unlikely(err))
1117 return err;
1118
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001119 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001120 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1121 skb->ip_summed = CHECKSUM_NONE;
1122
1123 return 0;
1124}
1125
Pravin B Shelar49560532013-08-19 11:23:17 -07001126int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
1127 struct rtable *rt, struct sk_buff *skb,
1128 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1129 __be16 src_port, __be16 dst_port, __be32 vni)
1130{
1131 struct vxlanhdr *vxh;
1132 struct udphdr *uh;
1133 int err;
1134
1135 if (!skb->encapsulation) {
1136 skb_reset_inner_headers(skb);
1137 skb->encapsulation = 1;
1138 }
1139
1140 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1141 vxh->vx_flags = htonl(VXLAN_FLAGS);
1142 vxh->vx_vni = vni;
1143
1144 __skb_push(skb, sizeof(*uh));
1145 skb_reset_transport_header(skb);
1146 uh = udp_hdr(skb);
1147
1148 uh->dest = dst_port;
1149 uh->source = src_port;
1150
1151 uh->len = htons(skb->len);
1152 uh->check = 0;
1153
1154 vxlan_set_owner(vs->sock->sk, skb);
1155
1156 err = handle_offloads(skb);
1157 if (err)
1158 return err;
1159
1160 return iptunnel_xmit(net, rt, skb, src, dst,
1161 IPPROTO_UDP, tos, ttl, df);
1162}
1163EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1164
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001165/* Bypass encapsulation if the destination is local */
1166static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1167 struct vxlan_dev *dst_vxlan)
1168{
1169 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1170 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1171
1172 skb->pkt_type = PACKET_HOST;
1173 skb->encapsulation = 0;
1174 skb->dev = dst_vxlan->dev;
1175 __skb_pull(skb, skb_network_offset(skb));
1176
1177 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001178 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1179 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001180
1181 u64_stats_update_begin(&tx_stats->syncp);
1182 tx_stats->tx_packets++;
1183 tx_stats->tx_bytes += skb->len;
1184 u64_stats_update_end(&tx_stats->syncp);
1185
1186 if (netif_rx(skb) == NET_RX_SUCCESS) {
1187 u64_stats_update_begin(&rx_stats->syncp);
1188 rx_stats->rx_packets++;
1189 rx_stats->rx_bytes += skb->len;
1190 u64_stats_update_end(&rx_stats->syncp);
1191 } else {
1192 skb->dev->stats.rx_dropped++;
1193 }
1194}
1195
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001196static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1197 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001198{
1199 struct vxlan_dev *vxlan = netdev_priv(dev);
1200 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001201 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001202 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001203 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001204 __be16 src_port, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001205 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001206 __be16 df = 0;
1207 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001208 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001209
stephen hemminger823aa872013-04-27 11:31:57 +00001210 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001211 vni = rdst->remote_vni;
1212 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001213
1214 if (!dst) {
1215 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001216 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001217 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001218 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001219 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001220 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001221 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001222
stephen hemmingerd3428942012-10-01 12:32:35 +00001223 /* Need space for new headers (invalidates iph ptr) */
1224 if (skb_cow_head(skb, VXLAN_HEADROOM))
1225 goto drop;
1226
stephen hemmingerd3428942012-10-01 12:32:35 +00001227 old_iph = ip_hdr(skb);
1228
stephen hemmingerd3428942012-10-01 12:32:35 +00001229 ttl = vxlan->ttl;
1230 if (!ttl && IN_MULTICAST(ntohl(dst)))
1231 ttl = 1;
1232
1233 tos = vxlan->tos;
1234 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001235 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001236
Pravin B Shelar49560532013-08-19 11:23:17 -07001237 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001238
stephen hemmingerca78f182012-10-09 20:35:48 +00001239 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001240 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001241 fl4.flowi4_tos = RT_TOS(tos);
1242 fl4.daddr = dst;
1243 fl4.saddr = vxlan->saddr;
1244
1245 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001246 if (IS_ERR(rt)) {
1247 netdev_dbg(dev, "no route to %pI4\n", &dst);
1248 dev->stats.tx_carrier_errors++;
1249 goto tx_error;
1250 }
1251
1252 if (rt->dst.dev == dev) {
1253 netdev_dbg(dev, "circular route to %pI4\n", &dst);
stephen hemmingerd3428942012-10-01 12:32:35 +00001254 dev->stats.collisions++;
Pravin B Shelar49560532013-08-19 11:23:17 -07001255 goto rt_tx_error;
stephen hemmingerd3428942012-10-01 12:32:35 +00001256 }
1257
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001258 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001259 if (rt->rt_flags & RTCF_LOCAL &&
1260 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001261 struct vxlan_dev *dst_vxlan;
1262
1263 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001264 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001265 if (!dst_vxlan)
1266 goto tx_error;
1267 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001268 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001269 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001270
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001271 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1272 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1273
Pravin B Shelar49560532013-08-19 11:23:17 -07001274 err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb,
1275 fl4.saddr, dst, tos, ttl, df,
1276 src_port, dst_port, htonl(vni << 8));
1277
1278 if (err < 0)
1279 goto rt_tx_error;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001280 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1281
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001282 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001283
1284drop:
1285 dev->stats.tx_dropped++;
1286 goto tx_free;
1287
Pravin B Shelar49560532013-08-19 11:23:17 -07001288rt_tx_error:
1289 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001290tx_error:
1291 dev->stats.tx_errors++;
1292tx_free:
1293 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001294}
1295
David Stevens66817122013-03-15 04:35:51 +00001296/* Transmit local packets over Vxlan
1297 *
1298 * Outer IP header inherits ECN and DF from inner header.
1299 * Outer UDP destination is the VXLAN assigned port.
1300 * source port is based on hash of flow
1301 */
1302static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1303{
1304 struct vxlan_dev *vxlan = netdev_priv(dev);
1305 struct ethhdr *eth;
1306 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001307 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001308 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001309
1310 skb_reset_mac_header(skb);
1311 eth = eth_hdr(skb);
1312
1313 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1314 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001315
1316 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001317 did_rsc = false;
1318
1319 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1320 ntohs(eth->h_proto) == ETH_P_IP) {
1321 did_rsc = route_shortcircuit(dev, skb);
1322 if (did_rsc)
1323 f = vxlan_find_mac(vxlan, eth->h_dest);
1324 }
1325
David Stevens66817122013-03-15 04:35:51 +00001326 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001327 f = vxlan_find_mac(vxlan, all_zeros_mac);
1328 if (f == NULL) {
1329 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1330 !is_multicast_ether_addr(eth->h_dest))
1331 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001332
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001333 dev->stats.tx_dropped++;
1334 dev_kfree_skb(skb);
1335 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001336 }
David Stevens66817122013-03-15 04:35:51 +00001337 }
1338
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001339 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1340 struct sk_buff *skb1;
1341
1342 skb1 = skb_clone(skb, GFP_ATOMIC);
1343 if (skb1)
1344 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1345 }
1346
1347 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001348 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001349}
1350
stephen hemmingerd3428942012-10-01 12:32:35 +00001351/* Walk the forwarding table and purge stale entries */
1352static void vxlan_cleanup(unsigned long arg)
1353{
1354 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1355 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1356 unsigned int h;
1357
1358 if (!netif_running(vxlan->dev))
1359 return;
1360
1361 spin_lock_bh(&vxlan->hash_lock);
1362 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1363 struct hlist_node *p, *n;
1364 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1365 struct vxlan_fdb *f
1366 = container_of(p, struct vxlan_fdb, hlist);
1367 unsigned long timeout;
1368
stephen hemminger3c172862012-10-26 06:24:34 +00001369 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001370 continue;
1371
1372 timeout = f->used + vxlan->age_interval * HZ;
1373 if (time_before_eq(timeout, jiffies)) {
1374 netdev_dbg(vxlan->dev,
1375 "garbage collect %pM\n",
1376 f->eth_addr);
1377 f->state = NUD_STALE;
1378 vxlan_fdb_destroy(vxlan, f);
1379 } else if (time_before(timeout, next_timer))
1380 next_timer = timeout;
1381 }
1382 }
1383 spin_unlock_bh(&vxlan->hash_lock);
1384
1385 mod_timer(&vxlan->age_timer, next_timer);
1386}
1387
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001388static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1389{
1390 __u32 vni = vxlan->default_dst.remote_vni;
1391
1392 vxlan->vn_sock = vs;
1393 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1394}
1395
stephen hemmingerd3428942012-10-01 12:32:35 +00001396/* Setup stats when device is created */
1397static int vxlan_init(struct net_device *dev)
1398{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001399 struct vxlan_dev *vxlan = netdev_priv(dev);
1400 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1401 struct vxlan_sock *vs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001402
Pravin B Shelare8171042013-03-25 14:49:46 +00001403 dev->tstats = alloc_percpu(struct pcpu_tstats);
1404 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001405 return -ENOMEM;
1406
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001407 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001408 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001409 if (vs) {
1410 /* If we have a socket with same port already, reuse it */
1411 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001412 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001413 } else {
1414 /* otherwise make new socket outside of RTNL */
1415 dev_hold(dev);
1416 queue_work(vxlan_wq, &vxlan->sock_work);
1417 }
1418 spin_unlock(&vn->sock_lock);
1419
stephen hemmingerd3428942012-10-01 12:32:35 +00001420 return 0;
1421}
1422
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001423static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001424{
1425 struct vxlan_fdb *f;
1426
1427 spin_lock_bh(&vxlan->hash_lock);
1428 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1429 if (f)
1430 vxlan_fdb_destroy(vxlan, f);
1431 spin_unlock_bh(&vxlan->hash_lock);
1432}
1433
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001434static void vxlan_uninit(struct net_device *dev)
1435{
1436 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001437 struct vxlan_sock *vs = vxlan->vn_sock;
1438
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001439 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001440
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001441 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07001442 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001443 free_percpu(dev->tstats);
1444}
1445
stephen hemmingerd3428942012-10-01 12:32:35 +00001446/* Start ageing timer and join group when device is brought up */
1447static int vxlan_open(struct net_device *dev)
1448{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001449 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001450 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001451 struct vxlan_sock *vs = vxlan->vn_sock;
1452
1453 /* socket hasn't been created */
1454 if (!vs)
1455 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001456
stephen hemminger3fc2de22013-07-18 08:40:15 -07001457 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
Cong Wang614334d2013-08-07 16:35:45 +08001458 vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001459 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001460 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001461 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00001462 }
1463
1464 if (vxlan->age_interval)
1465 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1466
1467 return 0;
1468}
1469
1470/* Purge the forwarding table */
1471static void vxlan_flush(struct vxlan_dev *vxlan)
1472{
Cong Wang31fec5a2013-05-27 22:35:52 +00001473 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001474
1475 spin_lock_bh(&vxlan->hash_lock);
1476 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1477 struct hlist_node *p, *n;
1478 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1479 struct vxlan_fdb *f
1480 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001481 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1482 if (!is_zero_ether_addr(f->eth_addr))
1483 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001484 }
1485 }
1486 spin_unlock_bh(&vxlan->hash_lock);
1487}
1488
1489/* Cleanup timer and forwarding table on shutdown */
1490static int vxlan_stop(struct net_device *dev)
1491{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001492 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001493 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001494 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001495
stephen hemminger3fc2de22013-07-18 08:40:15 -07001496 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1497 ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001498 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001499 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001500 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001501 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001502
1503 del_timer_sync(&vxlan->age_timer);
1504
1505 vxlan_flush(vxlan);
1506
1507 return 0;
1508}
1509
stephen hemmingerd3428942012-10-01 12:32:35 +00001510/* Stub, nothing needs to be done. */
1511static void vxlan_set_multicast_list(struct net_device *dev)
1512{
1513}
1514
1515static const struct net_device_ops vxlan_netdev_ops = {
1516 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001517 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001518 .ndo_open = vxlan_open,
1519 .ndo_stop = vxlan_stop,
1520 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001521 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001522 .ndo_set_rx_mode = vxlan_set_multicast_list,
1523 .ndo_change_mtu = eth_change_mtu,
1524 .ndo_validate_addr = eth_validate_addr,
1525 .ndo_set_mac_address = eth_mac_addr,
1526 .ndo_fdb_add = vxlan_fdb_add,
1527 .ndo_fdb_del = vxlan_fdb_delete,
1528 .ndo_fdb_dump = vxlan_fdb_dump,
1529};
1530
1531/* Info for udev, that this is a virtual tunnel endpoint */
1532static struct device_type vxlan_type = {
1533 .name = "vxlan",
1534};
1535
stephen hemmingerd3428942012-10-01 12:32:35 +00001536/* Initialize the device structure. */
1537static void vxlan_setup(struct net_device *dev)
1538{
1539 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001540 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001541 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001542
1543 eth_hw_addr_random(dev);
1544 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001545 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001546
1547 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001548 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001549 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1550
1551 dev->tx_queue_len = 0;
1552 dev->features |= NETIF_F_LLTX;
1553 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001554 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001555 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001556 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001557
1558 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001559 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001560 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001561 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001562
stephen hemminger553675f2013-05-16 11:35:20 +00001563 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001564 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001565 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1566 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001567 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001568
1569 init_timer_deferrable(&vxlan->age_timer);
1570 vxlan->age_timer.function = vxlan_cleanup;
1571 vxlan->age_timer.data = (unsigned long) vxlan;
1572
stephen hemminger05f47d62012-10-09 20:35:50 +00001573 inet_get_local_port_range(&low, &high);
1574 vxlan->port_min = low;
1575 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001576 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001577
stephen hemmingerd3428942012-10-01 12:32:35 +00001578 vxlan->dev = dev;
1579
1580 for (h = 0; h < FDB_HASH_SIZE; ++h)
1581 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1582}
1583
1584static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1585 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001586 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001587 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1588 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1589 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1590 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1591 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1592 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1593 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001594 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001595 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1596 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1597 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1598 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001599 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001600};
1601
1602static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1603{
1604 if (tb[IFLA_ADDRESS]) {
1605 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1606 pr_debug("invalid link address (not ethernet)\n");
1607 return -EINVAL;
1608 }
1609
1610 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1611 pr_debug("invalid all zero ethernet address\n");
1612 return -EADDRNOTAVAIL;
1613 }
1614 }
1615
1616 if (!data)
1617 return -EINVAL;
1618
1619 if (data[IFLA_VXLAN_ID]) {
1620 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1621 if (id >= VXLAN_VID_MASK)
1622 return -ERANGE;
1623 }
1624
stephen hemminger05f47d62012-10-09 20:35:50 +00001625 if (data[IFLA_VXLAN_PORT_RANGE]) {
1626 const struct ifla_vxlan_port_range *p
1627 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1628
1629 if (ntohs(p->high) < ntohs(p->low)) {
1630 pr_debug("port range %u .. %u not valid\n",
1631 ntohs(p->low), ntohs(p->high));
1632 return -EINVAL;
1633 }
1634 }
1635
stephen hemmingerd3428942012-10-01 12:32:35 +00001636 return 0;
1637}
1638
Yan Burman1b13c972013-01-29 23:43:07 +00001639static void vxlan_get_drvinfo(struct net_device *netdev,
1640 struct ethtool_drvinfo *drvinfo)
1641{
1642 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1643 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1644}
1645
1646static const struct ethtool_ops vxlan_ethtool_ops = {
1647 .get_drvinfo = vxlan_get_drvinfo,
1648 .get_link = ethtool_op_get_link,
1649};
1650
stephen hemminger553675f2013-05-16 11:35:20 +00001651static void vxlan_del_work(struct work_struct *work)
1652{
1653 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1654
1655 sk_release_kernel(vs->sock->sk);
1656 kfree_rcu(vs, rcu);
1657}
1658
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001659static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Pravin B Shelar012a5722013-08-19 11:23:07 -07001660 vxlan_rcv_t *rcv, void *data)
stephen hemminger553675f2013-05-16 11:35:20 +00001661{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001662 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemminger553675f2013-05-16 11:35:20 +00001663 struct vxlan_sock *vs;
1664 struct sock *sk;
1665 struct sockaddr_in vxlan_addr = {
1666 .sin_family = AF_INET,
1667 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07001668 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00001669 };
1670 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001671 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001672
1673 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001674 if (!vs) {
1675 pr_debug("memory alocation failure\n");
stephen hemminger553675f2013-05-16 11:35:20 +00001676 return ERR_PTR(-ENOMEM);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001677 }
stephen hemminger553675f2013-05-16 11:35:20 +00001678
1679 for (h = 0; h < VNI_HASH_SIZE; ++h)
1680 INIT_HLIST_HEAD(&vs->vni_list[h]);
1681
1682 INIT_WORK(&vs->del_work, vxlan_del_work);
1683
1684 /* Create UDP socket for encapsulation receive. */
1685 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1686 if (rc < 0) {
1687 pr_debug("UDP socket create failed\n");
1688 kfree(vs);
1689 return ERR_PTR(rc);
1690 }
1691
1692 /* Put in proper namespace */
1693 sk = vs->sock->sk;
1694 sk_change_net(sk, net);
1695
stephen hemminger553675f2013-05-16 11:35:20 +00001696 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1697 sizeof(vxlan_addr));
1698 if (rc < 0) {
1699 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1700 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1701 sk_release_kernel(sk);
1702 kfree(vs);
1703 return ERR_PTR(rc);
1704 }
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001705 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001706 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001707 vs->data = data;
stephen hemminger553675f2013-05-16 11:35:20 +00001708
1709 /* Disable multicast loopback */
1710 inet_sk(sk)->mc_loop = 0;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001711 spin_lock(&vn->sock_lock);
1712 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1713 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00001714
1715 /* Mark socket as an encapsulation socket. */
1716 udp_sk(sk)->encap_type = 1;
1717 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1718 udp_encap_enable();
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001719 return vs;
1720}
stephen hemminger553675f2013-05-16 11:35:20 +00001721
Pravin B Shelar012a5722013-08-19 11:23:07 -07001722struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
1723 vxlan_rcv_t *rcv, void *data,
1724 bool no_share)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001725{
1726 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1727 struct vxlan_sock *vs;
1728
Pravin B Shelar012a5722013-08-19 11:23:07 -07001729 vs = vxlan_socket_create(net, port, rcv, data);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001730 if (!IS_ERR(vs))
1731 return vs;
1732
Pravin B Shelar012a5722013-08-19 11:23:07 -07001733 if (no_share) /* Return error if sharing is not allowed. */
1734 return vs;
1735
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001736 spin_lock(&vn->sock_lock);
1737 vs = vxlan_find_sock(net, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001738 if (vs) {
1739 if (vs->rcv == rcv)
1740 atomic_inc(&vs->refcnt);
1741 else
1742 vs = ERR_PTR(-EBUSY);
1743 }
1744 spin_unlock(&vn->sock_lock);
1745
1746 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001747 vs = ERR_PTR(-EINVAL);
1748
stephen hemminger553675f2013-05-16 11:35:20 +00001749 return vs;
1750}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001751EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00001752
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001753/* Scheduled at device creation to bind to a socket */
1754static void vxlan_sock_work(struct work_struct *work)
1755{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001756 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
1757 struct net *net = dev_net(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001758 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001759 __be16 port = vxlan->dst_port;
1760 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001761
Pravin B Shelar012a5722013-08-19 11:23:07 -07001762 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001763 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001764 if (!IS_ERR(nvs))
1765 vxlan_vs_add_dev(nvs, vxlan);
1766 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001767
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001768 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001769}
1770
stephen hemmingerd3428942012-10-01 12:32:35 +00001771static int vxlan_newlink(struct net *net, struct net_device *dev,
1772 struct nlattr *tb[], struct nlattr *data[])
1773{
stephen hemminger553675f2013-05-16 11:35:20 +00001774 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001775 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001776 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001777 __u32 vni;
1778 int err;
1779
1780 if (!data[IFLA_VXLAN_ID])
1781 return -EINVAL;
1782
1783 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001784 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001785
stephen hemminger5d174dd2013-04-27 11:31:55 +00001786 if (data[IFLA_VXLAN_GROUP])
1787 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001788
1789 if (data[IFLA_VXLAN_LOCAL])
1790 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1791
stephen hemminger34e02aa2012-10-09 20:35:53 +00001792 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001793 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001794 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001795 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001796
stephen hemminger34e02aa2012-10-09 20:35:53 +00001797 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001798 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001799 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001800 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001801
1802 if (!tb[IFLA_MTU])
1803 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001804
1805 /* update header length based on lower device */
1806 dev->hard_header_len = lowerdev->hard_header_len +
1807 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001808 }
1809
1810 if (data[IFLA_VXLAN_TOS])
1811 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1812
Vincent Bernatafb97182012-10-30 10:27:16 +00001813 if (data[IFLA_VXLAN_TTL])
1814 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1815
stephen hemmingerd3428942012-10-01 12:32:35 +00001816 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001817 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001818
1819 if (data[IFLA_VXLAN_AGEING])
1820 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1821 else
1822 vxlan->age_interval = FDB_AGE_DEFAULT;
1823
David Stevense4f67ad2012-11-20 02:50:14 +00001824 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1825 vxlan->flags |= VXLAN_F_PROXY;
1826
1827 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1828 vxlan->flags |= VXLAN_F_RSC;
1829
1830 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1831 vxlan->flags |= VXLAN_F_L2MISS;
1832
1833 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1834 vxlan->flags |= VXLAN_F_L3MISS;
1835
stephen hemmingerd3428942012-10-01 12:32:35 +00001836 if (data[IFLA_VXLAN_LIMIT])
1837 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1838
stephen hemminger05f47d62012-10-09 20:35:50 +00001839 if (data[IFLA_VXLAN_PORT_RANGE]) {
1840 const struct ifla_vxlan_port_range *p
1841 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1842 vxlan->port_min = ntohs(p->low);
1843 vxlan->port_max = ntohs(p->high);
1844 }
1845
stephen hemminger823aa872013-04-27 11:31:57 +00001846 if (data[IFLA_VXLAN_PORT])
1847 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1848
stephen hemminger553675f2013-05-16 11:35:20 +00001849 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1850 pr_info("duplicate VNI %u\n", vni);
1851 return -EEXIST;
1852 }
1853
Yan Burman1b13c972013-01-29 23:43:07 +00001854 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1855
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001856 /* create an fdb entry for default destination */
1857 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1858 vxlan->default_dst.remote_ip,
1859 NUD_REACHABLE|NUD_PERMANENT,
1860 NLM_F_EXCL|NLM_F_CREATE,
1861 vxlan->dst_port, vxlan->default_dst.remote_vni,
1862 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001863 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001864 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001865
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001866 err = register_netdevice(dev);
1867 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001868 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001869 return err;
1870 }
1871
stephen hemminger553675f2013-05-16 11:35:20 +00001872 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001873
1874 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001875}
1876
1877static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1878{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001879 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001880 struct vxlan_dev *vxlan = netdev_priv(dev);
1881
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001882 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001883 if (!hlist_unhashed(&vxlan->hlist))
1884 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001885 spin_unlock(&vn->sock_lock);
1886
stephen hemminger553675f2013-05-16 11:35:20 +00001887 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001888 unregister_netdevice_queue(dev, head);
1889}
1890
1891static size_t vxlan_get_size(const struct net_device *dev)
1892{
1893
1894 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001895 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001896 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1897 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1898 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1899 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1900 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001901 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1902 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1903 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1904 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001905 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1906 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001907 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001908 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001909 0;
1910}
1911
1912static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1913{
1914 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001915 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001916 struct ifla_vxlan_port_range ports = {
1917 .low = htons(vxlan->port_min),
1918 .high = htons(vxlan->port_max),
1919 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001920
Atzm Watanabec7995c42013-04-16 02:50:52 +00001921 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001922 goto nla_put_failure;
1923
stephen hemminger5d174dd2013-04-27 11:31:55 +00001924 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001925 goto nla_put_failure;
1926
Atzm Watanabec7995c42013-04-16 02:50:52 +00001927 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001928 goto nla_put_failure;
1929
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001930 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001931 goto nla_put_failure;
1932
1933 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1934 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001935 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1936 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1937 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1938 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1939 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1940 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1941 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1942 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1943 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001944 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001945 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1946 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001947 goto nla_put_failure;
1948
stephen hemminger05f47d62012-10-09 20:35:50 +00001949 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1950 goto nla_put_failure;
1951
stephen hemmingerd3428942012-10-01 12:32:35 +00001952 return 0;
1953
1954nla_put_failure:
1955 return -EMSGSIZE;
1956}
1957
1958static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1959 .kind = "vxlan",
1960 .maxtype = IFLA_VXLAN_MAX,
1961 .policy = vxlan_policy,
1962 .priv_size = sizeof(struct vxlan_dev),
1963 .setup = vxlan_setup,
1964 .validate = vxlan_validate,
1965 .newlink = vxlan_newlink,
1966 .dellink = vxlan_dellink,
1967 .get_size = vxlan_get_size,
1968 .fill_info = vxlan_fill_info,
1969};
1970
1971static __net_init int vxlan_init_net(struct net *net)
1972{
1973 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001974 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001975
stephen hemminger553675f2013-05-16 11:35:20 +00001976 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001977 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001978
stephen hemminger553675f2013-05-16 11:35:20 +00001979 for (h = 0; h < PORT_HASH_SIZE; ++h)
1980 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001981
1982 return 0;
1983}
1984
1985static __net_exit void vxlan_exit_net(struct net *net)
1986{
1987 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001988 struct vxlan_dev *vxlan;
stephen hemminger372675a2013-07-18 08:38:26 -07001989 LIST_HEAD(list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001990
1991 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001992 list_for_each_entry(vxlan, &vn->vxlan_list, next)
stephen hemminger372675a2013-07-18 08:38:26 -07001993 unregister_netdevice_queue(vxlan->dev, &list);
1994 unregister_netdevice_many(&list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001995 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001996}
1997
1998static struct pernet_operations vxlan_net_ops = {
1999 .init = vxlan_init_net,
2000 .exit = vxlan_exit_net,
2001 .id = &vxlan_net_id,
2002 .size = sizeof(struct vxlan_net),
2003};
2004
2005static int __init vxlan_init_module(void)
2006{
2007 int rc;
2008
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002009 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2010 if (!vxlan_wq)
2011 return -ENOMEM;
2012
stephen hemmingerd3428942012-10-01 12:32:35 +00002013 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2014
2015 rc = register_pernet_device(&vxlan_net_ops);
2016 if (rc)
2017 goto out1;
2018
2019 rc = rtnl_link_register(&vxlan_link_ops);
2020 if (rc)
2021 goto out2;
2022
2023 return 0;
2024
2025out2:
2026 unregister_pernet_device(&vxlan_net_ops);
2027out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002028 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002029 return rc;
2030}
Cong Wang7332a132013-05-27 22:35:53 +00002031late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002032
2033static void __exit vxlan_cleanup_module(void)
2034{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002035 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002036 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07002037 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00002038 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00002039}
2040module_exit(vxlan_cleanup_module);
2041
2042MODULE_LICENSE("GPL");
2043MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002044MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00002045MODULE_ALIAS_RTNL_LINK("vxlan");