blob: 1afb9795105fdd279dc82c3f5d8924eeee0f98ef [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * TODO
stephen hemmingerd3428942012-10-01 12:32:35 +000011 * - IPv6 (not in RFC)
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/slab.h>
21#include <linux/skbuff.h>
22#include <linux/rculist.h>
23#include <linux/netdevice.h>
24#include <linux/in.h>
25#include <linux/ip.h>
26#include <linux/udp.h>
27#include <linux/igmp.h>
28#include <linux/etherdevice.h>
29#include <linux/if_ether.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000030#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000031#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000032#include <net/arp.h>
33#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000035#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000036#include <net/icmp.h>
37#include <net/udp.h>
38#include <net/rtnetlink.h>
39#include <net/route.h>
40#include <net/dsfield.h>
41#include <net/inet_ecn.h>
42#include <net/net_namespace.h>
43#include <net/netns/generic.h>
44
45#define VXLAN_VERSION "0.1"
46
stephen hemminger553675f2013-05-16 11:35:20 +000047#define PORT_HASH_BITS 8
48#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000049#define VNI_HASH_BITS 10
50#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
51#define FDB_HASH_BITS 8
52#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
53#define FDB_AGE_DEFAULT 300 /* 5 min */
54#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
55
56#define VXLAN_N_VID (1u << 24)
57#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000058/* IP header + UDP + VXLAN + Ethernet header */
59#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
Pravin B Shelar7ce04752013-08-19 11:22:54 -070060#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
stephen hemmingerd3428942012-10-01 12:32:35 +000061
62#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
63
64/* VXLAN protocol header */
65struct vxlanhdr {
66 __be32 vx_flags;
67 __be32 vx_vni;
68};
69
stephen hemminger23c578b2013-04-27 11:31:53 +000070/* UDP port for VXLAN traffic.
71 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070072 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000073 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070074static unsigned short vxlan_port __read_mostly = 8472;
75module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000076MODULE_PARM_DESC(udp_port, "Destination UDP port");
77
78static bool log_ecn_error = true;
79module_param(log_ecn_error, bool, 0644);
80MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
81
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070082static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000083
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030084static const u8 all_zeros_mac[ETH_ALEN];
85
stephen hemminger553675f2013-05-16 11:35:20 +000086/* per UDP socket information */
87struct vxlan_sock {
88 struct hlist_node hlist;
89 struct rcu_head rcu;
90 struct work_struct del_work;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -070091 atomic_t refcnt;
stephen hemminger553675f2013-05-16 11:35:20 +000092 struct socket *sock;
stephen hemmingerd3428942012-10-01 12:32:35 +000093 struct hlist_head vni_list[VNI_HASH_SIZE];
94};
95
stephen hemminger553675f2013-05-16 11:35:20 +000096/* per-network namespace private data for this module */
97struct vxlan_net {
98 struct list_head vxlan_list;
99 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700100 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +0000101};
102
David Stevens66817122013-03-15 04:35:51 +0000103struct vxlan_rdst {
David Stevens66817122013-03-15 04:35:51 +0000104 __be32 remote_ip;
105 __be16 remote_port;
106 u32 remote_vni;
107 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700108 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300109 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000110};
111
stephen hemmingerd3428942012-10-01 12:32:35 +0000112/* Forwarding table entry */
113struct vxlan_fdb {
114 struct hlist_node hlist; /* linked list of entries */
115 struct rcu_head rcu;
116 unsigned long updated; /* jiffies */
117 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700118 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000119 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000120 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000121 u8 eth_addr[ETH_ALEN];
122};
123
stephen hemmingerd3428942012-10-01 12:32:35 +0000124/* Pseudo network device */
125struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000126 struct hlist_node hlist; /* vni hash table */
127 struct list_head next; /* vxlan's per namespace list */
128 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000129 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000130 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000131 __be32 saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000132 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000133 __u16 port_min; /* source port range */
134 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000135 __u8 tos; /* TOS override */
136 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000137 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000138
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700139 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700140 struct work_struct igmp_join;
141 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700142
stephen hemmingerd3428942012-10-01 12:32:35 +0000143 unsigned long age_interval;
144 struct timer_list age_timer;
145 spinlock_t hash_lock;
146 unsigned int addrcnt;
147 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000148
149 struct hlist_head fdb_head[FDB_HASH_SIZE];
150};
151
David Stevense4f67ad2012-11-20 02:50:14 +0000152#define VXLAN_F_LEARN 0x01
153#define VXLAN_F_PROXY 0x02
154#define VXLAN_F_RSC 0x04
155#define VXLAN_F_L2MISS 0x08
156#define VXLAN_F_L3MISS 0x10
157
stephen hemmingerd3428942012-10-01 12:32:35 +0000158/* salt for hash table */
159static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700160static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000161
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700162static void vxlan_sock_work(struct work_struct *work);
163
stephen hemminger553675f2013-05-16 11:35:20 +0000164/* Virtual Network hash table head */
165static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
166{
167 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
168}
169
170/* Socket hash table head */
171static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000172{
173 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
174
stephen hemminger553675f2013-05-16 11:35:20 +0000175 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
176}
177
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700178/* First remote destination for a forwarding entry.
179 * Guaranteed to be non-NULL because remotes are never deleted.
180 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700181static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700182{
stephen hemminger5ca54612013-08-04 17:17:39 -0700183 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
184}
185
186static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
187{
188 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700189}
190
stephen hemminger553675f2013-05-16 11:35:20 +0000191/* Find VXLAN socket based on network namespace and UDP port */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -0700192static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000193{
194 struct vxlan_sock *vs;
195
196 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
197 if (inet_sk(vs->sock->sk)->inet_sport == port)
198 return vs;
199 }
200 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000201}
202
203/* Look up VNI in a per net namespace table */
stephen hemminger553675f2013-05-16 11:35:20 +0000204static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000205{
stephen hemminger553675f2013-05-16 11:35:20 +0000206 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000207 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000208
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -0700209 vs = vxlan_find_sock(net, port);
stephen hemminger553675f2013-05-16 11:35:20 +0000210 if (!vs)
211 return NULL;
212
213 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000214 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000215 return vxlan;
216 }
217
218 return NULL;
219}
220
221/* Fill in neighbour message in skbuff. */
222static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700223 const struct vxlan_fdb *fdb,
224 u32 portid, u32 seq, int type, unsigned int flags,
225 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000226{
227 unsigned long now = jiffies;
228 struct nda_cacheinfo ci;
229 struct nlmsghdr *nlh;
230 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000231 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000232
233 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
234 if (nlh == NULL)
235 return -EMSGSIZE;
236
237 ndm = nlmsg_data(nlh);
238 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000239
240 send_eth = send_ip = true;
241
242 if (type == RTM_GETNEIGH) {
243 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000244 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000245 send_eth = !is_zero_ether_addr(fdb->eth_addr);
246 } else
247 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000248 ndm->ndm_state = fdb->state;
249 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000250 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000251 ndm->ndm_type = NDA_DST;
252
David Stevense4f67ad2012-11-20 02:50:14 +0000253 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000254 goto nla_put_failure;
255
David Stevens66817122013-03-15 04:35:51 +0000256 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
257 goto nla_put_failure;
258
stephen hemminger823aa872013-04-27 11:31:57 +0000259 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000260 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
261 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000262 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700263 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000264 goto nla_put_failure;
265 if (rdst->remote_ifindex &&
266 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000267 goto nla_put_failure;
268
269 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
270 ci.ndm_confirmed = 0;
271 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
272 ci.ndm_refcnt = 0;
273
274 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
275 goto nla_put_failure;
276
277 return nlmsg_end(skb, nlh);
278
279nla_put_failure:
280 nlmsg_cancel(skb, nlh);
281 return -EMSGSIZE;
282}
283
284static inline size_t vxlan_nlmsg_size(void)
285{
286 return NLMSG_ALIGN(sizeof(struct ndmsg))
287 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
288 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000289 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000290 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
291 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000292 + nla_total_size(sizeof(struct nda_cacheinfo));
293}
294
295static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700296 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000297{
298 struct net *net = dev_net(vxlan->dev);
299 struct sk_buff *skb;
300 int err = -ENOBUFS;
301
302 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
303 if (skb == NULL)
304 goto errout;
305
stephen hemminger5ca54612013-08-04 17:17:39 -0700306 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
307 first_remote_rtnl(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000308 if (err < 0) {
309 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
310 WARN_ON(err == -EMSGSIZE);
311 kfree_skb(skb);
312 goto errout;
313 }
314
315 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
316 return;
317errout:
318 if (err < 0)
319 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
320}
321
David Stevense4f67ad2012-11-20 02:50:14 +0000322static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
323{
324 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700325 struct vxlan_fdb f = {
326 .state = NUD_STALE,
327 };
328 struct vxlan_rdst remote = {
329 .remote_ip = ipa, /* goes to NDA_DST */
330 .remote_vni = VXLAN_N_VID,
331 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700332
333 INIT_LIST_HEAD(&f.remotes);
334 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000335
336 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
337}
338
339static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
340{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700341 struct vxlan_fdb f = {
342 .state = NUD_STALE,
343 };
David Stevense4f67ad2012-11-20 02:50:14 +0000344
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700345 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000346 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
347
348 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
349}
350
stephen hemmingerd3428942012-10-01 12:32:35 +0000351/* Hash Ethernet address */
352static u32 eth_hash(const unsigned char *addr)
353{
354 u64 value = get_unaligned((u64 *)addr);
355
356 /* only want 6 bytes */
357#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000358 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000359#else
360 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000361#endif
362 return hash_64(value, FDB_HASH_BITS);
363}
364
365/* Hash chain to use given mac address */
366static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
367 const u8 *mac)
368{
369 return &vxlan->fdb_head[eth_hash(mac)];
370}
371
372/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000373static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000374 const u8 *mac)
375
376{
377 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
378 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000379
Sasha Levinb67bfe02013-02-27 17:06:00 -0800380 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000381 if (compare_ether_addr(mac, f->eth_addr) == 0)
382 return f;
383 }
384
385 return NULL;
386}
387
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000388static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
389 const u8 *mac)
390{
391 struct vxlan_fdb *f;
392
393 f = __vxlan_find_mac(vxlan, mac);
394 if (f)
395 f->used = jiffies;
396
397 return f;
398}
399
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300400/* caller should hold vxlan->hash_lock */
401static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
402 __be32 ip, __be16 port,
403 __u32 vni, __u32 ifindex)
404{
405 struct vxlan_rdst *rd;
406
407 list_for_each_entry(rd, &f->remotes, list) {
408 if (rd->remote_ip == ip &&
409 rd->remote_port == port &&
410 rd->remote_vni == vni &&
411 rd->remote_ifindex == ifindex)
412 return rd;
413 }
414
415 return NULL;
416}
417
Thomas Richter906dc182013-07-19 17:20:07 +0200418/* Replace destination of unicast mac */
419static int vxlan_fdb_replace(struct vxlan_fdb *f,
420 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
421{
422 struct vxlan_rdst *rd;
423
424 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
425 if (rd)
426 return 0;
427
428 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
429 if (!rd)
430 return 0;
431 rd->remote_ip = ip;
432 rd->remote_port = port;
433 rd->remote_vni = vni;
434 rd->remote_ifindex = ifindex;
435 return 1;
436}
437
David Stevens66817122013-03-15 04:35:51 +0000438/* Add/update destinations for multicast */
439static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000440 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000441{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700442 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000443
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300444 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
445 if (rd)
446 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700447
David Stevens66817122013-03-15 04:35:51 +0000448 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
449 if (rd == NULL)
450 return -ENOBUFS;
451 rd->remote_ip = ip;
452 rd->remote_port = port;
453 rd->remote_vni = vni;
454 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700455
456 list_add_tail_rcu(&rd->list, &f->remotes);
457
David Stevens66817122013-03-15 04:35:51 +0000458 return 1;
459}
460
stephen hemmingerd3428942012-10-01 12:32:35 +0000461/* Add new entry to forwarding table -- assumes lock held */
462static int vxlan_fdb_create(struct vxlan_dev *vxlan,
463 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000464 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000465 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000466 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000467{
468 struct vxlan_fdb *f;
469 int notify = 0;
470
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000471 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000472 if (f) {
473 if (flags & NLM_F_EXCL) {
474 netdev_dbg(vxlan->dev,
475 "lost race to create %pM\n", mac);
476 return -EEXIST;
477 }
478 if (f->state != state) {
479 f->state = state;
480 f->updated = jiffies;
481 notify = 1;
482 }
David Stevensae884082013-04-19 00:36:26 +0000483 if (f->flags != ndm_flags) {
484 f->flags = ndm_flags;
485 f->updated = jiffies;
486 notify = 1;
487 }
Thomas Richter906dc182013-07-19 17:20:07 +0200488 if ((flags & NLM_F_REPLACE)) {
489 /* Only change unicasts */
490 if (!(is_multicast_ether_addr(f->eth_addr) ||
491 is_zero_ether_addr(f->eth_addr))) {
492 int rc = vxlan_fdb_replace(f, ip, port, vni,
493 ifindex);
494
495 if (rc < 0)
496 return rc;
497 notify |= rc;
498 } else
499 return -EOPNOTSUPP;
500 }
David Stevens66817122013-03-15 04:35:51 +0000501 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300502 (is_multicast_ether_addr(f->eth_addr) ||
503 is_zero_ether_addr(f->eth_addr))) {
David Stevens66817122013-03-15 04:35:51 +0000504 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
505
506 if (rc < 0)
507 return rc;
508 notify |= rc;
509 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000510 } else {
511 if (!(flags & NLM_F_CREATE))
512 return -ENOENT;
513
514 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
515 return -ENOSPC;
516
Thomas Richter906dc182013-07-19 17:20:07 +0200517 /* Disallow replace to add a multicast entry */
518 if ((flags & NLM_F_REPLACE) &&
519 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
520 return -EOPNOTSUPP;
521
stephen hemmingerd3428942012-10-01 12:32:35 +0000522 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
523 f = kmalloc(sizeof(*f), GFP_ATOMIC);
524 if (!f)
525 return -ENOMEM;
526
527 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000528 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000529 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000530 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700531 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000532 memcpy(f->eth_addr, mac, ETH_ALEN);
533
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700534 vxlan_fdb_append(f, ip, port, vni, ifindex);
535
stephen hemmingerd3428942012-10-01 12:32:35 +0000536 ++vxlan->addrcnt;
537 hlist_add_head_rcu(&f->hlist,
538 vxlan_fdb_head(vxlan, mac));
539 }
540
541 if (notify)
542 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
543
544 return 0;
545}
546
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300547static void vxlan_fdb_free_rdst(struct rcu_head *head)
548{
549 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
550 kfree(rd);
551}
552
Wei Yongjun6706c822013-04-11 19:00:35 +0000553static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000554{
555 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700556 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000557
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700558 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000559 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000560 kfree(f);
561}
562
stephen hemmingerd3428942012-10-01 12:32:35 +0000563static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
564{
565 netdev_dbg(vxlan->dev,
566 "delete %pM\n", f->eth_addr);
567
568 --vxlan->addrcnt;
569 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
570
571 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000572 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000573}
574
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300575static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
576 __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
577{
578 struct net *net = dev_net(vxlan->dev);
579
580 if (tb[NDA_DST]) {
581 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
582 return -EAFNOSUPPORT;
583
584 *ip = nla_get_be32(tb[NDA_DST]);
585 } else {
586 *ip = htonl(INADDR_ANY);
587 }
588
589 if (tb[NDA_PORT]) {
590 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
591 return -EINVAL;
592 *port = nla_get_be16(tb[NDA_PORT]);
593 } else {
594 *port = vxlan->dst_port;
595 }
596
597 if (tb[NDA_VNI]) {
598 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
599 return -EINVAL;
600 *vni = nla_get_u32(tb[NDA_VNI]);
601 } else {
602 *vni = vxlan->default_dst.remote_vni;
603 }
604
605 if (tb[NDA_IFINDEX]) {
606 struct net_device *tdev;
607
608 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
609 return -EINVAL;
610 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
611 tdev = dev_get_by_index(net, *ifindex);
612 if (!tdev)
613 return -EADDRNOTAVAIL;
614 dev_put(tdev);
615 } else {
616 *ifindex = 0;
617 }
618
619 return 0;
620}
621
stephen hemmingerd3428942012-10-01 12:32:35 +0000622/* Add static entry (via netlink) */
623static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
624 struct net_device *dev,
625 const unsigned char *addr, u16 flags)
626{
627 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300628 /* struct net *net = dev_net(vxlan->dev); */
stephen hemmingerd3428942012-10-01 12:32:35 +0000629 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000630 __be16 port;
631 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000632 int err;
633
634 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
635 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
636 ndm->ndm_state);
637 return -EINVAL;
638 }
639
640 if (tb[NDA_DST] == NULL)
641 return -EINVAL;
642
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300643 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
644 if (err)
645 return err;
David Stevens66817122013-03-15 04:35:51 +0000646
stephen hemmingerd3428942012-10-01 12:32:35 +0000647 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000648 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
649 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000650 spin_unlock_bh(&vxlan->hash_lock);
651
652 return err;
653}
654
655/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000656static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
657 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000658 const unsigned char *addr)
659{
660 struct vxlan_dev *vxlan = netdev_priv(dev);
661 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300662 struct vxlan_rdst *rd = NULL;
663 __be32 ip;
664 __be16 port;
665 u32 vni, ifindex;
666 int err;
667
668 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
669 if (err)
670 return err;
671
672 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000673
674 spin_lock_bh(&vxlan->hash_lock);
675 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300676 if (!f)
677 goto out;
678
679 if (ip != htonl(INADDR_ANY)) {
680 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
681 if (!rd)
682 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000683 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300684
685 err = 0;
686
687 /* remove a destination if it's not the only one on the list,
688 * otherwise destroy the fdb entry
689 */
690 if (rd && !list_is_singular(&f->remotes)) {
691 list_del_rcu(&rd->list);
692 call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
693 goto out;
694 }
695
696 vxlan_fdb_destroy(vxlan, f);
697
698out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000699 spin_unlock_bh(&vxlan->hash_lock);
700
701 return err;
702}
703
704/* Dump forwarding table */
705static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
706 struct net_device *dev, int idx)
707{
708 struct vxlan_dev *vxlan = netdev_priv(dev);
709 unsigned int h;
710
711 for (h = 0; h < FDB_HASH_SIZE; ++h) {
712 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000713 int err;
714
Sasha Levinb67bfe02013-02-27 17:06:00 -0800715 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000716 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000717
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700718 if (idx < cb->args[0])
719 goto skip;
720
721 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000722 err = vxlan_fdb_info(skb, vxlan, f,
723 NETLINK_CB(cb->skb).portid,
724 cb->nlh->nlmsg_seq,
725 RTM_NEWNEIGH,
726 NLM_F_MULTI, rd);
727 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700728 goto out;
David Stevens66817122013-03-15 04:35:51 +0000729 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700730skip:
731 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000732 }
733 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700734out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000735 return idx;
736}
737
738/* Watch incoming packets to learn mapping between Ethernet address
739 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700740 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000741 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700742static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000743 __be32 src_ip, const u8 *src_mac)
744{
745 struct vxlan_dev *vxlan = netdev_priv(dev);
746 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000747
748 f = vxlan_find_mac(vxlan, src_mac);
749 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700750 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700751
752 if (likely(rdst->remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700753 return false;
754
755 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700756 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700757 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000758
759 if (net_ratelimit())
760 netdev_info(dev,
761 "%pM migrated from %pI4 to %pI4\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700762 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000763
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700764 rdst->remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000765 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700766 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000767 } else {
768 /* learned new entry */
769 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700770
771 /* close off race between vxlan_flush and incoming packets */
772 if (netif_running(dev))
773 vxlan_fdb_create(vxlan, src_mac, src_ip,
774 NUD_REACHABLE,
775 NLM_F_EXCL|NLM_F_CREATE,
776 vxlan->dst_port,
777 vxlan->default_dst.remote_vni,
778 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000779 spin_unlock(&vxlan->hash_lock);
780 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700781
782 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000783}
784
stephen hemmingerd3428942012-10-01 12:32:35 +0000785/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700786static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000787{
stephen hemminger553675f2013-05-16 11:35:20 +0000788 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000789
stephen hemminger553675f2013-05-16 11:35:20 +0000790 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000791 if (!netif_running(vxlan->dev))
792 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000793
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700794 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000795 return true;
796 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000797
798 return false;
799}
800
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700801static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000802{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700803 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000804}
805
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700806static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000807{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700808 if (!atomic_dec_and_test(&vs->refcnt))
809 return;
810
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700811 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700812 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700813 spin_unlock(&vn->sock_lock);
814
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700815 queue_work(vxlan_wq, &vs->del_work);
816}
817
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_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
826 struct vxlan_sock *vs = vxlan->vn_sock;
827 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000828 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000829 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
830 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000831 };
832
stephen hemmingerd3428942012-10-01 12:32:35 +0000833 lock_sock(sk);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700834 ip_mc_join_group(sk, &mreq);
835 release_sock(sk);
836
837 vxlan_sock_release(vn, vs);
838 dev_put(vxlan->dev);
839}
840
841/* Inverse of vxlan_igmp_join when last VNI is brought down */
842static void vxlan_igmp_leave(struct work_struct *work)
843{
844 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
845 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
846 struct vxlan_sock *vs = vxlan->vn_sock;
847 struct sock *sk = vs->sock->sk;
848 struct ip_mreqn mreq = {
849 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
850 .imr_ifindex = vxlan->default_dst.remote_ifindex,
851 };
852
853 lock_sock(sk);
854 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000855 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000856
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700857 vxlan_sock_release(vn, vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700858 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000859}
860
861/* Callback from net/ipv4/udp.c to receive packets */
862static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
863{
864 struct iphdr *oip;
865 struct vxlanhdr *vxh;
866 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000867 struct pcpu_tstats *stats;
stephen hemminger553675f2013-05-16 11:35:20 +0000868 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000869 __u32 vni;
870 int err;
871
stephen hemmingerd3428942012-10-01 12:32:35 +0000872 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700873 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +0000874 goto error;
875
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700876 /* Return packets with reserved bits set */
877 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +0000878 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
879 (vxh->vx_vni & htonl(0xff))) {
880 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
881 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
882 goto error;
883 }
884
stephen hemmingerd3428942012-10-01 12:32:35 +0000885 /* Is this VNI defined? */
886 vni = ntohl(vxh->vx_vni) >> 8;
stephen hemminger553675f2013-05-16 11:35:20 +0000887 port = inet_sk(sk)->inet_sport;
888 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000889 if (!vxlan) {
stephen hemminger553675f2013-05-16 11:35:20 +0000890 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
891 vni, ntohs(port));
stephen hemmingerd3428942012-10-01 12:32:35 +0000892 goto drop;
893 }
894
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700895 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB))) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000896 vxlan->dev->stats.rx_length_errors++;
897 vxlan->dev->stats.rx_errors++;
898 goto drop;
899 }
900
David Stevense4f67ad2012-11-20 02:50:14 +0000901 skb_reset_mac_header(skb);
902
stephen hemmingerd3428942012-10-01 12:32:35 +0000903 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000904
905 /* Ignore packet loops (and multicast echo) */
906 if (compare_ether_addr(eth_hdr(skb)->h_source,
907 vxlan->dev->dev_addr) == 0)
908 goto drop;
909
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700910 /* Re-examine inner Ethernet packet */
911 oip = ip_hdr(skb);
stephen hemminger26a41ae62013-06-17 12:09:58 -0700912 if ((vxlan->flags & VXLAN_F_LEARN) &&
913 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
914 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000915
stephen hemmingerd3428942012-10-01 12:32:35 +0000916 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000917
918 /* If the NIC driver gave us an encapsulated packet with
919 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
920 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
921 * for us. Otherwise force the upper layers to verify it.
922 */
923 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
924 !(vxlan->dev->features & NETIF_F_RXCSUM))
925 skb->ip_summed = CHECKSUM_NONE;
926
927 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000928
929 err = IP_ECN_decapsulate(oip, skb);
930 if (unlikely(err)) {
931 if (log_ecn_error)
932 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
933 &oip->saddr, oip->tos);
934 if (err > 1) {
935 ++vxlan->dev->stats.rx_frame_errors;
936 ++vxlan->dev->stats.rx_errors;
937 goto drop;
938 }
939 }
940
Pravin B Shelare8171042013-03-25 14:49:46 +0000941 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000942 u64_stats_update_begin(&stats->syncp);
943 stats->rx_packets++;
944 stats->rx_bytes += skb->len;
945 u64_stats_update_end(&stats->syncp);
946
947 netif_rx(skb);
948
949 return 0;
950error:
stephen hemmingerd3428942012-10-01 12:32:35 +0000951 return 1;
952drop:
953 /* Consume bad packet */
954 kfree_skb(skb);
955 return 0;
956}
957
David Stevense4f67ad2012-11-20 02:50:14 +0000958static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
959{
960 struct vxlan_dev *vxlan = netdev_priv(dev);
961 struct arphdr *parp;
962 u8 *arpptr, *sha;
963 __be32 sip, tip;
964 struct neighbour *n;
965
966 if (dev->flags & IFF_NOARP)
967 goto out;
968
969 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
970 dev->stats.tx_dropped++;
971 goto out;
972 }
973 parp = arp_hdr(skb);
974
975 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
976 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
977 parp->ar_pro != htons(ETH_P_IP) ||
978 parp->ar_op != htons(ARPOP_REQUEST) ||
979 parp->ar_hln != dev->addr_len ||
980 parp->ar_pln != 4)
981 goto out;
982 arpptr = (u8 *)parp + sizeof(struct arphdr);
983 sha = arpptr;
984 arpptr += dev->addr_len; /* sha */
985 memcpy(&sip, arpptr, sizeof(sip));
986 arpptr += sizeof(sip);
987 arpptr += dev->addr_len; /* tha */
988 memcpy(&tip, arpptr, sizeof(tip));
989
990 if (ipv4_is_loopback(tip) ||
991 ipv4_is_multicast(tip))
992 goto out;
993
994 n = neigh_lookup(&arp_tbl, &tip, dev);
995
996 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000997 struct vxlan_fdb *f;
998 struct sk_buff *reply;
999
1000 if (!(n->nud_state & NUD_CONNECTED)) {
1001 neigh_release(n);
1002 goto out;
1003 }
1004
1005 f = vxlan_find_mac(vxlan, n->ha);
stephen hemminger5ca54612013-08-04 17:17:39 -07001006 if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001007 /* bridge-local neighbor */
1008 neigh_release(n);
1009 goto out;
1010 }
1011
1012 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1013 n->ha, sha);
1014
1015 neigh_release(n);
1016
1017 skb_reset_mac_header(reply);
1018 __skb_pull(reply, skb_network_offset(reply));
1019 reply->ip_summed = CHECKSUM_UNNECESSARY;
1020 reply->pkt_type = PACKET_HOST;
1021
1022 if (netif_rx_ni(reply) == NET_RX_DROP)
1023 dev->stats.rx_dropped++;
1024 } else if (vxlan->flags & VXLAN_F_L3MISS)
1025 vxlan_ip_miss(dev, tip);
1026out:
1027 consume_skb(skb);
1028 return NETDEV_TX_OK;
1029}
1030
1031static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1032{
1033 struct vxlan_dev *vxlan = netdev_priv(dev);
1034 struct neighbour *n;
1035 struct iphdr *pip;
1036
1037 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1038 return false;
1039
1040 n = NULL;
1041 switch (ntohs(eth_hdr(skb)->h_proto)) {
1042 case ETH_P_IP:
1043 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1044 return false;
1045 pip = ip_hdr(skb);
1046 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1047 break;
1048 default:
1049 return false;
1050 }
1051
1052 if (n) {
1053 bool diff;
1054
1055 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1056 if (diff) {
1057 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1058 dev->addr_len);
1059 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1060 }
1061 neigh_release(n);
1062 return diff;
1063 } else if (vxlan->flags & VXLAN_F_L3MISS)
1064 vxlan_ip_miss(dev, pip->daddr);
1065 return false;
1066}
1067
stephen hemminger553675f2013-05-16 11:35:20 +00001068static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001069{
1070 sock_put(skb->sk);
1071}
1072
1073/* On transmit, associate with the tunnel socket */
1074static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1075{
stephen hemminger553675f2013-05-16 11:35:20 +00001076 struct vxlan_dev *vxlan = netdev_priv(dev);
1077 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +00001078
1079 skb_orphan(skb);
1080 sock_hold(sk);
1081 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001082 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001083}
1084
stephen hemminger05f47d62012-10-09 20:35:50 +00001085/* Compute source port for outgoing packet
1086 * first choice to use L4 flow hash since it will spread
1087 * better and maybe available from hardware
1088 * secondary choice is to use jhash on the Ethernet header
1089 */
stephen hemminger7d836a72013-04-27 11:31:56 +00001090static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001091{
1092 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1093 u32 hash;
1094
1095 hash = skb_get_rxhash(skb);
1096 if (!hash)
1097 hash = jhash(skb->data, 2 * ETH_ALEN,
1098 (__force u32) skb->protocol);
1099
stephen hemminger7d836a72013-04-27 11:31:56 +00001100 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001101}
1102
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001103static int handle_offloads(struct sk_buff *skb)
1104{
1105 if (skb_is_gso(skb)) {
1106 int err = skb_unclone(skb, GFP_ATOMIC);
1107 if (unlikely(err))
1108 return err;
1109
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001110 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001111 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1112 skb->ip_summed = CHECKSUM_NONE;
1113
1114 return 0;
1115}
1116
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001117/* Bypass encapsulation if the destination is local */
1118static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1119 struct vxlan_dev *dst_vxlan)
1120{
1121 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1122 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1123
1124 skb->pkt_type = PACKET_HOST;
1125 skb->encapsulation = 0;
1126 skb->dev = dst_vxlan->dev;
1127 __skb_pull(skb, skb_network_offset(skb));
1128
1129 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001130 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1131 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001132
1133 u64_stats_update_begin(&tx_stats->syncp);
1134 tx_stats->tx_packets++;
1135 tx_stats->tx_bytes += skb->len;
1136 u64_stats_update_end(&tx_stats->syncp);
1137
1138 if (netif_rx(skb) == NET_RX_SUCCESS) {
1139 u64_stats_update_begin(&rx_stats->syncp);
1140 rx_stats->rx_packets++;
1141 rx_stats->rx_bytes += skb->len;
1142 u64_stats_update_end(&rx_stats->syncp);
1143 } else {
1144 skb->dev->stats.rx_dropped++;
1145 }
1146}
1147
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001148static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1149 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001150{
1151 struct vxlan_dev *vxlan = netdev_priv(dev);
1152 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001153 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001154 struct vxlanhdr *vxh;
1155 struct udphdr *uh;
1156 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001157 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001158 __be16 src_port, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001159 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001160 __be16 df = 0;
1161 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001162 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001163
stephen hemminger823aa872013-04-27 11:31:57 +00001164 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001165 vni = rdst->remote_vni;
1166 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001167
1168 if (!dst) {
1169 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001170 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001171 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001172 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001173 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001174 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001175 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001176
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001177 if (!skb->encapsulation) {
1178 skb_reset_inner_headers(skb);
1179 skb->encapsulation = 1;
1180 }
1181
stephen hemmingerd3428942012-10-01 12:32:35 +00001182 /* Need space for new headers (invalidates iph ptr) */
1183 if (skb_cow_head(skb, VXLAN_HEADROOM))
1184 goto drop;
1185
stephen hemmingerd3428942012-10-01 12:32:35 +00001186 old_iph = ip_hdr(skb);
1187
stephen hemmingerd3428942012-10-01 12:32:35 +00001188 ttl = vxlan->ttl;
1189 if (!ttl && IN_MULTICAST(ntohl(dst)))
1190 ttl = 1;
1191
1192 tos = vxlan->tos;
1193 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001194 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001195
stephen hemminger05f47d62012-10-09 20:35:50 +00001196 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001197
stephen hemmingerca78f182012-10-09 20:35:48 +00001198 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001199 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001200 fl4.flowi4_tos = RT_TOS(tos);
1201 fl4.daddr = dst;
1202 fl4.saddr = vxlan->saddr;
1203
1204 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001205 if (IS_ERR(rt)) {
1206 netdev_dbg(dev, "no route to %pI4\n", &dst);
1207 dev->stats.tx_carrier_errors++;
1208 goto tx_error;
1209 }
1210
1211 if (rt->dst.dev == dev) {
1212 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1213 ip_rt_put(rt);
1214 dev->stats.collisions++;
1215 goto tx_error;
1216 }
1217
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001218 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001219 if (rt->rt_flags & RTCF_LOCAL &&
1220 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001221 struct vxlan_dev *dst_vxlan;
1222
1223 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001224 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001225 if (!dst_vxlan)
1226 goto tx_error;
1227 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001228 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001229 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001230 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1231 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001232 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001233
1234 __skb_push(skb, sizeof(*uh));
1235 skb_reset_transport_header(skb);
1236 uh = udp_hdr(skb);
1237
stephen hemminger73cf3312013-04-27 11:31:54 +00001238 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001239 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001240
1241 uh->len = htons(skb->len);
1242 uh->check = 0;
1243
stephen hemminger1cad8712012-10-09 20:35:49 +00001244 vxlan_set_owner(dev, skb);
1245
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001246 if (handle_offloads(skb))
1247 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001248
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001249 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1250 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1251
1252 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1253 IPPROTO_UDP, tos, ttl, df);
1254 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1255
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001256 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001257
1258drop:
1259 dev->stats.tx_dropped++;
1260 goto tx_free;
1261
1262tx_error:
1263 dev->stats.tx_errors++;
1264tx_free:
1265 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001266}
1267
David Stevens66817122013-03-15 04:35:51 +00001268/* Transmit local packets over Vxlan
1269 *
1270 * Outer IP header inherits ECN and DF from inner header.
1271 * Outer UDP destination is the VXLAN assigned port.
1272 * source port is based on hash of flow
1273 */
1274static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1275{
1276 struct vxlan_dev *vxlan = netdev_priv(dev);
1277 struct ethhdr *eth;
1278 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001279 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001280 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001281
1282 skb_reset_mac_header(skb);
1283 eth = eth_hdr(skb);
1284
1285 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1286 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001287
1288 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001289 did_rsc = false;
1290
1291 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1292 ntohs(eth->h_proto) == ETH_P_IP) {
1293 did_rsc = route_shortcircuit(dev, skb);
1294 if (did_rsc)
1295 f = vxlan_find_mac(vxlan, eth->h_dest);
1296 }
1297
David Stevens66817122013-03-15 04:35:51 +00001298 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001299 f = vxlan_find_mac(vxlan, all_zeros_mac);
1300 if (f == NULL) {
1301 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1302 !is_multicast_ether_addr(eth->h_dest))
1303 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001304
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001305 dev->stats.tx_dropped++;
1306 dev_kfree_skb(skb);
1307 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001308 }
David Stevens66817122013-03-15 04:35:51 +00001309 }
1310
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001311 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1312 struct sk_buff *skb1;
1313
1314 skb1 = skb_clone(skb, GFP_ATOMIC);
1315 if (skb1)
1316 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1317 }
1318
1319 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001320 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001321}
1322
stephen hemmingerd3428942012-10-01 12:32:35 +00001323/* Walk the forwarding table and purge stale entries */
1324static void vxlan_cleanup(unsigned long arg)
1325{
1326 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1327 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1328 unsigned int h;
1329
1330 if (!netif_running(vxlan->dev))
1331 return;
1332
1333 spin_lock_bh(&vxlan->hash_lock);
1334 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1335 struct hlist_node *p, *n;
1336 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1337 struct vxlan_fdb *f
1338 = container_of(p, struct vxlan_fdb, hlist);
1339 unsigned long timeout;
1340
stephen hemminger3c172862012-10-26 06:24:34 +00001341 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001342 continue;
1343
1344 timeout = f->used + vxlan->age_interval * HZ;
1345 if (time_before_eq(timeout, jiffies)) {
1346 netdev_dbg(vxlan->dev,
1347 "garbage collect %pM\n",
1348 f->eth_addr);
1349 f->state = NUD_STALE;
1350 vxlan_fdb_destroy(vxlan, f);
1351 } else if (time_before(timeout, next_timer))
1352 next_timer = timeout;
1353 }
1354 }
1355 spin_unlock_bh(&vxlan->hash_lock);
1356
1357 mod_timer(&vxlan->age_timer, next_timer);
1358}
1359
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001360static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1361{
1362 __u32 vni = vxlan->default_dst.remote_vni;
1363
1364 vxlan->vn_sock = vs;
1365 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1366}
1367
stephen hemmingerd3428942012-10-01 12:32:35 +00001368/* Setup stats when device is created */
1369static int vxlan_init(struct net_device *dev)
1370{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001371 struct vxlan_dev *vxlan = netdev_priv(dev);
1372 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1373 struct vxlan_sock *vs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001374
Pravin B Shelare8171042013-03-25 14:49:46 +00001375 dev->tstats = alloc_percpu(struct pcpu_tstats);
1376 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001377 return -ENOMEM;
1378
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001379 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001380 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001381 if (vs) {
1382 /* If we have a socket with same port already, reuse it */
1383 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001384 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001385 } else {
1386 /* otherwise make new socket outside of RTNL */
1387 dev_hold(dev);
1388 queue_work(vxlan_wq, &vxlan->sock_work);
1389 }
1390 spin_unlock(&vn->sock_lock);
1391
stephen hemmingerd3428942012-10-01 12:32:35 +00001392 return 0;
1393}
1394
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001395static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001396{
1397 struct vxlan_fdb *f;
1398
1399 spin_lock_bh(&vxlan->hash_lock);
1400 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1401 if (f)
1402 vxlan_fdb_destroy(vxlan, f);
1403 spin_unlock_bh(&vxlan->hash_lock);
1404}
1405
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001406static void vxlan_uninit(struct net_device *dev)
1407{
1408 struct vxlan_dev *vxlan = netdev_priv(dev);
1409 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1410 struct vxlan_sock *vs = vxlan->vn_sock;
1411
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001412 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001413
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001414 if (vs)
1415 vxlan_sock_release(vn, vs);
1416 free_percpu(dev->tstats);
1417}
1418
stephen hemmingerd3428942012-10-01 12:32:35 +00001419/* Start ageing timer and join group when device is brought up */
1420static int vxlan_open(struct net_device *dev)
1421{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001422 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001423 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001424 struct vxlan_sock *vs = vxlan->vn_sock;
1425
1426 /* socket hasn't been created */
1427 if (!vs)
1428 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001429
stephen hemminger3fc2de22013-07-18 08:40:15 -07001430 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
Cong Wang614334d2013-08-07 16:35:45 +08001431 vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001432 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001433 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001434 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00001435 }
1436
1437 if (vxlan->age_interval)
1438 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1439
1440 return 0;
1441}
1442
1443/* Purge the forwarding table */
1444static void vxlan_flush(struct vxlan_dev *vxlan)
1445{
Cong Wang31fec5a2013-05-27 22:35:52 +00001446 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001447
1448 spin_lock_bh(&vxlan->hash_lock);
1449 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1450 struct hlist_node *p, *n;
1451 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1452 struct vxlan_fdb *f
1453 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001454 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1455 if (!is_zero_ether_addr(f->eth_addr))
1456 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001457 }
1458 }
1459 spin_unlock_bh(&vxlan->hash_lock);
1460}
1461
1462/* Cleanup timer and forwarding table on shutdown */
1463static int vxlan_stop(struct net_device *dev)
1464{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001465 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001466 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001467 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001468
stephen hemminger3fc2de22013-07-18 08:40:15 -07001469 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1470 ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001471 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001472 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001473 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001474 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001475
1476 del_timer_sync(&vxlan->age_timer);
1477
1478 vxlan_flush(vxlan);
1479
1480 return 0;
1481}
1482
stephen hemmingerd3428942012-10-01 12:32:35 +00001483/* Stub, nothing needs to be done. */
1484static void vxlan_set_multicast_list(struct net_device *dev)
1485{
1486}
1487
1488static const struct net_device_ops vxlan_netdev_ops = {
1489 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001490 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001491 .ndo_open = vxlan_open,
1492 .ndo_stop = vxlan_stop,
1493 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001494 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001495 .ndo_set_rx_mode = vxlan_set_multicast_list,
1496 .ndo_change_mtu = eth_change_mtu,
1497 .ndo_validate_addr = eth_validate_addr,
1498 .ndo_set_mac_address = eth_mac_addr,
1499 .ndo_fdb_add = vxlan_fdb_add,
1500 .ndo_fdb_del = vxlan_fdb_delete,
1501 .ndo_fdb_dump = vxlan_fdb_dump,
1502};
1503
1504/* Info for udev, that this is a virtual tunnel endpoint */
1505static struct device_type vxlan_type = {
1506 .name = "vxlan",
1507};
1508
stephen hemmingerd3428942012-10-01 12:32:35 +00001509/* Initialize the device structure. */
1510static void vxlan_setup(struct net_device *dev)
1511{
1512 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001513 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001514 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001515
1516 eth_hw_addr_random(dev);
1517 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001518 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001519
1520 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001521 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001522 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1523
1524 dev->tx_queue_len = 0;
1525 dev->features |= NETIF_F_LLTX;
1526 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001527 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001528 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001529 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001530
1531 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001532 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001533 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001534 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001535
stephen hemminger553675f2013-05-16 11:35:20 +00001536 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001537 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001538 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1539 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001540 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001541
1542 init_timer_deferrable(&vxlan->age_timer);
1543 vxlan->age_timer.function = vxlan_cleanup;
1544 vxlan->age_timer.data = (unsigned long) vxlan;
1545
stephen hemminger05f47d62012-10-09 20:35:50 +00001546 inet_get_local_port_range(&low, &high);
1547 vxlan->port_min = low;
1548 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001549 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001550
stephen hemmingerd3428942012-10-01 12:32:35 +00001551 vxlan->dev = dev;
1552
1553 for (h = 0; h < FDB_HASH_SIZE; ++h)
1554 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1555}
1556
1557static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1558 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001559 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001560 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1561 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1562 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1563 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1564 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1565 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1566 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001567 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001568 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1569 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1570 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1571 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001572 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001573};
1574
1575static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1576{
1577 if (tb[IFLA_ADDRESS]) {
1578 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1579 pr_debug("invalid link address (not ethernet)\n");
1580 return -EINVAL;
1581 }
1582
1583 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1584 pr_debug("invalid all zero ethernet address\n");
1585 return -EADDRNOTAVAIL;
1586 }
1587 }
1588
1589 if (!data)
1590 return -EINVAL;
1591
1592 if (data[IFLA_VXLAN_ID]) {
1593 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1594 if (id >= VXLAN_VID_MASK)
1595 return -ERANGE;
1596 }
1597
stephen hemminger05f47d62012-10-09 20:35:50 +00001598 if (data[IFLA_VXLAN_PORT_RANGE]) {
1599 const struct ifla_vxlan_port_range *p
1600 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1601
1602 if (ntohs(p->high) < ntohs(p->low)) {
1603 pr_debug("port range %u .. %u not valid\n",
1604 ntohs(p->low), ntohs(p->high));
1605 return -EINVAL;
1606 }
1607 }
1608
stephen hemmingerd3428942012-10-01 12:32:35 +00001609 return 0;
1610}
1611
Yan Burman1b13c972013-01-29 23:43:07 +00001612static void vxlan_get_drvinfo(struct net_device *netdev,
1613 struct ethtool_drvinfo *drvinfo)
1614{
1615 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1616 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1617}
1618
1619static const struct ethtool_ops vxlan_ethtool_ops = {
1620 .get_drvinfo = vxlan_get_drvinfo,
1621 .get_link = ethtool_op_get_link,
1622};
1623
stephen hemminger553675f2013-05-16 11:35:20 +00001624static void vxlan_del_work(struct work_struct *work)
1625{
1626 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1627
1628 sk_release_kernel(vs->sock->sk);
1629 kfree_rcu(vs, rcu);
1630}
1631
stephen hemminger553675f2013-05-16 11:35:20 +00001632static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1633{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001634 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemminger553675f2013-05-16 11:35:20 +00001635 struct vxlan_sock *vs;
1636 struct sock *sk;
1637 struct sockaddr_in vxlan_addr = {
1638 .sin_family = AF_INET,
1639 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07001640 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00001641 };
1642 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001643 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001644
1645 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001646 if (!vs) {
1647 pr_debug("memory alocation failure\n");
stephen hemminger553675f2013-05-16 11:35:20 +00001648 return ERR_PTR(-ENOMEM);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001649 }
stephen hemminger553675f2013-05-16 11:35:20 +00001650
1651 for (h = 0; h < VNI_HASH_SIZE; ++h)
1652 INIT_HLIST_HEAD(&vs->vni_list[h]);
1653
1654 INIT_WORK(&vs->del_work, vxlan_del_work);
1655
1656 /* Create UDP socket for encapsulation receive. */
1657 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1658 if (rc < 0) {
1659 pr_debug("UDP socket create failed\n");
1660 kfree(vs);
1661 return ERR_PTR(rc);
1662 }
1663
1664 /* Put in proper namespace */
1665 sk = vs->sock->sk;
1666 sk_change_net(sk, net);
1667
stephen hemminger553675f2013-05-16 11:35:20 +00001668 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1669 sizeof(vxlan_addr));
1670 if (rc < 0) {
1671 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1672 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1673 sk_release_kernel(sk);
1674 kfree(vs);
1675 return ERR_PTR(rc);
1676 }
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001677 atomic_set(&vs->refcnt, 1);
stephen hemminger553675f2013-05-16 11:35:20 +00001678
1679 /* Disable multicast loopback */
1680 inet_sk(sk)->mc_loop = 0;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001681 spin_lock(&vn->sock_lock);
1682 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1683 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00001684
1685 /* Mark socket as an encapsulation socket. */
1686 udp_sk(sk)->encap_type = 1;
1687 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1688 udp_encap_enable();
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001689 return vs;
1690}
stephen hemminger553675f2013-05-16 11:35:20 +00001691
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001692static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port)
1693{
1694 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1695 struct vxlan_sock *vs;
1696
1697 vs = vxlan_socket_create(net, port);
1698 if (!IS_ERR(vs))
1699 return vs;
1700
1701 spin_lock(&vn->sock_lock);
1702 vs = vxlan_find_sock(net, port);
1703 if (vs)
1704 atomic_inc(&vs->refcnt);
1705 else
1706 vs = ERR_PTR(-EINVAL);
1707
1708 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00001709 return vs;
1710}
1711
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001712/* Scheduled at device creation to bind to a socket */
1713static void vxlan_sock_work(struct work_struct *work)
1714{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001715 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
1716 struct net *net = dev_net(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001717 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001718 __be16 port = vxlan->dst_port;
1719 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001720
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001721 nvs = vxlan_sock_add(net, port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001722 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001723 if (!IS_ERR(nvs))
1724 vxlan_vs_add_dev(nvs, vxlan);
1725 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001726
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001727 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001728}
1729
stephen hemmingerd3428942012-10-01 12:32:35 +00001730static int vxlan_newlink(struct net *net, struct net_device *dev,
1731 struct nlattr *tb[], struct nlattr *data[])
1732{
stephen hemminger553675f2013-05-16 11:35:20 +00001733 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001734 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001735 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001736 __u32 vni;
1737 int err;
1738
1739 if (!data[IFLA_VXLAN_ID])
1740 return -EINVAL;
1741
1742 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001743 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001744
stephen hemminger5d174dd2013-04-27 11:31:55 +00001745 if (data[IFLA_VXLAN_GROUP])
1746 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001747
1748 if (data[IFLA_VXLAN_LOCAL])
1749 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1750
stephen hemminger34e02aa2012-10-09 20:35:53 +00001751 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001752 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001753 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001754 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001755
stephen hemminger34e02aa2012-10-09 20:35:53 +00001756 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001757 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001758 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001759 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001760
1761 if (!tb[IFLA_MTU])
1762 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001763
1764 /* update header length based on lower device */
1765 dev->hard_header_len = lowerdev->hard_header_len +
1766 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001767 }
1768
1769 if (data[IFLA_VXLAN_TOS])
1770 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1771
Vincent Bernatafb97182012-10-30 10:27:16 +00001772 if (data[IFLA_VXLAN_TTL])
1773 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1774
stephen hemmingerd3428942012-10-01 12:32:35 +00001775 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001776 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001777
1778 if (data[IFLA_VXLAN_AGEING])
1779 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1780 else
1781 vxlan->age_interval = FDB_AGE_DEFAULT;
1782
David Stevense4f67ad2012-11-20 02:50:14 +00001783 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1784 vxlan->flags |= VXLAN_F_PROXY;
1785
1786 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1787 vxlan->flags |= VXLAN_F_RSC;
1788
1789 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1790 vxlan->flags |= VXLAN_F_L2MISS;
1791
1792 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1793 vxlan->flags |= VXLAN_F_L3MISS;
1794
stephen hemmingerd3428942012-10-01 12:32:35 +00001795 if (data[IFLA_VXLAN_LIMIT])
1796 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1797
stephen hemminger05f47d62012-10-09 20:35:50 +00001798 if (data[IFLA_VXLAN_PORT_RANGE]) {
1799 const struct ifla_vxlan_port_range *p
1800 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1801 vxlan->port_min = ntohs(p->low);
1802 vxlan->port_max = ntohs(p->high);
1803 }
1804
stephen hemminger823aa872013-04-27 11:31:57 +00001805 if (data[IFLA_VXLAN_PORT])
1806 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1807
stephen hemminger553675f2013-05-16 11:35:20 +00001808 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1809 pr_info("duplicate VNI %u\n", vni);
1810 return -EEXIST;
1811 }
1812
Yan Burman1b13c972013-01-29 23:43:07 +00001813 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1814
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001815 /* create an fdb entry for default destination */
1816 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1817 vxlan->default_dst.remote_ip,
1818 NUD_REACHABLE|NUD_PERMANENT,
1819 NLM_F_EXCL|NLM_F_CREATE,
1820 vxlan->dst_port, vxlan->default_dst.remote_vni,
1821 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001822 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001823 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001824
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001825 err = register_netdevice(dev);
1826 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001827 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001828 return err;
1829 }
1830
stephen hemminger553675f2013-05-16 11:35:20 +00001831 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001832
1833 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001834}
1835
1836static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1837{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001838 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001839 struct vxlan_dev *vxlan = netdev_priv(dev);
1840
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001841 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001842 if (!hlist_unhashed(&vxlan->hlist))
1843 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07001844 spin_unlock(&vn->sock_lock);
1845
stephen hemminger553675f2013-05-16 11:35:20 +00001846 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001847 unregister_netdevice_queue(dev, head);
1848}
1849
1850static size_t vxlan_get_size(const struct net_device *dev)
1851{
1852
1853 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001854 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001855 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1856 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1857 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1858 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1859 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001860 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1861 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1862 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1863 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001864 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1865 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001866 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001867 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001868 0;
1869}
1870
1871static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1872{
1873 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001874 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001875 struct ifla_vxlan_port_range ports = {
1876 .low = htons(vxlan->port_min),
1877 .high = htons(vxlan->port_max),
1878 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001879
Atzm Watanabec7995c42013-04-16 02:50:52 +00001880 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001881 goto nla_put_failure;
1882
stephen hemminger5d174dd2013-04-27 11:31:55 +00001883 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001884 goto nla_put_failure;
1885
Atzm Watanabec7995c42013-04-16 02:50:52 +00001886 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001887 goto nla_put_failure;
1888
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001889 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001890 goto nla_put_failure;
1891
1892 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1893 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001894 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1895 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1896 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1897 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1898 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1899 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1900 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1901 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1902 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001903 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001904 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1905 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001906 goto nla_put_failure;
1907
stephen hemminger05f47d62012-10-09 20:35:50 +00001908 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1909 goto nla_put_failure;
1910
stephen hemmingerd3428942012-10-01 12:32:35 +00001911 return 0;
1912
1913nla_put_failure:
1914 return -EMSGSIZE;
1915}
1916
1917static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1918 .kind = "vxlan",
1919 .maxtype = IFLA_VXLAN_MAX,
1920 .policy = vxlan_policy,
1921 .priv_size = sizeof(struct vxlan_dev),
1922 .setup = vxlan_setup,
1923 .validate = vxlan_validate,
1924 .newlink = vxlan_newlink,
1925 .dellink = vxlan_dellink,
1926 .get_size = vxlan_get_size,
1927 .fill_info = vxlan_fill_info,
1928};
1929
1930static __net_init int vxlan_init_net(struct net *net)
1931{
1932 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001933 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001934
stephen hemminger553675f2013-05-16 11:35:20 +00001935 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001936 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001937
stephen hemminger553675f2013-05-16 11:35:20 +00001938 for (h = 0; h < PORT_HASH_SIZE; ++h)
1939 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001940
1941 return 0;
1942}
1943
1944static __net_exit void vxlan_exit_net(struct net *net)
1945{
1946 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001947 struct vxlan_dev *vxlan;
stephen hemminger372675a2013-07-18 08:38:26 -07001948 LIST_HEAD(list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001949
1950 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001951 list_for_each_entry(vxlan, &vn->vxlan_list, next)
stephen hemminger372675a2013-07-18 08:38:26 -07001952 unregister_netdevice_queue(vxlan->dev, &list);
1953 unregister_netdevice_many(&list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001954 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001955}
1956
1957static struct pernet_operations vxlan_net_ops = {
1958 .init = vxlan_init_net,
1959 .exit = vxlan_exit_net,
1960 .id = &vxlan_net_id,
1961 .size = sizeof(struct vxlan_net),
1962};
1963
1964static int __init vxlan_init_module(void)
1965{
1966 int rc;
1967
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001968 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1969 if (!vxlan_wq)
1970 return -ENOMEM;
1971
stephen hemmingerd3428942012-10-01 12:32:35 +00001972 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1973
1974 rc = register_pernet_device(&vxlan_net_ops);
1975 if (rc)
1976 goto out1;
1977
1978 rc = rtnl_link_register(&vxlan_link_ops);
1979 if (rc)
1980 goto out2;
1981
1982 return 0;
1983
1984out2:
1985 unregister_pernet_device(&vxlan_net_ops);
1986out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001987 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00001988 return rc;
1989}
Cong Wang7332a132013-05-27 22:35:53 +00001990late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00001991
1992static void __exit vxlan_cleanup_module(void)
1993{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07001994 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001995 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07001996 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00001997 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001998}
1999module_exit(vxlan_cleanup_module);
2000
2001MODULE_LICENSE("GPL");
2002MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002003MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00002004MODULE_ALIAS_RTNL_LINK("vxlan");