blob: 9171c1f42fe9418130cc89d845d324ab635326e0 [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>
stephen hemmingerd3428942012-10-01 12:32:35 +000014#include <linux/module.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000017#include <linux/udp.h>
18#include <linux/igmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000019#include <linux/if_ether.h>
Yan Burman1b13c972013-01-29 23:43:07 +000020#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000021#include <net/arp.h>
22#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000023#include <net/ip.h>
24#include <net/icmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000025#include <net/rtnetlink.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000026#include <net/inet_ecn.h>
27#include <net/net_namespace.h>
28#include <net/netns/generic.h>
Jiri Bencfa20e0e2017-08-28 21:43:22 +020029#include <net/tun_proto.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070030#include <net/vxlan.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080031
Cong Wange4c7ed42013-08-31 13:44:33 +080032#if IS_ENABLED(CONFIG_IPV6)
Cong Wange4c7ed42013-08-31 13:44:33 +080033#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080034#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080035#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000036
37#define VXLAN_VERSION "0.1"
38
stephen hemminger553675f2013-05-16 11:35:20 +000039#define PORT_HASH_BITS 8
40#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000041#define FDB_AGE_DEFAULT 300 /* 5 min */
42#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
43
stephen hemminger23c578b2013-04-27 11:31:53 +000044/* UDP port for VXLAN traffic.
45 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070046 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000047 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070048static unsigned short vxlan_port __read_mostly = 8472;
49module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000050MODULE_PARM_DESC(udp_port, "Destination UDP port");
51
52static bool log_ecn_error = true;
53module_param(log_ecn_error, bool, 0644);
54MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
55
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030056static unsigned int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020057static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000058
Li RongQing7256eac2016-01-29 09:43:47 +080059static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030060
Jiri Benc205f3562015-09-24 13:50:01 +020061static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020062
Mark Blocha53cb292017-06-02 03:24:08 +030063static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
64
stephen hemminger553675f2013-05-16 11:35:20 +000065/* per-network namespace private data for this module */
66struct vxlan_net {
67 struct list_head vxlan_list;
68 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070069 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000070};
71
stephen hemmingerd3428942012-10-01 12:32:35 +000072/* Forwarding table entry */
73struct vxlan_fdb {
74 struct hlist_node hlist; /* linked list of entries */
75 struct rcu_head rcu;
76 unsigned long updated; /* jiffies */
77 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070078 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020079 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000080 u16 state; /* see ndm_state */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -080081 __be32 vni;
Petr Machata45598c12018-11-21 08:02:36 +000082 u16 flags; /* see ndm_flags and below */
stephen hemmingerd3428942012-10-01 12:32:35 +000083};
84
Petr Machata45598c12018-11-21 08:02:36 +000085#define NTF_VXLAN_ADDED_BY_USER 0x100
86
stephen hemmingerd3428942012-10-01 12:32:35 +000087/* salt for hash table */
88static u32 vxlan_salt __read_mostly;
89
Thomas Grafee122c72015-07-21 10:43:58 +020090static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
91{
Thomas Grafe7030872015-07-21 10:44:01 +020092 return vs->flags & VXLAN_F_COLLECT_METADATA ||
93 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020094}
95
Cong Wange4c7ed42013-08-31 13:44:33 +080096#if IS_ENABLED(CONFIG_IPV6)
97static inline
98bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
99{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200100 if (a->sa.sa_family != b->sa.sa_family)
101 return false;
102 if (a->sa.sa_family == AF_INET6)
103 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
104 else
105 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800106}
107
Cong Wange4c7ed42013-08-31 13:44:33 +0800108static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
109{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200110 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200111 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200112 ip->sa.sa_family = AF_INET6;
113 return 0;
114 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200115 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200116 ip->sa.sa_family = AF_INET;
117 return 0;
118 } else {
119 return -EAFNOSUPPORT;
120 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800121}
122
123static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200124 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800125{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200126 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200127 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200128 else
Jiri Benc930345e2015-03-29 16:59:25 +0200129 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800130}
131
132#else /* !CONFIG_IPV6 */
133
134static inline
135bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
136{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200137 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800138}
139
Cong Wange4c7ed42013-08-31 13:44:33 +0800140static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
141{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200142 if (nla_len(nla) >= sizeof(struct in6_addr)) {
143 return -EAFNOSUPPORT;
144 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200145 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200146 ip->sa.sa_family = AF_INET;
147 return 0;
148 } else {
149 return -EAFNOSUPPORT;
150 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800151}
152
153static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200154 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800155{
Jiri Benc930345e2015-03-29 16:59:25 +0200156 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800157}
158#endif
159
stephen hemminger553675f2013-05-16 11:35:20 +0000160/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100161static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000162{
Jiri Benc54bfd872016-02-16 21:58:58 +0100163 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000164}
165
166/* Socket hash table head */
167static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000168{
169 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
170
stephen hemminger553675f2013-05-16 11:35:20 +0000171 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
172}
173
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700174/* First remote destination for a forwarding entry.
175 * Guaranteed to be non-NULL because remotes are never deleted.
176 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700177static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700178{
stephen hemminger5ca54612013-08-04 17:17:39 -0700179 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
180}
181
182static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
183{
184 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700185}
186
Thomas Grafac5132d2015-01-15 03:53:56 +0100187/* Find VXLAN socket based on network namespace, address family and UDP port
188 * and enabled unshareable flags.
189 */
190static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
191 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000192{
193 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800194
195 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000196
197 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200198 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200199 vxlan_get_sk_family(vs) == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800200 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000201 return vs;
202 }
203 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000204}
205
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200206static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
207 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000208{
Jiri Benc69e76662017-07-02 19:00:57 +0200209 struct vxlan_dev_node *node;
stephen hemmingerd3428942012-10-01 12:32:35 +0000210
Jiri Bencb9167b22016-02-16 21:59:03 +0100211 /* For flow based devices, map all packets to VNI 0 */
212 if (vs->flags & VXLAN_F_COLLECT_METADATA)
213 vni = 0;
214
Jiri Benc69e76662017-07-02 19:00:57 +0200215 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
216 if (node->vxlan->default_dst.remote_vni != vni)
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200217 continue;
218
219 if (IS_ENABLED(CONFIG_IPV6)) {
Jiri Benc69e76662017-07-02 19:00:57 +0200220 const struct vxlan_config *cfg = &node->vxlan->cfg;
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200221
222 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
223 cfg->remote_ifindex != ifindex)
224 continue;
225 }
226
Jiri Benc69e76662017-07-02 19:00:57 +0200227 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000228 }
229
230 return NULL;
231}
232
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700233/* Look up VNI in a per net namespace table */
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200234static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
235 __be32 vni, sa_family_t family,
236 __be16 port, u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700237{
238 struct vxlan_sock *vs;
239
Thomas Grafac5132d2015-01-15 03:53:56 +0100240 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700241 if (!vs)
242 return NULL;
243
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200244 return vxlan_vs_find_vni(vs, ifindex, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700245}
246
stephen hemmingerd3428942012-10-01 12:32:35 +0000247/* Fill in neighbour message in skbuff. */
248static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700249 const struct vxlan_fdb *fdb,
250 u32 portid, u32 seq, int type, unsigned int flags,
251 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000252{
253 unsigned long now = jiffies;
254 struct nda_cacheinfo ci;
255 struct nlmsghdr *nlh;
256 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000257 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000258
259 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
260 if (nlh == NULL)
261 return -EMSGSIZE;
262
263 ndm = nlmsg_data(nlh);
264 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000265
266 send_eth = send_ip = true;
267
268 if (type == RTM_GETNEIGH) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800269 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000270 send_eth = !is_zero_ether_addr(fdb->eth_addr);
Vincent Bernat8f48ba72017-03-10 16:30:24 +0100271 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
David Stevense4f67ad2012-11-20 02:50:14 +0000272 } else
273 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000274 ndm->ndm_state = fdb->state;
275 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000276 ndm->ndm_flags = fdb->flags;
Petr Machata0efe1172018-10-17 08:53:26 +0000277 if (rdst->offloaded)
278 ndm->ndm_flags |= NTF_OFFLOADED;
Jun Zhao545469f2014-07-26 00:38:59 +0800279 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000280
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100281 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100282 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700283 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100284 goto nla_put_failure;
285
David Stevense4f67ad2012-11-20 02:50:14 +0000286 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000287 goto nla_put_failure;
288
Cong Wange4c7ed42013-08-31 13:44:33 +0800289 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000290 goto nla_put_failure;
291
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200292 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000293 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
294 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000295 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100296 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000297 goto nla_put_failure;
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200298 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800299 nla_put_u32(skb, NDA_SRC_VNI,
300 be32_to_cpu(fdb->vni)))
301 goto nla_put_failure;
David Stevens66817122013-03-15 04:35:51 +0000302 if (rdst->remote_ifindex &&
303 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000304 goto nla_put_failure;
305
306 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
307 ci.ndm_confirmed = 0;
308 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
309 ci.ndm_refcnt = 0;
310
311 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
312 goto nla_put_failure;
313
Johannes Berg053c0952015-01-16 22:09:00 +0100314 nlmsg_end(skb, nlh);
315 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000316
317nla_put_failure:
318 nlmsg_cancel(skb, nlh);
319 return -EMSGSIZE;
320}
321
322static inline size_t vxlan_nlmsg_size(void)
323{
324 return NLMSG_ALIGN(sizeof(struct ndmsg))
325 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800326 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000327 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000328 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
329 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100330 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000331 + nla_total_size(sizeof(struct nda_cacheinfo));
332}
333
Petr Machata9a997352018-10-17 08:53:22 +0000334static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
335 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000336{
337 struct net *net = dev_net(vxlan->dev);
338 struct sk_buff *skb;
339 int err = -ENOBUFS;
340
341 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
342 if (skb == NULL)
343 goto errout;
344
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200345 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000346 if (err < 0) {
347 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
348 WARN_ON(err == -EMSGSIZE);
349 kfree_skb(skb);
350 goto errout;
351 }
352
353 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
354 return;
355errout:
356 if (err < 0)
357 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
358}
359
Petr Machata9a997352018-10-17 08:53:22 +0000360static void vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
361 struct vxlan_fdb *fdb,
362 struct vxlan_rdst *rd,
363 bool adding)
364{
365 struct switchdev_notifier_vxlan_fdb_info info;
366 enum switchdev_notifier_type notifier_type;
367
368 if (WARN_ON(!rd))
369 return;
370
371 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
372 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
373
374 info = (struct switchdev_notifier_vxlan_fdb_info){
375 .remote_ip = rd->remote_ip,
376 .remote_port = rd->remote_port,
377 .remote_vni = rd->remote_vni,
378 .remote_ifindex = rd->remote_ifindex,
379 .vni = fdb->vni,
Petr Machata0efe1172018-10-17 08:53:26 +0000380 .offloaded = rd->offloaded,
Petr Machata45598c12018-11-21 08:02:36 +0000381 .added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER,
Petr Machata9a997352018-10-17 08:53:22 +0000382 };
383 memcpy(info.eth_addr, fdb->eth_addr, ETH_ALEN);
384
385 call_switchdev_notifiers(notifier_type, vxlan->dev,
386 &info.info);
387}
388
389static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
Petr Machata0e6160f2018-11-21 08:02:35 +0000390 struct vxlan_rdst *rd, int type, bool swdev_notify)
Petr Machata9a997352018-10-17 08:53:22 +0000391{
Petr Machata0e6160f2018-11-21 08:02:35 +0000392 if (swdev_notify) {
393 switch (type) {
394 case RTM_NEWNEIGH:
395 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
396 true);
397 break;
398 case RTM_DELNEIGH:
399 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
400 false);
401 break;
402 }
Petr Machata9a997352018-10-17 08:53:22 +0000403 }
404
405 __vxlan_fdb_notify(vxlan, fdb, rd, type);
406}
407
Cong Wange4c7ed42013-08-31 13:44:33 +0800408static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000409{
410 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700411 struct vxlan_fdb f = {
412 .state = NUD_STALE,
413 };
414 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800415 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100416 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700417 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700418
Petr Machata0e6160f2018-11-21 08:02:35 +0000419 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true);
David Stevense4f67ad2012-11-20 02:50:14 +0000420}
421
422static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
423{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700424 struct vxlan_fdb f = {
425 .state = NUD_STALE,
426 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200427 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000428
David Stevense4f67ad2012-11-20 02:50:14 +0000429 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
430
Petr Machata0e6160f2018-11-21 08:02:35 +0000431 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true);
David Stevense4f67ad2012-11-20 02:50:14 +0000432}
433
stephen hemmingerd3428942012-10-01 12:32:35 +0000434/* Hash Ethernet address */
435static u32 eth_hash(const unsigned char *addr)
436{
437 u64 value = get_unaligned((u64 *)addr);
438
439 /* only want 6 bytes */
440#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000441 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000442#else
443 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000444#endif
445 return hash_64(value, FDB_HASH_BITS);
446}
447
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800448static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
449{
450 /* use 1 byte of OUI and 3 bytes of NIC */
451 u32 key = get_unaligned((u32 *)(addr + 2));
452
453 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
454}
455
stephen hemmingerd3428942012-10-01 12:32:35 +0000456/* Hash chain to use given mac address */
457static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800458 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000459{
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200460 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800461 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
462 else
463 return &vxlan->fdb_head[eth_hash(mac)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000464}
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,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800468 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000469{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800470 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000471 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) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800474 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200475 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800476 if (vni == f->vni)
477 return f;
478 } else {
479 return f;
480 }
481 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000482 }
483
484 return NULL;
485}
486
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000487static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800488 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000489{
490 struct vxlan_fdb *f;
491
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800492 f = __vxlan_find_mac(vxlan, mac, vni);
Li RongQing016f3d12018-08-29 11:52:10 +0800493 if (f && f->used != jiffies)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000494 f->used = jiffies;
495
496 return f;
497}
498
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300499/* caller should hold vxlan->hash_lock */
500static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800501 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100502 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300503{
504 struct vxlan_rdst *rd;
505
506 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800507 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300508 rd->remote_port == port &&
509 rd->remote_vni == vni &&
510 rd->remote_ifindex == ifindex)
511 return rd;
512 }
513
514 return NULL;
515}
516
Petr Machata1941f1d2018-10-17 08:53:24 +0000517int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
518 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
519{
520 struct vxlan_dev *vxlan = netdev_priv(dev);
521 u8 eth_addr[ETH_ALEN + 2] = { 0 };
522 struct vxlan_rdst *rdst;
523 struct vxlan_fdb *f;
524 int rc = 0;
525
526 if (is_multicast_ether_addr(mac) ||
527 is_zero_ether_addr(mac))
528 return -EINVAL;
529
530 ether_addr_copy(eth_addr, mac);
531
532 rcu_read_lock();
533
534 f = __vxlan_find_mac(vxlan, eth_addr, vni);
535 if (!f) {
536 rc = -ENOENT;
537 goto out;
538 }
539
540 rdst = first_remote_rcu(f);
541
542 memset(fdb_info, 0, sizeof(*fdb_info));
543 fdb_info->info.dev = dev;
544 fdb_info->remote_ip = rdst->remote_ip;
545 fdb_info->remote_port = rdst->remote_port;
546 fdb_info->remote_vni = rdst->remote_vni;
547 fdb_info->remote_ifindex = rdst->remote_ifindex;
548 fdb_info->vni = vni;
Petr Machata0efe1172018-10-17 08:53:26 +0000549 fdb_info->offloaded = rdst->offloaded;
Petr Machata45598c12018-11-21 08:02:36 +0000550 fdb_info->added_by_user = f->flags & NTF_VXLAN_ADDED_BY_USER;
Petr Machata1941f1d2018-10-17 08:53:24 +0000551 ether_addr_copy(fdb_info->eth_addr, mac);
552
553out:
554 rcu_read_unlock();
555 return rc;
556}
557EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
558
Thomas Richter906dc182013-07-19 17:20:07 +0200559/* Replace destination of unicast mac */
560static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100561 union vxlan_addr *ip, __be16 port, __be32 vni,
562 __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200563{
564 struct vxlan_rdst *rd;
565
566 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
567 if (rd)
568 return 0;
569
570 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
571 if (!rd)
572 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100573
574 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800575 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200576 rd->remote_port = port;
577 rd->remote_vni = vni;
578 rd->remote_ifindex = ifindex;
579 return 1;
580}
581
David Stevens66817122013-03-15 04:35:51 +0000582/* Add/update destinations for multicast */
583static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100584 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200585 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000586{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700587 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000588
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300589 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
590 if (rd)
591 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700592
David Stevens66817122013-03-15 04:35:51 +0000593 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
594 if (rd == NULL)
595 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100596
597 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
598 kfree(rd);
599 return -ENOBUFS;
600 }
601
Cong Wange4c7ed42013-08-31 13:44:33 +0800602 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000603 rd->remote_port = port;
Petr Machata0efe1172018-10-17 08:53:26 +0000604 rd->offloaded = false;
David Stevens66817122013-03-15 04:35:51 +0000605 rd->remote_vni = vni;
606 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700607
608 list_add_tail_rcu(&rd->list, &f->remotes);
609
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200610 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000611 return 1;
612}
613
Tom Herbertdfd86452015-01-12 17:00:38 -0800614static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
615 unsigned int off,
616 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100617 __be32 vni_field,
618 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800619 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800620{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700621 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800622
623 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700624 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800625
626 if (!NAPI_GRO_CB(skb)->csum_valid)
627 return NULL;
628
Jiri Benc54bfd872016-02-16 21:58:58 +0100629 start = vxlan_rco_start(vni_field);
630 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800631
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700632 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
633 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800634
635 skb->remcsum_offload = 1;
636
637 return vh;
638}
639
David Millerd4546c22018-06-24 14:13:49 +0900640static struct sk_buff *vxlan_gro_receive(struct sock *sk,
641 struct list_head *head,
642 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200643{
David Millerd4546c22018-06-24 14:13:49 +0900644 struct sk_buff *pp = NULL;
645 struct sk_buff *p;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200646 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800647 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200648 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700649 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100650 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800651 struct gro_remcsum grc;
652
653 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200654
655 off_vx = skb_gro_offset(skb);
656 hlen = off_vx + sizeof(*vh);
657 vh = skb_gro_header_fast(skb, off_vx);
658 if (skb_gro_header_hard(skb, hlen)) {
659 vh = skb_gro_header_slow(skb, hlen, off_vx);
660 if (unlikely(!vh))
661 goto out;
662 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200663
Tom Herbertdfd86452015-01-12 17:00:38 -0800664 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
665
Jiri Benc54bfd872016-02-16 21:58:58 +0100666 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800667
668 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
669 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100670 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800671 !!(vs->flags &
672 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800673
674 if (!vh)
675 goto out;
676 }
677
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700678 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
679
David Millerd4546c22018-06-24 14:13:49 +0900680 list_for_each_entry(p, head, list) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200681 if (!NAPI_GRO_CB(p)->same_flow)
682 continue;
683
684 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100685 if (vh->vx_flags != vh2->vx_flags ||
686 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200687 NAPI_GRO_CB(p)->same_flow = 0;
688 continue;
689 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200690 }
691
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200692 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800693 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200694
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200695out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200696 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200697
698 return pp;
699}
700
Tom Herbert5602c482016-04-05 08:22:53 -0700701static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200702{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700703 /* Sets 'skb->inner_mac_header' since we are always called with
704 * 'skb->encapsulation' set.
705 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800706 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200707}
708
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700709static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan,
710 const u8 *mac, __u16 state,
Petr Machata45598c12018-11-21 08:02:36 +0000711 __be32 src_vni, __u16 ndm_flags)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700712{
713 struct vxlan_fdb *f;
714
715 f = kmalloc(sizeof(*f), GFP_ATOMIC);
716 if (!f)
717 return NULL;
718 f->state = state;
719 f->flags = ndm_flags;
720 f->updated = f->used = jiffies;
721 f->vni = src_vni;
722 INIT_LIST_HEAD(&f->remotes);
723 memcpy(f->eth_addr, mac, ETH_ALEN);
724
725 return f;
726}
727
stephen hemmingerd3428942012-10-01 12:32:35 +0000728static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800729 const u8 *mac, union vxlan_addr *ip,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700730 __u16 state, __be16 port, __be32 src_vni,
Petr Machata45598c12018-11-21 08:02:36 +0000731 __be32 vni, __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700732 struct vxlan_fdb **fdb)
733{
734 struct vxlan_rdst *rd = NULL;
735 struct vxlan_fdb *f;
736 int rc;
737
738 if (vxlan->cfg.addrmax &&
739 vxlan->addrcnt >= vxlan->cfg.addrmax)
740 return -ENOSPC;
741
742 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
743 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
744 if (!f)
745 return -ENOMEM;
746
747 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
748 if (rc < 0) {
749 kfree(f);
750 return rc;
751 }
752
753 ++vxlan->addrcnt;
754 hlist_add_head_rcu(&f->hlist,
755 vxlan_fdb_head(vxlan, mac, src_vni));
756
757 *fdb = f;
758
759 return 0;
760}
761
762/* Add new entry to forwarding table -- assumes lock held */
763static int vxlan_fdb_update(struct vxlan_dev *vxlan,
764 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000765 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800766 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata45598c12018-11-21 08:02:36 +0000767 __u32 ifindex, __u16 ndm_flags,
Petr Machata0e6160f2018-11-21 08:02:35 +0000768 bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +0000769{
Petr Machata45598c12018-11-21 08:02:36 +0000770 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200771 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000772 struct vxlan_fdb *f;
773 int notify = 0;
Haishuang Yan17b46362016-11-29 09:59:36 +0800774 int rc;
stephen hemmingerd3428942012-10-01 12:32:35 +0000775
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800776 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000777 if (f) {
778 if (flags & NLM_F_EXCL) {
779 netdev_dbg(vxlan->dev,
780 "lost race to create %pM\n", mac);
781 return -EEXIST;
782 }
783 if (f->state != state) {
784 f->state = state;
785 f->updated = jiffies;
786 notify = 1;
787 }
Roopa Prabhu0813e952018-10-11 12:35:13 -0700788 if (f->flags != fdb_flags) {
789 f->flags = fdb_flags;
David Stevensae884082013-04-19 00:36:26 +0000790 f->updated = jiffies;
791 notify = 1;
792 }
Thomas Richter906dc182013-07-19 17:20:07 +0200793 if ((flags & NLM_F_REPLACE)) {
794 /* Only change unicasts */
795 if (!(is_multicast_ether_addr(f->eth_addr) ||
796 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800797 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200798 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200799 } else
800 return -EOPNOTSUPP;
801 }
David Stevens66817122013-03-15 04:35:51 +0000802 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300803 (is_multicast_ether_addr(f->eth_addr) ||
804 is_zero_ether_addr(f->eth_addr))) {
Haishuang Yan17b46362016-11-29 09:59:36 +0800805 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
David Stevens66817122013-03-15 04:35:51 +0000806
807 if (rc < 0)
808 return rc;
809 notify |= rc;
810 }
Roopa Prabhu0813e952018-10-11 12:35:13 -0700811
812 if (ndm_flags & NTF_USE)
813 f->used = jiffies;
stephen hemmingerd3428942012-10-01 12:32:35 +0000814 } else {
815 if (!(flags & NLM_F_CREATE))
816 return -ENOENT;
817
Thomas Richter906dc182013-07-19 17:20:07 +0200818 /* Disallow replace to add a multicast entry */
819 if ((flags & NLM_F_REPLACE) &&
820 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
821 return -EOPNOTSUPP;
822
Cong Wange4c7ed42013-08-31 13:44:33 +0800823 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700824 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
Roopa Prabhu0813e952018-10-11 12:35:13 -0700825 vni, ifindex, fdb_flags, &f);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700826 if (rc < 0)
Haishuang Yan17b46362016-11-29 09:59:36 +0800827 return rc;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700828 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000829 }
830
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200831 if (notify) {
832 if (rd == NULL)
833 rd = first_remote_rtnl(f);
Petr Machata0e6160f2018-11-21 08:02:35 +0000834 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH, swdev_notify);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200835 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000836
837 return 0;
838}
839
Wei Yongjun6706c822013-04-11 19:00:35 +0000840static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000841{
842 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700843 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000844
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100845 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
846 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000847 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100848 }
David Stevens66817122013-03-15 04:35:51 +0000849 kfree(f);
850}
851
Roopa Prabhu4c2438b2018-07-04 16:46:31 -0700852static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +0000853 bool do_notify, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +0000854{
Petr Machata045a5a92018-10-17 08:53:27 +0000855 struct vxlan_rdst *rd;
856
stephen hemmingerd3428942012-10-01 12:32:35 +0000857 netdev_dbg(vxlan->dev,
858 "delete %pM\n", f->eth_addr);
859
860 --vxlan->addrcnt;
Roopa Prabhu4c2438b2018-07-04 16:46:31 -0700861 if (do_notify)
Petr Machata045a5a92018-10-17 08:53:27 +0000862 list_for_each_entry(rd, &f->remotes, list)
Petr Machata0e6160f2018-11-21 08:02:35 +0000863 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
864 swdev_notify);
stephen hemmingerd3428942012-10-01 12:32:35 +0000865
866 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000867 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000868}
869
Lance Richardson35cf2842017-05-29 13:25:57 -0400870static void vxlan_dst_free(struct rcu_head *head)
871{
872 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
873
874 dst_cache_destroy(&rd->dst_cache);
875 kfree(rd);
876}
877
878static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +0000879 struct vxlan_rdst *rd, bool swdev_notify)
Lance Richardson35cf2842017-05-29 13:25:57 -0400880{
881 list_del_rcu(&rd->list);
Petr Machata0e6160f2018-11-21 08:02:35 +0000882 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify);
Lance Richardson35cf2842017-05-29 13:25:57 -0400883 call_rcu(&rd->rcu, vxlan_dst_free);
884}
885
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300886static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800887 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
888 __be32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300889{
890 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800891 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300892
893 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800894 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
895 if (err)
896 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300897 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800898 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
899 if (remote->sa.sa_family == AF_INET) {
900 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
901 ip->sa.sa_family = AF_INET;
902#if IS_ENABLED(CONFIG_IPV6)
903 } else {
904 ip->sin6.sin6_addr = in6addr_any;
905 ip->sa.sa_family = AF_INET6;
906#endif
907 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300908 }
909
910 if (tb[NDA_PORT]) {
911 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
912 return -EINVAL;
913 *port = nla_get_be16(tb[NDA_PORT]);
914 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200915 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300916 }
917
918 if (tb[NDA_VNI]) {
919 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
920 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100921 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300922 } else {
923 *vni = vxlan->default_dst.remote_vni;
924 }
925
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800926 if (tb[NDA_SRC_VNI]) {
927 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
928 return -EINVAL;
929 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
930 } else {
931 *src_vni = vxlan->default_dst.remote_vni;
932 }
933
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300934 if (tb[NDA_IFINDEX]) {
935 struct net_device *tdev;
936
937 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
938 return -EINVAL;
939 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800940 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300941 if (!tdev)
942 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300943 } else {
944 *ifindex = 0;
945 }
946
947 return 0;
948}
949
stephen hemmingerd3428942012-10-01 12:32:35 +0000950/* Add static entry (via netlink) */
951static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
952 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100953 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000954{
955 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300956 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800957 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000958 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800959 __be32 src_vni, vni;
Jiri Benc54bfd872016-02-16 21:58:58 +0100960 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000961 int err;
962
963 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
964 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
965 ndm->ndm_state);
966 return -EINVAL;
967 }
968
969 if (tb[NDA_DST] == NULL)
970 return -EINVAL;
971
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800972 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300973 if (err)
974 return err;
David Stevens66817122013-03-15 04:35:51 +0000975
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300976 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
977 return -EAFNOSUPPORT;
978
stephen hemmingerd3428942012-10-01 12:32:35 +0000979 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700980 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
Petr Machata45598c12018-11-21 08:02:36 +0000981 port, src_vni, vni, ifindex,
982 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
Petr Machata0e6160f2018-11-21 08:02:35 +0000983 true);
stephen hemmingerd3428942012-10-01 12:32:35 +0000984 spin_unlock_bh(&vxlan->hash_lock);
985
986 return err;
987}
988
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800989static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
990 const unsigned char *addr, union vxlan_addr ip,
Xin Longfc39c382017-11-26 21:19:05 +0800991 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata0e6160f2018-11-21 08:02:35 +0000992 u32 ifindex, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +0000993{
stephen hemmingerd3428942012-10-01 12:32:35 +0000994 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300995 struct vxlan_rdst *rd = NULL;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800996 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300997
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800998 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300999 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001000 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001001
Cong Wange4c7ed42013-08-31 13:44:33 +08001002 if (!vxlan_addr_any(&ip)) {
1003 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001004 if (!rd)
1005 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +00001006 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001007
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001008 /* remove a destination if it's not the only one on the list,
1009 * otherwise destroy the fdb entry
1010 */
1011 if (rd && !list_is_singular(&f->remotes)) {
Petr Machata0e6160f2018-11-21 08:02:35 +00001012 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001013 goto out;
1014 }
1015
Petr Machata0e6160f2018-11-21 08:02:35 +00001016 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001017
1018out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001019 return 0;
1020}
1021
1022/* Delete entry (via netlink) */
1023static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1024 struct net_device *dev,
1025 const unsigned char *addr, u16 vid)
1026{
1027 struct vxlan_dev *vxlan = netdev_priv(dev);
1028 union vxlan_addr ip;
1029 __be32 src_vni, vni;
1030 __be16 port;
1031 u32 ifindex;
1032 int err;
1033
1034 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
1035 if (err)
1036 return err;
1037
1038 spin_lock_bh(&vxlan->hash_lock);
Petr Machata0e6160f2018-11-21 08:02:35 +00001039 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1040 true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001041 spin_unlock_bh(&vxlan->hash_lock);
1042
1043 return err;
1044}
1045
1046/* Dump forwarding table */
1047static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04001048 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07001049 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +00001050{
1051 struct vxlan_dev *vxlan = netdev_priv(dev);
1052 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -07001053 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001054
1055 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1056 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001057
Sasha Levinb67bfe02013-02-27 17:06:00 -08001058 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +00001059 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +00001060
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001061 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07001062 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001063 goto skip;
1064
David Stevens66817122013-03-15 04:35:51 +00001065 err = vxlan_fdb_info(skb, vxlan, f,
1066 NETLINK_CB(cb->skb).portid,
1067 cb->nlh->nlmsg_seq,
1068 RTM_NEWNEIGH,
1069 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -07001070 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001071 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001072skip:
Roopa Prabhud2976532016-08-30 21:56:45 -07001073 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001074 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001075 }
1076 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001077out:
Roopa Prabhud2976532016-08-30 21:56:45 -07001078 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001079}
1080
1081/* Watch incoming packets to learn mapping between Ethernet address
1082 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +09001083 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +00001084 */
stephen hemminger26a41ae62013-06-17 12:09:58 -07001085static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001086 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001087 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +00001088{
1089 struct vxlan_dev *vxlan = netdev_priv(dev);
1090 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001091 u32 ifindex = 0;
1092
1093#if IS_ENABLED(CONFIG_IPV6)
1094 if (src_ip->sa.sa_family == AF_INET6 &&
1095 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1096 ifindex = src_ifindex;
1097#endif
stephen hemmingerd3428942012-10-01 12:32:35 +00001098
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001099 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001100 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001101 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001102
Matthias Schiffer87613de2017-06-19 10:03:59 +02001103 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1104 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001105 return false;
1106
1107 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -07001108 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001109 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001110
1111 if (net_ratelimit())
1112 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001113 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001114 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001115
Cong Wange4c7ed42013-08-31 13:44:33 +08001116 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001117 f->updated = jiffies;
Petr Machata0e6160f2018-11-21 08:02:35 +00001118 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001119 } else {
1120 /* learned new entry */
1121 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001122
1123 /* close off race between vxlan_flush and incoming packets */
1124 if (netif_running(dev))
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001125 vxlan_fdb_update(vxlan, src_mac, src_ip,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001126 NUD_REACHABLE,
1127 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001128 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001129 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001130 vxlan->default_dst.remote_vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001131 ifindex, NTF_SELF, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001132 spin_unlock(&vxlan->hash_lock);
1133 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001134
1135 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001136}
1137
stephen hemmingerd3428942012-10-01 12:32:35 +00001138/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001139static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001140{
stephen hemminger553675f2013-05-16 11:35:20 +00001141 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001142 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001143#if IS_ENABLED(CONFIG_IPV6)
1144 struct vxlan_sock *sock6;
1145#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001146 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001147
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001148 sock4 = rtnl_dereference(dev->vn4_sock);
1149
Gao feng95ab0992013-12-10 16:37:33 +08001150 /* The vxlan_sock is only used by dev, leaving group has
1151 * no effect on other vxlan devices.
1152 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001153 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001154 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001155#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001156 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001157 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001158 return false;
1159#endif
Gao feng95ab0992013-12-10 16:37:33 +08001160
stephen hemminger553675f2013-05-16 11:35:20 +00001161 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001162 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001163 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001164
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001165 if (family == AF_INET &&
1166 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001167 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001168#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001169 if (family == AF_INET6 &&
1170 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001171 continue;
1172#endif
Gao feng95ab0992013-12-10 16:37:33 +08001173
1174 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1175 &dev->default_dst.remote_ip))
1176 continue;
1177
1178 if (vxlan->default_dst.remote_ifindex !=
1179 dev->default_dst.remote_ifindex)
1180 continue;
1181
1182 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001183 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001184
1185 return false;
1186}
1187
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001188static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001189{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001190 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001191
Jiri Bencb1be00a2015-09-24 13:50:02 +02001192 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001193 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001194 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001195 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001196
Jiri Bencb1be00a2015-09-24 13:50:02 +02001197 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001198 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001199 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001200 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001201 (vs->flags & VXLAN_F_GPE) ?
1202 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001203 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001204 spin_unlock(&vn->sock_lock);
1205
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001206 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001207}
1208
Jiri Bencb1be00a2015-09-24 13:50:02 +02001209static void vxlan_sock_release(struct vxlan_dev *vxlan)
1210{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001211 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001212#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001213 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1214
Mark Bloch57d88182017-06-07 14:36:58 +03001215 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001216#endif
1217
Mark Bloch57d88182017-06-07 14:36:58 +03001218 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001219 synchronize_net();
1220
Mark Blocha53cb292017-06-02 03:24:08 +03001221 vxlan_vs_del_dev(vxlan);
1222
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001223 if (__vxlan_sock_release_prep(sock4)) {
1224 udp_tunnel_sock_release(sock4->sock);
1225 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001226 }
1227
1228#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001229 if (__vxlan_sock_release_prep(sock6)) {
1230 udp_tunnel_sock_release(sock6->sock);
1231 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001232 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001233#endif
1234}
1235
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001236/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001237 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001238 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001239static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001240{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001241 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001242 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1243 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001244 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001245
Cong Wange4c7ed42013-08-31 13:44:33 +08001246 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001247 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001248 struct ip_mreqn mreq = {
1249 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1250 .imr_ifindex = ifindex,
1251 };
1252
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001253 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001254 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001255 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001256 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001257#if IS_ENABLED(CONFIG_IPV6)
1258 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001259 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1260
1261 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001262 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001263 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1264 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001265 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001266#endif
1267 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001268
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001269 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001270}
1271
1272/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001273static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001274{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001275 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001276 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1277 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001278 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001279
Cong Wange4c7ed42013-08-31 13:44:33 +08001280 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001281 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001282 struct ip_mreqn mreq = {
1283 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1284 .imr_ifindex = ifindex,
1285 };
1286
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001287 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001288 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001289 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001290 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001291#if IS_ENABLED(CONFIG_IPV6)
1292 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001293 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1294
1295 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001296 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001297 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1298 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001299 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001300#endif
1301 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001302
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001303 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001304}
1305
Jiri Bencf14eceb2016-02-16 21:59:01 +01001306static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1307 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001308{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001309 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001310
Jiri Bencf14eceb2016-02-16 21:59:01 +01001311 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1312 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001313
Jiri Bencf14eceb2016-02-16 21:59:01 +01001314 start = vxlan_rco_start(unparsed->vx_vni);
1315 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001316
Jiri Benc7d34fa72016-03-21 17:50:05 +01001317 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001318 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001319
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001320 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1321 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001322out:
1323 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1324 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001325 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001326}
1327
Jiri Bencf14eceb2016-02-16 21:59:01 +01001328static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001329 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001330 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001331{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001332 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001333 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001334
Jiri Bencf14eceb2016-02-16 21:59:01 +01001335 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1336 goto out;
1337
Jiri Benc3288af02016-02-16 21:59:00 +01001338 md->gbp = ntohs(gbp->policy_id);
1339
Jiri Benc10a5af22016-02-23 18:02:59 +01001340 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001341 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001342 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001343 tun_dst->u.tun_info.options_len = sizeof(*md);
1344 }
Jiri Benc3288af02016-02-16 21:59:00 +01001345 if (gbp->dont_learn)
1346 md->gbp |= VXLAN_GBP_DONT_LEARN;
1347
1348 if (gbp->policy_applied)
1349 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001350
Jiri Benc64f87d32016-02-23 18:02:55 +01001351 /* In flow-based mode, GBP is carried in dst_metadata */
1352 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1353 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001354out:
1355 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001356}
1357
Jiri Bence1e53142016-04-05 14:47:13 +02001358static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001359 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001360 struct sk_buff *skb, u32 vxflags)
1361{
1362 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1363
1364 /* Need to have Next Protocol set for interfaces in GPE mode. */
1365 if (!gpe->np_applied)
1366 return false;
1367 /* "The initial version is 0. If a receiver does not support the
1368 * version indicated it MUST drop the packet.
1369 */
1370 if (gpe->version != 0)
1371 return false;
1372 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1373 * processing MUST occur." However, we don't implement OAM
1374 * processing, thus drop the packet.
1375 */
1376 if (gpe->oam_flag)
1377 return false;
1378
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001379 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1380 if (!*protocol)
Jiri Bence1e53142016-04-05 14:47:13 +02001381 return false;
Jiri Bence1e53142016-04-05 14:47:13 +02001382
1383 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1384 return true;
1385}
1386
Jiri Benc1ab016e2016-02-23 18:02:56 +01001387static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1388 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001389 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001390{
Thomas Graf614732e2015-07-21 10:44:06 +02001391 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001392 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001393
Thomas Graf614732e2015-07-21 10:44:06 +02001394 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001395 skb->protocol = eth_type_trans(skb, vxlan->dev);
1396 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1397
1398 /* Ignore packet loops (and multicast echo) */
1399 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001400 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001401
Jiri Benc760c6802016-02-23 18:02:57 +01001402 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001403 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001404 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001405 saddr.sa.sa_family = AF_INET;
1406#if IS_ENABLED(CONFIG_IPV6)
1407 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001408 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001409 saddr.sa.sa_family = AF_INET6;
1410#endif
1411 }
1412
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001413 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001414 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001415 return false;
1416
1417 return true;
1418}
1419
Jiri Benc760c6802016-02-23 18:02:57 +01001420static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1421 struct sk_buff *skb)
1422{
1423 int err = 0;
1424
1425 if (vxlan_get_sk_family(vs) == AF_INET)
1426 err = IP_ECN_decapsulate(oiph, skb);
1427#if IS_ENABLED(CONFIG_IPV6)
1428 else
1429 err = IP6_ECN_decapsulate(oiph, skb);
1430#endif
1431
1432 if (unlikely(err) && log_ecn_error) {
1433 if (vxlan_get_sk_family(vs) == AF_INET)
1434 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1435 &((struct iphdr *)oiph)->saddr,
1436 ((struct iphdr *)oiph)->tos);
1437 else
1438 net_info_ratelimited("non-ECT from %pI6\n",
1439 &((struct ipv6hdr *)oiph)->saddr);
1440 }
1441 return err <= 1;
1442}
1443
stephen hemmingerd3428942012-10-01 12:32:35 +00001444/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001445static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001446{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001447 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001448 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001449 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001450 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001451 struct vxlan_metadata _md;
1452 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001453 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001454 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001455 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001456 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001457
Jiri Bence1e53142016-04-05 14:47:13 +02001458 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001459 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001460 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001461
Jiri Bencf14eceb2016-02-16 21:59:01 +01001462 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001463 /* VNI flag always required to be set */
1464 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1465 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1466 ntohl(vxlan_hdr(skb)->vx_flags),
1467 ntohl(vxlan_hdr(skb)->vx_vni));
1468 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001469 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001470 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001471 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1472 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001473
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001474 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001475 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001476 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001477
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001478 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1479
Matthias Schiffer49f810f2017-06-19 10:04:00 +02001480 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001481 if (!vxlan)
1482 goto drop;
1483
Jiri Bence1e53142016-04-05 14:47:13 +02001484 /* For backwards compatibility, only allow reserved fields to be
1485 * used by VXLAN extensions if explicitly requested.
1486 */
1487 if (vs->flags & VXLAN_F_GPE) {
1488 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1489 goto drop;
1490 raw_proto = true;
1491 }
1492
1493 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1494 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1495 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001496
Thomas Grafee122c72015-07-21 10:43:58 +02001497 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001498 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001499
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001500 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001501 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001502
Thomas Grafee122c72015-07-21 10:43:58 +02001503 if (!tun_dst)
1504 goto drop;
1505
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001506 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001507
1508 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001509 } else {
1510 memset(md, 0, sizeof(*md));
1511 }
1512
Jiri Bencf14eceb2016-02-16 21:59:01 +01001513 if (vs->flags & VXLAN_F_REMCSUM_RX)
1514 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1515 goto drop;
1516 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001517 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001518 /* Note that GBP and GPE can never be active together. This is
1519 * ensured in vxlan_dev_configure.
1520 */
Thomas Graf35114942015-01-15 03:53:55 +01001521
Jiri Bencf14eceb2016-02-16 21:59:01 +01001522 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001523 /* If there are any unprocessed flags remaining treat
1524 * this as a malformed packet. This behavior diverges from
1525 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1526 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001527 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001528 * is more robust and provides a little more security in
1529 * adding extensions to VXLAN.
1530 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001531 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001532 }
1533
Jiri Bence1e53142016-04-05 14:47:13 +02001534 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001535 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001536 goto drop;
1537 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001538 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001539 skb->dev = vxlan->dev;
1540 skb->pkt_type = PACKET_HOST;
1541 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001542
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001543 oiph = skb_network_header(skb);
1544 skb_reset_network_header(skb);
1545
1546 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1547 ++vxlan->dev->stats.rx_frame_errors;
1548 ++vxlan->dev->stats.rx_errors;
1549 goto drop;
1550 }
1551
1552 stats = this_cpu_ptr(vxlan->dev->tstats);
1553 u64_stats_update_begin(&stats->syncp);
1554 stats->rx_packets++;
1555 stats->rx_bytes += skb->len;
1556 u64_stats_update_end(&stats->syncp);
1557
1558 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001559 return 0;
1560
1561drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001562 /* Consume bad packet */
1563 kfree_skb(skb);
1564 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001565}
1566
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001567/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1568static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1569{
1570 struct vxlan_dev *vxlan;
1571 struct vxlan_sock *vs;
1572 struct vxlanhdr *hdr;
1573 __be32 vni;
1574
1575 if (skb->len < VXLAN_HLEN)
1576 return -EINVAL;
1577
1578 hdr = vxlan_hdr(skb);
1579
1580 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1581 return -EINVAL;
1582
1583 vs = rcu_dereference_sk_user_data(sk);
1584 if (!vs)
1585 return -ENOENT;
1586
1587 vni = vxlan_vni(hdr->vx_vni);
1588 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1589 if (!vxlan)
1590 return -ENOENT;
1591
1592 return 0;
1593}
1594
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001595static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001596{
1597 struct vxlan_dev *vxlan = netdev_priv(dev);
1598 struct arphdr *parp;
1599 u8 *arpptr, *sha;
1600 __be32 sip, tip;
1601 struct neighbour *n;
1602
1603 if (dev->flags & IFF_NOARP)
1604 goto out;
1605
1606 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1607 dev->stats.tx_dropped++;
1608 goto out;
1609 }
1610 parp = arp_hdr(skb);
1611
1612 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1613 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1614 parp->ar_pro != htons(ETH_P_IP) ||
1615 parp->ar_op != htons(ARPOP_REQUEST) ||
1616 parp->ar_hln != dev->addr_len ||
1617 parp->ar_pln != 4)
1618 goto out;
1619 arpptr = (u8 *)parp + sizeof(struct arphdr);
1620 sha = arpptr;
1621 arpptr += dev->addr_len; /* sha */
1622 memcpy(&sip, arpptr, sizeof(sip));
1623 arpptr += sizeof(sip);
1624 arpptr += dev->addr_len; /* tha */
1625 memcpy(&tip, arpptr, sizeof(tip));
1626
1627 if (ipv4_is_loopback(tip) ||
1628 ipv4_is_multicast(tip))
1629 goto out;
1630
1631 n = neigh_lookup(&arp_tbl, &tip, dev);
1632
1633 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001634 struct vxlan_fdb *f;
1635 struct sk_buff *reply;
1636
1637 if (!(n->nud_state & NUD_CONNECTED)) {
1638 neigh_release(n);
1639 goto out;
1640 }
1641
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001642 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08001643 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001644 /* bridge-local neighbor */
1645 neigh_release(n);
1646 goto out;
1647 }
1648
1649 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1650 n->ha, sha);
1651
1652 neigh_release(n);
1653
David Stevens73461352014-03-18 12:32:29 -04001654 if (reply == NULL)
1655 goto out;
1656
David Stevense4f67ad2012-11-20 02:50:14 +00001657 skb_reset_mac_header(reply);
1658 __skb_pull(reply, skb_network_offset(reply));
1659 reply->ip_summed = CHECKSUM_UNNECESSARY;
1660 reply->pkt_type = PACKET_HOST;
1661
1662 if (netif_rx_ni(reply) == NET_RX_DROP)
1663 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001664 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001665 union vxlan_addr ipa = {
1666 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001667 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001668 };
1669
1670 vxlan_ip_miss(dev, &ipa);
1671 }
David Stevense4f67ad2012-11-20 02:50:14 +00001672out:
1673 consume_skb(skb);
1674 return NETDEV_TX_OK;
1675}
1676
Cong Wangf564f452013-08-31 13:44:36 +08001677#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001678static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1679 struct neighbour *n, bool isrouter)
1680{
1681 struct net_device *dev = request->dev;
1682 struct sk_buff *reply;
1683 struct nd_msg *ns, *na;
1684 struct ipv6hdr *pip6;
1685 u8 *daddr;
1686 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1687 int ns_olen;
1688 int i, len;
1689
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001690 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04001691 return NULL;
1692
1693 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1694 sizeof(*na) + na_olen + dev->needed_tailroom;
1695 reply = alloc_skb(len, GFP_ATOMIC);
1696 if (reply == NULL)
1697 return NULL;
1698
1699 reply->protocol = htons(ETH_P_IPV6);
1700 reply->dev = dev;
1701 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1702 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001703 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001704
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001705 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04001706
1707 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001708 ns_olen = request->len - skb_network_offset(request) -
1709 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04001710 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1711 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1712 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1713 break;
1714 }
1715 }
1716
1717 /* Ethernet header */
1718 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1719 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1720 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1721 reply->protocol = htons(ETH_P_IPV6);
1722
1723 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001724 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001725 skb_put(reply, sizeof(struct ipv6hdr));
1726
1727 /* IPv6 header */
1728
1729 pip6 = ipv6_hdr(reply);
1730 memset(pip6, 0, sizeof(struct ipv6hdr));
1731 pip6->version = 6;
1732 pip6->priority = ipv6_hdr(request)->priority;
1733 pip6->nexthdr = IPPROTO_ICMPV6;
1734 pip6->hop_limit = 255;
1735 pip6->daddr = ipv6_hdr(request)->saddr;
1736 pip6->saddr = *(struct in6_addr *)n->primary_key;
1737
1738 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001739 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001740
David Stevens4b29dba2014-03-24 10:39:58 -04001741 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02001742 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04001743 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1744 na->icmph.icmp6_router = isrouter;
1745 na->icmph.icmp6_override = 1;
1746 na->icmph.icmp6_solicited = 1;
1747 na->target = ns->target;
1748 ether_addr_copy(&na->opt[2], n->ha);
1749 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1750 na->opt[1] = na_olen >> 3;
1751
1752 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1753 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1754 csum_partial(na, sizeof(*na)+na_olen, 0));
1755
1756 pip6->payload_len = htons(sizeof(*na)+na_olen);
1757
1758 skb_push(reply, sizeof(struct ipv6hdr));
1759
1760 reply->ip_summed = CHECKSUM_UNNECESSARY;
1761
1762 return reply;
1763}
1764
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001765static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08001766{
1767 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08001768 const struct in6_addr *daddr;
Xin Long8bff36852017-11-11 19:58:50 +08001769 const struct ipv6hdr *iphdr;
David Stevens4b29dba2014-03-24 10:39:58 -04001770 struct inet6_dev *in6_dev;
Xin Long8bff36852017-11-11 19:58:50 +08001771 struct neighbour *n;
1772 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001773
1774 in6_dev = __in6_dev_get(dev);
1775 if (!in6_dev)
1776 goto out;
1777
Cong Wangf564f452013-08-31 13:44:36 +08001778 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001779 daddr = &iphdr->daddr;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001780 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08001781
David Stevens4b29dba2014-03-24 10:39:58 -04001782 if (ipv6_addr_loopback(daddr) ||
1783 ipv6_addr_is_multicast(&msg->target))
1784 goto out;
1785
1786 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001787
1788 if (n) {
1789 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001790 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001791
1792 if (!(n->nud_state & NUD_CONNECTED)) {
1793 neigh_release(n);
1794 goto out;
1795 }
1796
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001797 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08001798 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1799 /* bridge-local neighbor */
1800 neigh_release(n);
1801 goto out;
1802 }
1803
David Stevens4b29dba2014-03-24 10:39:58 -04001804 reply = vxlan_na_create(skb, n,
1805 !!(f ? f->flags & NTF_ROUTER : 0));
1806
Cong Wangf564f452013-08-31 13:44:36 +08001807 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001808
1809 if (reply == NULL)
1810 goto out;
1811
1812 if (netif_rx_ni(reply) == NET_RX_DROP)
1813 dev->stats.rx_dropped++;
1814
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001815 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001816 union vxlan_addr ipa = {
1817 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001818 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001819 };
1820
Cong Wangf564f452013-08-31 13:44:36 +08001821 vxlan_ip_miss(dev, &ipa);
1822 }
1823
1824out:
1825 consume_skb(skb);
1826 return NETDEV_TX_OK;
1827}
1828#endif
1829
David Stevense4f67ad2012-11-20 02:50:14 +00001830static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1831{
1832 struct vxlan_dev *vxlan = netdev_priv(dev);
1833 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001834
1835 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1836 return false;
1837
1838 n = NULL;
1839 switch (ntohs(eth_hdr(skb)->h_proto)) {
1840 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001841 {
1842 struct iphdr *pip;
1843
David Stevense4f67ad2012-11-20 02:50:14 +00001844 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1845 return false;
1846 pip = ip_hdr(skb);
1847 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001848 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001849 union vxlan_addr ipa = {
1850 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001851 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001852 };
1853
1854 vxlan_ip_miss(dev, &ipa);
1855 return false;
1856 }
1857
David Stevense4f67ad2012-11-20 02:50:14 +00001858 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001859 }
1860#if IS_ENABLED(CONFIG_IPV6)
1861 case ETH_P_IPV6:
1862 {
1863 struct ipv6hdr *pip6;
1864
1865 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1866 return false;
1867 pip6 = ipv6_hdr(skb);
1868 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001869 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08001870 union vxlan_addr ipa = {
1871 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001872 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001873 };
1874
1875 vxlan_ip_miss(dev, &ipa);
1876 return false;
1877 }
1878
1879 break;
1880 }
1881#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001882 default:
1883 return false;
1884 }
1885
1886 if (n) {
1887 bool diff;
1888
Joe Perches7367d0b2013-09-01 11:51:23 -07001889 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001890 if (diff) {
1891 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1892 dev->addr_len);
1893 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1894 }
1895 neigh_release(n);
1896 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001897 }
1898
David Stevense4f67ad2012-11-20 02:50:14 +00001899 return false;
1900}
1901
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001902static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001903 struct vxlan_metadata *md)
1904{
1905 struct vxlanhdr_gbp *gbp;
1906
Thomas Grafdb79a622015-02-04 17:00:04 +01001907 if (!md->gbp)
1908 return;
1909
Thomas Graf35114942015-01-15 03:53:55 +01001910 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001911 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001912
1913 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1914 gbp->dont_learn = 1;
1915
1916 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1917 gbp->policy_applied = 1;
1918
1919 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1920}
1921
Jiri Bence1e53142016-04-05 14:47:13 +02001922static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1923 __be16 protocol)
1924{
1925 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1926
1927 gpe->np_applied = 1;
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001928 gpe->next_protocol = tun_p_from_eth_p(protocol);
1929 if (!gpe->next_protocol)
1930 return -EPFNOSUPPORT;
1931 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02001932}
1933
Jiri Bencf491e562016-02-02 18:09:16 +01001934static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1935 int iphdr_len, __be32 vni,
1936 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001937 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001938{
Cong Wange4c7ed42013-08-31 13:44:33 +08001939 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001940 int min_headroom;
1941 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001942 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001943 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001944
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001945 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001946 skb->ip_summed == CHECKSUM_PARTIAL) {
1947 int csum_start = skb_checksum_start_offset(skb);
1948
1949 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1950 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1951 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001952 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001953 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001954 }
1955
Cong Wange4c7ed42013-08-31 13:44:33 +08001956 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08001957 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001958
1959 /* Need space for new headers (invalidates iph ptr) */
1960 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001961 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08001962 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001963
Alexander Duyckaed069d2016-04-14 15:33:37 -04001964 err = iptunnel_handle_offloads(skb, type);
1965 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08001966 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07001967
Johannes Bergd58ff352017-06-16 14:29:23 +02001968 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001969 vxh->vx_flags = VXLAN_HF_VNI;
1970 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001971
Tom Herbertdfd86452015-01-12 17:00:38 -08001972 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001973 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001974
Jiri Benc54bfd872016-02-16 21:58:58 +01001975 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1976 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1977 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001978
1979 if (!skb_is_gso(skb)) {
1980 skb->ip_summed = CHECKSUM_NONE;
1981 skb->encapsulation = 0;
1982 }
1983 }
1984
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001985 if (vxflags & VXLAN_F_GBP)
1986 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001987 if (vxflags & VXLAN_F_GPE) {
1988 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1989 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08001990 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02001991 inner_protocol = skb->protocol;
1992 }
Thomas Graf35114942015-01-15 03:53:55 +01001993
Jiri Bence1e53142016-04-05 14:47:13 +02001994 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001995 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07001996}
Pravin B Shelar49560532013-08-19 11:23:17 -07001997
pravin shelar655c3de2016-11-13 20:43:55 -08001998static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
1999 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002000 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002001 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002002 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002003 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01002004{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002005 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002006 struct rtable *rt = NULL;
2007 struct flowi4 fl4;
2008
pravin shelar655c3de2016-11-13 20:43:55 -08002009 if (!sock4)
2010 return ERR_PTR(-EIO);
2011
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002012 if (tos && !info)
2013 use_cache = false;
2014 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002015 rt = dst_cache_get_ip4(dst_cache, saddr);
2016 if (rt)
2017 return rt;
2018 }
2019
Jiri Benc1a8496b2016-02-02 18:09:14 +01002020 memset(&fl4, 0, sizeof(fl4));
2021 fl4.flowi4_oif = oif;
2022 fl4.flowi4_tos = RT_TOS(tos);
2023 fl4.flowi4_mark = skb->mark;
2024 fl4.flowi4_proto = IPPROTO_UDP;
2025 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002026 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002027 fl4.fl4_dport = dport;
2028 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002029
2030 rt = ip_route_output_key(vxlan->net, &fl4);
pravin shelar655c3de2016-11-13 20:43:55 -08002031 if (likely(!IS_ERR(rt))) {
2032 if (rt->dst.dev == dev) {
2033 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2034 ip_rt_put(rt);
2035 return ERR_PTR(-ELOOP);
2036 }
2037
Jiri Benc1a8496b2016-02-02 18:09:14 +01002038 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002039 if (use_cache)
2040 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08002041 } else {
2042 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2043 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002044 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002045 return rt;
2046}
2047
Jiri Bence5d4b292015-12-07 13:04:30 +01002048#if IS_ENABLED(CONFIG_IPV6)
2049static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08002050 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08002051 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01002052 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002053 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01002054 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002055 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002056 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002057 struct dst_cache *dst_cache,
2058 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01002059{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002060 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002061 struct dst_entry *ndst;
2062 struct flowi6 fl6;
2063 int err;
2064
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002065 if (!sock6)
2066 return ERR_PTR(-EIO);
2067
Daniel Borkmann14006152016-03-04 15:15:08 +01002068 if (tos && !info)
2069 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002070 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002071 ndst = dst_cache_get_ip6(dst_cache, saddr);
2072 if (ndst)
2073 return ndst;
2074 }
2075
Jiri Bence5d4b292015-12-07 13:04:30 +01002076 memset(&fl6, 0, sizeof(fl6));
2077 fl6.flowi6_oif = oif;
2078 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002079 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01002080 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01002081 fl6.flowi6_mark = skb->mark;
2082 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002083 fl6.fl6_dport = dport;
2084 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01002085
2086 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002087 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01002088 &ndst, &fl6);
pravin shelar655c3de2016-11-13 20:43:55 -08002089 if (unlikely(err < 0)) {
2090 netdev_dbg(dev, "no route to %pI6\n", daddr);
2091 return ERR_PTR(-ENETUNREACH);
2092 }
2093
2094 if (unlikely(ndst->dev == dev)) {
2095 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2096 dst_release(ndst);
2097 return ERR_PTR(-ELOOP);
2098 }
Jiri Bence5d4b292015-12-07 13:04:30 +01002099
2100 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002101 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002102 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01002103 return ndst;
2104}
2105#endif
2106
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002107/* Bypass encapsulation if the destination is local */
2108static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002109 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002110{
Li RongQing8f849852014-01-04 13:57:59 +08002111 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08002112 union vxlan_addr loopback;
2113 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08002114 struct net_device *dev = skb->dev;
2115 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002116
Li RongQing8f849852014-01-04 13:57:59 +08002117 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2118 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002119 skb->pkt_type = PACKET_HOST;
2120 skb->encapsulation = 0;
2121 skb->dev = dst_vxlan->dev;
2122 __skb_pull(skb, skb_network_offset(skb));
2123
Cong Wange4c7ed42013-08-31 13:44:33 +08002124 if (remote_ip->sa.sa_family == AF_INET) {
2125 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2126 loopback.sa.sa_family = AF_INET;
2127#if IS_ENABLED(CONFIG_IPV6)
2128 } else {
2129 loopback.sin6.sin6_addr = in6addr_loopback;
2130 loopback.sa.sa_family = AF_INET6;
2131#endif
2132 }
2133
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002134 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
Matthias Schiffer87613de2017-06-19 10:03:59 +02002135 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0,
2136 vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002137
2138 u64_stats_update_begin(&tx_stats->syncp);
2139 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002140 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002141 u64_stats_update_end(&tx_stats->syncp);
2142
2143 if (netif_rx(skb) == NET_RX_SUCCESS) {
2144 u64_stats_update_begin(&rx_stats->syncp);
2145 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002146 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002147 u64_stats_update_end(&rx_stats->syncp);
2148 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08002149 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002150 }
2151}
2152
pravin shelarfee1fad2016-11-13 20:43:56 -08002153static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002154 struct vxlan_dev *vxlan,
2155 union vxlan_addr *daddr,
2156 __be16 dst_port, int dst_ifindex, __be32 vni,
2157 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002158 u32 rt_flags)
2159{
2160#if IS_ENABLED(CONFIG_IPV6)
2161 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2162 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2163 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2164 */
2165 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2166#endif
2167 /* Bypass encapsulation if the destination is local */
2168 if (rt_flags & RTCF_LOCAL &&
2169 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2170 struct vxlan_dev *dst_vxlan;
2171
2172 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002173 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002174 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002175 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002176 if (!dst_vxlan) {
2177 dev->stats.tx_errors++;
2178 kfree_skb(skb);
2179
2180 return -ENOENT;
2181 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002182 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002183 return 1;
2184 }
2185
2186 return 0;
2187}
2188
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002189static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002190 __be32 default_vni, struct vxlan_rdst *rdst,
2191 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002192{
Paolo Abenid71785f2016-02-12 15:43:57 +01002193 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002194 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002195 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002196 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002197 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002198 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002199 struct vxlan_metadata _md;
2200 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002201 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002202 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002203 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002204 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002205 int ifindex;
Pravin B Shelar0e6fbc5b2013-06-17 17:49:56 -07002206 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002207 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002208 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002209 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002210
Jiri Benc61adedf2015-08-20 13:56:25 +02002211 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002212
Thomas Grafee122c72015-07-21 10:43:58 +02002213 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002214 dst = &rdst->remote_ip;
2215 if (vxlan_addr_any(dst)) {
2216 if (did_rsc) {
2217 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002218 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002219 return;
2220 }
2221 goto drop;
2222 }
2223
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002224 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002225 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002226 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002227 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002228 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002229 md->gbp = skb->mark;
Hangbin Liu72f6d712018-04-17 14:11:28 +08002230 if (flags & VXLAN_F_TTL_INHERIT) {
2231 ttl = ip_tunnel_get_ttl(old_iph, skb);
2232 } else {
2233 ttl = vxlan->cfg.ttl;
2234 if (!ttl && vxlan_addr_multicast(dst))
2235 ttl = 1;
2236 }
pravin shelar0770b532016-11-13 20:43:57 -08002237
2238 tos = vxlan->cfg.tos;
2239 if (tos == 1)
2240 tos = ip_tunnel_get_dsfield(old_iph, skb);
2241
2242 if (dst->sa.sa_family == AF_INET)
2243 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2244 else
2245 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2246 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002247 } else {
2248 if (!info) {
2249 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2250 dev->name);
2251 goto drop;
2252 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002253 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002254 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002255 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002256 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2257 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002258 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002259 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2260 }
Thomas Grafee122c72015-07-21 10:43:58 +02002261 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002262 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2263 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002264 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002265 dst_cache = &info->dst_cache;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002266 if (info->options_len &&
2267 info->key.tun_flags & TUNNEL_VXLAN_OPT)
pravin shelar0770b532016-11-13 20:43:57 -08002268 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002269 ttl = info->key.ttl;
2270 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002271 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002272 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002273 }
pravin shelar0770b532016-11-13 20:43:57 -08002274 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2275 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002276
Jakub Kicinski56de8592017-02-24 11:43:36 -08002277 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002278 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002279 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002280 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002281 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002282
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002283 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002284 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002285 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002286 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002287 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002288 if (IS_ERR(rt)) {
2289 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002290 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002291 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002292
pravin shelarfee1fad2016-11-13 20:43:56 -08002293 if (!info) {
Stefano Briviob4d306972018-11-08 12:19:16 +01002294 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002295 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002296 dst_port, ifindex, vni,
2297 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002298 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002299 goto out_unlock;
Stefano Briviob4d306972018-11-08 12:19:16 +01002300
2301 if (vxlan->cfg.df == VXLAN_DF_SET) {
2302 df = htons(IP_DF);
2303 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2304 struct ethhdr *eth = eth_hdr(skb);
2305
2306 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2307 (ntohs(eth->h_proto) == ETH_P_IP &&
2308 old_iph->frag_off & htons(IP_DF)))
2309 df = htons(IP_DF);
2310 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002311 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002312 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002313 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002314
pravin shelarc46b7892016-11-13 20:43:54 -08002315 ndst = &rt->dst;
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002316 skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002317
Cong Wange4c7ed42013-08-31 13:44:33 +08002318 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2319 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002320 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002321 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002322 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002323 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002324
Brian Russell11586322017-02-24 17:47:11 +00002325 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002326 dst->sin.sin_addr.s_addr, tos, ttl, df,
2327 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002328#if IS_ENABLED(CONFIG_IPV6)
2329 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002330 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002331
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002332 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002333 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002334 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002335 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002336 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002337 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002338 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002339 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002340 goto tx_error;
2341 }
pravin shelar655c3de2016-11-13 20:43:55 -08002342
pravin shelarfee1fad2016-11-13 20:43:56 -08002343 if (!info) {
2344 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002345
pravin shelarfee1fad2016-11-13 20:43:56 -08002346 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002347 dst_port, ifindex, vni,
2348 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002349 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002350 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002351 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002352
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002353 skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002354
Daniel Borkmann14006152016-03-04 15:15:08 +01002355 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002356 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002357 skb_scrub_packet(skb, xnet);
2358 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002359 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002360 if (err < 0)
2361 goto tx_error;
2362
pravin shelar0770b532016-11-13 20:43:57 -08002363 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002364 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002365 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002366 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002367#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002368 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002369out_unlock:
2370 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002371 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002372
2373drop:
2374 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002375 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002376 return;
2377
2378tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002379 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002380 if (err == -ELOOP)
2381 dev->stats.collisions++;
2382 else if (err == -ENETUNREACH)
2383 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002384 dst_release(ndst);
2385 dev->stats.tx_errors++;
2386 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002387}
2388
David Stevens66817122013-03-15 04:35:51 +00002389/* Transmit local packets over Vxlan
2390 *
2391 * Outer IP header inherits ECN and DF from inner header.
2392 * Outer UDP destination is the VXLAN assigned port.
2393 * source port is based on hash of flow
2394 */
2395static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2396{
2397 struct vxlan_dev *vxlan = netdev_priv(dev);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002398 struct vxlan_rdst *rdst, *fdst = NULL;
Xin Long8bff36852017-11-11 19:58:50 +08002399 const struct ip_tunnel_info *info;
2400 bool did_rsc = false;
David Stevens66817122013-03-15 04:35:51 +00002401 struct vxlan_fdb *f;
Xin Long8bff36852017-11-11 19:58:50 +08002402 struct ethhdr *eth;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002403 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002404
Jiri Benc61adedf2015-08-20 13:56:25 +02002405 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002406
David Stevens66817122013-03-15 04:35:51 +00002407 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002408
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002409 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002410 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2411 info->mode & IP_TUNNEL_INFO_TX) {
2412 vni = tunnel_id_to_key32(info->key.tun_id);
2413 } else {
2414 if (info && info->mode & IP_TUNNEL_INFO_TX)
2415 vxlan_xmit_one(skb, dev, vni, NULL, false);
2416 else
2417 kfree_skb(skb);
2418 return NETDEV_TX_OK;
2419 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002420 }
2421
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002422 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002423 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002424 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002425 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002426#if IS_ENABLED(CONFIG_IPV6)
Xin Long8bff36852017-11-11 19:58:50 +08002427 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2428 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2429 sizeof(struct nd_msg)) &&
2430 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2431 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2432
2433 if (m->icmph.icmp6_code == 0 &&
2434 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002435 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002436 }
2437#endif
2438 }
David Stevens66817122013-03-15 04:35:51 +00002439
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002440 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002441 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002442 did_rsc = false;
2443
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002444 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002445 (ntohs(eth->h_proto) == ETH_P_IP ||
2446 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002447 did_rsc = route_shortcircuit(dev, skb);
2448 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002449 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002450 }
2451
David Stevens66817122013-03-15 04:35:51 +00002452 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002453 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002454 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002455 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002456 !is_multicast_ether_addr(eth->h_dest))
2457 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002458
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002459 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002460 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002461 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002462 }
David Stevens66817122013-03-15 04:35:51 +00002463 }
2464
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002465 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2466 struct sk_buff *skb1;
2467
Eric Dumazet8f646c92014-01-06 09:54:31 -08002468 if (!fdst) {
2469 fdst = rdst;
2470 continue;
2471 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002472 skb1 = skb_clone(skb, GFP_ATOMIC);
2473 if (skb1)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002474 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002475 }
2476
Eric Dumazet8f646c92014-01-06 09:54:31 -08002477 if (fdst)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002478 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002479 else
2480 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002481 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002482}
2483
stephen hemmingerd3428942012-10-01 12:32:35 +00002484/* Walk the forwarding table and purge stale entries */
Kees Cookdf7e8282017-10-04 16:26:59 -07002485static void vxlan_cleanup(struct timer_list *t)
stephen hemmingerd3428942012-10-01 12:32:35 +00002486{
Kees Cookdf7e8282017-10-04 16:26:59 -07002487 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
stephen hemmingerd3428942012-10-01 12:32:35 +00002488 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2489 unsigned int h;
2490
2491 if (!netif_running(vxlan->dev))
2492 return;
2493
stephen hemmingerd3428942012-10-01 12:32:35 +00002494 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2495 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002496
2497 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002498 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2499 struct vxlan_fdb *f
2500 = container_of(p, struct vxlan_fdb, hlist);
2501 unsigned long timeout;
2502
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002503 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002504 continue;
2505
Roopa Prabhudef499c2017-03-27 15:46:41 -07002506 if (f->flags & NTF_EXT_LEARNED)
2507 continue;
2508
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002509 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002510 if (time_before_eq(timeout, jiffies)) {
2511 netdev_dbg(vxlan->dev,
2512 "garbage collect %pM\n",
2513 f->eth_addr);
2514 f->state = NUD_STALE;
Petr Machata0e6160f2018-11-21 08:02:35 +00002515 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002516 } else if (time_before(timeout, next_timer))
2517 next_timer = timeout;
2518 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002519 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002520 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002521
2522 mod_timer(&vxlan->age_timer, next_timer);
2523}
2524
Mark Blocha53cb292017-06-02 03:24:08 +03002525static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2526{
2527 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2528
2529 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002530 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2531#if IS_ENABLED(CONFIG_IPV6)
2532 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2533#endif
Mark Blocha53cb292017-06-02 03:24:08 +03002534 spin_unlock(&vn->sock_lock);
2535}
2536
Jiri Benc69e76662017-07-02 19:00:57 +02002537static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2538 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002539{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002540 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002541 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002542
Jiri Benc69e76662017-07-02 19:00:57 +02002543 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002544 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002545 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002546 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002547}
2548
stephen hemmingerd3428942012-10-01 12:32:35 +00002549/* Setup stats when device is created */
2550static int vxlan_init(struct net_device *dev)
2551{
WANG Cong1c213bd2014-02-13 11:46:28 -08002552 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002553 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002554 return -ENOMEM;
2555
2556 return 0;
2557}
2558
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002559static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002560{
2561 struct vxlan_fdb *f;
2562
2563 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002564 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002565 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00002566 vxlan_fdb_destroy(vxlan, f, true, true);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002567 spin_unlock_bh(&vxlan->hash_lock);
2568}
2569
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002570static void vxlan_uninit(struct net_device *dev)
2571{
2572 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002573
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002574 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002575
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002576 free_percpu(dev->tstats);
2577}
2578
stephen hemmingerd3428942012-10-01 12:32:35 +00002579/* Start ageing timer and join group when device is brought up */
2580static int vxlan_open(struct net_device *dev)
2581{
2582 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002583 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002584
Jiri Benc205f3562015-09-24 13:50:01 +02002585 ret = vxlan_sock_add(vxlan);
2586 if (ret < 0)
2587 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002588
Gao feng79d4a942013-12-10 16:37:32 +08002589 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002590 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002591 if (ret == -EADDRINUSE)
2592 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002593 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002594 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002595 return ret;
2596 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002597 }
2598
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002599 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002600 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2601
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002602 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002603}
2604
2605/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002606static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00002607{
Cong Wang31fec5a2013-05-27 22:35:52 +00002608 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002609
2610 spin_lock_bh(&vxlan->hash_lock);
2611 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2612 struct hlist_node *p, *n;
2613 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2614 struct vxlan_fdb *f
2615 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002616 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2617 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002618 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2619 if (!is_zero_ether_addr(f->eth_addr))
Petr Machata0e6160f2018-11-21 08:02:35 +00002620 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002621 }
2622 }
2623 spin_unlock_bh(&vxlan->hash_lock);
2624}
2625
2626/* Cleanup timer and forwarding table on shutdown */
2627static int vxlan_stop(struct net_device *dev)
2628{
2629 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002630 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002631 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002632
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002633 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002634 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002635 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002636
2637 del_timer_sync(&vxlan->age_timer);
2638
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002639 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02002640 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002641
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002642 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002643}
2644
stephen hemmingerd3428942012-10-01 12:32:35 +00002645/* Stub, nothing needs to be done. */
2646static void vxlan_set_multicast_list(struct net_device *dev)
2647{
2648}
2649
Daniel Borkmann345010b2013-12-18 00:21:08 +01002650static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2651{
2652 struct vxlan_dev *vxlan = netdev_priv(dev);
2653 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002654 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2655 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002656 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04002657
2658 /* This check is different than dev->max_mtu, because it looks at
2659 * the lowerdev->mtu, rather than the static dev->max_mtu
2660 */
2661 if (lowerdev) {
2662 int max_mtu = lowerdev->mtu -
2663 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2664 if (new_mtu > max_mtu)
2665 return -EINVAL;
2666 }
2667
2668 dev->mtu = new_mtu;
2669 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01002670}
2671
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002672static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2673{
2674 struct vxlan_dev *vxlan = netdev_priv(dev);
2675 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2676 __be16 sport, dport;
2677
2678 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2679 vxlan->cfg.port_max, true);
2680 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2681
Jiri Benc239e9442015-12-07 13:04:31 +01002682 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002683 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002684 struct rtable *rt;
2685
pravin shelar655c3de2016-11-13 20:43:55 -08002686 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002687 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002688 &info->key.u.ipv4.src, dport, sport,
2689 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002690 if (IS_ERR(rt))
2691 return PTR_ERR(rt);
2692 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002693 } else {
2694#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08002695 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01002696 struct dst_entry *ndst;
2697
pravin shelar655c3de2016-11-13 20:43:55 -08002698 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002699 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002700 &info->key.u.ipv6.src, dport, sport,
2701 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002702 if (IS_ERR(ndst))
2703 return PTR_ERR(ndst);
2704 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002705#else /* !CONFIG_IPV6 */
2706 return -EPFNOSUPPORT;
2707#endif
2708 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002709 info->key.tp_src = sport;
2710 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002711 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002712}
2713
Jiri Benc0c867c92016-04-05 14:47:10 +02002714static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002715 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002716 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002717 .ndo_open = vxlan_open,
2718 .ndo_stop = vxlan_stop,
2719 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002720 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002721 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002722 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002723 .ndo_validate_addr = eth_validate_addr,
2724 .ndo_set_mac_address = eth_mac_addr,
2725 .ndo_fdb_add = vxlan_fdb_add,
2726 .ndo_fdb_del = vxlan_fdb_delete,
2727 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002728 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002729};
2730
Jiri Bence1e53142016-04-05 14:47:13 +02002731static const struct net_device_ops vxlan_netdev_raw_ops = {
2732 .ndo_init = vxlan_init,
2733 .ndo_uninit = vxlan_uninit,
2734 .ndo_open = vxlan_open,
2735 .ndo_stop = vxlan_stop,
2736 .ndo_start_xmit = vxlan_xmit,
2737 .ndo_get_stats64 = ip_tunnel_get_stats64,
2738 .ndo_change_mtu = vxlan_change_mtu,
2739 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2740};
2741
stephen hemmingerd3428942012-10-01 12:32:35 +00002742/* Info for udev, that this is a virtual tunnel endpoint */
2743static struct device_type vxlan_type = {
2744 .name = "vxlan",
2745};
2746
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002747/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002748 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002749 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002750 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002751static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002752{
2753 struct vxlan_sock *vs;
2754 struct net *net = dev_net(dev);
2755 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002756 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002757
2758 spin_lock(&vn->sock_lock);
2759 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002760 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2761 unsigned short type;
2762
2763 if (vs->flags & VXLAN_F_GPE)
2764 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
2765 else
2766 type = UDP_TUNNEL_TYPE_VXLAN;
2767
2768 if (push)
2769 udp_tunnel_push_rx_port(dev, vs->sock, type);
2770 else
2771 udp_tunnel_drop_rx_port(dev, vs->sock, type);
2772 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002773 }
2774 spin_unlock(&vn->sock_lock);
2775}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002776
stephen hemmingerd3428942012-10-01 12:32:35 +00002777/* Initialize the device structure. */
2778static void vxlan_setup(struct net_device *dev)
2779{
2780 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002781 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002782
Jiri Benc65226ef2016-04-28 16:36:30 +02002783 eth_hw_addr_random(dev);
2784 ether_setup(dev);
2785
David S. Millercf124db2017-05-08 12:52:56 -04002786 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00002787 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2788
stephen hemmingerd3428942012-10-01 12:32:35 +00002789 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002790 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002791 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002792 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002793
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002794 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002795 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002796 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07002797 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002798 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002799
Matthias Schiffera9853432017-06-19 10:03:55 +02002800 /* MTU range: 68 - 65535 */
2801 dev->min_mtu = ETH_MIN_MTU;
2802 dev->max_mtu = ETH_MAX_MTU;
2803
stephen hemminger553675f2013-05-16 11:35:20 +00002804 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002805 spin_lock_init(&vxlan->hash_lock);
2806
Kees Cookdf7e8282017-10-04 16:26:59 -07002807 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
stephen hemmingerd3428942012-10-01 12:32:35 +00002808
2809 vxlan->dev = dev;
2810
Tom Herbert58ce31c2015-08-19 17:07:33 -07002811 gro_cells_init(&vxlan->gro_cells, dev);
2812
stephen hemmingerd3428942012-10-01 12:32:35 +00002813 for (h = 0; h < FDB_HASH_SIZE; ++h)
2814 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2815}
2816
Jiri Benc0c867c92016-04-05 14:47:10 +02002817static void vxlan_ether_setup(struct net_device *dev)
2818{
Jiri Benc0c867c92016-04-05 14:47:10 +02002819 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2820 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2821 dev->netdev_ops = &vxlan_netdev_ether_ops;
2822}
2823
Jiri Bence1e53142016-04-05 14:47:13 +02002824static void vxlan_raw_setup(struct net_device *dev)
2825{
Jiri Benc65226ef2016-04-28 16:36:30 +02002826 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002827 dev->type = ARPHRD_NONE;
2828 dev->hard_header_len = 0;
2829 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002830 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2831 dev->netdev_ops = &vxlan_netdev_raw_ops;
2832}
2833
stephen hemmingerd3428942012-10-01 12:32:35 +00002834static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2835 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002836 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002837 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002838 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2839 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002840 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002841 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2842 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002843 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002844 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2845 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2846 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002847 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002848 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2849 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2850 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2851 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002852 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002853 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002854 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2855 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2856 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002857 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2858 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002859 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002860 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002861 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
Hangbin Liu72f6d712018-04-17 14:11:28 +08002862 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
Stefano Briviob4d306972018-11-08 12:19:16 +01002863 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002864};
2865
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002866static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
2867 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00002868{
2869 if (tb[IFLA_ADDRESS]) {
2870 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002871 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
2872 "Provided link layer address is not Ethernet");
stephen hemmingerd3428942012-10-01 12:32:35 +00002873 return -EINVAL;
2874 }
2875
2876 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002877 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
2878 "Provided Ethernet address is not unicast");
stephen hemmingerd3428942012-10-01 12:32:35 +00002879 return -EADDRNOTAVAIL;
2880 }
2881 }
2882
Matthias Schiffera9853432017-06-19 10:03:55 +02002883 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02002884 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02002885
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002886 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
2887 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
2888 "MTU must be between 68 and 65535");
Matthias Schiffera9853432017-06-19 10:03:55 +02002889 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002890 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002891 }
2892
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002893 if (!data) {
2894 NL_SET_ERR_MSG(extack,
2895 "Required attributes not provided to perform the operation");
stephen hemmingerd3428942012-10-01 12:32:35 +00002896 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002897 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002898
2899 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02002900 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2901
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002902 if (id >= VXLAN_N_VID) {
2903 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID],
2904 "VXLAN ID must be lower than 16777216");
stephen hemmingerd3428942012-10-01 12:32:35 +00002905 return -ERANGE;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002906 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002907 }
2908
stephen hemminger05f47d62012-10-09 20:35:50 +00002909 if (data[IFLA_VXLAN_PORT_RANGE]) {
2910 const struct ifla_vxlan_port_range *p
2911 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2912
2913 if (ntohs(p->high) < ntohs(p->low)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002914 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
2915 "Invalid source port range");
stephen hemminger05f47d62012-10-09 20:35:50 +00002916 return -EINVAL;
2917 }
2918 }
2919
Stefano Briviob4d306972018-11-08 12:19:16 +01002920 if (data[IFLA_VXLAN_DF]) {
2921 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
2922
2923 if (df < 0 || df > VXLAN_DF_MAX) {
2924 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_DF],
2925 "Invalid DF attribute");
2926 return -EINVAL;
2927 }
2928 }
2929
stephen hemmingerd3428942012-10-01 12:32:35 +00002930 return 0;
2931}
2932
Yan Burman1b13c972013-01-29 23:43:07 +00002933static void vxlan_get_drvinfo(struct net_device *netdev,
2934 struct ethtool_drvinfo *drvinfo)
2935{
2936 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2937 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2938}
2939
2940static const struct ethtool_ops vxlan_ethtool_ops = {
2941 .get_drvinfo = vxlan_get_drvinfo,
2942 .get_link = ethtool_op_get_link,
2943};
2944
Tom Herbert3ee64f32014-07-13 19:49:42 -07002945static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2946 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002947{
Cong Wange4c7ed42013-08-31 13:44:33 +08002948 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002949 struct udp_port_cfg udp_conf;
2950 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002951
Tom Herbert3ee64f32014-07-13 19:49:42 -07002952 memset(&udp_conf, 0, sizeof(udp_conf));
2953
2954 if (ipv6) {
2955 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002956 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002957 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002958 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002959 } else {
2960 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002961 }
2962
Tom Herbert3ee64f32014-07-13 19:49:42 -07002963 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002964
Tom Herbert3ee64f32014-07-13 19:49:42 -07002965 /* Open UDP socket */
2966 err = udp_sock_create(net, &udp_conf, &sock);
2967 if (err < 0)
2968 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002969
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002970 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002971}
2972
2973/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002974static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2975 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002976{
2977 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2978 struct vxlan_sock *vs;
2979 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002980 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002981 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002982
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002983 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002984 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002985 return ERR_PTR(-ENOMEM);
2986
2987 for (h = 0; h < VNI_HASH_SIZE; ++h)
2988 INIT_HLIST_HEAD(&vs->vni_list[h]);
2989
Tom Herbert3ee64f32014-07-13 19:49:42 -07002990 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002991 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002992 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002993 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002994 }
2995
Cong Wange4c7ed42013-08-31 13:44:33 +08002996 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03002997 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002998 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002999
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003000 spin_lock(&vn->sock_lock);
3001 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07003002 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07003003 (vs->flags & VXLAN_F_GPE) ?
3004 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07003005 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003006 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00003007
3008 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07003009 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07003010 tunnel_cfg.sk_user_data = vs;
3011 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01003012 tunnel_cfg.encap_rcv = vxlan_rcv;
Stefano Brivioc3a43b92018-11-08 12:19:15 +01003013 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003014 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07003015 tunnel_cfg.gro_receive = vxlan_gro_receive;
3016 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003017
3018 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08003019
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003020 return vs;
3021}
stephen hemminger553675f2013-05-16 11:35:20 +00003022
Jiri Bencb1be00a2015-09-24 13:50:02 +02003023static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003024{
Jiri Benc205f3562015-09-24 13:50:01 +02003025 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3026 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02003027 struct vxlan_dev_node *node;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003028
Jiri Benc205f3562015-09-24 13:50:01 +02003029 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003030 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003031 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003032 vxlan->cfg.dst_port, vxlan->cfg.flags);
Reshetova, Elena66af8462017-07-04 15:52:59 +03003033 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003034 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003035 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003036 }
3037 spin_unlock(&vn->sock_lock);
3038 }
Jiri Benc205f3562015-09-24 13:50:01 +02003039 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003040 vs = vxlan_socket_create(vxlan->net, ipv6,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003041 vxlan->cfg.dst_port, vxlan->cfg.flags);
Jiri Benc205f3562015-09-24 13:50:01 +02003042 if (IS_ERR(vs))
3043 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003044#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02003045 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003046 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003047 node = &vxlan->hlist6;
3048 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02003049#endif
Jiri Benc69e76662017-07-02 19:00:57 +02003050 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003051 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003052 node = &vxlan->hlist4;
3053 }
3054 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02003055 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00003056}
3057
Jiri Bencb1be00a2015-09-24 13:50:02 +02003058static int vxlan_sock_add(struct vxlan_dev *vxlan)
3059{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003060 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3061 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02003062 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003063 int ret = 0;
3064
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003065 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003066#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003067 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02003068 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02003069 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02003070 if (ret < 0 && ret != -EAFNOSUPPORT)
3071 ipv4 = false;
3072 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02003073#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02003074 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003075 ret = __vxlan_sock_add(vxlan, false);
3076 if (ret < 0)
3077 vxlan_sock_release(vxlan);
3078 return ret;
3079}
3080
Matthias Schiffera9853432017-06-19 10:03:55 +02003081static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3082 struct net_device **lower,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003083 struct vxlan_dev *old,
3084 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003085{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01003086 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02003087 struct vxlan_dev *tmp;
3088 bool use_ipv6 = false;
3089
3090 if (conf->flags & VXLAN_F_GPE) {
3091 /* For now, allow GPE only together with
3092 * COLLECT_METADATA. This can be relaxed later; in such
3093 * case, the other side of the PtP link will have to be
3094 * provided.
3095 */
3096 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3097 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003098 NL_SET_ERR_MSG(extack,
3099 "VXLAN GPE does not support this combination of attributes");
Matthias Schiffera9853432017-06-19 10:03:55 +02003100 return -EINVAL;
3101 }
3102 }
3103
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003104 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3105 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02003106 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003107 conf->saddr.sa.sa_family = AF_INET;
3108 } else if (!conf->remote_ip.sa.sa_family) {
3109 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3110 } else if (!conf->saddr.sa.sa_family) {
3111 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3112 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003113
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003114 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3115 NL_SET_ERR_MSG(extack,
3116 "Local and remote address must be from the same family");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003117 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003118 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003119
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003120 if (vxlan_addr_multicast(&conf->saddr)) {
3121 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003122 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003123 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003124
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003125 if (conf->saddr.sa.sa_family == AF_INET6) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003126 if (!IS_ENABLED(CONFIG_IPV6)) {
3127 NL_SET_ERR_MSG(extack,
3128 "IPv6 support not enabled in the kernel");
Matthias Schiffera9853432017-06-19 10:03:55 +02003129 return -EPFNOSUPPORT;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003130 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003131 use_ipv6 = true;
3132 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003133
3134 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3135 int local_type =
3136 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3137 int remote_type =
3138 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3139
3140 if (local_type & IPV6_ADDR_LINKLOCAL) {
3141 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003142 (remote_type != IPV6_ADDR_ANY)) {
3143 NL_SET_ERR_MSG(extack,
3144 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003145 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003146 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003147
3148 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3149 } else {
3150 if (remote_type ==
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003151 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3152 NL_SET_ERR_MSG(extack,
3153 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003154 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003155 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003156
3157 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3158 }
3159 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003160 }
3161
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003162 if (conf->label && !use_ipv6) {
3163 NL_SET_ERR_MSG(extack,
3164 "Label attribute only applies to IPv6 VXLAN devices");
Matthias Schiffera9853432017-06-19 10:03:55 +02003165 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003166 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003167
3168 if (conf->remote_ifindex) {
3169 struct net_device *lowerdev;
3170
3171 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003172 if (!lowerdev) {
3173 NL_SET_ERR_MSG(extack,
3174 "Invalid local interface, device not found");
Matthias Schiffera9853432017-06-19 10:03:55 +02003175 return -ENODEV;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003176 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003177
3178#if IS_ENABLED(CONFIG_IPV6)
3179 if (use_ipv6) {
3180 struct inet6_dev *idev = __in6_dev_get(lowerdev);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003181 if (idev && idev->cnf.disable_ipv6) {
3182 NL_SET_ERR_MSG(extack,
3183 "IPv6 support disabled by administrator");
Matthias Schiffera9853432017-06-19 10:03:55 +02003184 return -EPERM;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003185 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003186 }
3187#endif
3188
3189 *lower = lowerdev;
3190 } else {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003191 if (vxlan_addr_multicast(&conf->remote_ip)) {
3192 NL_SET_ERR_MSG(extack,
3193 "Local interface required for multicast remote destination");
3194
Matthias Schiffera9853432017-06-19 10:03:55 +02003195 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003196 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003197
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003198#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003199 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3200 NL_SET_ERR_MSG(extack,
3201 "Local interface required for link-local local/remote addresses");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003202 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003203 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003204#endif
3205
Matthias Schiffera9853432017-06-19 10:03:55 +02003206 *lower = NULL;
3207 }
3208
3209 if (!conf->dst_port) {
3210 if (conf->flags & VXLAN_F_GPE)
3211 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3212 else
3213 conf->dst_port = htons(vxlan_port);
3214 }
3215
3216 if (!conf->age_interval)
3217 conf->age_interval = FDB_AGE_DEFAULT;
3218
3219 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3220 if (tmp == old)
3221 continue;
3222
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003223 if (tmp->cfg.vni != conf->vni)
3224 continue;
3225 if (tmp->cfg.dst_port != conf->dst_port)
3226 continue;
3227 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003228 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003229 continue;
3230
3231 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3232 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3233 continue;
3234
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003235 NL_SET_ERR_MSG(extack,
3236 "A VXLAN device with the specified VNI already exists");
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003237 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003238 }
3239
3240 return 0;
3241}
3242
3243static void vxlan_config_apply(struct net_device *dev,
3244 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003245 struct net_device *lowerdev,
3246 struct net *src_net,
3247 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003248{
3249 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003250 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003251 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003252 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3253 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003254
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003255 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003256 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003257 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003258 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003259 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003260
Matthias Schiffera9853432017-06-19 10:03:55 +02003261 if (conf->mtu)
3262 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003263
3264 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003265 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003266
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003267 dst->remote_vni = conf->vni;
3268
3269 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003270
Matthias Schiffera9853432017-06-19 10:03:55 +02003271 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003272 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003273
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003274 dev->gso_max_size = lowerdev->gso_max_size;
3275 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003276
3277 needed_headroom = lowerdev->hard_header_len;
3278
3279 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3280 VXLAN_HEADROOM);
Alexey Kodanevf870c1f2017-12-14 20:20:00 +03003281 if (max_mtu < ETH_MIN_MTU)
3282 max_mtu = ETH_MIN_MTU;
3283
3284 if (!changelink && !conf->mtu)
3285 dev->mtu = max_mtu;
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003286 }
3287
Matthias Schiffera9853432017-06-19 10:03:55 +02003288 if (dev->mtu > max_mtu)
3289 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003290
Jiri Bencb1be00a2015-09-24 13:50:02 +02003291 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3292 needed_headroom += VXLAN6_HEADROOM;
3293 else
3294 needed_headroom += VXLAN_HEADROOM;
3295 dev->needed_headroom = needed_headroom;
3296
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003297 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003298}
stephen hemmingerd3428942012-10-01 12:32:35 +00003299
Matthias Schiffera9853432017-06-19 10:03:55 +02003300static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003301 struct vxlan_config *conf, bool changelink,
3302 struct netlink_ext_ack *extack)
Matthias Schiffera9853432017-06-19 10:03:55 +02003303{
3304 struct vxlan_dev *vxlan = netdev_priv(dev);
3305 struct net_device *lowerdev;
3306 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003307
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003308 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
Matthias Schiffera9853432017-06-19 10:03:55 +02003309 if (ret)
3310 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003311
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003312 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003313
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003314 return 0;
3315}
3316
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003317static int __vxlan_dev_create(struct net *net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003318 struct vxlan_config *conf,
3319 struct netlink_ext_ack *extack)
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003320{
3321 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3322 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003323 struct vxlan_fdb *f = NULL;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003324 int err;
3325
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003326 err = vxlan_dev_configure(net, dev, conf, false, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003327 if (err)
3328 return err;
3329
3330 dev->ethtool_ops = &vxlan_ethtool_ops;
3331
3332 /* create an fdb entry for a valid default destination */
3333 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003334 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003335 &vxlan->default_dst.remote_ip,
3336 NUD_REACHABLE | NUD_PERMANENT,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003337 vxlan->cfg.dst_port,
3338 vxlan->default_dst.remote_vni,
3339 vxlan->default_dst.remote_vni,
3340 vxlan->default_dst.remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003341 NTF_SELF, &f);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003342 if (err)
3343 return err;
3344 }
3345
3346 err = register_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003347 if (err)
3348 goto errout;
3349
3350 err = rtnl_configure_link(dev, NULL);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003351 if (err) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003352 unregister_netdevice(dev);
3353 goto errout;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003354 }
3355
Roopa Prabhu0241b832018-07-04 16:46:32 -07003356 /* notify default fdb entry */
3357 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003358 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
3359 true);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003360
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003361 list_add(&vxlan->next, &vn->vxlan_list);
3362 return 0;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003363errout:
3364 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003365 vxlan_fdb_destroy(vxlan, f, false, false);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003366 return err;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003367}
3368
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003369static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3370 struct net_device *dev, struct vxlan_config *conf,
3371 bool changelink)
3372{
3373 struct vxlan_dev *vxlan = netdev_priv(dev);
3374
3375 memset(conf, 0, sizeof(*conf));
3376
3377 /* if changelink operation, start with old existing cfg */
3378 if (changelink)
3379 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3380
3381 if (data[IFLA_VXLAN_ID]) {
3382 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3383
3384 if (changelink && (vni != conf->vni))
3385 return -EOPNOTSUPP;
3386 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3387 }
3388
3389 if (data[IFLA_VXLAN_GROUP]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003390 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET))
3391 return -EOPNOTSUPP;
3392
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003393 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003394 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003395 } else if (data[IFLA_VXLAN_GROUP6]) {
3396 if (!IS_ENABLED(CONFIG_IPV6))
3397 return -EPFNOSUPPORT;
3398
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003399 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6))
3400 return -EOPNOTSUPP;
3401
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003402 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3403 conf->remote_ip.sa.sa_family = AF_INET6;
3404 }
3405
3406 if (data[IFLA_VXLAN_LOCAL]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003407 if (changelink && (conf->saddr.sa.sa_family != AF_INET))
3408 return -EOPNOTSUPP;
3409
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003410 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3411 conf->saddr.sa.sa_family = AF_INET;
3412 } else if (data[IFLA_VXLAN_LOCAL6]) {
3413 if (!IS_ENABLED(CONFIG_IPV6))
3414 return -EPFNOSUPPORT;
3415
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003416 if (changelink && (conf->saddr.sa.sa_family != AF_INET6))
3417 return -EOPNOTSUPP;
3418
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003419 /* TODO: respect scope id */
3420 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3421 conf->saddr.sa.sa_family = AF_INET6;
3422 }
3423
3424 if (data[IFLA_VXLAN_LINK])
3425 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3426
3427 if (data[IFLA_VXLAN_TOS])
3428 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3429
3430 if (data[IFLA_VXLAN_TTL])
3431 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3432
Hangbin Liu72f6d712018-04-17 14:11:28 +08003433 if (data[IFLA_VXLAN_TTL_INHERIT]) {
3434 if (changelink)
3435 return -EOPNOTSUPP;
3436 conf->flags |= VXLAN_F_TTL_INHERIT;
3437 }
3438
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003439 if (data[IFLA_VXLAN_LABEL])
3440 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3441 IPV6_FLOWLABEL_MASK;
3442
3443 if (data[IFLA_VXLAN_LEARNING]) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003444 if (nla_get_u8(data[IFLA_VXLAN_LEARNING]))
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003445 conf->flags |= VXLAN_F_LEARN;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003446 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003447 conf->flags &= ~VXLAN_F_LEARN;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003448 } else if (!changelink) {
3449 /* default to learn on a new device */
3450 conf->flags |= VXLAN_F_LEARN;
3451 }
3452
3453 if (data[IFLA_VXLAN_AGEING]) {
3454 if (changelink)
3455 return -EOPNOTSUPP;
3456 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3457 }
3458
3459 if (data[IFLA_VXLAN_PROXY]) {
3460 if (changelink)
3461 return -EOPNOTSUPP;
3462 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3463 conf->flags |= VXLAN_F_PROXY;
3464 }
3465
3466 if (data[IFLA_VXLAN_RSC]) {
3467 if (changelink)
3468 return -EOPNOTSUPP;
3469 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3470 conf->flags |= VXLAN_F_RSC;
3471 }
3472
3473 if (data[IFLA_VXLAN_L2MISS]) {
3474 if (changelink)
3475 return -EOPNOTSUPP;
3476 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3477 conf->flags |= VXLAN_F_L2MISS;
3478 }
3479
3480 if (data[IFLA_VXLAN_L3MISS]) {
3481 if (changelink)
3482 return -EOPNOTSUPP;
3483 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3484 conf->flags |= VXLAN_F_L3MISS;
3485 }
3486
3487 if (data[IFLA_VXLAN_LIMIT]) {
3488 if (changelink)
3489 return -EOPNOTSUPP;
3490 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3491 }
3492
3493 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3494 if (changelink)
3495 return -EOPNOTSUPP;
3496 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3497 conf->flags |= VXLAN_F_COLLECT_METADATA;
3498 }
3499
3500 if (data[IFLA_VXLAN_PORT_RANGE]) {
3501 if (!changelink) {
3502 const struct ifla_vxlan_port_range *p
3503 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3504 conf->port_min = ntohs(p->low);
3505 conf->port_max = ntohs(p->high);
3506 } else {
3507 return -EOPNOTSUPP;
3508 }
3509 }
3510
3511 if (data[IFLA_VXLAN_PORT]) {
3512 if (changelink)
3513 return -EOPNOTSUPP;
3514 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3515 }
3516
3517 if (data[IFLA_VXLAN_UDP_CSUM]) {
3518 if (changelink)
3519 return -EOPNOTSUPP;
3520 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3521 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3522 }
3523
3524 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3525 if (changelink)
3526 return -EOPNOTSUPP;
3527 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3528 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3529 }
3530
3531 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3532 if (changelink)
3533 return -EOPNOTSUPP;
3534 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3535 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3536 }
3537
3538 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3539 if (changelink)
3540 return -EOPNOTSUPP;
3541 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3542 conf->flags |= VXLAN_F_REMCSUM_TX;
3543 }
3544
3545 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3546 if (changelink)
3547 return -EOPNOTSUPP;
3548 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3549 conf->flags |= VXLAN_F_REMCSUM_RX;
3550 }
3551
3552 if (data[IFLA_VXLAN_GBP]) {
3553 if (changelink)
3554 return -EOPNOTSUPP;
3555 conf->flags |= VXLAN_F_GBP;
3556 }
3557
3558 if (data[IFLA_VXLAN_GPE]) {
3559 if (changelink)
3560 return -EOPNOTSUPP;
3561 conf->flags |= VXLAN_F_GPE;
3562 }
3563
3564 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3565 if (changelink)
3566 return -EOPNOTSUPP;
3567 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3568 }
3569
3570 if (tb[IFLA_MTU]) {
3571 if (changelink)
3572 return -EOPNOTSUPP;
3573 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3574 }
3575
Stefano Briviob4d306972018-11-08 12:19:16 +01003576 if (data[IFLA_VXLAN_DF])
3577 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
3578
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003579 return 0;
3580}
3581
3582static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003583 struct nlattr *tb[], struct nlattr *data[],
3584 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003585{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003586 struct vxlan_config conf;
3587 int err;
3588
3589 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3590 if (err)
3591 return err;
3592
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003593 return __vxlan_dev_create(src_net, dev, &conf, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00003594}
3595
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003596static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02003597 struct nlattr *data[],
3598 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003599{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003600 struct vxlan_dev *vxlan = netdev_priv(dev);
3601 struct vxlan_rdst *dst = &vxlan->default_dst;
3602 struct vxlan_rdst old_dst;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003603 struct vxlan_config conf;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003604 struct vxlan_fdb *f = NULL;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003605 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003606
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003607 err = vxlan_nl2conf(tb, data,
3608 dev, &conf, true);
3609 if (err)
3610 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07003611
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003612 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003613
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003614 err = vxlan_dev_configure(vxlan->net, dev, &conf, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003615 if (err)
3616 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003617
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003618 /* handle default dst entry */
3619 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3620 spin_lock_bh(&vxlan->hash_lock);
3621 if (!vxlan_addr_any(&old_dst.remote_ip))
3622 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3623 old_dst.remote_ip,
3624 vxlan->cfg.dst_port,
3625 old_dst.remote_vni,
3626 old_dst.remote_vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00003627 old_dst.remote_ifindex,
3628 true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003629
3630 if (!vxlan_addr_any(&dst->remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003631 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003632 &dst->remote_ip,
3633 NUD_REACHABLE | NUD_PERMANENT,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003634 vxlan->cfg.dst_port,
3635 dst->remote_vni,
3636 dst->remote_vni,
3637 dst->remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003638 NTF_SELF, &f);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003639 if (err) {
3640 spin_unlock_bh(&vxlan->hash_lock);
3641 return err;
3642 }
Petr Machata0e6160f2018-11-21 08:02:35 +00003643 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3644 RTM_NEWNEIGH, true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003645 }
3646 spin_unlock_bh(&vxlan->hash_lock);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003647 }
3648
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003649 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003650}
3651
stephen hemmingerd3428942012-10-01 12:32:35 +00003652static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3653{
3654 struct vxlan_dev *vxlan = netdev_priv(dev);
3655
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003656 vxlan_flush(vxlan, true);
3657
Tom Herbert58ce31c2015-08-19 17:07:33 -07003658 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003659 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003660 unregister_netdevice_queue(dev, head);
3661}
3662
3663static size_t vxlan_get_size(const struct net_device *dev)
3664{
3665
3666 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003667 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003668 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003669 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003670 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
Hangbin Liu8fd78062018-09-26 10:35:42 +08003671 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
stephen hemmingerd3428942012-10-01 12:32:35 +00003672 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Stefano Briviob4d306972018-11-08 12:19:16 +01003673 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003674 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003675 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003676 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3677 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3678 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3679 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003680 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003681 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3682 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003683 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003684 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3685 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3686 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3687 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003688 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3689 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003690 0;
3691}
3692
3693static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3694{
3695 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003696 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003697 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003698 .low = htons(vxlan->cfg.port_min),
3699 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003700 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003701
Jiri Benc54bfd872016-02-16 21:58:58 +01003702 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003703 goto nla_put_failure;
3704
Cong Wange4c7ed42013-08-31 13:44:33 +08003705 if (!vxlan_addr_any(&dst->remote_ip)) {
3706 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003707 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3708 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003709 goto nla_put_failure;
3710#if IS_ENABLED(CONFIG_IPV6)
3711 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003712 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3713 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003714 goto nla_put_failure;
3715#endif
3716 }
3717 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003718
Atzm Watanabec7995c42013-04-16 02:50:52 +00003719 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003720 goto nla_put_failure;
3721
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003722 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3723 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003724 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003725 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003726 goto nla_put_failure;
3727#if IS_ENABLED(CONFIG_IPV6)
3728 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003729 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003730 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003731 goto nla_put_failure;
3732#endif
3733 }
3734 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003735
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003736 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
Hangbin Liu8fd78062018-09-26 10:35:42 +08003737 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
3738 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003739 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Stefano Briviob4d306972018-11-08 12:19:16 +01003740 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003741 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003742 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003743 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003744 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003745 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
3746 nla_put_u8(skb, IFLA_VXLAN_RSC,
3747 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003748 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003749 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003750 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003751 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003752 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003753 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003754 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3755 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3756 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003757 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003758 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003759 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003760 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003761 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003762 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003763 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003764 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003765 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003766 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003767 goto nla_put_failure;
3768
stephen hemminger05f47d62012-10-09 20:35:50 +00003769 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3770 goto nla_put_failure;
3771
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003772 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01003773 nla_put_flag(skb, IFLA_VXLAN_GBP))
3774 goto nla_put_failure;
3775
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003776 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02003777 nla_put_flag(skb, IFLA_VXLAN_GPE))
3778 goto nla_put_failure;
3779
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003780 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003781 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3782 goto nla_put_failure;
3783
stephen hemmingerd3428942012-10-01 12:32:35 +00003784 return 0;
3785
3786nla_put_failure:
3787 return -EMSGSIZE;
3788}
3789
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003790static struct net *vxlan_get_link_net(const struct net_device *dev)
3791{
3792 struct vxlan_dev *vxlan = netdev_priv(dev);
3793
3794 return vxlan->net;
3795}
3796
stephen hemmingerd3428942012-10-01 12:32:35 +00003797static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3798 .kind = "vxlan",
3799 .maxtype = IFLA_VXLAN_MAX,
3800 .policy = vxlan_policy,
3801 .priv_size = sizeof(struct vxlan_dev),
3802 .setup = vxlan_setup,
3803 .validate = vxlan_validate,
3804 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003805 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00003806 .dellink = vxlan_dellink,
3807 .get_size = vxlan_get_size,
3808 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003809 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003810};
3811
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003812struct net_device *vxlan_dev_create(struct net *net, const char *name,
3813 u8 name_assign_type,
3814 struct vxlan_config *conf)
3815{
3816 struct nlattr *tb[IFLA_MAX + 1];
3817 struct net_device *dev;
3818 int err;
3819
3820 memset(&tb, 0, sizeof(tb));
3821
3822 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08003823 &vxlan_link_ops, tb, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003824 if (IS_ERR(dev))
3825 return dev;
3826
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003827 err = __vxlan_dev_create(net, dev, conf, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003828 if (err < 0) {
3829 free_netdev(dev);
3830 return ERR_PTR(err);
3831 }
3832
3833 err = rtnl_configure_link(dev, NULL);
3834 if (err < 0) {
3835 LIST_HEAD(list_kill);
3836
3837 vxlan_dellink(dev, &list_kill);
3838 unregister_netdevice_many(&list_kill);
3839 return ERR_PTR(err);
3840 }
3841
3842 return dev;
3843}
3844EXPORT_SYMBOL_GPL(vxlan_dev_create);
3845
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003846static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3847 struct net_device *dev)
3848{
3849 struct vxlan_dev *vxlan, *next;
3850 LIST_HEAD(list_kill);
3851
3852 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3853 struct vxlan_rdst *dst = &vxlan->default_dst;
3854
3855 /* In case we created vxlan device with carrier
3856 * and we loose the carrier due to module unload
3857 * we also need to remove vxlan device. In other
3858 * cases, it's not necessary and remote_ifindex
3859 * is 0 here, so no matches.
3860 */
3861 if (dst->remote_ifindex == dev->ifindex)
3862 vxlan_dellink(vxlan->dev, &list_kill);
3863 }
3864
3865 unregister_netdevice_many(&list_kill);
3866}
3867
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003868static int vxlan_netdevice_event(struct notifier_block *unused,
3869 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003870{
3871 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003872 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003873
Sabrina Dubroca04584952017-07-21 12:49:33 +02003874 if (event == NETDEV_UNREGISTER) {
3875 vxlan_offload_rx_ports(dev, false);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003876 vxlan_handle_lowerdev_unregister(vn, dev);
Sabrina Dubroca04584952017-07-21 12:49:33 +02003877 } else if (event == NETDEV_REGISTER) {
3878 vxlan_offload_rx_ports(dev, true);
3879 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
3880 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003881 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02003882 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003883
3884 return NOTIFY_DONE;
3885}
3886
3887static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003888 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003889};
3890
Petr Machata0efe1172018-10-17 08:53:26 +00003891static void
3892vxlan_fdb_offloaded_set(struct net_device *dev,
3893 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
3894{
3895 struct vxlan_dev *vxlan = netdev_priv(dev);
3896 struct vxlan_rdst *rdst;
3897 struct vxlan_fdb *f;
3898
3899 spin_lock_bh(&vxlan->hash_lock);
3900
3901 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
3902 if (!f)
3903 goto out;
3904
3905 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
3906 fdb_info->remote_port,
3907 fdb_info->remote_vni,
3908 fdb_info->remote_ifindex);
3909 if (!rdst)
3910 goto out;
3911
3912 rdst->offloaded = fdb_info->offloaded;
3913
3914out:
3915 spin_unlock_bh(&vxlan->hash_lock);
3916}
3917
3918static int vxlan_switchdev_event(struct notifier_block *unused,
3919 unsigned long event, void *ptr)
3920{
3921 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3922
3923 switch (event) {
3924 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
3925 vxlan_fdb_offloaded_set(dev, ptr);
3926 break;
3927 }
3928
3929 return 0;
3930}
3931
3932static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
3933 .notifier_call = vxlan_switchdev_event,
3934};
3935
stephen hemmingerd3428942012-10-01 12:32:35 +00003936static __net_init int vxlan_init_net(struct net *net)
3937{
3938 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003939 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003940
stephen hemminger553675f2013-05-16 11:35:20 +00003941 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003942 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003943
stephen hemminger553675f2013-05-16 11:35:20 +00003944 for (h = 0; h < PORT_HASH_SIZE; ++h)
3945 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003946
3947 return 0;
3948}
3949
Haishuang Yan57b61122017-12-16 17:54:49 +08003950static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003951{
3952 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3953 struct vxlan_dev *vxlan, *next;
3954 struct net_device *dev, *aux;
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03003955 unsigned int h;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003956
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003957 for_each_netdev_safe(net, dev, aux)
3958 if (dev->rtnl_link_ops == &vxlan_link_ops)
Haishuang Yan57b61122017-12-16 17:54:49 +08003959 unregister_netdevice_queue(dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003960
3961 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3962 /* If vxlan->dev is in the same netns, it has already been added
3963 * to the list by the previous loop.
3964 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003965 if (!net_eq(dev_net(vxlan->dev), net)) {
3966 gro_cells_destroy(&vxlan->gro_cells);
Haishuang Yan57b61122017-12-16 17:54:49 +08003967 unregister_netdevice_queue(vxlan->dev, head);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003968 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003969 }
3970
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03003971 for (h = 0; h < PORT_HASH_SIZE; ++h)
3972 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003973}
3974
Haishuang Yan57b61122017-12-16 17:54:49 +08003975static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
3976{
3977 struct net *net;
3978 LIST_HEAD(list);
3979
3980 rtnl_lock();
3981 list_for_each_entry(net, net_list, exit_list)
3982 vxlan_destroy_tunnels(net, &list);
3983
3984 unregister_netdevice_many(&list);
3985 rtnl_unlock();
3986}
3987
stephen hemmingerd3428942012-10-01 12:32:35 +00003988static struct pernet_operations vxlan_net_ops = {
3989 .init = vxlan_init_net,
Haishuang Yan57b61122017-12-16 17:54:49 +08003990 .exit_batch = vxlan_exit_batch_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003991 .id = &vxlan_net_id,
3992 .size = sizeof(struct vxlan_net),
3993};
3994
3995static int __init vxlan_init_module(void)
3996{
3997 int rc;
3998
3999 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4000
Daniel Borkmann783c1462014-01-22 21:07:53 +01004001 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004002 if (rc)
4003 goto out1;
4004
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004005 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004006 if (rc)
4007 goto out2;
4008
Petr Machata0efe1172018-10-17 08:53:26 +00004009 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004010 if (rc)
4011 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00004012
Petr Machata0efe1172018-10-17 08:53:26 +00004013 rc = rtnl_link_register(&vxlan_link_ops);
4014 if (rc)
4015 goto out4;
4016
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004017 return 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004018out4:
4019 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004020out3:
4021 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004022out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01004023 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004024out1:
4025 return rc;
4026}
Cong Wang7332a132013-05-27 22:35:53 +00004027late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00004028
4029static void __exit vxlan_cleanup_module(void)
4030{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07004031 rtnl_link_unregister(&vxlan_link_ops);
Petr Machata0efe1172018-10-17 08:53:26 +00004032 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004033 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004034 unregister_pernet_subsys(&vxlan_net_ops);
4035 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00004036}
4037module_exit(vxlan_cleanup_module);
4038
4039MODULE_LICENSE("GPL");
4040MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004041MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08004042MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00004043MODULE_ALIAS_RTNL_LINK("vxlan");