blob: e17baac70f439f86c723732cc0298eaa5eee15de [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>
Pravin B Shelar012a5722013-08-19 11:23:07 -070029#include <net/vxlan.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080030
Cong Wange4c7ed42013-08-31 13:44:33 +080031#if IS_ENABLED(CONFIG_IPV6)
Cong Wange4c7ed42013-08-31 13:44:33 +080032#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080033#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080034#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000035
36#define VXLAN_VERSION "0.1"
37
stephen hemminger553675f2013-05-16 11:35:20 +000038#define PORT_HASH_BITS 8
39#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000040#define FDB_AGE_DEFAULT 300 /* 5 min */
41#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
42
stephen hemminger23c578b2013-04-27 11:31:53 +000043/* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070045 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000046 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070047static unsigned short vxlan_port __read_mostly = 8472;
48module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000049MODULE_PARM_DESC(udp_port, "Destination UDP port");
50
51static bool log_ecn_error = true;
52module_param(log_ecn_error, bool, 0644);
53MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
54
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030055static unsigned int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020056static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000057
Li RongQing7256eac2016-01-29 09:43:47 +080058static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8bae2013-06-25 16:01:51 +030059
Jiri Benc205f3562015-09-24 13:50:01 +020060static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020061
Mark Blocha53cb292017-06-02 03:24:08 +030062static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
63
stephen hemminger553675f2013-05-16 11:35:20 +000064/* per-network namespace private data for this module */
65struct vxlan_net {
66 struct list_head vxlan_list;
67 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070068 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000069};
70
stephen hemmingerd3428942012-10-01 12:32:35 +000071/* Forwarding table entry */
72struct vxlan_fdb {
73 struct hlist_node hlist; /* linked list of entries */
74 struct rcu_head rcu;
75 unsigned long updated; /* jiffies */
76 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070077 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020078 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000079 u16 state; /* see ndm_state */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -080080 __be32 vni;
David Stevensae884082013-04-19 00:36:26 +000081 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +000082};
83
stephen hemmingerd3428942012-10-01 12:32:35 +000084/* salt for hash table */
85static u32 vxlan_salt __read_mostly;
86
Thomas Grafee122c72015-07-21 10:43:58 +020087static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
88{
Thomas Grafe7030872015-07-21 10:44:01 +020089 return vs->flags & VXLAN_F_COLLECT_METADATA ||
90 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020091}
92
Cong Wange4c7ed42013-08-31 13:44:33 +080093#if IS_ENABLED(CONFIG_IPV6)
94static inline
95bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
96{
Jiri Bencf0ef3122015-03-29 16:17:37 +020097 if (a->sa.sa_family != b->sa.sa_family)
98 return false;
99 if (a->sa.sa_family == AF_INET6)
100 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
101 else
102 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800103}
104
105static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
106{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200107 if (ipa->sa.sa_family == AF_INET6)
108 return ipv6_addr_any(&ipa->sin6.sin6_addr);
109 else
110 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800111}
112
113static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
114{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200115 if (ipa->sa.sa_family == AF_INET6)
116 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
117 else
118 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800119}
120
121static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
122{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200123 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200124 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200125 ip->sa.sa_family = AF_INET6;
126 return 0;
127 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200128 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200129 ip->sa.sa_family = AF_INET;
130 return 0;
131 } else {
132 return -EAFNOSUPPORT;
133 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800134}
135
136static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200137 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800138{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200139 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200140 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200141 else
Jiri Benc930345e2015-03-29 16:59:25 +0200142 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800143}
144
145#else /* !CONFIG_IPV6 */
146
147static inline
148bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
149{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200150 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800151}
152
153static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
154{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200155 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800156}
157
158static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
159{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200160 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800161}
162
163static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
164{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200165 if (nla_len(nla) >= sizeof(struct in6_addr)) {
166 return -EAFNOSUPPORT;
167 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200168 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200169 ip->sa.sa_family = AF_INET;
170 return 0;
171 } else {
172 return -EAFNOSUPPORT;
173 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800174}
175
176static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200177 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800178{
Jiri Benc930345e2015-03-29 16:59:25 +0200179 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800180}
181#endif
182
stephen hemminger553675f2013-05-16 11:35:20 +0000183/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100184static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000185{
Jiri Benc54bfd872016-02-16 21:58:58 +0100186 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000187}
188
189/* Socket hash table head */
190static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000191{
192 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
193
stephen hemminger553675f2013-05-16 11:35:20 +0000194 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
195}
196
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700197/* First remote destination for a forwarding entry.
198 * Guaranteed to be non-NULL because remotes are never deleted.
199 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700200static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700201{
stephen hemminger5ca54612013-08-04 17:17:39 -0700202 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
203}
204
205static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
206{
207 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700208}
209
Thomas Grafac5132d2015-01-15 03:53:56 +0100210/* Find VXLAN socket based on network namespace, address family and UDP port
211 * and enabled unshareable flags.
212 */
213static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
214 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000215{
216 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800217
218 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000219
220 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200221 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200222 vxlan_get_sk_family(vs) == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800223 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000224 return vs;
225 }
226 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000227}
228
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200229static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
230 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000231{
Jiri Benc69e76662017-07-02 19:00:57 +0200232 struct vxlan_dev_node *node;
stephen hemmingerd3428942012-10-01 12:32:35 +0000233
Jiri Bencb9167b22016-02-16 21:59:03 +0100234 /* For flow based devices, map all packets to VNI 0 */
235 if (vs->flags & VXLAN_F_COLLECT_METADATA)
236 vni = 0;
237
Jiri Benc69e76662017-07-02 19:00:57 +0200238 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
239 if (node->vxlan->default_dst.remote_vni != vni)
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200240 continue;
241
242 if (IS_ENABLED(CONFIG_IPV6)) {
Jiri Benc69e76662017-07-02 19:00:57 +0200243 const struct vxlan_config *cfg = &node->vxlan->cfg;
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200244
245 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
246 cfg->remote_ifindex != ifindex)
247 continue;
248 }
249
Jiri Benc69e76662017-07-02 19:00:57 +0200250 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000251 }
252
253 return NULL;
254}
255
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700256/* Look up VNI in a per net namespace table */
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200257static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
258 __be32 vni, sa_family_t family,
259 __be16 port, u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700260{
261 struct vxlan_sock *vs;
262
Thomas Grafac5132d2015-01-15 03:53:56 +0100263 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700264 if (!vs)
265 return NULL;
266
Matthias Schiffer49f810f2017-06-19 10:04:00 +0200267 return vxlan_vs_find_vni(vs, ifindex, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700268}
269
stephen hemmingerd3428942012-10-01 12:32:35 +0000270/* Fill in neighbour message in skbuff. */
271static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700272 const struct vxlan_fdb *fdb,
273 u32 portid, u32 seq, int type, unsigned int flags,
274 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000275{
276 unsigned long now = jiffies;
277 struct nda_cacheinfo ci;
278 struct nlmsghdr *nlh;
279 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000280 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000281
282 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
283 if (nlh == NULL)
284 return -EMSGSIZE;
285
286 ndm = nlmsg_data(nlh);
287 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000288
289 send_eth = send_ip = true;
290
291 if (type == RTM_GETNEIGH) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800292 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000293 send_eth = !is_zero_ether_addr(fdb->eth_addr);
Vincent Bernat8f48ba72017-03-10 16:30:24 +0100294 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
David Stevense4f67ad2012-11-20 02:50:14 +0000295 } else
296 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000297 ndm->ndm_state = fdb->state;
298 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000299 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800300 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000301
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100302 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100303 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700304 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100305 goto nla_put_failure;
306
David Stevense4f67ad2012-11-20 02:50:14 +0000307 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000308 goto nla_put_failure;
309
Cong Wange4c7ed42013-08-31 13:44:33 +0800310 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000311 goto nla_put_failure;
312
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200313 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000314 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
315 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000316 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100317 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000318 goto nla_put_failure;
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200319 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800320 nla_put_u32(skb, NDA_SRC_VNI,
321 be32_to_cpu(fdb->vni)))
322 goto nla_put_failure;
David Stevens66817122013-03-15 04:35:51 +0000323 if (rdst->remote_ifindex &&
324 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000325 goto nla_put_failure;
326
327 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
328 ci.ndm_confirmed = 0;
329 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
330 ci.ndm_refcnt = 0;
331
332 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
333 goto nla_put_failure;
334
Johannes Berg053c0952015-01-16 22:09:00 +0100335 nlmsg_end(skb, nlh);
336 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000337
338nla_put_failure:
339 nlmsg_cancel(skb, nlh);
340 return -EMSGSIZE;
341}
342
343static inline size_t vxlan_nlmsg_size(void)
344{
345 return NLMSG_ALIGN(sizeof(struct ndmsg))
346 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800347 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf33172013-04-27 11:31:54 +0000348 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000349 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
350 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100351 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000352 + nla_total_size(sizeof(struct nda_cacheinfo));
353}
354
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200355static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
356 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000357{
358 struct net *net = dev_net(vxlan->dev);
359 struct sk_buff *skb;
360 int err = -ENOBUFS;
361
362 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
363 if (skb == NULL)
364 goto errout;
365
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200366 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000367 if (err < 0) {
368 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
369 WARN_ON(err == -EMSGSIZE);
370 kfree_skb(skb);
371 goto errout;
372 }
373
374 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
375 return;
376errout:
377 if (err < 0)
378 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
379}
380
Cong Wange4c7ed42013-08-31 13:44:33 +0800381static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000382{
383 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700384 struct vxlan_fdb f = {
385 .state = NUD_STALE,
386 };
387 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800388 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100389 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700390 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700391
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200392 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000393}
394
395static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
396{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700397 struct vxlan_fdb f = {
398 .state = NUD_STALE,
399 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200400 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000401
David Stevense4f67ad2012-11-20 02:50:14 +0000402 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
403
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200404 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000405}
406
stephen hemmingerd3428942012-10-01 12:32:35 +0000407/* Hash Ethernet address */
408static u32 eth_hash(const unsigned char *addr)
409{
410 u64 value = get_unaligned((u64 *)addr);
411
412 /* only want 6 bytes */
413#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000414 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000415#else
416 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000417#endif
418 return hash_64(value, FDB_HASH_BITS);
419}
420
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800421static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
422{
423 /* use 1 byte of OUI and 3 bytes of NIC */
424 u32 key = get_unaligned((u32 *)(addr + 2));
425
426 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
427}
428
stephen hemmingerd3428942012-10-01 12:32:35 +0000429/* Hash chain to use given mac address */
430static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800431 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000432{
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200433 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800434 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
435 else
436 return &vxlan->fdb_head[eth_hash(mac)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000437}
438
439/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000440static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800441 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000442{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800443 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000444 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000445
Sasha Levinb67bfe02013-02-27 17:06:00 -0800446 hlist_for_each_entry_rcu(f, head, hlist) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800447 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200448 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800449 if (vni == f->vni)
450 return f;
451 } else {
452 return f;
453 }
454 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000455 }
456
457 return NULL;
458}
459
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000460static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800461 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000462{
463 struct vxlan_fdb *f;
464
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800465 f = __vxlan_find_mac(vxlan, mac, vni);
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000466 if (f)
467 f->used = jiffies;
468
469 return f;
470}
471
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300472/* caller should hold vxlan->hash_lock */
473static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800474 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100475 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300476{
477 struct vxlan_rdst *rd;
478
479 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800480 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300481 rd->remote_port == port &&
482 rd->remote_vni == vni &&
483 rd->remote_ifindex == ifindex)
484 return rd;
485 }
486
487 return NULL;
488}
489
Thomas Richter906dc182013-07-19 17:20:07 +0200490/* Replace destination of unicast mac */
491static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100492 union vxlan_addr *ip, __be16 port, __be32 vni,
493 __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200494{
495 struct vxlan_rdst *rd;
496
497 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
498 if (rd)
499 return 0;
500
501 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
502 if (!rd)
503 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100504
505 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800506 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200507 rd->remote_port = port;
508 rd->remote_vni = vni;
509 rd->remote_ifindex = ifindex;
510 return 1;
511}
512
David Stevens66817122013-03-15 04:35:51 +0000513/* Add/update destinations for multicast */
514static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100515 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200516 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000517{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700518 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000519
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300520 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
521 if (rd)
522 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700523
David Stevens66817122013-03-15 04:35:51 +0000524 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
525 if (rd == NULL)
526 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100527
528 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
529 kfree(rd);
530 return -ENOBUFS;
531 }
532
Cong Wange4c7ed42013-08-31 13:44:33 +0800533 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000534 rd->remote_port = port;
535 rd->remote_vni = vni;
536 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700537
538 list_add_tail_rcu(&rd->list, &f->remotes);
539
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200540 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000541 return 1;
542}
543
Tom Herbertdfd86452015-01-12 17:00:38 -0800544static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
545 unsigned int off,
546 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100547 __be32 vni_field,
548 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800549 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800550{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700551 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800552
553 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700554 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800555
556 if (!NAPI_GRO_CB(skb)->csum_valid)
557 return NULL;
558
Jiri Benc54bfd872016-02-16 21:58:58 +0100559 start = vxlan_rco_start(vni_field);
560 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800561
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700562 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
563 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800564
565 skb->remcsum_offload = 1;
566
567 return vh;
568}
569
Tom Herbert5602c482016-04-05 08:22:53 -0700570static struct sk_buff **vxlan_gro_receive(struct sock *sk,
571 struct sk_buff **head,
572 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200573{
574 struct sk_buff *p, **pp = NULL;
575 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800576 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200577 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700578 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100579 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800580 struct gro_remcsum grc;
581
582 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200583
584 off_vx = skb_gro_offset(skb);
585 hlen = off_vx + sizeof(*vh);
586 vh = skb_gro_header_fast(skb, off_vx);
587 if (skb_gro_header_hard(skb, hlen)) {
588 vh = skb_gro_header_slow(skb, hlen, off_vx);
589 if (unlikely(!vh))
590 goto out;
591 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200592
Tom Herbertdfd86452015-01-12 17:00:38 -0800593 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
594
Jiri Benc54bfd872016-02-16 21:58:58 +0100595 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800596
597 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
598 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100599 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800600 !!(vs->flags &
601 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800602
603 if (!vh)
604 goto out;
605 }
606
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700607 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
608
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200609 for (p = *head; p; p = p->next) {
610 if (!NAPI_GRO_CB(p)->same_flow)
611 continue;
612
613 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100614 if (vh->vx_flags != vh2->vx_flags ||
615 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200616 NAPI_GRO_CB(p)->same_flow = 0;
617 continue;
618 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200619 }
620
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200621 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800622 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200623
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200624out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800625 skb_gro_remcsum_cleanup(skb, &grc);
K. Denbe73b302017-08-01 01:05:20 +0900626 skb->remcsum_offload = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200627 NAPI_GRO_CB(skb)->flush |= flush;
628
629 return pp;
630}
631
Tom Herbert5602c482016-04-05 08:22:53 -0700632static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200633{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700634 /* Sets 'skb->inner_mac_header' since we are always called with
635 * 'skb->encapsulation' set.
636 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800637 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200638}
639
stephen hemmingerd3428942012-10-01 12:32:35 +0000640/* Add new entry to forwarding table -- assumes lock held */
641static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800642 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000643 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800644 __be16 port, __be32 src_vni, __be32 vni,
645 __u32 ifindex, __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000646{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200647 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000648 struct vxlan_fdb *f;
649 int notify = 0;
Haishuang Yan17b46362016-11-29 09:59:36 +0800650 int rc;
stephen hemmingerd3428942012-10-01 12:32:35 +0000651
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800652 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000653 if (f) {
654 if (flags & NLM_F_EXCL) {
655 netdev_dbg(vxlan->dev,
656 "lost race to create %pM\n", mac);
657 return -EEXIST;
658 }
659 if (f->state != state) {
660 f->state = state;
661 f->updated = jiffies;
662 notify = 1;
663 }
David Stevensae884082013-04-19 00:36:26 +0000664 if (f->flags != ndm_flags) {
665 f->flags = ndm_flags;
666 f->updated = jiffies;
667 notify = 1;
668 }
Thomas Richter906dc182013-07-19 17:20:07 +0200669 if ((flags & NLM_F_REPLACE)) {
670 /* Only change unicasts */
671 if (!(is_multicast_ether_addr(f->eth_addr) ||
672 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800673 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200674 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200675 } else
676 return -EOPNOTSUPP;
677 }
David Stevens66817122013-03-15 04:35:51 +0000678 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300679 (is_multicast_ether_addr(f->eth_addr) ||
680 is_zero_ether_addr(f->eth_addr))) {
Haishuang Yan17b46362016-11-29 09:59:36 +0800681 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
David Stevens66817122013-03-15 04:35:51 +0000682
683 if (rc < 0)
684 return rc;
685 notify |= rc;
686 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000687 } else {
688 if (!(flags & NLM_F_CREATE))
689 return -ENOENT;
690
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200691 if (vxlan->cfg.addrmax &&
692 vxlan->addrcnt >= vxlan->cfg.addrmax)
stephen hemmingerd3428942012-10-01 12:32:35 +0000693 return -ENOSPC;
694
Thomas Richter906dc182013-07-19 17:20:07 +0200695 /* Disallow replace to add a multicast entry */
696 if ((flags & NLM_F_REPLACE) &&
697 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
698 return -EOPNOTSUPP;
699
Cong Wange4c7ed42013-08-31 13:44:33 +0800700 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000701 f = kmalloc(sizeof(*f), GFP_ATOMIC);
702 if (!f)
703 return -ENOMEM;
704
705 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000706 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000707 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000708 f->updated = f->used = jiffies;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800709 f->vni = src_vni;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700710 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000711 memcpy(f->eth_addr, mac, ETH_ALEN);
712
Haishuang Yan17b46362016-11-29 09:59:36 +0800713 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
714 if (rc < 0) {
715 kfree(f);
716 return rc;
717 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700718
stephen hemmingerd3428942012-10-01 12:32:35 +0000719 ++vxlan->addrcnt;
720 hlist_add_head_rcu(&f->hlist,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800721 vxlan_fdb_head(vxlan, mac, src_vni));
stephen hemmingerd3428942012-10-01 12:32:35 +0000722 }
723
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200724 if (notify) {
725 if (rd == NULL)
726 rd = first_remote_rtnl(f);
727 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
728 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000729
730 return 0;
731}
732
Wei Yongjun6706c822013-04-11 19:00:35 +0000733static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000734{
735 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700736 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000737
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100738 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
739 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000740 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100741 }
David Stevens66817122013-03-15 04:35:51 +0000742 kfree(f);
743}
744
stephen hemmingerd3428942012-10-01 12:32:35 +0000745static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
746{
747 netdev_dbg(vxlan->dev,
748 "delete %pM\n", f->eth_addr);
749
750 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200751 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000752
753 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000754 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000755}
756
Lance Richardson35cf2842017-05-29 13:25:57 -0400757static void vxlan_dst_free(struct rcu_head *head)
758{
759 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
760
761 dst_cache_destroy(&rd->dst_cache);
762 kfree(rd);
763}
764
765static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
766 struct vxlan_rdst *rd)
767{
768 list_del_rcu(&rd->list);
769 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
770 call_rcu(&rd->rcu, vxlan_dst_free);
771}
772
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300773static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800774 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
775 __be32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300776{
777 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800778 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300779
780 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800781 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
782 if (err)
783 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300784 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800785 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
786 if (remote->sa.sa_family == AF_INET) {
787 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
788 ip->sa.sa_family = AF_INET;
789#if IS_ENABLED(CONFIG_IPV6)
790 } else {
791 ip->sin6.sin6_addr = in6addr_any;
792 ip->sa.sa_family = AF_INET6;
793#endif
794 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300795 }
796
797 if (tb[NDA_PORT]) {
798 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
799 return -EINVAL;
800 *port = nla_get_be16(tb[NDA_PORT]);
801 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200802 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300803 }
804
805 if (tb[NDA_VNI]) {
806 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
807 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100808 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300809 } else {
810 *vni = vxlan->default_dst.remote_vni;
811 }
812
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800813 if (tb[NDA_SRC_VNI]) {
814 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
815 return -EINVAL;
816 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
817 } else {
818 *src_vni = vxlan->default_dst.remote_vni;
819 }
820
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300821 if (tb[NDA_IFINDEX]) {
822 struct net_device *tdev;
823
824 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
825 return -EINVAL;
826 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800827 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300828 if (!tdev)
829 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300830 } else {
831 *ifindex = 0;
832 }
833
834 return 0;
835}
836
stephen hemmingerd3428942012-10-01 12:32:35 +0000837/* Add static entry (via netlink) */
838static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
839 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100840 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000841{
842 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300843 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800844 union vxlan_addr ip;
stephen hemminger73cf33172013-04-27 11:31:54 +0000845 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800846 __be32 src_vni, vni;
Jiri Benc54bfd872016-02-16 21:58:58 +0100847 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000848 int err;
849
850 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
851 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
852 ndm->ndm_state);
853 return -EINVAL;
854 }
855
856 if (tb[NDA_DST] == NULL)
857 return -EINVAL;
858
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800859 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300860 if (err)
861 return err;
David Stevens66817122013-03-15 04:35:51 +0000862
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300863 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
864 return -EAFNOSUPPORT;
865
stephen hemmingerd3428942012-10-01 12:32:35 +0000866 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800867 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800868 port, src_vni, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000869 spin_unlock_bh(&vxlan->hash_lock);
870
871 return err;
872}
873
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800874static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
875 const unsigned char *addr, union vxlan_addr ip,
876 __be16 port, __be32 src_vni, u32 vni, u32 ifindex,
877 u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000878{
stephen hemmingerd3428942012-10-01 12:32:35 +0000879 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300880 struct vxlan_rdst *rd = NULL;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800881 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300882
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800883 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300884 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800885 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300886
Cong Wange4c7ed42013-08-31 13:44:33 +0800887 if (!vxlan_addr_any(&ip)) {
888 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300889 if (!rd)
890 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000891 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300892
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300893 /* remove a destination if it's not the only one on the list,
894 * otherwise destroy the fdb entry
895 */
896 if (rd && !list_is_singular(&f->remotes)) {
Lance Richardson35cf2842017-05-29 13:25:57 -0400897 vxlan_fdb_dst_destroy(vxlan, f, rd);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300898 goto out;
899 }
900
901 vxlan_fdb_destroy(vxlan, f);
902
903out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800904 return 0;
905}
906
907/* Delete entry (via netlink) */
908static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
909 struct net_device *dev,
910 const unsigned char *addr, u16 vid)
911{
912 struct vxlan_dev *vxlan = netdev_priv(dev);
913 union vxlan_addr ip;
914 __be32 src_vni, vni;
915 __be16 port;
916 u32 ifindex;
917 int err;
918
919 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
920 if (err)
921 return err;
922
923 spin_lock_bh(&vxlan->hash_lock);
924 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
925 vid);
stephen hemmingerd3428942012-10-01 12:32:35 +0000926 spin_unlock_bh(&vxlan->hash_lock);
927
928 return err;
929}
930
931/* Dump forwarding table */
932static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400933 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700934 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000935{
936 struct vxlan_dev *vxlan = netdev_priv(dev);
937 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -0700938 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000939
940 for (h = 0; h < FDB_HASH_SIZE; ++h) {
941 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000942
Sasha Levinb67bfe02013-02-27 17:06:00 -0800943 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000944 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000945
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700946 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -0700947 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900948 goto skip;
949
David Stevens66817122013-03-15 04:35:51 +0000950 err = vxlan_fdb_info(skb, vxlan, f,
951 NETLINK_CB(cb->skb).portid,
952 cb->nlh->nlmsg_seq,
953 RTM_NEWNEIGH,
954 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -0700955 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700956 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700957skip:
Roopa Prabhud2976532016-08-30 21:56:45 -0700958 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900959 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000960 }
961 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700962out:
Roopa Prabhud2976532016-08-30 21:56:45 -0700963 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +0000964}
965
966/* Watch incoming packets to learn mapping between Ethernet address
967 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900968 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000969 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700970static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800971 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +0200972 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000973{
974 struct vxlan_dev *vxlan = netdev_priv(dev);
975 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +0200976 u32 ifindex = 0;
977
978#if IS_ENABLED(CONFIG_IPV6)
979 if (src_ip->sa.sa_family == AF_INET6 &&
980 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
981 ifindex = src_ifindex;
982#endif
stephen hemmingerd3428942012-10-01 12:32:35 +0000983
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800984 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000985 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700986 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700987
Matthias Schiffer87613de2017-06-19 10:03:59 +0200988 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
989 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700990 return false;
991
992 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -0700993 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700994 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000995
996 if (net_ratelimit())
997 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800998 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +0100999 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001000
Cong Wange4c7ed42013-08-31 13:44:33 +08001001 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001002 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001003 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001004 } else {
1005 /* learned new entry */
1006 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001007
1008 /* close off race between vxlan_flush and incoming packets */
1009 if (netif_running(dev))
1010 vxlan_fdb_create(vxlan, src_mac, src_ip,
1011 NUD_REACHABLE,
1012 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001013 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001014 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001015 vxlan->default_dst.remote_vni,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001016 ifindex, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001017 spin_unlock(&vxlan->hash_lock);
1018 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001019
1020 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001021}
1022
stephen hemmingerd3428942012-10-01 12:32:35 +00001023/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001024static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001025{
stephen hemminger553675f2013-05-16 11:35:20 +00001026 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001027 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001028#if IS_ENABLED(CONFIG_IPV6)
1029 struct vxlan_sock *sock6;
1030#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001031 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001032
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001033 sock4 = rtnl_dereference(dev->vn4_sock);
1034
Gao feng95ab0992013-12-10 16:37:33 +08001035 /* The vxlan_sock is only used by dev, leaving group has
1036 * no effect on other vxlan devices.
1037 */
Reshetova, Elena66af8462017-07-04 15:52:59 +03001038 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001039 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001040#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001041 sock6 = rtnl_dereference(dev->vn6_sock);
Reshetova, Elena66af8462017-07-04 15:52:59 +03001042 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001043 return false;
1044#endif
Gao feng95ab0992013-12-10 16:37:33 +08001045
stephen hemminger553675f2013-05-16 11:35:20 +00001046 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001047 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001048 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001049
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001050 if (family == AF_INET &&
1051 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001052 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001053#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001054 if (family == AF_INET6 &&
1055 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001056 continue;
1057#endif
Gao feng95ab0992013-12-10 16:37:33 +08001058
1059 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1060 &dev->default_dst.remote_ip))
1061 continue;
1062
1063 if (vxlan->default_dst.remote_ifindex !=
1064 dev->default_dst.remote_ifindex)
1065 continue;
1066
1067 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001068 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001069
1070 return false;
1071}
1072
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001073static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001074{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001075 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001076
Jiri Bencb1be00a2015-09-24 13:50:02 +02001077 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001078 return false;
Reshetova, Elena66af8462017-07-04 15:52:59 +03001079 if (!refcount_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001080 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001081
Jiri Bencb1be00a2015-09-24 13:50:02 +02001082 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001083 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001084 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001085 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001086 (vs->flags & VXLAN_F_GPE) ?
1087 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001088 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001089 spin_unlock(&vn->sock_lock);
1090
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001091 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001092}
1093
Jiri Bencb1be00a2015-09-24 13:50:02 +02001094static void vxlan_sock_release(struct vxlan_dev *vxlan)
1095{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001096 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001097#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001098 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1099
Mark Bloch57d88182017-06-07 14:36:58 +03001100 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001101#endif
1102
Mark Bloch57d88182017-06-07 14:36:58 +03001103 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001104 synchronize_net();
1105
Mark Blocha53cb292017-06-02 03:24:08 +03001106 vxlan_vs_del_dev(vxlan);
1107
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001108 if (__vxlan_sock_release_prep(sock4)) {
1109 udp_tunnel_sock_release(sock4->sock);
1110 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001111 }
1112
1113#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001114 if (__vxlan_sock_release_prep(sock6)) {
1115 udp_tunnel_sock_release(sock6->sock);
1116 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001117 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001118#endif
1119}
1120
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001121/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001122 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001123 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001124static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001125{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001126 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001127 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1128 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001129 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001130
Cong Wange4c7ed42013-08-31 13:44:33 +08001131 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001132 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001133 struct ip_mreqn mreq = {
1134 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1135 .imr_ifindex = ifindex,
1136 };
1137
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001138 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001139 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001140 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001141 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001142#if IS_ENABLED(CONFIG_IPV6)
1143 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001144 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1145
1146 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001147 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001148 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1149 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001150 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001151#endif
1152 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001153
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001154 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001155}
1156
1157/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001158static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001159{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001160 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001161 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1162 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001163 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001164
Cong Wange4c7ed42013-08-31 13:44:33 +08001165 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001166 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001167 struct ip_mreqn mreq = {
1168 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1169 .imr_ifindex = ifindex,
1170 };
1171
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001172 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001173 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001174 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001175 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001176#if IS_ENABLED(CONFIG_IPV6)
1177 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001178 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1179
1180 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001181 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001182 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1183 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001184 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001185#endif
1186 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001187
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001188 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001189}
1190
Jiri Bencf14eceb2016-02-16 21:59:01 +01001191static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1192 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001193{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001194 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001195
Jiri Bencf14eceb2016-02-16 21:59:01 +01001196 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1197 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001198
Jiri Bencf14eceb2016-02-16 21:59:01 +01001199 start = vxlan_rco_start(unparsed->vx_vni);
1200 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001201
Jiri Benc7d34fa72016-03-21 17:50:05 +01001202 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001203 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001204
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001205 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1206 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001207out:
1208 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1209 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001210 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001211}
1212
Jiri Bencf14eceb2016-02-16 21:59:01 +01001213static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001214 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001215 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001216{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001217 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001218 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001219
Jiri Bencf14eceb2016-02-16 21:59:01 +01001220 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1221 goto out;
1222
Jiri Benc3288af02016-02-16 21:59:00 +01001223 md->gbp = ntohs(gbp->policy_id);
1224
Jiri Benc10a5af22016-02-23 18:02:59 +01001225 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001226 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001227 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001228 tun_dst->u.tun_info.options_len = sizeof(*md);
1229 }
Jiri Benc3288af02016-02-16 21:59:00 +01001230 if (gbp->dont_learn)
1231 md->gbp |= VXLAN_GBP_DONT_LEARN;
1232
1233 if (gbp->policy_applied)
1234 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001235
Jiri Benc64f87d32016-02-23 18:02:55 +01001236 /* In flow-based mode, GBP is carried in dst_metadata */
1237 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1238 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001239out:
1240 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001241}
1242
Jiri Bence1e53142016-04-05 14:47:13 +02001243static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001244 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001245 struct sk_buff *skb, u32 vxflags)
1246{
1247 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1248
1249 /* Need to have Next Protocol set for interfaces in GPE mode. */
1250 if (!gpe->np_applied)
1251 return false;
1252 /* "The initial version is 0. If a receiver does not support the
1253 * version indicated it MUST drop the packet.
1254 */
1255 if (gpe->version != 0)
1256 return false;
1257 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1258 * processing MUST occur." However, we don't implement OAM
1259 * processing, thus drop the packet.
1260 */
1261 if (gpe->oam_flag)
1262 return false;
1263
1264 switch (gpe->next_protocol) {
1265 case VXLAN_GPE_NP_IPV4:
1266 *protocol = htons(ETH_P_IP);
1267 break;
1268 case VXLAN_GPE_NP_IPV6:
1269 *protocol = htons(ETH_P_IPV6);
1270 break;
1271 case VXLAN_GPE_NP_ETHERNET:
1272 *protocol = htons(ETH_P_TEB);
1273 break;
1274 default:
1275 return false;
1276 }
1277
1278 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1279 return true;
1280}
1281
Jiri Benc1ab016e2016-02-23 18:02:56 +01001282static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1283 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001284 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001285{
Thomas Graf614732e2015-07-21 10:44:06 +02001286 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001287 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001288
Thomas Graf614732e2015-07-21 10:44:06 +02001289 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001290 skb->protocol = eth_type_trans(skb, vxlan->dev);
1291 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1292
1293 /* Ignore packet loops (and multicast echo) */
1294 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001295 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001296
Jiri Benc760c6802016-02-23 18:02:57 +01001297 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001298 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001299 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001300 saddr.sa.sa_family = AF_INET;
1301#if IS_ENABLED(CONFIG_IPV6)
1302 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001303 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001304 saddr.sa.sa_family = AF_INET6;
1305#endif
1306 }
1307
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001308 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001309 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001310 return false;
1311
1312 return true;
1313}
1314
Jiri Benc760c6802016-02-23 18:02:57 +01001315static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1316 struct sk_buff *skb)
1317{
1318 int err = 0;
1319
1320 if (vxlan_get_sk_family(vs) == AF_INET)
1321 err = IP_ECN_decapsulate(oiph, skb);
1322#if IS_ENABLED(CONFIG_IPV6)
1323 else
1324 err = IP6_ECN_decapsulate(oiph, skb);
1325#endif
1326
1327 if (unlikely(err) && log_ecn_error) {
1328 if (vxlan_get_sk_family(vs) == AF_INET)
1329 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1330 &((struct iphdr *)oiph)->saddr,
1331 ((struct iphdr *)oiph)->tos);
1332 else
1333 net_info_ratelimited("non-ECT from %pI6\n",
1334 &((struct ipv6hdr *)oiph)->saddr);
1335 }
1336 return err <= 1;
1337}
1338
stephen hemmingerd3428942012-10-01 12:32:35 +00001339/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001340static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001341{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001342 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001343 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001344 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001345 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001346 struct vxlan_metadata _md;
1347 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001348 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001349 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001350 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001351 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001352
Jiri Bence1e53142016-04-05 14:47:13 +02001353 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001354 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001355 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001356
Jiri Bencf14eceb2016-02-16 21:59:01 +01001357 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001358 /* VNI flag always required to be set */
1359 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1360 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1361 ntohl(vxlan_hdr(skb)->vx_flags),
1362 ntohl(vxlan_hdr(skb)->vx_vni));
1363 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001364 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001365 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001366 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1367 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001368
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001369 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001370 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001371 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001372
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001373 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1374
Matthias Schiffer49f810f2017-06-19 10:04:00 +02001375 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001376 if (!vxlan)
1377 goto drop;
1378
Jiri Bence1e53142016-04-05 14:47:13 +02001379 /* For backwards compatibility, only allow reserved fields to be
1380 * used by VXLAN extensions if explicitly requested.
1381 */
1382 if (vs->flags & VXLAN_F_GPE) {
1383 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1384 goto drop;
1385 raw_proto = true;
1386 }
1387
1388 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1389 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1390 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001391
Thomas Grafee122c72015-07-21 10:43:58 +02001392 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001393 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001394
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001395 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001396 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001397
Thomas Grafee122c72015-07-21 10:43:58 +02001398 if (!tun_dst)
1399 goto drop;
1400
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001401 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001402
1403 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001404 } else {
1405 memset(md, 0, sizeof(*md));
1406 }
1407
Jiri Bencf14eceb2016-02-16 21:59:01 +01001408 if (vs->flags & VXLAN_F_REMCSUM_RX)
1409 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1410 goto drop;
1411 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001412 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001413 /* Note that GBP and GPE can never be active together. This is
1414 * ensured in vxlan_dev_configure.
1415 */
Thomas Graf35114942015-01-15 03:53:55 +01001416
Jiri Bencf14eceb2016-02-16 21:59:01 +01001417 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001418 /* If there are any unprocessed flags remaining treat
1419 * this as a malformed packet. This behavior diverges from
1420 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1421 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001422 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001423 * is more robust and provides a little more security in
1424 * adding extensions to VXLAN.
1425 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001426 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001427 }
1428
Jiri Bence1e53142016-04-05 14:47:13 +02001429 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001430 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001431 goto drop;
1432 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001433 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001434 skb->dev = vxlan->dev;
1435 skb->pkt_type = PACKET_HOST;
1436 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001437
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001438 oiph = skb_network_header(skb);
1439 skb_reset_network_header(skb);
1440
1441 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1442 ++vxlan->dev->stats.rx_frame_errors;
1443 ++vxlan->dev->stats.rx_errors;
1444 goto drop;
1445 }
1446
1447 stats = this_cpu_ptr(vxlan->dev->tstats);
1448 u64_stats_update_begin(&stats->syncp);
1449 stats->rx_packets++;
1450 stats->rx_bytes += skb->len;
1451 u64_stats_update_end(&stats->syncp);
1452
1453 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001454 return 0;
1455
1456drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001457 /* Consume bad packet */
1458 kfree_skb(skb);
1459 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001460}
1461
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001462static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001463{
1464 struct vxlan_dev *vxlan = netdev_priv(dev);
1465 struct arphdr *parp;
1466 u8 *arpptr, *sha;
1467 __be32 sip, tip;
1468 struct neighbour *n;
1469
1470 if (dev->flags & IFF_NOARP)
1471 goto out;
1472
1473 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1474 dev->stats.tx_dropped++;
1475 goto out;
1476 }
1477 parp = arp_hdr(skb);
1478
1479 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1480 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1481 parp->ar_pro != htons(ETH_P_IP) ||
1482 parp->ar_op != htons(ARPOP_REQUEST) ||
1483 parp->ar_hln != dev->addr_len ||
1484 parp->ar_pln != 4)
1485 goto out;
1486 arpptr = (u8 *)parp + sizeof(struct arphdr);
1487 sha = arpptr;
1488 arpptr += dev->addr_len; /* sha */
1489 memcpy(&sip, arpptr, sizeof(sip));
1490 arpptr += sizeof(sip);
1491 arpptr += dev->addr_len; /* tha */
1492 memcpy(&tip, arpptr, sizeof(tip));
1493
1494 if (ipv4_is_loopback(tip) ||
1495 ipv4_is_multicast(tip))
1496 goto out;
1497
1498 n = neigh_lookup(&arp_tbl, &tip, dev);
1499
1500 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001501 struct vxlan_fdb *f;
1502 struct sk_buff *reply;
1503
1504 if (!(n->nud_state & NUD_CONNECTED)) {
1505 neigh_release(n);
1506 goto out;
1507 }
1508
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001509 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08001510 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001511 /* bridge-local neighbor */
1512 neigh_release(n);
1513 goto out;
1514 }
1515
1516 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1517 n->ha, sha);
1518
1519 neigh_release(n);
1520
David Stevens73461352014-03-18 12:32:29 -04001521 if (reply == NULL)
1522 goto out;
1523
David Stevense4f67ad2012-11-20 02:50:14 +00001524 skb_reset_mac_header(reply);
1525 __skb_pull(reply, skb_network_offset(reply));
1526 reply->ip_summed = CHECKSUM_UNNECESSARY;
1527 reply->pkt_type = PACKET_HOST;
1528
1529 if (netif_rx_ni(reply) == NET_RX_DROP)
1530 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001531 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001532 union vxlan_addr ipa = {
1533 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001534 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001535 };
1536
1537 vxlan_ip_miss(dev, &ipa);
1538 }
David Stevense4f67ad2012-11-20 02:50:14 +00001539out:
1540 consume_skb(skb);
1541 return NETDEV_TX_OK;
1542}
1543
Cong Wangf564f452013-08-31 13:44:36 +08001544#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001545static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1546 struct neighbour *n, bool isrouter)
1547{
1548 struct net_device *dev = request->dev;
1549 struct sk_buff *reply;
1550 struct nd_msg *ns, *na;
1551 struct ipv6hdr *pip6;
1552 u8 *daddr;
1553 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1554 int ns_olen;
1555 int i, len;
1556
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001557 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04001558 return NULL;
1559
1560 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1561 sizeof(*na) + na_olen + dev->needed_tailroom;
1562 reply = alloc_skb(len, GFP_ATOMIC);
1563 if (reply == NULL)
1564 return NULL;
1565
1566 reply->protocol = htons(ETH_P_IPV6);
1567 reply->dev = dev;
1568 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1569 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001570 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001571
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001572 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04001573
1574 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001575 ns_olen = request->len - skb_network_offset(request) -
1576 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04001577 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1578 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1579 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1580 break;
1581 }
1582 }
1583
1584 /* Ethernet header */
1585 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1586 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1587 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1588 reply->protocol = htons(ETH_P_IPV6);
1589
1590 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001591 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001592 skb_put(reply, sizeof(struct ipv6hdr));
1593
1594 /* IPv6 header */
1595
1596 pip6 = ipv6_hdr(reply);
1597 memset(pip6, 0, sizeof(struct ipv6hdr));
1598 pip6->version = 6;
1599 pip6->priority = ipv6_hdr(request)->priority;
1600 pip6->nexthdr = IPPROTO_ICMPV6;
1601 pip6->hop_limit = 255;
1602 pip6->daddr = ipv6_hdr(request)->saddr;
1603 pip6->saddr = *(struct in6_addr *)n->primary_key;
1604
1605 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001606 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001607
David Stevens4b29dba2014-03-24 10:39:58 -04001608 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02001609 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04001610 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1611 na->icmph.icmp6_router = isrouter;
1612 na->icmph.icmp6_override = 1;
1613 na->icmph.icmp6_solicited = 1;
1614 na->target = ns->target;
1615 ether_addr_copy(&na->opt[2], n->ha);
1616 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1617 na->opt[1] = na_olen >> 3;
1618
1619 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1620 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1621 csum_partial(na, sizeof(*na)+na_olen, 0));
1622
1623 pip6->payload_len = htons(sizeof(*na)+na_olen);
1624
1625 skb_push(reply, sizeof(struct ipv6hdr));
1626
1627 reply->ip_summed = CHECKSUM_UNNECESSARY;
1628
1629 return reply;
1630}
1631
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001632static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08001633{
1634 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001635 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001636 const struct ipv6hdr *iphdr;
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08001637 const struct in6_addr *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001638 struct neighbour *n;
1639 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001640
1641 in6_dev = __in6_dev_get(dev);
1642 if (!in6_dev)
1643 goto out;
1644
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001645 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
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;
1650
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001651 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08001652 if (msg->icmph.icmp6_code != 0 ||
1653 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1654 goto out;
1655
David Stevens4b29dba2014-03-24 10:39:58 -04001656 if (ipv6_addr_loopback(daddr) ||
1657 ipv6_addr_is_multicast(&msg->target))
1658 goto out;
1659
1660 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001661
1662 if (n) {
1663 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001664 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001665
1666 if (!(n->nud_state & NUD_CONNECTED)) {
1667 neigh_release(n);
1668 goto out;
1669 }
1670
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001671 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08001672 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1673 /* bridge-local neighbor */
1674 neigh_release(n);
1675 goto out;
1676 }
1677
David Stevens4b29dba2014-03-24 10:39:58 -04001678 reply = vxlan_na_create(skb, n,
1679 !!(f ? f->flags & NTF_ROUTER : 0));
1680
Cong Wangf564f452013-08-31 13:44:36 +08001681 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001682
1683 if (reply == NULL)
1684 goto out;
1685
1686 if (netif_rx_ni(reply) == NET_RX_DROP)
1687 dev->stats.rx_dropped++;
1688
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001689 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001690 union vxlan_addr ipa = {
1691 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001692 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001693 };
1694
Cong Wangf564f452013-08-31 13:44:36 +08001695 vxlan_ip_miss(dev, &ipa);
1696 }
1697
1698out:
1699 consume_skb(skb);
1700 return NETDEV_TX_OK;
1701}
1702#endif
1703
David Stevense4f67ad2012-11-20 02:50:14 +00001704static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1705{
1706 struct vxlan_dev *vxlan = netdev_priv(dev);
1707 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001708
1709 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1710 return false;
1711
1712 n = NULL;
1713 switch (ntohs(eth_hdr(skb)->h_proto)) {
1714 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001715 {
1716 struct iphdr *pip;
1717
David Stevense4f67ad2012-11-20 02:50:14 +00001718 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1719 return false;
1720 pip = ip_hdr(skb);
1721 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001722 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001723 union vxlan_addr ipa = {
1724 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001725 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001726 };
1727
1728 vxlan_ip_miss(dev, &ipa);
1729 return false;
1730 }
1731
David Stevense4f67ad2012-11-20 02:50:14 +00001732 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001733 }
1734#if IS_ENABLED(CONFIG_IPV6)
1735 case ETH_P_IPV6:
1736 {
1737 struct ipv6hdr *pip6;
1738
1739 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1740 return false;
1741 pip6 = ipv6_hdr(skb);
1742 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001743 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08001744 union vxlan_addr ipa = {
1745 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001746 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001747 };
1748
1749 vxlan_ip_miss(dev, &ipa);
1750 return false;
1751 }
1752
1753 break;
1754 }
1755#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001756 default:
1757 return false;
1758 }
1759
1760 if (n) {
1761 bool diff;
1762
Joe Perches7367d0b2013-09-01 11:51:23 -07001763 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001764 if (diff) {
1765 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1766 dev->addr_len);
1767 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1768 }
1769 neigh_release(n);
1770 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001771 }
1772
David Stevense4f67ad2012-11-20 02:50:14 +00001773 return false;
1774}
1775
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001776static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001777 struct vxlan_metadata *md)
1778{
1779 struct vxlanhdr_gbp *gbp;
1780
Thomas Grafdb79a622015-02-04 17:00:04 +01001781 if (!md->gbp)
1782 return;
1783
Thomas Graf35114942015-01-15 03:53:55 +01001784 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001785 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001786
1787 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1788 gbp->dont_learn = 1;
1789
1790 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1791 gbp->policy_applied = 1;
1792
1793 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1794}
1795
Jiri Bence1e53142016-04-05 14:47:13 +02001796static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1797 __be16 protocol)
1798{
1799 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1800
1801 gpe->np_applied = 1;
1802
1803 switch (protocol) {
1804 case htons(ETH_P_IP):
1805 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1806 return 0;
1807 case htons(ETH_P_IPV6):
1808 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1809 return 0;
1810 case htons(ETH_P_TEB):
1811 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1812 return 0;
1813 }
1814 return -EPFNOSUPPORT;
1815}
1816
Jiri Bencf491e562016-02-02 18:09:16 +01001817static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1818 int iphdr_len, __be32 vni,
1819 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001820 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001821{
Cong Wange4c7ed42013-08-31 13:44:33 +08001822 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001823 int min_headroom;
1824 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001825 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001826 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001827
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001828 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001829 skb->ip_summed == CHECKSUM_PARTIAL) {
1830 int csum_start = skb_checksum_start_offset(skb);
1831
1832 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1833 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1834 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001835 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001836 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001837 }
1838
Cong Wange4c7ed42013-08-31 13:44:33 +08001839 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08001840 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001841
1842 /* Need space for new headers (invalidates iph ptr) */
1843 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001844 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08001845 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001846
Alexander Duyckaed069d2016-04-14 15:33:37 -04001847 err = iptunnel_handle_offloads(skb, type);
1848 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08001849 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07001850
Johannes Bergd58ff352017-06-16 14:29:23 +02001851 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001852 vxh->vx_flags = VXLAN_HF_VNI;
1853 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001854
Tom Herbertdfd86452015-01-12 17:00:38 -08001855 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001856 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001857
Jiri Benc54bfd872016-02-16 21:58:58 +01001858 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1859 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1860 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001861
1862 if (!skb_is_gso(skb)) {
1863 skb->ip_summed = CHECKSUM_NONE;
1864 skb->encapsulation = 0;
1865 }
1866 }
1867
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001868 if (vxflags & VXLAN_F_GBP)
1869 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001870 if (vxflags & VXLAN_F_GPE) {
1871 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1872 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08001873 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02001874 inner_protocol = skb->protocol;
1875 }
Thomas Graf35114942015-01-15 03:53:55 +01001876
Jiri Bence1e53142016-04-05 14:47:13 +02001877 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001878 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07001879}
Pravin B Shelar49560532013-08-19 11:23:17 -07001880
pravin shelar655c3de2016-11-13 20:43:55 -08001881static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
1882 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01001883 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001884 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001885 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001886 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01001887{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001888 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01001889 struct rtable *rt = NULL;
1890 struct flowi4 fl4;
1891
pravin shelar655c3de2016-11-13 20:43:55 -08001892 if (!sock4)
1893 return ERR_PTR(-EIO);
1894
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001895 if (tos && !info)
1896 use_cache = false;
1897 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001898 rt = dst_cache_get_ip4(dst_cache, saddr);
1899 if (rt)
1900 return rt;
1901 }
1902
Jiri Benc1a8496b2016-02-02 18:09:14 +01001903 memset(&fl4, 0, sizeof(fl4));
1904 fl4.flowi4_oif = oif;
1905 fl4.flowi4_tos = RT_TOS(tos);
1906 fl4.flowi4_mark = skb->mark;
1907 fl4.flowi4_proto = IPPROTO_UDP;
1908 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001909 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001910 fl4.fl4_dport = dport;
1911 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01001912
1913 rt = ip_route_output_key(vxlan->net, &fl4);
pravin shelar655c3de2016-11-13 20:43:55 -08001914 if (likely(!IS_ERR(rt))) {
1915 if (rt->dst.dev == dev) {
1916 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
1917 ip_rt_put(rt);
1918 return ERR_PTR(-ELOOP);
1919 }
1920
Jiri Benc1a8496b2016-02-02 18:09:14 +01001921 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001922 if (use_cache)
1923 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08001924 } else {
1925 netdev_dbg(dev, "no route to %pI4\n", &daddr);
1926 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001927 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01001928 return rt;
1929}
1930
Jiri Bence5d4b292015-12-07 13:04:30 +01001931#if IS_ENABLED(CONFIG_IPV6)
1932static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08001933 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08001934 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01001935 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001936 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01001937 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001938 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001939 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001940 struct dst_cache *dst_cache,
1941 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01001942{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001943 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01001944 struct dst_entry *ndst;
1945 struct flowi6 fl6;
1946 int err;
1947
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001948 if (!sock6)
1949 return ERR_PTR(-EIO);
1950
Daniel Borkmann14006152016-03-04 15:15:08 +01001951 if (tos && !info)
1952 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001953 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001954 ndst = dst_cache_get_ip6(dst_cache, saddr);
1955 if (ndst)
1956 return ndst;
1957 }
1958
Jiri Bence5d4b292015-12-07 13:04:30 +01001959 memset(&fl6, 0, sizeof(fl6));
1960 fl6.flowi6_oif = oif;
1961 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001962 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01001963 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01001964 fl6.flowi6_mark = skb->mark;
1965 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001966 fl6.fl6_dport = dport;
1967 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01001968
1969 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001970 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01001971 &ndst, &fl6);
pravin shelar655c3de2016-11-13 20:43:55 -08001972 if (unlikely(err < 0)) {
1973 netdev_dbg(dev, "no route to %pI6\n", daddr);
1974 return ERR_PTR(-ENETUNREACH);
1975 }
1976
1977 if (unlikely(ndst->dev == dev)) {
1978 netdev_dbg(dev, "circular route to %pI6\n", daddr);
1979 dst_release(ndst);
1980 return ERR_PTR(-ELOOP);
1981 }
Jiri Bence5d4b292015-12-07 13:04:30 +01001982
1983 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001984 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001985 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01001986 return ndst;
1987}
1988#endif
1989
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001990/* Bypass encapsulation if the destination is local */
1991static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001992 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001993{
Li RongQing8f849852014-01-04 13:57:59 +08001994 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001995 union vxlan_addr loopback;
1996 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001997 struct net_device *dev = skb->dev;
1998 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001999
Li RongQing8f849852014-01-04 13:57:59 +08002000 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2001 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002002 skb->pkt_type = PACKET_HOST;
2003 skb->encapsulation = 0;
2004 skb->dev = dst_vxlan->dev;
2005 __skb_pull(skb, skb_network_offset(skb));
2006
Cong Wange4c7ed42013-08-31 13:44:33 +08002007 if (remote_ip->sa.sa_family == AF_INET) {
2008 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2009 loopback.sa.sa_family = AF_INET;
2010#if IS_ENABLED(CONFIG_IPV6)
2011 } else {
2012 loopback.sin6.sin6_addr = in6addr_loopback;
2013 loopback.sa.sa_family = AF_INET6;
2014#endif
2015 }
2016
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002017 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
Matthias Schiffer87613de2017-06-19 10:03:59 +02002018 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0,
2019 vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002020
2021 u64_stats_update_begin(&tx_stats->syncp);
2022 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002023 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002024 u64_stats_update_end(&tx_stats->syncp);
2025
2026 if (netif_rx(skb) == NET_RX_SUCCESS) {
2027 u64_stats_update_begin(&rx_stats->syncp);
2028 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002029 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002030 u64_stats_update_end(&rx_stats->syncp);
2031 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08002032 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002033 }
2034}
2035
pravin shelarfee1fad2016-11-13 20:43:56 -08002036static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002037 struct vxlan_dev *vxlan,
2038 union vxlan_addr *daddr,
2039 __be16 dst_port, int dst_ifindex, __be32 vni,
2040 struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002041 u32 rt_flags)
2042{
2043#if IS_ENABLED(CONFIG_IPV6)
2044 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2045 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2046 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2047 */
2048 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2049#endif
2050 /* Bypass encapsulation if the destination is local */
2051 if (rt_flags & RTCF_LOCAL &&
2052 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2053 struct vxlan_dev *dst_vxlan;
2054
2055 dst_release(dst);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002056 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
pravin shelarfee1fad2016-11-13 20:43:56 -08002057 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002058 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002059 if (!dst_vxlan) {
2060 dev->stats.tx_errors++;
2061 kfree_skb(skb);
2062
2063 return -ENOENT;
2064 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002065 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002066 return 1;
2067 }
2068
2069 return 0;
2070}
2071
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002072static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002073 __be32 default_vni, struct vxlan_rdst *rdst,
2074 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002075{
Paolo Abenid71785f2016-02-12 15:43:57 +01002076 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002077 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002078 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002079 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002080 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002081 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002082 struct vxlan_metadata _md;
2083 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002084 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002085 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002086 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002087 __u8 tos, ttl;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002088 int ifindex;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07002089 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002090 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002091 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002092 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002093
Jiri Benc61adedf2015-08-20 13:56:25 +02002094 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002095
Thomas Grafee122c72015-07-21 10:43:58 +02002096 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002097 dst = &rdst->remote_ip;
2098 if (vxlan_addr_any(dst)) {
2099 if (did_rsc) {
2100 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002101 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002102 return;
2103 }
2104 goto drop;
2105 }
2106
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002107 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002108 vni = (rdst->remote_vni) ? : default_vni;
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002109 ifindex = rdst->remote_ifindex;
Brian Russell11586322017-02-24 17:47:11 +00002110 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002111 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002112 md->gbp = skb->mark;
2113 ttl = vxlan->cfg.ttl;
2114 if (!ttl && vxlan_addr_multicast(dst))
2115 ttl = 1;
2116
2117 tos = vxlan->cfg.tos;
2118 if (tos == 1)
2119 tos = ip_tunnel_get_dsfield(old_iph, skb);
2120
2121 if (dst->sa.sa_family == AF_INET)
2122 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2123 else
2124 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2125 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002126 } else {
2127 if (!info) {
2128 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2129 dev->name);
2130 goto drop;
2131 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002132 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002133 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002134 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002135 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2136 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002137 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002138 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2139 }
Thomas Grafee122c72015-07-21 10:43:58 +02002140 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002141 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2142 vni = tunnel_id_to_key32(info->key.tun_id);
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002143 ifindex = 0;
Paolo Abenid71785f2016-02-12 15:43:57 +01002144 dst_cache = &info->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002145 if (info->options_len)
2146 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002147 ttl = info->key.ttl;
2148 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002149 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002150 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002151 }
pravin shelar0770b532016-11-13 20:43:57 -08002152 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2153 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002154
Jakub Kicinski56de8592017-02-24 11:43:36 -08002155 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002156 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002157 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002158 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002159 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002160
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002161 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002162 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002163 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002164 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002165 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002166 if (IS_ERR(rt)) {
2167 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002168 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002169 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002170
Cong Wange4c7ed42013-08-31 13:44:33 +08002171 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002172 if (!info) {
2173 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002174 dst_port, ifindex, vni,
2175 &rt->dst, rt->rt_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002176 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002177 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002178 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002179 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002180 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002181
pravin shelarc46b7892016-11-13 20:43:54 -08002182 ndst = &rt->dst;
Cong Wange4c7ed42013-08-31 13:44:33 +08002183 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2184 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002185 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002186 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002187 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002188 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002189
Brian Russell11586322017-02-24 17:47:11 +00002190 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002191 dst->sin.sin_addr.s_addr, tos, ttl, df,
2192 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002193#if IS_ENABLED(CONFIG_IPV6)
2194 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002195 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002196
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002197 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002198 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002199 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002200 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002201 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002202 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002203 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002204 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002205 goto tx_error;
2206 }
pravin shelar655c3de2016-11-13 20:43:55 -08002207
pravin shelarfee1fad2016-11-13 20:43:56 -08002208 if (!info) {
2209 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002210
pravin shelarfee1fad2016-11-13 20:43:56 -08002211 err = encap_bypass_if_local(skb, dev, vxlan, dst,
Matthias Schiffer49f810f2017-06-19 10:04:00 +02002212 dst_port, ifindex, vni,
2213 ndst, rt6i_flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002214 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002215 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002216 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002217
Daniel Borkmann14006152016-03-04 15:15:08 +01002218 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002219 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002220 skb_scrub_packet(skb, xnet);
2221 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002222 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002223 if (err < 0)
2224 goto tx_error;
2225
pravin shelar0770b532016-11-13 20:43:57 -08002226 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002227 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002228 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002229 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002230#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002231 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002232out_unlock:
2233 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002234 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002235
2236drop:
2237 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002238 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002239 return;
2240
2241tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002242 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002243 if (err == -ELOOP)
2244 dev->stats.collisions++;
2245 else if (err == -ENETUNREACH)
2246 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002247 dst_release(ndst);
2248 dev->stats.tx_errors++;
2249 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002250}
2251
David Stevens66817122013-03-15 04:35:51 +00002252/* Transmit local packets over Vxlan
2253 *
2254 * Outer IP header inherits ECN and DF from inner header.
2255 * Outer UDP destination is the VXLAN assigned port.
2256 * source port is based on hash of flow
2257 */
2258static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2259{
2260 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002261 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002262 struct ethhdr *eth;
2263 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002264 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002265 struct vxlan_fdb *f;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002266 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002267
Jiri Benc61adedf2015-08-20 13:56:25 +02002268 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002269
David Stevens66817122013-03-15 04:35:51 +00002270 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002271
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002272 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002273 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2274 info->mode & IP_TUNNEL_INFO_TX) {
2275 vni = tunnel_id_to_key32(info->key.tun_id);
2276 } else {
2277 if (info && info->mode & IP_TUNNEL_INFO_TX)
2278 vxlan_xmit_one(skb, dev, vni, NULL, false);
2279 else
2280 kfree_skb(skb);
2281 return NETDEV_TX_OK;
2282 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002283 }
2284
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002285 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002286 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002287 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002288 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002289#if IS_ENABLED(CONFIG_IPV6)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002290 else if (ntohs(eth->h_proto) == ETH_P_IPV6) {
2291 struct ipv6hdr *hdr, _hdr;
2292 if ((hdr = skb_header_pointer(skb,
2293 skb_network_offset(skb),
2294 sizeof(_hdr), &_hdr)) &&
2295 hdr->nexthdr == IPPROTO_ICMPV6)
2296 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002297 }
2298#endif
2299 }
David Stevens66817122013-03-15 04:35:51 +00002300
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002301 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002302 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002303 did_rsc = false;
2304
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002305 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002306 (ntohs(eth->h_proto) == ETH_P_IP ||
2307 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002308 did_rsc = route_shortcircuit(dev, skb);
2309 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002310 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002311 }
2312
David Stevens66817122013-03-15 04:35:51 +00002313 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002314 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002315 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002316 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002317 !is_multicast_ether_addr(eth->h_dest))
2318 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002319
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002320 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002321 kfree_skb(skb);
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002322 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002323 }
David Stevens66817122013-03-15 04:35:51 +00002324 }
2325
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002326 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2327 struct sk_buff *skb1;
2328
Eric Dumazet8f646c92014-01-06 09:54:31 -08002329 if (!fdst) {
2330 fdst = rdst;
2331 continue;
2332 }
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002333 skb1 = skb_clone(skb, GFP_ATOMIC);
2334 if (skb1)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002335 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002336 }
2337
Eric Dumazet8f646c92014-01-06 09:54:31 -08002338 if (fdst)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002339 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002340 else
2341 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002342 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002343}
2344
stephen hemmingerd3428942012-10-01 12:32:35 +00002345/* Walk the forwarding table and purge stale entries */
2346static void vxlan_cleanup(unsigned long arg)
2347{
2348 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2349 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2350 unsigned int h;
2351
2352 if (!netif_running(vxlan->dev))
2353 return;
2354
stephen hemmingerd3428942012-10-01 12:32:35 +00002355 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2356 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002357
2358 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002359 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2360 struct vxlan_fdb *f
2361 = container_of(p, struct vxlan_fdb, hlist);
2362 unsigned long timeout;
2363
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002364 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002365 continue;
2366
Roopa Prabhudef499c2017-03-27 15:46:41 -07002367 if (f->flags & NTF_EXT_LEARNED)
2368 continue;
2369
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002370 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002371 if (time_before_eq(timeout, jiffies)) {
2372 netdev_dbg(vxlan->dev,
2373 "garbage collect %pM\n",
2374 f->eth_addr);
2375 f->state = NUD_STALE;
2376 vxlan_fdb_destroy(vxlan, f);
2377 } else if (time_before(timeout, next_timer))
2378 next_timer = timeout;
2379 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002380 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002381 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002382
2383 mod_timer(&vxlan->age_timer, next_timer);
2384}
2385
Mark Blocha53cb292017-06-02 03:24:08 +03002386static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2387{
2388 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2389
2390 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002391 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2392#if IS_ENABLED(CONFIG_IPV6)
2393 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2394#endif
Mark Blocha53cb292017-06-02 03:24:08 +03002395 spin_unlock(&vn->sock_lock);
2396}
2397
Jiri Benc69e76662017-07-02 19:00:57 +02002398static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2399 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002400{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002401 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002402 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002403
Jiri Benc69e76662017-07-02 19:00:57 +02002404 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002405 spin_lock(&vn->sock_lock);
Jiri Benc69e76662017-07-02 19:00:57 +02002406 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002407 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002408}
2409
stephen hemmingerd3428942012-10-01 12:32:35 +00002410/* Setup stats when device is created */
2411static int vxlan_init(struct net_device *dev)
2412{
WANG Cong1c213bd2014-02-13 11:46:28 -08002413 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002414 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002415 return -ENOMEM;
2416
2417 return 0;
2418}
2419
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002420static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002421{
2422 struct vxlan_fdb *f;
2423
2424 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002425 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002426 if (f)
2427 vxlan_fdb_destroy(vxlan, f);
2428 spin_unlock_bh(&vxlan->hash_lock);
2429}
2430
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002431static void vxlan_uninit(struct net_device *dev)
2432{
2433 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002434
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002435 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002436
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002437 free_percpu(dev->tstats);
2438}
2439
stephen hemmingerd3428942012-10-01 12:32:35 +00002440/* Start ageing timer and join group when device is brought up */
2441static int vxlan_open(struct net_device *dev)
2442{
2443 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002444 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002445
Jiri Benc205f3562015-09-24 13:50:01 +02002446 ret = vxlan_sock_add(vxlan);
2447 if (ret < 0)
2448 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002449
Gao feng79d4a942013-12-10 16:37:32 +08002450 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002451 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002452 if (ret == -EADDRINUSE)
2453 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002454 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002455 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002456 return ret;
2457 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002458 }
2459
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002460 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002461 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2462
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002463 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002464}
2465
2466/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002467static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00002468{
Cong Wang31fec5a2013-05-27 22:35:52 +00002469 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002470
2471 spin_lock_bh(&vxlan->hash_lock);
2472 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2473 struct hlist_node *p, *n;
2474 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2475 struct vxlan_fdb *f
2476 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002477 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2478 continue;
Mike Rapoportafbd8bae2013-06-25 16:01:51 +03002479 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2480 if (!is_zero_ether_addr(f->eth_addr))
2481 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002482 }
2483 }
2484 spin_unlock_bh(&vxlan->hash_lock);
2485}
2486
2487/* Cleanup timer and forwarding table on shutdown */
2488static int vxlan_stop(struct net_device *dev)
2489{
2490 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002491 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002492 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002493
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002494 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002495 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002496 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002497
2498 del_timer_sync(&vxlan->age_timer);
2499
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002500 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02002501 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002502
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002503 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002504}
2505
stephen hemmingerd3428942012-10-01 12:32:35 +00002506/* Stub, nothing needs to be done. */
2507static void vxlan_set_multicast_list(struct net_device *dev)
2508{
2509}
2510
Daniel Borkmann345010b2013-12-18 00:21:08 +01002511static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2512{
2513 struct vxlan_dev *vxlan = netdev_priv(dev);
2514 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002515 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2516 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002517 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04002518
2519 /* This check is different than dev->max_mtu, because it looks at
2520 * the lowerdev->mtu, rather than the static dev->max_mtu
2521 */
2522 if (lowerdev) {
2523 int max_mtu = lowerdev->mtu -
2524 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2525 if (new_mtu > max_mtu)
2526 return -EINVAL;
2527 }
2528
2529 dev->mtu = new_mtu;
2530 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01002531}
2532
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002533static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2534{
2535 struct vxlan_dev *vxlan = netdev_priv(dev);
2536 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2537 __be16 sport, dport;
2538
2539 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2540 vxlan->cfg.port_max, true);
2541 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2542
Jiri Benc239e9442015-12-07 13:04:31 +01002543 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002544 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002545 struct rtable *rt;
2546
pravin shelar655c3de2016-11-13 20:43:55 -08002547 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002548 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002549 &info->key.u.ipv4.src, dport, sport,
2550 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002551 if (IS_ERR(rt))
2552 return PTR_ERR(rt);
2553 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002554 } else {
2555#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08002556 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01002557 struct dst_entry *ndst;
2558
pravin shelar655c3de2016-11-13 20:43:55 -08002559 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002560 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002561 &info->key.u.ipv6.src, dport, sport,
2562 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002563 if (IS_ERR(ndst))
2564 return PTR_ERR(ndst);
2565 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002566#else /* !CONFIG_IPV6 */
2567 return -EPFNOSUPPORT;
2568#endif
2569 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002570 info->key.tp_src = sport;
2571 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002572 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002573}
2574
Jiri Benc0c867c92016-04-05 14:47:10 +02002575static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002576 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002577 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002578 .ndo_open = vxlan_open,
2579 .ndo_stop = vxlan_stop,
2580 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002581 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002582 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002583 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002584 .ndo_validate_addr = eth_validate_addr,
2585 .ndo_set_mac_address = eth_mac_addr,
2586 .ndo_fdb_add = vxlan_fdb_add,
2587 .ndo_fdb_del = vxlan_fdb_delete,
2588 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002589 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002590};
2591
Jiri Bence1e53142016-04-05 14:47:13 +02002592static const struct net_device_ops vxlan_netdev_raw_ops = {
2593 .ndo_init = vxlan_init,
2594 .ndo_uninit = vxlan_uninit,
2595 .ndo_open = vxlan_open,
2596 .ndo_stop = vxlan_stop,
2597 .ndo_start_xmit = vxlan_xmit,
2598 .ndo_get_stats64 = ip_tunnel_get_stats64,
2599 .ndo_change_mtu = vxlan_change_mtu,
2600 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2601};
2602
stephen hemmingerd3428942012-10-01 12:32:35 +00002603/* Info for udev, that this is a virtual tunnel endpoint */
2604static struct device_type vxlan_type = {
2605 .name = "vxlan",
2606};
2607
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002608/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002609 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002610 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002611 */
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02002612static void vxlan_push_rx_ports(struct net_device *dev)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002613{
2614 struct vxlan_sock *vs;
2615 struct net *net = dev_net(dev);
2616 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002617 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002618
2619 spin_lock(&vn->sock_lock);
2620 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Alexander Duycke7b3db52016-06-16 12:20:52 -07002621 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2622 udp_tunnel_push_rx_port(dev, vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002623 (vs->flags & VXLAN_F_GPE) ?
2624 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002625 UDP_TUNNEL_TYPE_VXLAN);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002626 }
2627 spin_unlock(&vn->sock_lock);
2628}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002629
stephen hemmingerd3428942012-10-01 12:32:35 +00002630/* Initialize the device structure. */
2631static void vxlan_setup(struct net_device *dev)
2632{
2633 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002634 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002635
Jiri Benc65226ef2016-04-28 16:36:30 +02002636 eth_hw_addr_random(dev);
2637 ether_setup(dev);
2638
David S. Millercf124db2017-05-08 12:52:56 -04002639 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00002640 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2641
stephen hemmingerd3428942012-10-01 12:32:35 +00002642 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002643 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002644 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002645 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002646
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002647 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002648 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002649 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07002650 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002651 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002652
Matthias Schiffera9853432017-06-19 10:03:55 +02002653 /* MTU range: 68 - 65535 */
2654 dev->min_mtu = ETH_MIN_MTU;
2655 dev->max_mtu = ETH_MAX_MTU;
2656
stephen hemminger553675f2013-05-16 11:35:20 +00002657 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002658 spin_lock_init(&vxlan->hash_lock);
2659
2660 init_timer_deferrable(&vxlan->age_timer);
2661 vxlan->age_timer.function = vxlan_cleanup;
2662 vxlan->age_timer.data = (unsigned long) vxlan;
2663
2664 vxlan->dev = dev;
2665
Tom Herbert58ce31c2015-08-19 17:07:33 -07002666 gro_cells_init(&vxlan->gro_cells, dev);
2667
stephen hemmingerd3428942012-10-01 12:32:35 +00002668 for (h = 0; h < FDB_HASH_SIZE; ++h)
2669 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2670}
2671
Jiri Benc0c867c92016-04-05 14:47:10 +02002672static void vxlan_ether_setup(struct net_device *dev)
2673{
Jiri Benc0c867c92016-04-05 14:47:10 +02002674 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2675 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2676 dev->netdev_ops = &vxlan_netdev_ether_ops;
2677}
2678
Jiri Bence1e53142016-04-05 14:47:13 +02002679static void vxlan_raw_setup(struct net_device *dev)
2680{
Jiri Benc65226ef2016-04-28 16:36:30 +02002681 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002682 dev->type = ARPHRD_NONE;
2683 dev->hard_header_len = 0;
2684 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002685 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2686 dev->netdev_ops = &vxlan_netdev_raw_ops;
2687}
2688
stephen hemmingerd3428942012-10-01 12:32:35 +00002689static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2690 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002691 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002692 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002693 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2694 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002695 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002696 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2697 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002698 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002699 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2700 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2701 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002702 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002703 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2704 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2705 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2706 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002707 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002708 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002709 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2710 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2711 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002712 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2713 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002714 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002715 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002716 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002717};
2718
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002719static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
2720 struct netlink_ext_ack *extack)
stephen hemmingerd3428942012-10-01 12:32:35 +00002721{
2722 if (tb[IFLA_ADDRESS]) {
2723 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2724 pr_debug("invalid link address (not ethernet)\n");
2725 return -EINVAL;
2726 }
2727
2728 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2729 pr_debug("invalid all zero ethernet address\n");
2730 return -EADDRNOTAVAIL;
2731 }
2732 }
2733
Matthias Schiffera9853432017-06-19 10:03:55 +02002734 if (tb[IFLA_MTU]) {
Matthias Schiffer019b13a2017-06-27 14:42:43 +02002735 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
Matthias Schiffera9853432017-06-19 10:03:55 +02002736
2737 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU)
2738 return -EINVAL;
2739 }
2740
stephen hemmingerd3428942012-10-01 12:32:35 +00002741 if (!data)
2742 return -EINVAL;
2743
2744 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02002745 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2746
Matthias Schiffer4e37d692017-02-23 17:19:41 +01002747 if (id >= VXLAN_N_VID)
stephen hemmingerd3428942012-10-01 12:32:35 +00002748 return -ERANGE;
2749 }
2750
stephen hemminger05f47d62012-10-09 20:35:50 +00002751 if (data[IFLA_VXLAN_PORT_RANGE]) {
2752 const struct ifla_vxlan_port_range *p
2753 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2754
2755 if (ntohs(p->high) < ntohs(p->low)) {
2756 pr_debug("port range %u .. %u not valid\n",
2757 ntohs(p->low), ntohs(p->high));
2758 return -EINVAL;
2759 }
2760 }
2761
stephen hemmingerd3428942012-10-01 12:32:35 +00002762 return 0;
2763}
2764
Yan Burman1b13c972013-01-29 23:43:07 +00002765static void vxlan_get_drvinfo(struct net_device *netdev,
2766 struct ethtool_drvinfo *drvinfo)
2767{
2768 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2769 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2770}
2771
2772static const struct ethtool_ops vxlan_ethtool_ops = {
2773 .get_drvinfo = vxlan_get_drvinfo,
2774 .get_link = ethtool_op_get_link,
2775};
2776
Tom Herbert3ee64f32014-07-13 19:49:42 -07002777static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2778 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002779{
Cong Wange4c7ed42013-08-31 13:44:33 +08002780 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002781 struct udp_port_cfg udp_conf;
2782 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002783
Tom Herbert3ee64f32014-07-13 19:49:42 -07002784 memset(&udp_conf, 0, sizeof(udp_conf));
2785
2786 if (ipv6) {
2787 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002788 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002789 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002790 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002791 } else {
2792 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002793 }
2794
Tom Herbert3ee64f32014-07-13 19:49:42 -07002795 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002796
Tom Herbert3ee64f32014-07-13 19:49:42 -07002797 /* Open UDP socket */
2798 err = udp_sock_create(net, &udp_conf, &sock);
2799 if (err < 0)
2800 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002801
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002802 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002803}
2804
2805/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002806static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2807 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002808{
2809 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2810 struct vxlan_sock *vs;
2811 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002812 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002813 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002814
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002815 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002816 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002817 return ERR_PTR(-ENOMEM);
2818
2819 for (h = 0; h < VNI_HASH_SIZE; ++h)
2820 INIT_HLIST_HEAD(&vs->vni_list[h]);
2821
Tom Herbert3ee64f32014-07-13 19:49:42 -07002822 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002823 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002824 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002825 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002826 }
2827
Cong Wange4c7ed42013-08-31 13:44:33 +08002828 vs->sock = sock;
Reshetova, Elena66af8462017-07-04 15:52:59 +03002829 refcount_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002830 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002831
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002832 spin_lock(&vn->sock_lock);
2833 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07002834 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002835 (vs->flags & VXLAN_F_GPE) ?
2836 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002837 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002838 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002839
2840 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002841 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002842 tunnel_cfg.sk_user_data = vs;
2843 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002844 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002845 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002846 tunnel_cfg.gro_receive = vxlan_gro_receive;
2847 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002848
2849 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002850
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002851 return vs;
2852}
stephen hemminger553675f2013-05-16 11:35:20 +00002853
Jiri Bencb1be00a2015-09-24 13:50:02 +02002854static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002855{
Jiri Benc205f3562015-09-24 13:50:01 +02002856 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2857 struct vxlan_sock *vs = NULL;
Jiri Benc69e76662017-07-02 19:00:57 +02002858 struct vxlan_dev_node *node;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002859
Jiri Benc205f3562015-09-24 13:50:01 +02002860 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002861 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002862 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002863 vxlan->cfg.dst_port, vxlan->cfg.flags);
Reshetova, Elena66af8462017-07-04 15:52:59 +03002864 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002865 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002866 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002867 }
2868 spin_unlock(&vn->sock_lock);
2869 }
Jiri Benc205f3562015-09-24 13:50:01 +02002870 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002871 vs = vxlan_socket_create(vxlan->net, ipv6,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002872 vxlan->cfg.dst_port, vxlan->cfg.flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002873 if (IS_ERR(vs))
2874 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002875#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc69e76662017-07-02 19:00:57 +02002876 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002877 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02002878 node = &vxlan->hlist6;
2879 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02002880#endif
Jiri Benc69e76662017-07-02 19:00:57 +02002881 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002882 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc69e76662017-07-02 19:00:57 +02002883 node = &vxlan->hlist4;
2884 }
2885 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02002886 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002887}
2888
Jiri Bencb1be00a2015-09-24 13:50:02 +02002889static int vxlan_sock_add(struct vxlan_dev *vxlan)
2890{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002891 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
2892 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02002893 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002894 int ret = 0;
2895
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002896 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002897#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002898 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02002899 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02002900 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02002901 if (ret < 0 && ret != -EAFNOSUPPORT)
2902 ipv4 = false;
2903 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002904#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02002905 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002906 ret = __vxlan_sock_add(vxlan, false);
2907 if (ret < 0)
2908 vxlan_sock_release(vxlan);
2909 return ret;
2910}
2911
Matthias Schiffera9853432017-06-19 10:03:55 +02002912static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
2913 struct net_device **lower,
2914 struct vxlan_dev *old)
stephen hemmingerd3428942012-10-01 12:32:35 +00002915{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002916 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02002917 struct vxlan_dev *tmp;
2918 bool use_ipv6 = false;
2919
2920 if (conf->flags & VXLAN_F_GPE) {
2921 /* For now, allow GPE only together with
2922 * COLLECT_METADATA. This can be relaxed later; in such
2923 * case, the other side of the PtP link will have to be
2924 * provided.
2925 */
2926 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2927 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2928 return -EINVAL;
2929 }
2930 }
2931
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002932 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
2933 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02002934 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002935 conf->saddr.sa.sa_family = AF_INET;
2936 } else if (!conf->remote_ip.sa.sa_family) {
2937 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
2938 } else if (!conf->saddr.sa.sa_family) {
2939 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
2940 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002941
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002942 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family)
2943 return -EINVAL;
2944
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02002945 if (vxlan_addr_multicast(&conf->saddr))
2946 return -EINVAL;
2947
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002948 if (conf->saddr.sa.sa_family == AF_INET6) {
Matthias Schiffera9853432017-06-19 10:03:55 +02002949 if (!IS_ENABLED(CONFIG_IPV6))
2950 return -EPFNOSUPPORT;
2951 use_ipv6 = true;
2952 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02002953
2954 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2955 int local_type =
2956 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
2957 int remote_type =
2958 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
2959
2960 if (local_type & IPV6_ADDR_LINKLOCAL) {
2961 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
2962 (remote_type != IPV6_ADDR_ANY))
2963 return -EINVAL;
2964
2965 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
2966 } else {
2967 if (remote_type ==
2968 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL))
2969 return -EINVAL;
2970
2971 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
2972 }
2973 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002974 }
2975
2976 if (conf->label && !use_ipv6)
2977 return -EINVAL;
2978
2979 if (conf->remote_ifindex) {
2980 struct net_device *lowerdev;
2981
2982 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
2983 if (!lowerdev)
2984 return -ENODEV;
2985
2986#if IS_ENABLED(CONFIG_IPV6)
2987 if (use_ipv6) {
2988 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2989 if (idev && idev->cnf.disable_ipv6)
2990 return -EPERM;
2991 }
2992#endif
2993
2994 *lower = lowerdev;
2995 } else {
2996 if (vxlan_addr_multicast(&conf->remote_ip))
2997 return -EINVAL;
2998
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02002999#if IS_ENABLED(CONFIG_IPV6)
3000 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL)
3001 return -EINVAL;
3002#endif
3003
Matthias Schiffera9853432017-06-19 10:03:55 +02003004 *lower = NULL;
3005 }
3006
3007 if (!conf->dst_port) {
3008 if (conf->flags & VXLAN_F_GPE)
3009 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3010 else
3011 conf->dst_port = htons(vxlan_port);
3012 }
3013
3014 if (!conf->age_interval)
3015 conf->age_interval = FDB_AGE_DEFAULT;
3016
3017 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3018 if (tmp == old)
3019 continue;
3020
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003021 if (tmp->cfg.vni != conf->vni)
3022 continue;
3023 if (tmp->cfg.dst_port != conf->dst_port)
3024 continue;
3025 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003026 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffer49f810f2017-06-19 10:04:00 +02003027 continue;
3028
3029 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3030 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3031 continue;
3032
3033 return -EEXIST;
Matthias Schiffera9853432017-06-19 10:03:55 +02003034 }
3035
3036 return 0;
3037}
3038
3039static void vxlan_config_apply(struct net_device *dev,
3040 struct vxlan_config *conf,
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003041 struct net_device *lowerdev,
3042 struct net *src_net,
3043 bool changelink)
Matthias Schiffera9853432017-06-19 10:03:55 +02003044{
3045 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003046 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003047 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003048 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3049 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003050
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003051 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003052 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003053 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003054 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003055 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003056
Matthias Schiffera9853432017-06-19 10:03:55 +02003057 if (conf->mtu)
3058 dev->mtu = conf->mtu;
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003059
3060 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02003061 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003062
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003063 dst->remote_vni = conf->vni;
3064
3065 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003066
Matthias Schiffera9853432017-06-19 10:03:55 +02003067 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003068 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003069
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003070 dev->gso_max_size = lowerdev->gso_max_size;
3071 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003072
3073 needed_headroom = lowerdev->hard_header_len;
3074
3075 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3076 VXLAN_HEADROOM);
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003077 }
3078
Matthias Schiffera9853432017-06-19 10:03:55 +02003079 if (dev->mtu > max_mtu)
3080 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003081
Jiri Bencb1be00a2015-09-24 13:50:02 +02003082 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3083 needed_headroom += VXLAN6_HEADROOM;
3084 else
3085 needed_headroom += VXLAN_HEADROOM;
3086 dev->needed_headroom = needed_headroom;
3087
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003088 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003089}
stephen hemmingerd3428942012-10-01 12:32:35 +00003090
Matthias Schiffera9853432017-06-19 10:03:55 +02003091static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3092 struct vxlan_config *conf,
3093 bool changelink)
3094{
3095 struct vxlan_dev *vxlan = netdev_priv(dev);
3096 struct net_device *lowerdev;
3097 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003098
Matthias Schiffera9853432017-06-19 10:03:55 +02003099 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan);
3100 if (ret)
3101 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003102
Sabrina Dubroca889ce932017-06-30 15:50:00 +02003103 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003104
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003105 return 0;
3106}
3107
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003108static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3109 struct vxlan_config *conf)
3110{
3111 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3112 struct vxlan_dev *vxlan = netdev_priv(dev);
3113 int err;
3114
3115 err = vxlan_dev_configure(net, dev, conf, false);
3116 if (err)
3117 return err;
3118
3119 dev->ethtool_ops = &vxlan_ethtool_ops;
3120
3121 /* create an fdb entry for a valid default destination */
3122 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
3123 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3124 &vxlan->default_dst.remote_ip,
3125 NUD_REACHABLE | NUD_PERMANENT,
3126 NLM_F_EXCL | NLM_F_CREATE,
3127 vxlan->cfg.dst_port,
3128 vxlan->default_dst.remote_vni,
3129 vxlan->default_dst.remote_vni,
3130 vxlan->default_dst.remote_ifindex,
3131 NTF_SELF);
3132 if (err)
3133 return err;
3134 }
3135
3136 err = register_netdevice(dev);
3137 if (err) {
3138 vxlan_fdb_delete_default(vxlan, vxlan->default_dst.remote_vni);
3139 return err;
3140 }
3141
3142 list_add(&vxlan->next, &vn->vxlan_list);
3143 return 0;
3144}
3145
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003146static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3147 struct net_device *dev, struct vxlan_config *conf,
3148 bool changelink)
3149{
3150 struct vxlan_dev *vxlan = netdev_priv(dev);
3151
3152 memset(conf, 0, sizeof(*conf));
3153
3154 /* if changelink operation, start with old existing cfg */
3155 if (changelink)
3156 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3157
3158 if (data[IFLA_VXLAN_ID]) {
3159 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3160
3161 if (changelink && (vni != conf->vni))
3162 return -EOPNOTSUPP;
3163 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3164 }
3165
3166 if (data[IFLA_VXLAN_GROUP]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003167 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET))
3168 return -EOPNOTSUPP;
3169
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003170 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003171 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003172 } else if (data[IFLA_VXLAN_GROUP6]) {
3173 if (!IS_ENABLED(CONFIG_IPV6))
3174 return -EPFNOSUPPORT;
3175
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003176 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6))
3177 return -EOPNOTSUPP;
3178
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003179 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3180 conf->remote_ip.sa.sa_family = AF_INET6;
3181 }
3182
3183 if (data[IFLA_VXLAN_LOCAL]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003184 if (changelink && (conf->saddr.sa.sa_family != AF_INET))
3185 return -EOPNOTSUPP;
3186
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003187 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3188 conf->saddr.sa.sa_family = AF_INET;
3189 } else if (data[IFLA_VXLAN_LOCAL6]) {
3190 if (!IS_ENABLED(CONFIG_IPV6))
3191 return -EPFNOSUPPORT;
3192
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003193 if (changelink && (conf->saddr.sa.sa_family != AF_INET6))
3194 return -EOPNOTSUPP;
3195
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003196 /* TODO: respect scope id */
3197 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3198 conf->saddr.sa.sa_family = AF_INET6;
3199 }
3200
3201 if (data[IFLA_VXLAN_LINK])
3202 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3203
3204 if (data[IFLA_VXLAN_TOS])
3205 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3206
3207 if (data[IFLA_VXLAN_TTL])
3208 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3209
3210 if (data[IFLA_VXLAN_LABEL])
3211 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3212 IPV6_FLOWLABEL_MASK;
3213
3214 if (data[IFLA_VXLAN_LEARNING]) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003215 if (nla_get_u8(data[IFLA_VXLAN_LEARNING]))
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003216 conf->flags |= VXLAN_F_LEARN;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003217 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003218 conf->flags &= ~VXLAN_F_LEARN;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003219 } else if (!changelink) {
3220 /* default to learn on a new device */
3221 conf->flags |= VXLAN_F_LEARN;
3222 }
3223
3224 if (data[IFLA_VXLAN_AGEING]) {
3225 if (changelink)
3226 return -EOPNOTSUPP;
3227 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3228 }
3229
3230 if (data[IFLA_VXLAN_PROXY]) {
3231 if (changelink)
3232 return -EOPNOTSUPP;
3233 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3234 conf->flags |= VXLAN_F_PROXY;
3235 }
3236
3237 if (data[IFLA_VXLAN_RSC]) {
3238 if (changelink)
3239 return -EOPNOTSUPP;
3240 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3241 conf->flags |= VXLAN_F_RSC;
3242 }
3243
3244 if (data[IFLA_VXLAN_L2MISS]) {
3245 if (changelink)
3246 return -EOPNOTSUPP;
3247 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3248 conf->flags |= VXLAN_F_L2MISS;
3249 }
3250
3251 if (data[IFLA_VXLAN_L3MISS]) {
3252 if (changelink)
3253 return -EOPNOTSUPP;
3254 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3255 conf->flags |= VXLAN_F_L3MISS;
3256 }
3257
3258 if (data[IFLA_VXLAN_LIMIT]) {
3259 if (changelink)
3260 return -EOPNOTSUPP;
3261 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3262 }
3263
3264 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3265 if (changelink)
3266 return -EOPNOTSUPP;
3267 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3268 conf->flags |= VXLAN_F_COLLECT_METADATA;
3269 }
3270
3271 if (data[IFLA_VXLAN_PORT_RANGE]) {
3272 if (!changelink) {
3273 const struct ifla_vxlan_port_range *p
3274 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3275 conf->port_min = ntohs(p->low);
3276 conf->port_max = ntohs(p->high);
3277 } else {
3278 return -EOPNOTSUPP;
3279 }
3280 }
3281
3282 if (data[IFLA_VXLAN_PORT]) {
3283 if (changelink)
3284 return -EOPNOTSUPP;
3285 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3286 }
3287
3288 if (data[IFLA_VXLAN_UDP_CSUM]) {
3289 if (changelink)
3290 return -EOPNOTSUPP;
3291 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3292 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3293 }
3294
3295 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3296 if (changelink)
3297 return -EOPNOTSUPP;
3298 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3299 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3300 }
3301
3302 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3303 if (changelink)
3304 return -EOPNOTSUPP;
3305 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3306 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3307 }
3308
3309 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3310 if (changelink)
3311 return -EOPNOTSUPP;
3312 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3313 conf->flags |= VXLAN_F_REMCSUM_TX;
3314 }
3315
3316 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3317 if (changelink)
3318 return -EOPNOTSUPP;
3319 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3320 conf->flags |= VXLAN_F_REMCSUM_RX;
3321 }
3322
3323 if (data[IFLA_VXLAN_GBP]) {
3324 if (changelink)
3325 return -EOPNOTSUPP;
3326 conf->flags |= VXLAN_F_GBP;
3327 }
3328
3329 if (data[IFLA_VXLAN_GPE]) {
3330 if (changelink)
3331 return -EOPNOTSUPP;
3332 conf->flags |= VXLAN_F_GPE;
3333 }
3334
3335 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3336 if (changelink)
3337 return -EOPNOTSUPP;
3338 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3339 }
3340
3341 if (tb[IFLA_MTU]) {
3342 if (changelink)
3343 return -EOPNOTSUPP;
3344 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3345 }
3346
3347 return 0;
3348}
3349
3350static int vxlan_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003351 struct nlattr *tb[], struct nlattr *data[],
3352 struct netlink_ext_ack *extack)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003353{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003354 struct vxlan_config conf;
3355 int err;
3356
3357 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3358 if (err)
3359 return err;
3360
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003361 return __vxlan_dev_create(src_net, dev, &conf);
stephen hemmingerd3428942012-10-01 12:32:35 +00003362}
3363
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003364static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02003365 struct nlattr *data[],
3366 struct netlink_ext_ack *extack)
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003367{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003368 struct vxlan_dev *vxlan = netdev_priv(dev);
3369 struct vxlan_rdst *dst = &vxlan->default_dst;
3370 struct vxlan_rdst old_dst;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003371 struct vxlan_config conf;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003372 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003373
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003374 err = vxlan_nl2conf(tb, data,
3375 dev, &conf, true);
3376 if (err)
3377 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07003378
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003379 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003380
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003381 err = vxlan_dev_configure(vxlan->net, dev, &conf, true);
3382 if (err)
3383 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003384
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003385 /* handle default dst entry */
3386 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3387 spin_lock_bh(&vxlan->hash_lock);
3388 if (!vxlan_addr_any(&old_dst.remote_ip))
3389 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3390 old_dst.remote_ip,
3391 vxlan->cfg.dst_port,
3392 old_dst.remote_vni,
3393 old_dst.remote_vni,
3394 old_dst.remote_ifindex, 0);
3395
3396 if (!vxlan_addr_any(&dst->remote_ip)) {
3397 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3398 &dst->remote_ip,
3399 NUD_REACHABLE | NUD_PERMANENT,
3400 NLM_F_CREATE | NLM_F_APPEND,
3401 vxlan->cfg.dst_port,
3402 dst->remote_vni,
3403 dst->remote_vni,
3404 dst->remote_ifindex,
3405 NTF_SELF);
3406 if (err) {
3407 spin_unlock_bh(&vxlan->hash_lock);
3408 return err;
3409 }
3410 }
3411 spin_unlock_bh(&vxlan->hash_lock);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003412 }
3413
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003414 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003415}
3416
stephen hemmingerd3428942012-10-01 12:32:35 +00003417static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3418{
3419 struct vxlan_dev *vxlan = netdev_priv(dev);
3420
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003421 vxlan_flush(vxlan, true);
3422
Tom Herbert58ce31c2015-08-19 17:07:33 -07003423 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003424 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003425 unregister_netdevice_queue(dev, head);
3426}
3427
3428static size_t vxlan_get_size(const struct net_device *dev)
3429{
3430
3431 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003432 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003433 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003434 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003435 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3436 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003437 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003438 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003439 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3440 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3441 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3442 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003443 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003444 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3445 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003446 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003447 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3448 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3449 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3450 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003451 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3452 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003453 0;
3454}
3455
3456static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3457{
3458 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003459 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003460 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003461 .low = htons(vxlan->cfg.port_min),
3462 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003463 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003464
Jiri Benc54bfd872016-02-16 21:58:58 +01003465 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003466 goto nla_put_failure;
3467
Cong Wange4c7ed42013-08-31 13:44:33 +08003468 if (!vxlan_addr_any(&dst->remote_ip)) {
3469 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003470 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3471 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003472 goto nla_put_failure;
3473#if IS_ENABLED(CONFIG_IPV6)
3474 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003475 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3476 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003477 goto nla_put_failure;
3478#endif
3479 }
3480 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003481
Atzm Watanabec7995c42013-04-16 02:50:52 +00003482 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003483 goto nla_put_failure;
3484
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003485 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3486 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003487 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003488 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003489 goto nla_put_failure;
3490#if IS_ENABLED(CONFIG_IPV6)
3491 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003492 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003493 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003494 goto nla_put_failure;
3495#endif
3496 }
3497 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003498
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003499 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3500 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003501 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003502 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003503 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003504 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003505 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
3506 nla_put_u8(skb, IFLA_VXLAN_RSC,
3507 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003508 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003509 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003510 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003511 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003512 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003513 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003514 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3515 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3516 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003517 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003518 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003519 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003520 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003521 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003522 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003523 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003524 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003525 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003526 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003527 goto nla_put_failure;
3528
stephen hemminger05f47d62012-10-09 20:35:50 +00003529 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3530 goto nla_put_failure;
3531
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003532 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01003533 nla_put_flag(skb, IFLA_VXLAN_GBP))
3534 goto nla_put_failure;
3535
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003536 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02003537 nla_put_flag(skb, IFLA_VXLAN_GPE))
3538 goto nla_put_failure;
3539
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003540 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003541 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3542 goto nla_put_failure;
3543
stephen hemmingerd3428942012-10-01 12:32:35 +00003544 return 0;
3545
3546nla_put_failure:
3547 return -EMSGSIZE;
3548}
3549
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003550static struct net *vxlan_get_link_net(const struct net_device *dev)
3551{
3552 struct vxlan_dev *vxlan = netdev_priv(dev);
3553
3554 return vxlan->net;
3555}
3556
stephen hemmingerd3428942012-10-01 12:32:35 +00003557static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3558 .kind = "vxlan",
3559 .maxtype = IFLA_VXLAN_MAX,
3560 .policy = vxlan_policy,
3561 .priv_size = sizeof(struct vxlan_dev),
3562 .setup = vxlan_setup,
3563 .validate = vxlan_validate,
3564 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003565 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00003566 .dellink = vxlan_dellink,
3567 .get_size = vxlan_get_size,
3568 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003569 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003570};
3571
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003572struct net_device *vxlan_dev_create(struct net *net, const char *name,
3573 u8 name_assign_type,
3574 struct vxlan_config *conf)
3575{
3576 struct nlattr *tb[IFLA_MAX + 1];
3577 struct net_device *dev;
3578 int err;
3579
3580 memset(&tb, 0, sizeof(tb));
3581
3582 dev = rtnl_create_link(net, name, name_assign_type,
3583 &vxlan_link_ops, tb);
3584 if (IS_ERR(dev))
3585 return dev;
3586
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003587 err = __vxlan_dev_create(net, dev, conf);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003588 if (err < 0) {
3589 free_netdev(dev);
3590 return ERR_PTR(err);
3591 }
3592
3593 err = rtnl_configure_link(dev, NULL);
3594 if (err < 0) {
3595 LIST_HEAD(list_kill);
3596
3597 vxlan_dellink(dev, &list_kill);
3598 unregister_netdevice_many(&list_kill);
3599 return ERR_PTR(err);
3600 }
3601
3602 return dev;
3603}
3604EXPORT_SYMBOL_GPL(vxlan_dev_create);
3605
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003606static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3607 struct net_device *dev)
3608{
3609 struct vxlan_dev *vxlan, *next;
3610 LIST_HEAD(list_kill);
3611
3612 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3613 struct vxlan_rdst *dst = &vxlan->default_dst;
3614
3615 /* In case we created vxlan device with carrier
3616 * and we loose the carrier due to module unload
3617 * we also need to remove vxlan device. In other
3618 * cases, it's not necessary and remote_ifindex
3619 * is 0 here, so no matches.
3620 */
3621 if (dst->remote_ifindex == dev->ifindex)
3622 vxlan_dellink(vxlan->dev, &list_kill);
3623 }
3624
3625 unregister_netdevice_many(&list_kill);
3626}
3627
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003628static int vxlan_netdevice_event(struct notifier_block *unused,
3629 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003630{
3631 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003632 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003633
Daniel Borkmann783c1462014-01-22 21:07:53 +01003634 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003635 vxlan_handle_lowerdev_unregister(vn, dev);
Alexander Duyck7c46a642016-06-16 12:21:00 -07003636 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003637 vxlan_push_rx_ports(dev);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003638
3639 return NOTIFY_DONE;
3640}
3641
3642static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003643 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003644};
3645
stephen hemmingerd3428942012-10-01 12:32:35 +00003646static __net_init int vxlan_init_net(struct net *net)
3647{
3648 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003649 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003650
stephen hemminger553675f2013-05-16 11:35:20 +00003651 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003652 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003653
stephen hemminger553675f2013-05-16 11:35:20 +00003654 for (h = 0; h < PORT_HASH_SIZE; ++h)
3655 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003656
3657 return 0;
3658}
3659
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003660static void __net_exit vxlan_exit_net(struct net *net)
3661{
3662 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3663 struct vxlan_dev *vxlan, *next;
3664 struct net_device *dev, *aux;
3665 LIST_HEAD(list);
3666
3667 rtnl_lock();
3668 for_each_netdev_safe(net, dev, aux)
3669 if (dev->rtnl_link_ops == &vxlan_link_ops)
3670 unregister_netdevice_queue(dev, &list);
3671
3672 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3673 /* If vxlan->dev is in the same netns, it has already been added
3674 * to the list by the previous loop.
3675 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003676 if (!net_eq(dev_net(vxlan->dev), net)) {
3677 gro_cells_destroy(&vxlan->gro_cells);
John W. Linville13c3ed62015-05-18 13:51:24 -04003678 unregister_netdevice_queue(vxlan->dev, &list);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003679 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003680 }
3681
3682 unregister_netdevice_many(&list);
3683 rtnl_unlock();
3684}
3685
stephen hemmingerd3428942012-10-01 12:32:35 +00003686static struct pernet_operations vxlan_net_ops = {
3687 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003688 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003689 .id = &vxlan_net_id,
3690 .size = sizeof(struct vxlan_net),
3691};
3692
3693static int __init vxlan_init_module(void)
3694{
3695 int rc;
3696
3697 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3698
Daniel Borkmann783c1462014-01-22 21:07:53 +01003699 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003700 if (rc)
3701 goto out1;
3702
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003703 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003704 if (rc)
3705 goto out2;
3706
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003707 rc = rtnl_link_register(&vxlan_link_ops);
3708 if (rc)
3709 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003710
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003711 return 0;
3712out3:
3713 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003714out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003715 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003716out1:
3717 return rc;
3718}
Cong Wang7332a132013-05-27 22:35:53 +00003719late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003720
3721static void __exit vxlan_cleanup_module(void)
3722{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003723 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003724 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003725 unregister_pernet_subsys(&vxlan_net_ops);
3726 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003727}
3728module_exit(vxlan_cleanup_module);
3729
3730MODULE_LICENSE("GPL");
3731MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003732MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003733MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003734MODULE_ALIAS_RTNL_LINK("vxlan");