blob: 55c4408892be46f8224ed5f5fb7cdc84daa683e5 [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
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070055static int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020056static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000057
Li RongQing7256eac2016-01-29 09:43:47 +080058static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030059
Jiri Benc205f3562015-09-24 13:50:01 +020060static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020061
Mark Blochc242e1a2017-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 */
David Stevensae884082013-04-19 00:36:26 +000080 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +000081};
82
stephen hemmingerd3428942012-10-01 12:32:35 +000083/* salt for hash table */
84static u32 vxlan_salt __read_mostly;
85
Thomas Grafee122c72015-07-21 10:43:58 +020086static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
87{
Thomas Grafe7030872015-07-21 10:44:01 +020088 return vs->flags & VXLAN_F_COLLECT_METADATA ||
89 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020090}
91
Cong Wange4c7ed42013-08-31 13:44:33 +080092#if IS_ENABLED(CONFIG_IPV6)
93static inline
94bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
95{
Jiri Bencf0ef3122015-03-29 16:17:37 +020096 if (a->sa.sa_family != b->sa.sa_family)
97 return false;
98 if (a->sa.sa_family == AF_INET6)
99 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
100 else
101 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800102}
103
104static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
105{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200106 if (ipa->sa.sa_family == AF_INET6)
107 return ipv6_addr_any(&ipa->sin6.sin6_addr);
108 else
109 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800110}
111
112static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
113{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200114 if (ipa->sa.sa_family == AF_INET6)
115 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
116 else
117 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800118}
119
120static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
121{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200122 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200123 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200124 ip->sa.sa_family = AF_INET6;
125 return 0;
126 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200127 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200128 ip->sa.sa_family = AF_INET;
129 return 0;
130 } else {
131 return -EAFNOSUPPORT;
132 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800133}
134
135static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200136 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800137{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200138 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200139 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200140 else
Jiri Benc930345e2015-03-29 16:59:25 +0200141 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800142}
143
144#else /* !CONFIG_IPV6 */
145
146static inline
147bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
148{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200149 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800150}
151
152static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
153{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200154 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800155}
156
157static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
158{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200159 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800160}
161
162static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
163{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200164 if (nla_len(nla) >= sizeof(struct in6_addr)) {
165 return -EAFNOSUPPORT;
166 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200167 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200168 ip->sa.sa_family = AF_INET;
169 return 0;
170 } else {
171 return -EAFNOSUPPORT;
172 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800173}
174
175static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200176 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800177{
Jiri Benc930345e2015-03-29 16:59:25 +0200178 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800179}
180#endif
181
stephen hemminger553675f2013-05-16 11:35:20 +0000182/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100183static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000184{
Jiri Benc54bfd872016-02-16 21:58:58 +0100185 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000186}
187
188/* Socket hash table head */
189static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000190{
191 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
192
stephen hemminger553675f2013-05-16 11:35:20 +0000193 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
194}
195
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700196/* First remote destination for a forwarding entry.
197 * Guaranteed to be non-NULL because remotes are never deleted.
198 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700199static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700200{
stephen hemminger5ca54612013-08-04 17:17:39 -0700201 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
202}
203
204static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
205{
206 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700207}
208
Thomas Grafac5132d2015-01-15 03:53:56 +0100209/* Find VXLAN socket based on network namespace, address family and UDP port
210 * and enabled unshareable flags.
211 */
212static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
213 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000214{
215 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800216
217 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000218
219 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200220 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200221 vxlan_get_sk_family(vs) == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800222 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000223 return vs;
224 }
225 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000226}
227
Jiri Benc54bfd872016-02-16 21:58:58 +0100228static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000229{
230 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000231
Jiri Bencb9167b22016-02-16 21:59:03 +0100232 /* For flow based devices, map all packets to VNI 0 */
233 if (vs->flags & VXLAN_F_COLLECT_METADATA)
234 vni = 0;
235
Jiri Benc54bfd872016-02-16 21:58:58 +0100236 hlist_for_each_entry_rcu(vxlan, vni_head(vs, vni), hlist) {
237 if (vxlan->default_dst.remote_vni == vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000238 return vxlan;
239 }
240
241 return NULL;
242}
243
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700244/* Look up VNI in a per net namespace table */
Jiri Benc54bfd872016-02-16 21:58:58 +0100245static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni,
Thomas Grafac5132d2015-01-15 03:53:56 +0100246 sa_family_t family, __be16 port,
247 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700248{
249 struct vxlan_sock *vs;
250
Thomas Grafac5132d2015-01-15 03:53:56 +0100251 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700252 if (!vs)
253 return NULL;
254
Jiri Benc54bfd872016-02-16 21:58:58 +0100255 return vxlan_vs_find_vni(vs, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700256}
257
stephen hemmingerd3428942012-10-01 12:32:35 +0000258/* Fill in neighbour message in skbuff. */
259static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700260 const struct vxlan_fdb *fdb,
261 u32 portid, u32 seq, int type, unsigned int flags,
262 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000263{
264 unsigned long now = jiffies;
265 struct nda_cacheinfo ci;
266 struct nlmsghdr *nlh;
267 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000268 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000269
270 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
271 if (nlh == NULL)
272 return -EMSGSIZE;
273
274 ndm = nlmsg_data(nlh);
275 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000276
277 send_eth = send_ip = true;
278
279 if (type == RTM_GETNEIGH) {
280 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800281 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000282 send_eth = !is_zero_ether_addr(fdb->eth_addr);
283 } else
284 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000285 ndm->ndm_state = fdb->state;
286 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000287 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800288 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000289
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100290 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100291 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700292 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100293 goto nla_put_failure;
294
David Stevense4f67ad2012-11-20 02:50:14 +0000295 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000296 goto nla_put_failure;
297
Cong Wange4c7ed42013-08-31 13:44:33 +0800298 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000299 goto nla_put_failure;
300
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200301 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000302 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
303 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000304 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100305 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000306 goto nla_put_failure;
307 if (rdst->remote_ifindex &&
308 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000309 goto nla_put_failure;
310
311 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
312 ci.ndm_confirmed = 0;
313 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
314 ci.ndm_refcnt = 0;
315
316 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
317 goto nla_put_failure;
318
Johannes Berg053c0952015-01-16 22:09:00 +0100319 nlmsg_end(skb, nlh);
320 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000321
322nla_put_failure:
323 nlmsg_cancel(skb, nlh);
324 return -EMSGSIZE;
325}
326
327static inline size_t vxlan_nlmsg_size(void)
328{
329 return NLMSG_ALIGN(sizeof(struct ndmsg))
330 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800331 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000332 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000333 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
334 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100335 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000336 + nla_total_size(sizeof(struct nda_cacheinfo));
337}
338
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200339static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
340 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000341{
342 struct net *net = dev_net(vxlan->dev);
343 struct sk_buff *skb;
344 int err = -ENOBUFS;
345
346 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
347 if (skb == NULL)
348 goto errout;
349
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200350 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000351 if (err < 0) {
352 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
353 WARN_ON(err == -EMSGSIZE);
354 kfree_skb(skb);
355 goto errout;
356 }
357
358 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
359 return;
360errout:
361 if (err < 0)
362 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
363}
364
Cong Wange4c7ed42013-08-31 13:44:33 +0800365static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000366{
367 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700368 struct vxlan_fdb f = {
369 .state = NUD_STALE,
370 };
371 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800372 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100373 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700374 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700375
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200376 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000377}
378
379static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
380{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700381 struct vxlan_fdb f = {
382 .state = NUD_STALE,
383 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200384 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000385
David Stevense4f67ad2012-11-20 02:50:14 +0000386 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
387
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200388 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000389}
390
stephen hemmingerd3428942012-10-01 12:32:35 +0000391/* Hash Ethernet address */
392static u32 eth_hash(const unsigned char *addr)
393{
394 u64 value = get_unaligned((u64 *)addr);
395
396 /* only want 6 bytes */
397#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000398 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000399#else
400 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000401#endif
402 return hash_64(value, FDB_HASH_BITS);
403}
404
405/* Hash chain to use given mac address */
406static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
407 const u8 *mac)
408{
409 return &vxlan->fdb_head[eth_hash(mac)];
410}
411
412/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000413static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000414 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000415{
416 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
417 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000418
Sasha Levinb67bfe02013-02-27 17:06:00 -0800419 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700420 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000421 return f;
422 }
423
424 return NULL;
425}
426
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000427static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
428 const u8 *mac)
429{
430 struct vxlan_fdb *f;
431
432 f = __vxlan_find_mac(vxlan, mac);
433 if (f)
434 f->used = jiffies;
435
436 return f;
437}
438
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300439/* caller should hold vxlan->hash_lock */
440static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800441 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100442 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300443{
444 struct vxlan_rdst *rd;
445
446 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800447 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300448 rd->remote_port == port &&
449 rd->remote_vni == vni &&
450 rd->remote_ifindex == ifindex)
451 return rd;
452 }
453
454 return NULL;
455}
456
Thomas Richter906dc182013-07-19 17:20:07 +0200457/* Replace destination of unicast mac */
458static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100459 union vxlan_addr *ip, __be16 port, __be32 vni,
460 __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200461{
462 struct vxlan_rdst *rd;
463
464 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
465 if (rd)
466 return 0;
467
468 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
469 if (!rd)
470 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100471
472 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800473 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200474 rd->remote_port = port;
475 rd->remote_vni = vni;
476 rd->remote_ifindex = ifindex;
477 return 1;
478}
479
David Stevens66817122013-03-15 04:35:51 +0000480/* Add/update destinations for multicast */
481static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100482 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200483 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000484{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700485 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000486
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300487 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
488 if (rd)
489 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700490
David Stevens66817122013-03-15 04:35:51 +0000491 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
492 if (rd == NULL)
493 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100494
495 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
496 kfree(rd);
497 return -ENOBUFS;
498 }
499
Cong Wange4c7ed42013-08-31 13:44:33 +0800500 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000501 rd->remote_port = port;
502 rd->remote_vni = vni;
503 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700504
505 list_add_tail_rcu(&rd->list, &f->remotes);
506
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200507 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000508 return 1;
509}
510
Tom Herbertdfd86452015-01-12 17:00:38 -0800511static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
512 unsigned int off,
513 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100514 __be32 vni_field,
515 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800516 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800517{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700518 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800519
520 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700521 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800522
523 if (!NAPI_GRO_CB(skb)->csum_valid)
524 return NULL;
525
Jiri Benc54bfd872016-02-16 21:58:58 +0100526 start = vxlan_rco_start(vni_field);
527 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800528
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700529 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
530 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800531
532 skb->remcsum_offload = 1;
533
534 return vh;
535}
536
Tom Herbert5602c482016-04-05 08:22:53 -0700537static struct sk_buff **vxlan_gro_receive(struct sock *sk,
538 struct sk_buff **head,
539 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200540{
541 struct sk_buff *p, **pp = NULL;
542 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800543 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200544 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700545 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100546 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800547 struct gro_remcsum grc;
548
549 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200550
551 off_vx = skb_gro_offset(skb);
552 hlen = off_vx + sizeof(*vh);
553 vh = skb_gro_header_fast(skb, off_vx);
554 if (skb_gro_header_hard(skb, hlen)) {
555 vh = skb_gro_header_slow(skb, hlen, off_vx);
556 if (unlikely(!vh))
557 goto out;
558 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200559
Tom Herbertdfd86452015-01-12 17:00:38 -0800560 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
561
Jiri Benc54bfd872016-02-16 21:58:58 +0100562 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800563
564 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
565 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100566 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800567 !!(vs->flags &
568 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800569
570 if (!vh)
571 goto out;
572 }
573
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700574 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
575
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200576 for (p = *head; p; p = p->next) {
577 if (!NAPI_GRO_CB(p)->same_flow)
578 continue;
579
580 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100581 if (vh->vx_flags != vh2->vx_flags ||
582 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200583 NAPI_GRO_CB(p)->same_flow = 0;
584 continue;
585 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200586 }
587
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200588 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800589 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200590
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200591out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800592 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200593 NAPI_GRO_CB(skb)->flush |= flush;
594
595 return pp;
596}
597
Tom Herbert5602c482016-04-05 08:22:53 -0700598static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200599{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700600 /* Sets 'skb->inner_mac_header' since we are always called with
601 * 'skb->encapsulation' set.
602 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800603 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200604}
605
stephen hemmingerd3428942012-10-01 12:32:35 +0000606/* Add new entry to forwarding table -- assumes lock held */
607static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800608 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000609 __u16 state, __u16 flags,
Jiri Benc54bfd872016-02-16 21:58:58 +0100610 __be16 port, __be32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000611 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000612{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200613 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000614 struct vxlan_fdb *f;
615 int notify = 0;
Haishuang Yan17b46362016-11-29 09:59:36 +0800616 int rc;
stephen hemmingerd3428942012-10-01 12:32:35 +0000617
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000618 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000619 if (f) {
620 if (flags & NLM_F_EXCL) {
621 netdev_dbg(vxlan->dev,
622 "lost race to create %pM\n", mac);
623 return -EEXIST;
624 }
625 if (f->state != state) {
626 f->state = state;
627 f->updated = jiffies;
628 notify = 1;
629 }
David Stevensae884082013-04-19 00:36:26 +0000630 if (f->flags != ndm_flags) {
631 f->flags = ndm_flags;
632 f->updated = jiffies;
633 notify = 1;
634 }
Thomas Richter906dc182013-07-19 17:20:07 +0200635 if ((flags & NLM_F_REPLACE)) {
636 /* Only change unicasts */
637 if (!(is_multicast_ether_addr(f->eth_addr) ||
638 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800639 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200640 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200641 } else
642 return -EOPNOTSUPP;
643 }
David Stevens66817122013-03-15 04:35:51 +0000644 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300645 (is_multicast_ether_addr(f->eth_addr) ||
646 is_zero_ether_addr(f->eth_addr))) {
Haishuang Yan17b46362016-11-29 09:59:36 +0800647 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
David Stevens66817122013-03-15 04:35:51 +0000648
649 if (rc < 0)
650 return rc;
651 notify |= rc;
652 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000653 } else {
654 if (!(flags & NLM_F_CREATE))
655 return -ENOENT;
656
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200657 if (vxlan->cfg.addrmax &&
658 vxlan->addrcnt >= vxlan->cfg.addrmax)
stephen hemmingerd3428942012-10-01 12:32:35 +0000659 return -ENOSPC;
660
Thomas Richter906dc182013-07-19 17:20:07 +0200661 /* Disallow replace to add a multicast entry */
662 if ((flags & NLM_F_REPLACE) &&
663 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
664 return -EOPNOTSUPP;
665
Cong Wange4c7ed42013-08-31 13:44:33 +0800666 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000667 f = kmalloc(sizeof(*f), GFP_ATOMIC);
668 if (!f)
669 return -ENOMEM;
670
671 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000672 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000673 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000674 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700675 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000676 memcpy(f->eth_addr, mac, ETH_ALEN);
677
Haishuang Yan17b46362016-11-29 09:59:36 +0800678 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
679 if (rc < 0) {
680 kfree(f);
681 return rc;
682 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700683
stephen hemmingerd3428942012-10-01 12:32:35 +0000684 ++vxlan->addrcnt;
685 hlist_add_head_rcu(&f->hlist,
686 vxlan_fdb_head(vxlan, mac));
687 }
688
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200689 if (notify) {
690 if (rd == NULL)
691 rd = first_remote_rtnl(f);
692 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
693 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000694
695 return 0;
696}
697
Wei Yongjun6706c822013-04-11 19:00:35 +0000698static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000699{
700 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700701 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000702
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100703 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
704 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000705 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100706 }
David Stevens66817122013-03-15 04:35:51 +0000707 kfree(f);
708}
709
stephen hemmingerd3428942012-10-01 12:32:35 +0000710static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
711{
712 netdev_dbg(vxlan->dev,
713 "delete %pM\n", f->eth_addr);
714
715 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200716 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000717
718 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000719 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000720}
721
Lance Richardsonb5e9b7a2017-05-29 13:25:57 -0400722static void vxlan_dst_free(struct rcu_head *head)
723{
724 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
725
726 dst_cache_destroy(&rd->dst_cache);
727 kfree(rd);
728}
729
730static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
731 struct vxlan_rdst *rd)
732{
733 list_del_rcu(&rd->list);
734 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
735 call_rcu(&rd->rcu, vxlan_dst_free);
736}
737
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300738static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Jiri Benc54bfd872016-02-16 21:58:58 +0100739 union vxlan_addr *ip, __be16 *port, __be32 *vni,
740 u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300741{
742 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800743 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300744
745 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800746 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
747 if (err)
748 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300749 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800750 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
751 if (remote->sa.sa_family == AF_INET) {
752 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
753 ip->sa.sa_family = AF_INET;
754#if IS_ENABLED(CONFIG_IPV6)
755 } else {
756 ip->sin6.sin6_addr = in6addr_any;
757 ip->sa.sa_family = AF_INET6;
758#endif
759 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300760 }
761
762 if (tb[NDA_PORT]) {
763 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
764 return -EINVAL;
765 *port = nla_get_be16(tb[NDA_PORT]);
766 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200767 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300768 }
769
770 if (tb[NDA_VNI]) {
771 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
772 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100773 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300774 } else {
775 *vni = vxlan->default_dst.remote_vni;
776 }
777
778 if (tb[NDA_IFINDEX]) {
779 struct net_device *tdev;
780
781 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
782 return -EINVAL;
783 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800784 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300785 if (!tdev)
786 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300787 } else {
788 *ifindex = 0;
789 }
790
791 return 0;
792}
793
stephen hemmingerd3428942012-10-01 12:32:35 +0000794/* Add static entry (via netlink) */
795static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
796 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100797 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000798{
799 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300800 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800801 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000802 __be16 port;
Jiri Benc54bfd872016-02-16 21:58:58 +0100803 __be32 vni;
804 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000805 int err;
806
807 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
808 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
809 ndm->ndm_state);
810 return -EINVAL;
811 }
812
813 if (tb[NDA_DST] == NULL)
814 return -EINVAL;
815
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300816 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
817 if (err)
818 return err;
David Stevens66817122013-03-15 04:35:51 +0000819
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300820 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
821 return -EAFNOSUPPORT;
822
stephen hemmingerd3428942012-10-01 12:32:35 +0000823 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800824 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000825 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000826 spin_unlock_bh(&vxlan->hash_lock);
827
828 return err;
829}
830
831/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000832static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
833 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100834 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000835{
836 struct vxlan_dev *vxlan = netdev_priv(dev);
837 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300838 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800839 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300840 __be16 port;
Jiri Benc54bfd872016-02-16 21:58:58 +0100841 __be32 vni;
842 u32 ifindex;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300843 int err;
844
845 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
846 if (err)
847 return err;
848
849 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000850
851 spin_lock_bh(&vxlan->hash_lock);
852 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300853 if (!f)
854 goto out;
855
Cong Wange4c7ed42013-08-31 13:44:33 +0800856 if (!vxlan_addr_any(&ip)) {
857 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300858 if (!rd)
859 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000860 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300861
862 err = 0;
863
864 /* remove a destination if it's not the only one on the list,
865 * otherwise destroy the fdb entry
866 */
867 if (rd && !list_is_singular(&f->remotes)) {
Lance Richardsonb5e9b7a2017-05-29 13:25:57 -0400868 vxlan_fdb_dst_destroy(vxlan, f, rd);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300869 goto out;
870 }
871
872 vxlan_fdb_destroy(vxlan, f);
873
874out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000875 spin_unlock_bh(&vxlan->hash_lock);
876
877 return err;
878}
879
880/* Dump forwarding table */
881static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400882 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700883 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000884{
885 struct vxlan_dev *vxlan = netdev_priv(dev);
886 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -0700887 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000888
889 for (h = 0; h < FDB_HASH_SIZE; ++h) {
890 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000891
Sasha Levinb67bfe02013-02-27 17:06:00 -0800892 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000893 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000894
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700895 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -0700896 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900897 goto skip;
898
David Stevens66817122013-03-15 04:35:51 +0000899 err = vxlan_fdb_info(skb, vxlan, f,
900 NETLINK_CB(cb->skb).portid,
901 cb->nlh->nlmsg_seq,
902 RTM_NEWNEIGH,
903 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -0700904 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700905 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700906skip:
Roopa Prabhud2976532016-08-30 21:56:45 -0700907 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900908 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000909 }
910 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700911out:
Roopa Prabhud2976532016-08-30 21:56:45 -0700912 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +0000913}
914
915/* Watch incoming packets to learn mapping between Ethernet address
916 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900917 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000918 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700919static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800920 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000921{
922 struct vxlan_dev *vxlan = netdev_priv(dev);
923 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000924
925 f = vxlan_find_mac(vxlan, src_mac);
926 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700927 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700928
Cong Wange4c7ed42013-08-31 13:44:33 +0800929 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700930 return false;
931
932 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700933 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700934 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000935
936 if (net_ratelimit())
937 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800938 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +0100939 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +0000940
Cong Wange4c7ed42013-08-31 13:44:33 +0800941 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000942 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200943 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000944 } else {
945 /* learned new entry */
946 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700947
948 /* close off race between vxlan_flush and incoming packets */
949 if (netif_running(dev))
950 vxlan_fdb_create(vxlan, src_mac, src_ip,
951 NUD_REACHABLE,
952 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200953 vxlan->cfg.dst_port,
stephen hemminger3bf74b12013-06-17 12:09:57 -0700954 vxlan->default_dst.remote_vni,
955 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000956 spin_unlock(&vxlan->hash_lock);
957 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700958
959 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000960}
961
stephen hemmingerd3428942012-10-01 12:32:35 +0000962/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +0800963static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +0000964{
stephen hemminger553675f2013-05-16 11:35:20 +0000965 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -0700966 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +0100967#if IS_ENABLED(CONFIG_IPV6)
968 struct vxlan_sock *sock6;
969#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +0200970 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +0000971
pravin shelarc6fcc4f2016-10-28 09:59:15 -0700972 sock4 = rtnl_dereference(dev->vn4_sock);
973
Gao feng95ab0992013-12-10 16:37:33 +0800974 /* The vxlan_sock is only used by dev, leaving group has
975 * no effect on other vxlan devices.
976 */
pravin shelarc6fcc4f2016-10-28 09:59:15 -0700977 if (family == AF_INET && sock4 && atomic_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +0800978 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +0200979#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -0700980 sock6 = rtnl_dereference(dev->vn6_sock);
981 if (family == AF_INET6 && sock6 && atomic_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +0200982 return false;
983#endif
Gao feng95ab0992013-12-10 16:37:33 +0800984
stephen hemminger553675f2013-05-16 11:35:20 +0000985 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +0800986 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +0000987 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000988
pravin shelarc6fcc4f2016-10-28 09:59:15 -0700989 if (family == AF_INET &&
990 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +0800991 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +0200992#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -0700993 if (family == AF_INET6 &&
994 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +0200995 continue;
996#endif
Gao feng95ab0992013-12-10 16:37:33 +0800997
998 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
999 &dev->default_dst.remote_ip))
1000 continue;
1001
1002 if (vxlan->default_dst.remote_ifindex !=
1003 dev->default_dst.remote_ifindex)
1004 continue;
1005
1006 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001007 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001008
1009 return false;
1010}
1011
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001012static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001013{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001014 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001015
Jiri Bencb1be00a2015-09-24 13:50:02 +02001016 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001017 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001018 if (!atomic_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001019 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001020
Jiri Bencb1be00a2015-09-24 13:50:02 +02001021 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001022 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001023 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001024 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001025 (vs->flags & VXLAN_F_GPE) ?
1026 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001027 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001028 spin_unlock(&vn->sock_lock);
1029
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001030 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001031}
1032
Jiri Bencb1be00a2015-09-24 13:50:02 +02001033static void vxlan_sock_release(struct vxlan_dev *vxlan)
1034{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001035 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001036#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001037 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1038
1039 rcu_assign_pointer(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001040#endif
1041
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001042 rcu_assign_pointer(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001043 synchronize_net();
1044
Mark Blochc242e1a2017-06-02 03:24:08 +03001045 vxlan_vs_del_dev(vxlan);
1046
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001047 if (__vxlan_sock_release_prep(sock4)) {
1048 udp_tunnel_sock_release(sock4->sock);
1049 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001050 }
1051
1052#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001053 if (__vxlan_sock_release_prep(sock6)) {
1054 udp_tunnel_sock_release(sock6->sock);
1055 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001056 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001057#endif
1058}
1059
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001060/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001061 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001062 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001063static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001064{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001065 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001066 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1067 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001068 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001069
Cong Wange4c7ed42013-08-31 13:44:33 +08001070 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001071 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001072 struct ip_mreqn mreq = {
1073 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1074 .imr_ifindex = ifindex,
1075 };
1076
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001077 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001078 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001079 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001080 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001081#if IS_ENABLED(CONFIG_IPV6)
1082 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001083 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1084
1085 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001086 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001087 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1088 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001089 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001090#endif
1091 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001092
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001093 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001094}
1095
1096/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001097static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001098{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001099 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001100 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1101 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001102 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001103
Cong Wange4c7ed42013-08-31 13:44:33 +08001104 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001105 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001106 struct ip_mreqn mreq = {
1107 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1108 .imr_ifindex = ifindex,
1109 };
1110
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001111 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001112 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001113 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001114 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001115#if IS_ENABLED(CONFIG_IPV6)
1116 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001117 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1118
1119 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001120 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001121 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1122 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001123 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001124#endif
1125 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001126
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001127 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001128}
1129
Jiri Bencf14eceb2016-02-16 21:59:01 +01001130static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1131 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001132{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001133 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001134
Jiri Bencf14eceb2016-02-16 21:59:01 +01001135 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1136 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001137
Jiri Bencf14eceb2016-02-16 21:59:01 +01001138 start = vxlan_rco_start(unparsed->vx_vni);
1139 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001140
Jiri Benc7d34fa72016-03-21 17:50:05 +01001141 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001142 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001143
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001144 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1145 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001146out:
1147 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1148 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001149 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001150}
1151
Jiri Bencf14eceb2016-02-16 21:59:01 +01001152static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001153 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001154 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001155{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001156 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001157 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001158
Jiri Bencf14eceb2016-02-16 21:59:01 +01001159 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1160 goto out;
1161
Jiri Benc3288af02016-02-16 21:59:00 +01001162 md->gbp = ntohs(gbp->policy_id);
1163
Jiri Benc10a5af22016-02-23 18:02:59 +01001164 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001165 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001166 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001167 tun_dst->u.tun_info.options_len = sizeof(*md);
1168 }
Jiri Benc3288af02016-02-16 21:59:00 +01001169 if (gbp->dont_learn)
1170 md->gbp |= VXLAN_GBP_DONT_LEARN;
1171
1172 if (gbp->policy_applied)
1173 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001174
Jiri Benc64f87d32016-02-23 18:02:55 +01001175 /* In flow-based mode, GBP is carried in dst_metadata */
1176 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1177 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001178out:
1179 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001180}
1181
Jiri Bence1e53142016-04-05 14:47:13 +02001182static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001183 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001184 struct sk_buff *skb, u32 vxflags)
1185{
1186 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1187
1188 /* Need to have Next Protocol set for interfaces in GPE mode. */
1189 if (!gpe->np_applied)
1190 return false;
1191 /* "The initial version is 0. If a receiver does not support the
1192 * version indicated it MUST drop the packet.
1193 */
1194 if (gpe->version != 0)
1195 return false;
1196 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1197 * processing MUST occur." However, we don't implement OAM
1198 * processing, thus drop the packet.
1199 */
1200 if (gpe->oam_flag)
1201 return false;
1202
1203 switch (gpe->next_protocol) {
1204 case VXLAN_GPE_NP_IPV4:
1205 *protocol = htons(ETH_P_IP);
1206 break;
1207 case VXLAN_GPE_NP_IPV6:
1208 *protocol = htons(ETH_P_IPV6);
1209 break;
1210 case VXLAN_GPE_NP_ETHERNET:
1211 *protocol = htons(ETH_P_TEB);
1212 break;
1213 default:
1214 return false;
1215 }
1216
1217 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1218 return true;
1219}
1220
Jiri Benc1ab016e2016-02-23 18:02:56 +01001221static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1222 struct vxlan_sock *vs,
1223 struct sk_buff *skb)
Thomas Graf614732e2015-07-21 10:44:06 +02001224{
Thomas Graf614732e2015-07-21 10:44:06 +02001225 union vxlan_addr saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001226
Thomas Graf614732e2015-07-21 10:44:06 +02001227 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001228 skb->protocol = eth_type_trans(skb, vxlan->dev);
1229 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1230
1231 /* Ignore packet loops (and multicast echo) */
1232 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001233 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001234
Jiri Benc760c6802016-02-23 18:02:57 +01001235 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001236 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001237 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001238 saddr.sa.sa_family = AF_INET;
1239#if IS_ENABLED(CONFIG_IPV6)
1240 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001241 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001242 saddr.sa.sa_family = AF_INET6;
1243#endif
1244 }
1245
Jiri Benc1ab016e2016-02-23 18:02:56 +01001246 if ((vxlan->flags & VXLAN_F_LEARN) &&
1247 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1248 return false;
1249
1250 return true;
1251}
1252
Jiri Benc760c6802016-02-23 18:02:57 +01001253static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1254 struct sk_buff *skb)
1255{
1256 int err = 0;
1257
1258 if (vxlan_get_sk_family(vs) == AF_INET)
1259 err = IP_ECN_decapsulate(oiph, skb);
1260#if IS_ENABLED(CONFIG_IPV6)
1261 else
1262 err = IP6_ECN_decapsulate(oiph, skb);
1263#endif
1264
1265 if (unlikely(err) && log_ecn_error) {
1266 if (vxlan_get_sk_family(vs) == AF_INET)
1267 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1268 &((struct iphdr *)oiph)->saddr,
1269 ((struct iphdr *)oiph)->tos);
1270 else
1271 net_info_ratelimited("non-ECT from %pI6\n",
1272 &((struct ipv6hdr *)oiph)->saddr);
1273 }
1274 return err <= 1;
1275}
1276
stephen hemmingerd3428942012-10-01 12:32:35 +00001277/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001278static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001279{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001280 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001281 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001282 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001283 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001284 struct vxlan_metadata _md;
1285 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001286 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001287 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001288 void *oiph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001289
Jiri Bence1e53142016-04-05 14:47:13 +02001290 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001291 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001292 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001293
Jiri Bencf14eceb2016-02-16 21:59:01 +01001294 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001295 /* VNI flag always required to be set */
1296 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1297 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1298 ntohl(vxlan_hdr(skb)->vx_flags),
1299 ntohl(vxlan_hdr(skb)->vx_vni));
1300 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001301 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001302 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001303 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1304 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001305
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001306 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001307 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001308 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001309
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001310 vxlan = vxlan_vs_find_vni(vs, vxlan_vni(vxlan_hdr(skb)->vx_vni));
1311 if (!vxlan)
1312 goto drop;
1313
Jiri Bence1e53142016-04-05 14:47:13 +02001314 /* For backwards compatibility, only allow reserved fields to be
1315 * used by VXLAN extensions if explicitly requested.
1316 */
1317 if (vs->flags & VXLAN_F_GPE) {
1318 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1319 goto drop;
1320 raw_proto = true;
1321 }
1322
1323 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1324 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1325 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001326
Thomas Grafee122c72015-07-21 10:43:58 +02001327 if (vxlan_collect_metadata(vs)) {
Jiri Benc07dabf22016-02-18 19:19:29 +01001328 __be32 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
Jiri Benc10a5af22016-02-23 18:02:59 +01001329 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001330
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001331 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001332 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001333
Thomas Grafee122c72015-07-21 10:43:58 +02001334 if (!tun_dst)
1335 goto drop;
1336
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001337 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001338
1339 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001340 } else {
1341 memset(md, 0, sizeof(*md));
1342 }
1343
Jiri Bencf14eceb2016-02-16 21:59:01 +01001344 if (vs->flags & VXLAN_F_REMCSUM_RX)
1345 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1346 goto drop;
1347 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001348 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001349 /* Note that GBP and GPE can never be active together. This is
1350 * ensured in vxlan_dev_configure.
1351 */
Thomas Graf35114942015-01-15 03:53:55 +01001352
Jiri Bencf14eceb2016-02-16 21:59:01 +01001353 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001354 /* If there are any unprocessed flags remaining treat
1355 * this as a malformed packet. This behavior diverges from
1356 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1357 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001358 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001359 * is more robust and provides a little more security in
1360 * adding extensions to VXLAN.
1361 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001362 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001363 }
1364
Jiri Bence1e53142016-04-05 14:47:13 +02001365 if (!raw_proto) {
1366 if (!vxlan_set_mac(vxlan, vs, skb))
1367 goto drop;
1368 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001369 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001370 skb->dev = vxlan->dev;
1371 skb->pkt_type = PACKET_HOST;
1372 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001373
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001374 oiph = skb_network_header(skb);
1375 skb_reset_network_header(skb);
1376
1377 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1378 ++vxlan->dev->stats.rx_frame_errors;
1379 ++vxlan->dev->stats.rx_errors;
1380 goto drop;
1381 }
1382
1383 stats = this_cpu_ptr(vxlan->dev->tstats);
1384 u64_stats_update_begin(&stats->syncp);
1385 stats->rx_packets++;
1386 stats->rx_bytes += skb->len;
1387 u64_stats_update_end(&stats->syncp);
1388
1389 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001390 return 0;
1391
1392drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001393 /* Consume bad packet */
1394 kfree_skb(skb);
1395 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001396}
1397
David Stevense4f67ad2012-11-20 02:50:14 +00001398static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1399{
1400 struct vxlan_dev *vxlan = netdev_priv(dev);
1401 struct arphdr *parp;
1402 u8 *arpptr, *sha;
1403 __be32 sip, tip;
1404 struct neighbour *n;
1405
1406 if (dev->flags & IFF_NOARP)
1407 goto out;
1408
1409 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1410 dev->stats.tx_dropped++;
1411 goto out;
1412 }
1413 parp = arp_hdr(skb);
1414
1415 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1416 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1417 parp->ar_pro != htons(ETH_P_IP) ||
1418 parp->ar_op != htons(ARPOP_REQUEST) ||
1419 parp->ar_hln != dev->addr_len ||
1420 parp->ar_pln != 4)
1421 goto out;
1422 arpptr = (u8 *)parp + sizeof(struct arphdr);
1423 sha = arpptr;
1424 arpptr += dev->addr_len; /* sha */
1425 memcpy(&sip, arpptr, sizeof(sip));
1426 arpptr += sizeof(sip);
1427 arpptr += dev->addr_len; /* tha */
1428 memcpy(&tip, arpptr, sizeof(tip));
1429
1430 if (ipv4_is_loopback(tip) ||
1431 ipv4_is_multicast(tip))
1432 goto out;
1433
1434 n = neigh_lookup(&arp_tbl, &tip, dev);
1435
1436 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001437 struct vxlan_fdb *f;
1438 struct sk_buff *reply;
1439
1440 if (!(n->nud_state & NUD_CONNECTED)) {
1441 neigh_release(n);
1442 goto out;
1443 }
1444
1445 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001446 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001447 /* bridge-local neighbor */
1448 neigh_release(n);
1449 goto out;
1450 }
1451
1452 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1453 n->ha, sha);
1454
1455 neigh_release(n);
1456
David Stevens73461352014-03-18 12:32:29 -04001457 if (reply == NULL)
1458 goto out;
1459
David Stevense4f67ad2012-11-20 02:50:14 +00001460 skb_reset_mac_header(reply);
1461 __skb_pull(reply, skb_network_offset(reply));
1462 reply->ip_summed = CHECKSUM_UNNECESSARY;
1463 reply->pkt_type = PACKET_HOST;
1464
1465 if (netif_rx_ni(reply) == NET_RX_DROP)
1466 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001467 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1468 union vxlan_addr ipa = {
1469 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001470 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001471 };
1472
1473 vxlan_ip_miss(dev, &ipa);
1474 }
David Stevense4f67ad2012-11-20 02:50:14 +00001475out:
1476 consume_skb(skb);
1477 return NETDEV_TX_OK;
1478}
1479
Cong Wangf564f452013-08-31 13:44:36 +08001480#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001481static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1482 struct neighbour *n, bool isrouter)
1483{
1484 struct net_device *dev = request->dev;
1485 struct sk_buff *reply;
1486 struct nd_msg *ns, *na;
1487 struct ipv6hdr *pip6;
1488 u8 *daddr;
1489 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1490 int ns_olen;
1491 int i, len;
1492
1493 if (dev == NULL)
1494 return NULL;
1495
1496 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1497 sizeof(*na) + na_olen + dev->needed_tailroom;
1498 reply = alloc_skb(len, GFP_ATOMIC);
1499 if (reply == NULL)
1500 return NULL;
1501
1502 reply->protocol = htons(ETH_P_IPV6);
1503 reply->dev = dev;
1504 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1505 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001506 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001507
1508 ns = (struct nd_msg *)skb_transport_header(request);
1509
1510 daddr = eth_hdr(request)->h_source;
1511 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1512 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1513 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1514 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1515 break;
1516 }
1517 }
1518
1519 /* Ethernet header */
1520 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1521 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1522 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1523 reply->protocol = htons(ETH_P_IPV6);
1524
1525 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001526 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001527 skb_put(reply, sizeof(struct ipv6hdr));
1528
1529 /* IPv6 header */
1530
1531 pip6 = ipv6_hdr(reply);
1532 memset(pip6, 0, sizeof(struct ipv6hdr));
1533 pip6->version = 6;
1534 pip6->priority = ipv6_hdr(request)->priority;
1535 pip6->nexthdr = IPPROTO_ICMPV6;
1536 pip6->hop_limit = 255;
1537 pip6->daddr = ipv6_hdr(request)->saddr;
1538 pip6->saddr = *(struct in6_addr *)n->primary_key;
1539
1540 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001541 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001542
1543 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1544
1545 /* Neighbor Advertisement */
1546 memset(na, 0, sizeof(*na)+na_olen);
1547 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1548 na->icmph.icmp6_router = isrouter;
1549 na->icmph.icmp6_override = 1;
1550 na->icmph.icmp6_solicited = 1;
1551 na->target = ns->target;
1552 ether_addr_copy(&na->opt[2], n->ha);
1553 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1554 na->opt[1] = na_olen >> 3;
1555
1556 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1557 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1558 csum_partial(na, sizeof(*na)+na_olen, 0));
1559
1560 pip6->payload_len = htons(sizeof(*na)+na_olen);
1561
1562 skb_push(reply, sizeof(struct ipv6hdr));
1563
1564 reply->ip_summed = CHECKSUM_UNNECESSARY;
1565
1566 return reply;
1567}
1568
Cong Wangf564f452013-08-31 13:44:36 +08001569static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1570{
1571 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001572 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001573 const struct ipv6hdr *iphdr;
1574 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001575 struct neighbour *n;
1576 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001577
1578 in6_dev = __in6_dev_get(dev);
1579 if (!in6_dev)
1580 goto out;
1581
Cong Wangf564f452013-08-31 13:44:36 +08001582 iphdr = ipv6_hdr(skb);
1583 saddr = &iphdr->saddr;
1584 daddr = &iphdr->daddr;
1585
Cong Wangf564f452013-08-31 13:44:36 +08001586 msg = (struct nd_msg *)skb_transport_header(skb);
1587 if (msg->icmph.icmp6_code != 0 ||
1588 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1589 goto out;
1590
David Stevens4b29dba2014-03-24 10:39:58 -04001591 if (ipv6_addr_loopback(daddr) ||
1592 ipv6_addr_is_multicast(&msg->target))
1593 goto out;
1594
1595 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001596
1597 if (n) {
1598 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001599 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001600
1601 if (!(n->nud_state & NUD_CONNECTED)) {
1602 neigh_release(n);
1603 goto out;
1604 }
1605
1606 f = vxlan_find_mac(vxlan, n->ha);
1607 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1608 /* bridge-local neighbor */
1609 neigh_release(n);
1610 goto out;
1611 }
1612
David Stevens4b29dba2014-03-24 10:39:58 -04001613 reply = vxlan_na_create(skb, n,
1614 !!(f ? f->flags & NTF_ROUTER : 0));
1615
Cong Wangf564f452013-08-31 13:44:36 +08001616 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001617
1618 if (reply == NULL)
1619 goto out;
1620
1621 if (netif_rx_ni(reply) == NET_RX_DROP)
1622 dev->stats.rx_dropped++;
1623
Cong Wangf564f452013-08-31 13:44:36 +08001624 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001625 union vxlan_addr ipa = {
1626 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001627 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001628 };
1629
Cong Wangf564f452013-08-31 13:44:36 +08001630 vxlan_ip_miss(dev, &ipa);
1631 }
1632
1633out:
1634 consume_skb(skb);
1635 return NETDEV_TX_OK;
1636}
1637#endif
1638
David Stevense4f67ad2012-11-20 02:50:14 +00001639static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1640{
1641 struct vxlan_dev *vxlan = netdev_priv(dev);
1642 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001643
1644 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1645 return false;
1646
1647 n = NULL;
1648 switch (ntohs(eth_hdr(skb)->h_proto)) {
1649 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001650 {
1651 struct iphdr *pip;
1652
David Stevense4f67ad2012-11-20 02:50:14 +00001653 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1654 return false;
1655 pip = ip_hdr(skb);
1656 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001657 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1658 union vxlan_addr ipa = {
1659 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001660 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001661 };
1662
1663 vxlan_ip_miss(dev, &ipa);
1664 return false;
1665 }
1666
David Stevense4f67ad2012-11-20 02:50:14 +00001667 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001668 }
1669#if IS_ENABLED(CONFIG_IPV6)
1670 case ETH_P_IPV6:
1671 {
1672 struct ipv6hdr *pip6;
1673
1674 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1675 return false;
1676 pip6 = ipv6_hdr(skb);
1677 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1678 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1679 union vxlan_addr ipa = {
1680 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001681 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001682 };
1683
1684 vxlan_ip_miss(dev, &ipa);
1685 return false;
1686 }
1687
1688 break;
1689 }
1690#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001691 default:
1692 return false;
1693 }
1694
1695 if (n) {
1696 bool diff;
1697
Joe Perches7367d0b2013-09-01 11:51:23 -07001698 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001699 if (diff) {
1700 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1701 dev->addr_len);
1702 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1703 }
1704 neigh_release(n);
1705 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001706 }
1707
David Stevense4f67ad2012-11-20 02:50:14 +00001708 return false;
1709}
1710
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001711static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001712 struct vxlan_metadata *md)
1713{
1714 struct vxlanhdr_gbp *gbp;
1715
Thomas Grafdb79a622015-02-04 17:00:04 +01001716 if (!md->gbp)
1717 return;
1718
Thomas Graf35114942015-01-15 03:53:55 +01001719 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001720 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001721
1722 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1723 gbp->dont_learn = 1;
1724
1725 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1726 gbp->policy_applied = 1;
1727
1728 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1729}
1730
Jiri Bence1e53142016-04-05 14:47:13 +02001731static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1732 __be16 protocol)
1733{
1734 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1735
1736 gpe->np_applied = 1;
1737
1738 switch (protocol) {
1739 case htons(ETH_P_IP):
1740 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1741 return 0;
1742 case htons(ETH_P_IPV6):
1743 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1744 return 0;
1745 case htons(ETH_P_TEB):
1746 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1747 return 0;
1748 }
1749 return -EPFNOSUPPORT;
1750}
1751
Jiri Bencf491e562016-02-02 18:09:16 +01001752static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1753 int iphdr_len, __be32 vni,
1754 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001755 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001756{
Cong Wange4c7ed42013-08-31 13:44:33 +08001757 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001758 int min_headroom;
1759 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001760 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001761 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001762
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001763 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001764 skb->ip_summed == CHECKSUM_PARTIAL) {
1765 int csum_start = skb_checksum_start_offset(skb);
1766
1767 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1768 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1769 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001770 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001771 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001772 }
1773
Cong Wange4c7ed42013-08-31 13:44:33 +08001774 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
Jiri Bencf491e562016-02-02 18:09:16 +01001775 + VXLAN_HLEN + iphdr_len
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001776 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001777
1778 /* Need space for new headers (invalidates iph ptr) */
1779 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001780 if (unlikely(err))
1781 goto out_free;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001782
Jiri Pirko59682502014-11-19 14:04:59 +01001783 skb = vlan_hwaccel_push_inside(skb);
1784 if (WARN_ON(!skb))
1785 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001786
Alexander Duyckaed069d2016-04-14 15:33:37 -04001787 err = iptunnel_handle_offloads(skb, type);
1788 if (err)
1789 goto out_free;
Jesse Grossb736a622015-04-09 11:19:14 -07001790
Pravin B Shelar49560532013-08-19 11:23:17 -07001791 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001792 vxh->vx_flags = VXLAN_HF_VNI;
1793 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001794
Tom Herbertdfd86452015-01-12 17:00:38 -08001795 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001796 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001797
Jiri Benc54bfd872016-02-16 21:58:58 +01001798 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1799 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1800 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001801
1802 if (!skb_is_gso(skb)) {
1803 skb->ip_summed = CHECKSUM_NONE;
1804 skb->encapsulation = 0;
1805 }
1806 }
1807
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001808 if (vxflags & VXLAN_F_GBP)
1809 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001810 if (vxflags & VXLAN_F_GPE) {
1811 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1812 if (err < 0)
1813 goto out_free;
1814 inner_protocol = skb->protocol;
1815 }
Thomas Graf35114942015-01-15 03:53:55 +01001816
Jiri Bence1e53142016-04-05 14:47:13 +02001817 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001818 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02001819
1820out_free:
1821 kfree_skb(skb);
1822 return err;
Pravin B Shelar49560532013-08-19 11:23:17 -07001823}
Pravin B Shelar49560532013-08-19 11:23:17 -07001824
Jiri Benc1a8496b2016-02-02 18:09:14 +01001825static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan,
1826 struct sk_buff *skb, int oif, u8 tos,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001827 __be32 daddr, __be32 *saddr,
1828 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001829 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01001830{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001831 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01001832 struct rtable *rt = NULL;
1833 struct flowi4 fl4;
1834
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001835 if (tos && !info)
1836 use_cache = false;
1837 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001838 rt = dst_cache_get_ip4(dst_cache, saddr);
1839 if (rt)
1840 return rt;
1841 }
1842
Jiri Benc1a8496b2016-02-02 18:09:14 +01001843 memset(&fl4, 0, sizeof(fl4));
1844 fl4.flowi4_oif = oif;
1845 fl4.flowi4_tos = RT_TOS(tos);
1846 fl4.flowi4_mark = skb->mark;
1847 fl4.flowi4_proto = IPPROTO_UDP;
1848 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001849 fl4.saddr = *saddr;
Jiri Benc1a8496b2016-02-02 18:09:14 +01001850
1851 rt = ip_route_output_key(vxlan->net, &fl4);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001852 if (!IS_ERR(rt)) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01001853 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001854 if (use_cache)
1855 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
1856 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01001857 return rt;
1858}
1859
Jiri Bence5d4b292015-12-07 13:04:30 +01001860#if IS_ENABLED(CONFIG_IPV6)
1861static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
Daniel Borkmann14006152016-03-04 15:15:08 +01001862 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001863 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01001864 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001865 struct in6_addr *saddr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001866 struct dst_cache *dst_cache,
1867 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01001868{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001869 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001870 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01001871 struct dst_entry *ndst;
1872 struct flowi6 fl6;
1873 int err;
1874
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001875 if (!sock6)
1876 return ERR_PTR(-EIO);
1877
Daniel Borkmann14006152016-03-04 15:15:08 +01001878 if (tos && !info)
1879 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001880 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001881 ndst = dst_cache_get_ip6(dst_cache, saddr);
1882 if (ndst)
1883 return ndst;
1884 }
1885
Jiri Bence5d4b292015-12-07 13:04:30 +01001886 memset(&fl6, 0, sizeof(fl6));
1887 fl6.flowi6_oif = oif;
1888 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001889 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01001890 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01001891 fl6.flowi6_mark = skb->mark;
1892 fl6.flowi6_proto = IPPROTO_UDP;
1893
1894 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001895 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01001896 &ndst, &fl6);
1897 if (err < 0)
1898 return ERR_PTR(err);
1899
1900 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001901 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001902 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01001903 return ndst;
1904}
1905#endif
1906
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001907/* Bypass encapsulation if the destination is local */
1908static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1909 struct vxlan_dev *dst_vxlan)
1910{
Li RongQing8f849852014-01-04 13:57:59 +08001911 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001912 union vxlan_addr loopback;
1913 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001914 struct net_device *dev = skb->dev;
1915 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001916
Li RongQing8f849852014-01-04 13:57:59 +08001917 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1918 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001919 skb->pkt_type = PACKET_HOST;
1920 skb->encapsulation = 0;
1921 skb->dev = dst_vxlan->dev;
1922 __skb_pull(skb, skb_network_offset(skb));
1923
Cong Wange4c7ed42013-08-31 13:44:33 +08001924 if (remote_ip->sa.sa_family == AF_INET) {
1925 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1926 loopback.sa.sa_family = AF_INET;
1927#if IS_ENABLED(CONFIG_IPV6)
1928 } else {
1929 loopback.sin6.sin6_addr = in6addr_loopback;
1930 loopback.sa.sa_family = AF_INET6;
1931#endif
1932 }
1933
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001934 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001935 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001936
1937 u64_stats_update_begin(&tx_stats->syncp);
1938 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001939 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001940 u64_stats_update_end(&tx_stats->syncp);
1941
1942 if (netif_rx(skb) == NET_RX_SUCCESS) {
1943 u64_stats_update_begin(&rx_stats->syncp);
1944 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001945 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001946 u64_stats_update_end(&rx_stats->syncp);
1947 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001948 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001949 }
1950}
1951
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001952static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1953 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001954{
Paolo Abenid71785f2016-02-12 15:43:57 +01001955 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02001956 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00001957 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001958 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001959 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001960 const struct iphdr *old_iph;
Cong Wange4c7ed42013-08-31 13:44:33 +08001961 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07001962 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02001963 struct vxlan_metadata _md;
1964 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001965 __be16 src_port = 0, dst_port;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001966 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00001967 __be16 df = 0;
1968 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001969 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02001970 u32 flags = vxlan->flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001971 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01001972 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00001973
Jiri Benc61adedf2015-08-20 13:56:25 +02001974 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02001975
Jakub Kicinski0a40da42017-02-24 11:43:36 -08001976 rcu_read_lock();
Thomas Grafee122c72015-07-21 10:43:58 +02001977 if (rdst) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001978 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Thomas Grafee122c72015-07-21 10:43:58 +02001979 vni = rdst->remote_vni;
1980 dst = &rdst->remote_ip;
Brian Russella64407f2017-02-24 17:47:11 +00001981 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01001982 dst_cache = &rdst->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02001983 } else {
1984 if (!info) {
1985 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1986 dev->name);
1987 goto drop;
1988 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001989 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
Amir Vadaid817f432016-09-08 16:23:45 +03001990 vni = tunnel_id_to_key32(info->key.tun_id);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001991 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07001992 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02001993 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07001994 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
1995 } else {
Jiri Benca725e512015-08-20 13:56:30 +02001996 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07001997 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
1998 }
Thomas Grafee122c72015-07-21 10:43:58 +02001999 dst = &remote_ip;
Paolo Abenid71785f2016-02-12 15:43:57 +01002000 dst_cache = &info->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02002001 }
David Stevense4f67ad2012-11-20 02:50:14 +00002002
Cong Wange4c7ed42013-08-31 13:44:33 +08002003 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00002004 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00002005 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002006 vxlan_encap_bypass(skb, vxlan, vxlan);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002007 goto out_unlock;
David Stevense4f67ad2012-11-20 02:50:14 +00002008 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002009 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00002010 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002011
stephen hemmingerd3428942012-10-01 12:32:35 +00002012 old_iph = ip_hdr(skb);
2013
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002014 ttl = vxlan->cfg.ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08002015 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00002016 ttl = 1;
2017
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002018 tos = vxlan->cfg.tos;
stephen hemmingerd3428942012-10-01 12:32:35 +00002019 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00002020 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002021
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002022 label = vxlan->cfg.label;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002023 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2024 vxlan->cfg.port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002025
Jiri Benca725e512015-08-20 13:56:30 +02002026 if (info) {
Jiri Benca725e512015-08-20 13:56:30 +02002027 ttl = info->key.ttl;
2028 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002029 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002030 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002031
2032 if (info->options_len)
Pravin B Shelar4c222792015-08-30 18:09:38 -07002033 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002034 } else {
2035 md->gbp = skb->mark;
2036 }
2037
Cong Wange4c7ed42013-08-31 13:44:33 +08002038 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002039 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2040
2041 if (!sock4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002042 goto drop;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002043 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002044
Jiri Benc1a8496b2016-02-02 18:09:14 +01002045 rt = vxlan_get_route(vxlan, skb,
2046 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002047 dst->sin.sin_addr.s_addr,
Brian Russella64407f2017-02-24 17:47:11 +00002048 &local_ip.sin.sin_addr.s_addr,
Paolo Abenid71785f2016-02-12 15:43:57 +01002049 dst_cache, info);
Cong Wange4c7ed42013-08-31 13:44:33 +08002050 if (IS_ERR(rt)) {
2051 netdev_dbg(dev, "no route to %pI4\n",
2052 &dst->sin.sin_addr.s_addr);
2053 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002054 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002055 }
2056
2057 if (rt->dst.dev == dev) {
2058 netdev_dbg(dev, "circular route to %pI4\n",
2059 &dst->sin.sin_addr.s_addr);
2060 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08002061 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002062 }
2063
2064 /* Bypass encapsulation if the destination is local */
pravin shelarbbec7802016-08-05 17:45:37 -07002065 if (!info && rt->rt_flags & RTCF_LOCAL &&
Cong Wange4c7ed42013-08-31 13:44:33 +08002066 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2067 struct vxlan_dev *dst_vxlan;
2068
2069 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002070 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002071 dst->sa.sa_family, dst_port,
2072 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002073 if (!dst_vxlan)
2074 goto tx_error;
2075 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002076 goto out_unlock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002077 }
2078
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002079 if (!info)
2080 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2081 else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
2082 df = htons(IP_DF);
2083
Cong Wange4c7ed42013-08-31 13:44:33 +08002084 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2085 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Jiri Bencf491e562016-02-02 18:09:16 +01002086 err = vxlan_build_skb(skb, &rt->dst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002087 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002088 if (err < 0)
2089 goto xmit_tx_error;
2090
Brian Russella64407f2017-02-24 17:47:11 +00002091 udp_tunnel_xmit_skb(rt, sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002092 dst->sin.sin_addr.s_addr, tos, ttl, df,
2093 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002094#if IS_ENABLED(CONFIG_IPV6)
2095 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002096 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002097 struct dst_entry *ndst;
Jiri Benc6f264e42015-08-20 13:56:29 +02002098 u32 rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002099
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002100 if (!sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002101 goto drop;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002102 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002103
Jiri Bence5d4b292015-12-07 13:04:30 +01002104 ndst = vxlan6_get_route(vxlan, skb,
Daniel Borkmann14006152016-03-04 15:15:08 +01002105 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002106 label, &dst->sin6.sin6_addr,
Brian Russella64407f2017-02-24 17:47:11 +00002107 &local_ip.sin6.sin6_addr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002108 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002109 if (IS_ERR(ndst)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002110 netdev_dbg(dev, "no route to %pI6\n",
2111 &dst->sin6.sin6_addr);
2112 dev->stats.tx_carrier_errors++;
2113 goto tx_error;
2114 }
2115
2116 if (ndst->dev == dev) {
2117 netdev_dbg(dev, "circular route to %pI6\n",
2118 &dst->sin6.sin6_addr);
2119 dst_release(ndst);
2120 dev->stats.collisions++;
2121 goto tx_error;
2122 }
2123
2124 /* Bypass encapsulation if the destination is local */
Jiri Benc6f264e42015-08-20 13:56:29 +02002125 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
pravin shelarbbec7802016-08-05 17:45:37 -07002126 if (!info && rt6i_flags & RTF_LOCAL &&
Jiri Benc6f264e42015-08-20 13:56:29 +02002127 !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002128 struct vxlan_dev *dst_vxlan;
2129
2130 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002131 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002132 dst->sa.sa_family, dst_port,
2133 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002134 if (!dst_vxlan)
2135 goto tx_error;
2136 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002137 goto out_unlock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002138 }
2139
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002140 if (!info)
2141 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Jesse Gross35e2d112016-01-20 16:22:47 -08002142
Daniel Borkmann14006152016-03-04 15:15:08 +01002143 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002144 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002145 skb_scrub_packet(skb, xnet);
2146 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002147 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002148 if (err < 0) {
2149 dst_release(ndst);
Haishuang Yan5e1e61a2016-09-04 18:52:51 +08002150 dev->stats.tx_errors++;
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002151 goto out_unlock;
Jiri Bencf491e562016-02-02 18:09:16 +01002152 }
2153 udp_tunnel6_xmit_skb(ndst, sk, skb, dev,
Brian Russella64407f2017-02-24 17:47:11 +00002154 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002155 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002156 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002157#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002158 }
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002159out_unlock:
2160 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002161 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002162
2163drop:
2164 dev->stats.tx_dropped++;
2165 goto tx_free;
2166
Jiri Bencf491e562016-02-02 18:09:16 +01002167xmit_tx_error:
2168 /* skb is already freed. */
2169 skb = NULL;
Pravin B Shelar49560532013-08-19 11:23:17 -07002170rt_tx_error:
2171 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002172tx_error:
2173 dev->stats.tx_errors++;
2174tx_free:
2175 dev_kfree_skb(skb);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002176 rcu_read_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00002177}
2178
David Stevens66817122013-03-15 04:35:51 +00002179/* Transmit local packets over Vxlan
2180 *
2181 * Outer IP header inherits ECN and DF from inner header.
2182 * Outer UDP destination is the VXLAN assigned port.
2183 * source port is based on hash of flow
2184 */
2185static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2186{
2187 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002188 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002189 struct ethhdr *eth;
2190 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002191 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002192 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002193
Jiri Benc61adedf2015-08-20 13:56:25 +02002194 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002195
David Stevens66817122013-03-15 04:35:51 +00002196 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002197
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002198 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
2199 if (info && info->mode & IP_TUNNEL_INFO_TX)
2200 vxlan_xmit_one(skb, dev, NULL, false);
2201 else
2202 kfree_skb(skb);
2203 return NETDEV_TX_OK;
2204 }
2205
2206 if (vxlan->flags & VXLAN_F_PROXY) {
2207 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002208 if (ntohs(eth->h_proto) == ETH_P_ARP)
2209 return arp_reduce(dev, skb);
2210#if IS_ENABLED(CONFIG_IPV6)
2211 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002212 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2213 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002214 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2215 struct nd_msg *msg;
2216
2217 msg = (struct nd_msg *)skb_transport_header(skb);
2218 if (msg->icmph.icmp6_code == 0 &&
2219 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2220 return neigh_reduce(dev, skb);
2221 }
2222#endif
2223 }
David Stevens66817122013-03-15 04:35:51 +00002224
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002225 eth = eth_hdr(skb);
David Stevens66817122013-03-15 04:35:51 +00002226 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002227 did_rsc = false;
2228
2229 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002230 (ntohs(eth->h_proto) == ETH_P_IP ||
2231 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002232 did_rsc = route_shortcircuit(dev, skb);
2233 if (did_rsc)
2234 f = vxlan_find_mac(vxlan, eth->h_dest);
2235 }
2236
David Stevens66817122013-03-15 04:35:51 +00002237 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002238 f = vxlan_find_mac(vxlan, all_zeros_mac);
2239 if (f == NULL) {
2240 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2241 !is_multicast_ether_addr(eth->h_dest))
2242 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002243
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002244 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002245 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002246 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002247 }
David Stevens66817122013-03-15 04:35:51 +00002248 }
2249
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002250 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2251 struct sk_buff *skb1;
2252
Eric Dumazet8f646c92014-01-06 09:54:31 -08002253 if (!fdst) {
2254 fdst = rdst;
2255 continue;
2256 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002257 skb1 = skb_clone(skb, GFP_ATOMIC);
2258 if (skb1)
2259 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2260 }
2261
Eric Dumazet8f646c92014-01-06 09:54:31 -08002262 if (fdst)
2263 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2264 else
2265 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002266 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002267}
2268
stephen hemmingerd3428942012-10-01 12:32:35 +00002269/* Walk the forwarding table and purge stale entries */
2270static void vxlan_cleanup(unsigned long arg)
2271{
2272 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2273 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2274 unsigned int h;
2275
2276 if (!netif_running(vxlan->dev))
2277 return;
2278
stephen hemmingerd3428942012-10-01 12:32:35 +00002279 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2280 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002281
2282 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002283 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2284 struct vxlan_fdb *f
2285 = container_of(p, struct vxlan_fdb, hlist);
2286 unsigned long timeout;
2287
stephen hemminger3c172862012-10-26 06:24:34 +00002288 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002289 continue;
2290
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002291 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002292 if (time_before_eq(timeout, jiffies)) {
2293 netdev_dbg(vxlan->dev,
2294 "garbage collect %pM\n",
2295 f->eth_addr);
2296 f->state = NUD_STALE;
2297 vxlan_fdb_destroy(vxlan, f);
2298 } else if (time_before(timeout, next_timer))
2299 next_timer = timeout;
2300 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002301 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002302 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002303
2304 mod_timer(&vxlan->age_timer, next_timer);
2305}
2306
Mark Blochc242e1a2017-06-02 03:24:08 +03002307static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2308{
2309 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2310
2311 spin_lock(&vn->sock_lock);
2312 hlist_del_init_rcu(&vxlan->hlist);
2313 spin_unlock(&vn->sock_lock);
2314}
2315
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002316static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2317{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002318 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002319 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002320
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002321 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002322 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002323 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002324}
2325
stephen hemmingerd3428942012-10-01 12:32:35 +00002326/* Setup stats when device is created */
2327static int vxlan_init(struct net_device *dev)
2328{
WANG Cong1c213bd2014-02-13 11:46:28 -08002329 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002330 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002331 return -ENOMEM;
2332
2333 return 0;
2334}
2335
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002336static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002337{
2338 struct vxlan_fdb *f;
2339
2340 spin_lock_bh(&vxlan->hash_lock);
2341 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2342 if (f)
2343 vxlan_fdb_destroy(vxlan, f);
2344 spin_unlock_bh(&vxlan->hash_lock);
2345}
2346
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002347static void vxlan_uninit(struct net_device *dev)
2348{
2349 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002350
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002351 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002352
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002353 free_percpu(dev->tstats);
2354}
2355
stephen hemmingerd3428942012-10-01 12:32:35 +00002356/* Start ageing timer and join group when device is brought up */
2357static int vxlan_open(struct net_device *dev)
2358{
2359 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002360 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002361
Jiri Benc205f3562015-09-24 13:50:01 +02002362 ret = vxlan_sock_add(vxlan);
2363 if (ret < 0)
2364 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002365
Gao feng79d4a942013-12-10 16:37:32 +08002366 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002367 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002368 if (ret == -EADDRINUSE)
2369 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002370 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002371 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002372 return ret;
2373 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002374 }
2375
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002376 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002377 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2378
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002379 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002380}
2381
2382/* Purge the forwarding table */
2383static void vxlan_flush(struct vxlan_dev *vxlan)
2384{
Cong Wang31fec5a2013-05-27 22:35:52 +00002385 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002386
2387 spin_lock_bh(&vxlan->hash_lock);
2388 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2389 struct hlist_node *p, *n;
2390 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2391 struct vxlan_fdb *f
2392 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002393 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2394 if (!is_zero_ether_addr(f->eth_addr))
2395 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002396 }
2397 }
2398 spin_unlock_bh(&vxlan->hash_lock);
2399}
2400
2401/* Cleanup timer and forwarding table on shutdown */
2402static int vxlan_stop(struct net_device *dev)
2403{
2404 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002405 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002406 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002407
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002408 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002409 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002410 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002411
2412 del_timer_sync(&vxlan->age_timer);
2413
2414 vxlan_flush(vxlan);
Jiri Benc205f3562015-09-24 13:50:01 +02002415 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002416
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002417 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002418}
2419
stephen hemmingerd3428942012-10-01 12:32:35 +00002420/* Stub, nothing needs to be done. */
2421static void vxlan_set_multicast_list(struct net_device *dev)
2422{
2423}
2424
David Wragg72564b52016-02-10 00:05:55 +00002425static int __vxlan_change_mtu(struct net_device *dev,
2426 struct net_device *lowerdev,
2427 struct vxlan_rdst *dst, int new_mtu, bool strict)
2428{
2429 int max_mtu = IP_MAX_MTU;
2430
2431 if (lowerdev)
2432 max_mtu = lowerdev->mtu;
2433
2434 if (dst->remote_ip.sa.sa_family == AF_INET6)
2435 max_mtu -= VXLAN6_HEADROOM;
2436 else
2437 max_mtu -= VXLAN_HEADROOM;
2438
2439 if (new_mtu < 68)
2440 return -EINVAL;
2441
2442 if (new_mtu > max_mtu) {
2443 if (strict)
2444 return -EINVAL;
2445
2446 new_mtu = max_mtu;
2447 }
2448
2449 dev->mtu = new_mtu;
2450 return 0;
2451}
2452
Daniel Borkmann345010b2013-12-18 00:21:08 +01002453static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2454{
2455 struct vxlan_dev *vxlan = netdev_priv(dev);
2456 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002457 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2458 dst->remote_ifindex);
2459 return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002460}
2461
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002462static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2463{
2464 struct vxlan_dev *vxlan = netdev_priv(dev);
2465 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2466 __be16 sport, dport;
2467
2468 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2469 vxlan->cfg.port_max, true);
2470 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2471
Jiri Benc239e9442015-12-07 13:04:31 +01002472 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002473 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002474 struct rtable *rt;
2475
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002476 if (!sock4)
Jiri Benc239e9442015-12-07 13:04:31 +01002477 return -EINVAL;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002478 rt = vxlan_get_route(vxlan, skb, 0, info->key.tos,
2479 info->key.u.ipv4.dst,
Paolo Abenif23fd872017-02-17 19:14:27 +01002480 &info->key.u.ipv4.src,
2481 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002482 if (IS_ERR(rt))
2483 return PTR_ERR(rt);
2484 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002485 } else {
2486#if IS_ENABLED(CONFIG_IPV6)
2487 struct dst_entry *ndst;
2488
Daniel Borkmann14006152016-03-04 15:15:08 +01002489 ndst = vxlan6_get_route(vxlan, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002490 info->key.label, &info->key.u.ipv6.dst,
Paolo Abenif23fd872017-02-17 19:14:27 +01002491 &info->key.u.ipv6.src,
2492 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002493 if (IS_ERR(ndst))
2494 return PTR_ERR(ndst);
2495 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002496#else /* !CONFIG_IPV6 */
2497 return -EPFNOSUPPORT;
2498#endif
2499 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002500 info->key.tp_src = sport;
2501 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002502 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002503}
2504
Jiri Benc0c867c92016-04-05 14:47:10 +02002505static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002506 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002507 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002508 .ndo_open = vxlan_open,
2509 .ndo_stop = vxlan_stop,
2510 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002511 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002512 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002513 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002514 .ndo_validate_addr = eth_validate_addr,
2515 .ndo_set_mac_address = eth_mac_addr,
2516 .ndo_fdb_add = vxlan_fdb_add,
2517 .ndo_fdb_del = vxlan_fdb_delete,
2518 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002519 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002520};
2521
Jiri Bence1e53142016-04-05 14:47:13 +02002522static const struct net_device_ops vxlan_netdev_raw_ops = {
2523 .ndo_init = vxlan_init,
2524 .ndo_uninit = vxlan_uninit,
2525 .ndo_open = vxlan_open,
2526 .ndo_stop = vxlan_stop,
2527 .ndo_start_xmit = vxlan_xmit,
2528 .ndo_get_stats64 = ip_tunnel_get_stats64,
2529 .ndo_change_mtu = vxlan_change_mtu,
2530 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2531};
2532
stephen hemmingerd3428942012-10-01 12:32:35 +00002533/* Info for udev, that this is a virtual tunnel endpoint */
2534static struct device_type vxlan_type = {
2535 .name = "vxlan",
2536};
2537
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002538/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002539 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002540 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002541 */
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02002542static void vxlan_push_rx_ports(struct net_device *dev)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002543{
2544 struct vxlan_sock *vs;
2545 struct net *net = dev_net(dev);
2546 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002547 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002548
2549 spin_lock(&vn->sock_lock);
2550 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Alexander Duycke7b3db52016-06-16 12:20:52 -07002551 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2552 udp_tunnel_push_rx_port(dev, vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002553 (vs->flags & VXLAN_F_GPE) ?
2554 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002555 UDP_TUNNEL_TYPE_VXLAN);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002556 }
2557 spin_unlock(&vn->sock_lock);
2558}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002559
stephen hemmingerd3428942012-10-01 12:32:35 +00002560/* Initialize the device structure. */
2561static void vxlan_setup(struct net_device *dev)
2562{
2563 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002564 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002565
Jiri Benc65226ef2016-04-28 16:36:30 +02002566 eth_hw_addr_random(dev);
2567 ether_setup(dev);
2568
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002569 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002570 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2571
stephen hemmingerd3428942012-10-01 12:32:35 +00002572 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002573 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002574 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002575 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002576
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002577 dev->vlan_features = dev->features;
2578 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002579 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002580 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002581 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002582 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002583 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002584
stephen hemminger553675f2013-05-16 11:35:20 +00002585 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002586 spin_lock_init(&vxlan->hash_lock);
2587
2588 init_timer_deferrable(&vxlan->age_timer);
2589 vxlan->age_timer.function = vxlan_cleanup;
2590 vxlan->age_timer.data = (unsigned long) vxlan;
2591
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002592 vxlan->cfg.dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002593
stephen hemmingerd3428942012-10-01 12:32:35 +00002594 vxlan->dev = dev;
2595
Tom Herbert58ce31c2015-08-19 17:07:33 -07002596 gro_cells_init(&vxlan->gro_cells, dev);
2597
stephen hemmingerd3428942012-10-01 12:32:35 +00002598 for (h = 0; h < FDB_HASH_SIZE; ++h)
2599 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2600}
2601
Jiri Benc0c867c92016-04-05 14:47:10 +02002602static void vxlan_ether_setup(struct net_device *dev)
2603{
Jiri Benc0c867c92016-04-05 14:47:10 +02002604 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2605 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2606 dev->netdev_ops = &vxlan_netdev_ether_ops;
2607}
2608
Jiri Bence1e53142016-04-05 14:47:13 +02002609static void vxlan_raw_setup(struct net_device *dev)
2610{
Jiri Benc65226ef2016-04-28 16:36:30 +02002611 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002612 dev->type = ARPHRD_NONE;
2613 dev->hard_header_len = 0;
2614 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002615 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2616 dev->netdev_ops = &vxlan_netdev_raw_ops;
2617}
2618
stephen hemmingerd3428942012-10-01 12:32:35 +00002619static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2620 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002621 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002622 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002623 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2624 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002625 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002626 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2627 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002628 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002629 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2630 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2631 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002632 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002633 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2634 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2635 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2636 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002637 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002638 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002639 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2640 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2641 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002642 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2643 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002644 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002645 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002646 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002647};
2648
2649static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2650{
2651 if (tb[IFLA_ADDRESS]) {
2652 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2653 pr_debug("invalid link address (not ethernet)\n");
2654 return -EINVAL;
2655 }
2656
2657 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2658 pr_debug("invalid all zero ethernet address\n");
2659 return -EADDRNOTAVAIL;
2660 }
2661 }
2662
2663 if (!data)
2664 return -EINVAL;
2665
2666 if (data[IFLA_VXLAN_ID]) {
2667 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
Matthias Schifferee2da792017-02-23 17:19:41 +01002668 if (id >= VXLAN_N_VID)
stephen hemmingerd3428942012-10-01 12:32:35 +00002669 return -ERANGE;
2670 }
2671
stephen hemminger05f47d62012-10-09 20:35:50 +00002672 if (data[IFLA_VXLAN_PORT_RANGE]) {
2673 const struct ifla_vxlan_port_range *p
2674 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2675
2676 if (ntohs(p->high) < ntohs(p->low)) {
2677 pr_debug("port range %u .. %u not valid\n",
2678 ntohs(p->low), ntohs(p->high));
2679 return -EINVAL;
2680 }
2681 }
2682
stephen hemmingerd3428942012-10-01 12:32:35 +00002683 return 0;
2684}
2685
Yan Burman1b13c972013-01-29 23:43:07 +00002686static void vxlan_get_drvinfo(struct net_device *netdev,
2687 struct ethtool_drvinfo *drvinfo)
2688{
2689 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2690 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2691}
2692
2693static const struct ethtool_ops vxlan_ethtool_ops = {
2694 .get_drvinfo = vxlan_get_drvinfo,
2695 .get_link = ethtool_op_get_link,
2696};
2697
Tom Herbert3ee64f32014-07-13 19:49:42 -07002698static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2699 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002700{
Cong Wange4c7ed42013-08-31 13:44:33 +08002701 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002702 struct udp_port_cfg udp_conf;
2703 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002704
Tom Herbert3ee64f32014-07-13 19:49:42 -07002705 memset(&udp_conf, 0, sizeof(udp_conf));
2706
2707 if (ipv6) {
2708 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002709 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002710 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002711 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002712 } else {
2713 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002714 }
2715
Tom Herbert3ee64f32014-07-13 19:49:42 -07002716 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002717
Tom Herbert3ee64f32014-07-13 19:49:42 -07002718 /* Open UDP socket */
2719 err = udp_sock_create(net, &udp_conf, &sock);
2720 if (err < 0)
2721 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002722
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002723 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002724}
2725
2726/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002727static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2728 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002729{
2730 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2731 struct vxlan_sock *vs;
2732 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002733 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002734 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002735
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002736 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002737 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002738 return ERR_PTR(-ENOMEM);
2739
2740 for (h = 0; h < VNI_HASH_SIZE; ++h)
2741 INIT_HLIST_HEAD(&vs->vni_list[h]);
2742
Tom Herbert3ee64f32014-07-13 19:49:42 -07002743 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002744 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002745 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2746 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002747 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002748 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002749 }
2750
Cong Wange4c7ed42013-08-31 13:44:33 +08002751 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002752 atomic_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002753 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002754
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002755 spin_lock(&vn->sock_lock);
2756 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07002757 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002758 (vs->flags & VXLAN_F_GPE) ?
2759 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002760 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002761 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002762
2763 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002764 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002765 tunnel_cfg.sk_user_data = vs;
2766 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002767 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002768 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002769 tunnel_cfg.gro_receive = vxlan_gro_receive;
2770 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002771
2772 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002773
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002774 return vs;
2775}
stephen hemminger553675f2013-05-16 11:35:20 +00002776
Jiri Bencb1be00a2015-09-24 13:50:02 +02002777static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002778{
Jiri Benc205f3562015-09-24 13:50:01 +02002779 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2780 struct vxlan_sock *vs = NULL;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002781
Jiri Benc205f3562015-09-24 13:50:01 +02002782 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002783 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002784 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2785 vxlan->cfg.dst_port, vxlan->flags);
2786 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002787 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002788 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002789 }
2790 spin_unlock(&vn->sock_lock);
2791 }
Jiri Benc205f3562015-09-24 13:50:01 +02002792 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002793 vs = vxlan_socket_create(vxlan->net, ipv6,
2794 vxlan->cfg.dst_port, vxlan->flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002795 if (IS_ERR(vs))
2796 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002797#if IS_ENABLED(CONFIG_IPV6)
2798 if (ipv6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002799 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002800 else
2801#endif
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002802 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc205f3562015-09-24 13:50:01 +02002803 vxlan_vs_add_dev(vs, vxlan);
2804 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002805}
2806
Jiri Bencb1be00a2015-09-24 13:50:02 +02002807static int vxlan_sock_add(struct vxlan_dev *vxlan)
2808{
2809 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2810 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
2811 int ret = 0;
2812
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002813 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002814#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002815 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002816 if (ipv6 || metadata)
2817 ret = __vxlan_sock_add(vxlan, true);
2818#endif
2819 if (!ret && (!ipv6 || metadata))
2820 ret = __vxlan_sock_add(vxlan, false);
2821 if (ret < 0)
2822 vxlan_sock_release(vxlan);
2823 return ret;
2824}
2825
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002826static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2827 struct vxlan_config *conf)
stephen hemmingerd3428942012-10-01 12:32:35 +00002828{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002829 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002830 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
Atzm Watanabec7995c42013-04-16 02:50:52 +00002831 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002832 unsigned short needed_headroom = ETH_HLEN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002833 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002834 bool use_ipv6 = false;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002835 __be16 default_port = vxlan->cfg.dst_port;
David Wragg7e059152016-02-10 00:05:58 +00002836 struct net_device *lowerdev = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00002837
Jiri Bence1e53142016-04-05 14:47:13 +02002838 if (conf->flags & VXLAN_F_GPE) {
Jiri Bence1e53142016-04-05 14:47:13 +02002839 /* For now, allow GPE only together with COLLECT_METADATA.
2840 * This can be relaxed later; in such case, the other side
2841 * of the PtP link will have to be provided.
2842 */
Jiri Benc35556212016-09-02 13:37:12 +02002843 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2844 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2845 pr_info("unsupported combination of extensions\n");
Jiri Bence1e53142016-04-05 14:47:13 +02002846 return -EINVAL;
Jiri Benc35556212016-09-02 13:37:12 +02002847 }
Jiri Bence1e53142016-04-05 14:47:13 +02002848
2849 vxlan_raw_setup(dev);
2850 } else {
2851 vxlan_ether_setup(dev);
2852 }
Jiri Benc0c867c92016-04-05 14:47:10 +02002853
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002854 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002855
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002856 dst->remote_vni = conf->vni;
2857
2858 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00002859
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002860 /* Unless IPv6 is explicitly requested, assume IPv4 */
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002861 if (!dst->remote_ip.sa.sa_family)
2862 dst->remote_ip.sa.sa_family = AF_INET;
stephen hemmingerd3428942012-10-01 12:32:35 +00002863
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002864 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
Jiri Benc057ba292015-09-17 16:11:11 +02002865 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2866 if (!IS_ENABLED(CONFIG_IPV6))
2867 return -EPFNOSUPPORT;
Cong Wange4c7ed42013-08-31 13:44:33 +08002868 use_ipv6 = true;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002869 vxlan->flags |= VXLAN_F_IPV6;
Jiri Benc057ba292015-09-17 16:11:11 +02002870 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002871
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002872 if (conf->label && !use_ipv6) {
2873 pr_info("label only supported in use with IPv6\n");
2874 return -EINVAL;
2875 }
2876
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002877 if (conf->remote_ifindex) {
David Wragg7e059152016-02-10 00:05:58 +00002878 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002879 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00002880
stephen hemminger34e02aa2012-10-09 20:35:53 +00002881 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002882 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002883 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002884 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002885
Cong Wange4c7ed42013-08-31 13:44:33 +08002886#if IS_ENABLED(CONFIG_IPV6)
2887 if (use_ipv6) {
2888 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2889 if (idev && idev->cnf.disable_ipv6) {
2890 pr_info("IPv6 is disabled via sysctl\n");
2891 return -EPERM;
2892 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002893 }
2894#endif
2895
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002896 if (!conf->mtu)
Cong Wange4c7ed42013-08-31 13:44:33 +08002897 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002898
Jiri Bencb1be00a2015-09-24 13:50:02 +02002899 needed_headroom = lowerdev->hard_header_len;
Jiri Benc9b4cdd52016-09-02 13:37:11 +02002900 } else if (vxlan_addr_multicast(&dst->remote_ip)) {
2901 pr_info("multicast destination requires interface to be specified\n");
2902 return -EINVAL;
Jiri Benc9dc2ad12015-09-17 16:11:10 +02002903 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002904
David Wragg7e059152016-02-10 00:05:58 +00002905 if (conf->mtu) {
2906 err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false);
2907 if (err)
2908 return err;
2909 }
2910
Jiri Bencb1be00a2015-09-24 13:50:02 +02002911 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2912 needed_headroom += VXLAN6_HEADROOM;
2913 else
2914 needed_headroom += VXLAN_HEADROOM;
2915 dev->needed_headroom = needed_headroom;
2916
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002917 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Jiri Bence1e53142016-04-05 14:47:13 +02002918 if (!vxlan->cfg.dst_port) {
2919 if (conf->flags & VXLAN_F_GPE)
Lance Richardsond1c95f9c2017-01-16 18:37:58 -05002920 vxlan->cfg.dst_port = htons(4790); /* IANA VXLAN-GPE port */
Jiri Bence1e53142016-04-05 14:47:13 +02002921 else
2922 vxlan->cfg.dst_port = default_port;
2923 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002924 vxlan->flags |= conf->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00002925
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002926 if (!vxlan->cfg.age_interval)
2927 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
Vincent Bernatafb97182012-10-30 10:27:16 +00002928
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002929 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2930 if (tmp->cfg.vni == conf->vni &&
2931 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2932 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2933 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2934 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
Jiri Benc35556212016-09-02 13:37:12 +02002935 (vxlan->flags & VXLAN_F_RCV_FLAGS)) {
2936 pr_info("duplicate VNI %u\n", be32_to_cpu(conf->vni));
2937 return -EEXIST;
2938 }
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002939 }
stephen hemminger553675f2013-05-16 11:35:20 +00002940
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002941 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002942
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002943 /* create an fdb entry for a valid default destination */
2944 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2945 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2946 &vxlan->default_dst.remote_ip,
2947 NUD_REACHABLE|NUD_PERMANENT,
2948 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002949 vxlan->cfg.dst_port,
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002950 vxlan->default_dst.remote_vni,
2951 vxlan->default_dst.remote_ifindex,
2952 NTF_SELF);
2953 if (err)
2954 return err;
2955 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002956
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002957 err = register_netdevice(dev);
2958 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002959 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002960 return err;
2961 }
2962
stephen hemminger553675f2013-05-16 11:35:20 +00002963 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002964
2965 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002966}
2967
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002968static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2969 struct nlattr *tb[], struct nlattr *data[])
2970{
2971 struct vxlan_config conf;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002972
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002973 memset(&conf, 0, sizeof(conf));
Jesse Grosse277de52015-10-16 16:36:00 -07002974
2975 if (data[IFLA_VXLAN_ID])
Jiri Benc54bfd872016-02-16 21:58:58 +01002976 conf.vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002977
2978 if (data[IFLA_VXLAN_GROUP]) {
2979 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
2980 } else if (data[IFLA_VXLAN_GROUP6]) {
2981 if (!IS_ENABLED(CONFIG_IPV6))
2982 return -EPFNOSUPPORT;
2983
2984 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
2985 conf.remote_ip.sa.sa_family = AF_INET6;
2986 }
2987
2988 if (data[IFLA_VXLAN_LOCAL]) {
2989 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
2990 conf.saddr.sa.sa_family = AF_INET;
2991 } else if (data[IFLA_VXLAN_LOCAL6]) {
2992 if (!IS_ENABLED(CONFIG_IPV6))
2993 return -EPFNOSUPPORT;
2994
2995 /* TODO: respect scope id */
2996 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
2997 conf.saddr.sa.sa_family = AF_INET6;
2998 }
2999
3000 if (data[IFLA_VXLAN_LINK])
3001 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3002
3003 if (data[IFLA_VXLAN_TOS])
3004 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3005
3006 if (data[IFLA_VXLAN_TTL])
3007 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3008
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003009 if (data[IFLA_VXLAN_LABEL])
3010 conf.label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3011 IPV6_FLOWLABEL_MASK;
3012
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003013 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
3014 conf.flags |= VXLAN_F_LEARN;
3015
3016 if (data[IFLA_VXLAN_AGEING])
3017 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3018
3019 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
3020 conf.flags |= VXLAN_F_PROXY;
3021
3022 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
3023 conf.flags |= VXLAN_F_RSC;
3024
3025 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3026 conf.flags |= VXLAN_F_L2MISS;
3027
3028 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3029 conf.flags |= VXLAN_F_L3MISS;
3030
3031 if (data[IFLA_VXLAN_LIMIT])
3032 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3033
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003034 if (data[IFLA_VXLAN_COLLECT_METADATA] &&
3035 nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3036 conf.flags |= VXLAN_F_COLLECT_METADATA;
3037
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003038 if (data[IFLA_VXLAN_PORT_RANGE]) {
3039 const struct ifla_vxlan_port_range *p
3040 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3041 conf.port_min = ntohs(p->low);
3042 conf.port_max = ntohs(p->high);
3043 }
3044
3045 if (data[IFLA_VXLAN_PORT])
3046 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3047
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003048 if (data[IFLA_VXLAN_UDP_CSUM] &&
3049 !nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3050 conf.flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003051
3052 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
3053 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3054 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3055
3056 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
3057 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3058 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3059
3060 if (data[IFLA_VXLAN_REMCSUM_TX] &&
3061 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3062 conf.flags |= VXLAN_F_REMCSUM_TX;
3063
3064 if (data[IFLA_VXLAN_REMCSUM_RX] &&
3065 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3066 conf.flags |= VXLAN_F_REMCSUM_RX;
3067
3068 if (data[IFLA_VXLAN_GBP])
3069 conf.flags |= VXLAN_F_GBP;
3070
Jiri Bence1e53142016-04-05 14:47:13 +02003071 if (data[IFLA_VXLAN_GPE])
3072 conf.flags |= VXLAN_F_GPE;
3073
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003074 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
3075 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3076
Chen Haiquance577662016-05-27 10:49:11 +08003077 if (tb[IFLA_MTU])
3078 conf.mtu = nla_get_u32(tb[IFLA_MTU]);
3079
Jiri Benc35556212016-09-02 13:37:12 +02003080 return vxlan_dev_configure(src_net, dev, &conf);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003081}
3082
stephen hemmingerd3428942012-10-01 12:32:35 +00003083static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3084{
3085 struct vxlan_dev *vxlan = netdev_priv(dev);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003086
Tom Herbert58ce31c2015-08-19 17:07:33 -07003087 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003088 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003089 unregister_netdevice_queue(dev, head);
3090}
3091
3092static size_t vxlan_get_size(const struct net_device *dev)
3093{
3094
3095 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003096 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003097 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003098 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003099 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3100 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003101 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003102 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003103 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3104 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3105 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3106 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003107 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003108 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3109 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003110 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003111 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3112 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3113 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3114 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003115 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3116 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003117 0;
3118}
3119
3120static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3121{
3122 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003123 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003124 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003125 .low = htons(vxlan->cfg.port_min),
3126 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003127 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003128
Jiri Benc54bfd872016-02-16 21:58:58 +01003129 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003130 goto nla_put_failure;
3131
Cong Wange4c7ed42013-08-31 13:44:33 +08003132 if (!vxlan_addr_any(&dst->remote_ip)) {
3133 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003134 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3135 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003136 goto nla_put_failure;
3137#if IS_ENABLED(CONFIG_IPV6)
3138 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003139 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3140 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003141 goto nla_put_failure;
3142#endif
3143 }
3144 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003145
Atzm Watanabec7995c42013-04-16 02:50:52 +00003146 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003147 goto nla_put_failure;
3148
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003149 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3150 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003151 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003152 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003153 goto nla_put_failure;
3154#if IS_ENABLED(CONFIG_IPV6)
3155 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003156 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003157 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003158 goto nla_put_failure;
3159#endif
3160 }
3161 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003162
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003163 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3164 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003165 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003166 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3167 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3168 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3169 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3170 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3171 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3172 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3173 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3174 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003175 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3176 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003177 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3178 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3179 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003180 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003181 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003182 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3183 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3184 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08003185 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3186 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3187 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3188 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3189 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003190 goto nla_put_failure;
3191
stephen hemminger05f47d62012-10-09 20:35:50 +00003192 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3193 goto nla_put_failure;
3194
Thomas Graf35114942015-01-15 03:53:55 +01003195 if (vxlan->flags & VXLAN_F_GBP &&
3196 nla_put_flag(skb, IFLA_VXLAN_GBP))
3197 goto nla_put_failure;
3198
Jiri Bence1e53142016-04-05 14:47:13 +02003199 if (vxlan->flags & VXLAN_F_GPE &&
3200 nla_put_flag(skb, IFLA_VXLAN_GPE))
3201 goto nla_put_failure;
3202
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003203 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3204 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3205 goto nla_put_failure;
3206
stephen hemmingerd3428942012-10-01 12:32:35 +00003207 return 0;
3208
3209nla_put_failure:
3210 return -EMSGSIZE;
3211}
3212
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003213static struct net *vxlan_get_link_net(const struct net_device *dev)
3214{
3215 struct vxlan_dev *vxlan = netdev_priv(dev);
3216
3217 return vxlan->net;
3218}
3219
stephen hemmingerd3428942012-10-01 12:32:35 +00003220static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3221 .kind = "vxlan",
3222 .maxtype = IFLA_VXLAN_MAX,
3223 .policy = vxlan_policy,
3224 .priv_size = sizeof(struct vxlan_dev),
3225 .setup = vxlan_setup,
3226 .validate = vxlan_validate,
3227 .newlink = vxlan_newlink,
3228 .dellink = vxlan_dellink,
3229 .get_size = vxlan_get_size,
3230 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003231 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003232};
3233
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003234struct net_device *vxlan_dev_create(struct net *net, const char *name,
3235 u8 name_assign_type,
3236 struct vxlan_config *conf)
3237{
3238 struct nlattr *tb[IFLA_MAX + 1];
3239 struct net_device *dev;
3240 int err;
3241
3242 memset(&tb, 0, sizeof(tb));
3243
3244 dev = rtnl_create_link(net, name, name_assign_type,
3245 &vxlan_link_ops, tb);
3246 if (IS_ERR(dev))
3247 return dev;
3248
3249 err = vxlan_dev_configure(net, dev, conf);
3250 if (err < 0) {
3251 free_netdev(dev);
3252 return ERR_PTR(err);
3253 }
3254
3255 err = rtnl_configure_link(dev, NULL);
3256 if (err < 0) {
3257 LIST_HEAD(list_kill);
3258
3259 vxlan_dellink(dev, &list_kill);
3260 unregister_netdevice_many(&list_kill);
3261 return ERR_PTR(err);
3262 }
3263
3264 return dev;
3265}
3266EXPORT_SYMBOL_GPL(vxlan_dev_create);
3267
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003268static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3269 struct net_device *dev)
3270{
3271 struct vxlan_dev *vxlan, *next;
3272 LIST_HEAD(list_kill);
3273
3274 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3275 struct vxlan_rdst *dst = &vxlan->default_dst;
3276
3277 /* In case we created vxlan device with carrier
3278 * and we loose the carrier due to module unload
3279 * we also need to remove vxlan device. In other
3280 * cases, it's not necessary and remote_ifindex
3281 * is 0 here, so no matches.
3282 */
3283 if (dst->remote_ifindex == dev->ifindex)
3284 vxlan_dellink(vxlan->dev, &list_kill);
3285 }
3286
3287 unregister_netdevice_many(&list_kill);
3288}
3289
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003290static int vxlan_netdevice_event(struct notifier_block *unused,
3291 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003292{
3293 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003294 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003295
Daniel Borkmann783c1462014-01-22 21:07:53 +01003296 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003297 vxlan_handle_lowerdev_unregister(vn, dev);
Alexander Duyck7c46a642016-06-16 12:21:00 -07003298 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003299 vxlan_push_rx_ports(dev);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003300
3301 return NOTIFY_DONE;
3302}
3303
3304static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003305 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003306};
3307
stephen hemmingerd3428942012-10-01 12:32:35 +00003308static __net_init int vxlan_init_net(struct net *net)
3309{
3310 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003311 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003312
stephen hemminger553675f2013-05-16 11:35:20 +00003313 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003314 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003315
stephen hemminger553675f2013-05-16 11:35:20 +00003316 for (h = 0; h < PORT_HASH_SIZE; ++h)
3317 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003318
3319 return 0;
3320}
3321
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003322static void __net_exit vxlan_exit_net(struct net *net)
3323{
3324 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3325 struct vxlan_dev *vxlan, *next;
3326 struct net_device *dev, *aux;
3327 LIST_HEAD(list);
3328
3329 rtnl_lock();
3330 for_each_netdev_safe(net, dev, aux)
3331 if (dev->rtnl_link_ops == &vxlan_link_ops)
3332 unregister_netdevice_queue(dev, &list);
3333
3334 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3335 /* If vxlan->dev is in the same netns, it has already been added
3336 * to the list by the previous loop.
3337 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003338 if (!net_eq(dev_net(vxlan->dev), net)) {
3339 gro_cells_destroy(&vxlan->gro_cells);
John W. Linville13c3ed62015-05-18 13:51:24 -04003340 unregister_netdevice_queue(vxlan->dev, &list);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003341 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003342 }
3343
3344 unregister_netdevice_many(&list);
3345 rtnl_unlock();
3346}
3347
stephen hemmingerd3428942012-10-01 12:32:35 +00003348static struct pernet_operations vxlan_net_ops = {
3349 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003350 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003351 .id = &vxlan_net_id,
3352 .size = sizeof(struct vxlan_net),
3353};
3354
3355static int __init vxlan_init_module(void)
3356{
3357 int rc;
3358
3359 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3360
Daniel Borkmann783c1462014-01-22 21:07:53 +01003361 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003362 if (rc)
3363 goto out1;
3364
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003365 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003366 if (rc)
3367 goto out2;
3368
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003369 rc = rtnl_link_register(&vxlan_link_ops);
3370 if (rc)
3371 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003372
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003373 return 0;
3374out3:
3375 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003376out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003377 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003378out1:
3379 return rc;
3380}
Cong Wang7332a132013-05-27 22:35:53 +00003381late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003382
3383static void __exit vxlan_cleanup_module(void)
3384{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003385 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003386 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003387 unregister_pernet_subsys(&vxlan_net_ops);
3388 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003389}
3390module_exit(vxlan_cleanup_module);
3391
3392MODULE_LICENSE("GPL");
3393MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003394MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003395MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003396MODULE_ALIAS_RTNL_LINK("vxlan");