blob: 754e00240eea5e207be30422792dce762efcf63d [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;
stephen hemmingerd3428942012-10-01 12:32:35 +000084};
85
Petr Machata45598c12018-11-21 08:02:36 +000086#define NTF_VXLAN_ADDED_BY_USER 0x100
87
stephen hemmingerd3428942012-10-01 12:32:35 +000088/* salt for hash table */
89static u32 vxlan_salt __read_mostly;
90
Thomas Grafee122c72015-07-21 10:43:58 +020091static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
92{
Thomas Grafe7030872015-07-21 10:44:01 +020093 return vs->flags & VXLAN_F_COLLECT_METADATA ||
94 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020095}
96
Cong Wange4c7ed42013-08-31 13:44:33 +080097#if IS_ENABLED(CONFIG_IPV6)
98static inline
99bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
100{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200101 if (a->sa.sa_family != b->sa.sa_family)
102 return false;
103 if (a->sa.sa_family == AF_INET6)
104 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
105 else
106 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800107}
108
Cong Wange4c7ed42013-08-31 13:44:33 +0800109static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
110{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200111 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200112 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200113 ip->sa.sa_family = AF_INET6;
114 return 0;
115 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200116 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200117 ip->sa.sa_family = AF_INET;
118 return 0;
119 } else {
120 return -EAFNOSUPPORT;
121 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800122}
123
124static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200125 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800126{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200127 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200128 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200129 else
Jiri Benc930345e2015-03-29 16:59:25 +0200130 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800131}
132
133#else /* !CONFIG_IPV6 */
134
135static inline
136bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
137{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200138 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800139}
140
Cong Wange4c7ed42013-08-31 13:44:33 +0800141static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
142{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200143 if (nla_len(nla) >= sizeof(struct in6_addr)) {
144 return -EAFNOSUPPORT;
145 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200146 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200147 ip->sa.sa_family = AF_INET;
148 return 0;
149 } else {
150 return -EAFNOSUPPORT;
151 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800152}
153
154static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200155 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800156{
Jiri Benc930345e2015-03-29 16:59:25 +0200157 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800158}
159#endif
160
stephen hemminger553675f2013-05-16 11:35:20 +0000161/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100162static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000163{
Jiri Benc54bfd872016-02-16 21:58:58 +0100164 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000165}
166
167/* Socket hash table head */
168static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000169{
170 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
171
stephen hemminger553675f2013-05-16 11:35:20 +0000172 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
173}
174
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700175/* First remote destination for a forwarding entry.
176 * Guaranteed to be non-NULL because remotes are never deleted.
177 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700178static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700179{
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700180 if (rcu_access_pointer(fdb->nh))
181 return NULL;
stephen hemminger5ca54612013-08-04 17:17:39 -0700182 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
183}
184
185static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
186{
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700187 if (rcu_access_pointer(fdb->nh))
188 return NULL;
stephen hemminger5ca54612013-08-04 17:17:39 -0700189 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700190}
191
Thomas Grafac5132d2015-01-15 03:53:56 +0100192/* Find VXLAN socket based on network namespace, address family and UDP port
193 * and enabled unshareable flags.
194 */
195static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100196 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000197{
198 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800199
200 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000201
202 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200203 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200204 vxlan_get_sk_family(vs) == family &&
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100205 vs->flags == flags &&
206 vs->sock->sk->sk_bound_dev_if == ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000207 return vs;
208 }
209 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000210}
211
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200212static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
213 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000214{
Jiri Benc69e76662017-07-02 19:00:57 +0200215 struct vxlan_dev_node *node;
stephen hemmingerd3428942012-10-01 12:32:35 +0000216
Jiri Bencb9167b22016-02-16 21:59:03 +0100217 /* For flow based devices, map all packets to VNI 0 */
218 if (vs->flags & VXLAN_F_COLLECT_METADATA)
219 vni = 0;
220
Jiri Benc69e76662017-07-02 19:00:57 +0200221 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
222 if (node->vxlan->default_dst.remote_vni != vni)
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200223 continue;
224
225 if (IS_ENABLED(CONFIG_IPV6)) {
Jiri Benc69e76662017-07-02 19:00:57 +0200226 const struct vxlan_config *cfg = &node->vxlan->cfg;
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200227
228 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
229 cfg->remote_ifindex != ifindex)
230 continue;
231 }
232
Jiri Benc69e76662017-07-02 19:00:57 +0200233 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000234 }
235
236 return NULL;
237}
238
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700239/* Look up VNI in a per net namespace table */
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200240static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
241 __be32 vni, sa_family_t family,
242 __be16 port, u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700243{
244 struct vxlan_sock *vs;
245
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100246 vs = vxlan_find_sock(net, family, port, flags, ifindex);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700247 if (!vs)
248 return NULL;
249
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200250 return vxlan_vs_find_vni(vs, ifindex, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700251}
252
stephen hemmingerd3428942012-10-01 12:32:35 +0000253/* Fill in neighbour message in skbuff. */
254static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700255 const struct vxlan_fdb *fdb,
256 u32 portid, u32 seq, int type, unsigned int flags,
257 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000258{
259 unsigned long now = jiffies;
260 struct nda_cacheinfo ci;
David Stevense4f67ad2012-11-20 02:50:14 +0000261 bool send_ip, send_eth;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700262 struct nlmsghdr *nlh;
263 struct nexthop *nh;
264 struct ndmsg *ndm;
stephen hemmingerd3428942012-10-01 12:32:35 +0000265
266 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
267 if (nlh == NULL)
268 return -EMSGSIZE;
269
270 ndm = nlmsg_data(nlh);
271 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000272
273 send_eth = send_ip = true;
274
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700275 nh = rcu_dereference_rtnl(fdb->nh);
David Stevense4f67ad2012-11-20 02:50:14 +0000276 if (type == RTM_GETNEIGH) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700277 if (rdst) {
278 send_ip = !vxlan_addr_any(&rdst->remote_ip);
279 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
280 } else if (nh) {
281 ndm->ndm_family = nexthop_get_family(nh);
282 }
David Stevense4f67ad2012-11-20 02:50:14 +0000283 send_eth = !is_zero_ether_addr(fdb->eth_addr);
284 } else
285 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000286 ndm->ndm_state = fdb->state;
287 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000288 ndm->ndm_flags = fdb->flags;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700289 if (rdst && rdst->offloaded)
Petr Machata0efe1172018-10-17 08:53:26 +0000290 ndm->ndm_flags |= NTF_OFFLOADED;
Jun Zhao545469f2014-07-26 00:38:59 +0800291 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000292
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100293 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100294 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700295 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100296 goto nla_put_failure;
297
David Stevense4f67ad2012-11-20 02:50:14 +0000298 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000299 goto nla_put_failure;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700300 if (nh) {
301 if (nla_put_u32(skb, NDA_NH_ID, nh->id))
302 goto nla_put_failure;
303 } else if (rdst) {
304 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST,
305 &rdst->remote_ip))
306 goto nla_put_failure;
stephen hemmingerd3428942012-10-01 12:32:35 +0000307
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700308 if (rdst->remote_port &&
309 rdst->remote_port != vxlan->cfg.dst_port &&
310 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
311 goto nla_put_failure;
312 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
313 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
314 goto nla_put_failure;
315 if (rdst->remote_ifindex &&
316 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
317 goto nla_put_failure;
318 }
David Stevens66817122013-03-15 04:35:51 +0000319
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200320 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800321 nla_put_u32(skb, NDA_SRC_VNI,
322 be32_to_cpu(fdb->vni)))
323 goto nla_put_failure;
stephen hemmingerd3428942012-10-01 12:32:35 +0000324
325 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
326 ci.ndm_confirmed = 0;
327 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
328 ci.ndm_refcnt = 0;
329
330 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
331 goto nla_put_failure;
332
Johannes Berg053c0952015-01-16 22:09:00 +0100333 nlmsg_end(skb, nlh);
334 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000335
336nla_put_failure:
337 nlmsg_cancel(skb, nlh);
338 return -EMSGSIZE;
339}
340
341static inline size_t vxlan_nlmsg_size(void)
342{
343 return NLMSG_ALIGN(sizeof(struct ndmsg))
344 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800345 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000346 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000347 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
348 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100349 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000350 + nla_total_size(sizeof(struct nda_cacheinfo));
351}
352
Petr Machata9a997352018-10-17 08:53:22 +0000353static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
354 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000355{
356 struct net *net = dev_net(vxlan->dev);
357 struct sk_buff *skb;
358 int err = -ENOBUFS;
359
360 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
361 if (skb == NULL)
362 goto errout;
363
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200364 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000365 if (err < 0) {
366 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
367 WARN_ON(err == -EMSGSIZE);
368 kfree_skb(skb);
369 goto errout;
370 }
371
372 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
373 return;
374errout:
375 if (err < 0)
376 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
377}
378
Petr Machataff23b912018-12-07 19:55:03 +0000379static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
380 const struct vxlan_fdb *fdb,
381 const struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000382 struct netlink_ext_ack *extack,
Petr Machataff23b912018-12-07 19:55:03 +0000383 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
384{
385 fdb_info->info.dev = vxlan->dev;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000386 fdb_info->info.extack = extack;
Petr Machataff23b912018-12-07 19:55:03 +0000387 fdb_info->remote_ip = rd->remote_ip;
388 fdb_info->remote_port = rd->remote_port;
389 fdb_info->remote_vni = rd->remote_vni;
390 fdb_info->remote_ifindex = rd->remote_ifindex;
391 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
392 fdb_info->vni = fdb->vni;
393 fdb_info->offloaded = rd->offloaded;
394 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
395}
396
Petr Machata61f46fe2019-01-16 23:06:38 +0000397static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
398 struct vxlan_fdb *fdb,
399 struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000400 bool adding,
401 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000402{
403 struct switchdev_notifier_vxlan_fdb_info info;
404 enum switchdev_notifier_type notifier_type;
Petr Machata61f46fe2019-01-16 23:06:38 +0000405 int ret;
Petr Machata9a997352018-10-17 08:53:22 +0000406
407 if (WARN_ON(!rd))
Petr Machata61f46fe2019-01-16 23:06:38 +0000408 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000409
410 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
411 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000412 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
Petr Machata61f46fe2019-01-16 23:06:38 +0000413 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
Petr Machata66859872019-01-16 23:06:56 +0000414 &info.info, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000415 return notifier_to_errno(ret);
Petr Machata9a997352018-10-17 08:53:22 +0000416}
417
Petr Machata61f46fe2019-01-16 23:06:38 +0000418static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000419 struct vxlan_rdst *rd, int type, bool swdev_notify,
420 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000421{
Petr Machata61f46fe2019-01-16 23:06:38 +0000422 int err;
423
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700424 if (swdev_notify && rd) {
Petr Machata0e6160f2018-11-21 08:02:35 +0000425 switch (type) {
426 case RTM_NEWNEIGH:
Petr Machata61f46fe2019-01-16 23:06:38 +0000427 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000428 true, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000429 if (err)
430 return err;
Petr Machata0e6160f2018-11-21 08:02:35 +0000431 break;
432 case RTM_DELNEIGH:
433 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000434 false, extack);
Petr Machata0e6160f2018-11-21 08:02:35 +0000435 break;
436 }
Petr Machata9a997352018-10-17 08:53:22 +0000437 }
438
439 __vxlan_fdb_notify(vxlan, fdb, rd, type);
Petr Machata61f46fe2019-01-16 23:06:38 +0000440 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000441}
442
Cong Wange4c7ed42013-08-31 13:44:33 +0800443static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000444{
445 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700446 struct vxlan_fdb f = {
447 .state = NUD_STALE,
448 };
449 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800450 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100451 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700452 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700453
Petr Machata4c59b7d2019-01-16 23:06:54 +0000454 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000455}
456
457static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
458{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700459 struct vxlan_fdb f = {
460 .state = NUD_STALE,
461 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200462 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000463
David Stevense4f67ad2012-11-20 02:50:14 +0000464 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
465
Petr Machata4c59b7d2019-01-16 23:06:54 +0000466 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000467}
468
stephen hemmingerd3428942012-10-01 12:32:35 +0000469/* Hash Ethernet address */
470static u32 eth_hash(const unsigned char *addr)
471{
472 u64 value = get_unaligned((u64 *)addr);
473
474 /* only want 6 bytes */
475#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000476 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000477#else
478 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000479#endif
480 return hash_64(value, FDB_HASH_BITS);
481}
482
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800483static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
484{
485 /* use 1 byte of OUI and 3 bytes of NIC */
486 u32 key = get_unaligned((u32 *)(addr + 2));
487
488 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
489}
490
Litao jiaofe1e0712019-06-06 17:57:58 +0800491static u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
492{
493 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
494 return eth_vni_hash(mac, vni);
495 else
496 return eth_hash(mac);
497}
498
stephen hemmingerd3428942012-10-01 12:32:35 +0000499/* Hash chain to use given mac address */
500static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800501 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000502{
Litao jiaofe1e0712019-06-06 17:57:58 +0800503 return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000504}
505
506/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000507static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800508 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000509{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800510 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000511 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000512
Sasha Levinb67bfe02013-02-27 17:06:00 -0800513 hlist_for_each_entry_rcu(f, head, hlist) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800514 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200515 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800516 if (vni == f->vni)
517 return f;
518 } else {
519 return f;
520 }
521 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000522 }
523
524 return NULL;
525}
526
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000527static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800528 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000529{
530 struct vxlan_fdb *f;
531
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800532 f = __vxlan_find_mac(vxlan, mac, vni);
Li RongQing016f3d12018-08-29 11:52:10 +0800533 if (f && f->used != jiffies)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000534 f->used = jiffies;
535
536 return f;
537}
538
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300539/* caller should hold vxlan->hash_lock */
540static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800541 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100542 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300543{
544 struct vxlan_rdst *rd;
545
546 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800547 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300548 rd->remote_port == port &&
549 rd->remote_vni == vni &&
550 rd->remote_ifindex == ifindex)
551 return rd;
552 }
553
554 return NULL;
555}
556
Petr Machata1941f1d2018-10-17 08:53:24 +0000557int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
558 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
559{
560 struct vxlan_dev *vxlan = netdev_priv(dev);
561 u8 eth_addr[ETH_ALEN + 2] = { 0 };
562 struct vxlan_rdst *rdst;
563 struct vxlan_fdb *f;
564 int rc = 0;
565
566 if (is_multicast_ether_addr(mac) ||
567 is_zero_ether_addr(mac))
568 return -EINVAL;
569
570 ether_addr_copy(eth_addr, mac);
571
572 rcu_read_lock();
573
574 f = __vxlan_find_mac(vxlan, eth_addr, vni);
575 if (!f) {
576 rc = -ENOENT;
577 goto out;
578 }
579
580 rdst = first_remote_rcu(f);
Petr Machata4c59b7d2019-01-16 23:06:54 +0000581 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
Petr Machata1941f1d2018-10-17 08:53:24 +0000582
583out:
584 rcu_read_unlock();
585 return rc;
586}
587EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
588
Petr Machata4f89f5b2018-12-07 19:55:04 +0000589static int vxlan_fdb_notify_one(struct notifier_block *nb,
590 const struct vxlan_dev *vxlan,
591 const struct vxlan_fdb *f,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000592 const struct vxlan_rdst *rdst,
593 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000594{
595 struct switchdev_notifier_vxlan_fdb_info fdb_info;
596 int rc;
597
Petr Machata4c59b7d2019-01-16 23:06:54 +0000598 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000599 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
600 &fdb_info);
601 return notifier_to_errno(rc);
602}
603
604int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000605 struct notifier_block *nb,
606 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000607{
608 struct vxlan_dev *vxlan;
609 struct vxlan_rdst *rdst;
610 struct vxlan_fdb *f;
611 unsigned int h;
612 int rc = 0;
613
614 if (!netif_is_vxlan(dev))
615 return -EINVAL;
616 vxlan = netdev_priv(dev);
617
Petr Machata4f89f5b2018-12-07 19:55:04 +0000618 for (h = 0; h < FDB_HASH_SIZE; ++h) {
Litao jiaofe1e0712019-06-06 17:57:58 +0800619 spin_lock_bh(&vxlan->hash_lock[h]);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000620 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
621 if (f->vni == vni) {
622 list_for_each_entry(rdst, &f->remotes, list) {
623 rc = vxlan_fdb_notify_one(nb, vxlan,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000624 f, rdst,
625 extack);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000626 if (rc)
Litao jiaofe1e0712019-06-06 17:57:58 +0800627 goto unlock;
Petr Machata4f89f5b2018-12-07 19:55:04 +0000628 }
629 }
630 }
Litao jiaofe1e0712019-06-06 17:57:58 +0800631 spin_unlock_bh(&vxlan->hash_lock[h]);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000632 }
Litao jiaofe1e0712019-06-06 17:57:58 +0800633 return 0;
Petr Machata4f89f5b2018-12-07 19:55:04 +0000634
Litao jiaofe1e0712019-06-06 17:57:58 +0800635unlock:
636 spin_unlock_bh(&vxlan->hash_lock[h]);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000637 return rc;
638}
639EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
640
Petr Machatae5ff4b12018-12-07 19:55:06 +0000641void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
642{
643 struct vxlan_dev *vxlan;
644 struct vxlan_rdst *rdst;
645 struct vxlan_fdb *f;
646 unsigned int h;
647
648 if (!netif_is_vxlan(dev))
649 return;
650 vxlan = netdev_priv(dev);
651
Petr Machatae5ff4b12018-12-07 19:55:06 +0000652 for (h = 0; h < FDB_HASH_SIZE; ++h) {
Litao jiaofe1e0712019-06-06 17:57:58 +0800653 spin_lock_bh(&vxlan->hash_lock[h]);
Petr Machatae5ff4b12018-12-07 19:55:06 +0000654 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
655 if (f->vni == vni)
656 list_for_each_entry(rdst, &f->remotes, list)
657 rdst->offloaded = false;
Litao jiaofe1e0712019-06-06 17:57:58 +0800658 spin_unlock_bh(&vxlan->hash_lock[h]);
Petr Machatae5ff4b12018-12-07 19:55:06 +0000659 }
Litao jiaofe1e0712019-06-06 17:57:58 +0800660
Petr Machatae5ff4b12018-12-07 19:55:06 +0000661}
662EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
663
Thomas Richter906dc182013-07-19 17:20:07 +0200664/* Replace destination of unicast mac */
665static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100666 union vxlan_addr *ip, __be16 port, __be32 vni,
Petr Machataccdfd4f2019-01-16 23:06:34 +0000667 __u32 ifindex, struct vxlan_rdst *oldrd)
Thomas Richter906dc182013-07-19 17:20:07 +0200668{
669 struct vxlan_rdst *rd;
670
671 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
672 if (rd)
673 return 0;
674
675 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
676 if (!rd)
677 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100678
Petr Machataccdfd4f2019-01-16 23:06:34 +0000679 *oldrd = *rd;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100680 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800681 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200682 rd->remote_port = port;
683 rd->remote_vni = vni;
684 rd->remote_ifindex = ifindex;
Petr Machata6ad0b5a2018-12-18 13:15:59 +0000685 rd->offloaded = false;
Thomas Richter906dc182013-07-19 17:20:07 +0200686 return 1;
687}
688
David Stevens66817122013-03-15 04:35:51 +0000689/* Add/update destinations for multicast */
690static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100691 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200692 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000693{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700694 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000695
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300696 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
697 if (rd)
698 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700699
David Stevens66817122013-03-15 04:35:51 +0000700 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
701 if (rd == NULL)
702 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100703
704 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
705 kfree(rd);
706 return -ENOBUFS;
707 }
708
Cong Wange4c7ed42013-08-31 13:44:33 +0800709 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000710 rd->remote_port = port;
Petr Machata0efe1172018-10-17 08:53:26 +0000711 rd->offloaded = false;
David Stevens66817122013-03-15 04:35:51 +0000712 rd->remote_vni = vni;
713 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700714
715 list_add_tail_rcu(&rd->list, &f->remotes);
716
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200717 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000718 return 1;
719}
720
Tom Herbertdfd86452015-01-12 17:00:38 -0800721static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
722 unsigned int off,
723 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100724 __be32 vni_field,
725 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800726 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800727{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700728 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800729
730 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700731 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800732
733 if (!NAPI_GRO_CB(skb)->csum_valid)
734 return NULL;
735
Jiri Benc54bfd872016-02-16 21:58:58 +0100736 start = vxlan_rco_start(vni_field);
737 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800738
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700739 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
740 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800741
742 skb->remcsum_offload = 1;
743
744 return vh;
745}
746
David Millerd4546c22018-06-24 14:13:49 +0900747static struct sk_buff *vxlan_gro_receive(struct sock *sk,
748 struct list_head *head,
749 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200750{
David Millerd4546c22018-06-24 14:13:49 +0900751 struct sk_buff *pp = NULL;
752 struct sk_buff *p;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200753 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800754 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200755 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700756 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100757 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800758 struct gro_remcsum grc;
759
760 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200761
762 off_vx = skb_gro_offset(skb);
763 hlen = off_vx + sizeof(*vh);
764 vh = skb_gro_header_fast(skb, off_vx);
765 if (skb_gro_header_hard(skb, hlen)) {
766 vh = skb_gro_header_slow(skb, hlen, off_vx);
767 if (unlikely(!vh))
768 goto out;
769 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200770
Tom Herbertdfd86452015-01-12 17:00:38 -0800771 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
772
Jiri Benc54bfd872016-02-16 21:58:58 +0100773 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800774
775 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
776 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100777 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800778 !!(vs->flags &
779 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800780
781 if (!vh)
782 goto out;
783 }
784
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700785 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
786
David Millerd4546c22018-06-24 14:13:49 +0900787 list_for_each_entry(p, head, list) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200788 if (!NAPI_GRO_CB(p)->same_flow)
789 continue;
790
791 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100792 if (vh->vx_flags != vh2->vx_flags ||
793 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200794 NAPI_GRO_CB(p)->same_flow = 0;
795 continue;
796 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200797 }
798
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200799 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800800 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200801
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200802out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200803 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200804
805 return pp;
806}
807
Tom Herbert5602c482016-04-05 08:22:53 -0700808static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200809{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700810 /* Sets 'skb->inner_mac_header' since we are always called with
811 * 'skb->encapsulation' set.
812 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800813 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200814}
815
Guillaume Nault1d7a5522019-10-29 21:57:10 +0100816static struct vxlan_fdb *vxlan_fdb_alloc(const u8 *mac, __u16 state,
Petr Machata45598c12018-11-21 08:02:36 +0000817 __be32 src_vni, __u16 ndm_flags)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700818{
819 struct vxlan_fdb *f;
820
821 f = kmalloc(sizeof(*f), GFP_ATOMIC);
822 if (!f)
823 return NULL;
824 f->state = state;
825 f->flags = ndm_flags;
826 f->updated = f->used = jiffies;
827 f->vni = src_vni;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700828 f->nh = NULL;
829 INIT_LIST_HEAD(&f->nh_list);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700830 INIT_LIST_HEAD(&f->remotes);
831 memcpy(f->eth_addr, mac, ETH_ALEN);
832
833 return f;
834}
835
Taehee Yoo7c31e542019-06-28 14:07:25 +0900836static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
837 __be32 src_vni, struct vxlan_fdb *f)
838{
839 ++vxlan->addrcnt;
840 hlist_add_head_rcu(&f->hlist,
841 vxlan_fdb_head(vxlan, mac, src_vni));
842}
843
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700844static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
845 u32 nhid, struct netlink_ext_ack *extack)
846{
847 struct nexthop *old_nh = rtnl_dereference(fdb->nh);
848 struct nh_group *nhg;
849 struct nexthop *nh;
850 int err = -EINVAL;
851
852 if (old_nh && old_nh->id == nhid)
853 return 0;
854
855 nh = nexthop_find_by_id(vxlan->net, nhid);
856 if (!nh) {
857 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
858 goto err_inval;
859 }
860
861 if (nh) {
862 if (!nexthop_get(nh)) {
863 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
864 nh = NULL;
865 goto err_inval;
866 }
867 if (!nh->is_fdb_nh) {
868 NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop");
869 goto err_inval;
870 }
871
872 if (!nh->is_group || !nh->nh_grp->mpath) {
873 NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group");
874 goto err_inval;
875 }
876
877 /* check nexthop group family */
878 nhg = rtnl_dereference(nh->nh_grp);
879 switch (vxlan->default_dst.remote_ip.sa.sa_family) {
880 case AF_INET:
881 if (!nhg->has_v4) {
882 err = -EAFNOSUPPORT;
883 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
884 goto err_inval;
885 }
886 break;
887 case AF_INET6:
888 if (nhg->has_v4) {
889 err = -EAFNOSUPPORT;
890 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
891 goto err_inval;
892 }
893 }
894 }
895
896 if (old_nh) {
897 list_del_rcu(&fdb->nh_list);
898 nexthop_put(old_nh);
899 }
900 rcu_assign_pointer(fdb->nh, nh);
901 list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list);
902 return 1;
903
904err_inval:
905 if (nh)
906 nexthop_put(nh);
907 return err;
908}
909
stephen hemmingerd3428942012-10-01 12:32:35 +0000910static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800911 const u8 *mac, union vxlan_addr *ip,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700912 __u16 state, __be16 port, __be32 src_vni,
Petr Machata45598c12018-11-21 08:02:36 +0000913 __be32 vni, __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700914 u32 nhid, struct vxlan_fdb **fdb,
915 struct netlink_ext_ack *extack)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700916{
917 struct vxlan_rdst *rd = NULL;
918 struct vxlan_fdb *f;
919 int rc;
920
921 if (vxlan->cfg.addrmax &&
922 vxlan->addrcnt >= vxlan->cfg.addrmax)
923 return -ENOSPC;
924
925 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
Guillaume Nault1d7a5522019-10-29 21:57:10 +0100926 f = vxlan_fdb_alloc(mac, state, src_vni, ndm_flags);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700927 if (!f)
928 return -ENOMEM;
929
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700930 if (nhid)
931 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
932 else
933 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
934 if (rc < 0)
935 goto errout;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700936
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700937 *fdb = f;
938
939 return 0;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700940
941errout:
942 kfree(f);
943 return rc;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700944}
945
Taehee Yoo7c31e542019-06-28 14:07:25 +0900946static void __vxlan_fdb_free(struct vxlan_fdb *f)
Petr Machatac2b200e2019-01-16 23:06:30 +0000947{
Petr Machatac2b200e2019-01-16 23:06:30 +0000948 struct vxlan_rdst *rd, *nd;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700949 struct nexthop *nh;
950
951 nh = rcu_dereference_raw(f->nh);
952 if (nh) {
953 rcu_assign_pointer(f->nh, NULL);
954 list_del_rcu(&f->nh_list);
955 nexthop_put(nh);
956 }
Petr Machatac2b200e2019-01-16 23:06:30 +0000957
958 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
959 dst_cache_destroy(&rd->dst_cache);
960 kfree(rd);
961 }
962 kfree(f);
963}
964
Taehee Yoo7c31e542019-06-28 14:07:25 +0900965static void vxlan_fdb_free(struct rcu_head *head)
966{
967 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
968
969 __vxlan_fdb_free(f);
970}
971
Petr Machatac2b200e2019-01-16 23:06:30 +0000972static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
973 bool do_notify, bool swdev_notify)
974{
975 struct vxlan_rdst *rd;
976
977 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
978
979 --vxlan->addrcnt;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700980 if (do_notify) {
981 if (rcu_access_pointer(f->nh))
982 vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000983 swdev_notify, NULL);
Roopa Prabhu1274e1c2020-05-21 22:26:14 -0700984 else
985 list_for_each_entry(rd, &f->remotes, list)
986 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
987 swdev_notify, NULL);
988 }
Petr Machatac2b200e2019-01-16 23:06:30 +0000989
990 hlist_del_rcu(&f->hlist);
991 call_rcu(&f->rcu, vxlan_fdb_free);
992}
993
Petr Machatafc4aa1c2019-02-07 12:18:02 +0000994static void vxlan_dst_free(struct rcu_head *head)
995{
996 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
997
998 dst_cache_destroy(&rd->dst_cache);
999 kfree(rd);
1000}
1001
Petr Machataa76d1ca2019-01-16 23:06:32 +00001002static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
1003 union vxlan_addr *ip,
1004 __u16 state, __u16 flags,
1005 __be16 port, __be32 vni,
1006 __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001007 struct vxlan_fdb *f, u32 nhid,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001008 bool swdev_notify,
1009 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +00001010{
1011 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1012 struct vxlan_rdst *rd = NULL;
Petr Machataccdfd4f2019-01-16 23:06:34 +00001013 struct vxlan_rdst oldrd;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001014 int notify = 0;
Petr Machata61f46fe2019-01-16 23:06:38 +00001015 int rc = 0;
1016 int err;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001017
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001018 if (nhid && !rcu_access_pointer(f->nh)) {
1019 NL_SET_ERR_MSG(extack,
1020 "Cannot replace an existing non nexthop fdb with a nexthop");
1021 return -EOPNOTSUPP;
1022 }
1023
1024 if (nhid && (flags & NLM_F_APPEND)) {
1025 NL_SET_ERR_MSG(extack,
1026 "Cannot append to a nexthop fdb");
1027 return -EOPNOTSUPP;
1028 }
1029
Petr Machataa76d1ca2019-01-16 23:06:32 +00001030 /* Do not allow an externally learned entry to take over an entry added
1031 * by the user.
1032 */
1033 if (!(fdb_flags & NTF_EXT_LEARNED) ||
1034 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
1035 if (f->state != state) {
1036 f->state = state;
1037 f->updated = jiffies;
1038 notify = 1;
1039 }
1040 if (f->flags != fdb_flags) {
1041 f->flags = fdb_flags;
1042 f->updated = jiffies;
1043 notify = 1;
1044 }
1045 }
1046
1047 if ((flags & NLM_F_REPLACE)) {
1048 /* Only change unicasts */
1049 if (!(is_multicast_ether_addr(f->eth_addr) ||
1050 is_zero_ether_addr(f->eth_addr))) {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001051 if (nhid) {
1052 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
1053 if (rc < 0)
1054 return rc;
1055 } else {
1056 rc = vxlan_fdb_replace(f, ip, port, vni,
1057 ifindex, &oldrd);
1058 }
Petr Machataa76d1ca2019-01-16 23:06:32 +00001059 notify |= rc;
1060 } else {
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001061 NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries");
Petr Machataa76d1ca2019-01-16 23:06:32 +00001062 return -EOPNOTSUPP;
1063 }
1064 }
1065 if ((flags & NLM_F_APPEND) &&
1066 (is_multicast_ether_addr(f->eth_addr) ||
1067 is_zero_ether_addr(f->eth_addr))) {
1068 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
1069
1070 if (rc < 0)
1071 return rc;
1072 notify |= rc;
1073 }
1074
1075 if (ndm_flags & NTF_USE)
1076 f->used = jiffies;
1077
1078 if (notify) {
1079 if (rd == NULL)
1080 rd = first_remote_rtnl(f);
1081
Petr Machata61f46fe2019-01-16 23:06:38 +00001082 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001083 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +00001084 if (err)
1085 goto err_notify;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001086 }
1087
1088 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +00001089
1090err_notify:
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001091 if (nhid)
1092 return err;
Petr Machata61f46fe2019-01-16 23:06:38 +00001093 if ((flags & NLM_F_REPLACE) && rc)
1094 *rd = oldrd;
Petr Machatafc4aa1c2019-02-07 12:18:02 +00001095 else if ((flags & NLM_F_APPEND) && rc) {
Petr Machata61f46fe2019-01-16 23:06:38 +00001096 list_del_rcu(&rd->list);
Petr Machatafc4aa1c2019-02-07 12:18:02 +00001097 call_rcu(&rd->rcu, vxlan_dst_free);
1098 }
Petr Machata61f46fe2019-01-16 23:06:38 +00001099 return err;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001100}
1101
1102static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
1103 const u8 *mac, union vxlan_addr *ip,
1104 __u16 state, __u16 flags,
1105 __be16 port, __be32 src_vni, __be32 vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001106 __u32 ifindex, __u16 ndm_flags, u32 nhid,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001107 bool swdev_notify,
1108 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +00001109{
1110 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1111 struct vxlan_fdb *f;
1112 int rc;
1113
1114 /* Disallow replace to add a multicast entry */
1115 if ((flags & NLM_F_REPLACE) &&
1116 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
1117 return -EOPNOTSUPP;
1118
1119 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
1120 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001121 vni, ifindex, fdb_flags, nhid, &f, extack);
Petr Machataa76d1ca2019-01-16 23:06:32 +00001122 if (rc < 0)
1123 return rc;
1124
Taehee Yoo7c31e542019-06-28 14:07:25 +09001125 vxlan_fdb_insert(vxlan, mac, src_vni, f);
Petr Machata61f46fe2019-01-16 23:06:38 +00001126 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001127 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +00001128 if (rc)
1129 goto err_notify;
1130
Petr Machataa76d1ca2019-01-16 23:06:32 +00001131 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +00001132
1133err_notify:
1134 vxlan_fdb_destroy(vxlan, f, false, false);
1135 return rc;
Petr Machataa76d1ca2019-01-16 23:06:32 +00001136}
1137
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001138/* Add new entry to forwarding table -- assumes lock held */
1139static int vxlan_fdb_update(struct vxlan_dev *vxlan,
1140 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +00001141 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001142 __be16 port, __be32 src_vni, __be32 vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001143 __u32 ifindex, __u16 ndm_flags, u32 nhid,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001144 bool swdev_notify,
1145 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001146{
1147 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001148
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001149 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001150 if (f) {
1151 if (flags & NLM_F_EXCL) {
1152 netdev_dbg(vxlan->dev,
1153 "lost race to create %pM\n", mac);
1154 return -EEXIST;
1155 }
Petr Machata0ec566a2018-11-21 08:02:37 +00001156
Petr Machataa76d1ca2019-01-16 23:06:32 +00001157 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1158 vni, ifindex, ndm_flags, f,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001159 nhid, swdev_notify, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001160 } else {
1161 if (!(flags & NLM_F_CREATE))
1162 return -ENOENT;
1163
Petr Machataa76d1ca2019-01-16 23:06:32 +00001164 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1165 port, src_vni, vni, ifindex,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001166 ndm_flags, nhid, swdev_notify,
1167 extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001168 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001169}
1170
Lance Richardson35cf2842017-05-29 13:25:57 -04001171static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +00001172 struct vxlan_rdst *rd, bool swdev_notify)
Lance Richardson35cf2842017-05-29 13:25:57 -04001173{
1174 list_del_rcu(&rd->list);
Petr Machata4c59b7d2019-01-16 23:06:54 +00001175 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
Lance Richardson35cf2842017-05-29 13:25:57 -04001176 call_rcu(&rd->rcu, vxlan_dst_free);
1177}
1178
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001179static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001180 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001181 __be32 *vni, u32 *ifindex, u32 *nhid)
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001182{
1183 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001184 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001185
1186 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001187 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1188 if (err)
1189 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001190 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08001191 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
Niu Xilei98c81472019-12-30 17:52:22 +08001192
Cong Wange4c7ed42013-08-31 13:44:33 +08001193 if (remote->sa.sa_family == AF_INET) {
1194 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1195 ip->sa.sa_family = AF_INET;
1196#if IS_ENABLED(CONFIG_IPV6)
1197 } else {
1198 ip->sin6.sin6_addr = in6addr_any;
1199 ip->sa.sa_family = AF_INET6;
1200#endif
1201 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001202 }
1203
1204 if (tb[NDA_PORT]) {
1205 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1206 return -EINVAL;
1207 *port = nla_get_be16(tb[NDA_PORT]);
1208 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001209 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001210 }
1211
1212 if (tb[NDA_VNI]) {
1213 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1214 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +01001215 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001216 } else {
1217 *vni = vxlan->default_dst.remote_vni;
1218 }
1219
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001220 if (tb[NDA_SRC_VNI]) {
1221 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1222 return -EINVAL;
1223 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1224 } else {
1225 *src_vni = vxlan->default_dst.remote_vni;
1226 }
1227
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001228 if (tb[NDA_IFINDEX]) {
1229 struct net_device *tdev;
1230
1231 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1232 return -EINVAL;
1233 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +08001234 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001235 if (!tdev)
1236 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001237 } else {
1238 *ifindex = 0;
1239 }
1240
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001241 if (tb[NDA_NH_ID])
1242 *nhid = nla_get_u32(tb[NDA_NH_ID]);
1243 else
1244 *nhid = 0;
1245
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001246 return 0;
1247}
1248
stephen hemmingerd3428942012-10-01 12:32:35 +00001249/* Add static entry (via netlink) */
1250static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1251 struct net_device *dev,
Petr Machata87b09842019-01-16 23:06:50 +00001252 const unsigned char *addr, u16 vid, u16 flags,
1253 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001254{
1255 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001256 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +08001257 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +00001258 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001259 __be32 src_vni, vni;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001260 u32 ifindex, nhid;
Litao jiaofe1e0712019-06-06 17:57:58 +08001261 u32 hash_index;
stephen hemmingerd3428942012-10-01 12:32:35 +00001262 int err;
1263
1264 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1265 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1266 ndm->ndm_state);
1267 return -EINVAL;
1268 }
1269
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001270 if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID]))
stephen hemmingerd3428942012-10-01 12:32:35 +00001271 return -EINVAL;
1272
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001273 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1274 &nhid);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001275 if (err)
1276 return err;
David Stevens66817122013-03-15 04:35:51 +00001277
Mike Rapoport5933a7b2014-04-01 09:23:01 +03001278 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1279 return -EAFNOSUPPORT;
1280
Litao jiaofe1e0712019-06-06 17:57:58 +08001281 hash_index = fdb_head_index(vxlan, addr, src_vni);
1282 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001283 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
Petr Machata45598c12018-11-21 08:02:36 +00001284 port, src_vni, vni, ifindex,
1285 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001286 nhid, true, extack);
Litao jiaofe1e0712019-06-06 17:57:58 +08001287 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001288
1289 return err;
1290}
1291
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001292static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1293 const unsigned char *addr, union vxlan_addr ip,
Xin Longfc39c382017-11-26 21:19:05 +08001294 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001295 u32 ifindex, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +00001296{
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001297 struct vxlan_rdst *rd = NULL;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001298 struct vxlan_fdb *f;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001299 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001300
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001301 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001302 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001303 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001304
Cong Wange4c7ed42013-08-31 13:44:33 +08001305 if (!vxlan_addr_any(&ip)) {
1306 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001307 if (!rd)
1308 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +00001309 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001310
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001311 /* remove a destination if it's not the only one on the list,
1312 * otherwise destroy the fdb entry
1313 */
1314 if (rd && !list_is_singular(&f->remotes)) {
Petr Machata0e6160f2018-11-21 08:02:35 +00001315 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001316 goto out;
1317 }
1318
Petr Machata0e6160f2018-11-21 08:02:35 +00001319 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001320
1321out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001322 return 0;
1323}
1324
1325/* Delete entry (via netlink) */
1326static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1327 struct net_device *dev,
1328 const unsigned char *addr, u16 vid)
1329{
1330 struct vxlan_dev *vxlan = netdev_priv(dev);
1331 union vxlan_addr ip;
1332 __be32 src_vni, vni;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001333 u32 ifindex, nhid;
Litao jiaofe1e0712019-06-06 17:57:58 +08001334 u32 hash_index;
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001335 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001336 int err;
1337
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001338 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1339 &nhid);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001340 if (err)
1341 return err;
1342
Litao jiaofe1e0712019-06-06 17:57:58 +08001343 hash_index = fdb_head_index(vxlan, addr, src_vni);
1344 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata0e6160f2018-11-21 08:02:35 +00001345 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1346 true);
Litao jiaofe1e0712019-06-06 17:57:58 +08001347 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001348
1349 return err;
1350}
1351
1352/* Dump forwarding table */
1353static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04001354 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07001355 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +00001356{
1357 struct vxlan_dev *vxlan = netdev_priv(dev);
1358 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -07001359 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001360
1361 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1362 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001363
Sasha Levinb67bfe02013-02-27 17:06:00 -08001364 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +00001365 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +00001366
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001367 if (rcu_access_pointer(f->nh)) {
1368 err = vxlan_fdb_info(skb, vxlan, f,
1369 NETLINK_CB(cb->skb).portid,
1370 cb->nlh->nlmsg_seq,
1371 RTM_NEWNEIGH,
1372 NLM_F_MULTI, NULL);
1373 if (err < 0)
1374 goto out;
1375 continue;
1376 }
1377
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001378 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07001379 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001380 goto skip;
1381
David Stevens66817122013-03-15 04:35:51 +00001382 err = vxlan_fdb_info(skb, vxlan, f,
1383 NETLINK_CB(cb->skb).portid,
1384 cb->nlh->nlmsg_seq,
1385 RTM_NEWNEIGH,
1386 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -07001387 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001388 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001389skip:
Roopa Prabhud2976532016-08-30 21:56:45 -07001390 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001391 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001392 }
1393 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001394out:
Roopa Prabhud2976532016-08-30 21:56:45 -07001395 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001396}
1397
Roopa Prabhu474c3c82018-12-15 22:35:10 -08001398static int vxlan_fdb_get(struct sk_buff *skb,
1399 struct nlattr *tb[],
1400 struct net_device *dev,
1401 const unsigned char *addr,
1402 u16 vid, u32 portid, u32 seq,
1403 struct netlink_ext_ack *extack)
1404{
1405 struct vxlan_dev *vxlan = netdev_priv(dev);
1406 struct vxlan_fdb *f;
1407 __be32 vni;
1408 int err;
1409
1410 if (tb[NDA_VNI])
1411 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1412 else
1413 vni = vxlan->default_dst.remote_vni;
1414
1415 rcu_read_lock();
1416
1417 f = __vxlan_find_mac(vxlan, addr, vni);
1418 if (!f) {
1419 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1420 err = -ENOENT;
1421 goto errout;
1422 }
1423
1424 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1425 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1426errout:
1427 rcu_read_unlock();
1428 return err;
1429}
1430
stephen hemmingerd3428942012-10-01 12:32:35 +00001431/* Watch incoming packets to learn mapping between Ethernet address
1432 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +09001433 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +00001434 */
stephen hemminger26a41ae62013-06-17 12:09:58 -07001435static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001436 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001437 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +00001438{
1439 struct vxlan_dev *vxlan = netdev_priv(dev);
1440 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001441 u32 ifindex = 0;
1442
1443#if IS_ENABLED(CONFIG_IPV6)
1444 if (src_ip->sa.sa_family == AF_INET6 &&
1445 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1446 ifindex = src_ifindex;
1447#endif
stephen hemmingerd3428942012-10-01 12:32:35 +00001448
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001449 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001450 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001451 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001452
Matthias Schiffer87613de2017-06-19 10:03:59 +02001453 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1454 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001455 return false;
1456
1457 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -07001458 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001459 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001460
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001461 /* Don't override an fdb with nexthop with a learnt entry */
1462 if (rcu_access_pointer(f->nh))
1463 return true;
1464
stephen hemmingerd3428942012-10-01 12:32:35 +00001465 if (net_ratelimit())
1466 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001467 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001468 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001469
Cong Wange4c7ed42013-08-31 13:44:33 +08001470 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001471 f->updated = jiffies;
Petr Machata4c59b7d2019-01-16 23:06:54 +00001472 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
stephen hemmingerd3428942012-10-01 12:32:35 +00001473 } else {
Litao jiaofe1e0712019-06-06 17:57:58 +08001474 u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
1475
stephen hemmingerd3428942012-10-01 12:32:35 +00001476 /* learned new entry */
Litao jiaofe1e0712019-06-06 17:57:58 +08001477 spin_lock(&vxlan->hash_lock[hash_index]);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001478
1479 /* close off race between vxlan_flush and incoming packets */
1480 if (netif_running(dev))
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001481 vxlan_fdb_update(vxlan, src_mac, src_ip,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001482 NUD_REACHABLE,
1483 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001484 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001485 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001486 vxlan->default_dst.remote_vni,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07001487 ifindex, NTF_SELF, 0, true, NULL);
Litao jiaofe1e0712019-06-06 17:57:58 +08001488 spin_unlock(&vxlan->hash_lock[hash_index]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001489 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001490
1491 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001492}
1493
stephen hemmingerd3428942012-10-01 12:32:35 +00001494/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001495static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001496{
stephen hemminger553675f2013-05-16 11:35:20 +00001497 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001498 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001499#if IS_ENABLED(CONFIG_IPV6)
1500 struct vxlan_sock *sock6;
1501#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001502 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001503
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001504 sock4 = rtnl_dereference(dev->vn4_sock);
1505
Gao feng95ab0992013-12-10 16:37:33 +08001506 /* The vxlan_sock is only used by dev, leaving group has
1507 * no effect on other vxlan devices.
1508 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001509 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001510 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001511#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001512 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001513 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001514 return false;
1515#endif
Gao feng95ab0992013-12-10 16:37:33 +08001516
stephen hemminger553675f2013-05-16 11:35:20 +00001517 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001518 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001519 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001520
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001521 if (family == AF_INET &&
1522 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001523 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001524#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001525 if (family == AF_INET6 &&
1526 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001527 continue;
1528#endif
Gao feng95ab0992013-12-10 16:37:33 +08001529
1530 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1531 &dev->default_dst.remote_ip))
1532 continue;
1533
1534 if (vxlan->default_dst.remote_ifindex !=
1535 dev->default_dst.remote_ifindex)
1536 continue;
1537
1538 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001539 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001540
1541 return false;
1542}
1543
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001544static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001545{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001546 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001547
Jiri Bencb1be00a2015-09-24 13:50:02 +02001548 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001549 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001550 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001551 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001552
Jiri Bencb1be00a2015-09-24 13:50:02 +02001553 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001554 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001555 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001556 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001557 (vs->flags & VXLAN_F_GPE) ?
1558 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001559 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001560 spin_unlock(&vn->sock_lock);
1561
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001562 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001563}
1564
Jiri Bencb1be00a2015-09-24 13:50:02 +02001565static void vxlan_sock_release(struct vxlan_dev *vxlan)
1566{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001567 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001568#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001569 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1570
Mark Bloch57d88182017-06-07 14:36:58 +03001571 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001572#endif
1573
Mark Bloch57d88182017-06-07 14:36:58 +03001574 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001575 synchronize_net();
1576
Mark Blocha53cb292017-06-02 03:24:08 +03001577 vxlan_vs_del_dev(vxlan);
1578
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001579 if (__vxlan_sock_release_prep(sock4)) {
1580 udp_tunnel_sock_release(sock4->sock);
1581 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001582 }
1583
1584#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001585 if (__vxlan_sock_release_prep(sock6)) {
1586 udp_tunnel_sock_release(sock6->sock);
1587 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001588 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001589#endif
1590}
1591
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001592/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001593 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001594 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001595static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001596{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001597 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001598 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1599 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001600 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001601
Cong Wange4c7ed42013-08-31 13:44:33 +08001602 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001603 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001604 struct ip_mreqn mreq = {
1605 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1606 .imr_ifindex = ifindex,
1607 };
1608
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001609 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001610 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001611 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001612 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001613#if IS_ENABLED(CONFIG_IPV6)
1614 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001615 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1616
1617 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001618 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001619 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1620 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001621 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001622#endif
1623 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001624
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001625 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001626}
1627
1628/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001629static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001630{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001631 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001632 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1633 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001634 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001635
Cong Wange4c7ed42013-08-31 13:44:33 +08001636 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001637 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001638 struct ip_mreqn mreq = {
1639 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1640 .imr_ifindex = ifindex,
1641 };
1642
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001643 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001644 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001645 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001646 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001647#if IS_ENABLED(CONFIG_IPV6)
1648 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001649 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1650
1651 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001652 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001653 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1654 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001655 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001656#endif
1657 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001658
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001659 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001660}
1661
Jiri Bencf14eceb2016-02-16 21:59:01 +01001662static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1663 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001664{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001665 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001666
Jiri Bencf14eceb2016-02-16 21:59:01 +01001667 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1668 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001669
Jiri Bencf14eceb2016-02-16 21:59:01 +01001670 start = vxlan_rco_start(unparsed->vx_vni);
1671 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001672
Jiri Benc7d34fa72016-03-21 17:50:05 +01001673 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001674 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001675
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001676 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1677 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001678out:
1679 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1680 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001681 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001682}
1683
Jiri Bencf14eceb2016-02-16 21:59:01 +01001684static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001685 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001686 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001687{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001688 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001689 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001690
Jiri Bencf14eceb2016-02-16 21:59:01 +01001691 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1692 goto out;
1693
Jiri Benc3288af02016-02-16 21:59:00 +01001694 md->gbp = ntohs(gbp->policy_id);
1695
Jiri Benc10a5af22016-02-23 18:02:59 +01001696 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001697 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001698 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001699 tun_dst->u.tun_info.options_len = sizeof(*md);
1700 }
Jiri Benc3288af02016-02-16 21:59:00 +01001701 if (gbp->dont_learn)
1702 md->gbp |= VXLAN_GBP_DONT_LEARN;
1703
1704 if (gbp->policy_applied)
1705 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001706
Jiri Benc64f87d32016-02-23 18:02:55 +01001707 /* In flow-based mode, GBP is carried in dst_metadata */
1708 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1709 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001710out:
1711 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001712}
1713
Jiri Bence1e53142016-04-05 14:47:13 +02001714static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001715 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001716 struct sk_buff *skb, u32 vxflags)
1717{
1718 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1719
1720 /* Need to have Next Protocol set for interfaces in GPE mode. */
1721 if (!gpe->np_applied)
1722 return false;
1723 /* "The initial version is 0. If a receiver does not support the
1724 * version indicated it MUST drop the packet.
1725 */
1726 if (gpe->version != 0)
1727 return false;
1728 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1729 * processing MUST occur." However, we don't implement OAM
1730 * processing, thus drop the packet.
1731 */
1732 if (gpe->oam_flag)
1733 return false;
1734
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001735 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1736 if (!*protocol)
Jiri Bence1e53142016-04-05 14:47:13 +02001737 return false;
Jiri Bence1e53142016-04-05 14:47:13 +02001738
1739 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1740 return true;
1741}
1742
Jiri Benc1ab016e2016-02-23 18:02:56 +01001743static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1744 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001745 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001746{
Thomas Graf614732e2015-07-21 10:44:06 +02001747 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001748 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001749
Thomas Graf614732e2015-07-21 10:44:06 +02001750 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001751 skb->protocol = eth_type_trans(skb, vxlan->dev);
1752 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1753
1754 /* Ignore packet loops (and multicast echo) */
1755 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001756 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001757
Jiri Benc760c6802016-02-23 18:02:57 +01001758 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001759 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001760 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001761 saddr.sa.sa_family = AF_INET;
1762#if IS_ENABLED(CONFIG_IPV6)
1763 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001764 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001765 saddr.sa.sa_family = AF_INET6;
1766#endif
1767 }
1768
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001769 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001770 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001771 return false;
1772
1773 return true;
1774}
1775
Jiri Benc760c6802016-02-23 18:02:57 +01001776static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1777 struct sk_buff *skb)
1778{
1779 int err = 0;
1780
1781 if (vxlan_get_sk_family(vs) == AF_INET)
1782 err = IP_ECN_decapsulate(oiph, skb);
1783#if IS_ENABLED(CONFIG_IPV6)
1784 else
1785 err = IP6_ECN_decapsulate(oiph, skb);
1786#endif
1787
1788 if (unlikely(err) && log_ecn_error) {
1789 if (vxlan_get_sk_family(vs) == AF_INET)
1790 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1791 &((struct iphdr *)oiph)->saddr,
1792 ((struct iphdr *)oiph)->tos);
1793 else
1794 net_info_ratelimited("non-ECT from %pI6\n",
1795 &((struct ipv6hdr *)oiph)->saddr);
1796 }
1797 return err <= 1;
1798}
1799
stephen hemmingerd3428942012-10-01 12:32:35 +00001800/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001801static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001802{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001803 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001804 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001805 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001806 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001807 struct vxlan_metadata _md;
1808 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001809 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001810 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001811 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001812 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001813
Jiri Bence1e53142016-04-05 14:47:13 +02001814 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001815 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001816 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001817
Jiri Bencf14eceb2016-02-16 21:59:01 +01001818 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001819 /* VNI flag always required to be set */
1820 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1821 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1822 ntohl(vxlan_hdr(skb)->vx_flags),
1823 ntohl(vxlan_hdr(skb)->vx_vni));
1824 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001825 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001826 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001827 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1828 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001829
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001830 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001831 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001832 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001833
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001834 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1835
Matthias Schiffer49f810f2017-06-19 10:04:00 +02001836 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001837 if (!vxlan)
1838 goto drop;
1839
Jiri Bence1e53142016-04-05 14:47:13 +02001840 /* For backwards compatibility, only allow reserved fields to be
1841 * used by VXLAN extensions if explicitly requested.
1842 */
1843 if (vs->flags & VXLAN_F_GPE) {
1844 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1845 goto drop;
1846 raw_proto = true;
1847 }
1848
1849 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1850 !net_eq(vxlan->net, dev_net(vxlan->dev))))
Niu Xilei98c81472019-12-30 17:52:22 +08001851 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001852
Thomas Grafee122c72015-07-21 10:43:58 +02001853 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001854 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001855
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001856 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001857 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001858
Thomas Grafee122c72015-07-21 10:43:58 +02001859 if (!tun_dst)
1860 goto drop;
1861
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001862 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001863
1864 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001865 } else {
1866 memset(md, 0, sizeof(*md));
1867 }
1868
Jiri Bencf14eceb2016-02-16 21:59:01 +01001869 if (vs->flags & VXLAN_F_REMCSUM_RX)
1870 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1871 goto drop;
1872 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001873 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001874 /* Note that GBP and GPE can never be active together. This is
1875 * ensured in vxlan_dev_configure.
1876 */
Thomas Graf35114942015-01-15 03:53:55 +01001877
Jiri Bencf14eceb2016-02-16 21:59:01 +01001878 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001879 /* If there are any unprocessed flags remaining treat
1880 * this as a malformed packet. This behavior diverges from
1881 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1882 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001883 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001884 * is more robust and provides a little more security in
1885 * adding extensions to VXLAN.
1886 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001887 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001888 }
1889
Jiri Bence1e53142016-04-05 14:47:13 +02001890 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001891 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001892 goto drop;
1893 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001894 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001895 skb->dev = vxlan->dev;
1896 skb->pkt_type = PACKET_HOST;
1897 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001898
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001899 oiph = skb_network_header(skb);
1900 skb_reset_network_header(skb);
1901
1902 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1903 ++vxlan->dev->stats.rx_frame_errors;
1904 ++vxlan->dev->stats.rx_errors;
1905 goto drop;
1906 }
1907
Eric Dumazet59cbf562019-03-10 10:36:40 -07001908 rcu_read_lock();
1909
1910 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1911 rcu_read_unlock();
1912 atomic_long_inc(&vxlan->dev->rx_dropped);
1913 goto drop;
1914 }
1915
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001916 stats = this_cpu_ptr(vxlan->dev->tstats);
1917 u64_stats_update_begin(&stats->syncp);
1918 stats->rx_packets++;
1919 stats->rx_bytes += skb->len;
1920 u64_stats_update_end(&stats->syncp);
1921
1922 gro_cells_receive(&vxlan->gro_cells, skb);
Eric Dumazet59cbf562019-03-10 10:36:40 -07001923
1924 rcu_read_unlock();
1925
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001926 return 0;
1927
1928drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001929 /* Consume bad packet */
1930 kfree_skb(skb);
1931 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001932}
1933
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001934/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1935static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1936{
1937 struct vxlan_dev *vxlan;
1938 struct vxlan_sock *vs;
1939 struct vxlanhdr *hdr;
1940 __be32 vni;
1941
Stefano Brivio8399a692019-06-11 00:27:05 +02001942 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001943 return -EINVAL;
1944
1945 hdr = vxlan_hdr(skb);
1946
1947 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1948 return -EINVAL;
1949
1950 vs = rcu_dereference_sk_user_data(sk);
1951 if (!vs)
1952 return -ENOENT;
1953
1954 vni = vxlan_vni(hdr->vx_vni);
1955 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1956 if (!vxlan)
1957 return -ENOENT;
1958
1959 return 0;
1960}
1961
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001962static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001963{
1964 struct vxlan_dev *vxlan = netdev_priv(dev);
1965 struct arphdr *parp;
1966 u8 *arpptr, *sha;
1967 __be32 sip, tip;
1968 struct neighbour *n;
1969
1970 if (dev->flags & IFF_NOARP)
1971 goto out;
1972
1973 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1974 dev->stats.tx_dropped++;
1975 goto out;
1976 }
1977 parp = arp_hdr(skb);
1978
1979 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1980 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1981 parp->ar_pro != htons(ETH_P_IP) ||
1982 parp->ar_op != htons(ARPOP_REQUEST) ||
1983 parp->ar_hln != dev->addr_len ||
1984 parp->ar_pln != 4)
1985 goto out;
1986 arpptr = (u8 *)parp + sizeof(struct arphdr);
1987 sha = arpptr;
1988 arpptr += dev->addr_len; /* sha */
1989 memcpy(&sip, arpptr, sizeof(sip));
1990 arpptr += sizeof(sip);
1991 arpptr += dev->addr_len; /* tha */
1992 memcpy(&tip, arpptr, sizeof(tip));
1993
1994 if (ipv4_is_loopback(tip) ||
1995 ipv4_is_multicast(tip))
1996 goto out;
1997
1998 n = neigh_lookup(&arp_tbl, &tip, dev);
1999
2000 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00002001 struct vxlan_fdb *f;
2002 struct sk_buff *reply;
2003
2004 if (!(n->nud_state & NUD_CONNECTED)) {
2005 neigh_release(n);
2006 goto out;
2007 }
2008
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002009 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08002010 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00002011 /* bridge-local neighbor */
2012 neigh_release(n);
2013 goto out;
2014 }
2015
2016 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
2017 n->ha, sha);
2018
2019 neigh_release(n);
2020
David Stevens73461352014-03-18 12:32:29 -04002021 if (reply == NULL)
2022 goto out;
2023
David Stevense4f67ad2012-11-20 02:50:14 +00002024 skb_reset_mac_header(reply);
2025 __skb_pull(reply, skb_network_offset(reply));
2026 reply->ip_summed = CHECKSUM_UNNECESSARY;
2027 reply->pkt_type = PACKET_HOST;
2028
2029 if (netif_rx_ni(reply) == NET_RX_DROP)
2030 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002031 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002032 union vxlan_addr ipa = {
2033 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002034 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08002035 };
2036
2037 vxlan_ip_miss(dev, &ipa);
2038 }
David Stevense4f67ad2012-11-20 02:50:14 +00002039out:
2040 consume_skb(skb);
2041 return NETDEV_TX_OK;
2042}
2043
Cong Wangf564f452013-08-31 13:44:36 +08002044#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04002045static struct sk_buff *vxlan_na_create(struct sk_buff *request,
2046 struct neighbour *n, bool isrouter)
2047{
2048 struct net_device *dev = request->dev;
2049 struct sk_buff *reply;
2050 struct nd_msg *ns, *na;
2051 struct ipv6hdr *pip6;
2052 u8 *daddr;
2053 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
2054 int ns_olen;
2055 int i, len;
2056
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002057 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04002058 return NULL;
2059
2060 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
2061 sizeof(*na) + na_olen + dev->needed_tailroom;
2062 reply = alloc_skb(len, GFP_ATOMIC);
2063 if (reply == NULL)
2064 return NULL;
2065
2066 reply->protocol = htons(ETH_P_IPV6);
2067 reply->dev = dev;
2068 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
2069 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00002070 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04002071
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002072 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04002073
2074 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002075 ns_olen = request->len - skb_network_offset(request) -
2076 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04002077 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
2078 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
2079 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
2080 break;
2081 }
2082 }
2083
2084 /* Ethernet header */
2085 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
2086 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
2087 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
2088 reply->protocol = htons(ETH_P_IPV6);
2089
2090 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00002091 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04002092 skb_put(reply, sizeof(struct ipv6hdr));
2093
2094 /* IPv6 header */
2095
2096 pip6 = ipv6_hdr(reply);
2097 memset(pip6, 0, sizeof(struct ipv6hdr));
2098 pip6->version = 6;
2099 pip6->priority = ipv6_hdr(request)->priority;
2100 pip6->nexthdr = IPPROTO_ICMPV6;
2101 pip6->hop_limit = 255;
2102 pip6->daddr = ipv6_hdr(request)->saddr;
2103 pip6->saddr = *(struct in6_addr *)n->primary_key;
2104
2105 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00002106 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04002107
David Stevens4b29dba2014-03-24 10:39:58 -04002108 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02002109 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04002110 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
2111 na->icmph.icmp6_router = isrouter;
2112 na->icmph.icmp6_override = 1;
2113 na->icmph.icmp6_solicited = 1;
2114 na->target = ns->target;
2115 ether_addr_copy(&na->opt[2], n->ha);
2116 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
2117 na->opt[1] = na_olen >> 3;
2118
2119 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
2120 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
2121 csum_partial(na, sizeof(*na)+na_olen, 0));
2122
2123 pip6->payload_len = htons(sizeof(*na)+na_olen);
2124
2125 skb_push(reply, sizeof(struct ipv6hdr));
2126
2127 reply->ip_summed = CHECKSUM_UNNECESSARY;
2128
2129 return reply;
2130}
2131
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002132static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08002133{
2134 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08002135 const struct in6_addr *daddr;
Xin Long8bff36852017-11-11 19:58:50 +08002136 const struct ipv6hdr *iphdr;
David Stevens4b29dba2014-03-24 10:39:58 -04002137 struct inet6_dev *in6_dev;
Xin Long8bff36852017-11-11 19:58:50 +08002138 struct neighbour *n;
2139 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08002140
2141 in6_dev = __in6_dev_get(dev);
2142 if (!in6_dev)
2143 goto out;
2144
Cong Wangf564f452013-08-31 13:44:36 +08002145 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002146 daddr = &iphdr->daddr;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002147 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08002148
David Stevens4b29dba2014-03-24 10:39:58 -04002149 if (ipv6_addr_loopback(daddr) ||
2150 ipv6_addr_is_multicast(&msg->target))
2151 goto out;
2152
2153 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08002154
2155 if (n) {
2156 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04002157 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08002158
2159 if (!(n->nud_state & NUD_CONNECTED)) {
2160 neigh_release(n);
2161 goto out;
2162 }
2163
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002164 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002165 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2166 /* bridge-local neighbor */
2167 neigh_release(n);
2168 goto out;
2169 }
2170
David Stevens4b29dba2014-03-24 10:39:58 -04002171 reply = vxlan_na_create(skb, n,
2172 !!(f ? f->flags & NTF_ROUTER : 0));
2173
Cong Wangf564f452013-08-31 13:44:36 +08002174 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04002175
2176 if (reply == NULL)
2177 goto out;
2178
2179 if (netif_rx_ni(reply) == NET_RX_DROP)
2180 dev->stats.rx_dropped++;
2181
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002182 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04002183 union vxlan_addr ipa = {
2184 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002185 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04002186 };
2187
Cong Wangf564f452013-08-31 13:44:36 +08002188 vxlan_ip_miss(dev, &ipa);
2189 }
2190
2191out:
2192 consume_skb(skb);
2193 return NETDEV_TX_OK;
2194}
2195#endif
2196
David Stevense4f67ad2012-11-20 02:50:14 +00002197static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2198{
2199 struct vxlan_dev *vxlan = netdev_priv(dev);
2200 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00002201
2202 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2203 return false;
2204
2205 n = NULL;
2206 switch (ntohs(eth_hdr(skb)->h_proto)) {
2207 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08002208 {
2209 struct iphdr *pip;
2210
David Stevense4f67ad2012-11-20 02:50:14 +00002211 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2212 return false;
2213 pip = ip_hdr(skb);
2214 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002215 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002216 union vxlan_addr ipa = {
2217 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002218 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08002219 };
2220
2221 vxlan_ip_miss(dev, &ipa);
2222 return false;
2223 }
2224
David Stevense4f67ad2012-11-20 02:50:14 +00002225 break;
Cong Wange15a00a2013-08-31 13:44:34 +08002226 }
2227#if IS_ENABLED(CONFIG_IPV6)
2228 case ETH_P_IPV6:
2229 {
2230 struct ipv6hdr *pip6;
2231
2232 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2233 return false;
2234 pip6 = ipv6_hdr(skb);
2235 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002236 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08002237 union vxlan_addr ipa = {
2238 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002239 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08002240 };
2241
2242 vxlan_ip_miss(dev, &ipa);
2243 return false;
2244 }
2245
2246 break;
2247 }
2248#endif
David Stevense4f67ad2012-11-20 02:50:14 +00002249 default:
2250 return false;
2251 }
2252
2253 if (n) {
2254 bool diff;
2255
Joe Perches7367d0b2013-09-01 11:51:23 -07002256 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00002257 if (diff) {
2258 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2259 dev->addr_len);
2260 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2261 }
2262 neigh_release(n);
2263 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08002264 }
2265
David Stevense4f67ad2012-11-20 02:50:14 +00002266 return false;
2267}
2268
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002269static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01002270 struct vxlan_metadata *md)
2271{
2272 struct vxlanhdr_gbp *gbp;
2273
Thomas Grafdb79a622015-02-04 17:00:04 +01002274 if (!md->gbp)
2275 return;
2276
Thomas Graf35114942015-01-15 03:53:55 +01002277 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01002278 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01002279
2280 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2281 gbp->dont_learn = 1;
2282
2283 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2284 gbp->policy_applied = 1;
2285
2286 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2287}
2288
Jiri Bence1e53142016-04-05 14:47:13 +02002289static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2290 __be16 protocol)
2291{
2292 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2293
2294 gpe->np_applied = 1;
Jiri Bencfa20e0e2017-08-28 21:43:22 +02002295 gpe->next_protocol = tun_p_from_eth_p(protocol);
2296 if (!gpe->next_protocol)
2297 return -EPFNOSUPPORT;
2298 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002299}
2300
Jiri Bencf491e562016-02-02 18:09:16 +01002301static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2302 int iphdr_len, __be32 vni,
2303 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002304 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08002305{
Cong Wange4c7ed42013-08-31 13:44:33 +08002306 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08002307 int min_headroom;
2308 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08002309 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02002310 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08002311
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002312 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08002313 skb->ip_summed == CHECKSUM_PARTIAL) {
2314 int csum_start = skb_checksum_start_offset(skb);
2315
2316 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2317 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2318 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00002319 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08002320 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08002321 }
2322
Cong Wange4c7ed42013-08-31 13:44:33 +08002323 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08002324 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002325
2326 /* Need space for new headers (invalidates iph ptr) */
2327 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02002328 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08002329 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002330
Alexander Duyckaed069d2016-04-14 15:33:37 -04002331 err = iptunnel_handle_offloads(skb, type);
2332 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08002333 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07002334
Johannes Bergd58ff352017-06-16 14:29:23 +02002335 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01002336 vxh->vx_flags = VXLAN_HF_VNI;
2337 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07002338
Tom Herbertdfd86452015-01-12 17:00:38 -08002339 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01002340 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08002341
Jiri Benc54bfd872016-02-16 21:58:58 +01002342 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2343 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2344 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08002345
2346 if (!skb_is_gso(skb)) {
2347 skb->ip_summed = CHECKSUM_NONE;
2348 skb->encapsulation = 0;
2349 }
2350 }
2351
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002352 if (vxflags & VXLAN_F_GBP)
2353 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02002354 if (vxflags & VXLAN_F_GPE) {
2355 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2356 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002357 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02002358 inner_protocol = skb->protocol;
2359 }
Thomas Graf35114942015-01-15 03:53:55 +01002360
Jiri Bence1e53142016-04-05 14:47:13 +02002361 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08002362 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07002363}
Pravin B Shelar49560532013-08-19 11:23:17 -07002364
pravin shelar655c3de2016-11-13 20:43:55 -08002365static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2366 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002367 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002368 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002369 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002370 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01002371{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002372 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002373 struct rtable *rt = NULL;
2374 struct flowi4 fl4;
2375
pravin shelar655c3de2016-11-13 20:43:55 -08002376 if (!sock4)
2377 return ERR_PTR(-EIO);
2378
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002379 if (tos && !info)
2380 use_cache = false;
2381 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002382 rt = dst_cache_get_ip4(dst_cache, saddr);
2383 if (rt)
2384 return rt;
2385 }
2386
Jiri Benc1a8496b2016-02-02 18:09:14 +01002387 memset(&fl4, 0, sizeof(fl4));
2388 fl4.flowi4_oif = oif;
2389 fl4.flowi4_tos = RT_TOS(tos);
2390 fl4.flowi4_mark = skb->mark;
2391 fl4.flowi4_proto = IPPROTO_UDP;
2392 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002393 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002394 fl4.fl4_dport = dport;
2395 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002396
2397 rt = ip_route_output_key(vxlan->net, &fl4);
Enrico Weigelt478db1f2019-06-05 23:19:13 +02002398 if (!IS_ERR(rt)) {
pravin shelar655c3de2016-11-13 20:43:55 -08002399 if (rt->dst.dev == dev) {
2400 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2401 ip_rt_put(rt);
2402 return ERR_PTR(-ELOOP);
2403 }
2404
Jiri Benc1a8496b2016-02-02 18:09:14 +01002405 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002406 if (use_cache)
2407 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08002408 } else {
2409 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2410 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002411 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002412 return rt;
2413}
2414
Jiri Bence5d4b292015-12-07 13:04:30 +01002415#if IS_ENABLED(CONFIG_IPV6)
2416static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08002417 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08002418 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01002419 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002420 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01002421 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002422 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002423 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002424 struct dst_cache *dst_cache,
2425 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01002426{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002427 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002428 struct dst_entry *ndst;
2429 struct flowi6 fl6;
Jiri Bence5d4b292015-12-07 13:04:30 +01002430
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002431 if (!sock6)
2432 return ERR_PTR(-EIO);
2433
Daniel Borkmann14006152016-03-04 15:15:08 +01002434 if (tos && !info)
2435 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002436 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002437 ndst = dst_cache_get_ip6(dst_cache, saddr);
2438 if (ndst)
2439 return ndst;
2440 }
2441
Jiri Bence5d4b292015-12-07 13:04:30 +01002442 memset(&fl6, 0, sizeof(fl6));
2443 fl6.flowi6_oif = oif;
2444 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002445 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01002446 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01002447 fl6.flowi6_mark = skb->mark;
2448 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002449 fl6.fl6_dport = dport;
2450 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01002451
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +01002452 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk,
2453 &fl6, NULL);
2454 if (unlikely(IS_ERR(ndst))) {
pravin shelar655c3de2016-11-13 20:43:55 -08002455 netdev_dbg(dev, "no route to %pI6\n", daddr);
2456 return ERR_PTR(-ENETUNREACH);
2457 }
2458
2459 if (unlikely(ndst->dev == dev)) {
2460 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2461 dst_release(ndst);
2462 return ERR_PTR(-ELOOP);
2463 }
Jiri Bence5d4b292015-12-07 13:04:30 +01002464
2465 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002466 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002467 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01002468 return ndst;
2469}
2470#endif
2471
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002472/* Bypass encapsulation if the destination is local */
2473static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002474 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002475{
Li RongQing8f849852014-01-04 13:57:59 +08002476 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08002477 union vxlan_addr loopback;
2478 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Eric Dumazet4179cb52019-02-07 12:27:38 -08002479 struct net_device *dev;
Li RongQingce6502a2014-10-16 08:49:41 +08002480 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002481
Li RongQing8f849852014-01-04 13:57:59 +08002482 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2483 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002484 skb->pkt_type = PACKET_HOST;
2485 skb->encapsulation = 0;
2486 skb->dev = dst_vxlan->dev;
2487 __skb_pull(skb, skb_network_offset(skb));
2488
Cong Wange4c7ed42013-08-31 13:44:33 +08002489 if (remote_ip->sa.sa_family == AF_INET) {
2490 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2491 loopback.sa.sa_family = AF_INET;
2492#if IS_ENABLED(CONFIG_IPV6)
2493 } else {
2494 loopback.sin6.sin6_addr = in6addr_loopback;
2495 loopback.sa.sa_family = AF_INET6;
2496#endif
2497 }
2498
Eric Dumazet4179cb52019-02-07 12:27:38 -08002499 rcu_read_lock();
2500 dev = skb->dev;
2501 if (unlikely(!(dev->flags & IFF_UP))) {
2502 kfree_skb(skb);
2503 goto drop;
2504 }
2505
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002506 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
Eric Dumazet4179cb52019-02-07 12:27:38 -08002507 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002508
2509 u64_stats_update_begin(&tx_stats->syncp);
2510 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002511 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002512 u64_stats_update_end(&tx_stats->syncp);
2513
2514 if (netif_rx(skb) == NET_RX_SUCCESS) {
2515 u64_stats_update_begin(&rx_stats->syncp);
2516 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002517 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002518 u64_stats_update_end(&rx_stats->syncp);
2519 } else {
Eric Dumazet4179cb52019-02-07 12:27:38 -08002520drop:
Li RongQingce6502a2014-10-16 08:49:41 +08002521 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002522 }
Eric Dumazet4179cb52019-02-07 12:27:38 -08002523 rcu_read_unlock();
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002524}
2525
pravin shelarfee1fad2016-11-13 20:43:56 -08002526static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002527 struct vxlan_dev *vxlan,
2528 union vxlan_addr *daddr,
2529 __be16 dst_port, int dst_ifindex, __be32 vni,
2530 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002531 u32 rt_flags)
2532{
2533#if IS_ENABLED(CONFIG_IPV6)
2534 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2535 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2536 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2537 */
2538 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2539#endif
2540 /* Bypass encapsulation if the destination is local */
2541 if (rt_flags & RTCF_LOCAL &&
2542 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2543 struct vxlan_dev *dst_vxlan;
2544
2545 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002546 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002547 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002548 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002549 if (!dst_vxlan) {
2550 dev->stats.tx_errors++;
2551 kfree_skb(skb);
2552
2553 return -ENOENT;
2554 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002555 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002556 return 1;
2557 }
2558
2559 return 0;
2560}
2561
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002562static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002563 __be32 default_vni, struct vxlan_rdst *rdst,
2564 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002565{
Paolo Abenid71785f2016-02-12 15:43:57 +01002566 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002567 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002568 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002569 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002570 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002571 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002572 struct vxlan_metadata _md;
2573 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002574 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002575 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002576 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002577 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002578 int ifindex;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07002579 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002580 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002581 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002582 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002583
Jiri Benc61adedf2015-08-20 13:56:25 +02002584 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002585
Thomas Grafee122c72015-07-21 10:43:58 +02002586 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002587 dst = &rdst->remote_ip;
2588 if (vxlan_addr_any(dst)) {
2589 if (did_rsc) {
2590 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002591 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002592 return;
2593 }
2594 goto drop;
2595 }
2596
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002597 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002598 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002599 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002600 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002601 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002602 md->gbp = skb->mark;
Hangbin Liu72f6d712018-04-17 14:11:28 +08002603 if (flags & VXLAN_F_TTL_INHERIT) {
2604 ttl = ip_tunnel_get_ttl(old_iph, skb);
2605 } else {
2606 ttl = vxlan->cfg.ttl;
2607 if (!ttl && vxlan_addr_multicast(dst))
2608 ttl = 1;
2609 }
pravin shelar0770b532016-11-13 20:43:57 -08002610
2611 tos = vxlan->cfg.tos;
2612 if (tos == 1)
2613 tos = ip_tunnel_get_dsfield(old_iph, skb);
2614
2615 if (dst->sa.sa_family == AF_INET)
2616 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2617 else
2618 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2619 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002620 } else {
2621 if (!info) {
2622 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2623 dev->name);
2624 goto drop;
2625 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002626 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002627 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002628 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002629 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2630 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002631 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002632 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2633 }
Thomas Grafee122c72015-07-21 10:43:58 +02002634 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002635 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2636 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002637 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002638 dst_cache = &info->dst_cache;
Xin Longeadf52c2019-10-29 01:24:32 +08002639 if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
2640 if (info->options_len < sizeof(*md))
2641 goto drop;
pravin shelar0770b532016-11-13 20:43:57 -08002642 md = ip_tunnel_info_opts(info);
Xin Longeadf52c2019-10-29 01:24:32 +08002643 }
Jiri Benca725e512015-08-20 13:56:30 +02002644 ttl = info->key.ttl;
2645 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002646 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002647 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002648 }
pravin shelar0770b532016-11-13 20:43:57 -08002649 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2650 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002651
Jakub Kicinski56de8592017-02-24 11:43:36 -08002652 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002653 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002654 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002655 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002656 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002657
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002658 if (!ifindex)
2659 ifindex = sock4->sock->sk->sk_bound_dev_if;
2660
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002661 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002662 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002663 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002664 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002665 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002666 if (IS_ERR(rt)) {
2667 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002668 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002669 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002670
pravin shelarfee1fad2016-11-13 20:43:56 -08002671 if (!info) {
Stefano Briviob4d306972018-11-08 12:19:16 +01002672 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002673 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002674 dst_port, ifindex, vni,
2675 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002676 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002677 goto out_unlock;
Stefano Briviob4d306972018-11-08 12:19:16 +01002678
2679 if (vxlan->cfg.df == VXLAN_DF_SET) {
2680 df = htons(IP_DF);
2681 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2682 struct ethhdr *eth = eth_hdr(skb);
2683
2684 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2685 (ntohs(eth->h_proto) == ETH_P_IP &&
2686 old_iph->frag_off & htons(IP_DF)))
2687 df = htons(IP_DF);
2688 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002689 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002690 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002691 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002692
pravin shelarc46b7892016-11-13 20:43:54 -08002693 ndst = &rt->dst;
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002694 skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002695
Hangbin Liu71130f22020-01-02 17:23:45 +08002696 tos = ip_tunnel_ecn_encap(RT_TOS(tos), old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002697 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002698 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002699 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002700 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002701 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002702
Brian Russell11586322017-02-24 17:47:11 +00002703 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002704 dst->sin.sin_addr.s_addr, tos, ttl, df,
2705 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002706#if IS_ENABLED(CONFIG_IPV6)
2707 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002708 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002709
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002710 if (!ifindex)
2711 ifindex = sock6->sock->sk->sk_bound_dev_if;
2712
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002713 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002714 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002715 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002716 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002717 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002718 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002719 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002720 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002721 goto tx_error;
2722 }
pravin shelar655c3de2016-11-13 20:43:55 -08002723
pravin shelarfee1fad2016-11-13 20:43:56 -08002724 if (!info) {
2725 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002726
pravin shelarfee1fad2016-11-13 20:43:56 -08002727 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002728 dst_port, ifindex, vni,
2729 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002730 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002731 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002732 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002733
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002734 skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002735
Hangbin Liu71130f22020-01-02 17:23:45 +08002736 tos = ip_tunnel_ecn_encap(RT_TOS(tos), old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002737 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002738 skb_scrub_packet(skb, xnet);
2739 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002740 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002741 if (err < 0)
2742 goto tx_error;
2743
pravin shelar0770b532016-11-13 20:43:57 -08002744 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002745 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002746 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002747 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002748#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002749 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002750out_unlock:
2751 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002752 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002753
2754drop:
2755 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002756 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002757 return;
2758
2759tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002760 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002761 if (err == -ELOOP)
2762 dev->stats.collisions++;
2763 else if (err == -ENETUNREACH)
2764 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002765 dst_release(ndst);
2766 dev->stats.tx_errors++;
2767 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002768}
2769
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002770static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
2771 struct vxlan_fdb *f, __be32 vni, bool did_rsc)
2772{
2773 struct vxlan_rdst nh_rdst;
2774 struct nexthop *nh;
2775 bool do_xmit;
2776 u32 hash;
2777
2778 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2779 hash = skb_get_hash(skb);
2780
2781 rcu_read_lock();
2782 nh = rcu_dereference(f->nh);
2783 if (!nh) {
2784 rcu_read_unlock();
2785 goto drop;
2786 }
2787 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2788 rcu_read_unlock();
2789
2790 if (likely(do_xmit))
2791 vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc);
2792 else
2793 goto drop;
2794
2795 return;
2796
2797drop:
2798 dev->stats.tx_dropped++;
2799 dev_kfree_skb(skb);
2800}
2801
David Stevens66817122013-03-15 04:35:51 +00002802/* Transmit local packets over Vxlan
2803 *
2804 * Outer IP header inherits ECN and DF from inner header.
2805 * Outer UDP destination is the VXLAN assigned port.
2806 * source port is based on hash of flow
2807 */
2808static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2809{
2810 struct vxlan_dev *vxlan = netdev_priv(dev);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002811 struct vxlan_rdst *rdst, *fdst = NULL;
Xin Long8bff36852017-11-11 19:58:50 +08002812 const struct ip_tunnel_info *info;
2813 bool did_rsc = false;
David Stevens66817122013-03-15 04:35:51 +00002814 struct vxlan_fdb *f;
Xin Long8bff36852017-11-11 19:58:50 +08002815 struct ethhdr *eth;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002816 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002817
Jiri Benc61adedf2015-08-20 13:56:25 +02002818 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002819
David Stevens66817122013-03-15 04:35:51 +00002820 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002821
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002822 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002823 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2824 info->mode & IP_TUNNEL_INFO_TX) {
2825 vni = tunnel_id_to_key32(info->key.tun_id);
2826 } else {
2827 if (info && info->mode & IP_TUNNEL_INFO_TX)
2828 vxlan_xmit_one(skb, dev, vni, NULL, false);
2829 else
2830 kfree_skb(skb);
2831 return NETDEV_TX_OK;
2832 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002833 }
2834
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002835 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002836 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002837 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002838 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002839#if IS_ENABLED(CONFIG_IPV6)
Xin Long8bff36852017-11-11 19:58:50 +08002840 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2841 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2842 sizeof(struct nd_msg)) &&
2843 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2844 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2845
2846 if (m->icmph.icmp6_code == 0 &&
2847 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002848 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002849 }
2850#endif
2851 }
David Stevens66817122013-03-15 04:35:51 +00002852
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002853 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002854 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002855 did_rsc = false;
2856
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002857 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002858 (ntohs(eth->h_proto) == ETH_P_IP ||
2859 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002860 did_rsc = route_shortcircuit(dev, skb);
2861 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002862 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002863 }
2864
David Stevens66817122013-03-15 04:35:51 +00002865 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002866 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002867 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002868 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002869 !is_multicast_ether_addr(eth->h_dest))
2870 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002871
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002872 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002873 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002874 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002875 }
David Stevens66817122013-03-15 04:35:51 +00002876 }
2877
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002878 if (rcu_access_pointer(f->nh)) {
2879 vxlan_xmit_nh(skb, dev, f,
2880 (vni ? : vxlan->default_dst.remote_vni), did_rsc);
2881 } else {
2882 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2883 struct sk_buff *skb1;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002884
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002885 if (!fdst) {
2886 fdst = rdst;
2887 continue;
2888 }
2889 skb1 = skb_clone(skb, GFP_ATOMIC);
2890 if (skb1)
2891 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002892 }
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07002893 if (fdst)
2894 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2895 else
2896 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002897 }
2898
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002899 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002900}
2901
stephen hemmingerd3428942012-10-01 12:32:35 +00002902/* Walk the forwarding table and purge stale entries */
Kees Cookdf7e8282017-10-04 16:26:59 -07002903static void vxlan_cleanup(struct timer_list *t)
stephen hemmingerd3428942012-10-01 12:32:35 +00002904{
Kees Cookdf7e8282017-10-04 16:26:59 -07002905 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
stephen hemmingerd3428942012-10-01 12:32:35 +00002906 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2907 unsigned int h;
2908
2909 if (!netif_running(vxlan->dev))
2910 return;
2911
stephen hemmingerd3428942012-10-01 12:32:35 +00002912 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2913 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002914
Litao jiaofe1e0712019-06-06 17:57:58 +08002915 spin_lock(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002916 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2917 struct vxlan_fdb *f
2918 = container_of(p, struct vxlan_fdb, hlist);
2919 unsigned long timeout;
2920
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002921 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002922 continue;
2923
Roopa Prabhudef499c2017-03-27 15:46:41 -07002924 if (f->flags & NTF_EXT_LEARNED)
2925 continue;
2926
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002927 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002928 if (time_before_eq(timeout, jiffies)) {
2929 netdev_dbg(vxlan->dev,
2930 "garbage collect %pM\n",
2931 f->eth_addr);
2932 f->state = NUD_STALE;
Petr Machata0e6160f2018-11-21 08:02:35 +00002933 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002934 } else if (time_before(timeout, next_timer))
2935 next_timer = timeout;
2936 }
Litao jiaofe1e0712019-06-06 17:57:58 +08002937 spin_unlock(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002938 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002939
2940 mod_timer(&vxlan->age_timer, next_timer);
2941}
2942
Mark Blocha53cb292017-06-02 03:24:08 +03002943static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2944{
2945 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2946
2947 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002948 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2949#if IS_ENABLED(CONFIG_IPV6)
2950 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2951#endif
Mark Blocha53cb292017-06-02 03:24:08 +03002952 spin_unlock(&vn->sock_lock);
2953}
2954
Jiri Benc69e76662017-07-02 19:00:57 +02002955static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2956 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002957{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002958 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002959 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002960
Jiri Benc69e76662017-07-02 19:00:57 +02002961 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002962 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002963 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002964 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002965}
2966
stephen hemmingerd3428942012-10-01 12:32:35 +00002967/* Setup stats when device is created */
2968static int vxlan_init(struct net_device *dev)
2969{
Taehee Yoo384d91c2020-03-18 13:28:09 +00002970 struct vxlan_dev *vxlan = netdev_priv(dev);
2971 int err;
2972
WANG Cong1c213bd2014-02-13 11:46:28 -08002973 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002974 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002975 return -ENOMEM;
2976
Taehee Yoo384d91c2020-03-18 13:28:09 +00002977 err = gro_cells_init(&vxlan->gro_cells, dev);
2978 if (err) {
2979 free_percpu(dev->tstats);
2980 return err;
2981 }
2982
stephen hemmingerd3428942012-10-01 12:32:35 +00002983 return 0;
2984}
2985
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002986static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002987{
2988 struct vxlan_fdb *f;
Litao jiaofe1e0712019-06-06 17:57:58 +08002989 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002990
Litao jiaofe1e0712019-06-06 17:57:58 +08002991 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002992 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002993 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00002994 vxlan_fdb_destroy(vxlan, f, true, true);
Litao jiaofe1e0712019-06-06 17:57:58 +08002995 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002996}
2997
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002998static void vxlan_uninit(struct net_device *dev)
2999{
3000 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003001
Stefano Brivioad6c9982019-03-08 16:40:57 +01003002 gro_cells_destroy(&vxlan->gro_cells);
3003
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08003004 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003005
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003006 free_percpu(dev->tstats);
3007}
3008
stephen hemmingerd3428942012-10-01 12:32:35 +00003009/* Start ageing timer and join group when device is brought up */
3010static int vxlan_open(struct net_device *dev)
3011{
3012 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02003013 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003014
Jiri Benc205f3562015-09-24 13:50:01 +02003015 ret = vxlan_sock_add(vxlan);
3016 if (ret < 0)
3017 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00003018
Gao feng79d4a942013-12-10 16:37:32 +08003019 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003020 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03003021 if (ret == -EADDRINUSE)
3022 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003023 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02003024 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003025 return ret;
3026 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003027 }
3028
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003029 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00003030 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
3031
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003032 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00003033}
3034
3035/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003036static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00003037{
Cong Wang31fec5a2013-05-27 22:35:52 +00003038 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003039
stephen hemmingerd3428942012-10-01 12:32:35 +00003040 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3041 struct hlist_node *p, *n;
Litao jiaofe1e0712019-06-06 17:57:58 +08003042
3043 spin_lock_bh(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003044 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
3045 struct vxlan_fdb *f
3046 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003047 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
3048 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003049 /* the all_zeros_mac entry is deleted at vxlan_uninit */
3050 if (!is_zero_ether_addr(f->eth_addr))
Petr Machata0e6160f2018-11-21 08:02:35 +00003051 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00003052 }
Litao jiaofe1e0712019-06-06 17:57:58 +08003053 spin_unlock_bh(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003054 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003055}
3056
3057/* Cleanup timer and forwarding table on shutdown */
3058static int vxlan_stop(struct net_device *dev)
3059{
3060 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003061 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003062 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00003063
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03003064 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07003065 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003066 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00003067
3068 del_timer_sync(&vxlan->age_timer);
3069
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003070 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02003071 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00003072
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003073 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00003074}
3075
stephen hemmingerd3428942012-10-01 12:32:35 +00003076/* Stub, nothing needs to be done. */
3077static void vxlan_set_multicast_list(struct net_device *dev)
3078{
3079}
3080
Daniel Borkmann345010b2013-12-18 00:21:08 +01003081static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
3082{
3083 struct vxlan_dev *vxlan = netdev_priv(dev);
3084 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00003085 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3086 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003087 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04003088
3089 /* This check is different than dev->max_mtu, because it looks at
3090 * the lowerdev->mtu, rather than the static dev->max_mtu
3091 */
3092 if (lowerdev) {
3093 int max_mtu = lowerdev->mtu -
3094 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
3095 if (new_mtu > max_mtu)
3096 return -EINVAL;
3097 }
3098
3099 dev->mtu = new_mtu;
3100 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01003101}
3102
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07003103static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
3104{
3105 struct vxlan_dev *vxlan = netdev_priv(dev);
3106 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3107 __be16 sport, dport;
3108
3109 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
3110 vxlan->cfg.port_max, true);
3111 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
3112
Jiri Benc239e9442015-12-07 13:04:31 +01003113 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003114 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01003115 struct rtable *rt;
3116
pravin shelar655c3de2016-11-13 20:43:55 -08003117 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01003118 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01003119 &info->key.u.ipv4.src, dport, sport,
3120 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01003121 if (IS_ERR(rt))
3122 return PTR_ERR(rt);
3123 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01003124 } else {
3125#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08003126 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01003127 struct dst_entry *ndst;
3128
pravin shelar655c3de2016-11-13 20:43:55 -08003129 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003130 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01003131 &info->key.u.ipv6.src, dport, sport,
3132 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01003133 if (IS_ERR(ndst))
3134 return PTR_ERR(ndst);
3135 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01003136#else /* !CONFIG_IPV6 */
3137 return -EPFNOSUPPORT;
3138#endif
3139 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01003140 info->key.tp_src = sport;
3141 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01003142 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07003143}
3144
Jiri Benc0c867c92016-04-05 14:47:10 +02003145static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00003146 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07003147 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00003148 .ndo_open = vxlan_open,
3149 .ndo_stop = vxlan_stop,
3150 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00003151 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00003152 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01003153 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00003154 .ndo_validate_addr = eth_validate_addr,
3155 .ndo_set_mac_address = eth_mac_addr,
3156 .ndo_fdb_add = vxlan_fdb_add,
3157 .ndo_fdb_del = vxlan_fdb_delete,
3158 .ndo_fdb_dump = vxlan_fdb_dump,
Roopa Prabhu474c3c82018-12-15 22:35:10 -08003159 .ndo_fdb_get = vxlan_fdb_get,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07003160 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
Andy Roulin8f1af752019-02-22 18:06:38 +00003161 .ndo_change_proto_down = dev_change_proto_down_generic,
stephen hemmingerd3428942012-10-01 12:32:35 +00003162};
3163
Jiri Bence1e53142016-04-05 14:47:13 +02003164static const struct net_device_ops vxlan_netdev_raw_ops = {
3165 .ndo_init = vxlan_init,
3166 .ndo_uninit = vxlan_uninit,
3167 .ndo_open = vxlan_open,
3168 .ndo_stop = vxlan_stop,
3169 .ndo_start_xmit = vxlan_xmit,
3170 .ndo_get_stats64 = ip_tunnel_get_stats64,
3171 .ndo_change_mtu = vxlan_change_mtu,
3172 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3173};
3174
stephen hemmingerd3428942012-10-01 12:32:35 +00003175/* Info for udev, that this is a virtual tunnel endpoint */
3176static struct device_type vxlan_type = {
3177 .name = "vxlan",
3178};
3179
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02003180/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07003181 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02003182 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003183 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003184static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003185{
3186 struct vxlan_sock *vs;
3187 struct net *net = dev_net(dev);
3188 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07003189 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003190
3191 spin_lock(&vn->sock_lock);
3192 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003193 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
3194 unsigned short type;
3195
3196 if (vs->flags & VXLAN_F_GPE)
3197 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3198 else
3199 type = UDP_TUNNEL_TYPE_VXLAN;
3200
3201 if (push)
3202 udp_tunnel_push_rx_port(dev, vs->sock, type);
3203 else
3204 udp_tunnel_drop_rx_port(dev, vs->sock, type);
3205 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003206 }
3207 spin_unlock(&vn->sock_lock);
3208}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07003209
stephen hemmingerd3428942012-10-01 12:32:35 +00003210/* Initialize the device structure. */
3211static void vxlan_setup(struct net_device *dev)
3212{
3213 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00003214 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003215
Jiri Benc65226ef2016-04-28 16:36:30 +02003216 eth_hw_addr_random(dev);
3217 ether_setup(dev);
3218
David S. Millercf124db2017-05-08 12:52:56 -04003219 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00003220 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3221
stephen hemmingerd3428942012-10-01 12:32:35 +00003222 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00003223 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003224 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003225 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003226
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07003227 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003228 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003229 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07003230 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02003231 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00003232
Matthias Schiffera9853432017-06-19 10:03:55 +02003233 /* MTU range: 68 - 65535 */
3234 dev->min_mtu = ETH_MIN_MTU;
3235 dev->max_mtu = ETH_MAX_MTU;
3236
stephen hemminger553675f2013-05-16 11:35:20 +00003237 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003238
Kees Cookdf7e8282017-10-04 16:26:59 -07003239 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
stephen hemmingerd3428942012-10-01 12:32:35 +00003240
3241 vxlan->dev = dev;
3242
Litao jiaofe1e0712019-06-06 17:57:58 +08003243 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3244 spin_lock_init(&vxlan->hash_lock[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003245 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
Litao jiaofe1e0712019-06-06 17:57:58 +08003246 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003247}
3248
Jiri Benc0c867c92016-04-05 14:47:10 +02003249static void vxlan_ether_setup(struct net_device *dev)
3250{
Jiri Benc0c867c92016-04-05 14:47:10 +02003251 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3252 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3253 dev->netdev_ops = &vxlan_netdev_ether_ops;
3254}
3255
Jiri Bence1e53142016-04-05 14:47:13 +02003256static void vxlan_raw_setup(struct net_device *dev)
3257{
Jiri Benc65226ef2016-04-28 16:36:30 +02003258 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02003259 dev->type = ARPHRD_NONE;
3260 dev->hard_header_len = 0;
3261 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02003262 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3263 dev->netdev_ops = &vxlan_netdev_raw_ops;
3264}
3265
stephen hemmingerd3428942012-10-01 12:32:35 +00003266static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3267 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08003268 [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003269 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003270 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08003271 [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003272 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003273 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3274 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003275 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003276 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3277 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3278 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00003279 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00003280 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3281 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3282 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3283 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003284 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00003285 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08003286 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3287 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3288 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08003289 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3290 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01003291 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02003292 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003293 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
Hangbin Liu72f6d712018-04-17 14:11:28 +08003294 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
Stefano Briviob4d306972018-11-08 12:19:16 +01003295 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003296};
3297
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02003298static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3299 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003300{
3301 if (tb[IFLA_ADDRESS]) {
3302 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003303 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3304 "Provided link layer address is not Ethernet");
stephen hemmingerd3428942012-10-01 12:32:35 +00003305 return -EINVAL;
3306 }
3307
3308 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003309 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3310 "Provided Ethernet address is not unicast");
stephen hemmingerd3428942012-10-01 12:32:35 +00003311 return -EADDRNOTAVAIL;
3312 }
3313 }
3314
Matthias Schiffera9853432017-06-19 10:03:55 +02003315 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02003316 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02003317
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003318 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3319 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3320 "MTU must be between 68 and 65535");
Matthias Schiffera9853432017-06-19 10:03:55 +02003321 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003322 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003323 }
3324
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003325 if (!data) {
3326 NL_SET_ERR_MSG(extack,
3327 "Required attributes not provided to perform the operation");
stephen hemmingerd3428942012-10-01 12:32:35 +00003328 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003329 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003330
3331 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003332 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3333
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003334 if (id >= VXLAN_N_VID) {
Sabrina Dubrocacc8e7c62020-04-22 17:29:50 +02003335 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003336 "VXLAN ID must be lower than 16777216");
stephen hemmingerd3428942012-10-01 12:32:35 +00003337 return -ERANGE;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003338 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003339 }
3340
stephen hemminger05f47d62012-10-09 20:35:50 +00003341 if (data[IFLA_VXLAN_PORT_RANGE]) {
3342 const struct ifla_vxlan_port_range *p
3343 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3344
3345 if (ntohs(p->high) < ntohs(p->low)) {
Sabrina Dubrocacc8e7c62020-04-22 17:29:50 +02003346 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003347 "Invalid source port range");
stephen hemminger05f47d62012-10-09 20:35:50 +00003348 return -EINVAL;
3349 }
3350 }
3351
Stefano Briviob4d306972018-11-08 12:19:16 +01003352 if (data[IFLA_VXLAN_DF]) {
3353 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3354
3355 if (df < 0 || df > VXLAN_DF_MAX) {
Sabrina Dubrocacc8e7c62020-04-22 17:29:50 +02003356 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
Stefano Briviob4d306972018-11-08 12:19:16 +01003357 "Invalid DF attribute");
3358 return -EINVAL;
3359 }
3360 }
3361
stephen hemmingerd3428942012-10-01 12:32:35 +00003362 return 0;
3363}
3364
Yan Burman1b13c972013-01-29 23:43:07 +00003365static void vxlan_get_drvinfo(struct net_device *netdev,
3366 struct ethtool_drvinfo *drvinfo)
3367{
3368 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3369 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3370}
3371
Matthias Schiffer36fe3a62019-11-12 22:12:24 +01003372static int vxlan_get_link_ksettings(struct net_device *dev,
3373 struct ethtool_link_ksettings *cmd)
3374{
3375 struct vxlan_dev *vxlan = netdev_priv(dev);
3376 struct vxlan_rdst *dst = &vxlan->default_dst;
3377 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3378 dst->remote_ifindex);
3379
3380 if (!lowerdev) {
3381 cmd->base.duplex = DUPLEX_UNKNOWN;
3382 cmd->base.port = PORT_OTHER;
3383 cmd->base.speed = SPEED_UNKNOWN;
3384
3385 return 0;
3386 }
3387
3388 return __ethtool_get_link_ksettings(lowerdev, cmd);
3389}
3390
Yan Burman1b13c972013-01-29 23:43:07 +00003391static const struct ethtool_ops vxlan_ethtool_ops = {
Matthias Schiffer36fe3a62019-11-12 22:12:24 +01003392 .get_drvinfo = vxlan_get_drvinfo,
3393 .get_link = ethtool_op_get_link,
3394 .get_link_ksettings = vxlan_get_link_ksettings,
Yan Burman1b13c972013-01-29 23:43:07 +00003395};
3396
Tom Herbert3ee64f32014-07-13 19:49:42 -07003397static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003398 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +00003399{
Cong Wange4c7ed42013-08-31 13:44:33 +08003400 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003401 struct udp_port_cfg udp_conf;
3402 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08003403
Tom Herbert3ee64f32014-07-13 19:49:42 -07003404 memset(&udp_conf, 0, sizeof(udp_conf));
3405
3406 if (ipv6) {
3407 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003408 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08003409 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02003410 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003411 } else {
3412 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08003413 }
3414
Tom Herbert3ee64f32014-07-13 19:49:42 -07003415 udp_conf.local_udp_port = port;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003416 udp_conf.bind_ifindex = ifindex;
Cong Wange4c7ed42013-08-31 13:44:33 +08003417
Tom Herbert3ee64f32014-07-13 19:49:42 -07003418 /* Open UDP socket */
3419 err = udp_sock_create(net, &udp_conf, &sock);
3420 if (err < 0)
3421 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08003422
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003423 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08003424}
3425
3426/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02003427static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003428 __be16 port, u32 flags,
3429 int ifindex)
Cong Wange4c7ed42013-08-31 13:44:33 +08003430{
3431 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3432 struct vxlan_sock *vs;
3433 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00003434 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003435 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00003436
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02003437 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08003438 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00003439 return ERR_PTR(-ENOMEM);
3440
3441 for (h = 0; h < VNI_HASH_SIZE; ++h)
3442 INIT_HLIST_HEAD(&vs->vni_list[h]);
3443
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003444 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003445 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00003446 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08003447 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00003448 }
3449
Cong Wange4c7ed42013-08-31 13:44:33 +08003450 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03003451 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08003452 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00003453
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003454 spin_lock(&vn->sock_lock);
3455 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07003456 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07003457 (vs->flags & VXLAN_F_GPE) ?
3458 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07003459 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003460 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00003461
3462 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07003463 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07003464 tunnel_cfg.sk_user_data = vs;
3465 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01003466 tunnel_cfg.encap_rcv = vxlan_rcv;
Stefano Brivioc3a43b92018-11-08 12:19:15 +01003467 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003468 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07003469 tunnel_cfg.gro_receive = vxlan_gro_receive;
3470 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003471
3472 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08003473
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003474 return vs;
3475}
stephen hemminger553675f2013-05-16 11:35:20 +00003476
Jiri Bencb1be00a2015-09-24 13:50:02 +02003477static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003478{
Jiri Benc205f3562015-09-24 13:50:01 +02003479 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3480 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02003481 struct vxlan_dev_node *node;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003482 int l3mdev_index = 0;
3483
3484 if (vxlan->cfg.remote_ifindex)
3485 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3486 vxlan->net, vxlan->cfg.remote_ifindex);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003487
Jiri Benc205f3562015-09-24 13:50:01 +02003488 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003489 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003490 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003491 vxlan->cfg.dst_port, vxlan->cfg.flags,
3492 l3mdev_index);
Reshetova, Elena66af8462017-07-04 15:52:59 +03003493 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003494 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003495 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003496 }
3497 spin_unlock(&vn->sock_lock);
3498 }
Jiri Benc205f3562015-09-24 13:50:01 +02003499 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003500 vs = vxlan_socket_create(vxlan->net, ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003501 vxlan->cfg.dst_port, vxlan->cfg.flags,
3502 l3mdev_index);
Jiri Benc205f3562015-09-24 13:50:01 +02003503 if (IS_ERR(vs))
3504 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003505#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02003506 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003507 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003508 node = &vxlan->hlist6;
3509 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02003510#endif
Jiri Benc69e76662017-07-02 19:00:57 +02003511 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003512 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003513 node = &vxlan->hlist4;
3514 }
3515 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02003516 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00003517}
3518
Jiri Bencb1be00a2015-09-24 13:50:02 +02003519static int vxlan_sock_add(struct vxlan_dev *vxlan)
3520{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003521 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3522 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02003523 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003524 int ret = 0;
3525
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003526 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003527#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003528 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02003529 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02003530 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02003531 if (ret < 0 && ret != -EAFNOSUPPORT)
3532 ipv4 = false;
3533 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02003534#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02003535 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003536 ret = __vxlan_sock_add(vxlan, false);
3537 if (ret < 0)
3538 vxlan_sock_release(vxlan);
3539 return ret;
3540}
3541
Matthias Schiffera9853432017-06-19 10:03:55 +02003542static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3543 struct net_device **lower,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003544 struct vxlan_dev *old,
3545 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003546{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01003547 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02003548 struct vxlan_dev *tmp;
3549 bool use_ipv6 = false;
3550
3551 if (conf->flags & VXLAN_F_GPE) {
3552 /* For now, allow GPE only together with
3553 * COLLECT_METADATA. This can be relaxed later; in such
3554 * case, the other side of the PtP link will have to be
3555 * provided.
3556 */
3557 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3558 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003559 NL_SET_ERR_MSG(extack,
3560 "VXLAN GPE does not support this combination of attributes");
Matthias Schiffera9853432017-06-19 10:03:55 +02003561 return -EINVAL;
3562 }
3563 }
3564
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003565 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3566 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02003567 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003568 conf->saddr.sa.sa_family = AF_INET;
3569 } else if (!conf->remote_ip.sa.sa_family) {
3570 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3571 } else if (!conf->saddr.sa.sa_family) {
3572 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3573 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003574
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003575 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3576 NL_SET_ERR_MSG(extack,
3577 "Local and remote address must be from the same family");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003578 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003579 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003580
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003581 if (vxlan_addr_multicast(&conf->saddr)) {
3582 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003583 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003584 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003585
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003586 if (conf->saddr.sa.sa_family == AF_INET6) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003587 if (!IS_ENABLED(CONFIG_IPV6)) {
3588 NL_SET_ERR_MSG(extack,
3589 "IPv6 support not enabled in the kernel");
Matthias Schiffera9853432017-06-19 10:03:55 +02003590 return -EPFNOSUPPORT;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003591 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003592 use_ipv6 = true;
3593 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003594
3595 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3596 int local_type =
3597 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3598 int remote_type =
3599 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3600
3601 if (local_type & IPV6_ADDR_LINKLOCAL) {
3602 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003603 (remote_type != IPV6_ADDR_ANY)) {
3604 NL_SET_ERR_MSG(extack,
3605 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003606 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003607 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003608
3609 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3610 } else {
3611 if (remote_type ==
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003612 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3613 NL_SET_ERR_MSG(extack,
3614 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003615 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003616 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003617
3618 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3619 }
3620 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003621 }
3622
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003623 if (conf->label && !use_ipv6) {
3624 NL_SET_ERR_MSG(extack,
3625 "Label attribute only applies to IPv6 VXLAN devices");
Matthias Schiffera9853432017-06-19 10:03:55 +02003626 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003627 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003628
3629 if (conf->remote_ifindex) {
3630 struct net_device *lowerdev;
3631
3632 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003633 if (!lowerdev) {
3634 NL_SET_ERR_MSG(extack,
3635 "Invalid local interface, device not found");
Matthias Schiffera9853432017-06-19 10:03:55 +02003636 return -ENODEV;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003637 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003638
3639#if IS_ENABLED(CONFIG_IPV6)
3640 if (use_ipv6) {
3641 struct inet6_dev *idev = __in6_dev_get(lowerdev);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003642 if (idev && idev->cnf.disable_ipv6) {
3643 NL_SET_ERR_MSG(extack,
3644 "IPv6 support disabled by administrator");
Matthias Schiffera9853432017-06-19 10:03:55 +02003645 return -EPERM;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003646 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003647 }
3648#endif
3649
3650 *lower = lowerdev;
3651 } else {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003652 if (vxlan_addr_multicast(&conf->remote_ip)) {
3653 NL_SET_ERR_MSG(extack,
3654 "Local interface required for multicast remote destination");
3655
Matthias Schiffera9853432017-06-19 10:03:55 +02003656 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003657 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003658
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003659#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003660 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3661 NL_SET_ERR_MSG(extack,
3662 "Local interface required for link-local local/remote addresses");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003663 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003664 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003665#endif
3666
Matthias Schiffera9853432017-06-19 10:03:55 +02003667 *lower = NULL;
3668 }
3669
3670 if (!conf->dst_port) {
3671 if (conf->flags & VXLAN_F_GPE)
3672 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3673 else
3674 conf->dst_port = htons(vxlan_port);
3675 }
3676
3677 if (!conf->age_interval)
3678 conf->age_interval = FDB_AGE_DEFAULT;
3679
3680 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3681 if (tmp == old)
3682 continue;
3683
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003684 if (tmp->cfg.vni != conf->vni)
3685 continue;
3686 if (tmp->cfg.dst_port != conf->dst_port)
3687 continue;
3688 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003689 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003690 continue;
3691
3692 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3693 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3694 continue;
3695
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003696 NL_SET_ERR_MSG(extack,
3697 "A VXLAN device with the specified VNI already exists");
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003698 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003699 }
3700
3701 return 0;
3702}
3703
3704static void vxlan_config_apply(struct net_device *dev,
3705 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003706 struct net_device *lowerdev,
3707 struct net *src_net,
3708 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003709{
3710 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003711 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003712 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003713 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3714 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003715
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003716 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003717 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003718 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003719 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003720 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003721
Matthias Schiffera9853432017-06-19 10:03:55 +02003722 if (conf->mtu)
3723 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003724
3725 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003726 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003727
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003728 dst->remote_vni = conf->vni;
3729
3730 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003731
Matthias Schiffera9853432017-06-19 10:03:55 +02003732 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003733 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003734
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003735 dev->gso_max_size = lowerdev->gso_max_size;
3736 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003737
3738 needed_headroom = lowerdev->hard_header_len;
3739
3740 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3741 VXLAN_HEADROOM);
Alexey Kodanevf870c1f2017-12-14 20:20:00 +03003742 if (max_mtu < ETH_MIN_MTU)
3743 max_mtu = ETH_MIN_MTU;
3744
3745 if (!changelink && !conf->mtu)
3746 dev->mtu = max_mtu;
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003747 }
3748
Matthias Schiffera9853432017-06-19 10:03:55 +02003749 if (dev->mtu > max_mtu)
3750 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003751
Jiri Bencb1be00a2015-09-24 13:50:02 +02003752 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3753 needed_headroom += VXLAN6_HEADROOM;
3754 else
3755 needed_headroom += VXLAN_HEADROOM;
3756 dev->needed_headroom = needed_headroom;
3757
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003758 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003759}
stephen hemmingerd3428942012-10-01 12:32:35 +00003760
Matthias Schiffera9853432017-06-19 10:03:55 +02003761static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003762 struct vxlan_config *conf, bool changelink,
3763 struct netlink_ext_ack *extack)
Matthias Schiffera9853432017-06-19 10:03:55 +02003764{
3765 struct vxlan_dev *vxlan = netdev_priv(dev);
3766 struct net_device *lowerdev;
3767 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003768
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003769 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
Matthias Schiffera9853432017-06-19 10:03:55 +02003770 if (ret)
3771 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003772
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003773 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003774
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003775 return 0;
3776}
3777
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003778static int __vxlan_dev_create(struct net *net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003779 struct vxlan_config *conf,
3780 struct netlink_ext_ack *extack)
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003781{
3782 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3783 struct vxlan_dev *vxlan = netdev_priv(dev);
Taehee Yoo0ce18222019-10-21 18:47:57 +00003784 struct net_device *remote_dev = NULL;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003785 struct vxlan_fdb *f = NULL;
Petr Machata6db92462018-12-18 13:16:00 +00003786 bool unregister = false;
Taehee Yoo0ce18222019-10-21 18:47:57 +00003787 struct vxlan_rdst *dst;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003788 int err;
3789
Taehee Yoo0ce18222019-10-21 18:47:57 +00003790 dst = &vxlan->default_dst;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003791 err = vxlan_dev_configure(net, dev, conf, false, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003792 if (err)
3793 return err;
3794
3795 dev->ethtool_ops = &vxlan_ethtool_ops;
3796
3797 /* create an fdb entry for a valid default destination */
Taehee Yoo0ce18222019-10-21 18:47:57 +00003798 if (!vxlan_addr_any(&dst->remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003799 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Taehee Yoo0ce18222019-10-21 18:47:57 +00003800 &dst->remote_ip,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003801 NUD_REACHABLE | NUD_PERMANENT,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003802 vxlan->cfg.dst_port,
Taehee Yoo0ce18222019-10-21 18:47:57 +00003803 dst->remote_vni,
3804 dst->remote_vni,
3805 dst->remote_ifindex,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07003806 NTF_SELF, 0, &f, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003807 if (err)
3808 return err;
3809 }
3810
3811 err = register_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003812 if (err)
3813 goto errout;
Petr Machata6db92462018-12-18 13:16:00 +00003814 unregister = true;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003815
Taehee Yoo0ce18222019-10-21 18:47:57 +00003816 if (dst->remote_ifindex) {
3817 remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
3818 if (!remote_dev)
3819 goto errout;
3820
3821 err = netdev_upper_dev_link(remote_dev, dev, extack);
3822 if (err)
3823 goto errout;
3824 }
3825
Roopa Prabhu0241b832018-07-04 16:46:32 -07003826 err = rtnl_configure_link(dev, NULL);
Petr Machata6db92462018-12-18 13:16:00 +00003827 if (err)
Taehee Yoo0ce18222019-10-21 18:47:57 +00003828 goto unlink;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003829
Petr Machata61f46fe2019-01-16 23:06:38 +00003830 if (f) {
Taehee Yoo0ce18222019-10-21 18:47:57 +00003831 vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
Taehee Yoo7c31e542019-06-28 14:07:25 +09003832
3833 /* notify default fdb entry */
Petr Machata61f46fe2019-01-16 23:06:38 +00003834 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
Petr Machata4c59b7d2019-01-16 23:06:54 +00003835 RTM_NEWNEIGH, true, extack);
Taehee Yoo7c31e542019-06-28 14:07:25 +09003836 if (err) {
3837 vxlan_fdb_destroy(vxlan, f, false, false);
Taehee Yoo0ce18222019-10-21 18:47:57 +00003838 if (remote_dev)
3839 netdev_upper_dev_unlink(remote_dev, dev);
Taehee Yoo7c31e542019-06-28 14:07:25 +09003840 goto unregister;
3841 }
Petr Machata61f46fe2019-01-16 23:06:38 +00003842 }
Roopa Prabhu0241b832018-07-04 16:46:32 -07003843
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003844 list_add(&vxlan->next, &vn->vxlan_list);
Taehee Yoo0ce18222019-10-21 18:47:57 +00003845 if (remote_dev)
3846 dst->remote_dev = remote_dev;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003847 return 0;
Taehee Yoo0ce18222019-10-21 18:47:57 +00003848unlink:
3849 if (remote_dev)
3850 netdev_upper_dev_unlink(remote_dev, dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003851errout:
Petr Machata6db92462018-12-18 13:16:00 +00003852 /* unregister_netdevice() destroys the default FDB entry with deletion
3853 * notification. But the addition notification was not sent yet, so
3854 * destroy the entry by hand here.
3855 */
Roopa Prabhu0241b832018-07-04 16:46:32 -07003856 if (f)
Taehee Yoo7c31e542019-06-28 14:07:25 +09003857 __vxlan_fdb_free(f);
3858unregister:
Petr Machata6db92462018-12-18 13:16:00 +00003859 if (unregister)
3860 unregister_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003861 return err;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003862}
3863
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003864/* Set/clear flags based on attribute */
3865static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3866 int attrtype, unsigned long mask, bool changelink,
3867 bool changelink_supported,
3868 struct netlink_ext_ack *extack)
3869{
3870 unsigned long flags;
3871
3872 if (!tb[attrtype])
3873 return 0;
3874
3875 if (changelink && !changelink_supported) {
3876 vxlan_flag_attr_error(attrtype, extack);
3877 return -EOPNOTSUPP;
3878 }
3879
3880 if (vxlan_policy[attrtype].type == NLA_FLAG)
3881 flags = conf->flags | mask;
3882 else if (nla_get_u8(tb[attrtype]))
3883 flags = conf->flags | mask;
3884 else
3885 flags = conf->flags & ~mask;
3886
3887 conf->flags = flags;
3888
3889 return 0;
3890}
3891
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003892static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3893 struct net_device *dev, struct vxlan_config *conf,
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003894 bool changelink, struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003895{
3896 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003897 int err = 0;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003898
3899 memset(conf, 0, sizeof(*conf));
3900
3901 /* if changelink operation, start with old existing cfg */
3902 if (changelink)
3903 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3904
3905 if (data[IFLA_VXLAN_ID]) {
3906 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3907
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003908 if (changelink && (vni != conf->vni)) {
3909 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003910 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003911 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003912 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3913 }
3914
3915 if (data[IFLA_VXLAN_GROUP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003916 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3917 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 +02003918 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003919 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003920
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003921 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003922 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003923 } else if (data[IFLA_VXLAN_GROUP6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003924 if (!IS_ENABLED(CONFIG_IPV6)) {
3925 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003926 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003927 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003928
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003929 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3930 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 +02003931 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003932 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003933
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003934 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3935 conf->remote_ip.sa.sa_family = AF_INET6;
3936 }
3937
3938 if (data[IFLA_VXLAN_LOCAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003939 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
3940 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 +02003941 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003942 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003943
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003944 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3945 conf->saddr.sa.sa_family = AF_INET;
3946 } else if (data[IFLA_VXLAN_LOCAL6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003947 if (!IS_ENABLED(CONFIG_IPV6)) {
3948 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003949 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003950 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003951
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003952 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
3953 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 +02003954 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003955 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003956
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003957 /* TODO: respect scope id */
3958 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3959 conf->saddr.sa.sa_family = AF_INET6;
3960 }
3961
3962 if (data[IFLA_VXLAN_LINK])
3963 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3964
3965 if (data[IFLA_VXLAN_TOS])
3966 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3967
3968 if (data[IFLA_VXLAN_TTL])
3969 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3970
Hangbin Liu72f6d712018-04-17 14:11:28 +08003971 if (data[IFLA_VXLAN_TTL_INHERIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003972 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
3973 VXLAN_F_TTL_INHERIT, changelink, false,
3974 extack);
3975 if (err)
3976 return err;
3977
Hangbin Liu72f6d712018-04-17 14:11:28 +08003978 }
3979
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003980 if (data[IFLA_VXLAN_LABEL])
3981 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3982 IPV6_FLOWLABEL_MASK;
3983
3984 if (data[IFLA_VXLAN_LEARNING]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003985 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
3986 VXLAN_F_LEARN, changelink, true,
3987 extack);
3988 if (err)
3989 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003990 } else if (!changelink) {
3991 /* default to learn on a new device */
3992 conf->flags |= VXLAN_F_LEARN;
3993 }
3994
Ido Schimmel40051c42018-11-21 08:02:40 +00003995 if (data[IFLA_VXLAN_AGEING])
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003996 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003997
3998 if (data[IFLA_VXLAN_PROXY]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003999 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
4000 VXLAN_F_PROXY, changelink, false,
4001 extack);
4002 if (err)
4003 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004004 }
4005
4006 if (data[IFLA_VXLAN_RSC]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004007 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
4008 VXLAN_F_RSC, changelink, false,
4009 extack);
4010 if (err)
4011 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004012 }
4013
4014 if (data[IFLA_VXLAN_L2MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004015 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
4016 VXLAN_F_L2MISS, changelink, false,
4017 extack);
4018 if (err)
4019 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004020 }
4021
4022 if (data[IFLA_VXLAN_L3MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004023 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
4024 VXLAN_F_L3MISS, changelink, false,
4025 extack);
4026 if (err)
4027 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004028 }
4029
4030 if (data[IFLA_VXLAN_LIMIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004031 if (changelink) {
4032 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
4033 "Cannot change limit");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004034 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004035 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004036 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
4037 }
4038
4039 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004040 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
4041 VXLAN_F_COLLECT_METADATA, changelink, false,
4042 extack);
4043 if (err)
4044 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004045 }
4046
4047 if (data[IFLA_VXLAN_PORT_RANGE]) {
4048 if (!changelink) {
4049 const struct ifla_vxlan_port_range *p
4050 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
4051 conf->port_min = ntohs(p->low);
4052 conf->port_max = ntohs(p->high);
4053 } else {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004054 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
4055 "Cannot change port range");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004056 return -EOPNOTSUPP;
4057 }
4058 }
4059
4060 if (data[IFLA_VXLAN_PORT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004061 if (changelink) {
4062 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
4063 "Cannot change port");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004064 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004065 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004066 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
4067 }
4068
4069 if (data[IFLA_VXLAN_UDP_CSUM]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004070 if (changelink) {
4071 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
4072 "Cannot change UDP_CSUM flag");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004073 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004074 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004075 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
4076 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
4077 }
4078
4079 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004080 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4081 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
4082 false, extack);
4083 if (err)
4084 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004085 }
4086
4087 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004088 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4089 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
4090 false, extack);
4091 if (err)
4092 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004093 }
4094
4095 if (data[IFLA_VXLAN_REMCSUM_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004096 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
4097 VXLAN_F_REMCSUM_TX, changelink, false,
4098 extack);
4099 if (err)
4100 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004101 }
4102
4103 if (data[IFLA_VXLAN_REMCSUM_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004104 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
4105 VXLAN_F_REMCSUM_RX, changelink, false,
4106 extack);
4107 if (err)
4108 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004109 }
4110
4111 if (data[IFLA_VXLAN_GBP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004112 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
4113 VXLAN_F_GBP, changelink, false, extack);
4114 if (err)
4115 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004116 }
4117
4118 if (data[IFLA_VXLAN_GPE]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004119 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
4120 VXLAN_F_GPE, changelink, false,
4121 extack);
4122 if (err)
4123 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004124 }
4125
4126 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004127 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
4128 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
4129 false, extack);
4130 if (err)
4131 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004132 }
4133
4134 if (tb[IFLA_MTU]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004135 if (changelink) {
4136 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
4137 "Cannot change mtu");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004138 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004139 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004140 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
4141 }
4142
Stefano Briviob4d306972018-11-08 12:19:16 +01004143 if (data[IFLA_VXLAN_DF])
4144 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
4145
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004146 return 0;
4147}
4148
4149static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02004150 struct nlattr *tb[], struct nlattr *data[],
4151 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004152{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004153 struct vxlan_config conf;
4154 int err;
4155
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004156 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004157 if (err)
4158 return err;
4159
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07004160 return __vxlan_dev_create(src_net, dev, &conf, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00004161}
4162
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004163static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02004164 struct nlattr *data[],
4165 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004166{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004167 struct vxlan_dev *vxlan = netdev_priv(dev);
Petr Machata8db9427d52019-01-16 23:06:39 +00004168 struct net_device *lowerdev;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004169 struct vxlan_config conf;
Taehee Yoo0ce18222019-10-21 18:47:57 +00004170 struct vxlan_rdst *dst;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004171 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004172
Taehee Yoo0ce18222019-10-21 18:47:57 +00004173 dst = &vxlan->default_dst;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08004174 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004175 if (err)
4176 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07004177
Petr Machata8db9427d52019-01-16 23:06:39 +00004178 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
4179 vxlan, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004180 if (err)
4181 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004182
Taehee Yooc6761cf2019-10-30 08:15:12 +00004183 if (dst->remote_dev == lowerdev)
4184 lowerdev = NULL;
4185
Taehee Yoo0ce18222019-10-21 18:47:57 +00004186 err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
4187 extack);
4188 if (err)
4189 return err;
4190
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004191 /* handle default dst entry */
Petr Machata038a5a92019-01-16 23:06:41 +00004192 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
Litao jiaofe1e0712019-06-06 17:57:58 +08004193 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
4194
4195 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata038a5a92019-01-16 23:06:41 +00004196 if (!vxlan_addr_any(&conf.remote_ip)) {
Petr Machatace5e098f2018-12-18 13:16:02 +00004197 err = vxlan_fdb_update(vxlan, all_zeros_mac,
Petr Machata038a5a92019-01-16 23:06:41 +00004198 &conf.remote_ip,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004199 NUD_REACHABLE | NUD_PERMANENT,
Petr Machatace5e098f2018-12-18 13:16:02 +00004200 NLM_F_APPEND | NLM_F_CREATE,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004201 vxlan->cfg.dst_port,
Petr Machata038a5a92019-01-16 23:06:41 +00004202 conf.vni, conf.vni,
4203 conf.remote_ifindex,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07004204 NTF_SELF, 0, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004205 if (err) {
Litao jiaofe1e0712019-06-06 17:57:58 +08004206 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Taehee Yoo0ce18222019-10-21 18:47:57 +00004207 netdev_adjacent_change_abort(dst->remote_dev,
4208 lowerdev, dev);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004209 return err;
4210 }
4211 }
Petr Machata1cdc98c2019-01-16 23:06:43 +00004212 if (!vxlan_addr_any(&dst->remote_ip))
4213 __vxlan_fdb_delete(vxlan, all_zeros_mac,
4214 dst->remote_ip,
4215 vxlan->cfg.dst_port,
4216 dst->remote_vni,
4217 dst->remote_vni,
4218 dst->remote_ifindex,
4219 true);
Litao jiaofe1e0712019-06-06 17:57:58 +08004220 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004221 }
4222
Petr Machata038a5a92019-01-16 23:06:41 +00004223 if (conf.age_interval != vxlan->cfg.age_interval)
4224 mod_timer(&vxlan->age_timer, jiffies);
4225
Taehee Yoo0ce18222019-10-21 18:47:57 +00004226 netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
Taehee Yooc6761cf2019-10-30 08:15:12 +00004227 if (lowerdev && lowerdev != dst->remote_dev) {
Taehee Yoo0ce18222019-10-21 18:47:57 +00004228 dst->remote_dev = lowerdev;
Taehee Yooc6761cf2019-10-30 08:15:12 +00004229 netdev_update_lockdep_key(lowerdev);
4230 }
Petr Machata038a5a92019-01-16 23:06:41 +00004231 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004232 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004233}
4234
stephen hemmingerd3428942012-10-01 12:32:35 +00004235static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4236{
4237 struct vxlan_dev *vxlan = netdev_priv(dev);
4238
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08004239 vxlan_flush(vxlan, true);
4240
stephen hemminger553675f2013-05-16 11:35:20 +00004241 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00004242 unregister_netdevice_queue(dev, head);
Taehee Yoo0ce18222019-10-21 18:47:57 +00004243 if (vxlan->default_dst.remote_dev)
4244 netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00004245}
4246
4247static size_t vxlan_get_size(const struct net_device *dev)
4248{
4249
4250 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08004251 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00004252 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08004253 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00004254 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
Hangbin Liu8fd78062018-09-26 10:35:42 +08004255 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
stephen hemmingerd3428942012-10-01 12:32:35 +00004256 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Stefano Briviob4d306972018-11-08 12:19:16 +01004257 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01004258 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00004259 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00004260 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
4261 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
4262 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
4263 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07004264 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00004265 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
4266 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00004267 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07004268 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4269 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4270 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4271 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08004272 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4273 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00004274 0;
4275}
4276
4277static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4278{
4279 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00004280 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00004281 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004282 .low = htons(vxlan->cfg.port_min),
4283 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00004284 };
stephen hemmingerd3428942012-10-01 12:32:35 +00004285
Jiri Benc54bfd872016-02-16 21:58:58 +01004286 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004287 goto nla_put_failure;
4288
Cong Wange4c7ed42013-08-31 13:44:33 +08004289 if (!vxlan_addr_any(&dst->remote_ip)) {
4290 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004291 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4292 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004293 goto nla_put_failure;
4294#if IS_ENABLED(CONFIG_IPV6)
4295 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004296 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4297 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004298 goto nla_put_failure;
4299#endif
4300 }
4301 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004302
Atzm Watanabec7995c42013-04-16 02:50:52 +00004303 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00004304 goto nla_put_failure;
4305
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004306 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4307 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004308 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004309 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004310 goto nla_put_failure;
4311#if IS_ENABLED(CONFIG_IPV6)
4312 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004313 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004314 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004315 goto nla_put_failure;
4316#endif
4317 }
4318 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004319
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004320 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
Hangbin Liu8fd78062018-09-26 10:35:42 +08004321 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4322 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004323 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Stefano Briviob4d306972018-11-08 12:19:16 +01004324 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01004325 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004326 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Niu Xilei98c81472019-12-30 17:52:22 +08004327 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004328 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Niu Xilei98c81472019-12-30 17:52:22 +08004329 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004330 nla_put_u8(skb, IFLA_VXLAN_RSC,
4331 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004332 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Niu Xilei98c81472019-12-30 17:52:22 +08004333 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004334 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Niu Xilei98c81472019-12-30 17:52:22 +08004335 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07004336 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004337 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004338 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4339 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4340 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004341 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Niu Xilei98c81472019-12-30 17:52:22 +08004342 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004343 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Niu Xilei98c81472019-12-30 17:52:22 +08004344 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004345 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Niu Xilei98c81472019-12-30 17:52:22 +08004346 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004347 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Niu Xilei98c81472019-12-30 17:52:22 +08004348 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004349 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Niu Xilei98c81472019-12-30 17:52:22 +08004350 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004351 goto nla_put_failure;
4352
stephen hemminger05f47d62012-10-09 20:35:50 +00004353 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4354 goto nla_put_failure;
4355
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004356 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01004357 nla_put_flag(skb, IFLA_VXLAN_GBP))
4358 goto nla_put_failure;
4359
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004360 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02004361 nla_put_flag(skb, IFLA_VXLAN_GPE))
4362 goto nla_put_failure;
4363
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004364 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08004365 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4366 goto nla_put_failure;
4367
stephen hemmingerd3428942012-10-01 12:32:35 +00004368 return 0;
4369
4370nla_put_failure:
4371 return -EMSGSIZE;
4372}
4373
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004374static struct net *vxlan_get_link_net(const struct net_device *dev)
4375{
4376 struct vxlan_dev *vxlan = netdev_priv(dev);
4377
4378 return vxlan->net;
4379}
4380
stephen hemmingerd3428942012-10-01 12:32:35 +00004381static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4382 .kind = "vxlan",
4383 .maxtype = IFLA_VXLAN_MAX,
4384 .policy = vxlan_policy,
4385 .priv_size = sizeof(struct vxlan_dev),
4386 .setup = vxlan_setup,
4387 .validate = vxlan_validate,
4388 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004389 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00004390 .dellink = vxlan_dellink,
4391 .get_size = vxlan_get_size,
4392 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004393 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004394};
4395
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004396struct net_device *vxlan_dev_create(struct net *net, const char *name,
4397 u8 name_assign_type,
4398 struct vxlan_config *conf)
4399{
4400 struct nlattr *tb[IFLA_MAX + 1];
4401 struct net_device *dev;
4402 int err;
4403
4404 memset(&tb, 0, sizeof(tb));
4405
4406 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08004407 &vxlan_link_ops, tb, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004408 if (IS_ERR(dev))
4409 return dev;
4410
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07004411 err = __vxlan_dev_create(net, dev, conf, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004412 if (err < 0) {
4413 free_netdev(dev);
4414 return ERR_PTR(err);
4415 }
4416
4417 err = rtnl_configure_link(dev, NULL);
4418 if (err < 0) {
4419 LIST_HEAD(list_kill);
4420
4421 vxlan_dellink(dev, &list_kill);
4422 unregister_netdevice_many(&list_kill);
4423 return ERR_PTR(err);
4424 }
4425
4426 return dev;
4427}
4428EXPORT_SYMBOL_GPL(vxlan_dev_create);
4429
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004430static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4431 struct net_device *dev)
4432{
4433 struct vxlan_dev *vxlan, *next;
4434 LIST_HEAD(list_kill);
4435
4436 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4437 struct vxlan_rdst *dst = &vxlan->default_dst;
4438
4439 /* In case we created vxlan device with carrier
4440 * and we loose the carrier due to module unload
4441 * we also need to remove vxlan device. In other
4442 * cases, it's not necessary and remote_ifindex
4443 * is 0 here, so no matches.
4444 */
4445 if (dst->remote_ifindex == dev->ifindex)
4446 vxlan_dellink(vxlan->dev, &list_kill);
4447 }
4448
4449 unregister_netdevice_many(&list_kill);
4450}
4451
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004452static int vxlan_netdevice_event(struct notifier_block *unused,
4453 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004454{
4455 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004456 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004457
Sabrina Dubroca04584952017-07-21 12:49:33 +02004458 if (event == NETDEV_UNREGISTER) {
4459 vxlan_offload_rx_ports(dev, false);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004460 vxlan_handle_lowerdev_unregister(vn, dev);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004461 } else if (event == NETDEV_REGISTER) {
4462 vxlan_offload_rx_ports(dev, true);
4463 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4464 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02004465 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004466 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004467
4468 return NOTIFY_DONE;
4469}
4470
4471static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004472 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004473};
4474
Petr Machata0efe1172018-10-17 08:53:26 +00004475static void
4476vxlan_fdb_offloaded_set(struct net_device *dev,
4477 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4478{
4479 struct vxlan_dev *vxlan = netdev_priv(dev);
4480 struct vxlan_rdst *rdst;
4481 struct vxlan_fdb *f;
Litao jiaofe1e0712019-06-06 17:57:58 +08004482 u32 hash_index;
Petr Machata0efe1172018-10-17 08:53:26 +00004483
Litao jiaofe1e0712019-06-06 17:57:58 +08004484 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4485
4486 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata0efe1172018-10-17 08:53:26 +00004487
4488 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4489 if (!f)
4490 goto out;
4491
4492 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4493 fdb_info->remote_port,
4494 fdb_info->remote_vni,
4495 fdb_info->remote_ifindex);
4496 if (!rdst)
4497 goto out;
4498
4499 rdst->offloaded = fdb_info->offloaded;
4500
4501out:
Litao jiaofe1e0712019-06-06 17:57:58 +08004502 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata0efe1172018-10-17 08:53:26 +00004503}
4504
Petr Machata5728ae02018-11-21 08:02:39 +00004505static int
4506vxlan_fdb_external_learn_add(struct net_device *dev,
4507 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4508{
4509 struct vxlan_dev *vxlan = netdev_priv(dev);
Petr Machata4c59b7d2019-01-16 23:06:54 +00004510 struct netlink_ext_ack *extack;
Litao jiaofe1e0712019-06-06 17:57:58 +08004511 u32 hash_index;
Petr Machata5728ae02018-11-21 08:02:39 +00004512 int err;
4513
Litao jiaofe1e0712019-06-06 17:57:58 +08004514 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
Petr Machata4c59b7d2019-01-16 23:06:54 +00004515 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4516
Litao jiaofe1e0712019-06-06 17:57:58 +08004517 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004518 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4519 NUD_REACHABLE,
4520 NLM_F_CREATE | NLM_F_REPLACE,
4521 fdb_info->remote_port,
4522 fdb_info->vni,
4523 fdb_info->remote_vni,
4524 fdb_info->remote_ifindex,
4525 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
Roopa Prabhu1274e1c2020-05-21 22:26:14 -07004526 0, false, extack);
Litao jiaofe1e0712019-06-06 17:57:58 +08004527 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004528
4529 return err;
4530}
4531
4532static int
4533vxlan_fdb_external_learn_del(struct net_device *dev,
4534 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4535{
4536 struct vxlan_dev *vxlan = netdev_priv(dev);
4537 struct vxlan_fdb *f;
Litao jiaofe1e0712019-06-06 17:57:58 +08004538 u32 hash_index;
Petr Machata5728ae02018-11-21 08:02:39 +00004539 int err = 0;
4540
Litao jiaofe1e0712019-06-06 17:57:58 +08004541 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4542 spin_lock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004543
4544 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4545 if (!f)
4546 err = -ENOENT;
4547 else if (f->flags & NTF_EXT_LEARNED)
4548 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4549 fdb_info->remote_ip,
4550 fdb_info->remote_port,
4551 fdb_info->vni,
4552 fdb_info->remote_vni,
4553 fdb_info->remote_ifindex,
4554 false);
4555
Litao jiaofe1e0712019-06-06 17:57:58 +08004556 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
Petr Machata5728ae02018-11-21 08:02:39 +00004557
4558 return err;
4559}
4560
Petr Machata0efe1172018-10-17 08:53:26 +00004561static int vxlan_switchdev_event(struct notifier_block *unused,
4562 unsigned long event, void *ptr)
4563{
4564 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Petr Machata5728ae02018-11-21 08:02:39 +00004565 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4566 int err = 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004567
4568 switch (event) {
4569 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4570 vxlan_fdb_offloaded_set(dev, ptr);
4571 break;
Petr Machata5728ae02018-11-21 08:02:39 +00004572 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4573 fdb_info = ptr;
4574 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4575 if (err) {
4576 err = notifier_from_errno(err);
4577 break;
4578 }
4579 fdb_info->offloaded = true;
4580 vxlan_fdb_offloaded_set(dev, fdb_info);
4581 break;
4582 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4583 fdb_info = ptr;
4584 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4585 if (err) {
4586 err = notifier_from_errno(err);
4587 break;
4588 }
4589 fdb_info->offloaded = false;
4590 vxlan_fdb_offloaded_set(dev, fdb_info);
4591 break;
Petr Machata0efe1172018-10-17 08:53:26 +00004592 }
4593
Petr Machata5728ae02018-11-21 08:02:39 +00004594 return err;
Petr Machata0efe1172018-10-17 08:53:26 +00004595}
4596
4597static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4598 .notifier_call = vxlan_switchdev_event,
4599};
4600
stephen hemmingerd3428942012-10-01 12:32:35 +00004601static __net_init int vxlan_init_net(struct net *net)
4602{
4603 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00004604 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00004605
stephen hemminger553675f2013-05-16 11:35:20 +00004606 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07004607 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00004608
stephen hemminger553675f2013-05-16 11:35:20 +00004609 for (h = 0; h < PORT_HASH_SIZE; ++h)
4610 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00004611
4612 return 0;
4613}
4614
Haishuang Yan57b61122017-12-16 17:54:49 +08004615static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004616{
4617 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4618 struct vxlan_dev *vxlan, *next;
4619 struct net_device *dev, *aux;
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004620 unsigned int h;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004621
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004622 for_each_netdev_safe(net, dev, aux)
4623 if (dev->rtnl_link_ops == &vxlan_link_ops)
Haishuang Yan57b61122017-12-16 17:54:49 +08004624 unregister_netdevice_queue(dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004625
4626 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4627 /* If vxlan->dev is in the same netns, it has already been added
4628 * to the list by the previous loop.
4629 */
Zhiqiang Liucc4807b2019-03-16 17:02:54 +08004630 if (!net_eq(dev_net(vxlan->dev), net))
Haishuang Yan57b61122017-12-16 17:54:49 +08004631 unregister_netdevice_queue(vxlan->dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004632 }
4633
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004634 for (h = 0; h < PORT_HASH_SIZE; ++h)
4635 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004636}
4637
Haishuang Yan57b61122017-12-16 17:54:49 +08004638static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4639{
4640 struct net *net;
4641 LIST_HEAD(list);
4642
4643 rtnl_lock();
4644 list_for_each_entry(net, net_list, exit_list)
4645 vxlan_destroy_tunnels(net, &list);
4646
4647 unregister_netdevice_many(&list);
4648 rtnl_unlock();
4649}
4650
stephen hemmingerd3428942012-10-01 12:32:35 +00004651static struct pernet_operations vxlan_net_ops = {
4652 .init = vxlan_init_net,
Haishuang Yan57b61122017-12-16 17:54:49 +08004653 .exit_batch = vxlan_exit_batch_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004654 .id = &vxlan_net_id,
4655 .size = sizeof(struct vxlan_net),
4656};
4657
4658static int __init vxlan_init_module(void)
4659{
4660 int rc;
4661
4662 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4663
Daniel Borkmann783c1462014-01-22 21:07:53 +01004664 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004665 if (rc)
4666 goto out1;
4667
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004668 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004669 if (rc)
4670 goto out2;
4671
Petr Machata0efe1172018-10-17 08:53:26 +00004672 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004673 if (rc)
4674 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00004675
Petr Machata0efe1172018-10-17 08:53:26 +00004676 rc = rtnl_link_register(&vxlan_link_ops);
4677 if (rc)
4678 goto out4;
4679
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004680 return 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004681out4:
4682 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004683out3:
4684 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004685out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01004686 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004687out1:
4688 return rc;
4689}
Cong Wang7332a132013-05-27 22:35:53 +00004690late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00004691
4692static void __exit vxlan_cleanup_module(void)
4693{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07004694 rtnl_link_unregister(&vxlan_link_ops);
Petr Machata0efe1172018-10-17 08:53:26 +00004695 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004696 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004697 unregister_pernet_subsys(&vxlan_net_ops);
4698 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00004699}
4700module_exit(vxlan_cleanup_module);
4701
4702MODULE_LICENSE("GPL");
4703MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004704MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08004705MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00004706MODULE_ALIAS_RTNL_LINK("vxlan");