blob: 0fc2b1d82d4c185abfa8989201c98351d1e3e09c [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;
David Stevensae884082013-04-19 00:36:26 +000082 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +000083};
84
stephen hemmingerd3428942012-10-01 12:32:35 +000085/* salt for hash table */
86static u32 vxlan_salt __read_mostly;
87
Thomas Grafee122c72015-07-21 10:43:58 +020088static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
89{
Thomas Grafe7030872015-07-21 10:44:01 +020090 return vs->flags & VXLAN_F_COLLECT_METADATA ||
91 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020092}
93
Cong Wange4c7ed42013-08-31 13:44:33 +080094#if IS_ENABLED(CONFIG_IPV6)
95static inline
96bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
97{
Jiri Bencf0ef3122015-03-29 16:17:37 +020098 if (a->sa.sa_family != b->sa.sa_family)
99 return false;
100 if (a->sa.sa_family == AF_INET6)
101 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
102 else
103 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800104}
105
Cong Wange4c7ed42013-08-31 13:44:33 +0800106static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
107{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200108 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200109 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200110 ip->sa.sa_family = AF_INET6;
111 return 0;
112 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200113 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200114 ip->sa.sa_family = AF_INET;
115 return 0;
116 } else {
117 return -EAFNOSUPPORT;
118 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800119}
120
121static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200122 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800123{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200124 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200125 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200126 else
Jiri Benc930345e2015-03-29 16:59:25 +0200127 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800128}
129
130#else /* !CONFIG_IPV6 */
131
132static inline
133bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
134{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200135 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800136}
137
Cong Wange4c7ed42013-08-31 13:44:33 +0800138static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
139{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200140 if (nla_len(nla) >= sizeof(struct in6_addr)) {
141 return -EAFNOSUPPORT;
142 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200143 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200144 ip->sa.sa_family = AF_INET;
145 return 0;
146 } else {
147 return -EAFNOSUPPORT;
148 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800149}
150
151static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200152 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800153{
Jiri Benc930345e2015-03-29 16:59:25 +0200154 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800155}
156#endif
157
stephen hemminger553675f2013-05-16 11:35:20 +0000158/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100159static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000160{
Jiri Benc54bfd872016-02-16 21:58:58 +0100161 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000162}
163
164/* Socket hash table head */
165static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000166{
167 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
168
stephen hemminger553675f2013-05-16 11:35:20 +0000169 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
170}
171
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700172/* First remote destination for a forwarding entry.
173 * Guaranteed to be non-NULL because remotes are never deleted.
174 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700175static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700176{
stephen hemminger5ca54612013-08-04 17:17:39 -0700177 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
178}
179
180static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
181{
182 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700183}
184
Thomas Grafac5132d2015-01-15 03:53:56 +0100185/* Find VXLAN socket based on network namespace, address family and UDP port
186 * and enabled unshareable flags.
187 */
188static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
189 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000190{
191 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800192
193 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000194
195 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200196 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200197 vxlan_get_sk_family(vs) == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800198 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000199 return vs;
200 }
201 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000202}
203
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200204static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
205 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000206{
Jiri Benc69e76662017-07-02 19:00:57 +0200207 struct vxlan_dev_node *node;
stephen hemmingerd3428942012-10-01 12:32:35 +0000208
Jiri Bencb9167b22016-02-16 21:59:03 +0100209 /* For flow based devices, map all packets to VNI 0 */
210 if (vs->flags & VXLAN_F_COLLECT_METADATA)
211 vni = 0;
212
Jiri Benc69e76662017-07-02 19:00:57 +0200213 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
214 if (node->vxlan->default_dst.remote_vni != vni)
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200215 continue;
216
217 if (IS_ENABLED(CONFIG_IPV6)) {
Jiri Benc69e76662017-07-02 19:00:57 +0200218 const struct vxlan_config *cfg = &node->vxlan->cfg;
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200219
220 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
221 cfg->remote_ifindex != ifindex)
222 continue;
223 }
224
Jiri Benc69e76662017-07-02 19:00:57 +0200225 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000226 }
227
228 return NULL;
229}
230
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700231/* Look up VNI in a per net namespace table */
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200232static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
233 __be32 vni, sa_family_t family,
234 __be16 port, u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700235{
236 struct vxlan_sock *vs;
237
Thomas Grafac5132d2015-01-15 03:53:56 +0100238 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700239 if (!vs)
240 return NULL;
241
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200242 return vxlan_vs_find_vni(vs, ifindex, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700243}
244
stephen hemmingerd3428942012-10-01 12:32:35 +0000245/* Fill in neighbour message in skbuff. */
246static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700247 const struct vxlan_fdb *fdb,
248 u32 portid, u32 seq, int type, unsigned int flags,
249 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000250{
251 unsigned long now = jiffies;
252 struct nda_cacheinfo ci;
253 struct nlmsghdr *nlh;
254 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000255 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000256
257 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
258 if (nlh == NULL)
259 return -EMSGSIZE;
260
261 ndm = nlmsg_data(nlh);
262 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000263
264 send_eth = send_ip = true;
265
266 if (type == RTM_GETNEIGH) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800267 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000268 send_eth = !is_zero_ether_addr(fdb->eth_addr);
Vincent Bernat8f48ba72017-03-10 16:30:24 +0100269 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
David Stevense4f67ad2012-11-20 02:50:14 +0000270 } else
271 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000272 ndm->ndm_state = fdb->state;
273 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000274 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800275 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000276
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100277 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100278 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700279 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100280 goto nla_put_failure;
281
David Stevense4f67ad2012-11-20 02:50:14 +0000282 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000283 goto nla_put_failure;
284
Cong Wange4c7ed42013-08-31 13:44:33 +0800285 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000286 goto nla_put_failure;
287
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200288 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000289 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
290 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000291 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100292 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000293 goto nla_put_failure;
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200294 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800295 nla_put_u32(skb, NDA_SRC_VNI,
296 be32_to_cpu(fdb->vni)))
297 goto nla_put_failure;
David Stevens66817122013-03-15 04:35:51 +0000298 if (rdst->remote_ifindex &&
299 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000300 goto nla_put_failure;
301
302 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
303 ci.ndm_confirmed = 0;
304 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
305 ci.ndm_refcnt = 0;
306
307 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
308 goto nla_put_failure;
309
Johannes Berg053c0952015-01-16 22:09:00 +0100310 nlmsg_end(skb, nlh);
311 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000312
313nla_put_failure:
314 nlmsg_cancel(skb, nlh);
315 return -EMSGSIZE;
316}
317
318static inline size_t vxlan_nlmsg_size(void)
319{
320 return NLMSG_ALIGN(sizeof(struct ndmsg))
321 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800322 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000323 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000324 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
325 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100326 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000327 + nla_total_size(sizeof(struct nda_cacheinfo));
328}
329
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200330static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
331 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000332{
333 struct net *net = dev_net(vxlan->dev);
334 struct sk_buff *skb;
335 int err = -ENOBUFS;
336
337 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
338 if (skb == NULL)
339 goto errout;
340
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200341 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000342 if (err < 0) {
343 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
344 WARN_ON(err == -EMSGSIZE);
345 kfree_skb(skb);
346 goto errout;
347 }
348
349 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
350 return;
351errout:
352 if (err < 0)
353 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
354}
355
Cong Wange4c7ed42013-08-31 13:44:33 +0800356static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000357{
358 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700359 struct vxlan_fdb f = {
360 .state = NUD_STALE,
361 };
362 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800363 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100364 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700365 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700366
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200367 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000368}
369
370static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
371{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700372 struct vxlan_fdb f = {
373 .state = NUD_STALE,
374 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200375 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000376
David Stevense4f67ad2012-11-20 02:50:14 +0000377 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
378
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200379 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000380}
381
stephen hemmingerd3428942012-10-01 12:32:35 +0000382/* Hash Ethernet address */
383static u32 eth_hash(const unsigned char *addr)
384{
385 u64 value = get_unaligned((u64 *)addr);
386
387 /* only want 6 bytes */
388#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000389 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000390#else
391 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000392#endif
393 return hash_64(value, FDB_HASH_BITS);
394}
395
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800396static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
397{
398 /* use 1 byte of OUI and 3 bytes of NIC */
399 u32 key = get_unaligned((u32 *)(addr + 2));
400
401 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
402}
403
stephen hemmingerd3428942012-10-01 12:32:35 +0000404/* Hash chain to use given mac address */
405static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800406 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000407{
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200408 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800409 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
410 else
411 return &vxlan->fdb_head[eth_hash(mac)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000412}
413
414/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000415static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800416 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000417{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800418 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000419 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000420
Sasha Levinb67bfe02013-02-27 17:06:00 -0800421 hlist_for_each_entry_rcu(f, head, hlist) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800422 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200423 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800424 if (vni == f->vni)
425 return f;
426 } else {
427 return f;
428 }
429 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000430 }
431
432 return NULL;
433}
434
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000435static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800436 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000437{
438 struct vxlan_fdb *f;
439
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800440 f = __vxlan_find_mac(vxlan, mac, vni);
Li RongQing016f3d12018-08-29 11:52:10 +0800441 if (f && f->used != jiffies)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000442 f->used = jiffies;
443
444 return f;
445}
446
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300447/* caller should hold vxlan->hash_lock */
448static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800449 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100450 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300451{
452 struct vxlan_rdst *rd;
453
454 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800455 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300456 rd->remote_port == port &&
457 rd->remote_vni == vni &&
458 rd->remote_ifindex == ifindex)
459 return rd;
460 }
461
462 return NULL;
463}
464
Thomas Richter906dc182013-07-19 17:20:07 +0200465/* Replace destination of unicast mac */
466static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100467 union vxlan_addr *ip, __be16 port, __be32 vni,
468 __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200469{
470 struct vxlan_rdst *rd;
471
472 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
473 if (rd)
474 return 0;
475
476 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
477 if (!rd)
478 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100479
480 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800481 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200482 rd->remote_port = port;
483 rd->remote_vni = vni;
484 rd->remote_ifindex = ifindex;
485 return 1;
486}
487
David Stevens66817122013-03-15 04:35:51 +0000488/* Add/update destinations for multicast */
489static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100490 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200491 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000492{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700493 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000494
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300495 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
496 if (rd)
497 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700498
David Stevens66817122013-03-15 04:35:51 +0000499 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
500 if (rd == NULL)
501 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100502
503 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
504 kfree(rd);
505 return -ENOBUFS;
506 }
507
Cong Wange4c7ed42013-08-31 13:44:33 +0800508 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000509 rd->remote_port = port;
510 rd->remote_vni = vni;
511 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700512
513 list_add_tail_rcu(&rd->list, &f->remotes);
514
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200515 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000516 return 1;
517}
518
Tom Herbertdfd86452015-01-12 17:00:38 -0800519static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
520 unsigned int off,
521 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100522 __be32 vni_field,
523 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800524 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800525{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700526 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800527
528 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700529 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800530
531 if (!NAPI_GRO_CB(skb)->csum_valid)
532 return NULL;
533
Jiri Benc54bfd872016-02-16 21:58:58 +0100534 start = vxlan_rco_start(vni_field);
535 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800536
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700537 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
538 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800539
540 skb->remcsum_offload = 1;
541
542 return vh;
543}
544
David Millerd4546c22018-06-24 14:13:49 +0900545static struct sk_buff *vxlan_gro_receive(struct sock *sk,
546 struct list_head *head,
547 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200548{
David Millerd4546c22018-06-24 14:13:49 +0900549 struct sk_buff *pp = NULL;
550 struct sk_buff *p;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200551 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800552 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200553 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700554 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100555 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800556 struct gro_remcsum grc;
557
558 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200559
560 off_vx = skb_gro_offset(skb);
561 hlen = off_vx + sizeof(*vh);
562 vh = skb_gro_header_fast(skb, off_vx);
563 if (skb_gro_header_hard(skb, hlen)) {
564 vh = skb_gro_header_slow(skb, hlen, off_vx);
565 if (unlikely(!vh))
566 goto out;
567 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200568
Tom Herbertdfd86452015-01-12 17:00:38 -0800569 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
570
Jiri Benc54bfd872016-02-16 21:58:58 +0100571 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800572
573 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
574 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100575 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800576 !!(vs->flags &
577 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800578
579 if (!vh)
580 goto out;
581 }
582
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700583 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
584
David Millerd4546c22018-06-24 14:13:49 +0900585 list_for_each_entry(p, head, list) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200586 if (!NAPI_GRO_CB(p)->same_flow)
587 continue;
588
589 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100590 if (vh->vx_flags != vh2->vx_flags ||
591 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200592 NAPI_GRO_CB(p)->same_flow = 0;
593 continue;
594 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200595 }
596
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200597 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800598 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200599
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200600out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200601 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200602
603 return pp;
604}
605
Tom Herbert5602c482016-04-05 08:22:53 -0700606static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200607{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700608 /* Sets 'skb->inner_mac_header' since we are always called with
609 * 'skb->encapsulation' set.
610 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800611 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200612}
613
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700614static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan,
615 const u8 *mac, __u16 state,
616 __be32 src_vni, __u8 ndm_flags)
617{
618 struct vxlan_fdb *f;
619
620 f = kmalloc(sizeof(*f), GFP_ATOMIC);
621 if (!f)
622 return NULL;
623 f->state = state;
624 f->flags = ndm_flags;
625 f->updated = f->used = jiffies;
626 f->vni = src_vni;
627 INIT_LIST_HEAD(&f->remotes);
628 memcpy(f->eth_addr, mac, ETH_ALEN);
629
630 return f;
631}
632
stephen hemmingerd3428942012-10-01 12:32:35 +0000633static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800634 const u8 *mac, union vxlan_addr *ip,
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700635 __u16 state, __be16 port, __be32 src_vni,
636 __be32 vni, __u32 ifindex, __u8 ndm_flags,
637 struct vxlan_fdb **fdb)
638{
639 struct vxlan_rdst *rd = NULL;
640 struct vxlan_fdb *f;
641 int rc;
642
643 if (vxlan->cfg.addrmax &&
644 vxlan->addrcnt >= vxlan->cfg.addrmax)
645 return -ENOSPC;
646
647 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
648 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
649 if (!f)
650 return -ENOMEM;
651
652 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
653 if (rc < 0) {
654 kfree(f);
655 return rc;
656 }
657
658 ++vxlan->addrcnt;
659 hlist_add_head_rcu(&f->hlist,
660 vxlan_fdb_head(vxlan, mac, src_vni));
661
662 *fdb = f;
663
664 return 0;
665}
666
667/* Add new entry to forwarding table -- assumes lock held */
668static int vxlan_fdb_update(struct vxlan_dev *vxlan,
669 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000670 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800671 __be16 port, __be32 src_vni, __be32 vni,
672 __u32 ifindex, __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000673{
Roopa Prabhu0813e952018-10-11 12:35:13 -0700674 __u8 fdb_flags = (ndm_flags & ~NTF_USE);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200675 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000676 struct vxlan_fdb *f;
677 int notify = 0;
Haishuang Yan17b46362016-11-29 09:59:36 +0800678 int rc;
stephen hemmingerd3428942012-10-01 12:32:35 +0000679
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800680 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000681 if (f) {
682 if (flags & NLM_F_EXCL) {
683 netdev_dbg(vxlan->dev,
684 "lost race to create %pM\n", mac);
685 return -EEXIST;
686 }
687 if (f->state != state) {
688 f->state = state;
689 f->updated = jiffies;
690 notify = 1;
691 }
Roopa Prabhu0813e952018-10-11 12:35:13 -0700692 if (f->flags != fdb_flags) {
693 f->flags = fdb_flags;
David Stevensae884082013-04-19 00:36:26 +0000694 f->updated = jiffies;
695 notify = 1;
696 }
Thomas Richter906dc182013-07-19 17:20:07 +0200697 if ((flags & NLM_F_REPLACE)) {
698 /* Only change unicasts */
699 if (!(is_multicast_ether_addr(f->eth_addr) ||
700 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800701 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200702 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200703 } else
704 return -EOPNOTSUPP;
705 }
David Stevens66817122013-03-15 04:35:51 +0000706 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300707 (is_multicast_ether_addr(f->eth_addr) ||
708 is_zero_ether_addr(f->eth_addr))) {
Haishuang Yan17b46362016-11-29 09:59:36 +0800709 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
David Stevens66817122013-03-15 04:35:51 +0000710
711 if (rc < 0)
712 return rc;
713 notify |= rc;
714 }
Roopa Prabhu0813e952018-10-11 12:35:13 -0700715
716 if (ndm_flags & NTF_USE)
717 f->used = jiffies;
stephen hemmingerd3428942012-10-01 12:32:35 +0000718 } else {
719 if (!(flags & NLM_F_CREATE))
720 return -ENOENT;
721
Thomas Richter906dc182013-07-19 17:20:07 +0200722 /* Disallow replace to add a multicast entry */
723 if ((flags & NLM_F_REPLACE) &&
724 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
725 return -EOPNOTSUPP;
726
Cong Wange4c7ed42013-08-31 13:44:33 +0800727 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700728 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
Roopa Prabhu0813e952018-10-11 12:35:13 -0700729 vni, ifindex, fdb_flags, &f);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700730 if (rc < 0)
Haishuang Yan17b46362016-11-29 09:59:36 +0800731 return rc;
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700732 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000733 }
734
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200735 if (notify) {
736 if (rd == NULL)
737 rd = first_remote_rtnl(f);
738 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
739 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000740
741 return 0;
742}
743
Wei Yongjun6706c822013-04-11 19:00:35 +0000744static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000745{
746 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700747 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000748
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100749 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
750 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000751 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100752 }
David Stevens66817122013-03-15 04:35:51 +0000753 kfree(f);
754}
755
Roopa Prabhu4c2438b2018-07-04 16:46:31 -0700756static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
757 bool do_notify)
stephen hemmingerd3428942012-10-01 12:32:35 +0000758{
759 netdev_dbg(vxlan->dev,
760 "delete %pM\n", f->eth_addr);
761
762 --vxlan->addrcnt;
Roopa Prabhu4c2438b2018-07-04 16:46:31 -0700763 if (do_notify)
764 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000765
766 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000767 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000768}
769
Lance Richardson35cf2842017-05-29 13:25:57 -0400770static void vxlan_dst_free(struct rcu_head *head)
771{
772 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
773
774 dst_cache_destroy(&rd->dst_cache);
775 kfree(rd);
776}
777
778static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
779 struct vxlan_rdst *rd)
780{
781 list_del_rcu(&rd->list);
782 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
783 call_rcu(&rd->rcu, vxlan_dst_free);
784}
785
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300786static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800787 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
788 __be32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300789{
790 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800791 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300792
793 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800794 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
795 if (err)
796 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300797 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800798 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
799 if (remote->sa.sa_family == AF_INET) {
800 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
801 ip->sa.sa_family = AF_INET;
802#if IS_ENABLED(CONFIG_IPV6)
803 } else {
804 ip->sin6.sin6_addr = in6addr_any;
805 ip->sa.sa_family = AF_INET6;
806#endif
807 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300808 }
809
810 if (tb[NDA_PORT]) {
811 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
812 return -EINVAL;
813 *port = nla_get_be16(tb[NDA_PORT]);
814 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200815 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300816 }
817
818 if (tb[NDA_VNI]) {
819 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
820 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100821 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300822 } else {
823 *vni = vxlan->default_dst.remote_vni;
824 }
825
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800826 if (tb[NDA_SRC_VNI]) {
827 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
828 return -EINVAL;
829 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
830 } else {
831 *src_vni = vxlan->default_dst.remote_vni;
832 }
833
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300834 if (tb[NDA_IFINDEX]) {
835 struct net_device *tdev;
836
837 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
838 return -EINVAL;
839 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800840 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300841 if (!tdev)
842 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300843 } else {
844 *ifindex = 0;
845 }
846
847 return 0;
848}
849
stephen hemmingerd3428942012-10-01 12:32:35 +0000850/* Add static entry (via netlink) */
851static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
852 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100853 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000854{
855 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300856 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800857 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000858 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800859 __be32 src_vni, vni;
Jiri Benc54bfd872016-02-16 21:58:58 +0100860 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000861 int err;
862
863 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
864 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
865 ndm->ndm_state);
866 return -EINVAL;
867 }
868
869 if (tb[NDA_DST] == NULL)
870 return -EINVAL;
871
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800872 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300873 if (err)
874 return err;
David Stevens66817122013-03-15 04:35:51 +0000875
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300876 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
877 return -EAFNOSUPPORT;
878
stephen hemmingerd3428942012-10-01 12:32:35 +0000879 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu25e20e72018-07-04 16:46:30 -0700880 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800881 port, src_vni, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000882 spin_unlock_bh(&vxlan->hash_lock);
883
884 return err;
885}
886
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800887static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
888 const unsigned char *addr, union vxlan_addr ip,
Xin Longfc39c382017-11-26 21:19:05 +0800889 __be16 port, __be32 src_vni, __be32 vni,
890 u32 ifindex, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000891{
stephen hemmingerd3428942012-10-01 12:32:35 +0000892 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300893 struct vxlan_rdst *rd = NULL;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800894 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300895
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800896 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300897 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800898 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300899
Cong Wange4c7ed42013-08-31 13:44:33 +0800900 if (!vxlan_addr_any(&ip)) {
901 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300902 if (!rd)
903 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000904 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300905
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300906 /* remove a destination if it's not the only one on the list,
907 * otherwise destroy the fdb entry
908 */
909 if (rd && !list_is_singular(&f->remotes)) {
Lance Richardson35cf2842017-05-29 13:25:57 -0400910 vxlan_fdb_dst_destroy(vxlan, f, rd);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300911 goto out;
912 }
913
Roopa Prabhu4c2438b2018-07-04 16:46:31 -0700914 vxlan_fdb_destroy(vxlan, f, true);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300915
916out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800917 return 0;
918}
919
920/* Delete entry (via netlink) */
921static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
922 struct net_device *dev,
923 const unsigned char *addr, u16 vid)
924{
925 struct vxlan_dev *vxlan = netdev_priv(dev);
926 union vxlan_addr ip;
927 __be32 src_vni, vni;
928 __be16 port;
929 u32 ifindex;
930 int err;
931
932 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
933 if (err)
934 return err;
935
936 spin_lock_bh(&vxlan->hash_lock);
937 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
938 vid);
stephen hemmingerd3428942012-10-01 12:32:35 +0000939 spin_unlock_bh(&vxlan->hash_lock);
940
941 return err;
942}
943
944/* Dump forwarding table */
945static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400946 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700947 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000948{
949 struct vxlan_dev *vxlan = netdev_priv(dev);
950 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -0700951 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000952
953 for (h = 0; h < FDB_HASH_SIZE; ++h) {
954 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000955
Sasha Levinb67bfe02013-02-27 17:06:00 -0800956 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000957 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000958
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700959 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -0700960 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900961 goto skip;
962
David Stevens66817122013-03-15 04:35:51 +0000963 err = vxlan_fdb_info(skb, vxlan, f,
964 NETLINK_CB(cb->skb).portid,
965 cb->nlh->nlmsg_seq,
966 RTM_NEWNEIGH,
967 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -0700968 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700969 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700970skip:
Roopa Prabhud2976532016-08-30 21:56:45 -0700971 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900972 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000973 }
974 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700975out:
Roopa Prabhud2976532016-08-30 21:56:45 -0700976 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +0000977}
978
979/* Watch incoming packets to learn mapping between Ethernet address
980 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900981 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000982 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700983static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800984 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +0200985 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000986{
987 struct vxlan_dev *vxlan = netdev_priv(dev);
988 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +0200989 u32 ifindex = 0;
990
991#if IS_ENABLED(CONFIG_IPV6)
992 if (src_ip->sa.sa_family == AF_INET6 &&
993 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
994 ifindex = src_ifindex;
995#endif
stephen hemmingerd3428942012-10-01 12:32:35 +0000996
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800997 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000998 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700999 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001000
Matthias Schiffer87613de2017-06-19 10:03:59 +02001001 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1002 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001003 return false;
1004
1005 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -07001006 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001007 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001008
1009 if (net_ratelimit())
1010 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001011 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001012 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001013
Cong Wange4c7ed42013-08-31 13:44:33 +08001014 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001015 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001016 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001017 } else {
1018 /* learned new entry */
1019 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001020
1021 /* close off race between vxlan_flush and incoming packets */
1022 if (netif_running(dev))
Roopa Prabhu25e20e72018-07-04 16:46:30 -07001023 vxlan_fdb_update(vxlan, src_mac, src_ip,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001024 NUD_REACHABLE,
1025 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001026 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001027 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001028 vxlan->default_dst.remote_vni,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001029 ifindex, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001030 spin_unlock(&vxlan->hash_lock);
1031 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001032
1033 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001034}
1035
stephen hemmingerd3428942012-10-01 12:32:35 +00001036/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001037static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001038{
stephen hemminger553675f2013-05-16 11:35:20 +00001039 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001040 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001041#if IS_ENABLED(CONFIG_IPV6)
1042 struct vxlan_sock *sock6;
1043#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001044 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001045
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001046 sock4 = rtnl_dereference(dev->vn4_sock);
1047
Gao feng95ab0992013-12-10 16:37:33 +08001048 /* The vxlan_sock is only used by dev, leaving group has
1049 * no effect on other vxlan devices.
1050 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001051 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001052 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001053#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001054 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001055 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001056 return false;
1057#endif
Gao feng95ab0992013-12-10 16:37:33 +08001058
stephen hemminger553675f2013-05-16 11:35:20 +00001059 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001060 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001061 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001062
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001063 if (family == AF_INET &&
1064 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001065 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001066#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001067 if (family == AF_INET6 &&
1068 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001069 continue;
1070#endif
Gao feng95ab0992013-12-10 16:37:33 +08001071
1072 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1073 &dev->default_dst.remote_ip))
1074 continue;
1075
1076 if (vxlan->default_dst.remote_ifindex !=
1077 dev->default_dst.remote_ifindex)
1078 continue;
1079
1080 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001081 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001082
1083 return false;
1084}
1085
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001086static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001087{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001088 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001089
Jiri Bencb1be00a2015-09-24 13:50:02 +02001090 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001091 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001092 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001093 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001094
Jiri Bencb1be00a2015-09-24 13:50:02 +02001095 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001096 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001097 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001098 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001099 (vs->flags & VXLAN_F_GPE) ?
1100 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001101 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001102 spin_unlock(&vn->sock_lock);
1103
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001104 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001105}
1106
Jiri Bencb1be00a2015-09-24 13:50:02 +02001107static void vxlan_sock_release(struct vxlan_dev *vxlan)
1108{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001109 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001110#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001111 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1112
Mark Bloch57d88182017-06-07 14:36:58 +03001113 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001114#endif
1115
Mark Bloch57d88182017-06-07 14:36:58 +03001116 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001117 synchronize_net();
1118
Mark Blocha53cb292017-06-02 03:24:08 +03001119 vxlan_vs_del_dev(vxlan);
1120
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001121 if (__vxlan_sock_release_prep(sock4)) {
1122 udp_tunnel_sock_release(sock4->sock);
1123 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001124 }
1125
1126#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001127 if (__vxlan_sock_release_prep(sock6)) {
1128 udp_tunnel_sock_release(sock6->sock);
1129 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001130 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001131#endif
1132}
1133
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001134/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001135 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001136 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001137static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001138{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001139 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001140 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1141 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001142 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001143
Cong Wange4c7ed42013-08-31 13:44:33 +08001144 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001145 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001146 struct ip_mreqn mreq = {
1147 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1148 .imr_ifindex = ifindex,
1149 };
1150
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001151 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001152 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001153 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001154 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001155#if IS_ENABLED(CONFIG_IPV6)
1156 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001157 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1158
1159 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001160 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001161 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1162 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001163 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001164#endif
1165 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001166
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001167 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001168}
1169
1170/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001171static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001172{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001173 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001174 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1175 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001176 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001177
Cong Wange4c7ed42013-08-31 13:44:33 +08001178 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001179 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001180 struct ip_mreqn mreq = {
1181 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1182 .imr_ifindex = ifindex,
1183 };
1184
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001185 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001186 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001187 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001188 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001189#if IS_ENABLED(CONFIG_IPV6)
1190 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001191 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1192
1193 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001194 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001195 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1196 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001197 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001198#endif
1199 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001200
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001201 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001202}
1203
Jiri Bencf14eceb2016-02-16 21:59:01 +01001204static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1205 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001206{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001207 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001208
Jiri Bencf14eceb2016-02-16 21:59:01 +01001209 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1210 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001211
Jiri Bencf14eceb2016-02-16 21:59:01 +01001212 start = vxlan_rco_start(unparsed->vx_vni);
1213 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001214
Jiri Benc7d34fa72016-03-21 17:50:05 +01001215 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001216 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001217
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001218 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1219 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001220out:
1221 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1222 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001223 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001224}
1225
Jiri Bencf14eceb2016-02-16 21:59:01 +01001226static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001227 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001228 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001229{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001230 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001231 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001232
Jiri Bencf14eceb2016-02-16 21:59:01 +01001233 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1234 goto out;
1235
Jiri Benc3288af02016-02-16 21:59:00 +01001236 md->gbp = ntohs(gbp->policy_id);
1237
Jiri Benc10a5af22016-02-23 18:02:59 +01001238 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001239 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001240 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001241 tun_dst->u.tun_info.options_len = sizeof(*md);
1242 }
Jiri Benc3288af02016-02-16 21:59:00 +01001243 if (gbp->dont_learn)
1244 md->gbp |= VXLAN_GBP_DONT_LEARN;
1245
1246 if (gbp->policy_applied)
1247 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001248
Jiri Benc64f87d32016-02-23 18:02:55 +01001249 /* In flow-based mode, GBP is carried in dst_metadata */
1250 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1251 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001252out:
1253 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001254}
1255
Jiri Bence1e53142016-04-05 14:47:13 +02001256static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001257 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001258 struct sk_buff *skb, u32 vxflags)
1259{
1260 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1261
1262 /* Need to have Next Protocol set for interfaces in GPE mode. */
1263 if (!gpe->np_applied)
1264 return false;
1265 /* "The initial version is 0. If a receiver does not support the
1266 * version indicated it MUST drop the packet.
1267 */
1268 if (gpe->version != 0)
1269 return false;
1270 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1271 * processing MUST occur." However, we don't implement OAM
1272 * processing, thus drop the packet.
1273 */
1274 if (gpe->oam_flag)
1275 return false;
1276
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001277 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1278 if (!*protocol)
Jiri Bence1e53142016-04-05 14:47:13 +02001279 return false;
Jiri Bence1e53142016-04-05 14:47:13 +02001280
1281 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1282 return true;
1283}
1284
Jiri Benc1ab016e2016-02-23 18:02:56 +01001285static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1286 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001287 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001288{
Thomas Graf614732e2015-07-21 10:44:06 +02001289 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001290 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001291
Thomas Graf614732e2015-07-21 10:44:06 +02001292 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001293 skb->protocol = eth_type_trans(skb, vxlan->dev);
1294 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1295
1296 /* Ignore packet loops (and multicast echo) */
1297 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001298 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001299
Jiri Benc760c6802016-02-23 18:02:57 +01001300 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001301 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001302 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001303 saddr.sa.sa_family = AF_INET;
1304#if IS_ENABLED(CONFIG_IPV6)
1305 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001306 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001307 saddr.sa.sa_family = AF_INET6;
1308#endif
1309 }
1310
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001311 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001312 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001313 return false;
1314
1315 return true;
1316}
1317
Jiri Benc760c6802016-02-23 18:02:57 +01001318static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1319 struct sk_buff *skb)
1320{
1321 int err = 0;
1322
1323 if (vxlan_get_sk_family(vs) == AF_INET)
1324 err = IP_ECN_decapsulate(oiph, skb);
1325#if IS_ENABLED(CONFIG_IPV6)
1326 else
1327 err = IP6_ECN_decapsulate(oiph, skb);
1328#endif
1329
1330 if (unlikely(err) && log_ecn_error) {
1331 if (vxlan_get_sk_family(vs) == AF_INET)
1332 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1333 &((struct iphdr *)oiph)->saddr,
1334 ((struct iphdr *)oiph)->tos);
1335 else
1336 net_info_ratelimited("non-ECT from %pI6\n",
1337 &((struct ipv6hdr *)oiph)->saddr);
1338 }
1339 return err <= 1;
1340}
1341
stephen hemmingerd3428942012-10-01 12:32:35 +00001342/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001343static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001344{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001345 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001346 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001347 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001348 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001349 struct vxlan_metadata _md;
1350 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001351 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001352 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001353 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001354 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001355
Jiri Bence1e53142016-04-05 14:47:13 +02001356 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001357 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001358 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001359
Jiri Bencf14eceb2016-02-16 21:59:01 +01001360 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001361 /* VNI flag always required to be set */
1362 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1363 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1364 ntohl(vxlan_hdr(skb)->vx_flags),
1365 ntohl(vxlan_hdr(skb)->vx_vni));
1366 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001367 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001368 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001369 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1370 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001371
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001372 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001373 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001374 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001375
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001376 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1377
Matthias Schiffer49f810f2017-06-19 10:04:00 +02001378 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001379 if (!vxlan)
1380 goto drop;
1381
Jiri Bence1e53142016-04-05 14:47:13 +02001382 /* For backwards compatibility, only allow reserved fields to be
1383 * used by VXLAN extensions if explicitly requested.
1384 */
1385 if (vs->flags & VXLAN_F_GPE) {
1386 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1387 goto drop;
1388 raw_proto = true;
1389 }
1390
1391 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1392 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1393 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001394
Thomas Grafee122c72015-07-21 10:43:58 +02001395 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001396 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001397
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001398 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001399 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001400
Thomas Grafee122c72015-07-21 10:43:58 +02001401 if (!tun_dst)
1402 goto drop;
1403
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001404 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001405
1406 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001407 } else {
1408 memset(md, 0, sizeof(*md));
1409 }
1410
Jiri Bencf14eceb2016-02-16 21:59:01 +01001411 if (vs->flags & VXLAN_F_REMCSUM_RX)
1412 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1413 goto drop;
1414 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001415 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001416 /* Note that GBP and GPE can never be active together. This is
1417 * ensured in vxlan_dev_configure.
1418 */
Thomas Graf35114942015-01-15 03:53:55 +01001419
Jiri Bencf14eceb2016-02-16 21:59:01 +01001420 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001421 /* If there are any unprocessed flags remaining treat
1422 * this as a malformed packet. This behavior diverges from
1423 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1424 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001425 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001426 * is more robust and provides a little more security in
1427 * adding extensions to VXLAN.
1428 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001429 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001430 }
1431
Jiri Bence1e53142016-04-05 14:47:13 +02001432 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001433 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001434 goto drop;
1435 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001436 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001437 skb->dev = vxlan->dev;
1438 skb->pkt_type = PACKET_HOST;
1439 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001440
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001441 oiph = skb_network_header(skb);
1442 skb_reset_network_header(skb);
1443
1444 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1445 ++vxlan->dev->stats.rx_frame_errors;
1446 ++vxlan->dev->stats.rx_errors;
1447 goto drop;
1448 }
1449
1450 stats = this_cpu_ptr(vxlan->dev->tstats);
1451 u64_stats_update_begin(&stats->syncp);
1452 stats->rx_packets++;
1453 stats->rx_bytes += skb->len;
1454 u64_stats_update_end(&stats->syncp);
1455
1456 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001457 return 0;
1458
1459drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001460 /* Consume bad packet */
1461 kfree_skb(skb);
1462 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001463}
1464
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001465static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001466{
1467 struct vxlan_dev *vxlan = netdev_priv(dev);
1468 struct arphdr *parp;
1469 u8 *arpptr, *sha;
1470 __be32 sip, tip;
1471 struct neighbour *n;
1472
1473 if (dev->flags & IFF_NOARP)
1474 goto out;
1475
1476 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1477 dev->stats.tx_dropped++;
1478 goto out;
1479 }
1480 parp = arp_hdr(skb);
1481
1482 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1483 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1484 parp->ar_pro != htons(ETH_P_IP) ||
1485 parp->ar_op != htons(ARPOP_REQUEST) ||
1486 parp->ar_hln != dev->addr_len ||
1487 parp->ar_pln != 4)
1488 goto out;
1489 arpptr = (u8 *)parp + sizeof(struct arphdr);
1490 sha = arpptr;
1491 arpptr += dev->addr_len; /* sha */
1492 memcpy(&sip, arpptr, sizeof(sip));
1493 arpptr += sizeof(sip);
1494 arpptr += dev->addr_len; /* tha */
1495 memcpy(&tip, arpptr, sizeof(tip));
1496
1497 if (ipv4_is_loopback(tip) ||
1498 ipv4_is_multicast(tip))
1499 goto out;
1500
1501 n = neigh_lookup(&arp_tbl, &tip, dev);
1502
1503 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001504 struct vxlan_fdb *f;
1505 struct sk_buff *reply;
1506
1507 if (!(n->nud_state & NUD_CONNECTED)) {
1508 neigh_release(n);
1509 goto out;
1510 }
1511
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001512 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08001513 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001514 /* bridge-local neighbor */
1515 neigh_release(n);
1516 goto out;
1517 }
1518
1519 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1520 n->ha, sha);
1521
1522 neigh_release(n);
1523
David Stevens73461352014-03-18 12:32:29 -04001524 if (reply == NULL)
1525 goto out;
1526
David Stevense4f67ad2012-11-20 02:50:14 +00001527 skb_reset_mac_header(reply);
1528 __skb_pull(reply, skb_network_offset(reply));
1529 reply->ip_summed = CHECKSUM_UNNECESSARY;
1530 reply->pkt_type = PACKET_HOST;
1531
1532 if (netif_rx_ni(reply) == NET_RX_DROP)
1533 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001534 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001535 union vxlan_addr ipa = {
1536 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001537 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001538 };
1539
1540 vxlan_ip_miss(dev, &ipa);
1541 }
David Stevense4f67ad2012-11-20 02:50:14 +00001542out:
1543 consume_skb(skb);
1544 return NETDEV_TX_OK;
1545}
1546
Cong Wangf564f452013-08-31 13:44:36 +08001547#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001548static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1549 struct neighbour *n, bool isrouter)
1550{
1551 struct net_device *dev = request->dev;
1552 struct sk_buff *reply;
1553 struct nd_msg *ns, *na;
1554 struct ipv6hdr *pip6;
1555 u8 *daddr;
1556 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1557 int ns_olen;
1558 int i, len;
1559
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001560 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04001561 return NULL;
1562
1563 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1564 sizeof(*na) + na_olen + dev->needed_tailroom;
1565 reply = alloc_skb(len, GFP_ATOMIC);
1566 if (reply == NULL)
1567 return NULL;
1568
1569 reply->protocol = htons(ETH_P_IPV6);
1570 reply->dev = dev;
1571 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1572 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001573 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001574
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001575 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04001576
1577 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001578 ns_olen = request->len - skb_network_offset(request) -
1579 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04001580 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1581 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1582 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1583 break;
1584 }
1585 }
1586
1587 /* Ethernet header */
1588 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1589 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1590 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1591 reply->protocol = htons(ETH_P_IPV6);
1592
1593 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001594 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001595 skb_put(reply, sizeof(struct ipv6hdr));
1596
1597 /* IPv6 header */
1598
1599 pip6 = ipv6_hdr(reply);
1600 memset(pip6, 0, sizeof(struct ipv6hdr));
1601 pip6->version = 6;
1602 pip6->priority = ipv6_hdr(request)->priority;
1603 pip6->nexthdr = IPPROTO_ICMPV6;
1604 pip6->hop_limit = 255;
1605 pip6->daddr = ipv6_hdr(request)->saddr;
1606 pip6->saddr = *(struct in6_addr *)n->primary_key;
1607
1608 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001609 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001610
David Stevens4b29dba2014-03-24 10:39:58 -04001611 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02001612 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04001613 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1614 na->icmph.icmp6_router = isrouter;
1615 na->icmph.icmp6_override = 1;
1616 na->icmph.icmp6_solicited = 1;
1617 na->target = ns->target;
1618 ether_addr_copy(&na->opt[2], n->ha);
1619 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1620 na->opt[1] = na_olen >> 3;
1621
1622 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1623 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1624 csum_partial(na, sizeof(*na)+na_olen, 0));
1625
1626 pip6->payload_len = htons(sizeof(*na)+na_olen);
1627
1628 skb_push(reply, sizeof(struct ipv6hdr));
1629
1630 reply->ip_summed = CHECKSUM_UNNECESSARY;
1631
1632 return reply;
1633}
1634
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001635static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08001636{
1637 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08001638 const struct in6_addr *daddr;
Xin Long8bff36852017-11-11 19:58:50 +08001639 const struct ipv6hdr *iphdr;
David Stevens4b29dba2014-03-24 10:39:58 -04001640 struct inet6_dev *in6_dev;
Xin Long8bff36852017-11-11 19:58:50 +08001641 struct neighbour *n;
1642 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001643
1644 in6_dev = __in6_dev_get(dev);
1645 if (!in6_dev)
1646 goto out;
1647
Cong Wangf564f452013-08-31 13:44:36 +08001648 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001649 daddr = &iphdr->daddr;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001650 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08001651
David Stevens4b29dba2014-03-24 10:39:58 -04001652 if (ipv6_addr_loopback(daddr) ||
1653 ipv6_addr_is_multicast(&msg->target))
1654 goto out;
1655
1656 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001657
1658 if (n) {
1659 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001660 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001661
1662 if (!(n->nud_state & NUD_CONNECTED)) {
1663 neigh_release(n);
1664 goto out;
1665 }
1666
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001667 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08001668 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1669 /* bridge-local neighbor */
1670 neigh_release(n);
1671 goto out;
1672 }
1673
David Stevens4b29dba2014-03-24 10:39:58 -04001674 reply = vxlan_na_create(skb, n,
1675 !!(f ? f->flags & NTF_ROUTER : 0));
1676
Cong Wangf564f452013-08-31 13:44:36 +08001677 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001678
1679 if (reply == NULL)
1680 goto out;
1681
1682 if (netif_rx_ni(reply) == NET_RX_DROP)
1683 dev->stats.rx_dropped++;
1684
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001685 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001686 union vxlan_addr ipa = {
1687 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001688 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001689 };
1690
Cong Wangf564f452013-08-31 13:44:36 +08001691 vxlan_ip_miss(dev, &ipa);
1692 }
1693
1694out:
1695 consume_skb(skb);
1696 return NETDEV_TX_OK;
1697}
1698#endif
1699
David Stevense4f67ad2012-11-20 02:50:14 +00001700static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1701{
1702 struct vxlan_dev *vxlan = netdev_priv(dev);
1703 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001704
1705 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1706 return false;
1707
1708 n = NULL;
1709 switch (ntohs(eth_hdr(skb)->h_proto)) {
1710 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001711 {
1712 struct iphdr *pip;
1713
David Stevense4f67ad2012-11-20 02:50:14 +00001714 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1715 return false;
1716 pip = ip_hdr(skb);
1717 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001718 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001719 union vxlan_addr ipa = {
1720 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001721 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001722 };
1723
1724 vxlan_ip_miss(dev, &ipa);
1725 return false;
1726 }
1727
David Stevense4f67ad2012-11-20 02:50:14 +00001728 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001729 }
1730#if IS_ENABLED(CONFIG_IPV6)
1731 case ETH_P_IPV6:
1732 {
1733 struct ipv6hdr *pip6;
1734
1735 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1736 return false;
1737 pip6 = ipv6_hdr(skb);
1738 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001739 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08001740 union vxlan_addr ipa = {
1741 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001742 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001743 };
1744
1745 vxlan_ip_miss(dev, &ipa);
1746 return false;
1747 }
1748
1749 break;
1750 }
1751#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001752 default:
1753 return false;
1754 }
1755
1756 if (n) {
1757 bool diff;
1758
Joe Perches7367d0b2013-09-01 11:51:23 -07001759 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001760 if (diff) {
1761 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1762 dev->addr_len);
1763 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1764 }
1765 neigh_release(n);
1766 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001767 }
1768
David Stevense4f67ad2012-11-20 02:50:14 +00001769 return false;
1770}
1771
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001772static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001773 struct vxlan_metadata *md)
1774{
1775 struct vxlanhdr_gbp *gbp;
1776
Thomas Grafdb79a622015-02-04 17:00:04 +01001777 if (!md->gbp)
1778 return;
1779
Thomas Graf35114942015-01-15 03:53:55 +01001780 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001781 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001782
1783 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1784 gbp->dont_learn = 1;
1785
1786 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1787 gbp->policy_applied = 1;
1788
1789 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1790}
1791
Jiri Bence1e53142016-04-05 14:47:13 +02001792static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1793 __be16 protocol)
1794{
1795 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1796
1797 gpe->np_applied = 1;
Jiri Bencfa20e0e2017-08-28 21:43:22 +02001798 gpe->next_protocol = tun_p_from_eth_p(protocol);
1799 if (!gpe->next_protocol)
1800 return -EPFNOSUPPORT;
1801 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02001802}
1803
Jiri Bencf491e562016-02-02 18:09:16 +01001804static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1805 int iphdr_len, __be32 vni,
1806 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001807 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001808{
Cong Wange4c7ed42013-08-31 13:44:33 +08001809 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001810 int min_headroom;
1811 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001812 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001813 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001814
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001815 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001816 skb->ip_summed == CHECKSUM_PARTIAL) {
1817 int csum_start = skb_checksum_start_offset(skb);
1818
1819 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1820 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1821 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001822 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001823 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001824 }
1825
Cong Wange4c7ed42013-08-31 13:44:33 +08001826 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08001827 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001828
1829 /* Need space for new headers (invalidates iph ptr) */
1830 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001831 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08001832 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001833
Alexander Duyckaed069d2016-04-14 15:33:37 -04001834 err = iptunnel_handle_offloads(skb, type);
1835 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08001836 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07001837
Johannes Bergd58ff352017-06-16 14:29:23 +02001838 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001839 vxh->vx_flags = VXLAN_HF_VNI;
1840 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001841
Tom Herbertdfd86452015-01-12 17:00:38 -08001842 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001843 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001844
Jiri Benc54bfd872016-02-16 21:58:58 +01001845 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1846 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1847 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001848
1849 if (!skb_is_gso(skb)) {
1850 skb->ip_summed = CHECKSUM_NONE;
1851 skb->encapsulation = 0;
1852 }
1853 }
1854
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001855 if (vxflags & VXLAN_F_GBP)
1856 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001857 if (vxflags & VXLAN_F_GPE) {
1858 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1859 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08001860 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02001861 inner_protocol = skb->protocol;
1862 }
Thomas Graf35114942015-01-15 03:53:55 +01001863
Jiri Bence1e53142016-04-05 14:47:13 +02001864 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001865 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07001866}
Pravin B Shelar49560532013-08-19 11:23:17 -07001867
pravin shelar655c3de2016-11-13 20:43:55 -08001868static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
1869 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01001870 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001871 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001872 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001873 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01001874{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001875 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01001876 struct rtable *rt = NULL;
1877 struct flowi4 fl4;
1878
pravin shelar655c3de2016-11-13 20:43:55 -08001879 if (!sock4)
1880 return ERR_PTR(-EIO);
1881
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001882 if (tos && !info)
1883 use_cache = false;
1884 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001885 rt = dst_cache_get_ip4(dst_cache, saddr);
1886 if (rt)
1887 return rt;
1888 }
1889
Jiri Benc1a8496b2016-02-02 18:09:14 +01001890 memset(&fl4, 0, sizeof(fl4));
1891 fl4.flowi4_oif = oif;
1892 fl4.flowi4_tos = RT_TOS(tos);
1893 fl4.flowi4_mark = skb->mark;
1894 fl4.flowi4_proto = IPPROTO_UDP;
1895 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001896 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001897 fl4.fl4_dport = dport;
1898 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01001899
1900 rt = ip_route_output_key(vxlan->net, &fl4);
pravin shelar655c3de2016-11-13 20:43:55 -08001901 if (likely(!IS_ERR(rt))) {
1902 if (rt->dst.dev == dev) {
1903 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
1904 ip_rt_put(rt);
1905 return ERR_PTR(-ELOOP);
1906 }
1907
Jiri Benc1a8496b2016-02-02 18:09:14 +01001908 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001909 if (use_cache)
1910 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08001911 } else {
1912 netdev_dbg(dev, "no route to %pI4\n", &daddr);
1913 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001914 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01001915 return rt;
1916}
1917
Jiri Bence5d4b292015-12-07 13:04:30 +01001918#if IS_ENABLED(CONFIG_IPV6)
1919static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08001920 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08001921 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01001922 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001923 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01001924 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001925 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001926 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001927 struct dst_cache *dst_cache,
1928 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01001929{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001930 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01001931 struct dst_entry *ndst;
1932 struct flowi6 fl6;
1933 int err;
1934
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001935 if (!sock6)
1936 return ERR_PTR(-EIO);
1937
Daniel Borkmann14006152016-03-04 15:15:08 +01001938 if (tos && !info)
1939 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001940 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001941 ndst = dst_cache_get_ip6(dst_cache, saddr);
1942 if (ndst)
1943 return ndst;
1944 }
1945
Jiri Bence5d4b292015-12-07 13:04:30 +01001946 memset(&fl6, 0, sizeof(fl6));
1947 fl6.flowi6_oif = oif;
1948 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001949 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01001950 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01001951 fl6.flowi6_mark = skb->mark;
1952 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001953 fl6.fl6_dport = dport;
1954 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01001955
1956 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001957 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01001958 &ndst, &fl6);
pravin shelar655c3de2016-11-13 20:43:55 -08001959 if (unlikely(err < 0)) {
1960 netdev_dbg(dev, "no route to %pI6\n", daddr);
1961 return ERR_PTR(-ENETUNREACH);
1962 }
1963
1964 if (unlikely(ndst->dev == dev)) {
1965 netdev_dbg(dev, "circular route to %pI6\n", daddr);
1966 dst_release(ndst);
1967 return ERR_PTR(-ELOOP);
1968 }
Jiri Bence5d4b292015-12-07 13:04:30 +01001969
1970 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001971 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001972 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01001973 return ndst;
1974}
1975#endif
1976
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001977/* Bypass encapsulation if the destination is local */
1978static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001979 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001980{
Li RongQing8f849852014-01-04 13:57:59 +08001981 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001982 union vxlan_addr loopback;
1983 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001984 struct net_device *dev = skb->dev;
1985 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001986
Li RongQing8f849852014-01-04 13:57:59 +08001987 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1988 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001989 skb->pkt_type = PACKET_HOST;
1990 skb->encapsulation = 0;
1991 skb->dev = dst_vxlan->dev;
1992 __skb_pull(skb, skb_network_offset(skb));
1993
Cong Wange4c7ed42013-08-31 13:44:33 +08001994 if (remote_ip->sa.sa_family == AF_INET) {
1995 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1996 loopback.sa.sa_family = AF_INET;
1997#if IS_ENABLED(CONFIG_IPV6)
1998 } else {
1999 loopback.sin6.sin6_addr = in6addr_loopback;
2000 loopback.sa.sa_family = AF_INET6;
2001#endif
2002 }
2003
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002004 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
Matthias Schiffer87613de2017-06-19 10:03:59 +02002005 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0,
2006 vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002007
2008 u64_stats_update_begin(&tx_stats->syncp);
2009 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002010 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002011 u64_stats_update_end(&tx_stats->syncp);
2012
2013 if (netif_rx(skb) == NET_RX_SUCCESS) {
2014 u64_stats_update_begin(&rx_stats->syncp);
2015 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002016 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002017 u64_stats_update_end(&rx_stats->syncp);
2018 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08002019 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002020 }
2021}
2022
pravin shelarfee1fad2016-11-13 20:43:56 -08002023static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002024 struct vxlan_dev *vxlan,
2025 union vxlan_addr *daddr,
2026 __be16 dst_port, int dst_ifindex, __be32 vni,
2027 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002028 u32 rt_flags)
2029{
2030#if IS_ENABLED(CONFIG_IPV6)
2031 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2032 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2033 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2034 */
2035 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2036#endif
2037 /* Bypass encapsulation if the destination is local */
2038 if (rt_flags & RTCF_LOCAL &&
2039 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2040 struct vxlan_dev *dst_vxlan;
2041
2042 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002043 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002044 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002045 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002046 if (!dst_vxlan) {
2047 dev->stats.tx_errors++;
2048 kfree_skb(skb);
2049
2050 return -ENOENT;
2051 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002052 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002053 return 1;
2054 }
2055
2056 return 0;
2057}
2058
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002059static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002060 __be32 default_vni, struct vxlan_rdst *rdst,
2061 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002062{
Paolo Abenid71785f2016-02-12 15:43:57 +01002063 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002064 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002065 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002066 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002067 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002068 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002069 struct vxlan_metadata _md;
2070 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002071 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002072 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002073 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002074 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002075 int ifindex;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07002076 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002077 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002078 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002079 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002080
Jiri Benc61adedf2015-08-20 13:56:25 +02002081 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002082
Thomas Grafee122c72015-07-21 10:43:58 +02002083 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002084 dst = &rdst->remote_ip;
2085 if (vxlan_addr_any(dst)) {
2086 if (did_rsc) {
2087 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002088 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002089 return;
2090 }
2091 goto drop;
2092 }
2093
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002094 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002095 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002096 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002097 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002098 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002099 md->gbp = skb->mark;
Hangbin Liu72f6d712018-04-17 14:11:28 +08002100 if (flags & VXLAN_F_TTL_INHERIT) {
2101 ttl = ip_tunnel_get_ttl(old_iph, skb);
2102 } else {
2103 ttl = vxlan->cfg.ttl;
2104 if (!ttl && vxlan_addr_multicast(dst))
2105 ttl = 1;
2106 }
pravin shelar0770b532016-11-13 20:43:57 -08002107
2108 tos = vxlan->cfg.tos;
2109 if (tos == 1)
2110 tos = ip_tunnel_get_dsfield(old_iph, skb);
2111
2112 if (dst->sa.sa_family == AF_INET)
2113 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2114 else
2115 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2116 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002117 } else {
2118 if (!info) {
2119 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2120 dev->name);
2121 goto drop;
2122 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002123 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002124 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002125 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002126 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2127 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002128 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002129 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2130 }
Thomas Grafee122c72015-07-21 10:43:58 +02002131 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002132 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2133 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002134 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002135 dst_cache = &info->dst_cache;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002136 if (info->options_len &&
2137 info->key.tun_flags & TUNNEL_VXLAN_OPT)
pravin shelar0770b532016-11-13 20:43:57 -08002138 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002139 ttl = info->key.ttl;
2140 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002141 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002142 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002143 }
pravin shelar0770b532016-11-13 20:43:57 -08002144 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2145 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002146
Jakub Kicinski56de8592017-02-24 11:43:36 -08002147 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002148 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002149 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002150 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002151 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002152
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002153 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002154 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002155 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002156 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002157 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002158 if (IS_ERR(rt)) {
2159 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002160 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002161 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002162
Cong Wange4c7ed42013-08-31 13:44:33 +08002163 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002164 if (!info) {
2165 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002166 dst_port, ifindex, vni,
2167 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002168 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002169 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002170 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002171 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002172 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002173
pravin shelarc46b7892016-11-13 20:43:54 -08002174 ndst = &rt->dst;
Xin Longa93bf0f2017-12-18 14:20:56 +08002175 if (skb_dst(skb)) {
2176 int mtu = dst_mtu(ndst) - VXLAN_HEADROOM;
2177
Nicolas Dichtelf15ca722018-01-25 19:03:03 +01002178 skb_dst_update_pmtu(skb, mtu);
Xin Longa93bf0f2017-12-18 14:20:56 +08002179 }
2180
Cong Wange4c7ed42013-08-31 13:44:33 +08002181 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2182 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002183 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002184 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002185 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002186 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002187
Brian Russell11586322017-02-24 17:47:11 +00002188 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002189 dst->sin.sin_addr.s_addr, tos, ttl, df,
2190 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002191#if IS_ENABLED(CONFIG_IPV6)
2192 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002193 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002194
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002195 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002196 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002197 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002198 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002199 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002200 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002201 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002202 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002203 goto tx_error;
2204 }
pravin shelar655c3de2016-11-13 20:43:55 -08002205
pravin shelarfee1fad2016-11-13 20:43:56 -08002206 if (!info) {
2207 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002208
pravin shelarfee1fad2016-11-13 20:43:56 -08002209 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002210 dst_port, ifindex, vni,
2211 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002212 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002213 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002214 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002215
Xin Longa93bf0f2017-12-18 14:20:56 +08002216 if (skb_dst(skb)) {
2217 int mtu = dst_mtu(ndst) - VXLAN6_HEADROOM;
2218
Nicolas Dichtelf15ca722018-01-25 19:03:03 +01002219 skb_dst_update_pmtu(skb, mtu);
Xin Longa93bf0f2017-12-18 14:20:56 +08002220 }
2221
Daniel Borkmann14006152016-03-04 15:15:08 +01002222 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002223 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002224 skb_scrub_packet(skb, xnet);
2225 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002226 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002227 if (err < 0)
2228 goto tx_error;
2229
pravin shelar0770b532016-11-13 20:43:57 -08002230 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002231 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002232 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002233 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002234#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002235 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002236out_unlock:
2237 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002238 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002239
2240drop:
2241 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002242 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002243 return;
2244
2245tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002246 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002247 if (err == -ELOOP)
2248 dev->stats.collisions++;
2249 else if (err == -ENETUNREACH)
2250 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002251 dst_release(ndst);
2252 dev->stats.tx_errors++;
2253 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002254}
2255
David Stevens66817122013-03-15 04:35:51 +00002256/* Transmit local packets over Vxlan
2257 *
2258 * Outer IP header inherits ECN and DF from inner header.
2259 * Outer UDP destination is the VXLAN assigned port.
2260 * source port is based on hash of flow
2261 */
2262static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2263{
2264 struct vxlan_dev *vxlan = netdev_priv(dev);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002265 struct vxlan_rdst *rdst, *fdst = NULL;
Xin Long8bff36852017-11-11 19:58:50 +08002266 const struct ip_tunnel_info *info;
2267 bool did_rsc = false;
David Stevens66817122013-03-15 04:35:51 +00002268 struct vxlan_fdb *f;
Xin Long8bff36852017-11-11 19:58:50 +08002269 struct ethhdr *eth;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002270 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002271
Jiri Benc61adedf2015-08-20 13:56:25 +02002272 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002273
David Stevens66817122013-03-15 04:35:51 +00002274 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002275
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002276 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002277 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2278 info->mode & IP_TUNNEL_INFO_TX) {
2279 vni = tunnel_id_to_key32(info->key.tun_id);
2280 } else {
2281 if (info && info->mode & IP_TUNNEL_INFO_TX)
2282 vxlan_xmit_one(skb, dev, vni, NULL, false);
2283 else
2284 kfree_skb(skb);
2285 return NETDEV_TX_OK;
2286 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002287 }
2288
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002289 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002290 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002291 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002292 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002293#if IS_ENABLED(CONFIG_IPV6)
Xin Long8bff36852017-11-11 19:58:50 +08002294 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2295 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2296 sizeof(struct nd_msg)) &&
2297 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2298 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2299
2300 if (m->icmph.icmp6_code == 0 &&
2301 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002302 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002303 }
2304#endif
2305 }
David Stevens66817122013-03-15 04:35:51 +00002306
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002307 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002308 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002309 did_rsc = false;
2310
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002311 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002312 (ntohs(eth->h_proto) == ETH_P_IP ||
2313 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002314 did_rsc = route_shortcircuit(dev, skb);
2315 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002316 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002317 }
2318
David Stevens66817122013-03-15 04:35:51 +00002319 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002320 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002321 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002322 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002323 !is_multicast_ether_addr(eth->h_dest))
2324 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002325
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002326 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002327 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002328 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002329 }
David Stevens66817122013-03-15 04:35:51 +00002330 }
2331
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002332 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2333 struct sk_buff *skb1;
2334
Eric Dumazet8f646c92014-01-06 09:54:31 -08002335 if (!fdst) {
2336 fdst = rdst;
2337 continue;
2338 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002339 skb1 = skb_clone(skb, GFP_ATOMIC);
2340 if (skb1)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002341 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002342 }
2343
Eric Dumazet8f646c92014-01-06 09:54:31 -08002344 if (fdst)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002345 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002346 else
2347 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002348 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002349}
2350
stephen hemmingerd3428942012-10-01 12:32:35 +00002351/* Walk the forwarding table and purge stale entries */
Kees Cookdf7e8282017-10-04 16:26:59 -07002352static void vxlan_cleanup(struct timer_list *t)
stephen hemmingerd3428942012-10-01 12:32:35 +00002353{
Kees Cookdf7e8282017-10-04 16:26:59 -07002354 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
stephen hemmingerd3428942012-10-01 12:32:35 +00002355 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2356 unsigned int h;
2357
2358 if (!netif_running(vxlan->dev))
2359 return;
2360
stephen hemmingerd3428942012-10-01 12:32:35 +00002361 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2362 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002363
2364 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002365 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2366 struct vxlan_fdb *f
2367 = container_of(p, struct vxlan_fdb, hlist);
2368 unsigned long timeout;
2369
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002370 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002371 continue;
2372
Roopa Prabhudef499c2017-03-27 15:46:41 -07002373 if (f->flags & NTF_EXT_LEARNED)
2374 continue;
2375
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002376 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002377 if (time_before_eq(timeout, jiffies)) {
2378 netdev_dbg(vxlan->dev,
2379 "garbage collect %pM\n",
2380 f->eth_addr);
2381 f->state = NUD_STALE;
Roopa Prabhu4c2438b2018-07-04 16:46:31 -07002382 vxlan_fdb_destroy(vxlan, f, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002383 } else if (time_before(timeout, next_timer))
2384 next_timer = timeout;
2385 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002386 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002387 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002388
2389 mod_timer(&vxlan->age_timer, next_timer);
2390}
2391
Mark Blocha53cb292017-06-02 03:24:08 +03002392static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2393{
2394 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2395
2396 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002397 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2398#if IS_ENABLED(CONFIG_IPV6)
2399 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2400#endif
Mark Blocha53cb292017-06-02 03:24:08 +03002401 spin_unlock(&vn->sock_lock);
2402}
2403
Jiri Benc69e76662017-07-02 19:00:57 +02002404static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2405 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002406{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002407 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002408 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002409
Jiri Benc69e76662017-07-02 19:00:57 +02002410 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002411 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002412 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002413 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002414}
2415
stephen hemmingerd3428942012-10-01 12:32:35 +00002416/* Setup stats when device is created */
2417static int vxlan_init(struct net_device *dev)
2418{
WANG Cong1c213bd2014-02-13 11:46:28 -08002419 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002420 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002421 return -ENOMEM;
2422
2423 return 0;
2424}
2425
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002426static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002427{
2428 struct vxlan_fdb *f;
2429
2430 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002431 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002432 if (f)
Roopa Prabhu4c2438b2018-07-04 16:46:31 -07002433 vxlan_fdb_destroy(vxlan, f, true);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002434 spin_unlock_bh(&vxlan->hash_lock);
2435}
2436
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002437static void vxlan_uninit(struct net_device *dev)
2438{
2439 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002440
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002441 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002442
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002443 free_percpu(dev->tstats);
2444}
2445
stephen hemmingerd3428942012-10-01 12:32:35 +00002446/* Start ageing timer and join group when device is brought up */
2447static int vxlan_open(struct net_device *dev)
2448{
2449 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002450 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002451
Jiri Benc205f3562015-09-24 13:50:01 +02002452 ret = vxlan_sock_add(vxlan);
2453 if (ret < 0)
2454 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002455
Gao feng79d4a942013-12-10 16:37:32 +08002456 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002457 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002458 if (ret == -EADDRINUSE)
2459 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002460 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002461 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002462 return ret;
2463 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002464 }
2465
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002466 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002467 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2468
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002469 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002470}
2471
2472/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002473static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00002474{
Cong Wang31fec5a2013-05-27 22:35:52 +00002475 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002476
2477 spin_lock_bh(&vxlan->hash_lock);
2478 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2479 struct hlist_node *p, *n;
2480 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2481 struct vxlan_fdb *f
2482 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002483 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2484 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002485 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2486 if (!is_zero_ether_addr(f->eth_addr))
Roopa Prabhu4c2438b2018-07-04 16:46:31 -07002487 vxlan_fdb_destroy(vxlan, f, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002488 }
2489 }
2490 spin_unlock_bh(&vxlan->hash_lock);
2491}
2492
2493/* Cleanup timer and forwarding table on shutdown */
2494static int vxlan_stop(struct net_device *dev)
2495{
2496 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002497 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002498 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002499
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002500 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002501 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002502 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002503
2504 del_timer_sync(&vxlan->age_timer);
2505
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002506 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02002507 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002508
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002509 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002510}
2511
stephen hemmingerd3428942012-10-01 12:32:35 +00002512/* Stub, nothing needs to be done. */
2513static void vxlan_set_multicast_list(struct net_device *dev)
2514{
2515}
2516
Daniel Borkmann345010b2013-12-18 00:21:08 +01002517static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2518{
2519 struct vxlan_dev *vxlan = netdev_priv(dev);
2520 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002521 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2522 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002523 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04002524
2525 /* This check is different than dev->max_mtu, because it looks at
2526 * the lowerdev->mtu, rather than the static dev->max_mtu
2527 */
2528 if (lowerdev) {
2529 int max_mtu = lowerdev->mtu -
2530 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2531 if (new_mtu > max_mtu)
2532 return -EINVAL;
2533 }
2534
2535 dev->mtu = new_mtu;
2536 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01002537}
2538
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002539static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2540{
2541 struct vxlan_dev *vxlan = netdev_priv(dev);
2542 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2543 __be16 sport, dport;
2544
2545 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2546 vxlan->cfg.port_max, true);
2547 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2548
Jiri Benc239e9442015-12-07 13:04:31 +01002549 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002550 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002551 struct rtable *rt;
2552
pravin shelar655c3de2016-11-13 20:43:55 -08002553 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002554 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002555 &info->key.u.ipv4.src, dport, sport,
2556 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002557 if (IS_ERR(rt))
2558 return PTR_ERR(rt);
2559 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002560 } else {
2561#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08002562 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01002563 struct dst_entry *ndst;
2564
pravin shelar655c3de2016-11-13 20:43:55 -08002565 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002566 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002567 &info->key.u.ipv6.src, dport, sport,
2568 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002569 if (IS_ERR(ndst))
2570 return PTR_ERR(ndst);
2571 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002572#else /* !CONFIG_IPV6 */
2573 return -EPFNOSUPPORT;
2574#endif
2575 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002576 info->key.tp_src = sport;
2577 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002578 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002579}
2580
Jiri Benc0c867c92016-04-05 14:47:10 +02002581static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002582 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002583 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002584 .ndo_open = vxlan_open,
2585 .ndo_stop = vxlan_stop,
2586 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002587 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002588 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002589 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002590 .ndo_validate_addr = eth_validate_addr,
2591 .ndo_set_mac_address = eth_mac_addr,
2592 .ndo_fdb_add = vxlan_fdb_add,
2593 .ndo_fdb_del = vxlan_fdb_delete,
2594 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002595 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002596};
2597
Jiri Bence1e53142016-04-05 14:47:13 +02002598static const struct net_device_ops vxlan_netdev_raw_ops = {
2599 .ndo_init = vxlan_init,
2600 .ndo_uninit = vxlan_uninit,
2601 .ndo_open = vxlan_open,
2602 .ndo_stop = vxlan_stop,
2603 .ndo_start_xmit = vxlan_xmit,
2604 .ndo_get_stats64 = ip_tunnel_get_stats64,
2605 .ndo_change_mtu = vxlan_change_mtu,
2606 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2607};
2608
stephen hemmingerd3428942012-10-01 12:32:35 +00002609/* Info for udev, that this is a virtual tunnel endpoint */
2610static struct device_type vxlan_type = {
2611 .name = "vxlan",
2612};
2613
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002614/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002615 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002616 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002617 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002618static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002619{
2620 struct vxlan_sock *vs;
2621 struct net *net = dev_net(dev);
2622 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002623 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002624
2625 spin_lock(&vn->sock_lock);
2626 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02002627 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2628 unsigned short type;
2629
2630 if (vs->flags & VXLAN_F_GPE)
2631 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
2632 else
2633 type = UDP_TUNNEL_TYPE_VXLAN;
2634
2635 if (push)
2636 udp_tunnel_push_rx_port(dev, vs->sock, type);
2637 else
2638 udp_tunnel_drop_rx_port(dev, vs->sock, type);
2639 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002640 }
2641 spin_unlock(&vn->sock_lock);
2642}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002643
stephen hemmingerd3428942012-10-01 12:32:35 +00002644/* Initialize the device structure. */
2645static void vxlan_setup(struct net_device *dev)
2646{
2647 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002648 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002649
Jiri Benc65226ef2016-04-28 16:36:30 +02002650 eth_hw_addr_random(dev);
2651 ether_setup(dev);
2652
David S. Millercf124db2017-05-08 12:52:56 -04002653 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00002654 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2655
stephen hemmingerd3428942012-10-01 12:32:35 +00002656 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002657 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002658 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002659 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002660
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002661 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002662 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002663 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07002664 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002665 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002666
Matthias Schiffera9853432017-06-19 10:03:55 +02002667 /* MTU range: 68 - 65535 */
2668 dev->min_mtu = ETH_MIN_MTU;
2669 dev->max_mtu = ETH_MAX_MTU;
2670
stephen hemminger553675f2013-05-16 11:35:20 +00002671 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002672 spin_lock_init(&vxlan->hash_lock);
2673
Kees Cookdf7e8282017-10-04 16:26:59 -07002674 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
stephen hemmingerd3428942012-10-01 12:32:35 +00002675
2676 vxlan->dev = dev;
2677
Tom Herbert58ce31c2015-08-19 17:07:33 -07002678 gro_cells_init(&vxlan->gro_cells, dev);
2679
stephen hemmingerd3428942012-10-01 12:32:35 +00002680 for (h = 0; h < FDB_HASH_SIZE; ++h)
2681 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2682}
2683
Jiri Benc0c867c92016-04-05 14:47:10 +02002684static void vxlan_ether_setup(struct net_device *dev)
2685{
Jiri Benc0c867c92016-04-05 14:47:10 +02002686 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2687 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2688 dev->netdev_ops = &vxlan_netdev_ether_ops;
2689}
2690
Jiri Bence1e53142016-04-05 14:47:13 +02002691static void vxlan_raw_setup(struct net_device *dev)
2692{
Jiri Benc65226ef2016-04-28 16:36:30 +02002693 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002694 dev->type = ARPHRD_NONE;
2695 dev->hard_header_len = 0;
2696 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002697 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2698 dev->netdev_ops = &vxlan_netdev_raw_ops;
2699}
2700
stephen hemmingerd3428942012-10-01 12:32:35 +00002701static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2702 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002703 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002704 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002705 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2706 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002707 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002708 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2709 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002710 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002711 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2712 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2713 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002714 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002715 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2716 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2717 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2718 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002719 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002720 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002721 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2722 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2723 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002724 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2725 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002726 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002727 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002728 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
Hangbin Liu72f6d712018-04-17 14:11:28 +08002729 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002730};
2731
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002732static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
2733 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00002734{
2735 if (tb[IFLA_ADDRESS]) {
2736 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002737 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
2738 "Provided link layer address is not Ethernet");
stephen hemmingerd3428942012-10-01 12:32:35 +00002739 return -EINVAL;
2740 }
2741
2742 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002743 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
2744 "Provided Ethernet address is not unicast");
stephen hemmingerd3428942012-10-01 12:32:35 +00002745 return -EADDRNOTAVAIL;
2746 }
2747 }
2748
Matthias Schiffera9853432017-06-19 10:03:55 +02002749 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02002750 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02002751
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002752 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
2753 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
2754 "MTU must be between 68 and 65535");
Matthias Schiffera9853432017-06-19 10:03:55 +02002755 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002756 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002757 }
2758
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002759 if (!data) {
2760 NL_SET_ERR_MSG(extack,
2761 "Required attributes not provided to perform the operation");
stephen hemmingerd3428942012-10-01 12:32:35 +00002762 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002763 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002764
2765 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02002766 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2767
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002768 if (id >= VXLAN_N_VID) {
2769 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID],
2770 "VXLAN ID must be lower than 16777216");
stephen hemmingerd3428942012-10-01 12:32:35 +00002771 return -ERANGE;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002772 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002773 }
2774
stephen hemminger05f47d62012-10-09 20:35:50 +00002775 if (data[IFLA_VXLAN_PORT_RANGE]) {
2776 const struct ifla_vxlan_port_range *p
2777 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2778
2779 if (ntohs(p->high) < ntohs(p->low)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002780 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
2781 "Invalid source port range");
stephen hemminger05f47d62012-10-09 20:35:50 +00002782 return -EINVAL;
2783 }
2784 }
2785
stephen hemmingerd3428942012-10-01 12:32:35 +00002786 return 0;
2787}
2788
Yan Burman1b13c972013-01-29 23:43:07 +00002789static void vxlan_get_drvinfo(struct net_device *netdev,
2790 struct ethtool_drvinfo *drvinfo)
2791{
2792 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2793 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2794}
2795
2796static const struct ethtool_ops vxlan_ethtool_ops = {
2797 .get_drvinfo = vxlan_get_drvinfo,
2798 .get_link = ethtool_op_get_link,
2799};
2800
Tom Herbert3ee64f32014-07-13 19:49:42 -07002801static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2802 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002803{
Cong Wange4c7ed42013-08-31 13:44:33 +08002804 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002805 struct udp_port_cfg udp_conf;
2806 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002807
Tom Herbert3ee64f32014-07-13 19:49:42 -07002808 memset(&udp_conf, 0, sizeof(udp_conf));
2809
2810 if (ipv6) {
2811 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002812 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002813 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002814 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002815 } else {
2816 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002817 }
2818
Tom Herbert3ee64f32014-07-13 19:49:42 -07002819 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002820
Tom Herbert3ee64f32014-07-13 19:49:42 -07002821 /* Open UDP socket */
2822 err = udp_sock_create(net, &udp_conf, &sock);
2823 if (err < 0)
2824 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002825
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002826 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002827}
2828
2829/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002830static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2831 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002832{
2833 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2834 struct vxlan_sock *vs;
2835 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002836 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002837 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002838
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002839 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002840 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002841 return ERR_PTR(-ENOMEM);
2842
2843 for (h = 0; h < VNI_HASH_SIZE; ++h)
2844 INIT_HLIST_HEAD(&vs->vni_list[h]);
2845
Tom Herbert3ee64f32014-07-13 19:49:42 -07002846 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002847 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002848 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002849 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002850 }
2851
Cong Wange4c7ed42013-08-31 13:44:33 +08002852 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03002853 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002854 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002855
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002856 spin_lock(&vn->sock_lock);
2857 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07002858 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002859 (vs->flags & VXLAN_F_GPE) ?
2860 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002861 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002862 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002863
2864 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002865 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002866 tunnel_cfg.sk_user_data = vs;
2867 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002868 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002869 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002870 tunnel_cfg.gro_receive = vxlan_gro_receive;
2871 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002872
2873 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002874
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002875 return vs;
2876}
stephen hemminger553675f2013-05-16 11:35:20 +00002877
Jiri Bencb1be00a2015-09-24 13:50:02 +02002878static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002879{
Jiri Benc205f3562015-09-24 13:50:01 +02002880 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2881 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02002882 struct vxlan_dev_node *node;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002883
Jiri Benc205f3562015-09-24 13:50:01 +02002884 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002885 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002886 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002887 vxlan->cfg.dst_port, vxlan->cfg.flags);
Reshetova, Elena66af8462017-07-04 15:52:59 +03002888 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002889 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002890 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002891 }
2892 spin_unlock(&vn->sock_lock);
2893 }
Jiri Benc205f3562015-09-24 13:50:01 +02002894 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002895 vs = vxlan_socket_create(vxlan->net, ipv6,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002896 vxlan->cfg.dst_port, vxlan->cfg.flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002897 if (IS_ERR(vs))
2898 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002899#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02002900 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002901 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02002902 node = &vxlan->hlist6;
2903 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02002904#endif
Jiri Benc69e76662017-07-02 19:00:57 +02002905 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002906 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02002907 node = &vxlan->hlist4;
2908 }
2909 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02002910 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002911}
2912
Jiri Bencb1be00a2015-09-24 13:50:02 +02002913static int vxlan_sock_add(struct vxlan_dev *vxlan)
2914{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002915 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
2916 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02002917 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002918 int ret = 0;
2919
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002920 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002921#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002922 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02002923 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02002924 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02002925 if (ret < 0 && ret != -EAFNOSUPPORT)
2926 ipv4 = false;
2927 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002928#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02002929 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002930 ret = __vxlan_sock_add(vxlan, false);
2931 if (ret < 0)
2932 vxlan_sock_release(vxlan);
2933 return ret;
2934}
2935
Matthias Schiffera9853432017-06-19 10:03:55 +02002936static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
2937 struct net_device **lower,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002938 struct vxlan_dev *old,
2939 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00002940{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002941 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02002942 struct vxlan_dev *tmp;
2943 bool use_ipv6 = false;
2944
2945 if (conf->flags & VXLAN_F_GPE) {
2946 /* For now, allow GPE only together with
2947 * COLLECT_METADATA. This can be relaxed later; in such
2948 * case, the other side of the PtP link will have to be
2949 * provided.
2950 */
2951 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2952 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002953 NL_SET_ERR_MSG(extack,
2954 "VXLAN GPE does not support this combination of attributes");
Matthias Schiffera9853432017-06-19 10:03:55 +02002955 return -EINVAL;
2956 }
2957 }
2958
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002959 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
2960 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02002961 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002962 conf->saddr.sa.sa_family = AF_INET;
2963 } else if (!conf->remote_ip.sa.sa_family) {
2964 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
2965 } else if (!conf->saddr.sa.sa_family) {
2966 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
2967 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002968
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002969 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
2970 NL_SET_ERR_MSG(extack,
2971 "Local and remote address must be from the same family");
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002972 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002973 }
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002974
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002975 if (vxlan_addr_multicast(&conf->saddr)) {
2976 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02002977 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002978 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02002979
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002980 if (conf->saddr.sa.sa_family == AF_INET6) {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002981 if (!IS_ENABLED(CONFIG_IPV6)) {
2982 NL_SET_ERR_MSG(extack,
2983 "IPv6 support not enabled in the kernel");
Matthias Schiffera9853432017-06-19 10:03:55 +02002984 return -EPFNOSUPPORT;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002985 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002986 use_ipv6 = true;
2987 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02002988
2989 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2990 int local_type =
2991 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
2992 int remote_type =
2993 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
2994
2995 if (local_type & IPV6_ADDR_LINKLOCAL) {
2996 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07002997 (remote_type != IPV6_ADDR_ANY)) {
2998 NL_SET_ERR_MSG(extack,
2999 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003000 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003001 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003002
3003 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3004 } else {
3005 if (remote_type ==
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003006 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3007 NL_SET_ERR_MSG(extack,
3008 "Invalid combination of local and remote address scopes");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003009 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003010 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003011
3012 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3013 }
3014 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003015 }
3016
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003017 if (conf->label && !use_ipv6) {
3018 NL_SET_ERR_MSG(extack,
3019 "Label attribute only applies to IPv6 VXLAN devices");
Matthias Schiffera9853432017-06-19 10:03:55 +02003020 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003021 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003022
3023 if (conf->remote_ifindex) {
3024 struct net_device *lowerdev;
3025
3026 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003027 if (!lowerdev) {
3028 NL_SET_ERR_MSG(extack,
3029 "Invalid local interface, device not found");
Matthias Schiffera9853432017-06-19 10:03:55 +02003030 return -ENODEV;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003031 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003032
3033#if IS_ENABLED(CONFIG_IPV6)
3034 if (use_ipv6) {
3035 struct inet6_dev *idev = __in6_dev_get(lowerdev);
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003036 if (idev && idev->cnf.disable_ipv6) {
3037 NL_SET_ERR_MSG(extack,
3038 "IPv6 support disabled by administrator");
Matthias Schiffera9853432017-06-19 10:03:55 +02003039 return -EPERM;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003040 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003041 }
3042#endif
3043
3044 *lower = lowerdev;
3045 } else {
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003046 if (vxlan_addr_multicast(&conf->remote_ip)) {
3047 NL_SET_ERR_MSG(extack,
3048 "Local interface required for multicast remote destination");
3049
Matthias Schiffera9853432017-06-19 10:03:55 +02003050 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003051 }
Matthias Schiffera9853432017-06-19 10:03:55 +02003052
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003053#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003054 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3055 NL_SET_ERR_MSG(extack,
3056 "Local interface required for link-local local/remote addresses");
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003057 return -EINVAL;
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003058 }
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02003059#endif
3060
Matthias Schiffera9853432017-06-19 10:03:55 +02003061 *lower = NULL;
3062 }
3063
3064 if (!conf->dst_port) {
3065 if (conf->flags & VXLAN_F_GPE)
3066 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3067 else
3068 conf->dst_port = htons(vxlan_port);
3069 }
3070
3071 if (!conf->age_interval)
3072 conf->age_interval = FDB_AGE_DEFAULT;
3073
3074 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3075 if (tmp == old)
3076 continue;
3077
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003078 if (tmp->cfg.vni != conf->vni)
3079 continue;
3080 if (tmp->cfg.dst_port != conf->dst_port)
3081 continue;
3082 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003083 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003084 continue;
3085
3086 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3087 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3088 continue;
3089
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003090 NL_SET_ERR_MSG(extack,
3091 "A VXLAN device with the specified VNI already exists");
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003092 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003093 }
3094
3095 return 0;
3096}
3097
3098static void vxlan_config_apply(struct net_device *dev,
3099 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003100 struct net_device *lowerdev,
3101 struct net *src_net,
3102 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003103{
3104 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003105 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003106 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003107 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3108 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003109
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003110 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003111 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003112 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003113 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003114 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003115
Matthias Schiffera9853432017-06-19 10:03:55 +02003116 if (conf->mtu)
3117 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003118
3119 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003120 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003121
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003122 dst->remote_vni = conf->vni;
3123
3124 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003125
Matthias Schiffera9853432017-06-19 10:03:55 +02003126 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003127 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003128
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003129 dev->gso_max_size = lowerdev->gso_max_size;
3130 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003131
3132 needed_headroom = lowerdev->hard_header_len;
3133
3134 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3135 VXLAN_HEADROOM);
Alexey Kodanevf870c1f2017-12-14 20:20:00 +03003136 if (max_mtu < ETH_MIN_MTU)
3137 max_mtu = ETH_MIN_MTU;
3138
3139 if (!changelink && !conf->mtu)
3140 dev->mtu = max_mtu;
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003141 }
3142
Matthias Schiffera9853432017-06-19 10:03:55 +02003143 if (dev->mtu > max_mtu)
3144 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003145
Jiri Bencb1be00a2015-09-24 13:50:02 +02003146 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3147 needed_headroom += VXLAN6_HEADROOM;
3148 else
3149 needed_headroom += VXLAN_HEADROOM;
3150 dev->needed_headroom = needed_headroom;
3151
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003152 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003153}
stephen hemmingerd3428942012-10-01 12:32:35 +00003154
Matthias Schiffera9853432017-06-19 10:03:55 +02003155static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003156 struct vxlan_config *conf, bool changelink,
3157 struct netlink_ext_ack *extack)
Matthias Schiffera9853432017-06-19 10:03:55 +02003158{
3159 struct vxlan_dev *vxlan = netdev_priv(dev);
3160 struct net_device *lowerdev;
3161 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003162
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003163 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
Matthias Schiffera9853432017-06-19 10:03:55 +02003164 if (ret)
3165 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003166
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003167 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003168
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003169 return 0;
3170}
3171
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003172static int __vxlan_dev_create(struct net *net, struct net_device *dev,
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003173 struct vxlan_config *conf,
3174 struct netlink_ext_ack *extack)
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003175{
3176 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3177 struct vxlan_dev *vxlan = netdev_priv(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003178 struct vxlan_fdb *f = NULL;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003179 int err;
3180
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003181 err = vxlan_dev_configure(net, dev, conf, false, extack);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003182 if (err)
3183 return err;
3184
3185 dev->ethtool_ops = &vxlan_ethtool_ops;
3186
3187 /* create an fdb entry for a valid default destination */
3188 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003189 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003190 &vxlan->default_dst.remote_ip,
3191 NUD_REACHABLE | NUD_PERMANENT,
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003192 vxlan->cfg.dst_port,
3193 vxlan->default_dst.remote_vni,
3194 vxlan->default_dst.remote_vni,
3195 vxlan->default_dst.remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003196 NTF_SELF, &f);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003197 if (err)
3198 return err;
3199 }
3200
3201 err = register_netdevice(dev);
Roopa Prabhu0241b832018-07-04 16:46:32 -07003202 if (err)
3203 goto errout;
3204
3205 err = rtnl_configure_link(dev, NULL);
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003206 if (err) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003207 unregister_netdevice(dev);
3208 goto errout;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003209 }
3210
Roopa Prabhu0241b832018-07-04 16:46:32 -07003211 /* notify default fdb entry */
3212 if (f)
3213 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH);
3214
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003215 list_add(&vxlan->next, &vn->vxlan_list);
3216 return 0;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003217errout:
3218 if (f)
3219 vxlan_fdb_destroy(vxlan, f, false);
3220 return err;
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003221}
3222
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003223static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3224 struct net_device *dev, struct vxlan_config *conf,
3225 bool changelink)
3226{
3227 struct vxlan_dev *vxlan = netdev_priv(dev);
3228
3229 memset(conf, 0, sizeof(*conf));
3230
3231 /* if changelink operation, start with old existing cfg */
3232 if (changelink)
3233 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3234
3235 if (data[IFLA_VXLAN_ID]) {
3236 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3237
3238 if (changelink && (vni != conf->vni))
3239 return -EOPNOTSUPP;
3240 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3241 }
3242
3243 if (data[IFLA_VXLAN_GROUP]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003244 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET))
3245 return -EOPNOTSUPP;
3246
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003247 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003248 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003249 } else if (data[IFLA_VXLAN_GROUP6]) {
3250 if (!IS_ENABLED(CONFIG_IPV6))
3251 return -EPFNOSUPPORT;
3252
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003253 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6))
3254 return -EOPNOTSUPP;
3255
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003256 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3257 conf->remote_ip.sa.sa_family = AF_INET6;
3258 }
3259
3260 if (data[IFLA_VXLAN_LOCAL]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003261 if (changelink && (conf->saddr.sa.sa_family != AF_INET))
3262 return -EOPNOTSUPP;
3263
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003264 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3265 conf->saddr.sa.sa_family = AF_INET;
3266 } else if (data[IFLA_VXLAN_LOCAL6]) {
3267 if (!IS_ENABLED(CONFIG_IPV6))
3268 return -EPFNOSUPPORT;
3269
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003270 if (changelink && (conf->saddr.sa.sa_family != AF_INET6))
3271 return -EOPNOTSUPP;
3272
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003273 /* TODO: respect scope id */
3274 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3275 conf->saddr.sa.sa_family = AF_INET6;
3276 }
3277
3278 if (data[IFLA_VXLAN_LINK])
3279 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3280
3281 if (data[IFLA_VXLAN_TOS])
3282 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3283
3284 if (data[IFLA_VXLAN_TTL])
3285 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3286
Hangbin Liu72f6d712018-04-17 14:11:28 +08003287 if (data[IFLA_VXLAN_TTL_INHERIT]) {
3288 if (changelink)
3289 return -EOPNOTSUPP;
3290 conf->flags |= VXLAN_F_TTL_INHERIT;
3291 }
3292
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003293 if (data[IFLA_VXLAN_LABEL])
3294 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3295 IPV6_FLOWLABEL_MASK;
3296
3297 if (data[IFLA_VXLAN_LEARNING]) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003298 if (nla_get_u8(data[IFLA_VXLAN_LEARNING]))
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003299 conf->flags |= VXLAN_F_LEARN;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003300 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003301 conf->flags &= ~VXLAN_F_LEARN;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003302 } else if (!changelink) {
3303 /* default to learn on a new device */
3304 conf->flags |= VXLAN_F_LEARN;
3305 }
3306
3307 if (data[IFLA_VXLAN_AGEING]) {
3308 if (changelink)
3309 return -EOPNOTSUPP;
3310 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3311 }
3312
3313 if (data[IFLA_VXLAN_PROXY]) {
3314 if (changelink)
3315 return -EOPNOTSUPP;
3316 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3317 conf->flags |= VXLAN_F_PROXY;
3318 }
3319
3320 if (data[IFLA_VXLAN_RSC]) {
3321 if (changelink)
3322 return -EOPNOTSUPP;
3323 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3324 conf->flags |= VXLAN_F_RSC;
3325 }
3326
3327 if (data[IFLA_VXLAN_L2MISS]) {
3328 if (changelink)
3329 return -EOPNOTSUPP;
3330 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3331 conf->flags |= VXLAN_F_L2MISS;
3332 }
3333
3334 if (data[IFLA_VXLAN_L3MISS]) {
3335 if (changelink)
3336 return -EOPNOTSUPP;
3337 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3338 conf->flags |= VXLAN_F_L3MISS;
3339 }
3340
3341 if (data[IFLA_VXLAN_LIMIT]) {
3342 if (changelink)
3343 return -EOPNOTSUPP;
3344 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3345 }
3346
3347 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3348 if (changelink)
3349 return -EOPNOTSUPP;
3350 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3351 conf->flags |= VXLAN_F_COLLECT_METADATA;
3352 }
3353
3354 if (data[IFLA_VXLAN_PORT_RANGE]) {
3355 if (!changelink) {
3356 const struct ifla_vxlan_port_range *p
3357 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3358 conf->port_min = ntohs(p->low);
3359 conf->port_max = ntohs(p->high);
3360 } else {
3361 return -EOPNOTSUPP;
3362 }
3363 }
3364
3365 if (data[IFLA_VXLAN_PORT]) {
3366 if (changelink)
3367 return -EOPNOTSUPP;
3368 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3369 }
3370
3371 if (data[IFLA_VXLAN_UDP_CSUM]) {
3372 if (changelink)
3373 return -EOPNOTSUPP;
3374 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3375 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3376 }
3377
3378 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3379 if (changelink)
3380 return -EOPNOTSUPP;
3381 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3382 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3383 }
3384
3385 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3386 if (changelink)
3387 return -EOPNOTSUPP;
3388 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3389 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3390 }
3391
3392 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3393 if (changelink)
3394 return -EOPNOTSUPP;
3395 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3396 conf->flags |= VXLAN_F_REMCSUM_TX;
3397 }
3398
3399 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3400 if (changelink)
3401 return -EOPNOTSUPP;
3402 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3403 conf->flags |= VXLAN_F_REMCSUM_RX;
3404 }
3405
3406 if (data[IFLA_VXLAN_GBP]) {
3407 if (changelink)
3408 return -EOPNOTSUPP;
3409 conf->flags |= VXLAN_F_GBP;
3410 }
3411
3412 if (data[IFLA_VXLAN_GPE]) {
3413 if (changelink)
3414 return -EOPNOTSUPP;
3415 conf->flags |= VXLAN_F_GPE;
3416 }
3417
3418 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3419 if (changelink)
3420 return -EOPNOTSUPP;
3421 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3422 }
3423
3424 if (tb[IFLA_MTU]) {
3425 if (changelink)
3426 return -EOPNOTSUPP;
3427 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3428 }
3429
3430 return 0;
3431}
3432
3433static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003434 struct nlattr *tb[], struct nlattr *data[],
3435 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003436{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003437 struct vxlan_config conf;
3438 int err;
3439
3440 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3441 if (err)
3442 return err;
3443
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003444 return __vxlan_dev_create(src_net, dev, &conf, extack);
stephen hemmingerd3428942012-10-01 12:32:35 +00003445}
3446
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003447static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02003448 struct nlattr *data[],
3449 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003450{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003451 struct vxlan_dev *vxlan = netdev_priv(dev);
3452 struct vxlan_rdst *dst = &vxlan->default_dst;
3453 struct vxlan_rdst old_dst;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003454 struct vxlan_config conf;
Roopa Prabhu0241b832018-07-04 16:46:32 -07003455 struct vxlan_fdb *f = NULL;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003456 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003457
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003458 err = vxlan_nl2conf(tb, data,
3459 dev, &conf, true);
3460 if (err)
3461 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07003462
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003463 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003464
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003465 err = vxlan_dev_configure(vxlan->net, dev, &conf, true, extack);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003466 if (err)
3467 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003468
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003469 /* handle default dst entry */
3470 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3471 spin_lock_bh(&vxlan->hash_lock);
3472 if (!vxlan_addr_any(&old_dst.remote_ip))
3473 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3474 old_dst.remote_ip,
3475 vxlan->cfg.dst_port,
3476 old_dst.remote_vni,
3477 old_dst.remote_vni,
3478 old_dst.remote_ifindex, 0);
3479
3480 if (!vxlan_addr_any(&dst->remote_ip)) {
Roopa Prabhu0241b832018-07-04 16:46:32 -07003481 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003482 &dst->remote_ip,
3483 NUD_REACHABLE | NUD_PERMANENT,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003484 vxlan->cfg.dst_port,
3485 dst->remote_vni,
3486 dst->remote_vni,
3487 dst->remote_ifindex,
Roopa Prabhu0241b832018-07-04 16:46:32 -07003488 NTF_SELF, &f);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003489 if (err) {
3490 spin_unlock_bh(&vxlan->hash_lock);
3491 return err;
3492 }
Roopa Prabhu0241b832018-07-04 16:46:32 -07003493 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH);
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003494 }
3495 spin_unlock_bh(&vxlan->hash_lock);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003496 }
3497
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003498 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003499}
3500
stephen hemmingerd3428942012-10-01 12:32:35 +00003501static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3502{
3503 struct vxlan_dev *vxlan = netdev_priv(dev);
3504
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003505 vxlan_flush(vxlan, true);
3506
Tom Herbert58ce31c2015-08-19 17:07:33 -07003507 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003508 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003509 unregister_netdevice_queue(dev, head);
3510}
3511
3512static size_t vxlan_get_size(const struct net_device *dev)
3513{
3514
3515 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003516 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003517 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003518 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003519 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
Hangbin Liu8fd78062018-09-26 10:35:42 +08003520 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
stephen hemmingerd3428942012-10-01 12:32:35 +00003521 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003522 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003523 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003524 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3525 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3526 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3527 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003528 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003529 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3530 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003531 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003532 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3533 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3534 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3535 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003536 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3537 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003538 0;
3539}
3540
3541static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3542{
3543 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003544 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003545 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003546 .low = htons(vxlan->cfg.port_min),
3547 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003548 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003549
Jiri Benc54bfd872016-02-16 21:58:58 +01003550 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003551 goto nla_put_failure;
3552
Cong Wange4c7ed42013-08-31 13:44:33 +08003553 if (!vxlan_addr_any(&dst->remote_ip)) {
3554 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003555 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3556 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003557 goto nla_put_failure;
3558#if IS_ENABLED(CONFIG_IPV6)
3559 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003560 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3561 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003562 goto nla_put_failure;
3563#endif
3564 }
3565 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003566
Atzm Watanabec7995c42013-04-16 02:50:52 +00003567 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003568 goto nla_put_failure;
3569
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003570 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3571 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003572 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003573 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003574 goto nla_put_failure;
3575#if IS_ENABLED(CONFIG_IPV6)
3576 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003577 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003578 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003579 goto nla_put_failure;
3580#endif
3581 }
3582 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003583
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003584 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
Hangbin Liu8fd78062018-09-26 10:35:42 +08003585 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
3586 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003587 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003588 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003589 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003590 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003591 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003592 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
3593 nla_put_u8(skb, IFLA_VXLAN_RSC,
3594 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003595 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003596 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003597 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003598 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003599 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003600 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003601 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3602 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3603 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003604 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003605 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003606 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003607 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003608 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003609 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003610 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003611 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003612 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003613 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003614 goto nla_put_failure;
3615
stephen hemminger05f47d62012-10-09 20:35:50 +00003616 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3617 goto nla_put_failure;
3618
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003619 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01003620 nla_put_flag(skb, IFLA_VXLAN_GBP))
3621 goto nla_put_failure;
3622
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003623 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02003624 nla_put_flag(skb, IFLA_VXLAN_GPE))
3625 goto nla_put_failure;
3626
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003627 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003628 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3629 goto nla_put_failure;
3630
stephen hemmingerd3428942012-10-01 12:32:35 +00003631 return 0;
3632
3633nla_put_failure:
3634 return -EMSGSIZE;
3635}
3636
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003637static struct net *vxlan_get_link_net(const struct net_device *dev)
3638{
3639 struct vxlan_dev *vxlan = netdev_priv(dev);
3640
3641 return vxlan->net;
3642}
3643
stephen hemmingerd3428942012-10-01 12:32:35 +00003644static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3645 .kind = "vxlan",
3646 .maxtype = IFLA_VXLAN_MAX,
3647 .policy = vxlan_policy,
3648 .priv_size = sizeof(struct vxlan_dev),
3649 .setup = vxlan_setup,
3650 .validate = vxlan_validate,
3651 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003652 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00003653 .dellink = vxlan_dellink,
3654 .get_size = vxlan_get_size,
3655 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003656 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003657};
3658
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003659struct net_device *vxlan_dev_create(struct net *net, const char *name,
3660 u8 name_assign_type,
3661 struct vxlan_config *conf)
3662{
3663 struct nlattr *tb[IFLA_MAX + 1];
3664 struct net_device *dev;
3665 int err;
3666
3667 memset(&tb, 0, sizeof(tb));
3668
3669 dev = rtnl_create_link(net, name, name_assign_type,
3670 &vxlan_link_ops, tb);
3671 if (IS_ERR(dev))
3672 return dev;
3673
Girish Moodalbail653ef6a32017-08-11 15:20:59 -07003674 err = __vxlan_dev_create(net, dev, conf, NULL);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003675 if (err < 0) {
3676 free_netdev(dev);
3677 return ERR_PTR(err);
3678 }
3679
3680 err = rtnl_configure_link(dev, NULL);
3681 if (err < 0) {
3682 LIST_HEAD(list_kill);
3683
3684 vxlan_dellink(dev, &list_kill);
3685 unregister_netdevice_many(&list_kill);
3686 return ERR_PTR(err);
3687 }
3688
3689 return dev;
3690}
3691EXPORT_SYMBOL_GPL(vxlan_dev_create);
3692
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003693static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3694 struct net_device *dev)
3695{
3696 struct vxlan_dev *vxlan, *next;
3697 LIST_HEAD(list_kill);
3698
3699 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3700 struct vxlan_rdst *dst = &vxlan->default_dst;
3701
3702 /* In case we created vxlan device with carrier
3703 * and we loose the carrier due to module unload
3704 * we also need to remove vxlan device. In other
3705 * cases, it's not necessary and remote_ifindex
3706 * is 0 here, so no matches.
3707 */
3708 if (dst->remote_ifindex == dev->ifindex)
3709 vxlan_dellink(vxlan->dev, &list_kill);
3710 }
3711
3712 unregister_netdevice_many(&list_kill);
3713}
3714
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003715static int vxlan_netdevice_event(struct notifier_block *unused,
3716 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003717{
3718 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003719 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003720
Sabrina Dubroca04584952017-07-21 12:49:33 +02003721 if (event == NETDEV_UNREGISTER) {
3722 vxlan_offload_rx_ports(dev, false);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003723 vxlan_handle_lowerdev_unregister(vn, dev);
Sabrina Dubroca04584952017-07-21 12:49:33 +02003724 } else if (event == NETDEV_REGISTER) {
3725 vxlan_offload_rx_ports(dev, true);
3726 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
3727 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02003728 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02003729 }
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003730
3731 return NOTIFY_DONE;
3732}
3733
3734static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003735 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003736};
3737
stephen hemmingerd3428942012-10-01 12:32:35 +00003738static __net_init int vxlan_init_net(struct net *net)
3739{
3740 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003741 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003742
stephen hemminger553675f2013-05-16 11:35:20 +00003743 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003744 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003745
stephen hemminger553675f2013-05-16 11:35:20 +00003746 for (h = 0; h < PORT_HASH_SIZE; ++h)
3747 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003748
3749 return 0;
3750}
3751
Haishuang Yan57b61122017-12-16 17:54:49 +08003752static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003753{
3754 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3755 struct vxlan_dev *vxlan, *next;
3756 struct net_device *dev, *aux;
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03003757 unsigned int h;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003758
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003759 for_each_netdev_safe(net, dev, aux)
3760 if (dev->rtnl_link_ops == &vxlan_link_ops)
Haishuang Yan57b61122017-12-16 17:54:49 +08003761 unregister_netdevice_queue(dev, head);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003762
3763 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3764 /* If vxlan->dev is in the same netns, it has already been added
3765 * to the list by the previous loop.
3766 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003767 if (!net_eq(dev_net(vxlan->dev), net)) {
3768 gro_cells_destroy(&vxlan->gro_cells);
Haishuang Yan57b61122017-12-16 17:54:49 +08003769 unregister_netdevice_queue(vxlan->dev, head);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003770 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003771 }
3772
Vasily Averin0e4ec5a2017-11-12 22:28:10 +03003773 for (h = 0; h < PORT_HASH_SIZE; ++h)
3774 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003775}
3776
Haishuang Yan57b61122017-12-16 17:54:49 +08003777static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
3778{
3779 struct net *net;
3780 LIST_HEAD(list);
3781
3782 rtnl_lock();
3783 list_for_each_entry(net, net_list, exit_list)
3784 vxlan_destroy_tunnels(net, &list);
3785
3786 unregister_netdevice_many(&list);
3787 rtnl_unlock();
3788}
3789
stephen hemmingerd3428942012-10-01 12:32:35 +00003790static struct pernet_operations vxlan_net_ops = {
3791 .init = vxlan_init_net,
Haishuang Yan57b61122017-12-16 17:54:49 +08003792 .exit_batch = vxlan_exit_batch_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003793 .id = &vxlan_net_id,
3794 .size = sizeof(struct vxlan_net),
3795};
3796
3797static int __init vxlan_init_module(void)
3798{
3799 int rc;
3800
3801 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3802
Daniel Borkmann783c1462014-01-22 21:07:53 +01003803 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003804 if (rc)
3805 goto out1;
3806
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003807 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003808 if (rc)
3809 goto out2;
3810
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003811 rc = rtnl_link_register(&vxlan_link_ops);
3812 if (rc)
3813 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003814
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003815 return 0;
3816out3:
3817 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003818out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003819 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003820out1:
3821 return rc;
3822}
Cong Wang7332a132013-05-27 22:35:53 +00003823late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003824
3825static void __exit vxlan_cleanup_module(void)
3826{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003827 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003828 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003829 unregister_pernet_subsys(&vxlan_net_ops);
3830 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003831}
3832module_exit(vxlan_cleanup_module);
3833
3834MODULE_LICENSE("GPL");
3835MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003836MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003837MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003838MODULE_ALIAS_RTNL_LINK("vxlan");