blob: 373713faa1f586f37f2e97086f13bd519ad1a633 [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{
Jiri Bencbeabc602017-07-02 19:00:57 +0200230 struct vxlan_dev_node *node;
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 Bencbeabc602017-07-02 19:00:57 +0200236 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
237 if (node->vxlan->default_dst.remote_vni == vni)
238 return node->vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000239 }
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 */
Roopa Prabhudeaee4d2017-06-11 16:32:50 -0700933 if (f->state & (NUD_PERMANENT | 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;
Eric Dumazet163b06a2019-02-07 12:27:38 -08001914 struct net_device *dev;
Li RongQingce6502a2014-10-16 08:49:41 +08001915 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
Eric Dumazet163b06a2019-02-07 12:27:38 -08001934 rcu_read_lock();
1935 dev = skb->dev;
1936 if (unlikely(!(dev->flags & IFF_UP))) {
1937 kfree_skb(skb);
1938 goto drop;
1939 }
1940
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001941 if (dst_vxlan->flags & VXLAN_F_LEARN)
Eric Dumazet163b06a2019-02-07 12:27:38 -08001942 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001943
1944 u64_stats_update_begin(&tx_stats->syncp);
1945 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001946 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001947 u64_stats_update_end(&tx_stats->syncp);
1948
1949 if (netif_rx(skb) == NET_RX_SUCCESS) {
1950 u64_stats_update_begin(&rx_stats->syncp);
1951 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001952 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001953 u64_stats_update_end(&rx_stats->syncp);
1954 } else {
Eric Dumazet163b06a2019-02-07 12:27:38 -08001955drop:
Li RongQingce6502a2014-10-16 08:49:41 +08001956 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001957 }
Eric Dumazet163b06a2019-02-07 12:27:38 -08001958 rcu_read_unlock();
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001959}
1960
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001961static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1962 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001963{
Paolo Abenid71785f2016-02-12 15:43:57 +01001964 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02001965 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00001966 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001967 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001968 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001969 const struct iphdr *old_iph;
Cong Wange4c7ed42013-08-31 13:44:33 +08001970 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07001971 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02001972 struct vxlan_metadata _md;
1973 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001974 __be16 src_port = 0, dst_port;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001975 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00001976 __be16 df = 0;
1977 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001978 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02001979 u32 flags = vxlan->flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001980 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01001981 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00001982
Jiri Benc61adedf2015-08-20 13:56:25 +02001983 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02001984
Jakub Kicinski0a40da42017-02-24 11:43:36 -08001985 rcu_read_lock();
Thomas Grafee122c72015-07-21 10:43:58 +02001986 if (rdst) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001987 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Thomas Grafee122c72015-07-21 10:43:58 +02001988 vni = rdst->remote_vni;
1989 dst = &rdst->remote_ip;
Brian Russella64407f2017-02-24 17:47:11 +00001990 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01001991 dst_cache = &rdst->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02001992 } else {
1993 if (!info) {
1994 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1995 dev->name);
1996 goto drop;
1997 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001998 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
Amir Vadaid817f432016-09-08 16:23:45 +03001999 vni = tunnel_id_to_key32(info->key.tun_id);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002000 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002001 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002002 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002003 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2004 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002005 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002006 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2007 }
Thomas Grafee122c72015-07-21 10:43:58 +02002008 dst = &remote_ip;
Paolo Abenid71785f2016-02-12 15:43:57 +01002009 dst_cache = &info->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02002010 }
David Stevense4f67ad2012-11-20 02:50:14 +00002011
Cong Wange4c7ed42013-08-31 13:44:33 +08002012 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00002013 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00002014 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002015 vxlan_encap_bypass(skb, vxlan, vxlan);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002016 goto out_unlock;
David Stevense4f67ad2012-11-20 02:50:14 +00002017 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002018 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00002019 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002020
stephen hemmingerd3428942012-10-01 12:32:35 +00002021 old_iph = ip_hdr(skb);
2022
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002023 ttl = vxlan->cfg.ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08002024 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00002025 ttl = 1;
2026
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002027 tos = vxlan->cfg.tos;
stephen hemmingerd3428942012-10-01 12:32:35 +00002028 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00002029 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002030
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002031 label = vxlan->cfg.label;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002032 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2033 vxlan->cfg.port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002034
Jiri Benca725e512015-08-20 13:56:30 +02002035 if (info) {
Jiri Benca725e512015-08-20 13:56:30 +02002036 ttl = info->key.ttl;
2037 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002038 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002039 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002040
2041 if (info->options_len)
Pravin B Shelar4c222792015-08-30 18:09:38 -07002042 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002043 } else {
2044 md->gbp = skb->mark;
2045 }
2046
Cong Wange4c7ed42013-08-31 13:44:33 +08002047 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002048 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2049
2050 if (!sock4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002051 goto drop;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002052 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002053
Jiri Benc1a8496b2016-02-02 18:09:14 +01002054 rt = vxlan_get_route(vxlan, skb,
2055 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002056 dst->sin.sin_addr.s_addr,
Brian Russella64407f2017-02-24 17:47:11 +00002057 &local_ip.sin.sin_addr.s_addr,
Paolo Abenid71785f2016-02-12 15:43:57 +01002058 dst_cache, info);
Cong Wange4c7ed42013-08-31 13:44:33 +08002059 if (IS_ERR(rt)) {
2060 netdev_dbg(dev, "no route to %pI4\n",
2061 &dst->sin.sin_addr.s_addr);
2062 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002063 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002064 }
2065
2066 if (rt->dst.dev == dev) {
2067 netdev_dbg(dev, "circular route to %pI4\n",
2068 &dst->sin.sin_addr.s_addr);
2069 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08002070 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002071 }
2072
2073 /* Bypass encapsulation if the destination is local */
pravin shelarbbec7802016-08-05 17:45:37 -07002074 if (!info && rt->rt_flags & RTCF_LOCAL &&
Cong Wange4c7ed42013-08-31 13:44:33 +08002075 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2076 struct vxlan_dev *dst_vxlan;
2077
2078 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002079 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002080 dst->sa.sa_family, dst_port,
2081 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002082 if (!dst_vxlan)
2083 goto tx_error;
2084 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002085 goto out_unlock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002086 }
2087
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002088 if (!info)
2089 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2090 else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
2091 df = htons(IP_DF);
2092
Cong Wange4c7ed42013-08-31 13:44:33 +08002093 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2094 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Jiri Bencf491e562016-02-02 18:09:16 +01002095 err = vxlan_build_skb(skb, &rt->dst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002096 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002097 if (err < 0)
2098 goto xmit_tx_error;
2099
Brian Russella64407f2017-02-24 17:47:11 +00002100 udp_tunnel_xmit_skb(rt, sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002101 dst->sin.sin_addr.s_addr, tos, ttl, df,
2102 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002103#if IS_ENABLED(CONFIG_IPV6)
2104 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002105 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002106 struct dst_entry *ndst;
Jiri Benc6f264e42015-08-20 13:56:29 +02002107 u32 rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002108
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002109 if (!sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002110 goto drop;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002111 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002112
Jiri Bence5d4b292015-12-07 13:04:30 +01002113 ndst = vxlan6_get_route(vxlan, skb,
Daniel Borkmann14006152016-03-04 15:15:08 +01002114 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002115 label, &dst->sin6.sin6_addr,
Brian Russella64407f2017-02-24 17:47:11 +00002116 &local_ip.sin6.sin6_addr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002117 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002118 if (IS_ERR(ndst)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002119 netdev_dbg(dev, "no route to %pI6\n",
2120 &dst->sin6.sin6_addr);
2121 dev->stats.tx_carrier_errors++;
2122 goto tx_error;
2123 }
2124
2125 if (ndst->dev == dev) {
2126 netdev_dbg(dev, "circular route to %pI6\n",
2127 &dst->sin6.sin6_addr);
2128 dst_release(ndst);
2129 dev->stats.collisions++;
2130 goto tx_error;
2131 }
2132
2133 /* Bypass encapsulation if the destination is local */
Jiri Benc6f264e42015-08-20 13:56:29 +02002134 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
pravin shelarbbec7802016-08-05 17:45:37 -07002135 if (!info && rt6i_flags & RTF_LOCAL &&
Jiri Benc6f264e42015-08-20 13:56:29 +02002136 !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002137 struct vxlan_dev *dst_vxlan;
2138
2139 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002140 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002141 dst->sa.sa_family, dst_port,
2142 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002143 if (!dst_vxlan)
2144 goto tx_error;
2145 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002146 goto out_unlock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002147 }
2148
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002149 if (!info)
2150 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Jesse Gross35e2d112016-01-20 16:22:47 -08002151
Daniel Borkmann14006152016-03-04 15:15:08 +01002152 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002153 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002154 skb_scrub_packet(skb, xnet);
2155 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002156 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002157 if (err < 0) {
2158 dst_release(ndst);
Haishuang Yan5e1e61a2016-09-04 18:52:51 +08002159 dev->stats.tx_errors++;
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002160 goto out_unlock;
Jiri Bencf491e562016-02-02 18:09:16 +01002161 }
2162 udp_tunnel6_xmit_skb(ndst, sk, skb, dev,
Brian Russella64407f2017-02-24 17:47:11 +00002163 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002164 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002165 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002166#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002167 }
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002168out_unlock:
2169 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002170 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002171
2172drop:
2173 dev->stats.tx_dropped++;
2174 goto tx_free;
2175
Jiri Bencf491e562016-02-02 18:09:16 +01002176xmit_tx_error:
2177 /* skb is already freed. */
2178 skb = NULL;
Pravin B Shelar49560532013-08-19 11:23:17 -07002179rt_tx_error:
2180 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002181tx_error:
2182 dev->stats.tx_errors++;
2183tx_free:
2184 dev_kfree_skb(skb);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002185 rcu_read_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00002186}
2187
David Stevens66817122013-03-15 04:35:51 +00002188/* Transmit local packets over Vxlan
2189 *
2190 * Outer IP header inherits ECN and DF from inner header.
2191 * Outer UDP destination is the VXLAN assigned port.
2192 * source port is based on hash of flow
2193 */
2194static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2195{
2196 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002197 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002198 struct ethhdr *eth;
2199 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002200 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002201 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002202
Jiri Benc61adedf2015-08-20 13:56:25 +02002203 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002204
David Stevens66817122013-03-15 04:35:51 +00002205 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002206
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002207 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
2208 if (info && info->mode & IP_TUNNEL_INFO_TX)
2209 vxlan_xmit_one(skb, dev, NULL, false);
2210 else
2211 kfree_skb(skb);
2212 return NETDEV_TX_OK;
2213 }
2214
2215 if (vxlan->flags & VXLAN_F_PROXY) {
2216 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002217 if (ntohs(eth->h_proto) == ETH_P_ARP)
2218 return arp_reduce(dev, skb);
2219#if IS_ENABLED(CONFIG_IPV6)
2220 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002221 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2222 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002223 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2224 struct nd_msg *msg;
2225
2226 msg = (struct nd_msg *)skb_transport_header(skb);
2227 if (msg->icmph.icmp6_code == 0 &&
2228 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2229 return neigh_reduce(dev, skb);
2230 }
2231#endif
2232 }
David Stevens66817122013-03-15 04:35:51 +00002233
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002234 eth = eth_hdr(skb);
David Stevens66817122013-03-15 04:35:51 +00002235 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002236 did_rsc = false;
2237
2238 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002239 (ntohs(eth->h_proto) == ETH_P_IP ||
2240 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002241 did_rsc = route_shortcircuit(dev, skb);
2242 if (did_rsc)
2243 f = vxlan_find_mac(vxlan, eth->h_dest);
2244 }
2245
David Stevens66817122013-03-15 04:35:51 +00002246 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002247 f = vxlan_find_mac(vxlan, all_zeros_mac);
2248 if (f == NULL) {
2249 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2250 !is_multicast_ether_addr(eth->h_dest))
2251 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002252
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002253 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002254 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002255 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002256 }
David Stevens66817122013-03-15 04:35:51 +00002257 }
2258
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002259 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2260 struct sk_buff *skb1;
2261
Eric Dumazet8f646c92014-01-06 09:54:31 -08002262 if (!fdst) {
2263 fdst = rdst;
2264 continue;
2265 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002266 skb1 = skb_clone(skb, GFP_ATOMIC);
2267 if (skb1)
2268 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2269 }
2270
Eric Dumazet8f646c92014-01-06 09:54:31 -08002271 if (fdst)
2272 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2273 else
2274 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002275 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002276}
2277
stephen hemmingerd3428942012-10-01 12:32:35 +00002278/* Walk the forwarding table and purge stale entries */
2279static void vxlan_cleanup(unsigned long arg)
2280{
2281 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2282 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2283 unsigned int h;
2284
2285 if (!netif_running(vxlan->dev))
2286 return;
2287
stephen hemmingerd3428942012-10-01 12:32:35 +00002288 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2289 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002290
2291 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002292 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2293 struct vxlan_fdb *f
2294 = container_of(p, struct vxlan_fdb, hlist);
2295 unsigned long timeout;
2296
Balakrishnan Raman32bd4d22017-01-23 20:44:33 -08002297 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002298 continue;
2299
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002300 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002301 if (time_before_eq(timeout, jiffies)) {
2302 netdev_dbg(vxlan->dev,
2303 "garbage collect %pM\n",
2304 f->eth_addr);
2305 f->state = NUD_STALE;
2306 vxlan_fdb_destroy(vxlan, f);
2307 } else if (time_before(timeout, next_timer))
2308 next_timer = timeout;
2309 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002310 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002311 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002312
2313 mod_timer(&vxlan->age_timer, next_timer);
2314}
2315
Mark Blochc242e1a2017-06-02 03:24:08 +03002316static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2317{
2318 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2319
2320 spin_lock(&vn->sock_lock);
Jiri Bencbeabc602017-07-02 19:00:57 +02002321 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2322#if IS_ENABLED(CONFIG_IPV6)
2323 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2324#endif
Mark Blochc242e1a2017-06-02 03:24:08 +03002325 spin_unlock(&vn->sock_lock);
2326}
2327
Jiri Bencbeabc602017-07-02 19:00:57 +02002328static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2329 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002330{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002331 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002332 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002333
Jiri Bencbeabc602017-07-02 19:00:57 +02002334 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002335 spin_lock(&vn->sock_lock);
Jiri Bencbeabc602017-07-02 19:00:57 +02002336 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002337 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002338}
2339
stephen hemmingerd3428942012-10-01 12:32:35 +00002340/* Setup stats when device is created */
2341static int vxlan_init(struct net_device *dev)
2342{
WANG Cong1c213bd2014-02-13 11:46:28 -08002343 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002344 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002345 return -ENOMEM;
2346
2347 return 0;
2348}
2349
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002350static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002351{
2352 struct vxlan_fdb *f;
2353
2354 spin_lock_bh(&vxlan->hash_lock);
2355 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2356 if (f)
2357 vxlan_fdb_destroy(vxlan, f);
2358 spin_unlock_bh(&vxlan->hash_lock);
2359}
2360
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002361static void vxlan_uninit(struct net_device *dev)
2362{
2363 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002364
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002365 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002366
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002367 free_percpu(dev->tstats);
2368}
2369
stephen hemmingerd3428942012-10-01 12:32:35 +00002370/* Start ageing timer and join group when device is brought up */
2371static int vxlan_open(struct net_device *dev)
2372{
2373 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002374 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002375
Jiri Benc205f3562015-09-24 13:50:01 +02002376 ret = vxlan_sock_add(vxlan);
2377 if (ret < 0)
2378 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002379
Gao feng79d4a942013-12-10 16:37:32 +08002380 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002381 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002382 if (ret == -EADDRINUSE)
2383 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002384 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002385 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002386 return ret;
2387 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002388 }
2389
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002390 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002391 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2392
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002393 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002394}
2395
2396/* Purge the forwarding table */
2397static void vxlan_flush(struct vxlan_dev *vxlan)
2398{
Cong Wang31fec5a2013-05-27 22:35:52 +00002399 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002400
2401 spin_lock_bh(&vxlan->hash_lock);
2402 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2403 struct hlist_node *p, *n;
2404 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2405 struct vxlan_fdb *f
2406 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002407 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2408 if (!is_zero_ether_addr(f->eth_addr))
2409 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002410 }
2411 }
2412 spin_unlock_bh(&vxlan->hash_lock);
2413}
2414
2415/* Cleanup timer and forwarding table on shutdown */
2416static int vxlan_stop(struct net_device *dev)
2417{
2418 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002419 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002420 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002421
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002422 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002423 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002424 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002425
2426 del_timer_sync(&vxlan->age_timer);
2427
2428 vxlan_flush(vxlan);
Jiri Benc205f3562015-09-24 13:50:01 +02002429 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002430
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002431 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002432}
2433
stephen hemmingerd3428942012-10-01 12:32:35 +00002434/* Stub, nothing needs to be done. */
2435static void vxlan_set_multicast_list(struct net_device *dev)
2436{
2437}
2438
David Wragg72564b52016-02-10 00:05:55 +00002439static int __vxlan_change_mtu(struct net_device *dev,
2440 struct net_device *lowerdev,
2441 struct vxlan_rdst *dst, int new_mtu, bool strict)
2442{
2443 int max_mtu = IP_MAX_MTU;
2444
2445 if (lowerdev)
2446 max_mtu = lowerdev->mtu;
2447
2448 if (dst->remote_ip.sa.sa_family == AF_INET6)
2449 max_mtu -= VXLAN6_HEADROOM;
2450 else
2451 max_mtu -= VXLAN_HEADROOM;
2452
2453 if (new_mtu < 68)
2454 return -EINVAL;
2455
2456 if (new_mtu > max_mtu) {
2457 if (strict)
2458 return -EINVAL;
2459
2460 new_mtu = max_mtu;
2461 }
2462
2463 dev->mtu = new_mtu;
2464 return 0;
2465}
2466
Daniel Borkmann345010b2013-12-18 00:21:08 +01002467static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2468{
2469 struct vxlan_dev *vxlan = netdev_priv(dev);
2470 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002471 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2472 dst->remote_ifindex);
2473 return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002474}
2475
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002476static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2477{
2478 struct vxlan_dev *vxlan = netdev_priv(dev);
2479 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2480 __be16 sport, dport;
2481
2482 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2483 vxlan->cfg.port_max, true);
2484 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2485
Jiri Benc239e9442015-12-07 13:04:31 +01002486 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002487 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002488 struct rtable *rt;
2489
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002490 if (!sock4)
Jiri Benc239e9442015-12-07 13:04:31 +01002491 return -EINVAL;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002492 rt = vxlan_get_route(vxlan, skb, 0, info->key.tos,
2493 info->key.u.ipv4.dst,
Paolo Abenif23fd872017-02-17 19:14:27 +01002494 &info->key.u.ipv4.src,
2495 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002496 if (IS_ERR(rt))
2497 return PTR_ERR(rt);
2498 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002499 } else {
2500#if IS_ENABLED(CONFIG_IPV6)
2501 struct dst_entry *ndst;
2502
Daniel Borkmann14006152016-03-04 15:15:08 +01002503 ndst = vxlan6_get_route(vxlan, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002504 info->key.label, &info->key.u.ipv6.dst,
Paolo Abenif23fd872017-02-17 19:14:27 +01002505 &info->key.u.ipv6.src,
2506 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002507 if (IS_ERR(ndst))
2508 return PTR_ERR(ndst);
2509 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002510#else /* !CONFIG_IPV6 */
2511 return -EPFNOSUPPORT;
2512#endif
2513 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002514 info->key.tp_src = sport;
2515 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002516 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002517}
2518
Jiri Benc0c867c92016-04-05 14:47:10 +02002519static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002520 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002521 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002522 .ndo_open = vxlan_open,
2523 .ndo_stop = vxlan_stop,
2524 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002525 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002526 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002527 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002528 .ndo_validate_addr = eth_validate_addr,
2529 .ndo_set_mac_address = eth_mac_addr,
2530 .ndo_fdb_add = vxlan_fdb_add,
2531 .ndo_fdb_del = vxlan_fdb_delete,
2532 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002533 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002534};
2535
Jiri Bence1e53142016-04-05 14:47:13 +02002536static const struct net_device_ops vxlan_netdev_raw_ops = {
2537 .ndo_init = vxlan_init,
2538 .ndo_uninit = vxlan_uninit,
2539 .ndo_open = vxlan_open,
2540 .ndo_stop = vxlan_stop,
2541 .ndo_start_xmit = vxlan_xmit,
2542 .ndo_get_stats64 = ip_tunnel_get_stats64,
2543 .ndo_change_mtu = vxlan_change_mtu,
2544 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2545};
2546
stephen hemmingerd3428942012-10-01 12:32:35 +00002547/* Info for udev, that this is a virtual tunnel endpoint */
2548static struct device_type vxlan_type = {
2549 .name = "vxlan",
2550};
2551
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002552/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002553 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002554 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002555 */
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02002556static void vxlan_push_rx_ports(struct net_device *dev)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002557{
2558 struct vxlan_sock *vs;
2559 struct net *net = dev_net(dev);
2560 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002561 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002562
2563 spin_lock(&vn->sock_lock);
2564 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Alexander Duycke7b3db52016-06-16 12:20:52 -07002565 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2566 udp_tunnel_push_rx_port(dev, vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002567 (vs->flags & VXLAN_F_GPE) ?
2568 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002569 UDP_TUNNEL_TYPE_VXLAN);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002570 }
2571 spin_unlock(&vn->sock_lock);
2572}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002573
stephen hemmingerd3428942012-10-01 12:32:35 +00002574/* Initialize the device structure. */
2575static void vxlan_setup(struct net_device *dev)
2576{
2577 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002578 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002579
Jiri Benc65226ef2016-04-28 16:36:30 +02002580 eth_hw_addr_random(dev);
2581 ether_setup(dev);
2582
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002583 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002584 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2585
stephen hemmingerd3428942012-10-01 12:32:35 +00002586 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002587 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002588 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002589 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002590
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002591 dev->vlan_features = dev->features;
2592 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002593 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002594 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002595 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002596 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002597 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002598
stephen hemminger553675f2013-05-16 11:35:20 +00002599 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002600 spin_lock_init(&vxlan->hash_lock);
2601
2602 init_timer_deferrable(&vxlan->age_timer);
2603 vxlan->age_timer.function = vxlan_cleanup;
2604 vxlan->age_timer.data = (unsigned long) vxlan;
2605
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002606 vxlan->cfg.dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002607
stephen hemmingerd3428942012-10-01 12:32:35 +00002608 vxlan->dev = dev;
2609
Tom Herbert58ce31c2015-08-19 17:07:33 -07002610 gro_cells_init(&vxlan->gro_cells, dev);
2611
stephen hemmingerd3428942012-10-01 12:32:35 +00002612 for (h = 0; h < FDB_HASH_SIZE; ++h)
2613 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2614}
2615
Jiri Benc0c867c92016-04-05 14:47:10 +02002616static void vxlan_ether_setup(struct net_device *dev)
2617{
Jiri Benc0c867c92016-04-05 14:47:10 +02002618 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2619 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2620 dev->netdev_ops = &vxlan_netdev_ether_ops;
2621}
2622
Jiri Bence1e53142016-04-05 14:47:13 +02002623static void vxlan_raw_setup(struct net_device *dev)
2624{
Jiri Benc65226ef2016-04-28 16:36:30 +02002625 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002626 dev->type = ARPHRD_NONE;
2627 dev->hard_header_len = 0;
2628 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002629 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2630 dev->netdev_ops = &vxlan_netdev_raw_ops;
2631}
2632
stephen hemmingerd3428942012-10-01 12:32:35 +00002633static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2634 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002635 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002636 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002637 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2638 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002639 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002640 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2641 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002642 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002643 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2644 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2645 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002646 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002647 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2648 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2649 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2650 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002651 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002652 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002653 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2654 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2655 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002656 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2657 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002658 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002659 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002660 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002661};
2662
2663static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2664{
2665 if (tb[IFLA_ADDRESS]) {
2666 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2667 pr_debug("invalid link address (not ethernet)\n");
2668 return -EINVAL;
2669 }
2670
2671 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2672 pr_debug("invalid all zero ethernet address\n");
2673 return -EADDRNOTAVAIL;
2674 }
2675 }
2676
2677 if (!data)
2678 return -EINVAL;
2679
2680 if (data[IFLA_VXLAN_ID]) {
2681 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
Matthias Schifferee2da792017-02-23 17:19:41 +01002682 if (id >= VXLAN_N_VID)
stephen hemmingerd3428942012-10-01 12:32:35 +00002683 return -ERANGE;
2684 }
2685
stephen hemminger05f47d62012-10-09 20:35:50 +00002686 if (data[IFLA_VXLAN_PORT_RANGE]) {
2687 const struct ifla_vxlan_port_range *p
2688 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2689
2690 if (ntohs(p->high) < ntohs(p->low)) {
2691 pr_debug("port range %u .. %u not valid\n",
2692 ntohs(p->low), ntohs(p->high));
2693 return -EINVAL;
2694 }
2695 }
2696
stephen hemmingerd3428942012-10-01 12:32:35 +00002697 return 0;
2698}
2699
Yan Burman1b13c972013-01-29 23:43:07 +00002700static void vxlan_get_drvinfo(struct net_device *netdev,
2701 struct ethtool_drvinfo *drvinfo)
2702{
2703 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2704 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2705}
2706
2707static const struct ethtool_ops vxlan_ethtool_ops = {
2708 .get_drvinfo = vxlan_get_drvinfo,
2709 .get_link = ethtool_op_get_link,
2710};
2711
Tom Herbert3ee64f32014-07-13 19:49:42 -07002712static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2713 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002714{
Cong Wange4c7ed42013-08-31 13:44:33 +08002715 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002716 struct udp_port_cfg udp_conf;
2717 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002718
Tom Herbert3ee64f32014-07-13 19:49:42 -07002719 memset(&udp_conf, 0, sizeof(udp_conf));
2720
2721 if (ipv6) {
2722 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002723 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002724 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002725 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002726 } else {
2727 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002728 }
2729
Tom Herbert3ee64f32014-07-13 19:49:42 -07002730 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002731
Tom Herbert3ee64f32014-07-13 19:49:42 -07002732 /* Open UDP socket */
2733 err = udp_sock_create(net, &udp_conf, &sock);
2734 if (err < 0)
2735 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002736
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002737 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002738}
2739
2740/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002741static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2742 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002743{
2744 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2745 struct vxlan_sock *vs;
2746 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002747 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002748 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002749
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002750 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002751 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002752 return ERR_PTR(-ENOMEM);
2753
2754 for (h = 0; h < VNI_HASH_SIZE; ++h)
2755 INIT_HLIST_HEAD(&vs->vni_list[h]);
2756
Tom Herbert3ee64f32014-07-13 19:49:42 -07002757 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002758 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002759 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2760 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002761 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002762 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002763 }
2764
Cong Wange4c7ed42013-08-31 13:44:33 +08002765 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002766 atomic_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002767 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002768
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002769 spin_lock(&vn->sock_lock);
2770 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07002771 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002772 (vs->flags & VXLAN_F_GPE) ?
2773 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002774 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002775 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002776
2777 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002778 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002779 tunnel_cfg.sk_user_data = vs;
2780 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002781 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002782 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002783 tunnel_cfg.gro_receive = vxlan_gro_receive;
2784 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002785
2786 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002787
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002788 return vs;
2789}
stephen hemminger553675f2013-05-16 11:35:20 +00002790
Jiri Bencb1be00a2015-09-24 13:50:02 +02002791static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002792{
Jiri Benc205f3562015-09-24 13:50:01 +02002793 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2794 struct vxlan_sock *vs = NULL;
Jiri Bencbeabc602017-07-02 19:00:57 +02002795 struct vxlan_dev_node *node;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002796
Jiri Benc205f3562015-09-24 13:50:01 +02002797 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002798 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002799 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2800 vxlan->cfg.dst_port, vxlan->flags);
2801 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002802 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002803 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002804 }
2805 spin_unlock(&vn->sock_lock);
2806 }
Jiri Benc205f3562015-09-24 13:50:01 +02002807 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002808 vs = vxlan_socket_create(vxlan->net, ipv6,
2809 vxlan->cfg.dst_port, vxlan->flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002810 if (IS_ERR(vs))
2811 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002812#if IS_ENABLED(CONFIG_IPV6)
Jiri Bencbeabc602017-07-02 19:00:57 +02002813 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002814 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Bencbeabc602017-07-02 19:00:57 +02002815 node = &vxlan->hlist6;
2816 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02002817#endif
Jiri Bencbeabc602017-07-02 19:00:57 +02002818 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002819 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Bencbeabc602017-07-02 19:00:57 +02002820 node = &vxlan->hlist4;
2821 }
2822 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02002823 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002824}
2825
Jiri Bencb1be00a2015-09-24 13:50:02 +02002826static int vxlan_sock_add(struct vxlan_dev *vxlan)
2827{
Jiri Bencb1be00a2015-09-24 13:50:02 +02002828 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
Jiri Benc9c8cd2a2017-04-27 21:24:35 +02002829 bool ipv6 = vxlan->flags & VXLAN_F_IPV6 || metadata;
2830 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002831 int ret = 0;
2832
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002833 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002834#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002835 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Benc9c8cd2a2017-04-27 21:24:35 +02002836 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02002837 ret = __vxlan_sock_add(vxlan, true);
Jiri Benc9c8cd2a2017-04-27 21:24:35 +02002838 if (ret < 0 && ret != -EAFNOSUPPORT)
2839 ipv4 = false;
2840 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002841#endif
Jiri Benc9c8cd2a2017-04-27 21:24:35 +02002842 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002843 ret = __vxlan_sock_add(vxlan, false);
2844 if (ret < 0)
2845 vxlan_sock_release(vxlan);
2846 return ret;
2847}
2848
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002849static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2850 struct vxlan_config *conf)
stephen hemmingerd3428942012-10-01 12:32:35 +00002851{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002852 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002853 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
Atzm Watanabec7995c42013-04-16 02:50:52 +00002854 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002855 unsigned short needed_headroom = ETH_HLEN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002856 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002857 bool use_ipv6 = false;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002858 __be16 default_port = vxlan->cfg.dst_port;
David Wragg7e059152016-02-10 00:05:58 +00002859 struct net_device *lowerdev = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00002860
Jiri Bence1e53142016-04-05 14:47:13 +02002861 if (conf->flags & VXLAN_F_GPE) {
Jiri Bence1e53142016-04-05 14:47:13 +02002862 /* For now, allow GPE only together with COLLECT_METADATA.
2863 * This can be relaxed later; in such case, the other side
2864 * of the PtP link will have to be provided.
2865 */
Jiri Benc35556212016-09-02 13:37:12 +02002866 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2867 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2868 pr_info("unsupported combination of extensions\n");
Jiri Bence1e53142016-04-05 14:47:13 +02002869 return -EINVAL;
Jiri Benc35556212016-09-02 13:37:12 +02002870 }
Jiri Bence1e53142016-04-05 14:47:13 +02002871
2872 vxlan_raw_setup(dev);
2873 } else {
2874 vxlan_ether_setup(dev);
2875 }
Jiri Benc0c867c92016-04-05 14:47:10 +02002876
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002877 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002878
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002879 dst->remote_vni = conf->vni;
2880
2881 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00002882
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002883 /* Unless IPv6 is explicitly requested, assume IPv4 */
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002884 if (!dst->remote_ip.sa.sa_family)
2885 dst->remote_ip.sa.sa_family = AF_INET;
stephen hemmingerd3428942012-10-01 12:32:35 +00002886
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002887 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
Jiri Benc057ba292015-09-17 16:11:11 +02002888 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2889 if (!IS_ENABLED(CONFIG_IPV6))
2890 return -EPFNOSUPPORT;
Cong Wange4c7ed42013-08-31 13:44:33 +08002891 use_ipv6 = true;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002892 vxlan->flags |= VXLAN_F_IPV6;
Jiri Benc057ba292015-09-17 16:11:11 +02002893 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002894
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002895 if (conf->label && !use_ipv6) {
2896 pr_info("label only supported in use with IPv6\n");
2897 return -EINVAL;
2898 }
2899
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002900 if (conf->remote_ifindex) {
David Wragg7e059152016-02-10 00:05:58 +00002901 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002902 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00002903
stephen hemminger34e02aa2012-10-09 20:35:53 +00002904 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002905 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002906 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002907 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002908
Cong Wange4c7ed42013-08-31 13:44:33 +08002909#if IS_ENABLED(CONFIG_IPV6)
2910 if (use_ipv6) {
2911 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2912 if (idev && idev->cnf.disable_ipv6) {
2913 pr_info("IPv6 is disabled via sysctl\n");
2914 return -EPERM;
2915 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002916 }
2917#endif
2918
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002919 if (!conf->mtu)
Cong Wange4c7ed42013-08-31 13:44:33 +08002920 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002921
Jiri Bencb1be00a2015-09-24 13:50:02 +02002922 needed_headroom = lowerdev->hard_header_len;
Jiri Benc9b4cdd52016-09-02 13:37:11 +02002923 } else if (vxlan_addr_multicast(&dst->remote_ip)) {
2924 pr_info("multicast destination requires interface to be specified\n");
2925 return -EINVAL;
Jiri Benc9dc2ad12015-09-17 16:11:10 +02002926 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002927
Felix Manlunas441f5af2017-03-29 17:56:43 -07002928 if (lowerdev) {
2929 dev->gso_max_size = lowerdev->gso_max_size;
2930 dev->gso_max_segs = lowerdev->gso_max_segs;
2931 }
2932
David Wragg7e059152016-02-10 00:05:58 +00002933 if (conf->mtu) {
2934 err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false);
2935 if (err)
2936 return err;
2937 }
2938
Jiri Bencb1be00a2015-09-24 13:50:02 +02002939 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2940 needed_headroom += VXLAN6_HEADROOM;
2941 else
2942 needed_headroom += VXLAN_HEADROOM;
2943 dev->needed_headroom = needed_headroom;
2944
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002945 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Jiri Bence1e53142016-04-05 14:47:13 +02002946 if (!vxlan->cfg.dst_port) {
2947 if (conf->flags & VXLAN_F_GPE)
Lance Richardsond1c95f9c2017-01-16 18:37:58 -05002948 vxlan->cfg.dst_port = htons(4790); /* IANA VXLAN-GPE port */
Jiri Bence1e53142016-04-05 14:47:13 +02002949 else
2950 vxlan->cfg.dst_port = default_port;
2951 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002952 vxlan->flags |= conf->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00002953
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002954 if (!vxlan->cfg.age_interval)
2955 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
Vincent Bernatafb97182012-10-30 10:27:16 +00002956
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002957 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2958 if (tmp->cfg.vni == conf->vni &&
2959 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2960 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2961 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2962 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
Jiri Benc35556212016-09-02 13:37:12 +02002963 (vxlan->flags & VXLAN_F_RCV_FLAGS)) {
2964 pr_info("duplicate VNI %u\n", be32_to_cpu(conf->vni));
2965 return -EEXIST;
2966 }
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002967 }
stephen hemminger553675f2013-05-16 11:35:20 +00002968
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002969 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002970
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002971 /* create an fdb entry for a valid default destination */
2972 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2973 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2974 &vxlan->default_dst.remote_ip,
2975 NUD_REACHABLE|NUD_PERMANENT,
2976 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002977 vxlan->cfg.dst_port,
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002978 vxlan->default_dst.remote_vni,
2979 vxlan->default_dst.remote_ifindex,
2980 NTF_SELF);
2981 if (err)
2982 return err;
2983 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002984
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002985 err = register_netdevice(dev);
2986 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002987 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002988 return err;
2989 }
2990
stephen hemminger553675f2013-05-16 11:35:20 +00002991 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002992
2993 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002994}
2995
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002996static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2997 struct nlattr *tb[], struct nlattr *data[])
2998{
2999 struct vxlan_config conf;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003000
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003001 memset(&conf, 0, sizeof(conf));
Jesse Grosse277de52015-10-16 16:36:00 -07003002
3003 if (data[IFLA_VXLAN_ID])
Jiri Benc54bfd872016-02-16 21:58:58 +01003004 conf.vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003005
3006 if (data[IFLA_VXLAN_GROUP]) {
3007 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3008 } else if (data[IFLA_VXLAN_GROUP6]) {
3009 if (!IS_ENABLED(CONFIG_IPV6))
3010 return -EPFNOSUPPORT;
3011
3012 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3013 conf.remote_ip.sa.sa_family = AF_INET6;
3014 }
3015
3016 if (data[IFLA_VXLAN_LOCAL]) {
3017 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3018 conf.saddr.sa.sa_family = AF_INET;
3019 } else if (data[IFLA_VXLAN_LOCAL6]) {
3020 if (!IS_ENABLED(CONFIG_IPV6))
3021 return -EPFNOSUPPORT;
3022
3023 /* TODO: respect scope id */
3024 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3025 conf.saddr.sa.sa_family = AF_INET6;
3026 }
3027
3028 if (data[IFLA_VXLAN_LINK])
3029 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3030
3031 if (data[IFLA_VXLAN_TOS])
3032 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3033
3034 if (data[IFLA_VXLAN_TTL])
3035 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3036
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003037 if (data[IFLA_VXLAN_LABEL])
3038 conf.label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3039 IPV6_FLOWLABEL_MASK;
3040
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003041 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
3042 conf.flags |= VXLAN_F_LEARN;
3043
3044 if (data[IFLA_VXLAN_AGEING])
3045 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3046
3047 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
3048 conf.flags |= VXLAN_F_PROXY;
3049
3050 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
3051 conf.flags |= VXLAN_F_RSC;
3052
3053 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3054 conf.flags |= VXLAN_F_L2MISS;
3055
3056 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3057 conf.flags |= VXLAN_F_L3MISS;
3058
3059 if (data[IFLA_VXLAN_LIMIT])
3060 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3061
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003062 if (data[IFLA_VXLAN_COLLECT_METADATA] &&
3063 nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3064 conf.flags |= VXLAN_F_COLLECT_METADATA;
3065
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003066 if (data[IFLA_VXLAN_PORT_RANGE]) {
3067 const struct ifla_vxlan_port_range *p
3068 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3069 conf.port_min = ntohs(p->low);
3070 conf.port_max = ntohs(p->high);
3071 }
3072
3073 if (data[IFLA_VXLAN_PORT])
3074 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3075
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003076 if (data[IFLA_VXLAN_UDP_CSUM] &&
3077 !nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3078 conf.flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003079
3080 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
3081 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3082 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3083
3084 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
3085 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3086 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3087
3088 if (data[IFLA_VXLAN_REMCSUM_TX] &&
3089 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3090 conf.flags |= VXLAN_F_REMCSUM_TX;
3091
3092 if (data[IFLA_VXLAN_REMCSUM_RX] &&
3093 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3094 conf.flags |= VXLAN_F_REMCSUM_RX;
3095
3096 if (data[IFLA_VXLAN_GBP])
3097 conf.flags |= VXLAN_F_GBP;
3098
Jiri Bence1e53142016-04-05 14:47:13 +02003099 if (data[IFLA_VXLAN_GPE])
3100 conf.flags |= VXLAN_F_GPE;
3101
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003102 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
3103 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3104
Chen Haiquance577662016-05-27 10:49:11 +08003105 if (tb[IFLA_MTU])
3106 conf.mtu = nla_get_u32(tb[IFLA_MTU]);
3107
Jiri Benc35556212016-09-02 13:37:12 +02003108 return vxlan_dev_configure(src_net, dev, &conf);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003109}
3110
stephen hemmingerd3428942012-10-01 12:32:35 +00003111static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3112{
3113 struct vxlan_dev *vxlan = netdev_priv(dev);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003114
Tom Herbert58ce31c2015-08-19 17:07:33 -07003115 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003116 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003117 unregister_netdevice_queue(dev, head);
3118}
3119
3120static size_t vxlan_get_size(const struct net_device *dev)
3121{
3122
3123 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003124 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003125 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003126 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003127 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3128 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003129 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003130 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003131 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3132 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3133 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3134 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003135 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003136 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3137 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003138 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003139 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3140 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3141 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3142 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003143 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3144 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003145 0;
3146}
3147
3148static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3149{
3150 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003151 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003152 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003153 .low = htons(vxlan->cfg.port_min),
3154 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003155 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003156
Jiri Benc54bfd872016-02-16 21:58:58 +01003157 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003158 goto nla_put_failure;
3159
Cong Wange4c7ed42013-08-31 13:44:33 +08003160 if (!vxlan_addr_any(&dst->remote_ip)) {
3161 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003162 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3163 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003164 goto nla_put_failure;
3165#if IS_ENABLED(CONFIG_IPV6)
3166 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003167 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3168 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003169 goto nla_put_failure;
3170#endif
3171 }
3172 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003173
Atzm Watanabec7995c42013-04-16 02:50:52 +00003174 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003175 goto nla_put_failure;
3176
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003177 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3178 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003179 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003180 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003181 goto nla_put_failure;
3182#if IS_ENABLED(CONFIG_IPV6)
3183 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003184 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003185 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003186 goto nla_put_failure;
3187#endif
3188 }
3189 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003190
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003191 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3192 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003193 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003194 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3195 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3196 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3197 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3198 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3199 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3200 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3201 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3202 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003203 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3204 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003205 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3206 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3207 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003208 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003209 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003210 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3211 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3212 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08003213 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3214 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3215 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3216 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3217 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003218 goto nla_put_failure;
3219
stephen hemminger05f47d62012-10-09 20:35:50 +00003220 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3221 goto nla_put_failure;
3222
Thomas Graf35114942015-01-15 03:53:55 +01003223 if (vxlan->flags & VXLAN_F_GBP &&
3224 nla_put_flag(skb, IFLA_VXLAN_GBP))
3225 goto nla_put_failure;
3226
Jiri Bence1e53142016-04-05 14:47:13 +02003227 if (vxlan->flags & VXLAN_F_GPE &&
3228 nla_put_flag(skb, IFLA_VXLAN_GPE))
3229 goto nla_put_failure;
3230
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003231 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3232 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3233 goto nla_put_failure;
3234
stephen hemmingerd3428942012-10-01 12:32:35 +00003235 return 0;
3236
3237nla_put_failure:
3238 return -EMSGSIZE;
3239}
3240
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003241static struct net *vxlan_get_link_net(const struct net_device *dev)
3242{
3243 struct vxlan_dev *vxlan = netdev_priv(dev);
3244
3245 return vxlan->net;
3246}
3247
stephen hemmingerd3428942012-10-01 12:32:35 +00003248static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3249 .kind = "vxlan",
3250 .maxtype = IFLA_VXLAN_MAX,
3251 .policy = vxlan_policy,
3252 .priv_size = sizeof(struct vxlan_dev),
3253 .setup = vxlan_setup,
3254 .validate = vxlan_validate,
3255 .newlink = vxlan_newlink,
3256 .dellink = vxlan_dellink,
3257 .get_size = vxlan_get_size,
3258 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003259 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003260};
3261
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003262struct net_device *vxlan_dev_create(struct net *net, const char *name,
3263 u8 name_assign_type,
3264 struct vxlan_config *conf)
3265{
3266 struct nlattr *tb[IFLA_MAX + 1];
3267 struct net_device *dev;
3268 int err;
3269
3270 memset(&tb, 0, sizeof(tb));
3271
3272 dev = rtnl_create_link(net, name, name_assign_type,
3273 &vxlan_link_ops, tb);
3274 if (IS_ERR(dev))
3275 return dev;
3276
3277 err = vxlan_dev_configure(net, dev, conf);
3278 if (err < 0) {
3279 free_netdev(dev);
3280 return ERR_PTR(err);
3281 }
3282
3283 err = rtnl_configure_link(dev, NULL);
3284 if (err < 0) {
3285 LIST_HEAD(list_kill);
3286
3287 vxlan_dellink(dev, &list_kill);
3288 unregister_netdevice_many(&list_kill);
3289 return ERR_PTR(err);
3290 }
3291
3292 return dev;
3293}
3294EXPORT_SYMBOL_GPL(vxlan_dev_create);
3295
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003296static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3297 struct net_device *dev)
3298{
3299 struct vxlan_dev *vxlan, *next;
3300 LIST_HEAD(list_kill);
3301
3302 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3303 struct vxlan_rdst *dst = &vxlan->default_dst;
3304
3305 /* In case we created vxlan device with carrier
3306 * and we loose the carrier due to module unload
3307 * we also need to remove vxlan device. In other
3308 * cases, it's not necessary and remote_ifindex
3309 * is 0 here, so no matches.
3310 */
3311 if (dst->remote_ifindex == dev->ifindex)
3312 vxlan_dellink(vxlan->dev, &list_kill);
3313 }
3314
3315 unregister_netdevice_many(&list_kill);
3316}
3317
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003318static int vxlan_netdevice_event(struct notifier_block *unused,
3319 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003320{
3321 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003322 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003323
Daniel Borkmann783c1462014-01-22 21:07:53 +01003324 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003325 vxlan_handle_lowerdev_unregister(vn, dev);
Alexander Duyck7c46a642016-06-16 12:21:00 -07003326 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003327 vxlan_push_rx_ports(dev);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003328
3329 return NOTIFY_DONE;
3330}
3331
3332static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003333 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003334};
3335
stephen hemmingerd3428942012-10-01 12:32:35 +00003336static __net_init int vxlan_init_net(struct net *net)
3337{
3338 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003339 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003340
stephen hemminger553675f2013-05-16 11:35:20 +00003341 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003342 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003343
stephen hemminger553675f2013-05-16 11:35:20 +00003344 for (h = 0; h < PORT_HASH_SIZE; ++h)
3345 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003346
3347 return 0;
3348}
3349
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003350static void __net_exit vxlan_exit_net(struct net *net)
3351{
3352 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3353 struct vxlan_dev *vxlan, *next;
3354 struct net_device *dev, *aux;
3355 LIST_HEAD(list);
3356
3357 rtnl_lock();
3358 for_each_netdev_safe(net, dev, aux)
3359 if (dev->rtnl_link_ops == &vxlan_link_ops)
3360 unregister_netdevice_queue(dev, &list);
3361
3362 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3363 /* If vxlan->dev is in the same netns, it has already been added
3364 * to the list by the previous loop.
3365 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003366 if (!net_eq(dev_net(vxlan->dev), net)) {
3367 gro_cells_destroy(&vxlan->gro_cells);
John W. Linville13c3ed62015-05-18 13:51:24 -04003368 unregister_netdevice_queue(vxlan->dev, &list);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003369 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003370 }
3371
3372 unregister_netdevice_many(&list);
3373 rtnl_unlock();
3374}
3375
stephen hemmingerd3428942012-10-01 12:32:35 +00003376static struct pernet_operations vxlan_net_ops = {
3377 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003378 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003379 .id = &vxlan_net_id,
3380 .size = sizeof(struct vxlan_net),
3381};
3382
3383static int __init vxlan_init_module(void)
3384{
3385 int rc;
3386
3387 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3388
Daniel Borkmann783c1462014-01-22 21:07:53 +01003389 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003390 if (rc)
3391 goto out1;
3392
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003393 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003394 if (rc)
3395 goto out2;
3396
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003397 rc = rtnl_link_register(&vxlan_link_ops);
3398 if (rc)
3399 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003400
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003401 return 0;
3402out3:
3403 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003404out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003405 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003406out1:
3407 return rc;
3408}
Cong Wang7332a132013-05-27 22:35:53 +00003409late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003410
3411static void __exit vxlan_cleanup_module(void)
3412{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003413 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003414 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003415 unregister_pernet_subsys(&vxlan_net_ops);
3416 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003417}
3418module_exit(vxlan_cleanup_module);
3419
3420MODULE_LICENSE("GPL");
3421MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003422MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003423MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003424MODULE_ALIAS_RTNL_LINK("vxlan");