blob: 4c9bc29fe3d5897761dfd47c983c863bdd5b7b59 [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
stephen hemmingerd3428942012-10-01 12:32:35 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000014#include <linux/module.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000017#include <linux/udp.h>
18#include <linux/igmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000019#include <linux/if_ether.h>
Yan Burman1b13c972013-01-29 23:43:07 +000020#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000021#include <net/arp.h>
22#include <net/ndisc.h>
David Ahern3616d082019-03-22 06:06:09 -070023#include <net/ipv6_stubs.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000024#include <net/ip.h>
25#include <net/icmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000026#include <net/rtnetlink.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000027#include <net/inet_ecn.h>
28#include <net/net_namespace.h>
29#include <net/netns/generic.h>
Jiri Bencfa20e0e2017-08-28 21:43:22 +020030#include <net/tun_proto.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070031#include <net/vxlan.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080032
Cong Wange4c7ed42013-08-31 13:44:33 +080033#if IS_ENABLED(CONFIG_IPV6)
Cong Wange4c7ed42013-08-31 13:44:33 +080034#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080035#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080036#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000037
38#define VXLAN_VERSION "0.1"
39
stephen hemminger553675f2013-05-16 11:35:20 +000040#define PORT_HASH_BITS 8
41#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000042#define FDB_AGE_DEFAULT 300 /* 5 min */
43#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
44
stephen hemminger23c578b2013-04-27 11:31:53 +000045/* UDP port for VXLAN traffic.
46 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070047 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000048 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070049static unsigned short vxlan_port __read_mostly = 8472;
50module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000051MODULE_PARM_DESC(udp_port, "Destination UDP port");
52
53static bool log_ecn_error = true;
54module_param(log_ecn_error, bool, 0644);
55MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
56
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030057static unsigned int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020058static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000059
Li RongQing7256eac2016-01-29 09:43:47 +080060static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030061
Jiri Benc205f3562015-09-24 13:50:01 +020062static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020063
Mark Blocha53cb292017-06-02 03:24:08 +030064static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
65
stephen hemminger553675f2013-05-16 11:35:20 +000066/* per-network namespace private data for this module */
67struct vxlan_net {
68 struct list_head vxlan_list;
69 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070070 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000071};
72
stephen hemmingerd3428942012-10-01 12:32:35 +000073/* Forwarding table entry */
74struct vxlan_fdb {
75 struct hlist_node hlist; /* linked list of entries */
76 struct rcu_head rcu;
77 unsigned long updated; /* jiffies */
78 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070079 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020080 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000081 u16 state; /* see ndm_state */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -080082 __be32 vni;
Petr Machata45598c12018-11-21 08:02:36 +000083 u16 flags; /* see ndm_flags and below */
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{
stephen hemminger5ca54612013-08-04 17:17:39 -0700180 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
181}
182
183static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
184{
185 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700186}
187
Thomas Grafac5132d2015-01-15 03:53:56 +0100188/* Find VXLAN socket based on network namespace, address family and UDP port
189 * and enabled unshareable flags.
190 */
191static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100192 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000193{
194 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800195
196 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000197
198 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200199 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200200 vxlan_get_sk_family(vs) == family &&
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100201 vs->flags == flags &&
202 vs->sock->sk->sk_bound_dev_if == ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000203 return vs;
204 }
205 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000206}
207
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200208static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
209 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000210{
Jiri Benc69e76662017-07-02 19:00:57 +0200211 struct vxlan_dev_node *node;
stephen hemmingerd3428942012-10-01 12:32:35 +0000212
Jiri Bencb9167b22016-02-16 21:59:03 +0100213 /* For flow based devices, map all packets to VNI 0 */
214 if (vs->flags & VXLAN_F_COLLECT_METADATA)
215 vni = 0;
216
Jiri Benc69e76662017-07-02 19:00:57 +0200217 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
218 if (node->vxlan->default_dst.remote_vni != vni)
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200219 continue;
220
221 if (IS_ENABLED(CONFIG_IPV6)) {
Jiri Benc69e76662017-07-02 19:00:57 +0200222 const struct vxlan_config *cfg = &node->vxlan->cfg;
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200223
224 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
225 cfg->remote_ifindex != ifindex)
226 continue;
227 }
228
Jiri Benc69e76662017-07-02 19:00:57 +0200229 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000230 }
231
232 return NULL;
233}
234
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700235/* Look up VNI in a per net namespace table */
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200236static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
237 __be32 vni, sa_family_t family,
238 __be16 port, u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700239{
240 struct vxlan_sock *vs;
241
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100242 vs = vxlan_find_sock(net, family, port, flags, ifindex);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700243 if (!vs)
244 return NULL;
245
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200246 return vxlan_vs_find_vni(vs, ifindex, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700247}
248
stephen hemmingerd3428942012-10-01 12:32:35 +0000249/* Fill in neighbour message in skbuff. */
250static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700251 const struct vxlan_fdb *fdb,
252 u32 portid, u32 seq, int type, unsigned int flags,
253 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000254{
255 unsigned long now = jiffies;
256 struct nda_cacheinfo ci;
257 struct nlmsghdr *nlh;
258 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000259 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000260
261 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
262 if (nlh == NULL)
263 return -EMSGSIZE;
264
265 ndm = nlmsg_data(nlh);
266 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000267
268 send_eth = send_ip = true;
269
270 if (type == RTM_GETNEIGH) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800271 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000272 send_eth = !is_zero_ether_addr(fdb->eth_addr);
Vincent Bernat8f48ba72017-03-10 16:30:24 +0100273 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
David Stevense4f67ad2012-11-20 02:50:14 +0000274 } else
275 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000276 ndm->ndm_state = fdb->state;
277 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000278 ndm->ndm_flags = fdb->flags;
Petr Machata0efe1172018-10-17 08:53:26 +0000279 if (rdst->offloaded)
280 ndm->ndm_flags |= NTF_OFFLOADED;
Jun Zhao545469f2014-07-26 00:38:59 +0800281 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000282
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100283 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100284 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700285 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100286 goto nla_put_failure;
287
David Stevense4f67ad2012-11-20 02:50:14 +0000288 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000289 goto nla_put_failure;
290
Cong Wange4c7ed42013-08-31 13:44:33 +0800291 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000292 goto nla_put_failure;
293
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200294 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000295 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
296 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000297 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100298 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000299 goto nla_put_failure;
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200300 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800301 nla_put_u32(skb, NDA_SRC_VNI,
302 be32_to_cpu(fdb->vni)))
303 goto nla_put_failure;
David Stevens66817122013-03-15 04:35:51 +0000304 if (rdst->remote_ifindex &&
305 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000306 goto nla_put_failure;
307
308 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
309 ci.ndm_confirmed = 0;
310 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
311 ci.ndm_refcnt = 0;
312
313 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
314 goto nla_put_failure;
315
Johannes Berg053c0952015-01-16 22:09:00 +0100316 nlmsg_end(skb, nlh);
317 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000318
319nla_put_failure:
320 nlmsg_cancel(skb, nlh);
321 return -EMSGSIZE;
322}
323
324static inline size_t vxlan_nlmsg_size(void)
325{
326 return NLMSG_ALIGN(sizeof(struct ndmsg))
327 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800328 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000329 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000330 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
331 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100332 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000333 + nla_total_size(sizeof(struct nda_cacheinfo));
334}
335
Petr Machata9a997352018-10-17 08:53:22 +0000336static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
337 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000338{
339 struct net *net = dev_net(vxlan->dev);
340 struct sk_buff *skb;
341 int err = -ENOBUFS;
342
343 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
344 if (skb == NULL)
345 goto errout;
346
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200347 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000348 if (err < 0) {
349 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
350 WARN_ON(err == -EMSGSIZE);
351 kfree_skb(skb);
352 goto errout;
353 }
354
355 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
356 return;
357errout:
358 if (err < 0)
359 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
360}
361
Petr Machataff23b912018-12-07 19:55:03 +0000362static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
363 const struct vxlan_fdb *fdb,
364 const struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000365 struct netlink_ext_ack *extack,
Petr Machataff23b912018-12-07 19:55:03 +0000366 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
367{
368 fdb_info->info.dev = vxlan->dev;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000369 fdb_info->info.extack = extack;
Petr Machataff23b912018-12-07 19:55:03 +0000370 fdb_info->remote_ip = rd->remote_ip;
371 fdb_info->remote_port = rd->remote_port;
372 fdb_info->remote_vni = rd->remote_vni;
373 fdb_info->remote_ifindex = rd->remote_ifindex;
374 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
375 fdb_info->vni = fdb->vni;
376 fdb_info->offloaded = rd->offloaded;
377 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
378}
379
Petr Machata61f46fe2019-01-16 23:06:38 +0000380static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
381 struct vxlan_fdb *fdb,
382 struct vxlan_rdst *rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000383 bool adding,
384 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000385{
386 struct switchdev_notifier_vxlan_fdb_info info;
387 enum switchdev_notifier_type notifier_type;
Petr Machata61f46fe2019-01-16 23:06:38 +0000388 int ret;
Petr Machata9a997352018-10-17 08:53:22 +0000389
390 if (WARN_ON(!rd))
Petr Machata61f46fe2019-01-16 23:06:38 +0000391 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000392
393 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
394 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
Petr Machata4c59b7d2019-01-16 23:06:54 +0000395 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
Petr Machata61f46fe2019-01-16 23:06:38 +0000396 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
Petr Machata66859872019-01-16 23:06:56 +0000397 &info.info, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000398 return notifier_to_errno(ret);
Petr Machata9a997352018-10-17 08:53:22 +0000399}
400
Petr Machata61f46fe2019-01-16 23:06:38 +0000401static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000402 struct vxlan_rdst *rd, int type, bool swdev_notify,
403 struct netlink_ext_ack *extack)
Petr Machata9a997352018-10-17 08:53:22 +0000404{
Petr Machata61f46fe2019-01-16 23:06:38 +0000405 int err;
406
Petr Machata0e6160f2018-11-21 08:02:35 +0000407 if (swdev_notify) {
408 switch (type) {
409 case RTM_NEWNEIGH:
Petr Machata61f46fe2019-01-16 23:06:38 +0000410 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000411 true, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000412 if (err)
413 return err;
Petr Machata0e6160f2018-11-21 08:02:35 +0000414 break;
415 case RTM_DELNEIGH:
416 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000417 false, extack);
Petr Machata0e6160f2018-11-21 08:02:35 +0000418 break;
419 }
Petr Machata9a997352018-10-17 08:53:22 +0000420 }
421
422 __vxlan_fdb_notify(vxlan, fdb, rd, type);
Petr Machata61f46fe2019-01-16 23:06:38 +0000423 return 0;
Petr Machata9a997352018-10-17 08:53:22 +0000424}
425
Cong Wange4c7ed42013-08-31 13:44:33 +0800426static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000427{
428 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700429 struct vxlan_fdb f = {
430 .state = NUD_STALE,
431 };
432 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800433 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100434 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700435 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700436
Petr Machata4c59b7d2019-01-16 23:06:54 +0000437 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000438}
439
440static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
441{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700442 struct vxlan_fdb f = {
443 .state = NUD_STALE,
444 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200445 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000446
David Stevense4f67ad2012-11-20 02:50:14 +0000447 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
448
Petr Machata4c59b7d2019-01-16 23:06:54 +0000449 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
David Stevense4f67ad2012-11-20 02:50:14 +0000450}
451
stephen hemmingerd3428942012-10-01 12:32:35 +0000452/* Hash Ethernet address */
453static u32 eth_hash(const unsigned char *addr)
454{
455 u64 value = get_unaligned((u64 *)addr);
456
457 /* only want 6 bytes */
458#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000459 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000460#else
461 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000462#endif
463 return hash_64(value, FDB_HASH_BITS);
464}
465
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800466static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
467{
468 /* use 1 byte of OUI and 3 bytes of NIC */
469 u32 key = get_unaligned((u32 *)(addr + 2));
470
471 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
472}
473
stephen hemmingerd3428942012-10-01 12:32:35 +0000474/* Hash chain to use given mac address */
475static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800476 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000477{
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200478 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800479 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
480 else
481 return &vxlan->fdb_head[eth_hash(mac)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000482}
483
484/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000485static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800486 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000487{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800488 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000489 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000490
Sasha Levinb67bfe02013-02-27 17:06:00 -0800491 hlist_for_each_entry_rcu(f, head, hlist) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800492 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200493 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800494 if (vni == f->vni)
495 return f;
496 } else {
497 return f;
498 }
499 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000500 }
501
502 return NULL;
503}
504
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000505static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800506 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000507{
508 struct vxlan_fdb *f;
509
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800510 f = __vxlan_find_mac(vxlan, mac, vni);
Li RongQing016f3d12018-08-29 11:52:10 +0800511 if (f && f->used != jiffies)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000512 f->used = jiffies;
513
514 return f;
515}
516
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300517/* caller should hold vxlan->hash_lock */
518static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800519 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100520 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300521{
522 struct vxlan_rdst *rd;
523
524 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800525 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300526 rd->remote_port == port &&
527 rd->remote_vni == vni &&
528 rd->remote_ifindex == ifindex)
529 return rd;
530 }
531
532 return NULL;
533}
534
Petr Machata1941f1d2018-10-17 08:53:24 +0000535int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
536 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
537{
538 struct vxlan_dev *vxlan = netdev_priv(dev);
539 u8 eth_addr[ETH_ALEN + 2] = { 0 };
540 struct vxlan_rdst *rdst;
541 struct vxlan_fdb *f;
542 int rc = 0;
543
544 if (is_multicast_ether_addr(mac) ||
545 is_zero_ether_addr(mac))
546 return -EINVAL;
547
548 ether_addr_copy(eth_addr, mac);
549
550 rcu_read_lock();
551
552 f = __vxlan_find_mac(vxlan, eth_addr, vni);
553 if (!f) {
554 rc = -ENOENT;
555 goto out;
556 }
557
558 rdst = first_remote_rcu(f);
Petr Machata4c59b7d2019-01-16 23:06:54 +0000559 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
Petr Machata1941f1d2018-10-17 08:53:24 +0000560
561out:
562 rcu_read_unlock();
563 return rc;
564}
565EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
566
Petr Machata4f89f5b2018-12-07 19:55:04 +0000567static int vxlan_fdb_notify_one(struct notifier_block *nb,
568 const struct vxlan_dev *vxlan,
569 const struct vxlan_fdb *f,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000570 const struct vxlan_rdst *rdst,
571 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000572{
573 struct switchdev_notifier_vxlan_fdb_info fdb_info;
574 int rc;
575
Petr Machata4c59b7d2019-01-16 23:06:54 +0000576 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000577 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
578 &fdb_info);
579 return notifier_to_errno(rc);
580}
581
582int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000583 struct notifier_block *nb,
584 struct netlink_ext_ack *extack)
Petr Machata4f89f5b2018-12-07 19:55:04 +0000585{
586 struct vxlan_dev *vxlan;
587 struct vxlan_rdst *rdst;
588 struct vxlan_fdb *f;
589 unsigned int h;
590 int rc = 0;
591
592 if (!netif_is_vxlan(dev))
593 return -EINVAL;
594 vxlan = netdev_priv(dev);
595
596 spin_lock_bh(&vxlan->hash_lock);
597 for (h = 0; h < FDB_HASH_SIZE; ++h) {
598 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
599 if (f->vni == vni) {
600 list_for_each_entry(rdst, &f->remotes, list) {
601 rc = vxlan_fdb_notify_one(nb, vxlan,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000602 f, rdst,
603 extack);
Petr Machata4f89f5b2018-12-07 19:55:04 +0000604 if (rc)
605 goto out;
606 }
607 }
608 }
609 }
610
611out:
612 spin_unlock_bh(&vxlan->hash_lock);
613 return rc;
614}
615EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
616
Petr Machatae5ff4b12018-12-07 19:55:06 +0000617void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
618{
619 struct vxlan_dev *vxlan;
620 struct vxlan_rdst *rdst;
621 struct vxlan_fdb *f;
622 unsigned int h;
623
624 if (!netif_is_vxlan(dev))
625 return;
626 vxlan = netdev_priv(dev);
627
628 spin_lock_bh(&vxlan->hash_lock);
629 for (h = 0; h < FDB_HASH_SIZE; ++h) {
630 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
631 if (f->vni == vni)
632 list_for_each_entry(rdst, &f->remotes, list)
633 rdst->offloaded = false;
634 }
635 spin_unlock_bh(&vxlan->hash_lock);
636}
637EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
638
Thomas Richter906dc182013-07-19 17:20:07 +0200639/* Replace destination of unicast mac */
640static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100641 union vxlan_addr *ip, __be16 port, __be32 vni,
Petr Machataccdfd4f2019-01-16 23:06:34 +0000642 __u32 ifindex, struct vxlan_rdst *oldrd)
Thomas Richter906dc182013-07-19 17:20:07 +0200643{
644 struct vxlan_rdst *rd;
645
646 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
647 if (rd)
648 return 0;
649
650 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
651 if (!rd)
652 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100653
Petr Machataccdfd4f2019-01-16 23:06:34 +0000654 *oldrd = *rd;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100655 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800656 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200657 rd->remote_port = port;
658 rd->remote_vni = vni;
659 rd->remote_ifindex = ifindex;
Petr Machata6ad0b5a2018-12-18 13:15:59 +0000660 rd->offloaded = false;
Thomas Richter906dc182013-07-19 17:20:07 +0200661 return 1;
662}
663
David Stevens66817122013-03-15 04:35:51 +0000664/* Add/update destinations for multicast */
665static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100666 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200667 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000668{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700669 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000670
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300671 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
672 if (rd)
673 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700674
David Stevens66817122013-03-15 04:35:51 +0000675 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
676 if (rd == NULL)
677 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100678
679 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
680 kfree(rd);
681 return -ENOBUFS;
682 }
683
Cong Wange4c7ed42013-08-31 13:44:33 +0800684 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000685 rd->remote_port = port;
Petr Machata0efe1172018-10-17 08:53:26 +0000686 rd->offloaded = false;
David Stevens66817122013-03-15 04:35:51 +0000687 rd->remote_vni = vni;
688 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700689
690 list_add_tail_rcu(&rd->list, &f->remotes);
691
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200692 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000693 return 1;
694}
695
Tom Herbertdfd86452015-01-12 17:00:38 -0800696static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
697 unsigned int off,
698 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100699 __be32 vni_field,
700 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800701 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800702{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700703 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800704
705 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700706 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800707
708 if (!NAPI_GRO_CB(skb)->csum_valid)
709 return NULL;
710
Jiri Benc54bfd872016-02-16 21:58:58 +0100711 start = vxlan_rco_start(vni_field);
712 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800713
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700714 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
715 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800716
717 skb->remcsum_offload = 1;
718
719 return vh;
720}
721
David Millerd4546c22018-06-24 14:13:49 +0900722static struct sk_buff *vxlan_gro_receive(struct sock *sk,
723 struct list_head *head,
724 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200725{
David Millerd4546c22018-06-24 14:13:49 +0900726 struct sk_buff *pp = NULL;
727 struct sk_buff *p;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200728 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800729 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200730 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700731 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100732 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800733 struct gro_remcsum grc;
734
735 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200736
737 off_vx = skb_gro_offset(skb);
738 hlen = off_vx + sizeof(*vh);
739 vh = skb_gro_header_fast(skb, off_vx);
740 if (skb_gro_header_hard(skb, hlen)) {
741 vh = skb_gro_header_slow(skb, hlen, off_vx);
742 if (unlikely(!vh))
743 goto out;
744 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200745
Tom Herbertdfd86452015-01-12 17:00:38 -0800746 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
747
Jiri Benc54bfd872016-02-16 21:58:58 +0100748 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800749
750 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
751 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100752 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800753 !!(vs->flags &
754 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800755
756 if (!vh)
757 goto out;
758 }
759
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700760 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
761
David Millerd4546c22018-06-24 14:13:49 +0900762 list_for_each_entry(p, head, list) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200763 if (!NAPI_GRO_CB(p)->same_flow)
764 continue;
765
766 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100767 if (vh->vx_flags != vh2->vx_flags ||
768 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200769 NAPI_GRO_CB(p)->same_flow = 0;
770 continue;
771 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200772 }
773
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200774 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800775 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200776
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200777out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200778 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200779
780 return pp;
781}
782
Tom Herbert5602c482016-04-05 08:22:53 -0700783static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200784{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700785 /* Sets 'skb->inner_mac_header' since we are always called with
786 * 'skb->encapsulation' set.
787 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800788 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200789}
790
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700791static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan,
792 const u8 *mac, __u16 state,
Petr Machata45598c12018-11-21 08:02:36 +0000793 __be32 src_vni, __u16 ndm_flags)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700794{
795 struct vxlan_fdb *f;
796
797 f = kmalloc(sizeof(*f), GFP_ATOMIC);
798 if (!f)
799 return NULL;
800 f->state = state;
801 f->flags = ndm_flags;
802 f->updated = f->used = jiffies;
803 f->vni = src_vni;
804 INIT_LIST_HEAD(&f->remotes);
805 memcpy(f->eth_addr, mac, ETH_ALEN);
806
807 return f;
808}
809
stephen hemmingerd3428942012-10-01 12:32:35 +0000810static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800811 const u8 *mac, union vxlan_addr *ip,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700812 __u16 state, __be16 port, __be32 src_vni,
Petr Machata45598c12018-11-21 08:02:36 +0000813 __be32 vni, __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700814 struct vxlan_fdb **fdb)
815{
816 struct vxlan_rdst *rd = NULL;
817 struct vxlan_fdb *f;
818 int rc;
819
820 if (vxlan->cfg.addrmax &&
821 vxlan->addrcnt >= vxlan->cfg.addrmax)
822 return -ENOSPC;
823
824 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
825 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
826 if (!f)
827 return -ENOMEM;
828
829 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
830 if (rc < 0) {
831 kfree(f);
832 return rc;
833 }
834
835 ++vxlan->addrcnt;
836 hlist_add_head_rcu(&f->hlist,
837 vxlan_fdb_head(vxlan, mac, src_vni));
838
839 *fdb = f;
840
841 return 0;
842}
843
Petr Machatac2b200e2019-01-16 23:06:30 +0000844static void vxlan_fdb_free(struct rcu_head *head)
845{
846 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
847 struct vxlan_rdst *rd, *nd;
848
849 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
850 dst_cache_destroy(&rd->dst_cache);
851 kfree(rd);
852 }
853 kfree(f);
854}
855
856static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
857 bool do_notify, bool swdev_notify)
858{
859 struct vxlan_rdst *rd;
860
861 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
862
863 --vxlan->addrcnt;
864 if (do_notify)
865 list_for_each_entry(rd, &f->remotes, list)
866 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000867 swdev_notify, NULL);
Petr Machatac2b200e2019-01-16 23:06:30 +0000868
869 hlist_del_rcu(&f->hlist);
870 call_rcu(&f->rcu, vxlan_fdb_free);
871}
872
Petr Machatafc4aa1c2019-02-07 12:18:02 +0000873static void vxlan_dst_free(struct rcu_head *head)
874{
875 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
876
877 dst_cache_destroy(&rd->dst_cache);
878 kfree(rd);
879}
880
Petr Machataa76d1ca2019-01-16 23:06:32 +0000881static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
882 union vxlan_addr *ip,
883 __u16 state, __u16 flags,
884 __be16 port, __be32 vni,
885 __u32 ifindex, __u16 ndm_flags,
886 struct vxlan_fdb *f,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000887 bool swdev_notify,
888 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +0000889{
890 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
891 struct vxlan_rdst *rd = NULL;
Petr Machataccdfd4f2019-01-16 23:06:34 +0000892 struct vxlan_rdst oldrd;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000893 int notify = 0;
Petr Machata61f46fe2019-01-16 23:06:38 +0000894 int rc = 0;
895 int err;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000896
897 /* Do not allow an externally learned entry to take over an entry added
898 * by the user.
899 */
900 if (!(fdb_flags & NTF_EXT_LEARNED) ||
901 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
902 if (f->state != state) {
903 f->state = state;
904 f->updated = jiffies;
905 notify = 1;
906 }
907 if (f->flags != fdb_flags) {
908 f->flags = fdb_flags;
909 f->updated = jiffies;
910 notify = 1;
911 }
912 }
913
914 if ((flags & NLM_F_REPLACE)) {
915 /* Only change unicasts */
916 if (!(is_multicast_ether_addr(f->eth_addr) ||
917 is_zero_ether_addr(f->eth_addr))) {
918 rc = vxlan_fdb_replace(f, ip, port, vni,
Petr Machataccdfd4f2019-01-16 23:06:34 +0000919 ifindex, &oldrd);
Petr Machataa76d1ca2019-01-16 23:06:32 +0000920 notify |= rc;
921 } else {
922 return -EOPNOTSUPP;
923 }
924 }
925 if ((flags & NLM_F_APPEND) &&
926 (is_multicast_ether_addr(f->eth_addr) ||
927 is_zero_ether_addr(f->eth_addr))) {
928 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
929
930 if (rc < 0)
931 return rc;
932 notify |= rc;
933 }
934
935 if (ndm_flags & NTF_USE)
936 f->used = jiffies;
937
938 if (notify) {
939 if (rd == NULL)
940 rd = first_remote_rtnl(f);
941
Petr Machata61f46fe2019-01-16 23:06:38 +0000942 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000943 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000944 if (err)
945 goto err_notify;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000946 }
947
948 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +0000949
950err_notify:
951 if ((flags & NLM_F_REPLACE) && rc)
952 *rd = oldrd;
Petr Machatafc4aa1c2019-02-07 12:18:02 +0000953 else if ((flags & NLM_F_APPEND) && rc) {
Petr Machata61f46fe2019-01-16 23:06:38 +0000954 list_del_rcu(&rd->list);
Petr Machatafc4aa1c2019-02-07 12:18:02 +0000955 call_rcu(&rd->rcu, vxlan_dst_free);
956 }
Petr Machata61f46fe2019-01-16 23:06:38 +0000957 return err;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000958}
959
960static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
961 const u8 *mac, union vxlan_addr *ip,
962 __u16 state, __u16 flags,
963 __be16 port, __be32 src_vni, __be32 vni,
964 __u32 ifindex, __u16 ndm_flags,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000965 bool swdev_notify,
966 struct netlink_ext_ack *extack)
Petr Machataa76d1ca2019-01-16 23:06:32 +0000967{
968 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
969 struct vxlan_fdb *f;
970 int rc;
971
972 /* Disallow replace to add a multicast entry */
973 if ((flags & NLM_F_REPLACE) &&
974 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
975 return -EOPNOTSUPP;
976
977 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
978 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
979 vni, ifindex, fdb_flags, &f);
980 if (rc < 0)
981 return rc;
982
Petr Machata61f46fe2019-01-16 23:06:38 +0000983 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
Petr Machata4c59b7d2019-01-16 23:06:54 +0000984 swdev_notify, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +0000985 if (rc)
986 goto err_notify;
987
Petr Machataa76d1ca2019-01-16 23:06:32 +0000988 return 0;
Petr Machata61f46fe2019-01-16 23:06:38 +0000989
990err_notify:
991 vxlan_fdb_destroy(vxlan, f, false, false);
992 return rc;
Petr Machataa76d1ca2019-01-16 23:06:32 +0000993}
994
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700995/* Add new entry to forwarding table -- assumes lock held */
996static int vxlan_fdb_update(struct vxlan_dev *vxlan,
997 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000998 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800999 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata45598c12018-11-21 08:02:36 +00001000 __u32 ifindex, __u16 ndm_flags,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001001 bool swdev_notify,
1002 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001003{
1004 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001005
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001006 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001007 if (f) {
1008 if (flags & NLM_F_EXCL) {
1009 netdev_dbg(vxlan->dev,
1010 "lost race to create %pM\n", mac);
1011 return -EEXIST;
1012 }
Petr Machata0ec566a2018-11-21 08:02:37 +00001013
Petr Machataa76d1ca2019-01-16 23:06:32 +00001014 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1015 vni, ifindex, ndm_flags, f,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001016 swdev_notify, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001017 } else {
1018 if (!(flags & NLM_F_CREATE))
1019 return -ENOENT;
1020
Petr Machataa76d1ca2019-01-16 23:06:32 +00001021 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1022 port, src_vni, vni, ifindex,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001023 ndm_flags, swdev_notify, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001024 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001025}
1026
Lance Richardson35cf2842017-05-29 13:25:57 -04001027static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +00001028 struct vxlan_rdst *rd, bool swdev_notify)
Lance Richardson35cf2842017-05-29 13:25:57 -04001029{
1030 list_del_rcu(&rd->list);
Petr Machata4c59b7d2019-01-16 23:06:54 +00001031 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
Lance Richardson35cf2842017-05-29 13:25:57 -04001032 call_rcu(&rd->rcu, vxlan_dst_free);
1033}
1034
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001035static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001036 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
1037 __be32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001038{
1039 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001040 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001041
1042 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001043 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1044 if (err)
1045 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001046 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08001047 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
1048 if (remote->sa.sa_family == AF_INET) {
1049 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1050 ip->sa.sa_family = AF_INET;
1051#if IS_ENABLED(CONFIG_IPV6)
1052 } else {
1053 ip->sin6.sin6_addr = in6addr_any;
1054 ip->sa.sa_family = AF_INET6;
1055#endif
1056 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001057 }
1058
1059 if (tb[NDA_PORT]) {
1060 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1061 return -EINVAL;
1062 *port = nla_get_be16(tb[NDA_PORT]);
1063 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001064 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001065 }
1066
1067 if (tb[NDA_VNI]) {
1068 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1069 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +01001070 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001071 } else {
1072 *vni = vxlan->default_dst.remote_vni;
1073 }
1074
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001075 if (tb[NDA_SRC_VNI]) {
1076 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1077 return -EINVAL;
1078 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1079 } else {
1080 *src_vni = vxlan->default_dst.remote_vni;
1081 }
1082
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001083 if (tb[NDA_IFINDEX]) {
1084 struct net_device *tdev;
1085
1086 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1087 return -EINVAL;
1088 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +08001089 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001090 if (!tdev)
1091 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001092 } else {
1093 *ifindex = 0;
1094 }
1095
1096 return 0;
1097}
1098
stephen hemmingerd3428942012-10-01 12:32:35 +00001099/* Add static entry (via netlink) */
1100static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1101 struct net_device *dev,
Petr Machata87b09842019-01-16 23:06:50 +00001102 const unsigned char *addr, u16 vid, u16 flags,
1103 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00001104{
1105 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001106 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +08001107 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +00001108 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001109 __be32 src_vni, vni;
Jiri Benc54bfd872016-02-16 21:58:58 +01001110 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001111 int err;
1112
1113 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1114 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1115 ndm->ndm_state);
1116 return -EINVAL;
1117 }
1118
1119 if (tb[NDA_DST] == NULL)
1120 return -EINVAL;
1121
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001122 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +03001123 if (err)
1124 return err;
David Stevens66817122013-03-15 04:35:51 +00001125
Mike Rapoport5933a7b2014-04-01 09:23:01 +03001126 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1127 return -EAFNOSUPPORT;
1128
stephen hemmingerd3428942012-10-01 12:32:35 +00001129 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001130 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
Petr Machata45598c12018-11-21 08:02:36 +00001131 port, src_vni, vni, ifindex,
1132 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001133 true, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00001134 spin_unlock_bh(&vxlan->hash_lock);
1135
1136 return err;
1137}
1138
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001139static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1140 const unsigned char *addr, union vxlan_addr ip,
Xin Longfc39c382017-11-26 21:19:05 +08001141 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001142 u32 ifindex, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +00001143{
stephen hemmingerd3428942012-10-01 12:32:35 +00001144 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001145 struct vxlan_rdst *rd = NULL;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001146 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001147
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001148 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001149 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001150 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001151
Cong Wange4c7ed42013-08-31 13:44:33 +08001152 if (!vxlan_addr_any(&ip)) {
1153 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001154 if (!rd)
1155 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +00001156 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001157
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001158 /* remove a destination if it's not the only one on the list,
1159 * otherwise destroy the fdb entry
1160 */
1161 if (rd && !list_is_singular(&f->remotes)) {
Petr Machata0e6160f2018-11-21 08:02:35 +00001162 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001163 goto out;
1164 }
1165
Petr Machata0e6160f2018-11-21 08:02:35 +00001166 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001167
1168out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001169 return 0;
1170}
1171
1172/* Delete entry (via netlink) */
1173static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1174 struct net_device *dev,
1175 const unsigned char *addr, u16 vid)
1176{
1177 struct vxlan_dev *vxlan = netdev_priv(dev);
1178 union vxlan_addr ip;
1179 __be32 src_vni, vni;
1180 __be16 port;
1181 u32 ifindex;
1182 int err;
1183
1184 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
1185 if (err)
1186 return err;
1187
1188 spin_lock_bh(&vxlan->hash_lock);
Petr Machata0e6160f2018-11-21 08:02:35 +00001189 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1190 true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001191 spin_unlock_bh(&vxlan->hash_lock);
1192
1193 return err;
1194}
1195
1196/* Dump forwarding table */
1197static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04001198 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07001199 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +00001200{
1201 struct vxlan_dev *vxlan = netdev_priv(dev);
1202 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -07001203 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001204
1205 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1206 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001207
Sasha Levinb67bfe02013-02-27 17:06:00 -08001208 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +00001209 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +00001210
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001211 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07001212 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001213 goto skip;
1214
David Stevens66817122013-03-15 04:35:51 +00001215 err = vxlan_fdb_info(skb, vxlan, f,
1216 NETLINK_CB(cb->skb).portid,
1217 cb->nlh->nlmsg_seq,
1218 RTM_NEWNEIGH,
1219 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -07001220 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001221 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001222skip:
Roopa Prabhud2976532016-08-30 21:56:45 -07001223 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001224 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001225 }
1226 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001227out:
Roopa Prabhud2976532016-08-30 21:56:45 -07001228 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001229}
1230
Roopa Prabhu474c3c82018-12-15 22:35:10 -08001231static int vxlan_fdb_get(struct sk_buff *skb,
1232 struct nlattr *tb[],
1233 struct net_device *dev,
1234 const unsigned char *addr,
1235 u16 vid, u32 portid, u32 seq,
1236 struct netlink_ext_ack *extack)
1237{
1238 struct vxlan_dev *vxlan = netdev_priv(dev);
1239 struct vxlan_fdb *f;
1240 __be32 vni;
1241 int err;
1242
1243 if (tb[NDA_VNI])
1244 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1245 else
1246 vni = vxlan->default_dst.remote_vni;
1247
1248 rcu_read_lock();
1249
1250 f = __vxlan_find_mac(vxlan, addr, vni);
1251 if (!f) {
1252 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1253 err = -ENOENT;
1254 goto errout;
1255 }
1256
1257 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1258 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1259errout:
1260 rcu_read_unlock();
1261 return err;
1262}
1263
stephen hemmingerd3428942012-10-01 12:32:35 +00001264/* Watch incoming packets to learn mapping between Ethernet address
1265 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +09001266 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +00001267 */
stephen hemminger26a41ae62013-06-17 12:09:58 -07001268static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001269 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001270 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +00001271{
1272 struct vxlan_dev *vxlan = netdev_priv(dev);
1273 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001274 u32 ifindex = 0;
1275
1276#if IS_ENABLED(CONFIG_IPV6)
1277 if (src_ip->sa.sa_family == AF_INET6 &&
1278 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1279 ifindex = src_ifindex;
1280#endif
stephen hemmingerd3428942012-10-01 12:32:35 +00001281
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001282 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001283 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001284 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001285
Matthias Schiffer87613de2017-06-19 10:03:59 +02001286 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1287 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001288 return false;
1289
1290 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -07001291 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001292 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001293
1294 if (net_ratelimit())
1295 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001296 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001297 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001298
Cong Wange4c7ed42013-08-31 13:44:33 +08001299 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001300 f->updated = jiffies;
Petr Machata4c59b7d2019-01-16 23:06:54 +00001301 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
stephen hemmingerd3428942012-10-01 12:32:35 +00001302 } else {
1303 /* learned new entry */
1304 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001305
1306 /* close off race between vxlan_flush and incoming packets */
1307 if (netif_running(dev))
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001308 vxlan_fdb_update(vxlan, src_mac, src_ip,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001309 NUD_REACHABLE,
1310 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001311 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001312 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001313 vxlan->default_dst.remote_vni,
Petr Machata4c59b7d2019-01-16 23:06:54 +00001314 ifindex, NTF_SELF, true, NULL);
stephen hemmingerd3428942012-10-01 12:32:35 +00001315 spin_unlock(&vxlan->hash_lock);
1316 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001317
1318 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001319}
1320
stephen hemmingerd3428942012-10-01 12:32:35 +00001321/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001322static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001323{
stephen hemminger553675f2013-05-16 11:35:20 +00001324 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001325 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001326#if IS_ENABLED(CONFIG_IPV6)
1327 struct vxlan_sock *sock6;
1328#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001329 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001330
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001331 sock4 = rtnl_dereference(dev->vn4_sock);
1332
Gao feng95ab0992013-12-10 16:37:33 +08001333 /* The vxlan_sock is only used by dev, leaving group has
1334 * no effect on other vxlan devices.
1335 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001336 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001337 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001338#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001339 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001340 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001341 return false;
1342#endif
Gao feng95ab0992013-12-10 16:37:33 +08001343
stephen hemminger553675f2013-05-16 11:35:20 +00001344 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001345 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001346 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001347
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001348 if (family == AF_INET &&
1349 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001350 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001351#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001352 if (family == AF_INET6 &&
1353 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001354 continue;
1355#endif
Gao feng95ab0992013-12-10 16:37:33 +08001356
1357 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1358 &dev->default_dst.remote_ip))
1359 continue;
1360
1361 if (vxlan->default_dst.remote_ifindex !=
1362 dev->default_dst.remote_ifindex)
1363 continue;
1364
1365 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001366 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001367
1368 return false;
1369}
1370
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001371static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001372{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001373 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001374
Jiri Bencb1be00a2015-09-24 13:50:02 +02001375 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001376 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001377 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001378 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001379
Jiri Bencb1be00a2015-09-24 13:50:02 +02001380 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001381 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001382 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001383 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001384 (vs->flags & VXLAN_F_GPE) ?
1385 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001386 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001387 spin_unlock(&vn->sock_lock);
1388
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001389 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001390}
1391
Jiri Bencb1be00a2015-09-24 13:50:02 +02001392static void vxlan_sock_release(struct vxlan_dev *vxlan)
1393{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001394 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001395#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001396 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1397
Mark Bloch57d88182017-06-07 14:36:58 +03001398 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001399#endif
1400
Mark Bloch57d88182017-06-07 14:36:58 +03001401 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001402 synchronize_net();
1403
Mark Blocha53cb292017-06-02 03:24:08 +03001404 vxlan_vs_del_dev(vxlan);
1405
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001406 if (__vxlan_sock_release_prep(sock4)) {
1407 udp_tunnel_sock_release(sock4->sock);
1408 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001409 }
1410
1411#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001412 if (__vxlan_sock_release_prep(sock6)) {
1413 udp_tunnel_sock_release(sock6->sock);
1414 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001415 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001416#endif
1417}
1418
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001419/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001420 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001421 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001422static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001423{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001424 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001425 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1426 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001427 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001428
Cong Wange4c7ed42013-08-31 13:44:33 +08001429 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001430 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001431 struct ip_mreqn mreq = {
1432 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1433 .imr_ifindex = ifindex,
1434 };
1435
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001436 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001437 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001438 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001439 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001440#if IS_ENABLED(CONFIG_IPV6)
1441 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001442 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1443
1444 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001445 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001446 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1447 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001448 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001449#endif
1450 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001451
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001452 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001453}
1454
1455/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001456static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001457{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001458 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001459 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1460 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001461 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001462
Cong Wange4c7ed42013-08-31 13:44:33 +08001463 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001464 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001465 struct ip_mreqn mreq = {
1466 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1467 .imr_ifindex = ifindex,
1468 };
1469
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001470 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001471 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001472 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001473 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001474#if IS_ENABLED(CONFIG_IPV6)
1475 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001476 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1477
1478 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001479 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001480 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1481 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001482 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001483#endif
1484 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001485
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001486 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001487}
1488
Jiri Bencf14eceb2016-02-16 21:59:01 +01001489static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1490 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001491{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001492 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001493
Jiri Bencf14eceb2016-02-16 21:59:01 +01001494 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1495 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001496
Jiri Bencf14eceb2016-02-16 21:59:01 +01001497 start = vxlan_rco_start(unparsed->vx_vni);
1498 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001499
Jiri Benc7d34fa72016-03-21 17:50:05 +01001500 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001501 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001502
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001503 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1504 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001505out:
1506 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1507 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001508 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001509}
1510
Jiri Bencf14eceb2016-02-16 21:59:01 +01001511static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001512 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001513 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001514{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001515 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001516 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001517
Jiri Bencf14eceb2016-02-16 21:59:01 +01001518 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1519 goto out;
1520
Jiri Benc3288af02016-02-16 21:59:00 +01001521 md->gbp = ntohs(gbp->policy_id);
1522
Jiri Benc10a5af22016-02-23 18:02:59 +01001523 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001524 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001525 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001526 tun_dst->u.tun_info.options_len = sizeof(*md);
1527 }
Jiri Benc3288af02016-02-16 21:59:00 +01001528 if (gbp->dont_learn)
1529 md->gbp |= VXLAN_GBP_DONT_LEARN;
1530
1531 if (gbp->policy_applied)
1532 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001533
Jiri Benc64f87d32016-02-23 18:02:55 +01001534 /* In flow-based mode, GBP is carried in dst_metadata */
1535 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1536 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001537out:
1538 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001539}
1540
Jiri Bence1e53142016-04-05 14:47:13 +02001541static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001542 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001543 struct sk_buff *skb, u32 vxflags)
1544{
1545 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1546
1547 /* Need to have Next Protocol set for interfaces in GPE mode. */
1548 if (!gpe->np_applied)
1549 return false;
1550 /* "The initial version is 0. If a receiver does not support the
1551 * version indicated it MUST drop the packet.
1552 */
1553 if (gpe->version != 0)
1554 return false;
1555 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1556 * processing MUST occur." However, we don't implement OAM
1557 * processing, thus drop the packet.
1558 */
1559 if (gpe->oam_flag)
1560 return false;
1561
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001562 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1563 if (!*protocol)
Jiri Bence1e53142016-04-05 14:47:13 +02001564 return false;
Jiri Bence1e53142016-04-05 14:47:13 +02001565
1566 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1567 return true;
1568}
1569
Jiri Benc1ab016e2016-02-23 18:02:56 +01001570static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1571 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001572 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001573{
Thomas Graf614732e2015-07-21 10:44:06 +02001574 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001575 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001576
Thomas Graf614732e2015-07-21 10:44:06 +02001577 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001578 skb->protocol = eth_type_trans(skb, vxlan->dev);
1579 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1580
1581 /* Ignore packet loops (and multicast echo) */
1582 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001583 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001584
Jiri Benc760c6802016-02-23 18:02:57 +01001585 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001586 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001587 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001588 saddr.sa.sa_family = AF_INET;
1589#if IS_ENABLED(CONFIG_IPV6)
1590 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001591 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001592 saddr.sa.sa_family = AF_INET6;
1593#endif
1594 }
1595
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001596 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001597 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001598 return false;
1599
1600 return true;
1601}
1602
Jiri Benc760c6802016-02-23 18:02:57 +01001603static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1604 struct sk_buff *skb)
1605{
1606 int err = 0;
1607
1608 if (vxlan_get_sk_family(vs) == AF_INET)
1609 err = IP_ECN_decapsulate(oiph, skb);
1610#if IS_ENABLED(CONFIG_IPV6)
1611 else
1612 err = IP6_ECN_decapsulate(oiph, skb);
1613#endif
1614
1615 if (unlikely(err) && log_ecn_error) {
1616 if (vxlan_get_sk_family(vs) == AF_INET)
1617 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1618 &((struct iphdr *)oiph)->saddr,
1619 ((struct iphdr *)oiph)->tos);
1620 else
1621 net_info_ratelimited("non-ECT from %pI6\n",
1622 &((struct ipv6hdr *)oiph)->saddr);
1623 }
1624 return err <= 1;
1625}
1626
stephen hemmingerd3428942012-10-01 12:32:35 +00001627/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001628static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001629{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001630 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001631 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001632 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001633 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001634 struct vxlan_metadata _md;
1635 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001636 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001637 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001638 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001639 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001640
Jiri Bence1e53142016-04-05 14:47:13 +02001641 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001642 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001643 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001644
Jiri Bencf14eceb2016-02-16 21:59:01 +01001645 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001646 /* VNI flag always required to be set */
1647 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1648 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1649 ntohl(vxlan_hdr(skb)->vx_flags),
1650 ntohl(vxlan_hdr(skb)->vx_vni));
1651 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001652 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001653 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001654 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1655 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001656
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001657 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001658 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001659 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001660
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001661 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1662
Matthias Schiffer49f810f2017-06-19 10:04:00 +02001663 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001664 if (!vxlan)
1665 goto drop;
1666
Jiri Bence1e53142016-04-05 14:47:13 +02001667 /* For backwards compatibility, only allow reserved fields to be
1668 * used by VXLAN extensions if explicitly requested.
1669 */
1670 if (vs->flags & VXLAN_F_GPE) {
1671 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1672 goto drop;
1673 raw_proto = true;
1674 }
1675
1676 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1677 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1678 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001679
Thomas Grafee122c72015-07-21 10:43:58 +02001680 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001681 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001682
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001683 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001684 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001685
Thomas Grafee122c72015-07-21 10:43:58 +02001686 if (!tun_dst)
1687 goto drop;
1688
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001689 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001690
1691 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001692 } else {
1693 memset(md, 0, sizeof(*md));
1694 }
1695
Jiri Bencf14eceb2016-02-16 21:59:01 +01001696 if (vs->flags & VXLAN_F_REMCSUM_RX)
1697 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1698 goto drop;
1699 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001700 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001701 /* Note that GBP and GPE can never be active together. This is
1702 * ensured in vxlan_dev_configure.
1703 */
Thomas Graf35114942015-01-15 03:53:55 +01001704
Jiri Bencf14eceb2016-02-16 21:59:01 +01001705 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001706 /* If there are any unprocessed flags remaining treat
1707 * this as a malformed packet. This behavior diverges from
1708 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1709 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001710 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001711 * is more robust and provides a little more security in
1712 * adding extensions to VXLAN.
1713 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001714 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001715 }
1716
Jiri Bence1e53142016-04-05 14:47:13 +02001717 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001718 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001719 goto drop;
1720 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001721 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001722 skb->dev = vxlan->dev;
1723 skb->pkt_type = PACKET_HOST;
1724 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001725
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001726 oiph = skb_network_header(skb);
1727 skb_reset_network_header(skb);
1728
1729 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1730 ++vxlan->dev->stats.rx_frame_errors;
1731 ++vxlan->dev->stats.rx_errors;
1732 goto drop;
1733 }
1734
Eric Dumazet59cbf562019-03-10 10:36:40 -07001735 rcu_read_lock();
1736
1737 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1738 rcu_read_unlock();
1739 atomic_long_inc(&vxlan->dev->rx_dropped);
1740 goto drop;
1741 }
1742
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001743 stats = this_cpu_ptr(vxlan->dev->tstats);
1744 u64_stats_update_begin(&stats->syncp);
1745 stats->rx_packets++;
1746 stats->rx_bytes += skb->len;
1747 u64_stats_update_end(&stats->syncp);
1748
1749 gro_cells_receive(&vxlan->gro_cells, skb);
Eric Dumazet59cbf562019-03-10 10:36:40 -07001750
1751 rcu_read_unlock();
1752
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001753 return 0;
1754
1755drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001756 /* Consume bad packet */
1757 kfree_skb(skb);
1758 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001759}
1760
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001761/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1762static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1763{
1764 struct vxlan_dev *vxlan;
1765 struct vxlan_sock *vs;
1766 struct vxlanhdr *hdr;
1767 __be32 vni;
1768
Stefano Brivio8399a692019-06-11 00:27:05 +02001769 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001770 return -EINVAL;
1771
1772 hdr = vxlan_hdr(skb);
1773
1774 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1775 return -EINVAL;
1776
1777 vs = rcu_dereference_sk_user_data(sk);
1778 if (!vs)
1779 return -ENOENT;
1780
1781 vni = vxlan_vni(hdr->vx_vni);
1782 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1783 if (!vxlan)
1784 return -ENOENT;
1785
1786 return 0;
1787}
1788
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001789static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001790{
1791 struct vxlan_dev *vxlan = netdev_priv(dev);
1792 struct arphdr *parp;
1793 u8 *arpptr, *sha;
1794 __be32 sip, tip;
1795 struct neighbour *n;
1796
1797 if (dev->flags & IFF_NOARP)
1798 goto out;
1799
1800 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1801 dev->stats.tx_dropped++;
1802 goto out;
1803 }
1804 parp = arp_hdr(skb);
1805
1806 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1807 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1808 parp->ar_pro != htons(ETH_P_IP) ||
1809 parp->ar_op != htons(ARPOP_REQUEST) ||
1810 parp->ar_hln != dev->addr_len ||
1811 parp->ar_pln != 4)
1812 goto out;
1813 arpptr = (u8 *)parp + sizeof(struct arphdr);
1814 sha = arpptr;
1815 arpptr += dev->addr_len; /* sha */
1816 memcpy(&sip, arpptr, sizeof(sip));
1817 arpptr += sizeof(sip);
1818 arpptr += dev->addr_len; /* tha */
1819 memcpy(&tip, arpptr, sizeof(tip));
1820
1821 if (ipv4_is_loopback(tip) ||
1822 ipv4_is_multicast(tip))
1823 goto out;
1824
1825 n = neigh_lookup(&arp_tbl, &tip, dev);
1826
1827 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001828 struct vxlan_fdb *f;
1829 struct sk_buff *reply;
1830
1831 if (!(n->nud_state & NUD_CONNECTED)) {
1832 neigh_release(n);
1833 goto out;
1834 }
1835
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001836 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08001837 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001838 /* bridge-local neighbor */
1839 neigh_release(n);
1840 goto out;
1841 }
1842
1843 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1844 n->ha, sha);
1845
1846 neigh_release(n);
1847
David Stevens73461352014-03-18 12:32:29 -04001848 if (reply == NULL)
1849 goto out;
1850
David Stevense4f67ad2012-11-20 02:50:14 +00001851 skb_reset_mac_header(reply);
1852 __skb_pull(reply, skb_network_offset(reply));
1853 reply->ip_summed = CHECKSUM_UNNECESSARY;
1854 reply->pkt_type = PACKET_HOST;
1855
1856 if (netif_rx_ni(reply) == NET_RX_DROP)
1857 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001858 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001859 union vxlan_addr ipa = {
1860 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001861 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001862 };
1863
1864 vxlan_ip_miss(dev, &ipa);
1865 }
David Stevense4f67ad2012-11-20 02:50:14 +00001866out:
1867 consume_skb(skb);
1868 return NETDEV_TX_OK;
1869}
1870
Cong Wangf564f452013-08-31 13:44:36 +08001871#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001872static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1873 struct neighbour *n, bool isrouter)
1874{
1875 struct net_device *dev = request->dev;
1876 struct sk_buff *reply;
1877 struct nd_msg *ns, *na;
1878 struct ipv6hdr *pip6;
1879 u8 *daddr;
1880 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1881 int ns_olen;
1882 int i, len;
1883
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001884 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04001885 return NULL;
1886
1887 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1888 sizeof(*na) + na_olen + dev->needed_tailroom;
1889 reply = alloc_skb(len, GFP_ATOMIC);
1890 if (reply == NULL)
1891 return NULL;
1892
1893 reply->protocol = htons(ETH_P_IPV6);
1894 reply->dev = dev;
1895 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1896 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001897 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001898
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001899 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04001900
1901 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001902 ns_olen = request->len - skb_network_offset(request) -
1903 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04001904 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1905 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1906 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1907 break;
1908 }
1909 }
1910
1911 /* Ethernet header */
1912 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1913 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1914 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1915 reply->protocol = htons(ETH_P_IPV6);
1916
1917 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001918 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001919 skb_put(reply, sizeof(struct ipv6hdr));
1920
1921 /* IPv6 header */
1922
1923 pip6 = ipv6_hdr(reply);
1924 memset(pip6, 0, sizeof(struct ipv6hdr));
1925 pip6->version = 6;
1926 pip6->priority = ipv6_hdr(request)->priority;
1927 pip6->nexthdr = IPPROTO_ICMPV6;
1928 pip6->hop_limit = 255;
1929 pip6->daddr = ipv6_hdr(request)->saddr;
1930 pip6->saddr = *(struct in6_addr *)n->primary_key;
1931
1932 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001933 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001934
David Stevens4b29dba2014-03-24 10:39:58 -04001935 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02001936 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04001937 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1938 na->icmph.icmp6_router = isrouter;
1939 na->icmph.icmp6_override = 1;
1940 na->icmph.icmp6_solicited = 1;
1941 na->target = ns->target;
1942 ether_addr_copy(&na->opt[2], n->ha);
1943 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1944 na->opt[1] = na_olen >> 3;
1945
1946 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1947 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1948 csum_partial(na, sizeof(*na)+na_olen, 0));
1949
1950 pip6->payload_len = htons(sizeof(*na)+na_olen);
1951
1952 skb_push(reply, sizeof(struct ipv6hdr));
1953
1954 reply->ip_summed = CHECKSUM_UNNECESSARY;
1955
1956 return reply;
1957}
1958
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001959static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08001960{
1961 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08001962 const struct in6_addr *daddr;
Xin Long8bff36852017-11-11 19:58:50 +08001963 const struct ipv6hdr *iphdr;
David Stevens4b29dba2014-03-24 10:39:58 -04001964 struct inet6_dev *in6_dev;
Xin Long8bff36852017-11-11 19:58:50 +08001965 struct neighbour *n;
1966 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001967
1968 in6_dev = __in6_dev_get(dev);
1969 if (!in6_dev)
1970 goto out;
1971
Cong Wangf564f452013-08-31 13:44:36 +08001972 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001973 daddr = &iphdr->daddr;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001974 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08001975
David Stevens4b29dba2014-03-24 10:39:58 -04001976 if (ipv6_addr_loopback(daddr) ||
1977 ipv6_addr_is_multicast(&msg->target))
1978 goto out;
1979
1980 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001981
1982 if (n) {
1983 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001984 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001985
1986 if (!(n->nud_state & NUD_CONNECTED)) {
1987 neigh_release(n);
1988 goto out;
1989 }
1990
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001991 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08001992 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1993 /* bridge-local neighbor */
1994 neigh_release(n);
1995 goto out;
1996 }
1997
David Stevens4b29dba2014-03-24 10:39:58 -04001998 reply = vxlan_na_create(skb, n,
1999 !!(f ? f->flags & NTF_ROUTER : 0));
2000
Cong Wangf564f452013-08-31 13:44:36 +08002001 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04002002
2003 if (reply == NULL)
2004 goto out;
2005
2006 if (netif_rx_ni(reply) == NET_RX_DROP)
2007 dev->stats.rx_dropped++;
2008
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002009 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04002010 union vxlan_addr ipa = {
2011 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002012 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04002013 };
2014
Cong Wangf564f452013-08-31 13:44:36 +08002015 vxlan_ip_miss(dev, &ipa);
2016 }
2017
2018out:
2019 consume_skb(skb);
2020 return NETDEV_TX_OK;
2021}
2022#endif
2023
David Stevense4f67ad2012-11-20 02:50:14 +00002024static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2025{
2026 struct vxlan_dev *vxlan = netdev_priv(dev);
2027 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00002028
2029 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2030 return false;
2031
2032 n = NULL;
2033 switch (ntohs(eth_hdr(skb)->h_proto)) {
2034 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08002035 {
2036 struct iphdr *pip;
2037
David Stevense4f67ad2012-11-20 02:50:14 +00002038 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2039 return false;
2040 pip = ip_hdr(skb);
2041 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002042 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002043 union vxlan_addr ipa = {
2044 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002045 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08002046 };
2047
2048 vxlan_ip_miss(dev, &ipa);
2049 return false;
2050 }
2051
David Stevense4f67ad2012-11-20 02:50:14 +00002052 break;
Cong Wange15a00a2013-08-31 13:44:34 +08002053 }
2054#if IS_ENABLED(CONFIG_IPV6)
2055 case ETH_P_IPV6:
2056 {
2057 struct ipv6hdr *pip6;
2058
2059 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2060 return false;
2061 pip6 = ipv6_hdr(skb);
2062 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002063 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08002064 union vxlan_addr ipa = {
2065 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02002066 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08002067 };
2068
2069 vxlan_ip_miss(dev, &ipa);
2070 return false;
2071 }
2072
2073 break;
2074 }
2075#endif
David Stevense4f67ad2012-11-20 02:50:14 +00002076 default:
2077 return false;
2078 }
2079
2080 if (n) {
2081 bool diff;
2082
Joe Perches7367d0b2013-09-01 11:51:23 -07002083 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00002084 if (diff) {
2085 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2086 dev->addr_len);
2087 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2088 }
2089 neigh_release(n);
2090 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08002091 }
2092
David Stevense4f67ad2012-11-20 02:50:14 +00002093 return false;
2094}
2095
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002096static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01002097 struct vxlan_metadata *md)
2098{
2099 struct vxlanhdr_gbp *gbp;
2100
Thomas Grafdb79a622015-02-04 17:00:04 +01002101 if (!md->gbp)
2102 return;
2103
Thomas Graf35114942015-01-15 03:53:55 +01002104 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01002105 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01002106
2107 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2108 gbp->dont_learn = 1;
2109
2110 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2111 gbp->policy_applied = 1;
2112
2113 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2114}
2115
Jiri Bence1e53142016-04-05 14:47:13 +02002116static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2117 __be16 protocol)
2118{
2119 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2120
2121 gpe->np_applied = 1;
Jiri Bencfa20e0e2017-08-28 21:43:22 +02002122 gpe->next_protocol = tun_p_from_eth_p(protocol);
2123 if (!gpe->next_protocol)
2124 return -EPFNOSUPPORT;
2125 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002126}
2127
Jiri Bencf491e562016-02-02 18:09:16 +01002128static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2129 int iphdr_len, __be32 vni,
2130 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002131 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08002132{
Cong Wange4c7ed42013-08-31 13:44:33 +08002133 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08002134 int min_headroom;
2135 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08002136 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02002137 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08002138
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002139 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08002140 skb->ip_summed == CHECKSUM_PARTIAL) {
2141 int csum_start = skb_checksum_start_offset(skb);
2142
2143 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2144 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2145 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00002146 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08002147 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08002148 }
2149
Cong Wange4c7ed42013-08-31 13:44:33 +08002150 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08002151 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002152
2153 /* Need space for new headers (invalidates iph ptr) */
2154 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02002155 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08002156 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07002157
Alexander Duyckaed069d2016-04-14 15:33:37 -04002158 err = iptunnel_handle_offloads(skb, type);
2159 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08002160 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07002161
Johannes Bergd58ff352017-06-16 14:29:23 +02002162 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01002163 vxh->vx_flags = VXLAN_HF_VNI;
2164 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07002165
Tom Herbertdfd86452015-01-12 17:00:38 -08002166 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01002167 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08002168
Jiri Benc54bfd872016-02-16 21:58:58 +01002169 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2170 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2171 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08002172
2173 if (!skb_is_gso(skb)) {
2174 skb->ip_summed = CHECKSUM_NONE;
2175 skb->encapsulation = 0;
2176 }
2177 }
2178
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002179 if (vxflags & VXLAN_F_GBP)
2180 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02002181 if (vxflags & VXLAN_F_GPE) {
2182 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2183 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002184 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02002185 inner_protocol = skb->protocol;
2186 }
Thomas Graf35114942015-01-15 03:53:55 +01002187
Jiri Bence1e53142016-04-05 14:47:13 +02002188 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08002189 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07002190}
Pravin B Shelar49560532013-08-19 11:23:17 -07002191
pravin shelar655c3de2016-11-13 20:43:55 -08002192static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2193 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002194 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002195 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002196 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002197 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01002198{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002199 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002200 struct rtable *rt = NULL;
2201 struct flowi4 fl4;
2202
pravin shelar655c3de2016-11-13 20:43:55 -08002203 if (!sock4)
2204 return ERR_PTR(-EIO);
2205
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002206 if (tos && !info)
2207 use_cache = false;
2208 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002209 rt = dst_cache_get_ip4(dst_cache, saddr);
2210 if (rt)
2211 return rt;
2212 }
2213
Jiri Benc1a8496b2016-02-02 18:09:14 +01002214 memset(&fl4, 0, sizeof(fl4));
2215 fl4.flowi4_oif = oif;
2216 fl4.flowi4_tos = RT_TOS(tos);
2217 fl4.flowi4_mark = skb->mark;
2218 fl4.flowi4_proto = IPPROTO_UDP;
2219 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002220 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002221 fl4.fl4_dport = dport;
2222 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002223
2224 rt = ip_route_output_key(vxlan->net, &fl4);
pravin shelar655c3de2016-11-13 20:43:55 -08002225 if (likely(!IS_ERR(rt))) {
2226 if (rt->dst.dev == dev) {
2227 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2228 ip_rt_put(rt);
2229 return ERR_PTR(-ELOOP);
2230 }
2231
Jiri Benc1a8496b2016-02-02 18:09:14 +01002232 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002233 if (use_cache)
2234 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08002235 } else {
2236 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2237 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002238 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002239 return rt;
2240}
2241
Jiri Bence5d4b292015-12-07 13:04:30 +01002242#if IS_ENABLED(CONFIG_IPV6)
2243static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08002244 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08002245 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01002246 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002247 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01002248 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002249 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002250 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002251 struct dst_cache *dst_cache,
2252 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01002253{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002254 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002255 struct dst_entry *ndst;
2256 struct flowi6 fl6;
2257 int err;
2258
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002259 if (!sock6)
2260 return ERR_PTR(-EIO);
2261
Daniel Borkmann14006152016-03-04 15:15:08 +01002262 if (tos && !info)
2263 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002264 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002265 ndst = dst_cache_get_ip6(dst_cache, saddr);
2266 if (ndst)
2267 return ndst;
2268 }
2269
Jiri Bence5d4b292015-12-07 13:04:30 +01002270 memset(&fl6, 0, sizeof(fl6));
2271 fl6.flowi6_oif = oif;
2272 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002273 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01002274 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01002275 fl6.flowi6_mark = skb->mark;
2276 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002277 fl6.fl6_dport = dport;
2278 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01002279
2280 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002281 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01002282 &ndst, &fl6);
pravin shelar655c3de2016-11-13 20:43:55 -08002283 if (unlikely(err < 0)) {
2284 netdev_dbg(dev, "no route to %pI6\n", daddr);
2285 return ERR_PTR(-ENETUNREACH);
2286 }
2287
2288 if (unlikely(ndst->dev == dev)) {
2289 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2290 dst_release(ndst);
2291 return ERR_PTR(-ELOOP);
2292 }
Jiri Bence5d4b292015-12-07 13:04:30 +01002293
2294 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002295 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002296 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01002297 return ndst;
2298}
2299#endif
2300
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002301/* Bypass encapsulation if the destination is local */
2302static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002303 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002304{
Li RongQing8f849852014-01-04 13:57:59 +08002305 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08002306 union vxlan_addr loopback;
2307 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Eric Dumazet4179cb52019-02-07 12:27:38 -08002308 struct net_device *dev;
Li RongQingce6502a2014-10-16 08:49:41 +08002309 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002310
Li RongQing8f849852014-01-04 13:57:59 +08002311 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2312 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002313 skb->pkt_type = PACKET_HOST;
2314 skb->encapsulation = 0;
2315 skb->dev = dst_vxlan->dev;
2316 __skb_pull(skb, skb_network_offset(skb));
2317
Cong Wange4c7ed42013-08-31 13:44:33 +08002318 if (remote_ip->sa.sa_family == AF_INET) {
2319 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2320 loopback.sa.sa_family = AF_INET;
2321#if IS_ENABLED(CONFIG_IPV6)
2322 } else {
2323 loopback.sin6.sin6_addr = in6addr_loopback;
2324 loopback.sa.sa_family = AF_INET6;
2325#endif
2326 }
2327
Eric Dumazet4179cb52019-02-07 12:27:38 -08002328 rcu_read_lock();
2329 dev = skb->dev;
2330 if (unlikely(!(dev->flags & IFF_UP))) {
2331 kfree_skb(skb);
2332 goto drop;
2333 }
2334
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002335 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
Eric Dumazet4179cb52019-02-07 12:27:38 -08002336 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002337
2338 u64_stats_update_begin(&tx_stats->syncp);
2339 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002340 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002341 u64_stats_update_end(&tx_stats->syncp);
2342
2343 if (netif_rx(skb) == NET_RX_SUCCESS) {
2344 u64_stats_update_begin(&rx_stats->syncp);
2345 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002346 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002347 u64_stats_update_end(&rx_stats->syncp);
2348 } else {
Eric Dumazet4179cb52019-02-07 12:27:38 -08002349drop:
Li RongQingce6502a2014-10-16 08:49:41 +08002350 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002351 }
Eric Dumazet4179cb52019-02-07 12:27:38 -08002352 rcu_read_unlock();
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002353}
2354
pravin shelarfee1fad2016-11-13 20:43:56 -08002355static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002356 struct vxlan_dev *vxlan,
2357 union vxlan_addr *daddr,
2358 __be16 dst_port, int dst_ifindex, __be32 vni,
2359 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002360 u32 rt_flags)
2361{
2362#if IS_ENABLED(CONFIG_IPV6)
2363 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2364 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2365 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2366 */
2367 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2368#endif
2369 /* Bypass encapsulation if the destination is local */
2370 if (rt_flags & RTCF_LOCAL &&
2371 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2372 struct vxlan_dev *dst_vxlan;
2373
2374 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002375 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002376 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002377 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002378 if (!dst_vxlan) {
2379 dev->stats.tx_errors++;
2380 kfree_skb(skb);
2381
2382 return -ENOENT;
2383 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002384 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002385 return 1;
2386 }
2387
2388 return 0;
2389}
2390
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002391static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002392 __be32 default_vni, struct vxlan_rdst *rdst,
2393 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002394{
Paolo Abenid71785f2016-02-12 15:43:57 +01002395 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002396 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002397 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002398 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002399 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002400 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002401 struct vxlan_metadata _md;
2402 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002403 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002404 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002405 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002406 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002407 int ifindex;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07002408 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002409 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002410 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002411 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002412
Jiri Benc61adedf2015-08-20 13:56:25 +02002413 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002414
Thomas Grafee122c72015-07-21 10:43:58 +02002415 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002416 dst = &rdst->remote_ip;
2417 if (vxlan_addr_any(dst)) {
2418 if (did_rsc) {
2419 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002420 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002421 return;
2422 }
2423 goto drop;
2424 }
2425
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002426 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002427 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002428 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002429 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002430 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002431 md->gbp = skb->mark;
Hangbin Liu72f6d712018-04-17 14:11:28 +08002432 if (flags & VXLAN_F_TTL_INHERIT) {
2433 ttl = ip_tunnel_get_ttl(old_iph, skb);
2434 } else {
2435 ttl = vxlan->cfg.ttl;
2436 if (!ttl && vxlan_addr_multicast(dst))
2437 ttl = 1;
2438 }
pravin shelar0770b532016-11-13 20:43:57 -08002439
2440 tos = vxlan->cfg.tos;
2441 if (tos == 1)
2442 tos = ip_tunnel_get_dsfield(old_iph, skb);
2443
2444 if (dst->sa.sa_family == AF_INET)
2445 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2446 else
2447 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2448 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002449 } else {
2450 if (!info) {
2451 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2452 dev->name);
2453 goto drop;
2454 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002455 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002456 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002457 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002458 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2459 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002460 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002461 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2462 }
Thomas Grafee122c72015-07-21 10:43:58 +02002463 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002464 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2465 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002466 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002467 dst_cache = &info->dst_cache;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002468 if (info->options_len &&
2469 info->key.tun_flags & TUNNEL_VXLAN_OPT)
pravin shelar0770b532016-11-13 20:43:57 -08002470 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002471 ttl = info->key.ttl;
2472 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002473 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002474 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002475 }
pravin shelar0770b532016-11-13 20:43:57 -08002476 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2477 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002478
Jakub Kicinski56de8592017-02-24 11:43:36 -08002479 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002480 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002481 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002482 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002483 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002484
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002485 if (!ifindex)
2486 ifindex = sock4->sock->sk->sk_bound_dev_if;
2487
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002488 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002489 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002490 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002491 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002492 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002493 if (IS_ERR(rt)) {
2494 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002495 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002496 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002497
pravin shelarfee1fad2016-11-13 20:43:56 -08002498 if (!info) {
Stefano Briviob4d306972018-11-08 12:19:16 +01002499 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002500 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002501 dst_port, ifindex, vni,
2502 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002503 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002504 goto out_unlock;
Stefano Briviob4d306972018-11-08 12:19:16 +01002505
2506 if (vxlan->cfg.df == VXLAN_DF_SET) {
2507 df = htons(IP_DF);
2508 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2509 struct ethhdr *eth = eth_hdr(skb);
2510
2511 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2512 (ntohs(eth->h_proto) == ETH_P_IP &&
2513 old_iph->frag_off & htons(IP_DF)))
2514 df = htons(IP_DF);
2515 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002516 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002517 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002518 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002519
pravin shelarc46b7892016-11-13 20:43:54 -08002520 ndst = &rt->dst;
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002521 skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002522
Cong Wange4c7ed42013-08-31 13:44:33 +08002523 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2524 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002525 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002526 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002527 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002528 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002529
Brian Russell11586322017-02-24 17:47:11 +00002530 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002531 dst->sin.sin_addr.s_addr, tos, ttl, df,
2532 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002533#if IS_ENABLED(CONFIG_IPV6)
2534 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002535 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002536
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002537 if (!ifindex)
2538 ifindex = sock6->sock->sk->sk_bound_dev_if;
2539
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002540 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002541 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002542 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002543 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002544 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002545 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002546 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002547 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002548 goto tx_error;
2549 }
pravin shelar655c3de2016-11-13 20:43:55 -08002550
pravin shelarfee1fad2016-11-13 20:43:56 -08002551 if (!info) {
2552 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002553
pravin shelarfee1fad2016-11-13 20:43:56 -08002554 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002555 dst_port, ifindex, vni,
2556 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002557 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002558 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002559 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002560
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002561 skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002562
Daniel Borkmann14006152016-03-04 15:15:08 +01002563 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002564 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002565 skb_scrub_packet(skb, xnet);
2566 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002567 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002568 if (err < 0)
2569 goto tx_error;
2570
pravin shelar0770b532016-11-13 20:43:57 -08002571 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002572 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002573 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002574 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002575#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002576 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002577out_unlock:
2578 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002579 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002580
2581drop:
2582 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002583 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002584 return;
2585
2586tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002587 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002588 if (err == -ELOOP)
2589 dev->stats.collisions++;
2590 else if (err == -ENETUNREACH)
2591 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002592 dst_release(ndst);
2593 dev->stats.tx_errors++;
2594 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002595}
2596
David Stevens66817122013-03-15 04:35:51 +00002597/* Transmit local packets over Vxlan
2598 *
2599 * Outer IP header inherits ECN and DF from inner header.
2600 * Outer UDP destination is the VXLAN assigned port.
2601 * source port is based on hash of flow
2602 */
2603static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2604{
2605 struct vxlan_dev *vxlan = netdev_priv(dev);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002606 struct vxlan_rdst *rdst, *fdst = NULL;
Xin Long8bff36852017-11-11 19:58:50 +08002607 const struct ip_tunnel_info *info;
2608 bool did_rsc = false;
David Stevens66817122013-03-15 04:35:51 +00002609 struct vxlan_fdb *f;
Xin Long8bff36852017-11-11 19:58:50 +08002610 struct ethhdr *eth;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002611 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002612
Jiri Benc61adedf2015-08-20 13:56:25 +02002613 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002614
David Stevens66817122013-03-15 04:35:51 +00002615 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002616
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002617 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002618 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2619 info->mode & IP_TUNNEL_INFO_TX) {
2620 vni = tunnel_id_to_key32(info->key.tun_id);
2621 } else {
2622 if (info && info->mode & IP_TUNNEL_INFO_TX)
2623 vxlan_xmit_one(skb, dev, vni, NULL, false);
2624 else
2625 kfree_skb(skb);
2626 return NETDEV_TX_OK;
2627 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002628 }
2629
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002630 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002631 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002632 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002633 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002634#if IS_ENABLED(CONFIG_IPV6)
Xin Long8bff36852017-11-11 19:58:50 +08002635 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2636 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2637 sizeof(struct nd_msg)) &&
2638 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2639 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2640
2641 if (m->icmph.icmp6_code == 0 &&
2642 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002643 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002644 }
2645#endif
2646 }
David Stevens66817122013-03-15 04:35:51 +00002647
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002648 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002649 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002650 did_rsc = false;
2651
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002652 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002653 (ntohs(eth->h_proto) == ETH_P_IP ||
2654 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002655 did_rsc = route_shortcircuit(dev, skb);
2656 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002657 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002658 }
2659
David Stevens66817122013-03-15 04:35:51 +00002660 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002661 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002662 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002663 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002664 !is_multicast_ether_addr(eth->h_dest))
2665 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002666
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002667 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002668 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002669 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002670 }
David Stevens66817122013-03-15 04:35:51 +00002671 }
2672
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002673 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2674 struct sk_buff *skb1;
2675
Eric Dumazet8f646c92014-01-06 09:54:31 -08002676 if (!fdst) {
2677 fdst = rdst;
2678 continue;
2679 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002680 skb1 = skb_clone(skb, GFP_ATOMIC);
2681 if (skb1)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002682 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002683 }
2684
Eric Dumazet8f646c92014-01-06 09:54:31 -08002685 if (fdst)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002686 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002687 else
2688 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002689 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002690}
2691
stephen hemmingerd3428942012-10-01 12:32:35 +00002692/* Walk the forwarding table and purge stale entries */
Kees Cookdf7e8282017-10-04 16:26:59 -07002693static void vxlan_cleanup(struct timer_list *t)
stephen hemmingerd3428942012-10-01 12:32:35 +00002694{
Kees Cookdf7e8282017-10-04 16:26:59 -07002695 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
stephen hemmingerd3428942012-10-01 12:32:35 +00002696 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2697 unsigned int h;
2698
2699 if (!netif_running(vxlan->dev))
2700 return;
2701
stephen hemmingerd3428942012-10-01 12:32:35 +00002702 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2703 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002704
Litao Jiaof98ec782019-03-06 12:01:48 +08002705 spin_lock(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002706 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2707 struct vxlan_fdb *f
2708 = container_of(p, struct vxlan_fdb, hlist);
2709 unsigned long timeout;
2710
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002711 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002712 continue;
2713
Roopa Prabhudef499c2017-03-27 15:46:41 -07002714 if (f->flags & NTF_EXT_LEARNED)
2715 continue;
2716
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002717 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002718 if (time_before_eq(timeout, jiffies)) {
2719 netdev_dbg(vxlan->dev,
2720 "garbage collect %pM\n",
2721 f->eth_addr);
2722 f->state = NUD_STALE;
Petr Machata0e6160f2018-11-21 08:02:35 +00002723 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002724 } else if (time_before(timeout, next_timer))
2725 next_timer = timeout;
2726 }
Litao Jiaof98ec782019-03-06 12:01:48 +08002727 spin_unlock(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002728 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002729
2730 mod_timer(&vxlan->age_timer, next_timer);
2731}
2732
Mark Blocha53cb292017-06-02 03:24:08 +03002733static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2734{
2735 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2736
2737 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002738 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2739#if IS_ENABLED(CONFIG_IPV6)
2740 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2741#endif
Mark Blocha53cb292017-06-02 03:24:08 +03002742 spin_unlock(&vn->sock_lock);
2743}
2744
Jiri Benc69e76662017-07-02 19:00:57 +02002745static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2746 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002747{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002748 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002749 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002750
Jiri Benc69e76662017-07-02 19:00:57 +02002751 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002752 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002753 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002754 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002755}
2756
stephen hemmingerd3428942012-10-01 12:32:35 +00002757/* Setup stats when device is created */
2758static int vxlan_init(struct net_device *dev)
2759{
WANG Cong1c213bd2014-02-13 11:46:28 -08002760 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002761 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002762 return -ENOMEM;
2763
2764 return 0;
2765}
2766
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002767static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002768{
2769 struct vxlan_fdb *f;
2770
2771 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002772 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002773 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00002774 vxlan_fdb_destroy(vxlan, f, true, true);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002775 spin_unlock_bh(&vxlan->hash_lock);
2776}
2777
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002778static void vxlan_uninit(struct net_device *dev)
2779{
2780 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002781
Stefano Brivioad6c9982019-03-08 16:40:57 +01002782 gro_cells_destroy(&vxlan->gro_cells);
2783
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002784 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002785
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002786 free_percpu(dev->tstats);
2787}
2788
stephen hemmingerd3428942012-10-01 12:32:35 +00002789/* Start ageing timer and join group when device is brought up */
2790static int vxlan_open(struct net_device *dev)
2791{
2792 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002793 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002794
Jiri Benc205f3562015-09-24 13:50:01 +02002795 ret = vxlan_sock_add(vxlan);
2796 if (ret < 0)
2797 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002798
Gao feng79d4a942013-12-10 16:37:32 +08002799 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002800 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002801 if (ret == -EADDRINUSE)
2802 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002803 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002804 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002805 return ret;
2806 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002807 }
2808
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002809 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002810 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2811
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002812 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002813}
2814
2815/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002816static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00002817{
Cong Wang31fec5a2013-05-27 22:35:52 +00002818 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002819
2820 spin_lock_bh(&vxlan->hash_lock);
2821 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2822 struct hlist_node *p, *n;
2823 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2824 struct vxlan_fdb *f
2825 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002826 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2827 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002828 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2829 if (!is_zero_ether_addr(f->eth_addr))
Petr Machata0e6160f2018-11-21 08:02:35 +00002830 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002831 }
2832 }
2833 spin_unlock_bh(&vxlan->hash_lock);
2834}
2835
2836/* Cleanup timer and forwarding table on shutdown */
2837static int vxlan_stop(struct net_device *dev)
2838{
2839 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002840 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002841 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002842
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002843 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002844 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002845 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002846
2847 del_timer_sync(&vxlan->age_timer);
2848
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002849 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02002850 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002851
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002852 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002853}
2854
stephen hemmingerd3428942012-10-01 12:32:35 +00002855/* Stub, nothing needs to be done. */
2856static void vxlan_set_multicast_list(struct net_device *dev)
2857{
2858}
2859
Daniel Borkmann345010b2013-12-18 00:21:08 +01002860static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2861{
2862 struct vxlan_dev *vxlan = netdev_priv(dev);
2863 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002864 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2865 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002866 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04002867
2868 /* This check is different than dev->max_mtu, because it looks at
2869 * the lowerdev->mtu, rather than the static dev->max_mtu
2870 */
2871 if (lowerdev) {
2872 int max_mtu = lowerdev->mtu -
2873 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2874 if (new_mtu > max_mtu)
2875 return -EINVAL;
2876 }
2877
2878 dev->mtu = new_mtu;
2879 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01002880}
2881
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002882static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2883{
2884 struct vxlan_dev *vxlan = netdev_priv(dev);
2885 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2886 __be16 sport, dport;
2887
2888 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2889 vxlan->cfg.port_max, true);
2890 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2891
Jiri Benc239e9442015-12-07 13:04:31 +01002892 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002893 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002894 struct rtable *rt;
2895
pravin shelar655c3de2016-11-13 20:43:55 -08002896 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002897 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002898 &info->key.u.ipv4.src, dport, sport,
2899 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002900 if (IS_ERR(rt))
2901 return PTR_ERR(rt);
2902 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002903 } else {
2904#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08002905 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01002906 struct dst_entry *ndst;
2907
pravin shelar655c3de2016-11-13 20:43:55 -08002908 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002909 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002910 &info->key.u.ipv6.src, dport, sport,
2911 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002912 if (IS_ERR(ndst))
2913 return PTR_ERR(ndst);
2914 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002915#else /* !CONFIG_IPV6 */
2916 return -EPFNOSUPPORT;
2917#endif
2918 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002919 info->key.tp_src = sport;
2920 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002921 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002922}
2923
Jiri Benc0c867c92016-04-05 14:47:10 +02002924static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002925 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002926 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002927 .ndo_open = vxlan_open,
2928 .ndo_stop = vxlan_stop,
2929 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002930 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002931 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002932 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002933 .ndo_validate_addr = eth_validate_addr,
2934 .ndo_set_mac_address = eth_mac_addr,
2935 .ndo_fdb_add = vxlan_fdb_add,
2936 .ndo_fdb_del = vxlan_fdb_delete,
2937 .ndo_fdb_dump = vxlan_fdb_dump,
Roopa Prabhu474c3c82018-12-15 22:35:10 -08002938 .ndo_fdb_get = vxlan_fdb_get,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002939 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
Andy Roulin8f1af752019-02-22 18:06:38 +00002940 .ndo_change_proto_down = dev_change_proto_down_generic,
stephen hemmingerd3428942012-10-01 12:32:35 +00002941};
2942
Jiri Bence1e53142016-04-05 14:47:13 +02002943static const struct net_device_ops vxlan_netdev_raw_ops = {
2944 .ndo_init = vxlan_init,
2945 .ndo_uninit = vxlan_uninit,
2946 .ndo_open = vxlan_open,
2947 .ndo_stop = vxlan_stop,
2948 .ndo_start_xmit = vxlan_xmit,
2949 .ndo_get_stats64 = ip_tunnel_get_stats64,
2950 .ndo_change_mtu = vxlan_change_mtu,
2951 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2952};
2953
stephen hemmingerd3428942012-10-01 12:32:35 +00002954/* Info for udev, that this is a virtual tunnel endpoint */
2955static struct device_type vxlan_type = {
2956 .name = "vxlan",
2957};
2958
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002959/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002960 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002961 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002962 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002963static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002964{
2965 struct vxlan_sock *vs;
2966 struct net *net = dev_net(dev);
2967 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002968 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002969
2970 spin_lock(&vn->sock_lock);
2971 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002972 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2973 unsigned short type;
2974
2975 if (vs->flags & VXLAN_F_GPE)
2976 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
2977 else
2978 type = UDP_TUNNEL_TYPE_VXLAN;
2979
2980 if (push)
2981 udp_tunnel_push_rx_port(dev, vs->sock, type);
2982 else
2983 udp_tunnel_drop_rx_port(dev, vs->sock, type);
2984 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002985 }
2986 spin_unlock(&vn->sock_lock);
2987}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002988
stephen hemmingerd3428942012-10-01 12:32:35 +00002989/* Initialize the device structure. */
2990static void vxlan_setup(struct net_device *dev)
2991{
2992 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002993 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002994
Jiri Benc65226ef2016-04-28 16:36:30 +02002995 eth_hw_addr_random(dev);
2996 ether_setup(dev);
2997
David S. Millercf124db2017-05-08 12:52:56 -04002998 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00002999 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3000
stephen hemmingerd3428942012-10-01 12:32:35 +00003001 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00003002 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003003 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003004 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003005
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07003006 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00003007 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00003008 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07003009 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02003010 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00003011
Matthias Schiffera9853432017-06-19 10:03:55 +02003012 /* MTU range: 68 - 65535 */
3013 dev->min_mtu = ETH_MIN_MTU;
3014 dev->max_mtu = ETH_MAX_MTU;
3015
stephen hemminger553675f2013-05-16 11:35:20 +00003016 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003017 spin_lock_init(&vxlan->hash_lock);
3018
Kees Cookdf7e8282017-10-04 16:26:59 -07003019 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
stephen hemmingerd3428942012-10-01 12:32:35 +00003020
3021 vxlan->dev = dev;
3022
Tom Herbert58ce31c2015-08-19 17:07:33 -07003023 gro_cells_init(&vxlan->gro_cells, dev);
3024
stephen hemmingerd3428942012-10-01 12:32:35 +00003025 for (h = 0; h < FDB_HASH_SIZE; ++h)
3026 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
3027}
3028
Jiri Benc0c867c92016-04-05 14:47:10 +02003029static void vxlan_ether_setup(struct net_device *dev)
3030{
Jiri Benc0c867c92016-04-05 14:47:10 +02003031 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3032 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3033 dev->netdev_ops = &vxlan_netdev_ether_ops;
3034}
3035
Jiri Bence1e53142016-04-05 14:47:13 +02003036static void vxlan_raw_setup(struct net_device *dev)
3037{
Jiri Benc65226ef2016-04-28 16:36:30 +02003038 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02003039 dev->type = ARPHRD_NONE;
3040 dev->hard_header_len = 0;
3041 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02003042 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3043 dev->netdev_ops = &vxlan_netdev_raw_ops;
3044}
3045
stephen hemmingerd3428942012-10-01 12:32:35 +00003046static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3047 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00003048 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003049 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003050 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
3051 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08003052 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00003053 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3054 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003055 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003056 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3057 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3058 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00003059 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00003060 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3061 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3062 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3063 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003064 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00003065 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08003066 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3067 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3068 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08003069 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3070 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01003071 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02003072 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003073 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
Hangbin Liu72f6d712018-04-17 14:11:28 +08003074 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
Stefano Briviob4d306972018-11-08 12:19:16 +01003075 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00003076};
3077
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02003078static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3079 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003080{
3081 if (tb[IFLA_ADDRESS]) {
3082 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003083 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3084 "Provided link layer address is not Ethernet");
stephen hemmingerd3428942012-10-01 12:32:35 +00003085 return -EINVAL;
3086 }
3087
3088 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003089 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3090 "Provided Ethernet address is not unicast");
stephen hemmingerd3428942012-10-01 12:32:35 +00003091 return -EADDRNOTAVAIL;
3092 }
3093 }
3094
Matthias Schiffera9853432017-06-19 10:03:55 +02003095 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02003096 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02003097
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003098 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3099 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3100 "MTU must be between 68 and 65535");
Matthias Schiffera9853432017-06-19 10:03:55 +02003101 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003102 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003103 }
3104
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003105 if (!data) {
3106 NL_SET_ERR_MSG(extack,
3107 "Required attributes not provided to perform the operation");
stephen hemmingerd3428942012-10-01 12:32:35 +00003108 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003109 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003110
3111 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003112 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3113
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003114 if (id >= VXLAN_N_VID) {
3115 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID],
3116 "VXLAN ID must be lower than 16777216");
stephen hemmingerd3428942012-10-01 12:32:35 +00003117 return -ERANGE;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003118 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003119 }
3120
stephen hemminger05f47d62012-10-09 20:35:50 +00003121 if (data[IFLA_VXLAN_PORT_RANGE]) {
3122 const struct ifla_vxlan_port_range *p
3123 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3124
3125 if (ntohs(p->high) < ntohs(p->low)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003126 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
3127 "Invalid source port range");
stephen hemminger05f47d62012-10-09 20:35:50 +00003128 return -EINVAL;
3129 }
3130 }
3131
Stefano Briviob4d306972018-11-08 12:19:16 +01003132 if (data[IFLA_VXLAN_DF]) {
3133 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3134
3135 if (df < 0 || df > VXLAN_DF_MAX) {
3136 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_DF],
3137 "Invalid DF attribute");
3138 return -EINVAL;
3139 }
3140 }
3141
stephen hemmingerd3428942012-10-01 12:32:35 +00003142 return 0;
3143}
3144
Yan Burman1b13c972013-01-29 23:43:07 +00003145static void vxlan_get_drvinfo(struct net_device *netdev,
3146 struct ethtool_drvinfo *drvinfo)
3147{
3148 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3149 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3150}
3151
3152static const struct ethtool_ops vxlan_ethtool_ops = {
3153 .get_drvinfo = vxlan_get_drvinfo,
3154 .get_link = ethtool_op_get_link,
3155};
3156
Tom Herbert3ee64f32014-07-13 19:49:42 -07003157static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003158 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +00003159{
Cong Wange4c7ed42013-08-31 13:44:33 +08003160 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003161 struct udp_port_cfg udp_conf;
3162 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08003163
Tom Herbert3ee64f32014-07-13 19:49:42 -07003164 memset(&udp_conf, 0, sizeof(udp_conf));
3165
3166 if (ipv6) {
3167 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003168 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08003169 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02003170 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07003171 } else {
3172 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08003173 }
3174
Tom Herbert3ee64f32014-07-13 19:49:42 -07003175 udp_conf.local_udp_port = port;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003176 udp_conf.bind_ifindex = ifindex;
Cong Wange4c7ed42013-08-31 13:44:33 +08003177
Tom Herbert3ee64f32014-07-13 19:49:42 -07003178 /* Open UDP socket */
3179 err = udp_sock_create(net, &udp_conf, &sock);
3180 if (err < 0)
3181 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08003182
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003183 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08003184}
3185
3186/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02003187static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003188 __be16 port, u32 flags,
3189 int ifindex)
Cong Wange4c7ed42013-08-31 13:44:33 +08003190{
3191 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3192 struct vxlan_sock *vs;
3193 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00003194 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003195 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00003196
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02003197 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08003198 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00003199 return ERR_PTR(-ENOMEM);
3200
3201 for (h = 0; h < VNI_HASH_SIZE; ++h)
3202 INIT_HLIST_HEAD(&vs->vni_list[h]);
3203
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003204 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003205 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00003206 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08003207 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00003208 }
3209
Cong Wange4c7ed42013-08-31 13:44:33 +08003210 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03003211 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08003212 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00003213
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003214 spin_lock(&vn->sock_lock);
3215 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07003216 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07003217 (vs->flags & VXLAN_F_GPE) ?
3218 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07003219 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003220 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00003221
3222 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07003223 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07003224 tunnel_cfg.sk_user_data = vs;
3225 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01003226 tunnel_cfg.encap_rcv = vxlan_rcv;
Stefano Brivioc3a43b92018-11-08 12:19:15 +01003227 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003228 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07003229 tunnel_cfg.gro_receive = vxlan_gro_receive;
3230 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003231
3232 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08003233
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003234 return vs;
3235}
stephen hemminger553675f2013-05-16 11:35:20 +00003236
Jiri Bencb1be00a2015-09-24 13:50:02 +02003237static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003238{
Jiri Benc205f3562015-09-24 13:50:01 +02003239 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3240 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02003241 struct vxlan_dev_node *node;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003242 int l3mdev_index = 0;
3243
3244 if (vxlan->cfg.remote_ifindex)
3245 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3246 vxlan->net, vxlan->cfg.remote_ifindex);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003247
Jiri Benc205f3562015-09-24 13:50:01 +02003248 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003249 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003250 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003251 vxlan->cfg.dst_port, vxlan->cfg.flags,
3252 l3mdev_index);
Reshetova, Elena66af8462017-07-04 15:52:59 +03003253 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003254 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003255 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003256 }
3257 spin_unlock(&vn->sock_lock);
3258 }
Jiri Benc205f3562015-09-24 13:50:01 +02003259 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003260 vs = vxlan_socket_create(vxlan->net, ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003261 vxlan->cfg.dst_port, vxlan->cfg.flags,
3262 l3mdev_index);
Jiri Benc205f3562015-09-24 13:50:01 +02003263 if (IS_ERR(vs))
3264 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003265#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02003266 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003267 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003268 node = &vxlan->hlist6;
3269 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02003270#endif
Jiri Benc69e76662017-07-02 19:00:57 +02003271 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003272 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003273 node = &vxlan->hlist4;
3274 }
3275 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02003276 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00003277}
3278
Jiri Bencb1be00a2015-09-24 13:50:02 +02003279static int vxlan_sock_add(struct vxlan_dev *vxlan)
3280{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003281 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3282 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02003283 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003284 int ret = 0;
3285
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003286 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003287#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003288 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02003289 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02003290 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02003291 if (ret < 0 && ret != -EAFNOSUPPORT)
3292 ipv4 = false;
3293 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02003294#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02003295 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003296 ret = __vxlan_sock_add(vxlan, false);
3297 if (ret < 0)
3298 vxlan_sock_release(vxlan);
3299 return ret;
3300}
3301
Matthias Schiffera9853432017-06-19 10:03:55 +02003302static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3303 struct net_device **lower,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003304 struct vxlan_dev *old,
3305 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003306{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01003307 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02003308 struct vxlan_dev *tmp;
3309 bool use_ipv6 = false;
3310
3311 if (conf->flags & VXLAN_F_GPE) {
3312 /* For now, allow GPE only together with
3313 * COLLECT_METADATA. This can be relaxed later; in such
3314 * case, the other side of the PtP link will have to be
3315 * provided.
3316 */
3317 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3318 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003319 NL_SET_ERR_MSG(extack,
3320 "VXLAN GPE does not support this combination of attributes");
Matthias Schiffera9853432017-06-19 10:03:55 +02003321 return -EINVAL;
3322 }
3323 }
3324
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003325 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3326 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02003327 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003328 conf->saddr.sa.sa_family = AF_INET;
3329 } else if (!conf->remote_ip.sa.sa_family) {
3330 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3331 } else if (!conf->saddr.sa.sa_family) {
3332 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3333 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003334
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003335 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3336 NL_SET_ERR_MSG(extack,
3337 "Local and remote address must be from the same family");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003338 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003339 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003340
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003341 if (vxlan_addr_multicast(&conf->saddr)) {
3342 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003343 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003344 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003345
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003346 if (conf->saddr.sa.sa_family == AF_INET6) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003347 if (!IS_ENABLED(CONFIG_IPV6)) {
3348 NL_SET_ERR_MSG(extack,
3349 "IPv6 support not enabled in the kernel");
Matthias Schiffera9853432017-06-19 10:03:55 +02003350 return -EPFNOSUPPORT;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003351 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003352 use_ipv6 = true;
3353 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003354
3355 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3356 int local_type =
3357 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3358 int remote_type =
3359 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3360
3361 if (local_type & IPV6_ADDR_LINKLOCAL) {
3362 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003363 (remote_type != IPV6_ADDR_ANY)) {
3364 NL_SET_ERR_MSG(extack,
3365 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003366 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003367 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003368
3369 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3370 } else {
3371 if (remote_type ==
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003372 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3373 NL_SET_ERR_MSG(extack,
3374 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003375 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003376 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003377
3378 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3379 }
3380 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003381 }
3382
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003383 if (conf->label && !use_ipv6) {
3384 NL_SET_ERR_MSG(extack,
3385 "Label attribute only applies to IPv6 VXLAN devices");
Matthias Schiffera9853432017-06-19 10:03:55 +02003386 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003387 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003388
3389 if (conf->remote_ifindex) {
3390 struct net_device *lowerdev;
3391
3392 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003393 if (!lowerdev) {
3394 NL_SET_ERR_MSG(extack,
3395 "Invalid local interface, device not found");
Matthias Schiffera9853432017-06-19 10:03:55 +02003396 return -ENODEV;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003397 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003398
3399#if IS_ENABLED(CONFIG_IPV6)
3400 if (use_ipv6) {
3401 struct inet6_dev *idev = __in6_dev_get(lowerdev);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003402 if (idev && idev->cnf.disable_ipv6) {
3403 NL_SET_ERR_MSG(extack,
3404 "IPv6 support disabled by administrator");
Matthias Schiffera9853432017-06-19 10:03:55 +02003405 return -EPERM;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003406 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003407 }
3408#endif
3409
3410 *lower = lowerdev;
3411 } else {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003412 if (vxlan_addr_multicast(&conf->remote_ip)) {
3413 NL_SET_ERR_MSG(extack,
3414 "Local interface required for multicast remote destination");
3415
Matthias Schiffera9853432017-06-19 10:03:55 +02003416 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003417 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003418
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003419#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003420 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3421 NL_SET_ERR_MSG(extack,
3422 "Local interface required for link-local local/remote addresses");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003423 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003424 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003425#endif
3426
Matthias Schiffera9853432017-06-19 10:03:55 +02003427 *lower = NULL;
3428 }
3429
3430 if (!conf->dst_port) {
3431 if (conf->flags & VXLAN_F_GPE)
3432 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3433 else
3434 conf->dst_port = htons(vxlan_port);
3435 }
3436
3437 if (!conf->age_interval)
3438 conf->age_interval = FDB_AGE_DEFAULT;
3439
3440 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3441 if (tmp == old)
3442 continue;
3443
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003444 if (tmp->cfg.vni != conf->vni)
3445 continue;
3446 if (tmp->cfg.dst_port != conf->dst_port)
3447 continue;
3448 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003449 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003450 continue;
3451
3452 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3453 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3454 continue;
3455
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003456 NL_SET_ERR_MSG(extack,
3457 "A VXLAN device with the specified VNI already exists");
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003458 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003459 }
3460
3461 return 0;
3462}
3463
3464static void vxlan_config_apply(struct net_device *dev,
3465 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003466 struct net_device *lowerdev,
3467 struct net *src_net,
3468 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003469{
3470 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003471 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003472 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003473 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3474 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003475
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003476 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003477 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003478 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003479 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003480 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003481
Matthias Schiffera9853432017-06-19 10:03:55 +02003482 if (conf->mtu)
3483 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003484
3485 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003486 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003487
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003488 dst->remote_vni = conf->vni;
3489
3490 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003491
Matthias Schiffera9853432017-06-19 10:03:55 +02003492 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003493 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003494
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003495 dev->gso_max_size = lowerdev->gso_max_size;
3496 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003497
3498 needed_headroom = lowerdev->hard_header_len;
3499
3500 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3501 VXLAN_HEADROOM);
Alexey Kodanevf870c1f2017-12-14 20:20:00 +03003502 if (max_mtu < ETH_MIN_MTU)
3503 max_mtu = ETH_MIN_MTU;
3504
3505 if (!changelink && !conf->mtu)
3506 dev->mtu = max_mtu;
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003507 }
3508
Matthias Schiffera9853432017-06-19 10:03:55 +02003509 if (dev->mtu > max_mtu)
3510 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003511
Jiri Bencb1be00a2015-09-24 13:50:02 +02003512 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3513 needed_headroom += VXLAN6_HEADROOM;
3514 else
3515 needed_headroom += VXLAN_HEADROOM;
3516 dev->needed_headroom = needed_headroom;
3517
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003518 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003519}
stephen hemmingerd3428942012-10-01 12:32:35 +00003520
Matthias Schiffera9853432017-06-19 10:03:55 +02003521static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003522 struct vxlan_config *conf, bool changelink,
3523 struct netlink_ext_ack *extack)
Matthias Schiffera9853432017-06-19 10:03:55 +02003524{
3525 struct vxlan_dev *vxlan = netdev_priv(dev);
3526 struct net_device *lowerdev;
3527 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003528
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003529 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
Matthias Schiffera9853432017-06-19 10:03:55 +02003530 if (ret)
3531 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003532
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003533 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003534
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003535 return 0;
3536}
3537
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003538static int __vxlan_dev_create(struct net *net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003539 struct vxlan_config *conf,
3540 struct netlink_ext_ack *extack)
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003541{
3542 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3543 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003544 struct vxlan_fdb *f = NULL;
Petr Machata6db92462018-12-18 13:16:00 +00003545 bool unregister = false;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003546 int err;
3547
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003548 err = vxlan_dev_configure(net, dev, conf, false, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003549 if (err)
3550 return err;
3551
3552 dev->ethtool_ops = &vxlan_ethtool_ops;
3553
3554 /* create an fdb entry for a valid default destination */
3555 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003556 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003557 &vxlan->default_dst.remote_ip,
3558 NUD_REACHABLE | NUD_PERMANENT,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003559 vxlan->cfg.dst_port,
3560 vxlan->default_dst.remote_vni,
3561 vxlan->default_dst.remote_vni,
3562 vxlan->default_dst.remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003563 NTF_SELF, &f);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003564 if (err)
3565 return err;
3566 }
3567
3568 err = register_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003569 if (err)
3570 goto errout;
Petr Machata6db92462018-12-18 13:16:00 +00003571 unregister = true;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003572
3573 err = rtnl_configure_link(dev, NULL);
Petr Machata6db92462018-12-18 13:16:00 +00003574 if (err)
Roopa Prabhu0241b832018-07-04 16:46:32 -07003575 goto errout;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003576
Roopa Prabhu0241b832018-07-04 16:46:32 -07003577 /* notify default fdb entry */
Petr Machata61f46fe2019-01-16 23:06:38 +00003578 if (f) {
3579 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
Petr Machata4c59b7d2019-01-16 23:06:54 +00003580 RTM_NEWNEIGH, true, extack);
Petr Machata61f46fe2019-01-16 23:06:38 +00003581 if (err)
3582 goto errout;
3583 }
Roopa Prabhu0241b832018-07-04 16:46:32 -07003584
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003585 list_add(&vxlan->next, &vn->vxlan_list);
3586 return 0;
Petr Machata6db92462018-12-18 13:16:00 +00003587
Roopa Prabhu0241b832018-07-04 16:46:32 -07003588errout:
Petr Machata6db92462018-12-18 13:16:00 +00003589 /* unregister_netdevice() destroys the default FDB entry with deletion
3590 * notification. But the addition notification was not sent yet, so
3591 * destroy the entry by hand here.
3592 */
Roopa Prabhu0241b832018-07-04 16:46:32 -07003593 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003594 vxlan_fdb_destroy(vxlan, f, false, false);
Petr Machata6db92462018-12-18 13:16:00 +00003595 if (unregister)
3596 unregister_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003597 return err;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003598}
3599
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003600/* Set/clear flags based on attribute */
3601static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3602 int attrtype, unsigned long mask, bool changelink,
3603 bool changelink_supported,
3604 struct netlink_ext_ack *extack)
3605{
3606 unsigned long flags;
3607
3608 if (!tb[attrtype])
3609 return 0;
3610
3611 if (changelink && !changelink_supported) {
3612 vxlan_flag_attr_error(attrtype, extack);
3613 return -EOPNOTSUPP;
3614 }
3615
3616 if (vxlan_policy[attrtype].type == NLA_FLAG)
3617 flags = conf->flags | mask;
3618 else if (nla_get_u8(tb[attrtype]))
3619 flags = conf->flags | mask;
3620 else
3621 flags = conf->flags & ~mask;
3622
3623 conf->flags = flags;
3624
3625 return 0;
3626}
3627
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003628static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3629 struct net_device *dev, struct vxlan_config *conf,
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003630 bool changelink, struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003631{
3632 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003633 int err = 0;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003634
3635 memset(conf, 0, sizeof(*conf));
3636
3637 /* if changelink operation, start with old existing cfg */
3638 if (changelink)
3639 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3640
3641 if (data[IFLA_VXLAN_ID]) {
3642 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3643
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003644 if (changelink && (vni != conf->vni)) {
3645 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003646 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003647 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003648 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3649 }
3650
3651 if (data[IFLA_VXLAN_GROUP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003652 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3653 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 +02003654 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003655 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003656
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003657 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003658 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003659 } else if (data[IFLA_VXLAN_GROUP6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003660 if (!IS_ENABLED(CONFIG_IPV6)) {
3661 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003662 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003663 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003664
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003665 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3666 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 +02003667 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003668 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003669
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003670 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3671 conf->remote_ip.sa.sa_family = AF_INET6;
3672 }
3673
3674 if (data[IFLA_VXLAN_LOCAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003675 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
3676 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 +02003677 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003678 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003679
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003680 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3681 conf->saddr.sa.sa_family = AF_INET;
3682 } else if (data[IFLA_VXLAN_LOCAL6]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003683 if (!IS_ENABLED(CONFIG_IPV6)) {
3684 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003685 return -EPFNOSUPPORT;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003686 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003687
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003688 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
3689 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 +02003690 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003691 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003692
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003693 /* TODO: respect scope id */
3694 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3695 conf->saddr.sa.sa_family = AF_INET6;
3696 }
3697
3698 if (data[IFLA_VXLAN_LINK])
3699 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3700
3701 if (data[IFLA_VXLAN_TOS])
3702 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3703
3704 if (data[IFLA_VXLAN_TTL])
3705 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3706
Hangbin Liu72f6d712018-04-17 14:11:28 +08003707 if (data[IFLA_VXLAN_TTL_INHERIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003708 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
3709 VXLAN_F_TTL_INHERIT, changelink, false,
3710 extack);
3711 if (err)
3712 return err;
3713
Hangbin Liu72f6d712018-04-17 14:11:28 +08003714 }
3715
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003716 if (data[IFLA_VXLAN_LABEL])
3717 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3718 IPV6_FLOWLABEL_MASK;
3719
3720 if (data[IFLA_VXLAN_LEARNING]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003721 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
3722 VXLAN_F_LEARN, changelink, true,
3723 extack);
3724 if (err)
3725 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003726 } else if (!changelink) {
3727 /* default to learn on a new device */
3728 conf->flags |= VXLAN_F_LEARN;
3729 }
3730
Ido Schimmel40051c42018-11-21 08:02:40 +00003731 if (data[IFLA_VXLAN_AGEING])
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003732 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003733
3734 if (data[IFLA_VXLAN_PROXY]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003735 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
3736 VXLAN_F_PROXY, changelink, false,
3737 extack);
3738 if (err)
3739 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003740 }
3741
3742 if (data[IFLA_VXLAN_RSC]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003743 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
3744 VXLAN_F_RSC, changelink, false,
3745 extack);
3746 if (err)
3747 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003748 }
3749
3750 if (data[IFLA_VXLAN_L2MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003751 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
3752 VXLAN_F_L2MISS, changelink, false,
3753 extack);
3754 if (err)
3755 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003756 }
3757
3758 if (data[IFLA_VXLAN_L3MISS]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003759 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
3760 VXLAN_F_L3MISS, changelink, false,
3761 extack);
3762 if (err)
3763 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003764 }
3765
3766 if (data[IFLA_VXLAN_LIMIT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003767 if (changelink) {
3768 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
3769 "Cannot change limit");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003770 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003771 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003772 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3773 }
3774
3775 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003776 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
3777 VXLAN_F_COLLECT_METADATA, changelink, false,
3778 extack);
3779 if (err)
3780 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003781 }
3782
3783 if (data[IFLA_VXLAN_PORT_RANGE]) {
3784 if (!changelink) {
3785 const struct ifla_vxlan_port_range *p
3786 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3787 conf->port_min = ntohs(p->low);
3788 conf->port_max = ntohs(p->high);
3789 } else {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003790 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
3791 "Cannot change port range");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003792 return -EOPNOTSUPP;
3793 }
3794 }
3795
3796 if (data[IFLA_VXLAN_PORT]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003797 if (changelink) {
3798 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
3799 "Cannot change port");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003800 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003801 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003802 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3803 }
3804
3805 if (data[IFLA_VXLAN_UDP_CSUM]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003806 if (changelink) {
3807 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
3808 "Cannot change UDP_CSUM flag");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003809 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003810 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003811 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3812 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3813 }
3814
3815 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003816 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3817 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
3818 false, extack);
3819 if (err)
3820 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003821 }
3822
3823 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003824 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
3825 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
3826 false, extack);
3827 if (err)
3828 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003829 }
3830
3831 if (data[IFLA_VXLAN_REMCSUM_TX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003832 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
3833 VXLAN_F_REMCSUM_TX, changelink, false,
3834 extack);
3835 if (err)
3836 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003837 }
3838
3839 if (data[IFLA_VXLAN_REMCSUM_RX]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003840 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
3841 VXLAN_F_REMCSUM_RX, changelink, false,
3842 extack);
3843 if (err)
3844 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003845 }
3846
3847 if (data[IFLA_VXLAN_GBP]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003848 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
3849 VXLAN_F_GBP, changelink, false, extack);
3850 if (err)
3851 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003852 }
3853
3854 if (data[IFLA_VXLAN_GPE]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003855 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
3856 VXLAN_F_GPE, changelink, false,
3857 extack);
3858 if (err)
3859 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003860 }
3861
3862 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003863 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
3864 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
3865 false, extack);
3866 if (err)
3867 return err;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003868 }
3869
3870 if (tb[IFLA_MTU]) {
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003871 if (changelink) {
3872 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3873 "Cannot change mtu");
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003874 return -EOPNOTSUPP;
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003875 }
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003876 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3877 }
3878
Stefano Briviob4d306972018-11-08 12:19:16 +01003879 if (data[IFLA_VXLAN_DF])
3880 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
3881
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003882 return 0;
3883}
3884
3885static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003886 struct nlattr *tb[], struct nlattr *data[],
3887 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003888{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003889 struct vxlan_config conf;
3890 int err;
3891
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003892 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003893 if (err)
3894 return err;
3895
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003896 return __vxlan_dev_create(src_net, dev, &conf, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00003897}
3898
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003899static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02003900 struct nlattr *data[],
3901 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003902{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003903 struct vxlan_dev *vxlan = netdev_priv(dev);
3904 struct vxlan_rdst *dst = &vxlan->default_dst;
Petr Machata8db9427d52019-01-16 23:06:39 +00003905 struct net_device *lowerdev;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003906 struct vxlan_config conf;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003907 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003908
Roopa Prabhu70fb0822019-02-25 22:03:01 -08003909 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003910 if (err)
3911 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07003912
Petr Machata8db9427d52019-01-16 23:06:39 +00003913 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
3914 vxlan, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003915 if (err)
3916 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003917
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003918 /* handle default dst entry */
Petr Machata038a5a92019-01-16 23:06:41 +00003919 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003920 spin_lock_bh(&vxlan->hash_lock);
Petr Machata038a5a92019-01-16 23:06:41 +00003921 if (!vxlan_addr_any(&conf.remote_ip)) {
Petr Machatace5e098f2018-12-18 13:16:02 +00003922 err = vxlan_fdb_update(vxlan, all_zeros_mac,
Petr Machata038a5a92019-01-16 23:06:41 +00003923 &conf.remote_ip,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003924 NUD_REACHABLE | NUD_PERMANENT,
Petr Machatace5e098f2018-12-18 13:16:02 +00003925 NLM_F_APPEND | NLM_F_CREATE,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003926 vxlan->cfg.dst_port,
Petr Machata038a5a92019-01-16 23:06:41 +00003927 conf.vni, conf.vni,
3928 conf.remote_ifindex,
Petr Machata4c59b7d2019-01-16 23:06:54 +00003929 NTF_SELF, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003930 if (err) {
3931 spin_unlock_bh(&vxlan->hash_lock);
3932 return err;
3933 }
3934 }
Petr Machata1cdc98c2019-01-16 23:06:43 +00003935 if (!vxlan_addr_any(&dst->remote_ip))
3936 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3937 dst->remote_ip,
3938 vxlan->cfg.dst_port,
3939 dst->remote_vni,
3940 dst->remote_vni,
3941 dst->remote_ifindex,
3942 true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003943 spin_unlock_bh(&vxlan->hash_lock);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003944 }
3945
Petr Machata038a5a92019-01-16 23:06:41 +00003946 if (conf.age_interval != vxlan->cfg.age_interval)
3947 mod_timer(&vxlan->age_timer, jiffies);
3948
3949 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003950 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003951}
3952
stephen hemmingerd3428942012-10-01 12:32:35 +00003953static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3954{
3955 struct vxlan_dev *vxlan = netdev_priv(dev);
3956
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003957 vxlan_flush(vxlan, true);
3958
stephen hemminger553675f2013-05-16 11:35:20 +00003959 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003960 unregister_netdevice_queue(dev, head);
3961}
3962
3963static size_t vxlan_get_size(const struct net_device *dev)
3964{
3965
3966 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003967 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003968 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003969 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003970 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
Hangbin Liu8fd78062018-09-26 10:35:42 +08003971 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
stephen hemmingerd3428942012-10-01 12:32:35 +00003972 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Stefano Briviob4d306972018-11-08 12:19:16 +01003973 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003974 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003975 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003976 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3977 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3978 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3979 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003980 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003981 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3982 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003983 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003984 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3985 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3986 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3987 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003988 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3989 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003990 0;
3991}
3992
3993static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3994{
3995 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003996 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003997 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003998 .low = htons(vxlan->cfg.port_min),
3999 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00004000 };
stephen hemmingerd3428942012-10-01 12:32:35 +00004001
Jiri Benc54bfd872016-02-16 21:58:58 +01004002 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004003 goto nla_put_failure;
4004
Cong Wange4c7ed42013-08-31 13:44:33 +08004005 if (!vxlan_addr_any(&dst->remote_ip)) {
4006 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004007 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4008 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004009 goto nla_put_failure;
4010#if IS_ENABLED(CONFIG_IPV6)
4011 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004012 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4013 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004014 goto nla_put_failure;
4015#endif
4016 }
4017 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004018
Atzm Watanabec7995c42013-04-16 02:50:52 +00004019 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00004020 goto nla_put_failure;
4021
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004022 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4023 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02004024 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004025 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004026 goto nla_put_failure;
4027#if IS_ENABLED(CONFIG_IPV6)
4028 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02004029 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004030 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08004031 goto nla_put_failure;
4032#endif
4033 }
4034 }
stephen hemmingerd3428942012-10-01 12:32:35 +00004035
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004036 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
Hangbin Liu8fd78062018-09-26 10:35:42 +08004037 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4038 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004039 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Stefano Briviob4d306972018-11-08 12:19:16 +01004040 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01004041 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004042 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004043 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004044 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004045 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
4046 nla_put_u8(skb, IFLA_VXLAN_RSC,
4047 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004048 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004049 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00004050 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004051 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07004052 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004053 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02004054 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4055 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4056 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004057 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004058 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004059 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004060 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07004061 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004062 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004063 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004064 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08004065 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004066 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00004067 goto nla_put_failure;
4068
stephen hemminger05f47d62012-10-09 20:35:50 +00004069 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4070 goto nla_put_failure;
4071
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004072 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01004073 nla_put_flag(skb, IFLA_VXLAN_GBP))
4074 goto nla_put_failure;
4075
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004076 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02004077 nla_put_flag(skb, IFLA_VXLAN_GPE))
4078 goto nla_put_failure;
4079
Matthias Schifferdc5321d2017-06-19 10:03:56 +02004080 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08004081 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4082 goto nla_put_failure;
4083
stephen hemmingerd3428942012-10-01 12:32:35 +00004084 return 0;
4085
4086nla_put_failure:
4087 return -EMSGSIZE;
4088}
4089
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004090static struct net *vxlan_get_link_net(const struct net_device *dev)
4091{
4092 struct vxlan_dev *vxlan = netdev_priv(dev);
4093
4094 return vxlan->net;
4095}
4096
stephen hemmingerd3428942012-10-01 12:32:35 +00004097static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4098 .kind = "vxlan",
4099 .maxtype = IFLA_VXLAN_MAX,
4100 .policy = vxlan_policy,
4101 .priv_size = sizeof(struct vxlan_dev),
4102 .setup = vxlan_setup,
4103 .validate = vxlan_validate,
4104 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08004105 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00004106 .dellink = vxlan_dellink,
4107 .get_size = vxlan_get_size,
4108 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01004109 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004110};
4111
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004112struct net_device *vxlan_dev_create(struct net *net, const char *name,
4113 u8 name_assign_type,
4114 struct vxlan_config *conf)
4115{
4116 struct nlattr *tb[IFLA_MAX + 1];
4117 struct net_device *dev;
4118 int err;
4119
4120 memset(&tb, 0, sizeof(tb));
4121
4122 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08004123 &vxlan_link_ops, tb, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004124 if (IS_ERR(dev))
4125 return dev;
4126
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07004127 err = __vxlan_dev_create(net, dev, conf, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02004128 if (err < 0) {
4129 free_netdev(dev);
4130 return ERR_PTR(err);
4131 }
4132
4133 err = rtnl_configure_link(dev, NULL);
4134 if (err < 0) {
4135 LIST_HEAD(list_kill);
4136
4137 vxlan_dellink(dev, &list_kill);
4138 unregister_netdevice_many(&list_kill);
4139 return ERR_PTR(err);
4140 }
4141
4142 return dev;
4143}
4144EXPORT_SYMBOL_GPL(vxlan_dev_create);
4145
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004146static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4147 struct net_device *dev)
4148{
4149 struct vxlan_dev *vxlan, *next;
4150 LIST_HEAD(list_kill);
4151
4152 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4153 struct vxlan_rdst *dst = &vxlan->default_dst;
4154
4155 /* In case we created vxlan device with carrier
4156 * and we loose the carrier due to module unload
4157 * we also need to remove vxlan device. In other
4158 * cases, it's not necessary and remote_ifindex
4159 * is 0 here, so no matches.
4160 */
4161 if (dst->remote_ifindex == dev->ifindex)
4162 vxlan_dellink(vxlan->dev, &list_kill);
4163 }
4164
4165 unregister_netdevice_many(&list_kill);
4166}
4167
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004168static int vxlan_netdevice_event(struct notifier_block *unused,
4169 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004170{
4171 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004172 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004173
Sabrina Dubroca04584952017-07-21 12:49:33 +02004174 if (event == NETDEV_UNREGISTER) {
4175 vxlan_offload_rx_ports(dev, false);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004176 vxlan_handle_lowerdev_unregister(vn, dev);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004177 } else if (event == NETDEV_REGISTER) {
4178 vxlan_offload_rx_ports(dev, true);
4179 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4180 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02004181 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02004182 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004183
4184 return NOTIFY_DONE;
4185}
4186
4187static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02004188 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004189};
4190
Petr Machata0efe1172018-10-17 08:53:26 +00004191static void
4192vxlan_fdb_offloaded_set(struct net_device *dev,
4193 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4194{
4195 struct vxlan_dev *vxlan = netdev_priv(dev);
4196 struct vxlan_rdst *rdst;
4197 struct vxlan_fdb *f;
4198
4199 spin_lock_bh(&vxlan->hash_lock);
4200
4201 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4202 if (!f)
4203 goto out;
4204
4205 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4206 fdb_info->remote_port,
4207 fdb_info->remote_vni,
4208 fdb_info->remote_ifindex);
4209 if (!rdst)
4210 goto out;
4211
4212 rdst->offloaded = fdb_info->offloaded;
4213
4214out:
4215 spin_unlock_bh(&vxlan->hash_lock);
4216}
4217
Petr Machata5728ae02018-11-21 08:02:39 +00004218static int
4219vxlan_fdb_external_learn_add(struct net_device *dev,
4220 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4221{
4222 struct vxlan_dev *vxlan = netdev_priv(dev);
Petr Machata4c59b7d2019-01-16 23:06:54 +00004223 struct netlink_ext_ack *extack;
Petr Machata5728ae02018-11-21 08:02:39 +00004224 int err;
4225
Petr Machata4c59b7d2019-01-16 23:06:54 +00004226 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4227
Petr Machata5728ae02018-11-21 08:02:39 +00004228 spin_lock_bh(&vxlan->hash_lock);
4229 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4230 NUD_REACHABLE,
4231 NLM_F_CREATE | NLM_F_REPLACE,
4232 fdb_info->remote_port,
4233 fdb_info->vni,
4234 fdb_info->remote_vni,
4235 fdb_info->remote_ifindex,
4236 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
Petr Machata4c59b7d2019-01-16 23:06:54 +00004237 false, extack);
Petr Machata5728ae02018-11-21 08:02:39 +00004238 spin_unlock_bh(&vxlan->hash_lock);
4239
4240 return err;
4241}
4242
4243static int
4244vxlan_fdb_external_learn_del(struct net_device *dev,
4245 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4246{
4247 struct vxlan_dev *vxlan = netdev_priv(dev);
4248 struct vxlan_fdb *f;
4249 int err = 0;
4250
4251 spin_lock_bh(&vxlan->hash_lock);
4252
4253 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4254 if (!f)
4255 err = -ENOENT;
4256 else if (f->flags & NTF_EXT_LEARNED)
4257 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4258 fdb_info->remote_ip,
4259 fdb_info->remote_port,
4260 fdb_info->vni,
4261 fdb_info->remote_vni,
4262 fdb_info->remote_ifindex,
4263 false);
4264
4265 spin_unlock_bh(&vxlan->hash_lock);
4266
4267 return err;
4268}
4269
Petr Machata0efe1172018-10-17 08:53:26 +00004270static int vxlan_switchdev_event(struct notifier_block *unused,
4271 unsigned long event, void *ptr)
4272{
4273 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Petr Machata5728ae02018-11-21 08:02:39 +00004274 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4275 int err = 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004276
4277 switch (event) {
4278 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4279 vxlan_fdb_offloaded_set(dev, ptr);
4280 break;
Petr Machata5728ae02018-11-21 08:02:39 +00004281 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4282 fdb_info = ptr;
4283 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4284 if (err) {
4285 err = notifier_from_errno(err);
4286 break;
4287 }
4288 fdb_info->offloaded = true;
4289 vxlan_fdb_offloaded_set(dev, fdb_info);
4290 break;
4291 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4292 fdb_info = ptr;
4293 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4294 if (err) {
4295 err = notifier_from_errno(err);
4296 break;
4297 }
4298 fdb_info->offloaded = false;
4299 vxlan_fdb_offloaded_set(dev, fdb_info);
4300 break;
Petr Machata0efe1172018-10-17 08:53:26 +00004301 }
4302
Petr Machata5728ae02018-11-21 08:02:39 +00004303 return err;
Petr Machata0efe1172018-10-17 08:53:26 +00004304}
4305
4306static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4307 .notifier_call = vxlan_switchdev_event,
4308};
4309
stephen hemmingerd3428942012-10-01 12:32:35 +00004310static __net_init int vxlan_init_net(struct net *net)
4311{
4312 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00004313 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00004314
stephen hemminger553675f2013-05-16 11:35:20 +00004315 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07004316 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00004317
stephen hemminger553675f2013-05-16 11:35:20 +00004318 for (h = 0; h < PORT_HASH_SIZE; ++h)
4319 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00004320
4321 return 0;
4322}
4323
Haishuang Yan57b61122017-12-16 17:54:49 +08004324static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004325{
4326 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4327 struct vxlan_dev *vxlan, *next;
4328 struct net_device *dev, *aux;
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004329 unsigned int h;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004330
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004331 for_each_netdev_safe(net, dev, aux)
4332 if (dev->rtnl_link_ops == &vxlan_link_ops)
Haishuang Yan57b61122017-12-16 17:54:49 +08004333 unregister_netdevice_queue(dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004334
4335 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4336 /* If vxlan->dev is in the same netns, it has already been added
4337 * to the list by the previous loop.
4338 */
Zhiqiang Liucc4807b2019-03-16 17:02:54 +08004339 if (!net_eq(dev_net(vxlan->dev), net))
Haishuang Yan57b61122017-12-16 17:54:49 +08004340 unregister_netdevice_queue(vxlan->dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004341 }
4342
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004343 for (h = 0; h < PORT_HASH_SIZE; ++h)
4344 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004345}
4346
Haishuang Yan57b61122017-12-16 17:54:49 +08004347static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4348{
4349 struct net *net;
4350 LIST_HEAD(list);
4351
4352 rtnl_lock();
4353 list_for_each_entry(net, net_list, exit_list)
4354 vxlan_destroy_tunnels(net, &list);
4355
4356 unregister_netdevice_many(&list);
4357 rtnl_unlock();
4358}
4359
stephen hemmingerd3428942012-10-01 12:32:35 +00004360static struct pernet_operations vxlan_net_ops = {
4361 .init = vxlan_init_net,
Haishuang Yan57b61122017-12-16 17:54:49 +08004362 .exit_batch = vxlan_exit_batch_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004363 .id = &vxlan_net_id,
4364 .size = sizeof(struct vxlan_net),
4365};
4366
4367static int __init vxlan_init_module(void)
4368{
4369 int rc;
4370
4371 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4372
Daniel Borkmann783c1462014-01-22 21:07:53 +01004373 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004374 if (rc)
4375 goto out1;
4376
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004377 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004378 if (rc)
4379 goto out2;
4380
Petr Machata0efe1172018-10-17 08:53:26 +00004381 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004382 if (rc)
4383 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00004384
Petr Machata0efe1172018-10-17 08:53:26 +00004385 rc = rtnl_link_register(&vxlan_link_ops);
4386 if (rc)
4387 goto out4;
4388
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004389 return 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004390out4:
4391 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004392out3:
4393 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004394out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01004395 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004396out1:
4397 return rc;
4398}
Cong Wang7332a132013-05-27 22:35:53 +00004399late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00004400
4401static void __exit vxlan_cleanup_module(void)
4402{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07004403 rtnl_link_unregister(&vxlan_link_ops);
Petr Machata0efe1172018-10-17 08:53:26 +00004404 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004405 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004406 unregister_pernet_subsys(&vxlan_net_ops);
4407 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00004408}
4409module_exit(vxlan_cleanup_module);
4410
4411MODULE_LICENSE("GPL");
4412MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004413MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08004414MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00004415MODULE_ALIAS_RTNL_LINK("vxlan");