blob: 0fa4b9108e8264ddca1a9f721de3547263bb74a0 [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)
Pravin B Shelar7ce04752013-08-19 11:22:54 -070063#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
stephen hemmingerd3428942012-10-01 12:32:35 +000064
65#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
66
67/* VXLAN protocol header */
68struct vxlanhdr {
69 __be32 vx_flags;
70 __be32 vx_vni;
71};
72
stephen hemminger23c578b2013-04-27 11:31:53 +000073/* UDP port for VXLAN traffic.
74 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070075 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000076 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070077static unsigned short vxlan_port __read_mostly = 8472;
78module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000079MODULE_PARM_DESC(udp_port, "Destination UDP port");
80
81static bool log_ecn_error = true;
82module_param(log_ecn_error, bool, 0644);
83MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
84
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070085static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000086
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030087static const u8 all_zeros_mac[ETH_ALEN];
88
stephen hemminger553675f2013-05-16 11:35:20 +000089/* per-network namespace private data for this module */
90struct vxlan_net {
91 struct list_head vxlan_list;
92 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070093 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000094};
95
Cong Wange4c7ed42013-08-31 13:44:33 +080096union vxlan_addr {
97 struct sockaddr_in sin;
98 struct sockaddr_in6 sin6;
99 struct sockaddr sa;
100};
101
David Stevens66817122013-03-15 04:35:51 +0000102struct vxlan_rdst {
Cong Wange4c7ed42013-08-31 13:44:33 +0800103 union vxlan_addr remote_ip;
David Stevens66817122013-03-15 04:35:51 +0000104 __be16 remote_port;
105 u32 remote_vni;
106 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700107 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300108 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000109};
110
stephen hemmingerd3428942012-10-01 12:32:35 +0000111/* Forwarding table entry */
112struct vxlan_fdb {
113 struct hlist_node hlist; /* linked list of entries */
114 struct rcu_head rcu;
115 unsigned long updated; /* jiffies */
116 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700117 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000118 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000119 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000120 u8 eth_addr[ETH_ALEN];
121};
122
stephen hemmingerd3428942012-10-01 12:32:35 +0000123/* Pseudo network device */
124struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000125 struct hlist_node hlist; /* vni hash table */
126 struct list_head next; /* vxlan's per namespace list */
127 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000128 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000129 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800130 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000131 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000132 __u16 port_min; /* source port range */
133 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000134 __u8 tos; /* TOS override */
135 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000136 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000137
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700138 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700139 struct work_struct igmp_join;
140 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700141
stephen hemmingerd3428942012-10-01 12:32:35 +0000142 unsigned long age_interval;
143 struct timer_list age_timer;
144 spinlock_t hash_lock;
145 unsigned int addrcnt;
146 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000147
148 struct hlist_head fdb_head[FDB_HASH_SIZE];
149};
150
David Stevense4f67ad2012-11-20 02:50:14 +0000151#define VXLAN_F_LEARN 0x01
152#define VXLAN_F_PROXY 0x02
153#define VXLAN_F_RSC 0x04
154#define VXLAN_F_L2MISS 0x08
155#define VXLAN_F_L3MISS 0x10
Cong Wange4c7ed42013-08-31 13:44:33 +0800156#define VXLAN_F_IPV6 0x20 /* internal flag */
David Stevense4f67ad2012-11-20 02:50:14 +0000157
stephen hemmingerd3428942012-10-01 12:32:35 +0000158/* salt for hash table */
159static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700160static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000161
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700162static void vxlan_sock_work(struct work_struct *work);
163
Cong Wange4c7ed42013-08-31 13:44:33 +0800164#if IS_ENABLED(CONFIG_IPV6)
165static inline
166bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
167{
168 if (a->sa.sa_family != b->sa.sa_family)
169 return false;
170 if (a->sa.sa_family == AF_INET6)
171 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
172 else
173 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
174}
175
176static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
177{
178 if (ipa->sa.sa_family == AF_INET6)
179 return ipv6_addr_any(&ipa->sin6.sin6_addr);
180 else
181 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
182}
183
184static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
185{
186 if (ipa->sa.sa_family == AF_INET6)
187 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
188 else
189 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
190}
191
192static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
193{
194 if (nla_len(nla) >= sizeof(struct in6_addr)) {
195 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
196 ip->sa.sa_family = AF_INET6;
197 return 0;
198 } else if (nla_len(nla) >= sizeof(__be32)) {
199 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
200 ip->sa.sa_family = AF_INET;
201 return 0;
202 } else {
203 return -EAFNOSUPPORT;
204 }
205}
206
207static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
208 const union vxlan_addr *ip)
209{
210 if (ip->sa.sa_family == AF_INET6)
211 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
212 else
213 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
214}
215
216#else /* !CONFIG_IPV6 */
217
218static inline
219bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
220{
221 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
222}
223
224static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
225{
226 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
227}
228
229static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
230{
231 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
232}
233
234static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
235{
236 if (nla_len(nla) >= sizeof(struct in6_addr)) {
237 return -EAFNOSUPPORT;
238 } else if (nla_len(nla) >= sizeof(__be32)) {
239 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
240 ip->sa.sa_family = AF_INET;
241 return 0;
242 } else {
243 return -EAFNOSUPPORT;
244 }
245}
246
247static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
248 const union vxlan_addr *ip)
249{
250 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
251}
252#endif
253
stephen hemminger553675f2013-05-16 11:35:20 +0000254/* Virtual Network hash table head */
255static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
256{
257 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
258}
259
260/* Socket hash table head */
261static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000262{
263 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
264
stephen hemminger553675f2013-05-16 11:35:20 +0000265 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
266}
267
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700268/* First remote destination for a forwarding entry.
269 * Guaranteed to be non-NULL because remotes are never deleted.
270 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700271static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700272{
stephen hemminger5ca54612013-08-04 17:17:39 -0700273 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
274}
275
276static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
277{
278 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700279}
280
stephen hemminger553675f2013-05-16 11:35:20 +0000281/* Find VXLAN socket based on network namespace and UDP port */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -0700282static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000283{
284 struct vxlan_sock *vs;
285
286 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
287 if (inet_sk(vs->sock->sk)->inet_sport == port)
288 return vs;
289 }
290 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000291}
292
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700293static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000294{
295 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000296
stephen hemminger553675f2013-05-16 11:35:20 +0000297 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000298 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000299 return vxlan;
300 }
301
302 return NULL;
303}
304
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700305/* Look up VNI in a per net namespace table */
306static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
307{
308 struct vxlan_sock *vs;
309
310 vs = vxlan_find_sock(net, port);
311 if (!vs)
312 return NULL;
313
314 return vxlan_vs_find_vni(vs, id);
315}
316
stephen hemmingerd3428942012-10-01 12:32:35 +0000317/* Fill in neighbour message in skbuff. */
318static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700319 const struct vxlan_fdb *fdb,
320 u32 portid, u32 seq, int type, unsigned int flags,
321 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000322{
323 unsigned long now = jiffies;
324 struct nda_cacheinfo ci;
325 struct nlmsghdr *nlh;
326 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000327 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000328
329 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
330 if (nlh == NULL)
331 return -EMSGSIZE;
332
333 ndm = nlmsg_data(nlh);
334 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000335
336 send_eth = send_ip = true;
337
338 if (type == RTM_GETNEIGH) {
339 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800340 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000341 send_eth = !is_zero_ether_addr(fdb->eth_addr);
342 } else
343 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000344 ndm->ndm_state = fdb->state;
345 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000346 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000347 ndm->ndm_type = NDA_DST;
348
David Stevense4f67ad2012-11-20 02:50:14 +0000349 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000350 goto nla_put_failure;
351
Cong Wange4c7ed42013-08-31 13:44:33 +0800352 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000353 goto nla_put_failure;
354
stephen hemminger823aa872013-04-27 11:31:57 +0000355 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000356 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
357 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000358 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700359 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000360 goto nla_put_failure;
361 if (rdst->remote_ifindex &&
362 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000363 goto nla_put_failure;
364
365 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
366 ci.ndm_confirmed = 0;
367 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
368 ci.ndm_refcnt = 0;
369
370 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
371 goto nla_put_failure;
372
373 return nlmsg_end(skb, nlh);
374
375nla_put_failure:
376 nlmsg_cancel(skb, nlh);
377 return -EMSGSIZE;
378}
379
380static inline size_t vxlan_nlmsg_size(void)
381{
382 return NLMSG_ALIGN(sizeof(struct ndmsg))
383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000388 + nla_total_size(sizeof(struct nda_cacheinfo));
389}
390
391static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700392 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000393{
394 struct net *net = dev_net(vxlan->dev);
395 struct sk_buff *skb;
396 int err = -ENOBUFS;
397
398 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
399 if (skb == NULL)
400 goto errout;
401
stephen hemminger5ca54612013-08-04 17:17:39 -0700402 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
403 first_remote_rtnl(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000404 if (err < 0) {
405 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
406 WARN_ON(err == -EMSGSIZE);
407 kfree_skb(skb);
408 goto errout;
409 }
410
411 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
412 return;
413errout:
414 if (err < 0)
415 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
416}
417
Cong Wange4c7ed42013-08-31 13:44:33 +0800418static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000419{
420 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700421 struct vxlan_fdb f = {
422 .state = NUD_STALE,
423 };
424 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800425 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700426 .remote_vni = VXLAN_N_VID,
427 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700428
429 INIT_LIST_HEAD(&f.remotes);
430 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000431
432 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
433}
434
435static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
436{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700437 struct vxlan_fdb f = {
438 .state = NUD_STALE,
439 };
David Stevense4f67ad2012-11-20 02:50:14 +0000440
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700441 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000442 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
443
444 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
445}
446
stephen hemmingerd3428942012-10-01 12:32:35 +0000447/* Hash Ethernet address */
448static u32 eth_hash(const unsigned char *addr)
449{
450 u64 value = get_unaligned((u64 *)addr);
451
452 /* only want 6 bytes */
453#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000454 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000455#else
456 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000457#endif
458 return hash_64(value, FDB_HASH_BITS);
459}
460
461/* Hash chain to use given mac address */
462static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
463 const u8 *mac)
464{
465 return &vxlan->fdb_head[eth_hash(mac)];
466}
467
468/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000469static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000470 const u8 *mac)
471
472{
473 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
474 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000475
Sasha Levinb67bfe02013-02-27 17:06:00 -0800476 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700477 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000478 return f;
479 }
480
481 return NULL;
482}
483
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000484static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
485 const u8 *mac)
486{
487 struct vxlan_fdb *f;
488
489 f = __vxlan_find_mac(vxlan, mac);
490 if (f)
491 f->used = jiffies;
492
493 return f;
494}
495
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300496/* caller should hold vxlan->hash_lock */
497static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800498 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300499 __u32 vni, __u32 ifindex)
500{
501 struct vxlan_rdst *rd;
502
503 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800504 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300505 rd->remote_port == port &&
506 rd->remote_vni == vni &&
507 rd->remote_ifindex == ifindex)
508 return rd;
509 }
510
511 return NULL;
512}
513
Thomas Richter906dc182013-07-19 17:20:07 +0200514/* Replace destination of unicast mac */
515static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800516 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200517{
518 struct vxlan_rdst *rd;
519
520 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
521 if (rd)
522 return 0;
523
524 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
525 if (!rd)
526 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800527 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200528 rd->remote_port = port;
529 rd->remote_vni = vni;
530 rd->remote_ifindex = ifindex;
531 return 1;
532}
533
David Stevens66817122013-03-15 04:35:51 +0000534/* Add/update destinations for multicast */
535static int vxlan_fdb_append(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800536 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000537{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700538 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000539
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300540 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
541 if (rd)
542 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700543
David Stevens66817122013-03-15 04:35:51 +0000544 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
545 if (rd == NULL)
546 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800547 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000548 rd->remote_port = port;
549 rd->remote_vni = vni;
550 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700551
552 list_add_tail_rcu(&rd->list, &f->remotes);
553
David Stevens66817122013-03-15 04:35:51 +0000554 return 1;
555}
556
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700557/* Notify netdevs that UDP port started listening */
558static void vxlan_notify_add_rx_port(struct sock *sk)
559{
560 struct net_device *dev;
561 struct net *net = sock_net(sk);
562 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700563 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700564
565 rcu_read_lock();
566 for_each_netdev_rcu(net, dev) {
567 if (dev->netdev_ops->ndo_add_vxlan_port)
568 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
569 port);
570 }
571 rcu_read_unlock();
572}
573
574/* Notify netdevs that UDP port is no more listening */
575static void vxlan_notify_del_rx_port(struct sock *sk)
576{
577 struct net_device *dev;
578 struct net *net = sock_net(sk);
579 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700580 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700581
582 rcu_read_lock();
583 for_each_netdev_rcu(net, dev) {
584 if (dev->netdev_ops->ndo_del_vxlan_port)
585 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
586 port);
587 }
588 rcu_read_unlock();
589}
590
stephen hemmingerd3428942012-10-01 12:32:35 +0000591/* Add new entry to forwarding table -- assumes lock held */
592static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800593 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000594 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000595 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000596 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000597{
598 struct vxlan_fdb *f;
599 int notify = 0;
600
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000601 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000602 if (f) {
603 if (flags & NLM_F_EXCL) {
604 netdev_dbg(vxlan->dev,
605 "lost race to create %pM\n", mac);
606 return -EEXIST;
607 }
608 if (f->state != state) {
609 f->state = state;
610 f->updated = jiffies;
611 notify = 1;
612 }
David Stevensae884082013-04-19 00:36:26 +0000613 if (f->flags != ndm_flags) {
614 f->flags = ndm_flags;
615 f->updated = jiffies;
616 notify = 1;
617 }
Thomas Richter906dc182013-07-19 17:20:07 +0200618 if ((flags & NLM_F_REPLACE)) {
619 /* Only change unicasts */
620 if (!(is_multicast_ether_addr(f->eth_addr) ||
621 is_zero_ether_addr(f->eth_addr))) {
622 int rc = vxlan_fdb_replace(f, ip, port, vni,
623 ifindex);
624
625 if (rc < 0)
626 return rc;
627 notify |= rc;
628 } else
629 return -EOPNOTSUPP;
630 }
David Stevens66817122013-03-15 04:35:51 +0000631 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300632 (is_multicast_ether_addr(f->eth_addr) ||
633 is_zero_ether_addr(f->eth_addr))) {
David Stevens66817122013-03-15 04:35:51 +0000634 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
635
636 if (rc < 0)
637 return rc;
638 notify |= rc;
639 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000640 } else {
641 if (!(flags & NLM_F_CREATE))
642 return -ENOENT;
643
644 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
645 return -ENOSPC;
646
Thomas Richter906dc182013-07-19 17:20:07 +0200647 /* Disallow replace to add a multicast entry */
648 if ((flags & NLM_F_REPLACE) &&
649 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
650 return -EOPNOTSUPP;
651
Cong Wange4c7ed42013-08-31 13:44:33 +0800652 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000653 f = kmalloc(sizeof(*f), GFP_ATOMIC);
654 if (!f)
655 return -ENOMEM;
656
657 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000658 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000659 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000660 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700661 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000662 memcpy(f->eth_addr, mac, ETH_ALEN);
663
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700664 vxlan_fdb_append(f, ip, port, vni, ifindex);
665
stephen hemmingerd3428942012-10-01 12:32:35 +0000666 ++vxlan->addrcnt;
667 hlist_add_head_rcu(&f->hlist,
668 vxlan_fdb_head(vxlan, mac));
669 }
670
671 if (notify)
672 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
673
674 return 0;
675}
676
Wei Yongjun6706c822013-04-11 19:00:35 +0000677static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000678{
679 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700680 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000681
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700682 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000683 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000684 kfree(f);
685}
686
stephen hemmingerd3428942012-10-01 12:32:35 +0000687static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
688{
689 netdev_dbg(vxlan->dev,
690 "delete %pM\n", f->eth_addr);
691
692 --vxlan->addrcnt;
693 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
694
695 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000696 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000697}
698
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300699static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800700 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300701{
702 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800703 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300704
705 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800706 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
707 if (err)
708 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300709 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800710 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
711 if (remote->sa.sa_family == AF_INET) {
712 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
713 ip->sa.sa_family = AF_INET;
714#if IS_ENABLED(CONFIG_IPV6)
715 } else {
716 ip->sin6.sin6_addr = in6addr_any;
717 ip->sa.sa_family = AF_INET6;
718#endif
719 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300720 }
721
722 if (tb[NDA_PORT]) {
723 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
724 return -EINVAL;
725 *port = nla_get_be16(tb[NDA_PORT]);
726 } else {
727 *port = vxlan->dst_port;
728 }
729
730 if (tb[NDA_VNI]) {
731 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
732 return -EINVAL;
733 *vni = nla_get_u32(tb[NDA_VNI]);
734 } else {
735 *vni = vxlan->default_dst.remote_vni;
736 }
737
738 if (tb[NDA_IFINDEX]) {
739 struct net_device *tdev;
740
741 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
742 return -EINVAL;
743 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800744 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300745 if (!tdev)
746 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300747 } else {
748 *ifindex = 0;
749 }
750
751 return 0;
752}
753
stephen hemmingerd3428942012-10-01 12:32:35 +0000754/* Add static entry (via netlink) */
755static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
756 struct net_device *dev,
757 const unsigned char *addr, u16 flags)
758{
759 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300760 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800761 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000762 __be16 port;
763 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000764 int err;
765
766 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
767 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
768 ndm->ndm_state);
769 return -EINVAL;
770 }
771
772 if (tb[NDA_DST] == NULL)
773 return -EINVAL;
774
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300775 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
776 if (err)
777 return err;
David Stevens66817122013-03-15 04:35:51 +0000778
stephen hemmingerd3428942012-10-01 12:32:35 +0000779 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800780 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000781 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000782 spin_unlock_bh(&vxlan->hash_lock);
783
784 return err;
785}
786
787/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000788static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
789 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000790 const unsigned char *addr)
791{
792 struct vxlan_dev *vxlan = netdev_priv(dev);
793 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300794 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800795 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300796 __be16 port;
797 u32 vni, ifindex;
798 int err;
799
800 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
801 if (err)
802 return err;
803
804 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000805
806 spin_lock_bh(&vxlan->hash_lock);
807 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300808 if (!f)
809 goto out;
810
Cong Wange4c7ed42013-08-31 13:44:33 +0800811 if (!vxlan_addr_any(&ip)) {
812 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300813 if (!rd)
814 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000815 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300816
817 err = 0;
818
819 /* remove a destination if it's not the only one on the list,
820 * otherwise destroy the fdb entry
821 */
822 if (rd && !list_is_singular(&f->remotes)) {
823 list_del_rcu(&rd->list);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800824 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300825 goto out;
826 }
827
828 vxlan_fdb_destroy(vxlan, f);
829
830out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000831 spin_unlock_bh(&vxlan->hash_lock);
832
833 return err;
834}
835
836/* Dump forwarding table */
837static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
838 struct net_device *dev, int idx)
839{
840 struct vxlan_dev *vxlan = netdev_priv(dev);
841 unsigned int h;
842
843 for (h = 0; h < FDB_HASH_SIZE; ++h) {
844 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000845 int err;
846
Sasha Levinb67bfe02013-02-27 17:06:00 -0800847 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000848 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000849
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700850 if (idx < cb->args[0])
851 goto skip;
852
853 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000854 err = vxlan_fdb_info(skb, vxlan, f,
855 NETLINK_CB(cb->skb).portid,
856 cb->nlh->nlmsg_seq,
857 RTM_NEWNEIGH,
858 NLM_F_MULTI, rd);
859 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700860 goto out;
David Stevens66817122013-03-15 04:35:51 +0000861 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700862skip:
863 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000864 }
865 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700866out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000867 return idx;
868}
869
870/* Watch incoming packets to learn mapping between Ethernet address
871 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700872 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000873 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700874static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800875 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000876{
877 struct vxlan_dev *vxlan = netdev_priv(dev);
878 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000879
880 f = vxlan_find_mac(vxlan, src_mac);
881 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700882 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700883
Cong Wange4c7ed42013-08-31 13:44:33 +0800884 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700885 return false;
886
887 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700888 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700889 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000890
891 if (net_ratelimit())
892 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800893 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700894 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000895
Cong Wange4c7ed42013-08-31 13:44:33 +0800896 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000897 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700898 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000899 } else {
900 /* learned new entry */
901 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700902
903 /* close off race between vxlan_flush and incoming packets */
904 if (netif_running(dev))
905 vxlan_fdb_create(vxlan, src_mac, src_ip,
906 NUD_REACHABLE,
907 NLM_F_EXCL|NLM_F_CREATE,
908 vxlan->dst_port,
909 vxlan->default_dst.remote_vni,
910 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000911 spin_unlock(&vxlan->hash_lock);
912 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700913
914 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000915}
916
stephen hemmingerd3428942012-10-01 12:32:35 +0000917/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +0800918static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +0000919{
stephen hemminger553675f2013-05-16 11:35:20 +0000920 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000921
Gao feng95ab0992013-12-10 16:37:33 +0800922 /* The vxlan_sock is only used by dev, leaving group has
923 * no effect on other vxlan devices.
924 */
925 if (atomic_read(&dev->vn_sock->refcnt) == 1)
926 return false;
927
stephen hemminger553675f2013-05-16 11:35:20 +0000928 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +0800929 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +0000930 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000931
Gao feng95ab0992013-12-10 16:37:33 +0800932 if (vxlan->vn_sock != dev->vn_sock)
933 continue;
934
935 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
936 &dev->default_dst.remote_ip))
937 continue;
938
939 if (vxlan->default_dst.remote_ifindex !=
940 dev->default_dst.remote_ifindex)
941 continue;
942
943 return true;
stephen hemminger553675f2013-05-16 11:35:20 +0000944 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000945
946 return false;
947}
948
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700949static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000950{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700951 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000952}
953
Pravin B Shelar012a5722013-08-19 11:23:07 -0700954void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000955{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700956 struct sock *sk = vs->sock->sk;
957 struct net *net = sock_net(sk);
958 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -0700959
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700960 if (!atomic_dec_and_test(&vs->refcnt))
961 return;
962
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700963 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700964 hlist_del_rcu(&vs->hlist);
Pravin B Shelar559835ea2013-09-24 10:25:40 -0700965 rcu_assign_sk_user_data(vs->sock->sk, NULL);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700966 vxlan_notify_del_rx_port(sk);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700967 spin_unlock(&vn->sock_lock);
968
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700969 queue_work(vxlan_wq, &vs->del_work);
970}
Pravin B Shelar012a5722013-08-19 11:23:07 -0700971EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700972
stephen hemminger3fc2de22013-07-18 08:40:15 -0700973/* Callback to update multicast group membership when first VNI on
974 * multicast asddress is brought up
975 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700976 */
stephen hemminger3fc2de22013-07-18 08:40:15 -0700977static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700978{
stephen hemminger3fc2de22013-07-18 08:40:15 -0700979 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700980 struct vxlan_sock *vs = vxlan->vn_sock;
981 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +0800982 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
983 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000984
stephen hemmingerd3428942012-10-01 12:32:35 +0000985 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +0800986 if (ip->sa.sa_family == AF_INET) {
987 struct ip_mreqn mreq = {
988 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
989 .imr_ifindex = ifindex,
990 };
991
992 ip_mc_join_group(sk, &mreq);
993#if IS_ENABLED(CONFIG_IPV6)
994 } else {
995 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
996 &ip->sin6.sin6_addr);
997#endif
998 }
stephen hemminger3fc2de22013-07-18 08:40:15 -0700999 release_sock(sk);
1000
Pravin B Shelar012a5722013-08-19 11:23:07 -07001001 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001002 dev_put(vxlan->dev);
1003}
1004
1005/* Inverse of vxlan_igmp_join when last VNI is brought down */
1006static void vxlan_igmp_leave(struct work_struct *work)
1007{
1008 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001009 struct vxlan_sock *vs = vxlan->vn_sock;
1010 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001011 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1012 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001013
1014 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001015 if (ip->sa.sa_family == AF_INET) {
1016 struct ip_mreqn mreq = {
1017 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1018 .imr_ifindex = ifindex,
1019 };
1020
1021 ip_mc_leave_group(sk, &mreq);
1022#if IS_ENABLED(CONFIG_IPV6)
1023 } else {
1024 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1025 &ip->sin6.sin6_addr);
1026#endif
1027 }
1028
stephen hemmingerd3428942012-10-01 12:32:35 +00001029 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001030
Pravin B Shelar012a5722013-08-19 11:23:07 -07001031 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001032 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001033}
1034
1035/* Callback from net/ipv4/udp.c to receive packets */
1036static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1037{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001038 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001039 struct vxlanhdr *vxh;
stephen hemminger553675f2013-05-16 11:35:20 +00001040 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001041
stephen hemmingerd3428942012-10-01 12:32:35 +00001042 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001043 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001044 goto error;
1045
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001046 /* Return packets with reserved bits set */
1047 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001048 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1049 (vxh->vx_vni & htonl(0xff))) {
1050 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1051 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1052 goto error;
1053 }
1054
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001055 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001056 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001057
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001058 port = inet_sk(sk)->inet_sport;
1059
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001060 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001061 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001062 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001063
1064 vs->rcv(vs, skb, vxh->vx_vni);
1065 return 0;
1066
1067drop:
1068 /* Consume bad packet */
1069 kfree_skb(skb);
1070 return 0;
1071
1072error:
1073 /* Return non vxlan pkt */
1074 return 1;
1075}
1076
1077static void vxlan_rcv(struct vxlan_sock *vs,
1078 struct sk_buff *skb, __be32 vx_vni)
1079{
Cong Wange4c7ed42013-08-31 13:44:33 +08001080 struct iphdr *oip = NULL;
1081 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001082 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001083 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001084 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001085 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001086 int err = 0;
1087 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001088
1089 vni = ntohl(vx_vni) >> 8;
1090 /* Is this VNI defined? */
1091 vxlan = vxlan_vs_find_vni(vs, vni);
1092 if (!vxlan)
1093 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001094
Cong Wange4c7ed42013-08-31 13:44:33 +08001095 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001096 skb_reset_mac_header(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001097 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001098
1099 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001100 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001101 goto drop;
1102
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001103 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001104 if (remote_ip->sa.sa_family == AF_INET) {
1105 oip = ip_hdr(skb);
1106 saddr.sin.sin_addr.s_addr = oip->saddr;
1107 saddr.sa.sa_family = AF_INET;
1108#if IS_ENABLED(CONFIG_IPV6)
1109 } else {
1110 oip6 = ipv6_hdr(skb);
1111 saddr.sin6.sin6_addr = oip6->saddr;
1112 saddr.sa.sa_family = AF_INET6;
1113#endif
1114 }
1115
stephen hemminger26a41ae62013-06-17 12:09:58 -07001116 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001117 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001118 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001119
stephen hemmingerd3428942012-10-01 12:32:35 +00001120 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001121
1122 /* If the NIC driver gave us an encapsulated packet with
1123 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
1124 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
1125 * for us. Otherwise force the upper layers to verify it.
1126 */
1127 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
1128 !(vxlan->dev->features & NETIF_F_RXCSUM))
1129 skb->ip_summed = CHECKSUM_NONE;
1130
1131 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001132
Cong Wange4c7ed42013-08-31 13:44:33 +08001133 if (oip6)
1134 err = IP6_ECN_decapsulate(oip6, skb);
1135 if (oip)
1136 err = IP_ECN_decapsulate(oip, skb);
1137
stephen hemmingerd3428942012-10-01 12:32:35 +00001138 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001139 if (log_ecn_error) {
1140 if (oip6)
1141 net_info_ratelimited("non-ECT from %pI6\n",
1142 &oip6->saddr);
1143 if (oip)
1144 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1145 &oip->saddr, oip->tos);
1146 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001147 if (err > 1) {
1148 ++vxlan->dev->stats.rx_frame_errors;
1149 ++vxlan->dev->stats.rx_errors;
1150 goto drop;
1151 }
1152 }
1153
Pravin B Shelare8171042013-03-25 14:49:46 +00001154 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001155 u64_stats_update_begin(&stats->syncp);
1156 stats->rx_packets++;
1157 stats->rx_bytes += skb->len;
1158 u64_stats_update_end(&stats->syncp);
1159
1160 netif_rx(skb);
1161
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001162 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001163drop:
1164 /* Consume bad packet */
1165 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001166}
1167
David Stevense4f67ad2012-11-20 02:50:14 +00001168static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1169{
1170 struct vxlan_dev *vxlan = netdev_priv(dev);
1171 struct arphdr *parp;
1172 u8 *arpptr, *sha;
1173 __be32 sip, tip;
1174 struct neighbour *n;
1175
1176 if (dev->flags & IFF_NOARP)
1177 goto out;
1178
1179 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1180 dev->stats.tx_dropped++;
1181 goto out;
1182 }
1183 parp = arp_hdr(skb);
1184
1185 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1186 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1187 parp->ar_pro != htons(ETH_P_IP) ||
1188 parp->ar_op != htons(ARPOP_REQUEST) ||
1189 parp->ar_hln != dev->addr_len ||
1190 parp->ar_pln != 4)
1191 goto out;
1192 arpptr = (u8 *)parp + sizeof(struct arphdr);
1193 sha = arpptr;
1194 arpptr += dev->addr_len; /* sha */
1195 memcpy(&sip, arpptr, sizeof(sip));
1196 arpptr += sizeof(sip);
1197 arpptr += dev->addr_len; /* tha */
1198 memcpy(&tip, arpptr, sizeof(tip));
1199
1200 if (ipv4_is_loopback(tip) ||
1201 ipv4_is_multicast(tip))
1202 goto out;
1203
1204 n = neigh_lookup(&arp_tbl, &tip, dev);
1205
1206 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001207 struct vxlan_fdb *f;
1208 struct sk_buff *reply;
1209
1210 if (!(n->nud_state & NUD_CONNECTED)) {
1211 neigh_release(n);
1212 goto out;
1213 }
1214
1215 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001216 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001217 /* bridge-local neighbor */
1218 neigh_release(n);
1219 goto out;
1220 }
1221
1222 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1223 n->ha, sha);
1224
1225 neigh_release(n);
1226
1227 skb_reset_mac_header(reply);
1228 __skb_pull(reply, skb_network_offset(reply));
1229 reply->ip_summed = CHECKSUM_UNNECESSARY;
1230 reply->pkt_type = PACKET_HOST;
1231
1232 if (netif_rx_ni(reply) == NET_RX_DROP)
1233 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001234 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1235 union vxlan_addr ipa = {
1236 .sin.sin_addr.s_addr = tip,
1237 .sa.sa_family = AF_INET,
1238 };
1239
1240 vxlan_ip_miss(dev, &ipa);
1241 }
David Stevense4f67ad2012-11-20 02:50:14 +00001242out:
1243 consume_skb(skb);
1244 return NETDEV_TX_OK;
1245}
1246
Cong Wangf564f452013-08-31 13:44:36 +08001247#if IS_ENABLED(CONFIG_IPV6)
1248static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1249{
1250 struct vxlan_dev *vxlan = netdev_priv(dev);
1251 struct neighbour *n;
1252 union vxlan_addr ipa;
1253 const struct ipv6hdr *iphdr;
1254 const struct in6_addr *saddr, *daddr;
1255 struct nd_msg *msg;
1256 struct inet6_dev *in6_dev = NULL;
1257
1258 in6_dev = __in6_dev_get(dev);
1259 if (!in6_dev)
1260 goto out;
1261
1262 if (!pskb_may_pull(skb, skb->len))
1263 goto out;
1264
1265 iphdr = ipv6_hdr(skb);
1266 saddr = &iphdr->saddr;
1267 daddr = &iphdr->daddr;
1268
1269 if (ipv6_addr_loopback(daddr) ||
1270 ipv6_addr_is_multicast(daddr))
1271 goto out;
1272
1273 msg = (struct nd_msg *)skb_transport_header(skb);
1274 if (msg->icmph.icmp6_code != 0 ||
1275 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1276 goto out;
1277
1278 n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev);
1279
1280 if (n) {
1281 struct vxlan_fdb *f;
1282
1283 if (!(n->nud_state & NUD_CONNECTED)) {
1284 neigh_release(n);
1285 goto out;
1286 }
1287
1288 f = vxlan_find_mac(vxlan, n->ha);
1289 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1290 /* bridge-local neighbor */
1291 neigh_release(n);
1292 goto out;
1293 }
1294
1295 ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target,
1296 !!in6_dev->cnf.forwarding,
1297 true, false, false);
1298 neigh_release(n);
1299 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1300 ipa.sin6.sin6_addr = *daddr;
1301 ipa.sa.sa_family = AF_INET6;
1302 vxlan_ip_miss(dev, &ipa);
1303 }
1304
1305out:
1306 consume_skb(skb);
1307 return NETDEV_TX_OK;
1308}
1309#endif
1310
David Stevense4f67ad2012-11-20 02:50:14 +00001311static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1312{
1313 struct vxlan_dev *vxlan = netdev_priv(dev);
1314 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001315
1316 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1317 return false;
1318
1319 n = NULL;
1320 switch (ntohs(eth_hdr(skb)->h_proto)) {
1321 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001322 {
1323 struct iphdr *pip;
1324
David Stevense4f67ad2012-11-20 02:50:14 +00001325 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1326 return false;
1327 pip = ip_hdr(skb);
1328 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001329 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1330 union vxlan_addr ipa = {
1331 .sin.sin_addr.s_addr = pip->daddr,
1332 .sa.sa_family = AF_INET,
1333 };
1334
1335 vxlan_ip_miss(dev, &ipa);
1336 return false;
1337 }
1338
David Stevense4f67ad2012-11-20 02:50:14 +00001339 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001340 }
1341#if IS_ENABLED(CONFIG_IPV6)
1342 case ETH_P_IPV6:
1343 {
1344 struct ipv6hdr *pip6;
1345
1346 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1347 return false;
1348 pip6 = ipv6_hdr(skb);
1349 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1350 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1351 union vxlan_addr ipa = {
1352 .sin6.sin6_addr = pip6->daddr,
1353 .sa.sa_family = AF_INET6,
1354 };
1355
1356 vxlan_ip_miss(dev, &ipa);
1357 return false;
1358 }
1359
1360 break;
1361 }
1362#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001363 default:
1364 return false;
1365 }
1366
1367 if (n) {
1368 bool diff;
1369
Joe Perches7367d0b2013-09-01 11:51:23 -07001370 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001371 if (diff) {
1372 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1373 dev->addr_len);
1374 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1375 }
1376 neigh_release(n);
1377 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001378 }
1379
David Stevense4f67ad2012-11-20 02:50:14 +00001380 return false;
1381}
1382
stephen hemminger05f47d62012-10-09 20:35:50 +00001383/* Compute source port for outgoing packet
1384 * first choice to use L4 flow hash since it will spread
1385 * better and maybe available from hardware
1386 * secondary choice is to use jhash on the Ethernet header
1387 */
Pravin B Shelar49560532013-08-19 11:23:17 -07001388__be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001389{
Pravin B Shelar49560532013-08-19 11:23:17 -07001390 unsigned int range = (port_max - port_min) + 1;
stephen hemminger05f47d62012-10-09 20:35:50 +00001391 u32 hash;
1392
Tom Herbert3958afa1b2013-12-15 22:12:06 -08001393 hash = skb_get_hash(skb);
stephen hemminger05f47d62012-10-09 20:35:50 +00001394 if (!hash)
1395 hash = jhash(skb->data, 2 * ETH_ALEN,
1396 (__force u32) skb->protocol);
1397
Pravin B Shelar49560532013-08-19 11:23:17 -07001398 return htons((((u64) hash * range) >> 32) + port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001399}
Pravin B Shelar49560532013-08-19 11:23:17 -07001400EXPORT_SYMBOL_GPL(vxlan_src_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001401
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001402static int handle_offloads(struct sk_buff *skb)
1403{
1404 if (skb_is_gso(skb)) {
1405 int err = skb_unclone(skb, GFP_ATOMIC);
1406 if (unlikely(err))
1407 return err;
1408
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001409 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001410 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1411 skb->ip_summed = CHECKSUM_NONE;
1412
1413 return 0;
1414}
1415
Cong Wange4c7ed42013-08-31 13:44:33 +08001416#if IS_ENABLED(CONFIG_IPV6)
Nicolas Dichtel11796182013-09-02 15:34:55 +02001417static int vxlan6_xmit_skb(struct vxlan_sock *vs,
Cong Wange4c7ed42013-08-31 13:44:33 +08001418 struct dst_entry *dst, struct sk_buff *skb,
1419 struct net_device *dev, struct in6_addr *saddr,
1420 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1421 __be16 src_port, __be16 dst_port, __be32 vni)
1422{
1423 struct ipv6hdr *ip6h;
1424 struct vxlanhdr *vxh;
1425 struct udphdr *uh;
1426 int min_headroom;
1427 int err;
1428
1429 if (!skb->encapsulation) {
1430 skb_reset_inner_headers(skb);
1431 skb->encapsulation = 1;
1432 }
1433
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001434 skb_scrub_packet(skb, false);
1435
Cong Wange4c7ed42013-08-31 13:44:33 +08001436 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1437 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1438 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1439
1440 /* Need space for new headers (invalidates iph ptr) */
1441 err = skb_cow_head(skb, min_headroom);
1442 if (unlikely(err))
1443 return err;
1444
1445 if (vlan_tx_tag_present(skb)) {
1446 if (WARN_ON(!__vlan_put_tag(skb,
1447 skb->vlan_proto,
1448 vlan_tx_tag_get(skb))))
1449 return -ENOMEM;
1450
1451 skb->vlan_tci = 0;
1452 }
1453
1454 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1455 vxh->vx_flags = htonl(VXLAN_FLAGS);
1456 vxh->vx_vni = vni;
1457
1458 __skb_push(skb, sizeof(*uh));
1459 skb_reset_transport_header(skb);
1460 uh = udp_hdr(skb);
1461
1462 uh->dest = dst_port;
1463 uh->source = src_port;
1464
1465 uh->len = htons(skb->len);
1466 uh->check = 0;
1467
1468 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1469 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1470 IPSKB_REROUTED);
Cong Wange4c7ed42013-08-31 13:44:33 +08001471 skb_dst_set(skb, dst);
1472
1473 if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
1474 __wsum csum = skb_checksum(skb, 0, skb->len, 0);
1475 skb->ip_summed = CHECKSUM_UNNECESSARY;
1476 uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
1477 IPPROTO_UDP, csum);
1478 if (uh->check == 0)
1479 uh->check = CSUM_MANGLED_0;
1480 } else {
1481 skb->ip_summed = CHECKSUM_PARTIAL;
1482 skb->csum_start = skb_transport_header(skb) - skb->head;
1483 skb->csum_offset = offsetof(struct udphdr, check);
1484 uh->check = ~csum_ipv6_magic(saddr, daddr,
1485 skb->len, IPPROTO_UDP, 0);
1486 }
1487
1488 __skb_push(skb, sizeof(*ip6h));
1489 skb_reset_network_header(skb);
1490 ip6h = ipv6_hdr(skb);
1491 ip6h->version = 6;
1492 ip6h->priority = prio;
1493 ip6h->flow_lbl[0] = 0;
1494 ip6h->flow_lbl[1] = 0;
1495 ip6h->flow_lbl[2] = 0;
1496 ip6h->payload_len = htons(skb->len);
1497 ip6h->nexthdr = IPPROTO_UDP;
1498 ip6h->hop_limit = ttl;
1499 ip6h->daddr = *daddr;
1500 ip6h->saddr = *saddr;
1501
Cong Wange4c7ed42013-08-31 13:44:33 +08001502 err = handle_offloads(skb);
1503 if (err)
1504 return err;
1505
1506 ip6tunnel_xmit(skb, dev);
1507 return 0;
1508}
1509#endif
1510
Nicolas Dichtel11796182013-09-02 15:34:55 +02001511int vxlan_xmit_skb(struct vxlan_sock *vs,
Pravin B Shelar49560532013-08-19 11:23:17 -07001512 struct rtable *rt, struct sk_buff *skb,
1513 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1514 __be16 src_port, __be16 dst_port, __be32 vni)
1515{
1516 struct vxlanhdr *vxh;
1517 struct udphdr *uh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001518 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001519 int err;
1520
1521 if (!skb->encapsulation) {
1522 skb_reset_inner_headers(skb);
1523 skb->encapsulation = 1;
1524 }
1525
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001526 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001527 + VXLAN_HLEN + sizeof(struct iphdr)
1528 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001529
1530 /* Need space for new headers (invalidates iph ptr) */
1531 err = skb_cow_head(skb, min_headroom);
1532 if (unlikely(err))
1533 return err;
1534
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001535 if (vlan_tx_tag_present(skb)) {
1536 if (WARN_ON(!__vlan_put_tag(skb,
1537 skb->vlan_proto,
1538 vlan_tx_tag_get(skb))))
1539 return -ENOMEM;
1540
1541 skb->vlan_tci = 0;
1542 }
1543
Pravin B Shelar49560532013-08-19 11:23:17 -07001544 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1545 vxh->vx_flags = htonl(VXLAN_FLAGS);
1546 vxh->vx_vni = vni;
1547
1548 __skb_push(skb, sizeof(*uh));
1549 skb_reset_transport_header(skb);
1550 uh = udp_hdr(skb);
1551
1552 uh->dest = dst_port;
1553 uh->source = src_port;
1554
1555 uh->len = htons(skb->len);
1556 uh->check = 0;
1557
Pravin B Shelar49560532013-08-19 11:23:17 -07001558 err = handle_offloads(skb);
1559 if (err)
1560 return err;
1561
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001562 return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df,
1563 false);
Pravin B Shelar49560532013-08-19 11:23:17 -07001564}
1565EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1566
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001567/* Bypass encapsulation if the destination is local */
1568static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1569 struct vxlan_dev *dst_vxlan)
1570{
Li RongQing8f849852014-01-04 13:57:59 +08001571 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001572 union vxlan_addr loopback;
1573 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001574
Li RongQing8f849852014-01-04 13:57:59 +08001575 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1576 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001577 skb->pkt_type = PACKET_HOST;
1578 skb->encapsulation = 0;
1579 skb->dev = dst_vxlan->dev;
1580 __skb_pull(skb, skb_network_offset(skb));
1581
Cong Wange4c7ed42013-08-31 13:44:33 +08001582 if (remote_ip->sa.sa_family == AF_INET) {
1583 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1584 loopback.sa.sa_family = AF_INET;
1585#if IS_ENABLED(CONFIG_IPV6)
1586 } else {
1587 loopback.sin6.sin6_addr = in6addr_loopback;
1588 loopback.sa.sa_family = AF_INET6;
1589#endif
1590 }
1591
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001592 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001593 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001594
1595 u64_stats_update_begin(&tx_stats->syncp);
1596 tx_stats->tx_packets++;
1597 tx_stats->tx_bytes += skb->len;
1598 u64_stats_update_end(&tx_stats->syncp);
1599
1600 if (netif_rx(skb) == NET_RX_SUCCESS) {
1601 u64_stats_update_begin(&rx_stats->syncp);
1602 rx_stats->rx_packets++;
1603 rx_stats->rx_bytes += skb->len;
1604 u64_stats_update_end(&rx_stats->syncp);
1605 } else {
1606 skb->dev->stats.rx_dropped++;
1607 }
1608}
1609
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001610static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1611 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001612{
1613 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001614 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001615 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001616 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001617 union vxlan_addr *dst;
1618 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001619 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001620 __be16 df = 0;
1621 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001622 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001623
stephen hemminger823aa872013-04-27 11:31:57 +00001624 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001625 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001626 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001627
Cong Wange4c7ed42013-08-31 13:44:33 +08001628 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001629 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001630 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001631 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001632 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001633 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001634 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001635 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001636
stephen hemmingerd3428942012-10-01 12:32:35 +00001637 old_iph = ip_hdr(skb);
1638
stephen hemmingerd3428942012-10-01 12:32:35 +00001639 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001640 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001641 ttl = 1;
1642
1643 tos = vxlan->tos;
1644 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001645 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001646
Pravin B Shelar49560532013-08-19 11:23:17 -07001647 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001648
Cong Wange4c7ed42013-08-31 13:44:33 +08001649 if (dst->sa.sa_family == AF_INET) {
1650 memset(&fl4, 0, sizeof(fl4));
1651 fl4.flowi4_oif = rdst->remote_ifindex;
1652 fl4.flowi4_tos = RT_TOS(tos);
1653 fl4.daddr = dst->sin.sin_addr.s_addr;
1654 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001655
Cong Wange4c7ed42013-08-31 13:44:33 +08001656 rt = ip_route_output_key(dev_net(dev), &fl4);
1657 if (IS_ERR(rt)) {
1658 netdev_dbg(dev, "no route to %pI4\n",
1659 &dst->sin.sin_addr.s_addr);
1660 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001661 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001662 }
1663
1664 if (rt->dst.dev == dev) {
1665 netdev_dbg(dev, "circular route to %pI4\n",
1666 &dst->sin.sin_addr.s_addr);
1667 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001668 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001669 }
1670
1671 /* Bypass encapsulation if the destination is local */
1672 if (rt->rt_flags & RTCF_LOCAL &&
1673 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1674 struct vxlan_dev *dst_vxlan;
1675
1676 ip_rt_put(rt);
1677 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1678 if (!dst_vxlan)
1679 goto tx_error;
1680 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1681 return;
1682 }
1683
1684 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1685 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1686
Nicolas Dichtel11796182013-09-02 15:34:55 +02001687 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001688 fl4.saddr, dst->sin.sin_addr.s_addr,
1689 tos, ttl, df, src_port, dst_port,
1690 htonl(vni << 8));
1691
1692 if (err < 0)
1693 goto rt_tx_error;
1694 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1695#if IS_ENABLED(CONFIG_IPV6)
1696 } else {
1697 struct sock *sk = vxlan->vn_sock->sock->sk;
1698 struct dst_entry *ndst;
1699 struct flowi6 fl6;
1700 u32 flags;
1701
1702 memset(&fl6, 0, sizeof(fl6));
1703 fl6.flowi6_oif = rdst->remote_ifindex;
1704 fl6.daddr = dst->sin6.sin6_addr;
1705 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08001706 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001707
1708 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1709 netdev_dbg(dev, "no route to %pI6\n",
1710 &dst->sin6.sin6_addr);
1711 dev->stats.tx_carrier_errors++;
1712 goto tx_error;
1713 }
1714
1715 if (ndst->dev == dev) {
1716 netdev_dbg(dev, "circular route to %pI6\n",
1717 &dst->sin6.sin6_addr);
1718 dst_release(ndst);
1719 dev->stats.collisions++;
1720 goto tx_error;
1721 }
1722
1723 /* Bypass encapsulation if the destination is local */
1724 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1725 if (flags & RTF_LOCAL &&
1726 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1727 struct vxlan_dev *dst_vxlan;
1728
1729 dst_release(ndst);
1730 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1731 if (!dst_vxlan)
1732 goto tx_error;
1733 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1734 return;
1735 }
1736
1737 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1738
Nicolas Dichtel11796182013-09-02 15:34:55 +02001739 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001740 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1741 src_port, dst_port, htonl(vni << 8));
1742#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001743 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001744
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001745 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001746
1747drop:
1748 dev->stats.tx_dropped++;
1749 goto tx_free;
1750
Pravin B Shelar49560532013-08-19 11:23:17 -07001751rt_tx_error:
1752 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001753tx_error:
1754 dev->stats.tx_errors++;
1755tx_free:
1756 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001757}
1758
David Stevens66817122013-03-15 04:35:51 +00001759/* Transmit local packets over Vxlan
1760 *
1761 * Outer IP header inherits ECN and DF from inner header.
1762 * Outer UDP destination is the VXLAN assigned port.
1763 * source port is based on hash of flow
1764 */
1765static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1766{
1767 struct vxlan_dev *vxlan = netdev_priv(dev);
1768 struct ethhdr *eth;
1769 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08001770 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00001771 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001772
1773 skb_reset_mac_header(skb);
1774 eth = eth_hdr(skb);
1775
Cong Wangf564f452013-08-31 13:44:36 +08001776 if ((vxlan->flags & VXLAN_F_PROXY)) {
1777 if (ntohs(eth->h_proto) == ETH_P_ARP)
1778 return arp_reduce(dev, skb);
1779#if IS_ENABLED(CONFIG_IPV6)
1780 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
1781 skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
1782 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1783 struct nd_msg *msg;
1784
1785 msg = (struct nd_msg *)skb_transport_header(skb);
1786 if (msg->icmph.icmp6_code == 0 &&
1787 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1788 return neigh_reduce(dev, skb);
1789 }
1790#endif
1791 }
David Stevens66817122013-03-15 04:35:51 +00001792
1793 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001794 did_rsc = false;
1795
1796 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08001797 (ntohs(eth->h_proto) == ETH_P_IP ||
1798 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00001799 did_rsc = route_shortcircuit(dev, skb);
1800 if (did_rsc)
1801 f = vxlan_find_mac(vxlan, eth->h_dest);
1802 }
1803
David Stevens66817122013-03-15 04:35:51 +00001804 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001805 f = vxlan_find_mac(vxlan, all_zeros_mac);
1806 if (f == NULL) {
1807 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1808 !is_multicast_ether_addr(eth->h_dest))
1809 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001810
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001811 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08001812 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001813 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001814 }
David Stevens66817122013-03-15 04:35:51 +00001815 }
1816
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001817 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1818 struct sk_buff *skb1;
1819
Eric Dumazet8f646c92014-01-06 09:54:31 -08001820 if (!fdst) {
1821 fdst = rdst;
1822 continue;
1823 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001824 skb1 = skb_clone(skb, GFP_ATOMIC);
1825 if (skb1)
1826 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1827 }
1828
Eric Dumazet8f646c92014-01-06 09:54:31 -08001829 if (fdst)
1830 vxlan_xmit_one(skb, dev, fdst, did_rsc);
1831 else
1832 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001833 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001834}
1835
stephen hemmingerd3428942012-10-01 12:32:35 +00001836/* Walk the forwarding table and purge stale entries */
1837static void vxlan_cleanup(unsigned long arg)
1838{
1839 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1840 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1841 unsigned int h;
1842
1843 if (!netif_running(vxlan->dev))
1844 return;
1845
1846 spin_lock_bh(&vxlan->hash_lock);
1847 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1848 struct hlist_node *p, *n;
1849 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1850 struct vxlan_fdb *f
1851 = container_of(p, struct vxlan_fdb, hlist);
1852 unsigned long timeout;
1853
stephen hemminger3c172862012-10-26 06:24:34 +00001854 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001855 continue;
1856
1857 timeout = f->used + vxlan->age_interval * HZ;
1858 if (time_before_eq(timeout, jiffies)) {
1859 netdev_dbg(vxlan->dev,
1860 "garbage collect %pM\n",
1861 f->eth_addr);
1862 f->state = NUD_STALE;
1863 vxlan_fdb_destroy(vxlan, f);
1864 } else if (time_before(timeout, next_timer))
1865 next_timer = timeout;
1866 }
1867 }
1868 spin_unlock_bh(&vxlan->hash_lock);
1869
1870 mod_timer(&vxlan->age_timer, next_timer);
1871}
1872
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001873static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1874{
1875 __u32 vni = vxlan->default_dst.remote_vni;
1876
1877 vxlan->vn_sock = vs;
1878 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1879}
1880
stephen hemmingerd3428942012-10-01 12:32:35 +00001881/* Setup stats when device is created */
1882static int vxlan_init(struct net_device *dev)
1883{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001884 struct vxlan_dev *vxlan = netdev_priv(dev);
1885 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1886 struct vxlan_sock *vs;
John Stultz827da442013-10-07 15:51:58 -07001887 int i;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001888
Li RongQing8f849852014-01-04 13:57:59 +08001889 dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00001890 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001891 return -ENOMEM;
1892
John Stultz827da442013-10-07 15:51:58 -07001893 for_each_possible_cpu(i) {
Li RongQing8f849852014-01-04 13:57:59 +08001894 struct pcpu_sw_netstats *vxlan_stats;
John Stultz827da442013-10-07 15:51:58 -07001895 vxlan_stats = per_cpu_ptr(dev->tstats, i);
1896 u64_stats_init(&vxlan_stats->syncp);
1897 }
1898
1899
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001900 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001901 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001902 if (vs) {
1903 /* If we have a socket with same port already, reuse it */
1904 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001905 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001906 } else {
1907 /* otherwise make new socket outside of RTNL */
1908 dev_hold(dev);
1909 queue_work(vxlan_wq, &vxlan->sock_work);
1910 }
1911 spin_unlock(&vn->sock_lock);
1912
stephen hemmingerd3428942012-10-01 12:32:35 +00001913 return 0;
1914}
1915
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001916static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001917{
1918 struct vxlan_fdb *f;
1919
1920 spin_lock_bh(&vxlan->hash_lock);
1921 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1922 if (f)
1923 vxlan_fdb_destroy(vxlan, f);
1924 spin_unlock_bh(&vxlan->hash_lock);
1925}
1926
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001927static void vxlan_uninit(struct net_device *dev)
1928{
1929 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001930 struct vxlan_sock *vs = vxlan->vn_sock;
1931
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001932 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001933
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001934 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07001935 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001936 free_percpu(dev->tstats);
1937}
1938
stephen hemmingerd3428942012-10-01 12:32:35 +00001939/* Start ageing timer and join group when device is brought up */
1940static int vxlan_open(struct net_device *dev)
1941{
1942 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001943 struct vxlan_sock *vs = vxlan->vn_sock;
1944
1945 /* socket hasn't been created */
1946 if (!vs)
1947 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001948
Gao feng79d4a942013-12-10 16:37:32 +08001949 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001950 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001951 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001952 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00001953 }
1954
1955 if (vxlan->age_interval)
1956 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1957
1958 return 0;
1959}
1960
1961/* Purge the forwarding table */
1962static void vxlan_flush(struct vxlan_dev *vxlan)
1963{
Cong Wang31fec5a2013-05-27 22:35:52 +00001964 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001965
1966 spin_lock_bh(&vxlan->hash_lock);
1967 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1968 struct hlist_node *p, *n;
1969 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1970 struct vxlan_fdb *f
1971 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001972 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1973 if (!is_zero_ether_addr(f->eth_addr))
1974 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001975 }
1976 }
1977 spin_unlock_bh(&vxlan->hash_lock);
1978}
1979
1980/* Cleanup timer and forwarding table on shutdown */
1981static int vxlan_stop(struct net_device *dev)
1982{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001983 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001984 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001985 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001986
Cong Wange4c7ed42013-08-31 13:44:33 +08001987 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08001988 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001989 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001990 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001991 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001992 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001993
1994 del_timer_sync(&vxlan->age_timer);
1995
1996 vxlan_flush(vxlan);
1997
1998 return 0;
1999}
2000
stephen hemmingerd3428942012-10-01 12:32:35 +00002001/* Stub, nothing needs to be done. */
2002static void vxlan_set_multicast_list(struct net_device *dev)
2003{
2004}
2005
Daniel Borkmann345010b2013-12-18 00:21:08 +01002006static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2007{
2008 struct vxlan_dev *vxlan = netdev_priv(dev);
2009 struct vxlan_rdst *dst = &vxlan->default_dst;
2010 struct net_device *lowerdev;
2011 int max_mtu;
2012
2013 lowerdev = __dev_get_by_index(dev_net(dev), dst->remote_ifindex);
2014 if (lowerdev == NULL)
2015 return eth_change_mtu(dev, new_mtu);
2016
2017 if (dst->remote_ip.sa.sa_family == AF_INET6)
2018 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2019 else
2020 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2021
2022 if (new_mtu < 68 || new_mtu > max_mtu)
2023 return -EINVAL;
2024
2025 dev->mtu = new_mtu;
2026 return 0;
2027}
2028
stephen hemmingerd3428942012-10-01 12:32:35 +00002029static const struct net_device_ops vxlan_netdev_ops = {
2030 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002031 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002032 .ndo_open = vxlan_open,
2033 .ndo_stop = vxlan_stop,
2034 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002035 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002036 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002037 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002038 .ndo_validate_addr = eth_validate_addr,
2039 .ndo_set_mac_address = eth_mac_addr,
2040 .ndo_fdb_add = vxlan_fdb_add,
2041 .ndo_fdb_del = vxlan_fdb_delete,
2042 .ndo_fdb_dump = vxlan_fdb_dump,
2043};
2044
2045/* Info for udev, that this is a virtual tunnel endpoint */
2046static struct device_type vxlan_type = {
2047 .name = "vxlan",
2048};
2049
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002050/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002051 * supply the listening VXLAN udp ports. Callers are expected
2052 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002053 */
2054void vxlan_get_rx_port(struct net_device *dev)
2055{
2056 struct vxlan_sock *vs;
2057 struct net *net = dev_net(dev);
2058 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2059 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002060 __be16 port;
2061 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002062
2063 spin_lock(&vn->sock_lock);
2064 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002065 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2066 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002067 sa_family = vs->sock->sk->sk_family;
2068 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2069 port);
2070 }
2071 }
2072 spin_unlock(&vn->sock_lock);
2073}
2074EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2075
stephen hemmingerd3428942012-10-01 12:32:35 +00002076/* Initialize the device structure. */
2077static void vxlan_setup(struct net_device *dev)
2078{
2079 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002080 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00002081 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00002082
2083 eth_hw_addr_random(dev);
2084 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002085 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2086 dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM;
2087 else
2088 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002089
2090 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002091 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002092 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2093
2094 dev->tx_queue_len = 0;
2095 dev->features |= NETIF_F_LLTX;
2096 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002097 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002098 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002099 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002100
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002101 dev->vlan_features = dev->features;
2102 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002103 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002104 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002105 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
stephen hemmingerd3428942012-10-01 12:32:35 +00002106 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00002107 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002108
stephen hemminger553675f2013-05-16 11:35:20 +00002109 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002110 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002111 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2112 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002113 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002114
2115 init_timer_deferrable(&vxlan->age_timer);
2116 vxlan->age_timer.function = vxlan_cleanup;
2117 vxlan->age_timer.data = (unsigned long) vxlan;
2118
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07002119 inet_get_local_port_range(dev_net(dev), &low, &high);
stephen hemminger05f47d62012-10-09 20:35:50 +00002120 vxlan->port_min = low;
2121 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00002122 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002123
stephen hemmingerd3428942012-10-01 12:32:35 +00002124 vxlan->dev = dev;
2125
2126 for (h = 0; h < FDB_HASH_SIZE; ++h)
2127 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2128}
2129
2130static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2131 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002132 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002133 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002134 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2135 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002136 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002137 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2138 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2139 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2140 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2141 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002142 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002143 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2144 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2145 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2146 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002147 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002148};
2149
2150static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2151{
2152 if (tb[IFLA_ADDRESS]) {
2153 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2154 pr_debug("invalid link address (not ethernet)\n");
2155 return -EINVAL;
2156 }
2157
2158 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2159 pr_debug("invalid all zero ethernet address\n");
2160 return -EADDRNOTAVAIL;
2161 }
2162 }
2163
2164 if (!data)
2165 return -EINVAL;
2166
2167 if (data[IFLA_VXLAN_ID]) {
2168 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2169 if (id >= VXLAN_VID_MASK)
2170 return -ERANGE;
2171 }
2172
stephen hemminger05f47d62012-10-09 20:35:50 +00002173 if (data[IFLA_VXLAN_PORT_RANGE]) {
2174 const struct ifla_vxlan_port_range *p
2175 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2176
2177 if (ntohs(p->high) < ntohs(p->low)) {
2178 pr_debug("port range %u .. %u not valid\n",
2179 ntohs(p->low), ntohs(p->high));
2180 return -EINVAL;
2181 }
2182 }
2183
stephen hemmingerd3428942012-10-01 12:32:35 +00002184 return 0;
2185}
2186
Yan Burman1b13c972013-01-29 23:43:07 +00002187static void vxlan_get_drvinfo(struct net_device *netdev,
2188 struct ethtool_drvinfo *drvinfo)
2189{
2190 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2191 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2192}
2193
2194static const struct ethtool_ops vxlan_ethtool_ops = {
2195 .get_drvinfo = vxlan_get_drvinfo,
2196 .get_link = ethtool_op_get_link,
2197};
2198
stephen hemminger553675f2013-05-16 11:35:20 +00002199static void vxlan_del_work(struct work_struct *work)
2200{
2201 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2202
2203 sk_release_kernel(vs->sock->sk);
2204 kfree_rcu(vs, rcu);
2205}
2206
Cong Wange4c7ed42013-08-31 13:44:33 +08002207#if IS_ENABLED(CONFIG_IPV6)
2208/* Create UDP socket for encapsulation receive. AF_INET6 socket
2209 * could be used for both IPv4 and IPv6 communications, but
2210 * users may set bindv6only=1.
2211 */
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002212static struct socket *create_v6_sock(struct net *net, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +00002213{
stephen hemminger553675f2013-05-16 11:35:20 +00002214 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08002215 struct socket *sock;
2216 struct sockaddr_in6 vxlan_addr = {
2217 .sin6_family = AF_INET6,
2218 .sin6_port = port,
2219 };
2220 int rc, val = 1;
2221
2222 rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
2223 if (rc < 0) {
2224 pr_debug("UDPv6 socket create failed\n");
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002225 return ERR_PTR(rc);
Cong Wange4c7ed42013-08-31 13:44:33 +08002226 }
2227
2228 /* Put in proper namespace */
2229 sk = sock->sk;
2230 sk_change_net(sk, net);
2231
2232 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
2233 (char *)&val, sizeof(val));
2234 rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
2235 sizeof(struct sockaddr_in6));
2236 if (rc < 0) {
2237 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2238 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
2239 sk_release_kernel(sk);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002240 return ERR_PTR(rc);
Cong Wange4c7ed42013-08-31 13:44:33 +08002241 }
2242 /* At this point, IPv6 module should have been loaded in
2243 * sock_create_kern().
2244 */
2245 BUG_ON(!ipv6_stub);
2246
Cong Wange4c7ed42013-08-31 13:44:33 +08002247 /* Disable multicast loopback */
2248 inet_sk(sk)->mc_loop = 0;
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002249 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002250}
2251
2252#else
2253
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002254static struct socket *create_v6_sock(struct net *net, __be16 port)
Cong Wange4c7ed42013-08-31 13:44:33 +08002255{
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002256 return ERR_PTR(-EPFNOSUPPORT);
Cong Wange4c7ed42013-08-31 13:44:33 +08002257}
2258#endif
2259
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002260static struct socket *create_v4_sock(struct net *net, __be16 port)
Cong Wange4c7ed42013-08-31 13:44:33 +08002261{
2262 struct sock *sk;
2263 struct socket *sock;
stephen hemminger553675f2013-05-16 11:35:20 +00002264 struct sockaddr_in vxlan_addr = {
2265 .sin_family = AF_INET,
2266 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07002267 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00002268 };
2269 int rc;
Cong Wange4c7ed42013-08-31 13:44:33 +08002270
2271 /* Create UDP socket for encapsulation receive. */
2272 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
2273 if (rc < 0) {
2274 pr_debug("UDP socket create failed\n");
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002275 return ERR_PTR(rc);
Cong Wange4c7ed42013-08-31 13:44:33 +08002276 }
2277
2278 /* Put in proper namespace */
2279 sk = sock->sk;
2280 sk_change_net(sk, net);
2281
2282 rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
2283 sizeof(vxlan_addr));
2284 if (rc < 0) {
2285 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2286 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
2287 sk_release_kernel(sk);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002288 return ERR_PTR(rc);
Cong Wange4c7ed42013-08-31 13:44:33 +08002289 }
2290
Cong Wange4c7ed42013-08-31 13:44:33 +08002291 /* Disable multicast loopback */
2292 inet_sk(sk)->mc_loop = 0;
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002293 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002294}
2295
2296/* Create new listen socket if needed */
2297static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2298 vxlan_rcv_t *rcv, void *data, bool ipv6)
2299{
2300 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2301 struct vxlan_sock *vs;
2302 struct socket *sock;
2303 struct sock *sk;
Cong Wang31fec5a2013-05-27 22:35:52 +00002304 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00002305
2306 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002307 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002308 return ERR_PTR(-ENOMEM);
2309
2310 for (h = 0; h < VNI_HASH_SIZE; ++h)
2311 INIT_HLIST_HEAD(&vs->vni_list[h]);
2312
2313 INIT_WORK(&vs->del_work, vxlan_del_work);
2314
Cong Wange4c7ed42013-08-31 13:44:33 +08002315 if (ipv6)
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002316 sock = create_v6_sock(net, port);
Cong Wange4c7ed42013-08-31 13:44:33 +08002317 else
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002318 sock = create_v4_sock(net, port);
2319 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002320 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002321 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002322 }
2323
Cong Wange4c7ed42013-08-31 13:44:33 +08002324 vs->sock = sock;
2325 sk = sock->sk;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002326 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002327 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002328 vs->data = data;
Pravin B Shelar559835ea2013-09-24 10:25:40 -07002329 rcu_assign_sk_user_data(vs->sock->sk, vs);
stephen hemminger553675f2013-05-16 11:35:20 +00002330
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002331 spin_lock(&vn->sock_lock);
2332 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002333 vxlan_notify_add_rx_port(sk);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002334 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002335
2336 /* Mark socket as an encapsulation socket. */
2337 udp_sk(sk)->encap_type = 1;
2338 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
Cong Wange4c7ed42013-08-31 13:44:33 +08002339#if IS_ENABLED(CONFIG_IPV6)
2340 if (ipv6)
2341 ipv6_stub->udpv6_encap_enable();
2342 else
2343#endif
2344 udp_encap_enable();
2345
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002346 return vs;
2347}
stephen hemminger553675f2013-05-16 11:35:20 +00002348
Pravin B Shelar012a5722013-08-19 11:23:07 -07002349struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2350 vxlan_rcv_t *rcv, void *data,
Cong Wange4c7ed42013-08-31 13:44:33 +08002351 bool no_share, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002352{
2353 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2354 struct vxlan_sock *vs;
2355
Cong Wange4c7ed42013-08-31 13:44:33 +08002356 vs = vxlan_socket_create(net, port, rcv, data, ipv6);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002357 if (!IS_ERR(vs))
2358 return vs;
2359
Pravin B Shelar012a5722013-08-19 11:23:07 -07002360 if (no_share) /* Return error if sharing is not allowed. */
2361 return vs;
2362
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002363 spin_lock(&vn->sock_lock);
2364 vs = vxlan_find_sock(net, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002365 if (vs) {
2366 if (vs->rcv == rcv)
2367 atomic_inc(&vs->refcnt);
2368 else
2369 vs = ERR_PTR(-EBUSY);
2370 }
2371 spin_unlock(&vn->sock_lock);
2372
2373 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002374 vs = ERR_PTR(-EINVAL);
2375
stephen hemminger553675f2013-05-16 11:35:20 +00002376 return vs;
2377}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002378EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002379
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002380/* Scheduled at device creation to bind to a socket */
2381static void vxlan_sock_work(struct work_struct *work)
2382{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002383 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2384 struct net *net = dev_net(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002385 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002386 __be16 port = vxlan->dst_port;
2387 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002388
Cong Wange4c7ed42013-08-31 13:44:33 +08002389 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002390 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002391 if (!IS_ERR(nvs))
2392 vxlan_vs_add_dev(nvs, vxlan);
2393 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002394
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002395 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002396}
2397
stephen hemmingerd3428942012-10-01 12:32:35 +00002398static int vxlan_newlink(struct net *net, struct net_device *dev,
2399 struct nlattr *tb[], struct nlattr *data[])
2400{
stephen hemminger553675f2013-05-16 11:35:20 +00002401 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002402 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002403 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002404 __u32 vni;
2405 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002406 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002407
2408 if (!data[IFLA_VXLAN_ID])
2409 return -EINVAL;
2410
2411 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002412 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002413
Cong Wange4c7ed42013-08-31 13:44:33 +08002414 if (data[IFLA_VXLAN_GROUP]) {
2415 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2416 dst->remote_ip.sa.sa_family = AF_INET;
2417 } else if (data[IFLA_VXLAN_GROUP6]) {
2418 if (!IS_ENABLED(CONFIG_IPV6))
2419 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002420
Cong Wange4c7ed42013-08-31 13:44:33 +08002421 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2422 sizeof(struct in6_addr));
2423 dst->remote_ip.sa.sa_family = AF_INET6;
2424 use_ipv6 = true;
2425 }
2426
2427 if (data[IFLA_VXLAN_LOCAL]) {
2428 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2429 vxlan->saddr.sa.sa_family = AF_INET;
2430 } else if (data[IFLA_VXLAN_LOCAL6]) {
2431 if (!IS_ENABLED(CONFIG_IPV6))
2432 return -EPFNOSUPPORT;
2433
2434 /* TODO: respect scope id */
2435 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2436 sizeof(struct in6_addr));
2437 vxlan->saddr.sa.sa_family = AF_INET6;
2438 use_ipv6 = true;
2439 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002440
stephen hemminger34e02aa2012-10-09 20:35:53 +00002441 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002442 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002443 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002444 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002445
stephen hemminger34e02aa2012-10-09 20:35:53 +00002446 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002447 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002448 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002449 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002450
Cong Wange4c7ed42013-08-31 13:44:33 +08002451#if IS_ENABLED(CONFIG_IPV6)
2452 if (use_ipv6) {
2453 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2454 if (idev && idev->cnf.disable_ipv6) {
2455 pr_info("IPv6 is disabled via sysctl\n");
2456 return -EPERM;
2457 }
2458 vxlan->flags |= VXLAN_F_IPV6;
2459 }
2460#endif
2461
stephen hemminger34e02aa2012-10-09 20:35:53 +00002462 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002463 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002464
2465 /* update header length based on lower device */
2466 dev->hard_header_len = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002467 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002468 } else if (use_ipv6)
2469 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002470
2471 if (data[IFLA_VXLAN_TOS])
2472 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2473
Vincent Bernatafb97182012-10-30 10:27:16 +00002474 if (data[IFLA_VXLAN_TTL])
2475 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2476
stephen hemmingerd3428942012-10-01 12:32:35 +00002477 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002478 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002479
2480 if (data[IFLA_VXLAN_AGEING])
2481 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2482 else
2483 vxlan->age_interval = FDB_AGE_DEFAULT;
2484
David Stevense4f67ad2012-11-20 02:50:14 +00002485 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2486 vxlan->flags |= VXLAN_F_PROXY;
2487
2488 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2489 vxlan->flags |= VXLAN_F_RSC;
2490
2491 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2492 vxlan->flags |= VXLAN_F_L2MISS;
2493
2494 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2495 vxlan->flags |= VXLAN_F_L3MISS;
2496
stephen hemmingerd3428942012-10-01 12:32:35 +00002497 if (data[IFLA_VXLAN_LIMIT])
2498 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2499
stephen hemminger05f47d62012-10-09 20:35:50 +00002500 if (data[IFLA_VXLAN_PORT_RANGE]) {
2501 const struct ifla_vxlan_port_range *p
2502 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2503 vxlan->port_min = ntohs(p->low);
2504 vxlan->port_max = ntohs(p->high);
2505 }
2506
stephen hemminger823aa872013-04-27 11:31:57 +00002507 if (data[IFLA_VXLAN_PORT])
2508 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2509
stephen hemminger553675f2013-05-16 11:35:20 +00002510 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
2511 pr_info("duplicate VNI %u\n", vni);
2512 return -EEXIST;
2513 }
2514
Yan Burman1b13c972013-01-29 23:43:07 +00002515 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
2516
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002517 /* create an fdb entry for a valid default destination */
2518 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2519 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2520 &vxlan->default_dst.remote_ip,
2521 NUD_REACHABLE|NUD_PERMANENT,
2522 NLM_F_EXCL|NLM_F_CREATE,
2523 vxlan->dst_port,
2524 vxlan->default_dst.remote_vni,
2525 vxlan->default_dst.remote_ifindex,
2526 NTF_SELF);
2527 if (err)
2528 return err;
2529 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002530
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002531 err = register_netdevice(dev);
2532 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002533 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002534 return err;
2535 }
2536
stephen hemminger553675f2013-05-16 11:35:20 +00002537 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002538
2539 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002540}
2541
2542static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2543{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002544 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002545 struct vxlan_dev *vxlan = netdev_priv(dev);
2546
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002547 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002548 if (!hlist_unhashed(&vxlan->hlist))
2549 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002550 spin_unlock(&vn->sock_lock);
2551
stephen hemminger553675f2013-05-16 11:35:20 +00002552 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002553 unregister_netdevice_queue(dev, head);
2554}
2555
2556static size_t vxlan_get_size(const struct net_device *dev)
2557{
2558
2559 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002560 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002561 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002562 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002563 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2564 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2565 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002566 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2567 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2568 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2569 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002570 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2571 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002572 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00002573 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00002574 0;
2575}
2576
2577static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2578{
2579 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002580 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002581 struct ifla_vxlan_port_range ports = {
2582 .low = htons(vxlan->port_min),
2583 .high = htons(vxlan->port_max),
2584 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002585
Atzm Watanabec7995c42013-04-16 02:50:52 +00002586 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002587 goto nla_put_failure;
2588
Cong Wange4c7ed42013-08-31 13:44:33 +08002589 if (!vxlan_addr_any(&dst->remote_ip)) {
2590 if (dst->remote_ip.sa.sa_family == AF_INET) {
2591 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2592 dst->remote_ip.sin.sin_addr.s_addr))
2593 goto nla_put_failure;
2594#if IS_ENABLED(CONFIG_IPV6)
2595 } else {
2596 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2597 &dst->remote_ip.sin6.sin6_addr))
2598 goto nla_put_failure;
2599#endif
2600 }
2601 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002602
Atzm Watanabec7995c42013-04-16 02:50:52 +00002603 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002604 goto nla_put_failure;
2605
Cong Wange4c7ed42013-08-31 13:44:33 +08002606 if (!vxlan_addr_any(&vxlan->saddr)) {
2607 if (vxlan->saddr.sa.sa_family == AF_INET) {
2608 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2609 vxlan->saddr.sin.sin_addr.s_addr))
2610 goto nla_put_failure;
2611#if IS_ENABLED(CONFIG_IPV6)
2612 } else {
2613 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2614 &vxlan->saddr.sin6.sin6_addr))
2615 goto nla_put_failure;
2616#endif
2617 }
2618 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002619
2620 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2621 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002622 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2623 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2624 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2625 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2626 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2627 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2628 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2629 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2630 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002631 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002632 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2633 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00002634 goto nla_put_failure;
2635
stephen hemminger05f47d62012-10-09 20:35:50 +00002636 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2637 goto nla_put_failure;
2638
stephen hemmingerd3428942012-10-01 12:32:35 +00002639 return 0;
2640
2641nla_put_failure:
2642 return -EMSGSIZE;
2643}
2644
2645static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2646 .kind = "vxlan",
2647 .maxtype = IFLA_VXLAN_MAX,
2648 .policy = vxlan_policy,
2649 .priv_size = sizeof(struct vxlan_dev),
2650 .setup = vxlan_setup,
2651 .validate = vxlan_validate,
2652 .newlink = vxlan_newlink,
2653 .dellink = vxlan_dellink,
2654 .get_size = vxlan_get_size,
2655 .fill_info = vxlan_fill_info,
2656};
2657
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002658static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2659 struct net_device *dev)
2660{
2661 struct vxlan_dev *vxlan, *next;
2662 LIST_HEAD(list_kill);
2663
2664 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2665 struct vxlan_rdst *dst = &vxlan->default_dst;
2666
2667 /* In case we created vxlan device with carrier
2668 * and we loose the carrier due to module unload
2669 * we also need to remove vxlan device. In other
2670 * cases, it's not necessary and remote_ifindex
2671 * is 0 here, so no matches.
2672 */
2673 if (dst->remote_ifindex == dev->ifindex)
2674 vxlan_dellink(vxlan->dev, &list_kill);
2675 }
2676
2677 unregister_netdevice_many(&list_kill);
2678}
2679
2680static int vxlan_lowerdev_event(struct notifier_block *unused,
2681 unsigned long event, void *ptr)
2682{
2683 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmannfd27e0d2014-01-17 12:55:06 +01002684 struct vxlan_net *vn;
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002685
Daniel Borkmannfd27e0d2014-01-17 12:55:06 +01002686 if (event == NETDEV_UNREGISTER) {
2687 vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002688 vxlan_handle_lowerdev_unregister(vn, dev);
Daniel Borkmannfd27e0d2014-01-17 12:55:06 +01002689 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002690
2691 return NOTIFY_DONE;
2692}
2693
2694static struct notifier_block vxlan_notifier_block __read_mostly = {
2695 .notifier_call = vxlan_lowerdev_event,
2696};
2697
stephen hemmingerd3428942012-10-01 12:32:35 +00002698static __net_init int vxlan_init_net(struct net *net)
2699{
2700 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002701 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002702
stephen hemminger553675f2013-05-16 11:35:20 +00002703 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002704 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002705
stephen hemminger553675f2013-05-16 11:35:20 +00002706 for (h = 0; h < PORT_HASH_SIZE; ++h)
2707 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002708
2709 return 0;
2710}
2711
2712static __net_exit void vxlan_exit_net(struct net *net)
2713{
2714 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Daniel Borkmann84257832014-01-13 18:41:20 +01002715 struct vxlan_dev *vxlan, *next;
2716 LIST_HEAD(list_kill);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002717
2718 rtnl_lock();
Daniel Borkmann84257832014-01-13 18:41:20 +01002719 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next)
2720 vxlan_dellink(vxlan->dev, &list_kill);
2721 unregister_netdevice_many(&list_kill);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002722 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00002723}
2724
2725static struct pernet_operations vxlan_net_ops = {
2726 .init = vxlan_init_net,
2727 .exit = vxlan_exit_net,
2728 .id = &vxlan_net_id,
2729 .size = sizeof(struct vxlan_net),
2730};
2731
2732static int __init vxlan_init_module(void)
2733{
2734 int rc;
2735
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002736 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2737 if (!vxlan_wq)
2738 return -ENOMEM;
2739
stephen hemmingerd3428942012-10-01 12:32:35 +00002740 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2741
2742 rc = register_pernet_device(&vxlan_net_ops);
2743 if (rc)
2744 goto out1;
2745
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002746 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002747 if (rc)
2748 goto out2;
2749
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002750 rc = rtnl_link_register(&vxlan_link_ops);
2751 if (rc)
2752 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00002753
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002754 return 0;
2755out3:
2756 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002757out2:
2758 unregister_pernet_device(&vxlan_net_ops);
2759out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002760 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002761 return rc;
2762}
Cong Wang7332a132013-05-27 22:35:53 +00002763late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002764
2765static void __exit vxlan_cleanup_module(void)
2766{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002767 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002768 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002769 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07002770 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00002771 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00002772}
2773module_exit(vxlan_cleanup_module);
2774
2775MODULE_LICENSE("GPL");
2776MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002777MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08002778MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00002779MODULE_ALIAS_RTNL_LINK("vxlan");