blob: bc4542d9a08d33c04b67e73fa4f114a7a65e454d [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
Eric Dumazet9b0e9c22019-03-10 10:36:40 -07001383 rcu_read_lock();
1384
1385 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1386 rcu_read_unlock();
1387 atomic_long_inc(&vxlan->dev->rx_dropped);
1388 goto drop;
1389 }
1390
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001391 stats = this_cpu_ptr(vxlan->dev->tstats);
1392 u64_stats_update_begin(&stats->syncp);
1393 stats->rx_packets++;
1394 stats->rx_bytes += skb->len;
1395 u64_stats_update_end(&stats->syncp);
1396
1397 gro_cells_receive(&vxlan->gro_cells, skb);
Eric Dumazet9b0e9c22019-03-10 10:36:40 -07001398
1399 rcu_read_unlock();
1400
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001401 return 0;
1402
1403drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001404 /* Consume bad packet */
1405 kfree_skb(skb);
1406 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001407}
1408
David Stevense4f67ad2012-11-20 02:50:14 +00001409static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1410{
1411 struct vxlan_dev *vxlan = netdev_priv(dev);
1412 struct arphdr *parp;
1413 u8 *arpptr, *sha;
1414 __be32 sip, tip;
1415 struct neighbour *n;
1416
1417 if (dev->flags & IFF_NOARP)
1418 goto out;
1419
1420 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1421 dev->stats.tx_dropped++;
1422 goto out;
1423 }
1424 parp = arp_hdr(skb);
1425
1426 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1427 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1428 parp->ar_pro != htons(ETH_P_IP) ||
1429 parp->ar_op != htons(ARPOP_REQUEST) ||
1430 parp->ar_hln != dev->addr_len ||
1431 parp->ar_pln != 4)
1432 goto out;
1433 arpptr = (u8 *)parp + sizeof(struct arphdr);
1434 sha = arpptr;
1435 arpptr += dev->addr_len; /* sha */
1436 memcpy(&sip, arpptr, sizeof(sip));
1437 arpptr += sizeof(sip);
1438 arpptr += dev->addr_len; /* tha */
1439 memcpy(&tip, arpptr, sizeof(tip));
1440
1441 if (ipv4_is_loopback(tip) ||
1442 ipv4_is_multicast(tip))
1443 goto out;
1444
1445 n = neigh_lookup(&arp_tbl, &tip, dev);
1446
1447 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001448 struct vxlan_fdb *f;
1449 struct sk_buff *reply;
1450
1451 if (!(n->nud_state & NUD_CONNECTED)) {
1452 neigh_release(n);
1453 goto out;
1454 }
1455
1456 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001457 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001458 /* bridge-local neighbor */
1459 neigh_release(n);
1460 goto out;
1461 }
1462
1463 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1464 n->ha, sha);
1465
1466 neigh_release(n);
1467
David Stevens73461352014-03-18 12:32:29 -04001468 if (reply == NULL)
1469 goto out;
1470
David Stevense4f67ad2012-11-20 02:50:14 +00001471 skb_reset_mac_header(reply);
1472 __skb_pull(reply, skb_network_offset(reply));
1473 reply->ip_summed = CHECKSUM_UNNECESSARY;
1474 reply->pkt_type = PACKET_HOST;
1475
1476 if (netif_rx_ni(reply) == NET_RX_DROP)
1477 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001478 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1479 union vxlan_addr ipa = {
1480 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001481 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001482 };
1483
1484 vxlan_ip_miss(dev, &ipa);
1485 }
David Stevense4f67ad2012-11-20 02:50:14 +00001486out:
1487 consume_skb(skb);
1488 return NETDEV_TX_OK;
1489}
1490
Cong Wangf564f452013-08-31 13:44:36 +08001491#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001492static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1493 struct neighbour *n, bool isrouter)
1494{
1495 struct net_device *dev = request->dev;
1496 struct sk_buff *reply;
1497 struct nd_msg *ns, *na;
1498 struct ipv6hdr *pip6;
1499 u8 *daddr;
1500 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1501 int ns_olen;
1502 int i, len;
1503
1504 if (dev == NULL)
1505 return NULL;
1506
1507 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1508 sizeof(*na) + na_olen + dev->needed_tailroom;
1509 reply = alloc_skb(len, GFP_ATOMIC);
1510 if (reply == NULL)
1511 return NULL;
1512
1513 reply->protocol = htons(ETH_P_IPV6);
1514 reply->dev = dev;
1515 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1516 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001517 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001518
1519 ns = (struct nd_msg *)skb_transport_header(request);
1520
1521 daddr = eth_hdr(request)->h_source;
1522 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1523 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1524 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1525 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1526 break;
1527 }
1528 }
1529
1530 /* Ethernet header */
1531 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1532 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1533 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1534 reply->protocol = htons(ETH_P_IPV6);
1535
1536 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001537 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001538 skb_put(reply, sizeof(struct ipv6hdr));
1539
1540 /* IPv6 header */
1541
1542 pip6 = ipv6_hdr(reply);
1543 memset(pip6, 0, sizeof(struct ipv6hdr));
1544 pip6->version = 6;
1545 pip6->priority = ipv6_hdr(request)->priority;
1546 pip6->nexthdr = IPPROTO_ICMPV6;
1547 pip6->hop_limit = 255;
1548 pip6->daddr = ipv6_hdr(request)->saddr;
1549 pip6->saddr = *(struct in6_addr *)n->primary_key;
1550
1551 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001552 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001553
1554 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1555
1556 /* Neighbor Advertisement */
1557 memset(na, 0, sizeof(*na)+na_olen);
1558 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1559 na->icmph.icmp6_router = isrouter;
1560 na->icmph.icmp6_override = 1;
1561 na->icmph.icmp6_solicited = 1;
1562 na->target = ns->target;
1563 ether_addr_copy(&na->opt[2], n->ha);
1564 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1565 na->opt[1] = na_olen >> 3;
1566
1567 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1568 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1569 csum_partial(na, sizeof(*na)+na_olen, 0));
1570
1571 pip6->payload_len = htons(sizeof(*na)+na_olen);
1572
1573 skb_push(reply, sizeof(struct ipv6hdr));
1574
1575 reply->ip_summed = CHECKSUM_UNNECESSARY;
1576
1577 return reply;
1578}
1579
Cong Wangf564f452013-08-31 13:44:36 +08001580static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1581{
1582 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001583 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001584 const struct ipv6hdr *iphdr;
1585 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001586 struct neighbour *n;
1587 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001588
1589 in6_dev = __in6_dev_get(dev);
1590 if (!in6_dev)
1591 goto out;
1592
Cong Wangf564f452013-08-31 13:44:36 +08001593 iphdr = ipv6_hdr(skb);
1594 saddr = &iphdr->saddr;
1595 daddr = &iphdr->daddr;
1596
Cong Wangf564f452013-08-31 13:44:36 +08001597 msg = (struct nd_msg *)skb_transport_header(skb);
1598 if (msg->icmph.icmp6_code != 0 ||
1599 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1600 goto out;
1601
David Stevens4b29dba2014-03-24 10:39:58 -04001602 if (ipv6_addr_loopback(daddr) ||
1603 ipv6_addr_is_multicast(&msg->target))
1604 goto out;
1605
1606 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001607
1608 if (n) {
1609 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001610 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001611
1612 if (!(n->nud_state & NUD_CONNECTED)) {
1613 neigh_release(n);
1614 goto out;
1615 }
1616
1617 f = vxlan_find_mac(vxlan, n->ha);
1618 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1619 /* bridge-local neighbor */
1620 neigh_release(n);
1621 goto out;
1622 }
1623
David Stevens4b29dba2014-03-24 10:39:58 -04001624 reply = vxlan_na_create(skb, n,
1625 !!(f ? f->flags & NTF_ROUTER : 0));
1626
Cong Wangf564f452013-08-31 13:44:36 +08001627 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001628
1629 if (reply == NULL)
1630 goto out;
1631
1632 if (netif_rx_ni(reply) == NET_RX_DROP)
1633 dev->stats.rx_dropped++;
1634
Cong Wangf564f452013-08-31 13:44:36 +08001635 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001636 union vxlan_addr ipa = {
1637 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001638 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001639 };
1640
Cong Wangf564f452013-08-31 13:44:36 +08001641 vxlan_ip_miss(dev, &ipa);
1642 }
1643
1644out:
1645 consume_skb(skb);
1646 return NETDEV_TX_OK;
1647}
1648#endif
1649
David Stevense4f67ad2012-11-20 02:50:14 +00001650static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1651{
1652 struct vxlan_dev *vxlan = netdev_priv(dev);
1653 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001654
1655 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1656 return false;
1657
1658 n = NULL;
1659 switch (ntohs(eth_hdr(skb)->h_proto)) {
1660 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001661 {
1662 struct iphdr *pip;
1663
David Stevense4f67ad2012-11-20 02:50:14 +00001664 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1665 return false;
1666 pip = ip_hdr(skb);
1667 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001668 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1669 union vxlan_addr ipa = {
1670 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001671 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001672 };
1673
1674 vxlan_ip_miss(dev, &ipa);
1675 return false;
1676 }
1677
David Stevense4f67ad2012-11-20 02:50:14 +00001678 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001679 }
1680#if IS_ENABLED(CONFIG_IPV6)
1681 case ETH_P_IPV6:
1682 {
1683 struct ipv6hdr *pip6;
1684
1685 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1686 return false;
1687 pip6 = ipv6_hdr(skb);
1688 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1689 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1690 union vxlan_addr ipa = {
1691 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001692 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001693 };
1694
1695 vxlan_ip_miss(dev, &ipa);
1696 return false;
1697 }
1698
1699 break;
1700 }
1701#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001702 default:
1703 return false;
1704 }
1705
1706 if (n) {
1707 bool diff;
1708
Joe Perches7367d0b2013-09-01 11:51:23 -07001709 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001710 if (diff) {
1711 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1712 dev->addr_len);
1713 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1714 }
1715 neigh_release(n);
1716 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001717 }
1718
David Stevense4f67ad2012-11-20 02:50:14 +00001719 return false;
1720}
1721
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001722static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001723 struct vxlan_metadata *md)
1724{
1725 struct vxlanhdr_gbp *gbp;
1726
Thomas Grafdb79a622015-02-04 17:00:04 +01001727 if (!md->gbp)
1728 return;
1729
Thomas Graf35114942015-01-15 03:53:55 +01001730 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001731 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001732
1733 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1734 gbp->dont_learn = 1;
1735
1736 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1737 gbp->policy_applied = 1;
1738
1739 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1740}
1741
Jiri Bence1e53142016-04-05 14:47:13 +02001742static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1743 __be16 protocol)
1744{
1745 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1746
1747 gpe->np_applied = 1;
1748
1749 switch (protocol) {
1750 case htons(ETH_P_IP):
1751 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1752 return 0;
1753 case htons(ETH_P_IPV6):
1754 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1755 return 0;
1756 case htons(ETH_P_TEB):
1757 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1758 return 0;
1759 }
1760 return -EPFNOSUPPORT;
1761}
1762
Jiri Bencf491e562016-02-02 18:09:16 +01001763static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1764 int iphdr_len, __be32 vni,
1765 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001766 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001767{
Cong Wange4c7ed42013-08-31 13:44:33 +08001768 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001769 int min_headroom;
1770 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001771 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001772 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001773
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001774 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001775 skb->ip_summed == CHECKSUM_PARTIAL) {
1776 int csum_start = skb_checksum_start_offset(skb);
1777
1778 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1779 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1780 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001781 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001782 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001783 }
1784
Cong Wange4c7ed42013-08-31 13:44:33 +08001785 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
Jiri Bencf491e562016-02-02 18:09:16 +01001786 + VXLAN_HLEN + iphdr_len
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001787 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001788
1789 /* Need space for new headers (invalidates iph ptr) */
1790 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001791 if (unlikely(err))
1792 goto out_free;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001793
Jiri Pirko59682502014-11-19 14:04:59 +01001794 skb = vlan_hwaccel_push_inside(skb);
1795 if (WARN_ON(!skb))
1796 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001797
Alexander Duyckaed069d2016-04-14 15:33:37 -04001798 err = iptunnel_handle_offloads(skb, type);
1799 if (err)
1800 goto out_free;
Jesse Grossb736a622015-04-09 11:19:14 -07001801
Pravin B Shelar49560532013-08-19 11:23:17 -07001802 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001803 vxh->vx_flags = VXLAN_HF_VNI;
1804 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001805
Tom Herbertdfd86452015-01-12 17:00:38 -08001806 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001807 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001808
Jiri Benc54bfd872016-02-16 21:58:58 +01001809 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1810 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1811 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001812
1813 if (!skb_is_gso(skb)) {
1814 skb->ip_summed = CHECKSUM_NONE;
1815 skb->encapsulation = 0;
1816 }
1817 }
1818
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001819 if (vxflags & VXLAN_F_GBP)
1820 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001821 if (vxflags & VXLAN_F_GPE) {
1822 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1823 if (err < 0)
1824 goto out_free;
1825 inner_protocol = skb->protocol;
1826 }
Thomas Graf35114942015-01-15 03:53:55 +01001827
Jiri Bence1e53142016-04-05 14:47:13 +02001828 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001829 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02001830
1831out_free:
1832 kfree_skb(skb);
1833 return err;
Pravin B Shelar49560532013-08-19 11:23:17 -07001834}
Pravin B Shelar49560532013-08-19 11:23:17 -07001835
Jiri Benc1a8496b2016-02-02 18:09:14 +01001836static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan,
1837 struct sk_buff *skb, int oif, u8 tos,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001838 __be32 daddr, __be32 *saddr,
1839 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001840 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01001841{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001842 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01001843 struct rtable *rt = NULL;
1844 struct flowi4 fl4;
1845
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001846 if (tos && !info)
1847 use_cache = false;
1848 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001849 rt = dst_cache_get_ip4(dst_cache, saddr);
1850 if (rt)
1851 return rt;
1852 }
1853
Jiri Benc1a8496b2016-02-02 18:09:14 +01001854 memset(&fl4, 0, sizeof(fl4));
1855 fl4.flowi4_oif = oif;
1856 fl4.flowi4_tos = RT_TOS(tos);
1857 fl4.flowi4_mark = skb->mark;
1858 fl4.flowi4_proto = IPPROTO_UDP;
1859 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001860 fl4.saddr = *saddr;
Jiri Benc1a8496b2016-02-02 18:09:14 +01001861
1862 rt = ip_route_output_key(vxlan->net, &fl4);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001863 if (!IS_ERR(rt)) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01001864 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001865 if (use_cache)
1866 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
1867 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01001868 return rt;
1869}
1870
Jiri Bence5d4b292015-12-07 13:04:30 +01001871#if IS_ENABLED(CONFIG_IPV6)
1872static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
Daniel Borkmann14006152016-03-04 15:15:08 +01001873 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001874 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01001875 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001876 struct in6_addr *saddr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001877 struct dst_cache *dst_cache,
1878 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01001879{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001880 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001881 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01001882 struct dst_entry *ndst;
1883 struct flowi6 fl6;
1884 int err;
1885
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001886 if (!sock6)
1887 return ERR_PTR(-EIO);
1888
Daniel Borkmann14006152016-03-04 15:15:08 +01001889 if (tos && !info)
1890 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001891 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001892 ndst = dst_cache_get_ip6(dst_cache, saddr);
1893 if (ndst)
1894 return ndst;
1895 }
1896
Jiri Bence5d4b292015-12-07 13:04:30 +01001897 memset(&fl6, 0, sizeof(fl6));
1898 fl6.flowi6_oif = oif;
1899 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001900 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01001901 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01001902 fl6.flowi6_mark = skb->mark;
1903 fl6.flowi6_proto = IPPROTO_UDP;
1904
1905 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001906 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01001907 &ndst, &fl6);
1908 if (err < 0)
1909 return ERR_PTR(err);
1910
1911 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001912 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001913 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01001914 return ndst;
1915}
1916#endif
1917
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001918/* Bypass encapsulation if the destination is local */
1919static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1920 struct vxlan_dev *dst_vxlan)
1921{
Li RongQing8f849852014-01-04 13:57:59 +08001922 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001923 union vxlan_addr loopback;
1924 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Eric Dumazet163b06a2019-02-07 12:27:38 -08001925 struct net_device *dev;
Li RongQingce6502a2014-10-16 08:49:41 +08001926 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001927
Li RongQing8f849852014-01-04 13:57:59 +08001928 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1929 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001930 skb->pkt_type = PACKET_HOST;
1931 skb->encapsulation = 0;
1932 skb->dev = dst_vxlan->dev;
1933 __skb_pull(skb, skb_network_offset(skb));
1934
Cong Wange4c7ed42013-08-31 13:44:33 +08001935 if (remote_ip->sa.sa_family == AF_INET) {
1936 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1937 loopback.sa.sa_family = AF_INET;
1938#if IS_ENABLED(CONFIG_IPV6)
1939 } else {
1940 loopback.sin6.sin6_addr = in6addr_loopback;
1941 loopback.sa.sa_family = AF_INET6;
1942#endif
1943 }
1944
Eric Dumazet163b06a2019-02-07 12:27:38 -08001945 rcu_read_lock();
1946 dev = skb->dev;
1947 if (unlikely(!(dev->flags & IFF_UP))) {
1948 kfree_skb(skb);
1949 goto drop;
1950 }
1951
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001952 if (dst_vxlan->flags & VXLAN_F_LEARN)
Eric Dumazet163b06a2019-02-07 12:27:38 -08001953 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001954
1955 u64_stats_update_begin(&tx_stats->syncp);
1956 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001957 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001958 u64_stats_update_end(&tx_stats->syncp);
1959
1960 if (netif_rx(skb) == NET_RX_SUCCESS) {
1961 u64_stats_update_begin(&rx_stats->syncp);
1962 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001963 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001964 u64_stats_update_end(&rx_stats->syncp);
1965 } else {
Eric Dumazet163b06a2019-02-07 12:27:38 -08001966drop:
Li RongQingce6502a2014-10-16 08:49:41 +08001967 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001968 }
Eric Dumazet163b06a2019-02-07 12:27:38 -08001969 rcu_read_unlock();
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001970}
1971
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001972static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1973 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001974{
Paolo Abenid71785f2016-02-12 15:43:57 +01001975 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02001976 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00001977 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001978 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001979 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001980 const struct iphdr *old_iph;
Cong Wange4c7ed42013-08-31 13:44:33 +08001981 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07001982 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02001983 struct vxlan_metadata _md;
1984 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001985 __be16 src_port = 0, dst_port;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001986 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00001987 __be16 df = 0;
1988 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001989 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02001990 u32 flags = vxlan->flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001991 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01001992 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00001993
Jiri Benc61adedf2015-08-20 13:56:25 +02001994 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02001995
Jakub Kicinski0a40da42017-02-24 11:43:36 -08001996 rcu_read_lock();
Thomas Grafee122c72015-07-21 10:43:58 +02001997 if (rdst) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001998 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Thomas Grafee122c72015-07-21 10:43:58 +02001999 vni = rdst->remote_vni;
2000 dst = &rdst->remote_ip;
Brian Russella64407f2017-02-24 17:47:11 +00002001 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002002 dst_cache = &rdst->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02002003 } else {
2004 if (!info) {
2005 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2006 dev->name);
2007 goto drop;
2008 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002009 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
Amir Vadaid817f432016-09-08 16:23:45 +03002010 vni = tunnel_id_to_key32(info->key.tun_id);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002011 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002012 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002013 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002014 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2015 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002016 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002017 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2018 }
Thomas Grafee122c72015-07-21 10:43:58 +02002019 dst = &remote_ip;
Paolo Abenid71785f2016-02-12 15:43:57 +01002020 dst_cache = &info->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02002021 }
David Stevense4f67ad2012-11-20 02:50:14 +00002022
Cong Wange4c7ed42013-08-31 13:44:33 +08002023 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00002024 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00002025 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002026 vxlan_encap_bypass(skb, vxlan, vxlan);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002027 goto out_unlock;
David Stevense4f67ad2012-11-20 02:50:14 +00002028 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002029 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00002030 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002031
stephen hemmingerd3428942012-10-01 12:32:35 +00002032 old_iph = ip_hdr(skb);
2033
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002034 ttl = vxlan->cfg.ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08002035 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00002036 ttl = 1;
2037
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002038 tos = vxlan->cfg.tos;
stephen hemmingerd3428942012-10-01 12:32:35 +00002039 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00002040 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002041
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002042 label = vxlan->cfg.label;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002043 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2044 vxlan->cfg.port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002045
Jiri Benca725e512015-08-20 13:56:30 +02002046 if (info) {
Jiri Benca725e512015-08-20 13:56:30 +02002047 ttl = info->key.ttl;
2048 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002049 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002050 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002051
Xin Long0ee544c2019-10-29 01:24:32 +08002052 if (info->options_len) {
2053 if (info->options_len < sizeof(*md))
2054 goto drop;
Pravin B Shelar4c222792015-08-30 18:09:38 -07002055 md = ip_tunnel_info_opts(info);
Xin Long0ee544c2019-10-29 01:24:32 +08002056 }
Jiri Benca725e512015-08-20 13:56:30 +02002057 } else {
2058 md->gbp = skb->mark;
2059 }
2060
Cong Wange4c7ed42013-08-31 13:44:33 +08002061 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002062 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2063
2064 if (!sock4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002065 goto drop;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002066 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002067
Jiri Benc1a8496b2016-02-02 18:09:14 +01002068 rt = vxlan_get_route(vxlan, skb,
2069 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002070 dst->sin.sin_addr.s_addr,
Brian Russella64407f2017-02-24 17:47:11 +00002071 &local_ip.sin.sin_addr.s_addr,
Paolo Abenid71785f2016-02-12 15:43:57 +01002072 dst_cache, info);
Cong Wange4c7ed42013-08-31 13:44:33 +08002073 if (IS_ERR(rt)) {
2074 netdev_dbg(dev, "no route to %pI4\n",
2075 &dst->sin.sin_addr.s_addr);
2076 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002077 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002078 }
2079
2080 if (rt->dst.dev == dev) {
2081 netdev_dbg(dev, "circular route to %pI4\n",
2082 &dst->sin.sin_addr.s_addr);
2083 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08002084 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002085 }
2086
2087 /* Bypass encapsulation if the destination is local */
pravin shelarbbec7802016-08-05 17:45:37 -07002088 if (!info && rt->rt_flags & RTCF_LOCAL &&
Cong Wange4c7ed42013-08-31 13:44:33 +08002089 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2090 struct vxlan_dev *dst_vxlan;
2091
2092 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002093 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002094 dst->sa.sa_family, dst_port,
2095 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002096 if (!dst_vxlan)
2097 goto tx_error;
2098 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002099 goto out_unlock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002100 }
2101
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002102 if (!info)
2103 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2104 else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
2105 df = htons(IP_DF);
2106
Hangbin Liu57574c52020-01-02 17:23:45 +08002107 tos = ip_tunnel_ecn_encap(RT_TOS(tos), old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002108 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Jiri Bencf491e562016-02-02 18:09:16 +01002109 err = vxlan_build_skb(skb, &rt->dst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002110 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002111 if (err < 0)
2112 goto xmit_tx_error;
2113
Brian Russella64407f2017-02-24 17:47:11 +00002114 udp_tunnel_xmit_skb(rt, sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002115 dst->sin.sin_addr.s_addr, tos, ttl, df,
2116 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002117#if IS_ENABLED(CONFIG_IPV6)
2118 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002119 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002120 struct dst_entry *ndst;
Jiri Benc6f264e42015-08-20 13:56:29 +02002121 u32 rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002122
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002123 if (!sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002124 goto drop;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002125 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002126
Jiri Bence5d4b292015-12-07 13:04:30 +01002127 ndst = vxlan6_get_route(vxlan, skb,
Daniel Borkmann14006152016-03-04 15:15:08 +01002128 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002129 label, &dst->sin6.sin6_addr,
Brian Russella64407f2017-02-24 17:47:11 +00002130 &local_ip.sin6.sin6_addr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002131 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002132 if (IS_ERR(ndst)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002133 netdev_dbg(dev, "no route to %pI6\n",
2134 &dst->sin6.sin6_addr);
2135 dev->stats.tx_carrier_errors++;
2136 goto tx_error;
2137 }
2138
2139 if (ndst->dev == dev) {
2140 netdev_dbg(dev, "circular route to %pI6\n",
2141 &dst->sin6.sin6_addr);
2142 dst_release(ndst);
2143 dev->stats.collisions++;
2144 goto tx_error;
2145 }
2146
2147 /* Bypass encapsulation if the destination is local */
Jiri Benc6f264e42015-08-20 13:56:29 +02002148 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
pravin shelarbbec7802016-08-05 17:45:37 -07002149 if (!info && rt6i_flags & RTF_LOCAL &&
Jiri Benc6f264e42015-08-20 13:56:29 +02002150 !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002151 struct vxlan_dev *dst_vxlan;
2152
2153 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002154 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002155 dst->sa.sa_family, dst_port,
2156 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002157 if (!dst_vxlan)
2158 goto tx_error;
2159 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002160 goto out_unlock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002161 }
2162
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002163 if (!info)
2164 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Jesse Gross35e2d112016-01-20 16:22:47 -08002165
Hangbin Liu57574c52020-01-02 17:23:45 +08002166 tos = ip_tunnel_ecn_encap(RT_TOS(tos), old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002167 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002168 skb_scrub_packet(skb, xnet);
2169 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002170 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002171 if (err < 0) {
2172 dst_release(ndst);
Haishuang Yan5e1e61a2016-09-04 18:52:51 +08002173 dev->stats.tx_errors++;
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002174 goto out_unlock;
Jiri Bencf491e562016-02-02 18:09:16 +01002175 }
2176 udp_tunnel6_xmit_skb(ndst, sk, skb, dev,
Brian Russella64407f2017-02-24 17:47:11 +00002177 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002178 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002179 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002180#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002181 }
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002182out_unlock:
2183 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002184 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002185
2186drop:
2187 dev->stats.tx_dropped++;
2188 goto tx_free;
2189
Jiri Bencf491e562016-02-02 18:09:16 +01002190xmit_tx_error:
2191 /* skb is already freed. */
2192 skb = NULL;
Pravin B Shelar49560532013-08-19 11:23:17 -07002193rt_tx_error:
2194 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002195tx_error:
2196 dev->stats.tx_errors++;
2197tx_free:
2198 dev_kfree_skb(skb);
Jakub Kicinski0a40da42017-02-24 11:43:36 -08002199 rcu_read_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00002200}
2201
David Stevens66817122013-03-15 04:35:51 +00002202/* Transmit local packets over Vxlan
2203 *
2204 * Outer IP header inherits ECN and DF from inner header.
2205 * Outer UDP destination is the VXLAN assigned port.
2206 * source port is based on hash of flow
2207 */
2208static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2209{
2210 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002211 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002212 struct ethhdr *eth;
2213 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002214 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002215 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002216
Jiri Benc61adedf2015-08-20 13:56:25 +02002217 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002218
David Stevens66817122013-03-15 04:35:51 +00002219 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002220
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002221 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
2222 if (info && info->mode & IP_TUNNEL_INFO_TX)
2223 vxlan_xmit_one(skb, dev, NULL, false);
2224 else
2225 kfree_skb(skb);
2226 return NETDEV_TX_OK;
2227 }
2228
2229 if (vxlan->flags & VXLAN_F_PROXY) {
2230 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002231 if (ntohs(eth->h_proto) == ETH_P_ARP)
2232 return arp_reduce(dev, skb);
2233#if IS_ENABLED(CONFIG_IPV6)
2234 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002235 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2236 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002237 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2238 struct nd_msg *msg;
2239
2240 msg = (struct nd_msg *)skb_transport_header(skb);
2241 if (msg->icmph.icmp6_code == 0 &&
2242 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2243 return neigh_reduce(dev, skb);
2244 }
2245#endif
2246 }
David Stevens66817122013-03-15 04:35:51 +00002247
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002248 eth = eth_hdr(skb);
David Stevens66817122013-03-15 04:35:51 +00002249 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002250 did_rsc = false;
2251
2252 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002253 (ntohs(eth->h_proto) == ETH_P_IP ||
2254 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002255 did_rsc = route_shortcircuit(dev, skb);
2256 if (did_rsc)
2257 f = vxlan_find_mac(vxlan, eth->h_dest);
2258 }
2259
David Stevens66817122013-03-15 04:35:51 +00002260 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002261 f = vxlan_find_mac(vxlan, all_zeros_mac);
2262 if (f == NULL) {
2263 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2264 !is_multicast_ether_addr(eth->h_dest))
2265 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002266
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002267 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002268 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002269 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002270 }
David Stevens66817122013-03-15 04:35:51 +00002271 }
2272
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002273 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2274 struct sk_buff *skb1;
2275
Eric Dumazet8f646c92014-01-06 09:54:31 -08002276 if (!fdst) {
2277 fdst = rdst;
2278 continue;
2279 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002280 skb1 = skb_clone(skb, GFP_ATOMIC);
2281 if (skb1)
2282 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2283 }
2284
Eric Dumazet8f646c92014-01-06 09:54:31 -08002285 if (fdst)
2286 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2287 else
2288 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002289 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002290}
2291
stephen hemmingerd3428942012-10-01 12:32:35 +00002292/* Walk the forwarding table and purge stale entries */
2293static void vxlan_cleanup(unsigned long arg)
2294{
2295 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2296 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2297 unsigned int h;
2298
2299 if (!netif_running(vxlan->dev))
2300 return;
2301
stephen hemmingerd3428942012-10-01 12:32:35 +00002302 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2303 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002304
2305 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002306 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2307 struct vxlan_fdb *f
2308 = container_of(p, struct vxlan_fdb, hlist);
2309 unsigned long timeout;
2310
Balakrishnan Raman32bd4d22017-01-23 20:44:33 -08002311 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002312 continue;
2313
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002314 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002315 if (time_before_eq(timeout, jiffies)) {
2316 netdev_dbg(vxlan->dev,
2317 "garbage collect %pM\n",
2318 f->eth_addr);
2319 f->state = NUD_STALE;
2320 vxlan_fdb_destroy(vxlan, f);
2321 } else if (time_before(timeout, next_timer))
2322 next_timer = timeout;
2323 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002324 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002325 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002326
2327 mod_timer(&vxlan->age_timer, next_timer);
2328}
2329
Mark Blochc242e1a2017-06-02 03:24:08 +03002330static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2331{
2332 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2333
2334 spin_lock(&vn->sock_lock);
Jiri Bencbeabc602017-07-02 19:00:57 +02002335 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2336#if IS_ENABLED(CONFIG_IPV6)
2337 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2338#endif
Mark Blochc242e1a2017-06-02 03:24:08 +03002339 spin_unlock(&vn->sock_lock);
2340}
2341
Jiri Bencbeabc602017-07-02 19:00:57 +02002342static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2343 struct vxlan_dev_node *node)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002344{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002345 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002346 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002347
Jiri Bencbeabc602017-07-02 19:00:57 +02002348 node->vxlan = vxlan;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002349 spin_lock(&vn->sock_lock);
Jiri Bencbeabc602017-07-02 19:00:57 +02002350 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002351 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002352}
2353
stephen hemmingerd3428942012-10-01 12:32:35 +00002354/* Setup stats when device is created */
2355static int vxlan_init(struct net_device *dev)
2356{
Taehee Yoocc42f982020-03-18 13:28:09 +00002357 struct vxlan_dev *vxlan = netdev_priv(dev);
2358 int err;
2359
WANG Cong1c213bd2014-02-13 11:46:28 -08002360 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002361 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002362 return -ENOMEM;
2363
Taehee Yoocc42f982020-03-18 13:28:09 +00002364 err = gro_cells_init(&vxlan->gro_cells, dev);
2365 if (err) {
2366 free_percpu(dev->tstats);
2367 return err;
2368 }
2369
stephen hemmingerd3428942012-10-01 12:32:35 +00002370 return 0;
2371}
2372
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002373static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002374{
2375 struct vxlan_fdb *f;
2376
2377 spin_lock_bh(&vxlan->hash_lock);
2378 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2379 if (f)
2380 vxlan_fdb_destroy(vxlan, f);
2381 spin_unlock_bh(&vxlan->hash_lock);
2382}
2383
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002384static void vxlan_uninit(struct net_device *dev)
2385{
2386 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002387
Stefano Brivio8fa3e872019-03-08 16:40:57 +01002388 gro_cells_destroy(&vxlan->gro_cells);
2389
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002390 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002391
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002392 free_percpu(dev->tstats);
2393}
2394
stephen hemmingerd3428942012-10-01 12:32:35 +00002395/* Start ageing timer and join group when device is brought up */
2396static int vxlan_open(struct net_device *dev)
2397{
2398 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002399 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002400
Jiri Benc205f3562015-09-24 13:50:01 +02002401 ret = vxlan_sock_add(vxlan);
2402 if (ret < 0)
2403 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002404
Gao feng79d4a942013-12-10 16:37:32 +08002405 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002406 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002407 if (ret == -EADDRINUSE)
2408 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002409 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002410 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002411 return ret;
2412 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002413 }
2414
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002415 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002416 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2417
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002418 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002419}
2420
2421/* Purge the forwarding table */
2422static void vxlan_flush(struct vxlan_dev *vxlan)
2423{
Cong Wang31fec5a2013-05-27 22:35:52 +00002424 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002425
2426 spin_lock_bh(&vxlan->hash_lock);
2427 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2428 struct hlist_node *p, *n;
2429 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2430 struct vxlan_fdb *f
2431 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002432 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2433 if (!is_zero_ether_addr(f->eth_addr))
2434 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002435 }
2436 }
2437 spin_unlock_bh(&vxlan->hash_lock);
2438}
2439
2440/* Cleanup timer and forwarding table on shutdown */
2441static int vxlan_stop(struct net_device *dev)
2442{
2443 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002444 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002445 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002446
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002447 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002448 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002449 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002450
2451 del_timer_sync(&vxlan->age_timer);
2452
2453 vxlan_flush(vxlan);
Jiri Benc205f3562015-09-24 13:50:01 +02002454 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002455
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002456 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002457}
2458
stephen hemmingerd3428942012-10-01 12:32:35 +00002459/* Stub, nothing needs to be done. */
2460static void vxlan_set_multicast_list(struct net_device *dev)
2461{
2462}
2463
David Wragg72564b52016-02-10 00:05:55 +00002464static int __vxlan_change_mtu(struct net_device *dev,
2465 struct net_device *lowerdev,
2466 struct vxlan_rdst *dst, int new_mtu, bool strict)
2467{
2468 int max_mtu = IP_MAX_MTU;
2469
2470 if (lowerdev)
2471 max_mtu = lowerdev->mtu;
2472
2473 if (dst->remote_ip.sa.sa_family == AF_INET6)
2474 max_mtu -= VXLAN6_HEADROOM;
2475 else
2476 max_mtu -= VXLAN_HEADROOM;
2477
2478 if (new_mtu < 68)
2479 return -EINVAL;
2480
2481 if (new_mtu > max_mtu) {
2482 if (strict)
2483 return -EINVAL;
2484
2485 new_mtu = max_mtu;
2486 }
2487
2488 dev->mtu = new_mtu;
2489 return 0;
2490}
2491
Daniel Borkmann345010b2013-12-18 00:21:08 +01002492static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2493{
2494 struct vxlan_dev *vxlan = netdev_priv(dev);
2495 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002496 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2497 dst->remote_ifindex);
2498 return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002499}
2500
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002501static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2502{
2503 struct vxlan_dev *vxlan = netdev_priv(dev);
2504 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2505 __be16 sport, dport;
2506
2507 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2508 vxlan->cfg.port_max, true);
2509 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2510
Jiri Benc239e9442015-12-07 13:04:31 +01002511 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002512 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002513 struct rtable *rt;
2514
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002515 if (!sock4)
Jiri Benc239e9442015-12-07 13:04:31 +01002516 return -EINVAL;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002517 rt = vxlan_get_route(vxlan, skb, 0, info->key.tos,
2518 info->key.u.ipv4.dst,
Paolo Abenif23fd872017-02-17 19:14:27 +01002519 &info->key.u.ipv4.src,
2520 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002521 if (IS_ERR(rt))
2522 return PTR_ERR(rt);
2523 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002524 } else {
2525#if IS_ENABLED(CONFIG_IPV6)
2526 struct dst_entry *ndst;
2527
Daniel Borkmann14006152016-03-04 15:15:08 +01002528 ndst = vxlan6_get_route(vxlan, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002529 info->key.label, &info->key.u.ipv6.dst,
Paolo Abenif23fd872017-02-17 19:14:27 +01002530 &info->key.u.ipv6.src,
2531 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002532 if (IS_ERR(ndst))
2533 return PTR_ERR(ndst);
2534 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002535#else /* !CONFIG_IPV6 */
2536 return -EPFNOSUPPORT;
2537#endif
2538 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002539 info->key.tp_src = sport;
2540 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002541 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002542}
2543
Jiri Benc0c867c92016-04-05 14:47:10 +02002544static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002545 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002546 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002547 .ndo_open = vxlan_open,
2548 .ndo_stop = vxlan_stop,
2549 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002550 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002551 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002552 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002553 .ndo_validate_addr = eth_validate_addr,
2554 .ndo_set_mac_address = eth_mac_addr,
2555 .ndo_fdb_add = vxlan_fdb_add,
2556 .ndo_fdb_del = vxlan_fdb_delete,
2557 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002558 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002559};
2560
Jiri Bence1e53142016-04-05 14:47:13 +02002561static const struct net_device_ops vxlan_netdev_raw_ops = {
2562 .ndo_init = vxlan_init,
2563 .ndo_uninit = vxlan_uninit,
2564 .ndo_open = vxlan_open,
2565 .ndo_stop = vxlan_stop,
2566 .ndo_start_xmit = vxlan_xmit,
2567 .ndo_get_stats64 = ip_tunnel_get_stats64,
2568 .ndo_change_mtu = vxlan_change_mtu,
2569 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2570};
2571
stephen hemmingerd3428942012-10-01 12:32:35 +00002572/* Info for udev, that this is a virtual tunnel endpoint */
2573static struct device_type vxlan_type = {
2574 .name = "vxlan",
2575};
2576
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002577/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002578 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002579 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002580 */
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02002581static void vxlan_push_rx_ports(struct net_device *dev)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002582{
2583 struct vxlan_sock *vs;
2584 struct net *net = dev_net(dev);
2585 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002586 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002587
2588 spin_lock(&vn->sock_lock);
2589 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Alexander Duycke7b3db52016-06-16 12:20:52 -07002590 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2591 udp_tunnel_push_rx_port(dev, vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002592 (vs->flags & VXLAN_F_GPE) ?
2593 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002594 UDP_TUNNEL_TYPE_VXLAN);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002595 }
2596 spin_unlock(&vn->sock_lock);
2597}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002598
stephen hemmingerd3428942012-10-01 12:32:35 +00002599/* Initialize the device structure. */
2600static void vxlan_setup(struct net_device *dev)
2601{
2602 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002603 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002604
Jiri Benc65226ef2016-04-28 16:36:30 +02002605 eth_hw_addr_random(dev);
2606 ether_setup(dev);
2607
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002608 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002609 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2610
stephen hemmingerd3428942012-10-01 12:32:35 +00002611 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002612 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002613 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002614 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002615
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002616 dev->vlan_features = dev->features;
2617 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002618 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002619 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002620 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002621 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002622 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002623
stephen hemminger553675f2013-05-16 11:35:20 +00002624 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002625 spin_lock_init(&vxlan->hash_lock);
2626
2627 init_timer_deferrable(&vxlan->age_timer);
2628 vxlan->age_timer.function = vxlan_cleanup;
2629 vxlan->age_timer.data = (unsigned long) vxlan;
2630
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002631 vxlan->cfg.dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002632
stephen hemmingerd3428942012-10-01 12:32:35 +00002633 vxlan->dev = dev;
2634
2635 for (h = 0; h < FDB_HASH_SIZE; ++h)
2636 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2637}
2638
Jiri Benc0c867c92016-04-05 14:47:10 +02002639static void vxlan_ether_setup(struct net_device *dev)
2640{
Jiri Benc0c867c92016-04-05 14:47:10 +02002641 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2642 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2643 dev->netdev_ops = &vxlan_netdev_ether_ops;
2644}
2645
Jiri Bence1e53142016-04-05 14:47:13 +02002646static void vxlan_raw_setup(struct net_device *dev)
2647{
Jiri Benc65226ef2016-04-28 16:36:30 +02002648 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002649 dev->type = ARPHRD_NONE;
2650 dev->hard_header_len = 0;
2651 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002652 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2653 dev->netdev_ops = &vxlan_netdev_raw_ops;
2654}
2655
stephen hemmingerd3428942012-10-01 12:32:35 +00002656static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2657 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002658 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002659 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002660 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2661 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002662 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002663 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2664 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002665 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002666 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2667 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2668 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002669 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002670 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2671 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2672 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2673 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002674 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002675 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002676 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2677 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2678 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002679 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2680 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002681 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002682 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002683 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002684};
2685
2686static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2687{
2688 if (tb[IFLA_ADDRESS]) {
2689 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2690 pr_debug("invalid link address (not ethernet)\n");
2691 return -EINVAL;
2692 }
2693
2694 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2695 pr_debug("invalid all zero ethernet address\n");
2696 return -EADDRNOTAVAIL;
2697 }
2698 }
2699
2700 if (!data)
2701 return -EINVAL;
2702
2703 if (data[IFLA_VXLAN_ID]) {
2704 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
Matthias Schifferee2da792017-02-23 17:19:41 +01002705 if (id >= VXLAN_N_VID)
stephen hemmingerd3428942012-10-01 12:32:35 +00002706 return -ERANGE;
2707 }
2708
stephen hemminger05f47d62012-10-09 20:35:50 +00002709 if (data[IFLA_VXLAN_PORT_RANGE]) {
2710 const struct ifla_vxlan_port_range *p
2711 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2712
2713 if (ntohs(p->high) < ntohs(p->low)) {
2714 pr_debug("port range %u .. %u not valid\n",
2715 ntohs(p->low), ntohs(p->high));
2716 return -EINVAL;
2717 }
2718 }
2719
stephen hemmingerd3428942012-10-01 12:32:35 +00002720 return 0;
2721}
2722
Yan Burman1b13c972013-01-29 23:43:07 +00002723static void vxlan_get_drvinfo(struct net_device *netdev,
2724 struct ethtool_drvinfo *drvinfo)
2725{
2726 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2727 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2728}
2729
2730static const struct ethtool_ops vxlan_ethtool_ops = {
2731 .get_drvinfo = vxlan_get_drvinfo,
2732 .get_link = ethtool_op_get_link,
2733};
2734
Tom Herbert3ee64f32014-07-13 19:49:42 -07002735static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2736 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002737{
Cong Wange4c7ed42013-08-31 13:44:33 +08002738 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002739 struct udp_port_cfg udp_conf;
2740 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002741
Tom Herbert3ee64f32014-07-13 19:49:42 -07002742 memset(&udp_conf, 0, sizeof(udp_conf));
2743
2744 if (ipv6) {
2745 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002746 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002747 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002748 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002749 } else {
2750 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002751 }
2752
Tom Herbert3ee64f32014-07-13 19:49:42 -07002753 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002754
Tom Herbert3ee64f32014-07-13 19:49:42 -07002755 /* Open UDP socket */
2756 err = udp_sock_create(net, &udp_conf, &sock);
2757 if (err < 0)
2758 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002759
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002760 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002761}
2762
2763/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002764static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2765 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002766{
2767 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2768 struct vxlan_sock *vs;
2769 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002770 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002771 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002772
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002773 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002774 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002775 return ERR_PTR(-ENOMEM);
2776
2777 for (h = 0; h < VNI_HASH_SIZE; ++h)
2778 INIT_HLIST_HEAD(&vs->vni_list[h]);
2779
Tom Herbert3ee64f32014-07-13 19:49:42 -07002780 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002781 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002782 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2783 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002784 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002785 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002786 }
2787
Cong Wange4c7ed42013-08-31 13:44:33 +08002788 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002789 atomic_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002790 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002791
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002792 spin_lock(&vn->sock_lock);
2793 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07002794 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002795 (vs->flags & VXLAN_F_GPE) ?
2796 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002797 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002798 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002799
2800 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002801 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002802 tunnel_cfg.sk_user_data = vs;
2803 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002804 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002805 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002806 tunnel_cfg.gro_receive = vxlan_gro_receive;
2807 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002808
2809 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002810
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002811 return vs;
2812}
stephen hemminger553675f2013-05-16 11:35:20 +00002813
Jiri Bencb1be00a2015-09-24 13:50:02 +02002814static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002815{
Jiri Benc205f3562015-09-24 13:50:01 +02002816 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2817 struct vxlan_sock *vs = NULL;
Jiri Bencbeabc602017-07-02 19:00:57 +02002818 struct vxlan_dev_node *node;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002819
Jiri Benc205f3562015-09-24 13:50:01 +02002820 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002821 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002822 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2823 vxlan->cfg.dst_port, vxlan->flags);
2824 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002825 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002826 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002827 }
2828 spin_unlock(&vn->sock_lock);
2829 }
Jiri Benc205f3562015-09-24 13:50:01 +02002830 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002831 vs = vxlan_socket_create(vxlan->net, ipv6,
2832 vxlan->cfg.dst_port, vxlan->flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002833 if (IS_ERR(vs))
2834 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002835#if IS_ENABLED(CONFIG_IPV6)
Jiri Bencbeabc602017-07-02 19:00:57 +02002836 if (ipv6) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002837 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Bencbeabc602017-07-02 19:00:57 +02002838 node = &vxlan->hlist6;
2839 } else
Jiri Bencb1be00a2015-09-24 13:50:02 +02002840#endif
Jiri Bencbeabc602017-07-02 19:00:57 +02002841 {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002842 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Bencbeabc602017-07-02 19:00:57 +02002843 node = &vxlan->hlist4;
2844 }
2845 vxlan_vs_add_dev(vs, vxlan, node);
Jiri Benc205f3562015-09-24 13:50:01 +02002846 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002847}
2848
Jiri Bencb1be00a2015-09-24 13:50:02 +02002849static int vxlan_sock_add(struct vxlan_dev *vxlan)
2850{
Jiri Bencb1be00a2015-09-24 13:50:02 +02002851 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
Jiri Benc9c8cd2a2017-04-27 21:24:35 +02002852 bool ipv6 = vxlan->flags & VXLAN_F_IPV6 || metadata;
2853 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002854 int ret = 0;
2855
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002856 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002857#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002858 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Benc9c8cd2a2017-04-27 21:24:35 +02002859 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02002860 ret = __vxlan_sock_add(vxlan, true);
Jiri Benc9c8cd2a2017-04-27 21:24:35 +02002861 if (ret < 0 && ret != -EAFNOSUPPORT)
2862 ipv4 = false;
2863 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002864#endif
Jiri Benc9c8cd2a2017-04-27 21:24:35 +02002865 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002866 ret = __vxlan_sock_add(vxlan, false);
2867 if (ret < 0)
2868 vxlan_sock_release(vxlan);
2869 return ret;
2870}
2871
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002872static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2873 struct vxlan_config *conf)
stephen hemmingerd3428942012-10-01 12:32:35 +00002874{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002875 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002876 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
Atzm Watanabec7995c42013-04-16 02:50:52 +00002877 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002878 unsigned short needed_headroom = ETH_HLEN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002879 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002880 bool use_ipv6 = false;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002881 __be16 default_port = vxlan->cfg.dst_port;
David Wragg7e059152016-02-10 00:05:58 +00002882 struct net_device *lowerdev = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00002883
Jiri Bence1e53142016-04-05 14:47:13 +02002884 if (conf->flags & VXLAN_F_GPE) {
Jiri Bence1e53142016-04-05 14:47:13 +02002885 /* For now, allow GPE only together with COLLECT_METADATA.
2886 * This can be relaxed later; in such case, the other side
2887 * of the PtP link will have to be provided.
2888 */
Jiri Benc35556212016-09-02 13:37:12 +02002889 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2890 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2891 pr_info("unsupported combination of extensions\n");
Jiri Bence1e53142016-04-05 14:47:13 +02002892 return -EINVAL;
Jiri Benc35556212016-09-02 13:37:12 +02002893 }
Jiri Bence1e53142016-04-05 14:47:13 +02002894
2895 vxlan_raw_setup(dev);
2896 } else {
2897 vxlan_ether_setup(dev);
2898 }
Jiri Benc0c867c92016-04-05 14:47:10 +02002899
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002900 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002901
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002902 dst->remote_vni = conf->vni;
2903
2904 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00002905
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002906 /* Unless IPv6 is explicitly requested, assume IPv4 */
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002907 if (!dst->remote_ip.sa.sa_family)
2908 dst->remote_ip.sa.sa_family = AF_INET;
stephen hemmingerd3428942012-10-01 12:32:35 +00002909
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002910 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
Jiri Benc057ba292015-09-17 16:11:11 +02002911 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2912 if (!IS_ENABLED(CONFIG_IPV6))
2913 return -EPFNOSUPPORT;
Cong Wange4c7ed42013-08-31 13:44:33 +08002914 use_ipv6 = true;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002915 vxlan->flags |= VXLAN_F_IPV6;
Jiri Benc057ba292015-09-17 16:11:11 +02002916 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002917
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002918 if (conf->label && !use_ipv6) {
2919 pr_info("label only supported in use with IPv6\n");
2920 return -EINVAL;
2921 }
2922
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002923 if (conf->remote_ifindex) {
David Wragg7e059152016-02-10 00:05:58 +00002924 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002925 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00002926
stephen hemminger34e02aa2012-10-09 20:35:53 +00002927 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002928 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002929 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002930 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002931
Cong Wange4c7ed42013-08-31 13:44:33 +08002932#if IS_ENABLED(CONFIG_IPV6)
2933 if (use_ipv6) {
2934 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2935 if (idev && idev->cnf.disable_ipv6) {
2936 pr_info("IPv6 is disabled via sysctl\n");
2937 return -EPERM;
2938 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002939 }
2940#endif
2941
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002942 if (!conf->mtu)
Cong Wange4c7ed42013-08-31 13:44:33 +08002943 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002944
Jiri Bencb1be00a2015-09-24 13:50:02 +02002945 needed_headroom = lowerdev->hard_header_len;
Jiri Benc9b4cdd52016-09-02 13:37:11 +02002946 } else if (vxlan_addr_multicast(&dst->remote_ip)) {
2947 pr_info("multicast destination requires interface to be specified\n");
2948 return -EINVAL;
Jiri Benc9dc2ad12015-09-17 16:11:10 +02002949 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002950
Felix Manlunas441f5af2017-03-29 17:56:43 -07002951 if (lowerdev) {
2952 dev->gso_max_size = lowerdev->gso_max_size;
2953 dev->gso_max_segs = lowerdev->gso_max_segs;
2954 }
2955
David Wragg7e059152016-02-10 00:05:58 +00002956 if (conf->mtu) {
2957 err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false);
2958 if (err)
2959 return err;
2960 }
2961
Jiri Bencb1be00a2015-09-24 13:50:02 +02002962 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2963 needed_headroom += VXLAN6_HEADROOM;
2964 else
2965 needed_headroom += VXLAN_HEADROOM;
2966 dev->needed_headroom = needed_headroom;
2967
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002968 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Jiri Bence1e53142016-04-05 14:47:13 +02002969 if (!vxlan->cfg.dst_port) {
2970 if (conf->flags & VXLAN_F_GPE)
Lance Richardsond1c95f9c2017-01-16 18:37:58 -05002971 vxlan->cfg.dst_port = htons(4790); /* IANA VXLAN-GPE port */
Jiri Bence1e53142016-04-05 14:47:13 +02002972 else
2973 vxlan->cfg.dst_port = default_port;
2974 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002975 vxlan->flags |= conf->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00002976
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002977 if (!vxlan->cfg.age_interval)
2978 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
Vincent Bernatafb97182012-10-30 10:27:16 +00002979
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002980 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2981 if (tmp->cfg.vni == conf->vni &&
2982 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2983 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2984 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2985 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
Jiri Benc35556212016-09-02 13:37:12 +02002986 (vxlan->flags & VXLAN_F_RCV_FLAGS)) {
2987 pr_info("duplicate VNI %u\n", be32_to_cpu(conf->vni));
2988 return -EEXIST;
2989 }
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002990 }
stephen hemminger553675f2013-05-16 11:35:20 +00002991
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002992 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002993
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002994 /* create an fdb entry for a valid default destination */
2995 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2996 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2997 &vxlan->default_dst.remote_ip,
2998 NUD_REACHABLE|NUD_PERMANENT,
2999 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003000 vxlan->cfg.dst_port,
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07003001 vxlan->default_dst.remote_vni,
3002 vxlan->default_dst.remote_ifindex,
3003 NTF_SELF);
3004 if (err)
3005 return err;
3006 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003007
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003008 err = register_netdevice(dev);
3009 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07003010 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03003011 return err;
3012 }
3013
stephen hemminger553675f2013-05-16 11:35:20 +00003014 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00003015
3016 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00003017}
3018
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003019static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3020 struct nlattr *tb[], struct nlattr *data[])
3021{
3022 struct vxlan_config conf;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003023
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003024 memset(&conf, 0, sizeof(conf));
Jesse Grosse277de52015-10-16 16:36:00 -07003025
3026 if (data[IFLA_VXLAN_ID])
Jiri Benc54bfd872016-02-16 21:58:58 +01003027 conf.vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003028
3029 if (data[IFLA_VXLAN_GROUP]) {
3030 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3031 } else if (data[IFLA_VXLAN_GROUP6]) {
3032 if (!IS_ENABLED(CONFIG_IPV6))
3033 return -EPFNOSUPPORT;
3034
3035 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3036 conf.remote_ip.sa.sa_family = AF_INET6;
3037 }
3038
3039 if (data[IFLA_VXLAN_LOCAL]) {
3040 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3041 conf.saddr.sa.sa_family = AF_INET;
3042 } else if (data[IFLA_VXLAN_LOCAL6]) {
3043 if (!IS_ENABLED(CONFIG_IPV6))
3044 return -EPFNOSUPPORT;
3045
3046 /* TODO: respect scope id */
3047 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3048 conf.saddr.sa.sa_family = AF_INET6;
3049 }
3050
3051 if (data[IFLA_VXLAN_LINK])
3052 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3053
3054 if (data[IFLA_VXLAN_TOS])
3055 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3056
3057 if (data[IFLA_VXLAN_TTL])
3058 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3059
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003060 if (data[IFLA_VXLAN_LABEL])
3061 conf.label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3062 IPV6_FLOWLABEL_MASK;
3063
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003064 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
3065 conf.flags |= VXLAN_F_LEARN;
3066
3067 if (data[IFLA_VXLAN_AGEING])
3068 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3069
3070 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
3071 conf.flags |= VXLAN_F_PROXY;
3072
3073 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
3074 conf.flags |= VXLAN_F_RSC;
3075
3076 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3077 conf.flags |= VXLAN_F_L2MISS;
3078
3079 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3080 conf.flags |= VXLAN_F_L3MISS;
3081
3082 if (data[IFLA_VXLAN_LIMIT])
3083 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3084
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003085 if (data[IFLA_VXLAN_COLLECT_METADATA] &&
3086 nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3087 conf.flags |= VXLAN_F_COLLECT_METADATA;
3088
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003089 if (data[IFLA_VXLAN_PORT_RANGE]) {
3090 const struct ifla_vxlan_port_range *p
3091 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3092 conf.port_min = ntohs(p->low);
3093 conf.port_max = ntohs(p->high);
3094 }
3095
3096 if (data[IFLA_VXLAN_PORT])
3097 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3098
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003099 if (data[IFLA_VXLAN_UDP_CSUM] &&
3100 !nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3101 conf.flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003102
3103 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
3104 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3105 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3106
3107 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
3108 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3109 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3110
3111 if (data[IFLA_VXLAN_REMCSUM_TX] &&
3112 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3113 conf.flags |= VXLAN_F_REMCSUM_TX;
3114
3115 if (data[IFLA_VXLAN_REMCSUM_RX] &&
3116 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3117 conf.flags |= VXLAN_F_REMCSUM_RX;
3118
3119 if (data[IFLA_VXLAN_GBP])
3120 conf.flags |= VXLAN_F_GBP;
3121
Jiri Bence1e53142016-04-05 14:47:13 +02003122 if (data[IFLA_VXLAN_GPE])
3123 conf.flags |= VXLAN_F_GPE;
3124
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003125 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
3126 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3127
Chen Haiquance577662016-05-27 10:49:11 +08003128 if (tb[IFLA_MTU])
3129 conf.mtu = nla_get_u32(tb[IFLA_MTU]);
3130
Jiri Benc35556212016-09-02 13:37:12 +02003131 return vxlan_dev_configure(src_net, dev, &conf);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003132}
3133
stephen hemmingerd3428942012-10-01 12:32:35 +00003134static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3135{
3136 struct vxlan_dev *vxlan = netdev_priv(dev);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003137
stephen hemminger553675f2013-05-16 11:35:20 +00003138 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003139 unregister_netdevice_queue(dev, head);
3140}
3141
3142static size_t vxlan_get_size(const struct net_device *dev)
3143{
3144
3145 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003146 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003147 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003148 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003149 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3150 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003151 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003152 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003153 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3154 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3155 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3156 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003157 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003158 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3159 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003160 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003161 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3162 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3163 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3164 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003165 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3166 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003167 0;
3168}
3169
3170static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3171{
3172 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003173 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003174 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003175 .low = htons(vxlan->cfg.port_min),
3176 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003177 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003178
Jiri Benc54bfd872016-02-16 21:58:58 +01003179 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003180 goto nla_put_failure;
3181
Cong Wange4c7ed42013-08-31 13:44:33 +08003182 if (!vxlan_addr_any(&dst->remote_ip)) {
3183 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003184 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3185 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003186 goto nla_put_failure;
3187#if IS_ENABLED(CONFIG_IPV6)
3188 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003189 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3190 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003191 goto nla_put_failure;
3192#endif
3193 }
3194 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003195
Atzm Watanabec7995c42013-04-16 02:50:52 +00003196 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003197 goto nla_put_failure;
3198
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003199 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3200 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003201 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003202 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003203 goto nla_put_failure;
3204#if IS_ENABLED(CONFIG_IPV6)
3205 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003206 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003207 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003208 goto nla_put_failure;
3209#endif
3210 }
3211 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003212
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003213 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3214 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003215 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003216 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3217 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3218 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3219 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3220 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3221 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3222 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3223 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3224 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003225 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3226 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003227 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3228 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3229 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003230 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003231 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003232 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3233 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3234 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08003235 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3236 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3237 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3238 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3239 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003240 goto nla_put_failure;
3241
stephen hemminger05f47d62012-10-09 20:35:50 +00003242 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3243 goto nla_put_failure;
3244
Thomas Graf35114942015-01-15 03:53:55 +01003245 if (vxlan->flags & VXLAN_F_GBP &&
3246 nla_put_flag(skb, IFLA_VXLAN_GBP))
3247 goto nla_put_failure;
3248
Jiri Bence1e53142016-04-05 14:47:13 +02003249 if (vxlan->flags & VXLAN_F_GPE &&
3250 nla_put_flag(skb, IFLA_VXLAN_GPE))
3251 goto nla_put_failure;
3252
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003253 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3254 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3255 goto nla_put_failure;
3256
stephen hemmingerd3428942012-10-01 12:32:35 +00003257 return 0;
3258
3259nla_put_failure:
3260 return -EMSGSIZE;
3261}
3262
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003263static struct net *vxlan_get_link_net(const struct net_device *dev)
3264{
3265 struct vxlan_dev *vxlan = netdev_priv(dev);
3266
3267 return vxlan->net;
3268}
3269
stephen hemmingerd3428942012-10-01 12:32:35 +00003270static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3271 .kind = "vxlan",
3272 .maxtype = IFLA_VXLAN_MAX,
3273 .policy = vxlan_policy,
3274 .priv_size = sizeof(struct vxlan_dev),
3275 .setup = vxlan_setup,
3276 .validate = vxlan_validate,
3277 .newlink = vxlan_newlink,
3278 .dellink = vxlan_dellink,
3279 .get_size = vxlan_get_size,
3280 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003281 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003282};
3283
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003284struct net_device *vxlan_dev_create(struct net *net, const char *name,
3285 u8 name_assign_type,
3286 struct vxlan_config *conf)
3287{
3288 struct nlattr *tb[IFLA_MAX + 1];
3289 struct net_device *dev;
3290 int err;
3291
3292 memset(&tb, 0, sizeof(tb));
3293
3294 dev = rtnl_create_link(net, name, name_assign_type,
3295 &vxlan_link_ops, tb);
3296 if (IS_ERR(dev))
3297 return dev;
3298
3299 err = vxlan_dev_configure(net, dev, conf);
3300 if (err < 0) {
3301 free_netdev(dev);
3302 return ERR_PTR(err);
3303 }
3304
3305 err = rtnl_configure_link(dev, NULL);
3306 if (err < 0) {
3307 LIST_HEAD(list_kill);
3308
3309 vxlan_dellink(dev, &list_kill);
3310 unregister_netdevice_many(&list_kill);
3311 return ERR_PTR(err);
3312 }
3313
3314 return dev;
3315}
3316EXPORT_SYMBOL_GPL(vxlan_dev_create);
3317
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003318static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3319 struct net_device *dev)
3320{
3321 struct vxlan_dev *vxlan, *next;
3322 LIST_HEAD(list_kill);
3323
3324 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3325 struct vxlan_rdst *dst = &vxlan->default_dst;
3326
3327 /* In case we created vxlan device with carrier
3328 * and we loose the carrier due to module unload
3329 * we also need to remove vxlan device. In other
3330 * cases, it's not necessary and remote_ifindex
3331 * is 0 here, so no matches.
3332 */
3333 if (dst->remote_ifindex == dev->ifindex)
3334 vxlan_dellink(vxlan->dev, &list_kill);
3335 }
3336
3337 unregister_netdevice_many(&list_kill);
3338}
3339
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003340static int vxlan_netdevice_event(struct notifier_block *unused,
3341 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003342{
3343 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003344 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003345
Daniel Borkmann783c1462014-01-22 21:07:53 +01003346 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003347 vxlan_handle_lowerdev_unregister(vn, dev);
Alexander Duyck7c46a642016-06-16 12:21:00 -07003348 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003349 vxlan_push_rx_ports(dev);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003350
3351 return NOTIFY_DONE;
3352}
3353
3354static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003355 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003356};
3357
stephen hemmingerd3428942012-10-01 12:32:35 +00003358static __net_init int vxlan_init_net(struct net *net)
3359{
3360 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003361 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003362
stephen hemminger553675f2013-05-16 11:35:20 +00003363 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003364 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003365
stephen hemminger553675f2013-05-16 11:35:20 +00003366 for (h = 0; h < PORT_HASH_SIZE; ++h)
3367 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003368
3369 return 0;
3370}
3371
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003372static void __net_exit vxlan_exit_net(struct net *net)
3373{
3374 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3375 struct vxlan_dev *vxlan, *next;
3376 struct net_device *dev, *aux;
3377 LIST_HEAD(list);
3378
3379 rtnl_lock();
3380 for_each_netdev_safe(net, dev, aux)
3381 if (dev->rtnl_link_ops == &vxlan_link_ops)
3382 unregister_netdevice_queue(dev, &list);
3383
3384 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3385 /* If vxlan->dev is in the same netns, it has already been added
3386 * to the list by the previous loop.
3387 */
Zhiqiang Liu5da98282019-03-16 17:02:54 +08003388 if (!net_eq(dev_net(vxlan->dev), net))
John W. Linville13c3ed62015-05-18 13:51:24 -04003389 unregister_netdevice_queue(vxlan->dev, &list);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003390 }
3391
3392 unregister_netdevice_many(&list);
3393 rtnl_unlock();
3394}
3395
stephen hemmingerd3428942012-10-01 12:32:35 +00003396static struct pernet_operations vxlan_net_ops = {
3397 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003398 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003399 .id = &vxlan_net_id,
3400 .size = sizeof(struct vxlan_net),
3401};
3402
3403static int __init vxlan_init_module(void)
3404{
3405 int rc;
3406
3407 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3408
Daniel Borkmann783c1462014-01-22 21:07:53 +01003409 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003410 if (rc)
3411 goto out1;
3412
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003413 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003414 if (rc)
3415 goto out2;
3416
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003417 rc = rtnl_link_register(&vxlan_link_ops);
3418 if (rc)
3419 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003420
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003421 return 0;
3422out3:
3423 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003424out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003425 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003426out1:
3427 return rc;
3428}
Cong Wang7332a132013-05-27 22:35:53 +00003429late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003430
3431static void __exit vxlan_cleanup_module(void)
3432{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003433 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003434 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003435 unregister_pernet_subsys(&vxlan_net_ops);
3436 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003437}
3438module_exit(vxlan_cleanup_module);
3439
3440MODULE_LICENSE("GPL");
3441MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003442MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003443MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003444MODULE_ALIAS_RTNL_LINK("vxlan");