blob: 47c762f7f5b11d8360c732339d75ea5bb6089720 [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>
Roopa Prabhu1274e1c2020-05-21 22:26:14 -070029#include <net/nexthop.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080030
Cong Wange4c7ed42013-08-31 13:44:33 +080031#if IS_ENABLED(CONFIG_IPV6)
Cong Wange4c7ed42013-08-31 13:44:33 +080032#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080033#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080034#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000035
36#define VXLAN_VERSION "0.1"
37
stephen hemminger553675f2013-05-16 11:35:20 +000038#define PORT_HASH_BITS 8
39#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000040#define FDB_AGE_DEFAULT 300 /* 5 min */
41#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
42
stephen hemminger23c578b2013-04-27 11:31:53 +000043/* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070045 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000046 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070047static unsigned short vxlan_port __read_mostly = 8472;
48module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000049MODULE_PARM_DESC(udp_port, "Destination UDP port");
50
51static bool log_ecn_error = true;
52module_param(log_ecn_error, bool, 0644);
53MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
54
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030055static unsigned int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020056static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000057
Li RongQing7256eac2016-01-29 09:43:47 +080058static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030059
Jiri Benc205f3562015-09-24 13:50:01 +020060static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020061
Mark Blocha53cb292017-06-02 03:24:08 +030062static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
63
stephen hemminger553675f2013-05-16 11:35:20 +000064/* per-network namespace private data for this module */
65struct vxlan_net {
66 struct list_head vxlan_list;
67 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070068 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000069};
70
stephen hemmingerd3428942012-10-01 12:32:35 +000071/* Forwarding table entry */
72struct vxlan_fdb {
73 struct hlist_node hlist; /* linked list of entries */
74 struct rcu_head rcu;
75 unsigned long updated; /* jiffies */
76 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070077 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020078 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000079 u16 state; /* see ndm_state */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -080080 __be32 vni;
Petr Machata45598c12018-11-21 08:02:36 +000081 u16 flags; /* see ndm_flags and below */
Roopa Prabhu1274e1c2020-05-21 22:26:14 -070082 struct list_head nh_list;
83 struct nexthop __rcu *nh;
Roopa Prabhu79472fe2020-05-28 22:12:36 -070084 struct vxlan_dev __rcu *vdev;
stephen hemmingerd3428942012-10-01 12:32:35 +000085};
86
Petr Machata45598c12018-11-21 08:02:36 +000087#define NTF_VXLAN_ADDED_BY_USER 0x100
88
stephen hemmingerd3428942012-10-01 12:32:35 +000089/* salt for hash table */
90static u32 vxlan_salt __read_mostly;
91
Thomas Grafee122c72015-07-21 10:43:58 +020092static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
93{
Thomas Grafe7030872015-07-21 10:44:01 +020094 return vs->flags & VXLAN_F_COLLECT_METADATA ||
95 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020096}
97
Cong Wange4c7ed42013-08-31 13:44:33 +080098#if IS_ENABLED(CONFIG_IPV6)
99static inline
100bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
101{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200102 if (a->sa.sa_family != b->sa.sa_family)
103 return false;
104 if (a->sa.sa_family == AF_INET6)
105 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
106 else
107 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800108}
109
Cong Wange4c7ed42013-08-31 13:44:33 +0800110static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
111{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200112 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200113 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200114 ip->sa.sa_family = AF_INET6;
115 return 0;
116 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200117 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200118 ip->sa.sa_family = AF_INET;
119 return 0;
120 } else {
121 return -EAFNOSUPPORT;
122 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800123}
124
125static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200126 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800127{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200128 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200129 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200130 else
Jiri Benc930345e2015-03-29 16:59:25 +0200131 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800132}
133
134#else /* !CONFIG_IPV6 */
135
136static inline
137bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
138{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200139 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800140}
141
Cong Wange4c7ed42013-08-31 13:44:33 +0800142static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
143{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200144 if (nla_len(nla) >= sizeof(struct in6_addr)) {
145 return -EAFNOSUPPORT;
146 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200147 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200148 ip->sa.sa_family = AF_INET;
149 return 0;
150 } else {
151 return -EAFNOSUPPORT;
152 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800153}
154
155static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200156 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800157{
Jiri Benc930345e2015-03-29 16:59:25 +0200158 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800159}
160#endif
161
stephen hemminger553675f2013-05-16 11:35:20 +0000162/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100163static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000164{
Jiri Benc54bfd872016-02-16 21:58:58 +0100165 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000166}
167
168/* Socket hash table head */
169static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000170{
171 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
172
stephen hemminger553675f2013-05-16 11:35:20 +0000173 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
174}
175
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700176/* First remote destination for a forwarding entry.
177 * Guaranteed to be non-NULL because remotes are never deleted.
178 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700179static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700180{
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700181 if (rcu_access_pointer(fdb->nh))
182 return NULL;
stephen hemminger5ca54612013-08-04 17:17:39 -0700183 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
184}
185
186static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
187{
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700188 if (rcu_access_pointer(fdb->nh))
189 return NULL;
stephen hemminger5ca54612013-08-04 17:17:39 -0700190 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700191}
192
Thomas Grafac5132d2015-01-15 03:53:56 +0100193/* Find VXLAN socket based on network namespace, address family and UDP port
194 * and enabled unshareable flags.
195 */
196static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100197 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000198{
199 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800200
201 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000202
203 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200204 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200205 vxlan_get_sk_family(vs) == family &&
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100206 vs->flags == flags &&
207 vs->sock->sk->sk_bound_dev_if == ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000208 return vs;
209 }
210 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000211}
212
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200213static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
214 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000215{
Jiri Benc69e76662017-07-02 19:00:57 +0200216 struct vxlan_dev_node *node;
stephen hemmingerd3428942012-10-01 12:32:35 +0000217
Jiri Bencb9167b22016-02-16 21:59:03 +0100218 /* For flow based devices, map all packets to VNI 0 */
219 if (vs->flags & VXLAN_F_COLLECT_METADATA)
220 vni = 0;
221
Jiri Benc69e76662017-07-02 19:00:57 +0200222 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
223 if (node->vxlan->default_dst.remote_vni != vni)
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200224 continue;
225
226 if (IS_ENABLED(CONFIG_IPV6)) {
Jiri Benc69e76662017-07-02 19:00:57 +0200227 const struct vxlan_config *cfg = &node->vxlan->cfg;
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200228
229 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
230 cfg->remote_ifindex != ifindex)
231 continue;
232 }
233
Jiri Benc69e76662017-07-02 19:00:57 +0200234 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000235 }
236
237 return NULL;
238}
239
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700240/* Look up VNI in a per net namespace table */
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200241static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
242 __be32 vni, sa_family_t family,
243 __be16 port, u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700244{
245 struct vxlan_sock *vs;
246
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100247 vs = vxlan_find_sock(net, family, port, flags, ifindex);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700248 if (!vs)
249 return NULL;
250
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200251 return vxlan_vs_find_vni(vs, ifindex, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700252}
253
stephen hemmingerd3428942012-10-01 12:32:35 +0000254/* Fill in neighbour message in skbuff. */
255static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700256 const struct vxlan_fdb *fdb,
257 u32 portid, u32 seq, int type, unsigned int flags,
258 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000259{
260 unsigned long now = jiffies;
261 struct nda_cacheinfo ci;
David Stevense4f67ad2012-11-20 02:50:14 +0000262 bool send_ip, send_eth;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700263 struct nlmsghdr *nlh;
264 struct nexthop *nh;
265 struct ndmsg *ndm;
Ido Schimmel06ec3132020-05-25 00:38:56 +0300266 int nh_family;
267 u32 nh_id;
stephen hemmingerd3428942012-10-01 12:32:35 +0000268
269 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
270 if (nlh == NULL)
271 return -EMSGSIZE;
272
273 ndm = nlmsg_data(nlh);
274 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000275
276 send_eth = send_ip = true;
277
Ido Schimmel06ec3132020-05-25 00:38:56 +0300278 rcu_read_lock();
279 nh = rcu_dereference(fdb->nh);
280 if (nh) {
281 nh_family = nexthop_get_family(nh);
282 nh_id = nh->id;
283 }
284 rcu_read_unlock();
285
David Stevense4f67ad2012-11-20 02:50:14 +0000286 if (type == RTM_GETNEIGH) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700287 if (rdst) {
288 send_ip = !vxlan_addr_any(&rdst->remote_ip);
289 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
290 } else if (nh) {
Ido Schimmel06ec3132020-05-25 00:38:56 +0300291 ndm->ndm_family = nh_family;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700292 }
David Stevense4f67ad2012-11-20 02:50:14 +0000293 send_eth = !is_zero_ether_addr(fdb->eth_addr);
294 } else
295 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000296 ndm->ndm_state = fdb->state;
297 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000298 ndm->ndm_flags = fdb->flags;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700299 if (rdst && rdst->offloaded)
Petr Machata0efe1172018-10-17 08:53:26 +0000300 ndm->ndm_flags |= NTF_OFFLOADED;
Jun Zhao545469f2014-07-26 00:38:59 +0800301 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000302
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100303 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100304 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700305 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100306 goto nla_put_failure;
307
David Stevense4f67ad2012-11-20 02:50:14 +0000308 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000309 goto nla_put_failure;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700310 if (nh) {
Ido Schimmel06ec3132020-05-25 00:38:56 +0300311 if (nla_put_u32(skb, NDA_NH_ID, nh_id))
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700312 goto nla_put_failure;
313 } else if (rdst) {
314 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST,
315 &rdst->remote_ip))
316 goto nla_put_failure;
stephen hemmingerd3428942012-10-01 12:32:35 +0000317
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700318 if (rdst->remote_port &&
319 rdst->remote_port != vxlan->cfg.dst_port &&
320 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
321 goto nla_put_failure;
322 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
323 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
324 goto nla_put_failure;
325 if (rdst->remote_ifindex &&
326 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
327 goto nla_put_failure;
328 }
David Stevens66817122013-03-15 04:35:51 +0000329
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200330 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800331 nla_put_u32(skb, NDA_SRC_VNI,
332 be32_to_cpu(fdb->vni)))
333 goto nla_put_failure;
stephen hemmingerd3428942012-10-01 12:32:35 +0000334
335 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
336 ci.ndm_confirmed = 0;
337 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
338 ci.ndm_refcnt = 0;
339
340 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
341 goto nla_put_failure;
342
Johannes Berg053c0952015-01-16 22:09:00 +0100343 nlmsg_end(skb, nlh);
344 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000345
346nla_put_failure:
347 nlmsg_cancel(skb, nlh);
348 return -EMSGSIZE;
349}
350
351static inline size_t vxlan_nlmsg_size(void)
352{
353 return NLMSG_ALIGN(sizeof(struct ndmsg))
354 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800355 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000356 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000357 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
358 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100359 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000360 + nla_total_size(sizeof(struct nda_cacheinfo));
361}
362
Petr Machata9a997352018-10-17 08:53:22 +0000363static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
364 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000365{
366 struct net *net = dev_net(vxlan->dev);
367 struct sk_buff *skb;
368 int err = -ENOBUFS;
369
370 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
371 if (skb == NULL)
372 goto errout;
373
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200374 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000375 if (err < 0) {
376 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
377 WARN_ON(err == -EMSGSIZE);
378 kfree_skb(skb);
379 goto errout;
380 }
381
382 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
383 return;
384errout:
385 if (err < 0)
386 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
387}
388
Petr Machataff23b912018-12-07 19:55:03 +0000389static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
390 const struct vxlan_fdb *fdb,
391 const struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000392 struct netlink_ext_ack *extack,
Petr Machataff23b912018-12-07 19:55:03 +0000393 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
394{
395 fdb_info->info.dev = vxlan->dev;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000396 fdb_info->info.extack = extack;
Petr Machataff23b912018-12-07 19:55:03 +0000397 fdb_info->remote_ip = rd->remote_ip;
398 fdb_info->remote_port = rd->remote_port;
399 fdb_info->remote_vni = rd->remote_vni;
400 fdb_info->remote_ifindex = rd->remote_ifindex;
401 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
402 fdb_info->vni = fdb->vni;
403 fdb_info->offloaded = rd->offloaded;
404 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
405}
406
Petr Machata61f46fe2019-01-16 23:06:38 +0000407static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
408 struct vxlan_fdb *fdb,
409 struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000410 bool adding,
411 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000412{
413 struct switchdev_notifier_vxlan_fdb_info info;
414 enum switchdev_notifier_type notifier_type;
Petr Machata61f46fe2019-01-16 23:06:38 +0000415 int ret;
Petr Machata9a997352018-10-17 08:53:22 +0000416
417 if (WARN_ON(!rd))
Petr Machata61f46fe2019-01-16 23:06:38 +0000418 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000419
420 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
421 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000422 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
Petr Machata61f46fe2019-01-16 23:06:38 +0000423 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
Petr Machata66859872019-01-16 23:06:56 +0000424 &info.info, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000425 return notifier_to_errno(ret);
Petr Machata9a997352018-10-17 08:53:22 +0000426}
427
Petr Machata61f46fe2019-01-16 23:06:38 +0000428static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000429 struct vxlan_rdst *rd, int type, bool swdev_notify,
430 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000431{
Petr Machata61f46fe2019-01-16 23:06:38 +0000432 int err;
433
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700434 if (swdev_notify && rd) {
Petr Machata0e6160f2018-11-21 08:02:35 +0000435 switch (type) {
436 case RTM_NEWNEIGH:
Petr Machata61f46fe2019-01-16 23:06:38 +0000437 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000438 true, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000439 if (err)
440 return err;
Petr Machata0e6160f2018-11-21 08:02:35 +0000441 break;
442 case RTM_DELNEIGH:
443 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000444 false, extack);
Petr Machata0e6160f2018-11-21 08:02:35 +0000445 break;
446 }
Petr Machata9a997352018-10-17 08:53:22 +0000447 }
448
449 __vxlan_fdb_notify(vxlan, fdb, rd, type);
Petr Machata61f46fe2019-01-16 23:06:38 +0000450 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000451}
452
Cong Wange4c7ed42013-08-31 13:44:33 +0800453static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000454{
455 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700456 struct vxlan_fdb f = {
457 .state = NUD_STALE,
458 };
459 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800460 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100461 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700462 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700463
Petr Machata4c59b7d2019-01-16 23:06:54 +0000464 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000465}
466
467static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
468{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700469 struct vxlan_fdb f = {
470 .state = NUD_STALE,
471 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200472 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000473
David Stevense4f67ad2012-11-20 02:50:14 +0000474 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
475
Petr Machata4c59b7d2019-01-16 23:06:54 +0000476 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000477}
478
stephen hemmingerd3428942012-10-01 12:32:35 +0000479/* Hash Ethernet address */
480static u32 eth_hash(const unsigned char *addr)
481{
482 u64 value = get_unaligned((u64 *)addr);
483
484 /* only want 6 bytes */
485#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000486 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000487#else
488 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000489#endif
490 return hash_64(value, FDB_HASH_BITS);
491}
492
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800493static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
494{
495 /* use 1 byte of OUI and 3 bytes of NIC */
496 u32 key = get_unaligned((u32 *)(addr + 2));
497
498 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
499}
500
Litao jiaofe1e0712019-06-06 17:57:58 +0800501static u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
502{
503 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
504 return eth_vni_hash(mac, vni);
505 else
506 return eth_hash(mac);
507}
508
stephen hemmingerd3428942012-10-01 12:32:35 +0000509/* Hash chain to use given mac address */
510static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800511 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000512{
Litao jiaofe1e0712019-06-06 17:57:58 +0800513 return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000514}
515
516/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000517static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800518 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000519{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800520 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000521 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000522
Sasha Levinb67bfe02013-02-27 17:06:00 -0800523 hlist_for_each_entry_rcu(f, head, hlist) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800524 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200525 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800526 if (vni == f->vni)
527 return f;
528 } else {
529 return f;
530 }
531 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000532 }
533
534 return NULL;
535}
536
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000537static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800538 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000539{
540 struct vxlan_fdb *f;
541
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800542 f = __vxlan_find_mac(vxlan, mac, vni);
Li RongQing016f3d12018-08-29 11:52:10 +0800543 if (f && f->used != jiffies)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000544 f->used = jiffies;
545
546 return f;
547}
548
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300549/* caller should hold vxlan->hash_lock */
550static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800551 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100552 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300553{
554 struct vxlan_rdst *rd;
555
556 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800557 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300558 rd->remote_port == port &&
559 rd->remote_vni == vni &&
560 rd->remote_ifindex == ifindex)
561 return rd;
562 }
563
564 return NULL;
565}
566
Petr Machata1941f1d2018-10-17 08:53:24 +0000567int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
568 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
569{
570 struct vxlan_dev *vxlan = netdev_priv(dev);
571 u8 eth_addr[ETH_ALEN + 2] = { 0 };
572 struct vxlan_rdst *rdst;
573 struct vxlan_fdb *f;
574 int rc = 0;
575
576 if (is_multicast_ether_addr(mac) ||
577 is_zero_ether_addr(mac))
578 return -EINVAL;
579
580 ether_addr_copy(eth_addr, mac);
581
582 rcu_read_lock();
583
584 f = __vxlan_find_mac(vxlan, eth_addr, vni);
585 if (!f) {
586 rc = -ENOENT;
587 goto out;
588 }
589
590 rdst = first_remote_rcu(f);
Petr Machata4c59b7d2019-01-16 23:06:54 +0000591 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
Petr Machata1941f1d2018-10-17 08:53:24 +0000592
593out:
594 rcu_read_unlock();
595 return rc;
596}
597EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
598
Petr Machata4f89f5b2018-12-07 19:55:04 +0000599static int vxlan_fdb_notify_one(struct notifier_block *nb,
600 const struct vxlan_dev *vxlan,
601 const struct vxlan_fdb *f,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000602 const struct vxlan_rdst *rdst,
603 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000604{
605 struct switchdev_notifier_vxlan_fdb_info fdb_info;
606 int rc;
607
Petr Machata4c59b7d2019-01-16 23:06:54 +0000608 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000609 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
610 &fdb_info);
611 return notifier_to_errno(rc);
612}
613
614int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000615 struct notifier_block *nb,
616 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000617{
618 struct vxlan_dev *vxlan;
619 struct vxlan_rdst *rdst;
620 struct vxlan_fdb *f;
621 unsigned int h;
622 int rc = 0;
623
624 if (!netif_is_vxlan(dev))
625 return -EINVAL;
626 vxlan = netdev_priv(dev);
627
Petr Machata4f89f5b2018-12-07 19:55:04 +0000628 for (h = 0; h < FDB_HASH_SIZE; ++h) {
Litao jiaofe1e0712019-06-06 17:57:58 +0800629 spin_lock_bh(&vxlan->hash_lock[h]);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000630 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
631 if (f->vni == vni) {
632 list_for_each_entry(rdst, &f->remotes, list) {
633 rc = vxlan_fdb_notify_one(nb, vxlan,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000634 f, rdst,
635 extack);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000636 if (rc)
Litao jiaofe1e0712019-06-06 17:57:58 +0800637 goto unlock;
Petr Machata4f89f5b2018-12-07 19:55:04 +0000638 }
639 }
640 }
Litao jiaofe1e0712019-06-06 17:57:58 +0800641 spin_unlock_bh(&vxlan->hash_lock[h]);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000642 }
Litao jiaofe1e0712019-06-06 17:57:58 +0800643 return 0;
Petr Machata4f89f5b2018-12-07 19:55:04 +0000644
Litao jiaofe1e0712019-06-06 17:57:58 +0800645unlock:
646 spin_unlock_bh(&vxlan->hash_lock[h]);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000647 return rc;
648}
649EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
650
Petr Machatae5ff4b12018-12-07 19:55:06 +0000651void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
652{
653 struct vxlan_dev *vxlan;
654 struct vxlan_rdst *rdst;
655 struct vxlan_fdb *f;
656 unsigned int h;
657
658 if (!netif_is_vxlan(dev))
659 return;
660 vxlan = netdev_priv(dev);
661
Petr Machatae5ff4b12018-12-07 19:55:06 +0000662 for (h = 0; h < FDB_HASH_SIZE; ++h) {
Litao jiaofe1e0712019-06-06 17:57:58 +0800663 spin_lock_bh(&vxlan->hash_lock[h]);
Petr Machatae5ff4b12018-12-07 19:55:06 +0000664 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
665 if (f->vni == vni)
666 list_for_each_entry(rdst, &f->remotes, list)
667 rdst->offloaded = false;
Litao jiaofe1e0712019-06-06 17:57:58 +0800668 spin_unlock_bh(&vxlan->hash_lock[h]);
Petr Machatae5ff4b12018-12-07 19:55:06 +0000669 }
Litao jiaofe1e0712019-06-06 17:57:58 +0800670
Petr Machatae5ff4b12018-12-07 19:55:06 +0000671}
672EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
673
Thomas Richter906dc182013-07-19 17:20:07 +0200674/* Replace destination of unicast mac */
675static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100676 union vxlan_addr *ip, __be16 port, __be32 vni,
Petr Machataccdfd4f2019-01-16 23:06:34 +0000677 __u32 ifindex, struct vxlan_rdst *oldrd)
Thomas Richter906dc182013-07-19 17:20:07 +0200678{
679 struct vxlan_rdst *rd;
680
681 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
682 if (rd)
683 return 0;
684
685 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
686 if (!rd)
687 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100688
Petr Machataccdfd4f2019-01-16 23:06:34 +0000689 *oldrd = *rd;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100690 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800691 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200692 rd->remote_port = port;
693 rd->remote_vni = vni;
694 rd->remote_ifindex = ifindex;
Petr Machata6ad0b5a2018-12-18 13:15:59 +0000695 rd->offloaded = false;
Thomas Richter906dc182013-07-19 17:20:07 +0200696 return 1;
697}
698
David Stevens66817122013-03-15 04:35:51 +0000699/* Add/update destinations for multicast */
700static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100701 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200702 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000703{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700704 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000705
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300706 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
707 if (rd)
708 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700709
David Stevens66817122013-03-15 04:35:51 +0000710 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
711 if (rd == NULL)
712 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100713
714 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
715 kfree(rd);
716 return -ENOBUFS;
717 }
718
Cong Wange4c7ed42013-08-31 13:44:33 +0800719 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000720 rd->remote_port = port;
Petr Machata0efe1172018-10-17 08:53:26 +0000721 rd->offloaded = false;
David Stevens66817122013-03-15 04:35:51 +0000722 rd->remote_vni = vni;
723 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700724
725 list_add_tail_rcu(&rd->list, &f->remotes);
726
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200727 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000728 return 1;
729}
730
Tom Herbertdfd86452015-01-12 17:00:38 -0800731static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
732 unsigned int off,
733 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100734 __be32 vni_field,
735 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800736 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800737{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700738 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800739
740 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700741 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800742
743 if (!NAPI_GRO_CB(skb)->csum_valid)
744 return NULL;
745
Jiri Benc54bfd872016-02-16 21:58:58 +0100746 start = vxlan_rco_start(vni_field);
747 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800748
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700749 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
750 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800751
752 skb->remcsum_offload = 1;
753
754 return vh;
755}
756
David Millerd4546c22018-06-24 14:13:49 +0900757static struct sk_buff *vxlan_gro_receive(struct sock *sk,
758 struct list_head *head,
759 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200760{
David Millerd4546c22018-06-24 14:13:49 +0900761 struct sk_buff *pp = NULL;
762 struct sk_buff *p;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200763 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800764 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200765 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700766 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100767 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800768 struct gro_remcsum grc;
769
770 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200771
772 off_vx = skb_gro_offset(skb);
773 hlen = off_vx + sizeof(*vh);
774 vh = skb_gro_header_fast(skb, off_vx);
775 if (skb_gro_header_hard(skb, hlen)) {
776 vh = skb_gro_header_slow(skb, hlen, off_vx);
777 if (unlikely(!vh))
778 goto out;
779 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200780
Tom Herbertdfd86452015-01-12 17:00:38 -0800781 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
782
Jiri Benc54bfd872016-02-16 21:58:58 +0100783 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800784
785 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
786 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100787 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800788 !!(vs->flags &
789 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800790
791 if (!vh)
792 goto out;
793 }
794
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700795 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
796
David Millerd4546c22018-06-24 14:13:49 +0900797 list_for_each_entry(p, head, list) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200798 if (!NAPI_GRO_CB(p)->same_flow)
799 continue;
800
801 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100802 if (vh->vx_flags != vh2->vx_flags ||
803 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200804 NAPI_GRO_CB(p)->same_flow = 0;
805 continue;
806 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200807 }
808
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200809 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800810 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200811
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200812out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200813 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200814
815 return pp;
816}
817
Tom Herbert5602c482016-04-05 08:22:53 -0700818static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200819{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700820 /* Sets 'skb->inner_mac_header' since we are always called with
821 * 'skb->encapsulation' set.
822 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800823 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200824}
825
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -0700826static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, const u8 *mac,
827 __u16 state, __be32 src_vni,
828 __u16 ndm_flags)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700829{
830 struct vxlan_fdb *f;
831
832 f = kmalloc(sizeof(*f), GFP_ATOMIC);
833 if (!f)
834 return NULL;
835 f->state = state;
836 f->flags = ndm_flags;
837 f->updated = f->used = jiffies;
838 f->vni = src_vni;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700839 f->nh = NULL;
Roopa Prabhu79472fe2020-05-28 22:12:36 -0700840 RCU_INIT_POINTER(f->vdev, vxlan);
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700841 INIT_LIST_HEAD(&f->nh_list);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700842 INIT_LIST_HEAD(&f->remotes);
843 memcpy(f->eth_addr, mac, ETH_ALEN);
844
845 return f;
846}
847
Taehee Yoo7c31e542019-06-28 14:07:25 +0900848static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
849 __be32 src_vni, struct vxlan_fdb *f)
850{
851 ++vxlan->addrcnt;
852 hlist_add_head_rcu(&f->hlist,
853 vxlan_fdb_head(vxlan, mac, src_vni));
854}
855
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700856static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
857 u32 nhid, struct netlink_ext_ack *extack)
858{
859 struct nexthop *old_nh = rtnl_dereference(fdb->nh);
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700860 struct nexthop *nh;
861 int err = -EINVAL;
862
863 if (old_nh && old_nh->id == nhid)
864 return 0;
865
866 nh = nexthop_find_by_id(vxlan->net, nhid);
867 if (!nh) {
868 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
869 goto err_inval;
870 }
871
872 if (nh) {
873 if (!nexthop_get(nh)) {
874 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
875 nh = NULL;
876 goto err_inval;
877 }
David Ahernce9ac052020-06-08 20:54:43 -0600878 if (!nexthop_is_fdb(nh)) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700879 NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop");
880 goto err_inval;
881 }
882
David Ahern50cb8762020-06-09 17:27:28 -0600883 if (!nexthop_is_multipath(nh)) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700884 NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group");
885 goto err_inval;
886 }
887
888 /* check nexthop group family */
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700889 switch (vxlan->default_dst.remote_ip.sa.sa_family) {
890 case AF_INET:
David Ahern50cb8762020-06-09 17:27:28 -0600891 if (!nexthop_has_v4(nh)) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700892 err = -EAFNOSUPPORT;
893 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
894 goto err_inval;
895 }
896 break;
897 case AF_INET6:
David Ahern50cb8762020-06-09 17:27:28 -0600898 if (nexthop_has_v4(nh)) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700899 err = -EAFNOSUPPORT;
900 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
901 goto err_inval;
902 }
903 }
904 }
905
906 if (old_nh) {
907 list_del_rcu(&fdb->nh_list);
908 nexthop_put(old_nh);
909 }
910 rcu_assign_pointer(fdb->nh, nh);
911 list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list);
912 return 1;
913
914err_inval:
915 if (nh)
916 nexthop_put(nh);
917 return err;
918}
919
stephen hemmingerd3428942012-10-01 12:32:35 +0000920static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800921 const u8 *mac, union vxlan_addr *ip,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700922 __u16 state, __be16 port, __be32 src_vni,
Petr Machata45598c12018-11-21 08:02:36 +0000923 __be32 vni, __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700924 u32 nhid, struct vxlan_fdb **fdb,
925 struct netlink_ext_ack *extack)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700926{
927 struct vxlan_rdst *rd = NULL;
928 struct vxlan_fdb *f;
929 int rc;
930
931 if (vxlan->cfg.addrmax &&
932 vxlan->addrcnt >= vxlan->cfg.addrmax)
933 return -ENOSPC;
934
935 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -0700936 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700937 if (!f)
938 return -ENOMEM;
939
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700940 if (nhid)
941 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
942 else
943 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
944 if (rc < 0)
945 goto errout;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700946
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700947 *fdb = f;
948
949 return 0;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700950
951errout:
952 kfree(f);
953 return rc;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700954}
955
Taehee Yoo7c31e542019-06-28 14:07:25 +0900956static void __vxlan_fdb_free(struct vxlan_fdb *f)
Petr Machatac2b200e2019-01-16 23:06:30 +0000957{
Petr Machatac2b200e2019-01-16 23:06:30 +0000958 struct vxlan_rdst *rd, *nd;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700959 struct nexthop *nh;
960
961 nh = rcu_dereference_raw(f->nh);
962 if (nh) {
963 rcu_assign_pointer(f->nh, NULL);
Roopa Prabhu79472fe2020-05-28 22:12:36 -0700964 rcu_assign_pointer(f->vdev, NULL);
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700965 nexthop_put(nh);
966 }
Petr Machatac2b200e2019-01-16 23:06:30 +0000967
968 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
969 dst_cache_destroy(&rd->dst_cache);
970 kfree(rd);
971 }
972 kfree(f);
973}
974
Taehee Yoo7c31e542019-06-28 14:07:25 +0900975static void vxlan_fdb_free(struct rcu_head *head)
976{
977 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
978
979 __vxlan_fdb_free(f);
980}
981
Petr Machatac2b200e2019-01-16 23:06:30 +0000982static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
983 bool do_notify, bool swdev_notify)
984{
985 struct vxlan_rdst *rd;
986
987 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
988
989 --vxlan->addrcnt;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700990 if (do_notify) {
991 if (rcu_access_pointer(f->nh))
992 vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000993 swdev_notify, NULL);
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700994 else
995 list_for_each_entry(rd, &f->remotes, list)
996 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
997 swdev_notify, NULL);
998 }
Petr Machatac2b200e2019-01-16 23:06:30 +0000999
1000 hlist_del_rcu(&f->hlist);
Roopa Prabhu79472fe2020-05-28 22:12:36 -07001001 list_del_rcu(&f->nh_list);
Petr Machatac2b200e2019-01-16 23:06:30 +00001002 call_rcu(&f->rcu, vxlan_fdb_free);
1003}
1004
Petr Machatafc4aa1c2019-02-07 12:18:02 +00001005static void vxlan_dst_free(struct rcu_head *head)
1006{
1007 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
1008
1009 dst_cache_destroy(&rd->dst_cache);
1010 kfree(rd);
1011}
1012
Petr Machataa76d1ca2019-01-16 23:06:32 +00001013static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
1014 union vxlan_addr *ip,
1015 __u16 state, __u16 flags,
1016 __be16 port, __be32 vni,
1017 __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001018 struct vxlan_fdb *f, u32 nhid,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001019 bool swdev_notify,
1020 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +00001021{
1022 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1023 struct vxlan_rdst *rd = NULL;
Petr Machataccdfd4f2019-01-16 23:06:34 +00001024 struct vxlan_rdst oldrd;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001025 int notify = 0;
Petr Machata61f46fe2019-01-16 23:06:38 +00001026 int rc = 0;
1027 int err;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001028
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001029 if (nhid && !rcu_access_pointer(f->nh)) {
1030 NL_SET_ERR_MSG(extack,
1031 "Cannot replace an existing non nexthop fdb with a nexthop");
1032 return -EOPNOTSUPP;
1033 }
1034
1035 if (nhid && (flags & NLM_F_APPEND)) {
1036 NL_SET_ERR_MSG(extack,
1037 "Cannot append to a nexthop fdb");
1038 return -EOPNOTSUPP;
1039 }
1040
Petr Machataa76d1ca2019-01-16 23:06:32 +00001041 /* Do not allow an externally learned entry to take over an entry added
1042 * by the user.
1043 */
1044 if (!(fdb_flags & NTF_EXT_LEARNED) ||
1045 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
1046 if (f->state != state) {
1047 f->state = state;
1048 f->updated = jiffies;
1049 notify = 1;
1050 }
1051 if (f->flags != fdb_flags) {
1052 f->flags = fdb_flags;
1053 f->updated = jiffies;
1054 notify = 1;
1055 }
1056 }
1057
1058 if ((flags & NLM_F_REPLACE)) {
1059 /* Only change unicasts */
1060 if (!(is_multicast_ether_addr(f->eth_addr) ||
1061 is_zero_ether_addr(f->eth_addr))) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001062 if (nhid) {
1063 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
1064 if (rc < 0)
1065 return rc;
1066 } else {
1067 rc = vxlan_fdb_replace(f, ip, port, vni,
1068 ifindex, &oldrd);
1069 }
Petr Machataa76d1ca2019-01-16 23:06:32 +00001070 notify |= rc;
1071 } else {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001072 NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries");
Petr Machataa76d1ca2019-01-16 23:06:32 +00001073 return -EOPNOTSUPP;
1074 }
1075 }
1076 if ((flags & NLM_F_APPEND) &&
1077 (is_multicast_ether_addr(f->eth_addr) ||
1078 is_zero_ether_addr(f->eth_addr))) {
1079 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
1080
1081 if (rc < 0)
1082 return rc;
1083 notify |= rc;
1084 }
1085
1086 if (ndm_flags & NTF_USE)
1087 f->used = jiffies;
1088
1089 if (notify) {
1090 if (rd == NULL)
1091 rd = first_remote_rtnl(f);
1092
Petr Machata61f46fe2019-01-16 23:06:38 +00001093 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001094 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +00001095 if (err)
1096 goto err_notify;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001097 }
1098
1099 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +00001100
1101err_notify:
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001102 if (nhid)
1103 return err;
Petr Machata61f46fe2019-01-16 23:06:38 +00001104 if ((flags & NLM_F_REPLACE) && rc)
1105 *rd = oldrd;
Petr Machatafc4aa1c2019-02-07 12:18:02 +00001106 else if ((flags & NLM_F_APPEND) && rc) {
Petr Machata61f46fe2019-01-16 23:06:38 +00001107 list_del_rcu(&rd->list);
Petr Machatafc4aa1c2019-02-07 12:18:02 +00001108 call_rcu(&rd->rcu, vxlan_dst_free);
1109 }
Petr Machata61f46fe2019-01-16 23:06:38 +00001110 return err;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001111}
1112
1113static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
1114 const u8 *mac, union vxlan_addr *ip,
1115 __u16 state, __u16 flags,
1116 __be16 port, __be32 src_vni, __be32 vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001117 __u32 ifindex, __u16 ndm_flags, u32 nhid,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001118 bool swdev_notify,
1119 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +00001120{
1121 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1122 struct vxlan_fdb *f;
1123 int rc;
1124
1125 /* Disallow replace to add a multicast entry */
1126 if ((flags & NLM_F_REPLACE) &&
1127 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
1128 return -EOPNOTSUPP;
1129
1130 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
1131 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001132 vni, ifindex, fdb_flags, nhid, &f, extack);
Petr Machataa76d1ca2019-01-16 23:06:32 +00001133 if (rc < 0)
1134 return rc;
1135
Taehee Yoo7c31e542019-06-28 14:07:25 +09001136 vxlan_fdb_insert(vxlan, mac, src_vni, f);
Petr Machata61f46fe2019-01-16 23:06:38 +00001137 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001138 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +00001139 if (rc)
1140 goto err_notify;
1141
Petr Machataa76d1ca2019-01-16 23:06:32 +00001142 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +00001143
1144err_notify:
1145 vxlan_fdb_destroy(vxlan, f, false, false);
1146 return rc;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001147}
1148
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001149/* Add new entry to forwarding table -- assumes lock held */
1150static int vxlan_fdb_update(struct vxlan_dev *vxlan,
1151 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +00001152 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001153 __be16 port, __be32 src_vni, __be32 vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001154 __u32 ifindex, __u16 ndm_flags, u32 nhid,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001155 bool swdev_notify,
1156 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001157{
1158 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001159
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001160 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001161 if (f) {
1162 if (flags & NLM_F_EXCL) {
1163 netdev_dbg(vxlan->dev,
1164 "lost race to create %pM\n", mac);
1165 return -EEXIST;
1166 }
Petr Machata0ec566a2018-11-21 08:02:37 +00001167
Petr Machataa76d1ca2019-01-16 23:06:32 +00001168 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1169 vni, ifindex, ndm_flags, f,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001170 nhid, swdev_notify, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001171 } else {
1172 if (!(flags & NLM_F_CREATE))
1173 return -ENOENT;
1174
Petr Machataa76d1ca2019-01-16 23:06:32 +00001175 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1176 port, src_vni, vni, ifindex,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001177 ndm_flags, nhid, swdev_notify,
1178 extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001179 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001180}
1181
Lance Richardson35cf2842017-05-29 13:25:57 -04001182static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +00001183 struct vxlan_rdst *rd, bool swdev_notify)
Lance Richardson35cf2842017-05-29 13:25:57 -04001184{
1185 list_del_rcu(&rd->list);
Petr Machata4c59b7d2019-01-16 23:06:54 +00001186 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
Lance Richardson35cf2842017-05-29 13:25:57 -04001187 call_rcu(&rd->rcu, vxlan_dst_free);
1188}
1189
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001190static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001191 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001192 __be32 *vni, u32 *ifindex, u32 *nhid)
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001193{
1194 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001195 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001196
Roopa Prabhu72b48682020-05-28 22:12:35 -07001197 if (tb[NDA_NH_ID] && (tb[NDA_DST] || tb[NDA_VNI] || tb[NDA_IFINDEX] ||
1198 tb[NDA_PORT]))
1199 return -EINVAL;
1200
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001201 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001202 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1203 if (err)
1204 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001205 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08001206 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
Niu Xilei98c81472019-12-30 17:52:22 +08001207
Cong Wange4c7ed42013-08-31 13:44:33 +08001208 if (remote->sa.sa_family == AF_INET) {
1209 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1210 ip->sa.sa_family = AF_INET;
1211#if IS_ENABLED(CONFIG_IPV6)
1212 } else {
1213 ip->sin6.sin6_addr = in6addr_any;
1214 ip->sa.sa_family = AF_INET6;
1215#endif
1216 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001217 }
1218
1219 if (tb[NDA_PORT]) {
1220 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1221 return -EINVAL;
1222 *port = nla_get_be16(tb[NDA_PORT]);
1223 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001224 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001225 }
1226
1227 if (tb[NDA_VNI]) {
1228 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1229 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +01001230 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001231 } else {
1232 *vni = vxlan->default_dst.remote_vni;
1233 }
1234
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001235 if (tb[NDA_SRC_VNI]) {
1236 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1237 return -EINVAL;
1238 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1239 } else {
1240 *src_vni = vxlan->default_dst.remote_vni;
1241 }
1242
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001243 if (tb[NDA_IFINDEX]) {
1244 struct net_device *tdev;
1245
1246 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1247 return -EINVAL;
1248 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +08001249 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001250 if (!tdev)
1251 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001252 } else {
1253 *ifindex = 0;
1254 }
1255
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001256 if (tb[NDA_NH_ID])
1257 *nhid = nla_get_u32(tb[NDA_NH_ID]);
1258 else
1259 *nhid = 0;
1260
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001261 return 0;
1262}
1263
stephen hemmingerd3428942012-10-01 12:32:35 +00001264/* Add static entry (via netlink) */
1265static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1266 struct net_device *dev,
Petr Machata87b09842019-01-16 23:06:50 +00001267 const unsigned char *addr, u16 vid, u16 flags,
1268 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001269{
1270 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001271 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +08001272 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +00001273 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001274 __be32 src_vni, vni;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001275 u32 ifindex, nhid;
Litao jiaofe1e0712019-06-06 17:57:58 +08001276 u32 hash_index;
stephen hemmingerd3428942012-10-01 12:32:35 +00001277 int err;
1278
1279 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1280 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1281 ndm->ndm_state);
1282 return -EINVAL;
1283 }
1284
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001285 if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID]))
stephen hemmingerd3428942012-10-01 12:32:35 +00001286 return -EINVAL;
1287
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001288 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1289 &nhid);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001290 if (err)
1291 return err;
David Stevens66817122013-03-15 04:35:51 +00001292
Mike Rapoport5933a7b2014-04-01 09:23:01 +03001293 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1294 return -EAFNOSUPPORT;
1295
Litao jiaofe1e0712019-06-06 17:57:58 +08001296 hash_index = fdb_head_index(vxlan, addr, src_vni);
1297 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001298 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
Petr Machata45598c12018-11-21 08:02:36 +00001299 port, src_vni, vni, ifindex,
1300 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001301 nhid, true, extack);
Litao jiaofe1e0712019-06-06 17:57:58 +08001302 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001303
1304 return err;
1305}
1306
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001307static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1308 const unsigned char *addr, union vxlan_addr ip,
Xin Longfc39c382017-11-26 21:19:05 +08001309 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001310 u32 ifindex, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +00001311{
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001312 struct vxlan_rdst *rd = NULL;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001313 struct vxlan_fdb *f;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001314 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001315
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001316 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001317 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001318 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001319
Cong Wange4c7ed42013-08-31 13:44:33 +08001320 if (!vxlan_addr_any(&ip)) {
1321 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001322 if (!rd)
1323 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +00001324 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001325
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001326 /* remove a destination if it's not the only one on the list,
1327 * otherwise destroy the fdb entry
1328 */
1329 if (rd && !list_is_singular(&f->remotes)) {
Petr Machata0e6160f2018-11-21 08:02:35 +00001330 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001331 goto out;
1332 }
1333
Petr Machata0e6160f2018-11-21 08:02:35 +00001334 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001335
1336out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001337 return 0;
1338}
1339
1340/* Delete entry (via netlink) */
1341static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1342 struct net_device *dev,
1343 const unsigned char *addr, u16 vid)
1344{
1345 struct vxlan_dev *vxlan = netdev_priv(dev);
1346 union vxlan_addr ip;
1347 __be32 src_vni, vni;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001348 u32 ifindex, nhid;
Litao jiaofe1e0712019-06-06 17:57:58 +08001349 u32 hash_index;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001350 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001351 int err;
1352
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001353 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1354 &nhid);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001355 if (err)
1356 return err;
1357
Litao jiaofe1e0712019-06-06 17:57:58 +08001358 hash_index = fdb_head_index(vxlan, addr, src_vni);
1359 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata0e6160f2018-11-21 08:02:35 +00001360 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1361 true);
Litao jiaofe1e0712019-06-06 17:57:58 +08001362 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001363
1364 return err;
1365}
1366
1367/* Dump forwarding table */
1368static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04001369 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07001370 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +00001371{
1372 struct vxlan_dev *vxlan = netdev_priv(dev);
1373 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -07001374 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001375
1376 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1377 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001378
Ido Schimmelb5141912020-07-29 11:34:36 +03001379 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001380 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +00001381 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +00001382
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001383 if (rcu_access_pointer(f->nh)) {
Roopa Prabhub18e9832020-06-24 14:02:36 -07001384 if (*idx < cb->args[2])
1385 goto skip_nh;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001386 err = vxlan_fdb_info(skb, vxlan, f,
1387 NETLINK_CB(cb->skb).portid,
1388 cb->nlh->nlmsg_seq,
1389 RTM_NEWNEIGH,
1390 NLM_F_MULTI, NULL);
Ido Schimmelb5141912020-07-29 11:34:36 +03001391 if (err < 0) {
1392 rcu_read_unlock();
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001393 goto out;
Ido Schimmelb5141912020-07-29 11:34:36 +03001394 }
Roopa Prabhub18e9832020-06-24 14:02:36 -07001395skip_nh:
1396 *idx += 1;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001397 continue;
1398 }
1399
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001400 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07001401 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001402 goto skip;
1403
David Stevens66817122013-03-15 04:35:51 +00001404 err = vxlan_fdb_info(skb, vxlan, f,
1405 NETLINK_CB(cb->skb).portid,
1406 cb->nlh->nlmsg_seq,
1407 RTM_NEWNEIGH,
1408 NLM_F_MULTI, rd);
Ido Schimmelb5141912020-07-29 11:34:36 +03001409 if (err < 0) {
1410 rcu_read_unlock();
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001411 goto out;
Ido Schimmelb5141912020-07-29 11:34:36 +03001412 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001413skip:
Roopa Prabhud2976532016-08-30 21:56:45 -07001414 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001415 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001416 }
Ido Schimmelb5141912020-07-29 11:34:36 +03001417 rcu_read_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001418 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001419out:
Roopa Prabhud2976532016-08-30 21:56:45 -07001420 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001421}
1422
Roopa Prabhu474c3c82018-12-15 22:35:10 -08001423static int vxlan_fdb_get(struct sk_buff *skb,
1424 struct nlattr *tb[],
1425 struct net_device *dev,
1426 const unsigned char *addr,
1427 u16 vid, u32 portid, u32 seq,
1428 struct netlink_ext_ack *extack)
1429{
1430 struct vxlan_dev *vxlan = netdev_priv(dev);
1431 struct vxlan_fdb *f;
1432 __be32 vni;
1433 int err;
1434
1435 if (tb[NDA_VNI])
1436 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1437 else
1438 vni = vxlan->default_dst.remote_vni;
1439
1440 rcu_read_lock();
1441
1442 f = __vxlan_find_mac(vxlan, addr, vni);
1443 if (!f) {
1444 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1445 err = -ENOENT;
1446 goto errout;
1447 }
1448
1449 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1450 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1451errout:
1452 rcu_read_unlock();
1453 return err;
1454}
1455
stephen hemmingerd3428942012-10-01 12:32:35 +00001456/* Watch incoming packets to learn mapping between Ethernet address
1457 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +09001458 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +00001459 */
stephen hemminger26a41ae62013-06-17 12:09:58 -07001460static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001461 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001462 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +00001463{
1464 struct vxlan_dev *vxlan = netdev_priv(dev);
1465 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001466 u32 ifindex = 0;
1467
1468#if IS_ENABLED(CONFIG_IPV6)
1469 if (src_ip->sa.sa_family == AF_INET6 &&
1470 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1471 ifindex = src_ifindex;
1472#endif
stephen hemmingerd3428942012-10-01 12:32:35 +00001473
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001474 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001475 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001476 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001477
Matthias Schiffer87613de2017-06-19 10:03:59 +02001478 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1479 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001480 return false;
1481
1482 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -07001483 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001484 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001485
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001486 /* Don't override an fdb with nexthop with a learnt entry */
1487 if (rcu_access_pointer(f->nh))
1488 return true;
1489
stephen hemmingerd3428942012-10-01 12:32:35 +00001490 if (net_ratelimit())
1491 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001492 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001493 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001494
Cong Wange4c7ed42013-08-31 13:44:33 +08001495 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001496 f->updated = jiffies;
Petr Machata4c59b7d2019-01-16 23:06:54 +00001497 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
stephen hemmingerd3428942012-10-01 12:32:35 +00001498 } else {
Litao jiaofe1e0712019-06-06 17:57:58 +08001499 u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
1500
stephen hemmingerd3428942012-10-01 12:32:35 +00001501 /* learned new entry */
Litao jiaofe1e0712019-06-06 17:57:58 +08001502 spin_lock(&vxlan->hash_lock[hash_index]);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001503
1504 /* close off race between vxlan_flush and incoming packets */
1505 if (netif_running(dev))
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001506 vxlan_fdb_update(vxlan, src_mac, src_ip,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001507 NUD_REACHABLE,
1508 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001509 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001510 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001511 vxlan->default_dst.remote_vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001512 ifindex, NTF_SELF, 0, true, NULL);
Litao jiaofe1e0712019-06-06 17:57:58 +08001513 spin_unlock(&vxlan->hash_lock[hash_index]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001514 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001515
1516 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001517}
1518
stephen hemmingerd3428942012-10-01 12:32:35 +00001519/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001520static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001521{
stephen hemminger553675f2013-05-16 11:35:20 +00001522 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001523 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001524#if IS_ENABLED(CONFIG_IPV6)
1525 struct vxlan_sock *sock6;
1526#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001527 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001528
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001529 sock4 = rtnl_dereference(dev->vn4_sock);
1530
Gao feng95ab0992013-12-10 16:37:33 +08001531 /* The vxlan_sock is only used by dev, leaving group has
1532 * no effect on other vxlan devices.
1533 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001534 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001535 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001536#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001537 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001538 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001539 return false;
1540#endif
Gao feng95ab0992013-12-10 16:37:33 +08001541
stephen hemminger553675f2013-05-16 11:35:20 +00001542 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001543 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001544 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001545
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001546 if (family == AF_INET &&
1547 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001548 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001549#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001550 if (family == AF_INET6 &&
1551 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001552 continue;
1553#endif
Gao feng95ab0992013-12-10 16:37:33 +08001554
1555 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1556 &dev->default_dst.remote_ip))
1557 continue;
1558
1559 if (vxlan->default_dst.remote_ifindex !=
1560 dev->default_dst.remote_ifindex)
1561 continue;
1562
1563 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001564 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001565
1566 return false;
1567}
1568
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001569static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001570{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001571 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001572
Jiri Bencb1be00a2015-09-24 13:50:02 +02001573 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001574 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001575 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001576 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001577
Jiri Bencb1be00a2015-09-24 13:50:02 +02001578 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001579 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001580 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001581 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001582 (vs->flags & VXLAN_F_GPE) ?
1583 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001584 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001585 spin_unlock(&vn->sock_lock);
1586
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001587 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001588}
1589
Jiri Bencb1be00a2015-09-24 13:50:02 +02001590static void vxlan_sock_release(struct vxlan_dev *vxlan)
1591{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001592 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001593#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001594 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1595
Mark Bloch57d88182017-06-07 14:36:58 +03001596 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001597#endif
1598
Mark Bloch57d88182017-06-07 14:36:58 +03001599 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001600 synchronize_net();
1601
Mark Blocha53cb292017-06-02 03:24:08 +03001602 vxlan_vs_del_dev(vxlan);
1603
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001604 if (__vxlan_sock_release_prep(sock4)) {
1605 udp_tunnel_sock_release(sock4->sock);
1606 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001607 }
1608
1609#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001610 if (__vxlan_sock_release_prep(sock6)) {
1611 udp_tunnel_sock_release(sock6->sock);
1612 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001613 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001614#endif
1615}
1616
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001617/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001618 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001619 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001620static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001621{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001622 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001623 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1624 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001625 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001626
Cong Wange4c7ed42013-08-31 13:44:33 +08001627 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001628 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001629 struct ip_mreqn mreq = {
1630 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1631 .imr_ifindex = ifindex,
1632 };
1633
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001634 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001635 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001636 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001637 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001638#if IS_ENABLED(CONFIG_IPV6)
1639 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001640 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1641
1642 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001643 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001644 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1645 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001646 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001647#endif
1648 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001649
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001650 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001651}
1652
1653/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001654static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001655{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001656 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001657 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1658 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001659 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001660
Cong Wange4c7ed42013-08-31 13:44:33 +08001661 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001662 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001663 struct ip_mreqn mreq = {
1664 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1665 .imr_ifindex = ifindex,
1666 };
1667
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001668 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001669 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001670 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001671 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001672#if IS_ENABLED(CONFIG_IPV6)
1673 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001674 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1675
1676 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001677 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001678 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1679 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001680 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001681#endif
1682 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001683
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001684 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001685}
1686
Jiri Bencf14eceb2016-02-16 21:59:01 +01001687static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1688 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001689{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001690 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001691
Jiri Bencf14eceb2016-02-16 21:59:01 +01001692 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1693 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001694
Jiri Bencf14eceb2016-02-16 21:59:01 +01001695 start = vxlan_rco_start(unparsed->vx_vni);
1696 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001697
Jiri Benc7d34fa72016-03-21 17:50:05 +01001698 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001699 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001700
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001701 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1702 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001703out:
1704 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1705 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001706 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001707}
1708
Jiri Bencf14eceb2016-02-16 21:59:01 +01001709static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001710 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001711 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001712{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001713 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001714 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001715
Jiri Bencf14eceb2016-02-16 21:59:01 +01001716 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1717 goto out;
1718
Jiri Benc3288af02016-02-16 21:59:00 +01001719 md->gbp = ntohs(gbp->policy_id);
1720
Jiri Benc10a5af22016-02-23 18:02:59 +01001721 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001722 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001723 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001724 tun_dst->u.tun_info.options_len = sizeof(*md);
1725 }
Jiri Benc3288af02016-02-16 21:59:00 +01001726 if (gbp->dont_learn)
1727 md->gbp |= VXLAN_GBP_DONT_LEARN;
1728
1729 if (gbp->policy_applied)
1730 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001731
Jiri Benc64f87d32016-02-23 18:02:55 +01001732 /* In flow-based mode, GBP is carried in dst_metadata */
1733 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1734 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001735out:
1736 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001737}
1738
Jiri Bence1e53142016-04-05 14:47:13 +02001739static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001740 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001741 struct sk_buff *skb, u32 vxflags)
1742{
1743 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1744
1745 /* Need to have Next Protocol set for interfaces in GPE mode. */
1746 if (!gpe->np_applied)
1747 return false;
1748 /* "The initial version is 0. If a receiver does not support the
1749 * version indicated it MUST drop the packet.
1750 */
1751 if (gpe->version != 0)
1752 return false;
1753 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1754 * processing MUST occur." However, we don't implement OAM
1755 * processing, thus drop the packet.
1756 */
1757 if (gpe->oam_flag)
1758 return false;
1759
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001760 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1761 if (!*protocol)
Jiri Bence1e53142016-04-05 14:47:13 +02001762 return false;
Jiri Bence1e53142016-04-05 14:47:13 +02001763
1764 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1765 return true;
1766}
1767
Jiri Benc1ab016e2016-02-23 18:02:56 +01001768static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1769 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001770 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001771{
Thomas Graf614732e2015-07-21 10:44:06 +02001772 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001773 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001774
Thomas Graf614732e2015-07-21 10:44:06 +02001775 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001776 skb->protocol = eth_type_trans(skb, vxlan->dev);
1777 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1778
1779 /* Ignore packet loops (and multicast echo) */
1780 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001781 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001782
Jiri Benc760c6802016-02-23 18:02:57 +01001783 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001784 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001785 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001786 saddr.sa.sa_family = AF_INET;
1787#if IS_ENABLED(CONFIG_IPV6)
1788 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001789 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001790 saddr.sa.sa_family = AF_INET6;
1791#endif
1792 }
1793
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001794 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001795 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001796 return false;
1797
1798 return true;
1799}
1800
Jiri Benc760c6802016-02-23 18:02:57 +01001801static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1802 struct sk_buff *skb)
1803{
1804 int err = 0;
1805
1806 if (vxlan_get_sk_family(vs) == AF_INET)
1807 err = IP_ECN_decapsulate(oiph, skb);
1808#if IS_ENABLED(CONFIG_IPV6)
1809 else
1810 err = IP6_ECN_decapsulate(oiph, skb);
1811#endif
1812
1813 if (unlikely(err) && log_ecn_error) {
1814 if (vxlan_get_sk_family(vs) == AF_INET)
1815 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1816 &((struct iphdr *)oiph)->saddr,
1817 ((struct iphdr *)oiph)->tos);
1818 else
1819 net_info_ratelimited("non-ECT from %pI6\n",
1820 &((struct ipv6hdr *)oiph)->saddr);
1821 }
1822 return err <= 1;
1823}
1824
stephen hemmingerd3428942012-10-01 12:32:35 +00001825/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001826static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001827{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001828 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001829 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001830 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001831 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001832 struct vxlan_metadata _md;
1833 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001834 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001835 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001836 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001837 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001838
Jiri Bence1e53142016-04-05 14:47:13 +02001839 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001840 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001841 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001842
Jiri Bencf14eceb2016-02-16 21:59:01 +01001843 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001844 /* VNI flag always required to be set */
1845 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1846 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1847 ntohl(vxlan_hdr(skb)->vx_flags),
1848 ntohl(vxlan_hdr(skb)->vx_vni));
1849 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001850 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001851 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001852 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1853 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001854
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001855 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001856 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001857 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001858
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001859 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1860
Matthias Schiffer49f810f2017-06-19 10:04:00 +02001861 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001862 if (!vxlan)
1863 goto drop;
1864
Jiri Bence1e53142016-04-05 14:47:13 +02001865 /* For backwards compatibility, only allow reserved fields to be
1866 * used by VXLAN extensions if explicitly requested.
1867 */
1868 if (vs->flags & VXLAN_F_GPE) {
1869 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1870 goto drop;
1871 raw_proto = true;
1872 }
1873
1874 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1875 !net_eq(vxlan->net, dev_net(vxlan->dev))))
Niu Xilei98c81472019-12-30 17:52:22 +08001876 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001877
Fabian Frederick2ae29042020-09-25 15:16:02 +02001878 if (vs->flags & VXLAN_F_REMCSUM_RX)
1879 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1880 goto drop;
1881
Thomas Grafee122c72015-07-21 10:43:58 +02001882 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001883 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001884
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001885 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001886 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001887
Thomas Grafee122c72015-07-21 10:43:58 +02001888 if (!tun_dst)
1889 goto drop;
1890
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001891 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001892
1893 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001894 } else {
1895 memset(md, 0, sizeof(*md));
1896 }
1897
Jiri Bencf14eceb2016-02-16 21:59:01 +01001898 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001899 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001900 /* Note that GBP and GPE can never be active together. This is
1901 * ensured in vxlan_dev_configure.
1902 */
Thomas Graf35114942015-01-15 03:53:55 +01001903
Jiri Bencf14eceb2016-02-16 21:59:01 +01001904 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001905 /* If there are any unprocessed flags remaining treat
1906 * this as a malformed packet. This behavior diverges from
1907 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1908 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001909 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001910 * is more robust and provides a little more security in
1911 * adding extensions to VXLAN.
1912 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001913 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001914 }
1915
Jiri Bence1e53142016-04-05 14:47:13 +02001916 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001917 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001918 goto drop;
1919 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001920 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001921 skb->dev = vxlan->dev;
1922 skb->pkt_type = PACKET_HOST;
1923 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001924
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001925 oiph = skb_network_header(skb);
1926 skb_reset_network_header(skb);
1927
1928 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1929 ++vxlan->dev->stats.rx_frame_errors;
1930 ++vxlan->dev->stats.rx_errors;
1931 goto drop;
1932 }
1933
Eric Dumazet59cbf562019-03-10 10:36:40 -07001934 rcu_read_lock();
1935
1936 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1937 rcu_read_unlock();
1938 atomic_long_inc(&vxlan->dev->rx_dropped);
1939 goto drop;
1940 }
1941
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001942 stats = this_cpu_ptr(vxlan->dev->tstats);
1943 u64_stats_update_begin(&stats->syncp);
1944 stats->rx_packets++;
1945 stats->rx_bytes += skb->len;
1946 u64_stats_update_end(&stats->syncp);
1947
1948 gro_cells_receive(&vxlan->gro_cells, skb);
Eric Dumazet59cbf562019-03-10 10:36:40 -07001949
1950 rcu_read_unlock();
1951
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001952 return 0;
1953
1954drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001955 /* Consume bad packet */
1956 kfree_skb(skb);
1957 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001958}
1959
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001960/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1961static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1962{
1963 struct vxlan_dev *vxlan;
1964 struct vxlan_sock *vs;
1965 struct vxlanhdr *hdr;
1966 __be32 vni;
1967
Stefano Brivio8399a692019-06-11 00:27:05 +02001968 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001969 return -EINVAL;
1970
1971 hdr = vxlan_hdr(skb);
1972
1973 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1974 return -EINVAL;
1975
1976 vs = rcu_dereference_sk_user_data(sk);
1977 if (!vs)
1978 return -ENOENT;
1979
1980 vni = vxlan_vni(hdr->vx_vni);
1981 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1982 if (!vxlan)
1983 return -ENOENT;
1984
1985 return 0;
1986}
1987
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001988static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001989{
1990 struct vxlan_dev *vxlan = netdev_priv(dev);
1991 struct arphdr *parp;
1992 u8 *arpptr, *sha;
1993 __be32 sip, tip;
1994 struct neighbour *n;
1995
1996 if (dev->flags & IFF_NOARP)
1997 goto out;
1998
1999 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
2000 dev->stats.tx_dropped++;
2001 goto out;
2002 }
2003 parp = arp_hdr(skb);
2004
2005 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
2006 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
2007 parp->ar_pro != htons(ETH_P_IP) ||
2008 parp->ar_op != htons(ARPOP_REQUEST) ||
2009 parp->ar_hln != dev->addr_len ||
2010 parp->ar_pln != 4)
2011 goto out;
2012 arpptr = (u8 *)parp + sizeof(struct arphdr);
2013 sha = arpptr;
2014 arpptr += dev->addr_len; /* sha */
2015 memcpy(&sip, arpptr, sizeof(sip));
2016 arpptr += sizeof(sip);
2017 arpptr += dev->addr_len; /* tha */
2018 memcpy(&tip, arpptr, sizeof(tip));
2019
2020 if (ipv4_is_loopback(tip) ||
2021 ipv4_is_multicast(tip))
2022 goto out;
2023
2024 n = neigh_lookup(&arp_tbl, &tip, dev);
2025
2026 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00002027 struct vxlan_fdb *f;
2028 struct sk_buff *reply;
2029
2030 if (!(n->nud_state & NUD_CONNECTED)) {
2031 neigh_release(n);
2032 goto out;
2033 }
2034
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002035 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08002036 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00002037 /* bridge-local neighbor */
2038 neigh_release(n);
2039 goto out;
2040 }
2041
2042 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
2043 n->ha, sha);
2044
2045 neigh_release(n);
2046
David Stevens73461352014-03-18 12:32:29 -04002047 if (reply == NULL)
2048 goto out;
2049
David Stevense4f67ad2012-11-20 02:50:14 +00002050 skb_reset_mac_header(reply);
2051 __skb_pull(reply, skb_network_offset(reply));
2052 reply->ip_summed = CHECKSUM_UNNECESSARY;
2053 reply->pkt_type = PACKET_HOST;
2054
2055 if (netif_rx_ni(reply) == NET_RX_DROP)
2056 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002057 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002058 union vxlan_addr ipa = {
2059 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002060 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08002061 };
2062
2063 vxlan_ip_miss(dev, &ipa);
2064 }
David Stevense4f67ad2012-11-20 02:50:14 +00002065out:
2066 consume_skb(skb);
2067 return NETDEV_TX_OK;
2068}
2069
Cong Wangf564f452013-08-31 13:44:36 +08002070#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04002071static struct sk_buff *vxlan_na_create(struct sk_buff *request,
2072 struct neighbour *n, bool isrouter)
2073{
2074 struct net_device *dev = request->dev;
2075 struct sk_buff *reply;
2076 struct nd_msg *ns, *na;
2077 struct ipv6hdr *pip6;
2078 u8 *daddr;
2079 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
2080 int ns_olen;
2081 int i, len;
2082
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002083 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04002084 return NULL;
2085
2086 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
2087 sizeof(*na) + na_olen + dev->needed_tailroom;
2088 reply = alloc_skb(len, GFP_ATOMIC);
2089 if (reply == NULL)
2090 return NULL;
2091
2092 reply->protocol = htons(ETH_P_IPV6);
2093 reply->dev = dev;
2094 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
2095 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00002096 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04002097
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002098 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04002099
2100 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002101 ns_olen = request->len - skb_network_offset(request) -
2102 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04002103 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
Ido Schimmel8066e6b2020-06-01 15:58:55 +03002104 if (!ns->opt[i + 1]) {
2105 kfree_skb(reply);
2106 return NULL;
2107 }
David Stevens4b29dba2014-03-24 10:39:58 -04002108 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
2109 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
2110 break;
2111 }
2112 }
2113
2114 /* Ethernet header */
2115 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
2116 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
2117 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
2118 reply->protocol = htons(ETH_P_IPV6);
2119
2120 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00002121 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04002122 skb_put(reply, sizeof(struct ipv6hdr));
2123
2124 /* IPv6 header */
2125
2126 pip6 = ipv6_hdr(reply);
2127 memset(pip6, 0, sizeof(struct ipv6hdr));
2128 pip6->version = 6;
2129 pip6->priority = ipv6_hdr(request)->priority;
2130 pip6->nexthdr = IPPROTO_ICMPV6;
2131 pip6->hop_limit = 255;
2132 pip6->daddr = ipv6_hdr(request)->saddr;
2133 pip6->saddr = *(struct in6_addr *)n->primary_key;
2134
2135 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00002136 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04002137
David Stevens4b29dba2014-03-24 10:39:58 -04002138 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02002139 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04002140 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
2141 na->icmph.icmp6_router = isrouter;
2142 na->icmph.icmp6_override = 1;
2143 na->icmph.icmp6_solicited = 1;
2144 na->target = ns->target;
2145 ether_addr_copy(&na->opt[2], n->ha);
2146 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
2147 na->opt[1] = na_olen >> 3;
2148
2149 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
2150 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
2151 csum_partial(na, sizeof(*na)+na_olen, 0));
2152
2153 pip6->payload_len = htons(sizeof(*na)+na_olen);
2154
2155 skb_push(reply, sizeof(struct ipv6hdr));
2156
2157 reply->ip_summed = CHECKSUM_UNNECESSARY;
2158
2159 return reply;
2160}
2161
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002162static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08002163{
2164 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08002165 const struct in6_addr *daddr;
Xin Long8bff36852017-11-11 19:58:50 +08002166 const struct ipv6hdr *iphdr;
David Stevens4b29dba2014-03-24 10:39:58 -04002167 struct inet6_dev *in6_dev;
Xin Long8bff36852017-11-11 19:58:50 +08002168 struct neighbour *n;
2169 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08002170
2171 in6_dev = __in6_dev_get(dev);
2172 if (!in6_dev)
2173 goto out;
2174
Cong Wangf564f452013-08-31 13:44:36 +08002175 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002176 daddr = &iphdr->daddr;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002177 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08002178
David Stevens4b29dba2014-03-24 10:39:58 -04002179 if (ipv6_addr_loopback(daddr) ||
2180 ipv6_addr_is_multicast(&msg->target))
2181 goto out;
2182
2183 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08002184
2185 if (n) {
2186 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04002187 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08002188
2189 if (!(n->nud_state & NUD_CONNECTED)) {
2190 neigh_release(n);
2191 goto out;
2192 }
2193
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002194 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002195 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2196 /* bridge-local neighbor */
2197 neigh_release(n);
2198 goto out;
2199 }
2200
David Stevens4b29dba2014-03-24 10:39:58 -04002201 reply = vxlan_na_create(skb, n,
2202 !!(f ? f->flags & NTF_ROUTER : 0));
2203
Cong Wangf564f452013-08-31 13:44:36 +08002204 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04002205
2206 if (reply == NULL)
2207 goto out;
2208
2209 if (netif_rx_ni(reply) == NET_RX_DROP)
2210 dev->stats.rx_dropped++;
2211
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002212 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04002213 union vxlan_addr ipa = {
2214 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002215 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04002216 };
2217
Cong Wangf564f452013-08-31 13:44:36 +08002218 vxlan_ip_miss(dev, &ipa);
2219 }
2220
2221out:
2222 consume_skb(skb);
2223 return NETDEV_TX_OK;
2224}
2225#endif
2226
David Stevense4f67ad2012-11-20 02:50:14 +00002227static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2228{
2229 struct vxlan_dev *vxlan = netdev_priv(dev);
2230 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00002231
2232 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2233 return false;
2234
2235 n = NULL;
2236 switch (ntohs(eth_hdr(skb)->h_proto)) {
2237 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08002238 {
2239 struct iphdr *pip;
2240
David Stevense4f67ad2012-11-20 02:50:14 +00002241 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2242 return false;
2243 pip = ip_hdr(skb);
2244 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002245 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002246 union vxlan_addr ipa = {
2247 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002248 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08002249 };
2250
2251 vxlan_ip_miss(dev, &ipa);
2252 return false;
2253 }
2254
David Stevense4f67ad2012-11-20 02:50:14 +00002255 break;
Cong Wange15a00a2013-08-31 13:44:34 +08002256 }
2257#if IS_ENABLED(CONFIG_IPV6)
2258 case ETH_P_IPV6:
2259 {
2260 struct ipv6hdr *pip6;
2261
2262 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2263 return false;
2264 pip6 = ipv6_hdr(skb);
2265 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002266 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08002267 union vxlan_addr ipa = {
2268 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002269 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08002270 };
2271
2272 vxlan_ip_miss(dev, &ipa);
2273 return false;
2274 }
2275
2276 break;
2277 }
2278#endif
David Stevense4f67ad2012-11-20 02:50:14 +00002279 default:
2280 return false;
2281 }
2282
2283 if (n) {
2284 bool diff;
2285
Joe Perches7367d0b2013-09-01 11:51:23 -07002286 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00002287 if (diff) {
2288 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2289 dev->addr_len);
2290 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2291 }
2292 neigh_release(n);
2293 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08002294 }
2295
David Stevense4f67ad2012-11-20 02:50:14 +00002296 return false;
2297}
2298
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002299static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01002300 struct vxlan_metadata *md)
2301{
2302 struct vxlanhdr_gbp *gbp;
2303
Thomas Grafdb79a622015-02-04 17:00:04 +01002304 if (!md->gbp)
2305 return;
2306
Thomas Graf35114942015-01-15 03:53:55 +01002307 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01002308 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01002309
2310 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2311 gbp->dont_learn = 1;
2312
2313 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2314 gbp->policy_applied = 1;
2315
2316 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2317}
2318
Jiri Bence1e53142016-04-05 14:47:13 +02002319static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2320 __be16 protocol)
2321{
2322 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2323
2324 gpe->np_applied = 1;
Jiri Bencfa20e0e2017-08-28 21:43:22 +02002325 gpe->next_protocol = tun_p_from_eth_p(protocol);
2326 if (!gpe->next_protocol)
2327 return -EPFNOSUPPORT;
2328 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002329}
2330
Jiri Bencf491e562016-02-02 18:09:16 +01002331static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2332 int iphdr_len, __be32 vni,
2333 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002334 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08002335{
Cong Wange4c7ed42013-08-31 13:44:33 +08002336 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08002337 int min_headroom;
2338 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08002339 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02002340 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08002341
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002342 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08002343 skb->ip_summed == CHECKSUM_PARTIAL) {
2344 int csum_start = skb_checksum_start_offset(skb);
2345
2346 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2347 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2348 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00002349 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08002350 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08002351 }
2352
Cong Wange4c7ed42013-08-31 13:44:33 +08002353 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08002354 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002355
2356 /* Need space for new headers (invalidates iph ptr) */
2357 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02002358 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08002359 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002360
Alexander Duyckaed069d2016-04-14 15:33:37 -04002361 err = iptunnel_handle_offloads(skb, type);
2362 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08002363 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07002364
Johannes Bergd58ff352017-06-16 14:29:23 +02002365 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01002366 vxh->vx_flags = VXLAN_HF_VNI;
2367 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07002368
Tom Herbertdfd86452015-01-12 17:00:38 -08002369 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01002370 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08002371
Jiri Benc54bfd872016-02-16 21:58:58 +01002372 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2373 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2374 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08002375
2376 if (!skb_is_gso(skb)) {
2377 skb->ip_summed = CHECKSUM_NONE;
2378 skb->encapsulation = 0;
2379 }
2380 }
2381
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002382 if (vxflags & VXLAN_F_GBP)
2383 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02002384 if (vxflags & VXLAN_F_GPE) {
2385 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2386 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002387 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02002388 inner_protocol = skb->protocol;
2389 }
Thomas Graf35114942015-01-15 03:53:55 +01002390
Jiri Bence1e53142016-04-05 14:47:13 +02002391 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08002392 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07002393}
Pravin B Shelar49560532013-08-19 11:23:17 -07002394
pravin shelar655c3de2016-11-13 20:43:55 -08002395static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2396 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002397 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002398 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002399 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002400 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01002401{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002402 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002403 struct rtable *rt = NULL;
2404 struct flowi4 fl4;
2405
pravin shelar655c3de2016-11-13 20:43:55 -08002406 if (!sock4)
2407 return ERR_PTR(-EIO);
2408
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002409 if (tos && !info)
2410 use_cache = false;
2411 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002412 rt = dst_cache_get_ip4(dst_cache, saddr);
2413 if (rt)
2414 return rt;
2415 }
2416
Jiri Benc1a8496b2016-02-02 18:09:14 +01002417 memset(&fl4, 0, sizeof(fl4));
2418 fl4.flowi4_oif = oif;
2419 fl4.flowi4_tos = RT_TOS(tos);
2420 fl4.flowi4_mark = skb->mark;
2421 fl4.flowi4_proto = IPPROTO_UDP;
2422 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002423 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002424 fl4.fl4_dport = dport;
2425 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002426
2427 rt = ip_route_output_key(vxlan->net, &fl4);
Enrico Weigelt478db1f2019-06-05 23:19:13 +02002428 if (!IS_ERR(rt)) {
pravin shelar655c3de2016-11-13 20:43:55 -08002429 if (rt->dst.dev == dev) {
2430 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2431 ip_rt_put(rt);
2432 return ERR_PTR(-ELOOP);
2433 }
2434
Jiri Benc1a8496b2016-02-02 18:09:14 +01002435 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002436 if (use_cache)
2437 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08002438 } else {
2439 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2440 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002441 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002442 return rt;
2443}
2444
Jiri Bence5d4b292015-12-07 13:04:30 +01002445#if IS_ENABLED(CONFIG_IPV6)
2446static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08002447 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08002448 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01002449 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002450 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01002451 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002452 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002453 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002454 struct dst_cache *dst_cache,
2455 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01002456{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002457 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002458 struct dst_entry *ndst;
2459 struct flowi6 fl6;
Jiri Bence5d4b292015-12-07 13:04:30 +01002460
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002461 if (!sock6)
2462 return ERR_PTR(-EIO);
2463
Daniel Borkmann14006152016-03-04 15:15:08 +01002464 if (tos && !info)
2465 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002466 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002467 ndst = dst_cache_get_ip6(dst_cache, saddr);
2468 if (ndst)
2469 return ndst;
2470 }
2471
Jiri Bence5d4b292015-12-07 13:04:30 +01002472 memset(&fl6, 0, sizeof(fl6));
2473 fl6.flowi6_oif = oif;
2474 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002475 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01002476 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01002477 fl6.flowi6_mark = skb->mark;
2478 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002479 fl6.fl6_dport = dport;
2480 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01002481
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +01002482 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk,
2483 &fl6, NULL);
2484 if (unlikely(IS_ERR(ndst))) {
pravin shelar655c3de2016-11-13 20:43:55 -08002485 netdev_dbg(dev, "no route to %pI6\n", daddr);
2486 return ERR_PTR(-ENETUNREACH);
2487 }
2488
2489 if (unlikely(ndst->dev == dev)) {
2490 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2491 dst_release(ndst);
2492 return ERR_PTR(-ELOOP);
2493 }
Jiri Bence5d4b292015-12-07 13:04:30 +01002494
2495 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002496 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002497 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01002498 return ndst;
2499}
2500#endif
2501
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002502/* Bypass encapsulation if the destination is local */
2503static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Stefano Briviofc68c992020-08-04 07:53:44 +02002504 struct vxlan_dev *dst_vxlan, __be32 vni,
2505 bool snoop)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002506{
Li RongQing8f849852014-01-04 13:57:59 +08002507 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08002508 union vxlan_addr loopback;
2509 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Eric Dumazet4179cb52019-02-07 12:27:38 -08002510 struct net_device *dev;
Li RongQingce6502a2014-10-16 08:49:41 +08002511 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002512
Li RongQing8f849852014-01-04 13:57:59 +08002513 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2514 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002515 skb->pkt_type = PACKET_HOST;
2516 skb->encapsulation = 0;
2517 skb->dev = dst_vxlan->dev;
2518 __skb_pull(skb, skb_network_offset(skb));
2519
Cong Wange4c7ed42013-08-31 13:44:33 +08002520 if (remote_ip->sa.sa_family == AF_INET) {
2521 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2522 loopback.sa.sa_family = AF_INET;
2523#if IS_ENABLED(CONFIG_IPV6)
2524 } else {
2525 loopback.sin6.sin6_addr = in6addr_loopback;
2526 loopback.sa.sa_family = AF_INET6;
2527#endif
2528 }
2529
Eric Dumazet4179cb52019-02-07 12:27:38 -08002530 rcu_read_lock();
2531 dev = skb->dev;
2532 if (unlikely(!(dev->flags & IFF_UP))) {
2533 kfree_skb(skb);
2534 goto drop;
2535 }
2536
Stefano Briviofc68c992020-08-04 07:53:44 +02002537 if ((dst_vxlan->cfg.flags & VXLAN_F_LEARN) && snoop)
Eric Dumazet4179cb52019-02-07 12:27:38 -08002538 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002539
2540 u64_stats_update_begin(&tx_stats->syncp);
2541 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002542 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002543 u64_stats_update_end(&tx_stats->syncp);
2544
2545 if (netif_rx(skb) == NET_RX_SUCCESS) {
2546 u64_stats_update_begin(&rx_stats->syncp);
2547 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002548 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002549 u64_stats_update_end(&rx_stats->syncp);
2550 } else {
Eric Dumazet4179cb52019-02-07 12:27:38 -08002551drop:
Li RongQingce6502a2014-10-16 08:49:41 +08002552 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002553 }
Eric Dumazet4179cb52019-02-07 12:27:38 -08002554 rcu_read_unlock();
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002555}
2556
pravin shelarfee1fad2016-11-13 20:43:56 -08002557static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002558 struct vxlan_dev *vxlan,
2559 union vxlan_addr *daddr,
2560 __be16 dst_port, int dst_ifindex, __be32 vni,
2561 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002562 u32 rt_flags)
2563{
2564#if IS_ENABLED(CONFIG_IPV6)
2565 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2566 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2567 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2568 */
2569 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2570#endif
2571 /* Bypass encapsulation if the destination is local */
2572 if (rt_flags & RTCF_LOCAL &&
2573 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2574 struct vxlan_dev *dst_vxlan;
2575
2576 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002577 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002578 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002579 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002580 if (!dst_vxlan) {
2581 dev->stats.tx_errors++;
2582 kfree_skb(skb);
2583
2584 return -ENOENT;
2585 }
Stefano Briviofc68c992020-08-04 07:53:44 +02002586 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni, true);
pravin shelarfee1fad2016-11-13 20:43:56 -08002587 return 1;
2588 }
2589
2590 return 0;
2591}
2592
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002593static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002594 __be32 default_vni, struct vxlan_rdst *rdst,
2595 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002596{
Paolo Abenid71785f2016-02-12 15:43:57 +01002597 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002598 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002599 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002600 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002601 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002602 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002603 struct vxlan_metadata _md;
2604 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002605 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002606 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002607 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002608 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002609 int ifindex;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07002610 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002611 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002612 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002613 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002614
Jiri Benc61adedf2015-08-20 13:56:25 +02002615 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002616
Thomas Grafee122c72015-07-21 10:43:58 +02002617 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002618 dst = &rdst->remote_ip;
2619 if (vxlan_addr_any(dst)) {
2620 if (did_rsc) {
2621 /* short-circuited back to local bridge */
Stefano Briviofc68c992020-08-04 07:53:44 +02002622 vxlan_encap_bypass(skb, vxlan, vxlan,
2623 default_vni, true);
pravin shelar0770b532016-11-13 20:43:57 -08002624 return;
2625 }
2626 goto drop;
2627 }
2628
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002629 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002630 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002631 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002632 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002633 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002634 md->gbp = skb->mark;
Hangbin Liu72f6d712018-04-17 14:11:28 +08002635 if (flags & VXLAN_F_TTL_INHERIT) {
2636 ttl = ip_tunnel_get_ttl(old_iph, skb);
2637 } else {
2638 ttl = vxlan->cfg.ttl;
2639 if (!ttl && vxlan_addr_multicast(dst))
2640 ttl = 1;
2641 }
pravin shelar0770b532016-11-13 20:43:57 -08002642
2643 tos = vxlan->cfg.tos;
2644 if (tos == 1)
2645 tos = ip_tunnel_get_dsfield(old_iph, skb);
2646
2647 if (dst->sa.sa_family == AF_INET)
2648 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2649 else
2650 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2651 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002652 } else {
2653 if (!info) {
2654 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2655 dev->name);
2656 goto drop;
2657 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002658 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002659 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002660 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002661 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2662 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002663 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002664 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2665 }
Thomas Grafee122c72015-07-21 10:43:58 +02002666 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002667 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2668 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002669 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002670 dst_cache = &info->dst_cache;
Xin Longeadf52c2019-10-29 01:24:32 +08002671 if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
2672 if (info->options_len < sizeof(*md))
2673 goto drop;
pravin shelar0770b532016-11-13 20:43:57 -08002674 md = ip_tunnel_info_opts(info);
Xin Longeadf52c2019-10-29 01:24:32 +08002675 }
Jiri Benca725e512015-08-20 13:56:30 +02002676 ttl = info->key.ttl;
2677 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002678 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002679 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002680 }
pravin shelar0770b532016-11-13 20:43:57 -08002681 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2682 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002683
Jakub Kicinski56de8592017-02-24 11:43:36 -08002684 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002685 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002686 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002687 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002688 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002689
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002690 if (!ifindex)
2691 ifindex = sock4->sock->sk->sk_bound_dev_if;
2692
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002693 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002694 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002695 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002696 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002697 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002698 if (IS_ERR(rt)) {
2699 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002700 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002701 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002702
pravin shelarfee1fad2016-11-13 20:43:56 -08002703 if (!info) {
Stefano Briviob4d306972018-11-08 12:19:16 +01002704 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002705 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002706 dst_port, ifindex, vni,
2707 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002708 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002709 goto out_unlock;
Stefano Briviob4d306972018-11-08 12:19:16 +01002710
2711 if (vxlan->cfg.df == VXLAN_DF_SET) {
2712 df = htons(IP_DF);
2713 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2714 struct ethhdr *eth = eth_hdr(skb);
2715
2716 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2717 (ntohs(eth->h_proto) == ETH_P_IP &&
2718 old_iph->frag_off & htons(IP_DF)))
2719 df = htons(IP_DF);
2720 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002721 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002722 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002723 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002724
pravin shelarc46b7892016-11-13 20:43:54 -08002725 ndst = &rt->dst;
Stefano Briviofc68c992020-08-04 07:53:44 +02002726 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM,
2727 netif_is_any_bridge_port(dev));
2728 if (err < 0) {
2729 goto tx_error;
2730 } else if (err) {
2731 if (info) {
2732 struct in_addr src, dst;
2733
2734 src = remote_ip.sin.sin_addr;
2735 dst = local_ip.sin.sin_addr;
2736 info->key.u.ipv4.src = src.s_addr;
2737 info->key.u.ipv4.dst = dst.s_addr;
2738 }
2739 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2740 dst_release(ndst);
2741 goto out_unlock;
2742 }
Xin Longa93bf0f2017-12-18 14:20:56 +08002743
Hangbin Liua0dced12020-08-05 10:41:31 +08002744 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002745 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002746 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002747 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002748 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002749 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002750
Brian Russell11586322017-02-24 17:47:11 +00002751 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002752 dst->sin.sin_addr.s_addr, tos, ttl, df,
2753 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002754#if IS_ENABLED(CONFIG_IPV6)
2755 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002756 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002757
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002758 if (!ifindex)
2759 ifindex = sock6->sock->sk->sk_bound_dev_if;
2760
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002761 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002762 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002763 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002764 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002765 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002766 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002767 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002768 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002769 goto tx_error;
2770 }
pravin shelar655c3de2016-11-13 20:43:55 -08002771
pravin shelarfee1fad2016-11-13 20:43:56 -08002772 if (!info) {
2773 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002774
pravin shelarfee1fad2016-11-13 20:43:56 -08002775 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002776 dst_port, ifindex, vni,
2777 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002778 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002779 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002780 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002781
Stefano Briviofc68c992020-08-04 07:53:44 +02002782 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM,
2783 netif_is_any_bridge_port(dev));
2784 if (err < 0) {
2785 goto tx_error;
2786 } else if (err) {
2787 if (info) {
2788 struct in6_addr src, dst;
2789
2790 src = remote_ip.sin6.sin6_addr;
2791 dst = local_ip.sin6.sin6_addr;
2792 info->key.u.ipv6.src = src;
2793 info->key.u.ipv6.dst = dst;
2794 }
2795
2796 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2797 dst_release(ndst);
2798 goto out_unlock;
2799 }
Xin Longa93bf0f2017-12-18 14:20:56 +08002800
Hangbin Liua0dced12020-08-05 10:41:31 +08002801 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002802 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002803 skb_scrub_packet(skb, xnet);
2804 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002805 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002806 if (err < 0)
2807 goto tx_error;
2808
pravin shelar0770b532016-11-13 20:43:57 -08002809 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002810 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002811 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002812 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002813#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002814 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002815out_unlock:
2816 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002817 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002818
2819drop:
2820 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002821 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002822 return;
2823
2824tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002825 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002826 if (err == -ELOOP)
2827 dev->stats.collisions++;
2828 else if (err == -ENETUNREACH)
2829 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002830 dst_release(ndst);
2831 dev->stats.tx_errors++;
2832 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002833}
2834
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002835static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
2836 struct vxlan_fdb *f, __be32 vni, bool did_rsc)
2837{
2838 struct vxlan_rdst nh_rdst;
2839 struct nexthop *nh;
2840 bool do_xmit;
2841 u32 hash;
2842
2843 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2844 hash = skb_get_hash(skb);
2845
2846 rcu_read_lock();
2847 nh = rcu_dereference(f->nh);
2848 if (!nh) {
2849 rcu_read_unlock();
2850 goto drop;
2851 }
2852 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2853 rcu_read_unlock();
2854
2855 if (likely(do_xmit))
2856 vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc);
2857 else
2858 goto drop;
2859
2860 return;
2861
2862drop:
2863 dev->stats.tx_dropped++;
2864 dev_kfree_skb(skb);
2865}
2866
David Stevens66817122013-03-15 04:35:51 +00002867/* Transmit local packets over Vxlan
2868 *
2869 * Outer IP header inherits ECN and DF from inner header.
2870 * Outer UDP destination is the VXLAN assigned port.
2871 * source port is based on hash of flow
2872 */
2873static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2874{
2875 struct vxlan_dev *vxlan = netdev_priv(dev);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002876 struct vxlan_rdst *rdst, *fdst = NULL;
Xin Long8bff36852017-11-11 19:58:50 +08002877 const struct ip_tunnel_info *info;
2878 bool did_rsc = false;
David Stevens66817122013-03-15 04:35:51 +00002879 struct vxlan_fdb *f;
Xin Long8bff36852017-11-11 19:58:50 +08002880 struct ethhdr *eth;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002881 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002882
Jiri Benc61adedf2015-08-20 13:56:25 +02002883 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002884
David Stevens66817122013-03-15 04:35:51 +00002885 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002886
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002887 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002888 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2889 info->mode & IP_TUNNEL_INFO_TX) {
2890 vni = tunnel_id_to_key32(info->key.tun_id);
2891 } else {
2892 if (info && info->mode & IP_TUNNEL_INFO_TX)
2893 vxlan_xmit_one(skb, dev, vni, NULL, false);
2894 else
2895 kfree_skb(skb);
2896 return NETDEV_TX_OK;
2897 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002898 }
2899
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002900 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002901 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002902 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002903 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002904#if IS_ENABLED(CONFIG_IPV6)
Xin Long8bff36852017-11-11 19:58:50 +08002905 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2906 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2907 sizeof(struct nd_msg)) &&
2908 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2909 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2910
2911 if (m->icmph.icmp6_code == 0 &&
2912 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002913 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002914 }
2915#endif
2916 }
David Stevens66817122013-03-15 04:35:51 +00002917
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002918 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002919 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002920 did_rsc = false;
2921
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002922 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002923 (ntohs(eth->h_proto) == ETH_P_IP ||
2924 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002925 did_rsc = route_shortcircuit(dev, skb);
2926 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002927 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002928 }
2929
David Stevens66817122013-03-15 04:35:51 +00002930 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002931 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002932 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002933 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002934 !is_multicast_ether_addr(eth->h_dest))
2935 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002936
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002937 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002938 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002939 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002940 }
David Stevens66817122013-03-15 04:35:51 +00002941 }
2942
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002943 if (rcu_access_pointer(f->nh)) {
2944 vxlan_xmit_nh(skb, dev, f,
2945 (vni ? : vxlan->default_dst.remote_vni), did_rsc);
2946 } else {
2947 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2948 struct sk_buff *skb1;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002949
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002950 if (!fdst) {
2951 fdst = rdst;
2952 continue;
2953 }
2954 skb1 = skb_clone(skb, GFP_ATOMIC);
2955 if (skb1)
2956 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002957 }
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002958 if (fdst)
2959 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2960 else
2961 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002962 }
2963
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002964 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002965}
2966
stephen hemmingerd3428942012-10-01 12:32:35 +00002967/* Walk the forwarding table and purge stale entries */
Kees Cookdf7e8282017-10-04 16:26:59 -07002968static void vxlan_cleanup(struct timer_list *t)
stephen hemmingerd3428942012-10-01 12:32:35 +00002969{
Kees Cookdf7e8282017-10-04 16:26:59 -07002970 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
stephen hemmingerd3428942012-10-01 12:32:35 +00002971 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2972 unsigned int h;
2973
2974 if (!netif_running(vxlan->dev))
2975 return;
2976
stephen hemmingerd3428942012-10-01 12:32:35 +00002977 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2978 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002979
Litao jiaofe1e0712019-06-06 17:57:58 +08002980 spin_lock(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002981 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2982 struct vxlan_fdb *f
2983 = container_of(p, struct vxlan_fdb, hlist);
2984 unsigned long timeout;
2985
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002986 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002987 continue;
2988
Roopa Prabhudef499c2017-03-27 15:46:41 -07002989 if (f->flags & NTF_EXT_LEARNED)
2990 continue;
2991
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002992 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002993 if (time_before_eq(timeout, jiffies)) {
2994 netdev_dbg(vxlan->dev,
2995 "garbage collect %pM\n",
2996 f->eth_addr);
2997 f->state = NUD_STALE;
Petr Machata0e6160f2018-11-21 08:02:35 +00002998 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002999 } else if (time_before(timeout, next_timer))
3000 next_timer = timeout;
3001 }
Litao jiaofe1e0712019-06-06 17:57:58 +08003002 spin_unlock(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003003 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003004
3005 mod_timer(&vxlan->age_timer, next_timer);
3006}
3007
Mark Blocha53cb292017-06-02 03:24:08 +03003008static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
3009{
3010 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3011
3012 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02003013 hlist_del_init_rcu(&vxlan->hlist4.hlist);
3014#if IS_ENABLED(CONFIG_IPV6)
3015 hlist_del_init_rcu(&vxlan->hlist6.hlist);
3016#endif
Mark Blocha53cb292017-06-02 03:24:08 +03003017 spin_unlock(&vn->sock_lock);
3018}
3019
Jiri Benc69e76662017-07-02 19:00:57 +02003020static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
3021 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003022{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003023 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01003024 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003025
Jiri Benc69e76662017-07-02 19:00:57 +02003026 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003027 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02003028 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003029 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003030}
3031
stephen hemmingerd3428942012-10-01 12:32:35 +00003032/* Setup stats when device is created */
3033static int vxlan_init(struct net_device *dev)
3034{
Taehee Yoo384d91c2020-03-18 13:28:09 +00003035 struct vxlan_dev *vxlan = netdev_priv(dev);
3036 int err;
3037
WANG Cong1c213bd2014-02-13 11:46:28 -08003038 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00003039 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00003040 return -ENOMEM;
3041
Taehee Yoo384d91c2020-03-18 13:28:09 +00003042 err = gro_cells_init(&vxlan->gro_cells, dev);
3043 if (err) {
3044 free_percpu(dev->tstats);
3045 return err;
3046 }
3047
stephen hemmingerd3428942012-10-01 12:32:35 +00003048 return 0;
3049}
3050
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08003051static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003052{
3053 struct vxlan_fdb *f;
Litao jiaofe1e0712019-06-06 17:57:58 +08003054 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003055
Litao jiaofe1e0712019-06-06 17:57:58 +08003056 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08003057 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003058 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003059 vxlan_fdb_destroy(vxlan, f, true, true);
Litao jiaofe1e0712019-06-06 17:57:58 +08003060 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003061}
3062
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003063static void vxlan_uninit(struct net_device *dev)
3064{
3065 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003066
Stefano Brivioad6c9982019-03-08 16:40:57 +01003067 gro_cells_destroy(&vxlan->gro_cells);
3068
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08003069 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003070
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003071 free_percpu(dev->tstats);
3072}
3073
stephen hemmingerd3428942012-10-01 12:32:35 +00003074/* Start ageing timer and join group when device is brought up */
3075static int vxlan_open(struct net_device *dev)
3076{
3077 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02003078 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003079
Jiri Benc205f3562015-09-24 13:50:01 +02003080 ret = vxlan_sock_add(vxlan);
3081 if (ret < 0)
3082 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00003083
Gao feng79d4a942013-12-10 16:37:32 +08003084 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003085 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03003086 if (ret == -EADDRINUSE)
3087 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003088 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02003089 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003090 return ret;
3091 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003092 }
3093
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003094 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00003095 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
3096
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003097 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00003098}
3099
3100/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003101static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00003102{
Cong Wang31fec5a2013-05-27 22:35:52 +00003103 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003104
stephen hemmingerd3428942012-10-01 12:32:35 +00003105 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3106 struct hlist_node *p, *n;
Litao jiaofe1e0712019-06-06 17:57:58 +08003107
3108 spin_lock_bh(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003109 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
3110 struct vxlan_fdb *f
3111 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003112 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
3113 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003114 /* the all_zeros_mac entry is deleted at vxlan_uninit */
Taehee Yoofda2ec62020-08-01 07:07:50 +00003115 if (is_zero_ether_addr(f->eth_addr) &&
3116 f->vni == vxlan->cfg.vni)
3117 continue;
3118 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00003119 }
Litao jiaofe1e0712019-06-06 17:57:58 +08003120 spin_unlock_bh(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003121 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003122}
3123
3124/* Cleanup timer and forwarding table on shutdown */
3125static int vxlan_stop(struct net_device *dev)
3126{
3127 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003128 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003129 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00003130
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03003131 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07003132 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003133 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00003134
3135 del_timer_sync(&vxlan->age_timer);
3136
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003137 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02003138 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00003139
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003140 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00003141}
3142
stephen hemmingerd3428942012-10-01 12:32:35 +00003143/* Stub, nothing needs to be done. */
3144static void vxlan_set_multicast_list(struct net_device *dev)
3145{
3146}
3147
Daniel Borkmann345010b2013-12-18 00:21:08 +01003148static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
3149{
3150 struct vxlan_dev *vxlan = netdev_priv(dev);
3151 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00003152 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3153 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003154 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04003155
3156 /* This check is different than dev->max_mtu, because it looks at
3157 * the lowerdev->mtu, rather than the static dev->max_mtu
3158 */
3159 if (lowerdev) {
3160 int max_mtu = lowerdev->mtu -
3161 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
3162 if (new_mtu > max_mtu)
3163 return -EINVAL;
3164 }
3165
3166 dev->mtu = new_mtu;
3167 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01003168}
3169
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07003170static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
3171{
3172 struct vxlan_dev *vxlan = netdev_priv(dev);
3173 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3174 __be16 sport, dport;
3175
3176 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
3177 vxlan->cfg.port_max, true);
3178 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
3179
Jiri Benc239e9442015-12-07 13:04:31 +01003180 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003181 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01003182 struct rtable *rt;
3183
pravin shelar655c3de2016-11-13 20:43:55 -08003184 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01003185 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01003186 &info->key.u.ipv4.src, dport, sport,
3187 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01003188 if (IS_ERR(rt))
3189 return PTR_ERR(rt);
3190 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01003191 } else {
3192#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08003193 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01003194 struct dst_entry *ndst;
3195
pravin shelar655c3de2016-11-13 20:43:55 -08003196 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003197 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01003198 &info->key.u.ipv6.src, dport, sport,
3199 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01003200 if (IS_ERR(ndst))
3201 return PTR_ERR(ndst);
3202 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01003203#else /* !CONFIG_IPV6 */
3204 return -EPFNOSUPPORT;
3205#endif
3206 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01003207 info->key.tp_src = sport;
3208 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01003209 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07003210}
3211
Jiri Benc0c867c92016-04-05 14:47:10 +02003212static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00003213 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003214 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00003215 .ndo_open = vxlan_open,
3216 .ndo_stop = vxlan_stop,
3217 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00003218 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00003219 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01003220 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00003221 .ndo_validate_addr = eth_validate_addr,
3222 .ndo_set_mac_address = eth_mac_addr,
3223 .ndo_fdb_add = vxlan_fdb_add,
3224 .ndo_fdb_del = vxlan_fdb_delete,
3225 .ndo_fdb_dump = vxlan_fdb_dump,
Roopa Prabhu474c3c82018-12-15 22:35:10 -08003226 .ndo_fdb_get = vxlan_fdb_get,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07003227 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
Andy Roulin8f1af752019-02-22 18:06:38 +00003228 .ndo_change_proto_down = dev_change_proto_down_generic,
stephen hemmingerd3428942012-10-01 12:32:35 +00003229};
3230
Jiri Bence1e53142016-04-05 14:47:13 +02003231static const struct net_device_ops vxlan_netdev_raw_ops = {
3232 .ndo_init = vxlan_init,
3233 .ndo_uninit = vxlan_uninit,
3234 .ndo_open = vxlan_open,
3235 .ndo_stop = vxlan_stop,
3236 .ndo_start_xmit = vxlan_xmit,
3237 .ndo_get_stats64 = ip_tunnel_get_stats64,
3238 .ndo_change_mtu = vxlan_change_mtu,
3239 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3240};
3241
stephen hemmingerd3428942012-10-01 12:32:35 +00003242/* Info for udev, that this is a virtual tunnel endpoint */
3243static struct device_type vxlan_type = {
3244 .name = "vxlan",
3245};
3246
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02003247/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07003248 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02003249 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003250 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003251static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003252{
3253 struct vxlan_sock *vs;
3254 struct net *net = dev_net(dev);
3255 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07003256 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003257
3258 spin_lock(&vn->sock_lock);
3259 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003260 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
3261 unsigned short type;
3262
3263 if (vs->flags & VXLAN_F_GPE)
3264 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3265 else
3266 type = UDP_TUNNEL_TYPE_VXLAN;
3267
3268 if (push)
3269 udp_tunnel_push_rx_port(dev, vs->sock, type);
3270 else
3271 udp_tunnel_drop_rx_port(dev, vs->sock, type);
3272 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003273 }
3274 spin_unlock(&vn->sock_lock);
3275}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003276
stephen hemmingerd3428942012-10-01 12:32:35 +00003277/* Initialize the device structure. */
3278static void vxlan_setup(struct net_device *dev)
3279{
3280 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00003281 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003282
Jiri Benc65226ef2016-04-28 16:36:30 +02003283 eth_hw_addr_random(dev);
3284 ether_setup(dev);
3285
David S. Millercf124db2017-05-08 12:52:56 -04003286 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00003287 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3288
stephen hemmingerd3428942012-10-01 12:32:35 +00003289 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00003290 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003291 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003292 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003293
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07003294 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003295 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003296 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07003297 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02003298 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00003299
Matthias Schiffera9853432017-06-19 10:03:55 +02003300 /* MTU range: 68 - 65535 */
3301 dev->min_mtu = ETH_MIN_MTU;
3302 dev->max_mtu = ETH_MAX_MTU;
3303
stephen hemminger553675f2013-05-16 11:35:20 +00003304 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003305
Kees Cookdf7e8282017-10-04 16:26:59 -07003306 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
stephen hemmingerd3428942012-10-01 12:32:35 +00003307
3308 vxlan->dev = dev;
3309
Litao jiaofe1e0712019-06-06 17:57:58 +08003310 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3311 spin_lock_init(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003312 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
Litao jiaofe1e0712019-06-06 17:57:58 +08003313 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003314}
3315
Jiri Benc0c867c92016-04-05 14:47:10 +02003316static void vxlan_ether_setup(struct net_device *dev)
3317{
Jiri Benc0c867c92016-04-05 14:47:10 +02003318 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3319 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3320 dev->netdev_ops = &vxlan_netdev_ether_ops;
3321}
3322
Jiri Bence1e53142016-04-05 14:47:13 +02003323static void vxlan_raw_setup(struct net_device *dev)
3324{
Jiri Benc65226ef2016-04-28 16:36:30 +02003325 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02003326 dev->type = ARPHRD_NONE;
3327 dev->hard_header_len = 0;
3328 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02003329 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3330 dev->netdev_ops = &vxlan_netdev_raw_ops;
3331}
3332
stephen hemmingerd3428942012-10-01 12:32:35 +00003333static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3334 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08003335 [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003336 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003337 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08003338 [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003339 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003340 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3341 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003342 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003343 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3344 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3345 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00003346 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00003347 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3348 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3349 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3350 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003351 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00003352 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08003353 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3354 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3355 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08003356 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3357 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01003358 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02003359 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003360 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
Hangbin Liu72f6d712018-04-17 14:11:28 +08003361 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
Stefano Briviob4d306972018-11-08 12:19:16 +01003362 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003363};
3364
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02003365static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3366 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003367{
3368 if (tb[IFLA_ADDRESS]) {
3369 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003370 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3371 "Provided link layer address is not Ethernet");
stephen hemmingerd3428942012-10-01 12:32:35 +00003372 return -EINVAL;
3373 }
3374
3375 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003376 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3377 "Provided Ethernet address is not unicast");
stephen hemmingerd3428942012-10-01 12:32:35 +00003378 return -EADDRNOTAVAIL;
3379 }
3380 }
3381
Matthias Schiffera9853432017-06-19 10:03:55 +02003382 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02003383 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02003384
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003385 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3386 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3387 "MTU must be between 68 and 65535");
Matthias Schiffera9853432017-06-19 10:03:55 +02003388 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003389 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003390 }
3391
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003392 if (!data) {
3393 NL_SET_ERR_MSG(extack,
3394 "Required attributes not provided to perform the operation");
stephen hemmingerd3428942012-10-01 12:32:35 +00003395 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003396 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003397
3398 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003399 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3400
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003401 if (id >= VXLAN_N_VID) {
Sabrina Dubrocacc8e7c62020-04-22 17:29:50 +02003402 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003403 "VXLAN ID must be lower than 16777216");
stephen hemmingerd3428942012-10-01 12:32:35 +00003404 return -ERANGE;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003405 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003406 }
3407
stephen hemminger05f47d62012-10-09 20:35:50 +00003408 if (data[IFLA_VXLAN_PORT_RANGE]) {
3409 const struct ifla_vxlan_port_range *p
3410 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3411
3412 if (ntohs(p->high) < ntohs(p->low)) {
Sabrina Dubrocacc8e7c62020-04-22 17:29:50 +02003413 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003414 "Invalid source port range");
stephen hemminger05f47d62012-10-09 20:35:50 +00003415 return -EINVAL;
3416 }
3417 }
3418
Stefano Briviob4d306972018-11-08 12:19:16 +01003419 if (data[IFLA_VXLAN_DF]) {
3420 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3421
3422 if (df < 0 || df > VXLAN_DF_MAX) {
Sabrina Dubrocacc8e7c62020-04-22 17:29:50 +02003423 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
Stefano Briviob4d306972018-11-08 12:19:16 +01003424 "Invalid DF attribute");
3425 return -EINVAL;
3426 }
3427 }
3428
stephen hemmingerd3428942012-10-01 12:32:35 +00003429 return 0;
3430}
3431
Yan Burman1b13c972013-01-29 23:43:07 +00003432static void vxlan_get_drvinfo(struct net_device *netdev,
3433 struct ethtool_drvinfo *drvinfo)
3434{
3435 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3436 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3437}
3438
Matthias Schiffer36fe3a62019-11-12 22:12:24 +01003439static int vxlan_get_link_ksettings(struct net_device *dev,
3440 struct ethtool_link_ksettings *cmd)
3441{
3442 struct vxlan_dev *vxlan = netdev_priv(dev);
3443 struct vxlan_rdst *dst = &vxlan->default_dst;
3444 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3445 dst->remote_ifindex);
3446
3447 if (!lowerdev) {
3448 cmd->base.duplex = DUPLEX_UNKNOWN;
3449 cmd->base.port = PORT_OTHER;
3450 cmd->base.speed = SPEED_UNKNOWN;
3451
3452 return 0;
3453 }
3454
3455 return __ethtool_get_link_ksettings(lowerdev, cmd);
3456}
3457
Yan Burman1b13c972013-01-29 23:43:07 +00003458static const struct ethtool_ops vxlan_ethtool_ops = {
Matthias Schiffer36fe3a62019-11-12 22:12:24 +01003459 .get_drvinfo = vxlan_get_drvinfo,
3460 .get_link = ethtool_op_get_link,
3461 .get_link_ksettings = vxlan_get_link_ksettings,
Yan Burman1b13c972013-01-29 23:43:07 +00003462};
3463
Tom Herbert3ee64f32014-07-13 19:49:42 -07003464static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003465 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +00003466{
Cong Wange4c7ed42013-08-31 13:44:33 +08003467 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003468 struct udp_port_cfg udp_conf;
3469 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08003470
Tom Herbert3ee64f32014-07-13 19:49:42 -07003471 memset(&udp_conf, 0, sizeof(udp_conf));
3472
3473 if (ipv6) {
3474 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003475 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08003476 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02003477 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003478 } else {
3479 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08003480 }
3481
Tom Herbert3ee64f32014-07-13 19:49:42 -07003482 udp_conf.local_udp_port = port;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003483 udp_conf.bind_ifindex = ifindex;
Cong Wange4c7ed42013-08-31 13:44:33 +08003484
Tom Herbert3ee64f32014-07-13 19:49:42 -07003485 /* Open UDP socket */
3486 err = udp_sock_create(net, &udp_conf, &sock);
3487 if (err < 0)
3488 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08003489
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003490 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08003491}
3492
3493/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02003494static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003495 __be16 port, u32 flags,
3496 int ifindex)
Cong Wange4c7ed42013-08-31 13:44:33 +08003497{
3498 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3499 struct vxlan_sock *vs;
3500 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00003501 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003502 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00003503
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02003504 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08003505 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00003506 return ERR_PTR(-ENOMEM);
3507
3508 for (h = 0; h < VNI_HASH_SIZE; ++h)
3509 INIT_HLIST_HEAD(&vs->vni_list[h]);
3510
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003511 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003512 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00003513 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08003514 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00003515 }
3516
Cong Wange4c7ed42013-08-31 13:44:33 +08003517 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03003518 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08003519 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00003520
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003521 spin_lock(&vn->sock_lock);
3522 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07003523 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07003524 (vs->flags & VXLAN_F_GPE) ?
3525 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07003526 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003527 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00003528
3529 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07003530 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07003531 tunnel_cfg.sk_user_data = vs;
3532 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01003533 tunnel_cfg.encap_rcv = vxlan_rcv;
Stefano Brivioc3a43b92018-11-08 12:19:15 +01003534 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003535 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07003536 tunnel_cfg.gro_receive = vxlan_gro_receive;
3537 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003538
3539 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08003540
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003541 return vs;
3542}
stephen hemminger553675f2013-05-16 11:35:20 +00003543
Jiri Bencb1be00a2015-09-24 13:50:02 +02003544static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003545{
Jiri Benc205f3562015-09-24 13:50:01 +02003546 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3547 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02003548 struct vxlan_dev_node *node;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003549 int l3mdev_index = 0;
3550
3551 if (vxlan->cfg.remote_ifindex)
3552 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3553 vxlan->net, vxlan->cfg.remote_ifindex);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003554
Jiri Benc205f3562015-09-24 13:50:01 +02003555 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003556 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003557 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003558 vxlan->cfg.dst_port, vxlan->cfg.flags,
3559 l3mdev_index);
Reshetova, Elena66af8462017-07-04 15:52:59 +03003560 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003561 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003562 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003563 }
3564 spin_unlock(&vn->sock_lock);
3565 }
Jiri Benc205f3562015-09-24 13:50:01 +02003566 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003567 vs = vxlan_socket_create(vxlan->net, ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003568 vxlan->cfg.dst_port, vxlan->cfg.flags,
3569 l3mdev_index);
Jiri Benc205f3562015-09-24 13:50:01 +02003570 if (IS_ERR(vs))
3571 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003572#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02003573 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003574 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003575 node = &vxlan->hlist6;
3576 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02003577#endif
Jiri Benc69e76662017-07-02 19:00:57 +02003578 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003579 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003580 node = &vxlan->hlist4;
3581 }
3582 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02003583 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00003584}
3585
Jiri Bencb1be00a2015-09-24 13:50:02 +02003586static int vxlan_sock_add(struct vxlan_dev *vxlan)
3587{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003588 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3589 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02003590 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003591 int ret = 0;
3592
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003593 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003594#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003595 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02003596 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02003597 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02003598 if (ret < 0 && ret != -EAFNOSUPPORT)
3599 ipv4 = false;
3600 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02003601#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02003602 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003603 ret = __vxlan_sock_add(vxlan, false);
3604 if (ret < 0)
3605 vxlan_sock_release(vxlan);
3606 return ret;
3607}
3608
Matthias Schiffera9853432017-06-19 10:03:55 +02003609static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3610 struct net_device **lower,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003611 struct vxlan_dev *old,
3612 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003613{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01003614 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02003615 struct vxlan_dev *tmp;
3616 bool use_ipv6 = false;
3617
3618 if (conf->flags & VXLAN_F_GPE) {
3619 /* For now, allow GPE only together with
3620 * COLLECT_METADATA. This can be relaxed later; in such
3621 * case, the other side of the PtP link will have to be
3622 * provided.
3623 */
3624 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3625 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003626 NL_SET_ERR_MSG(extack,
3627 "VXLAN GPE does not support this combination of attributes");
Matthias Schiffera9853432017-06-19 10:03:55 +02003628 return -EINVAL;
3629 }
3630 }
3631
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003632 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3633 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02003634 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003635 conf->saddr.sa.sa_family = AF_INET;
3636 } else if (!conf->remote_ip.sa.sa_family) {
3637 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3638 } else if (!conf->saddr.sa.sa_family) {
3639 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3640 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003641
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003642 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3643 NL_SET_ERR_MSG(extack,
3644 "Local and remote address must be from the same family");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003645 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003646 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003647
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003648 if (vxlan_addr_multicast(&conf->saddr)) {
3649 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003650 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003651 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003652
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003653 if (conf->saddr.sa.sa_family == AF_INET6) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003654 if (!IS_ENABLED(CONFIG_IPV6)) {
3655 NL_SET_ERR_MSG(extack,
3656 "IPv6 support not enabled in the kernel");
Matthias Schiffera9853432017-06-19 10:03:55 +02003657 return -EPFNOSUPPORT;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003658 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003659 use_ipv6 = true;
3660 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003661
3662 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3663 int local_type =
3664 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3665 int remote_type =
3666 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3667
3668 if (local_type & IPV6_ADDR_LINKLOCAL) {
3669 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003670 (remote_type != IPV6_ADDR_ANY)) {
3671 NL_SET_ERR_MSG(extack,
3672 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003673 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003674 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003675
3676 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3677 } else {
3678 if (remote_type ==
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003679 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3680 NL_SET_ERR_MSG(extack,
3681 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003682 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003683 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003684
3685 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3686 }
3687 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003688 }
3689
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003690 if (conf->label && !use_ipv6) {
3691 NL_SET_ERR_MSG(extack,
3692 "Label attribute only applies to IPv6 VXLAN devices");
Matthias Schiffera9853432017-06-19 10:03:55 +02003693 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003694 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003695
3696 if (conf->remote_ifindex) {
3697 struct net_device *lowerdev;
3698
3699 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003700 if (!lowerdev) {
3701 NL_SET_ERR_MSG(extack,
3702 "Invalid local interface, device not found");
Matthias Schiffera9853432017-06-19 10:03:55 +02003703 return -ENODEV;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003704 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003705
3706#if IS_ENABLED(CONFIG_IPV6)
3707 if (use_ipv6) {
3708 struct inet6_dev *idev = __in6_dev_get(lowerdev);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003709 if (idev && idev->cnf.disable_ipv6) {
3710 NL_SET_ERR_MSG(extack,
3711 "IPv6 support disabled by administrator");
Matthias Schiffera9853432017-06-19 10:03:55 +02003712 return -EPERM;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003713 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003714 }
3715#endif
3716
3717 *lower = lowerdev;
3718 } else {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003719 if (vxlan_addr_multicast(&conf->remote_ip)) {
3720 NL_SET_ERR_MSG(extack,
3721 "Local interface required for multicast remote destination");
3722
Matthias Schiffera9853432017-06-19 10:03:55 +02003723 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003724 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003725
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003726#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003727 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3728 NL_SET_ERR_MSG(extack,
3729 "Local interface required for link-local local/remote addresses");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003730 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003731 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003732#endif
3733
Matthias Schiffera9853432017-06-19 10:03:55 +02003734 *lower = NULL;
3735 }
3736
3737 if (!conf->dst_port) {
3738 if (conf->flags & VXLAN_F_GPE)
3739 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3740 else
3741 conf->dst_port = htons(vxlan_port);
3742 }
3743
3744 if (!conf->age_interval)
3745 conf->age_interval = FDB_AGE_DEFAULT;
3746
3747 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3748 if (tmp == old)
3749 continue;
3750
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003751 if (tmp->cfg.vni != conf->vni)
3752 continue;
3753 if (tmp->cfg.dst_port != conf->dst_port)
3754 continue;
3755 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003756 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003757 continue;
3758
3759 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3760 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3761 continue;
3762
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003763 NL_SET_ERR_MSG(extack,
3764 "A VXLAN device with the specified VNI already exists");
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003765 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003766 }
3767
3768 return 0;
3769}
3770
3771static void vxlan_config_apply(struct net_device *dev,
3772 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003773 struct net_device *lowerdev,
3774 struct net *src_net,
3775 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003776{
3777 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003778 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003779 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003780 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3781 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003782
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003783 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003784 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003785 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003786 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003787 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003788
Matthias Schiffera9853432017-06-19 10:03:55 +02003789 if (conf->mtu)
3790 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003791
3792 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003793 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003794
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003795 dst->remote_vni = conf->vni;
3796
3797 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003798
Matthias Schiffera9853432017-06-19 10:03:55 +02003799 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003800 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003801
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003802 dev->gso_max_size = lowerdev->gso_max_size;
3803 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003804
3805 needed_headroom = lowerdev->hard_header_len;
3806
3807 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3808 VXLAN_HEADROOM);
Alexey Kodanevf870c1f2017-12-14 20:20:00 +03003809 if (max_mtu < ETH_MIN_MTU)
3810 max_mtu = ETH_MIN_MTU;
3811
3812 if (!changelink && !conf->mtu)
3813 dev->mtu = max_mtu;
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003814 }
3815
Matthias Schiffera9853432017-06-19 10:03:55 +02003816 if (dev->mtu > max_mtu)
3817 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003818
Jiri Bencb1be00a2015-09-24 13:50:02 +02003819 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3820 needed_headroom += VXLAN6_HEADROOM;
3821 else
3822 needed_headroom += VXLAN_HEADROOM;
3823 dev->needed_headroom = needed_headroom;
3824
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003825 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003826}
stephen hemmingerd3428942012-10-01 12:32:35 +00003827
Matthias Schiffera9853432017-06-19 10:03:55 +02003828static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003829 struct vxlan_config *conf, bool changelink,
3830 struct netlink_ext_ack *extack)
Matthias Schiffera9853432017-06-19 10:03:55 +02003831{
3832 struct vxlan_dev *vxlan = netdev_priv(dev);
3833 struct net_device *lowerdev;
3834 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003835
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003836 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
Matthias Schiffera9853432017-06-19 10:03:55 +02003837 if (ret)
3838 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003839
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003840 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003841
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003842 return 0;
3843}
3844
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003845static int __vxlan_dev_create(struct net *net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003846 struct vxlan_config *conf,
3847 struct netlink_ext_ack *extack)
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003848{
3849 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3850 struct vxlan_dev *vxlan = netdev_priv(dev);
Taehee Yoo0ce18222019-10-21 18:47:57 +00003851 struct net_device *remote_dev = NULL;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003852 struct vxlan_fdb *f = NULL;
Petr Machata6db92462018-12-18 13:16:00 +00003853 bool unregister = false;
Taehee Yoo0ce18222019-10-21 18:47:57 +00003854 struct vxlan_rdst *dst;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003855 int err;
3856
Taehee Yoo0ce18222019-10-21 18:47:57 +00003857 dst = &vxlan->default_dst;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003858 err = vxlan_dev_configure(net, dev, conf, false, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003859 if (err)
3860 return err;
3861
3862 dev->ethtool_ops = &vxlan_ethtool_ops;
3863
3864 /* create an fdb entry for a valid default destination */
Taehee Yoo0ce18222019-10-21 18:47:57 +00003865 if (!vxlan_addr_any(&dst->remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003866 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Taehee Yoo0ce18222019-10-21 18:47:57 +00003867 &dst->remote_ip,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003868 NUD_REACHABLE | NUD_PERMANENT,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003869 vxlan->cfg.dst_port,
Taehee Yoo0ce18222019-10-21 18:47:57 +00003870 dst->remote_vni,
3871 dst->remote_vni,
3872 dst->remote_ifindex,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07003873 NTF_SELF, 0, &f, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003874 if (err)
3875 return err;
3876 }
3877
3878 err = register_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003879 if (err)
3880 goto errout;
Petr Machata6db92462018-12-18 13:16:00 +00003881 unregister = true;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003882
Taehee Yoo0ce18222019-10-21 18:47:57 +00003883 if (dst->remote_ifindex) {
3884 remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
3885 if (!remote_dev)
3886 goto errout;
3887
3888 err = netdev_upper_dev_link(remote_dev, dev, extack);
3889 if (err)
3890 goto errout;
3891 }
3892
Roopa Prabhu0241b832018-07-04 16:46:32 -07003893 err = rtnl_configure_link(dev, NULL);
Petr Machata6db92462018-12-18 13:16:00 +00003894 if (err)
Taehee Yoo0ce18222019-10-21 18:47:57 +00003895 goto unlink;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003896
Petr Machata61f46fe2019-01-16 23:06:38 +00003897 if (f) {
Taehee Yoo0ce18222019-10-21 18:47:57 +00003898 vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
Taehee Yoo7c31e542019-06-28 14:07:25 +09003899
3900 /* notify default fdb entry */
Petr Machata61f46fe2019-01-16 23:06:38 +00003901 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
Petr Machata4c59b7d2019-01-16 23:06:54 +00003902 RTM_NEWNEIGH, true, extack);
Taehee Yoo7c31e542019-06-28 14:07:25 +09003903 if (err) {
3904 vxlan_fdb_destroy(vxlan, f, false, false);
Taehee Yoo0ce18222019-10-21 18:47:57 +00003905 if (remote_dev)
3906 netdev_upper_dev_unlink(remote_dev, dev);
Taehee Yoo7c31e542019-06-28 14:07:25 +09003907 goto unregister;
3908 }
Petr Machata61f46fe2019-01-16 23:06:38 +00003909 }
Roopa Prabhu0241b832018-07-04 16:46:32 -07003910
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003911 list_add(&vxlan->next, &vn->vxlan_list);
Taehee Yoo0ce18222019-10-21 18:47:57 +00003912 if (remote_dev)
3913 dst->remote_dev = remote_dev;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003914 return 0;
Taehee Yoo0ce18222019-10-21 18:47:57 +00003915unlink:
3916 if (remote_dev)
3917 netdev_upper_dev_unlink(remote_dev, dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003918errout:
Petr Machata6db92462018-12-18 13:16:00 +00003919 /* unregister_netdevice() destroys the default FDB entry with deletion
3920 * notification. But the addition notification was not sent yet, so
3921 * destroy the entry by hand here.
3922 */
Roopa Prabhu0241b832018-07-04 16:46:32 -07003923 if (f)
Taehee Yoo7c31e542019-06-28 14:07:25 +09003924 __vxlan_fdb_free(f);
3925unregister:
Petr Machata6db92462018-12-18 13:16:00 +00003926 if (unregister)
3927 unregister_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003928 return err;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003929}
3930
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003931/* Set/clear flags based on attribute */
3932static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3933 int attrtype, unsigned long mask, bool changelink,
3934 bool changelink_supported,
3935 struct netlink_ext_ack *extack)
3936{
3937 unsigned long flags;
3938
3939 if (!tb[attrtype])
3940 return 0;
3941
3942 if (changelink && !changelink_supported) {
3943 vxlan_flag_attr_error(attrtype, extack);
3944 return -EOPNOTSUPP;
3945 }
3946
3947 if (vxlan_policy[attrtype].type == NLA_FLAG)
3948 flags = conf->flags | mask;
3949 else if (nla_get_u8(tb[attrtype]))
3950 flags = conf->flags | mask;
3951 else
3952 flags = conf->flags & ~mask;
3953
3954 conf->flags = flags;
3955
3956 return 0;
3957}
3958
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003959static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3960 struct net_device *dev, struct vxlan_config *conf,
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003961 bool changelink, struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003962{
3963 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003964 int err = 0;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003965
3966 memset(conf, 0, sizeof(*conf));
3967
3968 /* if changelink operation, start with old existing cfg */
3969 if (changelink)
3970 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3971
3972 if (data[IFLA_VXLAN_ID]) {
3973 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3974
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003975 if (changelink && (vni != conf->vni)) {
3976 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003977 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003978 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003979 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3980 }
3981
3982 if (data[IFLA_VXLAN_GROUP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003983 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3984 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 +02003985 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003986 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003987
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003988 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003989 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003990 } else if (data[IFLA_VXLAN_GROUP6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003991 if (!IS_ENABLED(CONFIG_IPV6)) {
3992 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003993 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003994 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003995
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003996 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3997 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 +02003998 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003999 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02004000
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004001 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
4002 conf->remote_ip.sa.sa_family = AF_INET6;
4003 }
4004
4005 if (data[IFLA_VXLAN_LOCAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004006 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
4007 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 +02004008 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004009 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02004010
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004011 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
4012 conf->saddr.sa.sa_family = AF_INET;
4013 } else if (data[IFLA_VXLAN_LOCAL6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004014 if (!IS_ENABLED(CONFIG_IPV6)) {
4015 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004016 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004017 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004018
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004019 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
4020 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 +02004021 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004022 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02004023
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004024 /* TODO: respect scope id */
4025 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
4026 conf->saddr.sa.sa_family = AF_INET6;
4027 }
4028
4029 if (data[IFLA_VXLAN_LINK])
4030 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
4031
4032 if (data[IFLA_VXLAN_TOS])
4033 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
4034
4035 if (data[IFLA_VXLAN_TTL])
4036 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
4037
Hangbin Liu72f6d712018-04-17 14:11:28 +08004038 if (data[IFLA_VXLAN_TTL_INHERIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004039 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
4040 VXLAN_F_TTL_INHERIT, changelink, false,
4041 extack);
4042 if (err)
4043 return err;
4044
Hangbin Liu72f6d712018-04-17 14:11:28 +08004045 }
4046
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004047 if (data[IFLA_VXLAN_LABEL])
4048 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
4049 IPV6_FLOWLABEL_MASK;
4050
4051 if (data[IFLA_VXLAN_LEARNING]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004052 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
4053 VXLAN_F_LEARN, changelink, true,
4054 extack);
4055 if (err)
4056 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004057 } else if (!changelink) {
4058 /* default to learn on a new device */
4059 conf->flags |= VXLAN_F_LEARN;
4060 }
4061
Ido Schimmel40051c42018-11-21 08:02:40 +00004062 if (data[IFLA_VXLAN_AGEING])
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004063 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004064
4065 if (data[IFLA_VXLAN_PROXY]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004066 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
4067 VXLAN_F_PROXY, changelink, false,
4068 extack);
4069 if (err)
4070 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004071 }
4072
4073 if (data[IFLA_VXLAN_RSC]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004074 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
4075 VXLAN_F_RSC, changelink, false,
4076 extack);
4077 if (err)
4078 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004079 }
4080
4081 if (data[IFLA_VXLAN_L2MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004082 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
4083 VXLAN_F_L2MISS, changelink, false,
4084 extack);
4085 if (err)
4086 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004087 }
4088
4089 if (data[IFLA_VXLAN_L3MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004090 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
4091 VXLAN_F_L3MISS, changelink, false,
4092 extack);
4093 if (err)
4094 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004095 }
4096
4097 if (data[IFLA_VXLAN_LIMIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004098 if (changelink) {
4099 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
4100 "Cannot change limit");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004101 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004102 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004103 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
4104 }
4105
4106 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004107 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
4108 VXLAN_F_COLLECT_METADATA, changelink, false,
4109 extack);
4110 if (err)
4111 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004112 }
4113
4114 if (data[IFLA_VXLAN_PORT_RANGE]) {
4115 if (!changelink) {
4116 const struct ifla_vxlan_port_range *p
4117 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
4118 conf->port_min = ntohs(p->low);
4119 conf->port_max = ntohs(p->high);
4120 } else {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004121 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
4122 "Cannot change port range");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004123 return -EOPNOTSUPP;
4124 }
4125 }
4126
4127 if (data[IFLA_VXLAN_PORT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004128 if (changelink) {
4129 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
4130 "Cannot change port");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004131 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004132 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004133 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
4134 }
4135
4136 if (data[IFLA_VXLAN_UDP_CSUM]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004137 if (changelink) {
4138 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
4139 "Cannot change UDP_CSUM flag");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004140 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004141 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004142 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
4143 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
4144 }
4145
4146 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004147 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4148 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
4149 false, extack);
4150 if (err)
4151 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004152 }
4153
4154 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004155 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4156 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
4157 false, extack);
4158 if (err)
4159 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004160 }
4161
4162 if (data[IFLA_VXLAN_REMCSUM_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004163 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
4164 VXLAN_F_REMCSUM_TX, changelink, false,
4165 extack);
4166 if (err)
4167 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004168 }
4169
4170 if (data[IFLA_VXLAN_REMCSUM_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004171 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
4172 VXLAN_F_REMCSUM_RX, changelink, false,
4173 extack);
4174 if (err)
4175 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004176 }
4177
4178 if (data[IFLA_VXLAN_GBP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004179 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
4180 VXLAN_F_GBP, changelink, false, extack);
4181 if (err)
4182 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004183 }
4184
4185 if (data[IFLA_VXLAN_GPE]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004186 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
4187 VXLAN_F_GPE, changelink, false,
4188 extack);
4189 if (err)
4190 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004191 }
4192
4193 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004194 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
4195 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
4196 false, extack);
4197 if (err)
4198 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004199 }
4200
4201 if (tb[IFLA_MTU]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004202 if (changelink) {
4203 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
4204 "Cannot change mtu");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004205 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004206 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004207 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
4208 }
4209
Stefano Briviob4d306972018-11-08 12:19:16 +01004210 if (data[IFLA_VXLAN_DF])
4211 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
4212
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004213 return 0;
4214}
4215
4216static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02004217 struct nlattr *tb[], struct nlattr *data[],
4218 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004219{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004220 struct vxlan_config conf;
4221 int err;
4222
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004223 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004224 if (err)
4225 return err;
4226
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07004227 return __vxlan_dev_create(src_net, dev, &conf, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00004228}
4229
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004230static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02004231 struct nlattr *data[],
4232 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004233{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004234 struct vxlan_dev *vxlan = netdev_priv(dev);
Petr Machata8db9427d52019-01-16 23:06:39 +00004235 struct net_device *lowerdev;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004236 struct vxlan_config conf;
Taehee Yoo0ce18222019-10-21 18:47:57 +00004237 struct vxlan_rdst *dst;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004238 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004239
Taehee Yoo0ce18222019-10-21 18:47:57 +00004240 dst = &vxlan->default_dst;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004241 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004242 if (err)
4243 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07004244
Petr Machata8db9427d52019-01-16 23:06:39 +00004245 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
4246 vxlan, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004247 if (err)
4248 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004249
Taehee Yooc6761cf2019-10-30 08:15:12 +00004250 if (dst->remote_dev == lowerdev)
4251 lowerdev = NULL;
4252
Taehee Yoo0ce18222019-10-21 18:47:57 +00004253 err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
4254 extack);
4255 if (err)
4256 return err;
4257
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004258 /* handle default dst entry */
Petr Machata038a5a92019-01-16 23:06:41 +00004259 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
Litao jiaofe1e0712019-06-06 17:57:58 +08004260 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
4261
4262 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata038a5a92019-01-16 23:06:41 +00004263 if (!vxlan_addr_any(&conf.remote_ip)) {
Petr Machatace5e098f2018-12-18 13:16:02 +00004264 err = vxlan_fdb_update(vxlan, all_zeros_mac,
Petr Machata038a5a92019-01-16 23:06:41 +00004265 &conf.remote_ip,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004266 NUD_REACHABLE | NUD_PERMANENT,
Petr Machatace5e098f2018-12-18 13:16:02 +00004267 NLM_F_APPEND | NLM_F_CREATE,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004268 vxlan->cfg.dst_port,
Petr Machata038a5a92019-01-16 23:06:41 +00004269 conf.vni, conf.vni,
4270 conf.remote_ifindex,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07004271 NTF_SELF, 0, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004272 if (err) {
Litao jiaofe1e0712019-06-06 17:57:58 +08004273 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Taehee Yoo0ce18222019-10-21 18:47:57 +00004274 netdev_adjacent_change_abort(dst->remote_dev,
4275 lowerdev, dev);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004276 return err;
4277 }
4278 }
Petr Machata1cdc98c2019-01-16 23:06:43 +00004279 if (!vxlan_addr_any(&dst->remote_ip))
4280 __vxlan_fdb_delete(vxlan, all_zeros_mac,
4281 dst->remote_ip,
4282 vxlan->cfg.dst_port,
4283 dst->remote_vni,
4284 dst->remote_vni,
4285 dst->remote_ifindex,
4286 true);
Litao jiaofe1e0712019-06-06 17:57:58 +08004287 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004288 }
4289
Petr Machata038a5a92019-01-16 23:06:41 +00004290 if (conf.age_interval != vxlan->cfg.age_interval)
4291 mod_timer(&vxlan->age_timer, jiffies);
4292
Taehee Yoo0ce18222019-10-21 18:47:57 +00004293 netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
Cong Wang845e0eb2020-06-08 14:53:01 -07004294 if (lowerdev && lowerdev != dst->remote_dev)
Taehee Yoo0ce18222019-10-21 18:47:57 +00004295 dst->remote_dev = lowerdev;
Petr Machata038a5a92019-01-16 23:06:41 +00004296 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004297 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004298}
4299
stephen hemmingerd3428942012-10-01 12:32:35 +00004300static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4301{
4302 struct vxlan_dev *vxlan = netdev_priv(dev);
4303
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08004304 vxlan_flush(vxlan, true);
4305
stephen hemminger553675f2013-05-16 11:35:20 +00004306 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00004307 unregister_netdevice_queue(dev, head);
Taehee Yoo0ce18222019-10-21 18:47:57 +00004308 if (vxlan->default_dst.remote_dev)
4309 netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00004310}
4311
4312static size_t vxlan_get_size(const struct net_device *dev)
4313{
4314
4315 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08004316 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00004317 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08004318 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00004319 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
Hangbin Liu8fd78062018-09-26 10:35:42 +08004320 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
stephen hemmingerd3428942012-10-01 12:32:35 +00004321 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Stefano Briviob4d306972018-11-08 12:19:16 +01004322 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01004323 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00004324 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00004325 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
4326 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
4327 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
4328 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07004329 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00004330 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
4331 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00004332 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07004333 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4334 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4335 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4336 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08004337 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4338 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00004339 0;
4340}
4341
4342static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4343{
4344 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00004345 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00004346 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004347 .low = htons(vxlan->cfg.port_min),
4348 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00004349 };
stephen hemmingerd3428942012-10-01 12:32:35 +00004350
Jiri Benc54bfd872016-02-16 21:58:58 +01004351 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004352 goto nla_put_failure;
4353
Cong Wange4c7ed42013-08-31 13:44:33 +08004354 if (!vxlan_addr_any(&dst->remote_ip)) {
4355 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004356 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4357 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004358 goto nla_put_failure;
4359#if IS_ENABLED(CONFIG_IPV6)
4360 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004361 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4362 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004363 goto nla_put_failure;
4364#endif
4365 }
4366 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004367
Atzm Watanabec7995c42013-04-16 02:50:52 +00004368 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00004369 goto nla_put_failure;
4370
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004371 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4372 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004373 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004374 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004375 goto nla_put_failure;
4376#if IS_ENABLED(CONFIG_IPV6)
4377 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004378 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004379 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004380 goto nla_put_failure;
4381#endif
4382 }
4383 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004384
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004385 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
Hangbin Liu8fd78062018-09-26 10:35:42 +08004386 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4387 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004388 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Stefano Briviob4d306972018-11-08 12:19:16 +01004389 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01004390 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004391 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Niu Xilei98c81472019-12-30 17:52:22 +08004392 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004393 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Niu Xilei98c81472019-12-30 17:52:22 +08004394 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004395 nla_put_u8(skb, IFLA_VXLAN_RSC,
4396 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004397 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Niu Xilei98c81472019-12-30 17:52:22 +08004398 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004399 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Niu Xilei98c81472019-12-30 17:52:22 +08004400 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07004401 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004402 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004403 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4404 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4405 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004406 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Niu Xilei98c81472019-12-30 17:52:22 +08004407 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004408 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Niu Xilei98c81472019-12-30 17:52:22 +08004409 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004410 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Niu Xilei98c81472019-12-30 17:52:22 +08004411 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004412 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Niu Xilei98c81472019-12-30 17:52:22 +08004413 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004414 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Niu Xilei98c81472019-12-30 17:52:22 +08004415 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004416 goto nla_put_failure;
4417
stephen hemminger05f47d62012-10-09 20:35:50 +00004418 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4419 goto nla_put_failure;
4420
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004421 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01004422 nla_put_flag(skb, IFLA_VXLAN_GBP))
4423 goto nla_put_failure;
4424
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004425 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02004426 nla_put_flag(skb, IFLA_VXLAN_GPE))
4427 goto nla_put_failure;
4428
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004429 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08004430 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4431 goto nla_put_failure;
4432
stephen hemmingerd3428942012-10-01 12:32:35 +00004433 return 0;
4434
4435nla_put_failure:
4436 return -EMSGSIZE;
4437}
4438
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004439static struct net *vxlan_get_link_net(const struct net_device *dev)
4440{
4441 struct vxlan_dev *vxlan = netdev_priv(dev);
4442
4443 return vxlan->net;
4444}
4445
stephen hemmingerd3428942012-10-01 12:32:35 +00004446static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4447 .kind = "vxlan",
4448 .maxtype = IFLA_VXLAN_MAX,
4449 .policy = vxlan_policy,
4450 .priv_size = sizeof(struct vxlan_dev),
4451 .setup = vxlan_setup,
4452 .validate = vxlan_validate,
4453 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004454 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00004455 .dellink = vxlan_dellink,
4456 .get_size = vxlan_get_size,
4457 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004458 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004459};
4460
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004461struct net_device *vxlan_dev_create(struct net *net, const char *name,
4462 u8 name_assign_type,
4463 struct vxlan_config *conf)
4464{
4465 struct nlattr *tb[IFLA_MAX + 1];
4466 struct net_device *dev;
4467 int err;
4468
4469 memset(&tb, 0, sizeof(tb));
4470
4471 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08004472 &vxlan_link_ops, tb, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004473 if (IS_ERR(dev))
4474 return dev;
4475
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07004476 err = __vxlan_dev_create(net, dev, conf, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004477 if (err < 0) {
4478 free_netdev(dev);
4479 return ERR_PTR(err);
4480 }
4481
4482 err = rtnl_configure_link(dev, NULL);
4483 if (err < 0) {
4484 LIST_HEAD(list_kill);
4485
4486 vxlan_dellink(dev, &list_kill);
4487 unregister_netdevice_many(&list_kill);
4488 return ERR_PTR(err);
4489 }
4490
4491 return dev;
4492}
4493EXPORT_SYMBOL_GPL(vxlan_dev_create);
4494
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004495static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4496 struct net_device *dev)
4497{
4498 struct vxlan_dev *vxlan, *next;
4499 LIST_HEAD(list_kill);
4500
4501 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4502 struct vxlan_rdst *dst = &vxlan->default_dst;
4503
4504 /* In case we created vxlan device with carrier
4505 * and we loose the carrier due to module unload
4506 * we also need to remove vxlan device. In other
4507 * cases, it's not necessary and remote_ifindex
4508 * is 0 here, so no matches.
4509 */
4510 if (dst->remote_ifindex == dev->ifindex)
4511 vxlan_dellink(vxlan->dev, &list_kill);
4512 }
4513
4514 unregister_netdevice_many(&list_kill);
4515}
4516
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004517static int vxlan_netdevice_event(struct notifier_block *unused,
4518 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004519{
4520 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004521 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004522
Sabrina Dubroca04584952017-07-21 12:49:33 +02004523 if (event == NETDEV_UNREGISTER) {
Jakub Kicinskicc4e3832020-07-09 17:42:46 -07004524 if (!dev->udp_tunnel_nic_info)
4525 vxlan_offload_rx_ports(dev, false);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004526 vxlan_handle_lowerdev_unregister(vn, dev);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004527 } else if (event == NETDEV_REGISTER) {
Jakub Kicinskicc4e3832020-07-09 17:42:46 -07004528 if (!dev->udp_tunnel_nic_info)
4529 vxlan_offload_rx_ports(dev, true);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004530 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4531 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02004532 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004533 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004534
4535 return NOTIFY_DONE;
4536}
4537
4538static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004539 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004540};
4541
Petr Machata0efe1172018-10-17 08:53:26 +00004542static void
4543vxlan_fdb_offloaded_set(struct net_device *dev,
4544 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4545{
4546 struct vxlan_dev *vxlan = netdev_priv(dev);
4547 struct vxlan_rdst *rdst;
4548 struct vxlan_fdb *f;
Litao jiaofe1e0712019-06-06 17:57:58 +08004549 u32 hash_index;
Petr Machata0efe1172018-10-17 08:53:26 +00004550
Litao jiaofe1e0712019-06-06 17:57:58 +08004551 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4552
4553 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata0efe1172018-10-17 08:53:26 +00004554
4555 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4556 if (!f)
4557 goto out;
4558
4559 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4560 fdb_info->remote_port,
4561 fdb_info->remote_vni,
4562 fdb_info->remote_ifindex);
4563 if (!rdst)
4564 goto out;
4565
4566 rdst->offloaded = fdb_info->offloaded;
4567
4568out:
Litao jiaofe1e0712019-06-06 17:57:58 +08004569 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata0efe1172018-10-17 08:53:26 +00004570}
4571
Petr Machata5728ae02018-11-21 08:02:39 +00004572static int
4573vxlan_fdb_external_learn_add(struct net_device *dev,
4574 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4575{
4576 struct vxlan_dev *vxlan = netdev_priv(dev);
Petr Machata4c59b7d2019-01-16 23:06:54 +00004577 struct netlink_ext_ack *extack;
Litao jiaofe1e0712019-06-06 17:57:58 +08004578 u32 hash_index;
Petr Machata5728ae02018-11-21 08:02:39 +00004579 int err;
4580
Litao jiaofe1e0712019-06-06 17:57:58 +08004581 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
Petr Machata4c59b7d2019-01-16 23:06:54 +00004582 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4583
Litao jiaofe1e0712019-06-06 17:57:58 +08004584 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004585 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4586 NUD_REACHABLE,
4587 NLM_F_CREATE | NLM_F_REPLACE,
4588 fdb_info->remote_port,
4589 fdb_info->vni,
4590 fdb_info->remote_vni,
4591 fdb_info->remote_ifindex,
4592 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07004593 0, false, extack);
Litao jiaofe1e0712019-06-06 17:57:58 +08004594 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004595
4596 return err;
4597}
4598
4599static int
4600vxlan_fdb_external_learn_del(struct net_device *dev,
4601 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4602{
4603 struct vxlan_dev *vxlan = netdev_priv(dev);
4604 struct vxlan_fdb *f;
Litao jiaofe1e0712019-06-06 17:57:58 +08004605 u32 hash_index;
Petr Machata5728ae02018-11-21 08:02:39 +00004606 int err = 0;
4607
Litao jiaofe1e0712019-06-06 17:57:58 +08004608 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4609 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004610
4611 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4612 if (!f)
4613 err = -ENOENT;
4614 else if (f->flags & NTF_EXT_LEARNED)
4615 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4616 fdb_info->remote_ip,
4617 fdb_info->remote_port,
4618 fdb_info->vni,
4619 fdb_info->remote_vni,
4620 fdb_info->remote_ifindex,
4621 false);
4622
Litao jiaofe1e0712019-06-06 17:57:58 +08004623 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004624
4625 return err;
4626}
4627
Petr Machata0efe1172018-10-17 08:53:26 +00004628static int vxlan_switchdev_event(struct notifier_block *unused,
4629 unsigned long event, void *ptr)
4630{
4631 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Petr Machata5728ae02018-11-21 08:02:39 +00004632 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4633 int err = 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004634
4635 switch (event) {
4636 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4637 vxlan_fdb_offloaded_set(dev, ptr);
4638 break;
Petr Machata5728ae02018-11-21 08:02:39 +00004639 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4640 fdb_info = ptr;
4641 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4642 if (err) {
4643 err = notifier_from_errno(err);
4644 break;
4645 }
4646 fdb_info->offloaded = true;
4647 vxlan_fdb_offloaded_set(dev, fdb_info);
4648 break;
4649 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4650 fdb_info = ptr;
4651 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4652 if (err) {
4653 err = notifier_from_errno(err);
4654 break;
4655 }
4656 fdb_info->offloaded = false;
4657 vxlan_fdb_offloaded_set(dev, fdb_info);
4658 break;
Petr Machata0efe1172018-10-17 08:53:26 +00004659 }
4660
Petr Machata5728ae02018-11-21 08:02:39 +00004661 return err;
Petr Machata0efe1172018-10-17 08:53:26 +00004662}
4663
4664static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4665 .notifier_call = vxlan_switchdev_event,
4666};
4667
Roopa Prabhu79472fe2020-05-28 22:12:36 -07004668static void vxlan_fdb_nh_flush(struct nexthop *nh)
4669{
4670 struct vxlan_fdb *fdb;
4671 struct vxlan_dev *vxlan;
4672 u32 hash_index;
4673
4674 rcu_read_lock();
4675 list_for_each_entry_rcu(fdb, &nh->fdb_list, nh_list) {
4676 vxlan = rcu_dereference(fdb->vdev);
4677 WARN_ON(!vxlan);
4678 hash_index = fdb_head_index(vxlan, fdb->eth_addr,
4679 vxlan->default_dst.remote_vni);
4680 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4681 if (!hlist_unhashed(&fdb->hlist))
4682 vxlan_fdb_destroy(vxlan, fdb, false, false);
4683 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4684 }
4685 rcu_read_unlock();
4686}
4687
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004688static int vxlan_nexthop_event(struct notifier_block *nb,
4689 unsigned long event, void *ptr)
4690{
4691 struct nexthop *nh = ptr;
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004692
4693 if (!nh || event != NEXTHOP_EVENT_DEL)
4694 return NOTIFY_DONE;
4695
Roopa Prabhu79472fe2020-05-28 22:12:36 -07004696 vxlan_fdb_nh_flush(nh);
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004697
4698 return NOTIFY_DONE;
4699}
4700
4701static struct notifier_block vxlan_nexthop_notifier_block __read_mostly = {
4702 .notifier_call = vxlan_nexthop_event,
4703};
4704
stephen hemmingerd3428942012-10-01 12:32:35 +00004705static __net_init int vxlan_init_net(struct net *net)
4706{
4707 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00004708 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00004709
stephen hemminger553675f2013-05-16 11:35:20 +00004710 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07004711 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00004712
stephen hemminger553675f2013-05-16 11:35:20 +00004713 for (h = 0; h < PORT_HASH_SIZE; ++h)
4714 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00004715
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004716 return register_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004717}
4718
Haishuang Yan57b61122017-12-16 17:54:49 +08004719static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004720{
4721 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4722 struct vxlan_dev *vxlan, *next;
4723 struct net_device *dev, *aux;
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004724 unsigned int h;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004725
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004726 for_each_netdev_safe(net, dev, aux)
4727 if (dev->rtnl_link_ops == &vxlan_link_ops)
Haishuang Yan57b61122017-12-16 17:54:49 +08004728 unregister_netdevice_queue(dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004729
4730 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4731 /* If vxlan->dev is in the same netns, it has already been added
4732 * to the list by the previous loop.
4733 */
Zhiqiang Liucc4807b2019-03-16 17:02:54 +08004734 if (!net_eq(dev_net(vxlan->dev), net))
Haishuang Yan57b61122017-12-16 17:54:49 +08004735 unregister_netdevice_queue(vxlan->dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004736 }
4737
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004738 for (h = 0; h < PORT_HASH_SIZE; ++h)
4739 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004740}
4741
Haishuang Yan57b61122017-12-16 17:54:49 +08004742static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4743{
4744 struct net *net;
4745 LIST_HEAD(list);
4746
4747 rtnl_lock();
4748 list_for_each_entry(net, net_list, exit_list)
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004749 unregister_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
4750 list_for_each_entry(net, net_list, exit_list)
Haishuang Yan57b61122017-12-16 17:54:49 +08004751 vxlan_destroy_tunnels(net, &list);
4752
4753 unregister_netdevice_many(&list);
4754 rtnl_unlock();
4755}
4756
stephen hemmingerd3428942012-10-01 12:32:35 +00004757static struct pernet_operations vxlan_net_ops = {
4758 .init = vxlan_init_net,
Haishuang Yan57b61122017-12-16 17:54:49 +08004759 .exit_batch = vxlan_exit_batch_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004760 .id = &vxlan_net_id,
4761 .size = sizeof(struct vxlan_net),
4762};
4763
4764static int __init vxlan_init_module(void)
4765{
4766 int rc;
4767
4768 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4769
Daniel Borkmann783c1462014-01-22 21:07:53 +01004770 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004771 if (rc)
4772 goto out1;
4773
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004774 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004775 if (rc)
4776 goto out2;
4777
Petr Machata0efe1172018-10-17 08:53:26 +00004778 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004779 if (rc)
4780 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00004781
Petr Machata0efe1172018-10-17 08:53:26 +00004782 rc = rtnl_link_register(&vxlan_link_ops);
4783 if (rc)
4784 goto out4;
4785
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004786 return 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004787out4:
4788 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004789out3:
4790 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004791out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01004792 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004793out1:
4794 return rc;
4795}
Cong Wang7332a132013-05-27 22:35:53 +00004796late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00004797
4798static void __exit vxlan_cleanup_module(void)
4799{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07004800 rtnl_link_unregister(&vxlan_link_ops);
Petr Machata0efe1172018-10-17 08:53:26 +00004801 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004802 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004803 unregister_pernet_subsys(&vxlan_net_ops);
4804 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00004805}
4806module_exit(vxlan_cleanup_module);
4807
4808MODULE_LICENSE("GPL");
4809MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004810MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08004811MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00004812MODULE_ALIAS_RTNL_LINK("vxlan");