blob: a334bfb91c5a1cac97f92fa454d56c83e842bb0a [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.
stephen hemmingerd3428942012-10-01 12:32:35 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/rculist.h>
20#include <linux/netdevice.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/udp.h>
24#include <linux/igmp.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
Pravin B Shelar1eaa8172013-08-19 11:23:29 -070027#include <linux/if_vlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000028#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000029#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000030#include <net/arp.h>
31#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000032#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000033#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/icmp.h>
35#include <net/udp.h>
36#include <net/rtnetlink.h>
37#include <net/route.h>
38#include <net/dsfield.h>
39#include <net/inet_ecn.h>
40#include <net/net_namespace.h>
41#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070042#include <net/vxlan.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080043#if IS_ENABLED(CONFIG_IPV6)
44#include <net/ipv6.h>
45#include <net/addrconf.h>
46#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080047#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080048#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000049
50#define VXLAN_VERSION "0.1"
51
stephen hemminger553675f2013-05-16 11:35:20 +000052#define PORT_HASH_BITS 8
53#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000054#define VNI_HASH_BITS 10
55#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
56#define FDB_HASH_BITS 8
57#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
58#define FDB_AGE_DEFAULT 300 /* 5 min */
59#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
60
61#define VXLAN_N_VID (1u << 24)
62#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000063/* IP header + UDP + VXLAN + Ethernet header */
64#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
Cong Wange4c7ed42013-08-31 13:44:33 +080065/* IPv6 header + UDP + VXLAN + Ethernet header */
66#define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
Pravin B Shelar7ce04752013-08-19 11:22:54 -070067#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
stephen hemmingerd3428942012-10-01 12:32:35 +000068
69#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
70
71/* VXLAN protocol header */
72struct vxlanhdr {
73 __be32 vx_flags;
74 __be32 vx_vni;
75};
76
stephen hemminger23c578b2013-04-27 11:31:53 +000077/* UDP port for VXLAN traffic.
78 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070079 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000080 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070081static unsigned short vxlan_port __read_mostly = 8472;
82module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000083MODULE_PARM_DESC(udp_port, "Destination UDP port");
84
85static bool log_ecn_error = true;
86module_param(log_ecn_error, bool, 0644);
87MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
88
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070089static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000090
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030091static const u8 all_zeros_mac[ETH_ALEN];
92
stephen hemminger553675f2013-05-16 11:35:20 +000093/* per-network namespace private data for this module */
94struct vxlan_net {
95 struct list_head vxlan_list;
96 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070097 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000098};
99
Cong Wange4c7ed42013-08-31 13:44:33 +0800100union vxlan_addr {
101 struct sockaddr_in sin;
102 struct sockaddr_in6 sin6;
103 struct sockaddr sa;
104};
105
David Stevens66817122013-03-15 04:35:51 +0000106struct vxlan_rdst {
Cong Wange4c7ed42013-08-31 13:44:33 +0800107 union vxlan_addr remote_ip;
David Stevens66817122013-03-15 04:35:51 +0000108 __be16 remote_port;
109 u32 remote_vni;
110 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700111 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300112 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000113};
114
stephen hemmingerd3428942012-10-01 12:32:35 +0000115/* Forwarding table entry */
116struct vxlan_fdb {
117 struct hlist_node hlist; /* linked list of entries */
118 struct rcu_head rcu;
119 unsigned long updated; /* jiffies */
120 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700121 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000122 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000123 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000124 u8 eth_addr[ETH_ALEN];
125};
126
stephen hemmingerd3428942012-10-01 12:32:35 +0000127/* Pseudo network device */
128struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000129 struct hlist_node hlist; /* vni hash table */
130 struct list_head next; /* vxlan's per namespace list */
131 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000132 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000133 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800134 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000135 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000136 __u16 port_min; /* source port range */
137 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000138 __u8 tos; /* TOS override */
139 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000140 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000141
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700142 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700143 struct work_struct igmp_join;
144 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700145
stephen hemmingerd3428942012-10-01 12:32:35 +0000146 unsigned long age_interval;
147 struct timer_list age_timer;
148 spinlock_t hash_lock;
149 unsigned int addrcnt;
150 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000151
152 struct hlist_head fdb_head[FDB_HASH_SIZE];
153};
154
David Stevense4f67ad2012-11-20 02:50:14 +0000155#define VXLAN_F_LEARN 0x01
156#define VXLAN_F_PROXY 0x02
157#define VXLAN_F_RSC 0x04
158#define VXLAN_F_L2MISS 0x08
159#define VXLAN_F_L3MISS 0x10
Cong Wange4c7ed42013-08-31 13:44:33 +0800160#define VXLAN_F_IPV6 0x20 /* internal flag */
David Stevense4f67ad2012-11-20 02:50:14 +0000161
stephen hemmingerd3428942012-10-01 12:32:35 +0000162/* salt for hash table */
163static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700164static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000165
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700166static void vxlan_sock_work(struct work_struct *work);
167
Cong Wange4c7ed42013-08-31 13:44:33 +0800168#if IS_ENABLED(CONFIG_IPV6)
169static inline
170bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
171{
172 if (a->sa.sa_family != b->sa.sa_family)
173 return false;
174 if (a->sa.sa_family == AF_INET6)
175 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
176 else
177 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
178}
179
180static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
181{
182 if (ipa->sa.sa_family == AF_INET6)
183 return ipv6_addr_any(&ipa->sin6.sin6_addr);
184 else
185 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
186}
187
188static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
189{
190 if (ipa->sa.sa_family == AF_INET6)
191 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
192 else
193 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
194}
195
196static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
197{
198 if (nla_len(nla) >= sizeof(struct in6_addr)) {
199 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
200 ip->sa.sa_family = AF_INET6;
201 return 0;
202 } else if (nla_len(nla) >= sizeof(__be32)) {
203 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
204 ip->sa.sa_family = AF_INET;
205 return 0;
206 } else {
207 return -EAFNOSUPPORT;
208 }
209}
210
211static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
212 const union vxlan_addr *ip)
213{
214 if (ip->sa.sa_family == AF_INET6)
215 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
216 else
217 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
218}
219
220#else /* !CONFIG_IPV6 */
221
222static inline
223bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
224{
225 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
226}
227
228static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
229{
230 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
231}
232
233static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
234{
235 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
236}
237
238static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
239{
240 if (nla_len(nla) >= sizeof(struct in6_addr)) {
241 return -EAFNOSUPPORT;
242 } else if (nla_len(nla) >= sizeof(__be32)) {
243 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
244 ip->sa.sa_family = AF_INET;
245 return 0;
246 } else {
247 return -EAFNOSUPPORT;
248 }
249}
250
251static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
252 const union vxlan_addr *ip)
253{
254 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
255}
256#endif
257
stephen hemminger553675f2013-05-16 11:35:20 +0000258/* Virtual Network hash table head */
259static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
260{
261 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
262}
263
264/* Socket hash table head */
265static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000266{
267 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
268
stephen hemminger553675f2013-05-16 11:35:20 +0000269 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
270}
271
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700272/* First remote destination for a forwarding entry.
273 * Guaranteed to be non-NULL because remotes are never deleted.
274 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700275static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700276{
stephen hemminger5ca54612013-08-04 17:17:39 -0700277 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
278}
279
280static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
281{
282 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700283}
284
stephen hemminger553675f2013-05-16 11:35:20 +0000285/* Find VXLAN socket based on network namespace and UDP port */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -0700286static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000287{
288 struct vxlan_sock *vs;
289
290 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
291 if (inet_sk(vs->sock->sk)->inet_sport == port)
292 return vs;
293 }
294 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000295}
296
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700297static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000298{
299 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000300
stephen hemminger553675f2013-05-16 11:35:20 +0000301 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000302 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000303 return vxlan;
304 }
305
306 return NULL;
307}
308
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700309/* Look up VNI in a per net namespace table */
310static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
311{
312 struct vxlan_sock *vs;
313
314 vs = vxlan_find_sock(net, port);
315 if (!vs)
316 return NULL;
317
318 return vxlan_vs_find_vni(vs, id);
319}
320
stephen hemmingerd3428942012-10-01 12:32:35 +0000321/* Fill in neighbour message in skbuff. */
322static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700323 const struct vxlan_fdb *fdb,
324 u32 portid, u32 seq, int type, unsigned int flags,
325 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000326{
327 unsigned long now = jiffies;
328 struct nda_cacheinfo ci;
329 struct nlmsghdr *nlh;
330 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000331 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000332
333 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
334 if (nlh == NULL)
335 return -EMSGSIZE;
336
337 ndm = nlmsg_data(nlh);
338 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000339
340 send_eth = send_ip = true;
341
342 if (type == RTM_GETNEIGH) {
343 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800344 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000345 send_eth = !is_zero_ether_addr(fdb->eth_addr);
346 } else
347 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000348 ndm->ndm_state = fdb->state;
349 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000350 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000351 ndm->ndm_type = NDA_DST;
352
David Stevense4f67ad2012-11-20 02:50:14 +0000353 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000354 goto nla_put_failure;
355
Cong Wange4c7ed42013-08-31 13:44:33 +0800356 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000357 goto nla_put_failure;
358
stephen hemminger823aa872013-04-27 11:31:57 +0000359 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000360 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
361 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000362 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700363 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000364 goto nla_put_failure;
365 if (rdst->remote_ifindex &&
366 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000367 goto nla_put_failure;
368
369 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
370 ci.ndm_confirmed = 0;
371 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
372 ci.ndm_refcnt = 0;
373
374 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
375 goto nla_put_failure;
376
377 return nlmsg_end(skb, nlh);
378
379nla_put_failure:
380 nlmsg_cancel(skb, nlh);
381 return -EMSGSIZE;
382}
383
384static inline size_t vxlan_nlmsg_size(void)
385{
386 return NLMSG_ALIGN(sizeof(struct ndmsg))
387 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800388 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000389 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000390 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
391 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000392 + nla_total_size(sizeof(struct nda_cacheinfo));
393}
394
395static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700396 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000397{
398 struct net *net = dev_net(vxlan->dev);
399 struct sk_buff *skb;
400 int err = -ENOBUFS;
401
402 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
403 if (skb == NULL)
404 goto errout;
405
stephen hemminger5ca54612013-08-04 17:17:39 -0700406 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
407 first_remote_rtnl(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000408 if (err < 0) {
409 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
410 WARN_ON(err == -EMSGSIZE);
411 kfree_skb(skb);
412 goto errout;
413 }
414
415 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
416 return;
417errout:
418 if (err < 0)
419 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
420}
421
Cong Wange4c7ed42013-08-31 13:44:33 +0800422static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000423{
424 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700425 struct vxlan_fdb f = {
426 .state = NUD_STALE,
427 };
428 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800429 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700430 .remote_vni = VXLAN_N_VID,
431 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700432
433 INIT_LIST_HEAD(&f.remotes);
434 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000435
436 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
437}
438
439static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
440{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700441 struct vxlan_fdb f = {
442 .state = NUD_STALE,
443 };
David Stevense4f67ad2012-11-20 02:50:14 +0000444
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700445 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000446 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
447
448 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
449}
450
stephen hemmingerd3428942012-10-01 12:32:35 +0000451/* Hash Ethernet address */
452static u32 eth_hash(const unsigned char *addr)
453{
454 u64 value = get_unaligned((u64 *)addr);
455
456 /* only want 6 bytes */
457#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000458 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000459#else
460 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000461#endif
462 return hash_64(value, FDB_HASH_BITS);
463}
464
465/* Hash chain to use given mac address */
466static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
467 const u8 *mac)
468{
469 return &vxlan->fdb_head[eth_hash(mac)];
470}
471
472/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000473static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000474 const u8 *mac)
475
476{
477 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
478 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000479
Sasha Levinb67bfe02013-02-27 17:06:00 -0800480 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700481 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000482 return f;
483 }
484
485 return NULL;
486}
487
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000488static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
489 const u8 *mac)
490{
491 struct vxlan_fdb *f;
492
493 f = __vxlan_find_mac(vxlan, mac);
494 if (f)
495 f->used = jiffies;
496
497 return f;
498}
499
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300500/* caller should hold vxlan->hash_lock */
501static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800502 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300503 __u32 vni, __u32 ifindex)
504{
505 struct vxlan_rdst *rd;
506
507 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800508 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300509 rd->remote_port == port &&
510 rd->remote_vni == vni &&
511 rd->remote_ifindex == ifindex)
512 return rd;
513 }
514
515 return NULL;
516}
517
Thomas Richter906dc182013-07-19 17:20:07 +0200518/* Replace destination of unicast mac */
519static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800520 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200521{
522 struct vxlan_rdst *rd;
523
524 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
525 if (rd)
526 return 0;
527
528 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
529 if (!rd)
530 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800531 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200532 rd->remote_port = port;
533 rd->remote_vni = vni;
534 rd->remote_ifindex = ifindex;
535 return 1;
536}
537
David Stevens66817122013-03-15 04:35:51 +0000538/* Add/update destinations for multicast */
539static int vxlan_fdb_append(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800540 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000541{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700542 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000543
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300544 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
545 if (rd)
546 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700547
David Stevens66817122013-03-15 04:35:51 +0000548 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
549 if (rd == NULL)
550 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800551 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000552 rd->remote_port = port;
553 rd->remote_vni = vni;
554 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700555
556 list_add_tail_rcu(&rd->list, &f->remotes);
557
David Stevens66817122013-03-15 04:35:51 +0000558 return 1;
559}
560
stephen hemmingerd3428942012-10-01 12:32:35 +0000561/* Add new entry to forwarding table -- assumes lock held */
562static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800563 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000564 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000565 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000566 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000567{
568 struct vxlan_fdb *f;
569 int notify = 0;
570
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000571 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000572 if (f) {
573 if (flags & NLM_F_EXCL) {
574 netdev_dbg(vxlan->dev,
575 "lost race to create %pM\n", mac);
576 return -EEXIST;
577 }
578 if (f->state != state) {
579 f->state = state;
580 f->updated = jiffies;
581 notify = 1;
582 }
David Stevensae884082013-04-19 00:36:26 +0000583 if (f->flags != ndm_flags) {
584 f->flags = ndm_flags;
585 f->updated = jiffies;
586 notify = 1;
587 }
Thomas Richter906dc182013-07-19 17:20:07 +0200588 if ((flags & NLM_F_REPLACE)) {
589 /* Only change unicasts */
590 if (!(is_multicast_ether_addr(f->eth_addr) ||
591 is_zero_ether_addr(f->eth_addr))) {
592 int rc = vxlan_fdb_replace(f, ip, port, vni,
593 ifindex);
594
595 if (rc < 0)
596 return rc;
597 notify |= rc;
598 } else
599 return -EOPNOTSUPP;
600 }
David Stevens66817122013-03-15 04:35:51 +0000601 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300602 (is_multicast_ether_addr(f->eth_addr) ||
603 is_zero_ether_addr(f->eth_addr))) {
David Stevens66817122013-03-15 04:35:51 +0000604 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
605
606 if (rc < 0)
607 return rc;
608 notify |= rc;
609 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000610 } else {
611 if (!(flags & NLM_F_CREATE))
612 return -ENOENT;
613
614 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
615 return -ENOSPC;
616
Thomas Richter906dc182013-07-19 17:20:07 +0200617 /* Disallow replace to add a multicast entry */
618 if ((flags & NLM_F_REPLACE) &&
619 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
620 return -EOPNOTSUPP;
621
Cong Wange4c7ed42013-08-31 13:44:33 +0800622 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000623 f = kmalloc(sizeof(*f), GFP_ATOMIC);
624 if (!f)
625 return -ENOMEM;
626
627 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000628 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000629 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000630 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700631 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000632 memcpy(f->eth_addr, mac, ETH_ALEN);
633
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700634 vxlan_fdb_append(f, ip, port, vni, ifindex);
635
stephen hemmingerd3428942012-10-01 12:32:35 +0000636 ++vxlan->addrcnt;
637 hlist_add_head_rcu(&f->hlist,
638 vxlan_fdb_head(vxlan, mac));
639 }
640
641 if (notify)
642 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
643
644 return 0;
645}
646
Wei Yongjun6706c822013-04-11 19:00:35 +0000647static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000648{
649 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700650 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000651
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700652 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000653 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000654 kfree(f);
655}
656
stephen hemmingerd3428942012-10-01 12:32:35 +0000657static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
658{
659 netdev_dbg(vxlan->dev,
660 "delete %pM\n", f->eth_addr);
661
662 --vxlan->addrcnt;
663 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
664
665 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000666 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000667}
668
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300669static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800670 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300671{
672 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800673 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300674
675 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800676 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
677 if (err)
678 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300679 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800680 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
681 if (remote->sa.sa_family == AF_INET) {
682 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
683 ip->sa.sa_family = AF_INET;
684#if IS_ENABLED(CONFIG_IPV6)
685 } else {
686 ip->sin6.sin6_addr = in6addr_any;
687 ip->sa.sa_family = AF_INET6;
688#endif
689 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300690 }
691
692 if (tb[NDA_PORT]) {
693 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
694 return -EINVAL;
695 *port = nla_get_be16(tb[NDA_PORT]);
696 } else {
697 *port = vxlan->dst_port;
698 }
699
700 if (tb[NDA_VNI]) {
701 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
702 return -EINVAL;
703 *vni = nla_get_u32(tb[NDA_VNI]);
704 } else {
705 *vni = vxlan->default_dst.remote_vni;
706 }
707
708 if (tb[NDA_IFINDEX]) {
709 struct net_device *tdev;
710
711 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
712 return -EINVAL;
713 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
714 tdev = dev_get_by_index(net, *ifindex);
715 if (!tdev)
716 return -EADDRNOTAVAIL;
717 dev_put(tdev);
718 } else {
719 *ifindex = 0;
720 }
721
722 return 0;
723}
724
stephen hemmingerd3428942012-10-01 12:32:35 +0000725/* Add static entry (via netlink) */
726static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
727 struct net_device *dev,
728 const unsigned char *addr, u16 flags)
729{
730 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300731 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800732 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000733 __be16 port;
734 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000735 int err;
736
737 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
738 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
739 ndm->ndm_state);
740 return -EINVAL;
741 }
742
743 if (tb[NDA_DST] == NULL)
744 return -EINVAL;
745
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300746 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
747 if (err)
748 return err;
David Stevens66817122013-03-15 04:35:51 +0000749
stephen hemmingerd3428942012-10-01 12:32:35 +0000750 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800751 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000752 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000753 spin_unlock_bh(&vxlan->hash_lock);
754
755 return err;
756}
757
758/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000759static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
760 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000761 const unsigned char *addr)
762{
763 struct vxlan_dev *vxlan = netdev_priv(dev);
764 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300765 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800766 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300767 __be16 port;
768 u32 vni, ifindex;
769 int err;
770
771 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
772 if (err)
773 return err;
774
775 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000776
777 spin_lock_bh(&vxlan->hash_lock);
778 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300779 if (!f)
780 goto out;
781
Cong Wange4c7ed42013-08-31 13:44:33 +0800782 if (!vxlan_addr_any(&ip)) {
783 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300784 if (!rd)
785 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000786 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300787
788 err = 0;
789
790 /* remove a destination if it's not the only one on the list,
791 * otherwise destroy the fdb entry
792 */
793 if (rd && !list_is_singular(&f->remotes)) {
794 list_del_rcu(&rd->list);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800795 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300796 goto out;
797 }
798
799 vxlan_fdb_destroy(vxlan, f);
800
801out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000802 spin_unlock_bh(&vxlan->hash_lock);
803
804 return err;
805}
806
807/* Dump forwarding table */
808static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
809 struct net_device *dev, int idx)
810{
811 struct vxlan_dev *vxlan = netdev_priv(dev);
812 unsigned int h;
813
814 for (h = 0; h < FDB_HASH_SIZE; ++h) {
815 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000816 int err;
817
Sasha Levinb67bfe02013-02-27 17:06:00 -0800818 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000819 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000820
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700821 if (idx < cb->args[0])
822 goto skip;
823
824 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000825 err = vxlan_fdb_info(skb, vxlan, f,
826 NETLINK_CB(cb->skb).portid,
827 cb->nlh->nlmsg_seq,
828 RTM_NEWNEIGH,
829 NLM_F_MULTI, rd);
830 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700831 goto out;
David Stevens66817122013-03-15 04:35:51 +0000832 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700833skip:
834 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000835 }
836 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700837out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000838 return idx;
839}
840
841/* Watch incoming packets to learn mapping between Ethernet address
842 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700843 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000844 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700845static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800846 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000847{
848 struct vxlan_dev *vxlan = netdev_priv(dev);
849 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000850
851 f = vxlan_find_mac(vxlan, src_mac);
852 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700853 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700854
Cong Wange4c7ed42013-08-31 13:44:33 +0800855 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700856 return false;
857
858 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700859 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700860 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000861
862 if (net_ratelimit())
863 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800864 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700865 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000866
Cong Wange4c7ed42013-08-31 13:44:33 +0800867 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000868 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700869 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000870 } else {
871 /* learned new entry */
872 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700873
874 /* close off race between vxlan_flush and incoming packets */
875 if (netif_running(dev))
876 vxlan_fdb_create(vxlan, src_mac, src_ip,
877 NUD_REACHABLE,
878 NLM_F_EXCL|NLM_F_CREATE,
879 vxlan->dst_port,
880 vxlan->default_dst.remote_vni,
881 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000882 spin_unlock(&vxlan->hash_lock);
883 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700884
885 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000886}
887
stephen hemmingerd3428942012-10-01 12:32:35 +0000888/* See if multicast group is already in use by other ID */
Cong Wange4c7ed42013-08-31 13:44:33 +0800889static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000890{
stephen hemminger553675f2013-05-16 11:35:20 +0000891 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000892
stephen hemminger553675f2013-05-16 11:35:20 +0000893 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000894 if (!netif_running(vxlan->dev))
895 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000896
Cong Wange4c7ed42013-08-31 13:44:33 +0800897 if (vxlan_addr_equal(&vxlan->default_dst.remote_ip,
898 remote_ip))
stephen hemminger553675f2013-05-16 11:35:20 +0000899 return true;
900 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000901
902 return false;
903}
904
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700905static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000906{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700907 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000908}
909
Pravin B Shelar012a5722013-08-19 11:23:07 -0700910void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000911{
Pravin B Shelar012a5722013-08-19 11:23:07 -0700912 struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
913
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700914 if (!atomic_dec_and_test(&vs->refcnt))
915 return;
916
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700917 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700918 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700919 spin_unlock(&vn->sock_lock);
920
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700921 queue_work(vxlan_wq, &vs->del_work);
922}
Pravin B Shelar012a5722013-08-19 11:23:07 -0700923EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700924
stephen hemminger3fc2de22013-07-18 08:40:15 -0700925/* Callback to update multicast group membership when first VNI on
926 * multicast asddress is brought up
927 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700928 */
stephen hemminger3fc2de22013-07-18 08:40:15 -0700929static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700930{
stephen hemminger3fc2de22013-07-18 08:40:15 -0700931 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700932 struct vxlan_sock *vs = vxlan->vn_sock;
933 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +0800934 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
935 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000936
stephen hemmingerd3428942012-10-01 12:32:35 +0000937 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +0800938 if (ip->sa.sa_family == AF_INET) {
939 struct ip_mreqn mreq = {
940 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
941 .imr_ifindex = ifindex,
942 };
943
944 ip_mc_join_group(sk, &mreq);
945#if IS_ENABLED(CONFIG_IPV6)
946 } else {
947 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
948 &ip->sin6.sin6_addr);
949#endif
950 }
stephen hemminger3fc2de22013-07-18 08:40:15 -0700951 release_sock(sk);
952
Pravin B Shelar012a5722013-08-19 11:23:07 -0700953 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700954 dev_put(vxlan->dev);
955}
956
957/* Inverse of vxlan_igmp_join when last VNI is brought down */
958static void vxlan_igmp_leave(struct work_struct *work)
959{
960 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700961 struct vxlan_sock *vs = vxlan->vn_sock;
962 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +0800963 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
964 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700965
966 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +0800967 if (ip->sa.sa_family == AF_INET) {
968 struct ip_mreqn mreq = {
969 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
970 .imr_ifindex = ifindex,
971 };
972
973 ip_mc_leave_group(sk, &mreq);
974#if IS_ENABLED(CONFIG_IPV6)
975 } else {
976 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
977 &ip->sin6.sin6_addr);
978#endif
979 }
980
stephen hemmingerd3428942012-10-01 12:32:35 +0000981 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000982
Pravin B Shelar012a5722013-08-19 11:23:07 -0700983 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700984 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000985}
986
987/* Callback from net/ipv4/udp.c to receive packets */
988static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
989{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700990 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000991 struct vxlanhdr *vxh;
stephen hemminger553675f2013-05-16 11:35:20 +0000992 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000993
stephen hemmingerd3428942012-10-01 12:32:35 +0000994 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700995 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +0000996 goto error;
997
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700998 /* Return packets with reserved bits set */
999 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001000 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1001 (vxh->vx_vni & htonl(0xff))) {
1002 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1003 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1004 goto error;
1005 }
1006
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001007 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001008 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001009
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001010 port = inet_sk(sk)->inet_sport;
1011
1012 vs = vxlan_find_sock(sock_net(sk), port);
1013 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001014 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001015
1016 vs->rcv(vs, skb, vxh->vx_vni);
1017 return 0;
1018
1019drop:
1020 /* Consume bad packet */
1021 kfree_skb(skb);
1022 return 0;
1023
1024error:
1025 /* Return non vxlan pkt */
1026 return 1;
1027}
1028
1029static void vxlan_rcv(struct vxlan_sock *vs,
1030 struct sk_buff *skb, __be32 vx_vni)
1031{
Cong Wange4c7ed42013-08-31 13:44:33 +08001032 struct iphdr *oip = NULL;
1033 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001034 struct vxlan_dev *vxlan;
1035 struct pcpu_tstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001036 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001037 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001038 int err = 0;
1039 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001040
1041 vni = ntohl(vx_vni) >> 8;
1042 /* Is this VNI defined? */
1043 vxlan = vxlan_vs_find_vni(vs, vni);
1044 if (!vxlan)
1045 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001046
Cong Wange4c7ed42013-08-31 13:44:33 +08001047 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001048 skb_reset_mac_header(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001049 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001050
1051 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001052 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001053 goto drop;
1054
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001055 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001056 if (remote_ip->sa.sa_family == AF_INET) {
1057 oip = ip_hdr(skb);
1058 saddr.sin.sin_addr.s_addr = oip->saddr;
1059 saddr.sa.sa_family = AF_INET;
1060#if IS_ENABLED(CONFIG_IPV6)
1061 } else {
1062 oip6 = ipv6_hdr(skb);
1063 saddr.sin6.sin6_addr = oip6->saddr;
1064 saddr.sa.sa_family = AF_INET6;
1065#endif
1066 }
1067
stephen hemminger26a41ae62013-06-17 12:09:58 -07001068 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001069 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001070 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001071
stephen hemmingerd3428942012-10-01 12:32:35 +00001072 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001073
1074 /* If the NIC driver gave us an encapsulated packet with
1075 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
1076 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
1077 * for us. Otherwise force the upper layers to verify it.
1078 */
1079 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
1080 !(vxlan->dev->features & NETIF_F_RXCSUM))
1081 skb->ip_summed = CHECKSUM_NONE;
1082
1083 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001084
Cong Wange4c7ed42013-08-31 13:44:33 +08001085 if (oip6)
1086 err = IP6_ECN_decapsulate(oip6, skb);
1087 if (oip)
1088 err = IP_ECN_decapsulate(oip, skb);
1089
stephen hemmingerd3428942012-10-01 12:32:35 +00001090 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001091 if (log_ecn_error) {
1092 if (oip6)
1093 net_info_ratelimited("non-ECT from %pI6\n",
1094 &oip6->saddr);
1095 if (oip)
1096 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1097 &oip->saddr, oip->tos);
1098 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001099 if (err > 1) {
1100 ++vxlan->dev->stats.rx_frame_errors;
1101 ++vxlan->dev->stats.rx_errors;
1102 goto drop;
1103 }
1104 }
1105
Pravin B Shelare8171042013-03-25 14:49:46 +00001106 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001107 u64_stats_update_begin(&stats->syncp);
1108 stats->rx_packets++;
1109 stats->rx_bytes += skb->len;
1110 u64_stats_update_end(&stats->syncp);
1111
1112 netif_rx(skb);
1113
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001114 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001115drop:
1116 /* Consume bad packet */
1117 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001118}
1119
David Stevense4f67ad2012-11-20 02:50:14 +00001120static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1121{
1122 struct vxlan_dev *vxlan = netdev_priv(dev);
1123 struct arphdr *parp;
1124 u8 *arpptr, *sha;
1125 __be32 sip, tip;
1126 struct neighbour *n;
1127
1128 if (dev->flags & IFF_NOARP)
1129 goto out;
1130
1131 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1132 dev->stats.tx_dropped++;
1133 goto out;
1134 }
1135 parp = arp_hdr(skb);
1136
1137 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1138 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1139 parp->ar_pro != htons(ETH_P_IP) ||
1140 parp->ar_op != htons(ARPOP_REQUEST) ||
1141 parp->ar_hln != dev->addr_len ||
1142 parp->ar_pln != 4)
1143 goto out;
1144 arpptr = (u8 *)parp + sizeof(struct arphdr);
1145 sha = arpptr;
1146 arpptr += dev->addr_len; /* sha */
1147 memcpy(&sip, arpptr, sizeof(sip));
1148 arpptr += sizeof(sip);
1149 arpptr += dev->addr_len; /* tha */
1150 memcpy(&tip, arpptr, sizeof(tip));
1151
1152 if (ipv4_is_loopback(tip) ||
1153 ipv4_is_multicast(tip))
1154 goto out;
1155
1156 n = neigh_lookup(&arp_tbl, &tip, dev);
1157
1158 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001159 struct vxlan_fdb *f;
1160 struct sk_buff *reply;
1161
1162 if (!(n->nud_state & NUD_CONNECTED)) {
1163 neigh_release(n);
1164 goto out;
1165 }
1166
1167 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001168 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001169 /* bridge-local neighbor */
1170 neigh_release(n);
1171 goto out;
1172 }
1173
1174 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1175 n->ha, sha);
1176
1177 neigh_release(n);
1178
1179 skb_reset_mac_header(reply);
1180 __skb_pull(reply, skb_network_offset(reply));
1181 reply->ip_summed = CHECKSUM_UNNECESSARY;
1182 reply->pkt_type = PACKET_HOST;
1183
1184 if (netif_rx_ni(reply) == NET_RX_DROP)
1185 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001186 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1187 union vxlan_addr ipa = {
1188 .sin.sin_addr.s_addr = tip,
1189 .sa.sa_family = AF_INET,
1190 };
1191
1192 vxlan_ip_miss(dev, &ipa);
1193 }
David Stevense4f67ad2012-11-20 02:50:14 +00001194out:
1195 consume_skb(skb);
1196 return NETDEV_TX_OK;
1197}
1198
Cong Wangf564f452013-08-31 13:44:36 +08001199#if IS_ENABLED(CONFIG_IPV6)
1200static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1201{
1202 struct vxlan_dev *vxlan = netdev_priv(dev);
1203 struct neighbour *n;
1204 union vxlan_addr ipa;
1205 const struct ipv6hdr *iphdr;
1206 const struct in6_addr *saddr, *daddr;
1207 struct nd_msg *msg;
1208 struct inet6_dev *in6_dev = NULL;
1209
1210 in6_dev = __in6_dev_get(dev);
1211 if (!in6_dev)
1212 goto out;
1213
1214 if (!pskb_may_pull(skb, skb->len))
1215 goto out;
1216
1217 iphdr = ipv6_hdr(skb);
1218 saddr = &iphdr->saddr;
1219 daddr = &iphdr->daddr;
1220
1221 if (ipv6_addr_loopback(daddr) ||
1222 ipv6_addr_is_multicast(daddr))
1223 goto out;
1224
1225 msg = (struct nd_msg *)skb_transport_header(skb);
1226 if (msg->icmph.icmp6_code != 0 ||
1227 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1228 goto out;
1229
1230 n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev);
1231
1232 if (n) {
1233 struct vxlan_fdb *f;
1234
1235 if (!(n->nud_state & NUD_CONNECTED)) {
1236 neigh_release(n);
1237 goto out;
1238 }
1239
1240 f = vxlan_find_mac(vxlan, n->ha);
1241 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1242 /* bridge-local neighbor */
1243 neigh_release(n);
1244 goto out;
1245 }
1246
1247 ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target,
1248 !!in6_dev->cnf.forwarding,
1249 true, false, false);
1250 neigh_release(n);
1251 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1252 ipa.sin6.sin6_addr = *daddr;
1253 ipa.sa.sa_family = AF_INET6;
1254 vxlan_ip_miss(dev, &ipa);
1255 }
1256
1257out:
1258 consume_skb(skb);
1259 return NETDEV_TX_OK;
1260}
1261#endif
1262
David Stevense4f67ad2012-11-20 02:50:14 +00001263static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1264{
1265 struct vxlan_dev *vxlan = netdev_priv(dev);
1266 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001267
1268 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1269 return false;
1270
1271 n = NULL;
1272 switch (ntohs(eth_hdr(skb)->h_proto)) {
1273 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001274 {
1275 struct iphdr *pip;
1276
David Stevense4f67ad2012-11-20 02:50:14 +00001277 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1278 return false;
1279 pip = ip_hdr(skb);
1280 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001281 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1282 union vxlan_addr ipa = {
1283 .sin.sin_addr.s_addr = pip->daddr,
1284 .sa.sa_family = AF_INET,
1285 };
1286
1287 vxlan_ip_miss(dev, &ipa);
1288 return false;
1289 }
1290
David Stevense4f67ad2012-11-20 02:50:14 +00001291 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001292 }
1293#if IS_ENABLED(CONFIG_IPV6)
1294 case ETH_P_IPV6:
1295 {
1296 struct ipv6hdr *pip6;
1297
1298 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1299 return false;
1300 pip6 = ipv6_hdr(skb);
1301 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1302 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1303 union vxlan_addr ipa = {
1304 .sin6.sin6_addr = pip6->daddr,
1305 .sa.sa_family = AF_INET6,
1306 };
1307
1308 vxlan_ip_miss(dev, &ipa);
1309 return false;
1310 }
1311
1312 break;
1313 }
1314#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001315 default:
1316 return false;
1317 }
1318
1319 if (n) {
1320 bool diff;
1321
Joe Perches7367d0b2013-09-01 11:51:23 -07001322 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001323 if (diff) {
1324 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1325 dev->addr_len);
1326 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1327 }
1328 neigh_release(n);
1329 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001330 }
1331
David Stevense4f67ad2012-11-20 02:50:14 +00001332 return false;
1333}
1334
stephen hemminger553675f2013-05-16 11:35:20 +00001335static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001336{
1337 sock_put(skb->sk);
1338}
1339
1340/* On transmit, associate with the tunnel socket */
Pravin B Shelar49560532013-08-19 11:23:17 -07001341static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001342{
stephen hemminger1cad8712012-10-09 20:35:49 +00001343 skb_orphan(skb);
1344 sock_hold(sk);
1345 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001346 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001347}
1348
stephen hemminger05f47d62012-10-09 20:35:50 +00001349/* Compute source port for outgoing packet
1350 * first choice to use L4 flow hash since it will spread
1351 * better and maybe available from hardware
1352 * secondary choice is to use jhash on the Ethernet header
1353 */
Pravin B Shelar49560532013-08-19 11:23:17 -07001354__be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001355{
Pravin B Shelar49560532013-08-19 11:23:17 -07001356 unsigned int range = (port_max - port_min) + 1;
stephen hemminger05f47d62012-10-09 20:35:50 +00001357 u32 hash;
1358
1359 hash = skb_get_rxhash(skb);
1360 if (!hash)
1361 hash = jhash(skb->data, 2 * ETH_ALEN,
1362 (__force u32) skb->protocol);
1363
Pravin B Shelar49560532013-08-19 11:23:17 -07001364 return htons((((u64) hash * range) >> 32) + port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001365}
Pravin B Shelar49560532013-08-19 11:23:17 -07001366EXPORT_SYMBOL_GPL(vxlan_src_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001367
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001368static int handle_offloads(struct sk_buff *skb)
1369{
1370 if (skb_is_gso(skb)) {
1371 int err = skb_unclone(skb, GFP_ATOMIC);
1372 if (unlikely(err))
1373 return err;
1374
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001375 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001376 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1377 skb->ip_summed = CHECKSUM_NONE;
1378
1379 return 0;
1380}
1381
Cong Wange4c7ed42013-08-31 13:44:33 +08001382#if IS_ENABLED(CONFIG_IPV6)
Nicolas Dichtel11796182013-09-02 15:34:55 +02001383static int vxlan6_xmit_skb(struct vxlan_sock *vs,
Cong Wange4c7ed42013-08-31 13:44:33 +08001384 struct dst_entry *dst, struct sk_buff *skb,
1385 struct net_device *dev, struct in6_addr *saddr,
1386 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1387 __be16 src_port, __be16 dst_port, __be32 vni)
1388{
1389 struct ipv6hdr *ip6h;
1390 struct vxlanhdr *vxh;
1391 struct udphdr *uh;
1392 int min_headroom;
1393 int err;
1394
1395 if (!skb->encapsulation) {
1396 skb_reset_inner_headers(skb);
1397 skb->encapsulation = 1;
1398 }
1399
1400 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1401 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1402 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1403
1404 /* Need space for new headers (invalidates iph ptr) */
1405 err = skb_cow_head(skb, min_headroom);
1406 if (unlikely(err))
1407 return err;
1408
1409 if (vlan_tx_tag_present(skb)) {
1410 if (WARN_ON(!__vlan_put_tag(skb,
1411 skb->vlan_proto,
1412 vlan_tx_tag_get(skb))))
1413 return -ENOMEM;
1414
1415 skb->vlan_tci = 0;
1416 }
1417
1418 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1419 vxh->vx_flags = htonl(VXLAN_FLAGS);
1420 vxh->vx_vni = vni;
1421
1422 __skb_push(skb, sizeof(*uh));
1423 skb_reset_transport_header(skb);
1424 uh = udp_hdr(skb);
1425
1426 uh->dest = dst_port;
1427 uh->source = src_port;
1428
1429 uh->len = htons(skb->len);
1430 uh->check = 0;
1431
1432 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1433 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1434 IPSKB_REROUTED);
1435 skb_dst_drop(skb);
1436 skb_dst_set(skb, dst);
1437
1438 if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
1439 __wsum csum = skb_checksum(skb, 0, skb->len, 0);
1440 skb->ip_summed = CHECKSUM_UNNECESSARY;
1441 uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
1442 IPPROTO_UDP, csum);
1443 if (uh->check == 0)
1444 uh->check = CSUM_MANGLED_0;
1445 } else {
1446 skb->ip_summed = CHECKSUM_PARTIAL;
1447 skb->csum_start = skb_transport_header(skb) - skb->head;
1448 skb->csum_offset = offsetof(struct udphdr, check);
1449 uh->check = ~csum_ipv6_magic(saddr, daddr,
1450 skb->len, IPPROTO_UDP, 0);
1451 }
1452
1453 __skb_push(skb, sizeof(*ip6h));
1454 skb_reset_network_header(skb);
1455 ip6h = ipv6_hdr(skb);
1456 ip6h->version = 6;
1457 ip6h->priority = prio;
1458 ip6h->flow_lbl[0] = 0;
1459 ip6h->flow_lbl[1] = 0;
1460 ip6h->flow_lbl[2] = 0;
1461 ip6h->payload_len = htons(skb->len);
1462 ip6h->nexthdr = IPPROTO_UDP;
1463 ip6h->hop_limit = ttl;
1464 ip6h->daddr = *daddr;
1465 ip6h->saddr = *saddr;
1466
1467 vxlan_set_owner(vs->sock->sk, skb);
1468
1469 err = handle_offloads(skb);
1470 if (err)
1471 return err;
1472
1473 ip6tunnel_xmit(skb, dev);
1474 return 0;
1475}
1476#endif
1477
Nicolas Dichtel11796182013-09-02 15:34:55 +02001478int vxlan_xmit_skb(struct vxlan_sock *vs,
Pravin B Shelar49560532013-08-19 11:23:17 -07001479 struct rtable *rt, struct sk_buff *skb,
1480 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1481 __be16 src_port, __be16 dst_port, __be32 vni)
1482{
1483 struct vxlanhdr *vxh;
1484 struct udphdr *uh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001485 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001486 int err;
1487
1488 if (!skb->encapsulation) {
1489 skb_reset_inner_headers(skb);
1490 skb->encapsulation = 1;
1491 }
1492
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001493 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001494 + VXLAN_HLEN + sizeof(struct iphdr)
1495 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001496
1497 /* Need space for new headers (invalidates iph ptr) */
1498 err = skb_cow_head(skb, min_headroom);
1499 if (unlikely(err))
1500 return err;
1501
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001502 if (vlan_tx_tag_present(skb)) {
1503 if (WARN_ON(!__vlan_put_tag(skb,
1504 skb->vlan_proto,
1505 vlan_tx_tag_get(skb))))
1506 return -ENOMEM;
1507
1508 skb->vlan_tci = 0;
1509 }
1510
Pravin B Shelar49560532013-08-19 11:23:17 -07001511 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1512 vxh->vx_flags = htonl(VXLAN_FLAGS);
1513 vxh->vx_vni = vni;
1514
1515 __skb_push(skb, sizeof(*uh));
1516 skb_reset_transport_header(skb);
1517 uh = udp_hdr(skb);
1518
1519 uh->dest = dst_port;
1520 uh->source = src_port;
1521
1522 uh->len = htons(skb->len);
1523 uh->check = 0;
1524
1525 vxlan_set_owner(vs->sock->sk, skb);
1526
1527 err = handle_offloads(skb);
1528 if (err)
1529 return err;
1530
Nicolas Dichtel8b7ed2d2013-09-02 15:34:54 +02001531 return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df);
Pravin B Shelar49560532013-08-19 11:23:17 -07001532}
1533EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1534
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001535/* Bypass encapsulation if the destination is local */
1536static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1537 struct vxlan_dev *dst_vxlan)
1538{
1539 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1540 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Cong Wange4c7ed42013-08-31 13:44:33 +08001541 union vxlan_addr loopback;
1542 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001543
1544 skb->pkt_type = PACKET_HOST;
1545 skb->encapsulation = 0;
1546 skb->dev = dst_vxlan->dev;
1547 __skb_pull(skb, skb_network_offset(skb));
1548
Cong Wange4c7ed42013-08-31 13:44:33 +08001549 if (remote_ip->sa.sa_family == AF_INET) {
1550 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1551 loopback.sa.sa_family = AF_INET;
1552#if IS_ENABLED(CONFIG_IPV6)
1553 } else {
1554 loopback.sin6.sin6_addr = in6addr_loopback;
1555 loopback.sa.sa_family = AF_INET6;
1556#endif
1557 }
1558
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001559 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001560 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001561
1562 u64_stats_update_begin(&tx_stats->syncp);
1563 tx_stats->tx_packets++;
1564 tx_stats->tx_bytes += skb->len;
1565 u64_stats_update_end(&tx_stats->syncp);
1566
1567 if (netif_rx(skb) == NET_RX_SUCCESS) {
1568 u64_stats_update_begin(&rx_stats->syncp);
1569 rx_stats->rx_packets++;
1570 rx_stats->rx_bytes += skb->len;
1571 u64_stats_update_end(&rx_stats->syncp);
1572 } else {
1573 skb->dev->stats.rx_dropped++;
1574 }
1575}
1576
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001577static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1578 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001579{
1580 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001581 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001582 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001583 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001584 union vxlan_addr *dst;
1585 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001586 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001587 __be16 df = 0;
1588 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001589 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001590
stephen hemminger823aa872013-04-27 11:31:57 +00001591 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001592 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001593 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001594
Cong Wange4c7ed42013-08-31 13:44:33 +08001595 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001596 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001597 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001598 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001599 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001600 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001601 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001602 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001603
stephen hemmingerd3428942012-10-01 12:32:35 +00001604 old_iph = ip_hdr(skb);
1605
stephen hemmingerd3428942012-10-01 12:32:35 +00001606 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001607 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001608 ttl = 1;
1609
1610 tos = vxlan->tos;
1611 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001612 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001613
Pravin B Shelar49560532013-08-19 11:23:17 -07001614 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001615
Cong Wange4c7ed42013-08-31 13:44:33 +08001616 if (dst->sa.sa_family == AF_INET) {
1617 memset(&fl4, 0, sizeof(fl4));
1618 fl4.flowi4_oif = rdst->remote_ifindex;
1619 fl4.flowi4_tos = RT_TOS(tos);
1620 fl4.daddr = dst->sin.sin_addr.s_addr;
1621 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001622
Cong Wange4c7ed42013-08-31 13:44:33 +08001623 rt = ip_route_output_key(dev_net(dev), &fl4);
1624 if (IS_ERR(rt)) {
1625 netdev_dbg(dev, "no route to %pI4\n",
1626 &dst->sin.sin_addr.s_addr);
1627 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001628 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001629 }
1630
1631 if (rt->dst.dev == dev) {
1632 netdev_dbg(dev, "circular route to %pI4\n",
1633 &dst->sin.sin_addr.s_addr);
1634 dev->stats.collisions++;
1635 goto tx_error;
1636 }
1637
1638 /* Bypass encapsulation if the destination is local */
1639 if (rt->rt_flags & RTCF_LOCAL &&
1640 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1641 struct vxlan_dev *dst_vxlan;
1642
1643 ip_rt_put(rt);
1644 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1645 if (!dst_vxlan)
1646 goto tx_error;
1647 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1648 return;
1649 }
1650
1651 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1652 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1653
Nicolas Dichtel11796182013-09-02 15:34:55 +02001654 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001655 fl4.saddr, dst->sin.sin_addr.s_addr,
1656 tos, ttl, df, src_port, dst_port,
1657 htonl(vni << 8));
1658
1659 if (err < 0)
1660 goto rt_tx_error;
1661 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1662#if IS_ENABLED(CONFIG_IPV6)
1663 } else {
1664 struct sock *sk = vxlan->vn_sock->sock->sk;
1665 struct dst_entry *ndst;
1666 struct flowi6 fl6;
1667 u32 flags;
1668
1669 memset(&fl6, 0, sizeof(fl6));
1670 fl6.flowi6_oif = rdst->remote_ifindex;
1671 fl6.daddr = dst->sin6.sin6_addr;
1672 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08001673 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001674
1675 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1676 netdev_dbg(dev, "no route to %pI6\n",
1677 &dst->sin6.sin6_addr);
1678 dev->stats.tx_carrier_errors++;
1679 goto tx_error;
1680 }
1681
1682 if (ndst->dev == dev) {
1683 netdev_dbg(dev, "circular route to %pI6\n",
1684 &dst->sin6.sin6_addr);
1685 dst_release(ndst);
1686 dev->stats.collisions++;
1687 goto tx_error;
1688 }
1689
1690 /* Bypass encapsulation if the destination is local */
1691 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1692 if (flags & RTF_LOCAL &&
1693 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1694 struct vxlan_dev *dst_vxlan;
1695
1696 dst_release(ndst);
1697 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1698 if (!dst_vxlan)
1699 goto tx_error;
1700 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1701 return;
1702 }
1703
1704 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1705
Nicolas Dichtel11796182013-09-02 15:34:55 +02001706 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001707 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1708 src_port, dst_port, htonl(vni << 8));
1709#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001710 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001711
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001712 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001713
1714drop:
1715 dev->stats.tx_dropped++;
1716 goto tx_free;
1717
Pravin B Shelar49560532013-08-19 11:23:17 -07001718rt_tx_error:
1719 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001720tx_error:
1721 dev->stats.tx_errors++;
1722tx_free:
1723 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001724}
1725
David Stevens66817122013-03-15 04:35:51 +00001726/* Transmit local packets over Vxlan
1727 *
1728 * Outer IP header inherits ECN and DF from inner header.
1729 * Outer UDP destination is the VXLAN assigned port.
1730 * source port is based on hash of flow
1731 */
1732static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1733{
1734 struct vxlan_dev *vxlan = netdev_priv(dev);
1735 struct ethhdr *eth;
1736 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001737 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001738 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001739
1740 skb_reset_mac_header(skb);
1741 eth = eth_hdr(skb);
1742
Cong Wangf564f452013-08-31 13:44:36 +08001743 if ((vxlan->flags & VXLAN_F_PROXY)) {
1744 if (ntohs(eth->h_proto) == ETH_P_ARP)
1745 return arp_reduce(dev, skb);
1746#if IS_ENABLED(CONFIG_IPV6)
1747 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
1748 skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
1749 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1750 struct nd_msg *msg;
1751
1752 msg = (struct nd_msg *)skb_transport_header(skb);
1753 if (msg->icmph.icmp6_code == 0 &&
1754 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1755 return neigh_reduce(dev, skb);
1756 }
1757#endif
1758 }
David Stevens66817122013-03-15 04:35:51 +00001759
1760 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001761 did_rsc = false;
1762
1763 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08001764 (ntohs(eth->h_proto) == ETH_P_IP ||
1765 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00001766 did_rsc = route_shortcircuit(dev, skb);
1767 if (did_rsc)
1768 f = vxlan_find_mac(vxlan, eth->h_dest);
1769 }
1770
David Stevens66817122013-03-15 04:35:51 +00001771 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001772 f = vxlan_find_mac(vxlan, all_zeros_mac);
1773 if (f == NULL) {
1774 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1775 !is_multicast_ether_addr(eth->h_dest))
1776 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001777
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001778 dev->stats.tx_dropped++;
1779 dev_kfree_skb(skb);
1780 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001781 }
David Stevens66817122013-03-15 04:35:51 +00001782 }
1783
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001784 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1785 struct sk_buff *skb1;
1786
1787 skb1 = skb_clone(skb, GFP_ATOMIC);
1788 if (skb1)
1789 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1790 }
1791
1792 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001793 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001794}
1795
stephen hemmingerd3428942012-10-01 12:32:35 +00001796/* Walk the forwarding table and purge stale entries */
1797static void vxlan_cleanup(unsigned long arg)
1798{
1799 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1800 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1801 unsigned int h;
1802
1803 if (!netif_running(vxlan->dev))
1804 return;
1805
1806 spin_lock_bh(&vxlan->hash_lock);
1807 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1808 struct hlist_node *p, *n;
1809 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1810 struct vxlan_fdb *f
1811 = container_of(p, struct vxlan_fdb, hlist);
1812 unsigned long timeout;
1813
stephen hemminger3c172862012-10-26 06:24:34 +00001814 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001815 continue;
1816
1817 timeout = f->used + vxlan->age_interval * HZ;
1818 if (time_before_eq(timeout, jiffies)) {
1819 netdev_dbg(vxlan->dev,
1820 "garbage collect %pM\n",
1821 f->eth_addr);
1822 f->state = NUD_STALE;
1823 vxlan_fdb_destroy(vxlan, f);
1824 } else if (time_before(timeout, next_timer))
1825 next_timer = timeout;
1826 }
1827 }
1828 spin_unlock_bh(&vxlan->hash_lock);
1829
1830 mod_timer(&vxlan->age_timer, next_timer);
1831}
1832
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001833static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1834{
1835 __u32 vni = vxlan->default_dst.remote_vni;
1836
1837 vxlan->vn_sock = vs;
1838 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1839}
1840
stephen hemmingerd3428942012-10-01 12:32:35 +00001841/* Setup stats when device is created */
1842static int vxlan_init(struct net_device *dev)
1843{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001844 struct vxlan_dev *vxlan = netdev_priv(dev);
1845 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1846 struct vxlan_sock *vs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001847
Pravin B Shelare8171042013-03-25 14:49:46 +00001848 dev->tstats = alloc_percpu(struct pcpu_tstats);
1849 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001850 return -ENOMEM;
1851
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001852 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001853 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001854 if (vs) {
1855 /* If we have a socket with same port already, reuse it */
1856 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001857 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001858 } else {
1859 /* otherwise make new socket outside of RTNL */
1860 dev_hold(dev);
1861 queue_work(vxlan_wq, &vxlan->sock_work);
1862 }
1863 spin_unlock(&vn->sock_lock);
1864
stephen hemmingerd3428942012-10-01 12:32:35 +00001865 return 0;
1866}
1867
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001868static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001869{
1870 struct vxlan_fdb *f;
1871
1872 spin_lock_bh(&vxlan->hash_lock);
1873 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1874 if (f)
1875 vxlan_fdb_destroy(vxlan, f);
1876 spin_unlock_bh(&vxlan->hash_lock);
1877}
1878
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001879static void vxlan_uninit(struct net_device *dev)
1880{
1881 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001882 struct vxlan_sock *vs = vxlan->vn_sock;
1883
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001884 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001885
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001886 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07001887 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001888 free_percpu(dev->tstats);
1889}
1890
stephen hemmingerd3428942012-10-01 12:32:35 +00001891/* Start ageing timer and join group when device is brought up */
1892static int vxlan_open(struct net_device *dev)
1893{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001894 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001895 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001896 struct vxlan_sock *vs = vxlan->vn_sock;
1897
1898 /* socket hasn't been created */
1899 if (!vs)
1900 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001901
Cong Wange4c7ed42013-08-31 13:44:33 +08001902 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1903 vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001904 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001905 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001906 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00001907 }
1908
1909 if (vxlan->age_interval)
1910 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1911
1912 return 0;
1913}
1914
1915/* Purge the forwarding table */
1916static void vxlan_flush(struct vxlan_dev *vxlan)
1917{
Cong Wang31fec5a2013-05-27 22:35:52 +00001918 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001919
1920 spin_lock_bh(&vxlan->hash_lock);
1921 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1922 struct hlist_node *p, *n;
1923 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1924 struct vxlan_fdb *f
1925 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001926 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1927 if (!is_zero_ether_addr(f->eth_addr))
1928 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001929 }
1930 }
1931 spin_unlock_bh(&vxlan->hash_lock);
1932}
1933
1934/* Cleanup timer and forwarding table on shutdown */
1935static int vxlan_stop(struct net_device *dev)
1936{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001937 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001938 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001939 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001940
Cong Wange4c7ed42013-08-31 13:44:33 +08001941 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1942 ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001943 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001944 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001945 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001946 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001947
1948 del_timer_sync(&vxlan->age_timer);
1949
1950 vxlan_flush(vxlan);
1951
1952 return 0;
1953}
1954
stephen hemmingerd3428942012-10-01 12:32:35 +00001955/* Stub, nothing needs to be done. */
1956static void vxlan_set_multicast_list(struct net_device *dev)
1957{
1958}
1959
1960static const struct net_device_ops vxlan_netdev_ops = {
1961 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001962 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001963 .ndo_open = vxlan_open,
1964 .ndo_stop = vxlan_stop,
1965 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001966 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001967 .ndo_set_rx_mode = vxlan_set_multicast_list,
1968 .ndo_change_mtu = eth_change_mtu,
1969 .ndo_validate_addr = eth_validate_addr,
1970 .ndo_set_mac_address = eth_mac_addr,
1971 .ndo_fdb_add = vxlan_fdb_add,
1972 .ndo_fdb_del = vxlan_fdb_delete,
1973 .ndo_fdb_dump = vxlan_fdb_dump,
1974};
1975
1976/* Info for udev, that this is a virtual tunnel endpoint */
1977static struct device_type vxlan_type = {
1978 .name = "vxlan",
1979};
1980
stephen hemmingerd3428942012-10-01 12:32:35 +00001981/* Initialize the device structure. */
1982static void vxlan_setup(struct net_device *dev)
1983{
1984 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001985 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001986 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001987
1988 eth_hw_addr_random(dev);
1989 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001990 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
1991 dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM;
1992 else
1993 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001994
1995 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001996 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001997 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1998
1999 dev->tx_queue_len = 0;
2000 dev->features |= NETIF_F_LLTX;
2001 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002002 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002003 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002004 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002005
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002006 dev->vlan_features = dev->features;
2007 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002008 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002009 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002010 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
stephen hemmingerd3428942012-10-01 12:32:35 +00002011 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00002012 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002013
stephen hemminger553675f2013-05-16 11:35:20 +00002014 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002015 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002016 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2017 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002018 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002019
2020 init_timer_deferrable(&vxlan->age_timer);
2021 vxlan->age_timer.function = vxlan_cleanup;
2022 vxlan->age_timer.data = (unsigned long) vxlan;
2023
stephen hemminger05f47d62012-10-09 20:35:50 +00002024 inet_get_local_port_range(&low, &high);
2025 vxlan->port_min = low;
2026 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00002027 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002028
stephen hemmingerd3428942012-10-01 12:32:35 +00002029 vxlan->dev = dev;
2030
2031 for (h = 0; h < FDB_HASH_SIZE; ++h)
2032 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2033}
2034
2035static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2036 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002037 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002038 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002039 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2040 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002041 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002042 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2043 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2044 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2045 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2046 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002047 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002048 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2049 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2050 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2051 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002052 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002053};
2054
2055static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2056{
2057 if (tb[IFLA_ADDRESS]) {
2058 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2059 pr_debug("invalid link address (not ethernet)\n");
2060 return -EINVAL;
2061 }
2062
2063 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2064 pr_debug("invalid all zero ethernet address\n");
2065 return -EADDRNOTAVAIL;
2066 }
2067 }
2068
2069 if (!data)
2070 return -EINVAL;
2071
2072 if (data[IFLA_VXLAN_ID]) {
2073 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2074 if (id >= VXLAN_VID_MASK)
2075 return -ERANGE;
2076 }
2077
stephen hemminger05f47d62012-10-09 20:35:50 +00002078 if (data[IFLA_VXLAN_PORT_RANGE]) {
2079 const struct ifla_vxlan_port_range *p
2080 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2081
2082 if (ntohs(p->high) < ntohs(p->low)) {
2083 pr_debug("port range %u .. %u not valid\n",
2084 ntohs(p->low), ntohs(p->high));
2085 return -EINVAL;
2086 }
2087 }
2088
stephen hemmingerd3428942012-10-01 12:32:35 +00002089 return 0;
2090}
2091
Yan Burman1b13c972013-01-29 23:43:07 +00002092static void vxlan_get_drvinfo(struct net_device *netdev,
2093 struct ethtool_drvinfo *drvinfo)
2094{
2095 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2096 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2097}
2098
2099static const struct ethtool_ops vxlan_ethtool_ops = {
2100 .get_drvinfo = vxlan_get_drvinfo,
2101 .get_link = ethtool_op_get_link,
2102};
2103
stephen hemminger553675f2013-05-16 11:35:20 +00002104static void vxlan_del_work(struct work_struct *work)
2105{
2106 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2107
2108 sk_release_kernel(vs->sock->sk);
2109 kfree_rcu(vs, rcu);
2110}
2111
Cong Wange4c7ed42013-08-31 13:44:33 +08002112#if IS_ENABLED(CONFIG_IPV6)
2113/* Create UDP socket for encapsulation receive. AF_INET6 socket
2114 * could be used for both IPv4 and IPv6 communications, but
2115 * users may set bindv6only=1.
2116 */
2117static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
stephen hemminger553675f2013-05-16 11:35:20 +00002118{
stephen hemminger553675f2013-05-16 11:35:20 +00002119 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08002120 struct socket *sock;
2121 struct sockaddr_in6 vxlan_addr = {
2122 .sin6_family = AF_INET6,
2123 .sin6_port = port,
2124 };
2125 int rc, val = 1;
2126
2127 rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
2128 if (rc < 0) {
2129 pr_debug("UDPv6 socket create failed\n");
2130 return rc;
2131 }
2132
2133 /* Put in proper namespace */
2134 sk = sock->sk;
2135 sk_change_net(sk, net);
2136
2137 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
2138 (char *)&val, sizeof(val));
2139 rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
2140 sizeof(struct sockaddr_in6));
2141 if (rc < 0) {
2142 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2143 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
2144 sk_release_kernel(sk);
2145 return rc;
2146 }
2147 /* At this point, IPv6 module should have been loaded in
2148 * sock_create_kern().
2149 */
2150 BUG_ON(!ipv6_stub);
2151
2152 *psock = sock;
2153 /* Disable multicast loopback */
2154 inet_sk(sk)->mc_loop = 0;
2155 return 0;
2156}
2157
2158#else
2159
2160static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
2161{
2162 return -EPFNOSUPPORT;
2163}
2164#endif
2165
2166static int create_v4_sock(struct net *net, __be16 port, struct socket **psock)
2167{
2168 struct sock *sk;
2169 struct socket *sock;
stephen hemminger553675f2013-05-16 11:35:20 +00002170 struct sockaddr_in vxlan_addr = {
2171 .sin_family = AF_INET,
2172 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07002173 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00002174 };
2175 int rc;
Cong Wange4c7ed42013-08-31 13:44:33 +08002176
2177 /* Create UDP socket for encapsulation receive. */
2178 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
2179 if (rc < 0) {
2180 pr_debug("UDP socket create failed\n");
2181 return rc;
2182 }
2183
2184 /* Put in proper namespace */
2185 sk = sock->sk;
2186 sk_change_net(sk, net);
2187
2188 rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
2189 sizeof(vxlan_addr));
2190 if (rc < 0) {
2191 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2192 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
2193 sk_release_kernel(sk);
2194 return rc;
2195 }
2196
2197 *psock = sock;
2198 /* Disable multicast loopback */
2199 inet_sk(sk)->mc_loop = 0;
2200 return 0;
2201}
2202
2203/* Create new listen socket if needed */
2204static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2205 vxlan_rcv_t *rcv, void *data, bool ipv6)
2206{
2207 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2208 struct vxlan_sock *vs;
2209 struct socket *sock;
2210 struct sock *sk;
2211 int rc = 0;
Cong Wang31fec5a2013-05-27 22:35:52 +00002212 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00002213
2214 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002215 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002216 return ERR_PTR(-ENOMEM);
2217
2218 for (h = 0; h < VNI_HASH_SIZE; ++h)
2219 INIT_HLIST_HEAD(&vs->vni_list[h]);
2220
2221 INIT_WORK(&vs->del_work, vxlan_del_work);
2222
Cong Wange4c7ed42013-08-31 13:44:33 +08002223 if (ipv6)
2224 rc = create_v6_sock(net, port, &sock);
2225 else
2226 rc = create_v4_sock(net, port, &sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002227 if (rc < 0) {
stephen hemminger553675f2013-05-16 11:35:20 +00002228 kfree(vs);
2229 return ERR_PTR(rc);
2230 }
2231
Cong Wange4c7ed42013-08-31 13:44:33 +08002232 vs->sock = sock;
2233 sk = sock->sk;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002234 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002235 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002236 vs->data = data;
stephen hemminger553675f2013-05-16 11:35:20 +00002237
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002238 spin_lock(&vn->sock_lock);
2239 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2240 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002241
2242 /* Mark socket as an encapsulation socket. */
2243 udp_sk(sk)->encap_type = 1;
2244 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
Cong Wange4c7ed42013-08-31 13:44:33 +08002245#if IS_ENABLED(CONFIG_IPV6)
2246 if (ipv6)
2247 ipv6_stub->udpv6_encap_enable();
2248 else
2249#endif
2250 udp_encap_enable();
2251
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002252 return vs;
2253}
stephen hemminger553675f2013-05-16 11:35:20 +00002254
Pravin B Shelar012a5722013-08-19 11:23:07 -07002255struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2256 vxlan_rcv_t *rcv, void *data,
Cong Wange4c7ed42013-08-31 13:44:33 +08002257 bool no_share, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002258{
2259 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2260 struct vxlan_sock *vs;
2261
Cong Wange4c7ed42013-08-31 13:44:33 +08002262 vs = vxlan_socket_create(net, port, rcv, data, ipv6);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002263 if (!IS_ERR(vs))
2264 return vs;
2265
Pravin B Shelar012a5722013-08-19 11:23:07 -07002266 if (no_share) /* Return error if sharing is not allowed. */
2267 return vs;
2268
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002269 spin_lock(&vn->sock_lock);
2270 vs = vxlan_find_sock(net, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002271 if (vs) {
2272 if (vs->rcv == rcv)
2273 atomic_inc(&vs->refcnt);
2274 else
2275 vs = ERR_PTR(-EBUSY);
2276 }
2277 spin_unlock(&vn->sock_lock);
2278
2279 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002280 vs = ERR_PTR(-EINVAL);
2281
stephen hemminger553675f2013-05-16 11:35:20 +00002282 return vs;
2283}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002284EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002285
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002286/* Scheduled at device creation to bind to a socket */
2287static void vxlan_sock_work(struct work_struct *work)
2288{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002289 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2290 struct net *net = dev_net(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002291 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002292 __be16 port = vxlan->dst_port;
2293 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002294
Cong Wange4c7ed42013-08-31 13:44:33 +08002295 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002296 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002297 if (!IS_ERR(nvs))
2298 vxlan_vs_add_dev(nvs, vxlan);
2299 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002300
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002301 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002302}
2303
stephen hemmingerd3428942012-10-01 12:32:35 +00002304static int vxlan_newlink(struct net *net, struct net_device *dev,
2305 struct nlattr *tb[], struct nlattr *data[])
2306{
stephen hemminger553675f2013-05-16 11:35:20 +00002307 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002308 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002309 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002310 __u32 vni;
2311 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002312 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002313
2314 if (!data[IFLA_VXLAN_ID])
2315 return -EINVAL;
2316
2317 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002318 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002319
Cong Wange4c7ed42013-08-31 13:44:33 +08002320 if (data[IFLA_VXLAN_GROUP]) {
2321 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2322 dst->remote_ip.sa.sa_family = AF_INET;
2323 } else if (data[IFLA_VXLAN_GROUP6]) {
2324 if (!IS_ENABLED(CONFIG_IPV6))
2325 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002326
Cong Wange4c7ed42013-08-31 13:44:33 +08002327 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2328 sizeof(struct in6_addr));
2329 dst->remote_ip.sa.sa_family = AF_INET6;
2330 use_ipv6 = true;
2331 }
2332
2333 if (data[IFLA_VXLAN_LOCAL]) {
2334 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2335 vxlan->saddr.sa.sa_family = AF_INET;
2336 } else if (data[IFLA_VXLAN_LOCAL6]) {
2337 if (!IS_ENABLED(CONFIG_IPV6))
2338 return -EPFNOSUPPORT;
2339
2340 /* TODO: respect scope id */
2341 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2342 sizeof(struct in6_addr));
2343 vxlan->saddr.sa.sa_family = AF_INET6;
2344 use_ipv6 = true;
2345 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002346
stephen hemminger34e02aa2012-10-09 20:35:53 +00002347 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002348 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002349 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002350 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002351
stephen hemminger34e02aa2012-10-09 20:35:53 +00002352 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002353 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002354 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002355 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002356
Cong Wange4c7ed42013-08-31 13:44:33 +08002357#if IS_ENABLED(CONFIG_IPV6)
2358 if (use_ipv6) {
2359 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2360 if (idev && idev->cnf.disable_ipv6) {
2361 pr_info("IPv6 is disabled via sysctl\n");
2362 return -EPERM;
2363 }
2364 vxlan->flags |= VXLAN_F_IPV6;
2365 }
2366#endif
2367
stephen hemminger34e02aa2012-10-09 20:35:53 +00002368 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002369 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002370
2371 /* update header length based on lower device */
2372 dev->hard_header_len = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002373 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
stephen hemmingerd3428942012-10-01 12:32:35 +00002374 }
2375
2376 if (data[IFLA_VXLAN_TOS])
2377 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2378
Vincent Bernatafb97182012-10-30 10:27:16 +00002379 if (data[IFLA_VXLAN_TTL])
2380 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2381
stephen hemmingerd3428942012-10-01 12:32:35 +00002382 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002383 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002384
2385 if (data[IFLA_VXLAN_AGEING])
2386 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2387 else
2388 vxlan->age_interval = FDB_AGE_DEFAULT;
2389
David Stevense4f67ad2012-11-20 02:50:14 +00002390 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2391 vxlan->flags |= VXLAN_F_PROXY;
2392
2393 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2394 vxlan->flags |= VXLAN_F_RSC;
2395
2396 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2397 vxlan->flags |= VXLAN_F_L2MISS;
2398
2399 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2400 vxlan->flags |= VXLAN_F_L3MISS;
2401
stephen hemmingerd3428942012-10-01 12:32:35 +00002402 if (data[IFLA_VXLAN_LIMIT])
2403 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2404
stephen hemminger05f47d62012-10-09 20:35:50 +00002405 if (data[IFLA_VXLAN_PORT_RANGE]) {
2406 const struct ifla_vxlan_port_range *p
2407 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2408 vxlan->port_min = ntohs(p->low);
2409 vxlan->port_max = ntohs(p->high);
2410 }
2411
stephen hemminger823aa872013-04-27 11:31:57 +00002412 if (data[IFLA_VXLAN_PORT])
2413 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2414
stephen hemminger553675f2013-05-16 11:35:20 +00002415 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
2416 pr_info("duplicate VNI %u\n", vni);
2417 return -EEXIST;
2418 }
2419
Yan Burman1b13c972013-01-29 23:43:07 +00002420 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
2421
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002422 /* create an fdb entry for default destination */
2423 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Cong Wange4c7ed42013-08-31 13:44:33 +08002424 &vxlan->default_dst.remote_ip,
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002425 NUD_REACHABLE|NUD_PERMANENT,
2426 NLM_F_EXCL|NLM_F_CREATE,
2427 vxlan->dst_port, vxlan->default_dst.remote_vni,
2428 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002429 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00002430 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00002431
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002432 err = register_netdevice(dev);
2433 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002434 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002435 return err;
2436 }
2437
stephen hemminger553675f2013-05-16 11:35:20 +00002438 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002439
2440 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002441}
2442
2443static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2444{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002445 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002446 struct vxlan_dev *vxlan = netdev_priv(dev);
2447
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002448 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002449 if (!hlist_unhashed(&vxlan->hlist))
2450 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002451 spin_unlock(&vn->sock_lock);
2452
stephen hemminger553675f2013-05-16 11:35:20 +00002453 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002454 unregister_netdevice_queue(dev, head);
2455}
2456
2457static size_t vxlan_get_size(const struct net_device *dev)
2458{
2459
2460 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002461 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002462 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002463 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002464 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2465 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2466 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002467 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2468 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2469 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2470 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002471 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2472 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002473 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00002474 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00002475 0;
2476}
2477
2478static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2479{
2480 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002481 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002482 struct ifla_vxlan_port_range ports = {
2483 .low = htons(vxlan->port_min),
2484 .high = htons(vxlan->port_max),
2485 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002486
Atzm Watanabec7995c42013-04-16 02:50:52 +00002487 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002488 goto nla_put_failure;
2489
Cong Wange4c7ed42013-08-31 13:44:33 +08002490 if (!vxlan_addr_any(&dst->remote_ip)) {
2491 if (dst->remote_ip.sa.sa_family == AF_INET) {
2492 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2493 dst->remote_ip.sin.sin_addr.s_addr))
2494 goto nla_put_failure;
2495#if IS_ENABLED(CONFIG_IPV6)
2496 } else {
2497 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2498 &dst->remote_ip.sin6.sin6_addr))
2499 goto nla_put_failure;
2500#endif
2501 }
2502 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002503
Atzm Watanabec7995c42013-04-16 02:50:52 +00002504 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002505 goto nla_put_failure;
2506
Cong Wange4c7ed42013-08-31 13:44:33 +08002507 if (!vxlan_addr_any(&vxlan->saddr)) {
2508 if (vxlan->saddr.sa.sa_family == AF_INET) {
2509 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2510 vxlan->saddr.sin.sin_addr.s_addr))
2511 goto nla_put_failure;
2512#if IS_ENABLED(CONFIG_IPV6)
2513 } else {
2514 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2515 &vxlan->saddr.sin6.sin6_addr))
2516 goto nla_put_failure;
2517#endif
2518 }
2519 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002520
2521 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2522 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002523 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2524 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2525 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2526 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2527 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2528 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2529 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2530 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2531 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002532 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002533 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2534 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00002535 goto nla_put_failure;
2536
stephen hemminger05f47d62012-10-09 20:35:50 +00002537 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2538 goto nla_put_failure;
2539
stephen hemmingerd3428942012-10-01 12:32:35 +00002540 return 0;
2541
2542nla_put_failure:
2543 return -EMSGSIZE;
2544}
2545
2546static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2547 .kind = "vxlan",
2548 .maxtype = IFLA_VXLAN_MAX,
2549 .policy = vxlan_policy,
2550 .priv_size = sizeof(struct vxlan_dev),
2551 .setup = vxlan_setup,
2552 .validate = vxlan_validate,
2553 .newlink = vxlan_newlink,
2554 .dellink = vxlan_dellink,
2555 .get_size = vxlan_get_size,
2556 .fill_info = vxlan_fill_info,
2557};
2558
2559static __net_init int vxlan_init_net(struct net *net)
2560{
2561 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002562 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002563
stephen hemminger553675f2013-05-16 11:35:20 +00002564 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002565 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002566
stephen hemminger553675f2013-05-16 11:35:20 +00002567 for (h = 0; h < PORT_HASH_SIZE; ++h)
2568 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002569
2570 return 0;
2571}
2572
2573static __net_exit void vxlan_exit_net(struct net *net)
2574{
2575 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002576 struct vxlan_dev *vxlan;
stephen hemminger372675a2013-07-18 08:38:26 -07002577 LIST_HEAD(list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002578
2579 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00002580 list_for_each_entry(vxlan, &vn->vxlan_list, next)
stephen hemminger372675a2013-07-18 08:38:26 -07002581 unregister_netdevice_queue(vxlan->dev, &list);
2582 unregister_netdevice_many(&list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002583 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00002584}
2585
2586static struct pernet_operations vxlan_net_ops = {
2587 .init = vxlan_init_net,
2588 .exit = vxlan_exit_net,
2589 .id = &vxlan_net_id,
2590 .size = sizeof(struct vxlan_net),
2591};
2592
2593static int __init vxlan_init_module(void)
2594{
2595 int rc;
2596
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002597 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2598 if (!vxlan_wq)
2599 return -ENOMEM;
2600
stephen hemmingerd3428942012-10-01 12:32:35 +00002601 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2602
2603 rc = register_pernet_device(&vxlan_net_ops);
2604 if (rc)
2605 goto out1;
2606
2607 rc = rtnl_link_register(&vxlan_link_ops);
2608 if (rc)
2609 goto out2;
2610
2611 return 0;
2612
2613out2:
2614 unregister_pernet_device(&vxlan_net_ops);
2615out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002616 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002617 return rc;
2618}
Cong Wang7332a132013-05-27 22:35:53 +00002619late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002620
2621static void __exit vxlan_cleanup_module(void)
2622{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002623 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002624 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07002625 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00002626 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00002627}
2628module_exit(vxlan_cleanup_module);
2629
2630MODULE_LICENSE("GPL");
2631MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002632MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00002633MODULE_ALIAS_RTNL_LINK("vxlan");