blob: ef22fa0ad4de25606dab80a84a7f298cdbbc9eea [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
stephen hemmingerd3428942012-10-01 12:32:35 +00002/*
Rami Roseneb5ce432012-11-13 13:29:15 +00003 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00004 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00005 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00006 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/kernel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000011#include <linux/module.h>
12#include <linux/errno.h>
13#include <linux/slab.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000014#include <linux/udp.h>
15#include <linux/igmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000016#include <linux/if_ether.h>
Yan Burman1b13c972013-01-29 23:43:07 +000017#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000018#include <net/arp.h>
19#include <net/ndisc.h>
David Ahern3616d082019-03-22 06:06:09 -070020#include <net/ipv6_stubs.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000021#include <net/ip.h>
22#include <net/icmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000023#include <net/rtnetlink.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000024#include <net/inet_ecn.h>
25#include <net/net_namespace.h>
26#include <net/netns/generic.h>
Jiri Bencfa20e0e2017-08-28 21:43:22 +020027#include <net/tun_proto.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070028#include <net/vxlan.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080029
Cong Wange4c7ed42013-08-31 13:44:33 +080030#if IS_ENABLED(CONFIG_IPV6)
Cong Wange4c7ed42013-08-31 13:44:33 +080031#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080032#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080033#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000034
35#define VXLAN_VERSION "0.1"
36
stephen hemminger553675f2013-05-16 11:35:20 +000037#define PORT_HASH_BITS 8
38#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000039#define FDB_AGE_DEFAULT 300 /* 5 min */
40#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
41
stephen hemminger23c578b2013-04-27 11:31:53 +000042/* UDP port for VXLAN traffic.
43 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070044 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000045 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070046static unsigned short vxlan_port __read_mostly = 8472;
47module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000048MODULE_PARM_DESC(udp_port, "Destination UDP port");
49
50static bool log_ecn_error = true;
51module_param(log_ecn_error, bool, 0644);
52MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
53
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030054static unsigned int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020055static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000056
Li RongQing7256eac2016-01-29 09:43:47 +080057static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030058
Jiri Benc205f3562015-09-24 13:50:01 +020059static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020060
Mark Blocha53cb292017-06-02 03:24:08 +030061static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
62
stephen hemminger553675f2013-05-16 11:35:20 +000063/* per-network namespace private data for this module */
64struct vxlan_net {
65 struct list_head vxlan_list;
66 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070067 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000068};
69
stephen hemmingerd3428942012-10-01 12:32:35 +000070/* Forwarding table entry */
71struct vxlan_fdb {
72 struct hlist_node hlist; /* linked list of entries */
73 struct rcu_head rcu;
74 unsigned long updated; /* jiffies */
75 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070076 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020077 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000078 u16 state; /* see ndm_state */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -080079 __be32 vni;
Petr Machata45598c12018-11-21 08:02:36 +000080 u16 flags; /* see ndm_flags and below */
stephen hemmingerd3428942012-10-01 12:32:35 +000081};
82
Petr Machata45598c12018-11-21 08:02:36 +000083#define NTF_VXLAN_ADDED_BY_USER 0x100
84
stephen hemmingerd3428942012-10-01 12:32:35 +000085/* salt for hash table */
86static u32 vxlan_salt __read_mostly;
87
Thomas Grafee122c72015-07-21 10:43:58 +020088static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
89{
Thomas Grafe7030872015-07-21 10:44:01 +020090 return vs->flags & VXLAN_F_COLLECT_METADATA ||
91 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020092}
93
Cong Wange4c7ed42013-08-31 13:44:33 +080094#if IS_ENABLED(CONFIG_IPV6)
95static inline
96bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
97{
Jiri Bencf0ef3122015-03-29 16:17:37 +020098 if (a->sa.sa_family != b->sa.sa_family)
99 return false;
100 if (a->sa.sa_family == AF_INET6)
101 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
102 else
103 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800104}
105
Cong Wange4c7ed42013-08-31 13:44:33 +0800106static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
107{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200108 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200109 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200110 ip->sa.sa_family = AF_INET6;
111 return 0;
112 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200113 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200114 ip->sa.sa_family = AF_INET;
115 return 0;
116 } else {
117 return -EAFNOSUPPORT;
118 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800119}
120
121static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200122 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800123{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200124 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200125 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200126 else
Jiri Benc930345e2015-03-29 16:59:25 +0200127 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800128}
129
130#else /* !CONFIG_IPV6 */
131
132static inline
133bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
134{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200135 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800136}
137
Cong Wange4c7ed42013-08-31 13:44:33 +0800138static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
139{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200140 if (nla_len(nla) >= sizeof(struct in6_addr)) {
141 return -EAFNOSUPPORT;
142 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200143 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200144 ip->sa.sa_family = AF_INET;
145 return 0;
146 } else {
147 return -EAFNOSUPPORT;
148 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800149}
150
151static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200152 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800153{
Jiri Benc930345e2015-03-29 16:59:25 +0200154 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800155}
156#endif
157
stephen hemminger553675f2013-05-16 11:35:20 +0000158/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100159static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000160{
Jiri Benc54bfd872016-02-16 21:58:58 +0100161 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000162}
163
164/* Socket hash table head */
165static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000166{
167 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
168
stephen hemminger553675f2013-05-16 11:35:20 +0000169 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
170}
171
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700172/* First remote destination for a forwarding entry.
173 * Guaranteed to be non-NULL because remotes are never deleted.
174 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700175static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700176{
stephen hemminger5ca54612013-08-04 17:17:39 -0700177 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
178}
179
180static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
181{
182 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700183}
184
Thomas Grafac5132d2015-01-15 03:53:56 +0100185/* Find VXLAN socket based on network namespace, address family and UDP port
186 * and enabled unshareable flags.
187 */
188static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100189 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000190{
191 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800192
193 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000194
195 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200196 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200197 vxlan_get_sk_family(vs) == family &&
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100198 vs->flags == flags &&
199 vs->sock->sk->sk_bound_dev_if == ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000200 return vs;
201 }
202 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000203}
204
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200205static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
206 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000207{
Jiri Benc69e76662017-07-02 19:00:57 +0200208 struct vxlan_dev_node *node;
stephen hemmingerd3428942012-10-01 12:32:35 +0000209
Jiri Bencb9167b22016-02-16 21:59:03 +0100210 /* For flow based devices, map all packets to VNI 0 */
211 if (vs->flags & VXLAN_F_COLLECT_METADATA)
212 vni = 0;
213
Jiri Benc69e76662017-07-02 19:00:57 +0200214 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
215 if (node->vxlan->default_dst.remote_vni != vni)
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200216 continue;
217
218 if (IS_ENABLED(CONFIG_IPV6)) {
Jiri Benc69e76662017-07-02 19:00:57 +0200219 const struct vxlan_config *cfg = &node->vxlan->cfg;
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200220
221 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
222 cfg->remote_ifindex != ifindex)
223 continue;
224 }
225
Jiri Benc69e76662017-07-02 19:00:57 +0200226 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000227 }
228
229 return NULL;
230}
231
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700232/* Look up VNI in a per net namespace table */
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200233static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
234 __be32 vni, sa_family_t family,
235 __be16 port, u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700236{
237 struct vxlan_sock *vs;
238
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100239 vs = vxlan_find_sock(net, family, port, flags, ifindex);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700240 if (!vs)
241 return NULL;
242
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200243 return vxlan_vs_find_vni(vs, ifindex, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700244}
245
stephen hemmingerd3428942012-10-01 12:32:35 +0000246/* Fill in neighbour message in skbuff. */
247static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700248 const struct vxlan_fdb *fdb,
249 u32 portid, u32 seq, int type, unsigned int flags,
250 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000251{
252 unsigned long now = jiffies;
253 struct nda_cacheinfo ci;
254 struct nlmsghdr *nlh;
255 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000256 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000257
258 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
259 if (nlh == NULL)
260 return -EMSGSIZE;
261
262 ndm = nlmsg_data(nlh);
263 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000264
265 send_eth = send_ip = true;
266
267 if (type == RTM_GETNEIGH) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800268 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000269 send_eth = !is_zero_ether_addr(fdb->eth_addr);
Vincent Bernat8f48ba72017-03-10 16:30:24 +0100270 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
David Stevense4f67ad2012-11-20 02:50:14 +0000271 } else
272 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000273 ndm->ndm_state = fdb->state;
274 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000275 ndm->ndm_flags = fdb->flags;
Petr Machata0efe1172018-10-17 08:53:26 +0000276 if (rdst->offloaded)
277 ndm->ndm_flags |= NTF_OFFLOADED;
Jun Zhao545469f2014-07-26 00:38:59 +0800278 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000279
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100280 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100281 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700282 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100283 goto nla_put_failure;
284
David Stevense4f67ad2012-11-20 02:50:14 +0000285 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000286 goto nla_put_failure;
287
Cong Wange4c7ed42013-08-31 13:44:33 +0800288 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000289 goto nla_put_failure;
290
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200291 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000292 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
293 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000294 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100295 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000296 goto nla_put_failure;
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200297 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800298 nla_put_u32(skb, NDA_SRC_VNI,
299 be32_to_cpu(fdb->vni)))
300 goto nla_put_failure;
David Stevens66817122013-03-15 04:35:51 +0000301 if (rdst->remote_ifindex &&
302 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000303 goto nla_put_failure;
304
305 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
306 ci.ndm_confirmed = 0;
307 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
308 ci.ndm_refcnt = 0;
309
310 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
311 goto nla_put_failure;
312
Johannes Berg053c0952015-01-16 22:09:00 +0100313 nlmsg_end(skb, nlh);
314 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000315
316nla_put_failure:
317 nlmsg_cancel(skb, nlh);
318 return -EMSGSIZE;
319}
320
321static inline size_t vxlan_nlmsg_size(void)
322{
323 return NLMSG_ALIGN(sizeof(struct ndmsg))
324 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800325 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000326 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000327 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
328 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100329 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000330 + nla_total_size(sizeof(struct nda_cacheinfo));
331}
332
Petr Machata9a997352018-10-17 08:53:22 +0000333static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
334 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000335{
336 struct net *net = dev_net(vxlan->dev);
337 struct sk_buff *skb;
338 int err = -ENOBUFS;
339
340 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
341 if (skb == NULL)
342 goto errout;
343
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200344 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000345 if (err < 0) {
346 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
347 WARN_ON(err == -EMSGSIZE);
348 kfree_skb(skb);
349 goto errout;
350 }
351
352 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
353 return;
354errout:
355 if (err < 0)
356 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
357}
358
Petr Machataff23b912018-12-07 19:55:03 +0000359static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
360 const struct vxlan_fdb *fdb,
361 const struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000362 struct netlink_ext_ack *extack,
Petr Machataff23b912018-12-07 19:55:03 +0000363 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
364{
365 fdb_info->info.dev = vxlan->dev;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000366 fdb_info->info.extack = extack;
Petr Machataff23b912018-12-07 19:55:03 +0000367 fdb_info->remote_ip = rd->remote_ip;
368 fdb_info->remote_port = rd->remote_port;
369 fdb_info->remote_vni = rd->remote_vni;
370 fdb_info->remote_ifindex = rd->remote_ifindex;
371 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
372 fdb_info->vni = fdb->vni;
373 fdb_info->offloaded = rd->offloaded;
374 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
375}
376
Petr Machata61f46fe2019-01-16 23:06:38 +0000377static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
378 struct vxlan_fdb *fdb,
379 struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000380 bool adding,
381 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000382{
383 struct switchdev_notifier_vxlan_fdb_info info;
384 enum switchdev_notifier_type notifier_type;
Petr Machata61f46fe2019-01-16 23:06:38 +0000385 int ret;
Petr Machata9a997352018-10-17 08:53:22 +0000386
387 if (WARN_ON(!rd))
Petr Machata61f46fe2019-01-16 23:06:38 +0000388 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000389
390 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
391 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000392 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
Petr Machata61f46fe2019-01-16 23:06:38 +0000393 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
Petr Machata66859872019-01-16 23:06:56 +0000394 &info.info, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000395 return notifier_to_errno(ret);
Petr Machata9a997352018-10-17 08:53:22 +0000396}
397
Petr Machata61f46fe2019-01-16 23:06:38 +0000398static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000399 struct vxlan_rdst *rd, int type, bool swdev_notify,
400 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000401{
Petr Machata61f46fe2019-01-16 23:06:38 +0000402 int err;
403
Petr Machata0e6160f2018-11-21 08:02:35 +0000404 if (swdev_notify) {
405 switch (type) {
406 case RTM_NEWNEIGH:
Petr Machata61f46fe2019-01-16 23:06:38 +0000407 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000408 true, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000409 if (err)
410 return err;
Petr Machata0e6160f2018-11-21 08:02:35 +0000411 break;
412 case RTM_DELNEIGH:
413 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000414 false, extack);
Petr Machata0e6160f2018-11-21 08:02:35 +0000415 break;
416 }
Petr Machata9a997352018-10-17 08:53:22 +0000417 }
418
419 __vxlan_fdb_notify(vxlan, fdb, rd, type);
Petr Machata61f46fe2019-01-16 23:06:38 +0000420 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000421}
422
Cong Wange4c7ed42013-08-31 13:44:33 +0800423static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000424{
425 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700426 struct vxlan_fdb f = {
427 .state = NUD_STALE,
428 };
429 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800430 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100431 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700432 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700433
Petr Machata4c59b7d2019-01-16 23:06:54 +0000434 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000435}
436
437static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
438{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700439 struct vxlan_fdb f = {
440 .state = NUD_STALE,
441 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200442 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000443
David Stevense4f67ad2012-11-20 02:50:14 +0000444 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
445
Petr Machata4c59b7d2019-01-16 23:06:54 +0000446 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000447}
448
stephen hemmingerd3428942012-10-01 12:32:35 +0000449/* Hash Ethernet address */
450static u32 eth_hash(const unsigned char *addr)
451{
452 u64 value = get_unaligned((u64 *)addr);
453
454 /* only want 6 bytes */
455#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000456 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000457#else
458 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000459#endif
460 return hash_64(value, FDB_HASH_BITS);
461}
462
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800463static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
464{
465 /* use 1 byte of OUI and 3 bytes of NIC */
466 u32 key = get_unaligned((u32 *)(addr + 2));
467
468 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
469}
470
stephen hemmingerd3428942012-10-01 12:32:35 +0000471/* Hash chain to use given mac address */
472static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800473 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000474{
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200475 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800476 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
477 else
478 return &vxlan->fdb_head[eth_hash(mac)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000479}
480
481/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000482static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800483 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000484{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800485 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000486 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000487
Sasha Levinb67bfe02013-02-27 17:06:00 -0800488 hlist_for_each_entry_rcu(f, head, hlist) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800489 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200490 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800491 if (vni == f->vni)
492 return f;
493 } else {
494 return f;
495 }
496 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000497 }
498
499 return NULL;
500}
501
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000502static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800503 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000504{
505 struct vxlan_fdb *f;
506
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800507 f = __vxlan_find_mac(vxlan, mac, vni);
Li RongQing016f3d12018-08-29 11:52:10 +0800508 if (f && f->used != jiffies)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000509 f->used = jiffies;
510
511 return f;
512}
513
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300514/* caller should hold vxlan->hash_lock */
515static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800516 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100517 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300518{
519 struct vxlan_rdst *rd;
520
521 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800522 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300523 rd->remote_port == port &&
524 rd->remote_vni == vni &&
525 rd->remote_ifindex == ifindex)
526 return rd;
527 }
528
529 return NULL;
530}
531
Petr Machata1941f1d2018-10-17 08:53:24 +0000532int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
533 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
534{
535 struct vxlan_dev *vxlan = netdev_priv(dev);
536 u8 eth_addr[ETH_ALEN + 2] = { 0 };
537 struct vxlan_rdst *rdst;
538 struct vxlan_fdb *f;
539 int rc = 0;
540
541 if (is_multicast_ether_addr(mac) ||
542 is_zero_ether_addr(mac))
543 return -EINVAL;
544
545 ether_addr_copy(eth_addr, mac);
546
547 rcu_read_lock();
548
549 f = __vxlan_find_mac(vxlan, eth_addr, vni);
550 if (!f) {
551 rc = -ENOENT;
552 goto out;
553 }
554
555 rdst = first_remote_rcu(f);
Petr Machata4c59b7d2019-01-16 23:06:54 +0000556 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
Petr Machata1941f1d2018-10-17 08:53:24 +0000557
558out:
559 rcu_read_unlock();
560 return rc;
561}
562EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
563
Petr Machata4f89f5b2018-12-07 19:55:04 +0000564static int vxlan_fdb_notify_one(struct notifier_block *nb,
565 const struct vxlan_dev *vxlan,
566 const struct vxlan_fdb *f,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000567 const struct vxlan_rdst *rdst,
568 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000569{
570 struct switchdev_notifier_vxlan_fdb_info fdb_info;
571 int rc;
572
Petr Machata4c59b7d2019-01-16 23:06:54 +0000573 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000574 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
575 &fdb_info);
576 return notifier_to_errno(rc);
577}
578
579int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000580 struct notifier_block *nb,
581 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000582{
583 struct vxlan_dev *vxlan;
584 struct vxlan_rdst *rdst;
585 struct vxlan_fdb *f;
586 unsigned int h;
587 int rc = 0;
588
589 if (!netif_is_vxlan(dev))
590 return -EINVAL;
591 vxlan = netdev_priv(dev);
592
593 spin_lock_bh(&vxlan->hash_lock);
594 for (h = 0; h < FDB_HASH_SIZE; ++h) {
595 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
596 if (f->vni == vni) {
597 list_for_each_entry(rdst, &f->remotes, list) {
598 rc = vxlan_fdb_notify_one(nb, vxlan,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000599 f, rdst,
600 extack);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000601 if (rc)
602 goto out;
603 }
604 }
605 }
606 }
607
608out:
609 spin_unlock_bh(&vxlan->hash_lock);
610 return rc;
611}
612EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
613
Petr Machatae5ff4b12018-12-07 19:55:06 +0000614void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
615{
616 struct vxlan_dev *vxlan;
617 struct vxlan_rdst *rdst;
618 struct vxlan_fdb *f;
619 unsigned int h;
620
621 if (!netif_is_vxlan(dev))
622 return;
623 vxlan = netdev_priv(dev);
624
625 spin_lock_bh(&vxlan->hash_lock);
626 for (h = 0; h < FDB_HASH_SIZE; ++h) {
627 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
628 if (f->vni == vni)
629 list_for_each_entry(rdst, &f->remotes, list)
630 rdst->offloaded = false;
631 }
632 spin_unlock_bh(&vxlan->hash_lock);
633}
634EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
635
Thomas Richter906dc182013-07-19 17:20:07 +0200636/* Replace destination of unicast mac */
637static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100638 union vxlan_addr *ip, __be16 port, __be32 vni,
Petr Machataccdfd4f2019-01-16 23:06:34 +0000639 __u32 ifindex, struct vxlan_rdst *oldrd)
Thomas Richter906dc182013-07-19 17:20:07 +0200640{
641 struct vxlan_rdst *rd;
642
643 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
644 if (rd)
645 return 0;
646
647 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
648 if (!rd)
649 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100650
Petr Machataccdfd4f2019-01-16 23:06:34 +0000651 *oldrd = *rd;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100652 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800653 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200654 rd->remote_port = port;
655 rd->remote_vni = vni;
656 rd->remote_ifindex = ifindex;
Petr Machata6ad0b5a2018-12-18 13:15:59 +0000657 rd->offloaded = false;
Thomas Richter906dc182013-07-19 17:20:07 +0200658 return 1;
659}
660
David Stevens66817122013-03-15 04:35:51 +0000661/* Add/update destinations for multicast */
662static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100663 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200664 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000665{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700666 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000667
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300668 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
669 if (rd)
670 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700671
David Stevens66817122013-03-15 04:35:51 +0000672 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
673 if (rd == NULL)
674 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100675
676 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
677 kfree(rd);
678 return -ENOBUFS;
679 }
680
Cong Wange4c7ed42013-08-31 13:44:33 +0800681 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000682 rd->remote_port = port;
Petr Machata0efe1172018-10-17 08:53:26 +0000683 rd->offloaded = false;
David Stevens66817122013-03-15 04:35:51 +0000684 rd->remote_vni = vni;
685 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700686
687 list_add_tail_rcu(&rd->list, &f->remotes);
688
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200689 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000690 return 1;
691}
692
Tom Herbertdfd86452015-01-12 17:00:38 -0800693static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
694 unsigned int off,
695 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100696 __be32 vni_field,
697 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800698 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800699{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700700 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800701
702 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700703 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800704
705 if (!NAPI_GRO_CB(skb)->csum_valid)
706 return NULL;
707
Jiri Benc54bfd872016-02-16 21:58:58 +0100708 start = vxlan_rco_start(vni_field);
709 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800710
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700711 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
712 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800713
714 skb->remcsum_offload = 1;
715
716 return vh;
717}
718
David Millerd4546c22018-06-24 14:13:49 +0900719static struct sk_buff *vxlan_gro_receive(struct sock *sk,
720 struct list_head *head,
721 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200722{
David Millerd4546c22018-06-24 14:13:49 +0900723 struct sk_buff *pp = NULL;
724 struct sk_buff *p;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200725 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800726 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200727 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700728 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100729 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800730 struct gro_remcsum grc;
731
732 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200733
734 off_vx = skb_gro_offset(skb);
735 hlen = off_vx + sizeof(*vh);
736 vh = skb_gro_header_fast(skb, off_vx);
737 if (skb_gro_header_hard(skb, hlen)) {
738 vh = skb_gro_header_slow(skb, hlen, off_vx);
739 if (unlikely(!vh))
740 goto out;
741 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200742
Tom Herbertdfd86452015-01-12 17:00:38 -0800743 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
744
Jiri Benc54bfd872016-02-16 21:58:58 +0100745 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800746
747 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
748 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100749 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800750 !!(vs->flags &
751 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800752
753 if (!vh)
754 goto out;
755 }
756
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700757 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
758
David Millerd4546c22018-06-24 14:13:49 +0900759 list_for_each_entry(p, head, list) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200760 if (!NAPI_GRO_CB(p)->same_flow)
761 continue;
762
763 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100764 if (vh->vx_flags != vh2->vx_flags ||
765 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200766 NAPI_GRO_CB(p)->same_flow = 0;
767 continue;
768 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200769 }
770
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200771 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800772 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200773
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200774out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200775 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200776
777 return pp;
778}
779
Tom Herbert5602c482016-04-05 08:22:53 -0700780static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200781{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700782 /* Sets 'skb->inner_mac_header' since we are always called with
783 * 'skb->encapsulation' set.
784 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800785 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200786}
787
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700788static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan,
789 const u8 *mac, __u16 state,
Petr Machata45598c12018-11-21 08:02:36 +0000790 __be32 src_vni, __u16 ndm_flags)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700791{
792 struct vxlan_fdb *f;
793
794 f = kmalloc(sizeof(*f), GFP_ATOMIC);
795 if (!f)
796 return NULL;
797 f->state = state;
798 f->flags = ndm_flags;
799 f->updated = f->used = jiffies;
800 f->vni = src_vni;
801 INIT_LIST_HEAD(&f->remotes);
802 memcpy(f->eth_addr, mac, ETH_ALEN);
803
804 return f;
805}
806
stephen hemmingerd3428942012-10-01 12:32:35 +0000807static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800808 const u8 *mac, union vxlan_addr *ip,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700809 __u16 state, __be16 port, __be32 src_vni,
Petr Machata45598c12018-11-21 08:02:36 +0000810 __be32 vni, __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700811 struct vxlan_fdb **fdb)
812{
813 struct vxlan_rdst *rd = NULL;
814 struct vxlan_fdb *f;
815 int rc;
816
817 if (vxlan->cfg.addrmax &&
818 vxlan->addrcnt >= vxlan->cfg.addrmax)
819 return -ENOSPC;
820
821 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
822 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
823 if (!f)
824 return -ENOMEM;
825
826 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
827 if (rc < 0) {
828 kfree(f);
829 return rc;
830 }
831
832 ++vxlan->addrcnt;
833 hlist_add_head_rcu(&f->hlist,
834 vxlan_fdb_head(vxlan, mac, src_vni));
835
836 *fdb = f;
837
838 return 0;
839}
840
Petr Machatac2b200e2019-01-16 23:06:30 +0000841static void vxlan_fdb_free(struct rcu_head *head)
842{
843 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
844 struct vxlan_rdst *rd, *nd;
845
846 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
847 dst_cache_destroy(&rd->dst_cache);
848 kfree(rd);
849 }
850 kfree(f);
851}
852
853static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
854 bool do_notify, bool swdev_notify)
855{
856 struct vxlan_rdst *rd;
857
858 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
859
860 --vxlan->addrcnt;
861 if (do_notify)
862 list_for_each_entry(rd, &f->remotes, list)
863 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000864 swdev_notify, NULL);
Petr Machatac2b200e2019-01-16 23:06:30 +0000865
866 hlist_del_rcu(&f->hlist);
867 call_rcu(&f->rcu, vxlan_fdb_free);
868}
869
Petr Machatafc4aa1c2019-02-07 12:18:02 +0000870static void vxlan_dst_free(struct rcu_head *head)
871{
872 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
873
874 dst_cache_destroy(&rd->dst_cache);
875 kfree(rd);
876}
877
Petr Machataa76d1ca2019-01-16 23:06:32 +0000878static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
879 union vxlan_addr *ip,
880 __u16 state, __u16 flags,
881 __be16 port, __be32 vni,
882 __u32 ifindex, __u16 ndm_flags,
883 struct vxlan_fdb *f,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000884 bool swdev_notify,
885 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +0000886{
887 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
888 struct vxlan_rdst *rd = NULL;
Petr Machataccdfd4f2019-01-16 23:06:34 +0000889 struct vxlan_rdst oldrd;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000890 int notify = 0;
Petr Machata61f46fe2019-01-16 23:06:38 +0000891 int rc = 0;
892 int err;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000893
894 /* Do not allow an externally learned entry to take over an entry added
895 * by the user.
896 */
897 if (!(fdb_flags & NTF_EXT_LEARNED) ||
898 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
899 if (f->state != state) {
900 f->state = state;
901 f->updated = jiffies;
902 notify = 1;
903 }
904 if (f->flags != fdb_flags) {
905 f->flags = fdb_flags;
906 f->updated = jiffies;
907 notify = 1;
908 }
909 }
910
911 if ((flags & NLM_F_REPLACE)) {
912 /* Only change unicasts */
913 if (!(is_multicast_ether_addr(f->eth_addr) ||
914 is_zero_ether_addr(f->eth_addr))) {
915 rc = vxlan_fdb_replace(f, ip, port, vni,
Petr Machataccdfd4f2019-01-16 23:06:34 +0000916 ifindex, &oldrd);
Petr Machataa76d1ca2019-01-16 23:06:32 +0000917 notify |= rc;
918 } else {
919 return -EOPNOTSUPP;
920 }
921 }
922 if ((flags & NLM_F_APPEND) &&
923 (is_multicast_ether_addr(f->eth_addr) ||
924 is_zero_ether_addr(f->eth_addr))) {
925 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
926
927 if (rc < 0)
928 return rc;
929 notify |= rc;
930 }
931
932 if (ndm_flags & NTF_USE)
933 f->used = jiffies;
934
935 if (notify) {
936 if (rd == NULL)
937 rd = first_remote_rtnl(f);
938
Petr Machata61f46fe2019-01-16 23:06:38 +0000939 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000940 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000941 if (err)
942 goto err_notify;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000943 }
944
945 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +0000946
947err_notify:
948 if ((flags & NLM_F_REPLACE) && rc)
949 *rd = oldrd;
Petr Machatafc4aa1c2019-02-07 12:18:02 +0000950 else if ((flags & NLM_F_APPEND) && rc) {
Petr Machata61f46fe2019-01-16 23:06:38 +0000951 list_del_rcu(&rd->list);
Petr Machatafc4aa1c2019-02-07 12:18:02 +0000952 call_rcu(&rd->rcu, vxlan_dst_free);
953 }
Petr Machata61f46fe2019-01-16 23:06:38 +0000954 return err;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000955}
956
957static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
958 const u8 *mac, union vxlan_addr *ip,
959 __u16 state, __u16 flags,
960 __be16 port, __be32 src_vni, __be32 vni,
961 __u32 ifindex, __u16 ndm_flags,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000962 bool swdev_notify,
963 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +0000964{
965 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
966 struct vxlan_fdb *f;
967 int rc;
968
969 /* Disallow replace to add a multicast entry */
970 if ((flags & NLM_F_REPLACE) &&
971 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
972 return -EOPNOTSUPP;
973
974 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
975 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
976 vni, ifindex, fdb_flags, &f);
977 if (rc < 0)
978 return rc;
979
Petr Machata61f46fe2019-01-16 23:06:38 +0000980 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000981 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000982 if (rc)
983 goto err_notify;
984
Petr Machataa76d1ca2019-01-16 23:06:32 +0000985 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +0000986
987err_notify:
988 vxlan_fdb_destroy(vxlan, f, false, false);
989 return rc;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000990}
991
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700992/* Add new entry to forwarding table -- assumes lock held */
993static int vxlan_fdb_update(struct vxlan_dev *vxlan,
994 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000995 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800996 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata45598c12018-11-21 08:02:36 +0000997 __u32 ifindex, __u16 ndm_flags,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000998 bool swdev_notify,
999 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001000{
1001 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001002
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001003 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001004 if (f) {
1005 if (flags & NLM_F_EXCL) {
1006 netdev_dbg(vxlan->dev,
1007 "lost race to create %pM\n", mac);
1008 return -EEXIST;
1009 }
Petr Machata0ec566a2018-11-21 08:02:37 +00001010
Petr Machataa76d1ca2019-01-16 23:06:32 +00001011 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1012 vni, ifindex, ndm_flags, f,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001013 swdev_notify, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001014 } else {
1015 if (!(flags & NLM_F_CREATE))
1016 return -ENOENT;
1017
Petr Machataa76d1ca2019-01-16 23:06:32 +00001018 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1019 port, src_vni, vni, ifindex,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001020 ndm_flags, swdev_notify, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001021 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001022}
1023
Lance Richardson35cf2842017-05-29 13:25:57 -04001024static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +00001025 struct vxlan_rdst *rd, bool swdev_notify)
Lance Richardson35cf2842017-05-29 13:25:57 -04001026{
1027 list_del_rcu(&rd->list);
Petr Machata4c59b7d2019-01-16 23:06:54 +00001028 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
Lance Richardson35cf2842017-05-29 13:25:57 -04001029 call_rcu(&rd->rcu, vxlan_dst_free);
1030}
1031
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001032static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001033 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
1034 __be32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001035{
1036 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001037 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001038
1039 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001040 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1041 if (err)
1042 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001043 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08001044 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
1045 if (remote->sa.sa_family == AF_INET) {
1046 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1047 ip->sa.sa_family = AF_INET;
1048#if IS_ENABLED(CONFIG_IPV6)
1049 } else {
1050 ip->sin6.sin6_addr = in6addr_any;
1051 ip->sa.sa_family = AF_INET6;
1052#endif
1053 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001054 }
1055
1056 if (tb[NDA_PORT]) {
1057 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1058 return -EINVAL;
1059 *port = nla_get_be16(tb[NDA_PORT]);
1060 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001061 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001062 }
1063
1064 if (tb[NDA_VNI]) {
1065 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1066 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +01001067 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001068 } else {
1069 *vni = vxlan->default_dst.remote_vni;
1070 }
1071
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001072 if (tb[NDA_SRC_VNI]) {
1073 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1074 return -EINVAL;
1075 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1076 } else {
1077 *src_vni = vxlan->default_dst.remote_vni;
1078 }
1079
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001080 if (tb[NDA_IFINDEX]) {
1081 struct net_device *tdev;
1082
1083 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1084 return -EINVAL;
1085 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +08001086 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001087 if (!tdev)
1088 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001089 } else {
1090 *ifindex = 0;
1091 }
1092
1093 return 0;
1094}
1095
stephen hemmingerd3428942012-10-01 12:32:35 +00001096/* Add static entry (via netlink) */
1097static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1098 struct net_device *dev,
Petr Machata87b09842019-01-16 23:06:50 +00001099 const unsigned char *addr, u16 vid, u16 flags,
1100 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001101{
1102 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001103 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +08001104 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +00001105 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001106 __be32 src_vni, vni;
Jiri Benc54bfd872016-02-16 21:58:58 +01001107 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001108 int err;
1109
1110 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1111 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1112 ndm->ndm_state);
1113 return -EINVAL;
1114 }
1115
1116 if (tb[NDA_DST] == NULL)
1117 return -EINVAL;
1118
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001119 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001120 if (err)
1121 return err;
David Stevens66817122013-03-15 04:35:51 +00001122
Mike Rapoport5933a7b2014-04-01 09:23:01 +03001123 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1124 return -EAFNOSUPPORT;
1125
stephen hemmingerd3428942012-10-01 12:32:35 +00001126 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001127 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
Petr Machata45598c12018-11-21 08:02:36 +00001128 port, src_vni, vni, ifindex,
1129 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001130 true, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001131 spin_unlock_bh(&vxlan->hash_lock);
1132
1133 return err;
1134}
1135
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001136static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1137 const unsigned char *addr, union vxlan_addr ip,
Xin Longfc39c382017-11-26 21:19:05 +08001138 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001139 u32 ifindex, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +00001140{
stephen hemmingerd3428942012-10-01 12:32:35 +00001141 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001142 struct vxlan_rdst *rd = NULL;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001143 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001144
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001145 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001146 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001147 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001148
Cong Wange4c7ed42013-08-31 13:44:33 +08001149 if (!vxlan_addr_any(&ip)) {
1150 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001151 if (!rd)
1152 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +00001153 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001154
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001155 /* remove a destination if it's not the only one on the list,
1156 * otherwise destroy the fdb entry
1157 */
1158 if (rd && !list_is_singular(&f->remotes)) {
Petr Machata0e6160f2018-11-21 08:02:35 +00001159 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001160 goto out;
1161 }
1162
Petr Machata0e6160f2018-11-21 08:02:35 +00001163 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001164
1165out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001166 return 0;
1167}
1168
1169/* Delete entry (via netlink) */
1170static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1171 struct net_device *dev,
1172 const unsigned char *addr, u16 vid)
1173{
1174 struct vxlan_dev *vxlan = netdev_priv(dev);
1175 union vxlan_addr ip;
1176 __be32 src_vni, vni;
1177 __be16 port;
1178 u32 ifindex;
1179 int err;
1180
1181 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
1182 if (err)
1183 return err;
1184
1185 spin_lock_bh(&vxlan->hash_lock);
Petr Machata0e6160f2018-11-21 08:02:35 +00001186 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1187 true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001188 spin_unlock_bh(&vxlan->hash_lock);
1189
1190 return err;
1191}
1192
1193/* Dump forwarding table */
1194static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04001195 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07001196 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +00001197{
1198 struct vxlan_dev *vxlan = netdev_priv(dev);
1199 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -07001200 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001201
1202 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1203 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001204
Sasha Levinb67bfe02013-02-27 17:06:00 -08001205 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +00001206 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +00001207
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001208 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07001209 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001210 goto skip;
1211
David Stevens66817122013-03-15 04:35:51 +00001212 err = vxlan_fdb_info(skb, vxlan, f,
1213 NETLINK_CB(cb->skb).portid,
1214 cb->nlh->nlmsg_seq,
1215 RTM_NEWNEIGH,
1216 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -07001217 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001218 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001219skip:
Roopa Prabhud2976532016-08-30 21:56:45 -07001220 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001221 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001222 }
1223 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001224out:
Roopa Prabhud2976532016-08-30 21:56:45 -07001225 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001226}
1227
Roopa Prabhu474c3c82018-12-15 22:35:10 -08001228static int vxlan_fdb_get(struct sk_buff *skb,
1229 struct nlattr *tb[],
1230 struct net_device *dev,
1231 const unsigned char *addr,
1232 u16 vid, u32 portid, u32 seq,
1233 struct netlink_ext_ack *extack)
1234{
1235 struct vxlan_dev *vxlan = netdev_priv(dev);
1236 struct vxlan_fdb *f;
1237 __be32 vni;
1238 int err;
1239
1240 if (tb[NDA_VNI])
1241 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1242 else
1243 vni = vxlan->default_dst.remote_vni;
1244
1245 rcu_read_lock();
1246
1247 f = __vxlan_find_mac(vxlan, addr, vni);
1248 if (!f) {
1249 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1250 err = -ENOENT;
1251 goto errout;
1252 }
1253
1254 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1255 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1256errout:
1257 rcu_read_unlock();
1258 return err;
1259}
1260
stephen hemmingerd3428942012-10-01 12:32:35 +00001261/* Watch incoming packets to learn mapping between Ethernet address
1262 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +09001263 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +00001264 */
stephen hemminger26a41ae62013-06-17 12:09:58 -07001265static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001266 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001267 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +00001268{
1269 struct vxlan_dev *vxlan = netdev_priv(dev);
1270 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001271 u32 ifindex = 0;
1272
1273#if IS_ENABLED(CONFIG_IPV6)
1274 if (src_ip->sa.sa_family == AF_INET6 &&
1275 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1276 ifindex = src_ifindex;
1277#endif
stephen hemmingerd3428942012-10-01 12:32:35 +00001278
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001279 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001280 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001281 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001282
Matthias Schiffer87613de2017-06-19 10:03:59 +02001283 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1284 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001285 return false;
1286
1287 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -07001288 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001289 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001290
1291 if (net_ratelimit())
1292 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001293 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001294 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001295
Cong Wange4c7ed42013-08-31 13:44:33 +08001296 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001297 f->updated = jiffies;
Petr Machata4c59b7d2019-01-16 23:06:54 +00001298 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
stephen hemmingerd3428942012-10-01 12:32:35 +00001299 } else {
1300 /* learned new entry */
1301 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001302
1303 /* close off race between vxlan_flush and incoming packets */
1304 if (netif_running(dev))
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001305 vxlan_fdb_update(vxlan, src_mac, src_ip,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001306 NUD_REACHABLE,
1307 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001308 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001309 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001310 vxlan->default_dst.remote_vni,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001311 ifindex, NTF_SELF, true, NULL);
stephen hemmingerd3428942012-10-01 12:32:35 +00001312 spin_unlock(&vxlan->hash_lock);
1313 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001314
1315 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001316}
1317
stephen hemmingerd3428942012-10-01 12:32:35 +00001318/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001319static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001320{
stephen hemminger553675f2013-05-16 11:35:20 +00001321 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001322 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001323#if IS_ENABLED(CONFIG_IPV6)
1324 struct vxlan_sock *sock6;
1325#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001326 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001327
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001328 sock4 = rtnl_dereference(dev->vn4_sock);
1329
Gao feng95ab0992013-12-10 16:37:33 +08001330 /* The vxlan_sock is only used by dev, leaving group has
1331 * no effect on other vxlan devices.
1332 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001333 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001334 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001335#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001336 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001337 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001338 return false;
1339#endif
Gao feng95ab0992013-12-10 16:37:33 +08001340
stephen hemminger553675f2013-05-16 11:35:20 +00001341 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001342 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001343 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001344
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001345 if (family == AF_INET &&
1346 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001347 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001348#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001349 if (family == AF_INET6 &&
1350 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001351 continue;
1352#endif
Gao feng95ab0992013-12-10 16:37:33 +08001353
1354 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1355 &dev->default_dst.remote_ip))
1356 continue;
1357
1358 if (vxlan->default_dst.remote_ifindex !=
1359 dev->default_dst.remote_ifindex)
1360 continue;
1361
1362 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001363 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001364
1365 return false;
1366}
1367
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001368static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001369{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001370 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001371
Jiri Bencb1be00a2015-09-24 13:50:02 +02001372 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001373 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001374 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001375 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001376
Jiri Bencb1be00a2015-09-24 13:50:02 +02001377 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001378 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001379 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001380 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001381 (vs->flags & VXLAN_F_GPE) ?
1382 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001383 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001384 spin_unlock(&vn->sock_lock);
1385
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001386 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001387}
1388
Jiri Bencb1be00a2015-09-24 13:50:02 +02001389static void vxlan_sock_release(struct vxlan_dev *vxlan)
1390{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001391 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001392#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001393 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1394
Mark Bloch57d88182017-06-07 14:36:58 +03001395 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001396#endif
1397
Mark Bloch57d88182017-06-07 14:36:58 +03001398 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001399 synchronize_net();
1400
Mark Blocha53cb292017-06-02 03:24:08 +03001401 vxlan_vs_del_dev(vxlan);
1402
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001403 if (__vxlan_sock_release_prep(sock4)) {
1404 udp_tunnel_sock_release(sock4->sock);
1405 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001406 }
1407
1408#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001409 if (__vxlan_sock_release_prep(sock6)) {
1410 udp_tunnel_sock_release(sock6->sock);
1411 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001412 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001413#endif
1414}
1415
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001416/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001417 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001418 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001419static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001420{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001421 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001422 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1423 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001424 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001425
Cong Wange4c7ed42013-08-31 13:44:33 +08001426 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001427 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001428 struct ip_mreqn mreq = {
1429 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1430 .imr_ifindex = ifindex,
1431 };
1432
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001433 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001434 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001435 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001436 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001437#if IS_ENABLED(CONFIG_IPV6)
1438 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001439 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1440
1441 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001442 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001443 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1444 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001445 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001446#endif
1447 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001448
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001449 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001450}
1451
1452/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001453static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001454{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001455 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001456 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1457 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001458 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001459
Cong Wange4c7ed42013-08-31 13:44:33 +08001460 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001461 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001462 struct ip_mreqn mreq = {
1463 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1464 .imr_ifindex = ifindex,
1465 };
1466
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001467 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001468 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001469 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001470 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001471#if IS_ENABLED(CONFIG_IPV6)
1472 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001473 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1474
1475 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001476 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001477 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1478 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001479 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001480#endif
1481 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001482
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001483 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001484}
1485
Jiri Bencf14eceb2016-02-16 21:59:01 +01001486static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1487 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001488{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001489 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001490
Jiri Bencf14eceb2016-02-16 21:59:01 +01001491 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1492 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001493
Jiri Bencf14eceb2016-02-16 21:59:01 +01001494 start = vxlan_rco_start(unparsed->vx_vni);
1495 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001496
Jiri Benc7d34fa72016-03-21 17:50:05 +01001497 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001498 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001499
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001500 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1501 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001502out:
1503 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1504 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001505 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001506}
1507
Jiri Bencf14eceb2016-02-16 21:59:01 +01001508static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001509 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001510 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001511{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001512 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001513 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001514
Jiri Bencf14eceb2016-02-16 21:59:01 +01001515 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1516 goto out;
1517
Jiri Benc3288af02016-02-16 21:59:00 +01001518 md->gbp = ntohs(gbp->policy_id);
1519
Jiri Benc10a5af22016-02-23 18:02:59 +01001520 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001521 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001522 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001523 tun_dst->u.tun_info.options_len = sizeof(*md);
1524 }
Jiri Benc3288af02016-02-16 21:59:00 +01001525 if (gbp->dont_learn)
1526 md->gbp |= VXLAN_GBP_DONT_LEARN;
1527
1528 if (gbp->policy_applied)
1529 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001530
Jiri Benc64f87d32016-02-23 18:02:55 +01001531 /* In flow-based mode, GBP is carried in dst_metadata */
1532 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1533 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001534out:
1535 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001536}
1537
Jiri Bence1e53142016-04-05 14:47:13 +02001538static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001539 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001540 struct sk_buff *skb, u32 vxflags)
1541{
1542 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1543
1544 /* Need to have Next Protocol set for interfaces in GPE mode. */
1545 if (!gpe->np_applied)
1546 return false;
1547 /* "The initial version is 0. If a receiver does not support the
1548 * version indicated it MUST drop the packet.
1549 */
1550 if (gpe->version != 0)
1551 return false;
1552 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1553 * processing MUST occur." However, we don't implement OAM
1554 * processing, thus drop the packet.
1555 */
1556 if (gpe->oam_flag)
1557 return false;
1558
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001559 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1560 if (!*protocol)
Jiri Bence1e53142016-04-05 14:47:13 +02001561 return false;
Jiri Bence1e53142016-04-05 14:47:13 +02001562
1563 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1564 return true;
1565}
1566
Jiri Benc1ab016e2016-02-23 18:02:56 +01001567static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1568 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001569 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001570{
Thomas Graf614732e2015-07-21 10:44:06 +02001571 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001572 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001573
Thomas Graf614732e2015-07-21 10:44:06 +02001574 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001575 skb->protocol = eth_type_trans(skb, vxlan->dev);
1576 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1577
1578 /* Ignore packet loops (and multicast echo) */
1579 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001580 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001581
Jiri Benc760c6802016-02-23 18:02:57 +01001582 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001583 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001584 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001585 saddr.sa.sa_family = AF_INET;
1586#if IS_ENABLED(CONFIG_IPV6)
1587 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001588 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001589 saddr.sa.sa_family = AF_INET6;
1590#endif
1591 }
1592
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001593 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001594 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001595 return false;
1596
1597 return true;
1598}
1599
Jiri Benc760c6802016-02-23 18:02:57 +01001600static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1601 struct sk_buff *skb)
1602{
1603 int err = 0;
1604
1605 if (vxlan_get_sk_family(vs) == AF_INET)
1606 err = IP_ECN_decapsulate(oiph, skb);
1607#if IS_ENABLED(CONFIG_IPV6)
1608 else
1609 err = IP6_ECN_decapsulate(oiph, skb);
1610#endif
1611
1612 if (unlikely(err) && log_ecn_error) {
1613 if (vxlan_get_sk_family(vs) == AF_INET)
1614 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1615 &((struct iphdr *)oiph)->saddr,
1616 ((struct iphdr *)oiph)->tos);
1617 else
1618 net_info_ratelimited("non-ECT from %pI6\n",
1619 &((struct ipv6hdr *)oiph)->saddr);
1620 }
1621 return err <= 1;
1622}
1623
stephen hemmingerd3428942012-10-01 12:32:35 +00001624/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001625static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001626{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001627 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001628 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001629 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001630 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001631 struct vxlan_metadata _md;
1632 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001633 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001634 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001635 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001636 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001637
Jiri Bence1e53142016-04-05 14:47:13 +02001638 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001639 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001640 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001641
Jiri Bencf14eceb2016-02-16 21:59:01 +01001642 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001643 /* VNI flag always required to be set */
1644 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1645 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1646 ntohl(vxlan_hdr(skb)->vx_flags),
1647 ntohl(vxlan_hdr(skb)->vx_vni));
1648 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001649 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001650 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001651 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1652 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001653
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001654 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001655 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001656 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001657
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001658 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1659
Matthias Schiffer49f810f2017-06-19 10:04:00 +02001660 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001661 if (!vxlan)
1662 goto drop;
1663
Jiri Bence1e53142016-04-05 14:47:13 +02001664 /* For backwards compatibility, only allow reserved fields to be
1665 * used by VXLAN extensions if explicitly requested.
1666 */
1667 if (vs->flags & VXLAN_F_GPE) {
1668 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1669 goto drop;
1670 raw_proto = true;
1671 }
1672
1673 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1674 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1675 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001676
Thomas Grafee122c72015-07-21 10:43:58 +02001677 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001678 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001679
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001680 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001681 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001682
Thomas Grafee122c72015-07-21 10:43:58 +02001683 if (!tun_dst)
1684 goto drop;
1685
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001686 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001687
1688 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001689 } else {
1690 memset(md, 0, sizeof(*md));
1691 }
1692
Jiri Bencf14eceb2016-02-16 21:59:01 +01001693 if (vs->flags & VXLAN_F_REMCSUM_RX)
1694 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1695 goto drop;
1696 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001697 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001698 /* Note that GBP and GPE can never be active together. This is
1699 * ensured in vxlan_dev_configure.
1700 */
Thomas Graf35114942015-01-15 03:53:55 +01001701
Jiri Bencf14eceb2016-02-16 21:59:01 +01001702 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001703 /* If there are any unprocessed flags remaining treat
1704 * this as a malformed packet. This behavior diverges from
1705 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1706 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001707 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001708 * is more robust and provides a little more security in
1709 * adding extensions to VXLAN.
1710 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001711 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001712 }
1713
Jiri Bence1e53142016-04-05 14:47:13 +02001714 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001715 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001716 goto drop;
1717 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001718 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001719 skb->dev = vxlan->dev;
1720 skb->pkt_type = PACKET_HOST;
1721 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001722
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001723 oiph = skb_network_header(skb);
1724 skb_reset_network_header(skb);
1725
1726 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1727 ++vxlan->dev->stats.rx_frame_errors;
1728 ++vxlan->dev->stats.rx_errors;
1729 goto drop;
1730 }
1731
Eric Dumazet59cbf562019-03-10 10:36:40 -07001732 rcu_read_lock();
1733
1734 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1735 rcu_read_unlock();
1736 atomic_long_inc(&vxlan->dev->rx_dropped);
1737 goto drop;
1738 }
1739
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001740 stats = this_cpu_ptr(vxlan->dev->tstats);
1741 u64_stats_update_begin(&stats->syncp);
1742 stats->rx_packets++;
1743 stats->rx_bytes += skb->len;
1744 u64_stats_update_end(&stats->syncp);
1745
1746 gro_cells_receive(&vxlan->gro_cells, skb);
Eric Dumazet59cbf562019-03-10 10:36:40 -07001747
1748 rcu_read_unlock();
1749
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001750 return 0;
1751
1752drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001753 /* Consume bad packet */
1754 kfree_skb(skb);
1755 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001756}
1757
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001758/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1759static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1760{
1761 struct vxlan_dev *vxlan;
1762 struct vxlan_sock *vs;
1763 struct vxlanhdr *hdr;
1764 __be32 vni;
1765
1766 if (skb->len < VXLAN_HLEN)
1767 return -EINVAL;
1768
1769 hdr = vxlan_hdr(skb);
1770
1771 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1772 return -EINVAL;
1773
1774 vs = rcu_dereference_sk_user_data(sk);
1775 if (!vs)
1776 return -ENOENT;
1777
1778 vni = vxlan_vni(hdr->vx_vni);
1779 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1780 if (!vxlan)
1781 return -ENOENT;
1782
1783 return 0;
1784}
1785
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001786static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001787{
1788 struct vxlan_dev *vxlan = netdev_priv(dev);
1789 struct arphdr *parp;
1790 u8 *arpptr, *sha;
1791 __be32 sip, tip;
1792 struct neighbour *n;
1793
1794 if (dev->flags & IFF_NOARP)
1795 goto out;
1796
1797 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1798 dev->stats.tx_dropped++;
1799 goto out;
1800 }
1801 parp = arp_hdr(skb);
1802
1803 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1804 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1805 parp->ar_pro != htons(ETH_P_IP) ||
1806 parp->ar_op != htons(ARPOP_REQUEST) ||
1807 parp->ar_hln != dev->addr_len ||
1808 parp->ar_pln != 4)
1809 goto out;
1810 arpptr = (u8 *)parp + sizeof(struct arphdr);
1811 sha = arpptr;
1812 arpptr += dev->addr_len; /* sha */
1813 memcpy(&sip, arpptr, sizeof(sip));
1814 arpptr += sizeof(sip);
1815 arpptr += dev->addr_len; /* tha */
1816 memcpy(&tip, arpptr, sizeof(tip));
1817
1818 if (ipv4_is_loopback(tip) ||
1819 ipv4_is_multicast(tip))
1820 goto out;
1821
1822 n = neigh_lookup(&arp_tbl, &tip, dev);
1823
1824 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001825 struct vxlan_fdb *f;
1826 struct sk_buff *reply;
1827
1828 if (!(n->nud_state & NUD_CONNECTED)) {
1829 neigh_release(n);
1830 goto out;
1831 }
1832
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001833 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08001834 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001835 /* bridge-local neighbor */
1836 neigh_release(n);
1837 goto out;
1838 }
1839
1840 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1841 n->ha, sha);
1842
1843 neigh_release(n);
1844
David Stevens73461352014-03-18 12:32:29 -04001845 if (reply == NULL)
1846 goto out;
1847
David Stevense4f67ad2012-11-20 02:50:14 +00001848 skb_reset_mac_header(reply);
1849 __skb_pull(reply, skb_network_offset(reply));
1850 reply->ip_summed = CHECKSUM_UNNECESSARY;
1851 reply->pkt_type = PACKET_HOST;
1852
1853 if (netif_rx_ni(reply) == NET_RX_DROP)
1854 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001855 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001856 union vxlan_addr ipa = {
1857 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001858 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001859 };
1860
1861 vxlan_ip_miss(dev, &ipa);
1862 }
David Stevense4f67ad2012-11-20 02:50:14 +00001863out:
1864 consume_skb(skb);
1865 return NETDEV_TX_OK;
1866}
1867
Cong Wangf564f452013-08-31 13:44:36 +08001868#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001869static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1870 struct neighbour *n, bool isrouter)
1871{
1872 struct net_device *dev = request->dev;
1873 struct sk_buff *reply;
1874 struct nd_msg *ns, *na;
1875 struct ipv6hdr *pip6;
1876 u8 *daddr;
1877 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1878 int ns_olen;
1879 int i, len;
1880
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001881 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04001882 return NULL;
1883
1884 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1885 sizeof(*na) + na_olen + dev->needed_tailroom;
1886 reply = alloc_skb(len, GFP_ATOMIC);
1887 if (reply == NULL)
1888 return NULL;
1889
1890 reply->protocol = htons(ETH_P_IPV6);
1891 reply->dev = dev;
1892 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1893 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001894 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001895
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001896 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04001897
1898 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001899 ns_olen = request->len - skb_network_offset(request) -
1900 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04001901 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1902 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1903 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1904 break;
1905 }
1906 }
1907
1908 /* Ethernet header */
1909 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1910 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1911 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1912 reply->protocol = htons(ETH_P_IPV6);
1913
1914 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001915 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001916 skb_put(reply, sizeof(struct ipv6hdr));
1917
1918 /* IPv6 header */
1919
1920 pip6 = ipv6_hdr(reply);
1921 memset(pip6, 0, sizeof(struct ipv6hdr));
1922 pip6->version = 6;
1923 pip6->priority = ipv6_hdr(request)->priority;
1924 pip6->nexthdr = IPPROTO_ICMPV6;
1925 pip6->hop_limit = 255;
1926 pip6->daddr = ipv6_hdr(request)->saddr;
1927 pip6->saddr = *(struct in6_addr *)n->primary_key;
1928
1929 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001930 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001931
David Stevens4b29dba2014-03-24 10:39:58 -04001932 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02001933 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04001934 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1935 na->icmph.icmp6_router = isrouter;
1936 na->icmph.icmp6_override = 1;
1937 na->icmph.icmp6_solicited = 1;
1938 na->target = ns->target;
1939 ether_addr_copy(&na->opt[2], n->ha);
1940 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1941 na->opt[1] = na_olen >> 3;
1942
1943 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1944 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1945 csum_partial(na, sizeof(*na)+na_olen, 0));
1946
1947 pip6->payload_len = htons(sizeof(*na)+na_olen);
1948
1949 skb_push(reply, sizeof(struct ipv6hdr));
1950
1951 reply->ip_summed = CHECKSUM_UNNECESSARY;
1952
1953 return reply;
1954}
1955
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001956static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08001957{
1958 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08001959 const struct in6_addr *daddr;
Xin Long8bff36852017-11-11 19:58:50 +08001960 const struct ipv6hdr *iphdr;
David Stevens4b29dba2014-03-24 10:39:58 -04001961 struct inet6_dev *in6_dev;
Xin Long8bff36852017-11-11 19:58:50 +08001962 struct neighbour *n;
1963 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001964
1965 in6_dev = __in6_dev_get(dev);
1966 if (!in6_dev)
1967 goto out;
1968
Cong Wangf564f452013-08-31 13:44:36 +08001969 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001970 daddr = &iphdr->daddr;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001971 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08001972
David Stevens4b29dba2014-03-24 10:39:58 -04001973 if (ipv6_addr_loopback(daddr) ||
1974 ipv6_addr_is_multicast(&msg->target))
1975 goto out;
1976
1977 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001978
1979 if (n) {
1980 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001981 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001982
1983 if (!(n->nud_state & NUD_CONNECTED)) {
1984 neigh_release(n);
1985 goto out;
1986 }
1987
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001988 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08001989 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1990 /* bridge-local neighbor */
1991 neigh_release(n);
1992 goto out;
1993 }
1994
David Stevens4b29dba2014-03-24 10:39:58 -04001995 reply = vxlan_na_create(skb, n,
1996 !!(f ? f->flags & NTF_ROUTER : 0));
1997
Cong Wangf564f452013-08-31 13:44:36 +08001998 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001999
2000 if (reply == NULL)
2001 goto out;
2002
2003 if (netif_rx_ni(reply) == NET_RX_DROP)
2004 dev->stats.rx_dropped++;
2005
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002006 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04002007 union vxlan_addr ipa = {
2008 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002009 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04002010 };
2011
Cong Wangf564f452013-08-31 13:44:36 +08002012 vxlan_ip_miss(dev, &ipa);
2013 }
2014
2015out:
2016 consume_skb(skb);
2017 return NETDEV_TX_OK;
2018}
2019#endif
2020
David Stevense4f67ad2012-11-20 02:50:14 +00002021static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2022{
2023 struct vxlan_dev *vxlan = netdev_priv(dev);
2024 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00002025
2026 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2027 return false;
2028
2029 n = NULL;
2030 switch (ntohs(eth_hdr(skb)->h_proto)) {
2031 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08002032 {
2033 struct iphdr *pip;
2034
David Stevense4f67ad2012-11-20 02:50:14 +00002035 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2036 return false;
2037 pip = ip_hdr(skb);
2038 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002039 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002040 union vxlan_addr ipa = {
2041 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002042 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08002043 };
2044
2045 vxlan_ip_miss(dev, &ipa);
2046 return false;
2047 }
2048
David Stevense4f67ad2012-11-20 02:50:14 +00002049 break;
Cong Wange15a00a2013-08-31 13:44:34 +08002050 }
2051#if IS_ENABLED(CONFIG_IPV6)
2052 case ETH_P_IPV6:
2053 {
2054 struct ipv6hdr *pip6;
2055
2056 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2057 return false;
2058 pip6 = ipv6_hdr(skb);
2059 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002060 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08002061 union vxlan_addr ipa = {
2062 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002063 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08002064 };
2065
2066 vxlan_ip_miss(dev, &ipa);
2067 return false;
2068 }
2069
2070 break;
2071 }
2072#endif
David Stevense4f67ad2012-11-20 02:50:14 +00002073 default:
2074 return false;
2075 }
2076
2077 if (n) {
2078 bool diff;
2079
Joe Perches7367d0b2013-09-01 11:51:23 -07002080 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00002081 if (diff) {
2082 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2083 dev->addr_len);
2084 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2085 }
2086 neigh_release(n);
2087 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08002088 }
2089
David Stevense4f67ad2012-11-20 02:50:14 +00002090 return false;
2091}
2092
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002093static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01002094 struct vxlan_metadata *md)
2095{
2096 struct vxlanhdr_gbp *gbp;
2097
Thomas Grafdb79a622015-02-04 17:00:04 +01002098 if (!md->gbp)
2099 return;
2100
Thomas Graf35114942015-01-15 03:53:55 +01002101 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01002102 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01002103
2104 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2105 gbp->dont_learn = 1;
2106
2107 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2108 gbp->policy_applied = 1;
2109
2110 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2111}
2112
Jiri Bence1e53142016-04-05 14:47:13 +02002113static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2114 __be16 protocol)
2115{
2116 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2117
2118 gpe->np_applied = 1;
Jiri Bencfa20e0e2017-08-28 21:43:22 +02002119 gpe->next_protocol = tun_p_from_eth_p(protocol);
2120 if (!gpe->next_protocol)
2121 return -EPFNOSUPPORT;
2122 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002123}
2124
Jiri Bencf491e562016-02-02 18:09:16 +01002125static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2126 int iphdr_len, __be32 vni,
2127 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002128 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08002129{
Cong Wange4c7ed42013-08-31 13:44:33 +08002130 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08002131 int min_headroom;
2132 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08002133 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02002134 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08002135
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002136 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08002137 skb->ip_summed == CHECKSUM_PARTIAL) {
2138 int csum_start = skb_checksum_start_offset(skb);
2139
2140 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2141 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2142 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00002143 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08002144 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08002145 }
2146
Cong Wange4c7ed42013-08-31 13:44:33 +08002147 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08002148 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002149
2150 /* Need space for new headers (invalidates iph ptr) */
2151 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02002152 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08002153 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002154
Alexander Duyckaed069d2016-04-14 15:33:37 -04002155 err = iptunnel_handle_offloads(skb, type);
2156 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08002157 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07002158
Johannes Bergd58ff352017-06-16 14:29:23 +02002159 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01002160 vxh->vx_flags = VXLAN_HF_VNI;
2161 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07002162
Tom Herbertdfd86452015-01-12 17:00:38 -08002163 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01002164 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08002165
Jiri Benc54bfd872016-02-16 21:58:58 +01002166 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2167 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2168 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08002169
2170 if (!skb_is_gso(skb)) {
2171 skb->ip_summed = CHECKSUM_NONE;
2172 skb->encapsulation = 0;
2173 }
2174 }
2175
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002176 if (vxflags & VXLAN_F_GBP)
2177 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02002178 if (vxflags & VXLAN_F_GPE) {
2179 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2180 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002181 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02002182 inner_protocol = skb->protocol;
2183 }
Thomas Graf35114942015-01-15 03:53:55 +01002184
Jiri Bence1e53142016-04-05 14:47:13 +02002185 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08002186 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07002187}
Pravin B Shelar49560532013-08-19 11:23:17 -07002188
pravin shelar655c3de2016-11-13 20:43:55 -08002189static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2190 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002191 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002192 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002193 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002194 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01002195{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002196 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002197 struct rtable *rt = NULL;
2198 struct flowi4 fl4;
2199
pravin shelar655c3de2016-11-13 20:43:55 -08002200 if (!sock4)
2201 return ERR_PTR(-EIO);
2202
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002203 if (tos && !info)
2204 use_cache = false;
2205 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002206 rt = dst_cache_get_ip4(dst_cache, saddr);
2207 if (rt)
2208 return rt;
2209 }
2210
Jiri Benc1a8496b2016-02-02 18:09:14 +01002211 memset(&fl4, 0, sizeof(fl4));
2212 fl4.flowi4_oif = oif;
2213 fl4.flowi4_tos = RT_TOS(tos);
2214 fl4.flowi4_mark = skb->mark;
2215 fl4.flowi4_proto = IPPROTO_UDP;
2216 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002217 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002218 fl4.fl4_dport = dport;
2219 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002220
2221 rt = ip_route_output_key(vxlan->net, &fl4);
pravin shelar655c3de2016-11-13 20:43:55 -08002222 if (likely(!IS_ERR(rt))) {
2223 if (rt->dst.dev == dev) {
2224 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2225 ip_rt_put(rt);
2226 return ERR_PTR(-ELOOP);
2227 }
2228
Jiri Benc1a8496b2016-02-02 18:09:14 +01002229 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002230 if (use_cache)
2231 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08002232 } else {
2233 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2234 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002235 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002236 return rt;
2237}
2238
Jiri Bence5d4b292015-12-07 13:04:30 +01002239#if IS_ENABLED(CONFIG_IPV6)
2240static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08002241 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08002242 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01002243 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002244 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01002245 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002246 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002247 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002248 struct dst_cache *dst_cache,
2249 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01002250{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002251 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002252 struct dst_entry *ndst;
2253 struct flowi6 fl6;
2254 int err;
2255
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002256 if (!sock6)
2257 return ERR_PTR(-EIO);
2258
Daniel Borkmann14006152016-03-04 15:15:08 +01002259 if (tos && !info)
2260 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002261 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002262 ndst = dst_cache_get_ip6(dst_cache, saddr);
2263 if (ndst)
2264 return ndst;
2265 }
2266
Jiri Bence5d4b292015-12-07 13:04:30 +01002267 memset(&fl6, 0, sizeof(fl6));
2268 fl6.flowi6_oif = oif;
2269 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002270 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01002271 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01002272 fl6.flowi6_mark = skb->mark;
2273 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002274 fl6.fl6_dport = dport;
2275 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01002276
2277 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002278 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01002279 &ndst, &fl6);
pravin shelar655c3de2016-11-13 20:43:55 -08002280 if (unlikely(err < 0)) {
2281 netdev_dbg(dev, "no route to %pI6\n", daddr);
2282 return ERR_PTR(-ENETUNREACH);
2283 }
2284
2285 if (unlikely(ndst->dev == dev)) {
2286 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2287 dst_release(ndst);
2288 return ERR_PTR(-ELOOP);
2289 }
Jiri Bence5d4b292015-12-07 13:04:30 +01002290
2291 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002292 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002293 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01002294 return ndst;
2295}
2296#endif
2297
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002298/* Bypass encapsulation if the destination is local */
2299static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002300 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002301{
Li RongQing8f849852014-01-04 13:57:59 +08002302 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08002303 union vxlan_addr loopback;
2304 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Eric Dumazet4179cb52019-02-07 12:27:38 -08002305 struct net_device *dev;
Li RongQingce6502a2014-10-16 08:49:41 +08002306 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002307
Li RongQing8f849852014-01-04 13:57:59 +08002308 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2309 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002310 skb->pkt_type = PACKET_HOST;
2311 skb->encapsulation = 0;
2312 skb->dev = dst_vxlan->dev;
2313 __skb_pull(skb, skb_network_offset(skb));
2314
Cong Wange4c7ed42013-08-31 13:44:33 +08002315 if (remote_ip->sa.sa_family == AF_INET) {
2316 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2317 loopback.sa.sa_family = AF_INET;
2318#if IS_ENABLED(CONFIG_IPV6)
2319 } else {
2320 loopback.sin6.sin6_addr = in6addr_loopback;
2321 loopback.sa.sa_family = AF_INET6;
2322#endif
2323 }
2324
Eric Dumazet4179cb52019-02-07 12:27:38 -08002325 rcu_read_lock();
2326 dev = skb->dev;
2327 if (unlikely(!(dev->flags & IFF_UP))) {
2328 kfree_skb(skb);
2329 goto drop;
2330 }
2331
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002332 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
Eric Dumazet4179cb52019-02-07 12:27:38 -08002333 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002334
2335 u64_stats_update_begin(&tx_stats->syncp);
2336 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002337 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002338 u64_stats_update_end(&tx_stats->syncp);
2339
2340 if (netif_rx(skb) == NET_RX_SUCCESS) {
2341 u64_stats_update_begin(&rx_stats->syncp);
2342 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002343 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002344 u64_stats_update_end(&rx_stats->syncp);
2345 } else {
Eric Dumazet4179cb52019-02-07 12:27:38 -08002346drop:
Li RongQingce6502a2014-10-16 08:49:41 +08002347 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002348 }
Eric Dumazet4179cb52019-02-07 12:27:38 -08002349 rcu_read_unlock();
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002350}
2351
pravin shelarfee1fad2016-11-13 20:43:56 -08002352static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002353 struct vxlan_dev *vxlan,
2354 union vxlan_addr *daddr,
2355 __be16 dst_port, int dst_ifindex, __be32 vni,
2356 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002357 u32 rt_flags)
2358{
2359#if IS_ENABLED(CONFIG_IPV6)
2360 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2361 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2362 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2363 */
2364 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2365#endif
2366 /* Bypass encapsulation if the destination is local */
2367 if (rt_flags & RTCF_LOCAL &&
2368 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2369 struct vxlan_dev *dst_vxlan;
2370
2371 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002372 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002373 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002374 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002375 if (!dst_vxlan) {
2376 dev->stats.tx_errors++;
2377 kfree_skb(skb);
2378
2379 return -ENOENT;
2380 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002381 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002382 return 1;
2383 }
2384
2385 return 0;
2386}
2387
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002388static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002389 __be32 default_vni, struct vxlan_rdst *rdst,
2390 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002391{
Paolo Abenid71785f2016-02-12 15:43:57 +01002392 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002393 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002394 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002395 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002396 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002397 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002398 struct vxlan_metadata _md;
2399 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002400 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002401 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002402 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002403 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002404 int ifindex;
Pravin B Shelar0e6fbc5b2013-06-17 17:49:56 -07002405 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002406 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002407 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002408 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002409
Jiri Benc61adedf2015-08-20 13:56:25 +02002410 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002411
Thomas Grafee122c72015-07-21 10:43:58 +02002412 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002413 dst = &rdst->remote_ip;
2414 if (vxlan_addr_any(dst)) {
2415 if (did_rsc) {
2416 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002417 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002418 return;
2419 }
2420 goto drop;
2421 }
2422
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002423 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002424 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002425 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002426 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002427 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002428 md->gbp = skb->mark;
Hangbin Liu72f6d712018-04-17 14:11:28 +08002429 if (flags & VXLAN_F_TTL_INHERIT) {
2430 ttl = ip_tunnel_get_ttl(old_iph, skb);
2431 } else {
2432 ttl = vxlan->cfg.ttl;
2433 if (!ttl && vxlan_addr_multicast(dst))
2434 ttl = 1;
2435 }
pravin shelar0770b532016-11-13 20:43:57 -08002436
2437 tos = vxlan->cfg.tos;
2438 if (tos == 1)
2439 tos = ip_tunnel_get_dsfield(old_iph, skb);
2440
2441 if (dst->sa.sa_family == AF_INET)
2442 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2443 else
2444 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2445 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002446 } else {
2447 if (!info) {
2448 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2449 dev->name);
2450 goto drop;
2451 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002452 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002453 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002454 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002455 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2456 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002457 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002458 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2459 }
Thomas Grafee122c72015-07-21 10:43:58 +02002460 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002461 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2462 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002463 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002464 dst_cache = &info->dst_cache;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002465 if (info->options_len &&
2466 info->key.tun_flags & TUNNEL_VXLAN_OPT)
pravin shelar0770b532016-11-13 20:43:57 -08002467 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002468 ttl = info->key.ttl;
2469 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002470 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002471 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002472 }
pravin shelar0770b532016-11-13 20:43:57 -08002473 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2474 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002475
Jakub Kicinski56de8592017-02-24 11:43:36 -08002476 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002477 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002478 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002479 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002480 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002481
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002482 if (!ifindex)
2483 ifindex = sock4->sock->sk->sk_bound_dev_if;
2484
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002485 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002486 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002487 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002488 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002489 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002490 if (IS_ERR(rt)) {
2491 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002492 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002493 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002494
pravin shelarfee1fad2016-11-13 20:43:56 -08002495 if (!info) {
Stefano Briviob4d306972018-11-08 12:19:16 +01002496 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002497 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002498 dst_port, ifindex, vni,
2499 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002500 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002501 goto out_unlock;
Stefano Briviob4d306972018-11-08 12:19:16 +01002502
2503 if (vxlan->cfg.df == VXLAN_DF_SET) {
2504 df = htons(IP_DF);
2505 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2506 struct ethhdr *eth = eth_hdr(skb);
2507
2508 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2509 (ntohs(eth->h_proto) == ETH_P_IP &&
2510 old_iph->frag_off & htons(IP_DF)))
2511 df = htons(IP_DF);
2512 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002513 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002514 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002515 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002516
pravin shelarc46b7892016-11-13 20:43:54 -08002517 ndst = &rt->dst;
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002518 skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002519
Cong Wange4c7ed42013-08-31 13:44:33 +08002520 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2521 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002522 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002523 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002524 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002525 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002526
Brian Russell11586322017-02-24 17:47:11 +00002527 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002528 dst->sin.sin_addr.s_addr, tos, ttl, df,
2529 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002530#if IS_ENABLED(CONFIG_IPV6)
2531 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002532 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002533
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002534 if (!ifindex)
2535 ifindex = sock6->sock->sk->sk_bound_dev_if;
2536
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002537 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002538 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002539 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002540 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002541 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002542 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002543 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002544 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002545 goto tx_error;
2546 }
pravin shelar655c3de2016-11-13 20:43:55 -08002547
pravin shelarfee1fad2016-11-13 20:43:56 -08002548 if (!info) {
2549 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002550
pravin shelarfee1fad2016-11-13 20:43:56 -08002551 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002552 dst_port, ifindex, vni,
2553 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002554 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002555 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002556 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002557
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002558 skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002559
Daniel Borkmann14006152016-03-04 15:15:08 +01002560 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002561 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002562 skb_scrub_packet(skb, xnet);
2563 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002564 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002565 if (err < 0)
2566 goto tx_error;
2567
pravin shelar0770b532016-11-13 20:43:57 -08002568 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002569 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002570 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002571 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002572#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002573 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002574out_unlock:
2575 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002576 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002577
2578drop:
2579 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002580 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002581 return;
2582
2583tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002584 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002585 if (err == -ELOOP)
2586 dev->stats.collisions++;
2587 else if (err == -ENETUNREACH)
2588 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002589 dst_release(ndst);
2590 dev->stats.tx_errors++;
2591 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002592}
2593
David Stevens66817122013-03-15 04:35:51 +00002594/* Transmit local packets over Vxlan
2595 *
2596 * Outer IP header inherits ECN and DF from inner header.
2597 * Outer UDP destination is the VXLAN assigned port.
2598 * source port is based on hash of flow
2599 */
2600static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2601{
2602 struct vxlan_dev *vxlan = netdev_priv(dev);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002603 struct vxlan_rdst *rdst, *fdst = NULL;
Xin Long8bff36852017-11-11 19:58:50 +08002604 const struct ip_tunnel_info *info;
2605 bool did_rsc = false;
David Stevens66817122013-03-15 04:35:51 +00002606 struct vxlan_fdb *f;
Xin Long8bff36852017-11-11 19:58:50 +08002607 struct ethhdr *eth;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002608 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002609
Jiri Benc61adedf2015-08-20 13:56:25 +02002610 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002611
David Stevens66817122013-03-15 04:35:51 +00002612 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002613
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002614 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002615 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2616 info->mode & IP_TUNNEL_INFO_TX) {
2617 vni = tunnel_id_to_key32(info->key.tun_id);
2618 } else {
2619 if (info && info->mode & IP_TUNNEL_INFO_TX)
2620 vxlan_xmit_one(skb, dev, vni, NULL, false);
2621 else
2622 kfree_skb(skb);
2623 return NETDEV_TX_OK;
2624 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002625 }
2626
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002627 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002628 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002629 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002630 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002631#if IS_ENABLED(CONFIG_IPV6)
Xin Long8bff36852017-11-11 19:58:50 +08002632 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2633 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2634 sizeof(struct nd_msg)) &&
2635 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2636 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2637
2638 if (m->icmph.icmp6_code == 0 &&
2639 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002640 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002641 }
2642#endif
2643 }
David Stevens66817122013-03-15 04:35:51 +00002644
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002645 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002646 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002647 did_rsc = false;
2648
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002649 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002650 (ntohs(eth->h_proto) == ETH_P_IP ||
2651 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002652 did_rsc = route_shortcircuit(dev, skb);
2653 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002654 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002655 }
2656
David Stevens66817122013-03-15 04:35:51 +00002657 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002658 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002659 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002660 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002661 !is_multicast_ether_addr(eth->h_dest))
2662 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002663
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002664 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002665 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002666 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002667 }
David Stevens66817122013-03-15 04:35:51 +00002668 }
2669
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002670 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2671 struct sk_buff *skb1;
2672
Eric Dumazet8f646c92014-01-06 09:54:31 -08002673 if (!fdst) {
2674 fdst = rdst;
2675 continue;
2676 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002677 skb1 = skb_clone(skb, GFP_ATOMIC);
2678 if (skb1)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002679 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002680 }
2681
Eric Dumazet8f646c92014-01-06 09:54:31 -08002682 if (fdst)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002683 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002684 else
2685 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002686 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002687}
2688
stephen hemmingerd3428942012-10-01 12:32:35 +00002689/* Walk the forwarding table and purge stale entries */
Kees Cookdf7e8282017-10-04 16:26:59 -07002690static void vxlan_cleanup(struct timer_list *t)
stephen hemmingerd3428942012-10-01 12:32:35 +00002691{
Kees Cookdf7e8282017-10-04 16:26:59 -07002692 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
stephen hemmingerd3428942012-10-01 12:32:35 +00002693 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2694 unsigned int h;
2695
2696 if (!netif_running(vxlan->dev))
2697 return;
2698
stephen hemmingerd3428942012-10-01 12:32:35 +00002699 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2700 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002701
Litao Jiaof98ec782019-03-06 12:01:48 +08002702 spin_lock(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002703 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2704 struct vxlan_fdb *f
2705 = container_of(p, struct vxlan_fdb, hlist);
2706 unsigned long timeout;
2707
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002708 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002709 continue;
2710
Roopa Prabhudef499c2017-03-27 15:46:41 -07002711 if (f->flags & NTF_EXT_LEARNED)
2712 continue;
2713
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002714 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002715 if (time_before_eq(timeout, jiffies)) {
2716 netdev_dbg(vxlan->dev,
2717 "garbage collect %pM\n",
2718 f->eth_addr);
2719 f->state = NUD_STALE;
Petr Machata0e6160f2018-11-21 08:02:35 +00002720 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002721 } else if (time_before(timeout, next_timer))
2722 next_timer = timeout;
2723 }
Litao Jiaof98ec782019-03-06 12:01:48 +08002724 spin_unlock(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002725 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002726
2727 mod_timer(&vxlan->age_timer, next_timer);
2728}
2729
Mark Blocha53cb292017-06-02 03:24:08 +03002730static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2731{
2732 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2733
2734 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002735 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2736#if IS_ENABLED(CONFIG_IPV6)
2737 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2738#endif
Mark Blocha53cb292017-06-02 03:24:08 +03002739 spin_unlock(&vn->sock_lock);
2740}
2741
Jiri Benc69e76662017-07-02 19:00:57 +02002742static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2743 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002744{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002745 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002746 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002747
Jiri Benc69e76662017-07-02 19:00:57 +02002748 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002749 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002750 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002751 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002752}
2753
stephen hemmingerd3428942012-10-01 12:32:35 +00002754/* Setup stats when device is created */
2755static int vxlan_init(struct net_device *dev)
2756{
WANG Cong1c213bd2014-02-13 11:46:28 -08002757 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002758 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002759 return -ENOMEM;
2760
2761 return 0;
2762}
2763
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002764static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002765{
2766 struct vxlan_fdb *f;
2767
2768 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002769 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002770 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00002771 vxlan_fdb_destroy(vxlan, f, true, true);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002772 spin_unlock_bh(&vxlan->hash_lock);
2773}
2774
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002775static void vxlan_uninit(struct net_device *dev)
2776{
2777 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002778
Stefano Brivioad6c9982019-03-08 16:40:57 +01002779 gro_cells_destroy(&vxlan->gro_cells);
2780
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002781 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002782
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002783 free_percpu(dev->tstats);
2784}
2785
stephen hemmingerd3428942012-10-01 12:32:35 +00002786/* Start ageing timer and join group when device is brought up */
2787static int vxlan_open(struct net_device *dev)
2788{
2789 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002790 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002791
Jiri Benc205f3562015-09-24 13:50:01 +02002792 ret = vxlan_sock_add(vxlan);
2793 if (ret < 0)
2794 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002795
Gao feng79d4a942013-12-10 16:37:32 +08002796 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002797 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002798 if (ret == -EADDRINUSE)
2799 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002800 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002801 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002802 return ret;
2803 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002804 }
2805
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002806 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002807 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2808
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002809 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002810}
2811
2812/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002813static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00002814{
Cong Wang31fec5a2013-05-27 22:35:52 +00002815 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002816
2817 spin_lock_bh(&vxlan->hash_lock);
2818 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2819 struct hlist_node *p, *n;
2820 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2821 struct vxlan_fdb *f
2822 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002823 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2824 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002825 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2826 if (!is_zero_ether_addr(f->eth_addr))
Petr Machata0e6160f2018-11-21 08:02:35 +00002827 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002828 }
2829 }
2830 spin_unlock_bh(&vxlan->hash_lock);
2831}
2832
2833/* Cleanup timer and forwarding table on shutdown */
2834static int vxlan_stop(struct net_device *dev)
2835{
2836 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002837 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002838 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002839
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002840 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002841 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002842 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002843
2844 del_timer_sync(&vxlan->age_timer);
2845
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002846 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02002847 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002848
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002849 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002850}
2851
stephen hemmingerd3428942012-10-01 12:32:35 +00002852/* Stub, nothing needs to be done. */
2853static void vxlan_set_multicast_list(struct net_device *dev)
2854{
2855}
2856
Daniel Borkmann345010b2013-12-18 00:21:08 +01002857static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2858{
2859 struct vxlan_dev *vxlan = netdev_priv(dev);
2860 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002861 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2862 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002863 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04002864
2865 /* This check is different than dev->max_mtu, because it looks at
2866 * the lowerdev->mtu, rather than the static dev->max_mtu
2867 */
2868 if (lowerdev) {
2869 int max_mtu = lowerdev->mtu -
2870 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2871 if (new_mtu > max_mtu)
2872 return -EINVAL;
2873 }
2874
2875 dev->mtu = new_mtu;
2876 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01002877}
2878
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002879static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2880{
2881 struct vxlan_dev *vxlan = netdev_priv(dev);
2882 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2883 __be16 sport, dport;
2884
2885 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2886 vxlan->cfg.port_max, true);
2887 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2888
Jiri Benc239e9442015-12-07 13:04:31 +01002889 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002890 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002891 struct rtable *rt;
2892
pravin shelar655c3de2016-11-13 20:43:55 -08002893 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002894 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002895 &info->key.u.ipv4.src, dport, sport,
2896 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002897 if (IS_ERR(rt))
2898 return PTR_ERR(rt);
2899 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002900 } else {
2901#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08002902 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01002903 struct dst_entry *ndst;
2904
pravin shelar655c3de2016-11-13 20:43:55 -08002905 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002906 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002907 &info->key.u.ipv6.src, dport, sport,
2908 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002909 if (IS_ERR(ndst))
2910 return PTR_ERR(ndst);
2911 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002912#else /* !CONFIG_IPV6 */
2913 return -EPFNOSUPPORT;
2914#endif
2915 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002916 info->key.tp_src = sport;
2917 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002918 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002919}
2920
Jiri Benc0c867c92016-04-05 14:47:10 +02002921static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002922 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002923 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002924 .ndo_open = vxlan_open,
2925 .ndo_stop = vxlan_stop,
2926 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002927 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002928 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002929 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002930 .ndo_validate_addr = eth_validate_addr,
2931 .ndo_set_mac_address = eth_mac_addr,
2932 .ndo_fdb_add = vxlan_fdb_add,
2933 .ndo_fdb_del = vxlan_fdb_delete,
2934 .ndo_fdb_dump = vxlan_fdb_dump,
Roopa Prabhu474c3c82018-12-15 22:35:10 -08002935 .ndo_fdb_get = vxlan_fdb_get,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002936 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
Andy Roulin8f1af752019-02-22 18:06:38 +00002937 .ndo_change_proto_down = dev_change_proto_down_generic,
stephen hemmingerd3428942012-10-01 12:32:35 +00002938};
2939
Jiri Bence1e53142016-04-05 14:47:13 +02002940static const struct net_device_ops vxlan_netdev_raw_ops = {
2941 .ndo_init = vxlan_init,
2942 .ndo_uninit = vxlan_uninit,
2943 .ndo_open = vxlan_open,
2944 .ndo_stop = vxlan_stop,
2945 .ndo_start_xmit = vxlan_xmit,
2946 .ndo_get_stats64 = ip_tunnel_get_stats64,
2947 .ndo_change_mtu = vxlan_change_mtu,
2948 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2949};
2950
stephen hemmingerd3428942012-10-01 12:32:35 +00002951/* Info for udev, that this is a virtual tunnel endpoint */
2952static struct device_type vxlan_type = {
2953 .name = "vxlan",
2954};
2955
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002956/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002957 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002958 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002959 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002960static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002961{
2962 struct vxlan_sock *vs;
2963 struct net *net = dev_net(dev);
2964 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002965 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002966
2967 spin_lock(&vn->sock_lock);
2968 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002969 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2970 unsigned short type;
2971
2972 if (vs->flags & VXLAN_F_GPE)
2973 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
2974 else
2975 type = UDP_TUNNEL_TYPE_VXLAN;
2976
2977 if (push)
2978 udp_tunnel_push_rx_port(dev, vs->sock, type);
2979 else
2980 udp_tunnel_drop_rx_port(dev, vs->sock, type);
2981 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002982 }
2983 spin_unlock(&vn->sock_lock);
2984}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002985
stephen hemmingerd3428942012-10-01 12:32:35 +00002986/* Initialize the device structure. */
2987static void vxlan_setup(struct net_device *dev)
2988{
2989 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002990 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002991
Jiri Benc65226ef2016-04-28 16:36:30 +02002992 eth_hw_addr_random(dev);
2993 ether_setup(dev);
2994
David S. Millercf124db2017-05-08 12:52:56 -04002995 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00002996 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2997
stephen hemmingerd3428942012-10-01 12:32:35 +00002998 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002999 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003000 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003001 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003002
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07003003 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003004 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003005 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07003006 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02003007 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00003008
Matthias Schiffera9853432017-06-19 10:03:55 +02003009 /* MTU range: 68 - 65535 */
3010 dev->min_mtu = ETH_MIN_MTU;
3011 dev->max_mtu = ETH_MAX_MTU;
3012
stephen hemminger553675f2013-05-16 11:35:20 +00003013 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003014 spin_lock_init(&vxlan->hash_lock);
3015
Kees Cookdf7e8282017-10-04 16:26:59 -07003016 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
stephen hemmingerd3428942012-10-01 12:32:35 +00003017
3018 vxlan->dev = dev;
3019
Tom Herbert58ce31c2015-08-19 17:07:33 -07003020 gro_cells_init(&vxlan->gro_cells, dev);
3021
stephen hemmingerd3428942012-10-01 12:32:35 +00003022 for (h = 0; h < FDB_HASH_SIZE; ++h)
3023 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
3024}
3025
Jiri Benc0c867c92016-04-05 14:47:10 +02003026static void vxlan_ether_setup(struct net_device *dev)
3027{
Jiri Benc0c867c92016-04-05 14:47:10 +02003028 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3029 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3030 dev->netdev_ops = &vxlan_netdev_ether_ops;
3031}
3032
Jiri Bence1e53142016-04-05 14:47:13 +02003033static void vxlan_raw_setup(struct net_device *dev)
3034{
Jiri Benc65226ef2016-04-28 16:36:30 +02003035 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02003036 dev->type = ARPHRD_NONE;
3037 dev->hard_header_len = 0;
3038 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02003039 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3040 dev->netdev_ops = &vxlan_netdev_raw_ops;
3041}
3042
stephen hemmingerd3428942012-10-01 12:32:35 +00003043static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3044 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00003045 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003046 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003047 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
3048 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003049 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003050 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3051 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003052 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003053 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3054 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3055 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00003056 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00003057 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3058 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3059 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3060 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003061 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00003062 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08003063 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3064 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3065 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08003066 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3067 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01003068 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02003069 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003070 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
Hangbin Liu72f6d712018-04-17 14:11:28 +08003071 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
Stefano Briviob4d306972018-11-08 12:19:16 +01003072 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003073};
3074
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02003075static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3076 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003077{
3078 if (tb[IFLA_ADDRESS]) {
3079 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003080 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3081 "Provided link layer address is not Ethernet");
stephen hemmingerd3428942012-10-01 12:32:35 +00003082 return -EINVAL;
3083 }
3084
3085 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003086 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3087 "Provided Ethernet address is not unicast");
stephen hemmingerd3428942012-10-01 12:32:35 +00003088 return -EADDRNOTAVAIL;
3089 }
3090 }
3091
Matthias Schiffera9853432017-06-19 10:03:55 +02003092 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02003093 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02003094
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003095 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3096 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3097 "MTU must be between 68 and 65535");
Matthias Schiffera9853432017-06-19 10:03:55 +02003098 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003099 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003100 }
3101
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003102 if (!data) {
3103 NL_SET_ERR_MSG(extack,
3104 "Required attributes not provided to perform the operation");
stephen hemmingerd3428942012-10-01 12:32:35 +00003105 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003106 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003107
3108 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003109 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3110
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003111 if (id >= VXLAN_N_VID) {
3112 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID],
3113 "VXLAN ID must be lower than 16777216");
stephen hemmingerd3428942012-10-01 12:32:35 +00003114 return -ERANGE;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003115 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003116 }
3117
stephen hemminger05f47d62012-10-09 20:35:50 +00003118 if (data[IFLA_VXLAN_PORT_RANGE]) {
3119 const struct ifla_vxlan_port_range *p
3120 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3121
3122 if (ntohs(p->high) < ntohs(p->low)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003123 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
3124 "Invalid source port range");
stephen hemminger05f47d62012-10-09 20:35:50 +00003125 return -EINVAL;
3126 }
3127 }
3128
Stefano Briviob4d306972018-11-08 12:19:16 +01003129 if (data[IFLA_VXLAN_DF]) {
3130 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3131
3132 if (df < 0 || df > VXLAN_DF_MAX) {
3133 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_DF],
3134 "Invalid DF attribute");
3135 return -EINVAL;
3136 }
3137 }
3138
stephen hemmingerd3428942012-10-01 12:32:35 +00003139 return 0;
3140}
3141
Yan Burman1b13c972013-01-29 23:43:07 +00003142static void vxlan_get_drvinfo(struct net_device *netdev,
3143 struct ethtool_drvinfo *drvinfo)
3144{
3145 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3146 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3147}
3148
3149static const struct ethtool_ops vxlan_ethtool_ops = {
3150 .get_drvinfo = vxlan_get_drvinfo,
3151 .get_link = ethtool_op_get_link,
3152};
3153
Tom Herbert3ee64f32014-07-13 19:49:42 -07003154static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003155 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +00003156{
Cong Wange4c7ed42013-08-31 13:44:33 +08003157 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003158 struct udp_port_cfg udp_conf;
3159 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08003160
Tom Herbert3ee64f32014-07-13 19:49:42 -07003161 memset(&udp_conf, 0, sizeof(udp_conf));
3162
3163 if (ipv6) {
3164 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003165 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08003166 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02003167 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003168 } else {
3169 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08003170 }
3171
Tom Herbert3ee64f32014-07-13 19:49:42 -07003172 udp_conf.local_udp_port = port;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003173 udp_conf.bind_ifindex = ifindex;
Cong Wange4c7ed42013-08-31 13:44:33 +08003174
Tom Herbert3ee64f32014-07-13 19:49:42 -07003175 /* Open UDP socket */
3176 err = udp_sock_create(net, &udp_conf, &sock);
3177 if (err < 0)
3178 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08003179
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003180 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08003181}
3182
3183/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02003184static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003185 __be16 port, u32 flags,
3186 int ifindex)
Cong Wange4c7ed42013-08-31 13:44:33 +08003187{
3188 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3189 struct vxlan_sock *vs;
3190 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00003191 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003192 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00003193
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02003194 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08003195 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00003196 return ERR_PTR(-ENOMEM);
3197
3198 for (h = 0; h < VNI_HASH_SIZE; ++h)
3199 INIT_HLIST_HEAD(&vs->vni_list[h]);
3200
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003201 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003202 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00003203 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08003204 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00003205 }
3206
Cong Wange4c7ed42013-08-31 13:44:33 +08003207 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03003208 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08003209 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00003210
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003211 spin_lock(&vn->sock_lock);
3212 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07003213 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07003214 (vs->flags & VXLAN_F_GPE) ?
3215 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07003216 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003217 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00003218
3219 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07003220 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07003221 tunnel_cfg.sk_user_data = vs;
3222 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01003223 tunnel_cfg.encap_rcv = vxlan_rcv;
Stefano Brivioc3a43b92018-11-08 12:19:15 +01003224 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003225 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07003226 tunnel_cfg.gro_receive = vxlan_gro_receive;
3227 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003228
3229 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08003230
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003231 return vs;
3232}
stephen hemminger553675f2013-05-16 11:35:20 +00003233
Jiri Bencb1be00a2015-09-24 13:50:02 +02003234static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003235{
Jiri Benc205f3562015-09-24 13:50:01 +02003236 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3237 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02003238 struct vxlan_dev_node *node;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003239 int l3mdev_index = 0;
3240
3241 if (vxlan->cfg.remote_ifindex)
3242 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3243 vxlan->net, vxlan->cfg.remote_ifindex);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003244
Jiri Benc205f3562015-09-24 13:50:01 +02003245 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003246 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003247 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003248 vxlan->cfg.dst_port, vxlan->cfg.flags,
3249 l3mdev_index);
Reshetova, Elena66af8462017-07-04 15:52:59 +03003250 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003251 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003252 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003253 }
3254 spin_unlock(&vn->sock_lock);
3255 }
Jiri Benc205f3562015-09-24 13:50:01 +02003256 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003257 vs = vxlan_socket_create(vxlan->net, ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003258 vxlan->cfg.dst_port, vxlan->cfg.flags,
3259 l3mdev_index);
Jiri Benc205f3562015-09-24 13:50:01 +02003260 if (IS_ERR(vs))
3261 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003262#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02003263 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003264 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003265 node = &vxlan->hlist6;
3266 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02003267#endif
Jiri Benc69e76662017-07-02 19:00:57 +02003268 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003269 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003270 node = &vxlan->hlist4;
3271 }
3272 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02003273 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00003274}
3275
Jiri Bencb1be00a2015-09-24 13:50:02 +02003276static int vxlan_sock_add(struct vxlan_dev *vxlan)
3277{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003278 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3279 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02003280 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003281 int ret = 0;
3282
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003283 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003284#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003285 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02003286 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02003287 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02003288 if (ret < 0 && ret != -EAFNOSUPPORT)
3289 ipv4 = false;
3290 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02003291#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02003292 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003293 ret = __vxlan_sock_add(vxlan, false);
3294 if (ret < 0)
3295 vxlan_sock_release(vxlan);
3296 return ret;
3297}
3298
Matthias Schiffera9853432017-06-19 10:03:55 +02003299static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3300 struct net_device **lower,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003301 struct vxlan_dev *old,
3302 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003303{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01003304 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02003305 struct vxlan_dev *tmp;
3306 bool use_ipv6 = false;
3307
3308 if (conf->flags & VXLAN_F_GPE) {
3309 /* For now, allow GPE only together with
3310 * COLLECT_METADATA. This can be relaxed later; in such
3311 * case, the other side of the PtP link will have to be
3312 * provided.
3313 */
3314 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3315 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003316 NL_SET_ERR_MSG(extack,
3317 "VXLAN GPE does not support this combination of attributes");
Matthias Schiffera9853432017-06-19 10:03:55 +02003318 return -EINVAL;
3319 }
3320 }
3321
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003322 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3323 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02003324 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003325 conf->saddr.sa.sa_family = AF_INET;
3326 } else if (!conf->remote_ip.sa.sa_family) {
3327 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3328 } else if (!conf->saddr.sa.sa_family) {
3329 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3330 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003331
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003332 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3333 NL_SET_ERR_MSG(extack,
3334 "Local and remote address must be from the same family");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003335 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003336 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003337
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003338 if (vxlan_addr_multicast(&conf->saddr)) {
3339 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003340 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003341 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003342
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003343 if (conf->saddr.sa.sa_family == AF_INET6) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003344 if (!IS_ENABLED(CONFIG_IPV6)) {
3345 NL_SET_ERR_MSG(extack,
3346 "IPv6 support not enabled in the kernel");
Matthias Schiffera9853432017-06-19 10:03:55 +02003347 return -EPFNOSUPPORT;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003348 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003349 use_ipv6 = true;
3350 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003351
3352 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3353 int local_type =
3354 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3355 int remote_type =
3356 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3357
3358 if (local_type & IPV6_ADDR_LINKLOCAL) {
3359 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003360 (remote_type != IPV6_ADDR_ANY)) {
3361 NL_SET_ERR_MSG(extack,
3362 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003363 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003364 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003365
3366 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3367 } else {
3368 if (remote_type ==
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003369 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3370 NL_SET_ERR_MSG(extack,
3371 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003372 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003373 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003374
3375 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3376 }
3377 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003378 }
3379
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003380 if (conf->label && !use_ipv6) {
3381 NL_SET_ERR_MSG(extack,
3382 "Label attribute only applies to IPv6 VXLAN devices");
Matthias Schiffera9853432017-06-19 10:03:55 +02003383 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003384 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003385
3386 if (conf->remote_ifindex) {
3387 struct net_device *lowerdev;
3388
3389 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003390 if (!lowerdev) {
3391 NL_SET_ERR_MSG(extack,
3392 "Invalid local interface, device not found");
Matthias Schiffera9853432017-06-19 10:03:55 +02003393 return -ENODEV;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003394 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003395
3396#if IS_ENABLED(CONFIG_IPV6)
3397 if (use_ipv6) {
3398 struct inet6_dev *idev = __in6_dev_get(lowerdev);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003399 if (idev && idev->cnf.disable_ipv6) {
3400 NL_SET_ERR_MSG(extack,
3401 "IPv6 support disabled by administrator");
Matthias Schiffera9853432017-06-19 10:03:55 +02003402 return -EPERM;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003403 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003404 }
3405#endif
3406
3407 *lower = lowerdev;
3408 } else {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003409 if (vxlan_addr_multicast(&conf->remote_ip)) {
3410 NL_SET_ERR_MSG(extack,
3411 "Local interface required for multicast remote destination");
3412
Matthias Schiffera9853432017-06-19 10:03:55 +02003413 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003414 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003415
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003416#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003417 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3418 NL_SET_ERR_MSG(extack,
3419 "Local interface required for link-local local/remote addresses");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003420 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003421 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003422#endif
3423
Matthias Schiffera9853432017-06-19 10:03:55 +02003424 *lower = NULL;
3425 }
3426
3427 if (!conf->dst_port) {
3428 if (conf->flags & VXLAN_F_GPE)
3429 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3430 else
3431 conf->dst_port = htons(vxlan_port);
3432 }
3433
3434 if (!conf->age_interval)
3435 conf->age_interval = FDB_AGE_DEFAULT;
3436
3437 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3438 if (tmp == old)
3439 continue;
3440
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003441 if (tmp->cfg.vni != conf->vni)
3442 continue;
3443 if (tmp->cfg.dst_port != conf->dst_port)
3444 continue;
3445 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003446 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003447 continue;
3448
3449 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3450 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3451 continue;
3452
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003453 NL_SET_ERR_MSG(extack,
3454 "A VXLAN device with the specified VNI already exists");
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003455 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003456 }
3457
3458 return 0;
3459}
3460
3461static void vxlan_config_apply(struct net_device *dev,
3462 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003463 struct net_device *lowerdev,
3464 struct net *src_net,
3465 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003466{
3467 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003468 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003469 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003470 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3471 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003472
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003473 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003474 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003475 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003476 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003477 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003478
Matthias Schiffera9853432017-06-19 10:03:55 +02003479 if (conf->mtu)
3480 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003481
3482 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003483 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003484
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003485 dst->remote_vni = conf->vni;
3486
3487 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003488
Matthias Schiffera9853432017-06-19 10:03:55 +02003489 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003490 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003491
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003492 dev->gso_max_size = lowerdev->gso_max_size;
3493 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003494
3495 needed_headroom = lowerdev->hard_header_len;
3496
3497 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3498 VXLAN_HEADROOM);
Alexey Kodanevf870c1f2017-12-14 20:20:00 +03003499 if (max_mtu < ETH_MIN_MTU)
3500 max_mtu = ETH_MIN_MTU;
3501
3502 if (!changelink && !conf->mtu)
3503 dev->mtu = max_mtu;
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003504 }
3505
Matthias Schiffera9853432017-06-19 10:03:55 +02003506 if (dev->mtu > max_mtu)
3507 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003508
Jiri Bencb1be00a2015-09-24 13:50:02 +02003509 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3510 needed_headroom += VXLAN6_HEADROOM;
3511 else
3512 needed_headroom += VXLAN_HEADROOM;
3513 dev->needed_headroom = needed_headroom;
3514
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003515 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003516}
stephen hemmingerd3428942012-10-01 12:32:35 +00003517
Matthias Schiffera9853432017-06-19 10:03:55 +02003518static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003519 struct vxlan_config *conf, bool changelink,
3520 struct netlink_ext_ack *extack)
Matthias Schiffera9853432017-06-19 10:03:55 +02003521{
3522 struct vxlan_dev *vxlan = netdev_priv(dev);
3523 struct net_device *lowerdev;
3524 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003525
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003526 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
Matthias Schiffera9853432017-06-19 10:03:55 +02003527 if (ret)
3528 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003529
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003530 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003531
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003532 return 0;
3533}
3534
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003535static int __vxlan_dev_create(struct net *net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003536 struct vxlan_config *conf,
3537 struct netlink_ext_ack *extack)
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003538{
3539 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3540 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003541 struct vxlan_fdb *f = NULL;
Petr Machata6db92462018-12-18 13:16:00 +00003542 bool unregister = false;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003543 int err;
3544
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003545 err = vxlan_dev_configure(net, dev, conf, false, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003546 if (err)
3547 return err;
3548
3549 dev->ethtool_ops = &vxlan_ethtool_ops;
3550
3551 /* create an fdb entry for a valid default destination */
3552 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003553 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003554 &vxlan->default_dst.remote_ip,
3555 NUD_REACHABLE | NUD_PERMANENT,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003556 vxlan->cfg.dst_port,
3557 vxlan->default_dst.remote_vni,
3558 vxlan->default_dst.remote_vni,
3559 vxlan->default_dst.remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003560 NTF_SELF, &f);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003561 if (err)
3562 return err;
3563 }
3564
3565 err = register_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003566 if (err)
3567 goto errout;
Petr Machata6db92462018-12-18 13:16:00 +00003568 unregister = true;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003569
3570 err = rtnl_configure_link(dev, NULL);
Petr Machata6db92462018-12-18 13:16:00 +00003571 if (err)
Roopa Prabhu0241b832018-07-04 16:46:32 -07003572 goto errout;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003573
Roopa Prabhu0241b832018-07-04 16:46:32 -07003574 /* notify default fdb entry */
Petr Machata61f46fe2019-01-16 23:06:38 +00003575 if (f) {
3576 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
Petr Machata4c59b7d2019-01-16 23:06:54 +00003577 RTM_NEWNEIGH, true, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +00003578 if (err)
3579 goto errout;
3580 }
Roopa Prabhu0241b832018-07-04 16:46:32 -07003581
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003582 list_add(&vxlan->next, &vn->vxlan_list);
3583 return 0;
Petr Machata6db92462018-12-18 13:16:00 +00003584
Roopa Prabhu0241b832018-07-04 16:46:32 -07003585errout:
Petr Machata6db92462018-12-18 13:16:00 +00003586 /* unregister_netdevice() destroys the default FDB entry with deletion
3587 * notification. But the addition notification was not sent yet, so
3588 * destroy the entry by hand here.
3589 */
Roopa Prabhu0241b832018-07-04 16:46:32 -07003590 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003591 vxlan_fdb_destroy(vxlan, f, false, false);
Petr Machata6db92462018-12-18 13:16:00 +00003592 if (unregister)
3593 unregister_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003594 return err;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003595}
3596
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003597/* Set/clear flags based on attribute */
3598static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3599 int attrtype, unsigned long mask, bool changelink,
3600 bool changelink_supported,
3601 struct netlink_ext_ack *extack)
3602{
3603 unsigned long flags;
3604
3605 if (!tb[attrtype])
3606 return 0;
3607
3608 if (changelink && !changelink_supported) {
3609 vxlan_flag_attr_error(attrtype, extack);
3610 return -EOPNOTSUPP;
3611 }
3612
3613 if (vxlan_policy[attrtype].type == NLA_FLAG)
3614 flags = conf->flags | mask;
3615 else if (nla_get_u8(tb[attrtype]))
3616 flags = conf->flags | mask;
3617 else
3618 flags = conf->flags & ~mask;
3619
3620 conf->flags = flags;
3621
3622 return 0;
3623}
3624
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003625static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3626 struct net_device *dev, struct vxlan_config *conf,
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003627 bool changelink, struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003628{
3629 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003630 int err = 0;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003631
3632 memset(conf, 0, sizeof(*conf));
3633
3634 /* if changelink operation, start with old existing cfg */
3635 if (changelink)
3636 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3637
3638 if (data[IFLA_VXLAN_ID]) {
3639 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3640
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003641 if (changelink && (vni != conf->vni)) {
3642 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003643 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003644 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003645 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3646 }
3647
3648 if (data[IFLA_VXLAN_GROUP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003649 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3650 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003651 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003652 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003653
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003654 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003655 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003656 } else if (data[IFLA_VXLAN_GROUP6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003657 if (!IS_ENABLED(CONFIG_IPV6)) {
3658 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003659 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003660 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003661
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003662 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3663 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003664 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003665 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003666
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003667 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3668 conf->remote_ip.sa.sa_family = AF_INET6;
3669 }
3670
3671 if (data[IFLA_VXLAN_LOCAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003672 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
3673 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003674 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003675 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003676
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003677 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3678 conf->saddr.sa.sa_family = AF_INET;
3679 } else if (data[IFLA_VXLAN_LOCAL6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003680 if (!IS_ENABLED(CONFIG_IPV6)) {
3681 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003682 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003683 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003684
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003685 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
3686 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003687 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003688 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003689
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003690 /* TODO: respect scope id */
3691 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3692 conf->saddr.sa.sa_family = AF_INET6;
3693 }
3694
3695 if (data[IFLA_VXLAN_LINK])
3696 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3697
3698 if (data[IFLA_VXLAN_TOS])
3699 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3700
3701 if (data[IFLA_VXLAN_TTL])
3702 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3703
Hangbin Liu72f6d712018-04-17 14:11:28 +08003704 if (data[IFLA_VXLAN_TTL_INHERIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003705 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
3706 VXLAN_F_TTL_INHERIT, changelink, false,
3707 extack);
3708 if (err)
3709 return err;
3710
Hangbin Liu72f6d712018-04-17 14:11:28 +08003711 }
3712
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003713 if (data[IFLA_VXLAN_LABEL])
3714 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3715 IPV6_FLOWLABEL_MASK;
3716
3717 if (data[IFLA_VXLAN_LEARNING]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003718 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
3719 VXLAN_F_LEARN, changelink, true,
3720 extack);
3721 if (err)
3722 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003723 } else if (!changelink) {
3724 /* default to learn on a new device */
3725 conf->flags |= VXLAN_F_LEARN;
3726 }
3727
Ido Schimmel40051c42018-11-21 08:02:40 +00003728 if (data[IFLA_VXLAN_AGEING])
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003729 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003730
3731 if (data[IFLA_VXLAN_PROXY]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003732 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
3733 VXLAN_F_PROXY, changelink, false,
3734 extack);
3735 if (err)
3736 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003737 }
3738
3739 if (data[IFLA_VXLAN_RSC]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003740 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
3741 VXLAN_F_RSC, changelink, false,
3742 extack);
3743 if (err)
3744 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003745 }
3746
3747 if (data[IFLA_VXLAN_L2MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003748 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
3749 VXLAN_F_L2MISS, changelink, false,
3750 extack);
3751 if (err)
3752 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003753 }
3754
3755 if (data[IFLA_VXLAN_L3MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003756 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
3757 VXLAN_F_L3MISS, changelink, false,
3758 extack);
3759 if (err)
3760 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003761 }
3762
3763 if (data[IFLA_VXLAN_LIMIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003764 if (changelink) {
3765 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
3766 "Cannot change limit");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003767 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003768 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003769 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3770 }
3771
3772 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003773 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
3774 VXLAN_F_COLLECT_METADATA, changelink, false,
3775 extack);
3776 if (err)
3777 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003778 }
3779
3780 if (data[IFLA_VXLAN_PORT_RANGE]) {
3781 if (!changelink) {
3782 const struct ifla_vxlan_port_range *p
3783 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3784 conf->port_min = ntohs(p->low);
3785 conf->port_max = ntohs(p->high);
3786 } else {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003787 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
3788 "Cannot change port range");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003789 return -EOPNOTSUPP;
3790 }
3791 }
3792
3793 if (data[IFLA_VXLAN_PORT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003794 if (changelink) {
3795 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
3796 "Cannot change port");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003797 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003798 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003799 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3800 }
3801
3802 if (data[IFLA_VXLAN_UDP_CSUM]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003803 if (changelink) {
3804 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
3805 "Cannot change UDP_CSUM flag");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003806 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003807 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003808 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3809 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3810 }
3811
3812 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003813 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3814 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
3815 false, extack);
3816 if (err)
3817 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003818 }
3819
3820 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003821 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
3822 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
3823 false, extack);
3824 if (err)
3825 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003826 }
3827
3828 if (data[IFLA_VXLAN_REMCSUM_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003829 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
3830 VXLAN_F_REMCSUM_TX, changelink, false,
3831 extack);
3832 if (err)
3833 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003834 }
3835
3836 if (data[IFLA_VXLAN_REMCSUM_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003837 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
3838 VXLAN_F_REMCSUM_RX, changelink, false,
3839 extack);
3840 if (err)
3841 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003842 }
3843
3844 if (data[IFLA_VXLAN_GBP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003845 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
3846 VXLAN_F_GBP, changelink, false, extack);
3847 if (err)
3848 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003849 }
3850
3851 if (data[IFLA_VXLAN_GPE]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003852 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
3853 VXLAN_F_GPE, changelink, false,
3854 extack);
3855 if (err)
3856 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003857 }
3858
3859 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003860 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
3861 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
3862 false, extack);
3863 if (err)
3864 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003865 }
3866
3867 if (tb[IFLA_MTU]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003868 if (changelink) {
3869 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3870 "Cannot change mtu");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003871 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003872 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003873 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3874 }
3875
Stefano Briviob4d306972018-11-08 12:19:16 +01003876 if (data[IFLA_VXLAN_DF])
3877 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
3878
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003879 return 0;
3880}
3881
3882static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003883 struct nlattr *tb[], struct nlattr *data[],
3884 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003885{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003886 struct vxlan_config conf;
3887 int err;
3888
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003889 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003890 if (err)
3891 return err;
3892
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003893 return __vxlan_dev_create(src_net, dev, &conf, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00003894}
3895
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003896static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02003897 struct nlattr *data[],
3898 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003899{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003900 struct vxlan_dev *vxlan = netdev_priv(dev);
3901 struct vxlan_rdst *dst = &vxlan->default_dst;
Petr Machata8db9427d52019-01-16 23:06:39 +00003902 struct net_device *lowerdev;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003903 struct vxlan_config conf;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003904 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003905
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003906 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003907 if (err)
3908 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07003909
Petr Machata8db9427d52019-01-16 23:06:39 +00003910 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
3911 vxlan, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003912 if (err)
3913 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003914
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003915 /* handle default dst entry */
Petr Machata038a5a92019-01-16 23:06:41 +00003916 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003917 spin_lock_bh(&vxlan->hash_lock);
Petr Machata038a5a92019-01-16 23:06:41 +00003918 if (!vxlan_addr_any(&conf.remote_ip)) {
Petr Machatace5e098f2018-12-18 13:16:02 +00003919 err = vxlan_fdb_update(vxlan, all_zeros_mac,
Petr Machata038a5a92019-01-16 23:06:41 +00003920 &conf.remote_ip,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003921 NUD_REACHABLE | NUD_PERMANENT,
Petr Machatace5e098f2018-12-18 13:16:02 +00003922 NLM_F_APPEND | NLM_F_CREATE,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003923 vxlan->cfg.dst_port,
Petr Machata038a5a92019-01-16 23:06:41 +00003924 conf.vni, conf.vni,
3925 conf.remote_ifindex,
Petr Machata4c59b7d2019-01-16 23:06:54 +00003926 NTF_SELF, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003927 if (err) {
3928 spin_unlock_bh(&vxlan->hash_lock);
3929 return err;
3930 }
3931 }
Petr Machata1cdc98c2019-01-16 23:06:43 +00003932 if (!vxlan_addr_any(&dst->remote_ip))
3933 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3934 dst->remote_ip,
3935 vxlan->cfg.dst_port,
3936 dst->remote_vni,
3937 dst->remote_vni,
3938 dst->remote_ifindex,
3939 true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003940 spin_unlock_bh(&vxlan->hash_lock);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003941 }
3942
Petr Machata038a5a92019-01-16 23:06:41 +00003943 if (conf.age_interval != vxlan->cfg.age_interval)
3944 mod_timer(&vxlan->age_timer, jiffies);
3945
3946 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003947 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003948}
3949
stephen hemmingerd3428942012-10-01 12:32:35 +00003950static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3951{
3952 struct vxlan_dev *vxlan = netdev_priv(dev);
3953
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003954 vxlan_flush(vxlan, true);
3955
stephen hemminger553675f2013-05-16 11:35:20 +00003956 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003957 unregister_netdevice_queue(dev, head);
3958}
3959
3960static size_t vxlan_get_size(const struct net_device *dev)
3961{
3962
3963 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003964 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003965 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003966 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003967 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
Hangbin Liu8fd78062018-09-26 10:35:42 +08003968 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
stephen hemmingerd3428942012-10-01 12:32:35 +00003969 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Stefano Briviob4d306972018-11-08 12:19:16 +01003970 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003971 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003972 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003973 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3974 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3975 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3976 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003977 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003978 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3979 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003980 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003981 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3982 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3983 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3984 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003985 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3986 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003987 0;
3988}
3989
3990static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3991{
3992 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003993 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003994 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003995 .low = htons(vxlan->cfg.port_min),
3996 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003997 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003998
Jiri Benc54bfd872016-02-16 21:58:58 +01003999 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004000 goto nla_put_failure;
4001
Cong Wange4c7ed42013-08-31 13:44:33 +08004002 if (!vxlan_addr_any(&dst->remote_ip)) {
4003 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004004 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4005 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004006 goto nla_put_failure;
4007#if IS_ENABLED(CONFIG_IPV6)
4008 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004009 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4010 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004011 goto nla_put_failure;
4012#endif
4013 }
4014 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004015
Atzm Watanabec7995c42013-04-16 02:50:52 +00004016 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00004017 goto nla_put_failure;
4018
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004019 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4020 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004021 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004022 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004023 goto nla_put_failure;
4024#if IS_ENABLED(CONFIG_IPV6)
4025 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004026 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004027 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004028 goto nla_put_failure;
4029#endif
4030 }
4031 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004032
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004033 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
Hangbin Liu8fd78062018-09-26 10:35:42 +08004034 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4035 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004036 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Stefano Briviob4d306972018-11-08 12:19:16 +01004037 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01004038 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004039 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004040 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004041 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004042 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
4043 nla_put_u8(skb, IFLA_VXLAN_RSC,
4044 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004045 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004046 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004047 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004048 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07004049 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004050 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004051 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4052 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4053 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004054 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004055 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004056 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004057 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004058 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004059 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004060 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004061 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004062 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004063 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004064 goto nla_put_failure;
4065
stephen hemminger05f47d62012-10-09 20:35:50 +00004066 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4067 goto nla_put_failure;
4068
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004069 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01004070 nla_put_flag(skb, IFLA_VXLAN_GBP))
4071 goto nla_put_failure;
4072
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004073 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02004074 nla_put_flag(skb, IFLA_VXLAN_GPE))
4075 goto nla_put_failure;
4076
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004077 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08004078 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4079 goto nla_put_failure;
4080
stephen hemmingerd3428942012-10-01 12:32:35 +00004081 return 0;
4082
4083nla_put_failure:
4084 return -EMSGSIZE;
4085}
4086
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004087static struct net *vxlan_get_link_net(const struct net_device *dev)
4088{
4089 struct vxlan_dev *vxlan = netdev_priv(dev);
4090
4091 return vxlan->net;
4092}
4093
stephen hemmingerd3428942012-10-01 12:32:35 +00004094static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4095 .kind = "vxlan",
4096 .maxtype = IFLA_VXLAN_MAX,
4097 .policy = vxlan_policy,
4098 .priv_size = sizeof(struct vxlan_dev),
4099 .setup = vxlan_setup,
4100 .validate = vxlan_validate,
4101 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004102 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00004103 .dellink = vxlan_dellink,
4104 .get_size = vxlan_get_size,
4105 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004106 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004107};
4108
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004109struct net_device *vxlan_dev_create(struct net *net, const char *name,
4110 u8 name_assign_type,
4111 struct vxlan_config *conf)
4112{
4113 struct nlattr *tb[IFLA_MAX + 1];
4114 struct net_device *dev;
4115 int err;
4116
4117 memset(&tb, 0, sizeof(tb));
4118
4119 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08004120 &vxlan_link_ops, tb, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004121 if (IS_ERR(dev))
4122 return dev;
4123
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07004124 err = __vxlan_dev_create(net, dev, conf, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004125 if (err < 0) {
4126 free_netdev(dev);
4127 return ERR_PTR(err);
4128 }
4129
4130 err = rtnl_configure_link(dev, NULL);
4131 if (err < 0) {
4132 LIST_HEAD(list_kill);
4133
4134 vxlan_dellink(dev, &list_kill);
4135 unregister_netdevice_many(&list_kill);
4136 return ERR_PTR(err);
4137 }
4138
4139 return dev;
4140}
4141EXPORT_SYMBOL_GPL(vxlan_dev_create);
4142
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004143static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4144 struct net_device *dev)
4145{
4146 struct vxlan_dev *vxlan, *next;
4147 LIST_HEAD(list_kill);
4148
4149 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4150 struct vxlan_rdst *dst = &vxlan->default_dst;
4151
4152 /* In case we created vxlan device with carrier
4153 * and we loose the carrier due to module unload
4154 * we also need to remove vxlan device. In other
4155 * cases, it's not necessary and remote_ifindex
4156 * is 0 here, so no matches.
4157 */
4158 if (dst->remote_ifindex == dev->ifindex)
4159 vxlan_dellink(vxlan->dev, &list_kill);
4160 }
4161
4162 unregister_netdevice_many(&list_kill);
4163}
4164
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004165static int vxlan_netdevice_event(struct notifier_block *unused,
4166 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004167{
4168 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004169 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004170
Sabrina Dubroca04584952017-07-21 12:49:33 +02004171 if (event == NETDEV_UNREGISTER) {
4172 vxlan_offload_rx_ports(dev, false);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004173 vxlan_handle_lowerdev_unregister(vn, dev);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004174 } else if (event == NETDEV_REGISTER) {
4175 vxlan_offload_rx_ports(dev, true);
4176 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4177 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02004178 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004179 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004180
4181 return NOTIFY_DONE;
4182}
4183
4184static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004185 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004186};
4187
Petr Machata0efe1172018-10-17 08:53:26 +00004188static void
4189vxlan_fdb_offloaded_set(struct net_device *dev,
4190 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4191{
4192 struct vxlan_dev *vxlan = netdev_priv(dev);
4193 struct vxlan_rdst *rdst;
4194 struct vxlan_fdb *f;
4195
4196 spin_lock_bh(&vxlan->hash_lock);
4197
4198 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4199 if (!f)
4200 goto out;
4201
4202 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4203 fdb_info->remote_port,
4204 fdb_info->remote_vni,
4205 fdb_info->remote_ifindex);
4206 if (!rdst)
4207 goto out;
4208
4209 rdst->offloaded = fdb_info->offloaded;
4210
4211out:
4212 spin_unlock_bh(&vxlan->hash_lock);
4213}
4214
Petr Machata5728ae02018-11-21 08:02:39 +00004215static int
4216vxlan_fdb_external_learn_add(struct net_device *dev,
4217 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4218{
4219 struct vxlan_dev *vxlan = netdev_priv(dev);
Petr Machata4c59b7d2019-01-16 23:06:54 +00004220 struct netlink_ext_ack *extack;
Petr Machata5728ae02018-11-21 08:02:39 +00004221 int err;
4222
Petr Machata4c59b7d2019-01-16 23:06:54 +00004223 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4224
Petr Machata5728ae02018-11-21 08:02:39 +00004225 spin_lock_bh(&vxlan->hash_lock);
4226 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4227 NUD_REACHABLE,
4228 NLM_F_CREATE | NLM_F_REPLACE,
4229 fdb_info->remote_port,
4230 fdb_info->vni,
4231 fdb_info->remote_vni,
4232 fdb_info->remote_ifindex,
4233 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
Petr Machata4c59b7d2019-01-16 23:06:54 +00004234 false, extack);
Petr Machata5728ae02018-11-21 08:02:39 +00004235 spin_unlock_bh(&vxlan->hash_lock);
4236
4237 return err;
4238}
4239
4240static int
4241vxlan_fdb_external_learn_del(struct net_device *dev,
4242 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4243{
4244 struct vxlan_dev *vxlan = netdev_priv(dev);
4245 struct vxlan_fdb *f;
4246 int err = 0;
4247
4248 spin_lock_bh(&vxlan->hash_lock);
4249
4250 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4251 if (!f)
4252 err = -ENOENT;
4253 else if (f->flags & NTF_EXT_LEARNED)
4254 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4255 fdb_info->remote_ip,
4256 fdb_info->remote_port,
4257 fdb_info->vni,
4258 fdb_info->remote_vni,
4259 fdb_info->remote_ifindex,
4260 false);
4261
4262 spin_unlock_bh(&vxlan->hash_lock);
4263
4264 return err;
4265}
4266
Petr Machata0efe1172018-10-17 08:53:26 +00004267static int vxlan_switchdev_event(struct notifier_block *unused,
4268 unsigned long event, void *ptr)
4269{
4270 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Petr Machata5728ae02018-11-21 08:02:39 +00004271 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4272 int err = 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004273
4274 switch (event) {
4275 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4276 vxlan_fdb_offloaded_set(dev, ptr);
4277 break;
Petr Machata5728ae02018-11-21 08:02:39 +00004278 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4279 fdb_info = ptr;
4280 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4281 if (err) {
4282 err = notifier_from_errno(err);
4283 break;
4284 }
4285 fdb_info->offloaded = true;
4286 vxlan_fdb_offloaded_set(dev, fdb_info);
4287 break;
4288 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4289 fdb_info = ptr;
4290 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4291 if (err) {
4292 err = notifier_from_errno(err);
4293 break;
4294 }
4295 fdb_info->offloaded = false;
4296 vxlan_fdb_offloaded_set(dev, fdb_info);
4297 break;
Petr Machata0efe1172018-10-17 08:53:26 +00004298 }
4299
Petr Machata5728ae02018-11-21 08:02:39 +00004300 return err;
Petr Machata0efe1172018-10-17 08:53:26 +00004301}
4302
4303static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4304 .notifier_call = vxlan_switchdev_event,
4305};
4306
stephen hemmingerd3428942012-10-01 12:32:35 +00004307static __net_init int vxlan_init_net(struct net *net)
4308{
4309 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00004310 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00004311
stephen hemminger553675f2013-05-16 11:35:20 +00004312 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07004313 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00004314
stephen hemminger553675f2013-05-16 11:35:20 +00004315 for (h = 0; h < PORT_HASH_SIZE; ++h)
4316 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00004317
4318 return 0;
4319}
4320
Haishuang Yan57b61122017-12-16 17:54:49 +08004321static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004322{
4323 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4324 struct vxlan_dev *vxlan, *next;
4325 struct net_device *dev, *aux;
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004326 unsigned int h;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004327
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004328 for_each_netdev_safe(net, dev, aux)
4329 if (dev->rtnl_link_ops == &vxlan_link_ops)
Haishuang Yan57b61122017-12-16 17:54:49 +08004330 unregister_netdevice_queue(dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004331
4332 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4333 /* If vxlan->dev is in the same netns, it has already been added
4334 * to the list by the previous loop.
4335 */
Zhiqiang Liucc4807b2019-03-16 17:02:54 +08004336 if (!net_eq(dev_net(vxlan->dev), net))
Haishuang Yan57b61122017-12-16 17:54:49 +08004337 unregister_netdevice_queue(vxlan->dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004338 }
4339
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004340 for (h = 0; h < PORT_HASH_SIZE; ++h)
4341 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004342}
4343
Haishuang Yan57b61122017-12-16 17:54:49 +08004344static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4345{
4346 struct net *net;
4347 LIST_HEAD(list);
4348
4349 rtnl_lock();
4350 list_for_each_entry(net, net_list, exit_list)
4351 vxlan_destroy_tunnels(net, &list);
4352
4353 unregister_netdevice_many(&list);
4354 rtnl_unlock();
4355}
4356
stephen hemmingerd3428942012-10-01 12:32:35 +00004357static struct pernet_operations vxlan_net_ops = {
4358 .init = vxlan_init_net,
Haishuang Yan57b61122017-12-16 17:54:49 +08004359 .exit_batch = vxlan_exit_batch_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004360 .id = &vxlan_net_id,
4361 .size = sizeof(struct vxlan_net),
4362};
4363
4364static int __init vxlan_init_module(void)
4365{
4366 int rc;
4367
4368 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4369
Daniel Borkmann783c1462014-01-22 21:07:53 +01004370 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004371 if (rc)
4372 goto out1;
4373
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004374 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004375 if (rc)
4376 goto out2;
4377
Petr Machata0efe1172018-10-17 08:53:26 +00004378 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004379 if (rc)
4380 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00004381
Petr Machata0efe1172018-10-17 08:53:26 +00004382 rc = rtnl_link_register(&vxlan_link_ops);
4383 if (rc)
4384 goto out4;
4385
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004386 return 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004387out4:
4388 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004389out3:
4390 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004391out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01004392 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004393out1:
4394 return rc;
4395}
Cong Wang7332a132013-05-27 22:35:53 +00004396late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00004397
4398static void __exit vxlan_cleanup_module(void)
4399{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07004400 rtnl_link_unregister(&vxlan_link_ops);
Petr Machata0efe1172018-10-17 08:53:26 +00004401 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004402 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004403 unregister_pernet_subsys(&vxlan_net_ops);
4404 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00004405}
4406module_exit(vxlan_cleanup_module);
4407
4408MODULE_LICENSE("GPL");
4409MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004410MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08004411MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00004412MODULE_ALIAS_RTNL_LINK("vxlan");