blob: 901eef428280ca7c00de3619a28cd214359466ab [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>
stephen hemmingerd3428942012-10-01 12:32:35 +000023#include <net/ip.h>
24#include <net/icmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000025#include <net/rtnetlink.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000026#include <net/inet_ecn.h>
27#include <net/net_namespace.h>
28#include <net/netns/generic.h>
Jiri Bencfa20e0e2017-08-28 21:43:22 +020029#include <net/tun_proto.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070030#include <net/vxlan.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080031
Cong Wange4c7ed42013-08-31 13:44:33 +080032#if IS_ENABLED(CONFIG_IPV6)
Cong Wange4c7ed42013-08-31 13:44:33 +080033#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080034#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080035#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000036
37#define VXLAN_VERSION "0.1"
38
stephen hemminger553675f2013-05-16 11:35:20 +000039#define PORT_HASH_BITS 8
40#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000041#define FDB_AGE_DEFAULT 300 /* 5 min */
42#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
43
stephen hemminger23c578b2013-04-27 11:31:53 +000044/* UDP port for VXLAN traffic.
45 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070046 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000047 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070048static unsigned short vxlan_port __read_mostly = 8472;
49module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000050MODULE_PARM_DESC(udp_port, "Destination UDP port");
51
52static bool log_ecn_error = true;
53module_param(log_ecn_error, bool, 0644);
54MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
55
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030056static unsigned int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020057static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000058
Li RongQing7256eac2016-01-29 09:43:47 +080059static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030060
Jiri Benc205f3562015-09-24 13:50:01 +020061static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020062
Mark Blocha53cb292017-06-02 03:24:08 +030063static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
64
stephen hemminger553675f2013-05-16 11:35:20 +000065/* per-network namespace private data for this module */
66struct vxlan_net {
67 struct list_head vxlan_list;
68 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070069 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000070};
71
stephen hemmingerd3428942012-10-01 12:32:35 +000072/* Forwarding table entry */
73struct vxlan_fdb {
74 struct hlist_node hlist; /* linked list of entries */
75 struct rcu_head rcu;
76 unsigned long updated; /* jiffies */
77 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070078 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020079 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000080 u16 state; /* see ndm_state */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -080081 __be32 vni;
Petr Machata45598c12018-11-21 08:02:36 +000082 u16 flags; /* see ndm_flags and below */
stephen hemmingerd3428942012-10-01 12:32:35 +000083};
84
Petr Machata45598c12018-11-21 08:02:36 +000085#define NTF_VXLAN_ADDED_BY_USER 0x100
86
stephen hemmingerd3428942012-10-01 12:32:35 +000087/* salt for hash table */
88static u32 vxlan_salt __read_mostly;
89
Thomas Grafee122c72015-07-21 10:43:58 +020090static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
91{
Thomas Grafe7030872015-07-21 10:44:01 +020092 return vs->flags & VXLAN_F_COLLECT_METADATA ||
93 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020094}
95
Cong Wange4c7ed42013-08-31 13:44:33 +080096#if IS_ENABLED(CONFIG_IPV6)
97static inline
98bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
99{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200100 if (a->sa.sa_family != b->sa.sa_family)
101 return false;
102 if (a->sa.sa_family == AF_INET6)
103 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
104 else
105 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800106}
107
Cong Wange4c7ed42013-08-31 13:44:33 +0800108static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
109{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200110 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200111 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200112 ip->sa.sa_family = AF_INET6;
113 return 0;
114 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200115 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200116 ip->sa.sa_family = AF_INET;
117 return 0;
118 } else {
119 return -EAFNOSUPPORT;
120 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800121}
122
123static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200124 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800125{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200126 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200127 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200128 else
Jiri Benc930345e2015-03-29 16:59:25 +0200129 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800130}
131
132#else /* !CONFIG_IPV6 */
133
134static inline
135bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
136{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200137 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800138}
139
Cong Wange4c7ed42013-08-31 13:44:33 +0800140static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
141{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200142 if (nla_len(nla) >= sizeof(struct in6_addr)) {
143 return -EAFNOSUPPORT;
144 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200145 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200146 ip->sa.sa_family = AF_INET;
147 return 0;
148 } else {
149 return -EAFNOSUPPORT;
150 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800151}
152
153static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200154 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800155{
Jiri Benc930345e2015-03-29 16:59:25 +0200156 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800157}
158#endif
159
stephen hemminger553675f2013-05-16 11:35:20 +0000160/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100161static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000162{
Jiri Benc54bfd872016-02-16 21:58:58 +0100163 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000164}
165
166/* Socket hash table head */
167static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000168{
169 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
170
stephen hemminger553675f2013-05-16 11:35:20 +0000171 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
172}
173
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700174/* First remote destination for a forwarding entry.
175 * Guaranteed to be non-NULL because remotes are never deleted.
176 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700177static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700178{
stephen hemminger5ca54612013-08-04 17:17:39 -0700179 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
180}
181
182static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
183{
184 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700185}
186
Thomas Grafac5132d2015-01-15 03:53:56 +0100187/* Find VXLAN socket based on network namespace, address family and UDP port
188 * and enabled unshareable flags.
189 */
190static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100191 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000192{
193 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800194
195 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000196
197 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200198 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200199 vxlan_get_sk_family(vs) == family &&
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100200 vs->flags == flags &&
201 vs->sock->sk->sk_bound_dev_if == ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +0000202 return vs;
203 }
204 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000205}
206
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200207static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
208 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000209{
Jiri Benc69e76662017-07-02 19:00:57 +0200210 struct vxlan_dev_node *node;
stephen hemmingerd3428942012-10-01 12:32:35 +0000211
Jiri Bencb9167b22016-02-16 21:59:03 +0100212 /* For flow based devices, map all packets to VNI 0 */
213 if (vs->flags & VXLAN_F_COLLECT_METADATA)
214 vni = 0;
215
Jiri Benc69e76662017-07-02 19:00:57 +0200216 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
217 if (node->vxlan->default_dst.remote_vni != vni)
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200218 continue;
219
220 if (IS_ENABLED(CONFIG_IPV6)) {
Jiri Benc69e76662017-07-02 19:00:57 +0200221 const struct vxlan_config *cfg = &node->vxlan->cfg;
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200222
223 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
224 cfg->remote_ifindex != ifindex)
225 continue;
226 }
227
Jiri Benc69e76662017-07-02 19:00:57 +0200228 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000229 }
230
231 return NULL;
232}
233
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700234/* Look up VNI in a per net namespace table */
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200235static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
236 __be32 vni, sa_family_t family,
237 __be16 port, u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700238{
239 struct vxlan_sock *vs;
240
Alexis Bauvinaab8cc32018-12-03 10:54:40 +0100241 vs = vxlan_find_sock(net, family, port, flags, ifindex);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700242 if (!vs)
243 return NULL;
244
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200245 return vxlan_vs_find_vni(vs, ifindex, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700246}
247
stephen hemmingerd3428942012-10-01 12:32:35 +0000248/* Fill in neighbour message in skbuff. */
249static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700250 const struct vxlan_fdb *fdb,
251 u32 portid, u32 seq, int type, unsigned int flags,
252 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000253{
254 unsigned long now = jiffies;
255 struct nda_cacheinfo ci;
256 struct nlmsghdr *nlh;
257 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000258 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000259
260 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
261 if (nlh == NULL)
262 return -EMSGSIZE;
263
264 ndm = nlmsg_data(nlh);
265 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000266
267 send_eth = send_ip = true;
268
269 if (type == RTM_GETNEIGH) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800270 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000271 send_eth = !is_zero_ether_addr(fdb->eth_addr);
Vincent Bernat8f48ba72017-03-10 16:30:24 +0100272 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
David Stevense4f67ad2012-11-20 02:50:14 +0000273 } else
274 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000275 ndm->ndm_state = fdb->state;
276 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000277 ndm->ndm_flags = fdb->flags;
Petr Machata0efe1172018-10-17 08:53:26 +0000278 if (rdst->offloaded)
279 ndm->ndm_flags |= NTF_OFFLOADED;
Jun Zhao545469f2014-07-26 00:38:59 +0800280 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000281
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100282 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100283 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700284 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100285 goto nla_put_failure;
286
David Stevense4f67ad2012-11-20 02:50:14 +0000287 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000288 goto nla_put_failure;
289
Cong Wange4c7ed42013-08-31 13:44:33 +0800290 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000291 goto nla_put_failure;
292
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200293 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000294 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
295 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000296 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100297 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000298 goto nla_put_failure;
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200299 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800300 nla_put_u32(skb, NDA_SRC_VNI,
301 be32_to_cpu(fdb->vni)))
302 goto nla_put_failure;
David Stevens66817122013-03-15 04:35:51 +0000303 if (rdst->remote_ifindex &&
304 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000305 goto nla_put_failure;
306
307 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
308 ci.ndm_confirmed = 0;
309 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
310 ci.ndm_refcnt = 0;
311
312 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
313 goto nla_put_failure;
314
Johannes Berg053c0952015-01-16 22:09:00 +0100315 nlmsg_end(skb, nlh);
316 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000317
318nla_put_failure:
319 nlmsg_cancel(skb, nlh);
320 return -EMSGSIZE;
321}
322
323static inline size_t vxlan_nlmsg_size(void)
324{
325 return NLMSG_ALIGN(sizeof(struct ndmsg))
326 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800327 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000328 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000329 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
330 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100331 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000332 + nla_total_size(sizeof(struct nda_cacheinfo));
333}
334
Petr Machata9a997352018-10-17 08:53:22 +0000335static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
336 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000337{
338 struct net *net = dev_net(vxlan->dev);
339 struct sk_buff *skb;
340 int err = -ENOBUFS;
341
342 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
343 if (skb == NULL)
344 goto errout;
345
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200346 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000347 if (err < 0) {
348 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
349 WARN_ON(err == -EMSGSIZE);
350 kfree_skb(skb);
351 goto errout;
352 }
353
354 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
355 return;
356errout:
357 if (err < 0)
358 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
359}
360
Petr Machata9a997352018-10-17 08:53:22 +0000361static void vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
362 struct vxlan_fdb *fdb,
363 struct vxlan_rdst *rd,
364 bool adding)
365{
366 struct switchdev_notifier_vxlan_fdb_info info;
367 enum switchdev_notifier_type notifier_type;
368
369 if (WARN_ON(!rd))
370 return;
371
372 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
373 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
374
375 info = (struct switchdev_notifier_vxlan_fdb_info){
376 .remote_ip = rd->remote_ip,
377 .remote_port = rd->remote_port,
378 .remote_vni = rd->remote_vni,
379 .remote_ifindex = rd->remote_ifindex,
380 .vni = fdb->vni,
Petr Machata0efe1172018-10-17 08:53:26 +0000381 .offloaded = rd->offloaded,
Petr Machata45598c12018-11-21 08:02:36 +0000382 .added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER,
Petr Machata9a997352018-10-17 08:53:22 +0000383 };
384 memcpy(info.eth_addr, fdb->eth_addr, ETH_ALEN);
385
386 call_switchdev_notifiers(notifier_type, vxlan->dev,
387 &info.info);
388}
389
390static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
Petr Machata0e6160f2018-11-21 08:02:35 +0000391 struct vxlan_rdst *rd, int type, bool swdev_notify)
Petr Machata9a997352018-10-17 08:53:22 +0000392{
Petr Machata0e6160f2018-11-21 08:02:35 +0000393 if (swdev_notify) {
394 switch (type) {
395 case RTM_NEWNEIGH:
396 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
397 true);
398 break;
399 case RTM_DELNEIGH:
400 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
401 false);
402 break;
403 }
Petr Machata9a997352018-10-17 08:53:22 +0000404 }
405
406 __vxlan_fdb_notify(vxlan, fdb, rd, type);
407}
408
Cong Wange4c7ed42013-08-31 13:44:33 +0800409static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000410{
411 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700412 struct vxlan_fdb f = {
413 .state = NUD_STALE,
414 };
415 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800416 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100417 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700418 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700419
Petr Machata0e6160f2018-11-21 08:02:35 +0000420 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true);
David Stevense4f67ad2012-11-20 02:50:14 +0000421}
422
423static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
424{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700425 struct vxlan_fdb f = {
426 .state = NUD_STALE,
427 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200428 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000429
David Stevense4f67ad2012-11-20 02:50:14 +0000430 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
431
Petr Machata0e6160f2018-11-21 08:02:35 +0000432 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true);
David Stevense4f67ad2012-11-20 02:50:14 +0000433}
434
stephen hemmingerd3428942012-10-01 12:32:35 +0000435/* Hash Ethernet address */
436static u32 eth_hash(const unsigned char *addr)
437{
438 u64 value = get_unaligned((u64 *)addr);
439
440 /* only want 6 bytes */
441#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000442 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000443#else
444 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000445#endif
446 return hash_64(value, FDB_HASH_BITS);
447}
448
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800449static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
450{
451 /* use 1 byte of OUI and 3 bytes of NIC */
452 u32 key = get_unaligned((u32 *)(addr + 2));
453
454 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
455}
456
stephen hemmingerd3428942012-10-01 12:32:35 +0000457/* Hash chain to use given mac address */
458static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800459 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000460{
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200461 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800462 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
463 else
464 return &vxlan->fdb_head[eth_hash(mac)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000465}
466
467/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000468static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800469 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000470{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800471 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000472 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000473
Sasha Levinb67bfe02013-02-27 17:06:00 -0800474 hlist_for_each_entry_rcu(f, head, hlist) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800475 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200476 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800477 if (vni == f->vni)
478 return f;
479 } else {
480 return f;
481 }
482 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000483 }
484
485 return NULL;
486}
487
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000488static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800489 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000490{
491 struct vxlan_fdb *f;
492
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800493 f = __vxlan_find_mac(vxlan, mac, vni);
Li RongQing016f3d12018-08-29 11:52:10 +0800494 if (f && f->used != jiffies)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000495 f->used = jiffies;
496
497 return f;
498}
499
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300500/* caller should hold vxlan->hash_lock */
501static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800502 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100503 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300504{
505 struct vxlan_rdst *rd;
506
507 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800508 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300509 rd->remote_port == port &&
510 rd->remote_vni == vni &&
511 rd->remote_ifindex == ifindex)
512 return rd;
513 }
514
515 return NULL;
516}
517
Petr Machata1941f1d2018-10-17 08:53:24 +0000518int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
519 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
520{
521 struct vxlan_dev *vxlan = netdev_priv(dev);
522 u8 eth_addr[ETH_ALEN + 2] = { 0 };
523 struct vxlan_rdst *rdst;
524 struct vxlan_fdb *f;
525 int rc = 0;
526
527 if (is_multicast_ether_addr(mac) ||
528 is_zero_ether_addr(mac))
529 return -EINVAL;
530
531 ether_addr_copy(eth_addr, mac);
532
533 rcu_read_lock();
534
535 f = __vxlan_find_mac(vxlan, eth_addr, vni);
536 if (!f) {
537 rc = -ENOENT;
538 goto out;
539 }
540
541 rdst = first_remote_rcu(f);
542
543 memset(fdb_info, 0, sizeof(*fdb_info));
544 fdb_info->info.dev = dev;
545 fdb_info->remote_ip = rdst->remote_ip;
546 fdb_info->remote_port = rdst->remote_port;
547 fdb_info->remote_vni = rdst->remote_vni;
548 fdb_info->remote_ifindex = rdst->remote_ifindex;
549 fdb_info->vni = vni;
Petr Machata0efe1172018-10-17 08:53:26 +0000550 fdb_info->offloaded = rdst->offloaded;
Petr Machata45598c12018-11-21 08:02:36 +0000551 fdb_info->added_by_user = f->flags & NTF_VXLAN_ADDED_BY_USER;
Petr Machata1941f1d2018-10-17 08:53:24 +0000552 ether_addr_copy(fdb_info->eth_addr, mac);
553
554out:
555 rcu_read_unlock();
556 return rc;
557}
558EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
559
Thomas Richter906dc182013-07-19 17:20:07 +0200560/* Replace destination of unicast mac */
561static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100562 union vxlan_addr *ip, __be16 port, __be32 vni,
563 __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200564{
565 struct vxlan_rdst *rd;
566
567 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
568 if (rd)
569 return 0;
570
571 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
572 if (!rd)
573 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100574
575 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800576 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200577 rd->remote_port = port;
578 rd->remote_vni = vni;
579 rd->remote_ifindex = ifindex;
580 return 1;
581}
582
David Stevens66817122013-03-15 04:35:51 +0000583/* Add/update destinations for multicast */
584static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100585 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200586 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000587{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700588 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000589
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300590 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
591 if (rd)
592 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700593
David Stevens66817122013-03-15 04:35:51 +0000594 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
595 if (rd == NULL)
596 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100597
598 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
599 kfree(rd);
600 return -ENOBUFS;
601 }
602
Cong Wange4c7ed42013-08-31 13:44:33 +0800603 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000604 rd->remote_port = port;
Petr Machata0efe1172018-10-17 08:53:26 +0000605 rd->offloaded = false;
David Stevens66817122013-03-15 04:35:51 +0000606 rd->remote_vni = vni;
607 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700608
609 list_add_tail_rcu(&rd->list, &f->remotes);
610
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200611 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000612 return 1;
613}
614
Tom Herbertdfd86452015-01-12 17:00:38 -0800615static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
616 unsigned int off,
617 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100618 __be32 vni_field,
619 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800620 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800621{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700622 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800623
624 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700625 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800626
627 if (!NAPI_GRO_CB(skb)->csum_valid)
628 return NULL;
629
Jiri Benc54bfd872016-02-16 21:58:58 +0100630 start = vxlan_rco_start(vni_field);
631 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800632
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700633 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
634 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800635
636 skb->remcsum_offload = 1;
637
638 return vh;
639}
640
David Millerd4546c22018-06-24 14:13:49 +0900641static struct sk_buff *vxlan_gro_receive(struct sock *sk,
642 struct list_head *head,
643 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200644{
David Millerd4546c22018-06-24 14:13:49 +0900645 struct sk_buff *pp = NULL;
646 struct sk_buff *p;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200647 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800648 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200649 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700650 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100651 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800652 struct gro_remcsum grc;
653
654 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200655
656 off_vx = skb_gro_offset(skb);
657 hlen = off_vx + sizeof(*vh);
658 vh = skb_gro_header_fast(skb, off_vx);
659 if (skb_gro_header_hard(skb, hlen)) {
660 vh = skb_gro_header_slow(skb, hlen, off_vx);
661 if (unlikely(!vh))
662 goto out;
663 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200664
Tom Herbertdfd86452015-01-12 17:00:38 -0800665 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
666
Jiri Benc54bfd872016-02-16 21:58:58 +0100667 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800668
669 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
670 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100671 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800672 !!(vs->flags &
673 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800674
675 if (!vh)
676 goto out;
677 }
678
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700679 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
680
David Millerd4546c22018-06-24 14:13:49 +0900681 list_for_each_entry(p, head, list) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200682 if (!NAPI_GRO_CB(p)->same_flow)
683 continue;
684
685 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100686 if (vh->vx_flags != vh2->vx_flags ||
687 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200688 NAPI_GRO_CB(p)->same_flow = 0;
689 continue;
690 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200691 }
692
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200693 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800694 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200695
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200696out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200697 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200698
699 return pp;
700}
701
Tom Herbert5602c482016-04-05 08:22:53 -0700702static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200703{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700704 /* Sets 'skb->inner_mac_header' since we are always called with
705 * 'skb->encapsulation' set.
706 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800707 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200708}
709
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700710static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan,
711 const u8 *mac, __u16 state,
Petr Machata45598c12018-11-21 08:02:36 +0000712 __be32 src_vni, __u16 ndm_flags)
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700713{
714 struct vxlan_fdb *f;
715
716 f = kmalloc(sizeof(*f), GFP_ATOMIC);
717 if (!f)
718 return NULL;
719 f->state = state;
720 f->flags = ndm_flags;
721 f->updated = f->used = jiffies;
722 f->vni = src_vni;
723 INIT_LIST_HEAD(&f->remotes);
724 memcpy(f->eth_addr, mac, ETH_ALEN);
725
726 return f;
727}
728
stephen hemmingerd3428942012-10-01 12:32:35 +0000729static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800730 const u8 *mac, union vxlan_addr *ip,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700731 __u16 state, __be16 port, __be32 src_vni,
Petr Machata45598c12018-11-21 08:02:36 +0000732 __be32 vni, __u32 ifindex, __u16 ndm_flags,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700733 struct vxlan_fdb **fdb)
734{
735 struct vxlan_rdst *rd = NULL;
736 struct vxlan_fdb *f;
737 int rc;
738
739 if (vxlan->cfg.addrmax &&
740 vxlan->addrcnt >= vxlan->cfg.addrmax)
741 return -ENOSPC;
742
743 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
744 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
745 if (!f)
746 return -ENOMEM;
747
748 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
749 if (rc < 0) {
750 kfree(f);
751 return rc;
752 }
753
754 ++vxlan->addrcnt;
755 hlist_add_head_rcu(&f->hlist,
756 vxlan_fdb_head(vxlan, mac, src_vni));
757
758 *fdb = f;
759
760 return 0;
761}
762
763/* Add new entry to forwarding table -- assumes lock held */
764static int vxlan_fdb_update(struct vxlan_dev *vxlan,
765 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000766 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800767 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata45598c12018-11-21 08:02:36 +0000768 __u32 ifindex, __u16 ndm_flags,
Petr Machata0e6160f2018-11-21 08:02:35 +0000769 bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +0000770{
Petr Machata45598c12018-11-21 08:02:36 +0000771 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200772 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000773 struct vxlan_fdb *f;
774 int notify = 0;
Haishuang Yan17b46362016-11-29 09:59:36 +0800775 int rc;
stephen hemmingerd3428942012-10-01 12:32:35 +0000776
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800777 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000778 if (f) {
779 if (flags & NLM_F_EXCL) {
780 netdev_dbg(vxlan->dev,
781 "lost race to create %pM\n", mac);
782 return -EEXIST;
783 }
Petr Machata0ec566a2018-11-21 08:02:37 +0000784
785 /* Do not allow an externally learned entry to take over an
786 * entry added by the user.
787 */
788 if (!(fdb_flags & NTF_EXT_LEARNED) ||
789 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
790 if (f->state != state) {
791 f->state = state;
792 f->updated = jiffies;
793 notify = 1;
794 }
795 if (f->flags != fdb_flags) {
796 f->flags = fdb_flags;
797 f->updated = jiffies;
798 notify = 1;
799 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000800 }
Petr Machata0ec566a2018-11-21 08:02:37 +0000801
Thomas Richter906dc182013-07-19 17:20:07 +0200802 if ((flags & NLM_F_REPLACE)) {
803 /* Only change unicasts */
804 if (!(is_multicast_ether_addr(f->eth_addr) ||
805 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800806 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200807 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200808 } else
809 return -EOPNOTSUPP;
810 }
David Stevens66817122013-03-15 04:35:51 +0000811 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300812 (is_multicast_ether_addr(f->eth_addr) ||
813 is_zero_ether_addr(f->eth_addr))) {
Haishuang Yan17b46362016-11-29 09:59:36 +0800814 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
David Stevens66817122013-03-15 04:35:51 +0000815
816 if (rc < 0)
817 return rc;
818 notify |= rc;
819 }
Roopa Prabhu0813e952018-10-11 12:35:13 -0700820
821 if (ndm_flags & NTF_USE)
822 f->used = jiffies;
stephen hemmingerd3428942012-10-01 12:32:35 +0000823 } else {
824 if (!(flags & NLM_F_CREATE))
825 return -ENOENT;
826
Thomas Richter906dc182013-07-19 17:20:07 +0200827 /* Disallow replace to add a multicast entry */
828 if ((flags & NLM_F_REPLACE) &&
829 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
830 return -EOPNOTSUPP;
831
Cong Wange4c7ed42013-08-31 13:44:33 +0800832 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700833 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
Roopa Prabhu0813e952018-10-11 12:35:13 -0700834 vni, ifindex, fdb_flags, &f);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700835 if (rc < 0)
Haishuang Yan17b46362016-11-29 09:59:36 +0800836 return rc;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700837 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000838 }
839
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200840 if (notify) {
841 if (rd == NULL)
842 rd = first_remote_rtnl(f);
Petr Machata0e6160f2018-11-21 08:02:35 +0000843 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH, swdev_notify);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200844 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000845
846 return 0;
847}
848
Wei Yongjun6706c822013-04-11 19:00:35 +0000849static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000850{
851 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700852 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000853
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100854 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
855 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000856 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100857 }
David Stevens66817122013-03-15 04:35:51 +0000858 kfree(f);
859}
860
Roopa Prabhu4c2438b2018-07-04 16:46:31 -0700861static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +0000862 bool do_notify, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +0000863{
Petr Machata045a5a92018-10-17 08:53:27 +0000864 struct vxlan_rdst *rd;
865
stephen hemmingerd3428942012-10-01 12:32:35 +0000866 netdev_dbg(vxlan->dev,
867 "delete %pM\n", f->eth_addr);
868
869 --vxlan->addrcnt;
Roopa Prabhu4c2438b2018-07-04 16:46:31 -0700870 if (do_notify)
Petr Machata045a5a92018-10-17 08:53:27 +0000871 list_for_each_entry(rd, &f->remotes, list)
Petr Machata0e6160f2018-11-21 08:02:35 +0000872 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
873 swdev_notify);
stephen hemmingerd3428942012-10-01 12:32:35 +0000874
875 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000876 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000877}
878
Lance Richardson35cf2842017-05-29 13:25:57 -0400879static void vxlan_dst_free(struct rcu_head *head)
880{
881 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
882
883 dst_cache_destroy(&rd->dst_cache);
884 kfree(rd);
885}
886
887static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
Petr Machata0e6160f2018-11-21 08:02:35 +0000888 struct vxlan_rdst *rd, bool swdev_notify)
Lance Richardson35cf2842017-05-29 13:25:57 -0400889{
890 list_del_rcu(&rd->list);
Petr Machata0e6160f2018-11-21 08:02:35 +0000891 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify);
Lance Richardson35cf2842017-05-29 13:25:57 -0400892 call_rcu(&rd->rcu, vxlan_dst_free);
893}
894
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300895static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800896 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
897 __be32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300898{
899 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800900 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300901
902 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800903 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
904 if (err)
905 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300906 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800907 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
908 if (remote->sa.sa_family == AF_INET) {
909 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
910 ip->sa.sa_family = AF_INET;
911#if IS_ENABLED(CONFIG_IPV6)
912 } else {
913 ip->sin6.sin6_addr = in6addr_any;
914 ip->sa.sa_family = AF_INET6;
915#endif
916 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300917 }
918
919 if (tb[NDA_PORT]) {
920 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
921 return -EINVAL;
922 *port = nla_get_be16(tb[NDA_PORT]);
923 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200924 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300925 }
926
927 if (tb[NDA_VNI]) {
928 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
929 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100930 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300931 } else {
932 *vni = vxlan->default_dst.remote_vni;
933 }
934
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800935 if (tb[NDA_SRC_VNI]) {
936 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
937 return -EINVAL;
938 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
939 } else {
940 *src_vni = vxlan->default_dst.remote_vni;
941 }
942
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300943 if (tb[NDA_IFINDEX]) {
944 struct net_device *tdev;
945
946 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
947 return -EINVAL;
948 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800949 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300950 if (!tdev)
951 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300952 } else {
953 *ifindex = 0;
954 }
955
956 return 0;
957}
958
stephen hemmingerd3428942012-10-01 12:32:35 +0000959/* Add static entry (via netlink) */
960static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
961 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100962 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000963{
964 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300965 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800966 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000967 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800968 __be32 src_vni, vni;
Jiri Benc54bfd872016-02-16 21:58:58 +0100969 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000970 int err;
971
972 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
973 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
974 ndm->ndm_state);
975 return -EINVAL;
976 }
977
978 if (tb[NDA_DST] == NULL)
979 return -EINVAL;
980
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800981 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300982 if (err)
983 return err;
David Stevens66817122013-03-15 04:35:51 +0000984
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300985 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
986 return -EAFNOSUPPORT;
987
stephen hemmingerd3428942012-10-01 12:32:35 +0000988 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700989 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
Petr Machata45598c12018-11-21 08:02:36 +0000990 port, src_vni, vni, ifindex,
991 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
Petr Machata0e6160f2018-11-21 08:02:35 +0000992 true);
stephen hemmingerd3428942012-10-01 12:32:35 +0000993 spin_unlock_bh(&vxlan->hash_lock);
994
995 return err;
996}
997
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800998static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
999 const unsigned char *addr, union vxlan_addr ip,
Xin Longfc39c382017-11-26 21:19:05 +08001000 __be16 port, __be32 src_vni, __be32 vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001001 u32 ifindex, bool swdev_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +00001002{
stephen hemmingerd3428942012-10-01 12:32:35 +00001003 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001004 struct vxlan_rdst *rd = NULL;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001005 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001006
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001007 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001008 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001009 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001010
Cong Wange4c7ed42013-08-31 13:44:33 +08001011 if (!vxlan_addr_any(&ip)) {
1012 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001013 if (!rd)
1014 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +00001015 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001016
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001017 /* remove a destination if it's not the only one on the list,
1018 * otherwise destroy the fdb entry
1019 */
1020 if (rd && !list_is_singular(&f->remotes)) {
Petr Machata0e6160f2018-11-21 08:02:35 +00001021 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001022 goto out;
1023 }
1024
Petr Machata0e6160f2018-11-21 08:02:35 +00001025 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
Mike Rapoportbc7892b2013-06-25 16:01:54 +03001026
1027out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001028 return 0;
1029}
1030
1031/* Delete entry (via netlink) */
1032static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1033 struct net_device *dev,
1034 const unsigned char *addr, u16 vid)
1035{
1036 struct vxlan_dev *vxlan = netdev_priv(dev);
1037 union vxlan_addr ip;
1038 __be32 src_vni, vni;
1039 __be16 port;
1040 u32 ifindex;
1041 int err;
1042
1043 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
1044 if (err)
1045 return err;
1046
1047 spin_lock_bh(&vxlan->hash_lock);
Petr Machata0e6160f2018-11-21 08:02:35 +00001048 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1049 true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001050 spin_unlock_bh(&vxlan->hash_lock);
1051
1052 return err;
1053}
1054
1055/* Dump forwarding table */
1056static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04001057 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07001058 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +00001059{
1060 struct vxlan_dev *vxlan = netdev_priv(dev);
1061 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -07001062 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001063
1064 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1065 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001066
Sasha Levinb67bfe02013-02-27 17:06:00 -08001067 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +00001068 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +00001069
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001070 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07001071 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001072 goto skip;
1073
David Stevens66817122013-03-15 04:35:51 +00001074 err = vxlan_fdb_info(skb, vxlan, f,
1075 NETLINK_CB(cb->skb).portid,
1076 cb->nlh->nlmsg_seq,
1077 RTM_NEWNEIGH,
1078 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -07001079 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001080 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001081skip:
Roopa Prabhud2976532016-08-30 21:56:45 -07001082 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +09001083 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001084 }
1085 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001086out:
Roopa Prabhud2976532016-08-30 21:56:45 -07001087 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001088}
1089
1090/* Watch incoming packets to learn mapping between Ethernet address
1091 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +09001092 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +00001093 */
stephen hemminger26a41ae62013-06-17 12:09:58 -07001094static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001095 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001096 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +00001097{
1098 struct vxlan_dev *vxlan = netdev_priv(dev);
1099 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001100 u32 ifindex = 0;
1101
1102#if IS_ENABLED(CONFIG_IPV6)
1103 if (src_ip->sa.sa_family == AF_INET6 &&
1104 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1105 ifindex = src_ifindex;
1106#endif
stephen hemmingerd3428942012-10-01 12:32:35 +00001107
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001108 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +00001109 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001110 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001111
Matthias Schiffer87613de2017-06-19 10:03:59 +02001112 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1113 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001114 return false;
1115
1116 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -07001117 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001118 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001119
1120 if (net_ratelimit())
1121 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001122 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001123 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001124
Cong Wange4c7ed42013-08-31 13:44:33 +08001125 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001126 f->updated = jiffies;
Petr Machata0e6160f2018-11-21 08:02:35 +00001127 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001128 } else {
1129 /* learned new entry */
1130 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001131
1132 /* close off race between vxlan_flush and incoming packets */
1133 if (netif_running(dev))
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001134 vxlan_fdb_update(vxlan, src_mac, src_ip,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001135 NUD_REACHABLE,
1136 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001137 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001138 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001139 vxlan->default_dst.remote_vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00001140 ifindex, NTF_SELF, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001141 spin_unlock(&vxlan->hash_lock);
1142 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001143
1144 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001145}
1146
stephen hemmingerd3428942012-10-01 12:32:35 +00001147/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001148static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001149{
stephen hemminger553675f2013-05-16 11:35:20 +00001150 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001151 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001152#if IS_ENABLED(CONFIG_IPV6)
1153 struct vxlan_sock *sock6;
1154#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001155 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001156
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001157 sock4 = rtnl_dereference(dev->vn4_sock);
1158
Gao feng95ab0992013-12-10 16:37:33 +08001159 /* The vxlan_sock is only used by dev, leaving group has
1160 * no effect on other vxlan devices.
1161 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001162 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001163 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001164#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001165 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001166 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001167 return false;
1168#endif
Gao feng95ab0992013-12-10 16:37:33 +08001169
stephen hemminger553675f2013-05-16 11:35:20 +00001170 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001171 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001172 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001173
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001174 if (family == AF_INET &&
1175 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001176 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001177#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001178 if (family == AF_INET6 &&
1179 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001180 continue;
1181#endif
Gao feng95ab0992013-12-10 16:37:33 +08001182
1183 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1184 &dev->default_dst.remote_ip))
1185 continue;
1186
1187 if (vxlan->default_dst.remote_ifindex !=
1188 dev->default_dst.remote_ifindex)
1189 continue;
1190
1191 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001192 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001193
1194 return false;
1195}
1196
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001197static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001198{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001199 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001200
Jiri Bencb1be00a2015-09-24 13:50:02 +02001201 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001202 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001203 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001204 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001205
Jiri Bencb1be00a2015-09-24 13:50:02 +02001206 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001207 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001208 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001209 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001210 (vs->flags & VXLAN_F_GPE) ?
1211 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001212 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001213 spin_unlock(&vn->sock_lock);
1214
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001215 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001216}
1217
Jiri Bencb1be00a2015-09-24 13:50:02 +02001218static void vxlan_sock_release(struct vxlan_dev *vxlan)
1219{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001220 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001221#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001222 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1223
Mark Bloch57d88182017-06-07 14:36:58 +03001224 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001225#endif
1226
Mark Bloch57d88182017-06-07 14:36:58 +03001227 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001228 synchronize_net();
1229
Mark Blocha53cb292017-06-02 03:24:08 +03001230 vxlan_vs_del_dev(vxlan);
1231
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001232 if (__vxlan_sock_release_prep(sock4)) {
1233 udp_tunnel_sock_release(sock4->sock);
1234 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001235 }
1236
1237#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001238 if (__vxlan_sock_release_prep(sock6)) {
1239 udp_tunnel_sock_release(sock6->sock);
1240 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001241 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001242#endif
1243}
1244
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001245/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001246 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001247 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001248static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001249{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001250 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001251 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1252 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001253 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001254
Cong Wange4c7ed42013-08-31 13:44:33 +08001255 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001256 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001257 struct ip_mreqn mreq = {
1258 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1259 .imr_ifindex = ifindex,
1260 };
1261
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001262 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001263 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001264 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001265 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001266#if IS_ENABLED(CONFIG_IPV6)
1267 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001268 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1269
1270 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001271 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001272 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1273 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001274 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001275#endif
1276 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001277
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001278 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001279}
1280
1281/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001282static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001283{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001284 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001285 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1286 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001287 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001288
Cong Wange4c7ed42013-08-31 13:44:33 +08001289 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001290 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001291 struct ip_mreqn mreq = {
1292 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1293 .imr_ifindex = ifindex,
1294 };
1295
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001296 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001297 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001298 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001299 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001300#if IS_ENABLED(CONFIG_IPV6)
1301 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001302 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1303
1304 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001305 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001306 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1307 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001308 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001309#endif
1310 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001311
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001312 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001313}
1314
Jiri Bencf14eceb2016-02-16 21:59:01 +01001315static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1316 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001317{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001318 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001319
Jiri Bencf14eceb2016-02-16 21:59:01 +01001320 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1321 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001322
Jiri Bencf14eceb2016-02-16 21:59:01 +01001323 start = vxlan_rco_start(unparsed->vx_vni);
1324 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001325
Jiri Benc7d34fa72016-03-21 17:50:05 +01001326 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001327 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001328
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001329 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1330 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001331out:
1332 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1333 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001334 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001335}
1336
Jiri Bencf14eceb2016-02-16 21:59:01 +01001337static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001338 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001339 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001340{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001341 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001342 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001343
Jiri Bencf14eceb2016-02-16 21:59:01 +01001344 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1345 goto out;
1346
Jiri Benc3288af02016-02-16 21:59:00 +01001347 md->gbp = ntohs(gbp->policy_id);
1348
Jiri Benc10a5af22016-02-23 18:02:59 +01001349 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001350 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001351 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001352 tun_dst->u.tun_info.options_len = sizeof(*md);
1353 }
Jiri Benc3288af02016-02-16 21:59:00 +01001354 if (gbp->dont_learn)
1355 md->gbp |= VXLAN_GBP_DONT_LEARN;
1356
1357 if (gbp->policy_applied)
1358 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001359
Jiri Benc64f87d32016-02-23 18:02:55 +01001360 /* In flow-based mode, GBP is carried in dst_metadata */
1361 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1362 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001363out:
1364 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001365}
1366
Jiri Bence1e53142016-04-05 14:47:13 +02001367static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001368 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001369 struct sk_buff *skb, u32 vxflags)
1370{
1371 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1372
1373 /* Need to have Next Protocol set for interfaces in GPE mode. */
1374 if (!gpe->np_applied)
1375 return false;
1376 /* "The initial version is 0. If a receiver does not support the
1377 * version indicated it MUST drop the packet.
1378 */
1379 if (gpe->version != 0)
1380 return false;
1381 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1382 * processing MUST occur." However, we don't implement OAM
1383 * processing, thus drop the packet.
1384 */
1385 if (gpe->oam_flag)
1386 return false;
1387
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001388 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1389 if (!*protocol)
Jiri Bence1e53142016-04-05 14:47:13 +02001390 return false;
Jiri Bence1e53142016-04-05 14:47:13 +02001391
1392 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1393 return true;
1394}
1395
Jiri Benc1ab016e2016-02-23 18:02:56 +01001396static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1397 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001398 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001399{
Thomas Graf614732e2015-07-21 10:44:06 +02001400 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001401 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001402
Thomas Graf614732e2015-07-21 10:44:06 +02001403 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001404 skb->protocol = eth_type_trans(skb, vxlan->dev);
1405 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1406
1407 /* Ignore packet loops (and multicast echo) */
1408 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001409 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001410
Jiri Benc760c6802016-02-23 18:02:57 +01001411 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001412 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001413 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001414 saddr.sa.sa_family = AF_INET;
1415#if IS_ENABLED(CONFIG_IPV6)
1416 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001417 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001418 saddr.sa.sa_family = AF_INET6;
1419#endif
1420 }
1421
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001422 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001423 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001424 return false;
1425
1426 return true;
1427}
1428
Jiri Benc760c6802016-02-23 18:02:57 +01001429static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1430 struct sk_buff *skb)
1431{
1432 int err = 0;
1433
1434 if (vxlan_get_sk_family(vs) == AF_INET)
1435 err = IP_ECN_decapsulate(oiph, skb);
1436#if IS_ENABLED(CONFIG_IPV6)
1437 else
1438 err = IP6_ECN_decapsulate(oiph, skb);
1439#endif
1440
1441 if (unlikely(err) && log_ecn_error) {
1442 if (vxlan_get_sk_family(vs) == AF_INET)
1443 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1444 &((struct iphdr *)oiph)->saddr,
1445 ((struct iphdr *)oiph)->tos);
1446 else
1447 net_info_ratelimited("non-ECT from %pI6\n",
1448 &((struct ipv6hdr *)oiph)->saddr);
1449 }
1450 return err <= 1;
1451}
1452
stephen hemmingerd3428942012-10-01 12:32:35 +00001453/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001454static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001455{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001456 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001457 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001458 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001459 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001460 struct vxlan_metadata _md;
1461 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001462 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001463 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001464 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001465 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001466
Jiri Bence1e53142016-04-05 14:47:13 +02001467 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001468 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001469 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001470
Jiri Bencf14eceb2016-02-16 21:59:01 +01001471 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001472 /* VNI flag always required to be set */
1473 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1474 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1475 ntohl(vxlan_hdr(skb)->vx_flags),
1476 ntohl(vxlan_hdr(skb)->vx_vni));
1477 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001478 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001479 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001480 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1481 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001482
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001483 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001484 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001485 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001486
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001487 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1488
Matthias Schiffer49f810f2017-06-19 10:04:00 +02001489 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001490 if (!vxlan)
1491 goto drop;
1492
Jiri Bence1e53142016-04-05 14:47:13 +02001493 /* For backwards compatibility, only allow reserved fields to be
1494 * used by VXLAN extensions if explicitly requested.
1495 */
1496 if (vs->flags & VXLAN_F_GPE) {
1497 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1498 goto drop;
1499 raw_proto = true;
1500 }
1501
1502 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1503 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1504 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001505
Thomas Grafee122c72015-07-21 10:43:58 +02001506 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001507 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001508
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001509 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001510 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001511
Thomas Grafee122c72015-07-21 10:43:58 +02001512 if (!tun_dst)
1513 goto drop;
1514
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001515 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001516
1517 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001518 } else {
1519 memset(md, 0, sizeof(*md));
1520 }
1521
Jiri Bencf14eceb2016-02-16 21:59:01 +01001522 if (vs->flags & VXLAN_F_REMCSUM_RX)
1523 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1524 goto drop;
1525 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001526 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001527 /* Note that GBP and GPE can never be active together. This is
1528 * ensured in vxlan_dev_configure.
1529 */
Thomas Graf35114942015-01-15 03:53:55 +01001530
Jiri Bencf14eceb2016-02-16 21:59:01 +01001531 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001532 /* If there are any unprocessed flags remaining treat
1533 * this as a malformed packet. This behavior diverges from
1534 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1535 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001536 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001537 * is more robust and provides a little more security in
1538 * adding extensions to VXLAN.
1539 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001540 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001541 }
1542
Jiri Bence1e53142016-04-05 14:47:13 +02001543 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001544 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001545 goto drop;
1546 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001547 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001548 skb->dev = vxlan->dev;
1549 skb->pkt_type = PACKET_HOST;
1550 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001551
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001552 oiph = skb_network_header(skb);
1553 skb_reset_network_header(skb);
1554
1555 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1556 ++vxlan->dev->stats.rx_frame_errors;
1557 ++vxlan->dev->stats.rx_errors;
1558 goto drop;
1559 }
1560
1561 stats = this_cpu_ptr(vxlan->dev->tstats);
1562 u64_stats_update_begin(&stats->syncp);
1563 stats->rx_packets++;
1564 stats->rx_bytes += skb->len;
1565 u64_stats_update_end(&stats->syncp);
1566
1567 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001568 return 0;
1569
1570drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001571 /* Consume bad packet */
1572 kfree_skb(skb);
1573 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001574}
1575
Stefano Brivioc3a43b92018-11-08 12:19:15 +01001576/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1577static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1578{
1579 struct vxlan_dev *vxlan;
1580 struct vxlan_sock *vs;
1581 struct vxlanhdr *hdr;
1582 __be32 vni;
1583
1584 if (skb->len < VXLAN_HLEN)
1585 return -EINVAL;
1586
1587 hdr = vxlan_hdr(skb);
1588
1589 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1590 return -EINVAL;
1591
1592 vs = rcu_dereference_sk_user_data(sk);
1593 if (!vs)
1594 return -ENOENT;
1595
1596 vni = vxlan_vni(hdr->vx_vni);
1597 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1598 if (!vxlan)
1599 return -ENOENT;
1600
1601 return 0;
1602}
1603
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001604static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001605{
1606 struct vxlan_dev *vxlan = netdev_priv(dev);
1607 struct arphdr *parp;
1608 u8 *arpptr, *sha;
1609 __be32 sip, tip;
1610 struct neighbour *n;
1611
1612 if (dev->flags & IFF_NOARP)
1613 goto out;
1614
1615 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1616 dev->stats.tx_dropped++;
1617 goto out;
1618 }
1619 parp = arp_hdr(skb);
1620
1621 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1622 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1623 parp->ar_pro != htons(ETH_P_IP) ||
1624 parp->ar_op != htons(ARPOP_REQUEST) ||
1625 parp->ar_hln != dev->addr_len ||
1626 parp->ar_pln != 4)
1627 goto out;
1628 arpptr = (u8 *)parp + sizeof(struct arphdr);
1629 sha = arpptr;
1630 arpptr += dev->addr_len; /* sha */
1631 memcpy(&sip, arpptr, sizeof(sip));
1632 arpptr += sizeof(sip);
1633 arpptr += dev->addr_len; /* tha */
1634 memcpy(&tip, arpptr, sizeof(tip));
1635
1636 if (ipv4_is_loopback(tip) ||
1637 ipv4_is_multicast(tip))
1638 goto out;
1639
1640 n = neigh_lookup(&arp_tbl, &tip, dev);
1641
1642 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001643 struct vxlan_fdb *f;
1644 struct sk_buff *reply;
1645
1646 if (!(n->nud_state & NUD_CONNECTED)) {
1647 neigh_release(n);
1648 goto out;
1649 }
1650
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001651 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08001652 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001653 /* bridge-local neighbor */
1654 neigh_release(n);
1655 goto out;
1656 }
1657
1658 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1659 n->ha, sha);
1660
1661 neigh_release(n);
1662
David Stevens73461352014-03-18 12:32:29 -04001663 if (reply == NULL)
1664 goto out;
1665
David Stevense4f67ad2012-11-20 02:50:14 +00001666 skb_reset_mac_header(reply);
1667 __skb_pull(reply, skb_network_offset(reply));
1668 reply->ip_summed = CHECKSUM_UNNECESSARY;
1669 reply->pkt_type = PACKET_HOST;
1670
1671 if (netif_rx_ni(reply) == NET_RX_DROP)
1672 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001673 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001674 union vxlan_addr ipa = {
1675 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001676 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001677 };
1678
1679 vxlan_ip_miss(dev, &ipa);
1680 }
David Stevense4f67ad2012-11-20 02:50:14 +00001681out:
1682 consume_skb(skb);
1683 return NETDEV_TX_OK;
1684}
1685
Cong Wangf564f452013-08-31 13:44:36 +08001686#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001687static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1688 struct neighbour *n, bool isrouter)
1689{
1690 struct net_device *dev = request->dev;
1691 struct sk_buff *reply;
1692 struct nd_msg *ns, *na;
1693 struct ipv6hdr *pip6;
1694 u8 *daddr;
1695 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1696 int ns_olen;
1697 int i, len;
1698
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001699 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04001700 return NULL;
1701
1702 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1703 sizeof(*na) + na_olen + dev->needed_tailroom;
1704 reply = alloc_skb(len, GFP_ATOMIC);
1705 if (reply == NULL)
1706 return NULL;
1707
1708 reply->protocol = htons(ETH_P_IPV6);
1709 reply->dev = dev;
1710 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1711 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001712 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001713
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001714 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04001715
1716 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001717 ns_olen = request->len - skb_network_offset(request) -
1718 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04001719 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1720 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1721 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1722 break;
1723 }
1724 }
1725
1726 /* Ethernet header */
1727 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1728 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1729 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1730 reply->protocol = htons(ETH_P_IPV6);
1731
1732 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001733 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001734 skb_put(reply, sizeof(struct ipv6hdr));
1735
1736 /* IPv6 header */
1737
1738 pip6 = ipv6_hdr(reply);
1739 memset(pip6, 0, sizeof(struct ipv6hdr));
1740 pip6->version = 6;
1741 pip6->priority = ipv6_hdr(request)->priority;
1742 pip6->nexthdr = IPPROTO_ICMPV6;
1743 pip6->hop_limit = 255;
1744 pip6->daddr = ipv6_hdr(request)->saddr;
1745 pip6->saddr = *(struct in6_addr *)n->primary_key;
1746
1747 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001748 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001749
David Stevens4b29dba2014-03-24 10:39:58 -04001750 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02001751 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04001752 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1753 na->icmph.icmp6_router = isrouter;
1754 na->icmph.icmp6_override = 1;
1755 na->icmph.icmp6_solicited = 1;
1756 na->target = ns->target;
1757 ether_addr_copy(&na->opt[2], n->ha);
1758 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1759 na->opt[1] = na_olen >> 3;
1760
1761 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1762 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1763 csum_partial(na, sizeof(*na)+na_olen, 0));
1764
1765 pip6->payload_len = htons(sizeof(*na)+na_olen);
1766
1767 skb_push(reply, sizeof(struct ipv6hdr));
1768
1769 reply->ip_summed = CHECKSUM_UNNECESSARY;
1770
1771 return reply;
1772}
1773
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001774static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08001775{
1776 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08001777 const struct in6_addr *daddr;
Xin Long8bff36852017-11-11 19:58:50 +08001778 const struct ipv6hdr *iphdr;
David Stevens4b29dba2014-03-24 10:39:58 -04001779 struct inet6_dev *in6_dev;
Xin Long8bff36852017-11-11 19:58:50 +08001780 struct neighbour *n;
1781 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001782
1783 in6_dev = __in6_dev_get(dev);
1784 if (!in6_dev)
1785 goto out;
1786
Cong Wangf564f452013-08-31 13:44:36 +08001787 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001788 daddr = &iphdr->daddr;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001789 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08001790
David Stevens4b29dba2014-03-24 10:39:58 -04001791 if (ipv6_addr_loopback(daddr) ||
1792 ipv6_addr_is_multicast(&msg->target))
1793 goto out;
1794
1795 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001796
1797 if (n) {
1798 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001799 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001800
1801 if (!(n->nud_state & NUD_CONNECTED)) {
1802 neigh_release(n);
1803 goto out;
1804 }
1805
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001806 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08001807 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1808 /* bridge-local neighbor */
1809 neigh_release(n);
1810 goto out;
1811 }
1812
David Stevens4b29dba2014-03-24 10:39:58 -04001813 reply = vxlan_na_create(skb, n,
1814 !!(f ? f->flags & NTF_ROUTER : 0));
1815
Cong Wangf564f452013-08-31 13:44:36 +08001816 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001817
1818 if (reply == NULL)
1819 goto out;
1820
1821 if (netif_rx_ni(reply) == NET_RX_DROP)
1822 dev->stats.rx_dropped++;
1823
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001824 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001825 union vxlan_addr ipa = {
1826 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001827 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001828 };
1829
Cong Wangf564f452013-08-31 13:44:36 +08001830 vxlan_ip_miss(dev, &ipa);
1831 }
1832
1833out:
1834 consume_skb(skb);
1835 return NETDEV_TX_OK;
1836}
1837#endif
1838
David Stevense4f67ad2012-11-20 02:50:14 +00001839static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1840{
1841 struct vxlan_dev *vxlan = netdev_priv(dev);
1842 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001843
1844 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1845 return false;
1846
1847 n = NULL;
1848 switch (ntohs(eth_hdr(skb)->h_proto)) {
1849 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001850 {
1851 struct iphdr *pip;
1852
David Stevense4f67ad2012-11-20 02:50:14 +00001853 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1854 return false;
1855 pip = ip_hdr(skb);
1856 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001857 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001858 union vxlan_addr ipa = {
1859 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001860 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001861 };
1862
1863 vxlan_ip_miss(dev, &ipa);
1864 return false;
1865 }
1866
David Stevense4f67ad2012-11-20 02:50:14 +00001867 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001868 }
1869#if IS_ENABLED(CONFIG_IPV6)
1870 case ETH_P_IPV6:
1871 {
1872 struct ipv6hdr *pip6;
1873
1874 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1875 return false;
1876 pip6 = ipv6_hdr(skb);
1877 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001878 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08001879 union vxlan_addr ipa = {
1880 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001881 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001882 };
1883
1884 vxlan_ip_miss(dev, &ipa);
1885 return false;
1886 }
1887
1888 break;
1889 }
1890#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001891 default:
1892 return false;
1893 }
1894
1895 if (n) {
1896 bool diff;
1897
Joe Perches7367d0b2013-09-01 11:51:23 -07001898 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001899 if (diff) {
1900 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1901 dev->addr_len);
1902 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1903 }
1904 neigh_release(n);
1905 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001906 }
1907
David Stevense4f67ad2012-11-20 02:50:14 +00001908 return false;
1909}
1910
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001911static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001912 struct vxlan_metadata *md)
1913{
1914 struct vxlanhdr_gbp *gbp;
1915
Thomas Grafdb79a622015-02-04 17:00:04 +01001916 if (!md->gbp)
1917 return;
1918
Thomas Graf35114942015-01-15 03:53:55 +01001919 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001920 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001921
1922 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1923 gbp->dont_learn = 1;
1924
1925 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1926 gbp->policy_applied = 1;
1927
1928 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1929}
1930
Jiri Bence1e53142016-04-05 14:47:13 +02001931static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1932 __be16 protocol)
1933{
1934 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1935
1936 gpe->np_applied = 1;
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001937 gpe->next_protocol = tun_p_from_eth_p(protocol);
1938 if (!gpe->next_protocol)
1939 return -EPFNOSUPPORT;
1940 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02001941}
1942
Jiri Bencf491e562016-02-02 18:09:16 +01001943static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1944 int iphdr_len, __be32 vni,
1945 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001946 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001947{
Cong Wange4c7ed42013-08-31 13:44:33 +08001948 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001949 int min_headroom;
1950 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001951 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001952 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001953
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001954 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001955 skb->ip_summed == CHECKSUM_PARTIAL) {
1956 int csum_start = skb_checksum_start_offset(skb);
1957
1958 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1959 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1960 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001961 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001962 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001963 }
1964
Cong Wange4c7ed42013-08-31 13:44:33 +08001965 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08001966 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001967
1968 /* Need space for new headers (invalidates iph ptr) */
1969 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001970 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08001971 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001972
Alexander Duyckaed069d2016-04-14 15:33:37 -04001973 err = iptunnel_handle_offloads(skb, type);
1974 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08001975 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07001976
Johannes Bergd58ff352017-06-16 14:29:23 +02001977 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001978 vxh->vx_flags = VXLAN_HF_VNI;
1979 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001980
Tom Herbertdfd86452015-01-12 17:00:38 -08001981 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001982 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001983
Jiri Benc54bfd872016-02-16 21:58:58 +01001984 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1985 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1986 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001987
1988 if (!skb_is_gso(skb)) {
1989 skb->ip_summed = CHECKSUM_NONE;
1990 skb->encapsulation = 0;
1991 }
1992 }
1993
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001994 if (vxflags & VXLAN_F_GBP)
1995 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001996 if (vxflags & VXLAN_F_GPE) {
1997 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1998 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08001999 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02002000 inner_protocol = skb->protocol;
2001 }
Thomas Graf35114942015-01-15 03:53:55 +01002002
Jiri Bence1e53142016-04-05 14:47:13 +02002003 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08002004 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07002005}
Pravin B Shelar49560532013-08-19 11:23:17 -07002006
pravin shelar655c3de2016-11-13 20:43:55 -08002007static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2008 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002009 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002010 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002011 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002012 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01002013{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002014 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002015 struct rtable *rt = NULL;
2016 struct flowi4 fl4;
2017
pravin shelar655c3de2016-11-13 20:43:55 -08002018 if (!sock4)
2019 return ERR_PTR(-EIO);
2020
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002021 if (tos && !info)
2022 use_cache = false;
2023 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002024 rt = dst_cache_get_ip4(dst_cache, saddr);
2025 if (rt)
2026 return rt;
2027 }
2028
Jiri Benc1a8496b2016-02-02 18:09:14 +01002029 memset(&fl4, 0, sizeof(fl4));
2030 fl4.flowi4_oif = oif;
2031 fl4.flowi4_tos = RT_TOS(tos);
2032 fl4.flowi4_mark = skb->mark;
2033 fl4.flowi4_proto = IPPROTO_UDP;
2034 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002035 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002036 fl4.fl4_dport = dport;
2037 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002038
2039 rt = ip_route_output_key(vxlan->net, &fl4);
pravin shelar655c3de2016-11-13 20:43:55 -08002040 if (likely(!IS_ERR(rt))) {
2041 if (rt->dst.dev == dev) {
2042 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2043 ip_rt_put(rt);
2044 return ERR_PTR(-ELOOP);
2045 }
2046
Jiri Benc1a8496b2016-02-02 18:09:14 +01002047 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002048 if (use_cache)
2049 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08002050 } else {
2051 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2052 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002053 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002054 return rt;
2055}
2056
Jiri Bence5d4b292015-12-07 13:04:30 +01002057#if IS_ENABLED(CONFIG_IPV6)
2058static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08002059 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08002060 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01002061 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002062 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01002063 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002064 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002065 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002066 struct dst_cache *dst_cache,
2067 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01002068{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002069 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002070 struct dst_entry *ndst;
2071 struct flowi6 fl6;
2072 int err;
2073
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002074 if (!sock6)
2075 return ERR_PTR(-EIO);
2076
Daniel Borkmann14006152016-03-04 15:15:08 +01002077 if (tos && !info)
2078 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002079 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002080 ndst = dst_cache_get_ip6(dst_cache, saddr);
2081 if (ndst)
2082 return ndst;
2083 }
2084
Jiri Bence5d4b292015-12-07 13:04:30 +01002085 memset(&fl6, 0, sizeof(fl6));
2086 fl6.flowi6_oif = oif;
2087 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07002088 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01002089 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01002090 fl6.flowi6_mark = skb->mark;
2091 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002092 fl6.fl6_dport = dport;
2093 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01002094
2095 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002096 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01002097 &ndst, &fl6);
pravin shelar655c3de2016-11-13 20:43:55 -08002098 if (unlikely(err < 0)) {
2099 netdev_dbg(dev, "no route to %pI6\n", daddr);
2100 return ERR_PTR(-ENETUNREACH);
2101 }
2102
2103 if (unlikely(ndst->dev == dev)) {
2104 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2105 dst_release(ndst);
2106 return ERR_PTR(-ELOOP);
2107 }
Jiri Bence5d4b292015-12-07 13:04:30 +01002108
2109 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002110 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002111 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01002112 return ndst;
2113}
2114#endif
2115
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002116/* Bypass encapsulation if the destination is local */
2117static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002118 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002119{
Li RongQing8f849852014-01-04 13:57:59 +08002120 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08002121 union vxlan_addr loopback;
2122 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08002123 struct net_device *dev = skb->dev;
2124 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002125
Li RongQing8f849852014-01-04 13:57:59 +08002126 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2127 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002128 skb->pkt_type = PACKET_HOST;
2129 skb->encapsulation = 0;
2130 skb->dev = dst_vxlan->dev;
2131 __skb_pull(skb, skb_network_offset(skb));
2132
Cong Wange4c7ed42013-08-31 13:44:33 +08002133 if (remote_ip->sa.sa_family == AF_INET) {
2134 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2135 loopback.sa.sa_family = AF_INET;
2136#if IS_ENABLED(CONFIG_IPV6)
2137 } else {
2138 loopback.sin6.sin6_addr = in6addr_loopback;
2139 loopback.sa.sa_family = AF_INET6;
2140#endif
2141 }
2142
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002143 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
Matthias Schiffer87613de2017-06-19 10:03:59 +02002144 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0,
2145 vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002146
2147 u64_stats_update_begin(&tx_stats->syncp);
2148 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002149 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002150 u64_stats_update_end(&tx_stats->syncp);
2151
2152 if (netif_rx(skb) == NET_RX_SUCCESS) {
2153 u64_stats_update_begin(&rx_stats->syncp);
2154 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002155 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002156 u64_stats_update_end(&rx_stats->syncp);
2157 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08002158 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002159 }
2160}
2161
pravin shelarfee1fad2016-11-13 20:43:56 -08002162static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002163 struct vxlan_dev *vxlan,
2164 union vxlan_addr *daddr,
2165 __be16 dst_port, int dst_ifindex, __be32 vni,
2166 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002167 u32 rt_flags)
2168{
2169#if IS_ENABLED(CONFIG_IPV6)
2170 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2171 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2172 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2173 */
2174 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2175#endif
2176 /* Bypass encapsulation if the destination is local */
2177 if (rt_flags & RTCF_LOCAL &&
2178 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2179 struct vxlan_dev *dst_vxlan;
2180
2181 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002182 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002183 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002184 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002185 if (!dst_vxlan) {
2186 dev->stats.tx_errors++;
2187 kfree_skb(skb);
2188
2189 return -ENOENT;
2190 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002191 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002192 return 1;
2193 }
2194
2195 return 0;
2196}
2197
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002198static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002199 __be32 default_vni, struct vxlan_rdst *rdst,
2200 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002201{
Paolo Abenid71785f2016-02-12 15:43:57 +01002202 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002203 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002204 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002205 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002206 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002207 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002208 struct vxlan_metadata _md;
2209 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002210 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002211 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002212 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002213 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002214 int ifindex;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07002215 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002216 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002217 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002218 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002219
Jiri Benc61adedf2015-08-20 13:56:25 +02002220 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002221
Thomas Grafee122c72015-07-21 10:43:58 +02002222 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002223 dst = &rdst->remote_ip;
2224 if (vxlan_addr_any(dst)) {
2225 if (did_rsc) {
2226 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002227 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002228 return;
2229 }
2230 goto drop;
2231 }
2232
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002233 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002234 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002235 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002236 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002237 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002238 md->gbp = skb->mark;
Hangbin Liu72f6d712018-04-17 14:11:28 +08002239 if (flags & VXLAN_F_TTL_INHERIT) {
2240 ttl = ip_tunnel_get_ttl(old_iph, skb);
2241 } else {
2242 ttl = vxlan->cfg.ttl;
2243 if (!ttl && vxlan_addr_multicast(dst))
2244 ttl = 1;
2245 }
pravin shelar0770b532016-11-13 20:43:57 -08002246
2247 tos = vxlan->cfg.tos;
2248 if (tos == 1)
2249 tos = ip_tunnel_get_dsfield(old_iph, skb);
2250
2251 if (dst->sa.sa_family == AF_INET)
2252 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2253 else
2254 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2255 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002256 } else {
2257 if (!info) {
2258 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2259 dev->name);
2260 goto drop;
2261 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002262 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002263 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002264 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002265 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2266 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002267 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002268 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2269 }
Thomas Grafee122c72015-07-21 10:43:58 +02002270 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002271 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2272 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002273 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002274 dst_cache = &info->dst_cache;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002275 if (info->options_len &&
2276 info->key.tun_flags & TUNNEL_VXLAN_OPT)
pravin shelar0770b532016-11-13 20:43:57 -08002277 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002278 ttl = info->key.ttl;
2279 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002280 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002281 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002282 }
pravin shelar0770b532016-11-13 20:43:57 -08002283 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2284 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002285
Jakub Kicinski56de8592017-02-24 11:43:36 -08002286 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002287 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002288 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002289 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002290 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002291
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002292 if (!ifindex)
2293 ifindex = sock4->sock->sk->sk_bound_dev_if;
2294
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002295 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002296 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002297 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002298 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002299 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002300 if (IS_ERR(rt)) {
2301 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002302 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002303 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002304
pravin shelarfee1fad2016-11-13 20:43:56 -08002305 if (!info) {
Stefano Briviob4d306972018-11-08 12:19:16 +01002306 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002307 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002308 dst_port, ifindex, vni,
2309 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002310 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002311 goto out_unlock;
Stefano Briviob4d306972018-11-08 12:19:16 +01002312
2313 if (vxlan->cfg.df == VXLAN_DF_SET) {
2314 df = htons(IP_DF);
2315 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2316 struct ethhdr *eth = eth_hdr(skb);
2317
2318 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2319 (ntohs(eth->h_proto) == ETH_P_IP &&
2320 old_iph->frag_off & htons(IP_DF)))
2321 df = htons(IP_DF);
2322 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002323 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002324 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002325 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002326
pravin shelarc46b7892016-11-13 20:43:54 -08002327 ndst = &rt->dst;
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002328 skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002329
Cong Wange4c7ed42013-08-31 13:44:33 +08002330 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2331 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002332 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002333 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002334 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002335 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002336
Brian Russell11586322017-02-24 17:47:11 +00002337 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002338 dst->sin.sin_addr.s_addr, tos, ttl, df,
2339 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002340#if IS_ENABLED(CONFIG_IPV6)
2341 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002342 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002343
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002344 if (!ifindex)
2345 ifindex = sock6->sock->sk->sk_bound_dev_if;
2346
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002347 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002348 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002349 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002350 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002351 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002352 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002353 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002354 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002355 goto tx_error;
2356 }
pravin shelar655c3de2016-11-13 20:43:55 -08002357
pravin shelarfee1fad2016-11-13 20:43:56 -08002358 if (!info) {
2359 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002360
pravin shelarfee1fad2016-11-13 20:43:56 -08002361 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002362 dst_port, ifindex, vni,
2363 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002364 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002365 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002366 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002367
Stefano Brivio6b4f92a2018-10-12 23:53:59 +02002368 skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
Xin Longa93bf0f2017-12-18 14:20:56 +08002369
Daniel Borkmann14006152016-03-04 15:15:08 +01002370 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002371 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002372 skb_scrub_packet(skb, xnet);
2373 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002374 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002375 if (err < 0)
2376 goto tx_error;
2377
pravin shelar0770b532016-11-13 20:43:57 -08002378 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002379 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002380 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002381 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002382#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002383 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002384out_unlock:
2385 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002386 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002387
2388drop:
2389 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002390 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002391 return;
2392
2393tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002394 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002395 if (err == -ELOOP)
2396 dev->stats.collisions++;
2397 else if (err == -ENETUNREACH)
2398 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002399 dst_release(ndst);
2400 dev->stats.tx_errors++;
2401 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002402}
2403
David Stevens66817122013-03-15 04:35:51 +00002404/* Transmit local packets over Vxlan
2405 *
2406 * Outer IP header inherits ECN and DF from inner header.
2407 * Outer UDP destination is the VXLAN assigned port.
2408 * source port is based on hash of flow
2409 */
2410static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2411{
2412 struct vxlan_dev *vxlan = netdev_priv(dev);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002413 struct vxlan_rdst *rdst, *fdst = NULL;
Xin Long8bff36852017-11-11 19:58:50 +08002414 const struct ip_tunnel_info *info;
2415 bool did_rsc = false;
David Stevens66817122013-03-15 04:35:51 +00002416 struct vxlan_fdb *f;
Xin Long8bff36852017-11-11 19:58:50 +08002417 struct ethhdr *eth;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002418 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002419
Jiri Benc61adedf2015-08-20 13:56:25 +02002420 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002421
David Stevens66817122013-03-15 04:35:51 +00002422 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002423
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002424 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002425 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2426 info->mode & IP_TUNNEL_INFO_TX) {
2427 vni = tunnel_id_to_key32(info->key.tun_id);
2428 } else {
2429 if (info && info->mode & IP_TUNNEL_INFO_TX)
2430 vxlan_xmit_one(skb, dev, vni, NULL, false);
2431 else
2432 kfree_skb(skb);
2433 return NETDEV_TX_OK;
2434 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002435 }
2436
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002437 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002438 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002439 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002440 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002441#if IS_ENABLED(CONFIG_IPV6)
Xin Long8bff36852017-11-11 19:58:50 +08002442 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2443 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2444 sizeof(struct nd_msg)) &&
2445 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2446 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2447
2448 if (m->icmph.icmp6_code == 0 &&
2449 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002450 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002451 }
2452#endif
2453 }
David Stevens66817122013-03-15 04:35:51 +00002454
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002455 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002456 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002457 did_rsc = false;
2458
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002459 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002460 (ntohs(eth->h_proto) == ETH_P_IP ||
2461 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002462 did_rsc = route_shortcircuit(dev, skb);
2463 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002464 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002465 }
2466
David Stevens66817122013-03-15 04:35:51 +00002467 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002468 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002469 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002470 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002471 !is_multicast_ether_addr(eth->h_dest))
2472 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002473
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002474 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002475 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002476 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002477 }
David Stevens66817122013-03-15 04:35:51 +00002478 }
2479
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002480 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2481 struct sk_buff *skb1;
2482
Eric Dumazet8f646c92014-01-06 09:54:31 -08002483 if (!fdst) {
2484 fdst = rdst;
2485 continue;
2486 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002487 skb1 = skb_clone(skb, GFP_ATOMIC);
2488 if (skb1)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002489 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002490 }
2491
Eric Dumazet8f646c92014-01-06 09:54:31 -08002492 if (fdst)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002493 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002494 else
2495 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002496 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002497}
2498
stephen hemmingerd3428942012-10-01 12:32:35 +00002499/* Walk the forwarding table and purge stale entries */
Kees Cookdf7e8282017-10-04 16:26:59 -07002500static void vxlan_cleanup(struct timer_list *t)
stephen hemmingerd3428942012-10-01 12:32:35 +00002501{
Kees Cookdf7e8282017-10-04 16:26:59 -07002502 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
stephen hemmingerd3428942012-10-01 12:32:35 +00002503 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2504 unsigned int h;
2505
2506 if (!netif_running(vxlan->dev))
2507 return;
2508
stephen hemmingerd3428942012-10-01 12:32:35 +00002509 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2510 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002511
2512 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002513 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2514 struct vxlan_fdb *f
2515 = container_of(p, struct vxlan_fdb, hlist);
2516 unsigned long timeout;
2517
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002518 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002519 continue;
2520
Roopa Prabhudef499c2017-03-27 15:46:41 -07002521 if (f->flags & NTF_EXT_LEARNED)
2522 continue;
2523
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002524 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002525 if (time_before_eq(timeout, jiffies)) {
2526 netdev_dbg(vxlan->dev,
2527 "garbage collect %pM\n",
2528 f->eth_addr);
2529 f->state = NUD_STALE;
Petr Machata0e6160f2018-11-21 08:02:35 +00002530 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002531 } else if (time_before(timeout, next_timer))
2532 next_timer = timeout;
2533 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002534 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002535 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002536
2537 mod_timer(&vxlan->age_timer, next_timer);
2538}
2539
Mark Blocha53cb292017-06-02 03:24:08 +03002540static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2541{
2542 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2543
2544 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002545 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2546#if IS_ENABLED(CONFIG_IPV6)
2547 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2548#endif
Mark Blocha53cb292017-06-02 03:24:08 +03002549 spin_unlock(&vn->sock_lock);
2550}
2551
Jiri Benc69e76662017-07-02 19:00:57 +02002552static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2553 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002554{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002555 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002556 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002557
Jiri Benc69e76662017-07-02 19:00:57 +02002558 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002559 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002560 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002561 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002562}
2563
stephen hemmingerd3428942012-10-01 12:32:35 +00002564/* Setup stats when device is created */
2565static int vxlan_init(struct net_device *dev)
2566{
WANG Cong1c213bd2014-02-13 11:46:28 -08002567 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002568 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002569 return -ENOMEM;
2570
2571 return 0;
2572}
2573
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002574static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002575{
2576 struct vxlan_fdb *f;
2577
2578 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002579 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002580 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00002581 vxlan_fdb_destroy(vxlan, f, true, true);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002582 spin_unlock_bh(&vxlan->hash_lock);
2583}
2584
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002585static void vxlan_uninit(struct net_device *dev)
2586{
2587 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002588
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002589 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002590
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002591 free_percpu(dev->tstats);
2592}
2593
stephen hemmingerd3428942012-10-01 12:32:35 +00002594/* Start ageing timer and join group when device is brought up */
2595static int vxlan_open(struct net_device *dev)
2596{
2597 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002598 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002599
Jiri Benc205f3562015-09-24 13:50:01 +02002600 ret = vxlan_sock_add(vxlan);
2601 if (ret < 0)
2602 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002603
Gao feng79d4a942013-12-10 16:37:32 +08002604 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002605 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002606 if (ret == -EADDRINUSE)
2607 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002608 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002609 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002610 return ret;
2611 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002612 }
2613
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002614 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002615 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2616
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002617 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002618}
2619
2620/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002621static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00002622{
Cong Wang31fec5a2013-05-27 22:35:52 +00002623 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002624
2625 spin_lock_bh(&vxlan->hash_lock);
2626 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2627 struct hlist_node *p, *n;
2628 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2629 struct vxlan_fdb *f
2630 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002631 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2632 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002633 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2634 if (!is_zero_ether_addr(f->eth_addr))
Petr Machata0e6160f2018-11-21 08:02:35 +00002635 vxlan_fdb_destroy(vxlan, f, true, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002636 }
2637 }
2638 spin_unlock_bh(&vxlan->hash_lock);
2639}
2640
2641/* Cleanup timer and forwarding table on shutdown */
2642static int vxlan_stop(struct net_device *dev)
2643{
2644 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002645 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002646 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002647
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002648 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002649 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002650 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002651
2652 del_timer_sync(&vxlan->age_timer);
2653
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002654 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02002655 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002656
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002657 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002658}
2659
stephen hemmingerd3428942012-10-01 12:32:35 +00002660/* Stub, nothing needs to be done. */
2661static void vxlan_set_multicast_list(struct net_device *dev)
2662{
2663}
2664
Daniel Borkmann345010b2013-12-18 00:21:08 +01002665static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2666{
2667 struct vxlan_dev *vxlan = netdev_priv(dev);
2668 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002669 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2670 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002671 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04002672
2673 /* This check is different than dev->max_mtu, because it looks at
2674 * the lowerdev->mtu, rather than the static dev->max_mtu
2675 */
2676 if (lowerdev) {
2677 int max_mtu = lowerdev->mtu -
2678 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2679 if (new_mtu > max_mtu)
2680 return -EINVAL;
2681 }
2682
2683 dev->mtu = new_mtu;
2684 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01002685}
2686
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002687static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2688{
2689 struct vxlan_dev *vxlan = netdev_priv(dev);
2690 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2691 __be16 sport, dport;
2692
2693 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2694 vxlan->cfg.port_max, true);
2695 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2696
Jiri Benc239e9442015-12-07 13:04:31 +01002697 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002698 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002699 struct rtable *rt;
2700
pravin shelar655c3de2016-11-13 20:43:55 -08002701 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002702 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002703 &info->key.u.ipv4.src, dport, sport,
2704 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002705 if (IS_ERR(rt))
2706 return PTR_ERR(rt);
2707 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002708 } else {
2709#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08002710 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01002711 struct dst_entry *ndst;
2712
pravin shelar655c3de2016-11-13 20:43:55 -08002713 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002714 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002715 &info->key.u.ipv6.src, dport, sport,
2716 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002717 if (IS_ERR(ndst))
2718 return PTR_ERR(ndst);
2719 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002720#else /* !CONFIG_IPV6 */
2721 return -EPFNOSUPPORT;
2722#endif
2723 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002724 info->key.tp_src = sport;
2725 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002726 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002727}
2728
Jiri Benc0c867c92016-04-05 14:47:10 +02002729static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002730 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002731 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002732 .ndo_open = vxlan_open,
2733 .ndo_stop = vxlan_stop,
2734 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002735 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002736 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002737 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002738 .ndo_validate_addr = eth_validate_addr,
2739 .ndo_set_mac_address = eth_mac_addr,
2740 .ndo_fdb_add = vxlan_fdb_add,
2741 .ndo_fdb_del = vxlan_fdb_delete,
2742 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002743 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002744};
2745
Jiri Bence1e53142016-04-05 14:47:13 +02002746static const struct net_device_ops vxlan_netdev_raw_ops = {
2747 .ndo_init = vxlan_init,
2748 .ndo_uninit = vxlan_uninit,
2749 .ndo_open = vxlan_open,
2750 .ndo_stop = vxlan_stop,
2751 .ndo_start_xmit = vxlan_xmit,
2752 .ndo_get_stats64 = ip_tunnel_get_stats64,
2753 .ndo_change_mtu = vxlan_change_mtu,
2754 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2755};
2756
stephen hemmingerd3428942012-10-01 12:32:35 +00002757/* Info for udev, that this is a virtual tunnel endpoint */
2758static struct device_type vxlan_type = {
2759 .name = "vxlan",
2760};
2761
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002762/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002763 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002764 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002765 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002766static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002767{
2768 struct vxlan_sock *vs;
2769 struct net *net = dev_net(dev);
2770 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002771 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002772
2773 spin_lock(&vn->sock_lock);
2774 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002775 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2776 unsigned short type;
2777
2778 if (vs->flags & VXLAN_F_GPE)
2779 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
2780 else
2781 type = UDP_TUNNEL_TYPE_VXLAN;
2782
2783 if (push)
2784 udp_tunnel_push_rx_port(dev, vs->sock, type);
2785 else
2786 udp_tunnel_drop_rx_port(dev, vs->sock, type);
2787 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002788 }
2789 spin_unlock(&vn->sock_lock);
2790}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002791
stephen hemmingerd3428942012-10-01 12:32:35 +00002792/* Initialize the device structure. */
2793static void vxlan_setup(struct net_device *dev)
2794{
2795 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002796 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002797
Jiri Benc65226ef2016-04-28 16:36:30 +02002798 eth_hw_addr_random(dev);
2799 ether_setup(dev);
2800
David S. Millercf124db2017-05-08 12:52:56 -04002801 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00002802 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2803
stephen hemmingerd3428942012-10-01 12:32:35 +00002804 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002805 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002806 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002807 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002808
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002809 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002810 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002811 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07002812 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002813 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002814
Matthias Schiffera9853432017-06-19 10:03:55 +02002815 /* MTU range: 68 - 65535 */
2816 dev->min_mtu = ETH_MIN_MTU;
2817 dev->max_mtu = ETH_MAX_MTU;
2818
stephen hemminger553675f2013-05-16 11:35:20 +00002819 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002820 spin_lock_init(&vxlan->hash_lock);
2821
Kees Cookdf7e8282017-10-04 16:26:59 -07002822 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
stephen hemmingerd3428942012-10-01 12:32:35 +00002823
2824 vxlan->dev = dev;
2825
Tom Herbert58ce31c2015-08-19 17:07:33 -07002826 gro_cells_init(&vxlan->gro_cells, dev);
2827
stephen hemmingerd3428942012-10-01 12:32:35 +00002828 for (h = 0; h < FDB_HASH_SIZE; ++h)
2829 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2830}
2831
Jiri Benc0c867c92016-04-05 14:47:10 +02002832static void vxlan_ether_setup(struct net_device *dev)
2833{
Jiri Benc0c867c92016-04-05 14:47:10 +02002834 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2835 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2836 dev->netdev_ops = &vxlan_netdev_ether_ops;
2837}
2838
Jiri Bence1e53142016-04-05 14:47:13 +02002839static void vxlan_raw_setup(struct net_device *dev)
2840{
Jiri Benc65226ef2016-04-28 16:36:30 +02002841 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002842 dev->type = ARPHRD_NONE;
2843 dev->hard_header_len = 0;
2844 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002845 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2846 dev->netdev_ops = &vxlan_netdev_raw_ops;
2847}
2848
stephen hemmingerd3428942012-10-01 12:32:35 +00002849static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2850 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002851 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002852 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002853 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2854 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002855 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002856 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2857 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002858 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002859 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2860 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2861 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002862 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002863 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2864 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2865 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2866 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002867 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002868 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002869 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2870 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2871 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002872 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2873 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002874 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002875 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002876 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
Hangbin Liu72f6d712018-04-17 14:11:28 +08002877 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
Stefano Briviob4d306972018-11-08 12:19:16 +01002878 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002879};
2880
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002881static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
2882 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00002883{
2884 if (tb[IFLA_ADDRESS]) {
2885 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002886 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
2887 "Provided link layer address is not Ethernet");
stephen hemmingerd3428942012-10-01 12:32:35 +00002888 return -EINVAL;
2889 }
2890
2891 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002892 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
2893 "Provided Ethernet address is not unicast");
stephen hemmingerd3428942012-10-01 12:32:35 +00002894 return -EADDRNOTAVAIL;
2895 }
2896 }
2897
Matthias Schiffera9853432017-06-19 10:03:55 +02002898 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02002899 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02002900
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002901 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
2902 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
2903 "MTU must be between 68 and 65535");
Matthias Schiffera9853432017-06-19 10:03:55 +02002904 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002905 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002906 }
2907
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002908 if (!data) {
2909 NL_SET_ERR_MSG(extack,
2910 "Required attributes not provided to perform the operation");
stephen hemmingerd3428942012-10-01 12:32:35 +00002911 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002912 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002913
2914 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02002915 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2916
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002917 if (id >= VXLAN_N_VID) {
2918 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID],
2919 "VXLAN ID must be lower than 16777216");
stephen hemmingerd3428942012-10-01 12:32:35 +00002920 return -ERANGE;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002921 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002922 }
2923
stephen hemminger05f47d62012-10-09 20:35:50 +00002924 if (data[IFLA_VXLAN_PORT_RANGE]) {
2925 const struct ifla_vxlan_port_range *p
2926 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2927
2928 if (ntohs(p->high) < ntohs(p->low)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002929 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
2930 "Invalid source port range");
stephen hemminger05f47d62012-10-09 20:35:50 +00002931 return -EINVAL;
2932 }
2933 }
2934
Stefano Briviob4d306972018-11-08 12:19:16 +01002935 if (data[IFLA_VXLAN_DF]) {
2936 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
2937
2938 if (df < 0 || df > VXLAN_DF_MAX) {
2939 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_DF],
2940 "Invalid DF attribute");
2941 return -EINVAL;
2942 }
2943 }
2944
stephen hemmingerd3428942012-10-01 12:32:35 +00002945 return 0;
2946}
2947
Yan Burman1b13c972013-01-29 23:43:07 +00002948static void vxlan_get_drvinfo(struct net_device *netdev,
2949 struct ethtool_drvinfo *drvinfo)
2950{
2951 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2952 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2953}
2954
2955static const struct ethtool_ops vxlan_ethtool_ops = {
2956 .get_drvinfo = vxlan_get_drvinfo,
2957 .get_link = ethtool_op_get_link,
2958};
2959
Tom Herbert3ee64f32014-07-13 19:49:42 -07002960static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002961 __be16 port, u32 flags, int ifindex)
stephen hemminger553675f2013-05-16 11:35:20 +00002962{
Cong Wange4c7ed42013-08-31 13:44:33 +08002963 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002964 struct udp_port_cfg udp_conf;
2965 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002966
Tom Herbert3ee64f32014-07-13 19:49:42 -07002967 memset(&udp_conf, 0, sizeof(udp_conf));
2968
2969 if (ipv6) {
2970 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002971 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002972 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002973 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002974 } else {
2975 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002976 }
2977
Tom Herbert3ee64f32014-07-13 19:49:42 -07002978 udp_conf.local_udp_port = port;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002979 udp_conf.bind_ifindex = ifindex;
Cong Wange4c7ed42013-08-31 13:44:33 +08002980
Tom Herbert3ee64f32014-07-13 19:49:42 -07002981 /* Open UDP socket */
2982 err = udp_sock_create(net, &udp_conf, &sock);
2983 if (err < 0)
2984 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002985
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002986 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002987}
2988
2989/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002990static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01002991 __be16 port, u32 flags,
2992 int ifindex)
Cong Wange4c7ed42013-08-31 13:44:33 +08002993{
2994 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2995 struct vxlan_sock *vs;
2996 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002997 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002998 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002999
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02003000 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08003001 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00003002 return ERR_PTR(-ENOMEM);
3003
3004 for (h = 0; h < VNI_HASH_SIZE; ++h)
3005 INIT_HLIST_HEAD(&vs->vni_list[h]);
3006
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003007 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08003008 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00003009 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08003010 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00003011 }
3012
Cong Wange4c7ed42013-08-31 13:44:33 +08003013 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03003014 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08003015 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00003016
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003017 spin_lock(&vn->sock_lock);
3018 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07003019 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07003020 (vs->flags & VXLAN_F_GPE) ?
3021 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07003022 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003023 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00003024
3025 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07003026 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07003027 tunnel_cfg.sk_user_data = vs;
3028 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01003029 tunnel_cfg.encap_rcv = vxlan_rcv;
Stefano Brivioc3a43b92018-11-08 12:19:15 +01003030 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003031 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07003032 tunnel_cfg.gro_receive = vxlan_gro_receive;
3033 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07003034
3035 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08003036
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003037 return vs;
3038}
stephen hemminger553675f2013-05-16 11:35:20 +00003039
Jiri Bencb1be00a2015-09-24 13:50:02 +02003040static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003041{
Jiri Benc205f3562015-09-24 13:50:01 +02003042 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3043 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02003044 struct vxlan_dev_node *node;
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003045 int l3mdev_index = 0;
3046
3047 if (vxlan->cfg.remote_ifindex)
3048 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3049 vxlan->net, vxlan->cfg.remote_ifindex);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003050
Jiri Benc205f3562015-09-24 13:50:01 +02003051 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003052 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003053 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003054 vxlan->cfg.dst_port, vxlan->cfg.flags,
3055 l3mdev_index);
Reshetova, Elena66af8462017-07-04 15:52:59 +03003056 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003057 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02003058 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03003059 }
3060 spin_unlock(&vn->sock_lock);
3061 }
Jiri Benc205f3562015-09-24 13:50:01 +02003062 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003063 vs = vxlan_socket_create(vxlan->net, ipv6,
Alexis Bauvinaab8cc32018-12-03 10:54:40 +01003064 vxlan->cfg.dst_port, vxlan->cfg.flags,
3065 l3mdev_index);
Jiri Benc205f3562015-09-24 13:50:01 +02003066 if (IS_ERR(vs))
3067 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003068#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02003069 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003070 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003071 node = &vxlan->hlist6;
3072 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02003073#endif
Jiri Benc69e76662017-07-02 19:00:57 +02003074 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003075 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02003076 node = &vxlan->hlist4;
3077 }
3078 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02003079 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00003080}
3081
Jiri Bencb1be00a2015-09-24 13:50:02 +02003082static int vxlan_sock_add(struct vxlan_dev *vxlan)
3083{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003084 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3085 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02003086 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003087 int ret = 0;
3088
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003089 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02003090#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07003091 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02003092 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02003093 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02003094 if (ret < 0 && ret != -EAFNOSUPPORT)
3095 ipv4 = false;
3096 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02003097#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02003098 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02003099 ret = __vxlan_sock_add(vxlan, false);
3100 if (ret < 0)
3101 vxlan_sock_release(vxlan);
3102 return ret;
3103}
3104
Matthias Schiffera9853432017-06-19 10:03:55 +02003105static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3106 struct net_device **lower,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003107 struct vxlan_dev *old,
3108 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00003109{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01003110 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02003111 struct vxlan_dev *tmp;
3112 bool use_ipv6 = false;
3113
3114 if (conf->flags & VXLAN_F_GPE) {
3115 /* For now, allow GPE only together with
3116 * COLLECT_METADATA. This can be relaxed later; in such
3117 * case, the other side of the PtP link will have to be
3118 * provided.
3119 */
3120 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3121 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003122 NL_SET_ERR_MSG(extack,
3123 "VXLAN GPE does not support this combination of attributes");
Matthias Schiffera9853432017-06-19 10:03:55 +02003124 return -EINVAL;
3125 }
3126 }
3127
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003128 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3129 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02003130 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003131 conf->saddr.sa.sa_family = AF_INET;
3132 } else if (!conf->remote_ip.sa.sa_family) {
3133 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3134 } else if (!conf->saddr.sa.sa_family) {
3135 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3136 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003137
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003138 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3139 NL_SET_ERR_MSG(extack,
3140 "Local and remote address must be from the same family");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003141 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003142 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003143
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003144 if (vxlan_addr_multicast(&conf->saddr)) {
3145 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003146 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003147 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003148
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003149 if (conf->saddr.sa.sa_family == AF_INET6) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003150 if (!IS_ENABLED(CONFIG_IPV6)) {
3151 NL_SET_ERR_MSG(extack,
3152 "IPv6 support not enabled in the kernel");
Matthias Schiffera9853432017-06-19 10:03:55 +02003153 return -EPFNOSUPPORT;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003154 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003155 use_ipv6 = true;
3156 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003157
3158 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3159 int local_type =
3160 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3161 int remote_type =
3162 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3163
3164 if (local_type & IPV6_ADDR_LINKLOCAL) {
3165 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003166 (remote_type != IPV6_ADDR_ANY)) {
3167 NL_SET_ERR_MSG(extack,
3168 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003169 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003170 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003171
3172 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3173 } else {
3174 if (remote_type ==
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003175 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3176 NL_SET_ERR_MSG(extack,
3177 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003178 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003179 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003180
3181 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3182 }
3183 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003184 }
3185
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003186 if (conf->label && !use_ipv6) {
3187 NL_SET_ERR_MSG(extack,
3188 "Label attribute only applies to IPv6 VXLAN devices");
Matthias Schiffera9853432017-06-19 10:03:55 +02003189 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003190 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003191
3192 if (conf->remote_ifindex) {
3193 struct net_device *lowerdev;
3194
3195 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003196 if (!lowerdev) {
3197 NL_SET_ERR_MSG(extack,
3198 "Invalid local interface, device not found");
Matthias Schiffera9853432017-06-19 10:03:55 +02003199 return -ENODEV;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003200 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003201
3202#if IS_ENABLED(CONFIG_IPV6)
3203 if (use_ipv6) {
3204 struct inet6_dev *idev = __in6_dev_get(lowerdev);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003205 if (idev && idev->cnf.disable_ipv6) {
3206 NL_SET_ERR_MSG(extack,
3207 "IPv6 support disabled by administrator");
Matthias Schiffera9853432017-06-19 10:03:55 +02003208 return -EPERM;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003209 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003210 }
3211#endif
3212
3213 *lower = lowerdev;
3214 } else {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003215 if (vxlan_addr_multicast(&conf->remote_ip)) {
3216 NL_SET_ERR_MSG(extack,
3217 "Local interface required for multicast remote destination");
3218
Matthias Schiffera9853432017-06-19 10:03:55 +02003219 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003220 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003221
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003222#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003223 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3224 NL_SET_ERR_MSG(extack,
3225 "Local interface required for link-local local/remote addresses");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003226 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003227 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003228#endif
3229
Matthias Schiffera9853432017-06-19 10:03:55 +02003230 *lower = NULL;
3231 }
3232
3233 if (!conf->dst_port) {
3234 if (conf->flags & VXLAN_F_GPE)
3235 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3236 else
3237 conf->dst_port = htons(vxlan_port);
3238 }
3239
3240 if (!conf->age_interval)
3241 conf->age_interval = FDB_AGE_DEFAULT;
3242
3243 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3244 if (tmp == old)
3245 continue;
3246
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003247 if (tmp->cfg.vni != conf->vni)
3248 continue;
3249 if (tmp->cfg.dst_port != conf->dst_port)
3250 continue;
3251 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003252 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003253 continue;
3254
3255 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3256 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3257 continue;
3258
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003259 NL_SET_ERR_MSG(extack,
3260 "A VXLAN device with the specified VNI already exists");
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003261 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003262 }
3263
3264 return 0;
3265}
3266
3267static void vxlan_config_apply(struct net_device *dev,
3268 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003269 struct net_device *lowerdev,
3270 struct net *src_net,
3271 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003272{
3273 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003274 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003275 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003276 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3277 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003278
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003279 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003280 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003281 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003282 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003283 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003284
Matthias Schiffera9853432017-06-19 10:03:55 +02003285 if (conf->mtu)
3286 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003287
3288 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003289 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003290
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003291 dst->remote_vni = conf->vni;
3292
3293 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003294
Matthias Schiffera9853432017-06-19 10:03:55 +02003295 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003296 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003297
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003298 dev->gso_max_size = lowerdev->gso_max_size;
3299 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003300
3301 needed_headroom = lowerdev->hard_header_len;
3302
3303 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3304 VXLAN_HEADROOM);
Alexey Kodanevf870c1f2017-12-14 20:20:00 +03003305 if (max_mtu < ETH_MIN_MTU)
3306 max_mtu = ETH_MIN_MTU;
3307
3308 if (!changelink && !conf->mtu)
3309 dev->mtu = max_mtu;
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003310 }
3311
Matthias Schiffera9853432017-06-19 10:03:55 +02003312 if (dev->mtu > max_mtu)
3313 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003314
Jiri Bencb1be00a2015-09-24 13:50:02 +02003315 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3316 needed_headroom += VXLAN6_HEADROOM;
3317 else
3318 needed_headroom += VXLAN_HEADROOM;
3319 dev->needed_headroom = needed_headroom;
3320
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003321 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003322}
stephen hemmingerd3428942012-10-01 12:32:35 +00003323
Matthias Schiffera9853432017-06-19 10:03:55 +02003324static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003325 struct vxlan_config *conf, bool changelink,
3326 struct netlink_ext_ack *extack)
Matthias Schiffera9853432017-06-19 10:03:55 +02003327{
3328 struct vxlan_dev *vxlan = netdev_priv(dev);
3329 struct net_device *lowerdev;
3330 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003331
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003332 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
Matthias Schiffera9853432017-06-19 10:03:55 +02003333 if (ret)
3334 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003335
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003336 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003337
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003338 return 0;
3339}
3340
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003341static int __vxlan_dev_create(struct net *net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003342 struct vxlan_config *conf,
3343 struct netlink_ext_ack *extack)
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003344{
3345 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3346 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003347 struct vxlan_fdb *f = NULL;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003348 int err;
3349
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003350 err = vxlan_dev_configure(net, dev, conf, false, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003351 if (err)
3352 return err;
3353
3354 dev->ethtool_ops = &vxlan_ethtool_ops;
3355
3356 /* create an fdb entry for a valid default destination */
3357 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003358 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003359 &vxlan->default_dst.remote_ip,
3360 NUD_REACHABLE | NUD_PERMANENT,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003361 vxlan->cfg.dst_port,
3362 vxlan->default_dst.remote_vni,
3363 vxlan->default_dst.remote_vni,
3364 vxlan->default_dst.remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003365 NTF_SELF, &f);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003366 if (err)
3367 return err;
3368 }
3369
3370 err = register_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003371 if (err)
3372 goto errout;
3373
3374 err = rtnl_configure_link(dev, NULL);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003375 if (err) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003376 unregister_netdevice(dev);
3377 goto errout;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003378 }
3379
Roopa Prabhu0241b832018-07-04 16:46:32 -07003380 /* notify default fdb entry */
3381 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003382 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
3383 true);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003384
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003385 list_add(&vxlan->next, &vn->vxlan_list);
3386 return 0;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003387errout:
3388 if (f)
Petr Machata0e6160f2018-11-21 08:02:35 +00003389 vxlan_fdb_destroy(vxlan, f, false, false);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003390 return err;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003391}
3392
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003393static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3394 struct net_device *dev, struct vxlan_config *conf,
3395 bool changelink)
3396{
3397 struct vxlan_dev *vxlan = netdev_priv(dev);
3398
3399 memset(conf, 0, sizeof(*conf));
3400
3401 /* if changelink operation, start with old existing cfg */
3402 if (changelink)
3403 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3404
3405 if (data[IFLA_VXLAN_ID]) {
3406 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3407
3408 if (changelink && (vni != conf->vni))
3409 return -EOPNOTSUPP;
3410 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3411 }
3412
3413 if (data[IFLA_VXLAN_GROUP]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003414 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET))
3415 return -EOPNOTSUPP;
3416
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003417 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003418 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003419 } else if (data[IFLA_VXLAN_GROUP6]) {
3420 if (!IS_ENABLED(CONFIG_IPV6))
3421 return -EPFNOSUPPORT;
3422
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003423 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6))
3424 return -EOPNOTSUPP;
3425
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003426 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3427 conf->remote_ip.sa.sa_family = AF_INET6;
3428 }
3429
3430 if (data[IFLA_VXLAN_LOCAL]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003431 if (changelink && (conf->saddr.sa.sa_family != AF_INET))
3432 return -EOPNOTSUPP;
3433
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003434 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3435 conf->saddr.sa.sa_family = AF_INET;
3436 } else if (data[IFLA_VXLAN_LOCAL6]) {
3437 if (!IS_ENABLED(CONFIG_IPV6))
3438 return -EPFNOSUPPORT;
3439
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003440 if (changelink && (conf->saddr.sa.sa_family != AF_INET6))
3441 return -EOPNOTSUPP;
3442
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003443 /* TODO: respect scope id */
3444 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3445 conf->saddr.sa.sa_family = AF_INET6;
3446 }
3447
3448 if (data[IFLA_VXLAN_LINK])
3449 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3450
3451 if (data[IFLA_VXLAN_TOS])
3452 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3453
3454 if (data[IFLA_VXLAN_TTL])
3455 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3456
Hangbin Liu72f6d712018-04-17 14:11:28 +08003457 if (data[IFLA_VXLAN_TTL_INHERIT]) {
3458 if (changelink)
3459 return -EOPNOTSUPP;
3460 conf->flags |= VXLAN_F_TTL_INHERIT;
3461 }
3462
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003463 if (data[IFLA_VXLAN_LABEL])
3464 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3465 IPV6_FLOWLABEL_MASK;
3466
3467 if (data[IFLA_VXLAN_LEARNING]) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003468 if (nla_get_u8(data[IFLA_VXLAN_LEARNING]))
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003469 conf->flags |= VXLAN_F_LEARN;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003470 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003471 conf->flags &= ~VXLAN_F_LEARN;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003472 } else if (!changelink) {
3473 /* default to learn on a new device */
3474 conf->flags |= VXLAN_F_LEARN;
3475 }
3476
Ido Schimmel40051c42018-11-21 08:02:40 +00003477 if (data[IFLA_VXLAN_AGEING])
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003478 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003479
3480 if (data[IFLA_VXLAN_PROXY]) {
3481 if (changelink)
3482 return -EOPNOTSUPP;
3483 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3484 conf->flags |= VXLAN_F_PROXY;
3485 }
3486
3487 if (data[IFLA_VXLAN_RSC]) {
3488 if (changelink)
3489 return -EOPNOTSUPP;
3490 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3491 conf->flags |= VXLAN_F_RSC;
3492 }
3493
3494 if (data[IFLA_VXLAN_L2MISS]) {
3495 if (changelink)
3496 return -EOPNOTSUPP;
3497 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3498 conf->flags |= VXLAN_F_L2MISS;
3499 }
3500
3501 if (data[IFLA_VXLAN_L3MISS]) {
3502 if (changelink)
3503 return -EOPNOTSUPP;
3504 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3505 conf->flags |= VXLAN_F_L3MISS;
3506 }
3507
3508 if (data[IFLA_VXLAN_LIMIT]) {
3509 if (changelink)
3510 return -EOPNOTSUPP;
3511 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3512 }
3513
3514 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3515 if (changelink)
3516 return -EOPNOTSUPP;
3517 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3518 conf->flags |= VXLAN_F_COLLECT_METADATA;
3519 }
3520
3521 if (data[IFLA_VXLAN_PORT_RANGE]) {
3522 if (!changelink) {
3523 const struct ifla_vxlan_port_range *p
3524 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3525 conf->port_min = ntohs(p->low);
3526 conf->port_max = ntohs(p->high);
3527 } else {
3528 return -EOPNOTSUPP;
3529 }
3530 }
3531
3532 if (data[IFLA_VXLAN_PORT]) {
3533 if (changelink)
3534 return -EOPNOTSUPP;
3535 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3536 }
3537
3538 if (data[IFLA_VXLAN_UDP_CSUM]) {
3539 if (changelink)
3540 return -EOPNOTSUPP;
3541 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3542 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3543 }
3544
3545 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3546 if (changelink)
3547 return -EOPNOTSUPP;
3548 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3549 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3550 }
3551
3552 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3553 if (changelink)
3554 return -EOPNOTSUPP;
3555 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3556 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3557 }
3558
3559 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3560 if (changelink)
3561 return -EOPNOTSUPP;
3562 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3563 conf->flags |= VXLAN_F_REMCSUM_TX;
3564 }
3565
3566 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3567 if (changelink)
3568 return -EOPNOTSUPP;
3569 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3570 conf->flags |= VXLAN_F_REMCSUM_RX;
3571 }
3572
3573 if (data[IFLA_VXLAN_GBP]) {
3574 if (changelink)
3575 return -EOPNOTSUPP;
3576 conf->flags |= VXLAN_F_GBP;
3577 }
3578
3579 if (data[IFLA_VXLAN_GPE]) {
3580 if (changelink)
3581 return -EOPNOTSUPP;
3582 conf->flags |= VXLAN_F_GPE;
3583 }
3584
3585 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3586 if (changelink)
3587 return -EOPNOTSUPP;
3588 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3589 }
3590
3591 if (tb[IFLA_MTU]) {
3592 if (changelink)
3593 return -EOPNOTSUPP;
3594 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3595 }
3596
Stefano Briviob4d306972018-11-08 12:19:16 +01003597 if (data[IFLA_VXLAN_DF])
3598 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
3599
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003600 return 0;
3601}
3602
3603static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003604 struct nlattr *tb[], struct nlattr *data[],
3605 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003606{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003607 struct vxlan_config conf;
3608 int err;
3609
3610 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3611 if (err)
3612 return err;
3613
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003614 return __vxlan_dev_create(src_net, dev, &conf, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00003615}
3616
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003617static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02003618 struct nlattr *data[],
3619 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003620{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003621 struct vxlan_dev *vxlan = netdev_priv(dev);
3622 struct vxlan_rdst *dst = &vxlan->default_dst;
Ido Schimmel40051c42018-11-21 08:02:40 +00003623 unsigned long old_age_interval;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003624 struct vxlan_rdst old_dst;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003625 struct vxlan_config conf;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003626 struct vxlan_fdb *f = NULL;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003627 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003628
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003629 err = vxlan_nl2conf(tb, data,
3630 dev, &conf, true);
3631 if (err)
3632 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07003633
Ido Schimmel40051c42018-11-21 08:02:40 +00003634 old_age_interval = vxlan->cfg.age_interval;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003635 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003636
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003637 err = vxlan_dev_configure(vxlan->net, dev, &conf, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003638 if (err)
3639 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003640
Ido Schimmel40051c42018-11-21 08:02:40 +00003641 if (old_age_interval != vxlan->cfg.age_interval)
3642 mod_timer(&vxlan->age_timer, jiffies);
3643
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003644 /* handle default dst entry */
3645 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3646 spin_lock_bh(&vxlan->hash_lock);
3647 if (!vxlan_addr_any(&old_dst.remote_ip))
3648 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3649 old_dst.remote_ip,
3650 vxlan->cfg.dst_port,
3651 old_dst.remote_vni,
3652 old_dst.remote_vni,
Petr Machata0e6160f2018-11-21 08:02:35 +00003653 old_dst.remote_ifindex,
3654 true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003655
3656 if (!vxlan_addr_any(&dst->remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003657 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003658 &dst->remote_ip,
3659 NUD_REACHABLE | NUD_PERMANENT,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003660 vxlan->cfg.dst_port,
3661 dst->remote_vni,
3662 dst->remote_vni,
3663 dst->remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003664 NTF_SELF, &f);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003665 if (err) {
3666 spin_unlock_bh(&vxlan->hash_lock);
3667 return err;
3668 }
Petr Machata0e6160f2018-11-21 08:02:35 +00003669 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3670 RTM_NEWNEIGH, true);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003671 }
3672 spin_unlock_bh(&vxlan->hash_lock);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003673 }
3674
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003675 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003676}
3677
stephen hemmingerd3428942012-10-01 12:32:35 +00003678static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3679{
3680 struct vxlan_dev *vxlan = netdev_priv(dev);
3681
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003682 vxlan_flush(vxlan, true);
3683
Tom Herbert58ce31c2015-08-19 17:07:33 -07003684 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003685 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003686 unregister_netdevice_queue(dev, head);
3687}
3688
3689static size_t vxlan_get_size(const struct net_device *dev)
3690{
3691
3692 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003693 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003694 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003695 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003696 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
Hangbin Liu8fd78062018-09-26 10:35:42 +08003697 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
stephen hemmingerd3428942012-10-01 12:32:35 +00003698 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Stefano Briviob4d306972018-11-08 12:19:16 +01003699 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003700 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003701 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003702 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3703 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3704 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3705 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003706 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003707 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3708 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003709 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003710 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3711 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3712 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3713 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003714 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3715 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003716 0;
3717}
3718
3719static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3720{
3721 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003722 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003723 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003724 .low = htons(vxlan->cfg.port_min),
3725 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003726 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003727
Jiri Benc54bfd872016-02-16 21:58:58 +01003728 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003729 goto nla_put_failure;
3730
Cong Wange4c7ed42013-08-31 13:44:33 +08003731 if (!vxlan_addr_any(&dst->remote_ip)) {
3732 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003733 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3734 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003735 goto nla_put_failure;
3736#if IS_ENABLED(CONFIG_IPV6)
3737 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003738 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3739 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003740 goto nla_put_failure;
3741#endif
3742 }
3743 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003744
Atzm Watanabec7995c42013-04-16 02:50:52 +00003745 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003746 goto nla_put_failure;
3747
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003748 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3749 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003750 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003751 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003752 goto nla_put_failure;
3753#if IS_ENABLED(CONFIG_IPV6)
3754 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003755 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003756 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003757 goto nla_put_failure;
3758#endif
3759 }
3760 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003761
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003762 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
Hangbin Liu8fd78062018-09-26 10:35:42 +08003763 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
3764 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003765 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Stefano Briviob4d306972018-11-08 12:19:16 +01003766 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003767 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003768 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003769 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003770 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003771 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
3772 nla_put_u8(skb, IFLA_VXLAN_RSC,
3773 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003774 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003775 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003776 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003777 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003778 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003779 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003780 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3781 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3782 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003783 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003784 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003785 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003786 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003787 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003788 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003789 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003790 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003791 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003792 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003793 goto nla_put_failure;
3794
stephen hemminger05f47d62012-10-09 20:35:50 +00003795 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3796 goto nla_put_failure;
3797
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003798 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01003799 nla_put_flag(skb, IFLA_VXLAN_GBP))
3800 goto nla_put_failure;
3801
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003802 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02003803 nla_put_flag(skb, IFLA_VXLAN_GPE))
3804 goto nla_put_failure;
3805
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003806 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003807 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3808 goto nla_put_failure;
3809
stephen hemmingerd3428942012-10-01 12:32:35 +00003810 return 0;
3811
3812nla_put_failure:
3813 return -EMSGSIZE;
3814}
3815
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003816static struct net *vxlan_get_link_net(const struct net_device *dev)
3817{
3818 struct vxlan_dev *vxlan = netdev_priv(dev);
3819
3820 return vxlan->net;
3821}
3822
stephen hemmingerd3428942012-10-01 12:32:35 +00003823static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3824 .kind = "vxlan",
3825 .maxtype = IFLA_VXLAN_MAX,
3826 .policy = vxlan_policy,
3827 .priv_size = sizeof(struct vxlan_dev),
3828 .setup = vxlan_setup,
3829 .validate = vxlan_validate,
3830 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003831 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00003832 .dellink = vxlan_dellink,
3833 .get_size = vxlan_get_size,
3834 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003835 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003836};
3837
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003838struct net_device *vxlan_dev_create(struct net *net, const char *name,
3839 u8 name_assign_type,
3840 struct vxlan_config *conf)
3841{
3842 struct nlattr *tb[IFLA_MAX + 1];
3843 struct net_device *dev;
3844 int err;
3845
3846 memset(&tb, 0, sizeof(tb));
3847
3848 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08003849 &vxlan_link_ops, tb, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003850 if (IS_ERR(dev))
3851 return dev;
3852
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003853 err = __vxlan_dev_create(net, dev, conf, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003854 if (err < 0) {
3855 free_netdev(dev);
3856 return ERR_PTR(err);
3857 }
3858
3859 err = rtnl_configure_link(dev, NULL);
3860 if (err < 0) {
3861 LIST_HEAD(list_kill);
3862
3863 vxlan_dellink(dev, &list_kill);
3864 unregister_netdevice_many(&list_kill);
3865 return ERR_PTR(err);
3866 }
3867
3868 return dev;
3869}
3870EXPORT_SYMBOL_GPL(vxlan_dev_create);
3871
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003872static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3873 struct net_device *dev)
3874{
3875 struct vxlan_dev *vxlan, *next;
3876 LIST_HEAD(list_kill);
3877
3878 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3879 struct vxlan_rdst *dst = &vxlan->default_dst;
3880
3881 /* In case we created vxlan device with carrier
3882 * and we loose the carrier due to module unload
3883 * we also need to remove vxlan device. In other
3884 * cases, it's not necessary and remote_ifindex
3885 * is 0 here, so no matches.
3886 */
3887 if (dst->remote_ifindex == dev->ifindex)
3888 vxlan_dellink(vxlan->dev, &list_kill);
3889 }
3890
3891 unregister_netdevice_many(&list_kill);
3892}
3893
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003894static int vxlan_netdevice_event(struct notifier_block *unused,
3895 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003896{
3897 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003898 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003899
Sabrina Dubroca04584952017-07-21 12:49:33 +02003900 if (event == NETDEV_UNREGISTER) {
3901 vxlan_offload_rx_ports(dev, false);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003902 vxlan_handle_lowerdev_unregister(vn, dev);
Sabrina Dubroca04584952017-07-21 12:49:33 +02003903 } else if (event == NETDEV_REGISTER) {
3904 vxlan_offload_rx_ports(dev, true);
3905 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
3906 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003907 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02003908 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003909
3910 return NOTIFY_DONE;
3911}
3912
3913static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003914 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003915};
3916
Petr Machata0efe1172018-10-17 08:53:26 +00003917static void
3918vxlan_fdb_offloaded_set(struct net_device *dev,
3919 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
3920{
3921 struct vxlan_dev *vxlan = netdev_priv(dev);
3922 struct vxlan_rdst *rdst;
3923 struct vxlan_fdb *f;
3924
3925 spin_lock_bh(&vxlan->hash_lock);
3926
3927 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
3928 if (!f)
3929 goto out;
3930
3931 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
3932 fdb_info->remote_port,
3933 fdb_info->remote_vni,
3934 fdb_info->remote_ifindex);
3935 if (!rdst)
3936 goto out;
3937
3938 rdst->offloaded = fdb_info->offloaded;
3939
3940out:
3941 spin_unlock_bh(&vxlan->hash_lock);
3942}
3943
Petr Machata5728ae02018-11-21 08:02:39 +00003944static int
3945vxlan_fdb_external_learn_add(struct net_device *dev,
3946 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
3947{
3948 struct vxlan_dev *vxlan = netdev_priv(dev);
3949 int err;
3950
3951 spin_lock_bh(&vxlan->hash_lock);
3952 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
3953 NUD_REACHABLE,
3954 NLM_F_CREATE | NLM_F_REPLACE,
3955 fdb_info->remote_port,
3956 fdb_info->vni,
3957 fdb_info->remote_vni,
3958 fdb_info->remote_ifindex,
3959 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
3960 false);
3961 spin_unlock_bh(&vxlan->hash_lock);
3962
3963 return err;
3964}
3965
3966static int
3967vxlan_fdb_external_learn_del(struct net_device *dev,
3968 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
3969{
3970 struct vxlan_dev *vxlan = netdev_priv(dev);
3971 struct vxlan_fdb *f;
3972 int err = 0;
3973
3974 spin_lock_bh(&vxlan->hash_lock);
3975
3976 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
3977 if (!f)
3978 err = -ENOENT;
3979 else if (f->flags & NTF_EXT_LEARNED)
3980 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
3981 fdb_info->remote_ip,
3982 fdb_info->remote_port,
3983 fdb_info->vni,
3984 fdb_info->remote_vni,
3985 fdb_info->remote_ifindex,
3986 false);
3987
3988 spin_unlock_bh(&vxlan->hash_lock);
3989
3990 return err;
3991}
3992
Petr Machata0efe1172018-10-17 08:53:26 +00003993static int vxlan_switchdev_event(struct notifier_block *unused,
3994 unsigned long event, void *ptr)
3995{
3996 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Petr Machata5728ae02018-11-21 08:02:39 +00003997 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
3998 int err = 0;
Petr Machata0efe1172018-10-17 08:53:26 +00003999
4000 switch (event) {
4001 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4002 vxlan_fdb_offloaded_set(dev, ptr);
4003 break;
Petr Machata5728ae02018-11-21 08:02:39 +00004004 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4005 fdb_info = ptr;
4006 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4007 if (err) {
4008 err = notifier_from_errno(err);
4009 break;
4010 }
4011 fdb_info->offloaded = true;
4012 vxlan_fdb_offloaded_set(dev, fdb_info);
4013 break;
4014 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4015 fdb_info = ptr;
4016 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4017 if (err) {
4018 err = notifier_from_errno(err);
4019 break;
4020 }
4021 fdb_info->offloaded = false;
4022 vxlan_fdb_offloaded_set(dev, fdb_info);
4023 break;
Petr Machata0efe1172018-10-17 08:53:26 +00004024 }
4025
Petr Machata5728ae02018-11-21 08:02:39 +00004026 return err;
Petr Machata0efe1172018-10-17 08:53:26 +00004027}
4028
4029static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4030 .notifier_call = vxlan_switchdev_event,
4031};
4032
stephen hemmingerd3428942012-10-01 12:32:35 +00004033static __net_init int vxlan_init_net(struct net *net)
4034{
4035 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00004036 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00004037
stephen hemminger553675f2013-05-16 11:35:20 +00004038 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07004039 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00004040
stephen hemminger553675f2013-05-16 11:35:20 +00004041 for (h = 0; h < PORT_HASH_SIZE; ++h)
4042 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00004043
4044 return 0;
4045}
4046
Haishuang Yan57b61122017-12-16 17:54:49 +08004047static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004048{
4049 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4050 struct vxlan_dev *vxlan, *next;
4051 struct net_device *dev, *aux;
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004052 unsigned int h;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004053
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004054 for_each_netdev_safe(net, dev, aux)
4055 if (dev->rtnl_link_ops == &vxlan_link_ops)
Haishuang Yan57b61122017-12-16 17:54:49 +08004056 unregister_netdevice_queue(dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004057
4058 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4059 /* If vxlan->dev is in the same netns, it has already been added
4060 * to the list by the previous loop.
4061 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07004062 if (!net_eq(dev_net(vxlan->dev), net)) {
4063 gro_cells_destroy(&vxlan->gro_cells);
Haishuang Yan57b61122017-12-16 17:54:49 +08004064 unregister_netdevice_queue(vxlan->dev, head);
Tom Herbert58ce31c2015-08-19 17:07:33 -07004065 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004066 }
4067
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03004068 for (h = 0; h < PORT_HASH_SIZE; ++h)
4069 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02004070}
4071
Haishuang Yan57b61122017-12-16 17:54:49 +08004072static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4073{
4074 struct net *net;
4075 LIST_HEAD(list);
4076
4077 rtnl_lock();
4078 list_for_each_entry(net, net_list, exit_list)
4079 vxlan_destroy_tunnels(net, &list);
4080
4081 unregister_netdevice_many(&list);
4082 rtnl_unlock();
4083}
4084
stephen hemmingerd3428942012-10-01 12:32:35 +00004085static struct pernet_operations vxlan_net_ops = {
4086 .init = vxlan_init_net,
Haishuang Yan57b61122017-12-16 17:54:49 +08004087 .exit_batch = vxlan_exit_batch_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00004088 .id = &vxlan_net_id,
4089 .size = sizeof(struct vxlan_net),
4090};
4091
4092static int __init vxlan_init_module(void)
4093{
4094 int rc;
4095
4096 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4097
Daniel Borkmann783c1462014-01-22 21:07:53 +01004098 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004099 if (rc)
4100 goto out1;
4101
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004102 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004103 if (rc)
4104 goto out2;
4105
Petr Machata0efe1172018-10-17 08:53:26 +00004106 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004107 if (rc)
4108 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00004109
Petr Machata0efe1172018-10-17 08:53:26 +00004110 rc = rtnl_link_register(&vxlan_link_ops);
4111 if (rc)
4112 goto out4;
4113
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004114 return 0;
Petr Machata0efe1172018-10-17 08:53:26 +00004115out4:
4116 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004117out3:
4118 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00004119out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01004120 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00004121out1:
4122 return rc;
4123}
Cong Wang7332a132013-05-27 22:35:53 +00004124late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00004125
4126static void __exit vxlan_cleanup_module(void)
4127{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07004128 rtnl_link_unregister(&vxlan_link_ops);
Petr Machata0efe1172018-10-17 08:53:26 +00004129 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01004130 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01004131 unregister_pernet_subsys(&vxlan_net_ops);
4132 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00004133}
4134module_exit(vxlan_cleanup_module);
4135
4136MODULE_LICENSE("GPL");
4137MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004138MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08004139MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00004140MODULE_ALIAS_RTNL_LINK("vxlan");