blob: 48fbdce6a70e7667040935a6567b7be8ac92e83e [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
Fabian Frederick78ec710e2020-09-25 15:17:17 +0200193/* Find VXLAN socket based on network namespace, address family, UDP port,
194 * enabled unshareable flags and socket device binding (see l3mdev with
195 * non-default VRF).
Thomas Grafac5132d2015-01-15 03:53:56 +0100196 */
197static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100198 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000199{
200 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800201
202 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000203
204 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200205 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200206 vxlan_get_sk_family(vs) == family &&
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100207 vs->flags == flags &&
208 vs->sock->sk->sk_bound_dev_if == ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000209 return vs;
210 }
211 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000212}
213
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200214static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
215 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000216{
Jiri Benc69e76662017-07-02 19:00:57 +0200217 struct vxlan_dev_node *node;
stephen hemmingerd3428942012-10-01 12:32:35 +0000218
Jiri Bencb9167b22016-02-16 21:59:03 +0100219 /* For flow based devices, map all packets to VNI 0 */
220 if (vs->flags & VXLAN_F_COLLECT_METADATA)
221 vni = 0;
222
Jiri Benc69e76662017-07-02 19:00:57 +0200223 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
224 if (node->vxlan->default_dst.remote_vni != vni)
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200225 continue;
226
227 if (IS_ENABLED(CONFIG_IPV6)) {
Jiri Benc69e76662017-07-02 19:00:57 +0200228 const struct vxlan_config *cfg = &node->vxlan->cfg;
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200229
230 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
231 cfg->remote_ifindex != ifindex)
232 continue;
233 }
234
Jiri Benc69e76662017-07-02 19:00:57 +0200235 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000236 }
237
238 return NULL;
239}
240
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700241/* Look up VNI in a per net namespace table */
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200242static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
243 __be32 vni, sa_family_t family,
244 __be16 port, u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700245{
246 struct vxlan_sock *vs;
247
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100248 vs = vxlan_find_sock(net, family, port, flags, ifindex);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700249 if (!vs)
250 return NULL;
251
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200252 return vxlan_vs_find_vni(vs, ifindex, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700253}
254
stephen hemmingerd3428942012-10-01 12:32:35 +0000255/* Fill in neighbour message in skbuff. */
256static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700257 const struct vxlan_fdb *fdb,
258 u32 portid, u32 seq, int type, unsigned int flags,
259 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000260{
261 unsigned long now = jiffies;
262 struct nda_cacheinfo ci;
David Stevense4f67ad2012-11-20 02:50:14 +0000263 bool send_ip, send_eth;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700264 struct nlmsghdr *nlh;
265 struct nexthop *nh;
266 struct ndmsg *ndm;
Ido Schimmel06ec3132020-05-25 00:38:56 +0300267 int nh_family;
268 u32 nh_id;
stephen hemmingerd3428942012-10-01 12:32:35 +0000269
270 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
271 if (nlh == NULL)
272 return -EMSGSIZE;
273
274 ndm = nlmsg_data(nlh);
275 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000276
277 send_eth = send_ip = true;
278
Ido Schimmel06ec3132020-05-25 00:38:56 +0300279 rcu_read_lock();
280 nh = rcu_dereference(fdb->nh);
281 if (nh) {
282 nh_family = nexthop_get_family(nh);
283 nh_id = nh->id;
284 }
285 rcu_read_unlock();
286
David Stevense4f67ad2012-11-20 02:50:14 +0000287 if (type == RTM_GETNEIGH) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700288 if (rdst) {
289 send_ip = !vxlan_addr_any(&rdst->remote_ip);
290 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
291 } else if (nh) {
Ido Schimmel06ec3132020-05-25 00:38:56 +0300292 ndm->ndm_family = nh_family;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700293 }
David Stevense4f67ad2012-11-20 02:50:14 +0000294 send_eth = !is_zero_ether_addr(fdb->eth_addr);
295 } else
296 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000297 ndm->ndm_state = fdb->state;
298 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000299 ndm->ndm_flags = fdb->flags;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700300 if (rdst && rdst->offloaded)
Petr Machata0efe1172018-10-17 08:53:26 +0000301 ndm->ndm_flags |= NTF_OFFLOADED;
Jun Zhao545469f2014-07-26 00:38:59 +0800302 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000303
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100304 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100305 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700306 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100307 goto nla_put_failure;
308
David Stevense4f67ad2012-11-20 02:50:14 +0000309 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000310 goto nla_put_failure;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700311 if (nh) {
Ido Schimmel06ec3132020-05-25 00:38:56 +0300312 if (nla_put_u32(skb, NDA_NH_ID, nh_id))
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700313 goto nla_put_failure;
314 } else if (rdst) {
315 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST,
316 &rdst->remote_ip))
317 goto nla_put_failure;
stephen hemmingerd3428942012-10-01 12:32:35 +0000318
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700319 if (rdst->remote_port &&
320 rdst->remote_port != vxlan->cfg.dst_port &&
321 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
322 goto nla_put_failure;
323 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
324 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
325 goto nla_put_failure;
326 if (rdst->remote_ifindex &&
327 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
328 goto nla_put_failure;
329 }
David Stevens66817122013-03-15 04:35:51 +0000330
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200331 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800332 nla_put_u32(skb, NDA_SRC_VNI,
333 be32_to_cpu(fdb->vni)))
334 goto nla_put_failure;
stephen hemmingerd3428942012-10-01 12:32:35 +0000335
336 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
337 ci.ndm_confirmed = 0;
338 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
339 ci.ndm_refcnt = 0;
340
341 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
342 goto nla_put_failure;
343
Johannes Berg053c0952015-01-16 22:09:00 +0100344 nlmsg_end(skb, nlh);
345 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000346
347nla_put_failure:
348 nlmsg_cancel(skb, nlh);
349 return -EMSGSIZE;
350}
351
352static inline size_t vxlan_nlmsg_size(void)
353{
354 return NLMSG_ALIGN(sizeof(struct ndmsg))
355 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800356 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000357 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000358 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
359 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100360 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000361 + nla_total_size(sizeof(struct nda_cacheinfo));
362}
363
Petr Machata9a997352018-10-17 08:53:22 +0000364static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
365 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000366{
367 struct net *net = dev_net(vxlan->dev);
368 struct sk_buff *skb;
369 int err = -ENOBUFS;
370
371 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
372 if (skb == NULL)
373 goto errout;
374
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200375 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000376 if (err < 0) {
377 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
378 WARN_ON(err == -EMSGSIZE);
379 kfree_skb(skb);
380 goto errout;
381 }
382
383 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
384 return;
385errout:
386 if (err < 0)
387 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
388}
389
Petr Machataff23b912018-12-07 19:55:03 +0000390static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
391 const struct vxlan_fdb *fdb,
392 const struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000393 struct netlink_ext_ack *extack,
Petr Machataff23b912018-12-07 19:55:03 +0000394 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
395{
396 fdb_info->info.dev = vxlan->dev;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000397 fdb_info->info.extack = extack;
Petr Machataff23b912018-12-07 19:55:03 +0000398 fdb_info->remote_ip = rd->remote_ip;
399 fdb_info->remote_port = rd->remote_port;
400 fdb_info->remote_vni = rd->remote_vni;
401 fdb_info->remote_ifindex = rd->remote_ifindex;
402 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
403 fdb_info->vni = fdb->vni;
404 fdb_info->offloaded = rd->offloaded;
405 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
406}
407
Petr Machata61f46fe2019-01-16 23:06:38 +0000408static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
409 struct vxlan_fdb *fdb,
410 struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000411 bool adding,
412 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000413{
414 struct switchdev_notifier_vxlan_fdb_info info;
415 enum switchdev_notifier_type notifier_type;
Petr Machata61f46fe2019-01-16 23:06:38 +0000416 int ret;
Petr Machata9a997352018-10-17 08:53:22 +0000417
418 if (WARN_ON(!rd))
Petr Machata61f46fe2019-01-16 23:06:38 +0000419 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000420
421 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
422 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000423 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
Petr Machata61f46fe2019-01-16 23:06:38 +0000424 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
Petr Machata66859872019-01-16 23:06:56 +0000425 &info.info, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000426 return notifier_to_errno(ret);
Petr Machata9a997352018-10-17 08:53:22 +0000427}
428
Petr Machata61f46fe2019-01-16 23:06:38 +0000429static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000430 struct vxlan_rdst *rd, int type, bool swdev_notify,
431 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000432{
Petr Machata61f46fe2019-01-16 23:06:38 +0000433 int err;
434
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700435 if (swdev_notify && rd) {
Petr Machata0e6160f2018-11-21 08:02:35 +0000436 switch (type) {
437 case RTM_NEWNEIGH:
Petr Machata61f46fe2019-01-16 23:06:38 +0000438 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000439 true, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000440 if (err)
441 return err;
Petr Machata0e6160f2018-11-21 08:02:35 +0000442 break;
443 case RTM_DELNEIGH:
444 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000445 false, extack);
Petr Machata0e6160f2018-11-21 08:02:35 +0000446 break;
447 }
Petr Machata9a997352018-10-17 08:53:22 +0000448 }
449
450 __vxlan_fdb_notify(vxlan, fdb, rd, type);
Petr Machata61f46fe2019-01-16 23:06:38 +0000451 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000452}
453
Cong Wange4c7ed42013-08-31 13:44:33 +0800454static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000455{
456 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700457 struct vxlan_fdb f = {
458 .state = NUD_STALE,
459 };
460 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800461 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100462 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700463 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700464
Petr Machata4c59b7d2019-01-16 23:06:54 +0000465 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000466}
467
468static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
469{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700470 struct vxlan_fdb f = {
471 .state = NUD_STALE,
472 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200473 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000474
David Stevense4f67ad2012-11-20 02:50:14 +0000475 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
476
Petr Machata4c59b7d2019-01-16 23:06:54 +0000477 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000478}
479
stephen hemmingerd3428942012-10-01 12:32:35 +0000480/* Hash Ethernet address */
481static u32 eth_hash(const unsigned char *addr)
482{
483 u64 value = get_unaligned((u64 *)addr);
484
485 /* only want 6 bytes */
486#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000487 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000488#else
489 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000490#endif
491 return hash_64(value, FDB_HASH_BITS);
492}
493
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800494static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
495{
496 /* use 1 byte of OUI and 3 bytes of NIC */
497 u32 key = get_unaligned((u32 *)(addr + 2));
498
499 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
500}
501
Litao jiaofe1e0712019-06-06 17:57:58 +0800502static u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
503{
504 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
505 return eth_vni_hash(mac, vni);
506 else
507 return eth_hash(mac);
508}
509
stephen hemmingerd3428942012-10-01 12:32:35 +0000510/* Hash chain to use given mac address */
511static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800512 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000513{
Litao jiaofe1e0712019-06-06 17:57:58 +0800514 return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000515}
516
517/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000518static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800519 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000520{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800521 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000522 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000523
Sasha Levinb67bfe02013-02-27 17:06:00 -0800524 hlist_for_each_entry_rcu(f, head, hlist) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800525 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200526 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800527 if (vni == f->vni)
528 return f;
529 } else {
530 return f;
531 }
532 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000533 }
534
535 return NULL;
536}
537
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000538static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800539 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000540{
541 struct vxlan_fdb *f;
542
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800543 f = __vxlan_find_mac(vxlan, mac, vni);
Li RongQing016f3d12018-08-29 11:52:10 +0800544 if (f && f->used != jiffies)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000545 f->used = jiffies;
546
547 return f;
548}
549
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300550/* caller should hold vxlan->hash_lock */
551static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800552 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100553 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300554{
555 struct vxlan_rdst *rd;
556
557 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800558 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300559 rd->remote_port == port &&
560 rd->remote_vni == vni &&
561 rd->remote_ifindex == ifindex)
562 return rd;
563 }
564
565 return NULL;
566}
567
Petr Machata1941f1d2018-10-17 08:53:24 +0000568int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
569 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
570{
571 struct vxlan_dev *vxlan = netdev_priv(dev);
572 u8 eth_addr[ETH_ALEN + 2] = { 0 };
573 struct vxlan_rdst *rdst;
574 struct vxlan_fdb *f;
575 int rc = 0;
576
577 if (is_multicast_ether_addr(mac) ||
578 is_zero_ether_addr(mac))
579 return -EINVAL;
580
581 ether_addr_copy(eth_addr, mac);
582
583 rcu_read_lock();
584
585 f = __vxlan_find_mac(vxlan, eth_addr, vni);
586 if (!f) {
587 rc = -ENOENT;
588 goto out;
589 }
590
591 rdst = first_remote_rcu(f);
Petr Machata4c59b7d2019-01-16 23:06:54 +0000592 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
Petr Machata1941f1d2018-10-17 08:53:24 +0000593
594out:
595 rcu_read_unlock();
596 return rc;
597}
598EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
599
Petr Machata4f89f5b2018-12-07 19:55:04 +0000600static int vxlan_fdb_notify_one(struct notifier_block *nb,
601 const struct vxlan_dev *vxlan,
602 const struct vxlan_fdb *f,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000603 const struct vxlan_rdst *rdst,
604 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000605{
606 struct switchdev_notifier_vxlan_fdb_info fdb_info;
607 int rc;
608
Petr Machata4c59b7d2019-01-16 23:06:54 +0000609 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000610 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
611 &fdb_info);
612 return notifier_to_errno(rc);
613}
614
615int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000616 struct notifier_block *nb,
617 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000618{
619 struct vxlan_dev *vxlan;
620 struct vxlan_rdst *rdst;
621 struct vxlan_fdb *f;
622 unsigned int h;
623 int rc = 0;
624
625 if (!netif_is_vxlan(dev))
626 return -EINVAL;
627 vxlan = netdev_priv(dev);
628
Petr Machata4f89f5b2018-12-07 19:55:04 +0000629 for (h = 0; h < FDB_HASH_SIZE; ++h) {
Litao jiaofe1e0712019-06-06 17:57:58 +0800630 spin_lock_bh(&vxlan->hash_lock[h]);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000631 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
632 if (f->vni == vni) {
633 list_for_each_entry(rdst, &f->remotes, list) {
634 rc = vxlan_fdb_notify_one(nb, vxlan,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000635 f, rdst,
636 extack);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000637 if (rc)
Litao jiaofe1e0712019-06-06 17:57:58 +0800638 goto unlock;
Petr Machata4f89f5b2018-12-07 19:55:04 +0000639 }
640 }
641 }
Litao jiaofe1e0712019-06-06 17:57:58 +0800642 spin_unlock_bh(&vxlan->hash_lock[h]);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000643 }
Litao jiaofe1e0712019-06-06 17:57:58 +0800644 return 0;
Petr Machata4f89f5b2018-12-07 19:55:04 +0000645
Litao jiaofe1e0712019-06-06 17:57:58 +0800646unlock:
647 spin_unlock_bh(&vxlan->hash_lock[h]);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000648 return rc;
649}
650EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
651
Petr Machatae5ff4b12018-12-07 19:55:06 +0000652void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
653{
654 struct vxlan_dev *vxlan;
655 struct vxlan_rdst *rdst;
656 struct vxlan_fdb *f;
657 unsigned int h;
658
659 if (!netif_is_vxlan(dev))
660 return;
661 vxlan = netdev_priv(dev);
662
Petr Machatae5ff4b12018-12-07 19:55:06 +0000663 for (h = 0; h < FDB_HASH_SIZE; ++h) {
Litao jiaofe1e0712019-06-06 17:57:58 +0800664 spin_lock_bh(&vxlan->hash_lock[h]);
Petr Machatae5ff4b12018-12-07 19:55:06 +0000665 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
666 if (f->vni == vni)
667 list_for_each_entry(rdst, &f->remotes, list)
668 rdst->offloaded = false;
Litao jiaofe1e0712019-06-06 17:57:58 +0800669 spin_unlock_bh(&vxlan->hash_lock[h]);
Petr Machatae5ff4b12018-12-07 19:55:06 +0000670 }
Litao jiaofe1e0712019-06-06 17:57:58 +0800671
Petr Machatae5ff4b12018-12-07 19:55:06 +0000672}
673EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
674
Thomas Richter906dc182013-07-19 17:20:07 +0200675/* Replace destination of unicast mac */
676static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100677 union vxlan_addr *ip, __be16 port, __be32 vni,
Petr Machataccdfd4f2019-01-16 23:06:34 +0000678 __u32 ifindex, struct vxlan_rdst *oldrd)
Thomas Richter906dc182013-07-19 17:20:07 +0200679{
680 struct vxlan_rdst *rd;
681
682 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
683 if (rd)
684 return 0;
685
686 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
687 if (!rd)
688 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100689
Petr Machataccdfd4f2019-01-16 23:06:34 +0000690 *oldrd = *rd;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100691 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800692 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200693 rd->remote_port = port;
694 rd->remote_vni = vni;
695 rd->remote_ifindex = ifindex;
Petr Machata6ad0b5a2018-12-18 13:15:59 +0000696 rd->offloaded = false;
Thomas Richter906dc182013-07-19 17:20:07 +0200697 return 1;
698}
699
David Stevens66817122013-03-15 04:35:51 +0000700/* Add/update destinations for multicast */
701static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100702 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200703 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000704{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700705 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000706
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300707 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
708 if (rd)
709 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700710
David Stevens66817122013-03-15 04:35:51 +0000711 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
712 if (rd == NULL)
713 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100714
715 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
716 kfree(rd);
717 return -ENOBUFS;
718 }
719
Cong Wange4c7ed42013-08-31 13:44:33 +0800720 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000721 rd->remote_port = port;
Petr Machata0efe1172018-10-17 08:53:26 +0000722 rd->offloaded = false;
David Stevens66817122013-03-15 04:35:51 +0000723 rd->remote_vni = vni;
724 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700725
726 list_add_tail_rcu(&rd->list, &f->remotes);
727
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200728 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000729 return 1;
730}
731
Tom Herbertdfd86452015-01-12 17:00:38 -0800732static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
733 unsigned int off,
734 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100735 __be32 vni_field,
736 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800737 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800738{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700739 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800740
741 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700742 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800743
744 if (!NAPI_GRO_CB(skb)->csum_valid)
745 return NULL;
746
Jiri Benc54bfd872016-02-16 21:58:58 +0100747 start = vxlan_rco_start(vni_field);
748 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800749
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700750 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
751 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800752
753 skb->remcsum_offload = 1;
754
755 return vh;
756}
757
David Millerd4546c22018-06-24 14:13:49 +0900758static struct sk_buff *vxlan_gro_receive(struct sock *sk,
759 struct list_head *head,
760 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200761{
David Millerd4546c22018-06-24 14:13:49 +0900762 struct sk_buff *pp = NULL;
763 struct sk_buff *p;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200764 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800765 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200766 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700767 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100768 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800769 struct gro_remcsum grc;
770
771 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200772
773 off_vx = skb_gro_offset(skb);
774 hlen = off_vx + sizeof(*vh);
775 vh = skb_gro_header_fast(skb, off_vx);
776 if (skb_gro_header_hard(skb, hlen)) {
777 vh = skb_gro_header_slow(skb, hlen, off_vx);
778 if (unlikely(!vh))
779 goto out;
780 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200781
Tom Herbertdfd86452015-01-12 17:00:38 -0800782 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
783
Jiri Benc54bfd872016-02-16 21:58:58 +0100784 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800785
786 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
787 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100788 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800789 !!(vs->flags &
790 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800791
792 if (!vh)
793 goto out;
794 }
795
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700796 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
797
David Millerd4546c22018-06-24 14:13:49 +0900798 list_for_each_entry(p, head, list) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200799 if (!NAPI_GRO_CB(p)->same_flow)
800 continue;
801
802 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100803 if (vh->vx_flags != vh2->vx_flags ||
804 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200805 NAPI_GRO_CB(p)->same_flow = 0;
806 continue;
807 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200808 }
809
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200810 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800811 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200812
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200813out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200814 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200815
816 return pp;
817}
818
Tom Herbert5602c482016-04-05 08:22:53 -0700819static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200820{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700821 /* Sets 'skb->inner_mac_header' since we are always called with
822 * 'skb->encapsulation' set.
823 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800824 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200825}
826
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -0700827static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, const u8 *mac,
828 __u16 state, __be32 src_vni,
829 __u16 ndm_flags)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700830{
831 struct vxlan_fdb *f;
832
833 f = kmalloc(sizeof(*f), GFP_ATOMIC);
834 if (!f)
835 return NULL;
836 f->state = state;
837 f->flags = ndm_flags;
838 f->updated = f->used = jiffies;
839 f->vni = src_vni;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700840 f->nh = NULL;
Roopa Prabhu79472fe2020-05-28 22:12:36 -0700841 RCU_INIT_POINTER(f->vdev, vxlan);
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700842 INIT_LIST_HEAD(&f->nh_list);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700843 INIT_LIST_HEAD(&f->remotes);
844 memcpy(f->eth_addr, mac, ETH_ALEN);
845
846 return f;
847}
848
Taehee Yoo7c31e542019-06-28 14:07:25 +0900849static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
850 __be32 src_vni, struct vxlan_fdb *f)
851{
852 ++vxlan->addrcnt;
853 hlist_add_head_rcu(&f->hlist,
854 vxlan_fdb_head(vxlan, mac, src_vni));
855}
856
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700857static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
858 u32 nhid, struct netlink_ext_ack *extack)
859{
860 struct nexthop *old_nh = rtnl_dereference(fdb->nh);
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700861 struct nexthop *nh;
862 int err = -EINVAL;
863
864 if (old_nh && old_nh->id == nhid)
865 return 0;
866
867 nh = nexthop_find_by_id(vxlan->net, nhid);
868 if (!nh) {
869 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
870 goto err_inval;
871 }
872
873 if (nh) {
874 if (!nexthop_get(nh)) {
875 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
876 nh = NULL;
877 goto err_inval;
878 }
David Ahernce9ac052020-06-08 20:54:43 -0600879 if (!nexthop_is_fdb(nh)) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700880 NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop");
881 goto err_inval;
882 }
883
David Ahern50cb8762020-06-09 17:27:28 -0600884 if (!nexthop_is_multipath(nh)) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700885 NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group");
886 goto err_inval;
887 }
888
889 /* check nexthop group family */
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700890 switch (vxlan->default_dst.remote_ip.sa.sa_family) {
891 case AF_INET:
David Ahern50cb8762020-06-09 17:27:28 -0600892 if (!nexthop_has_v4(nh)) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700893 err = -EAFNOSUPPORT;
894 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
895 goto err_inval;
896 }
897 break;
898 case AF_INET6:
David Ahern50cb8762020-06-09 17:27:28 -0600899 if (nexthop_has_v4(nh)) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700900 err = -EAFNOSUPPORT;
901 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
902 goto err_inval;
903 }
904 }
905 }
906
907 if (old_nh) {
908 list_del_rcu(&fdb->nh_list);
909 nexthop_put(old_nh);
910 }
911 rcu_assign_pointer(fdb->nh, nh);
912 list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list);
913 return 1;
914
915err_inval:
916 if (nh)
917 nexthop_put(nh);
918 return err;
919}
920
stephen hemmingerd3428942012-10-01 12:32:35 +0000921static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800922 const u8 *mac, union vxlan_addr *ip,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700923 __u16 state, __be16 port, __be32 src_vni,
Petr Machata45598c12018-11-21 08:02:36 +0000924 __be32 vni, __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700925 u32 nhid, struct vxlan_fdb **fdb,
926 struct netlink_ext_ack *extack)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700927{
928 struct vxlan_rdst *rd = NULL;
929 struct vxlan_fdb *f;
930 int rc;
931
932 if (vxlan->cfg.addrmax &&
933 vxlan->addrcnt >= vxlan->cfg.addrmax)
934 return -ENOSPC;
935
936 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -0700937 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700938 if (!f)
939 return -ENOMEM;
940
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700941 if (nhid)
942 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
943 else
944 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
945 if (rc < 0)
946 goto errout;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700947
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700948 *fdb = f;
949
950 return 0;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700951
952errout:
953 kfree(f);
954 return rc;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700955}
956
Taehee Yoo7c31e542019-06-28 14:07:25 +0900957static void __vxlan_fdb_free(struct vxlan_fdb *f)
Petr Machatac2b200e2019-01-16 23:06:30 +0000958{
Petr Machatac2b200e2019-01-16 23:06:30 +0000959 struct vxlan_rdst *rd, *nd;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700960 struct nexthop *nh;
961
962 nh = rcu_dereference_raw(f->nh);
963 if (nh) {
964 rcu_assign_pointer(f->nh, NULL);
Roopa Prabhu79472fe2020-05-28 22:12:36 -0700965 rcu_assign_pointer(f->vdev, NULL);
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700966 nexthop_put(nh);
967 }
Petr Machatac2b200e2019-01-16 23:06:30 +0000968
969 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
970 dst_cache_destroy(&rd->dst_cache);
971 kfree(rd);
972 }
973 kfree(f);
974}
975
Taehee Yoo7c31e542019-06-28 14:07:25 +0900976static void vxlan_fdb_free(struct rcu_head *head)
977{
978 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
979
980 __vxlan_fdb_free(f);
981}
982
Petr Machatac2b200e2019-01-16 23:06:30 +0000983static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
984 bool do_notify, bool swdev_notify)
985{
986 struct vxlan_rdst *rd;
987
988 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
989
990 --vxlan->addrcnt;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700991 if (do_notify) {
992 if (rcu_access_pointer(f->nh))
993 vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000994 swdev_notify, NULL);
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700995 else
996 list_for_each_entry(rd, &f->remotes, list)
997 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
998 swdev_notify, NULL);
999 }
Petr Machatac2b200e2019-01-16 23:06:30 +00001000
1001 hlist_del_rcu(&f->hlist);
Roopa Prabhu79472fe2020-05-28 22:12:36 -07001002 list_del_rcu(&f->nh_list);
Petr Machatac2b200e2019-01-16 23:06:30 +00001003 call_rcu(&f->rcu, vxlan_fdb_free);
1004}
1005
Petr Machatafc4aa1c2019-02-07 12:18:02 +00001006static void vxlan_dst_free(struct rcu_head *head)
1007{
1008 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
1009
1010 dst_cache_destroy(&rd->dst_cache);
1011 kfree(rd);
1012}
1013
Petr Machataa76d1ca2019-01-16 23:06:32 +00001014static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
1015 union vxlan_addr *ip,
1016 __u16 state, __u16 flags,
1017 __be16 port, __be32 vni,
1018 __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001019 struct vxlan_fdb *f, u32 nhid,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001020 bool swdev_notify,
1021 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +00001022{
1023 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1024 struct vxlan_rdst *rd = NULL;
Petr Machataccdfd4f2019-01-16 23:06:34 +00001025 struct vxlan_rdst oldrd;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001026 int notify = 0;
Petr Machata61f46fe2019-01-16 23:06:38 +00001027 int rc = 0;
1028 int err;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001029
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001030 if (nhid && !rcu_access_pointer(f->nh)) {
1031 NL_SET_ERR_MSG(extack,
1032 "Cannot replace an existing non nexthop fdb with a nexthop");
1033 return -EOPNOTSUPP;
1034 }
1035
1036 if (nhid && (flags & NLM_F_APPEND)) {
1037 NL_SET_ERR_MSG(extack,
1038 "Cannot append to a nexthop fdb");
1039 return -EOPNOTSUPP;
1040 }
1041
Petr Machataa76d1ca2019-01-16 23:06:32 +00001042 /* Do not allow an externally learned entry to take over an entry added
1043 * by the user.
1044 */
1045 if (!(fdb_flags & NTF_EXT_LEARNED) ||
1046 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
1047 if (f->state != state) {
1048 f->state = state;
1049 f->updated = jiffies;
1050 notify = 1;
1051 }
1052 if (f->flags != fdb_flags) {
1053 f->flags = fdb_flags;
1054 f->updated = jiffies;
1055 notify = 1;
1056 }
1057 }
1058
1059 if ((flags & NLM_F_REPLACE)) {
1060 /* Only change unicasts */
1061 if (!(is_multicast_ether_addr(f->eth_addr) ||
1062 is_zero_ether_addr(f->eth_addr))) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001063 if (nhid) {
1064 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
1065 if (rc < 0)
1066 return rc;
1067 } else {
1068 rc = vxlan_fdb_replace(f, ip, port, vni,
1069 ifindex, &oldrd);
1070 }
Petr Machataa76d1ca2019-01-16 23:06:32 +00001071 notify |= rc;
1072 } else {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001073 NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries");
Petr Machataa76d1ca2019-01-16 23:06:32 +00001074 return -EOPNOTSUPP;
1075 }
1076 }
1077 if ((flags & NLM_F_APPEND) &&
1078 (is_multicast_ether_addr(f->eth_addr) ||
1079 is_zero_ether_addr(f->eth_addr))) {
1080 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
1081
1082 if (rc < 0)
1083 return rc;
1084 notify |= rc;
1085 }
1086
1087 if (ndm_flags & NTF_USE)
1088 f->used = jiffies;
1089
1090 if (notify) {
1091 if (rd == NULL)
1092 rd = first_remote_rtnl(f);
1093
Petr Machata61f46fe2019-01-16 23:06:38 +00001094 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001095 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +00001096 if (err)
1097 goto err_notify;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001098 }
1099
1100 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +00001101
1102err_notify:
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001103 if (nhid)
1104 return err;
Petr Machata61f46fe2019-01-16 23:06:38 +00001105 if ((flags & NLM_F_REPLACE) && rc)
1106 *rd = oldrd;
Petr Machatafc4aa1c2019-02-07 12:18:02 +00001107 else if ((flags & NLM_F_APPEND) && rc) {
Petr Machata61f46fe2019-01-16 23:06:38 +00001108 list_del_rcu(&rd->list);
Petr Machatafc4aa1c2019-02-07 12:18:02 +00001109 call_rcu(&rd->rcu, vxlan_dst_free);
1110 }
Petr Machata61f46fe2019-01-16 23:06:38 +00001111 return err;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001112}
1113
1114static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
1115 const u8 *mac, union vxlan_addr *ip,
1116 __u16 state, __u16 flags,
1117 __be16 port, __be32 src_vni, __be32 vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001118 __u32 ifindex, __u16 ndm_flags, u32 nhid,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001119 bool swdev_notify,
1120 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +00001121{
1122 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1123 struct vxlan_fdb *f;
1124 int rc;
1125
1126 /* Disallow replace to add a multicast entry */
1127 if ((flags & NLM_F_REPLACE) &&
1128 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
1129 return -EOPNOTSUPP;
1130
1131 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
1132 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001133 vni, ifindex, fdb_flags, nhid, &f, extack);
Petr Machataa76d1ca2019-01-16 23:06:32 +00001134 if (rc < 0)
1135 return rc;
1136
Taehee Yoo7c31e542019-06-28 14:07:25 +09001137 vxlan_fdb_insert(vxlan, mac, src_vni, f);
Petr Machata61f46fe2019-01-16 23:06:38 +00001138 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001139 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +00001140 if (rc)
1141 goto err_notify;
1142
Petr Machataa76d1ca2019-01-16 23:06:32 +00001143 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +00001144
1145err_notify:
1146 vxlan_fdb_destroy(vxlan, f, false, false);
1147 return rc;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001148}
1149
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001150/* Add new entry to forwarding table -- assumes lock held */
1151static int vxlan_fdb_update(struct vxlan_dev *vxlan,
1152 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +00001153 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001154 __be16 port, __be32 src_vni, __be32 vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001155 __u32 ifindex, __u16 ndm_flags, u32 nhid,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001156 bool swdev_notify,
1157 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001158{
1159 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001160
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001161 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001162 if (f) {
1163 if (flags & NLM_F_EXCL) {
1164 netdev_dbg(vxlan->dev,
1165 "lost race to create %pM\n", mac);
1166 return -EEXIST;
1167 }
Petr Machata0ec566a2018-11-21 08:02:37 +00001168
Petr Machataa76d1ca2019-01-16 23:06:32 +00001169 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1170 vni, ifindex, ndm_flags, f,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001171 nhid, swdev_notify, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001172 } else {
1173 if (!(flags & NLM_F_CREATE))
1174 return -ENOENT;
1175
Petr Machataa76d1ca2019-01-16 23:06:32 +00001176 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1177 port, src_vni, vni, ifindex,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001178 ndm_flags, nhid, swdev_notify,
1179 extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001180 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001181}
1182
Lance Richardson35cf2842017-05-29 13:25:57 -04001183static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +00001184 struct vxlan_rdst *rd, bool swdev_notify)
Lance Richardson35cf2842017-05-29 13:25:57 -04001185{
1186 list_del_rcu(&rd->list);
Petr Machata4c59b7d2019-01-16 23:06:54 +00001187 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
Lance Richardson35cf2842017-05-29 13:25:57 -04001188 call_rcu(&rd->rcu, vxlan_dst_free);
1189}
1190
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001191static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001192 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001193 __be32 *vni, u32 *ifindex, u32 *nhid)
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001194{
1195 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001196 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001197
Roopa Prabhu72b48682020-05-28 22:12:35 -07001198 if (tb[NDA_NH_ID] && (tb[NDA_DST] || tb[NDA_VNI] || tb[NDA_IFINDEX] ||
1199 tb[NDA_PORT]))
1200 return -EINVAL;
1201
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001202 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001203 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1204 if (err)
1205 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001206 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08001207 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
Niu Xilei98c81472019-12-30 17:52:22 +08001208
Cong Wange4c7ed42013-08-31 13:44:33 +08001209 if (remote->sa.sa_family == AF_INET) {
1210 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1211 ip->sa.sa_family = AF_INET;
1212#if IS_ENABLED(CONFIG_IPV6)
1213 } else {
1214 ip->sin6.sin6_addr = in6addr_any;
1215 ip->sa.sa_family = AF_INET6;
1216#endif
1217 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001218 }
1219
1220 if (tb[NDA_PORT]) {
1221 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1222 return -EINVAL;
1223 *port = nla_get_be16(tb[NDA_PORT]);
1224 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001225 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001226 }
1227
1228 if (tb[NDA_VNI]) {
1229 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1230 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +01001231 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001232 } else {
1233 *vni = vxlan->default_dst.remote_vni;
1234 }
1235
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001236 if (tb[NDA_SRC_VNI]) {
1237 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1238 return -EINVAL;
1239 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1240 } else {
1241 *src_vni = vxlan->default_dst.remote_vni;
1242 }
1243
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001244 if (tb[NDA_IFINDEX]) {
1245 struct net_device *tdev;
1246
1247 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1248 return -EINVAL;
1249 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +08001250 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001251 if (!tdev)
1252 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001253 } else {
1254 *ifindex = 0;
1255 }
1256
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001257 if (tb[NDA_NH_ID])
1258 *nhid = nla_get_u32(tb[NDA_NH_ID]);
1259 else
1260 *nhid = 0;
1261
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001262 return 0;
1263}
1264
stephen hemmingerd3428942012-10-01 12:32:35 +00001265/* Add static entry (via netlink) */
1266static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1267 struct net_device *dev,
Petr Machata87b09842019-01-16 23:06:50 +00001268 const unsigned char *addr, u16 vid, u16 flags,
1269 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001270{
1271 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001272 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +08001273 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +00001274 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001275 __be32 src_vni, vni;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001276 u32 ifindex, nhid;
Litao jiaofe1e0712019-06-06 17:57:58 +08001277 u32 hash_index;
stephen hemmingerd3428942012-10-01 12:32:35 +00001278 int err;
1279
1280 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1281 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1282 ndm->ndm_state);
1283 return -EINVAL;
1284 }
1285
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001286 if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID]))
stephen hemmingerd3428942012-10-01 12:32:35 +00001287 return -EINVAL;
1288
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001289 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1290 &nhid);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001291 if (err)
1292 return err;
David Stevens66817122013-03-15 04:35:51 +00001293
Mike Rapoport5933a7b2014-04-01 09:23:01 +03001294 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1295 return -EAFNOSUPPORT;
1296
Litao jiaofe1e0712019-06-06 17:57:58 +08001297 hash_index = fdb_head_index(vxlan, addr, src_vni);
1298 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001299 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
Petr Machata45598c12018-11-21 08:02:36 +00001300 port, src_vni, vni, ifindex,
1301 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001302 nhid, true, extack);
Litao jiaofe1e0712019-06-06 17:57:58 +08001303 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001304
1305 return err;
1306}
1307
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001308static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1309 const unsigned char *addr, union vxlan_addr ip,
Xin Longfc39c382017-11-26 21:19:05 +08001310 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001311 u32 ifindex, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +00001312{
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001313 struct vxlan_rdst *rd = NULL;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001314 struct vxlan_fdb *f;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001315 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001316
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001317 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001318 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001319 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001320
Cong Wange4c7ed42013-08-31 13:44:33 +08001321 if (!vxlan_addr_any(&ip)) {
1322 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001323 if (!rd)
1324 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +00001325 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001326
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001327 /* remove a destination if it's not the only one on the list,
1328 * otherwise destroy the fdb entry
1329 */
1330 if (rd && !list_is_singular(&f->remotes)) {
Petr Machata0e6160f2018-11-21 08:02:35 +00001331 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001332 goto out;
1333 }
1334
Petr Machata0e6160f2018-11-21 08:02:35 +00001335 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001336
1337out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001338 return 0;
1339}
1340
1341/* Delete entry (via netlink) */
1342static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1343 struct net_device *dev,
1344 const unsigned char *addr, u16 vid)
1345{
1346 struct vxlan_dev *vxlan = netdev_priv(dev);
1347 union vxlan_addr ip;
1348 __be32 src_vni, vni;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001349 u32 ifindex, nhid;
Litao jiaofe1e0712019-06-06 17:57:58 +08001350 u32 hash_index;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001351 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001352 int err;
1353
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001354 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1355 &nhid);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001356 if (err)
1357 return err;
1358
Litao jiaofe1e0712019-06-06 17:57:58 +08001359 hash_index = fdb_head_index(vxlan, addr, src_vni);
1360 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata0e6160f2018-11-21 08:02:35 +00001361 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1362 true);
Litao jiaofe1e0712019-06-06 17:57:58 +08001363 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001364
1365 return err;
1366}
1367
1368/* Dump forwarding table */
1369static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04001370 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07001371 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +00001372{
1373 struct vxlan_dev *vxlan = netdev_priv(dev);
1374 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -07001375 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001376
1377 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1378 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001379
Ido Schimmelb5141912020-07-29 11:34:36 +03001380 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001381 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +00001382 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +00001383
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001384 if (rcu_access_pointer(f->nh)) {
Roopa Prabhub18e9832020-06-24 14:02:36 -07001385 if (*idx < cb->args[2])
1386 goto skip_nh;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001387 err = vxlan_fdb_info(skb, vxlan, f,
1388 NETLINK_CB(cb->skb).portid,
1389 cb->nlh->nlmsg_seq,
1390 RTM_NEWNEIGH,
1391 NLM_F_MULTI, NULL);
Ido Schimmelb5141912020-07-29 11:34:36 +03001392 if (err < 0) {
1393 rcu_read_unlock();
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001394 goto out;
Ido Schimmelb5141912020-07-29 11:34:36 +03001395 }
Roopa Prabhub18e9832020-06-24 14:02:36 -07001396skip_nh:
1397 *idx += 1;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001398 continue;
1399 }
1400
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001401 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07001402 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001403 goto skip;
1404
David Stevens66817122013-03-15 04:35:51 +00001405 err = vxlan_fdb_info(skb, vxlan, f,
1406 NETLINK_CB(cb->skb).portid,
1407 cb->nlh->nlmsg_seq,
1408 RTM_NEWNEIGH,
1409 NLM_F_MULTI, rd);
Ido Schimmelb5141912020-07-29 11:34:36 +03001410 if (err < 0) {
1411 rcu_read_unlock();
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001412 goto out;
Ido Schimmelb5141912020-07-29 11:34:36 +03001413 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001414skip:
Roopa Prabhud2976532016-08-30 21:56:45 -07001415 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001416 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001417 }
Ido Schimmelb5141912020-07-29 11:34:36 +03001418 rcu_read_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001419 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001420out:
Roopa Prabhud2976532016-08-30 21:56:45 -07001421 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001422}
1423
Roopa Prabhu474c3c82018-12-15 22:35:10 -08001424static int vxlan_fdb_get(struct sk_buff *skb,
1425 struct nlattr *tb[],
1426 struct net_device *dev,
1427 const unsigned char *addr,
1428 u16 vid, u32 portid, u32 seq,
1429 struct netlink_ext_ack *extack)
1430{
1431 struct vxlan_dev *vxlan = netdev_priv(dev);
1432 struct vxlan_fdb *f;
1433 __be32 vni;
1434 int err;
1435
1436 if (tb[NDA_VNI])
1437 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1438 else
1439 vni = vxlan->default_dst.remote_vni;
1440
1441 rcu_read_lock();
1442
1443 f = __vxlan_find_mac(vxlan, addr, vni);
1444 if (!f) {
1445 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1446 err = -ENOENT;
1447 goto errout;
1448 }
1449
1450 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1451 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1452errout:
1453 rcu_read_unlock();
1454 return err;
1455}
1456
stephen hemmingerd3428942012-10-01 12:32:35 +00001457/* Watch incoming packets to learn mapping between Ethernet address
1458 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +09001459 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +00001460 */
stephen hemminger26a41ae62013-06-17 12:09:58 -07001461static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001462 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001463 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +00001464{
1465 struct vxlan_dev *vxlan = netdev_priv(dev);
1466 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001467 u32 ifindex = 0;
1468
1469#if IS_ENABLED(CONFIG_IPV6)
1470 if (src_ip->sa.sa_family == AF_INET6 &&
1471 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1472 ifindex = src_ifindex;
1473#endif
stephen hemmingerd3428942012-10-01 12:32:35 +00001474
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001475 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001476 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001477 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001478
Matthias Schiffer87613de2017-06-19 10:03:59 +02001479 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1480 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001481 return false;
1482
1483 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -07001484 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001485 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001486
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001487 /* Don't override an fdb with nexthop with a learnt entry */
1488 if (rcu_access_pointer(f->nh))
1489 return true;
1490
stephen hemmingerd3428942012-10-01 12:32:35 +00001491 if (net_ratelimit())
1492 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001493 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001494 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001495
Cong Wange4c7ed42013-08-31 13:44:33 +08001496 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001497 f->updated = jiffies;
Petr Machata4c59b7d2019-01-16 23:06:54 +00001498 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
stephen hemmingerd3428942012-10-01 12:32:35 +00001499 } else {
Litao jiaofe1e0712019-06-06 17:57:58 +08001500 u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
1501
stephen hemmingerd3428942012-10-01 12:32:35 +00001502 /* learned new entry */
Litao jiaofe1e0712019-06-06 17:57:58 +08001503 spin_lock(&vxlan->hash_lock[hash_index]);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001504
1505 /* close off race between vxlan_flush and incoming packets */
1506 if (netif_running(dev))
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001507 vxlan_fdb_update(vxlan, src_mac, src_ip,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001508 NUD_REACHABLE,
1509 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001510 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001511 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001512 vxlan->default_dst.remote_vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001513 ifindex, NTF_SELF, 0, true, NULL);
Litao jiaofe1e0712019-06-06 17:57:58 +08001514 spin_unlock(&vxlan->hash_lock[hash_index]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001515 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001516
1517 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001518}
1519
stephen hemmingerd3428942012-10-01 12:32:35 +00001520/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001521static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001522{
stephen hemminger553675f2013-05-16 11:35:20 +00001523 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001524 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001525#if IS_ENABLED(CONFIG_IPV6)
1526 struct vxlan_sock *sock6;
1527#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001528 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001529
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001530 sock4 = rtnl_dereference(dev->vn4_sock);
1531
Gao feng95ab0992013-12-10 16:37:33 +08001532 /* The vxlan_sock is only used by dev, leaving group has
1533 * no effect on other vxlan devices.
1534 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001535 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001536 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001537#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001538 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001539 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001540 return false;
1541#endif
Gao feng95ab0992013-12-10 16:37:33 +08001542
stephen hemminger553675f2013-05-16 11:35:20 +00001543 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001544 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001545 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001546
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001547 if (family == AF_INET &&
1548 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001549 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001550#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001551 if (family == AF_INET6 &&
1552 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001553 continue;
1554#endif
Gao feng95ab0992013-12-10 16:37:33 +08001555
1556 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1557 &dev->default_dst.remote_ip))
1558 continue;
1559
1560 if (vxlan->default_dst.remote_ifindex !=
1561 dev->default_dst.remote_ifindex)
1562 continue;
1563
1564 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001565 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001566
1567 return false;
1568}
1569
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001570static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001571{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001572 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001573
Jiri Bencb1be00a2015-09-24 13:50:02 +02001574 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001575 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001576 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001577 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001578
Jiri Bencb1be00a2015-09-24 13:50:02 +02001579 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001580 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001581 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001582 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001583 (vs->flags & VXLAN_F_GPE) ?
1584 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001585 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001586 spin_unlock(&vn->sock_lock);
1587
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001588 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001589}
1590
Jiri Bencb1be00a2015-09-24 13:50:02 +02001591static void vxlan_sock_release(struct vxlan_dev *vxlan)
1592{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001593 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001594#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001595 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1596
Mark Bloch57d88182017-06-07 14:36:58 +03001597 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001598#endif
1599
Mark Bloch57d88182017-06-07 14:36:58 +03001600 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001601 synchronize_net();
1602
Mark Blocha53cb292017-06-02 03:24:08 +03001603 vxlan_vs_del_dev(vxlan);
1604
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001605 if (__vxlan_sock_release_prep(sock4)) {
1606 udp_tunnel_sock_release(sock4->sock);
1607 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001608 }
1609
1610#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001611 if (__vxlan_sock_release_prep(sock6)) {
1612 udp_tunnel_sock_release(sock6->sock);
1613 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001614 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001615#endif
1616}
1617
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001618/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001619 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001620 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001621static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001622{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001623 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001624 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1625 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001626 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001627
Cong Wange4c7ed42013-08-31 13:44:33 +08001628 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001629 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001630 struct ip_mreqn mreq = {
1631 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1632 .imr_ifindex = ifindex,
1633 };
1634
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001635 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001636 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001637 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001638 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001639#if IS_ENABLED(CONFIG_IPV6)
1640 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001641 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1642
1643 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001644 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001645 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1646 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001647 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001648#endif
1649 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001650
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001651 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001652}
1653
1654/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001655static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001656{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001657 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001658 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1659 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001660 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001661
Cong Wange4c7ed42013-08-31 13:44:33 +08001662 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001663 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001664 struct ip_mreqn mreq = {
1665 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1666 .imr_ifindex = ifindex,
1667 };
1668
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001669 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001670 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001671 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001672 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001673#if IS_ENABLED(CONFIG_IPV6)
1674 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001675 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1676
1677 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001678 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001679 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1680 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001681 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001682#endif
1683 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001684
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001685 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001686}
1687
Jiri Bencf14eceb2016-02-16 21:59:01 +01001688static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1689 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001690{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001691 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001692
Jiri Bencf14eceb2016-02-16 21:59:01 +01001693 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1694 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001695
Jiri Bencf14eceb2016-02-16 21:59:01 +01001696 start = vxlan_rco_start(unparsed->vx_vni);
1697 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001698
Jiri Benc7d34fa72016-03-21 17:50:05 +01001699 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001700 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001701
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001702 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1703 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001704out:
1705 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1706 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001707 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001708}
1709
Jiri Bencf14eceb2016-02-16 21:59:01 +01001710static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001711 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001712 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001713{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001714 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001715 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001716
Jiri Bencf14eceb2016-02-16 21:59:01 +01001717 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1718 goto out;
1719
Jiri Benc3288af02016-02-16 21:59:00 +01001720 md->gbp = ntohs(gbp->policy_id);
1721
Jiri Benc10a5af22016-02-23 18:02:59 +01001722 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001723 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001724 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001725 tun_dst->u.tun_info.options_len = sizeof(*md);
1726 }
Jiri Benc3288af02016-02-16 21:59:00 +01001727 if (gbp->dont_learn)
1728 md->gbp |= VXLAN_GBP_DONT_LEARN;
1729
1730 if (gbp->policy_applied)
1731 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001732
Jiri Benc64f87d32016-02-23 18:02:55 +01001733 /* In flow-based mode, GBP is carried in dst_metadata */
1734 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1735 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001736out:
1737 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001738}
1739
Jiri Bence1e53142016-04-05 14:47:13 +02001740static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001741 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001742 struct sk_buff *skb, u32 vxflags)
1743{
1744 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1745
1746 /* Need to have Next Protocol set for interfaces in GPE mode. */
1747 if (!gpe->np_applied)
1748 return false;
1749 /* "The initial version is 0. If a receiver does not support the
1750 * version indicated it MUST drop the packet.
1751 */
1752 if (gpe->version != 0)
1753 return false;
1754 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1755 * processing MUST occur." However, we don't implement OAM
1756 * processing, thus drop the packet.
1757 */
1758 if (gpe->oam_flag)
1759 return false;
1760
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001761 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1762 if (!*protocol)
Jiri Bence1e53142016-04-05 14:47:13 +02001763 return false;
Jiri Bence1e53142016-04-05 14:47:13 +02001764
1765 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1766 return true;
1767}
1768
Jiri Benc1ab016e2016-02-23 18:02:56 +01001769static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1770 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001771 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001772{
Thomas Graf614732e2015-07-21 10:44:06 +02001773 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001774 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001775
Thomas Graf614732e2015-07-21 10:44:06 +02001776 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001777 skb->protocol = eth_type_trans(skb, vxlan->dev);
1778 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1779
1780 /* Ignore packet loops (and multicast echo) */
1781 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001782 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001783
Jiri Benc760c6802016-02-23 18:02:57 +01001784 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001785 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001786 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001787 saddr.sa.sa_family = AF_INET;
1788#if IS_ENABLED(CONFIG_IPV6)
1789 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001790 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001791 saddr.sa.sa_family = AF_INET6;
1792#endif
1793 }
1794
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001795 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001796 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001797 return false;
1798
1799 return true;
1800}
1801
Jiri Benc760c6802016-02-23 18:02:57 +01001802static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1803 struct sk_buff *skb)
1804{
1805 int err = 0;
1806
1807 if (vxlan_get_sk_family(vs) == AF_INET)
1808 err = IP_ECN_decapsulate(oiph, skb);
1809#if IS_ENABLED(CONFIG_IPV6)
1810 else
1811 err = IP6_ECN_decapsulate(oiph, skb);
1812#endif
1813
1814 if (unlikely(err) && log_ecn_error) {
1815 if (vxlan_get_sk_family(vs) == AF_INET)
1816 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1817 &((struct iphdr *)oiph)->saddr,
1818 ((struct iphdr *)oiph)->tos);
1819 else
1820 net_info_ratelimited("non-ECT from %pI6\n",
1821 &((struct ipv6hdr *)oiph)->saddr);
1822 }
1823 return err <= 1;
1824}
1825
stephen hemmingerd3428942012-10-01 12:32:35 +00001826/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001827static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001828{
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)
Fabian Frederick01893992020-09-25 15:16:18 +02001879 if (unlikely(!vxlan_remcsum(&unparsed, skb, vs->flags)))
Fabian Frederick2ae29042020-09-25 15:16:02 +02001880 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
Fabian Frederick1f8dda12020-10-05 22:34:28 +02001942 dev_sw_netstats_rx_add(vxlan->dev, skb->len);
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001943 gro_cells_receive(&vxlan->gro_cells, skb);
Eric Dumazet59cbf562019-03-10 10:36:40 -07001944
1945 rcu_read_unlock();
1946
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001947 return 0;
1948
1949drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001950 /* Consume bad packet */
1951 kfree_skb(skb);
1952 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001953}
1954
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001955/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1956static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1957{
1958 struct vxlan_dev *vxlan;
1959 struct vxlan_sock *vs;
1960 struct vxlanhdr *hdr;
1961 __be32 vni;
1962
Stefano Brivio8399a692019-06-11 00:27:05 +02001963 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001964 return -EINVAL;
1965
1966 hdr = vxlan_hdr(skb);
1967
1968 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1969 return -EINVAL;
1970
1971 vs = rcu_dereference_sk_user_data(sk);
1972 if (!vs)
1973 return -ENOENT;
1974
1975 vni = vxlan_vni(hdr->vx_vni);
1976 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1977 if (!vxlan)
1978 return -ENOENT;
1979
1980 return 0;
1981}
1982
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001983static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001984{
1985 struct vxlan_dev *vxlan = netdev_priv(dev);
1986 struct arphdr *parp;
1987 u8 *arpptr, *sha;
1988 __be32 sip, tip;
1989 struct neighbour *n;
1990
1991 if (dev->flags & IFF_NOARP)
1992 goto out;
1993
1994 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1995 dev->stats.tx_dropped++;
1996 goto out;
1997 }
1998 parp = arp_hdr(skb);
1999
2000 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
2001 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
2002 parp->ar_pro != htons(ETH_P_IP) ||
2003 parp->ar_op != htons(ARPOP_REQUEST) ||
2004 parp->ar_hln != dev->addr_len ||
2005 parp->ar_pln != 4)
2006 goto out;
2007 arpptr = (u8 *)parp + sizeof(struct arphdr);
2008 sha = arpptr;
2009 arpptr += dev->addr_len; /* sha */
2010 memcpy(&sip, arpptr, sizeof(sip));
2011 arpptr += sizeof(sip);
2012 arpptr += dev->addr_len; /* tha */
2013 memcpy(&tip, arpptr, sizeof(tip));
2014
2015 if (ipv4_is_loopback(tip) ||
2016 ipv4_is_multicast(tip))
2017 goto out;
2018
2019 n = neigh_lookup(&arp_tbl, &tip, dev);
2020
2021 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00002022 struct vxlan_fdb *f;
2023 struct sk_buff *reply;
2024
2025 if (!(n->nud_state & NUD_CONNECTED)) {
2026 neigh_release(n);
2027 goto out;
2028 }
2029
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002030 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08002031 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00002032 /* bridge-local neighbor */
2033 neigh_release(n);
2034 goto out;
2035 }
2036
2037 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
2038 n->ha, sha);
2039
2040 neigh_release(n);
2041
David Stevens73461352014-03-18 12:32:29 -04002042 if (reply == NULL)
2043 goto out;
2044
David Stevense4f67ad2012-11-20 02:50:14 +00002045 skb_reset_mac_header(reply);
2046 __skb_pull(reply, skb_network_offset(reply));
2047 reply->ip_summed = CHECKSUM_UNNECESSARY;
2048 reply->pkt_type = PACKET_HOST;
2049
2050 if (netif_rx_ni(reply) == NET_RX_DROP)
2051 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002052 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002053 union vxlan_addr ipa = {
2054 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002055 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08002056 };
2057
2058 vxlan_ip_miss(dev, &ipa);
2059 }
David Stevense4f67ad2012-11-20 02:50:14 +00002060out:
2061 consume_skb(skb);
2062 return NETDEV_TX_OK;
2063}
2064
Cong Wangf564f452013-08-31 13:44:36 +08002065#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04002066static struct sk_buff *vxlan_na_create(struct sk_buff *request,
2067 struct neighbour *n, bool isrouter)
2068{
2069 struct net_device *dev = request->dev;
2070 struct sk_buff *reply;
2071 struct nd_msg *ns, *na;
2072 struct ipv6hdr *pip6;
2073 u8 *daddr;
2074 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
2075 int ns_olen;
2076 int i, len;
2077
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002078 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04002079 return NULL;
2080
2081 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
2082 sizeof(*na) + na_olen + dev->needed_tailroom;
2083 reply = alloc_skb(len, GFP_ATOMIC);
2084 if (reply == NULL)
2085 return NULL;
2086
2087 reply->protocol = htons(ETH_P_IPV6);
2088 reply->dev = dev;
2089 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
2090 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00002091 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04002092
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002093 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04002094
2095 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002096 ns_olen = request->len - skb_network_offset(request) -
2097 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04002098 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
Ido Schimmel8066e6b2020-06-01 15:58:55 +03002099 if (!ns->opt[i + 1]) {
2100 kfree_skb(reply);
2101 return NULL;
2102 }
David Stevens4b29dba2014-03-24 10:39:58 -04002103 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
2104 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
2105 break;
2106 }
2107 }
2108
2109 /* Ethernet header */
2110 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
2111 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
2112 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
2113 reply->protocol = htons(ETH_P_IPV6);
2114
2115 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00002116 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04002117 skb_put(reply, sizeof(struct ipv6hdr));
2118
2119 /* IPv6 header */
2120
2121 pip6 = ipv6_hdr(reply);
2122 memset(pip6, 0, sizeof(struct ipv6hdr));
2123 pip6->version = 6;
2124 pip6->priority = ipv6_hdr(request)->priority;
2125 pip6->nexthdr = IPPROTO_ICMPV6;
2126 pip6->hop_limit = 255;
2127 pip6->daddr = ipv6_hdr(request)->saddr;
2128 pip6->saddr = *(struct in6_addr *)n->primary_key;
2129
2130 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00002131 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04002132
David Stevens4b29dba2014-03-24 10:39:58 -04002133 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02002134 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04002135 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
2136 na->icmph.icmp6_router = isrouter;
2137 na->icmph.icmp6_override = 1;
2138 na->icmph.icmp6_solicited = 1;
2139 na->target = ns->target;
2140 ether_addr_copy(&na->opt[2], n->ha);
2141 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
2142 na->opt[1] = na_olen >> 3;
2143
2144 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
2145 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
2146 csum_partial(na, sizeof(*na)+na_olen, 0));
2147
2148 pip6->payload_len = htons(sizeof(*na)+na_olen);
2149
2150 skb_push(reply, sizeof(struct ipv6hdr));
2151
2152 reply->ip_summed = CHECKSUM_UNNECESSARY;
2153
2154 return reply;
2155}
2156
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002157static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08002158{
2159 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08002160 const struct in6_addr *daddr;
Xin Long8bff36852017-11-11 19:58:50 +08002161 const struct ipv6hdr *iphdr;
David Stevens4b29dba2014-03-24 10:39:58 -04002162 struct inet6_dev *in6_dev;
Xin Long8bff36852017-11-11 19:58:50 +08002163 struct neighbour *n;
2164 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08002165
Eric Dumazet162e7562021-06-21 07:44:17 -07002166 rcu_read_lock();
Cong Wangf564f452013-08-31 13:44:36 +08002167 in6_dev = __in6_dev_get(dev);
2168 if (!in6_dev)
2169 goto out;
2170
Cong Wangf564f452013-08-31 13:44:36 +08002171 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002172 daddr = &iphdr->daddr;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002173 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08002174
David Stevens4b29dba2014-03-24 10:39:58 -04002175 if (ipv6_addr_loopback(daddr) ||
2176 ipv6_addr_is_multicast(&msg->target))
2177 goto out;
2178
2179 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08002180
2181 if (n) {
2182 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04002183 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08002184
2185 if (!(n->nud_state & NUD_CONNECTED)) {
2186 neigh_release(n);
2187 goto out;
2188 }
2189
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002190 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002191 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2192 /* bridge-local neighbor */
2193 neigh_release(n);
2194 goto out;
2195 }
2196
David Stevens4b29dba2014-03-24 10:39:58 -04002197 reply = vxlan_na_create(skb, n,
2198 !!(f ? f->flags & NTF_ROUTER : 0));
2199
Cong Wangf564f452013-08-31 13:44:36 +08002200 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04002201
2202 if (reply == NULL)
2203 goto out;
2204
2205 if (netif_rx_ni(reply) == NET_RX_DROP)
2206 dev->stats.rx_dropped++;
2207
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002208 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04002209 union vxlan_addr ipa = {
2210 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002211 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04002212 };
2213
Cong Wangf564f452013-08-31 13:44:36 +08002214 vxlan_ip_miss(dev, &ipa);
2215 }
2216
2217out:
Eric Dumazet162e7562021-06-21 07:44:17 -07002218 rcu_read_unlock();
Cong Wangf564f452013-08-31 13:44:36 +08002219 consume_skb(skb);
2220 return NETDEV_TX_OK;
2221}
2222#endif
2223
David Stevense4f67ad2012-11-20 02:50:14 +00002224static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2225{
2226 struct vxlan_dev *vxlan = netdev_priv(dev);
2227 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00002228
2229 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2230 return false;
2231
2232 n = NULL;
2233 switch (ntohs(eth_hdr(skb)->h_proto)) {
2234 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08002235 {
2236 struct iphdr *pip;
2237
David Stevense4f67ad2012-11-20 02:50:14 +00002238 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2239 return false;
2240 pip = ip_hdr(skb);
2241 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002242 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002243 union vxlan_addr ipa = {
2244 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002245 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08002246 };
2247
2248 vxlan_ip_miss(dev, &ipa);
2249 return false;
2250 }
2251
David Stevense4f67ad2012-11-20 02:50:14 +00002252 break;
Cong Wange15a00a2013-08-31 13:44:34 +08002253 }
2254#if IS_ENABLED(CONFIG_IPV6)
2255 case ETH_P_IPV6:
2256 {
2257 struct ipv6hdr *pip6;
2258
2259 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2260 return false;
2261 pip6 = ipv6_hdr(skb);
2262 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002263 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08002264 union vxlan_addr ipa = {
2265 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002266 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08002267 };
2268
2269 vxlan_ip_miss(dev, &ipa);
2270 return false;
2271 }
2272
2273 break;
2274 }
2275#endif
David Stevense4f67ad2012-11-20 02:50:14 +00002276 default:
2277 return false;
2278 }
2279
2280 if (n) {
2281 bool diff;
2282
Joe Perches7367d0b2013-09-01 11:51:23 -07002283 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00002284 if (diff) {
2285 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2286 dev->addr_len);
2287 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2288 }
2289 neigh_release(n);
2290 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08002291 }
2292
David Stevense4f67ad2012-11-20 02:50:14 +00002293 return false;
2294}
2295
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002296static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01002297 struct vxlan_metadata *md)
2298{
2299 struct vxlanhdr_gbp *gbp;
2300
Thomas Grafdb79a622015-02-04 17:00:04 +01002301 if (!md->gbp)
2302 return;
2303
Thomas Graf35114942015-01-15 03:53:55 +01002304 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01002305 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01002306
2307 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2308 gbp->dont_learn = 1;
2309
2310 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2311 gbp->policy_applied = 1;
2312
2313 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2314}
2315
Jiri Bence1e53142016-04-05 14:47:13 +02002316static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2317 __be16 protocol)
2318{
2319 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2320
2321 gpe->np_applied = 1;
Jiri Bencfa20e0e2017-08-28 21:43:22 +02002322 gpe->next_protocol = tun_p_from_eth_p(protocol);
2323 if (!gpe->next_protocol)
2324 return -EPFNOSUPPORT;
2325 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002326}
2327
Jiri Bencf491e562016-02-02 18:09:16 +01002328static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2329 int iphdr_len, __be32 vni,
2330 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002331 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08002332{
Cong Wange4c7ed42013-08-31 13:44:33 +08002333 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08002334 int min_headroom;
2335 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08002336 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02002337 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08002338
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002339 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08002340 skb->ip_summed == CHECKSUM_PARTIAL) {
2341 int csum_start = skb_checksum_start_offset(skb);
2342
2343 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2344 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2345 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00002346 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08002347 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08002348 }
2349
Cong Wange4c7ed42013-08-31 13:44:33 +08002350 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08002351 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002352
2353 /* Need space for new headers (invalidates iph ptr) */
2354 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02002355 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08002356 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002357
Alexander Duyckaed069d2016-04-14 15:33:37 -04002358 err = iptunnel_handle_offloads(skb, type);
2359 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08002360 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07002361
Johannes Bergd58ff352017-06-16 14:29:23 +02002362 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01002363 vxh->vx_flags = VXLAN_HF_VNI;
2364 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07002365
Tom Herbertdfd86452015-01-12 17:00:38 -08002366 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01002367 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08002368
Jiri Benc54bfd872016-02-16 21:58:58 +01002369 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2370 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2371 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08002372
2373 if (!skb_is_gso(skb)) {
2374 skb->ip_summed = CHECKSUM_NONE;
2375 skb->encapsulation = 0;
2376 }
2377 }
2378
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002379 if (vxflags & VXLAN_F_GBP)
2380 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02002381 if (vxflags & VXLAN_F_GPE) {
2382 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2383 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002384 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02002385 inner_protocol = skb->protocol;
2386 }
Thomas Graf35114942015-01-15 03:53:55 +01002387
Jiri Bence1e53142016-04-05 14:47:13 +02002388 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08002389 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07002390}
Pravin B Shelar49560532013-08-19 11:23:17 -07002391
pravin shelar655c3de2016-11-13 20:43:55 -08002392static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2393 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002394 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002395 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002396 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002397 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01002398{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002399 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002400 struct rtable *rt = NULL;
2401 struct flowi4 fl4;
2402
pravin shelar655c3de2016-11-13 20:43:55 -08002403 if (!sock4)
2404 return ERR_PTR(-EIO);
2405
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002406 if (tos && !info)
2407 use_cache = false;
2408 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002409 rt = dst_cache_get_ip4(dst_cache, saddr);
2410 if (rt)
2411 return rt;
2412 }
2413
Jiri Benc1a8496b2016-02-02 18:09:14 +01002414 memset(&fl4, 0, sizeof(fl4));
2415 fl4.flowi4_oif = oif;
2416 fl4.flowi4_tos = RT_TOS(tos);
2417 fl4.flowi4_mark = skb->mark;
2418 fl4.flowi4_proto = IPPROTO_UDP;
2419 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002420 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002421 fl4.fl4_dport = dport;
2422 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002423
2424 rt = ip_route_output_key(vxlan->net, &fl4);
Enrico Weigelt478db1f2019-06-05 23:19:13 +02002425 if (!IS_ERR(rt)) {
pravin shelar655c3de2016-11-13 20:43:55 -08002426 if (rt->dst.dev == dev) {
2427 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2428 ip_rt_put(rt);
2429 return ERR_PTR(-ELOOP);
2430 }
2431
Jiri Benc1a8496b2016-02-02 18:09:14 +01002432 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002433 if (use_cache)
2434 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08002435 } else {
2436 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2437 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002438 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002439 return rt;
2440}
2441
Jiri Bence5d4b292015-12-07 13:04:30 +01002442#if IS_ENABLED(CONFIG_IPV6)
2443static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08002444 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08002445 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01002446 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002447 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01002448 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002449 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002450 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002451 struct dst_cache *dst_cache,
2452 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01002453{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002454 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002455 struct dst_entry *ndst;
2456 struct flowi6 fl6;
Jiri Bence5d4b292015-12-07 13:04:30 +01002457
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002458 if (!sock6)
2459 return ERR_PTR(-EIO);
2460
Daniel Borkmann14006152016-03-04 15:15:08 +01002461 if (tos && !info)
2462 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002463 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002464 ndst = dst_cache_get_ip6(dst_cache, saddr);
2465 if (ndst)
2466 return ndst;
2467 }
2468
Jiri Bence5d4b292015-12-07 13:04:30 +01002469 memset(&fl6, 0, sizeof(fl6));
2470 fl6.flowi6_oif = oif;
2471 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002472 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01002473 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01002474 fl6.flowi6_mark = skb->mark;
2475 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002476 fl6.fl6_dport = dport;
2477 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01002478
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +01002479 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk,
2480 &fl6, NULL);
2481 if (unlikely(IS_ERR(ndst))) {
pravin shelar655c3de2016-11-13 20:43:55 -08002482 netdev_dbg(dev, "no route to %pI6\n", daddr);
2483 return ERR_PTR(-ENETUNREACH);
2484 }
2485
2486 if (unlikely(ndst->dev == dev)) {
2487 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2488 dst_release(ndst);
2489 return ERR_PTR(-ELOOP);
2490 }
Jiri Bence5d4b292015-12-07 13:04:30 +01002491
2492 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002493 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002494 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01002495 return ndst;
2496}
2497#endif
2498
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002499/* Bypass encapsulation if the destination is local */
2500static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Stefano Briviofc68c992020-08-04 07:53:44 +02002501 struct vxlan_dev *dst_vxlan, __be32 vni,
2502 bool snoop)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002503{
Li RongQing8f849852014-01-04 13:57:59 +08002504 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08002505 union vxlan_addr loopback;
2506 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Eric Dumazet4179cb52019-02-07 12:27:38 -08002507 struct net_device *dev;
Li RongQingce6502a2014-10-16 08:49:41 +08002508 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002509
Li RongQing8f849852014-01-04 13:57:59 +08002510 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2511 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002512 skb->pkt_type = PACKET_HOST;
2513 skb->encapsulation = 0;
2514 skb->dev = dst_vxlan->dev;
2515 __skb_pull(skb, skb_network_offset(skb));
2516
Cong Wange4c7ed42013-08-31 13:44:33 +08002517 if (remote_ip->sa.sa_family == AF_INET) {
2518 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2519 loopback.sa.sa_family = AF_INET;
2520#if IS_ENABLED(CONFIG_IPV6)
2521 } else {
2522 loopback.sin6.sin6_addr = in6addr_loopback;
2523 loopback.sa.sa_family = AF_INET6;
2524#endif
2525 }
2526
Eric Dumazet4179cb52019-02-07 12:27:38 -08002527 rcu_read_lock();
2528 dev = skb->dev;
2529 if (unlikely(!(dev->flags & IFF_UP))) {
2530 kfree_skb(skb);
2531 goto drop;
2532 }
2533
Stefano Briviofc68c992020-08-04 07:53:44 +02002534 if ((dst_vxlan->cfg.flags & VXLAN_F_LEARN) && snoop)
Eric Dumazet4179cb52019-02-07 12:27:38 -08002535 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002536
2537 u64_stats_update_begin(&tx_stats->syncp);
2538 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002539 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002540 u64_stats_update_end(&tx_stats->syncp);
2541
2542 if (netif_rx(skb) == NET_RX_SUCCESS) {
2543 u64_stats_update_begin(&rx_stats->syncp);
2544 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002545 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002546 u64_stats_update_end(&rx_stats->syncp);
2547 } else {
Eric Dumazet4179cb52019-02-07 12:27:38 -08002548drop:
Li RongQingce6502a2014-10-16 08:49:41 +08002549 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002550 }
Eric Dumazet4179cb52019-02-07 12:27:38 -08002551 rcu_read_unlock();
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002552}
2553
pravin shelarfee1fad2016-11-13 20:43:56 -08002554static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002555 struct vxlan_dev *vxlan,
2556 union vxlan_addr *daddr,
2557 __be16 dst_port, int dst_ifindex, __be32 vni,
2558 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002559 u32 rt_flags)
2560{
2561#if IS_ENABLED(CONFIG_IPV6)
2562 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2563 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2564 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2565 */
2566 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2567#endif
2568 /* Bypass encapsulation if the destination is local */
2569 if (rt_flags & RTCF_LOCAL &&
2570 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2571 struct vxlan_dev *dst_vxlan;
2572
2573 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002574 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002575 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002576 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002577 if (!dst_vxlan) {
2578 dev->stats.tx_errors++;
2579 kfree_skb(skb);
2580
2581 return -ENOENT;
2582 }
Stefano Briviofc68c992020-08-04 07:53:44 +02002583 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni, true);
pravin shelarfee1fad2016-11-13 20:43:56 -08002584 return 1;
2585 }
2586
2587 return 0;
2588}
2589
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002590static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002591 __be32 default_vni, struct vxlan_rdst *rdst,
2592 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002593{
Paolo Abenid71785f2016-02-12 15:43:57 +01002594 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002595 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002596 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002597 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002598 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002599 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002600 struct vxlan_metadata _md;
2601 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002602 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002603 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002604 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002605 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002606 int ifindex;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07002607 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002608 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002609 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002610 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002611
Jiri Benc61adedf2015-08-20 13:56:25 +02002612 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002613
Thomas Grafee122c72015-07-21 10:43:58 +02002614 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002615 dst = &rdst->remote_ip;
2616 if (vxlan_addr_any(dst)) {
2617 if (did_rsc) {
2618 /* short-circuited back to local bridge */
Stefano Briviofc68c992020-08-04 07:53:44 +02002619 vxlan_encap_bypass(skb, vxlan, vxlan,
2620 default_vni, true);
pravin shelar0770b532016-11-13 20:43:57 -08002621 return;
2622 }
2623 goto drop;
2624 }
2625
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002626 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002627 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002628 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002629 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002630 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002631 md->gbp = skb->mark;
Hangbin Liu72f6d712018-04-17 14:11:28 +08002632 if (flags & VXLAN_F_TTL_INHERIT) {
2633 ttl = ip_tunnel_get_ttl(old_iph, skb);
2634 } else {
2635 ttl = vxlan->cfg.ttl;
2636 if (!ttl && vxlan_addr_multicast(dst))
2637 ttl = 1;
2638 }
pravin shelar0770b532016-11-13 20:43:57 -08002639
2640 tos = vxlan->cfg.tos;
2641 if (tos == 1)
2642 tos = ip_tunnel_get_dsfield(old_iph, skb);
2643
2644 if (dst->sa.sa_family == AF_INET)
2645 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2646 else
2647 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2648 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002649 } else {
Jakub Kicinski435be282020-09-25 18:56:04 -07002650 if (!info) {
2651 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2652 dev->name);
2653 goto drop;
2654 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002655 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002656 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002657 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002658 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2659 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002660 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002661 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2662 }
Thomas Grafee122c72015-07-21 10:43:58 +02002663 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002664 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2665 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002666 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002667 dst_cache = &info->dst_cache;
Xin Longeadf52c2019-10-29 01:24:32 +08002668 if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
2669 if (info->options_len < sizeof(*md))
2670 goto drop;
pravin shelar0770b532016-11-13 20:43:57 -08002671 md = ip_tunnel_info_opts(info);
Xin Longeadf52c2019-10-29 01:24:32 +08002672 }
Jiri Benca725e512015-08-20 13:56:30 +02002673 ttl = info->key.ttl;
2674 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002675 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002676 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002677 }
pravin shelar0770b532016-11-13 20:43:57 -08002678 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2679 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002680
Jakub Kicinski56de8592017-02-24 11:43:36 -08002681 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002682 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002683 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002684 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002685 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002686
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002687 if (!ifindex)
2688 ifindex = sock4->sock->sk->sk_bound_dev_if;
2689
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002690 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002691 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002692 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002693 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002694 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002695 if (IS_ERR(rt)) {
2696 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002697 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002698 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002699
pravin shelarfee1fad2016-11-13 20:43:56 -08002700 if (!info) {
Stefano Briviob4d306972018-11-08 12:19:16 +01002701 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002702 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002703 dst_port, ifindex, vni,
2704 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002705 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002706 goto out_unlock;
Stefano Briviob4d306972018-11-08 12:19:16 +01002707
2708 if (vxlan->cfg.df == VXLAN_DF_SET) {
2709 df = htons(IP_DF);
2710 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2711 struct ethhdr *eth = eth_hdr(skb);
2712
2713 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2714 (ntohs(eth->h_proto) == ETH_P_IP &&
2715 old_iph->frag_off & htons(IP_DF)))
2716 df = htons(IP_DF);
2717 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002718 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002719 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002720 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002721
pravin shelarc46b7892016-11-13 20:43:54 -08002722 ndst = &rt->dst;
Stefano Briviofc68c992020-08-04 07:53:44 +02002723 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM,
2724 netif_is_any_bridge_port(dev));
2725 if (err < 0) {
2726 goto tx_error;
2727 } else if (err) {
2728 if (info) {
Antoine Tenartf3bc1882021-03-25 16:35:32 +01002729 struct ip_tunnel_info *unclone;
Stefano Briviofc68c992020-08-04 07:53:44 +02002730 struct in_addr src, dst;
2731
Antoine Tenartf3bc1882021-03-25 16:35:32 +01002732 unclone = skb_tunnel_info_unclone(skb);
2733 if (unlikely(!unclone))
2734 goto tx_error;
2735
Stefano Briviofc68c992020-08-04 07:53:44 +02002736 src = remote_ip.sin.sin_addr;
2737 dst = local_ip.sin.sin_addr;
Antoine Tenartf3bc1882021-03-25 16:35:32 +01002738 unclone->key.u.ipv4.src = src.s_addr;
2739 unclone->key.u.ipv4.dst = dst.s_addr;
Stefano Briviofc68c992020-08-04 07:53:44 +02002740 }
2741 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2742 dst_release(ndst);
2743 goto out_unlock;
2744 }
Xin Longa93bf0f2017-12-18 14:20:56 +08002745
Hangbin Liua0dced12020-08-05 10:41:31 +08002746 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002747 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002748 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002749 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002750 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002751 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002752
Brian Russell11586322017-02-24 17:47:11 +00002753 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002754 dst->sin.sin_addr.s_addr, tos, ttl, df,
2755 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002756#if IS_ENABLED(CONFIG_IPV6)
2757 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002758 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002759
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002760 if (!ifindex)
2761 ifindex = sock6->sock->sk->sk_bound_dev_if;
2762
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002763 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002764 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002765 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002766 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002767 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002768 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002769 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002770 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002771 goto tx_error;
2772 }
pravin shelar655c3de2016-11-13 20:43:55 -08002773
pravin shelarfee1fad2016-11-13 20:43:56 -08002774 if (!info) {
2775 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002776
pravin shelarfee1fad2016-11-13 20:43:56 -08002777 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002778 dst_port, ifindex, vni,
2779 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002780 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002781 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002782 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002783
Stefano Briviofc68c992020-08-04 07:53:44 +02002784 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM,
2785 netif_is_any_bridge_port(dev));
2786 if (err < 0) {
2787 goto tx_error;
2788 } else if (err) {
2789 if (info) {
Antoine Tenartf3bc1882021-03-25 16:35:32 +01002790 struct ip_tunnel_info *unclone;
Stefano Briviofc68c992020-08-04 07:53:44 +02002791 struct in6_addr src, dst;
2792
Antoine Tenartf3bc1882021-03-25 16:35:32 +01002793 unclone = skb_tunnel_info_unclone(skb);
2794 if (unlikely(!unclone))
2795 goto tx_error;
2796
Stefano Briviofc68c992020-08-04 07:53:44 +02002797 src = remote_ip.sin6.sin6_addr;
2798 dst = local_ip.sin6.sin6_addr;
Antoine Tenartf3bc1882021-03-25 16:35:32 +01002799 unclone->key.u.ipv6.src = src;
2800 unclone->key.u.ipv6.dst = dst;
Stefano Briviofc68c992020-08-04 07:53:44 +02002801 }
2802
2803 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2804 dst_release(ndst);
2805 goto out_unlock;
2806 }
Xin Longa93bf0f2017-12-18 14:20:56 +08002807
Hangbin Liua0dced12020-08-05 10:41:31 +08002808 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002809 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002810 skb_scrub_packet(skb, xnet);
2811 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002812 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002813 if (err < 0)
2814 goto tx_error;
2815
pravin shelar0770b532016-11-13 20:43:57 -08002816 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002817 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002818 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002819 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002820#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002821 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002822out_unlock:
2823 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002824 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002825
2826drop:
2827 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002828 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002829 return;
2830
2831tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002832 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002833 if (err == -ELOOP)
2834 dev->stats.collisions++;
2835 else if (err == -ENETUNREACH)
2836 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002837 dst_release(ndst);
2838 dev->stats.tx_errors++;
2839 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002840}
2841
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002842static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
2843 struct vxlan_fdb *f, __be32 vni, bool did_rsc)
2844{
2845 struct vxlan_rdst nh_rdst;
2846 struct nexthop *nh;
2847 bool do_xmit;
2848 u32 hash;
2849
2850 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2851 hash = skb_get_hash(skb);
2852
2853 rcu_read_lock();
2854 nh = rcu_dereference(f->nh);
2855 if (!nh) {
2856 rcu_read_unlock();
2857 goto drop;
2858 }
2859 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2860 rcu_read_unlock();
2861
2862 if (likely(do_xmit))
2863 vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc);
2864 else
2865 goto drop;
2866
2867 return;
2868
2869drop:
2870 dev->stats.tx_dropped++;
2871 dev_kfree_skb(skb);
2872}
2873
David Stevens66817122013-03-15 04:35:51 +00002874/* Transmit local packets over Vxlan
2875 *
2876 * Outer IP header inherits ECN and DF from inner header.
2877 * Outer UDP destination is the VXLAN assigned port.
2878 * source port is based on hash of flow
2879 */
2880static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2881{
2882 struct vxlan_dev *vxlan = netdev_priv(dev);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002883 struct vxlan_rdst *rdst, *fdst = NULL;
Xin Long8bff36852017-11-11 19:58:50 +08002884 const struct ip_tunnel_info *info;
2885 bool did_rsc = false;
David Stevens66817122013-03-15 04:35:51 +00002886 struct vxlan_fdb *f;
Xin Long8bff36852017-11-11 19:58:50 +08002887 struct ethhdr *eth;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002888 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002889
Jiri Benc61adedf2015-08-20 13:56:25 +02002890 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002891
David Stevens66817122013-03-15 04:35:51 +00002892 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002893
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002894 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002895 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2896 info->mode & IP_TUNNEL_INFO_TX) {
2897 vni = tunnel_id_to_key32(info->key.tun_id);
2898 } else {
2899 if (info && info->mode & IP_TUNNEL_INFO_TX)
2900 vxlan_xmit_one(skb, dev, vni, NULL, false);
2901 else
2902 kfree_skb(skb);
2903 return NETDEV_TX_OK;
2904 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002905 }
2906
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002907 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002908 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002909 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002910 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002911#if IS_ENABLED(CONFIG_IPV6)
Xin Long8bff36852017-11-11 19:58:50 +08002912 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2913 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2914 sizeof(struct nd_msg)) &&
2915 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2916 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2917
2918 if (m->icmph.icmp6_code == 0 &&
2919 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002920 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002921 }
2922#endif
2923 }
David Stevens66817122013-03-15 04:35:51 +00002924
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002925 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002926 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002927 did_rsc = false;
2928
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002929 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002930 (ntohs(eth->h_proto) == ETH_P_IP ||
2931 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002932 did_rsc = route_shortcircuit(dev, skb);
2933 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002934 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002935 }
2936
David Stevens66817122013-03-15 04:35:51 +00002937 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002938 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002939 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002940 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002941 !is_multicast_ether_addr(eth->h_dest))
2942 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002943
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002944 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002945 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002946 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002947 }
David Stevens66817122013-03-15 04:35:51 +00002948 }
2949
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002950 if (rcu_access_pointer(f->nh)) {
2951 vxlan_xmit_nh(skb, dev, f,
2952 (vni ? : vxlan->default_dst.remote_vni), did_rsc);
2953 } else {
2954 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2955 struct sk_buff *skb1;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002956
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002957 if (!fdst) {
2958 fdst = rdst;
2959 continue;
2960 }
2961 skb1 = skb_clone(skb, GFP_ATOMIC);
2962 if (skb1)
2963 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002964 }
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002965 if (fdst)
2966 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2967 else
2968 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002969 }
2970
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002971 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002972}
2973
stephen hemmingerd3428942012-10-01 12:32:35 +00002974/* Walk the forwarding table and purge stale entries */
Kees Cookdf7e8282017-10-04 16:26:59 -07002975static void vxlan_cleanup(struct timer_list *t)
stephen hemmingerd3428942012-10-01 12:32:35 +00002976{
Kees Cookdf7e8282017-10-04 16:26:59 -07002977 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
stephen hemmingerd3428942012-10-01 12:32:35 +00002978 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2979 unsigned int h;
2980
2981 if (!netif_running(vxlan->dev))
2982 return;
2983
stephen hemmingerd3428942012-10-01 12:32:35 +00002984 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2985 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002986
Litao jiaofe1e0712019-06-06 17:57:58 +08002987 spin_lock(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002988 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2989 struct vxlan_fdb *f
2990 = container_of(p, struct vxlan_fdb, hlist);
2991 unsigned long timeout;
2992
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002993 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002994 continue;
2995
Roopa Prabhudef499c2017-03-27 15:46:41 -07002996 if (f->flags & NTF_EXT_LEARNED)
2997 continue;
2998
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002999 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00003000 if (time_before_eq(timeout, jiffies)) {
3001 netdev_dbg(vxlan->dev,
3002 "garbage collect %pM\n",
3003 f->eth_addr);
3004 f->state = NUD_STALE;
Petr Machata0e6160f2018-11-21 08:02:35 +00003005 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00003006 } else if (time_before(timeout, next_timer))
3007 next_timer = timeout;
3008 }
Litao jiaofe1e0712019-06-06 17:57:58 +08003009 spin_unlock(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003010 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003011
3012 mod_timer(&vxlan->age_timer, next_timer);
3013}
3014
Mark Blocha53cb292017-06-02 03:24:08 +03003015static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
3016{
3017 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3018
3019 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02003020 hlist_del_init_rcu(&vxlan->hlist4.hlist);
3021#if IS_ENABLED(CONFIG_IPV6)
3022 hlist_del_init_rcu(&vxlan->hlist6.hlist);
3023#endif
Mark Blocha53cb292017-06-02 03:24:08 +03003024 spin_unlock(&vn->sock_lock);
3025}
3026
Jiri Benc69e76662017-07-02 19:00:57 +02003027static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
3028 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003029{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003030 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01003031 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003032
Jiri Benc69e76662017-07-02 19:00:57 +02003033 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003034 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02003035 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003036 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003037}
3038
stephen hemmingerd3428942012-10-01 12:32:35 +00003039/* Setup stats when device is created */
3040static int vxlan_init(struct net_device *dev)
3041{
Taehee Yoo384d91c2020-03-18 13:28:09 +00003042 struct vxlan_dev *vxlan = netdev_priv(dev);
3043 int err;
3044
WANG Cong1c213bd2014-02-13 11:46:28 -08003045 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00003046 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00003047 return -ENOMEM;
3048
Taehee Yoo384d91c2020-03-18 13:28:09 +00003049 err = gro_cells_init(&vxlan->gro_cells, dev);
3050 if (err) {
3051 free_percpu(dev->tstats);
3052 return err;
3053 }
3054
stephen hemmingerd3428942012-10-01 12:32:35 +00003055 return 0;
3056}
3057
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08003058static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003059{
3060 struct vxlan_fdb *f;
Litao jiaofe1e0712019-06-06 17:57:58 +08003061 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003062
Litao jiaofe1e0712019-06-06 17:57:58 +08003063 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08003064 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003065 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003066 vxlan_fdb_destroy(vxlan, f, true, true);
Litao jiaofe1e0712019-06-06 17:57:58 +08003067 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003068}
3069
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003070static void vxlan_uninit(struct net_device *dev)
3071{
3072 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003073
Stefano Brivioad6c9982019-03-08 16:40:57 +01003074 gro_cells_destroy(&vxlan->gro_cells);
3075
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08003076 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003077
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003078 free_percpu(dev->tstats);
3079}
3080
stephen hemmingerd3428942012-10-01 12:32:35 +00003081/* Start ageing timer and join group when device is brought up */
3082static int vxlan_open(struct net_device *dev)
3083{
3084 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02003085 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003086
Jiri Benc205f3562015-09-24 13:50:01 +02003087 ret = vxlan_sock_add(vxlan);
3088 if (ret < 0)
3089 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00003090
Gao feng79d4a942013-12-10 16:37:32 +08003091 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003092 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03003093 if (ret == -EADDRINUSE)
3094 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003095 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02003096 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003097 return ret;
3098 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003099 }
3100
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003101 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00003102 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
3103
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003104 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00003105}
3106
3107/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003108static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00003109{
Cong Wang31fec5a2013-05-27 22:35:52 +00003110 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003111
stephen hemmingerd3428942012-10-01 12:32:35 +00003112 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3113 struct hlist_node *p, *n;
Litao jiaofe1e0712019-06-06 17:57:58 +08003114
3115 spin_lock_bh(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003116 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
3117 struct vxlan_fdb *f
3118 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003119 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
3120 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003121 /* the all_zeros_mac entry is deleted at vxlan_uninit */
Taehee Yoofda2ec62020-08-01 07:07:50 +00003122 if (is_zero_ether_addr(f->eth_addr) &&
3123 f->vni == vxlan->cfg.vni)
3124 continue;
3125 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00003126 }
Litao jiaofe1e0712019-06-06 17:57:58 +08003127 spin_unlock_bh(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003128 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003129}
3130
3131/* Cleanup timer and forwarding table on shutdown */
3132static int vxlan_stop(struct net_device *dev)
3133{
3134 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003135 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003136 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00003137
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03003138 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07003139 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003140 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00003141
3142 del_timer_sync(&vxlan->age_timer);
3143
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003144 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02003145 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00003146
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003147 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00003148}
3149
stephen hemmingerd3428942012-10-01 12:32:35 +00003150/* Stub, nothing needs to be done. */
3151static void vxlan_set_multicast_list(struct net_device *dev)
3152{
3153}
3154
Daniel Borkmann345010b2013-12-18 00:21:08 +01003155static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
3156{
3157 struct vxlan_dev *vxlan = netdev_priv(dev);
3158 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00003159 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3160 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003161 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04003162
3163 /* This check is different than dev->max_mtu, because it looks at
3164 * the lowerdev->mtu, rather than the static dev->max_mtu
3165 */
3166 if (lowerdev) {
3167 int max_mtu = lowerdev->mtu -
3168 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
3169 if (new_mtu > max_mtu)
3170 return -EINVAL;
3171 }
3172
3173 dev->mtu = new_mtu;
3174 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01003175}
3176
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07003177static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
3178{
3179 struct vxlan_dev *vxlan = netdev_priv(dev);
3180 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3181 __be16 sport, dport;
3182
3183 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
3184 vxlan->cfg.port_max, true);
3185 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
3186
Jiri Benc239e9442015-12-07 13:04:31 +01003187 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003188 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01003189 struct rtable *rt;
3190
pravin shelar655c3de2016-11-13 20:43:55 -08003191 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01003192 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01003193 &info->key.u.ipv4.src, dport, sport,
3194 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01003195 if (IS_ERR(rt))
3196 return PTR_ERR(rt);
3197 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01003198 } else {
3199#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08003200 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01003201 struct dst_entry *ndst;
3202
pravin shelar655c3de2016-11-13 20:43:55 -08003203 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003204 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01003205 &info->key.u.ipv6.src, dport, sport,
3206 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01003207 if (IS_ERR(ndst))
3208 return PTR_ERR(ndst);
3209 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01003210#else /* !CONFIG_IPV6 */
3211 return -EPFNOSUPPORT;
3212#endif
3213 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01003214 info->key.tp_src = sport;
3215 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01003216 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07003217}
3218
Jiri Benc0c867c92016-04-05 14:47:10 +02003219static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00003220 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003221 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00003222 .ndo_open = vxlan_open,
3223 .ndo_stop = vxlan_stop,
3224 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00003225 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00003226 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01003227 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00003228 .ndo_validate_addr = eth_validate_addr,
3229 .ndo_set_mac_address = eth_mac_addr,
3230 .ndo_fdb_add = vxlan_fdb_add,
3231 .ndo_fdb_del = vxlan_fdb_delete,
3232 .ndo_fdb_dump = vxlan_fdb_dump,
Roopa Prabhu474c3c82018-12-15 22:35:10 -08003233 .ndo_fdb_get = vxlan_fdb_get,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07003234 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
Andy Roulin8f1af752019-02-22 18:06:38 +00003235 .ndo_change_proto_down = dev_change_proto_down_generic,
stephen hemmingerd3428942012-10-01 12:32:35 +00003236};
3237
Jiri Bence1e53142016-04-05 14:47:13 +02003238static const struct net_device_ops vxlan_netdev_raw_ops = {
3239 .ndo_init = vxlan_init,
3240 .ndo_uninit = vxlan_uninit,
3241 .ndo_open = vxlan_open,
3242 .ndo_stop = vxlan_stop,
3243 .ndo_start_xmit = vxlan_xmit,
3244 .ndo_get_stats64 = ip_tunnel_get_stats64,
3245 .ndo_change_mtu = vxlan_change_mtu,
3246 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3247};
3248
stephen hemmingerd3428942012-10-01 12:32:35 +00003249/* Info for udev, that this is a virtual tunnel endpoint */
3250static struct device_type vxlan_type = {
3251 .name = "vxlan",
3252};
3253
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02003254/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07003255 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02003256 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003257 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003258static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003259{
3260 struct vxlan_sock *vs;
3261 struct net *net = dev_net(dev);
3262 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07003263 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003264
3265 spin_lock(&vn->sock_lock);
3266 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003267 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
3268 unsigned short type;
3269
3270 if (vs->flags & VXLAN_F_GPE)
3271 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3272 else
3273 type = UDP_TUNNEL_TYPE_VXLAN;
3274
3275 if (push)
3276 udp_tunnel_push_rx_port(dev, vs->sock, type);
3277 else
3278 udp_tunnel_drop_rx_port(dev, vs->sock, type);
3279 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003280 }
3281 spin_unlock(&vn->sock_lock);
3282}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003283
stephen hemmingerd3428942012-10-01 12:32:35 +00003284/* Initialize the device structure. */
3285static void vxlan_setup(struct net_device *dev)
3286{
3287 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00003288 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003289
Jiri Benc65226ef2016-04-28 16:36:30 +02003290 eth_hw_addr_random(dev);
3291 ether_setup(dev);
3292
David S. Millercf124db2017-05-08 12:52:56 -04003293 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00003294 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3295
stephen hemmingerd3428942012-10-01 12:32:35 +00003296 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00003297 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003298 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003299 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003300
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07003301 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003302 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003303 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07003304 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02003305 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00003306
Matthias Schiffera9853432017-06-19 10:03:55 +02003307 /* MTU range: 68 - 65535 */
3308 dev->min_mtu = ETH_MIN_MTU;
3309 dev->max_mtu = ETH_MAX_MTU;
3310
stephen hemminger553675f2013-05-16 11:35:20 +00003311 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003312
Kees Cookdf7e8282017-10-04 16:26:59 -07003313 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
stephen hemmingerd3428942012-10-01 12:32:35 +00003314
3315 vxlan->dev = dev;
3316
Litao jiaofe1e0712019-06-06 17:57:58 +08003317 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3318 spin_lock_init(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003319 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
Litao jiaofe1e0712019-06-06 17:57:58 +08003320 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003321}
3322
Jiri Benc0c867c92016-04-05 14:47:10 +02003323static void vxlan_ether_setup(struct net_device *dev)
3324{
Jiri Benc0c867c92016-04-05 14:47:10 +02003325 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3326 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3327 dev->netdev_ops = &vxlan_netdev_ether_ops;
3328}
3329
Jiri Bence1e53142016-04-05 14:47:13 +02003330static void vxlan_raw_setup(struct net_device *dev)
3331{
Jiri Benc65226ef2016-04-28 16:36:30 +02003332 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02003333 dev->type = ARPHRD_NONE;
3334 dev->hard_header_len = 0;
3335 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02003336 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3337 dev->netdev_ops = &vxlan_netdev_raw_ops;
3338}
3339
stephen hemmingerd3428942012-10-01 12:32:35 +00003340static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3341 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08003342 [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003343 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003344 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08003345 [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003346 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003347 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3348 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003349 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003350 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3351 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3352 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00003353 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00003354 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3355 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3356 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3357 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003358 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00003359 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08003360 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3361 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3362 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08003363 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3364 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01003365 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02003366 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003367 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
Hangbin Liu72f6d712018-04-17 14:11:28 +08003368 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
Stefano Briviob4d306972018-11-08 12:19:16 +01003369 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003370};
3371
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02003372static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3373 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003374{
3375 if (tb[IFLA_ADDRESS]) {
3376 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003377 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3378 "Provided link layer address is not Ethernet");
stephen hemmingerd3428942012-10-01 12:32:35 +00003379 return -EINVAL;
3380 }
3381
3382 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003383 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3384 "Provided Ethernet address is not unicast");
stephen hemmingerd3428942012-10-01 12:32:35 +00003385 return -EADDRNOTAVAIL;
3386 }
3387 }
3388
Matthias Schiffera9853432017-06-19 10:03:55 +02003389 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02003390 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02003391
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003392 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3393 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3394 "MTU must be between 68 and 65535");
Matthias Schiffera9853432017-06-19 10:03:55 +02003395 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003396 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003397 }
3398
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003399 if (!data) {
3400 NL_SET_ERR_MSG(extack,
3401 "Required attributes not provided to perform the operation");
stephen hemmingerd3428942012-10-01 12:32:35 +00003402 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003403 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003404
3405 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003406 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3407
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003408 if (id >= VXLAN_N_VID) {
Sabrina Dubrocacc8e7c62020-04-22 17:29:50 +02003409 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003410 "VXLAN ID must be lower than 16777216");
stephen hemmingerd3428942012-10-01 12:32:35 +00003411 return -ERANGE;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003412 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003413 }
3414
stephen hemminger05f47d62012-10-09 20:35:50 +00003415 if (data[IFLA_VXLAN_PORT_RANGE]) {
3416 const struct ifla_vxlan_port_range *p
3417 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3418
3419 if (ntohs(p->high) < ntohs(p->low)) {
Sabrina Dubrocacc8e7c62020-04-22 17:29:50 +02003420 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003421 "Invalid source port range");
stephen hemminger05f47d62012-10-09 20:35:50 +00003422 return -EINVAL;
3423 }
3424 }
3425
Stefano Briviob4d306972018-11-08 12:19:16 +01003426 if (data[IFLA_VXLAN_DF]) {
3427 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3428
3429 if (df < 0 || df > VXLAN_DF_MAX) {
Sabrina Dubrocacc8e7c62020-04-22 17:29:50 +02003430 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
Stefano Briviob4d306972018-11-08 12:19:16 +01003431 "Invalid DF attribute");
3432 return -EINVAL;
3433 }
3434 }
3435
stephen hemmingerd3428942012-10-01 12:32:35 +00003436 return 0;
3437}
3438
Yan Burman1b13c972013-01-29 23:43:07 +00003439static void vxlan_get_drvinfo(struct net_device *netdev,
3440 struct ethtool_drvinfo *drvinfo)
3441{
3442 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3443 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3444}
3445
Matthias Schiffer36fe3a62019-11-12 22:12:24 +01003446static int vxlan_get_link_ksettings(struct net_device *dev,
3447 struct ethtool_link_ksettings *cmd)
3448{
3449 struct vxlan_dev *vxlan = netdev_priv(dev);
3450 struct vxlan_rdst *dst = &vxlan->default_dst;
3451 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3452 dst->remote_ifindex);
3453
3454 if (!lowerdev) {
3455 cmd->base.duplex = DUPLEX_UNKNOWN;
3456 cmd->base.port = PORT_OTHER;
3457 cmd->base.speed = SPEED_UNKNOWN;
3458
3459 return 0;
3460 }
3461
3462 return __ethtool_get_link_ksettings(lowerdev, cmd);
3463}
3464
Yan Burman1b13c972013-01-29 23:43:07 +00003465static const struct ethtool_ops vxlan_ethtool_ops = {
Matthias Schiffer36fe3a62019-11-12 22:12:24 +01003466 .get_drvinfo = vxlan_get_drvinfo,
3467 .get_link = ethtool_op_get_link,
3468 .get_link_ksettings = vxlan_get_link_ksettings,
Yan Burman1b13c972013-01-29 23:43:07 +00003469};
3470
Tom Herbert3ee64f32014-07-13 19:49:42 -07003471static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003472 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +00003473{
Cong Wange4c7ed42013-08-31 13:44:33 +08003474 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003475 struct udp_port_cfg udp_conf;
3476 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08003477
Tom Herbert3ee64f32014-07-13 19:49:42 -07003478 memset(&udp_conf, 0, sizeof(udp_conf));
3479
3480 if (ipv6) {
3481 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003482 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08003483 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02003484 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003485 } else {
3486 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08003487 }
3488
Tom Herbert3ee64f32014-07-13 19:49:42 -07003489 udp_conf.local_udp_port = port;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003490 udp_conf.bind_ifindex = ifindex;
Cong Wange4c7ed42013-08-31 13:44:33 +08003491
Tom Herbert3ee64f32014-07-13 19:49:42 -07003492 /* Open UDP socket */
3493 err = udp_sock_create(net, &udp_conf, &sock);
3494 if (err < 0)
3495 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08003496
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003497 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08003498}
3499
3500/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02003501static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003502 __be16 port, u32 flags,
3503 int ifindex)
Cong Wange4c7ed42013-08-31 13:44:33 +08003504{
3505 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3506 struct vxlan_sock *vs;
3507 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00003508 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003509 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00003510
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02003511 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08003512 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00003513 return ERR_PTR(-ENOMEM);
3514
3515 for (h = 0; h < VNI_HASH_SIZE; ++h)
3516 INIT_HLIST_HEAD(&vs->vni_list[h]);
3517
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003518 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003519 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00003520 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08003521 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00003522 }
3523
Cong Wange4c7ed42013-08-31 13:44:33 +08003524 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03003525 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08003526 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00003527
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003528 spin_lock(&vn->sock_lock);
3529 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07003530 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07003531 (vs->flags & VXLAN_F_GPE) ?
3532 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07003533 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003534 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00003535
3536 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07003537 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07003538 tunnel_cfg.sk_user_data = vs;
3539 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01003540 tunnel_cfg.encap_rcv = vxlan_rcv;
Stefano Brivioc3a43b92018-11-08 12:19:15 +01003541 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003542 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07003543 tunnel_cfg.gro_receive = vxlan_gro_receive;
3544 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003545
3546 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08003547
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003548 return vs;
3549}
stephen hemminger553675f2013-05-16 11:35:20 +00003550
Jiri Bencb1be00a2015-09-24 13:50:02 +02003551static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003552{
Jiri Benc205f3562015-09-24 13:50:01 +02003553 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3554 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02003555 struct vxlan_dev_node *node;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003556 int l3mdev_index = 0;
3557
3558 if (vxlan->cfg.remote_ifindex)
3559 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3560 vxlan->net, vxlan->cfg.remote_ifindex);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003561
Jiri Benc205f3562015-09-24 13:50:01 +02003562 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003563 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003564 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003565 vxlan->cfg.dst_port, vxlan->cfg.flags,
3566 l3mdev_index);
Reshetova, Elena66af8462017-07-04 15:52:59 +03003567 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003568 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003569 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003570 }
3571 spin_unlock(&vn->sock_lock);
3572 }
Jiri Benc205f3562015-09-24 13:50:01 +02003573 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003574 vs = vxlan_socket_create(vxlan->net, ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003575 vxlan->cfg.dst_port, vxlan->cfg.flags,
3576 l3mdev_index);
Jiri Benc205f3562015-09-24 13:50:01 +02003577 if (IS_ERR(vs))
3578 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003579#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02003580 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003581 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003582 node = &vxlan->hlist6;
3583 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02003584#endif
Jiri Benc69e76662017-07-02 19:00:57 +02003585 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003586 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003587 node = &vxlan->hlist4;
3588 }
3589 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02003590 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00003591}
3592
Jiri Bencb1be00a2015-09-24 13:50:02 +02003593static int vxlan_sock_add(struct vxlan_dev *vxlan)
3594{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003595 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3596 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02003597 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003598 int ret = 0;
3599
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003600 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003601#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003602 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02003603 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02003604 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02003605 if (ret < 0 && ret != -EAFNOSUPPORT)
3606 ipv4 = false;
3607 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02003608#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02003609 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003610 ret = __vxlan_sock_add(vxlan, false);
3611 if (ret < 0)
3612 vxlan_sock_release(vxlan);
3613 return ret;
3614}
3615
Matthias Schiffera9853432017-06-19 10:03:55 +02003616static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3617 struct net_device **lower,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003618 struct vxlan_dev *old,
3619 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003620{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01003621 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02003622 struct vxlan_dev *tmp;
3623 bool use_ipv6 = false;
3624
3625 if (conf->flags & VXLAN_F_GPE) {
3626 /* For now, allow GPE only together with
3627 * COLLECT_METADATA. This can be relaxed later; in such
3628 * case, the other side of the PtP link will have to be
3629 * provided.
3630 */
3631 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3632 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003633 NL_SET_ERR_MSG(extack,
3634 "VXLAN GPE does not support this combination of attributes");
Matthias Schiffera9853432017-06-19 10:03:55 +02003635 return -EINVAL;
3636 }
3637 }
3638
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003639 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3640 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02003641 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003642 conf->saddr.sa.sa_family = AF_INET;
3643 } else if (!conf->remote_ip.sa.sa_family) {
3644 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3645 } else if (!conf->saddr.sa.sa_family) {
3646 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3647 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003648
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003649 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3650 NL_SET_ERR_MSG(extack,
3651 "Local and remote address must be from the same family");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003652 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003653 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003654
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003655 if (vxlan_addr_multicast(&conf->saddr)) {
3656 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003657 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003658 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003659
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003660 if (conf->saddr.sa.sa_family == AF_INET6) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003661 if (!IS_ENABLED(CONFIG_IPV6)) {
3662 NL_SET_ERR_MSG(extack,
3663 "IPv6 support not enabled in the kernel");
Matthias Schiffera9853432017-06-19 10:03:55 +02003664 return -EPFNOSUPPORT;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003665 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003666 use_ipv6 = true;
3667 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003668
3669 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3670 int local_type =
3671 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3672 int remote_type =
3673 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3674
3675 if (local_type & IPV6_ADDR_LINKLOCAL) {
3676 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003677 (remote_type != IPV6_ADDR_ANY)) {
3678 NL_SET_ERR_MSG(extack,
3679 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003680 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003681 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003682
3683 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3684 } else {
3685 if (remote_type ==
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003686 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3687 NL_SET_ERR_MSG(extack,
3688 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003689 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003690 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003691
3692 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3693 }
3694 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003695 }
3696
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003697 if (conf->label && !use_ipv6) {
3698 NL_SET_ERR_MSG(extack,
3699 "Label attribute only applies to IPv6 VXLAN devices");
Matthias Schiffera9853432017-06-19 10:03:55 +02003700 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003701 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003702
3703 if (conf->remote_ifindex) {
3704 struct net_device *lowerdev;
3705
3706 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003707 if (!lowerdev) {
3708 NL_SET_ERR_MSG(extack,
3709 "Invalid local interface, device not found");
Matthias Schiffera9853432017-06-19 10:03:55 +02003710 return -ENODEV;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003711 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003712
3713#if IS_ENABLED(CONFIG_IPV6)
3714 if (use_ipv6) {
3715 struct inet6_dev *idev = __in6_dev_get(lowerdev);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003716 if (idev && idev->cnf.disable_ipv6) {
3717 NL_SET_ERR_MSG(extack,
3718 "IPv6 support disabled by administrator");
Matthias Schiffera9853432017-06-19 10:03:55 +02003719 return -EPERM;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003720 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003721 }
3722#endif
3723
3724 *lower = lowerdev;
3725 } else {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003726 if (vxlan_addr_multicast(&conf->remote_ip)) {
3727 NL_SET_ERR_MSG(extack,
3728 "Local interface required for multicast remote destination");
3729
Matthias Schiffera9853432017-06-19 10:03:55 +02003730 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003731 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003732
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003733#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003734 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3735 NL_SET_ERR_MSG(extack,
3736 "Local interface required for link-local local/remote addresses");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003737 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003738 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003739#endif
3740
Matthias Schiffera9853432017-06-19 10:03:55 +02003741 *lower = NULL;
3742 }
3743
3744 if (!conf->dst_port) {
3745 if (conf->flags & VXLAN_F_GPE)
3746 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3747 else
3748 conf->dst_port = htons(vxlan_port);
3749 }
3750
3751 if (!conf->age_interval)
3752 conf->age_interval = FDB_AGE_DEFAULT;
3753
3754 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3755 if (tmp == old)
3756 continue;
3757
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003758 if (tmp->cfg.vni != conf->vni)
3759 continue;
3760 if (tmp->cfg.dst_port != conf->dst_port)
3761 continue;
3762 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003763 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003764 continue;
3765
3766 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3767 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3768 continue;
3769
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003770 NL_SET_ERR_MSG(extack,
3771 "A VXLAN device with the specified VNI already exists");
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003772 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003773 }
3774
3775 return 0;
3776}
3777
3778static void vxlan_config_apply(struct net_device *dev,
3779 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003780 struct net_device *lowerdev,
3781 struct net *src_net,
3782 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003783{
3784 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003785 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003786 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003787 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3788 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003789
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003790 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003791 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003792 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003793 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003794 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003795
Matthias Schiffera9853432017-06-19 10:03:55 +02003796 if (conf->mtu)
3797 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003798
3799 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003800 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003801
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003802 dst->remote_vni = conf->vni;
3803
3804 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003805
Matthias Schiffera9853432017-06-19 10:03:55 +02003806 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003807 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003808
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003809 dev->gso_max_size = lowerdev->gso_max_size;
3810 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003811
3812 needed_headroom = lowerdev->hard_header_len;
Sven Eckelmann0a35dc42020-11-26 13:52:46 +01003813 needed_headroom += lowerdev->needed_headroom;
Matthias Schiffera9853432017-06-19 10:03:55 +02003814
Sven Eckelmanna5e74022020-11-26 13:52:47 +01003815 dev->needed_tailroom = lowerdev->needed_tailroom;
3816
Matthias Schiffera9853432017-06-19 10:03:55 +02003817 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3818 VXLAN_HEADROOM);
Alexey Kodanevf870c1f2017-12-14 20:20:00 +03003819 if (max_mtu < ETH_MIN_MTU)
3820 max_mtu = ETH_MIN_MTU;
3821
3822 if (!changelink && !conf->mtu)
3823 dev->mtu = max_mtu;
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003824 }
3825
Matthias Schiffera9853432017-06-19 10:03:55 +02003826 if (dev->mtu > max_mtu)
3827 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003828
Jiri Bencb1be00a2015-09-24 13:50:02 +02003829 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3830 needed_headroom += VXLAN6_HEADROOM;
3831 else
3832 needed_headroom += VXLAN_HEADROOM;
3833 dev->needed_headroom = needed_headroom;
3834
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003835 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003836}
stephen hemmingerd3428942012-10-01 12:32:35 +00003837
Matthias Schiffera9853432017-06-19 10:03:55 +02003838static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003839 struct vxlan_config *conf, bool changelink,
3840 struct netlink_ext_ack *extack)
Matthias Schiffera9853432017-06-19 10:03:55 +02003841{
3842 struct vxlan_dev *vxlan = netdev_priv(dev);
3843 struct net_device *lowerdev;
3844 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003845
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003846 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
Matthias Schiffera9853432017-06-19 10:03:55 +02003847 if (ret)
3848 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003849
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003850 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003851
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003852 return 0;
3853}
3854
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003855static int __vxlan_dev_create(struct net *net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003856 struct vxlan_config *conf,
3857 struct netlink_ext_ack *extack)
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003858{
3859 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3860 struct vxlan_dev *vxlan = netdev_priv(dev);
Taehee Yoo0ce18222019-10-21 18:47:57 +00003861 struct net_device *remote_dev = NULL;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003862 struct vxlan_fdb *f = NULL;
Petr Machata6db92462018-12-18 13:16:00 +00003863 bool unregister = false;
Taehee Yoo0ce18222019-10-21 18:47:57 +00003864 struct vxlan_rdst *dst;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003865 int err;
3866
Taehee Yoo0ce18222019-10-21 18:47:57 +00003867 dst = &vxlan->default_dst;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003868 err = vxlan_dev_configure(net, dev, conf, false, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003869 if (err)
3870 return err;
3871
3872 dev->ethtool_ops = &vxlan_ethtool_ops;
3873
3874 /* create an fdb entry for a valid default destination */
Taehee Yoo0ce18222019-10-21 18:47:57 +00003875 if (!vxlan_addr_any(&dst->remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003876 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Taehee Yoo0ce18222019-10-21 18:47:57 +00003877 &dst->remote_ip,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003878 NUD_REACHABLE | NUD_PERMANENT,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003879 vxlan->cfg.dst_port,
Taehee Yoo0ce18222019-10-21 18:47:57 +00003880 dst->remote_vni,
3881 dst->remote_vni,
3882 dst->remote_ifindex,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07003883 NTF_SELF, 0, &f, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003884 if (err)
3885 return err;
3886 }
3887
3888 err = register_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003889 if (err)
3890 goto errout;
Petr Machata6db92462018-12-18 13:16:00 +00003891 unregister = true;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003892
Taehee Yoo0ce18222019-10-21 18:47:57 +00003893 if (dst->remote_ifindex) {
3894 remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
Zhang Changzhong832e0972020-12-02 17:58:42 +08003895 if (!remote_dev) {
3896 err = -ENODEV;
Taehee Yoo0ce18222019-10-21 18:47:57 +00003897 goto errout;
Zhang Changzhong832e0972020-12-02 17:58:42 +08003898 }
Taehee Yoo0ce18222019-10-21 18:47:57 +00003899
3900 err = netdev_upper_dev_link(remote_dev, dev, extack);
3901 if (err)
3902 goto errout;
3903 }
3904
Roopa Prabhu0241b832018-07-04 16:46:32 -07003905 err = rtnl_configure_link(dev, NULL);
Fabian Frederick2eabcb82020-09-25 15:16:59 +02003906 if (err < 0)
Taehee Yoo0ce18222019-10-21 18:47:57 +00003907 goto unlink;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003908
Petr Machata61f46fe2019-01-16 23:06:38 +00003909 if (f) {
Taehee Yoo0ce18222019-10-21 18:47:57 +00003910 vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
Taehee Yoo7c31e542019-06-28 14:07:25 +09003911
3912 /* notify default fdb entry */
Petr Machata61f46fe2019-01-16 23:06:38 +00003913 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
Petr Machata4c59b7d2019-01-16 23:06:54 +00003914 RTM_NEWNEIGH, true, extack);
Taehee Yoo7c31e542019-06-28 14:07:25 +09003915 if (err) {
3916 vxlan_fdb_destroy(vxlan, f, false, false);
Taehee Yoo0ce18222019-10-21 18:47:57 +00003917 if (remote_dev)
3918 netdev_upper_dev_unlink(remote_dev, dev);
Taehee Yoo7c31e542019-06-28 14:07:25 +09003919 goto unregister;
3920 }
Petr Machata61f46fe2019-01-16 23:06:38 +00003921 }
Roopa Prabhu0241b832018-07-04 16:46:32 -07003922
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003923 list_add(&vxlan->next, &vn->vxlan_list);
Taehee Yoo0ce18222019-10-21 18:47:57 +00003924 if (remote_dev)
3925 dst->remote_dev = remote_dev;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003926 return 0;
Taehee Yoo0ce18222019-10-21 18:47:57 +00003927unlink:
3928 if (remote_dev)
3929 netdev_upper_dev_unlink(remote_dev, dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003930errout:
Petr Machata6db92462018-12-18 13:16:00 +00003931 /* unregister_netdevice() destroys the default FDB entry with deletion
3932 * notification. But the addition notification was not sent yet, so
3933 * destroy the entry by hand here.
3934 */
Roopa Prabhu0241b832018-07-04 16:46:32 -07003935 if (f)
Taehee Yoo7c31e542019-06-28 14:07:25 +09003936 __vxlan_fdb_free(f);
3937unregister:
Petr Machata6db92462018-12-18 13:16:00 +00003938 if (unregister)
3939 unregister_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003940 return err;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003941}
3942
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003943/* Set/clear flags based on attribute */
3944static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3945 int attrtype, unsigned long mask, bool changelink,
3946 bool changelink_supported,
3947 struct netlink_ext_ack *extack)
3948{
3949 unsigned long flags;
3950
3951 if (!tb[attrtype])
3952 return 0;
3953
3954 if (changelink && !changelink_supported) {
3955 vxlan_flag_attr_error(attrtype, extack);
3956 return -EOPNOTSUPP;
3957 }
3958
3959 if (vxlan_policy[attrtype].type == NLA_FLAG)
3960 flags = conf->flags | mask;
3961 else if (nla_get_u8(tb[attrtype]))
3962 flags = conf->flags | mask;
3963 else
3964 flags = conf->flags & ~mask;
3965
3966 conf->flags = flags;
3967
3968 return 0;
3969}
3970
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003971static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3972 struct net_device *dev, struct vxlan_config *conf,
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003973 bool changelink, struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003974{
3975 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003976 int err = 0;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003977
3978 memset(conf, 0, sizeof(*conf));
3979
3980 /* if changelink operation, start with old existing cfg */
3981 if (changelink)
3982 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3983
3984 if (data[IFLA_VXLAN_ID]) {
3985 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3986
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003987 if (changelink && (vni != conf->vni)) {
3988 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003989 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003990 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003991 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3992 }
3993
3994 if (data[IFLA_VXLAN_GROUP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003995 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3996 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 +02003997 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003998 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003999
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004000 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02004001 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004002 } else if (data[IFLA_VXLAN_GROUP6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004003 if (!IS_ENABLED(CONFIG_IPV6)) {
4004 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004005 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004006 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004007
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004008 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
4009 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 +02004010 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004011 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02004012
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004013 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
4014 conf->remote_ip.sa.sa_family = AF_INET6;
4015 }
4016
4017 if (data[IFLA_VXLAN_LOCAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004018 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
4019 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 +02004020 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004021 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02004022
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004023 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
4024 conf->saddr.sa.sa_family = AF_INET;
4025 } else if (data[IFLA_VXLAN_LOCAL6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004026 if (!IS_ENABLED(CONFIG_IPV6)) {
4027 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004028 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004029 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004030
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004031 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
4032 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 +02004033 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004034 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02004035
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004036 /* TODO: respect scope id */
4037 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
4038 conf->saddr.sa.sa_family = AF_INET6;
4039 }
4040
4041 if (data[IFLA_VXLAN_LINK])
4042 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
4043
4044 if (data[IFLA_VXLAN_TOS])
4045 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
4046
4047 if (data[IFLA_VXLAN_TTL])
4048 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
4049
Hangbin Liu72f6d712018-04-17 14:11:28 +08004050 if (data[IFLA_VXLAN_TTL_INHERIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004051 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
4052 VXLAN_F_TTL_INHERIT, changelink, false,
4053 extack);
4054 if (err)
4055 return err;
4056
Hangbin Liu72f6d712018-04-17 14:11:28 +08004057 }
4058
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004059 if (data[IFLA_VXLAN_LABEL])
4060 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
4061 IPV6_FLOWLABEL_MASK;
4062
4063 if (data[IFLA_VXLAN_LEARNING]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004064 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
4065 VXLAN_F_LEARN, changelink, true,
4066 extack);
4067 if (err)
4068 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004069 } else if (!changelink) {
4070 /* default to learn on a new device */
4071 conf->flags |= VXLAN_F_LEARN;
4072 }
4073
Ido Schimmel40051c42018-11-21 08:02:40 +00004074 if (data[IFLA_VXLAN_AGEING])
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004075 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004076
4077 if (data[IFLA_VXLAN_PROXY]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004078 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
4079 VXLAN_F_PROXY, changelink, false,
4080 extack);
4081 if (err)
4082 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004083 }
4084
4085 if (data[IFLA_VXLAN_RSC]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004086 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
4087 VXLAN_F_RSC, changelink, false,
4088 extack);
4089 if (err)
4090 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004091 }
4092
4093 if (data[IFLA_VXLAN_L2MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004094 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
4095 VXLAN_F_L2MISS, changelink, false,
4096 extack);
4097 if (err)
4098 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004099 }
4100
4101 if (data[IFLA_VXLAN_L3MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004102 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
4103 VXLAN_F_L3MISS, changelink, false,
4104 extack);
4105 if (err)
4106 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004107 }
4108
4109 if (data[IFLA_VXLAN_LIMIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004110 if (changelink) {
4111 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
4112 "Cannot change limit");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004113 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004114 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004115 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
4116 }
4117
4118 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004119 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
4120 VXLAN_F_COLLECT_METADATA, changelink, false,
4121 extack);
4122 if (err)
4123 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004124 }
4125
4126 if (data[IFLA_VXLAN_PORT_RANGE]) {
4127 if (!changelink) {
4128 const struct ifla_vxlan_port_range *p
4129 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
4130 conf->port_min = ntohs(p->low);
4131 conf->port_max = ntohs(p->high);
4132 } else {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004133 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
4134 "Cannot change port range");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004135 return -EOPNOTSUPP;
4136 }
4137 }
4138
4139 if (data[IFLA_VXLAN_PORT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004140 if (changelink) {
4141 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
4142 "Cannot change port");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004143 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004144 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004145 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
4146 }
4147
4148 if (data[IFLA_VXLAN_UDP_CSUM]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004149 if (changelink) {
4150 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
4151 "Cannot change UDP_CSUM flag");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004152 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004153 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004154 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
4155 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
4156 }
4157
4158 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004159 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4160 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
4161 false, extack);
4162 if (err)
4163 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004164 }
4165
4166 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004167 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4168 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
4169 false, extack);
4170 if (err)
4171 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004172 }
4173
4174 if (data[IFLA_VXLAN_REMCSUM_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004175 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
4176 VXLAN_F_REMCSUM_TX, changelink, false,
4177 extack);
4178 if (err)
4179 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004180 }
4181
4182 if (data[IFLA_VXLAN_REMCSUM_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004183 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
4184 VXLAN_F_REMCSUM_RX, changelink, false,
4185 extack);
4186 if (err)
4187 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004188 }
4189
4190 if (data[IFLA_VXLAN_GBP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004191 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
4192 VXLAN_F_GBP, changelink, false, extack);
4193 if (err)
4194 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004195 }
4196
4197 if (data[IFLA_VXLAN_GPE]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004198 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
4199 VXLAN_F_GPE, changelink, false,
4200 extack);
4201 if (err)
4202 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004203 }
4204
4205 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004206 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
4207 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
4208 false, extack);
4209 if (err)
4210 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004211 }
4212
4213 if (tb[IFLA_MTU]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004214 if (changelink) {
4215 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
4216 "Cannot change mtu");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004217 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004218 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004219 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
4220 }
4221
Stefano Briviob4d306972018-11-08 12:19:16 +01004222 if (data[IFLA_VXLAN_DF])
4223 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
4224
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004225 return 0;
4226}
4227
4228static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02004229 struct nlattr *tb[], struct nlattr *data[],
4230 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004231{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004232 struct vxlan_config conf;
4233 int err;
4234
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004235 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004236 if (err)
4237 return err;
4238
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07004239 return __vxlan_dev_create(src_net, dev, &conf, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00004240}
4241
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004242static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02004243 struct nlattr *data[],
4244 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004245{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004246 struct vxlan_dev *vxlan = netdev_priv(dev);
Petr Machata8db9427d52019-01-16 23:06:39 +00004247 struct net_device *lowerdev;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004248 struct vxlan_config conf;
Taehee Yoo0ce18222019-10-21 18:47:57 +00004249 struct vxlan_rdst *dst;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004250 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004251
Taehee Yoo0ce18222019-10-21 18:47:57 +00004252 dst = &vxlan->default_dst;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004253 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004254 if (err)
4255 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07004256
Petr Machata8db9427d52019-01-16 23:06:39 +00004257 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
4258 vxlan, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004259 if (err)
4260 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004261
Taehee Yooc6761cf2019-10-30 08:15:12 +00004262 if (dst->remote_dev == lowerdev)
4263 lowerdev = NULL;
4264
Taehee Yoo0ce18222019-10-21 18:47:57 +00004265 err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
4266 extack);
4267 if (err)
4268 return err;
4269
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004270 /* handle default dst entry */
Petr Machata038a5a92019-01-16 23:06:41 +00004271 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
Litao jiaofe1e0712019-06-06 17:57:58 +08004272 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
4273
4274 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata038a5a92019-01-16 23:06:41 +00004275 if (!vxlan_addr_any(&conf.remote_ip)) {
Petr Machatace5e098f2018-12-18 13:16:02 +00004276 err = vxlan_fdb_update(vxlan, all_zeros_mac,
Petr Machata038a5a92019-01-16 23:06:41 +00004277 &conf.remote_ip,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004278 NUD_REACHABLE | NUD_PERMANENT,
Petr Machatace5e098f2018-12-18 13:16:02 +00004279 NLM_F_APPEND | NLM_F_CREATE,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004280 vxlan->cfg.dst_port,
Petr Machata038a5a92019-01-16 23:06:41 +00004281 conf.vni, conf.vni,
4282 conf.remote_ifindex,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07004283 NTF_SELF, 0, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004284 if (err) {
Litao jiaofe1e0712019-06-06 17:57:58 +08004285 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Taehee Yoo0ce18222019-10-21 18:47:57 +00004286 netdev_adjacent_change_abort(dst->remote_dev,
4287 lowerdev, dev);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004288 return err;
4289 }
4290 }
Petr Machata1cdc98c2019-01-16 23:06:43 +00004291 if (!vxlan_addr_any(&dst->remote_ip))
4292 __vxlan_fdb_delete(vxlan, all_zeros_mac,
4293 dst->remote_ip,
4294 vxlan->cfg.dst_port,
4295 dst->remote_vni,
4296 dst->remote_vni,
4297 dst->remote_ifindex,
4298 true);
Litao jiaofe1e0712019-06-06 17:57:58 +08004299 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004300 }
4301
Petr Machata038a5a92019-01-16 23:06:41 +00004302 if (conf.age_interval != vxlan->cfg.age_interval)
4303 mod_timer(&vxlan->age_timer, jiffies);
4304
Taehee Yoo0ce18222019-10-21 18:47:57 +00004305 netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
Cong Wang845e0eb2020-06-08 14:53:01 -07004306 if (lowerdev && lowerdev != dst->remote_dev)
Taehee Yoo0ce18222019-10-21 18:47:57 +00004307 dst->remote_dev = lowerdev;
Petr Machata038a5a92019-01-16 23:06:41 +00004308 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004309 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004310}
4311
stephen hemmingerd3428942012-10-01 12:32:35 +00004312static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4313{
4314 struct vxlan_dev *vxlan = netdev_priv(dev);
4315
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08004316 vxlan_flush(vxlan, true);
4317
stephen hemminger553675f2013-05-16 11:35:20 +00004318 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00004319 unregister_netdevice_queue(dev, head);
Taehee Yoo0ce18222019-10-21 18:47:57 +00004320 if (vxlan->default_dst.remote_dev)
4321 netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00004322}
4323
4324static size_t vxlan_get_size(const struct net_device *dev)
4325{
4326
4327 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08004328 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00004329 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08004330 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00004331 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
Hangbin Liu8fd78062018-09-26 10:35:42 +08004332 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
stephen hemmingerd3428942012-10-01 12:32:35 +00004333 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Stefano Briviob4d306972018-11-08 12:19:16 +01004334 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01004335 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00004336 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00004337 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
4338 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
4339 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
4340 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07004341 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00004342 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
4343 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00004344 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07004345 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4346 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4347 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4348 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08004349 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4350 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00004351 0;
4352}
4353
4354static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4355{
4356 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00004357 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00004358 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004359 .low = htons(vxlan->cfg.port_min),
4360 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00004361 };
stephen hemmingerd3428942012-10-01 12:32:35 +00004362
Jiri Benc54bfd872016-02-16 21:58:58 +01004363 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004364 goto nla_put_failure;
4365
Cong Wange4c7ed42013-08-31 13:44:33 +08004366 if (!vxlan_addr_any(&dst->remote_ip)) {
4367 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004368 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4369 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004370 goto nla_put_failure;
4371#if IS_ENABLED(CONFIG_IPV6)
4372 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004373 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4374 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004375 goto nla_put_failure;
4376#endif
4377 }
4378 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004379
Atzm Watanabec7995c42013-04-16 02:50:52 +00004380 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00004381 goto nla_put_failure;
4382
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004383 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4384 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004385 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004386 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004387 goto nla_put_failure;
4388#if IS_ENABLED(CONFIG_IPV6)
4389 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004390 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004391 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004392 goto nla_put_failure;
4393#endif
4394 }
4395 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004396
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004397 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
Hangbin Liu8fd78062018-09-26 10:35:42 +08004398 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4399 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004400 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Stefano Briviob4d306972018-11-08 12:19:16 +01004401 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01004402 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004403 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Niu Xilei98c81472019-12-30 17:52:22 +08004404 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004405 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Niu Xilei98c81472019-12-30 17:52:22 +08004406 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004407 nla_put_u8(skb, IFLA_VXLAN_RSC,
4408 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004409 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Niu Xilei98c81472019-12-30 17:52:22 +08004410 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004411 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Niu Xilei98c81472019-12-30 17:52:22 +08004412 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07004413 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004414 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004415 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4416 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4417 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004418 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Niu Xilei98c81472019-12-30 17:52:22 +08004419 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004420 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Niu Xilei98c81472019-12-30 17:52:22 +08004421 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004422 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Niu Xilei98c81472019-12-30 17:52:22 +08004423 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004424 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Niu Xilei98c81472019-12-30 17:52:22 +08004425 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004426 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Niu Xilei98c81472019-12-30 17:52:22 +08004427 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004428 goto nla_put_failure;
4429
stephen hemminger05f47d62012-10-09 20:35:50 +00004430 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4431 goto nla_put_failure;
4432
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004433 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01004434 nla_put_flag(skb, IFLA_VXLAN_GBP))
4435 goto nla_put_failure;
4436
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004437 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02004438 nla_put_flag(skb, IFLA_VXLAN_GPE))
4439 goto nla_put_failure;
4440
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004441 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08004442 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4443 goto nla_put_failure;
4444
stephen hemmingerd3428942012-10-01 12:32:35 +00004445 return 0;
4446
4447nla_put_failure:
4448 return -EMSGSIZE;
4449}
4450
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004451static struct net *vxlan_get_link_net(const struct net_device *dev)
4452{
4453 struct vxlan_dev *vxlan = netdev_priv(dev);
4454
4455 return vxlan->net;
4456}
4457
stephen hemmingerd3428942012-10-01 12:32:35 +00004458static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4459 .kind = "vxlan",
4460 .maxtype = IFLA_VXLAN_MAX,
4461 .policy = vxlan_policy,
4462 .priv_size = sizeof(struct vxlan_dev),
4463 .setup = vxlan_setup,
4464 .validate = vxlan_validate,
4465 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004466 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00004467 .dellink = vxlan_dellink,
4468 .get_size = vxlan_get_size,
4469 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004470 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004471};
4472
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004473struct net_device *vxlan_dev_create(struct net *net, const char *name,
4474 u8 name_assign_type,
4475 struct vxlan_config *conf)
4476{
4477 struct nlattr *tb[IFLA_MAX + 1];
4478 struct net_device *dev;
4479 int err;
4480
4481 memset(&tb, 0, sizeof(tb));
4482
4483 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08004484 &vxlan_link_ops, tb, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004485 if (IS_ERR(dev))
4486 return dev;
4487
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07004488 err = __vxlan_dev_create(net, dev, conf, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004489 if (err < 0) {
4490 free_netdev(dev);
4491 return ERR_PTR(err);
4492 }
4493
4494 err = rtnl_configure_link(dev, NULL);
4495 if (err < 0) {
4496 LIST_HEAD(list_kill);
4497
4498 vxlan_dellink(dev, &list_kill);
4499 unregister_netdevice_many(&list_kill);
4500 return ERR_PTR(err);
4501 }
4502
4503 return dev;
4504}
4505EXPORT_SYMBOL_GPL(vxlan_dev_create);
4506
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004507static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4508 struct net_device *dev)
4509{
4510 struct vxlan_dev *vxlan, *next;
4511 LIST_HEAD(list_kill);
4512
4513 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4514 struct vxlan_rdst *dst = &vxlan->default_dst;
4515
4516 /* In case we created vxlan device with carrier
4517 * and we loose the carrier due to module unload
4518 * we also need to remove vxlan device. In other
4519 * cases, it's not necessary and remote_ifindex
4520 * is 0 here, so no matches.
4521 */
4522 if (dst->remote_ifindex == dev->ifindex)
4523 vxlan_dellink(vxlan->dev, &list_kill);
4524 }
4525
4526 unregister_netdevice_many(&list_kill);
4527}
4528
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004529static int vxlan_netdevice_event(struct notifier_block *unused,
4530 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004531{
4532 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004533 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004534
Sabrina Dubroca04584952017-07-21 12:49:33 +02004535 if (event == NETDEV_UNREGISTER) {
Jakub Kicinskicc4e3832020-07-09 17:42:46 -07004536 if (!dev->udp_tunnel_nic_info)
4537 vxlan_offload_rx_ports(dev, false);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004538 vxlan_handle_lowerdev_unregister(vn, dev);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004539 } else if (event == NETDEV_REGISTER) {
Jakub Kicinskicc4e3832020-07-09 17:42:46 -07004540 if (!dev->udp_tunnel_nic_info)
4541 vxlan_offload_rx_ports(dev, true);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004542 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4543 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02004544 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004545 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004546
4547 return NOTIFY_DONE;
4548}
4549
4550static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004551 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004552};
4553
Petr Machata0efe1172018-10-17 08:53:26 +00004554static void
4555vxlan_fdb_offloaded_set(struct net_device *dev,
4556 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4557{
4558 struct vxlan_dev *vxlan = netdev_priv(dev);
4559 struct vxlan_rdst *rdst;
4560 struct vxlan_fdb *f;
Litao jiaofe1e0712019-06-06 17:57:58 +08004561 u32 hash_index;
Petr Machata0efe1172018-10-17 08:53:26 +00004562
Litao jiaofe1e0712019-06-06 17:57:58 +08004563 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4564
4565 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata0efe1172018-10-17 08:53:26 +00004566
4567 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4568 if (!f)
4569 goto out;
4570
4571 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4572 fdb_info->remote_port,
4573 fdb_info->remote_vni,
4574 fdb_info->remote_ifindex);
4575 if (!rdst)
4576 goto out;
4577
4578 rdst->offloaded = fdb_info->offloaded;
4579
4580out:
Litao jiaofe1e0712019-06-06 17:57:58 +08004581 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata0efe1172018-10-17 08:53:26 +00004582}
4583
Petr Machata5728ae02018-11-21 08:02:39 +00004584static int
4585vxlan_fdb_external_learn_add(struct net_device *dev,
4586 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4587{
4588 struct vxlan_dev *vxlan = netdev_priv(dev);
Petr Machata4c59b7d2019-01-16 23:06:54 +00004589 struct netlink_ext_ack *extack;
Litao jiaofe1e0712019-06-06 17:57:58 +08004590 u32 hash_index;
Petr Machata5728ae02018-11-21 08:02:39 +00004591 int err;
4592
Litao jiaofe1e0712019-06-06 17:57:58 +08004593 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
Petr Machata4c59b7d2019-01-16 23:06:54 +00004594 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4595
Litao jiaofe1e0712019-06-06 17:57:58 +08004596 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004597 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4598 NUD_REACHABLE,
4599 NLM_F_CREATE | NLM_F_REPLACE,
4600 fdb_info->remote_port,
4601 fdb_info->vni,
4602 fdb_info->remote_vni,
4603 fdb_info->remote_ifindex,
4604 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07004605 0, false, extack);
Litao jiaofe1e0712019-06-06 17:57:58 +08004606 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004607
4608 return err;
4609}
4610
4611static int
4612vxlan_fdb_external_learn_del(struct net_device *dev,
4613 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4614{
4615 struct vxlan_dev *vxlan = netdev_priv(dev);
4616 struct vxlan_fdb *f;
Litao jiaofe1e0712019-06-06 17:57:58 +08004617 u32 hash_index;
Petr Machata5728ae02018-11-21 08:02:39 +00004618 int err = 0;
4619
Litao jiaofe1e0712019-06-06 17:57:58 +08004620 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4621 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004622
4623 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4624 if (!f)
4625 err = -ENOENT;
4626 else if (f->flags & NTF_EXT_LEARNED)
4627 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4628 fdb_info->remote_ip,
4629 fdb_info->remote_port,
4630 fdb_info->vni,
4631 fdb_info->remote_vni,
4632 fdb_info->remote_ifindex,
4633 false);
4634
Litao jiaofe1e0712019-06-06 17:57:58 +08004635 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004636
4637 return err;
4638}
4639
Petr Machata0efe1172018-10-17 08:53:26 +00004640static int vxlan_switchdev_event(struct notifier_block *unused,
4641 unsigned long event, void *ptr)
4642{
4643 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Petr Machata5728ae02018-11-21 08:02:39 +00004644 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4645 int err = 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004646
4647 switch (event) {
4648 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4649 vxlan_fdb_offloaded_set(dev, ptr);
4650 break;
Petr Machata5728ae02018-11-21 08:02:39 +00004651 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4652 fdb_info = ptr;
4653 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4654 if (err) {
4655 err = notifier_from_errno(err);
4656 break;
4657 }
4658 fdb_info->offloaded = true;
4659 vxlan_fdb_offloaded_set(dev, fdb_info);
4660 break;
4661 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4662 fdb_info = ptr;
4663 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4664 if (err) {
4665 err = notifier_from_errno(err);
4666 break;
4667 }
4668 fdb_info->offloaded = false;
4669 vxlan_fdb_offloaded_set(dev, fdb_info);
4670 break;
Petr Machata0efe1172018-10-17 08:53:26 +00004671 }
4672
Petr Machata5728ae02018-11-21 08:02:39 +00004673 return err;
Petr Machata0efe1172018-10-17 08:53:26 +00004674}
4675
4676static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4677 .notifier_call = vxlan_switchdev_event,
4678};
4679
Roopa Prabhu79472fe2020-05-28 22:12:36 -07004680static void vxlan_fdb_nh_flush(struct nexthop *nh)
4681{
4682 struct vxlan_fdb *fdb;
4683 struct vxlan_dev *vxlan;
4684 u32 hash_index;
4685
4686 rcu_read_lock();
4687 list_for_each_entry_rcu(fdb, &nh->fdb_list, nh_list) {
4688 vxlan = rcu_dereference(fdb->vdev);
4689 WARN_ON(!vxlan);
4690 hash_index = fdb_head_index(vxlan, fdb->eth_addr,
4691 vxlan->default_dst.remote_vni);
4692 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4693 if (!hlist_unhashed(&fdb->hlist))
4694 vxlan_fdb_destroy(vxlan, fdb, false, false);
4695 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4696 }
4697 rcu_read_unlock();
4698}
4699
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004700static int vxlan_nexthop_event(struct notifier_block *nb,
4701 unsigned long event, void *ptr)
4702{
4703 struct nexthop *nh = ptr;
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004704
4705 if (!nh || event != NEXTHOP_EVENT_DEL)
4706 return NOTIFY_DONE;
4707
Roopa Prabhu79472fe2020-05-28 22:12:36 -07004708 vxlan_fdb_nh_flush(nh);
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004709
4710 return NOTIFY_DONE;
4711}
4712
4713static struct notifier_block vxlan_nexthop_notifier_block __read_mostly = {
4714 .notifier_call = vxlan_nexthop_event,
4715};
4716
stephen hemmingerd3428942012-10-01 12:32:35 +00004717static __net_init int vxlan_init_net(struct net *net)
4718{
4719 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00004720 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00004721
stephen hemminger553675f2013-05-16 11:35:20 +00004722 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07004723 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00004724
stephen hemminger553675f2013-05-16 11:35:20 +00004725 for (h = 0; h < PORT_HASH_SIZE; ++h)
4726 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00004727
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004728 return register_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004729}
4730
Haishuang Yan57b61122017-12-16 17:54:49 +08004731static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004732{
4733 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4734 struct vxlan_dev *vxlan, *next;
4735 struct net_device *dev, *aux;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004736
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004737 for_each_netdev_safe(net, dev, aux)
4738 if (dev->rtnl_link_ops == &vxlan_link_ops)
Haishuang Yan57b61122017-12-16 17:54:49 +08004739 unregister_netdevice_queue(dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004740
4741 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4742 /* If vxlan->dev is in the same netns, it has already been added
4743 * to the list by the previous loop.
4744 */
Zhiqiang Liucc4807b2019-03-16 17:02:54 +08004745 if (!net_eq(dev_net(vxlan->dev), net))
Haishuang Yan57b61122017-12-16 17:54:49 +08004746 unregister_netdevice_queue(vxlan->dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004747 }
4748
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004749}
4750
Haishuang Yan57b61122017-12-16 17:54:49 +08004751static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4752{
4753 struct net *net;
4754 LIST_HEAD(list);
Taehee Yoo3bbb8572021-02-21 15:45:52 +00004755 unsigned int h;
Haishuang Yan57b61122017-12-16 17:54:49 +08004756
4757 rtnl_lock();
4758 list_for_each_entry(net, net_list, exit_list)
Roopa Prabhuc7cdbe22020-05-21 22:26:16 -07004759 unregister_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
4760 list_for_each_entry(net, net_list, exit_list)
Haishuang Yan57b61122017-12-16 17:54:49 +08004761 vxlan_destroy_tunnels(net, &list);
4762
4763 unregister_netdevice_many(&list);
4764 rtnl_unlock();
Taehee Yoo3bbb8572021-02-21 15:45:52 +00004765
4766 list_for_each_entry(net, net_list, exit_list) {
4767 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4768
4769 for (h = 0; h < PORT_HASH_SIZE; ++h)
4770 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
4771 }
Haishuang Yan57b61122017-12-16 17:54:49 +08004772}
4773
stephen hemmingerd3428942012-10-01 12:32:35 +00004774static struct pernet_operations vxlan_net_ops = {
4775 .init = vxlan_init_net,
Haishuang Yan57b61122017-12-16 17:54:49 +08004776 .exit_batch = vxlan_exit_batch_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004777 .id = &vxlan_net_id,
4778 .size = sizeof(struct vxlan_net),
4779};
4780
4781static int __init vxlan_init_module(void)
4782{
4783 int rc;
4784
4785 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4786
Daniel Borkmann783c1462014-01-22 21:07:53 +01004787 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004788 if (rc)
4789 goto out1;
4790
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004791 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004792 if (rc)
4793 goto out2;
4794
Petr Machata0efe1172018-10-17 08:53:26 +00004795 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004796 if (rc)
4797 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00004798
Petr Machata0efe1172018-10-17 08:53:26 +00004799 rc = rtnl_link_register(&vxlan_link_ops);
4800 if (rc)
4801 goto out4;
4802
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004803 return 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004804out4:
4805 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004806out3:
4807 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004808out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01004809 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004810out1:
4811 return rc;
4812}
Cong Wang7332a132013-05-27 22:35:53 +00004813late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00004814
4815static void __exit vxlan_cleanup_module(void)
4816{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07004817 rtnl_link_unregister(&vxlan_link_ops);
Petr Machata0efe1172018-10-17 08:53:26 +00004818 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004819 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004820 unregister_pernet_subsys(&vxlan_net_ops);
4821 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00004822}
4823module_exit(vxlan_cleanup_module);
4824
4825MODULE_LICENSE("GPL");
4826MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004827MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08004828MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00004829MODULE_ALIAS_RTNL_LINK("vxlan");