blob: aab927b92fb36582363f1b4fc553c2f87c4b28bb [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 */
1085static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1086{
stephen hemminger553675f2013-05-16 11:35:20 +00001087 struct vxlan_dev *vxlan = netdev_priv(dev);
1088 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +00001089
1090 skb_orphan(skb);
1091 sock_hold(sk);
1092 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001093 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001094}
1095
stephen hemminger05f47d62012-10-09 20:35:50 +00001096/* Compute source port for outgoing packet
1097 * first choice to use L4 flow hash since it will spread
1098 * better and maybe available from hardware
1099 * secondary choice is to use jhash on the Ethernet header
1100 */
stephen hemminger7d836a72013-04-27 11:31:56 +00001101static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001102{
1103 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1104 u32 hash;
1105
1106 hash = skb_get_rxhash(skb);
1107 if (!hash)
1108 hash = jhash(skb->data, 2 * ETH_ALEN,
1109 (__force u32) skb->protocol);
1110
stephen hemminger7d836a72013-04-27 11:31:56 +00001111 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001112}
1113
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001114static int handle_offloads(struct sk_buff *skb)
1115{
1116 if (skb_is_gso(skb)) {
1117 int err = skb_unclone(skb, GFP_ATOMIC);
1118 if (unlikely(err))
1119 return err;
1120
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001121 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001122 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1123 skb->ip_summed = CHECKSUM_NONE;
1124
1125 return 0;
1126}
1127
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001128/* Bypass encapsulation if the destination is local */
1129static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1130 struct vxlan_dev *dst_vxlan)
1131{
1132 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1133 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1134
1135 skb->pkt_type = PACKET_HOST;
1136 skb->encapsulation = 0;
1137 skb->dev = dst_vxlan->dev;
1138 __skb_pull(skb, skb_network_offset(skb));
1139
1140 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001141 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1142 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001143
1144 u64_stats_update_begin(&tx_stats->syncp);
1145 tx_stats->tx_packets++;
1146 tx_stats->tx_bytes += skb->len;
1147 u64_stats_update_end(&tx_stats->syncp);
1148
1149 if (netif_rx(skb) == NET_RX_SUCCESS) {
1150 u64_stats_update_begin(&rx_stats->syncp);
1151 rx_stats->rx_packets++;
1152 rx_stats->rx_bytes += skb->len;
1153 u64_stats_update_end(&rx_stats->syncp);
1154 } else {
1155 skb->dev->stats.rx_dropped++;
1156 }
1157}
1158
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001159static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1160 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001161{
1162 struct vxlan_dev *vxlan = netdev_priv(dev);
1163 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001164 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001165 struct vxlanhdr *vxh;
1166 struct udphdr *uh;
1167 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001168 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001169 __be16 src_port, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001170 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001171 __be16 df = 0;
1172 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001173 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001174
stephen hemminger823aa872013-04-27 11:31:57 +00001175 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001176 vni = rdst->remote_vni;
1177 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001178
1179 if (!dst) {
1180 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001181 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001182 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001183 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001184 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001185 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001186 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001187
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001188 if (!skb->encapsulation) {
1189 skb_reset_inner_headers(skb);
1190 skb->encapsulation = 1;
1191 }
1192
stephen hemmingerd3428942012-10-01 12:32:35 +00001193 /* Need space for new headers (invalidates iph ptr) */
1194 if (skb_cow_head(skb, VXLAN_HEADROOM))
1195 goto drop;
1196
stephen hemmingerd3428942012-10-01 12:32:35 +00001197 old_iph = ip_hdr(skb);
1198
stephen hemmingerd3428942012-10-01 12:32:35 +00001199 ttl = vxlan->ttl;
1200 if (!ttl && IN_MULTICAST(ntohl(dst)))
1201 ttl = 1;
1202
1203 tos = vxlan->tos;
1204 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001205 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001206
stephen hemminger05f47d62012-10-09 20:35:50 +00001207 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001208
stephen hemmingerca78f182012-10-09 20:35:48 +00001209 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001210 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001211 fl4.flowi4_tos = RT_TOS(tos);
1212 fl4.daddr = dst;
1213 fl4.saddr = vxlan->saddr;
1214
1215 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001216 if (IS_ERR(rt)) {
1217 netdev_dbg(dev, "no route to %pI4\n", &dst);
1218 dev->stats.tx_carrier_errors++;
1219 goto tx_error;
1220 }
1221
1222 if (rt->dst.dev == dev) {
1223 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1224 ip_rt_put(rt);
1225 dev->stats.collisions++;
1226 goto tx_error;
1227 }
1228
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001229 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001230 if (rt->rt_flags & RTCF_LOCAL &&
1231 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001232 struct vxlan_dev *dst_vxlan;
1233
1234 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001235 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001236 if (!dst_vxlan)
1237 goto tx_error;
1238 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001239 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001240 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001241 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1242 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001243 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001244
1245 __skb_push(skb, sizeof(*uh));
1246 skb_reset_transport_header(skb);
1247 uh = udp_hdr(skb);
1248
stephen hemminger73cf3312013-04-27 11:31:54 +00001249 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001250 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001251
1252 uh->len = htons(skb->len);
1253 uh->check = 0;
1254
stephen hemminger1cad8712012-10-09 20:35:49 +00001255 vxlan_set_owner(dev, skb);
1256
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001257 if (handle_offloads(skb))
1258 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001259
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001260 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1261 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1262
1263 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1264 IPPROTO_UDP, tos, ttl, df);
1265 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1266
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001267 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001268
1269drop:
1270 dev->stats.tx_dropped++;
1271 goto tx_free;
1272
1273tx_error:
1274 dev->stats.tx_errors++;
1275tx_free:
1276 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001277}
1278
David Stevens66817122013-03-15 04:35:51 +00001279/* Transmit local packets over Vxlan
1280 *
1281 * Outer IP header inherits ECN and DF from inner header.
1282 * Outer UDP destination is the VXLAN assigned port.
1283 * source port is based on hash of flow
1284 */
1285static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1286{
1287 struct vxlan_dev *vxlan = netdev_priv(dev);
1288 struct ethhdr *eth;
1289 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001290 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001291 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001292
1293 skb_reset_mac_header(skb);
1294 eth = eth_hdr(skb);
1295
1296 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1297 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001298
1299 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001300 did_rsc = false;
1301
1302 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1303 ntohs(eth->h_proto) == ETH_P_IP) {
1304 did_rsc = route_shortcircuit(dev, skb);
1305 if (did_rsc)
1306 f = vxlan_find_mac(vxlan, eth->h_dest);
1307 }
1308
David Stevens66817122013-03-15 04:35:51 +00001309 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001310 f = vxlan_find_mac(vxlan, all_zeros_mac);
1311 if (f == NULL) {
1312 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1313 !is_multicast_ether_addr(eth->h_dest))
1314 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001315
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001316 dev->stats.tx_dropped++;
1317 dev_kfree_skb(skb);
1318 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001319 }
David Stevens66817122013-03-15 04:35:51 +00001320 }
1321
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001322 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1323 struct sk_buff *skb1;
1324
1325 skb1 = skb_clone(skb, GFP_ATOMIC);
1326 if (skb1)
1327 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1328 }
1329
1330 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001331 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001332}
1333
stephen hemmingerd3428942012-10-01 12:32:35 +00001334/* Walk the forwarding table and purge stale entries */
1335static void vxlan_cleanup(unsigned long arg)
1336{
1337 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1338 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1339 unsigned int h;
1340
1341 if (!netif_running(vxlan->dev))
1342 return;
1343
1344 spin_lock_bh(&vxlan->hash_lock);
1345 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1346 struct hlist_node *p, *n;
1347 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1348 struct vxlan_fdb *f
1349 = container_of(p, struct vxlan_fdb, hlist);
1350 unsigned long timeout;
1351
stephen hemminger3c172862012-10-26 06:24:34 +00001352 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001353 continue;
1354
1355 timeout = f->used + vxlan->age_interval * HZ;
1356 if (time_before_eq(timeout, jiffies)) {
1357 netdev_dbg(vxlan->dev,
1358 "garbage collect %pM\n",
1359 f->eth_addr);
1360 f->state = NUD_STALE;
1361 vxlan_fdb_destroy(vxlan, f);
1362 } else if (time_before(timeout, next_timer))
1363 next_timer = timeout;
1364 }
1365 }
1366 spin_unlock_bh(&vxlan->hash_lock);
1367
1368 mod_timer(&vxlan->age_timer, next_timer);
1369}
1370
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001371static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1372{
1373 __u32 vni = vxlan->default_dst.remote_vni;
1374
1375 vxlan->vn_sock = vs;
1376 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1377}
1378
stephen hemmingerd3428942012-10-01 12:32:35 +00001379/* Setup stats when device is created */
1380static int vxlan_init(struct net_device *dev)
1381{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001382 struct vxlan_dev *vxlan = netdev_priv(dev);
1383 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1384 struct vxlan_sock *vs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001385
Pravin B Shelare8171042013-03-25 14:49:46 +00001386 dev->tstats = alloc_percpu(struct pcpu_tstats);
1387 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001388 return -ENOMEM;
1389
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001390 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001391 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001392 if (vs) {
1393 /* If we have a socket with same port already, reuse it */
1394 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001395 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001396 } else {
1397 /* otherwise make new socket outside of RTNL */
1398 dev_hold(dev);
1399 queue_work(vxlan_wq, &vxlan->sock_work);
1400 }
1401 spin_unlock(&vn->sock_lock);
1402
stephen hemmingerd3428942012-10-01 12:32:35 +00001403 return 0;
1404}
1405
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001406static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001407{
1408 struct vxlan_fdb *f;
1409
1410 spin_lock_bh(&vxlan->hash_lock);
1411 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1412 if (f)
1413 vxlan_fdb_destroy(vxlan, f);
1414 spin_unlock_bh(&vxlan->hash_lock);
1415}
1416
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001417static void vxlan_uninit(struct net_device *dev)
1418{
1419 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001420 struct vxlan_sock *vs = vxlan->vn_sock;
1421
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001422 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001423
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001424 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07001425 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001426 free_percpu(dev->tstats);
1427}
1428
stephen hemmingerd3428942012-10-01 12:32:35 +00001429/* Start ageing timer and join group when device is brought up */
1430static int vxlan_open(struct net_device *dev)
1431{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001432 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001433 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001434 struct vxlan_sock *vs = vxlan->vn_sock;
1435
1436 /* socket hasn't been created */
1437 if (!vs)
1438 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001439
stephen hemminger3fc2de22013-07-18 08:40:15 -07001440 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
Cong Wang614334d2013-08-07 16:35:45 +08001441 vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001442 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001443 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001444 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00001445 }
1446
1447 if (vxlan->age_interval)
1448 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1449
1450 return 0;
1451}
1452
1453/* Purge the forwarding table */
1454static void vxlan_flush(struct vxlan_dev *vxlan)
1455{
Cong Wang31fec5a2013-05-27 22:35:52 +00001456 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001457
1458 spin_lock_bh(&vxlan->hash_lock);
1459 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1460 struct hlist_node *p, *n;
1461 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1462 struct vxlan_fdb *f
1463 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001464 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1465 if (!is_zero_ether_addr(f->eth_addr))
1466 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001467 }
1468 }
1469 spin_unlock_bh(&vxlan->hash_lock);
1470}
1471
1472/* Cleanup timer and forwarding table on shutdown */
1473static int vxlan_stop(struct net_device *dev)
1474{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001475 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001476 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001477 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001478
stephen hemminger3fc2de22013-07-18 08:40:15 -07001479 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1480 ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001481 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001482 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001483 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001484 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001485
1486 del_timer_sync(&vxlan->age_timer);
1487
1488 vxlan_flush(vxlan);
1489
1490 return 0;
1491}
1492
stephen hemmingerd3428942012-10-01 12:32:35 +00001493/* Stub, nothing needs to be done. */
1494static void vxlan_set_multicast_list(struct net_device *dev)
1495{
1496}
1497
1498static const struct net_device_ops vxlan_netdev_ops = {
1499 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001500 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001501 .ndo_open = vxlan_open,
1502 .ndo_stop = vxlan_stop,
1503 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001504 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001505 .ndo_set_rx_mode = vxlan_set_multicast_list,
1506 .ndo_change_mtu = eth_change_mtu,
1507 .ndo_validate_addr = eth_validate_addr,
1508 .ndo_set_mac_address = eth_mac_addr,
1509 .ndo_fdb_add = vxlan_fdb_add,
1510 .ndo_fdb_del = vxlan_fdb_delete,
1511 .ndo_fdb_dump = vxlan_fdb_dump,
1512};
1513
1514/* Info for udev, that this is a virtual tunnel endpoint */
1515static struct device_type vxlan_type = {
1516 .name = "vxlan",
1517};
1518
stephen hemmingerd3428942012-10-01 12:32:35 +00001519/* Initialize the device structure. */
1520static void vxlan_setup(struct net_device *dev)
1521{
1522 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001523 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001524 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001525
1526 eth_hw_addr_random(dev);
1527 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001528 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001529
1530 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001531 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001532 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1533
1534 dev->tx_queue_len = 0;
1535 dev->features |= NETIF_F_LLTX;
1536 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001537 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001538 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001539 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001540
1541 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001542 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001543 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001544 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001545
stephen hemminger553675f2013-05-16 11:35:20 +00001546 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001547 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001548 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1549 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001550 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001551
1552 init_timer_deferrable(&vxlan->age_timer);
1553 vxlan->age_timer.function = vxlan_cleanup;
1554 vxlan->age_timer.data = (unsigned long) vxlan;
1555
stephen hemminger05f47d62012-10-09 20:35:50 +00001556 inet_get_local_port_range(&low, &high);
1557 vxlan->port_min = low;
1558 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001559 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001560
stephen hemmingerd3428942012-10-01 12:32:35 +00001561 vxlan->dev = dev;
1562
1563 for (h = 0; h < FDB_HASH_SIZE; ++h)
1564 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1565}
1566
1567static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1568 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001569 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001570 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1571 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1572 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1573 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1574 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1575 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1576 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001577 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001578 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1579 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1580 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1581 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001582 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001583};
1584
1585static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1586{
1587 if (tb[IFLA_ADDRESS]) {
1588 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1589 pr_debug("invalid link address (not ethernet)\n");
1590 return -EINVAL;
1591 }
1592
1593 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1594 pr_debug("invalid all zero ethernet address\n");
1595 return -EADDRNOTAVAIL;
1596 }
1597 }
1598
1599 if (!data)
1600 return -EINVAL;
1601
1602 if (data[IFLA_VXLAN_ID]) {
1603 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1604 if (id >= VXLAN_VID_MASK)
1605 return -ERANGE;
1606 }
1607
stephen hemminger05f47d62012-10-09 20:35:50 +00001608 if (data[IFLA_VXLAN_PORT_RANGE]) {
1609 const struct ifla_vxlan_port_range *p
1610 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1611
1612 if (ntohs(p->high) < ntohs(p->low)) {
1613 pr_debug("port range %u .. %u not valid\n",
1614 ntohs(p->low), ntohs(p->high));
1615 return -EINVAL;
1616 }
1617 }
1618
stephen hemmingerd3428942012-10-01 12:32:35 +00001619 return 0;
1620}
1621
Yan Burman1b13c972013-01-29 23:43:07 +00001622static void vxlan_get_drvinfo(struct net_device *netdev,
1623 struct ethtool_drvinfo *drvinfo)
1624{
1625 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1626 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1627}
1628
1629static const struct ethtool_ops vxlan_ethtool_ops = {
1630 .get_drvinfo = vxlan_get_drvinfo,
1631 .get_link = ethtool_op_get_link,
1632};
1633
stephen hemminger553675f2013-05-16 11:35:20 +00001634static void vxlan_del_work(struct work_struct *work)
1635{
1636 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1637
1638 sk_release_kernel(vs->sock->sk);
1639 kfree_rcu(vs, rcu);
1640}
1641
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001642static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Pravin B Shelar012a5722013-08-19 11:23:07 -07001643 vxlan_rcv_t *rcv, void *data)
stephen hemminger553675f2013-05-16 11:35:20 +00001644{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001645 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemminger553675f2013-05-16 11:35:20 +00001646 struct vxlan_sock *vs;
1647 struct sock *sk;
1648 struct sockaddr_in vxlan_addr = {
1649 .sin_family = AF_INET,
1650 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07001651 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00001652 };
1653 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001654 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001655
1656 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001657 if (!vs) {
1658 pr_debug("memory alocation failure\n");
stephen hemminger553675f2013-05-16 11:35:20 +00001659 return ERR_PTR(-ENOMEM);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001660 }
stephen hemminger553675f2013-05-16 11:35:20 +00001661
1662 for (h = 0; h < VNI_HASH_SIZE; ++h)
1663 INIT_HLIST_HEAD(&vs->vni_list[h]);
1664
1665 INIT_WORK(&vs->del_work, vxlan_del_work);
1666
1667 /* Create UDP socket for encapsulation receive. */
1668 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1669 if (rc < 0) {
1670 pr_debug("UDP socket create failed\n");
1671 kfree(vs);
1672 return ERR_PTR(rc);
1673 }
1674
1675 /* Put in proper namespace */
1676 sk = vs->sock->sk;
1677 sk_change_net(sk, net);
1678
stephen hemminger553675f2013-05-16 11:35:20 +00001679 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1680 sizeof(vxlan_addr));
1681 if (rc < 0) {
1682 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1683 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1684 sk_release_kernel(sk);
1685 kfree(vs);
1686 return ERR_PTR(rc);
1687 }
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001688 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001689 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001690 vs->data = data;
stephen hemminger553675f2013-05-16 11:35:20 +00001691
1692 /* Disable multicast loopback */
1693 inet_sk(sk)->mc_loop = 0;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001694 spin_lock(&vn->sock_lock);
1695 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1696 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00001697
1698 /* Mark socket as an encapsulation socket. */
1699 udp_sk(sk)->encap_type = 1;
1700 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1701 udp_encap_enable();
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001702 return vs;
1703}
stephen hemminger553675f2013-05-16 11:35:20 +00001704
Pravin B Shelar012a5722013-08-19 11:23:07 -07001705struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
1706 vxlan_rcv_t *rcv, void *data,
1707 bool no_share)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001708{
1709 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1710 struct vxlan_sock *vs;
1711
Pravin B Shelar012a5722013-08-19 11:23:07 -07001712 vs = vxlan_socket_create(net, port, rcv, data);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001713 if (!IS_ERR(vs))
1714 return vs;
1715
Pravin B Shelar012a5722013-08-19 11:23:07 -07001716 if (no_share) /* Return error if sharing is not allowed. */
1717 return vs;
1718
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001719 spin_lock(&vn->sock_lock);
1720 vs = vxlan_find_sock(net, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001721 if (vs) {
1722 if (vs->rcv == rcv)
1723 atomic_inc(&vs->refcnt);
1724 else
1725 vs = ERR_PTR(-EBUSY);
1726 }
1727 spin_unlock(&vn->sock_lock);
1728
1729 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001730 vs = ERR_PTR(-EINVAL);
1731
stephen hemminger553675f2013-05-16 11:35:20 +00001732 return vs;
1733}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001734EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00001735
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001736/* Scheduled at device creation to bind to a socket */
1737static void vxlan_sock_work(struct work_struct *work)
1738{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001739 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
1740 struct net *net = dev_net(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001741 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001742 __be16 port = vxlan->dst_port;
1743 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001744
Pravin B Shelar012a5722013-08-19 11:23:07 -07001745 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001746 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001747 if (!IS_ERR(nvs))
1748 vxlan_vs_add_dev(nvs, vxlan);
1749 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001750
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001751 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001752}
1753
stephen hemmingerd3428942012-10-01 12:32:35 +00001754static int vxlan_newlink(struct net *net, struct net_device *dev,
1755 struct nlattr *tb[], struct nlattr *data[])
1756{
stephen hemminger553675f2013-05-16 11:35:20 +00001757 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001758 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001759 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001760 __u32 vni;
1761 int err;
1762
1763 if (!data[IFLA_VXLAN_ID])
1764 return -EINVAL;
1765
1766 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001767 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001768
stephen hemminger5d174dd2013-04-27 11:31:55 +00001769 if (data[IFLA_VXLAN_GROUP])
1770 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001771
1772 if (data[IFLA_VXLAN_LOCAL])
1773 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1774
stephen hemminger34e02aa2012-10-09 20:35:53 +00001775 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001776 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001777 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001778 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001779
stephen hemminger34e02aa2012-10-09 20:35:53 +00001780 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001781 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001782 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001783 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001784
1785 if (!tb[IFLA_MTU])
1786 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001787
1788 /* update header length based on lower device */
1789 dev->hard_header_len = lowerdev->hard_header_len +
1790 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001791 }
1792
1793 if (data[IFLA_VXLAN_TOS])
1794 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1795
Vincent Bernatafb97182012-10-30 10:27:16 +00001796 if (data[IFLA_VXLAN_TTL])
1797 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1798
stephen hemmingerd3428942012-10-01 12:32:35 +00001799 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001800 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001801
1802 if (data[IFLA_VXLAN_AGEING])
1803 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1804 else
1805 vxlan->age_interval = FDB_AGE_DEFAULT;
1806
David Stevense4f67ad2012-11-20 02:50:14 +00001807 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1808 vxlan->flags |= VXLAN_F_PROXY;
1809
1810 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1811 vxlan->flags |= VXLAN_F_RSC;
1812
1813 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1814 vxlan->flags |= VXLAN_F_L2MISS;
1815
1816 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1817 vxlan->flags |= VXLAN_F_L3MISS;
1818
stephen hemmingerd3428942012-10-01 12:32:35 +00001819 if (data[IFLA_VXLAN_LIMIT])
1820 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1821
stephen hemminger05f47d62012-10-09 20:35:50 +00001822 if (data[IFLA_VXLAN_PORT_RANGE]) {
1823 const struct ifla_vxlan_port_range *p
1824 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1825 vxlan->port_min = ntohs(p->low);
1826 vxlan->port_max = ntohs(p->high);
1827 }
1828
stephen hemminger823aa872013-04-27 11:31:57 +00001829 if (data[IFLA_VXLAN_PORT])
1830 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1831
stephen hemminger553675f2013-05-16 11:35:20 +00001832 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1833 pr_info("duplicate VNI %u\n", vni);
1834 return -EEXIST;
1835 }
1836
Yan Burman1b13c972013-01-29 23:43:07 +00001837 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1838
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001839 /* create an fdb entry for default destination */
1840 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1841 vxlan->default_dst.remote_ip,
1842 NUD_REACHABLE|NUD_PERMANENT,
1843 NLM_F_EXCL|NLM_F_CREATE,
1844 vxlan->dst_port, vxlan->default_dst.remote_vni,
1845 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001846 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001847 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001848
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001849 err = register_netdevice(dev);
1850 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001851 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001852 return err;
1853 }
1854
stephen hemminger553675f2013-05-16 11:35:20 +00001855 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001856
1857 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001858}
1859
1860static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1861{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001862 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001863 struct vxlan_dev *vxlan = netdev_priv(dev);
1864
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001865 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001866 if (!hlist_unhashed(&vxlan->hlist))
1867 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001868 spin_unlock(&vn->sock_lock);
1869
stephen hemminger553675f2013-05-16 11:35:20 +00001870 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001871 unregister_netdevice_queue(dev, head);
1872}
1873
1874static size_t vxlan_get_size(const struct net_device *dev)
1875{
1876
1877 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001878 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001879 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1880 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1881 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1882 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1883 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001884 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1885 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1886 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1887 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001888 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1889 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001890 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001891 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001892 0;
1893}
1894
1895static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1896{
1897 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001898 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001899 struct ifla_vxlan_port_range ports = {
1900 .low = htons(vxlan->port_min),
1901 .high = htons(vxlan->port_max),
1902 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001903
Atzm Watanabec7995c42013-04-16 02:50:52 +00001904 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001905 goto nla_put_failure;
1906
stephen hemminger5d174dd2013-04-27 11:31:55 +00001907 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001908 goto nla_put_failure;
1909
Atzm Watanabec7995c42013-04-16 02:50:52 +00001910 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001911 goto nla_put_failure;
1912
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001913 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001914 goto nla_put_failure;
1915
1916 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1917 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001918 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1919 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1920 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1921 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1922 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1923 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1924 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1925 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1926 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001927 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001928 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1929 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001930 goto nla_put_failure;
1931
stephen hemminger05f47d62012-10-09 20:35:50 +00001932 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1933 goto nla_put_failure;
1934
stephen hemmingerd3428942012-10-01 12:32:35 +00001935 return 0;
1936
1937nla_put_failure:
1938 return -EMSGSIZE;
1939}
1940
1941static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1942 .kind = "vxlan",
1943 .maxtype = IFLA_VXLAN_MAX,
1944 .policy = vxlan_policy,
1945 .priv_size = sizeof(struct vxlan_dev),
1946 .setup = vxlan_setup,
1947 .validate = vxlan_validate,
1948 .newlink = vxlan_newlink,
1949 .dellink = vxlan_dellink,
1950 .get_size = vxlan_get_size,
1951 .fill_info = vxlan_fill_info,
1952};
1953
1954static __net_init int vxlan_init_net(struct net *net)
1955{
1956 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001957 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001958
stephen hemminger553675f2013-05-16 11:35:20 +00001959 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001960 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001961
stephen hemminger553675f2013-05-16 11:35:20 +00001962 for (h = 0; h < PORT_HASH_SIZE; ++h)
1963 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001964
1965 return 0;
1966}
1967
1968static __net_exit void vxlan_exit_net(struct net *net)
1969{
1970 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001971 struct vxlan_dev *vxlan;
stephen hemminger372675a2013-07-18 08:38:26 -07001972 LIST_HEAD(list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001973
1974 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001975 list_for_each_entry(vxlan, &vn->vxlan_list, next)
stephen hemminger372675a2013-07-18 08:38:26 -07001976 unregister_netdevice_queue(vxlan->dev, &list);
1977 unregister_netdevice_many(&list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001978 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001979}
1980
1981static struct pernet_operations vxlan_net_ops = {
1982 .init = vxlan_init_net,
1983 .exit = vxlan_exit_net,
1984 .id = &vxlan_net_id,
1985 .size = sizeof(struct vxlan_net),
1986};
1987
1988static int __init vxlan_init_module(void)
1989{
1990 int rc;
1991
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001992 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1993 if (!vxlan_wq)
1994 return -ENOMEM;
1995
stephen hemmingerd3428942012-10-01 12:32:35 +00001996 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1997
1998 rc = register_pernet_device(&vxlan_net_ops);
1999 if (rc)
2000 goto out1;
2001
2002 rc = rtnl_link_register(&vxlan_link_ops);
2003 if (rc)
2004 goto out2;
2005
2006 return 0;
2007
2008out2:
2009 unregister_pernet_device(&vxlan_net_ops);
2010out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002011 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002012 return rc;
2013}
Cong Wang7332a132013-05-27 22:35:53 +00002014late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002015
2016static void __exit vxlan_cleanup_module(void)
2017{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002018 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002019 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07002020 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00002021 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00002022}
2023module_exit(vxlan_cleanup_module);
2024
2025MODULE_LICENSE("GPL");
2026MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002027MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00002028MODULE_ALIAS_RTNL_LINK("vxlan");