blob: 911066299a836f53107c3c82fa098abf9bee15da [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 }
Petr Machata0ec566a2018-11-21 08:02:37 +0000783
784 /* Do not allow an externally learned entry to take over an
785 * entry added by the user.
786 */
787 if (!(fdb_flags & NTF_EXT_LEARNED) ||
788 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
789 if (f->state != state) {
790 f->state = state;
791 f->updated = jiffies;
792 notify = 1;
793 }
794 if (f->flags != fdb_flags) {
795 f->flags = fdb_flags;
796 f->updated = jiffies;
797 notify = 1;
798 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000799 }
Petr Machata0ec566a2018-11-21 08:02:37 +0000800
Thomas Richter906dc182013-07-19 17:20:07 +0200801 if ((flags & NLM_F_REPLACE)) {
802 /* Only change unicasts */
803 if (!(is_multicast_ether_addr(f->eth_addr) ||
804 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800805 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200806 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200807 } else
808 return -EOPNOTSUPP;
809 }
David Stevens66817122013-03-15 04:35:51 +0000810 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300811 (is_multicast_ether_addr(f->eth_addr) ||
812 is_zero_ether_addr(f->eth_addr))) {
Haishuang Yan17b46362016-11-29 09:59:36 +0800813 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
David Stevens66817122013-03-15 04:35:51 +0000814
815 if (rc < 0)
816 return rc;
817 notify |= rc;
818 }
Roopa Prabhu0813e952018-10-11 12:35:13 -0700819
820 if (ndm_flags & NTF_USE)
821 f->used = jiffies;
stephen hemmingerd3428942012-10-01 12:32:35 +0000822 } else {
823 if (!(flags & NLM_F_CREATE))
824 return -ENOENT;
825
Thomas Richter906dc182013-07-19 17:20:07 +0200826 /* Disallow replace to add a multicast entry */
827 if ((flags & NLM_F_REPLACE) &&
828 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
829 return -EOPNOTSUPP;
830
Cong Wange4c7ed42013-08-31 13:44:33 +0800831 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700832 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
Roopa Prabhu0813e952018-10-11 12:35:13 -0700833 vni, ifindex, fdb_flags, &f);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700834 if (rc < 0)
Haishuang Yan17b46362016-11-29 09:59:36 +0800835 return rc;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700836 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000837 }
838
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200839 if (notify) {
840 if (rd == NULL)
841 rd = first_remote_rtnl(f);
Petr Machata0e6160f2018-11-21 08:02:35 +0000842 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH, swdev_notify);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200843 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000844
845 return 0;
846}
847
Wei Yongjun6706c822013-04-11 19:00:35 +0000848static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000849{
850 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700851 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000852
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100853 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
854 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000855 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100856 }
David Stevens66817122013-03-15 04:35:51 +0000857 kfree(f);
858}
859
Roopa Prabhu4c2438b2018-07-04 16:46:31 -0700860static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +0000861 bool do_notify, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +0000862{
Petr Machata045a5a92018-10-17 08:53:27 +0000863 struct vxlan_rdst *rd;
864
stephen hemmingerd3428942012-10-01 12:32:35 +0000865 netdev_dbg(vxlan->dev,
866 "delete %pM\n", f->eth_addr);
867
868 --vxlan->addrcnt;
Roopa Prabhu4c2438b2018-07-04 16:46:31 -0700869 if (do_notify)
Petr Machata045a5a92018-10-17 08:53:27 +0000870 list_for_each_entry(rd, &f->remotes, list)
Petr Machata0e6160f2018-11-21 08:02:35 +0000871 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
872 swdev_notify);
stephen hemmingerd3428942012-10-01 12:32:35 +0000873
874 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000875 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000876}
877
Lance Richardson35cf2842017-05-29 13:25:57 -0400878static void vxlan_dst_free(struct rcu_head *head)
879{
880 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
881
882 dst_cache_destroy(&rd->dst_cache);
883 kfree(rd);
884}
885
886static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +0000887 struct vxlan_rdst *rd, bool swdev_notify)
Lance Richardson35cf2842017-05-29 13:25:57 -0400888{
889 list_del_rcu(&rd->list);
Petr Machata0e6160f2018-11-21 08:02:35 +0000890 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify);
Lance Richardson35cf2842017-05-29 13:25:57 -0400891 call_rcu(&rd->rcu, vxlan_dst_free);
892}
893
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300894static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800895 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
896 __be32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300897{
898 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800899 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300900
901 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800902 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
903 if (err)
904 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300905 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800906 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
907 if (remote->sa.sa_family == AF_INET) {
908 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
909 ip->sa.sa_family = AF_INET;
910#if IS_ENABLED(CONFIG_IPV6)
911 } else {
912 ip->sin6.sin6_addr = in6addr_any;
913 ip->sa.sa_family = AF_INET6;
914#endif
915 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300916 }
917
918 if (tb[NDA_PORT]) {
919 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
920 return -EINVAL;
921 *port = nla_get_be16(tb[NDA_PORT]);
922 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200923 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300924 }
925
926 if (tb[NDA_VNI]) {
927 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
928 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100929 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300930 } else {
931 *vni = vxlan->default_dst.remote_vni;
932 }
933
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800934 if (tb[NDA_SRC_VNI]) {
935 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
936 return -EINVAL;
937 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
938 } else {
939 *src_vni = vxlan->default_dst.remote_vni;
940 }
941
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300942 if (tb[NDA_IFINDEX]) {
943 struct net_device *tdev;
944
945 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
946 return -EINVAL;
947 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800948 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300949 if (!tdev)
950 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300951 } else {
952 *ifindex = 0;
953 }
954
955 return 0;
956}
957
stephen hemmingerd3428942012-10-01 12:32:35 +0000958/* Add static entry (via netlink) */
959static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
960 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100961 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000962{
963 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300964 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800965 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000966 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800967 __be32 src_vni, vni;
Jiri Benc54bfd872016-02-16 21:58:58 +0100968 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000969 int err;
970
971 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
972 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
973 ndm->ndm_state);
974 return -EINVAL;
975 }
976
977 if (tb[NDA_DST] == NULL)
978 return -EINVAL;
979
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800980 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300981 if (err)
982 return err;
David Stevens66817122013-03-15 04:35:51 +0000983
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300984 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
985 return -EAFNOSUPPORT;
986
stephen hemmingerd3428942012-10-01 12:32:35 +0000987 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700988 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
Petr Machata45598c12018-11-21 08:02:36 +0000989 port, src_vni, vni, ifindex,
990 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
Petr Machata0e6160f2018-11-21 08:02:35 +0000991 true);
stephen hemmingerd3428942012-10-01 12:32:35 +0000992 spin_unlock_bh(&vxlan->hash_lock);
993
994 return err;
995}
996
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800997static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
998 const unsigned char *addr, union vxlan_addr ip,
Xin Longfc39c382017-11-26 21:19:05 +0800999 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001000 u32 ifindex, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +00001001{
stephen hemmingerd3428942012-10-01 12:32:35 +00001002 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001003 struct vxlan_rdst *rd = NULL;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001004 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001005
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001006 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001007 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001008 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001009
Cong Wange4c7ed42013-08-31 13:44:33 +08001010 if (!vxlan_addr_any(&ip)) {
1011 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001012 if (!rd)
1013 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +00001014 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001015
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001016 /* remove a destination if it's not the only one on the list,
1017 * otherwise destroy the fdb entry
1018 */
1019 if (rd && !list_is_singular(&f->remotes)) {
Petr Machata0e6160f2018-11-21 08:02:35 +00001020 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001021 goto out;
1022 }
1023
Petr Machata0e6160f2018-11-21 08:02:35 +00001024 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001025
1026out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001027 return 0;
1028}
1029
1030/* Delete entry (via netlink) */
1031static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1032 struct net_device *dev,
1033 const unsigned char *addr, u16 vid)
1034{
1035 struct vxlan_dev *vxlan = netdev_priv(dev);
1036 union vxlan_addr ip;
1037 __be32 src_vni, vni;
1038 __be16 port;
1039 u32 ifindex;
1040 int err;
1041
1042 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
1043 if (err)
1044 return err;
1045
1046 spin_lock_bh(&vxlan->hash_lock);
Petr Machata0e6160f2018-11-21 08:02:35 +00001047 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1048 true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001049 spin_unlock_bh(&vxlan->hash_lock);
1050
1051 return err;
1052}
1053
1054/* Dump forwarding table */
1055static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04001056 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07001057 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +00001058{
1059 struct vxlan_dev *vxlan = netdev_priv(dev);
1060 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -07001061 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001062
1063 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1064 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001065
Sasha Levinb67bfe02013-02-27 17:06:00 -08001066 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +00001067 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +00001068
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001069 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07001070 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001071 goto skip;
1072
David Stevens66817122013-03-15 04:35:51 +00001073 err = vxlan_fdb_info(skb, vxlan, f,
1074 NETLINK_CB(cb->skb).portid,
1075 cb->nlh->nlmsg_seq,
1076 RTM_NEWNEIGH,
1077 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -07001078 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001079 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001080skip:
Roopa Prabhud2976532016-08-30 21:56:45 -07001081 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001082 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001083 }
1084 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001085out:
Roopa Prabhud2976532016-08-30 21:56:45 -07001086 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001087}
1088
1089/* Watch incoming packets to learn mapping between Ethernet address
1090 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +09001091 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +00001092 */
stephen hemminger26a41ae62013-06-17 12:09:58 -07001093static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001094 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001095 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +00001096{
1097 struct vxlan_dev *vxlan = netdev_priv(dev);
1098 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001099 u32 ifindex = 0;
1100
1101#if IS_ENABLED(CONFIG_IPV6)
1102 if (src_ip->sa.sa_family == AF_INET6 &&
1103 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1104 ifindex = src_ifindex;
1105#endif
stephen hemmingerd3428942012-10-01 12:32:35 +00001106
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001107 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001108 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001109 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001110
Matthias Schiffer87613de2017-06-19 10:03:59 +02001111 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1112 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001113 return false;
1114
1115 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -07001116 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001117 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001118
1119 if (net_ratelimit())
1120 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001121 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001122 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001123
Cong Wange4c7ed42013-08-31 13:44:33 +08001124 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001125 f->updated = jiffies;
Petr Machata0e6160f2018-11-21 08:02:35 +00001126 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001127 } else {
1128 /* learned new entry */
1129 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001130
1131 /* close off race between vxlan_flush and incoming packets */
1132 if (netif_running(dev))
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001133 vxlan_fdb_update(vxlan, src_mac, src_ip,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001134 NUD_REACHABLE,
1135 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001136 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001137 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001138 vxlan->default_dst.remote_vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001139 ifindex, NTF_SELF, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001140 spin_unlock(&vxlan->hash_lock);
1141 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001142
1143 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001144}
1145
stephen hemmingerd3428942012-10-01 12:32:35 +00001146/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001147static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001148{
stephen hemminger553675f2013-05-16 11:35:20 +00001149 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001150 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001151#if IS_ENABLED(CONFIG_IPV6)
1152 struct vxlan_sock *sock6;
1153#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001154 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001155
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001156 sock4 = rtnl_dereference(dev->vn4_sock);
1157
Gao feng95ab0992013-12-10 16:37:33 +08001158 /* The vxlan_sock is only used by dev, leaving group has
1159 * no effect on other vxlan devices.
1160 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001161 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001162 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001163#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001164 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001165 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001166 return false;
1167#endif
Gao feng95ab0992013-12-10 16:37:33 +08001168
stephen hemminger553675f2013-05-16 11:35:20 +00001169 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001170 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001171 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001172
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001173 if (family == AF_INET &&
1174 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001175 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001176#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001177 if (family == AF_INET6 &&
1178 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001179 continue;
1180#endif
Gao feng95ab0992013-12-10 16:37:33 +08001181
1182 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1183 &dev->default_dst.remote_ip))
1184 continue;
1185
1186 if (vxlan->default_dst.remote_ifindex !=
1187 dev->default_dst.remote_ifindex)
1188 continue;
1189
1190 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001191 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001192
1193 return false;
1194}
1195
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001196static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001197{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001198 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001199
Jiri Bencb1be00a2015-09-24 13:50:02 +02001200 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001201 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001202 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001203 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001204
Jiri Bencb1be00a2015-09-24 13:50:02 +02001205 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001206 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001207 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001208 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001209 (vs->flags & VXLAN_F_GPE) ?
1210 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001211 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001212 spin_unlock(&vn->sock_lock);
1213
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001214 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001215}
1216
Jiri Bencb1be00a2015-09-24 13:50:02 +02001217static void vxlan_sock_release(struct vxlan_dev *vxlan)
1218{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001219 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001220#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001221 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1222
Mark Bloch57d88182017-06-07 14:36:58 +03001223 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001224#endif
1225
Mark Bloch57d88182017-06-07 14:36:58 +03001226 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001227 synchronize_net();
1228
Mark Blocha53cb292017-06-02 03:24:08 +03001229 vxlan_vs_del_dev(vxlan);
1230
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001231 if (__vxlan_sock_release_prep(sock4)) {
1232 udp_tunnel_sock_release(sock4->sock);
1233 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001234 }
1235
1236#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001237 if (__vxlan_sock_release_prep(sock6)) {
1238 udp_tunnel_sock_release(sock6->sock);
1239 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001240 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001241#endif
1242}
1243
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001244/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001245 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001246 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001247static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001248{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001249 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001250 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1251 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001252 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001253
Cong Wange4c7ed42013-08-31 13:44:33 +08001254 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001255 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001256 struct ip_mreqn mreq = {
1257 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1258 .imr_ifindex = ifindex,
1259 };
1260
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001261 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001262 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001263 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001264 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001265#if IS_ENABLED(CONFIG_IPV6)
1266 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001267 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1268
1269 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001270 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001271 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1272 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001273 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001274#endif
1275 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001276
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001277 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001278}
1279
1280/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001281static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001282{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001283 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001284 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1285 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001286 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001287
Cong Wange4c7ed42013-08-31 13:44:33 +08001288 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001289 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001290 struct ip_mreqn mreq = {
1291 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1292 .imr_ifindex = ifindex,
1293 };
1294
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001295 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001296 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001297 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001298 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001299#if IS_ENABLED(CONFIG_IPV6)
1300 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001301 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1302
1303 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001304 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001305 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1306 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001307 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001308#endif
1309 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001310
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001311 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001312}
1313
Jiri Bencf14eceb2016-02-16 21:59:01 +01001314static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1315 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001316{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001317 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001318
Jiri Bencf14eceb2016-02-16 21:59:01 +01001319 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1320 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001321
Jiri Bencf14eceb2016-02-16 21:59:01 +01001322 start = vxlan_rco_start(unparsed->vx_vni);
1323 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001324
Jiri Benc7d34fa72016-03-21 17:50:05 +01001325 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001326 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001327
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001328 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1329 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001330out:
1331 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1332 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001333 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001334}
1335
Jiri Bencf14eceb2016-02-16 21:59:01 +01001336static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001337 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001338 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001339{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001340 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001341 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001342
Jiri Bencf14eceb2016-02-16 21:59:01 +01001343 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1344 goto out;
1345
Jiri Benc3288af02016-02-16 21:59:00 +01001346 md->gbp = ntohs(gbp->policy_id);
1347
Jiri Benc10a5af22016-02-23 18:02:59 +01001348 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001349 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001350 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001351 tun_dst->u.tun_info.options_len = sizeof(*md);
1352 }
Jiri Benc3288af02016-02-16 21:59:00 +01001353 if (gbp->dont_learn)
1354 md->gbp |= VXLAN_GBP_DONT_LEARN;
1355
1356 if (gbp->policy_applied)
1357 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001358
Jiri Benc64f87d32016-02-23 18:02:55 +01001359 /* In flow-based mode, GBP is carried in dst_metadata */
1360 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1361 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001362out:
1363 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001364}
1365
Jiri Bence1e53142016-04-05 14:47:13 +02001366static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001367 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001368 struct sk_buff *skb, u32 vxflags)
1369{
1370 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1371
1372 /* Need to have Next Protocol set for interfaces in GPE mode. */
1373 if (!gpe->np_applied)
1374 return false;
1375 /* "The initial version is 0. If a receiver does not support the
1376 * version indicated it MUST drop the packet.
1377 */
1378 if (gpe->version != 0)
1379 return false;
1380 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1381 * processing MUST occur." However, we don't implement OAM
1382 * processing, thus drop the packet.
1383 */
1384 if (gpe->oam_flag)
1385 return false;
1386
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001387 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1388 if (!*protocol)
Jiri Bence1e53142016-04-05 14:47:13 +02001389 return false;
Jiri Bence1e53142016-04-05 14:47:13 +02001390
1391 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1392 return true;
1393}
1394
Jiri Benc1ab016e2016-02-23 18:02:56 +01001395static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1396 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001397 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001398{
Thomas Graf614732e2015-07-21 10:44:06 +02001399 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001400 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001401
Thomas Graf614732e2015-07-21 10:44:06 +02001402 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001403 skb->protocol = eth_type_trans(skb, vxlan->dev);
1404 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1405
1406 /* Ignore packet loops (and multicast echo) */
1407 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001408 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001409
Jiri Benc760c6802016-02-23 18:02:57 +01001410 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001411 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001412 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001413 saddr.sa.sa_family = AF_INET;
1414#if IS_ENABLED(CONFIG_IPV6)
1415 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001416 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001417 saddr.sa.sa_family = AF_INET6;
1418#endif
1419 }
1420
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001421 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001422 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001423 return false;
1424
1425 return true;
1426}
1427
Jiri Benc760c6802016-02-23 18:02:57 +01001428static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1429 struct sk_buff *skb)
1430{
1431 int err = 0;
1432
1433 if (vxlan_get_sk_family(vs) == AF_INET)
1434 err = IP_ECN_decapsulate(oiph, skb);
1435#if IS_ENABLED(CONFIG_IPV6)
1436 else
1437 err = IP6_ECN_decapsulate(oiph, skb);
1438#endif
1439
1440 if (unlikely(err) && log_ecn_error) {
1441 if (vxlan_get_sk_family(vs) == AF_INET)
1442 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1443 &((struct iphdr *)oiph)->saddr,
1444 ((struct iphdr *)oiph)->tos);
1445 else
1446 net_info_ratelimited("non-ECT from %pI6\n",
1447 &((struct ipv6hdr *)oiph)->saddr);
1448 }
1449 return err <= 1;
1450}
1451
stephen hemmingerd3428942012-10-01 12:32:35 +00001452/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001453static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001454{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001455 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001456 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001457 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001458 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001459 struct vxlan_metadata _md;
1460 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001461 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001462 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001463 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001464 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001465
Jiri Bence1e53142016-04-05 14:47:13 +02001466 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001467 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001468 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001469
Jiri Bencf14eceb2016-02-16 21:59:01 +01001470 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001471 /* VNI flag always required to be set */
1472 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1473 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1474 ntohl(vxlan_hdr(skb)->vx_flags),
1475 ntohl(vxlan_hdr(skb)->vx_vni));
1476 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001477 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001478 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001479 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1480 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001481
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001482 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001483 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001484 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001485
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001486 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1487
Matthias Schiffer49f810f2017-06-19 10:04:00 +02001488 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001489 if (!vxlan)
1490 goto drop;
1491
Jiri Bence1e53142016-04-05 14:47:13 +02001492 /* For backwards compatibility, only allow reserved fields to be
1493 * used by VXLAN extensions if explicitly requested.
1494 */
1495 if (vs->flags & VXLAN_F_GPE) {
1496 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1497 goto drop;
1498 raw_proto = true;
1499 }
1500
1501 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1502 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1503 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001504
Thomas Grafee122c72015-07-21 10:43:58 +02001505 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001506 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001507
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001508 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001509 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001510
Thomas Grafee122c72015-07-21 10:43:58 +02001511 if (!tun_dst)
1512 goto drop;
1513
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001514 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001515
1516 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001517 } else {
1518 memset(md, 0, sizeof(*md));
1519 }
1520
Jiri Bencf14eceb2016-02-16 21:59:01 +01001521 if (vs->flags & VXLAN_F_REMCSUM_RX)
1522 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1523 goto drop;
1524 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001525 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001526 /* Note that GBP and GPE can never be active together. This is
1527 * ensured in vxlan_dev_configure.
1528 */
Thomas Graf35114942015-01-15 03:53:55 +01001529
Jiri Bencf14eceb2016-02-16 21:59:01 +01001530 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001531 /* If there are any unprocessed flags remaining treat
1532 * this as a malformed packet. This behavior diverges from
1533 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1534 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001535 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001536 * is more robust and provides a little more security in
1537 * adding extensions to VXLAN.
1538 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001539 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001540 }
1541
Jiri Bence1e53142016-04-05 14:47:13 +02001542 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001543 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001544 goto drop;
1545 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001546 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001547 skb->dev = vxlan->dev;
1548 skb->pkt_type = PACKET_HOST;
1549 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001550
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001551 oiph = skb_network_header(skb);
1552 skb_reset_network_header(skb);
1553
1554 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1555 ++vxlan->dev->stats.rx_frame_errors;
1556 ++vxlan->dev->stats.rx_errors;
1557 goto drop;
1558 }
1559
1560 stats = this_cpu_ptr(vxlan->dev->tstats);
1561 u64_stats_update_begin(&stats->syncp);
1562 stats->rx_packets++;
1563 stats->rx_bytes += skb->len;
1564 u64_stats_update_end(&stats->syncp);
1565
1566 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001567 return 0;
1568
1569drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001570 /* Consume bad packet */
1571 kfree_skb(skb);
1572 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001573}
1574
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001575/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1576static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1577{
1578 struct vxlan_dev *vxlan;
1579 struct vxlan_sock *vs;
1580 struct vxlanhdr *hdr;
1581 __be32 vni;
1582
1583 if (skb->len < VXLAN_HLEN)
1584 return -EINVAL;
1585
1586 hdr = vxlan_hdr(skb);
1587
1588 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1589 return -EINVAL;
1590
1591 vs = rcu_dereference_sk_user_data(sk);
1592 if (!vs)
1593 return -ENOENT;
1594
1595 vni = vxlan_vni(hdr->vx_vni);
1596 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1597 if (!vxlan)
1598 return -ENOENT;
1599
1600 return 0;
1601}
1602
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001603static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001604{
1605 struct vxlan_dev *vxlan = netdev_priv(dev);
1606 struct arphdr *parp;
1607 u8 *arpptr, *sha;
1608 __be32 sip, tip;
1609 struct neighbour *n;
1610
1611 if (dev->flags & IFF_NOARP)
1612 goto out;
1613
1614 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1615 dev->stats.tx_dropped++;
1616 goto out;
1617 }
1618 parp = arp_hdr(skb);
1619
1620 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1621 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1622 parp->ar_pro != htons(ETH_P_IP) ||
1623 parp->ar_op != htons(ARPOP_REQUEST) ||
1624 parp->ar_hln != dev->addr_len ||
1625 parp->ar_pln != 4)
1626 goto out;
1627 arpptr = (u8 *)parp + sizeof(struct arphdr);
1628 sha = arpptr;
1629 arpptr += dev->addr_len; /* sha */
1630 memcpy(&sip, arpptr, sizeof(sip));
1631 arpptr += sizeof(sip);
1632 arpptr += dev->addr_len; /* tha */
1633 memcpy(&tip, arpptr, sizeof(tip));
1634
1635 if (ipv4_is_loopback(tip) ||
1636 ipv4_is_multicast(tip))
1637 goto out;
1638
1639 n = neigh_lookup(&arp_tbl, &tip, dev);
1640
1641 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001642 struct vxlan_fdb *f;
1643 struct sk_buff *reply;
1644
1645 if (!(n->nud_state & NUD_CONNECTED)) {
1646 neigh_release(n);
1647 goto out;
1648 }
1649
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001650 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08001651 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001652 /* bridge-local neighbor */
1653 neigh_release(n);
1654 goto out;
1655 }
1656
1657 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1658 n->ha, sha);
1659
1660 neigh_release(n);
1661
David Stevens73461352014-03-18 12:32:29 -04001662 if (reply == NULL)
1663 goto out;
1664
David Stevense4f67ad2012-11-20 02:50:14 +00001665 skb_reset_mac_header(reply);
1666 __skb_pull(reply, skb_network_offset(reply));
1667 reply->ip_summed = CHECKSUM_UNNECESSARY;
1668 reply->pkt_type = PACKET_HOST;
1669
1670 if (netif_rx_ni(reply) == NET_RX_DROP)
1671 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001672 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001673 union vxlan_addr ipa = {
1674 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001675 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001676 };
1677
1678 vxlan_ip_miss(dev, &ipa);
1679 }
David Stevense4f67ad2012-11-20 02:50:14 +00001680out:
1681 consume_skb(skb);
1682 return NETDEV_TX_OK;
1683}
1684
Cong Wangf564f452013-08-31 13:44:36 +08001685#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001686static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1687 struct neighbour *n, bool isrouter)
1688{
1689 struct net_device *dev = request->dev;
1690 struct sk_buff *reply;
1691 struct nd_msg *ns, *na;
1692 struct ipv6hdr *pip6;
1693 u8 *daddr;
1694 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1695 int ns_olen;
1696 int i, len;
1697
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001698 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04001699 return NULL;
1700
1701 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1702 sizeof(*na) + na_olen + dev->needed_tailroom;
1703 reply = alloc_skb(len, GFP_ATOMIC);
1704 if (reply == NULL)
1705 return NULL;
1706
1707 reply->protocol = htons(ETH_P_IPV6);
1708 reply->dev = dev;
1709 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1710 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001711 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001712
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001713 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04001714
1715 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001716 ns_olen = request->len - skb_network_offset(request) -
1717 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04001718 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1719 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1720 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1721 break;
1722 }
1723 }
1724
1725 /* Ethernet header */
1726 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1727 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1728 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1729 reply->protocol = htons(ETH_P_IPV6);
1730
1731 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001732 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001733 skb_put(reply, sizeof(struct ipv6hdr));
1734
1735 /* IPv6 header */
1736
1737 pip6 = ipv6_hdr(reply);
1738 memset(pip6, 0, sizeof(struct ipv6hdr));
1739 pip6->version = 6;
1740 pip6->priority = ipv6_hdr(request)->priority;
1741 pip6->nexthdr = IPPROTO_ICMPV6;
1742 pip6->hop_limit = 255;
1743 pip6->daddr = ipv6_hdr(request)->saddr;
1744 pip6->saddr = *(struct in6_addr *)n->primary_key;
1745
1746 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001747 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001748
David Stevens4b29dba2014-03-24 10:39:58 -04001749 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02001750 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04001751 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1752 na->icmph.icmp6_router = isrouter;
1753 na->icmph.icmp6_override = 1;
1754 na->icmph.icmp6_solicited = 1;
1755 na->target = ns->target;
1756 ether_addr_copy(&na->opt[2], n->ha);
1757 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1758 na->opt[1] = na_olen >> 3;
1759
1760 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1761 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1762 csum_partial(na, sizeof(*na)+na_olen, 0));
1763
1764 pip6->payload_len = htons(sizeof(*na)+na_olen);
1765
1766 skb_push(reply, sizeof(struct ipv6hdr));
1767
1768 reply->ip_summed = CHECKSUM_UNNECESSARY;
1769
1770 return reply;
1771}
1772
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001773static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08001774{
1775 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08001776 const struct in6_addr *daddr;
Xin Long8bff36852017-11-11 19:58:50 +08001777 const struct ipv6hdr *iphdr;
David Stevens4b29dba2014-03-24 10:39:58 -04001778 struct inet6_dev *in6_dev;
Xin Long8bff36852017-11-11 19:58:50 +08001779 struct neighbour *n;
1780 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001781
1782 in6_dev = __in6_dev_get(dev);
1783 if (!in6_dev)
1784 goto out;
1785
Cong Wangf564f452013-08-31 13:44:36 +08001786 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001787 daddr = &iphdr->daddr;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001788 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08001789
David Stevens4b29dba2014-03-24 10:39:58 -04001790 if (ipv6_addr_loopback(daddr) ||
1791 ipv6_addr_is_multicast(&msg->target))
1792 goto out;
1793
1794 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001795
1796 if (n) {
1797 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001798 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001799
1800 if (!(n->nud_state & NUD_CONNECTED)) {
1801 neigh_release(n);
1802 goto out;
1803 }
1804
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001805 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08001806 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1807 /* bridge-local neighbor */
1808 neigh_release(n);
1809 goto out;
1810 }
1811
David Stevens4b29dba2014-03-24 10:39:58 -04001812 reply = vxlan_na_create(skb, n,
1813 !!(f ? f->flags & NTF_ROUTER : 0));
1814
Cong Wangf564f452013-08-31 13:44:36 +08001815 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001816
1817 if (reply == NULL)
1818 goto out;
1819
1820 if (netif_rx_ni(reply) == NET_RX_DROP)
1821 dev->stats.rx_dropped++;
1822
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001823 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001824 union vxlan_addr ipa = {
1825 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001826 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001827 };
1828
Cong Wangf564f452013-08-31 13:44:36 +08001829 vxlan_ip_miss(dev, &ipa);
1830 }
1831
1832out:
1833 consume_skb(skb);
1834 return NETDEV_TX_OK;
1835}
1836#endif
1837
David Stevense4f67ad2012-11-20 02:50:14 +00001838static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1839{
1840 struct vxlan_dev *vxlan = netdev_priv(dev);
1841 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001842
1843 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1844 return false;
1845
1846 n = NULL;
1847 switch (ntohs(eth_hdr(skb)->h_proto)) {
1848 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001849 {
1850 struct iphdr *pip;
1851
David Stevense4f67ad2012-11-20 02:50:14 +00001852 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1853 return false;
1854 pip = ip_hdr(skb);
1855 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001856 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001857 union vxlan_addr ipa = {
1858 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001859 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001860 };
1861
1862 vxlan_ip_miss(dev, &ipa);
1863 return false;
1864 }
1865
David Stevense4f67ad2012-11-20 02:50:14 +00001866 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001867 }
1868#if IS_ENABLED(CONFIG_IPV6)
1869 case ETH_P_IPV6:
1870 {
1871 struct ipv6hdr *pip6;
1872
1873 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1874 return false;
1875 pip6 = ipv6_hdr(skb);
1876 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001877 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08001878 union vxlan_addr ipa = {
1879 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001880 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001881 };
1882
1883 vxlan_ip_miss(dev, &ipa);
1884 return false;
1885 }
1886
1887 break;
1888 }
1889#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001890 default:
1891 return false;
1892 }
1893
1894 if (n) {
1895 bool diff;
1896
Joe Perches7367d0b2013-09-01 11:51:23 -07001897 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001898 if (diff) {
1899 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1900 dev->addr_len);
1901 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1902 }
1903 neigh_release(n);
1904 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001905 }
1906
David Stevense4f67ad2012-11-20 02:50:14 +00001907 return false;
1908}
1909
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001910static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001911 struct vxlan_metadata *md)
1912{
1913 struct vxlanhdr_gbp *gbp;
1914
Thomas Grafdb79a622015-02-04 17:00:04 +01001915 if (!md->gbp)
1916 return;
1917
Thomas Graf35114942015-01-15 03:53:55 +01001918 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001919 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001920
1921 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1922 gbp->dont_learn = 1;
1923
1924 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1925 gbp->policy_applied = 1;
1926
1927 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1928}
1929
Jiri Bence1e53142016-04-05 14:47:13 +02001930static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1931 __be16 protocol)
1932{
1933 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1934
1935 gpe->np_applied = 1;
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001936 gpe->next_protocol = tun_p_from_eth_p(protocol);
1937 if (!gpe->next_protocol)
1938 return -EPFNOSUPPORT;
1939 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02001940}
1941
Jiri Bencf491e562016-02-02 18:09:16 +01001942static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1943 int iphdr_len, __be32 vni,
1944 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001945 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001946{
Cong Wange4c7ed42013-08-31 13:44:33 +08001947 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001948 int min_headroom;
1949 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001950 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001951 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001952
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001953 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001954 skb->ip_summed == CHECKSUM_PARTIAL) {
1955 int csum_start = skb_checksum_start_offset(skb);
1956
1957 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1958 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1959 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001960 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001961 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001962 }
1963
Cong Wange4c7ed42013-08-31 13:44:33 +08001964 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08001965 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001966
1967 /* Need space for new headers (invalidates iph ptr) */
1968 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001969 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08001970 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001971
Alexander Duyckaed069d2016-04-14 15:33:37 -04001972 err = iptunnel_handle_offloads(skb, type);
1973 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08001974 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07001975
Johannes Bergd58ff352017-06-16 14:29:23 +02001976 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001977 vxh->vx_flags = VXLAN_HF_VNI;
1978 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001979
Tom Herbertdfd86452015-01-12 17:00:38 -08001980 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001981 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001982
Jiri Benc54bfd872016-02-16 21:58:58 +01001983 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1984 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1985 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001986
1987 if (!skb_is_gso(skb)) {
1988 skb->ip_summed = CHECKSUM_NONE;
1989 skb->encapsulation = 0;
1990 }
1991 }
1992
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001993 if (vxflags & VXLAN_F_GBP)
1994 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001995 if (vxflags & VXLAN_F_GPE) {
1996 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1997 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08001998 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02001999 inner_protocol = skb->protocol;
2000 }
Thomas Graf35114942015-01-15 03:53:55 +01002001
Jiri Bence1e53142016-04-05 14:47:13 +02002002 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08002003 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07002004}
Pravin B Shelar49560532013-08-19 11:23:17 -07002005
pravin shelar655c3de2016-11-13 20:43:55 -08002006static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2007 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002008 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002009 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002010 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002011 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01002012{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002013 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002014 struct rtable *rt = NULL;
2015 struct flowi4 fl4;
2016
pravin shelar655c3de2016-11-13 20:43:55 -08002017 if (!sock4)
2018 return ERR_PTR(-EIO);
2019
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002020 if (tos && !info)
2021 use_cache = false;
2022 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002023 rt = dst_cache_get_ip4(dst_cache, saddr);
2024 if (rt)
2025 return rt;
2026 }
2027
Jiri Benc1a8496b2016-02-02 18:09:14 +01002028 memset(&fl4, 0, sizeof(fl4));
2029 fl4.flowi4_oif = oif;
2030 fl4.flowi4_tos = RT_TOS(tos);
2031 fl4.flowi4_mark = skb->mark;
2032 fl4.flowi4_proto = IPPROTO_UDP;
2033 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002034 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002035 fl4.fl4_dport = dport;
2036 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002037
2038 rt = ip_route_output_key(vxlan->net, &fl4);
pravin shelar655c3de2016-11-13 20:43:55 -08002039 if (likely(!IS_ERR(rt))) {
2040 if (rt->dst.dev == dev) {
2041 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2042 ip_rt_put(rt);
2043 return ERR_PTR(-ELOOP);
2044 }
2045
Jiri Benc1a8496b2016-02-02 18:09:14 +01002046 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002047 if (use_cache)
2048 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08002049 } else {
2050 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2051 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002052 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002053 return rt;
2054}
2055
Jiri Bence5d4b292015-12-07 13:04:30 +01002056#if IS_ENABLED(CONFIG_IPV6)
2057static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08002058 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08002059 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01002060 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002061 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01002062 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002063 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002064 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002065 struct dst_cache *dst_cache,
2066 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01002067{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002068 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002069 struct dst_entry *ndst;
2070 struct flowi6 fl6;
2071 int err;
2072
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002073 if (!sock6)
2074 return ERR_PTR(-EIO);
2075
Daniel Borkmann14006152016-03-04 15:15:08 +01002076 if (tos && !info)
2077 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002078 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002079 ndst = dst_cache_get_ip6(dst_cache, saddr);
2080 if (ndst)
2081 return ndst;
2082 }
2083
Jiri Bence5d4b292015-12-07 13:04:30 +01002084 memset(&fl6, 0, sizeof(fl6));
2085 fl6.flowi6_oif = oif;
2086 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002087 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01002088 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01002089 fl6.flowi6_mark = skb->mark;
2090 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002091 fl6.fl6_dport = dport;
2092 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01002093
2094 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002095 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01002096 &ndst, &fl6);
pravin shelar655c3de2016-11-13 20:43:55 -08002097 if (unlikely(err < 0)) {
2098 netdev_dbg(dev, "no route to %pI6\n", daddr);
2099 return ERR_PTR(-ENETUNREACH);
2100 }
2101
2102 if (unlikely(ndst->dev == dev)) {
2103 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2104 dst_release(ndst);
2105 return ERR_PTR(-ELOOP);
2106 }
Jiri Bence5d4b292015-12-07 13:04:30 +01002107
2108 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002109 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002110 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01002111 return ndst;
2112}
2113#endif
2114
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002115/* Bypass encapsulation if the destination is local */
2116static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002117 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002118{
Li RongQing8f849852014-01-04 13:57:59 +08002119 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08002120 union vxlan_addr loopback;
2121 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08002122 struct net_device *dev = skb->dev;
2123 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002124
Li RongQing8f849852014-01-04 13:57:59 +08002125 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2126 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002127 skb->pkt_type = PACKET_HOST;
2128 skb->encapsulation = 0;
2129 skb->dev = dst_vxlan->dev;
2130 __skb_pull(skb, skb_network_offset(skb));
2131
Cong Wange4c7ed42013-08-31 13:44:33 +08002132 if (remote_ip->sa.sa_family == AF_INET) {
2133 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2134 loopback.sa.sa_family = AF_INET;
2135#if IS_ENABLED(CONFIG_IPV6)
2136 } else {
2137 loopback.sin6.sin6_addr = in6addr_loopback;
2138 loopback.sa.sa_family = AF_INET6;
2139#endif
2140 }
2141
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002142 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
Matthias Schiffer87613de2017-06-19 10:03:59 +02002143 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0,
2144 vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002145
2146 u64_stats_update_begin(&tx_stats->syncp);
2147 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002148 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002149 u64_stats_update_end(&tx_stats->syncp);
2150
2151 if (netif_rx(skb) == NET_RX_SUCCESS) {
2152 u64_stats_update_begin(&rx_stats->syncp);
2153 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002154 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002155 u64_stats_update_end(&rx_stats->syncp);
2156 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08002157 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002158 }
2159}
2160
pravin shelarfee1fad2016-11-13 20:43:56 -08002161static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002162 struct vxlan_dev *vxlan,
2163 union vxlan_addr *daddr,
2164 __be16 dst_port, int dst_ifindex, __be32 vni,
2165 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002166 u32 rt_flags)
2167{
2168#if IS_ENABLED(CONFIG_IPV6)
2169 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2170 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2171 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2172 */
2173 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2174#endif
2175 /* Bypass encapsulation if the destination is local */
2176 if (rt_flags & RTCF_LOCAL &&
2177 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2178 struct vxlan_dev *dst_vxlan;
2179
2180 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002181 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002182 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002183 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002184 if (!dst_vxlan) {
2185 dev->stats.tx_errors++;
2186 kfree_skb(skb);
2187
2188 return -ENOENT;
2189 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002190 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002191 return 1;
2192 }
2193
2194 return 0;
2195}
2196
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002197static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002198 __be32 default_vni, struct vxlan_rdst *rdst,
2199 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002200{
Paolo Abenid71785f2016-02-12 15:43:57 +01002201 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002202 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002203 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002204 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002205 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002206 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002207 struct vxlan_metadata _md;
2208 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002209 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002210 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002211 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002212 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002213 int ifindex;
Pravin B Shelar0e6fbc5b2013-06-17 17:49:56 -07002214 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002215 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002216 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002217 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002218
Jiri Benc61adedf2015-08-20 13:56:25 +02002219 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002220
Thomas Grafee122c72015-07-21 10:43:58 +02002221 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002222 dst = &rdst->remote_ip;
2223 if (vxlan_addr_any(dst)) {
2224 if (did_rsc) {
2225 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002226 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002227 return;
2228 }
2229 goto drop;
2230 }
2231
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002232 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002233 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002234 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002235 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002236 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002237 md->gbp = skb->mark;
Hangbin Liu72f6d712018-04-17 14:11:28 +08002238 if (flags & VXLAN_F_TTL_INHERIT) {
2239 ttl = ip_tunnel_get_ttl(old_iph, skb);
2240 } else {
2241 ttl = vxlan->cfg.ttl;
2242 if (!ttl && vxlan_addr_multicast(dst))
2243 ttl = 1;
2244 }
pravin shelar0770b532016-11-13 20:43:57 -08002245
2246 tos = vxlan->cfg.tos;
2247 if (tos == 1)
2248 tos = ip_tunnel_get_dsfield(old_iph, skb);
2249
2250 if (dst->sa.sa_family == AF_INET)
2251 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2252 else
2253 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2254 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002255 } else {
2256 if (!info) {
2257 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2258 dev->name);
2259 goto drop;
2260 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002261 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002262 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002263 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002264 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2265 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002266 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002267 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2268 }
Thomas Grafee122c72015-07-21 10:43:58 +02002269 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002270 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2271 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002272 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002273 dst_cache = &info->dst_cache;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002274 if (info->options_len &&
2275 info->key.tun_flags & TUNNEL_VXLAN_OPT)
pravin shelar0770b532016-11-13 20:43:57 -08002276 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002277 ttl = info->key.ttl;
2278 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002279 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002280 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002281 }
pravin shelar0770b532016-11-13 20:43:57 -08002282 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2283 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002284
Jakub Kicinski56de8592017-02-24 11:43:36 -08002285 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002286 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002287 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002288 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002289 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002290
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002291 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002292 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002293 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002294 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002295 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002296 if (IS_ERR(rt)) {
2297 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002298 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002299 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002300
pravin shelarfee1fad2016-11-13 20:43:56 -08002301 if (!info) {
Stefano Briviob4d306972018-11-08 12:19:16 +01002302 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002303 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002304 dst_port, ifindex, vni,
2305 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002306 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002307 goto out_unlock;
Stefano Briviob4d306972018-11-08 12:19:16 +01002308
2309 if (vxlan->cfg.df == VXLAN_DF_SET) {
2310 df = htons(IP_DF);
2311 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2312 struct ethhdr *eth = eth_hdr(skb);
2313
2314 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2315 (ntohs(eth->h_proto) == ETH_P_IP &&
2316 old_iph->frag_off & htons(IP_DF)))
2317 df = htons(IP_DF);
2318 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002319 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002320 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002321 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002322
pravin shelarc46b7892016-11-13 20:43:54 -08002323 ndst = &rt->dst;
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002324 skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002325
Cong Wange4c7ed42013-08-31 13:44:33 +08002326 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2327 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002328 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002329 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002330 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002331 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002332
Brian Russell11586322017-02-24 17:47:11 +00002333 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002334 dst->sin.sin_addr.s_addr, tos, ttl, df,
2335 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002336#if IS_ENABLED(CONFIG_IPV6)
2337 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002338 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002339
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002340 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002341 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002342 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002343 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002344 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002345 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002346 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002347 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002348 goto tx_error;
2349 }
pravin shelar655c3de2016-11-13 20:43:55 -08002350
pravin shelarfee1fad2016-11-13 20:43:56 -08002351 if (!info) {
2352 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002353
pravin shelarfee1fad2016-11-13 20:43:56 -08002354 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002355 dst_port, ifindex, vni,
2356 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002357 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002358 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002359 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002360
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002361 skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002362
Daniel Borkmann14006152016-03-04 15:15:08 +01002363 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002364 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002365 skb_scrub_packet(skb, xnet);
2366 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002367 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002368 if (err < 0)
2369 goto tx_error;
2370
pravin shelar0770b532016-11-13 20:43:57 -08002371 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002372 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002373 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002374 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002375#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002376 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002377out_unlock:
2378 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002379 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002380
2381drop:
2382 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002383 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002384 return;
2385
2386tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002387 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002388 if (err == -ELOOP)
2389 dev->stats.collisions++;
2390 else if (err == -ENETUNREACH)
2391 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002392 dst_release(ndst);
2393 dev->stats.tx_errors++;
2394 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002395}
2396
David Stevens66817122013-03-15 04:35:51 +00002397/* Transmit local packets over Vxlan
2398 *
2399 * Outer IP header inherits ECN and DF from inner header.
2400 * Outer UDP destination is the VXLAN assigned port.
2401 * source port is based on hash of flow
2402 */
2403static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2404{
2405 struct vxlan_dev *vxlan = netdev_priv(dev);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002406 struct vxlan_rdst *rdst, *fdst = NULL;
Xin Long8bff36852017-11-11 19:58:50 +08002407 const struct ip_tunnel_info *info;
2408 bool did_rsc = false;
David Stevens66817122013-03-15 04:35:51 +00002409 struct vxlan_fdb *f;
Xin Long8bff36852017-11-11 19:58:50 +08002410 struct ethhdr *eth;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002411 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002412
Jiri Benc61adedf2015-08-20 13:56:25 +02002413 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002414
David Stevens66817122013-03-15 04:35:51 +00002415 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002416
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002417 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002418 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2419 info->mode & IP_TUNNEL_INFO_TX) {
2420 vni = tunnel_id_to_key32(info->key.tun_id);
2421 } else {
2422 if (info && info->mode & IP_TUNNEL_INFO_TX)
2423 vxlan_xmit_one(skb, dev, vni, NULL, false);
2424 else
2425 kfree_skb(skb);
2426 return NETDEV_TX_OK;
2427 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002428 }
2429
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002430 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002431 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002432 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002433 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002434#if IS_ENABLED(CONFIG_IPV6)
Xin Long8bff36852017-11-11 19:58:50 +08002435 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2436 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2437 sizeof(struct nd_msg)) &&
2438 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2439 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2440
2441 if (m->icmph.icmp6_code == 0 &&
2442 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002443 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002444 }
2445#endif
2446 }
David Stevens66817122013-03-15 04:35:51 +00002447
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002448 eth = eth_hdr(skb);
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 did_rsc = false;
2451
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002452 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002453 (ntohs(eth->h_proto) == ETH_P_IP ||
2454 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002455 did_rsc = route_shortcircuit(dev, skb);
2456 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002457 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002458 }
2459
David Stevens66817122013-03-15 04:35:51 +00002460 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002461 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002462 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002463 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002464 !is_multicast_ether_addr(eth->h_dest))
2465 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002466
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002467 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002468 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002469 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002470 }
David Stevens66817122013-03-15 04:35:51 +00002471 }
2472
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002473 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2474 struct sk_buff *skb1;
2475
Eric Dumazet8f646c92014-01-06 09:54:31 -08002476 if (!fdst) {
2477 fdst = rdst;
2478 continue;
2479 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002480 skb1 = skb_clone(skb, GFP_ATOMIC);
2481 if (skb1)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002482 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002483 }
2484
Eric Dumazet8f646c92014-01-06 09:54:31 -08002485 if (fdst)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002486 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002487 else
2488 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002489 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002490}
2491
stephen hemmingerd3428942012-10-01 12:32:35 +00002492/* Walk the forwarding table and purge stale entries */
Kees Cookdf7e8282017-10-04 16:26:59 -07002493static void vxlan_cleanup(struct timer_list *t)
stephen hemmingerd3428942012-10-01 12:32:35 +00002494{
Kees Cookdf7e8282017-10-04 16:26:59 -07002495 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
stephen hemmingerd3428942012-10-01 12:32:35 +00002496 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2497 unsigned int h;
2498
2499 if (!netif_running(vxlan->dev))
2500 return;
2501
stephen hemmingerd3428942012-10-01 12:32:35 +00002502 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2503 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002504
2505 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002506 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2507 struct vxlan_fdb *f
2508 = container_of(p, struct vxlan_fdb, hlist);
2509 unsigned long timeout;
2510
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002511 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002512 continue;
2513
Roopa Prabhudef499c2017-03-27 15:46:41 -07002514 if (f->flags & NTF_EXT_LEARNED)
2515 continue;
2516
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002517 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002518 if (time_before_eq(timeout, jiffies)) {
2519 netdev_dbg(vxlan->dev,
2520 "garbage collect %pM\n",
2521 f->eth_addr);
2522 f->state = NUD_STALE;
Petr Machata0e6160f2018-11-21 08:02:35 +00002523 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002524 } else if (time_before(timeout, next_timer))
2525 next_timer = timeout;
2526 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002527 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002528 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002529
2530 mod_timer(&vxlan->age_timer, next_timer);
2531}
2532
Mark Blocha53cb292017-06-02 03:24:08 +03002533static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2534{
2535 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2536
2537 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002538 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2539#if IS_ENABLED(CONFIG_IPV6)
2540 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2541#endif
Mark Blocha53cb292017-06-02 03:24:08 +03002542 spin_unlock(&vn->sock_lock);
2543}
2544
Jiri Benc69e76662017-07-02 19:00:57 +02002545static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2546 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002547{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002548 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002549 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002550
Jiri Benc69e76662017-07-02 19:00:57 +02002551 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002552 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002553 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002554 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002555}
2556
stephen hemmingerd3428942012-10-01 12:32:35 +00002557/* Setup stats when device is created */
2558static int vxlan_init(struct net_device *dev)
2559{
WANG Cong1c213bd2014-02-13 11:46:28 -08002560 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002561 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002562 return -ENOMEM;
2563
2564 return 0;
2565}
2566
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002567static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002568{
2569 struct vxlan_fdb *f;
2570
2571 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002572 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002573 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00002574 vxlan_fdb_destroy(vxlan, f, true, true);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002575 spin_unlock_bh(&vxlan->hash_lock);
2576}
2577
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002578static void vxlan_uninit(struct net_device *dev)
2579{
2580 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002581
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002582 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002583
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002584 free_percpu(dev->tstats);
2585}
2586
stephen hemmingerd3428942012-10-01 12:32:35 +00002587/* Start ageing timer and join group when device is brought up */
2588static int vxlan_open(struct net_device *dev)
2589{
2590 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002591 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002592
Jiri Benc205f3562015-09-24 13:50:01 +02002593 ret = vxlan_sock_add(vxlan);
2594 if (ret < 0)
2595 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002596
Gao feng79d4a942013-12-10 16:37:32 +08002597 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002598 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002599 if (ret == -EADDRINUSE)
2600 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002601 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002602 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002603 return ret;
2604 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002605 }
2606
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002607 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002608 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2609
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002610 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002611}
2612
2613/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002614static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00002615{
Cong Wang31fec5a2013-05-27 22:35:52 +00002616 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002617
2618 spin_lock_bh(&vxlan->hash_lock);
2619 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2620 struct hlist_node *p, *n;
2621 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2622 struct vxlan_fdb *f
2623 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002624 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2625 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002626 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2627 if (!is_zero_ether_addr(f->eth_addr))
Petr Machata0e6160f2018-11-21 08:02:35 +00002628 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002629 }
2630 }
2631 spin_unlock_bh(&vxlan->hash_lock);
2632}
2633
2634/* Cleanup timer and forwarding table on shutdown */
2635static int vxlan_stop(struct net_device *dev)
2636{
2637 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002638 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002639 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002640
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002641 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002642 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002643 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002644
2645 del_timer_sync(&vxlan->age_timer);
2646
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002647 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02002648 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002649
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002650 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002651}
2652
stephen hemmingerd3428942012-10-01 12:32:35 +00002653/* Stub, nothing needs to be done. */
2654static void vxlan_set_multicast_list(struct net_device *dev)
2655{
2656}
2657
Daniel Borkmann345010b2013-12-18 00:21:08 +01002658static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2659{
2660 struct vxlan_dev *vxlan = netdev_priv(dev);
2661 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002662 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2663 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002664 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04002665
2666 /* This check is different than dev->max_mtu, because it looks at
2667 * the lowerdev->mtu, rather than the static dev->max_mtu
2668 */
2669 if (lowerdev) {
2670 int max_mtu = lowerdev->mtu -
2671 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2672 if (new_mtu > max_mtu)
2673 return -EINVAL;
2674 }
2675
2676 dev->mtu = new_mtu;
2677 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01002678}
2679
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002680static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2681{
2682 struct vxlan_dev *vxlan = netdev_priv(dev);
2683 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2684 __be16 sport, dport;
2685
2686 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2687 vxlan->cfg.port_max, true);
2688 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2689
Jiri Benc239e9442015-12-07 13:04:31 +01002690 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002691 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002692 struct rtable *rt;
2693
pravin shelar655c3de2016-11-13 20:43:55 -08002694 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002695 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002696 &info->key.u.ipv4.src, dport, sport,
2697 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002698 if (IS_ERR(rt))
2699 return PTR_ERR(rt);
2700 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002701 } else {
2702#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08002703 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01002704 struct dst_entry *ndst;
2705
pravin shelar655c3de2016-11-13 20:43:55 -08002706 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002707 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002708 &info->key.u.ipv6.src, dport, sport,
2709 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002710 if (IS_ERR(ndst))
2711 return PTR_ERR(ndst);
2712 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002713#else /* !CONFIG_IPV6 */
2714 return -EPFNOSUPPORT;
2715#endif
2716 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002717 info->key.tp_src = sport;
2718 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002719 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002720}
2721
Jiri Benc0c867c92016-04-05 14:47:10 +02002722static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002723 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002724 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002725 .ndo_open = vxlan_open,
2726 .ndo_stop = vxlan_stop,
2727 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002728 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002729 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002730 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002731 .ndo_validate_addr = eth_validate_addr,
2732 .ndo_set_mac_address = eth_mac_addr,
2733 .ndo_fdb_add = vxlan_fdb_add,
2734 .ndo_fdb_del = vxlan_fdb_delete,
2735 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002736 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002737};
2738
Jiri Bence1e53142016-04-05 14:47:13 +02002739static const struct net_device_ops vxlan_netdev_raw_ops = {
2740 .ndo_init = vxlan_init,
2741 .ndo_uninit = vxlan_uninit,
2742 .ndo_open = vxlan_open,
2743 .ndo_stop = vxlan_stop,
2744 .ndo_start_xmit = vxlan_xmit,
2745 .ndo_get_stats64 = ip_tunnel_get_stats64,
2746 .ndo_change_mtu = vxlan_change_mtu,
2747 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2748};
2749
stephen hemmingerd3428942012-10-01 12:32:35 +00002750/* Info for udev, that this is a virtual tunnel endpoint */
2751static struct device_type vxlan_type = {
2752 .name = "vxlan",
2753};
2754
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002755/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002756 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002757 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002758 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002759static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002760{
2761 struct vxlan_sock *vs;
2762 struct net *net = dev_net(dev);
2763 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002764 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002765
2766 spin_lock(&vn->sock_lock);
2767 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002768 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2769 unsigned short type;
2770
2771 if (vs->flags & VXLAN_F_GPE)
2772 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
2773 else
2774 type = UDP_TUNNEL_TYPE_VXLAN;
2775
2776 if (push)
2777 udp_tunnel_push_rx_port(dev, vs->sock, type);
2778 else
2779 udp_tunnel_drop_rx_port(dev, vs->sock, type);
2780 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002781 }
2782 spin_unlock(&vn->sock_lock);
2783}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002784
stephen hemmingerd3428942012-10-01 12:32:35 +00002785/* Initialize the device structure. */
2786static void vxlan_setup(struct net_device *dev)
2787{
2788 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002789 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002790
Jiri Benc65226ef2016-04-28 16:36:30 +02002791 eth_hw_addr_random(dev);
2792 ether_setup(dev);
2793
David S. Millercf124db2017-05-08 12:52:56 -04002794 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00002795 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2796
stephen hemmingerd3428942012-10-01 12:32:35 +00002797 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002798 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002799 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002800 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002801
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002802 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002803 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002804 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07002805 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002806 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002807
Matthias Schiffera9853432017-06-19 10:03:55 +02002808 /* MTU range: 68 - 65535 */
2809 dev->min_mtu = ETH_MIN_MTU;
2810 dev->max_mtu = ETH_MAX_MTU;
2811
stephen hemminger553675f2013-05-16 11:35:20 +00002812 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002813 spin_lock_init(&vxlan->hash_lock);
2814
Kees Cookdf7e8282017-10-04 16:26:59 -07002815 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
stephen hemmingerd3428942012-10-01 12:32:35 +00002816
2817 vxlan->dev = dev;
2818
Tom Herbert58ce31c2015-08-19 17:07:33 -07002819 gro_cells_init(&vxlan->gro_cells, dev);
2820
stephen hemmingerd3428942012-10-01 12:32:35 +00002821 for (h = 0; h < FDB_HASH_SIZE; ++h)
2822 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2823}
2824
Jiri Benc0c867c92016-04-05 14:47:10 +02002825static void vxlan_ether_setup(struct net_device *dev)
2826{
Jiri Benc0c867c92016-04-05 14:47:10 +02002827 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2828 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2829 dev->netdev_ops = &vxlan_netdev_ether_ops;
2830}
2831
Jiri Bence1e53142016-04-05 14:47:13 +02002832static void vxlan_raw_setup(struct net_device *dev)
2833{
Jiri Benc65226ef2016-04-28 16:36:30 +02002834 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002835 dev->type = ARPHRD_NONE;
2836 dev->hard_header_len = 0;
2837 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002838 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2839 dev->netdev_ops = &vxlan_netdev_raw_ops;
2840}
2841
stephen hemmingerd3428942012-10-01 12:32:35 +00002842static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2843 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002844 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002845 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002846 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2847 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002848 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002849 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2850 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002851 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002852 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2853 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2854 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002855 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002856 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2857 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2858 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2859 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002860 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002861 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002862 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2863 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2864 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002865 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2866 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002867 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002868 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002869 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
Hangbin Liu72f6d712018-04-17 14:11:28 +08002870 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
Stefano Briviob4d306972018-11-08 12:19:16 +01002871 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002872};
2873
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002874static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
2875 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00002876{
2877 if (tb[IFLA_ADDRESS]) {
2878 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002879 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
2880 "Provided link layer address is not Ethernet");
stephen hemmingerd3428942012-10-01 12:32:35 +00002881 return -EINVAL;
2882 }
2883
2884 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002885 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
2886 "Provided Ethernet address is not unicast");
stephen hemmingerd3428942012-10-01 12:32:35 +00002887 return -EADDRNOTAVAIL;
2888 }
2889 }
2890
Matthias Schiffera9853432017-06-19 10:03:55 +02002891 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02002892 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02002893
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002894 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
2895 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
2896 "MTU must be between 68 and 65535");
Matthias Schiffera9853432017-06-19 10:03:55 +02002897 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002898 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002899 }
2900
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002901 if (!data) {
2902 NL_SET_ERR_MSG(extack,
2903 "Required attributes not provided to perform the operation");
stephen hemmingerd3428942012-10-01 12:32:35 +00002904 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002905 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002906
2907 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02002908 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2909
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002910 if (id >= VXLAN_N_VID) {
2911 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID],
2912 "VXLAN ID must be lower than 16777216");
stephen hemmingerd3428942012-10-01 12:32:35 +00002913 return -ERANGE;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002914 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002915 }
2916
stephen hemminger05f47d62012-10-09 20:35:50 +00002917 if (data[IFLA_VXLAN_PORT_RANGE]) {
2918 const struct ifla_vxlan_port_range *p
2919 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2920
2921 if (ntohs(p->high) < ntohs(p->low)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002922 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
2923 "Invalid source port range");
stephen hemminger05f47d62012-10-09 20:35:50 +00002924 return -EINVAL;
2925 }
2926 }
2927
Stefano Briviob4d306972018-11-08 12:19:16 +01002928 if (data[IFLA_VXLAN_DF]) {
2929 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
2930
2931 if (df < 0 || df > VXLAN_DF_MAX) {
2932 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_DF],
2933 "Invalid DF attribute");
2934 return -EINVAL;
2935 }
2936 }
2937
stephen hemmingerd3428942012-10-01 12:32:35 +00002938 return 0;
2939}
2940
Yan Burman1b13c972013-01-29 23:43:07 +00002941static void vxlan_get_drvinfo(struct net_device *netdev,
2942 struct ethtool_drvinfo *drvinfo)
2943{
2944 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2945 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2946}
2947
2948static const struct ethtool_ops vxlan_ethtool_ops = {
2949 .get_drvinfo = vxlan_get_drvinfo,
2950 .get_link = ethtool_op_get_link,
2951};
2952
Tom Herbert3ee64f32014-07-13 19:49:42 -07002953static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2954 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002955{
Cong Wange4c7ed42013-08-31 13:44:33 +08002956 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002957 struct udp_port_cfg udp_conf;
2958 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002959
Tom Herbert3ee64f32014-07-13 19:49:42 -07002960 memset(&udp_conf, 0, sizeof(udp_conf));
2961
2962 if (ipv6) {
2963 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002964 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002965 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002966 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002967 } else {
2968 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002969 }
2970
Tom Herbert3ee64f32014-07-13 19:49:42 -07002971 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002972
Tom Herbert3ee64f32014-07-13 19:49:42 -07002973 /* Open UDP socket */
2974 err = udp_sock_create(net, &udp_conf, &sock);
2975 if (err < 0)
2976 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002977
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002978 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002979}
2980
2981/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002982static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2983 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002984{
2985 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2986 struct vxlan_sock *vs;
2987 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002988 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002989 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002990
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002991 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002992 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002993 return ERR_PTR(-ENOMEM);
2994
2995 for (h = 0; h < VNI_HASH_SIZE; ++h)
2996 INIT_HLIST_HEAD(&vs->vni_list[h]);
2997
Tom Herbert3ee64f32014-07-13 19:49:42 -07002998 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002999 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00003000 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08003001 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00003002 }
3003
Cong Wange4c7ed42013-08-31 13:44:33 +08003004 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03003005 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08003006 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00003007
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003008 spin_lock(&vn->sock_lock);
3009 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07003010 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07003011 (vs->flags & VXLAN_F_GPE) ?
3012 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07003013 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003014 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00003015
3016 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07003017 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07003018 tunnel_cfg.sk_user_data = vs;
3019 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01003020 tunnel_cfg.encap_rcv = vxlan_rcv;
Stefano Brivioc3a43b92018-11-08 12:19:15 +01003021 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003022 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07003023 tunnel_cfg.gro_receive = vxlan_gro_receive;
3024 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003025
3026 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08003027
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003028 return vs;
3029}
stephen hemminger553675f2013-05-16 11:35:20 +00003030
Jiri Bencb1be00a2015-09-24 13:50:02 +02003031static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003032{
Jiri Benc205f3562015-09-24 13:50:01 +02003033 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3034 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02003035 struct vxlan_dev_node *node;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003036
Jiri Benc205f3562015-09-24 13:50:01 +02003037 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003038 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003039 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003040 vxlan->cfg.dst_port, vxlan->cfg.flags);
Reshetova, Elena66af8462017-07-04 15:52:59 +03003041 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003042 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003043 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003044 }
3045 spin_unlock(&vn->sock_lock);
3046 }
Jiri Benc205f3562015-09-24 13:50:01 +02003047 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003048 vs = vxlan_socket_create(vxlan->net, ipv6,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003049 vxlan->cfg.dst_port, vxlan->cfg.flags);
Jiri Benc205f3562015-09-24 13:50:01 +02003050 if (IS_ERR(vs))
3051 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003052#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02003053 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003054 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003055 node = &vxlan->hlist6;
3056 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02003057#endif
Jiri Benc69e76662017-07-02 19:00:57 +02003058 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003059 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003060 node = &vxlan->hlist4;
3061 }
3062 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02003063 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00003064}
3065
Jiri Bencb1be00a2015-09-24 13:50:02 +02003066static int vxlan_sock_add(struct vxlan_dev *vxlan)
3067{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003068 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3069 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02003070 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003071 int ret = 0;
3072
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003073 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003074#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003075 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02003076 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02003077 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02003078 if (ret < 0 && ret != -EAFNOSUPPORT)
3079 ipv4 = false;
3080 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02003081#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02003082 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003083 ret = __vxlan_sock_add(vxlan, false);
3084 if (ret < 0)
3085 vxlan_sock_release(vxlan);
3086 return ret;
3087}
3088
Matthias Schiffera9853432017-06-19 10:03:55 +02003089static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3090 struct net_device **lower,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003091 struct vxlan_dev *old,
3092 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003093{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01003094 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02003095 struct vxlan_dev *tmp;
3096 bool use_ipv6 = false;
3097
3098 if (conf->flags & VXLAN_F_GPE) {
3099 /* For now, allow GPE only together with
3100 * COLLECT_METADATA. This can be relaxed later; in such
3101 * case, the other side of the PtP link will have to be
3102 * provided.
3103 */
3104 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3105 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003106 NL_SET_ERR_MSG(extack,
3107 "VXLAN GPE does not support this combination of attributes");
Matthias Schiffera9853432017-06-19 10:03:55 +02003108 return -EINVAL;
3109 }
3110 }
3111
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003112 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3113 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02003114 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003115 conf->saddr.sa.sa_family = AF_INET;
3116 } else if (!conf->remote_ip.sa.sa_family) {
3117 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3118 } else if (!conf->saddr.sa.sa_family) {
3119 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3120 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003121
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003122 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3123 NL_SET_ERR_MSG(extack,
3124 "Local and remote address must be from the same family");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003125 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003126 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003127
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003128 if (vxlan_addr_multicast(&conf->saddr)) {
3129 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003130 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003131 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003132
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003133 if (conf->saddr.sa.sa_family == AF_INET6) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003134 if (!IS_ENABLED(CONFIG_IPV6)) {
3135 NL_SET_ERR_MSG(extack,
3136 "IPv6 support not enabled in the kernel");
Matthias Schiffera9853432017-06-19 10:03:55 +02003137 return -EPFNOSUPPORT;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003138 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003139 use_ipv6 = true;
3140 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003141
3142 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3143 int local_type =
3144 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3145 int remote_type =
3146 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3147
3148 if (local_type & IPV6_ADDR_LINKLOCAL) {
3149 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003150 (remote_type != IPV6_ADDR_ANY)) {
3151 NL_SET_ERR_MSG(extack,
3152 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003153 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003154 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003155
3156 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3157 } else {
3158 if (remote_type ==
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003159 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3160 NL_SET_ERR_MSG(extack,
3161 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003162 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003163 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003164
3165 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3166 }
3167 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003168 }
3169
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003170 if (conf->label && !use_ipv6) {
3171 NL_SET_ERR_MSG(extack,
3172 "Label attribute only applies to IPv6 VXLAN devices");
Matthias Schiffera9853432017-06-19 10:03:55 +02003173 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003174 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003175
3176 if (conf->remote_ifindex) {
3177 struct net_device *lowerdev;
3178
3179 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003180 if (!lowerdev) {
3181 NL_SET_ERR_MSG(extack,
3182 "Invalid local interface, device not found");
Matthias Schiffera9853432017-06-19 10:03:55 +02003183 return -ENODEV;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003184 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003185
3186#if IS_ENABLED(CONFIG_IPV6)
3187 if (use_ipv6) {
3188 struct inet6_dev *idev = __in6_dev_get(lowerdev);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003189 if (idev && idev->cnf.disable_ipv6) {
3190 NL_SET_ERR_MSG(extack,
3191 "IPv6 support disabled by administrator");
Matthias Schiffera9853432017-06-19 10:03:55 +02003192 return -EPERM;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003193 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003194 }
3195#endif
3196
3197 *lower = lowerdev;
3198 } else {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003199 if (vxlan_addr_multicast(&conf->remote_ip)) {
3200 NL_SET_ERR_MSG(extack,
3201 "Local interface required for multicast remote destination");
3202
Matthias Schiffera9853432017-06-19 10:03:55 +02003203 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003204 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003205
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003206#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003207 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3208 NL_SET_ERR_MSG(extack,
3209 "Local interface required for link-local local/remote addresses");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003210 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003211 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003212#endif
3213
Matthias Schiffera9853432017-06-19 10:03:55 +02003214 *lower = NULL;
3215 }
3216
3217 if (!conf->dst_port) {
3218 if (conf->flags & VXLAN_F_GPE)
3219 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3220 else
3221 conf->dst_port = htons(vxlan_port);
3222 }
3223
3224 if (!conf->age_interval)
3225 conf->age_interval = FDB_AGE_DEFAULT;
3226
3227 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3228 if (tmp == old)
3229 continue;
3230
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003231 if (tmp->cfg.vni != conf->vni)
3232 continue;
3233 if (tmp->cfg.dst_port != conf->dst_port)
3234 continue;
3235 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003236 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003237 continue;
3238
3239 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3240 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3241 continue;
3242
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003243 NL_SET_ERR_MSG(extack,
3244 "A VXLAN device with the specified VNI already exists");
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003245 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003246 }
3247
3248 return 0;
3249}
3250
3251static void vxlan_config_apply(struct net_device *dev,
3252 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003253 struct net_device *lowerdev,
3254 struct net *src_net,
3255 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003256{
3257 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003258 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003259 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003260 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3261 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003262
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003263 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003264 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003265 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003266 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003267 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003268
Matthias Schiffera9853432017-06-19 10:03:55 +02003269 if (conf->mtu)
3270 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003271
3272 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003273 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003274
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003275 dst->remote_vni = conf->vni;
3276
3277 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003278
Matthias Schiffera9853432017-06-19 10:03:55 +02003279 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003280 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003281
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003282 dev->gso_max_size = lowerdev->gso_max_size;
3283 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003284
3285 needed_headroom = lowerdev->hard_header_len;
3286
3287 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3288 VXLAN_HEADROOM);
Alexey Kodanevf870c1f2017-12-14 20:20:00 +03003289 if (max_mtu < ETH_MIN_MTU)
3290 max_mtu = ETH_MIN_MTU;
3291
3292 if (!changelink && !conf->mtu)
3293 dev->mtu = max_mtu;
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003294 }
3295
Matthias Schiffera9853432017-06-19 10:03:55 +02003296 if (dev->mtu > max_mtu)
3297 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003298
Jiri Bencb1be00a2015-09-24 13:50:02 +02003299 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3300 needed_headroom += VXLAN6_HEADROOM;
3301 else
3302 needed_headroom += VXLAN_HEADROOM;
3303 dev->needed_headroom = needed_headroom;
3304
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003305 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003306}
stephen hemmingerd3428942012-10-01 12:32:35 +00003307
Matthias Schiffera9853432017-06-19 10:03:55 +02003308static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003309 struct vxlan_config *conf, bool changelink,
3310 struct netlink_ext_ack *extack)
Matthias Schiffera9853432017-06-19 10:03:55 +02003311{
3312 struct vxlan_dev *vxlan = netdev_priv(dev);
3313 struct net_device *lowerdev;
3314 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003315
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003316 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
Matthias Schiffera9853432017-06-19 10:03:55 +02003317 if (ret)
3318 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003319
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003320 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003321
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003322 return 0;
3323}
3324
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003325static int __vxlan_dev_create(struct net *net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003326 struct vxlan_config *conf,
3327 struct netlink_ext_ack *extack)
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003328{
3329 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3330 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003331 struct vxlan_fdb *f = NULL;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003332 int err;
3333
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003334 err = vxlan_dev_configure(net, dev, conf, false, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003335 if (err)
3336 return err;
3337
3338 dev->ethtool_ops = &vxlan_ethtool_ops;
3339
3340 /* create an fdb entry for a valid default destination */
3341 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003342 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003343 &vxlan->default_dst.remote_ip,
3344 NUD_REACHABLE | NUD_PERMANENT,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003345 vxlan->cfg.dst_port,
3346 vxlan->default_dst.remote_vni,
3347 vxlan->default_dst.remote_vni,
3348 vxlan->default_dst.remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003349 NTF_SELF, &f);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003350 if (err)
3351 return err;
3352 }
3353
3354 err = register_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003355 if (err)
3356 goto errout;
3357
3358 err = rtnl_configure_link(dev, NULL);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003359 if (err) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003360 unregister_netdevice(dev);
3361 goto errout;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003362 }
3363
Roopa Prabhu0241b832018-07-04 16:46:32 -07003364 /* notify default fdb entry */
3365 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003366 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
3367 true);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003368
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003369 list_add(&vxlan->next, &vn->vxlan_list);
3370 return 0;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003371errout:
3372 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003373 vxlan_fdb_destroy(vxlan, f, false, false);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003374 return err;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003375}
3376
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003377static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3378 struct net_device *dev, struct vxlan_config *conf,
3379 bool changelink)
3380{
3381 struct vxlan_dev *vxlan = netdev_priv(dev);
3382
3383 memset(conf, 0, sizeof(*conf));
3384
3385 /* if changelink operation, start with old existing cfg */
3386 if (changelink)
3387 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3388
3389 if (data[IFLA_VXLAN_ID]) {
3390 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3391
3392 if (changelink && (vni != conf->vni))
3393 return -EOPNOTSUPP;
3394 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3395 }
3396
3397 if (data[IFLA_VXLAN_GROUP]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003398 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET))
3399 return -EOPNOTSUPP;
3400
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003401 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003402 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003403 } else if (data[IFLA_VXLAN_GROUP6]) {
3404 if (!IS_ENABLED(CONFIG_IPV6))
3405 return -EPFNOSUPPORT;
3406
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003407 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6))
3408 return -EOPNOTSUPP;
3409
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003410 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3411 conf->remote_ip.sa.sa_family = AF_INET6;
3412 }
3413
3414 if (data[IFLA_VXLAN_LOCAL]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003415 if (changelink && (conf->saddr.sa.sa_family != AF_INET))
3416 return -EOPNOTSUPP;
3417
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003418 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3419 conf->saddr.sa.sa_family = AF_INET;
3420 } else if (data[IFLA_VXLAN_LOCAL6]) {
3421 if (!IS_ENABLED(CONFIG_IPV6))
3422 return -EPFNOSUPPORT;
3423
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003424 if (changelink && (conf->saddr.sa.sa_family != AF_INET6))
3425 return -EOPNOTSUPP;
3426
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003427 /* TODO: respect scope id */
3428 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3429 conf->saddr.sa.sa_family = AF_INET6;
3430 }
3431
3432 if (data[IFLA_VXLAN_LINK])
3433 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3434
3435 if (data[IFLA_VXLAN_TOS])
3436 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3437
3438 if (data[IFLA_VXLAN_TTL])
3439 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3440
Hangbin Liu72f6d712018-04-17 14:11:28 +08003441 if (data[IFLA_VXLAN_TTL_INHERIT]) {
3442 if (changelink)
3443 return -EOPNOTSUPP;
3444 conf->flags |= VXLAN_F_TTL_INHERIT;
3445 }
3446
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003447 if (data[IFLA_VXLAN_LABEL])
3448 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3449 IPV6_FLOWLABEL_MASK;
3450
3451 if (data[IFLA_VXLAN_LEARNING]) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003452 if (nla_get_u8(data[IFLA_VXLAN_LEARNING]))
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003453 conf->flags |= VXLAN_F_LEARN;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003454 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003455 conf->flags &= ~VXLAN_F_LEARN;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003456 } else if (!changelink) {
3457 /* default to learn on a new device */
3458 conf->flags |= VXLAN_F_LEARN;
3459 }
3460
Ido Schimmel40051c42018-11-21 08:02:40 +00003461 if (data[IFLA_VXLAN_AGEING])
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003462 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003463
3464 if (data[IFLA_VXLAN_PROXY]) {
3465 if (changelink)
3466 return -EOPNOTSUPP;
3467 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3468 conf->flags |= VXLAN_F_PROXY;
3469 }
3470
3471 if (data[IFLA_VXLAN_RSC]) {
3472 if (changelink)
3473 return -EOPNOTSUPP;
3474 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3475 conf->flags |= VXLAN_F_RSC;
3476 }
3477
3478 if (data[IFLA_VXLAN_L2MISS]) {
3479 if (changelink)
3480 return -EOPNOTSUPP;
3481 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3482 conf->flags |= VXLAN_F_L2MISS;
3483 }
3484
3485 if (data[IFLA_VXLAN_L3MISS]) {
3486 if (changelink)
3487 return -EOPNOTSUPP;
3488 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3489 conf->flags |= VXLAN_F_L3MISS;
3490 }
3491
3492 if (data[IFLA_VXLAN_LIMIT]) {
3493 if (changelink)
3494 return -EOPNOTSUPP;
3495 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3496 }
3497
3498 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3499 if (changelink)
3500 return -EOPNOTSUPP;
3501 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3502 conf->flags |= VXLAN_F_COLLECT_METADATA;
3503 }
3504
3505 if (data[IFLA_VXLAN_PORT_RANGE]) {
3506 if (!changelink) {
3507 const struct ifla_vxlan_port_range *p
3508 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3509 conf->port_min = ntohs(p->low);
3510 conf->port_max = ntohs(p->high);
3511 } else {
3512 return -EOPNOTSUPP;
3513 }
3514 }
3515
3516 if (data[IFLA_VXLAN_PORT]) {
3517 if (changelink)
3518 return -EOPNOTSUPP;
3519 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3520 }
3521
3522 if (data[IFLA_VXLAN_UDP_CSUM]) {
3523 if (changelink)
3524 return -EOPNOTSUPP;
3525 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3526 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3527 }
3528
3529 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3530 if (changelink)
3531 return -EOPNOTSUPP;
3532 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3533 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3534 }
3535
3536 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3537 if (changelink)
3538 return -EOPNOTSUPP;
3539 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3540 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3541 }
3542
3543 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3544 if (changelink)
3545 return -EOPNOTSUPP;
3546 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3547 conf->flags |= VXLAN_F_REMCSUM_TX;
3548 }
3549
3550 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3551 if (changelink)
3552 return -EOPNOTSUPP;
3553 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3554 conf->flags |= VXLAN_F_REMCSUM_RX;
3555 }
3556
3557 if (data[IFLA_VXLAN_GBP]) {
3558 if (changelink)
3559 return -EOPNOTSUPP;
3560 conf->flags |= VXLAN_F_GBP;
3561 }
3562
3563 if (data[IFLA_VXLAN_GPE]) {
3564 if (changelink)
3565 return -EOPNOTSUPP;
3566 conf->flags |= VXLAN_F_GPE;
3567 }
3568
3569 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3570 if (changelink)
3571 return -EOPNOTSUPP;
3572 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3573 }
3574
3575 if (tb[IFLA_MTU]) {
3576 if (changelink)
3577 return -EOPNOTSUPP;
3578 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3579 }
3580
Stefano Briviob4d306972018-11-08 12:19:16 +01003581 if (data[IFLA_VXLAN_DF])
3582 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
3583
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003584 return 0;
3585}
3586
3587static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003588 struct nlattr *tb[], struct nlattr *data[],
3589 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003590{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003591 struct vxlan_config conf;
3592 int err;
3593
3594 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3595 if (err)
3596 return err;
3597
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003598 return __vxlan_dev_create(src_net, dev, &conf, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00003599}
3600
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003601static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02003602 struct nlattr *data[],
3603 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003604{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003605 struct vxlan_dev *vxlan = netdev_priv(dev);
3606 struct vxlan_rdst *dst = &vxlan->default_dst;
Ido Schimmel40051c42018-11-21 08:02:40 +00003607 unsigned long old_age_interval;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003608 struct vxlan_rdst old_dst;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003609 struct vxlan_config conf;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003610 struct vxlan_fdb *f = NULL;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003611 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003612
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003613 err = vxlan_nl2conf(tb, data,
3614 dev, &conf, true);
3615 if (err)
3616 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07003617
Ido Schimmel40051c42018-11-21 08:02:40 +00003618 old_age_interval = vxlan->cfg.age_interval;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003619 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003620
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003621 err = vxlan_dev_configure(vxlan->net, dev, &conf, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003622 if (err)
3623 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003624
Ido Schimmel40051c42018-11-21 08:02:40 +00003625 if (old_age_interval != vxlan->cfg.age_interval)
3626 mod_timer(&vxlan->age_timer, jiffies);
3627
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003628 /* handle default dst entry */
3629 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3630 spin_lock_bh(&vxlan->hash_lock);
3631 if (!vxlan_addr_any(&old_dst.remote_ip))
3632 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3633 old_dst.remote_ip,
3634 vxlan->cfg.dst_port,
3635 old_dst.remote_vni,
3636 old_dst.remote_vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00003637 old_dst.remote_ifindex,
3638 true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003639
3640 if (!vxlan_addr_any(&dst->remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003641 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003642 &dst->remote_ip,
3643 NUD_REACHABLE | NUD_PERMANENT,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003644 vxlan->cfg.dst_port,
3645 dst->remote_vni,
3646 dst->remote_vni,
3647 dst->remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003648 NTF_SELF, &f);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003649 if (err) {
3650 spin_unlock_bh(&vxlan->hash_lock);
3651 return err;
3652 }
Petr Machata0e6160f2018-11-21 08:02:35 +00003653 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3654 RTM_NEWNEIGH, true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003655 }
3656 spin_unlock_bh(&vxlan->hash_lock);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003657 }
3658
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003659 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003660}
3661
stephen hemmingerd3428942012-10-01 12:32:35 +00003662static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3663{
3664 struct vxlan_dev *vxlan = netdev_priv(dev);
3665
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003666 vxlan_flush(vxlan, true);
3667
Tom Herbert58ce31c2015-08-19 17:07:33 -07003668 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003669 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003670 unregister_netdevice_queue(dev, head);
3671}
3672
3673static size_t vxlan_get_size(const struct net_device *dev)
3674{
3675
3676 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003677 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003678 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003679 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003680 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
Hangbin Liu8fd78062018-09-26 10:35:42 +08003681 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
stephen hemmingerd3428942012-10-01 12:32:35 +00003682 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Stefano Briviob4d306972018-11-08 12:19:16 +01003683 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003684 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003685 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003686 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3687 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3688 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3689 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003690 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003691 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3692 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003693 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003694 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3695 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3696 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3697 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003698 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3699 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003700 0;
3701}
3702
3703static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3704{
3705 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003706 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003707 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003708 .low = htons(vxlan->cfg.port_min),
3709 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003710 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003711
Jiri Benc54bfd872016-02-16 21:58:58 +01003712 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003713 goto nla_put_failure;
3714
Cong Wange4c7ed42013-08-31 13:44:33 +08003715 if (!vxlan_addr_any(&dst->remote_ip)) {
3716 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003717 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3718 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003719 goto nla_put_failure;
3720#if IS_ENABLED(CONFIG_IPV6)
3721 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003722 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3723 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003724 goto nla_put_failure;
3725#endif
3726 }
3727 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003728
Atzm Watanabec7995c42013-04-16 02:50:52 +00003729 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003730 goto nla_put_failure;
3731
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003732 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3733 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003734 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003735 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003736 goto nla_put_failure;
3737#if IS_ENABLED(CONFIG_IPV6)
3738 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003739 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003740 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003741 goto nla_put_failure;
3742#endif
3743 }
3744 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003745
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003746 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
Hangbin Liu8fd78062018-09-26 10:35:42 +08003747 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
3748 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003749 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Stefano Briviob4d306972018-11-08 12:19:16 +01003750 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003751 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003752 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003753 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003754 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003755 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
3756 nla_put_u8(skb, IFLA_VXLAN_RSC,
3757 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003758 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003759 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003760 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003761 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003762 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003763 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003764 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3765 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3766 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003767 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003768 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003769 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003770 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003771 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003772 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003773 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003774 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003775 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003776 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003777 goto nla_put_failure;
3778
stephen hemminger05f47d62012-10-09 20:35:50 +00003779 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3780 goto nla_put_failure;
3781
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003782 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01003783 nla_put_flag(skb, IFLA_VXLAN_GBP))
3784 goto nla_put_failure;
3785
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003786 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02003787 nla_put_flag(skb, IFLA_VXLAN_GPE))
3788 goto nla_put_failure;
3789
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003790 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003791 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3792 goto nla_put_failure;
3793
stephen hemmingerd3428942012-10-01 12:32:35 +00003794 return 0;
3795
3796nla_put_failure:
3797 return -EMSGSIZE;
3798}
3799
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003800static struct net *vxlan_get_link_net(const struct net_device *dev)
3801{
3802 struct vxlan_dev *vxlan = netdev_priv(dev);
3803
3804 return vxlan->net;
3805}
3806
stephen hemmingerd3428942012-10-01 12:32:35 +00003807static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3808 .kind = "vxlan",
3809 .maxtype = IFLA_VXLAN_MAX,
3810 .policy = vxlan_policy,
3811 .priv_size = sizeof(struct vxlan_dev),
3812 .setup = vxlan_setup,
3813 .validate = vxlan_validate,
3814 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003815 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00003816 .dellink = vxlan_dellink,
3817 .get_size = vxlan_get_size,
3818 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003819 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003820};
3821
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003822struct net_device *vxlan_dev_create(struct net *net, const char *name,
3823 u8 name_assign_type,
3824 struct vxlan_config *conf)
3825{
3826 struct nlattr *tb[IFLA_MAX + 1];
3827 struct net_device *dev;
3828 int err;
3829
3830 memset(&tb, 0, sizeof(tb));
3831
3832 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08003833 &vxlan_link_ops, tb, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003834 if (IS_ERR(dev))
3835 return dev;
3836
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003837 err = __vxlan_dev_create(net, dev, conf, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003838 if (err < 0) {
3839 free_netdev(dev);
3840 return ERR_PTR(err);
3841 }
3842
3843 err = rtnl_configure_link(dev, NULL);
3844 if (err < 0) {
3845 LIST_HEAD(list_kill);
3846
3847 vxlan_dellink(dev, &list_kill);
3848 unregister_netdevice_many(&list_kill);
3849 return ERR_PTR(err);
3850 }
3851
3852 return dev;
3853}
3854EXPORT_SYMBOL_GPL(vxlan_dev_create);
3855
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003856static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3857 struct net_device *dev)
3858{
3859 struct vxlan_dev *vxlan, *next;
3860 LIST_HEAD(list_kill);
3861
3862 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3863 struct vxlan_rdst *dst = &vxlan->default_dst;
3864
3865 /* In case we created vxlan device with carrier
3866 * and we loose the carrier due to module unload
3867 * we also need to remove vxlan device. In other
3868 * cases, it's not necessary and remote_ifindex
3869 * is 0 here, so no matches.
3870 */
3871 if (dst->remote_ifindex == dev->ifindex)
3872 vxlan_dellink(vxlan->dev, &list_kill);
3873 }
3874
3875 unregister_netdevice_many(&list_kill);
3876}
3877
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003878static int vxlan_netdevice_event(struct notifier_block *unused,
3879 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003880{
3881 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003882 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003883
Sabrina Dubroca04584952017-07-21 12:49:33 +02003884 if (event == NETDEV_UNREGISTER) {
3885 vxlan_offload_rx_ports(dev, false);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003886 vxlan_handle_lowerdev_unregister(vn, dev);
Sabrina Dubroca04584952017-07-21 12:49:33 +02003887 } else if (event == NETDEV_REGISTER) {
3888 vxlan_offload_rx_ports(dev, true);
3889 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
3890 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003891 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02003892 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003893
3894 return NOTIFY_DONE;
3895}
3896
3897static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003898 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003899};
3900
Petr Machata0efe1172018-10-17 08:53:26 +00003901static void
3902vxlan_fdb_offloaded_set(struct net_device *dev,
3903 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
3904{
3905 struct vxlan_dev *vxlan = netdev_priv(dev);
3906 struct vxlan_rdst *rdst;
3907 struct vxlan_fdb *f;
3908
3909 spin_lock_bh(&vxlan->hash_lock);
3910
3911 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
3912 if (!f)
3913 goto out;
3914
3915 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
3916 fdb_info->remote_port,
3917 fdb_info->remote_vni,
3918 fdb_info->remote_ifindex);
3919 if (!rdst)
3920 goto out;
3921
3922 rdst->offloaded = fdb_info->offloaded;
3923
3924out:
3925 spin_unlock_bh(&vxlan->hash_lock);
3926}
3927
Petr Machata5728ae02018-11-21 08:02:39 +00003928static int
3929vxlan_fdb_external_learn_add(struct net_device *dev,
3930 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
3931{
3932 struct vxlan_dev *vxlan = netdev_priv(dev);
3933 int err;
3934
3935 spin_lock_bh(&vxlan->hash_lock);
3936 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
3937 NUD_REACHABLE,
3938 NLM_F_CREATE | NLM_F_REPLACE,
3939 fdb_info->remote_port,
3940 fdb_info->vni,
3941 fdb_info->remote_vni,
3942 fdb_info->remote_ifindex,
3943 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
3944 false);
3945 spin_unlock_bh(&vxlan->hash_lock);
3946
3947 return err;
3948}
3949
3950static int
3951vxlan_fdb_external_learn_del(struct net_device *dev,
3952 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
3953{
3954 struct vxlan_dev *vxlan = netdev_priv(dev);
3955 struct vxlan_fdb *f;
3956 int err = 0;
3957
3958 spin_lock_bh(&vxlan->hash_lock);
3959
3960 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
3961 if (!f)
3962 err = -ENOENT;
3963 else if (f->flags & NTF_EXT_LEARNED)
3964 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
3965 fdb_info->remote_ip,
3966 fdb_info->remote_port,
3967 fdb_info->vni,
3968 fdb_info->remote_vni,
3969 fdb_info->remote_ifindex,
3970 false);
3971
3972 spin_unlock_bh(&vxlan->hash_lock);
3973
3974 return err;
3975}
3976
Petr Machata0efe1172018-10-17 08:53:26 +00003977static int vxlan_switchdev_event(struct notifier_block *unused,
3978 unsigned long event, void *ptr)
3979{
3980 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Petr Machata5728ae02018-11-21 08:02:39 +00003981 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
3982 int err = 0;
Petr Machata0efe1172018-10-17 08:53:26 +00003983
3984 switch (event) {
3985 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
3986 vxlan_fdb_offloaded_set(dev, ptr);
3987 break;
Petr Machata5728ae02018-11-21 08:02:39 +00003988 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
3989 fdb_info = ptr;
3990 err = vxlan_fdb_external_learn_add(dev, fdb_info);
3991 if (err) {
3992 err = notifier_from_errno(err);
3993 break;
3994 }
3995 fdb_info->offloaded = true;
3996 vxlan_fdb_offloaded_set(dev, fdb_info);
3997 break;
3998 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
3999 fdb_info = ptr;
4000 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4001 if (err) {
4002 err = notifier_from_errno(err);
4003 break;
4004 }
4005 fdb_info->offloaded = false;
4006 vxlan_fdb_offloaded_set(dev, fdb_info);
4007 break;
Petr Machata0efe1172018-10-17 08:53:26 +00004008 }
4009
Petr Machata5728ae02018-11-21 08:02:39 +00004010 return err;
Petr Machata0efe1172018-10-17 08:53:26 +00004011}
4012
4013static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4014 .notifier_call = vxlan_switchdev_event,
4015};
4016
stephen hemmingerd3428942012-10-01 12:32:35 +00004017static __net_init int vxlan_init_net(struct net *net)
4018{
4019 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00004020 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00004021
stephen hemminger553675f2013-05-16 11:35:20 +00004022 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07004023 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00004024
stephen hemminger553675f2013-05-16 11:35:20 +00004025 for (h = 0; h < PORT_HASH_SIZE; ++h)
4026 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00004027
4028 return 0;
4029}
4030
Haishuang Yan57b61122017-12-16 17:54:49 +08004031static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004032{
4033 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4034 struct vxlan_dev *vxlan, *next;
4035 struct net_device *dev, *aux;
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004036 unsigned int h;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004037
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004038 for_each_netdev_safe(net, dev, aux)
4039 if (dev->rtnl_link_ops == &vxlan_link_ops)
Haishuang Yan57b61122017-12-16 17:54:49 +08004040 unregister_netdevice_queue(dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004041
4042 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4043 /* If vxlan->dev is in the same netns, it has already been added
4044 * to the list by the previous loop.
4045 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07004046 if (!net_eq(dev_net(vxlan->dev), net)) {
4047 gro_cells_destroy(&vxlan->gro_cells);
Haishuang Yan57b61122017-12-16 17:54:49 +08004048 unregister_netdevice_queue(vxlan->dev, head);
Tom Herbert58ce31c2015-08-19 17:07:33 -07004049 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004050 }
4051
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004052 for (h = 0; h < PORT_HASH_SIZE; ++h)
4053 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004054}
4055
Haishuang Yan57b61122017-12-16 17:54:49 +08004056static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4057{
4058 struct net *net;
4059 LIST_HEAD(list);
4060
4061 rtnl_lock();
4062 list_for_each_entry(net, net_list, exit_list)
4063 vxlan_destroy_tunnels(net, &list);
4064
4065 unregister_netdevice_many(&list);
4066 rtnl_unlock();
4067}
4068
stephen hemmingerd3428942012-10-01 12:32:35 +00004069static struct pernet_operations vxlan_net_ops = {
4070 .init = vxlan_init_net,
Haishuang Yan57b61122017-12-16 17:54:49 +08004071 .exit_batch = vxlan_exit_batch_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004072 .id = &vxlan_net_id,
4073 .size = sizeof(struct vxlan_net),
4074};
4075
4076static int __init vxlan_init_module(void)
4077{
4078 int rc;
4079
4080 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4081
Daniel Borkmann783c1462014-01-22 21:07:53 +01004082 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004083 if (rc)
4084 goto out1;
4085
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004086 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004087 if (rc)
4088 goto out2;
4089
Petr Machata0efe1172018-10-17 08:53:26 +00004090 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004091 if (rc)
4092 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00004093
Petr Machata0efe1172018-10-17 08:53:26 +00004094 rc = rtnl_link_register(&vxlan_link_ops);
4095 if (rc)
4096 goto out4;
4097
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004098 return 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004099out4:
4100 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004101out3:
4102 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004103out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01004104 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004105out1:
4106 return rc;
4107}
Cong Wang7332a132013-05-27 22:35:53 +00004108late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00004109
4110static void __exit vxlan_cleanup_module(void)
4111{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07004112 rtnl_link_unregister(&vxlan_link_ops);
Petr Machata0efe1172018-10-17 08:53:26 +00004113 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004114 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004115 unregister_pernet_subsys(&vxlan_net_ops);
4116 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00004117}
4118module_exit(vxlan_cleanup_module);
4119
4120MODULE_LICENSE("GPL");
4121MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004122MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08004123MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00004124MODULE_ALIAS_RTNL_LINK("vxlan");