blob: 2587ac84f71a95206e79121642482b98c537cd60 [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>
Tom Herbert3ee64f32014-07-13 19:49:42 -070036#include <net/udp_tunnel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000037#include <net/rtnetlink.h>
38#include <net/route.h>
39#include <net/dsfield.h>
40#include <net/inet_ecn.h>
41#include <net/net_namespace.h>
42#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070043#include <net/vxlan.h>
Or Gerlitzdc01e7d2014-01-20 13:59:21 +020044#include <net/protocol.h>
Andy Zhouacbf74a2014-09-16 17:31:18 -070045#include <net/udp_tunnel.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080046#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ipv6.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080050#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080051#endif
Thomas Grafee122c72015-07-21 10:43:58 +020052#include <net/dst_metadata.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000053
54#define VXLAN_VERSION "0.1"
55
stephen hemminger553675f2013-05-16 11:35:20 +000056#define PORT_HASH_BITS 8
57#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000058#define VNI_HASH_BITS 10
59#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
60#define FDB_HASH_BITS 8
61#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
62#define FDB_AGE_DEFAULT 300 /* 5 min */
63#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
64
stephen hemminger23c578b2013-04-27 11:31:53 +000065/* UDP port for VXLAN traffic.
66 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070067 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000068 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070069static unsigned short vxlan_port __read_mostly = 8472;
70module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000071MODULE_PARM_DESC(udp_port, "Destination UDP port");
72
73static bool log_ecn_error = true;
74module_param(log_ecn_error, bool, 0644);
75MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
76
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070077static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000078
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030079static const u8 all_zeros_mac[ETH_ALEN];
80
stephen hemminger553675f2013-05-16 11:35:20 +000081/* per-network namespace private data for this module */
82struct vxlan_net {
83 struct list_head vxlan_list;
84 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070085 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000086};
87
Cong Wange4c7ed42013-08-31 13:44:33 +080088union vxlan_addr {
89 struct sockaddr_in sin;
90 struct sockaddr_in6 sin6;
91 struct sockaddr sa;
92};
93
David Stevens66817122013-03-15 04:35:51 +000094struct vxlan_rdst {
Cong Wange4c7ed42013-08-31 13:44:33 +080095 union vxlan_addr remote_ip;
David Stevens66817122013-03-15 04:35:51 +000096 __be16 remote_port;
97 u32 remote_vni;
98 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070099 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300100 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000101};
102
stephen hemmingerd3428942012-10-01 12:32:35 +0000103/* Forwarding table entry */
104struct vxlan_fdb {
105 struct hlist_node hlist; /* linked list of entries */
106 struct rcu_head rcu;
107 unsigned long updated; /* jiffies */
108 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700109 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +0200110 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +0000111 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000112 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000113};
114
stephen hemmingerd3428942012-10-01 12:32:35 +0000115/* Pseudo network device */
116struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000117 struct hlist_node hlist; /* vni hash table */
118 struct list_head next; /* vxlan's per namespace list */
119 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000120 struct net_device *dev;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +0200121 struct net *net; /* netns for packet i/o */
Atzm Watanabec7995c42013-04-16 02:50:52 +0000122 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800123 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000124 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000125 __u16 port_min; /* source port range */
126 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000127 __u8 tos; /* TOS override */
128 __u8 ttl;
Tom Herbert359a0ea2014-06-04 17:20:29 -0700129 u32 flags; /* VXLAN_F_* in vxlan.h */
stephen hemmingerd3428942012-10-01 12:32:35 +0000130
131 unsigned long age_interval;
132 struct timer_list age_timer;
133 spinlock_t hash_lock;
134 unsigned int addrcnt;
135 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000136
137 struct hlist_head fdb_head[FDB_HASH_SIZE];
138};
139
140/* salt for hash table */
141static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700142static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000143
Thomas Grafee122c72015-07-21 10:43:58 +0200144static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
145{
Thomas Grafe7030872015-07-21 10:44:01 +0200146 return vs->flags & VXLAN_F_COLLECT_METADATA ||
147 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +0200148}
149
Cong Wange4c7ed42013-08-31 13:44:33 +0800150#if IS_ENABLED(CONFIG_IPV6)
151static inline
152bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
153{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200154 if (a->sa.sa_family != b->sa.sa_family)
155 return false;
156 if (a->sa.sa_family == AF_INET6)
157 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
158 else
159 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800160}
161
162static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
163{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200164 if (ipa->sa.sa_family == AF_INET6)
165 return ipv6_addr_any(&ipa->sin6.sin6_addr);
166 else
167 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800168}
169
170static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
171{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200172 if (ipa->sa.sa_family == AF_INET6)
173 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
174 else
175 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800176}
177
178static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
179{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200180 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200181 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200182 ip->sa.sa_family = AF_INET6;
183 return 0;
184 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200185 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200186 ip->sa.sa_family = AF_INET;
187 return 0;
188 } else {
189 return -EAFNOSUPPORT;
190 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800191}
192
193static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200194 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800195{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200196 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200197 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200198 else
Jiri Benc930345e2015-03-29 16:59:25 +0200199 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800200}
201
202#else /* !CONFIG_IPV6 */
203
204static inline
205bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
206{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200207 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800208}
209
210static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
211{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200212 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800213}
214
215static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
216{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200217 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800218}
219
220static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
221{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200222 if (nla_len(nla) >= sizeof(struct in6_addr)) {
223 return -EAFNOSUPPORT;
224 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200225 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200226 ip->sa.sa_family = AF_INET;
227 return 0;
228 } else {
229 return -EAFNOSUPPORT;
230 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800231}
232
233static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200234 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800235{
Jiri Benc930345e2015-03-29 16:59:25 +0200236 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800237}
238#endif
239
stephen hemminger553675f2013-05-16 11:35:20 +0000240/* Virtual Network hash table head */
241static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
242{
243 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
244}
245
246/* Socket hash table head */
247static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000248{
249 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
250
stephen hemminger553675f2013-05-16 11:35:20 +0000251 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
252}
253
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700254/* First remote destination for a forwarding entry.
255 * Guaranteed to be non-NULL because remotes are never deleted.
256 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700257static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700258{
stephen hemminger5ca54612013-08-04 17:17:39 -0700259 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
260}
261
262static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
263{
264 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700265}
266
Thomas Grafac5132d2015-01-15 03:53:56 +0100267/* Find VXLAN socket based on network namespace, address family and UDP port
268 * and enabled unshareable flags.
269 */
270static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
271 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000272{
273 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800274
275 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000276
277 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200278 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Thomas Grafac5132d2015-01-15 03:53:56 +0100279 inet_sk(vs->sock->sk)->sk.sk_family == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800280 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000281 return vs;
282 }
283 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000284}
285
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700286static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000287{
288 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000289
stephen hemminger553675f2013-05-16 11:35:20 +0000290 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000291 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000292 return vxlan;
293 }
294
295 return NULL;
296}
297
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700298/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200299static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
Thomas Grafac5132d2015-01-15 03:53:56 +0100300 sa_family_t family, __be16 port,
301 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700302{
303 struct vxlan_sock *vs;
304
Thomas Grafac5132d2015-01-15 03:53:56 +0100305 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700306 if (!vs)
307 return NULL;
308
309 return vxlan_vs_find_vni(vs, id);
310}
311
stephen hemmingerd3428942012-10-01 12:32:35 +0000312/* Fill in neighbour message in skbuff. */
313static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700314 const struct vxlan_fdb *fdb,
315 u32 portid, u32 seq, int type, unsigned int flags,
316 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000317{
318 unsigned long now = jiffies;
319 struct nda_cacheinfo ci;
320 struct nlmsghdr *nlh;
321 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000322 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000323
324 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
325 if (nlh == NULL)
326 return -EMSGSIZE;
327
328 ndm = nlmsg_data(nlh);
329 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000330
331 send_eth = send_ip = true;
332
333 if (type == RTM_GETNEIGH) {
334 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800335 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000336 send_eth = !is_zero_ether_addr(fdb->eth_addr);
337 } else
338 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000339 ndm->ndm_state = fdb->state;
340 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000341 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800342 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000343
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100344 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100345 nla_put_s32(skb, NDA_LINK_NETNSID,
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +0200346 peernet2id_alloc(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100347 goto nla_put_failure;
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
Johannes Berg053c0952015-01-16 22:09:00 +0100373 nlmsg_end(skb, nlh);
374 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000375
376nla_put_failure:
377 nlmsg_cancel(skb, nlh);
378 return -EMSGSIZE;
379}
380
381static inline size_t vxlan_nlmsg_size(void)
382{
383 return NLMSG_ALIGN(sizeof(struct ndmsg))
384 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800385 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000386 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000387 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
388 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100389 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000390 + nla_total_size(sizeof(struct nda_cacheinfo));
391}
392
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200393static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
394 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000395{
396 struct net *net = dev_net(vxlan->dev);
397 struct sk_buff *skb;
398 int err = -ENOBUFS;
399
400 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
401 if (skb == NULL)
402 goto errout;
403
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200404 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000405 if (err < 0) {
406 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
407 WARN_ON(err == -EMSGSIZE);
408 kfree_skb(skb);
409 goto errout;
410 }
411
412 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
413 return;
414errout:
415 if (err < 0)
416 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
417}
418
Cong Wange4c7ed42013-08-31 13:44:33 +0800419static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000420{
421 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700422 struct vxlan_fdb f = {
423 .state = NUD_STALE,
424 };
425 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800426 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700427 .remote_vni = VXLAN_N_VID,
428 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700429
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200430 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000431}
432
433static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
434{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700435 struct vxlan_fdb f = {
436 .state = NUD_STALE,
437 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200438 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000439
David Stevense4f67ad2012-11-20 02:50:14 +0000440 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
441
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200442 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000443}
444
stephen hemmingerd3428942012-10-01 12:32:35 +0000445/* Hash Ethernet address */
446static u32 eth_hash(const unsigned char *addr)
447{
448 u64 value = get_unaligned((u64 *)addr);
449
450 /* only want 6 bytes */
451#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000452 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000453#else
454 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000455#endif
456 return hash_64(value, FDB_HASH_BITS);
457}
458
459/* Hash chain to use given mac address */
460static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
461 const u8 *mac)
462{
463 return &vxlan->fdb_head[eth_hash(mac)];
464}
465
466/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000467static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000468 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000469{
470 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
471 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000472
Sasha Levinb67bfe02013-02-27 17:06:00 -0800473 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700474 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000475 return f;
476 }
477
478 return NULL;
479}
480
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000481static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
482 const u8 *mac)
483{
484 struct vxlan_fdb *f;
485
486 f = __vxlan_find_mac(vxlan, mac);
487 if (f)
488 f->used = jiffies;
489
490 return f;
491}
492
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300493/* caller should hold vxlan->hash_lock */
494static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800495 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300496 __u32 vni, __u32 ifindex)
497{
498 struct vxlan_rdst *rd;
499
500 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800501 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300502 rd->remote_port == port &&
503 rd->remote_vni == vni &&
504 rd->remote_ifindex == ifindex)
505 return rd;
506 }
507
508 return NULL;
509}
510
Thomas Richter906dc182013-07-19 17:20:07 +0200511/* Replace destination of unicast mac */
512static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800513 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200514{
515 struct vxlan_rdst *rd;
516
517 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
518 if (rd)
519 return 0;
520
521 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
522 if (!rd)
523 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800524 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200525 rd->remote_port = port;
526 rd->remote_vni = vni;
527 rd->remote_ifindex = ifindex;
528 return 1;
529}
530
David Stevens66817122013-03-15 04:35:51 +0000531/* Add/update destinations for multicast */
532static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200533 union vxlan_addr *ip, __be16 port, __u32 vni,
534 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000535{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700536 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000537
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300538 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
539 if (rd)
540 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700541
David Stevens66817122013-03-15 04:35:51 +0000542 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
543 if (rd == NULL)
544 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800545 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000546 rd->remote_port = port;
547 rd->remote_vni = vni;
548 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700549
550 list_add_tail_rcu(&rd->list, &f->remotes);
551
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200552 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000553 return 1;
554}
555
Tom Herbertdfd86452015-01-12 17:00:38 -0800556static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
557 unsigned int off,
558 struct vxlanhdr *vh, size_t hdrlen,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800559 u32 data, struct gro_remcsum *grc,
560 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800561{
562 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -0800563
564 if (skb->remcsum_offload)
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800565 return NULL;
Tom Herbertdfd86452015-01-12 17:00:38 -0800566
567 if (!NAPI_GRO_CB(skb)->csum_valid)
568 return NULL;
569
570 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
571 offset = start + ((data & VXLAN_RCO_UDP) ?
572 offsetof(struct udphdr, check) :
573 offsetof(struct tcphdr, check));
574
575 plen = hdrlen + offset + sizeof(u16);
576
577 /* Pull checksum that will be written */
578 if (skb_gro_header_hard(skb, off + plen)) {
579 vh = skb_gro_header_slow(skb, off + plen, off);
580 if (!vh)
581 return NULL;
582 }
583
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800584 skb_gro_remcsum_process(skb, (void *)vh + hdrlen,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800585 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800586
587 skb->remcsum_offload = 1;
588
589 return vh;
590}
591
Tom Herberta2b12f32015-01-12 17:00:37 -0800592static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
593 struct sk_buff *skb,
594 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200595{
596 struct sk_buff *p, **pp = NULL;
597 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800598 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200599 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800600 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
601 udp_offloads);
602 u32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800603 struct gro_remcsum grc;
604
605 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200606
607 off_vx = skb_gro_offset(skb);
608 hlen = off_vx + sizeof(*vh);
609 vh = skb_gro_header_fast(skb, off_vx);
610 if (skb_gro_header_hard(skb, hlen)) {
611 vh = skb_gro_header_slow(skb, hlen, off_vx);
612 if (unlikely(!vh))
613 goto out;
614 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200615
Tom Herbertdfd86452015-01-12 17:00:38 -0800616 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
617 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
618
619 flags = ntohl(vh->vx_flags);
620
621 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
622 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800623 ntohl(vh->vx_vni), &grc,
624 !!(vs->flags &
625 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800626
627 if (!vh)
628 goto out;
629 }
630
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200631 flush = 0;
632
633 for (p = *head; p; p = p->next) {
634 if (!NAPI_GRO_CB(p)->same_flow)
635 continue;
636
637 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100638 if (vh->vx_flags != vh2->vx_flags ||
639 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200640 NAPI_GRO_CB(p)->same_flow = 0;
641 continue;
642 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200643 }
644
Jesse Gross9b174d82014-12-30 19:10:15 -0800645 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200646
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200647out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800648 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200649 NAPI_GRO_CB(skb)->flush |= flush;
650
651 return pp;
652}
653
Tom Herberta2b12f32015-01-12 17:00:37 -0800654static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
655 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200656{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800657 udp_tunnel_gro_complete(skb, nhoff);
658
Jesse Gross9b174d82014-12-30 19:10:15 -0800659 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200660}
661
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700662/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200663static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700664{
665 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200666 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700667 struct net *net = sock_net(sk);
668 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700669 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200670 int err;
671
672 if (sa_family == AF_INET) {
673 err = udp_add_offload(&vs->udp_offloads);
674 if (err)
675 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
676 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700677
678 rcu_read_lock();
679 for_each_netdev_rcu(net, dev) {
680 if (dev->netdev_ops->ndo_add_vxlan_port)
681 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
682 port);
683 }
684 rcu_read_unlock();
685}
686
687/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200688static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700689{
690 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200691 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700692 struct net *net = sock_net(sk);
693 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700694 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700695
696 rcu_read_lock();
697 for_each_netdev_rcu(net, dev) {
698 if (dev->netdev_ops->ndo_del_vxlan_port)
699 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
700 port);
701 }
702 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200703
704 if (sa_family == AF_INET)
705 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700706}
707
stephen hemmingerd3428942012-10-01 12:32:35 +0000708/* Add new entry to forwarding table -- assumes lock held */
709static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800710 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000711 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000712 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000713 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000714{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200715 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000716 struct vxlan_fdb *f;
717 int notify = 0;
718
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000719 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000720 if (f) {
721 if (flags & NLM_F_EXCL) {
722 netdev_dbg(vxlan->dev,
723 "lost race to create %pM\n", mac);
724 return -EEXIST;
725 }
726 if (f->state != state) {
727 f->state = state;
728 f->updated = jiffies;
729 notify = 1;
730 }
David Stevensae884082013-04-19 00:36:26 +0000731 if (f->flags != ndm_flags) {
732 f->flags = ndm_flags;
733 f->updated = jiffies;
734 notify = 1;
735 }
Thomas Richter906dc182013-07-19 17:20:07 +0200736 if ((flags & NLM_F_REPLACE)) {
737 /* Only change unicasts */
738 if (!(is_multicast_ether_addr(f->eth_addr) ||
739 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800740 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200741 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200742 } else
743 return -EOPNOTSUPP;
744 }
David Stevens66817122013-03-15 04:35:51 +0000745 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300746 (is_multicast_ether_addr(f->eth_addr) ||
747 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200748 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
749 &rd);
David Stevens66817122013-03-15 04:35:51 +0000750
751 if (rc < 0)
752 return rc;
753 notify |= rc;
754 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000755 } else {
756 if (!(flags & NLM_F_CREATE))
757 return -ENOENT;
758
759 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
760 return -ENOSPC;
761
Thomas Richter906dc182013-07-19 17:20:07 +0200762 /* Disallow replace to add a multicast entry */
763 if ((flags & NLM_F_REPLACE) &&
764 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
765 return -EOPNOTSUPP;
766
Cong Wange4c7ed42013-08-31 13:44:33 +0800767 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000768 f = kmalloc(sizeof(*f), GFP_ATOMIC);
769 if (!f)
770 return -ENOMEM;
771
772 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000773 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000774 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000775 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700776 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000777 memcpy(f->eth_addr, mac, ETH_ALEN);
778
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200779 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700780
stephen hemmingerd3428942012-10-01 12:32:35 +0000781 ++vxlan->addrcnt;
782 hlist_add_head_rcu(&f->hlist,
783 vxlan_fdb_head(vxlan, mac));
784 }
785
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200786 if (notify) {
787 if (rd == NULL)
788 rd = first_remote_rtnl(f);
789 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
790 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000791
792 return 0;
793}
794
Wei Yongjun6706c822013-04-11 19:00:35 +0000795static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000796{
797 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700798 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000799
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700800 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000801 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000802 kfree(f);
803}
804
stephen hemmingerd3428942012-10-01 12:32:35 +0000805static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
806{
807 netdev_dbg(vxlan->dev,
808 "delete %pM\n", f->eth_addr);
809
810 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200811 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000812
813 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000814 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000815}
816
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300817static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800818 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300819{
820 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800821 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300822
823 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800824 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
825 if (err)
826 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300827 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800828 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
829 if (remote->sa.sa_family == AF_INET) {
830 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
831 ip->sa.sa_family = AF_INET;
832#if IS_ENABLED(CONFIG_IPV6)
833 } else {
834 ip->sin6.sin6_addr = in6addr_any;
835 ip->sa.sa_family = AF_INET6;
836#endif
837 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300838 }
839
840 if (tb[NDA_PORT]) {
841 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
842 return -EINVAL;
843 *port = nla_get_be16(tb[NDA_PORT]);
844 } else {
845 *port = vxlan->dst_port;
846 }
847
848 if (tb[NDA_VNI]) {
849 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
850 return -EINVAL;
851 *vni = nla_get_u32(tb[NDA_VNI]);
852 } else {
853 *vni = vxlan->default_dst.remote_vni;
854 }
855
856 if (tb[NDA_IFINDEX]) {
857 struct net_device *tdev;
858
859 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
860 return -EINVAL;
861 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800862 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300863 if (!tdev)
864 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300865 } else {
866 *ifindex = 0;
867 }
868
869 return 0;
870}
871
stephen hemmingerd3428942012-10-01 12:32:35 +0000872/* Add static entry (via netlink) */
873static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
874 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100875 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000876{
877 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300878 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800879 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000880 __be16 port;
881 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000882 int err;
883
884 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
885 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
886 ndm->ndm_state);
887 return -EINVAL;
888 }
889
890 if (tb[NDA_DST] == NULL)
891 return -EINVAL;
892
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300893 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
894 if (err)
895 return err;
David Stevens66817122013-03-15 04:35:51 +0000896
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300897 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
898 return -EAFNOSUPPORT;
899
stephen hemmingerd3428942012-10-01 12:32:35 +0000900 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800901 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000902 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000903 spin_unlock_bh(&vxlan->hash_lock);
904
905 return err;
906}
907
908/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000909static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
910 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100911 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000912{
913 struct vxlan_dev *vxlan = netdev_priv(dev);
914 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300915 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800916 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300917 __be16 port;
918 u32 vni, ifindex;
919 int err;
920
921 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
922 if (err)
923 return err;
924
925 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000926
927 spin_lock_bh(&vxlan->hash_lock);
928 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300929 if (!f)
930 goto out;
931
Cong Wange4c7ed42013-08-31 13:44:33 +0800932 if (!vxlan_addr_any(&ip)) {
933 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300934 if (!rd)
935 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000936 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300937
938 err = 0;
939
940 /* remove a destination if it's not the only one on the list,
941 * otherwise destroy the fdb entry
942 */
943 if (rd && !list_is_singular(&f->remotes)) {
944 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200945 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800946 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300947 goto out;
948 }
949
950 vxlan_fdb_destroy(vxlan, f);
951
952out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000953 spin_unlock_bh(&vxlan->hash_lock);
954
955 return err;
956}
957
958/* Dump forwarding table */
959static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400960 struct net_device *dev,
961 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000962{
963 struct vxlan_dev *vxlan = netdev_priv(dev);
964 unsigned int h;
965
966 for (h = 0; h < FDB_HASH_SIZE; ++h) {
967 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000968 int err;
969
Sasha Levinb67bfe02013-02-27 17:06:00 -0800970 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000971 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000972
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700973 if (idx < cb->args[0])
974 goto skip;
975
976 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000977 err = vxlan_fdb_info(skb, vxlan, f,
978 NETLINK_CB(cb->skb).portid,
979 cb->nlh->nlmsg_seq,
980 RTM_NEWNEIGH,
981 NLM_F_MULTI, rd);
982 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700983 goto out;
David Stevens66817122013-03-15 04:35:51 +0000984 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700985skip:
986 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000987 }
988 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700989out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000990 return idx;
991}
992
993/* Watch incoming packets to learn mapping between Ethernet address
994 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900995 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000996 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700997static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800998 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000999{
1000 struct vxlan_dev *vxlan = netdev_priv(dev);
1001 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001002
1003 f = vxlan_find_mac(vxlan, src_mac);
1004 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001005 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001006
Cong Wange4c7ed42013-08-31 13:44:33 +08001007 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001008 return false;
1009
1010 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -07001011 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -07001012 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001013
1014 if (net_ratelimit())
1015 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001016 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001017 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001018
Cong Wange4c7ed42013-08-31 13:44:33 +08001019 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001020 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001021 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001022 } else {
1023 /* learned new entry */
1024 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001025
1026 /* close off race between vxlan_flush and incoming packets */
1027 if (netif_running(dev))
1028 vxlan_fdb_create(vxlan, src_mac, src_ip,
1029 NUD_REACHABLE,
1030 NLM_F_EXCL|NLM_F_CREATE,
1031 vxlan->dst_port,
1032 vxlan->default_dst.remote_vni,
1033 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001034 spin_unlock(&vxlan->hash_lock);
1035 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001036
1037 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001038}
1039
stephen hemmingerd3428942012-10-01 12:32:35 +00001040/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001041static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001042{
stephen hemminger553675f2013-05-16 11:35:20 +00001043 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001044
Gao feng95ab0992013-12-10 16:37:33 +08001045 /* The vxlan_sock is only used by dev, leaving group has
1046 * no effect on other vxlan devices.
1047 */
1048 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1049 return false;
1050
stephen hemminger553675f2013-05-16 11:35:20 +00001051 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001052 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001053 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001054
Gao feng95ab0992013-12-10 16:37:33 +08001055 if (vxlan->vn_sock != dev->vn_sock)
1056 continue;
1057
1058 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1059 &dev->default_dst.remote_ip))
1060 continue;
1061
1062 if (vxlan->default_dst.remote_ifindex !=
1063 dev->default_dst.remote_ifindex)
1064 continue;
1065
1066 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001067 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001068
1069 return false;
1070}
1071
Pravin B Shelar012a5722013-08-19 11:23:07 -07001072void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001073{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001074 struct sock *sk = vs->sock->sk;
1075 struct net *net = sock_net(sk);
1076 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001077
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001078 if (!atomic_dec_and_test(&vs->refcnt))
1079 return;
1080
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001081 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001082 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001083 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001084 spin_unlock(&vn->sock_lock);
1085
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001086 queue_work(vxlan_wq, &vs->del_work);
1087}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001088EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001089
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001090/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001091 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001092 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001093static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001094{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001095 struct vxlan_sock *vs = vxlan->vn_sock;
1096 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001097 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1098 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001099 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001100
stephen hemmingerd3428942012-10-01 12:32:35 +00001101 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001102 if (ip->sa.sa_family == AF_INET) {
1103 struct ip_mreqn mreq = {
1104 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1105 .imr_ifindex = ifindex,
1106 };
1107
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001108 ret = ip_mc_join_group(sk, &mreq);
Cong Wange4c7ed42013-08-31 13:44:33 +08001109#if IS_ENABLED(CONFIG_IPV6)
1110 } else {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001111 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1112 &ip->sin6.sin6_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001113#endif
1114 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001115 release_sock(sk);
1116
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001117 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001118}
1119
1120/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001121static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001122{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001123 struct vxlan_sock *vs = vxlan->vn_sock;
1124 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001125 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1126 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001127 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001128
1129 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001130 if (ip->sa.sa_family == AF_INET) {
1131 struct ip_mreqn mreq = {
1132 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1133 .imr_ifindex = ifindex,
1134 };
1135
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001136 ret = ip_mc_leave_group(sk, &mreq);
Cong Wange4c7ed42013-08-31 13:44:33 +08001137#if IS_ENABLED(CONFIG_IPV6)
1138 } else {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001139 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1140 &ip->sin6.sin6_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001141#endif
1142 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001143 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001144
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001145 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001146}
1147
Tom Herbertdfd86452015-01-12 17:00:38 -08001148static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001149 size_t hdrlen, u32 data, bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -08001150{
1151 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -08001152
Tom Herbertdfd86452015-01-12 17:00:38 -08001153 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1154 offset = start + ((data & VXLAN_RCO_UDP) ?
1155 offsetof(struct udphdr, check) :
1156 offsetof(struct tcphdr, check));
1157
1158 plen = hdrlen + offset + sizeof(u16);
1159
1160 if (!pskb_may_pull(skb, plen))
1161 return NULL;
1162
1163 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1164
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001165 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset,
1166 nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -08001167
1168 return vh;
1169}
1170
stephen hemmingerd3428942012-10-01 12:32:35 +00001171/* Callback from net/ipv4/udp.c to receive packets */
1172static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1173{
Thomas Grafee122c72015-07-21 10:43:58 +02001174 struct metadata_dst *tun_dst = NULL;
1175 struct ip_tunnel_info *info;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001176 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001177 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001178 u32 flags, vni;
Thomas Grafee122c72015-07-21 10:43:58 +02001179 struct vxlan_metadata _md;
1180 struct vxlan_metadata *md = &_md;
stephen hemmingerd3428942012-10-01 12:32:35 +00001181
stephen hemmingerd3428942012-10-01 12:32:35 +00001182 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001183 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001184 goto error;
1185
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001186 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001187 flags = ntohl(vxh->vx_flags);
1188 vni = ntohl(vxh->vx_vni);
1189
1190 if (flags & VXLAN_HF_VNI) {
1191 flags &= ~VXLAN_HF_VNI;
1192 } else {
1193 /* VNI flag always required to be set */
1194 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001195 }
1196
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001197 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001198 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001199 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001200
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001201 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001202 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001203 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001204
Tom Herbertdfd86452015-01-12 17:00:38 -08001205 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001206 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni,
1207 !!(vs->flags & VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -08001208 if (!vxh)
1209 goto drop;
1210
1211 flags &= ~VXLAN_HF_RCO;
Alexey Kodanev40fb70f2015-03-13 19:13:53 +03001212 vni &= VXLAN_VNI_MASK;
Tom Herbertdfd86452015-01-12 17:00:38 -08001213 }
1214
Thomas Grafee122c72015-07-21 10:43:58 +02001215 if (vxlan_collect_metadata(vs)) {
1216 const struct iphdr *iph = ip_hdr(skb);
1217
1218 tun_dst = metadata_dst_alloc(sizeof(*md), GFP_ATOMIC);
1219 if (!tun_dst)
1220 goto drop;
1221
1222 info = &tun_dst->u.tun_info;
1223 info->key.ipv4_src = iph->saddr;
1224 info->key.ipv4_dst = iph->daddr;
1225 info->key.ipv4_tos = iph->tos;
1226 info->key.ipv4_ttl = iph->ttl;
1227 info->key.tp_src = udp_hdr(skb)->source;
1228 info->key.tp_dst = udp_hdr(skb)->dest;
1229
1230 info->mode = IP_TUNNEL_INFO_RX;
1231 info->key.tun_flags = TUNNEL_KEY;
1232 info->key.tun_id = cpu_to_be64(vni >> 8);
1233 if (udp_hdr(skb)->check != 0)
1234 info->key.tun_flags |= TUNNEL_CSUM;
1235
1236 md = ip_tunnel_info_opts(info, sizeof(*md));
1237 md->tun_dst = tun_dst;
1238 } else {
1239 memset(md, 0, sizeof(*md));
1240 }
1241
Thomas Graf35114942015-01-15 03:53:55 +01001242 /* For backwards compatibility, only allow reserved fields to be
1243 * used by VXLAN extensions if explicitly requested.
1244 */
1245 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1246 struct vxlanhdr_gbp *gbp;
1247
1248 gbp = (struct vxlanhdr_gbp *)vxh;
Thomas Grafee122c72015-07-21 10:43:58 +02001249 md->gbp = ntohs(gbp->policy_id);
1250
1251 if (tun_dst)
1252 info->key.tun_flags |= TUNNEL_VXLAN_OPT;
Thomas Graf35114942015-01-15 03:53:55 +01001253
1254 if (gbp->dont_learn)
Thomas Grafee122c72015-07-21 10:43:58 +02001255 md->gbp |= VXLAN_GBP_DONT_LEARN;
Thomas Graf35114942015-01-15 03:53:55 +01001256
1257 if (gbp->policy_applied)
Thomas Grafee122c72015-07-21 10:43:58 +02001258 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Thomas Graf35114942015-01-15 03:53:55 +01001259
1260 flags &= ~VXLAN_GBP_USED_BITS;
1261 }
1262
Alexey Kodanev40fb70f2015-03-13 19:13:53 +03001263 if (flags || vni & ~VXLAN_VNI_MASK) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001264 /* If there are any unprocessed flags remaining treat
1265 * this as a malformed packet. This behavior diverges from
1266 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1267 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001268 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001269 * is more robust and provides a little more security in
1270 * adding extensions to VXLAN.
1271 */
1272
1273 goto bad_flags;
1274 }
1275
Thomas Grafee122c72015-07-21 10:43:58 +02001276 md->vni = vxh->vx_vni;
1277 vs->rcv(vs, skb, md);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001278 return 0;
1279
1280drop:
1281 /* Consume bad packet */
1282 kfree_skb(skb);
1283 return 0;
1284
Tom Herbert3bf39472015-01-08 12:31:18 -08001285bad_flags:
1286 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1287 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1288
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001289error:
Thomas Grafee122c72015-07-21 10:43:58 +02001290 if (tun_dst)
1291 dst_release((struct dst_entry *)tun_dst);
1292
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001293 /* Return non vxlan pkt */
1294 return 1;
1295}
1296
Thomas Graf35114942015-01-15 03:53:55 +01001297static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1298 struct vxlan_metadata *md)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001299{
Cong Wange4c7ed42013-08-31 13:44:33 +08001300 struct iphdr *oip = NULL;
1301 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001302 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001303 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001304 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001305 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001306 int err = 0;
1307 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001308
Thomas Grafee122c72015-07-21 10:43:58 +02001309 /* For flow based devices, map all packets to VNI 0 */
1310 if (vs->flags & VXLAN_F_FLOW_BASED)
1311 vni = 0;
1312 else
1313 vni = ntohl(md->vni) >> 8;
1314
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001315 /* Is this VNI defined? */
1316 vxlan = vxlan_vs_find_vni(vs, vni);
1317 if (!vxlan)
1318 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001319
Cong Wange4c7ed42013-08-31 13:44:33 +08001320 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001321 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001322 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001323 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001324 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001325
1326 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001327 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001328 goto drop;
1329
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001330 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001331 if (remote_ip->sa.sa_family == AF_INET) {
1332 oip = ip_hdr(skb);
1333 saddr.sin.sin_addr.s_addr = oip->saddr;
1334 saddr.sa.sa_family = AF_INET;
1335#if IS_ENABLED(CONFIG_IPV6)
1336 } else {
1337 oip6 = ipv6_hdr(skb);
1338 saddr.sin6.sin6_addr = oip6->saddr;
1339 saddr.sa.sa_family = AF_INET6;
1340#endif
1341 }
1342
Thomas Grafee122c72015-07-21 10:43:58 +02001343 if (md->tun_dst) {
1344 skb_dst_set(skb, (struct dst_entry *)md->tun_dst);
1345 md->tun_dst = NULL;
1346 }
1347
stephen hemminger26a41ae62013-06-17 12:09:58 -07001348 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001349 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001350 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001351
stephen hemmingerd3428942012-10-01 12:32:35 +00001352 skb_reset_network_header(skb);
Thomas Grafee122c72015-07-21 10:43:58 +02001353 /* In flow-based mode, GBP is carried in dst_metadata */
1354 if (!(vs->flags & VXLAN_F_FLOW_BASED))
1355 skb->mark = md->gbp;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001356
Cong Wange4c7ed42013-08-31 13:44:33 +08001357 if (oip6)
1358 err = IP6_ECN_decapsulate(oip6, skb);
1359 if (oip)
1360 err = IP_ECN_decapsulate(oip, skb);
1361
stephen hemmingerd3428942012-10-01 12:32:35 +00001362 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001363 if (log_ecn_error) {
1364 if (oip6)
1365 net_info_ratelimited("non-ECT from %pI6\n",
1366 &oip6->saddr);
1367 if (oip)
1368 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1369 &oip->saddr, oip->tos);
1370 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001371 if (err > 1) {
1372 ++vxlan->dev->stats.rx_frame_errors;
1373 ++vxlan->dev->stats.rx_errors;
1374 goto drop;
1375 }
1376 }
1377
Pravin B Shelare8171042013-03-25 14:49:46 +00001378 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001379 u64_stats_update_begin(&stats->syncp);
1380 stats->rx_packets++;
1381 stats->rx_bytes += skb->len;
1382 u64_stats_update_end(&stats->syncp);
1383
1384 netif_rx(skb);
1385
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001386 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001387drop:
Thomas Grafee122c72015-07-21 10:43:58 +02001388 if (md->tun_dst)
1389 dst_release((struct dst_entry *)md->tun_dst);
1390
stephen hemmingerd3428942012-10-01 12:32:35 +00001391 /* Consume bad packet */
1392 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001393}
1394
David Stevense4f67ad2012-11-20 02:50:14 +00001395static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1396{
1397 struct vxlan_dev *vxlan = netdev_priv(dev);
1398 struct arphdr *parp;
1399 u8 *arpptr, *sha;
1400 __be32 sip, tip;
1401 struct neighbour *n;
1402
1403 if (dev->flags & IFF_NOARP)
1404 goto out;
1405
1406 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1407 dev->stats.tx_dropped++;
1408 goto out;
1409 }
1410 parp = arp_hdr(skb);
1411
1412 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1413 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1414 parp->ar_pro != htons(ETH_P_IP) ||
1415 parp->ar_op != htons(ARPOP_REQUEST) ||
1416 parp->ar_hln != dev->addr_len ||
1417 parp->ar_pln != 4)
1418 goto out;
1419 arpptr = (u8 *)parp + sizeof(struct arphdr);
1420 sha = arpptr;
1421 arpptr += dev->addr_len; /* sha */
1422 memcpy(&sip, arpptr, sizeof(sip));
1423 arpptr += sizeof(sip);
1424 arpptr += dev->addr_len; /* tha */
1425 memcpy(&tip, arpptr, sizeof(tip));
1426
1427 if (ipv4_is_loopback(tip) ||
1428 ipv4_is_multicast(tip))
1429 goto out;
1430
1431 n = neigh_lookup(&arp_tbl, &tip, dev);
1432
1433 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001434 struct vxlan_fdb *f;
1435 struct sk_buff *reply;
1436
1437 if (!(n->nud_state & NUD_CONNECTED)) {
1438 neigh_release(n);
1439 goto out;
1440 }
1441
1442 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001443 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001444 /* bridge-local neighbor */
1445 neigh_release(n);
1446 goto out;
1447 }
1448
1449 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1450 n->ha, sha);
1451
1452 neigh_release(n);
1453
David Stevens73461352014-03-18 12:32:29 -04001454 if (reply == NULL)
1455 goto out;
1456
David Stevense4f67ad2012-11-20 02:50:14 +00001457 skb_reset_mac_header(reply);
1458 __skb_pull(reply, skb_network_offset(reply));
1459 reply->ip_summed = CHECKSUM_UNNECESSARY;
1460 reply->pkt_type = PACKET_HOST;
1461
1462 if (netif_rx_ni(reply) == NET_RX_DROP)
1463 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001464 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1465 union vxlan_addr ipa = {
1466 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001467 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001468 };
1469
1470 vxlan_ip_miss(dev, &ipa);
1471 }
David Stevense4f67ad2012-11-20 02:50:14 +00001472out:
1473 consume_skb(skb);
1474 return NETDEV_TX_OK;
1475}
1476
Cong Wangf564f452013-08-31 13:44:36 +08001477#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001478static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1479 struct neighbour *n, bool isrouter)
1480{
1481 struct net_device *dev = request->dev;
1482 struct sk_buff *reply;
1483 struct nd_msg *ns, *na;
1484 struct ipv6hdr *pip6;
1485 u8 *daddr;
1486 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1487 int ns_olen;
1488 int i, len;
1489
1490 if (dev == NULL)
1491 return NULL;
1492
1493 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1494 sizeof(*na) + na_olen + dev->needed_tailroom;
1495 reply = alloc_skb(len, GFP_ATOMIC);
1496 if (reply == NULL)
1497 return NULL;
1498
1499 reply->protocol = htons(ETH_P_IPV6);
1500 reply->dev = dev;
1501 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1502 skb_push(reply, sizeof(struct ethhdr));
1503 skb_set_mac_header(reply, 0);
1504
1505 ns = (struct nd_msg *)skb_transport_header(request);
1506
1507 daddr = eth_hdr(request)->h_source;
1508 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1509 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1510 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1511 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1512 break;
1513 }
1514 }
1515
1516 /* Ethernet header */
1517 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1518 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1519 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1520 reply->protocol = htons(ETH_P_IPV6);
1521
1522 skb_pull(reply, sizeof(struct ethhdr));
1523 skb_set_network_header(reply, 0);
1524 skb_put(reply, sizeof(struct ipv6hdr));
1525
1526 /* IPv6 header */
1527
1528 pip6 = ipv6_hdr(reply);
1529 memset(pip6, 0, sizeof(struct ipv6hdr));
1530 pip6->version = 6;
1531 pip6->priority = ipv6_hdr(request)->priority;
1532 pip6->nexthdr = IPPROTO_ICMPV6;
1533 pip6->hop_limit = 255;
1534 pip6->daddr = ipv6_hdr(request)->saddr;
1535 pip6->saddr = *(struct in6_addr *)n->primary_key;
1536
1537 skb_pull(reply, sizeof(struct ipv6hdr));
1538 skb_set_transport_header(reply, 0);
1539
1540 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1541
1542 /* Neighbor Advertisement */
1543 memset(na, 0, sizeof(*na)+na_olen);
1544 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1545 na->icmph.icmp6_router = isrouter;
1546 na->icmph.icmp6_override = 1;
1547 na->icmph.icmp6_solicited = 1;
1548 na->target = ns->target;
1549 ether_addr_copy(&na->opt[2], n->ha);
1550 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1551 na->opt[1] = na_olen >> 3;
1552
1553 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1554 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1555 csum_partial(na, sizeof(*na)+na_olen, 0));
1556
1557 pip6->payload_len = htons(sizeof(*na)+na_olen);
1558
1559 skb_push(reply, sizeof(struct ipv6hdr));
1560
1561 reply->ip_summed = CHECKSUM_UNNECESSARY;
1562
1563 return reply;
1564}
1565
Cong Wangf564f452013-08-31 13:44:36 +08001566static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1567{
1568 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001569 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001570 const struct ipv6hdr *iphdr;
1571 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001572 struct neighbour *n;
1573 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001574
1575 in6_dev = __in6_dev_get(dev);
1576 if (!in6_dev)
1577 goto out;
1578
Cong Wangf564f452013-08-31 13:44:36 +08001579 iphdr = ipv6_hdr(skb);
1580 saddr = &iphdr->saddr;
1581 daddr = &iphdr->daddr;
1582
Cong Wangf564f452013-08-31 13:44:36 +08001583 msg = (struct nd_msg *)skb_transport_header(skb);
1584 if (msg->icmph.icmp6_code != 0 ||
1585 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1586 goto out;
1587
David Stevens4b29dba2014-03-24 10:39:58 -04001588 if (ipv6_addr_loopback(daddr) ||
1589 ipv6_addr_is_multicast(&msg->target))
1590 goto out;
1591
1592 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001593
1594 if (n) {
1595 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001596 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001597
1598 if (!(n->nud_state & NUD_CONNECTED)) {
1599 neigh_release(n);
1600 goto out;
1601 }
1602
1603 f = vxlan_find_mac(vxlan, n->ha);
1604 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1605 /* bridge-local neighbor */
1606 neigh_release(n);
1607 goto out;
1608 }
1609
David Stevens4b29dba2014-03-24 10:39:58 -04001610 reply = vxlan_na_create(skb, n,
1611 !!(f ? f->flags & NTF_ROUTER : 0));
1612
Cong Wangf564f452013-08-31 13:44:36 +08001613 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001614
1615 if (reply == NULL)
1616 goto out;
1617
1618 if (netif_rx_ni(reply) == NET_RX_DROP)
1619 dev->stats.rx_dropped++;
1620
Cong Wangf564f452013-08-31 13:44:36 +08001621 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001622 union vxlan_addr ipa = {
1623 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001624 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001625 };
1626
Cong Wangf564f452013-08-31 13:44:36 +08001627 vxlan_ip_miss(dev, &ipa);
1628 }
1629
1630out:
1631 consume_skb(skb);
1632 return NETDEV_TX_OK;
1633}
1634#endif
1635
David Stevense4f67ad2012-11-20 02:50:14 +00001636static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1637{
1638 struct vxlan_dev *vxlan = netdev_priv(dev);
1639 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001640
1641 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1642 return false;
1643
1644 n = NULL;
1645 switch (ntohs(eth_hdr(skb)->h_proto)) {
1646 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001647 {
1648 struct iphdr *pip;
1649
David Stevense4f67ad2012-11-20 02:50:14 +00001650 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1651 return false;
1652 pip = ip_hdr(skb);
1653 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001654 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1655 union vxlan_addr ipa = {
1656 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001657 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001658 };
1659
1660 vxlan_ip_miss(dev, &ipa);
1661 return false;
1662 }
1663
David Stevense4f67ad2012-11-20 02:50:14 +00001664 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001665 }
1666#if IS_ENABLED(CONFIG_IPV6)
1667 case ETH_P_IPV6:
1668 {
1669 struct ipv6hdr *pip6;
1670
1671 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1672 return false;
1673 pip6 = ipv6_hdr(skb);
1674 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1675 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1676 union vxlan_addr ipa = {
1677 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001678 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001679 };
1680
1681 vxlan_ip_miss(dev, &ipa);
1682 return false;
1683 }
1684
1685 break;
1686 }
1687#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001688 default:
1689 return false;
1690 }
1691
1692 if (n) {
1693 bool diff;
1694
Joe Perches7367d0b2013-09-01 11:51:23 -07001695 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001696 if (diff) {
1697 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1698 dev->addr_len);
1699 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1700 }
1701 neigh_release(n);
1702 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001703 }
1704
David Stevense4f67ad2012-11-20 02:50:14 +00001705 return false;
1706}
1707
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001708static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001709 struct vxlan_metadata *md)
1710{
1711 struct vxlanhdr_gbp *gbp;
1712
Thomas Grafdb79a622015-02-04 17:00:04 +01001713 if (!md->gbp)
1714 return;
1715
Thomas Graf35114942015-01-15 03:53:55 +01001716 gbp = (struct vxlanhdr_gbp *)vxh;
1717 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1718
1719 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1720 gbp->dont_learn = 1;
1721
1722 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1723 gbp->policy_applied = 1;
1724
1725 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1726}
1727
Cong Wange4c7ed42013-08-31 13:44:33 +08001728#if IS_ENABLED(CONFIG_IPV6)
David Miller79b16aa2015-04-05 22:19:09 -04001729static int vxlan6_xmit_skb(struct dst_entry *dst, struct sock *sk,
1730 struct sk_buff *skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001731 struct net_device *dev, struct in6_addr *saddr,
1732 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Thomas Graf35114942015-01-15 03:53:55 +01001733 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001734 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Cong Wange4c7ed42013-08-31 13:44:33 +08001735{
Cong Wange4c7ed42013-08-31 13:44:33 +08001736 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001737 int min_headroom;
1738 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001739 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbertdfd86452015-01-12 17:00:38 -08001740 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1741 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001742
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001743 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001744 skb->ip_summed == CHECKSUM_PARTIAL) {
1745 int csum_start = skb_checksum_start_offset(skb);
1746
1747 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1748 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1749 (skb->csum_offset == offsetof(struct udphdr, check) ||
1750 skb->csum_offset == offsetof(struct tcphdr, check))) {
1751 udp_sum = false;
1752 type |= SKB_GSO_TUNNEL_REMCSUM;
1753 }
1754 }
1755
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001756 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001757
Cong Wange4c7ed42013-08-31 13:44:33 +08001758 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1759 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001760 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001761
1762 /* Need space for new headers (invalidates iph ptr) */
1763 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001764 if (unlikely(err)) {
1765 kfree_skb(skb);
1766 goto err;
1767 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001768
Jiri Pirko59682502014-11-19 14:04:59 +01001769 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001770 if (WARN_ON(!skb)) {
1771 err = -ENOMEM;
1772 goto err;
1773 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001774
Jesse Grossb736a622015-04-09 11:19:14 -07001775 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1776 if (IS_ERR(skb)) {
1777 err = -EINVAL;
1778 goto err;
1779 }
1780
Cong Wange4c7ed42013-08-31 13:44:33 +08001781 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001782 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001783 vxh->vx_vni = md->vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001784
Tom Herbertdfd86452015-01-12 17:00:38 -08001785 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1786 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1787 VXLAN_RCO_SHIFT;
1788
1789 if (skb->csum_offset == offsetof(struct udphdr, check))
1790 data |= VXLAN_RCO_UDP;
1791
1792 vxh->vx_vni |= htonl(data);
1793 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1794
1795 if (!skb_is_gso(skb)) {
1796 skb->ip_summed = CHECKSUM_NONE;
1797 skb->encapsulation = 0;
1798 }
1799 }
1800
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001801 if (vxflags & VXLAN_F_GBP)
1802 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001803
Tom Herbert996c9fd2014-09-29 20:22:33 -07001804 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1805
David Miller79b16aa2015-04-05 22:19:09 -04001806 udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio,
Tom Herbertd998f8e2015-01-20 11:23:04 -08001807 ttl, src_port, dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001808 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
Cong Wange4c7ed42013-08-31 13:44:33 +08001809 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001810err:
1811 dst_release(dst);
1812 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001813}
1814#endif
1815
David Miller79b16aa2015-04-05 22:19:09 -04001816int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
Pravin B Shelar49560532013-08-19 11:23:17 -07001817 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Thomas Graf35114942015-01-15 03:53:55 +01001818 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001819 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Pravin B Shelar49560532013-08-19 11:23:17 -07001820{
1821 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001822 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001823 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001824 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
Tom Herbertdfd86452015-01-12 17:00:38 -08001825 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1826 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001827
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001828 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001829 skb->ip_summed == CHECKSUM_PARTIAL) {
1830 int csum_start = skb_checksum_start_offset(skb);
1831
1832 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1833 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1834 (skb->csum_offset == offsetof(struct udphdr, check) ||
1835 skb->csum_offset == offsetof(struct tcphdr, check))) {
1836 udp_sum = false;
1837 type |= SKB_GSO_TUNNEL_REMCSUM;
1838 }
1839 }
1840
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001841 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001842 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001843 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001844
1845 /* Need space for new headers (invalidates iph ptr) */
1846 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001847 if (unlikely(err)) {
1848 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001849 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001850 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001851
Jiri Pirko59682502014-11-19 14:04:59 +01001852 skb = vlan_hwaccel_push_inside(skb);
1853 if (WARN_ON(!skb))
1854 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001855
Jesse Grossb736a622015-04-09 11:19:14 -07001856 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1857 if (IS_ERR(skb))
1858 return PTR_ERR(skb);
1859
Pravin B Shelar49560532013-08-19 11:23:17 -07001860 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001861 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001862 vxh->vx_vni = md->vni;
Pravin B Shelar49560532013-08-19 11:23:17 -07001863
Tom Herbertdfd86452015-01-12 17:00:38 -08001864 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1865 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1866 VXLAN_RCO_SHIFT;
1867
1868 if (skb->csum_offset == offsetof(struct udphdr, check))
1869 data |= VXLAN_RCO_UDP;
1870
1871 vxh->vx_vni |= htonl(data);
1872 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1873
1874 if (!skb_is_gso(skb)) {
1875 skb->ip_summed = CHECKSUM_NONE;
1876 skb->encapsulation = 0;
1877 }
1878 }
1879
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001880 if (vxflags & VXLAN_F_GBP)
1881 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001882
Tom Herbert996c9fd2014-09-29 20:22:33 -07001883 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1884
David Miller79b16aa2015-04-05 22:19:09 -04001885 return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
Tom Herbertd998f8e2015-01-20 11:23:04 -08001886 ttl, df, src_port, dst_port, xnet,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001887 !(vxflags & VXLAN_F_UDP_CSUM));
Pravin B Shelar49560532013-08-19 11:23:17 -07001888}
1889EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1890
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001891/* Bypass encapsulation if the destination is local */
1892static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1893 struct vxlan_dev *dst_vxlan)
1894{
Li RongQing8f849852014-01-04 13:57:59 +08001895 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001896 union vxlan_addr loopback;
1897 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001898 struct net_device *dev = skb->dev;
1899 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001900
Li RongQing8f849852014-01-04 13:57:59 +08001901 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1902 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001903 skb->pkt_type = PACKET_HOST;
1904 skb->encapsulation = 0;
1905 skb->dev = dst_vxlan->dev;
1906 __skb_pull(skb, skb_network_offset(skb));
1907
Cong Wange4c7ed42013-08-31 13:44:33 +08001908 if (remote_ip->sa.sa_family == AF_INET) {
1909 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1910 loopback.sa.sa_family = AF_INET;
1911#if IS_ENABLED(CONFIG_IPV6)
1912 } else {
1913 loopback.sin6.sin6_addr = in6addr_loopback;
1914 loopback.sa.sa_family = AF_INET6;
1915#endif
1916 }
1917
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001918 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001919 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001920
1921 u64_stats_update_begin(&tx_stats->syncp);
1922 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001923 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001924 u64_stats_update_end(&tx_stats->syncp);
1925
1926 if (netif_rx(skb) == NET_RX_SUCCESS) {
1927 u64_stats_update_begin(&rx_stats->syncp);
1928 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001929 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001930 u64_stats_update_end(&rx_stats->syncp);
1931 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001932 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001933 }
1934}
1935
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001936static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1937 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001938{
Thomas Graf3093fbe2015-07-21 10:44:00 +02001939 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00001940 struct vxlan_dev *vxlan = netdev_priv(dev);
David Miller79b16aa2015-04-05 22:19:09 -04001941 struct sock *sk = vxlan->vn_sock->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001942 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001943 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001944 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001945 union vxlan_addr *dst;
Thomas Grafee122c72015-07-21 10:43:58 +02001946 union vxlan_addr remote_ip;
1947 struct vxlan_metadata _md;
1948 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001949 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001950 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001951 __be16 df = 0;
1952 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001953 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02001954 u32 flags = vxlan->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001955
Thomas Graf3093fbe2015-07-21 10:44:00 +02001956 /* FIXME: Support IPv6 */
1957 info = skb_tunnel_info(skb, AF_INET);
1958
Thomas Grafee122c72015-07-21 10:43:58 +02001959 if (rdst) {
1960 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1961 vni = rdst->remote_vni;
1962 dst = &rdst->remote_ip;
1963 } else {
1964 if (!info) {
1965 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1966 dev->name);
1967 goto drop;
1968 }
1969
1970 dst_port = info->key.tp_dst ? : vxlan->dst_port;
1971 vni = be64_to_cpu(info->key.tun_id);
1972 remote_ip.sin.sin_family = AF_INET;
1973 remote_ip.sin.sin_addr.s_addr = info->key.ipv4_dst;
1974 dst = &remote_ip;
1975 }
David Stevense4f67ad2012-11-20 02:50:14 +00001976
Cong Wange4c7ed42013-08-31 13:44:33 +08001977 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001978 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001979 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001980 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001981 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001982 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001983 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001984 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001985
stephen hemmingerd3428942012-10-01 12:32:35 +00001986 old_iph = ip_hdr(skb);
1987
stephen hemmingerd3428942012-10-01 12:32:35 +00001988 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001989 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001990 ttl = 1;
1991
1992 tos = vxlan->tos;
1993 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001994 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001995
Tom Herbert535fb8d2014-07-01 21:32:49 -07001996 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1997 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001998
Cong Wange4c7ed42013-08-31 13:44:33 +08001999 if (dst->sa.sa_family == AF_INET) {
Thomas Grafee122c72015-07-21 10:43:58 +02002000 if (info) {
2001 if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
2002 df = htons(IP_DF);
2003 if (info->key.tun_flags & TUNNEL_CSUM)
2004 flags |= VXLAN_F_UDP_CSUM;
2005 else
2006 flags &= ~VXLAN_F_UDP_CSUM;
2007
2008 ttl = info->key.ipv4_ttl;
2009 tos = info->key.ipv4_tos;
2010
2011 if (info->options_len)
2012 md = ip_tunnel_info_opts(info, sizeof(*md));
2013 } else {
2014 md->gbp = skb->mark;
2015 }
2016
Cong Wange4c7ed42013-08-31 13:44:33 +08002017 memset(&fl4, 0, sizeof(fl4));
Thomas Grafee122c72015-07-21 10:43:58 +02002018 fl4.flowi4_oif = rdst ? rdst->remote_ifindex : 0;
Cong Wange4c7ed42013-08-31 13:44:33 +08002019 fl4.flowi4_tos = RT_TOS(tos);
Thomas Graf239fb792015-05-05 15:09:21 +02002020 fl4.flowi4_mark = skb->mark;
2021 fl4.flowi4_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08002022 fl4.daddr = dst->sin.sin_addr.s_addr;
2023 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00002024
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002025 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08002026 if (IS_ERR(rt)) {
2027 netdev_dbg(dev, "no route to %pI4\n",
2028 &dst->sin.sin_addr.s_addr);
2029 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002030 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002031 }
2032
2033 if (rt->dst.dev == dev) {
2034 netdev_dbg(dev, "circular route to %pI4\n",
2035 &dst->sin.sin_addr.s_addr);
2036 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08002037 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002038 }
2039
2040 /* Bypass encapsulation if the destination is local */
2041 if (rt->rt_flags & RTCF_LOCAL &&
2042 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2043 struct vxlan_dev *dst_vxlan;
2044
2045 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002046 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002047 dst->sa.sa_family, dst_port,
2048 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002049 if (!dst_vxlan)
2050 goto tx_error;
2051 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2052 return;
2053 }
2054
2055 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2056 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Thomas Grafee122c72015-07-21 10:43:58 +02002057 md->vni = htonl(vni << 8);
David Miller79b16aa2015-04-05 22:19:09 -04002058 err = vxlan_xmit_skb(rt, sk, skb, fl4.saddr,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002059 dst->sin.sin_addr.s_addr, tos, ttl, df,
Thomas Grafee122c72015-07-21 10:43:58 +02002060 src_port, dst_port, md,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002061 !net_eq(vxlan->net, dev_net(vxlan->dev)),
Thomas Grafee122c72015-07-21 10:43:58 +02002062 flags);
Pravin B Shelar74f47272014-12-23 16:20:36 -08002063 if (err < 0) {
2064 /* skb is already freed. */
2065 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002066 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08002067 }
2068
Cong Wange4c7ed42013-08-31 13:44:33 +08002069 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
2070#if IS_ENABLED(CONFIG_IPV6)
2071 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08002072 struct dst_entry *ndst;
2073 struct flowi6 fl6;
2074 u32 flags;
2075
2076 memset(&fl6, 0, sizeof(fl6));
Thomas Grafee122c72015-07-21 10:43:58 +02002077 fl6.flowi6_oif = rdst ? rdst->remote_ifindex : 0;
Cong Wange4c7ed42013-08-31 13:44:33 +08002078 fl6.daddr = dst->sin6.sin6_addr;
2079 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Thomas Graf239fb792015-05-05 15:09:21 +02002080 fl6.flowi6_mark = skb->mark;
Cong Wang8c1bb792013-09-02 10:06:51 +08002081 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08002082
2083 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2084 netdev_dbg(dev, "no route to %pI6\n",
2085 &dst->sin6.sin6_addr);
2086 dev->stats.tx_carrier_errors++;
2087 goto tx_error;
2088 }
2089
2090 if (ndst->dev == dev) {
2091 netdev_dbg(dev, "circular route to %pI6\n",
2092 &dst->sin6.sin6_addr);
2093 dst_release(ndst);
2094 dev->stats.collisions++;
2095 goto tx_error;
2096 }
2097
2098 /* Bypass encapsulation if the destination is local */
2099 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2100 if (flags & RTF_LOCAL &&
2101 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2102 struct vxlan_dev *dst_vxlan;
2103
2104 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002105 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002106 dst->sa.sa_family, dst_port,
2107 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002108 if (!dst_vxlan)
2109 goto tx_error;
2110 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2111 return;
2112 }
2113
2114 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Thomas Grafee122c72015-07-21 10:43:58 +02002115 md->vni = htonl(vni << 8);
2116 md->gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08002117
David Miller79b16aa2015-04-05 22:19:09 -04002118 err = vxlan6_xmit_skb(ndst, sk, skb, dev, &fl6.saddr, &fl6.daddr,
Thomas Grafee122c72015-07-21 10:43:58 +02002119 0, ttl, src_port, dst_port, md,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002120 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2121 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002122#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002123 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002124
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002125 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002126
2127drop:
2128 dev->stats.tx_dropped++;
2129 goto tx_free;
2130
Pravin B Shelar49560532013-08-19 11:23:17 -07002131rt_tx_error:
2132 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002133tx_error:
2134 dev->stats.tx_errors++;
2135tx_free:
2136 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002137}
2138
David Stevens66817122013-03-15 04:35:51 +00002139/* Transmit local packets over Vxlan
2140 *
2141 * Outer IP header inherits ECN and DF from inner header.
2142 * Outer UDP destination is the VXLAN assigned port.
2143 * source port is based on hash of flow
2144 */
2145static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2146{
2147 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002148 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002149 struct ethhdr *eth;
2150 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002151 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002152 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002153
Thomas Graf3093fbe2015-07-21 10:44:00 +02002154 /* FIXME: Support IPv6 */
2155 info = skb_tunnel_info(skb, AF_INET);
2156
David Stevens66817122013-03-15 04:35:51 +00002157 skb_reset_mac_header(skb);
2158 eth = eth_hdr(skb);
2159
Cong Wangf564f452013-08-31 13:44:36 +08002160 if ((vxlan->flags & VXLAN_F_PROXY)) {
2161 if (ntohs(eth->h_proto) == ETH_P_ARP)
2162 return arp_reduce(dev, skb);
2163#if IS_ENABLED(CONFIG_IPV6)
2164 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002165 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2166 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002167 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2168 struct nd_msg *msg;
2169
2170 msg = (struct nd_msg *)skb_transport_header(skb);
2171 if (msg->icmph.icmp6_code == 0 &&
2172 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2173 return neigh_reduce(dev, skb);
2174 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002175 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002176#endif
2177 }
David Stevens66817122013-03-15 04:35:51 +00002178
Thomas Grafee122c72015-07-21 10:43:58 +02002179 if (vxlan->flags & VXLAN_F_FLOW_BASED &&
2180 info && info->mode == IP_TUNNEL_INFO_TX) {
2181 vxlan_xmit_one(skb, dev, NULL, false);
2182 return NETDEV_TX_OK;
2183 }
2184
David Stevens66817122013-03-15 04:35:51 +00002185 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002186 did_rsc = false;
2187
2188 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002189 (ntohs(eth->h_proto) == ETH_P_IP ||
2190 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002191 did_rsc = route_shortcircuit(dev, skb);
2192 if (did_rsc)
2193 f = vxlan_find_mac(vxlan, eth->h_dest);
2194 }
2195
David Stevens66817122013-03-15 04:35:51 +00002196 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002197 f = vxlan_find_mac(vxlan, all_zeros_mac);
2198 if (f == NULL) {
2199 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2200 !is_multicast_ether_addr(eth->h_dest))
2201 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002202
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002203 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002204 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002205 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002206 }
David Stevens66817122013-03-15 04:35:51 +00002207 }
2208
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002209 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2210 struct sk_buff *skb1;
2211
Eric Dumazet8f646c92014-01-06 09:54:31 -08002212 if (!fdst) {
2213 fdst = rdst;
2214 continue;
2215 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002216 skb1 = skb_clone(skb, GFP_ATOMIC);
2217 if (skb1)
2218 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2219 }
2220
Eric Dumazet8f646c92014-01-06 09:54:31 -08002221 if (fdst)
2222 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2223 else
2224 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002225 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002226}
2227
stephen hemmingerd3428942012-10-01 12:32:35 +00002228/* Walk the forwarding table and purge stale entries */
2229static void vxlan_cleanup(unsigned long arg)
2230{
2231 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2232 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2233 unsigned int h;
2234
2235 if (!netif_running(vxlan->dev))
2236 return;
2237
stephen hemmingerd3428942012-10-01 12:32:35 +00002238 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2239 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002240
2241 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002242 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2243 struct vxlan_fdb *f
2244 = container_of(p, struct vxlan_fdb, hlist);
2245 unsigned long timeout;
2246
stephen hemminger3c172862012-10-26 06:24:34 +00002247 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002248 continue;
2249
2250 timeout = f->used + vxlan->age_interval * HZ;
2251 if (time_before_eq(timeout, jiffies)) {
2252 netdev_dbg(vxlan->dev,
2253 "garbage collect %pM\n",
2254 f->eth_addr);
2255 f->state = NUD_STALE;
2256 vxlan_fdb_destroy(vxlan, f);
2257 } else if (time_before(timeout, next_timer))
2258 next_timer = timeout;
2259 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002260 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002261 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002262
2263 mod_timer(&vxlan->age_timer, next_timer);
2264}
2265
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002266static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2267{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002268 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002269 __u32 vni = vxlan->default_dst.remote_vni;
2270
2271 vxlan->vn_sock = vs;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002272 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002273 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002274 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002275}
2276
stephen hemmingerd3428942012-10-01 12:32:35 +00002277/* Setup stats when device is created */
2278static int vxlan_init(struct net_device *dev)
2279{
WANG Cong1c213bd2014-02-13 11:46:28 -08002280 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002281 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002282 return -ENOMEM;
2283
2284 return 0;
2285}
2286
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002287static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002288{
2289 struct vxlan_fdb *f;
2290
2291 spin_lock_bh(&vxlan->hash_lock);
2292 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2293 if (f)
2294 vxlan_fdb_destroy(vxlan, f);
2295 spin_unlock_bh(&vxlan->hash_lock);
2296}
2297
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002298static void vxlan_uninit(struct net_device *dev)
2299{
2300 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002301
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002302 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002303
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002304 free_percpu(dev->tstats);
2305}
2306
stephen hemmingerd3428942012-10-01 12:32:35 +00002307/* Start ageing timer and join group when device is brought up */
2308static int vxlan_open(struct net_device *dev)
2309{
2310 struct vxlan_dev *vxlan = netdev_priv(dev);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002311 struct vxlan_sock *vs;
2312 int ret = 0;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002313
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002314 vs = vxlan_sock_add(vxlan->net, vxlan->dst_port, vxlan_rcv, NULL,
2315 false, vxlan->flags);
2316 if (IS_ERR(vs))
2317 return PTR_ERR(vs);
2318
2319 vxlan_vs_add_dev(vs, vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002320
Gao feng79d4a942013-12-10 16:37:32 +08002321 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002322 ret = vxlan_igmp_join(vxlan);
2323 if (ret) {
2324 vxlan_sock_release(vs);
2325 return ret;
2326 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002327 }
2328
2329 if (vxlan->age_interval)
2330 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2331
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002332 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002333}
2334
2335/* Purge the forwarding table */
2336static void vxlan_flush(struct vxlan_dev *vxlan)
2337{
Cong Wang31fec5a2013-05-27 22:35:52 +00002338 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002339
2340 spin_lock_bh(&vxlan->hash_lock);
2341 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2342 struct hlist_node *p, *n;
2343 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2344 struct vxlan_fdb *f
2345 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002346 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2347 if (!is_zero_ether_addr(f->eth_addr))
2348 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002349 }
2350 }
2351 spin_unlock_bh(&vxlan->hash_lock);
2352}
2353
2354/* Cleanup timer and forwarding table on shutdown */
2355static int vxlan_stop(struct net_device *dev)
2356{
2357 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002358 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002359 struct vxlan_sock *vs = vxlan->vn_sock;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002360 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002361
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002362 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002363 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002364 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002365
2366 del_timer_sync(&vxlan->age_timer);
2367
2368 vxlan_flush(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002369 vxlan_sock_release(vs);
stephen hemmingerd3428942012-10-01 12:32:35 +00002370
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002371 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002372}
2373
stephen hemmingerd3428942012-10-01 12:32:35 +00002374/* Stub, nothing needs to be done. */
2375static void vxlan_set_multicast_list(struct net_device *dev)
2376{
2377}
2378
Daniel Borkmann345010b2013-12-18 00:21:08 +01002379static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2380{
2381 struct vxlan_dev *vxlan = netdev_priv(dev);
2382 struct vxlan_rdst *dst = &vxlan->default_dst;
2383 struct net_device *lowerdev;
2384 int max_mtu;
2385
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002386 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002387 if (lowerdev == NULL)
2388 return eth_change_mtu(dev, new_mtu);
2389
2390 if (dst->remote_ip.sa.sa_family == AF_INET6)
2391 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2392 else
2393 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2394
2395 if (new_mtu < 68 || new_mtu > max_mtu)
2396 return -EINVAL;
2397
2398 dev->mtu = new_mtu;
2399 return 0;
2400}
2401
stephen hemmingerd3428942012-10-01 12:32:35 +00002402static const struct net_device_ops vxlan_netdev_ops = {
2403 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002404 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002405 .ndo_open = vxlan_open,
2406 .ndo_stop = vxlan_stop,
2407 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002408 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002409 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002410 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002411 .ndo_validate_addr = eth_validate_addr,
2412 .ndo_set_mac_address = eth_mac_addr,
2413 .ndo_fdb_add = vxlan_fdb_add,
2414 .ndo_fdb_del = vxlan_fdb_delete,
2415 .ndo_fdb_dump = vxlan_fdb_dump,
2416};
2417
2418/* Info for udev, that this is a virtual tunnel endpoint */
2419static struct device_type vxlan_type = {
2420 .name = "vxlan",
2421};
2422
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002423/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002424 * supply the listening VXLAN udp ports. Callers are expected
2425 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002426 */
2427void vxlan_get_rx_port(struct net_device *dev)
2428{
2429 struct vxlan_sock *vs;
2430 struct net *net = dev_net(dev);
2431 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2432 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002433 __be16 port;
2434 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002435
2436 spin_lock(&vn->sock_lock);
2437 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002438 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2439 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002440 sa_family = vs->sock->sk->sk_family;
2441 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2442 port);
2443 }
2444 }
2445 spin_unlock(&vn->sock_lock);
2446}
2447EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2448
stephen hemmingerd3428942012-10-01 12:32:35 +00002449/* Initialize the device structure. */
2450static void vxlan_setup(struct net_device *dev)
2451{
2452 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002453 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002454
2455 eth_hw_addr_random(dev);
2456 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002457 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002458 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002459 else
Cong Wang2853af62014-06-12 11:53:10 -07002460 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002461
2462 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002463 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002464 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2465
2466 dev->tx_queue_len = 0;
2467 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002468 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002469 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002470 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002471
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002472 dev->vlan_features = dev->features;
2473 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002474 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002475 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002476 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002477 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002478 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002479
stephen hemminger553675f2013-05-16 11:35:20 +00002480 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002481 spin_lock_init(&vxlan->hash_lock);
2482
2483 init_timer_deferrable(&vxlan->age_timer);
2484 vxlan->age_timer.function = vxlan_cleanup;
2485 vxlan->age_timer.data = (unsigned long) vxlan;
2486
stephen hemminger823aa872013-04-27 11:31:57 +00002487 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002488
stephen hemmingerd3428942012-10-01 12:32:35 +00002489 vxlan->dev = dev;
2490
2491 for (h = 0; h < FDB_HASH_SIZE; ++h)
2492 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2493}
2494
2495static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2496 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002497 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002498 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002499 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2500 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002501 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002502 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2503 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2504 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2505 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2506 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002507 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002508 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2509 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2510 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2511 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Thomas Grafee122c72015-07-21 10:43:58 +02002512 [IFLA_VXLAN_FLOWBASED] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002513 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002514 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2515 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2516 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002517 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2518 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002519 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002520 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002521};
2522
2523static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2524{
2525 if (tb[IFLA_ADDRESS]) {
2526 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2527 pr_debug("invalid link address (not ethernet)\n");
2528 return -EINVAL;
2529 }
2530
2531 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2532 pr_debug("invalid all zero ethernet address\n");
2533 return -EADDRNOTAVAIL;
2534 }
2535 }
2536
2537 if (!data)
2538 return -EINVAL;
2539
2540 if (data[IFLA_VXLAN_ID]) {
2541 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2542 if (id >= VXLAN_VID_MASK)
2543 return -ERANGE;
2544 }
2545
stephen hemminger05f47d62012-10-09 20:35:50 +00002546 if (data[IFLA_VXLAN_PORT_RANGE]) {
2547 const struct ifla_vxlan_port_range *p
2548 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2549
2550 if (ntohs(p->high) < ntohs(p->low)) {
2551 pr_debug("port range %u .. %u not valid\n",
2552 ntohs(p->low), ntohs(p->high));
2553 return -EINVAL;
2554 }
2555 }
2556
stephen hemmingerd3428942012-10-01 12:32:35 +00002557 return 0;
2558}
2559
Yan Burman1b13c972013-01-29 23:43:07 +00002560static void vxlan_get_drvinfo(struct net_device *netdev,
2561 struct ethtool_drvinfo *drvinfo)
2562{
2563 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2564 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2565}
2566
2567static const struct ethtool_ops vxlan_ethtool_ops = {
2568 .get_drvinfo = vxlan_get_drvinfo,
2569 .get_link = ethtool_op_get_link,
2570};
2571
stephen hemminger553675f2013-05-16 11:35:20 +00002572static void vxlan_del_work(struct work_struct *work)
2573{
2574 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002575 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002576 kfree_rcu(vs, rcu);
2577}
2578
Tom Herbert3ee64f32014-07-13 19:49:42 -07002579static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2580 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002581{
Cong Wange4c7ed42013-08-31 13:44:33 +08002582 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002583 struct udp_port_cfg udp_conf;
2584 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002585
Tom Herbert3ee64f32014-07-13 19:49:42 -07002586 memset(&udp_conf, 0, sizeof(udp_conf));
2587
2588 if (ipv6) {
2589 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002590 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002591 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002592 } else {
2593 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002594 }
2595
Tom Herbert3ee64f32014-07-13 19:49:42 -07002596 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002597
Tom Herbert3ee64f32014-07-13 19:49:42 -07002598 /* Open UDP socket */
2599 err = udp_sock_create(net, &udp_conf, &sock);
2600 if (err < 0)
2601 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002602
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002603 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002604}
2605
2606/* Create new listen socket if needed */
2607static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002608 vxlan_rcv_t *rcv, void *data,
2609 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002610{
2611 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2612 struct vxlan_sock *vs;
2613 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002614 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002615 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002616 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002617
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002618 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002619 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002620 return ERR_PTR(-ENOMEM);
2621
2622 for (h = 0; h < VNI_HASH_SIZE; ++h)
2623 INIT_HLIST_HEAD(&vs->vni_list[h]);
2624
2625 INIT_WORK(&vs->del_work, vxlan_del_work);
2626
Tom Herbert3ee64f32014-07-13 19:49:42 -07002627 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002628 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002629 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2630 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002631 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002632 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002633 }
2634
Cong Wange4c7ed42013-08-31 13:44:33 +08002635 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002636 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002637 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002638 vs->data = data;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002639 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002640
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002641 /* Initialize the vxlan udp offloads structure */
2642 vs->udp_offloads.port = port;
2643 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2644 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2645
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002646 spin_lock(&vn->sock_lock);
2647 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002648 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002649 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002650
2651 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002652 tunnel_cfg.sk_user_data = vs;
2653 tunnel_cfg.encap_type = 1;
2654 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2655 tunnel_cfg.encap_destroy = NULL;
2656
2657 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002658
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002659 return vs;
2660}
stephen hemminger553675f2013-05-16 11:35:20 +00002661
Pravin B Shelar012a5722013-08-19 11:23:07 -07002662struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2663 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002664 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002665{
2666 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2667 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002668 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002669
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002670 if (!no_share) {
2671 spin_lock(&vn->sock_lock);
2672 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port,
2673 flags);
2674 if (vs && vs->rcv == rcv) {
2675 if (!atomic_add_unless(&vs->refcnt, 1, 0))
2676 vs = ERR_PTR(-EBUSY);
2677 spin_unlock(&vn->sock_lock);
2678 return vs;
2679 }
2680 spin_unlock(&vn->sock_lock);
2681 }
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002682
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002683 return vxlan_socket_create(net, port, rcv, data, flags);
stephen hemminger553675f2013-05-16 11:35:20 +00002684}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002685EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002686
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002687static int vxlan_newlink(struct net *src_net, struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +00002688 struct nlattr *tb[], struct nlattr *data[])
2689{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002690 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002691 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002692 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002693 __u32 vni;
2694 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002695 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002696
2697 if (!data[IFLA_VXLAN_ID])
2698 return -EINVAL;
2699
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002700 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002701
stephen hemmingerd3428942012-10-01 12:32:35 +00002702 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002703 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002704
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002705 /* Unless IPv6 is explicitly requested, assume IPv4 */
2706 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002707 if (data[IFLA_VXLAN_GROUP]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002708 dst->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002709 } else if (data[IFLA_VXLAN_GROUP6]) {
2710 if (!IS_ENABLED(CONFIG_IPV6))
2711 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002712
Jiri Benc67b61f62015-03-29 16:59:26 +02002713 dst->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002714 dst->remote_ip.sa.sa_family = AF_INET6;
2715 use_ipv6 = true;
2716 }
2717
2718 if (data[IFLA_VXLAN_LOCAL]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002719 vxlan->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002720 vxlan->saddr.sa.sa_family = AF_INET;
2721 } else if (data[IFLA_VXLAN_LOCAL6]) {
2722 if (!IS_ENABLED(CONFIG_IPV6))
2723 return -EPFNOSUPPORT;
2724
2725 /* TODO: respect scope id */
Jiri Benc67b61f62015-03-29 16:59:26 +02002726 vxlan->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002727 vxlan->saddr.sa.sa_family = AF_INET6;
2728 use_ipv6 = true;
2729 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002730
stephen hemminger34e02aa2012-10-09 20:35:53 +00002731 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002732 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002733 struct net_device *lowerdev
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002734 = __dev_get_by_index(src_net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002735
stephen hemminger34e02aa2012-10-09 20:35:53 +00002736 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002737 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002738 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002739 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002740
Cong Wange4c7ed42013-08-31 13:44:33 +08002741#if IS_ENABLED(CONFIG_IPV6)
2742 if (use_ipv6) {
2743 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2744 if (idev && idev->cnf.disable_ipv6) {
2745 pr_info("IPv6 is disabled via sysctl\n");
2746 return -EPERM;
2747 }
2748 vxlan->flags |= VXLAN_F_IPV6;
2749 }
2750#endif
2751
stephen hemminger34e02aa2012-10-09 20:35:53 +00002752 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002753 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002754
Cong Wang2853af62014-06-12 11:53:10 -07002755 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002756 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002757 } else if (use_ipv6)
2758 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002759
2760 if (data[IFLA_VXLAN_TOS])
2761 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2762
Vincent Bernatafb97182012-10-30 10:27:16 +00002763 if (data[IFLA_VXLAN_TTL])
2764 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2765
stephen hemmingerd3428942012-10-01 12:32:35 +00002766 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002767 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002768
2769 if (data[IFLA_VXLAN_AGEING])
2770 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2771 else
2772 vxlan->age_interval = FDB_AGE_DEFAULT;
2773
David Stevense4f67ad2012-11-20 02:50:14 +00002774 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2775 vxlan->flags |= VXLAN_F_PROXY;
2776
2777 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2778 vxlan->flags |= VXLAN_F_RSC;
2779
2780 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2781 vxlan->flags |= VXLAN_F_L2MISS;
2782
2783 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2784 vxlan->flags |= VXLAN_F_L3MISS;
2785
stephen hemmingerd3428942012-10-01 12:32:35 +00002786 if (data[IFLA_VXLAN_LIMIT])
2787 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2788
Thomas Grafee122c72015-07-21 10:43:58 +02002789 if (data[IFLA_VXLAN_FLOWBASED] &&
2790 nla_get_u8(data[IFLA_VXLAN_FLOWBASED]))
2791 vxlan->flags |= VXLAN_F_FLOW_BASED;
2792
stephen hemminger05f47d62012-10-09 20:35:50 +00002793 if (data[IFLA_VXLAN_PORT_RANGE]) {
2794 const struct ifla_vxlan_port_range *p
2795 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2796 vxlan->port_min = ntohs(p->low);
2797 vxlan->port_max = ntohs(p->high);
2798 }
2799
stephen hemminger823aa872013-04-27 11:31:57 +00002800 if (data[IFLA_VXLAN_PORT])
2801 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2802
Tom Herbert359a0ea2014-06-04 17:20:29 -07002803 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2804 vxlan->flags |= VXLAN_F_UDP_CSUM;
2805
2806 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2807 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2808 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2809
2810 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2811 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2812 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2813
Tom Herbertdfd86452015-01-12 17:00:38 -08002814 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2815 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2816 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2817
2818 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2819 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2820 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2821
Thomas Graf35114942015-01-15 03:53:55 +01002822 if (data[IFLA_VXLAN_GBP])
2823 vxlan->flags |= VXLAN_F_GBP;
2824
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002825 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
2826 vxlan->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
2827
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002828 if (vxlan_find_vni(src_net, vni, use_ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002829 vxlan->dst_port, vxlan->flags)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002830 pr_info("duplicate VNI %u\n", vni);
2831 return -EEXIST;
2832 }
2833
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002834 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002835
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002836 /* create an fdb entry for a valid default destination */
2837 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2838 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2839 &vxlan->default_dst.remote_ip,
2840 NUD_REACHABLE|NUD_PERMANENT,
2841 NLM_F_EXCL|NLM_F_CREATE,
2842 vxlan->dst_port,
2843 vxlan->default_dst.remote_vni,
2844 vxlan->default_dst.remote_ifindex,
2845 NTF_SELF);
2846 if (err)
2847 return err;
2848 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002849
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002850 err = register_netdevice(dev);
2851 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002852 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002853 return err;
2854 }
2855
stephen hemminger553675f2013-05-16 11:35:20 +00002856 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002857
2858 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002859}
2860
2861static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2862{
2863 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002864 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002865
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002866 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002867 if (!hlist_unhashed(&vxlan->hlist))
2868 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002869 spin_unlock(&vn->sock_lock);
2870
stephen hemminger553675f2013-05-16 11:35:20 +00002871 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002872 unregister_netdevice_queue(dev, head);
2873}
2874
2875static size_t vxlan_get_size(const struct net_device *dev)
2876{
2877
2878 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002879 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002880 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002881 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002882 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2883 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2884 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002885 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2886 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2887 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2888 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Thomas Grafee122c72015-07-21 10:43:58 +02002889 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_FLOWBASED */
stephen hemmingerd3428942012-10-01 12:32:35 +00002890 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2891 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002892 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002893 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2894 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2895 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2896 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002897 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2898 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002899 0;
2900}
2901
2902static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2903{
2904 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002905 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002906 struct ifla_vxlan_port_range ports = {
2907 .low = htons(vxlan->port_min),
2908 .high = htons(vxlan->port_max),
2909 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002910
Atzm Watanabec7995c42013-04-16 02:50:52 +00002911 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002912 goto nla_put_failure;
2913
Cong Wange4c7ed42013-08-31 13:44:33 +08002914 if (!vxlan_addr_any(&dst->remote_ip)) {
2915 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02002916 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
2917 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002918 goto nla_put_failure;
2919#if IS_ENABLED(CONFIG_IPV6)
2920 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02002921 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
2922 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002923 goto nla_put_failure;
2924#endif
2925 }
2926 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002927
Atzm Watanabec7995c42013-04-16 02:50:52 +00002928 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002929 goto nla_put_failure;
2930
Cong Wange4c7ed42013-08-31 13:44:33 +08002931 if (!vxlan_addr_any(&vxlan->saddr)) {
2932 if (vxlan->saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02002933 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
2934 vxlan->saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002935 goto nla_put_failure;
2936#if IS_ENABLED(CONFIG_IPV6)
2937 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02002938 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
2939 &vxlan->saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002940 goto nla_put_failure;
2941#endif
2942 }
2943 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002944
2945 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2946 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002947 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2948 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2949 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2950 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2951 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2952 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2953 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2954 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2955 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Thomas Grafee122c72015-07-21 10:43:58 +02002956 nla_put_u8(skb, IFLA_VXLAN_FLOWBASED,
2957 !!(vxlan->flags & VXLAN_F_FLOW_BASED)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002958 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002959 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002960 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2961 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2962 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2963 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2964 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2965 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002966 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2967 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2968 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2969 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2970 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002971 goto nla_put_failure;
2972
stephen hemminger05f47d62012-10-09 20:35:50 +00002973 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2974 goto nla_put_failure;
2975
Thomas Graf35114942015-01-15 03:53:55 +01002976 if (vxlan->flags & VXLAN_F_GBP &&
2977 nla_put_flag(skb, IFLA_VXLAN_GBP))
2978 goto nla_put_failure;
2979
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002980 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
2981 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
2982 goto nla_put_failure;
2983
stephen hemmingerd3428942012-10-01 12:32:35 +00002984 return 0;
2985
2986nla_put_failure:
2987 return -EMSGSIZE;
2988}
2989
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002990static struct net *vxlan_get_link_net(const struct net_device *dev)
2991{
2992 struct vxlan_dev *vxlan = netdev_priv(dev);
2993
2994 return vxlan->net;
2995}
2996
stephen hemmingerd3428942012-10-01 12:32:35 +00002997static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2998 .kind = "vxlan",
2999 .maxtype = IFLA_VXLAN_MAX,
3000 .policy = vxlan_policy,
3001 .priv_size = sizeof(struct vxlan_dev),
3002 .setup = vxlan_setup,
3003 .validate = vxlan_validate,
3004 .newlink = vxlan_newlink,
3005 .dellink = vxlan_dellink,
3006 .get_size = vxlan_get_size,
3007 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003008 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003009};
3010
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003011static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3012 struct net_device *dev)
3013{
3014 struct vxlan_dev *vxlan, *next;
3015 LIST_HEAD(list_kill);
3016
3017 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3018 struct vxlan_rdst *dst = &vxlan->default_dst;
3019
3020 /* In case we created vxlan device with carrier
3021 * and we loose the carrier due to module unload
3022 * we also need to remove vxlan device. In other
3023 * cases, it's not necessary and remote_ifindex
3024 * is 0 here, so no matches.
3025 */
3026 if (dst->remote_ifindex == dev->ifindex)
3027 vxlan_dellink(vxlan->dev, &list_kill);
3028 }
3029
3030 unregister_netdevice_many(&list_kill);
3031}
3032
3033static int vxlan_lowerdev_event(struct notifier_block *unused,
3034 unsigned long event, void *ptr)
3035{
3036 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003037 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003038
Daniel Borkmann783c1462014-01-22 21:07:53 +01003039 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003040 vxlan_handle_lowerdev_unregister(vn, dev);
3041
3042 return NOTIFY_DONE;
3043}
3044
3045static struct notifier_block vxlan_notifier_block __read_mostly = {
3046 .notifier_call = vxlan_lowerdev_event,
3047};
3048
stephen hemmingerd3428942012-10-01 12:32:35 +00003049static __net_init int vxlan_init_net(struct net *net)
3050{
3051 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003052 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003053
stephen hemminger553675f2013-05-16 11:35:20 +00003054 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003055 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003056
stephen hemminger553675f2013-05-16 11:35:20 +00003057 for (h = 0; h < PORT_HASH_SIZE; ++h)
3058 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003059
3060 return 0;
3061}
3062
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003063static void __net_exit vxlan_exit_net(struct net *net)
3064{
3065 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3066 struct vxlan_dev *vxlan, *next;
3067 struct net_device *dev, *aux;
3068 LIST_HEAD(list);
3069
3070 rtnl_lock();
3071 for_each_netdev_safe(net, dev, aux)
3072 if (dev->rtnl_link_ops == &vxlan_link_ops)
3073 unregister_netdevice_queue(dev, &list);
3074
3075 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3076 /* If vxlan->dev is in the same netns, it has already been added
3077 * to the list by the previous loop.
3078 */
3079 if (!net_eq(dev_net(vxlan->dev), net))
John W. Linville13c3ed62015-05-18 13:51:24 -04003080 unregister_netdevice_queue(vxlan->dev, &list);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003081 }
3082
3083 unregister_netdevice_many(&list);
3084 rtnl_unlock();
3085}
3086
stephen hemmingerd3428942012-10-01 12:32:35 +00003087static struct pernet_operations vxlan_net_ops = {
3088 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003089 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003090 .id = &vxlan_net_id,
3091 .size = sizeof(struct vxlan_net),
3092};
3093
3094static int __init vxlan_init_module(void)
3095{
3096 int rc;
3097
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003098 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3099 if (!vxlan_wq)
3100 return -ENOMEM;
3101
stephen hemmingerd3428942012-10-01 12:32:35 +00003102 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3103
Daniel Borkmann783c1462014-01-22 21:07:53 +01003104 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003105 if (rc)
3106 goto out1;
3107
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003108 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003109 if (rc)
3110 goto out2;
3111
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003112 rc = rtnl_link_register(&vxlan_link_ops);
3113 if (rc)
3114 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003115
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003116 return 0;
3117out3:
3118 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003119out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003120 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003121out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003122 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00003123 return rc;
3124}
Cong Wang7332a132013-05-27 22:35:53 +00003125late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003126
3127static void __exit vxlan_cleanup_module(void)
3128{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003129 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003130 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003131 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003132 unregister_pernet_subsys(&vxlan_net_ops);
3133 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003134}
3135module_exit(vxlan_cleanup_module);
3136
3137MODULE_LICENSE("GPL");
3138MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003139MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003140MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003141MODULE_ALIAS_RTNL_LINK("vxlan");