blob: 306bd94efa898a11366af65ef83ab38b69008932 [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * TODO
stephen hemmingerd3428942012-10-01 12:32:35 +000011 * - IPv6 (not in RFC)
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/slab.h>
21#include <linux/skbuff.h>
22#include <linux/rculist.h>
23#include <linux/netdevice.h>
24#include <linux/in.h>
25#include <linux/ip.h>
26#include <linux/udp.h>
27#include <linux/igmp.h>
28#include <linux/etherdevice.h>
29#include <linux/if_ether.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000030#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000031#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000032#include <net/arp.h>
33#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000035#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000036#include <net/icmp.h>
37#include <net/udp.h>
38#include <net/rtnetlink.h>
39#include <net/route.h>
40#include <net/dsfield.h>
41#include <net/inet_ecn.h>
42#include <net/net_namespace.h>
43#include <net/netns/generic.h>
44
45#define VXLAN_VERSION "0.1"
46
stephen hemminger553675f2013-05-16 11:35:20 +000047#define PORT_HASH_BITS 8
48#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000049#define VNI_HASH_BITS 10
50#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
51#define FDB_HASH_BITS 8
52#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
53#define FDB_AGE_DEFAULT 300 /* 5 min */
54#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
55
56#define VXLAN_N_VID (1u << 24)
57#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000058/* IP header + UDP + VXLAN + Ethernet header */
59#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
stephen hemmingerd3428942012-10-01 12:32:35 +000060
61#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
62
63/* VXLAN protocol header */
64struct vxlanhdr {
65 __be32 vx_flags;
66 __be32 vx_vni;
67};
68
stephen hemminger23c578b2013-04-27 11:31:53 +000069/* UDP port for VXLAN traffic.
70 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070071 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000072 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070073static unsigned short vxlan_port __read_mostly = 8472;
74module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000075MODULE_PARM_DESC(udp_port, "Destination UDP port");
76
77static bool log_ecn_error = true;
78module_param(log_ecn_error, bool, 0644);
79MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
80
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070081static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000082
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030083static const u8 all_zeros_mac[ETH_ALEN];
84
stephen hemminger553675f2013-05-16 11:35:20 +000085/* per UDP socket information */
86struct vxlan_sock {
87 struct hlist_node hlist;
88 struct rcu_head rcu;
89 struct work_struct del_work;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -070090 atomic_t refcnt;
stephen hemminger553675f2013-05-16 11:35:20 +000091 struct socket *sock;
stephen hemmingerd3428942012-10-01 12:32:35 +000092 struct hlist_head vni_list[VNI_HASH_SIZE];
93};
94
stephen hemminger553675f2013-05-16 11:35:20 +000095/* per-network namespace private data for this module */
96struct vxlan_net {
97 struct list_head vxlan_list;
98 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070099 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +0000100};
101
David Stevens66817122013-03-15 04:35:51 +0000102struct vxlan_rdst {
David Stevens66817122013-03-15 04:35:51 +0000103 __be32 remote_ip;
104 __be16 remote_port;
105 u32 remote_vni;
106 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700107 struct list_head list;
David Stevens66817122013-03-15 04:35:51 +0000108};
109
stephen hemmingerd3428942012-10-01 12:32:35 +0000110/* Forwarding table entry */
111struct vxlan_fdb {
112 struct hlist_node hlist; /* linked list of entries */
113 struct rcu_head rcu;
114 unsigned long updated; /* jiffies */
115 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700116 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000117 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000118 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000119 u8 eth_addr[ETH_ALEN];
120};
121
stephen hemmingerd3428942012-10-01 12:32:35 +0000122/* Pseudo network device */
123struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000124 struct hlist_node hlist; /* vni hash table */
125 struct list_head next; /* vxlan's per namespace list */
126 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000127 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000128 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000129 __be32 saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000130 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000131 __u16 port_min; /* source port range */
132 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000133 __u8 tos; /* TOS override */
134 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000135 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000136
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700137 struct work_struct sock_work;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700138 struct work_struct igmp_work;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700139
stephen hemmingerd3428942012-10-01 12:32:35 +0000140 unsigned long age_interval;
141 struct timer_list age_timer;
142 spinlock_t hash_lock;
143 unsigned int addrcnt;
144 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000145
146 struct hlist_head fdb_head[FDB_HASH_SIZE];
147};
148
David Stevense4f67ad2012-11-20 02:50:14 +0000149#define VXLAN_F_LEARN 0x01
150#define VXLAN_F_PROXY 0x02
151#define VXLAN_F_RSC 0x04
152#define VXLAN_F_L2MISS 0x08
153#define VXLAN_F_L3MISS 0x10
154
stephen hemmingerd3428942012-10-01 12:32:35 +0000155/* salt for hash table */
156static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700157static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000158
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700159static void vxlan_sock_work(struct work_struct *work);
160
stephen hemminger553675f2013-05-16 11:35:20 +0000161/* Virtual Network hash table head */
162static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
163{
164 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
165}
166
167/* Socket hash table head */
168static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000169{
170 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
171
stephen hemminger553675f2013-05-16 11:35:20 +0000172 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
173}
174
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700175/* First remote destination for a forwarding entry.
176 * Guaranteed to be non-NULL because remotes are never deleted.
177 */
178static inline struct vxlan_rdst *first_remote(struct vxlan_fdb *fdb)
179{
180 return list_first_or_null_rcu(&fdb->remotes, struct vxlan_rdst, list);
181}
182
stephen hemminger553675f2013-05-16 11:35:20 +0000183/* Find VXLAN socket based on network namespace and UDP port */
184static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
185{
186 struct vxlan_sock *vs;
187
188 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
189 if (inet_sk(vs->sock->sk)->inet_sport == port)
190 return vs;
191 }
192 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000193}
194
195/* Look up VNI in a per net namespace table */
stephen hemminger553675f2013-05-16 11:35:20 +0000196static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000197{
stephen hemminger553675f2013-05-16 11:35:20 +0000198 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000199 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000200
stephen hemminger553675f2013-05-16 11:35:20 +0000201 vs = vxlan_find_port(net, port);
202 if (!vs)
203 return NULL;
204
205 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000206 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000207 return vxlan;
208 }
209
210 return NULL;
211}
212
213/* Fill in neighbour message in skbuff. */
214static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700215 const struct vxlan_fdb *fdb,
216 u32 portid, u32 seq, int type, unsigned int flags,
217 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000218{
219 unsigned long now = jiffies;
220 struct nda_cacheinfo ci;
221 struct nlmsghdr *nlh;
222 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000223 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000224
225 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
226 if (nlh == NULL)
227 return -EMSGSIZE;
228
229 ndm = nlmsg_data(nlh);
230 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000231
232 send_eth = send_ip = true;
233
234 if (type == RTM_GETNEIGH) {
235 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000236 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000237 send_eth = !is_zero_ether_addr(fdb->eth_addr);
238 } else
239 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000240 ndm->ndm_state = fdb->state;
241 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000242 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000243 ndm->ndm_type = NDA_DST;
244
David Stevense4f67ad2012-11-20 02:50:14 +0000245 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000246 goto nla_put_failure;
247
David Stevens66817122013-03-15 04:35:51 +0000248 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
249 goto nla_put_failure;
250
stephen hemminger823aa872013-04-27 11:31:57 +0000251 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000252 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
253 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000254 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700255 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000256 goto nla_put_failure;
257 if (rdst->remote_ifindex &&
258 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000259 goto nla_put_failure;
260
261 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
262 ci.ndm_confirmed = 0;
263 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
264 ci.ndm_refcnt = 0;
265
266 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
267 goto nla_put_failure;
268
269 return nlmsg_end(skb, nlh);
270
271nla_put_failure:
272 nlmsg_cancel(skb, nlh);
273 return -EMSGSIZE;
274}
275
276static inline size_t vxlan_nlmsg_size(void)
277{
278 return NLMSG_ALIGN(sizeof(struct ndmsg))
279 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
280 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000281 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000282 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
283 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000284 + nla_total_size(sizeof(struct nda_cacheinfo));
285}
286
287static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700288 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000289{
290 struct net *net = dev_net(vxlan->dev);
291 struct sk_buff *skb;
292 int err = -ENOBUFS;
293
294 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
295 if (skb == NULL)
296 goto errout;
297
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700298 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, first_remote(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000299 if (err < 0) {
300 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
301 WARN_ON(err == -EMSGSIZE);
302 kfree_skb(skb);
303 goto errout;
304 }
305
306 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
307 return;
308errout:
309 if (err < 0)
310 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
311}
312
David Stevense4f67ad2012-11-20 02:50:14 +0000313static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
314{
315 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700316 struct vxlan_fdb f = {
317 .state = NUD_STALE,
318 };
319 struct vxlan_rdst remote = {
320 .remote_ip = ipa, /* goes to NDA_DST */
321 .remote_vni = VXLAN_N_VID,
322 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700323
324 INIT_LIST_HEAD(&f.remotes);
325 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000326
327 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
328}
329
330static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
331{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700332 struct vxlan_fdb f = {
333 .state = NUD_STALE,
334 };
David Stevense4f67ad2012-11-20 02:50:14 +0000335
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700336 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000337 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
338
339 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
340}
341
stephen hemmingerd3428942012-10-01 12:32:35 +0000342/* Hash Ethernet address */
343static u32 eth_hash(const unsigned char *addr)
344{
345 u64 value = get_unaligned((u64 *)addr);
346
347 /* only want 6 bytes */
348#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000349 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000350#else
351 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000352#endif
353 return hash_64(value, FDB_HASH_BITS);
354}
355
356/* Hash chain to use given mac address */
357static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
358 const u8 *mac)
359{
360 return &vxlan->fdb_head[eth_hash(mac)];
361}
362
363/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000364static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000365 const u8 *mac)
366
367{
368 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
369 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000370
Sasha Levinb67bfe02013-02-27 17:06:00 -0800371 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000372 if (compare_ether_addr(mac, f->eth_addr) == 0)
373 return f;
374 }
375
376 return NULL;
377}
378
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000379static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
380 const u8 *mac)
381{
382 struct vxlan_fdb *f;
383
384 f = __vxlan_find_mac(vxlan, mac);
385 if (f)
386 f->used = jiffies;
387
388 return f;
389}
390
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300391/* caller should hold vxlan->hash_lock */
392static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
393 __be32 ip, __be16 port,
394 __u32 vni, __u32 ifindex)
395{
396 struct vxlan_rdst *rd;
397
398 list_for_each_entry(rd, &f->remotes, list) {
399 if (rd->remote_ip == ip &&
400 rd->remote_port == port &&
401 rd->remote_vni == vni &&
402 rd->remote_ifindex == ifindex)
403 return rd;
404 }
405
406 return NULL;
407}
408
David Stevens66817122013-03-15 04:35:51 +0000409/* Add/update destinations for multicast */
410static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000411 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000412{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700413 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000414
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300415 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
416 if (rd)
417 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700418
David Stevens66817122013-03-15 04:35:51 +0000419 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
420 if (rd == NULL)
421 return -ENOBUFS;
422 rd->remote_ip = ip;
423 rd->remote_port = port;
424 rd->remote_vni = vni;
425 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700426
427 list_add_tail_rcu(&rd->list, &f->remotes);
428
David Stevens66817122013-03-15 04:35:51 +0000429 return 1;
430}
431
stephen hemmingerd3428942012-10-01 12:32:35 +0000432/* Add new entry to forwarding table -- assumes lock held */
433static int vxlan_fdb_create(struct vxlan_dev *vxlan,
434 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000435 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000436 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000437 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000438{
439 struct vxlan_fdb *f;
440 int notify = 0;
441
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000442 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000443 if (f) {
444 if (flags & NLM_F_EXCL) {
445 netdev_dbg(vxlan->dev,
446 "lost race to create %pM\n", mac);
447 return -EEXIST;
448 }
449 if (f->state != state) {
450 f->state = state;
451 f->updated = jiffies;
452 notify = 1;
453 }
David Stevensae884082013-04-19 00:36:26 +0000454 if (f->flags != ndm_flags) {
455 f->flags = ndm_flags;
456 f->updated = jiffies;
457 notify = 1;
458 }
David Stevens66817122013-03-15 04:35:51 +0000459 if ((flags & NLM_F_APPEND) &&
460 is_multicast_ether_addr(f->eth_addr)) {
461 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
462
463 if (rc < 0)
464 return rc;
465 notify |= rc;
466 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000467 } else {
468 if (!(flags & NLM_F_CREATE))
469 return -ENOENT;
470
471 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
472 return -ENOSPC;
473
474 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
475 f = kmalloc(sizeof(*f), GFP_ATOMIC);
476 if (!f)
477 return -ENOMEM;
478
479 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000480 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000481 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000482 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700483 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000484 memcpy(f->eth_addr, mac, ETH_ALEN);
485
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700486 vxlan_fdb_append(f, ip, port, vni, ifindex);
487
stephen hemmingerd3428942012-10-01 12:32:35 +0000488 ++vxlan->addrcnt;
489 hlist_add_head_rcu(&f->hlist,
490 vxlan_fdb_head(vxlan, mac));
491 }
492
493 if (notify)
494 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
495
496 return 0;
497}
498
Wei Yongjun6706c822013-04-11 19:00:35 +0000499static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000500{
501 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700502 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000503
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700504 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000505 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000506 kfree(f);
507}
508
stephen hemmingerd3428942012-10-01 12:32:35 +0000509static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
510{
511 netdev_dbg(vxlan->dev,
512 "delete %pM\n", f->eth_addr);
513
514 --vxlan->addrcnt;
515 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
516
517 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000518 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000519}
520
521/* Add static entry (via netlink) */
522static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
523 struct net_device *dev,
524 const unsigned char *addr, u16 flags)
525{
526 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens66817122013-03-15 04:35:51 +0000527 struct net *net = dev_net(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000528 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000529 __be16 port;
530 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000531 int err;
532
533 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
534 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
535 ndm->ndm_state);
536 return -EINVAL;
537 }
538
539 if (tb[NDA_DST] == NULL)
540 return -EINVAL;
541
542 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
543 return -EAFNOSUPPORT;
544
545 ip = nla_get_be32(tb[NDA_DST]);
546
David Stevens66817122013-03-15 04:35:51 +0000547 if (tb[NDA_PORT]) {
stephen hemminger73cf3312013-04-27 11:31:54 +0000548 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
David Stevens66817122013-03-15 04:35:51 +0000549 return -EINVAL;
stephen hemminger73cf3312013-04-27 11:31:54 +0000550 port = nla_get_be16(tb[NDA_PORT]);
David Stevens66817122013-03-15 04:35:51 +0000551 } else
stephen hemminger823aa872013-04-27 11:31:57 +0000552 port = vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +0000553
554 if (tb[NDA_VNI]) {
555 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
556 return -EINVAL;
557 vni = nla_get_u32(tb[NDA_VNI]);
558 } else
Atzm Watanabec7995c42013-04-16 02:50:52 +0000559 vni = vxlan->default_dst.remote_vni;
David Stevens66817122013-03-15 04:35:51 +0000560
561 if (tb[NDA_IFINDEX]) {
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000562 struct net_device *tdev;
David Stevens66817122013-03-15 04:35:51 +0000563
564 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
565 return -EINVAL;
566 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000567 tdev = dev_get_by_index(net, ifindex);
568 if (!tdev)
David Stevens66817122013-03-15 04:35:51 +0000569 return -EADDRNOTAVAIL;
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000570 dev_put(tdev);
David Stevens66817122013-03-15 04:35:51 +0000571 } else
572 ifindex = 0;
573
stephen hemmingerd3428942012-10-01 12:32:35 +0000574 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000575 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
576 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000577 spin_unlock_bh(&vxlan->hash_lock);
578
579 return err;
580}
581
582/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000583static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
584 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000585 const unsigned char *addr)
586{
587 struct vxlan_dev *vxlan = netdev_priv(dev);
588 struct vxlan_fdb *f;
589 int err = -ENOENT;
590
591 spin_lock_bh(&vxlan->hash_lock);
592 f = vxlan_find_mac(vxlan, addr);
593 if (f) {
594 vxlan_fdb_destroy(vxlan, f);
595 err = 0;
596 }
597 spin_unlock_bh(&vxlan->hash_lock);
598
599 return err;
600}
601
602/* Dump forwarding table */
603static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
604 struct net_device *dev, int idx)
605{
606 struct vxlan_dev *vxlan = netdev_priv(dev);
607 unsigned int h;
608
609 for (h = 0; h < FDB_HASH_SIZE; ++h) {
610 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000611 int err;
612
Sasha Levinb67bfe02013-02-27 17:06:00 -0800613 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000614 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000615
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700616 if (idx < cb->args[0])
617 goto skip;
618
619 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000620 err = vxlan_fdb_info(skb, vxlan, f,
621 NETLINK_CB(cb->skb).portid,
622 cb->nlh->nlmsg_seq,
623 RTM_NEWNEIGH,
624 NLM_F_MULTI, rd);
625 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700626 goto out;
David Stevens66817122013-03-15 04:35:51 +0000627 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700628skip:
629 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000630 }
631 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700632out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000633 return idx;
634}
635
636/* Watch incoming packets to learn mapping between Ethernet address
637 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700638 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000639 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700640static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000641 __be32 src_ip, const u8 *src_mac)
642{
643 struct vxlan_dev *vxlan = netdev_priv(dev);
644 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000645
646 f = vxlan_find_mac(vxlan, src_mac);
647 if (likely(f)) {
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700648 struct vxlan_rdst *rdst = first_remote(f);
649
650 if (likely(rdst->remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700651 return false;
652
653 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700654 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700655 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000656
657 if (net_ratelimit())
658 netdev_info(dev,
659 "%pM migrated from %pI4 to %pI4\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700660 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000661
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700662 rdst->remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000663 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700664 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000665 } else {
666 /* learned new entry */
667 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700668
669 /* close off race between vxlan_flush and incoming packets */
670 if (netif_running(dev))
671 vxlan_fdb_create(vxlan, src_mac, src_ip,
672 NUD_REACHABLE,
673 NLM_F_EXCL|NLM_F_CREATE,
674 vxlan->dst_port,
675 vxlan->default_dst.remote_vni,
676 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000677 spin_unlock(&vxlan->hash_lock);
678 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700679
680 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000681}
682
683
684/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700685static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000686{
stephen hemminger553675f2013-05-16 11:35:20 +0000687 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000688
stephen hemminger553675f2013-05-16 11:35:20 +0000689 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000690 if (!netif_running(vxlan->dev))
691 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000692
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700693 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000694 return true;
695 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000696
697 return false;
698}
699
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700700static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000701{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700702 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000703}
704
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700705static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000706{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700707 if (!atomic_dec_and_test(&vs->refcnt))
708 return;
709
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700710 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700711 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700712 spin_unlock(&vn->sock_lock);
713
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700714 queue_work(vxlan_wq, &vs->del_work);
715}
716
717/* Callback to update multicast group membership.
718 * Scheduled when vxlan goes up/down.
719 */
720static void vxlan_igmp_work(struct work_struct *work)
721{
722 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_work);
723 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
724 struct vxlan_sock *vs = vxlan->vn_sock;
725 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000726 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000727 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
728 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000729 };
730
stephen hemmingerd3428942012-10-01 12:32:35 +0000731 lock_sock(sk);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700732 if (vxlan_group_used(vn, vxlan->default_dst.remote_ip))
733 ip_mc_join_group(sk, &mreq);
734 else
735 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000736 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000737
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700738 vxlan_sock_release(vn, vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700739 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000740}
741
742/* Callback from net/ipv4/udp.c to receive packets */
743static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
744{
745 struct iphdr *oip;
746 struct vxlanhdr *vxh;
747 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000748 struct pcpu_tstats *stats;
stephen hemminger553675f2013-05-16 11:35:20 +0000749 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000750 __u32 vni;
751 int err;
752
753 /* pop off outer UDP header */
754 __skb_pull(skb, sizeof(struct udphdr));
755
756 /* Need Vxlan and inner Ethernet header to be present */
757 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
758 goto error;
759
760 /* Drop packets with reserved bits set */
761 vxh = (struct vxlanhdr *) skb->data;
762 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
763 (vxh->vx_vni & htonl(0xff))) {
764 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
765 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
766 goto error;
767 }
768
769 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000770
771 /* Is this VNI defined? */
772 vni = ntohl(vxh->vx_vni) >> 8;
stephen hemminger553675f2013-05-16 11:35:20 +0000773 port = inet_sk(sk)->inet_sport;
774 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000775 if (!vxlan) {
stephen hemminger553675f2013-05-16 11:35:20 +0000776 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
777 vni, ntohs(port));
stephen hemmingerd3428942012-10-01 12:32:35 +0000778 goto drop;
779 }
780
781 if (!pskb_may_pull(skb, ETH_HLEN)) {
782 vxlan->dev->stats.rx_length_errors++;
783 vxlan->dev->stats.rx_errors++;
784 goto drop;
785 }
786
David Stevense4f67ad2012-11-20 02:50:14 +0000787 skb_reset_mac_header(skb);
788
stephen hemmingerd3428942012-10-01 12:32:35 +0000789 /* Re-examine inner Ethernet packet */
790 oip = ip_hdr(skb);
791 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000792
793 /* Ignore packet loops (and multicast echo) */
794 if (compare_ether_addr(eth_hdr(skb)->h_source,
795 vxlan->dev->dev_addr) == 0)
796 goto drop;
797
stephen hemminger26a41ae62013-06-17 12:09:58 -0700798 if ((vxlan->flags & VXLAN_F_LEARN) &&
799 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
800 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000801
802 __skb_tunnel_rx(skb, vxlan->dev);
803 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000804
805 /* If the NIC driver gave us an encapsulated packet with
806 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
807 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
808 * for us. Otherwise force the upper layers to verify it.
809 */
810 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
811 !(vxlan->dev->features & NETIF_F_RXCSUM))
812 skb->ip_summed = CHECKSUM_NONE;
813
814 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000815
816 err = IP_ECN_decapsulate(oip, skb);
817 if (unlikely(err)) {
818 if (log_ecn_error)
819 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
820 &oip->saddr, oip->tos);
821 if (err > 1) {
822 ++vxlan->dev->stats.rx_frame_errors;
823 ++vxlan->dev->stats.rx_errors;
824 goto drop;
825 }
826 }
827
Pravin B Shelare8171042013-03-25 14:49:46 +0000828 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000829 u64_stats_update_begin(&stats->syncp);
830 stats->rx_packets++;
831 stats->rx_bytes += skb->len;
832 u64_stats_update_end(&stats->syncp);
833
834 netif_rx(skb);
835
836 return 0;
837error:
838 /* Put UDP header back */
839 __skb_push(skb, sizeof(struct udphdr));
840
841 return 1;
842drop:
843 /* Consume bad packet */
844 kfree_skb(skb);
845 return 0;
846}
847
David Stevense4f67ad2012-11-20 02:50:14 +0000848static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
849{
850 struct vxlan_dev *vxlan = netdev_priv(dev);
851 struct arphdr *parp;
852 u8 *arpptr, *sha;
853 __be32 sip, tip;
854 struct neighbour *n;
855
856 if (dev->flags & IFF_NOARP)
857 goto out;
858
859 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
860 dev->stats.tx_dropped++;
861 goto out;
862 }
863 parp = arp_hdr(skb);
864
865 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
866 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
867 parp->ar_pro != htons(ETH_P_IP) ||
868 parp->ar_op != htons(ARPOP_REQUEST) ||
869 parp->ar_hln != dev->addr_len ||
870 parp->ar_pln != 4)
871 goto out;
872 arpptr = (u8 *)parp + sizeof(struct arphdr);
873 sha = arpptr;
874 arpptr += dev->addr_len; /* sha */
875 memcpy(&sip, arpptr, sizeof(sip));
876 arpptr += sizeof(sip);
877 arpptr += dev->addr_len; /* tha */
878 memcpy(&tip, arpptr, sizeof(tip));
879
880 if (ipv4_is_loopback(tip) ||
881 ipv4_is_multicast(tip))
882 goto out;
883
884 n = neigh_lookup(&arp_tbl, &tip, dev);
885
886 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000887 struct vxlan_fdb *f;
888 struct sk_buff *reply;
889
890 if (!(n->nud_state & NUD_CONNECTED)) {
891 neigh_release(n);
892 goto out;
893 }
894
895 f = vxlan_find_mac(vxlan, n->ha);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700896 if (f && first_remote(f)->remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000897 /* bridge-local neighbor */
898 neigh_release(n);
899 goto out;
900 }
901
902 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
903 n->ha, sha);
904
905 neigh_release(n);
906
907 skb_reset_mac_header(reply);
908 __skb_pull(reply, skb_network_offset(reply));
909 reply->ip_summed = CHECKSUM_UNNECESSARY;
910 reply->pkt_type = PACKET_HOST;
911
912 if (netif_rx_ni(reply) == NET_RX_DROP)
913 dev->stats.rx_dropped++;
914 } else if (vxlan->flags & VXLAN_F_L3MISS)
915 vxlan_ip_miss(dev, tip);
916out:
917 consume_skb(skb);
918 return NETDEV_TX_OK;
919}
920
921static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
922{
923 struct vxlan_dev *vxlan = netdev_priv(dev);
924 struct neighbour *n;
925 struct iphdr *pip;
926
927 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
928 return false;
929
930 n = NULL;
931 switch (ntohs(eth_hdr(skb)->h_proto)) {
932 case ETH_P_IP:
933 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
934 return false;
935 pip = ip_hdr(skb);
936 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
937 break;
938 default:
939 return false;
940 }
941
942 if (n) {
943 bool diff;
944
945 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
946 if (diff) {
947 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
948 dev->addr_len);
949 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
950 }
951 neigh_release(n);
952 return diff;
953 } else if (vxlan->flags & VXLAN_F_L3MISS)
954 vxlan_ip_miss(dev, pip->daddr);
955 return false;
956}
957
stephen hemminger553675f2013-05-16 11:35:20 +0000958static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +0000959{
960 sock_put(skb->sk);
961}
962
963/* On transmit, associate with the tunnel socket */
964static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
965{
stephen hemminger553675f2013-05-16 11:35:20 +0000966 struct vxlan_dev *vxlan = netdev_priv(dev);
967 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +0000968
969 skb_orphan(skb);
970 sock_hold(sk);
971 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +0000972 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +0000973}
974
stephen hemminger05f47d62012-10-09 20:35:50 +0000975/* Compute source port for outgoing packet
976 * first choice to use L4 flow hash since it will spread
977 * better and maybe available from hardware
978 * secondary choice is to use jhash on the Ethernet header
979 */
stephen hemminger7d836a72013-04-27 11:31:56 +0000980static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +0000981{
982 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
983 u32 hash;
984
985 hash = skb_get_rxhash(skb);
986 if (!hash)
987 hash = jhash(skb->data, 2 * ETH_ALEN,
988 (__force u32) skb->protocol);
989
stephen hemminger7d836a72013-04-27 11:31:56 +0000990 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +0000991}
992
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000993static int handle_offloads(struct sk_buff *skb)
994{
995 if (skb_is_gso(skb)) {
996 int err = skb_unclone(skb, GFP_ATOMIC);
997 if (unlikely(err))
998 return err;
999
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001000 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001001 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1002 skb->ip_summed = CHECKSUM_NONE;
1003
1004 return 0;
1005}
1006
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001007/* Bypass encapsulation if the destination is local */
1008static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1009 struct vxlan_dev *dst_vxlan)
1010{
1011 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1012 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1013
1014 skb->pkt_type = PACKET_HOST;
1015 skb->encapsulation = 0;
1016 skb->dev = dst_vxlan->dev;
1017 __skb_pull(skb, skb_network_offset(skb));
1018
1019 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +00001020 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1021 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001022
1023 u64_stats_update_begin(&tx_stats->syncp);
1024 tx_stats->tx_packets++;
1025 tx_stats->tx_bytes += skb->len;
1026 u64_stats_update_end(&tx_stats->syncp);
1027
1028 if (netif_rx(skb) == NET_RX_SUCCESS) {
1029 u64_stats_update_begin(&rx_stats->syncp);
1030 rx_stats->rx_packets++;
1031 rx_stats->rx_bytes += skb->len;
1032 u64_stats_update_end(&rx_stats->syncp);
1033 } else {
1034 skb->dev->stats.rx_dropped++;
1035 }
1036}
1037
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001038static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1039 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001040{
1041 struct vxlan_dev *vxlan = netdev_priv(dev);
1042 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001043 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001044 struct vxlanhdr *vxh;
1045 struct udphdr *uh;
1046 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001047 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001048 __be16 src_port, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001049 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001050 __be16 df = 0;
1051 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001052 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001053
stephen hemminger823aa872013-04-27 11:31:57 +00001054 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001055 vni = rdst->remote_vni;
1056 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001057
1058 if (!dst) {
1059 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001060 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001061 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001062 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001063 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001064 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001065 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001066
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001067 if (!skb->encapsulation) {
1068 skb_reset_inner_headers(skb);
1069 skb->encapsulation = 1;
1070 }
1071
stephen hemmingerd3428942012-10-01 12:32:35 +00001072 /* Need space for new headers (invalidates iph ptr) */
1073 if (skb_cow_head(skb, VXLAN_HEADROOM))
1074 goto drop;
1075
stephen hemmingerd3428942012-10-01 12:32:35 +00001076 old_iph = ip_hdr(skb);
1077
stephen hemmingerd3428942012-10-01 12:32:35 +00001078 ttl = vxlan->ttl;
1079 if (!ttl && IN_MULTICAST(ntohl(dst)))
1080 ttl = 1;
1081
1082 tos = vxlan->tos;
1083 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001084 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001085
stephen hemminger05f47d62012-10-09 20:35:50 +00001086 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001087
stephen hemmingerca78f182012-10-09 20:35:48 +00001088 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001089 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001090 fl4.flowi4_tos = RT_TOS(tos);
1091 fl4.daddr = dst;
1092 fl4.saddr = vxlan->saddr;
1093
1094 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001095 if (IS_ERR(rt)) {
1096 netdev_dbg(dev, "no route to %pI4\n", &dst);
1097 dev->stats.tx_carrier_errors++;
1098 goto tx_error;
1099 }
1100
1101 if (rt->dst.dev == dev) {
1102 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1103 ip_rt_put(rt);
1104 dev->stats.collisions++;
1105 goto tx_error;
1106 }
1107
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001108 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001109 if (rt->rt_flags & RTCF_LOCAL &&
1110 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001111 struct vxlan_dev *dst_vxlan;
1112
1113 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001114 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001115 if (!dst_vxlan)
1116 goto tx_error;
1117 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001118 return;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001119 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001120 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1121 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001122 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001123
1124 __skb_push(skb, sizeof(*uh));
1125 skb_reset_transport_header(skb);
1126 uh = udp_hdr(skb);
1127
stephen hemminger73cf3312013-04-27 11:31:54 +00001128 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001129 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001130
1131 uh->len = htons(skb->len);
1132 uh->check = 0;
1133
stephen hemminger1cad8712012-10-09 20:35:49 +00001134 vxlan_set_owner(dev, skb);
1135
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001136 if (handle_offloads(skb))
1137 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001138
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001139 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1140 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1141
1142 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1143 IPPROTO_UDP, tos, ttl, df);
1144 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1145
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001146 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001147
1148drop:
1149 dev->stats.tx_dropped++;
1150 goto tx_free;
1151
1152tx_error:
1153 dev->stats.tx_errors++;
1154tx_free:
1155 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001156}
1157
David Stevens66817122013-03-15 04:35:51 +00001158/* Transmit local packets over Vxlan
1159 *
1160 * Outer IP header inherits ECN and DF from inner header.
1161 * Outer UDP destination is the VXLAN assigned port.
1162 * source port is based on hash of flow
1163 */
1164static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1165{
1166 struct vxlan_dev *vxlan = netdev_priv(dev);
1167 struct ethhdr *eth;
1168 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001169 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001170 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001171
1172 skb_reset_mac_header(skb);
1173 eth = eth_hdr(skb);
1174
1175 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1176 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001177
1178 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001179 did_rsc = false;
1180
1181 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1182 ntohs(eth->h_proto) == ETH_P_IP) {
1183 did_rsc = route_shortcircuit(dev, skb);
1184 if (did_rsc)
1185 f = vxlan_find_mac(vxlan, eth->h_dest);
1186 }
1187
David Stevens66817122013-03-15 04:35:51 +00001188 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001189 f = vxlan_find_mac(vxlan, all_zeros_mac);
1190 if (f == NULL) {
1191 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1192 !is_multicast_ether_addr(eth->h_dest))
1193 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001194
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001195 dev->stats.tx_dropped++;
1196 dev_kfree_skb(skb);
1197 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001198 }
David Stevens66817122013-03-15 04:35:51 +00001199 }
1200
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001201 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1202 struct sk_buff *skb1;
1203
1204 skb1 = skb_clone(skb, GFP_ATOMIC);
1205 if (skb1)
1206 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1207 }
1208
1209 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001210 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001211}
1212
stephen hemmingerd3428942012-10-01 12:32:35 +00001213/* Walk the forwarding table and purge stale entries */
1214static void vxlan_cleanup(unsigned long arg)
1215{
1216 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1217 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1218 unsigned int h;
1219
1220 if (!netif_running(vxlan->dev))
1221 return;
1222
1223 spin_lock_bh(&vxlan->hash_lock);
1224 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1225 struct hlist_node *p, *n;
1226 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1227 struct vxlan_fdb *f
1228 = container_of(p, struct vxlan_fdb, hlist);
1229 unsigned long timeout;
1230
stephen hemminger3c172862012-10-26 06:24:34 +00001231 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001232 continue;
1233
1234 timeout = f->used + vxlan->age_interval * HZ;
1235 if (time_before_eq(timeout, jiffies)) {
1236 netdev_dbg(vxlan->dev,
1237 "garbage collect %pM\n",
1238 f->eth_addr);
1239 f->state = NUD_STALE;
1240 vxlan_fdb_destroy(vxlan, f);
1241 } else if (time_before(timeout, next_timer))
1242 next_timer = timeout;
1243 }
1244 }
1245 spin_unlock_bh(&vxlan->hash_lock);
1246
1247 mod_timer(&vxlan->age_timer, next_timer);
1248}
1249
1250/* Setup stats when device is created */
1251static int vxlan_init(struct net_device *dev)
1252{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001253 struct vxlan_dev *vxlan = netdev_priv(dev);
1254 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1255 struct vxlan_sock *vs;
1256 __u32 vni = vxlan->default_dst.remote_vni;
1257
Pravin B Shelare8171042013-03-25 14:49:46 +00001258 dev->tstats = alloc_percpu(struct pcpu_tstats);
1259 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001260 return -ENOMEM;
1261
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001262 spin_lock(&vn->sock_lock);
1263 vs = vxlan_find_port(dev_net(dev), vxlan->dst_port);
1264 if (vs) {
1265 /* If we have a socket with same port already, reuse it */
1266 atomic_inc(&vs->refcnt);
1267 vxlan->vn_sock = vs;
1268 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1269 } else {
1270 /* otherwise make new socket outside of RTNL */
1271 dev_hold(dev);
1272 queue_work(vxlan_wq, &vxlan->sock_work);
1273 }
1274 spin_unlock(&vn->sock_lock);
1275
stephen hemmingerd3428942012-10-01 12:32:35 +00001276 return 0;
1277}
1278
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001279static void vxlan_fdb_delete_defualt(struct vxlan_dev *vxlan)
1280{
1281 struct vxlan_fdb *f;
1282
1283 spin_lock_bh(&vxlan->hash_lock);
1284 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1285 if (f)
1286 vxlan_fdb_destroy(vxlan, f);
1287 spin_unlock_bh(&vxlan->hash_lock);
1288}
1289
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001290static void vxlan_uninit(struct net_device *dev)
1291{
1292 struct vxlan_dev *vxlan = netdev_priv(dev);
1293 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1294 struct vxlan_sock *vs = vxlan->vn_sock;
1295
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001296 vxlan_fdb_delete_defualt(vxlan);
1297
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001298 if (vs)
1299 vxlan_sock_release(vn, vs);
1300 free_percpu(dev->tstats);
1301}
1302
stephen hemmingerd3428942012-10-01 12:32:35 +00001303/* Start ageing timer and join group when device is brought up */
1304static int vxlan_open(struct net_device *dev)
1305{
1306 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001307 struct vxlan_sock *vs = vxlan->vn_sock;
1308
1309 /* socket hasn't been created */
1310 if (!vs)
1311 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001312
Atzm Watanabec7995c42013-04-16 02:50:52 +00001313 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001314 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001315 dev_hold(dev);
1316 queue_work(vxlan_wq, &vxlan->igmp_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001317 }
1318
1319 if (vxlan->age_interval)
1320 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1321
1322 return 0;
1323}
1324
1325/* Purge the forwarding table */
1326static void vxlan_flush(struct vxlan_dev *vxlan)
1327{
Cong Wang31fec5a2013-05-27 22:35:52 +00001328 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001329
1330 spin_lock_bh(&vxlan->hash_lock);
1331 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1332 struct hlist_node *p, *n;
1333 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1334 struct vxlan_fdb *f
1335 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001336 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1337 if (!is_zero_ether_addr(f->eth_addr))
1338 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001339 }
1340 }
1341 spin_unlock_bh(&vxlan->hash_lock);
1342}
1343
1344/* Cleanup timer and forwarding table on shutdown */
1345static int vxlan_stop(struct net_device *dev)
1346{
1347 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001348 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001349
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001350 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1351 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001352 dev_hold(dev);
1353 queue_work(vxlan_wq, &vxlan->igmp_work);
1354 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001355
1356 del_timer_sync(&vxlan->age_timer);
1357
1358 vxlan_flush(vxlan);
1359
1360 return 0;
1361}
1362
stephen hemmingerd3428942012-10-01 12:32:35 +00001363/* Stub, nothing needs to be done. */
1364static void vxlan_set_multicast_list(struct net_device *dev)
1365{
1366}
1367
1368static const struct net_device_ops vxlan_netdev_ops = {
1369 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001370 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001371 .ndo_open = vxlan_open,
1372 .ndo_stop = vxlan_stop,
1373 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001374 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001375 .ndo_set_rx_mode = vxlan_set_multicast_list,
1376 .ndo_change_mtu = eth_change_mtu,
1377 .ndo_validate_addr = eth_validate_addr,
1378 .ndo_set_mac_address = eth_mac_addr,
1379 .ndo_fdb_add = vxlan_fdb_add,
1380 .ndo_fdb_del = vxlan_fdb_delete,
1381 .ndo_fdb_dump = vxlan_fdb_dump,
1382};
1383
1384/* Info for udev, that this is a virtual tunnel endpoint */
1385static struct device_type vxlan_type = {
1386 .name = "vxlan",
1387};
1388
stephen hemmingerd3428942012-10-01 12:32:35 +00001389/* Initialize the device structure. */
1390static void vxlan_setup(struct net_device *dev)
1391{
1392 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001393 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001394 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001395
1396 eth_hw_addr_random(dev);
1397 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001398 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001399
1400 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001401 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001402 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1403
1404 dev->tx_queue_len = 0;
1405 dev->features |= NETIF_F_LLTX;
1406 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001407 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001408 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001409 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001410
1411 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001412 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001413 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001414 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001415
stephen hemminger553675f2013-05-16 11:35:20 +00001416 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001417 spin_lock_init(&vxlan->hash_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001418 INIT_WORK(&vxlan->igmp_work, vxlan_igmp_work);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001419 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001420
1421 init_timer_deferrable(&vxlan->age_timer);
1422 vxlan->age_timer.function = vxlan_cleanup;
1423 vxlan->age_timer.data = (unsigned long) vxlan;
1424
stephen hemminger05f47d62012-10-09 20:35:50 +00001425 inet_get_local_port_range(&low, &high);
1426 vxlan->port_min = low;
1427 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001428 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001429
stephen hemmingerd3428942012-10-01 12:32:35 +00001430 vxlan->dev = dev;
1431
1432 for (h = 0; h < FDB_HASH_SIZE; ++h)
1433 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1434}
1435
1436static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1437 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001438 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001439 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1440 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1441 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1442 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1443 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1444 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1445 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001446 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001447 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1448 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1449 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1450 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001451 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001452};
1453
1454static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1455{
1456 if (tb[IFLA_ADDRESS]) {
1457 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1458 pr_debug("invalid link address (not ethernet)\n");
1459 return -EINVAL;
1460 }
1461
1462 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1463 pr_debug("invalid all zero ethernet address\n");
1464 return -EADDRNOTAVAIL;
1465 }
1466 }
1467
1468 if (!data)
1469 return -EINVAL;
1470
1471 if (data[IFLA_VXLAN_ID]) {
1472 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1473 if (id >= VXLAN_VID_MASK)
1474 return -ERANGE;
1475 }
1476
stephen hemminger05f47d62012-10-09 20:35:50 +00001477 if (data[IFLA_VXLAN_PORT_RANGE]) {
1478 const struct ifla_vxlan_port_range *p
1479 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1480
1481 if (ntohs(p->high) < ntohs(p->low)) {
1482 pr_debug("port range %u .. %u not valid\n",
1483 ntohs(p->low), ntohs(p->high));
1484 return -EINVAL;
1485 }
1486 }
1487
stephen hemmingerd3428942012-10-01 12:32:35 +00001488 return 0;
1489}
1490
Yan Burman1b13c972013-01-29 23:43:07 +00001491static void vxlan_get_drvinfo(struct net_device *netdev,
1492 struct ethtool_drvinfo *drvinfo)
1493{
1494 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1495 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1496}
1497
1498static const struct ethtool_ops vxlan_ethtool_ops = {
1499 .get_drvinfo = vxlan_get_drvinfo,
1500 .get_link = ethtool_op_get_link,
1501};
1502
stephen hemminger553675f2013-05-16 11:35:20 +00001503static void vxlan_del_work(struct work_struct *work)
1504{
1505 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1506
1507 sk_release_kernel(vs->sock->sk);
1508 kfree_rcu(vs, rcu);
1509}
1510
stephen hemminger553675f2013-05-16 11:35:20 +00001511static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1512{
1513 struct vxlan_sock *vs;
1514 struct sock *sk;
1515 struct sockaddr_in vxlan_addr = {
1516 .sin_family = AF_INET,
1517 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07001518 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00001519 };
1520 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001521 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001522
1523 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1524 if (!vs)
1525 return ERR_PTR(-ENOMEM);
1526
1527 for (h = 0; h < VNI_HASH_SIZE; ++h)
1528 INIT_HLIST_HEAD(&vs->vni_list[h]);
1529
1530 INIT_WORK(&vs->del_work, vxlan_del_work);
1531
1532 /* Create UDP socket for encapsulation receive. */
1533 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1534 if (rc < 0) {
1535 pr_debug("UDP socket create failed\n");
1536 kfree(vs);
1537 return ERR_PTR(rc);
1538 }
1539
1540 /* Put in proper namespace */
1541 sk = vs->sock->sk;
1542 sk_change_net(sk, net);
1543
stephen hemminger553675f2013-05-16 11:35:20 +00001544 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1545 sizeof(vxlan_addr));
1546 if (rc < 0) {
1547 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1548 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1549 sk_release_kernel(sk);
1550 kfree(vs);
1551 return ERR_PTR(rc);
1552 }
1553
1554 /* Disable multicast loopback */
1555 inet_sk(sk)->mc_loop = 0;
1556
1557 /* Mark socket as an encapsulation socket. */
1558 udp_sk(sk)->encap_type = 1;
1559 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1560 udp_encap_enable();
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001561 atomic_set(&vs->refcnt, 1);
stephen hemminger553675f2013-05-16 11:35:20 +00001562
stephen hemminger553675f2013-05-16 11:35:20 +00001563 return vs;
1564}
1565
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001566/* Scheduled at device creation to bind to a socket */
1567static void vxlan_sock_work(struct work_struct *work)
1568{
1569 struct vxlan_dev *vxlan
1570 = container_of(work, struct vxlan_dev, sock_work);
1571 struct net_device *dev = vxlan->dev;
1572 struct net *net = dev_net(dev);
1573 __u32 vni = vxlan->default_dst.remote_vni;
1574 __be16 port = vxlan->dst_port;
1575 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1576 struct vxlan_sock *nvs, *ovs;
1577
1578 nvs = vxlan_socket_create(net, port);
1579 if (IS_ERR(nvs)) {
1580 netdev_err(vxlan->dev, "Can not create UDP socket, %ld\n",
1581 PTR_ERR(nvs));
1582 goto out;
1583 }
1584
1585 spin_lock(&vn->sock_lock);
1586 /* Look again to see if can reuse socket */
1587 ovs = vxlan_find_port(net, port);
1588 if (ovs) {
1589 atomic_inc(&ovs->refcnt);
1590 vxlan->vn_sock = ovs;
1591 hlist_add_head_rcu(&vxlan->hlist, vni_head(ovs, vni));
1592 spin_unlock(&vn->sock_lock);
1593
1594 sk_release_kernel(nvs->sock->sk);
1595 kfree(nvs);
1596 } else {
1597 vxlan->vn_sock = nvs;
1598 hlist_add_head_rcu(&nvs->hlist, vs_head(net, port));
1599 hlist_add_head_rcu(&vxlan->hlist, vni_head(nvs, vni));
1600 spin_unlock(&vn->sock_lock);
1601 }
1602out:
1603 dev_put(dev);
1604}
1605
stephen hemmingerd3428942012-10-01 12:32:35 +00001606static int vxlan_newlink(struct net *net, struct net_device *dev,
1607 struct nlattr *tb[], struct nlattr *data[])
1608{
stephen hemminger553675f2013-05-16 11:35:20 +00001609 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001610 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001611 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00001612 __u32 vni;
1613 int err;
1614
1615 if (!data[IFLA_VXLAN_ID])
1616 return -EINVAL;
1617
1618 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001619 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001620
stephen hemminger5d174dd2013-04-27 11:31:55 +00001621 if (data[IFLA_VXLAN_GROUP])
1622 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001623
1624 if (data[IFLA_VXLAN_LOCAL])
1625 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1626
stephen hemminger34e02aa2012-10-09 20:35:53 +00001627 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001628 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001629 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001630 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001631
stephen hemminger34e02aa2012-10-09 20:35:53 +00001632 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001633 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001634 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001635 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001636
1637 if (!tb[IFLA_MTU])
1638 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001639
1640 /* update header length based on lower device */
1641 dev->hard_header_len = lowerdev->hard_header_len +
1642 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001643 }
1644
1645 if (data[IFLA_VXLAN_TOS])
1646 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1647
Vincent Bernatafb97182012-10-30 10:27:16 +00001648 if (data[IFLA_VXLAN_TTL])
1649 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1650
stephen hemmingerd3428942012-10-01 12:32:35 +00001651 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001652 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001653
1654 if (data[IFLA_VXLAN_AGEING])
1655 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1656 else
1657 vxlan->age_interval = FDB_AGE_DEFAULT;
1658
David Stevense4f67ad2012-11-20 02:50:14 +00001659 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1660 vxlan->flags |= VXLAN_F_PROXY;
1661
1662 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1663 vxlan->flags |= VXLAN_F_RSC;
1664
1665 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1666 vxlan->flags |= VXLAN_F_L2MISS;
1667
1668 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1669 vxlan->flags |= VXLAN_F_L3MISS;
1670
stephen hemmingerd3428942012-10-01 12:32:35 +00001671 if (data[IFLA_VXLAN_LIMIT])
1672 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1673
stephen hemminger05f47d62012-10-09 20:35:50 +00001674 if (data[IFLA_VXLAN_PORT_RANGE]) {
1675 const struct ifla_vxlan_port_range *p
1676 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1677 vxlan->port_min = ntohs(p->low);
1678 vxlan->port_max = ntohs(p->high);
1679 }
1680
stephen hemminger823aa872013-04-27 11:31:57 +00001681 if (data[IFLA_VXLAN_PORT])
1682 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1683
stephen hemminger553675f2013-05-16 11:35:20 +00001684 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1685 pr_info("duplicate VNI %u\n", vni);
1686 return -EEXIST;
1687 }
1688
Yan Burman1b13c972013-01-29 23:43:07 +00001689 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1690
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001691 /* create an fdb entry for default destination */
1692 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1693 vxlan->default_dst.remote_ip,
1694 NUD_REACHABLE|NUD_PERMANENT,
1695 NLM_F_EXCL|NLM_F_CREATE,
1696 vxlan->dst_port, vxlan->default_dst.remote_vni,
1697 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001698 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00001699 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001700
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001701 err = register_netdevice(dev);
1702 if (err) {
1703 vxlan_fdb_delete_defualt(vxlan);
1704 return err;
1705 }
1706
stephen hemminger553675f2013-05-16 11:35:20 +00001707 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00001708
1709 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001710}
1711
1712static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1713{
1714 struct vxlan_dev *vxlan = netdev_priv(dev);
1715
1716 hlist_del_rcu(&vxlan->hlist);
stephen hemminger553675f2013-05-16 11:35:20 +00001717 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001718 unregister_netdevice_queue(dev, head);
1719}
1720
1721static size_t vxlan_get_size(const struct net_device *dev)
1722{
1723
1724 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001725 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001726 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1727 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1728 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1729 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1730 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001731 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1732 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1733 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1734 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001735 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1736 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001737 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001738 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001739 0;
1740}
1741
1742static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1743{
1744 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001745 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001746 struct ifla_vxlan_port_range ports = {
1747 .low = htons(vxlan->port_min),
1748 .high = htons(vxlan->port_max),
1749 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001750
Atzm Watanabec7995c42013-04-16 02:50:52 +00001751 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001752 goto nla_put_failure;
1753
stephen hemminger5d174dd2013-04-27 11:31:55 +00001754 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001755 goto nla_put_failure;
1756
Atzm Watanabec7995c42013-04-16 02:50:52 +00001757 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001758 goto nla_put_failure;
1759
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001760 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001761 goto nla_put_failure;
1762
1763 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1764 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001765 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1766 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1767 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1768 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1769 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1770 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1771 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1772 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1773 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001774 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001775 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1776 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001777 goto nla_put_failure;
1778
stephen hemminger05f47d62012-10-09 20:35:50 +00001779 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1780 goto nla_put_failure;
1781
stephen hemmingerd3428942012-10-01 12:32:35 +00001782 return 0;
1783
1784nla_put_failure:
1785 return -EMSGSIZE;
1786}
1787
1788static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1789 .kind = "vxlan",
1790 .maxtype = IFLA_VXLAN_MAX,
1791 .policy = vxlan_policy,
1792 .priv_size = sizeof(struct vxlan_dev),
1793 .setup = vxlan_setup,
1794 .validate = vxlan_validate,
1795 .newlink = vxlan_newlink,
1796 .dellink = vxlan_dellink,
1797 .get_size = vxlan_get_size,
1798 .fill_info = vxlan_fill_info,
1799};
1800
1801static __net_init int vxlan_init_net(struct net *net)
1802{
1803 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001804 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001805
stephen hemminger553675f2013-05-16 11:35:20 +00001806 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001807 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00001808
stephen hemminger553675f2013-05-16 11:35:20 +00001809 for (h = 0; h < PORT_HASH_SIZE; ++h)
1810 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001811
1812 return 0;
1813}
1814
1815static __net_exit void vxlan_exit_net(struct net *net)
1816{
1817 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001818 struct vxlan_dev *vxlan;
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001819
1820 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001821 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1822 dev_close(vxlan->dev);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001823 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001824}
1825
1826static struct pernet_operations vxlan_net_ops = {
1827 .init = vxlan_init_net,
1828 .exit = vxlan_exit_net,
1829 .id = &vxlan_net_id,
1830 .size = sizeof(struct vxlan_net),
1831};
1832
1833static int __init vxlan_init_module(void)
1834{
1835 int rc;
1836
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001837 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1838 if (!vxlan_wq)
1839 return -ENOMEM;
1840
stephen hemmingerd3428942012-10-01 12:32:35 +00001841 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1842
1843 rc = register_pernet_device(&vxlan_net_ops);
1844 if (rc)
1845 goto out1;
1846
1847 rc = rtnl_link_register(&vxlan_link_ops);
1848 if (rc)
1849 goto out2;
1850
1851 return 0;
1852
1853out2:
1854 unregister_pernet_device(&vxlan_net_ops);
1855out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001856 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00001857 return rc;
1858}
Cong Wang7332a132013-05-27 22:35:53 +00001859late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00001860
1861static void __exit vxlan_cleanup_module(void)
1862{
stephen hemmingerd3428942012-10-01 12:32:35 +00001863 unregister_pernet_device(&vxlan_net_ops);
Stephen Hemmingerb7153982013-06-17 14:16:09 -07001864 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001865 destroy_workqueue(vxlan_wq);
David Stevens66817122013-03-15 04:35:51 +00001866 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001867}
1868module_exit(vxlan_cleanup_module);
1869
1870MODULE_LICENSE("GPL");
1871MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00001872MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00001873MODULE_ALIAS_RTNL_LINK("vxlan");