blob: ebc98bb17a51088acecc1174b6ea3e39256f6bed [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
stephen hemmingerd3428942012-10-01 12:32:35 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000014#include <linux/module.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000017#include <linux/udp.h>
18#include <linux/igmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000019#include <linux/if_ether.h>
Yan Burman1b13c972013-01-29 23:43:07 +000020#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000021#include <net/arp.h>
22#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000023#include <net/ip.h>
24#include <net/icmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000025#include <net/rtnetlink.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000026#include <net/inet_ecn.h>
27#include <net/net_namespace.h>
28#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070029#include <net/vxlan.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080030
Cong Wange4c7ed42013-08-31 13:44:33 +080031#if IS_ENABLED(CONFIG_IPV6)
Cong Wange4c7ed42013-08-31 13:44:33 +080032#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080033#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080034#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000035
36#define VXLAN_VERSION "0.1"
37
stephen hemminger553675f2013-05-16 11:35:20 +000038#define PORT_HASH_BITS 8
39#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000040#define FDB_AGE_DEFAULT 300 /* 5 min */
41#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
42
stephen hemminger23c578b2013-04-27 11:31:53 +000043/* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070045 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000046 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070047static unsigned short vxlan_port __read_mostly = 8472;
48module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000049MODULE_PARM_DESC(udp_port, "Destination UDP port");
50
51static bool log_ecn_error = true;
52module_param(log_ecn_error, bool, 0644);
53MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
54
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030055static unsigned int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020056static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000057
Li RongQing7256eac2016-01-29 09:43:47 +080058static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike 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
stephen hemminger553675f2013-05-16 11:35:20 +000062/* per-network namespace private data for this module */
63struct vxlan_net {
64 struct list_head vxlan_list;
65 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070066 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000067};
68
stephen hemmingerd3428942012-10-01 12:32:35 +000069/* Forwarding table entry */
70struct vxlan_fdb {
71 struct hlist_node hlist; /* linked list of entries */
72 struct rcu_head rcu;
73 unsigned long updated; /* jiffies */
74 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070075 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020076 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000077 u16 state; /* see ndm_state */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -080078 __be32 vni;
David Stevensae884082013-04-19 00:36:26 +000079 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +000080};
81
stephen hemmingerd3428942012-10-01 12:32:35 +000082/* salt for hash table */
83static u32 vxlan_salt __read_mostly;
84
Thomas Grafee122c72015-07-21 10:43:58 +020085static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
86{
Thomas Grafe7030872015-07-21 10:44:01 +020087 return vs->flags & VXLAN_F_COLLECT_METADATA ||
88 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020089}
90
Cong Wange4c7ed42013-08-31 13:44:33 +080091#if IS_ENABLED(CONFIG_IPV6)
92static inline
93bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
94{
Jiri Bencf0ef3122015-03-29 16:17:37 +020095 if (a->sa.sa_family != b->sa.sa_family)
96 return false;
97 if (a->sa.sa_family == AF_INET6)
98 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
99 else
100 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800101}
102
103static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
104{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200105 if (ipa->sa.sa_family == AF_INET6)
106 return ipv6_addr_any(&ipa->sin6.sin6_addr);
107 else
108 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800109}
110
111static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
112{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200113 if (ipa->sa.sa_family == AF_INET6)
114 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
115 else
116 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800117}
118
119static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
120{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200121 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200122 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200123 ip->sa.sa_family = AF_INET6;
124 return 0;
125 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200126 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200127 ip->sa.sa_family = AF_INET;
128 return 0;
129 } else {
130 return -EAFNOSUPPORT;
131 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800132}
133
134static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200135 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800136{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200137 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200138 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200139 else
Jiri Benc930345e2015-03-29 16:59:25 +0200140 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800141}
142
143#else /* !CONFIG_IPV6 */
144
145static inline
146bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
147{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200148 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800149}
150
151static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
152{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200153 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800154}
155
156static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
157{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200158 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800159}
160
161static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
162{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200163 if (nla_len(nla) >= sizeof(struct in6_addr)) {
164 return -EAFNOSUPPORT;
165 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200166 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200167 ip->sa.sa_family = AF_INET;
168 return 0;
169 } else {
170 return -EAFNOSUPPORT;
171 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800172}
173
174static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200175 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800176{
Jiri Benc930345e2015-03-29 16:59:25 +0200177 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800178}
179#endif
180
stephen hemminger553675f2013-05-16 11:35:20 +0000181/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100182static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000183{
Jiri Benc54bfd872016-02-16 21:58:58 +0100184 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000185}
186
187/* Socket hash table head */
188static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000189{
190 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
191
stephen hemminger553675f2013-05-16 11:35:20 +0000192 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
193}
194
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700195/* First remote destination for a forwarding entry.
196 * Guaranteed to be non-NULL because remotes are never deleted.
197 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700198static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700199{
stephen hemminger5ca54612013-08-04 17:17:39 -0700200 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
201}
202
203static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
204{
205 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700206}
207
Thomas Grafac5132d2015-01-15 03:53:56 +0100208/* Find VXLAN socket based on network namespace, address family and UDP port
209 * and enabled unshareable flags.
210 */
211static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
212 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000213{
214 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800215
216 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000217
218 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200219 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200220 vxlan_get_sk_family(vs) == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800221 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000222 return vs;
223 }
224 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000225}
226
Jiri Benc54bfd872016-02-16 21:58:58 +0100227static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000228{
229 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000230
Jiri Bencb9167b22016-02-16 21:59:03 +0100231 /* For flow based devices, map all packets to VNI 0 */
232 if (vs->flags & VXLAN_F_COLLECT_METADATA)
233 vni = 0;
234
Jiri Benc54bfd872016-02-16 21:58:58 +0100235 hlist_for_each_entry_rcu(vxlan, vni_head(vs, vni), hlist) {
236 if (vxlan->default_dst.remote_vni == vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000237 return vxlan;
238 }
239
240 return NULL;
241}
242
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700243/* Look up VNI in a per net namespace table */
Jiri Benc54bfd872016-02-16 21:58:58 +0100244static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni,
Thomas Grafac5132d2015-01-15 03:53:56 +0100245 sa_family_t family, __be16 port,
246 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700247{
248 struct vxlan_sock *vs;
249
Thomas Grafac5132d2015-01-15 03:53:56 +0100250 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700251 if (!vs)
252 return NULL;
253
Jiri Benc54bfd872016-02-16 21:58:58 +0100254 return vxlan_vs_find_vni(vs, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700255}
256
stephen hemmingerd3428942012-10-01 12:32:35 +0000257/* Fill in neighbour message in skbuff. */
258static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700259 const struct vxlan_fdb *fdb,
260 u32 portid, u32 seq, int type, unsigned int flags,
261 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000262{
263 unsigned long now = jiffies;
264 struct nda_cacheinfo ci;
265 struct nlmsghdr *nlh;
266 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000267 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000268
269 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
270 if (nlh == NULL)
271 return -EMSGSIZE;
272
273 ndm = nlmsg_data(nlh);
274 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000275
276 send_eth = send_ip = true;
277
278 if (type == RTM_GETNEIGH) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800279 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000280 send_eth = !is_zero_ether_addr(fdb->eth_addr);
Vincent Bernat8f48ba72017-03-10 16:30:24 +0100281 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
David Stevense4f67ad2012-11-20 02:50:14 +0000282 } else
283 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000284 ndm->ndm_state = fdb->state;
285 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000286 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800287 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000288
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100289 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100290 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700291 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100292 goto nla_put_failure;
293
David Stevense4f67ad2012-11-20 02:50:14 +0000294 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000295 goto nla_put_failure;
296
Cong Wange4c7ed42013-08-31 13:44:33 +0800297 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000298 goto nla_put_failure;
299
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200300 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000301 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
302 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000303 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100304 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000305 goto nla_put_failure;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800306 if ((vxlan->flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
307 nla_put_u32(skb, NDA_SRC_VNI,
308 be32_to_cpu(fdb->vni)))
309 goto nla_put_failure;
David Stevens66817122013-03-15 04:35:51 +0000310 if (rdst->remote_ifindex &&
311 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000312 goto nla_put_failure;
313
314 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
315 ci.ndm_confirmed = 0;
316 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
317 ci.ndm_refcnt = 0;
318
319 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
320 goto nla_put_failure;
321
Johannes Berg053c0952015-01-16 22:09:00 +0100322 nlmsg_end(skb, nlh);
323 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000324
325nla_put_failure:
326 nlmsg_cancel(skb, nlh);
327 return -EMSGSIZE;
328}
329
330static inline size_t vxlan_nlmsg_size(void)
331{
332 return NLMSG_ALIGN(sizeof(struct ndmsg))
333 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800334 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000335 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000336 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
337 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100338 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000339 + nla_total_size(sizeof(struct nda_cacheinfo));
340}
341
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200342static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
343 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000344{
345 struct net *net = dev_net(vxlan->dev);
346 struct sk_buff *skb;
347 int err = -ENOBUFS;
348
349 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
350 if (skb == NULL)
351 goto errout;
352
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200353 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000354 if (err < 0) {
355 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
356 WARN_ON(err == -EMSGSIZE);
357 kfree_skb(skb);
358 goto errout;
359 }
360
361 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
362 return;
363errout:
364 if (err < 0)
365 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
366}
367
Cong Wange4c7ed42013-08-31 13:44:33 +0800368static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000369{
370 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700371 struct vxlan_fdb f = {
372 .state = NUD_STALE,
373 };
374 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800375 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100376 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700377 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700378
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200379 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000380}
381
382static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
383{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700384 struct vxlan_fdb f = {
385 .state = NUD_STALE,
386 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200387 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000388
David Stevense4f67ad2012-11-20 02:50:14 +0000389 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
390
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200391 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000392}
393
stephen hemmingerd3428942012-10-01 12:32:35 +0000394/* Hash Ethernet address */
395static u32 eth_hash(const unsigned char *addr)
396{
397 u64 value = get_unaligned((u64 *)addr);
398
399 /* only want 6 bytes */
400#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000401 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000402#else
403 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000404#endif
405 return hash_64(value, FDB_HASH_BITS);
406}
407
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800408static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
409{
410 /* use 1 byte of OUI and 3 bytes of NIC */
411 u32 key = get_unaligned((u32 *)(addr + 2));
412
413 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
414}
415
stephen hemmingerd3428942012-10-01 12:32:35 +0000416/* Hash chain to use given mac address */
417static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800418 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000419{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800420 if (vxlan->flags & VXLAN_F_COLLECT_METADATA)
421 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
422 else
423 return &vxlan->fdb_head[eth_hash(mac)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000424}
425
426/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000427static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800428 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000429{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800430 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000431 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000432
Sasha Levinb67bfe02013-02-27 17:06:00 -0800433 hlist_for_each_entry_rcu(f, head, hlist) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800434 if (ether_addr_equal(mac, f->eth_addr)) {
435 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
436 if (vni == f->vni)
437 return f;
438 } else {
439 return f;
440 }
441 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000442 }
443
444 return NULL;
445}
446
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000447static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800448 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000449{
450 struct vxlan_fdb *f;
451
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800452 f = __vxlan_find_mac(vxlan, mac, vni);
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000453 if (f)
454 f->used = jiffies;
455
456 return f;
457}
458
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300459/* caller should hold vxlan->hash_lock */
460static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800461 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100462 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300463{
464 struct vxlan_rdst *rd;
465
466 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800467 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300468 rd->remote_port == port &&
469 rd->remote_vni == vni &&
470 rd->remote_ifindex == ifindex)
471 return rd;
472 }
473
474 return NULL;
475}
476
Thomas Richter906dc182013-07-19 17:20:07 +0200477/* Replace destination of unicast mac */
478static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100479 union vxlan_addr *ip, __be16 port, __be32 vni,
480 __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200481{
482 struct vxlan_rdst *rd;
483
484 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
485 if (rd)
486 return 0;
487
488 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
489 if (!rd)
490 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100491
492 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800493 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200494 rd->remote_port = port;
495 rd->remote_vni = vni;
496 rd->remote_ifindex = ifindex;
497 return 1;
498}
499
David Stevens66817122013-03-15 04:35:51 +0000500/* Add/update destinations for multicast */
501static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100502 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200503 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000504{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700505 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000506
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300507 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
508 if (rd)
509 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700510
David Stevens66817122013-03-15 04:35:51 +0000511 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
512 if (rd == NULL)
513 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100514
515 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
516 kfree(rd);
517 return -ENOBUFS;
518 }
519
Cong Wange4c7ed42013-08-31 13:44:33 +0800520 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000521 rd->remote_port = port;
522 rd->remote_vni = vni;
523 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700524
525 list_add_tail_rcu(&rd->list, &f->remotes);
526
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200527 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000528 return 1;
529}
530
Tom Herbertdfd86452015-01-12 17:00:38 -0800531static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
532 unsigned int off,
533 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100534 __be32 vni_field,
535 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800536 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800537{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700538 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800539
540 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700541 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800542
543 if (!NAPI_GRO_CB(skb)->csum_valid)
544 return NULL;
545
Jiri Benc54bfd872016-02-16 21:58:58 +0100546 start = vxlan_rco_start(vni_field);
547 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800548
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700549 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
550 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800551
552 skb->remcsum_offload = 1;
553
554 return vh;
555}
556
Tom Herbert5602c482016-04-05 08:22:53 -0700557static struct sk_buff **vxlan_gro_receive(struct sock *sk,
558 struct sk_buff **head,
559 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200560{
561 struct sk_buff *p, **pp = NULL;
562 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800563 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200564 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700565 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100566 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800567 struct gro_remcsum grc;
568
569 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200570
571 off_vx = skb_gro_offset(skb);
572 hlen = off_vx + sizeof(*vh);
573 vh = skb_gro_header_fast(skb, off_vx);
574 if (skb_gro_header_hard(skb, hlen)) {
575 vh = skb_gro_header_slow(skb, hlen, off_vx);
576 if (unlikely(!vh))
577 goto out;
578 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200579
Tom Herbertdfd86452015-01-12 17:00:38 -0800580 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
581
Jiri Benc54bfd872016-02-16 21:58:58 +0100582 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800583
584 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
585 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100586 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800587 !!(vs->flags &
588 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800589
590 if (!vh)
591 goto out;
592 }
593
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700594 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
595
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200596 for (p = *head; p; p = p->next) {
597 if (!NAPI_GRO_CB(p)->same_flow)
598 continue;
599
600 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100601 if (vh->vx_flags != vh2->vx_flags ||
602 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200603 NAPI_GRO_CB(p)->same_flow = 0;
604 continue;
605 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200606 }
607
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200608 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800609 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200610
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200611out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800612 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200613 NAPI_GRO_CB(skb)->flush |= flush;
614
615 return pp;
616}
617
Tom Herbert5602c482016-04-05 08:22:53 -0700618static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200619{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700620 /* Sets 'skb->inner_mac_header' since we are always called with
621 * 'skb->encapsulation' set.
622 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800623 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200624}
625
stephen hemmingerd3428942012-10-01 12:32:35 +0000626/* Add new entry to forwarding table -- assumes lock held */
627static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800628 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000629 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800630 __be16 port, __be32 src_vni, __be32 vni,
631 __u32 ifindex, __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000632{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200633 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000634 struct vxlan_fdb *f;
635 int notify = 0;
Haishuang Yan17b46362016-11-29 09:59:36 +0800636 int rc;
stephen hemmingerd3428942012-10-01 12:32:35 +0000637
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800638 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000639 if (f) {
640 if (flags & NLM_F_EXCL) {
641 netdev_dbg(vxlan->dev,
642 "lost race to create %pM\n", mac);
643 return -EEXIST;
644 }
645 if (f->state != state) {
646 f->state = state;
647 f->updated = jiffies;
648 notify = 1;
649 }
David Stevensae884082013-04-19 00:36:26 +0000650 if (f->flags != ndm_flags) {
651 f->flags = ndm_flags;
652 f->updated = jiffies;
653 notify = 1;
654 }
Thomas Richter906dc182013-07-19 17:20:07 +0200655 if ((flags & NLM_F_REPLACE)) {
656 /* Only change unicasts */
657 if (!(is_multicast_ether_addr(f->eth_addr) ||
658 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800659 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200660 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200661 } else
662 return -EOPNOTSUPP;
663 }
David Stevens66817122013-03-15 04:35:51 +0000664 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300665 (is_multicast_ether_addr(f->eth_addr) ||
666 is_zero_ether_addr(f->eth_addr))) {
Haishuang Yan17b46362016-11-29 09:59:36 +0800667 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
David Stevens66817122013-03-15 04:35:51 +0000668
669 if (rc < 0)
670 return rc;
671 notify |= rc;
672 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000673 } else {
674 if (!(flags & NLM_F_CREATE))
675 return -ENOENT;
676
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200677 if (vxlan->cfg.addrmax &&
678 vxlan->addrcnt >= vxlan->cfg.addrmax)
stephen hemmingerd3428942012-10-01 12:32:35 +0000679 return -ENOSPC;
680
Thomas Richter906dc182013-07-19 17:20:07 +0200681 /* Disallow replace to add a multicast entry */
682 if ((flags & NLM_F_REPLACE) &&
683 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
684 return -EOPNOTSUPP;
685
Cong Wange4c7ed42013-08-31 13:44:33 +0800686 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000687 f = kmalloc(sizeof(*f), GFP_ATOMIC);
688 if (!f)
689 return -ENOMEM;
690
691 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000692 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000693 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000694 f->updated = f->used = jiffies;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800695 f->vni = src_vni;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700696 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000697 memcpy(f->eth_addr, mac, ETH_ALEN);
698
Haishuang Yan17b46362016-11-29 09:59:36 +0800699 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
700 if (rc < 0) {
701 kfree(f);
702 return rc;
703 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700704
stephen hemmingerd3428942012-10-01 12:32:35 +0000705 ++vxlan->addrcnt;
706 hlist_add_head_rcu(&f->hlist,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800707 vxlan_fdb_head(vxlan, mac, src_vni));
stephen hemmingerd3428942012-10-01 12:32:35 +0000708 }
709
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200710 if (notify) {
711 if (rd == NULL)
712 rd = first_remote_rtnl(f);
713 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
714 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000715
716 return 0;
717}
718
Wei Yongjun6706c822013-04-11 19:00:35 +0000719static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000720{
721 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700722 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000723
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100724 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
725 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000726 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100727 }
David Stevens66817122013-03-15 04:35:51 +0000728 kfree(f);
729}
730
stephen hemmingerd3428942012-10-01 12:32:35 +0000731static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
732{
733 netdev_dbg(vxlan->dev,
734 "delete %pM\n", f->eth_addr);
735
736 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200737 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000738
739 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000740 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000741}
742
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300743static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800744 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
745 __be32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300746{
747 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800748 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300749
750 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800751 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
752 if (err)
753 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300754 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800755 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
756 if (remote->sa.sa_family == AF_INET) {
757 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
758 ip->sa.sa_family = AF_INET;
759#if IS_ENABLED(CONFIG_IPV6)
760 } else {
761 ip->sin6.sin6_addr = in6addr_any;
762 ip->sa.sa_family = AF_INET6;
763#endif
764 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300765 }
766
767 if (tb[NDA_PORT]) {
768 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
769 return -EINVAL;
770 *port = nla_get_be16(tb[NDA_PORT]);
771 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200772 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300773 }
774
775 if (tb[NDA_VNI]) {
776 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
777 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100778 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300779 } else {
780 *vni = vxlan->default_dst.remote_vni;
781 }
782
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800783 if (tb[NDA_SRC_VNI]) {
784 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
785 return -EINVAL;
786 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
787 } else {
788 *src_vni = vxlan->default_dst.remote_vni;
789 }
790
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300791 if (tb[NDA_IFINDEX]) {
792 struct net_device *tdev;
793
794 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
795 return -EINVAL;
796 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800797 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300798 if (!tdev)
799 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300800 } else {
801 *ifindex = 0;
802 }
803
804 return 0;
805}
806
stephen hemmingerd3428942012-10-01 12:32:35 +0000807/* Add static entry (via netlink) */
808static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
809 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100810 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000811{
812 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300813 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800814 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000815 __be16 port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800816 __be32 src_vni, vni;
Jiri Benc54bfd872016-02-16 21:58:58 +0100817 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000818 int err;
819
820 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
821 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
822 ndm->ndm_state);
823 return -EINVAL;
824 }
825
826 if (tb[NDA_DST] == NULL)
827 return -EINVAL;
828
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800829 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300830 if (err)
831 return err;
David Stevens66817122013-03-15 04:35:51 +0000832
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300833 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
834 return -EAFNOSUPPORT;
835
stephen hemmingerd3428942012-10-01 12:32:35 +0000836 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800837 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800838 port, src_vni, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000839 spin_unlock_bh(&vxlan->hash_lock);
840
841 return err;
842}
843
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800844static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
845 const unsigned char *addr, union vxlan_addr ip,
846 __be16 port, __be32 src_vni, u32 vni, u32 ifindex,
847 u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000848{
stephen hemmingerd3428942012-10-01 12:32:35 +0000849 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300850 struct vxlan_rdst *rd = NULL;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800851 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300852
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800853 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300854 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800855 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300856
Cong Wange4c7ed42013-08-31 13:44:33 +0800857 if (!vxlan_addr_any(&ip)) {
858 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300859 if (!rd)
860 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000861 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300862
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300863 /* remove a destination if it's not the only one on the list,
864 * otherwise destroy the fdb entry
865 */
866 if (rd && !list_is_singular(&f->remotes)) {
867 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200868 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800869 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300870 goto out;
871 }
872
873 vxlan_fdb_destroy(vxlan, f);
874
875out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800876 return 0;
877}
878
879/* Delete entry (via netlink) */
880static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
881 struct net_device *dev,
882 const unsigned char *addr, u16 vid)
883{
884 struct vxlan_dev *vxlan = netdev_priv(dev);
885 union vxlan_addr ip;
886 __be32 src_vni, vni;
887 __be16 port;
888 u32 ifindex;
889 int err;
890
891 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
892 if (err)
893 return err;
894
895 spin_lock_bh(&vxlan->hash_lock);
896 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
897 vid);
stephen hemmingerd3428942012-10-01 12:32:35 +0000898 spin_unlock_bh(&vxlan->hash_lock);
899
900 return err;
901}
902
903/* Dump forwarding table */
904static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400905 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700906 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000907{
908 struct vxlan_dev *vxlan = netdev_priv(dev);
909 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -0700910 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000911
912 for (h = 0; h < FDB_HASH_SIZE; ++h) {
913 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000914
Sasha Levinb67bfe02013-02-27 17:06:00 -0800915 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000916 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000917
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700918 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -0700919 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900920 goto skip;
921
David Stevens66817122013-03-15 04:35:51 +0000922 err = vxlan_fdb_info(skb, vxlan, f,
923 NETLINK_CB(cb->skb).portid,
924 cb->nlh->nlmsg_seq,
925 RTM_NEWNEIGH,
926 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -0700927 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700928 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700929skip:
Roopa Prabhud2976532016-08-30 21:56:45 -0700930 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900931 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000932 }
933 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700934out:
Roopa Prabhud2976532016-08-30 21:56:45 -0700935 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +0000936}
937
938/* Watch incoming packets to learn mapping between Ethernet address
939 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900940 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000941 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700942static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800943 union vxlan_addr *src_ip, const u8 *src_mac,
944 __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000945{
946 struct vxlan_dev *vxlan = netdev_priv(dev);
947 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000948
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800949 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000950 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700951 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700952
Cong Wange4c7ed42013-08-31 13:44:33 +0800953 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700954 return false;
955
956 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700957 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700958 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000959
960 if (net_ratelimit())
961 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800962 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +0100963 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +0000964
Cong Wange4c7ed42013-08-31 13:44:33 +0800965 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000966 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200967 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000968 } else {
969 /* learned new entry */
970 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700971
972 /* close off race between vxlan_flush and incoming packets */
973 if (netif_running(dev))
974 vxlan_fdb_create(vxlan, src_mac, src_ip,
975 NUD_REACHABLE,
976 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200977 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800978 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -0700979 vxlan->default_dst.remote_vni,
980 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000981 spin_unlock(&vxlan->hash_lock);
982 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700983
984 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000985}
986
stephen hemmingerd3428942012-10-01 12:32:35 +0000987/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +0800988static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +0000989{
stephen hemminger553675f2013-05-16 11:35:20 +0000990 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -0700991 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +0100992#if IS_ENABLED(CONFIG_IPV6)
993 struct vxlan_sock *sock6;
994#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +0200995 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +0000996
pravin shelarc6fcc4f2016-10-28 09:59:15 -0700997 sock4 = rtnl_dereference(dev->vn4_sock);
998
Gao feng95ab0992013-12-10 16:37:33 +0800999 /* The vxlan_sock is only used by dev, leaving group has
1000 * no effect on other vxlan devices.
1001 */
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001002 if (family == AF_INET && sock4 && atomic_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001003 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001004#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001005 sock6 = rtnl_dereference(dev->vn6_sock);
1006 if (family == AF_INET6 && sock6 && atomic_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001007 return false;
1008#endif
Gao feng95ab0992013-12-10 16:37:33 +08001009
stephen hemminger553675f2013-05-16 11:35:20 +00001010 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001011 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001012 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001013
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001014 if (family == AF_INET &&
1015 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001016 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001017#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001018 if (family == AF_INET6 &&
1019 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001020 continue;
1021#endif
Gao feng95ab0992013-12-10 16:37:33 +08001022
1023 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1024 &dev->default_dst.remote_ip))
1025 continue;
1026
1027 if (vxlan->default_dst.remote_ifindex !=
1028 dev->default_dst.remote_ifindex)
1029 continue;
1030
1031 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001032 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001033
1034 return false;
1035}
1036
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001037static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001038{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001039 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001040
Jiri Bencb1be00a2015-09-24 13:50:02 +02001041 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001042 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001043 if (!atomic_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001044 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001045
Jiri Bencb1be00a2015-09-24 13:50:02 +02001046 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001047 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001048 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001049 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001050 (vs->flags & VXLAN_F_GPE) ?
1051 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001052 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001053 spin_unlock(&vn->sock_lock);
1054
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001055 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001056}
1057
Jiri Bencb1be00a2015-09-24 13:50:02 +02001058static void vxlan_sock_release(struct vxlan_dev *vxlan)
1059{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001060 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001061#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001062 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1063
1064 rcu_assign_pointer(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001065#endif
1066
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001067 rcu_assign_pointer(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001068 synchronize_net();
1069
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001070 if (__vxlan_sock_release_prep(sock4)) {
1071 udp_tunnel_sock_release(sock4->sock);
1072 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001073 }
1074
1075#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001076 if (__vxlan_sock_release_prep(sock6)) {
1077 udp_tunnel_sock_release(sock6->sock);
1078 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001079 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001080#endif
1081}
1082
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001083/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001084 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001085 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001086static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001087{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001088 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001089 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1090 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001091 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001092
Cong Wange4c7ed42013-08-31 13:44:33 +08001093 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001094 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001095 struct ip_mreqn mreq = {
1096 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1097 .imr_ifindex = ifindex,
1098 };
1099
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001100 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001101 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001102 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001103 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001104#if IS_ENABLED(CONFIG_IPV6)
1105 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001106 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1107
1108 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001109 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001110 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1111 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001112 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001113#endif
1114 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001115
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001116 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001117}
1118
1119/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001120static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001121{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001122 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001123 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1124 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001125 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001126
Cong Wange4c7ed42013-08-31 13:44:33 +08001127 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001128 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001129 struct ip_mreqn mreq = {
1130 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1131 .imr_ifindex = ifindex,
1132 };
1133
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001134 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001135 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001136 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001137 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001138#if IS_ENABLED(CONFIG_IPV6)
1139 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001140 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1141
1142 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001143 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001144 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1145 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001146 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001147#endif
1148 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001149
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001150 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001151}
1152
Jiri Bencf14eceb2016-02-16 21:59:01 +01001153static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1154 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001155{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001156 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001157
Jiri Bencf14eceb2016-02-16 21:59:01 +01001158 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1159 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001160
Jiri Bencf14eceb2016-02-16 21:59:01 +01001161 start = vxlan_rco_start(unparsed->vx_vni);
1162 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001163
Jiri Benc7d34fa72016-03-21 17:50:05 +01001164 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001165 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001166
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001167 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1168 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001169out:
1170 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1171 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001172 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001173}
1174
Jiri Bencf14eceb2016-02-16 21:59:01 +01001175static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001176 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001177 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001178{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001179 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001180 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001181
Jiri Bencf14eceb2016-02-16 21:59:01 +01001182 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1183 goto out;
1184
Jiri Benc3288af02016-02-16 21:59:00 +01001185 md->gbp = ntohs(gbp->policy_id);
1186
Jiri Benc10a5af22016-02-23 18:02:59 +01001187 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001188 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001189 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001190 tun_dst->u.tun_info.options_len = sizeof(*md);
1191 }
Jiri Benc3288af02016-02-16 21:59:00 +01001192 if (gbp->dont_learn)
1193 md->gbp |= VXLAN_GBP_DONT_LEARN;
1194
1195 if (gbp->policy_applied)
1196 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001197
Jiri Benc64f87d32016-02-23 18:02:55 +01001198 /* In flow-based mode, GBP is carried in dst_metadata */
1199 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1200 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001201out:
1202 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001203}
1204
Jiri Bence1e53142016-04-05 14:47:13 +02001205static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001206 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001207 struct sk_buff *skb, u32 vxflags)
1208{
1209 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1210
1211 /* Need to have Next Protocol set for interfaces in GPE mode. */
1212 if (!gpe->np_applied)
1213 return false;
1214 /* "The initial version is 0. If a receiver does not support the
1215 * version indicated it MUST drop the packet.
1216 */
1217 if (gpe->version != 0)
1218 return false;
1219 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1220 * processing MUST occur." However, we don't implement OAM
1221 * processing, thus drop the packet.
1222 */
1223 if (gpe->oam_flag)
1224 return false;
1225
1226 switch (gpe->next_protocol) {
1227 case VXLAN_GPE_NP_IPV4:
1228 *protocol = htons(ETH_P_IP);
1229 break;
1230 case VXLAN_GPE_NP_IPV6:
1231 *protocol = htons(ETH_P_IPV6);
1232 break;
1233 case VXLAN_GPE_NP_ETHERNET:
1234 *protocol = htons(ETH_P_TEB);
1235 break;
1236 default:
1237 return false;
1238 }
1239
1240 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1241 return true;
1242}
1243
Jiri Benc1ab016e2016-02-23 18:02:56 +01001244static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1245 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001246 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001247{
Thomas Graf614732e2015-07-21 10:44:06 +02001248 union vxlan_addr saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001249
Thomas Graf614732e2015-07-21 10:44:06 +02001250 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001251 skb->protocol = eth_type_trans(skb, vxlan->dev);
1252 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1253
1254 /* Ignore packet loops (and multicast echo) */
1255 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001256 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001257
Jiri Benc760c6802016-02-23 18:02:57 +01001258 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001259 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001260 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001261 saddr.sa.sa_family = AF_INET;
1262#if IS_ENABLED(CONFIG_IPV6)
1263 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001264 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001265 saddr.sa.sa_family = AF_INET6;
1266#endif
1267 }
1268
Jiri Benc1ab016e2016-02-23 18:02:56 +01001269 if ((vxlan->flags & VXLAN_F_LEARN) &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001270 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001271 return false;
1272
1273 return true;
1274}
1275
Jiri Benc760c6802016-02-23 18:02:57 +01001276static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1277 struct sk_buff *skb)
1278{
1279 int err = 0;
1280
1281 if (vxlan_get_sk_family(vs) == AF_INET)
1282 err = IP_ECN_decapsulate(oiph, skb);
1283#if IS_ENABLED(CONFIG_IPV6)
1284 else
1285 err = IP6_ECN_decapsulate(oiph, skb);
1286#endif
1287
1288 if (unlikely(err) && log_ecn_error) {
1289 if (vxlan_get_sk_family(vs) == AF_INET)
1290 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1291 &((struct iphdr *)oiph)->saddr,
1292 ((struct iphdr *)oiph)->tos);
1293 else
1294 net_info_ratelimited("non-ECT from %pI6\n",
1295 &((struct ipv6hdr *)oiph)->saddr);
1296 }
1297 return err <= 1;
1298}
1299
stephen hemmingerd3428942012-10-01 12:32:35 +00001300/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001301static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001302{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001303 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001304 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001305 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001306 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001307 struct vxlan_metadata _md;
1308 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001309 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001310 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001311 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001312 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001313
Jiri Bence1e53142016-04-05 14:47:13 +02001314 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001315 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001316 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001317
Jiri Bencf14eceb2016-02-16 21:59:01 +01001318 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001319 /* VNI flag always required to be set */
1320 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1321 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1322 ntohl(vxlan_hdr(skb)->vx_flags),
1323 ntohl(vxlan_hdr(skb)->vx_vni));
1324 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001325 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001326 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001327 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1328 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001329
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001330 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001331 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001332 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001333
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001334 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1335
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001336 vxlan = vxlan_vs_find_vni(vs, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001337 if (!vxlan)
1338 goto drop;
1339
Jiri Bence1e53142016-04-05 14:47:13 +02001340 /* For backwards compatibility, only allow reserved fields to be
1341 * used by VXLAN extensions if explicitly requested.
1342 */
1343 if (vs->flags & VXLAN_F_GPE) {
1344 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1345 goto drop;
1346 raw_proto = true;
1347 }
1348
1349 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1350 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1351 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001352
Thomas Grafee122c72015-07-21 10:43:58 +02001353 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001354 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001355
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001356 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001357 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001358
Thomas Grafee122c72015-07-21 10:43:58 +02001359 if (!tun_dst)
1360 goto drop;
1361
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001362 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001363
1364 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001365 } else {
1366 memset(md, 0, sizeof(*md));
1367 }
1368
Jiri Bencf14eceb2016-02-16 21:59:01 +01001369 if (vs->flags & VXLAN_F_REMCSUM_RX)
1370 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1371 goto drop;
1372 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001373 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001374 /* Note that GBP and GPE can never be active together. This is
1375 * ensured in vxlan_dev_configure.
1376 */
Thomas Graf35114942015-01-15 03:53:55 +01001377
Jiri Bencf14eceb2016-02-16 21:59:01 +01001378 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001379 /* If there are any unprocessed flags remaining treat
1380 * this as a malformed packet. This behavior diverges from
1381 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1382 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001383 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001384 * is more robust and provides a little more security in
1385 * adding extensions to VXLAN.
1386 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001387 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001388 }
1389
Jiri Bence1e53142016-04-05 14:47:13 +02001390 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001391 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001392 goto drop;
1393 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001394 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001395 skb->dev = vxlan->dev;
1396 skb->pkt_type = PACKET_HOST;
1397 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001398
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001399 oiph = skb_network_header(skb);
1400 skb_reset_network_header(skb);
1401
1402 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1403 ++vxlan->dev->stats.rx_frame_errors;
1404 ++vxlan->dev->stats.rx_errors;
1405 goto drop;
1406 }
1407
1408 stats = this_cpu_ptr(vxlan->dev->tstats);
1409 u64_stats_update_begin(&stats->syncp);
1410 stats->rx_packets++;
1411 stats->rx_bytes += skb->len;
1412 u64_stats_update_end(&stats->syncp);
1413
1414 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001415 return 0;
1416
1417drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001418 /* Consume bad packet */
1419 kfree_skb(skb);
1420 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001421}
1422
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001423static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001424{
1425 struct vxlan_dev *vxlan = netdev_priv(dev);
1426 struct arphdr *parp;
1427 u8 *arpptr, *sha;
1428 __be32 sip, tip;
1429 struct neighbour *n;
1430
1431 if (dev->flags & IFF_NOARP)
1432 goto out;
1433
1434 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1435 dev->stats.tx_dropped++;
1436 goto out;
1437 }
1438 parp = arp_hdr(skb);
1439
1440 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1441 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1442 parp->ar_pro != htons(ETH_P_IP) ||
1443 parp->ar_op != htons(ARPOP_REQUEST) ||
1444 parp->ar_hln != dev->addr_len ||
1445 parp->ar_pln != 4)
1446 goto out;
1447 arpptr = (u8 *)parp + sizeof(struct arphdr);
1448 sha = arpptr;
1449 arpptr += dev->addr_len; /* sha */
1450 memcpy(&sip, arpptr, sizeof(sip));
1451 arpptr += sizeof(sip);
1452 arpptr += dev->addr_len; /* tha */
1453 memcpy(&tip, arpptr, sizeof(tip));
1454
1455 if (ipv4_is_loopback(tip) ||
1456 ipv4_is_multicast(tip))
1457 goto out;
1458
1459 n = neigh_lookup(&arp_tbl, &tip, dev);
1460
1461 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001462 struct vxlan_fdb *f;
1463 struct sk_buff *reply;
1464
1465 if (!(n->nud_state & NUD_CONNECTED)) {
1466 neigh_release(n);
1467 goto out;
1468 }
1469
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001470 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08001471 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001472 /* bridge-local neighbor */
1473 neigh_release(n);
1474 goto out;
1475 }
1476
1477 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1478 n->ha, sha);
1479
1480 neigh_release(n);
1481
David Stevens73461352014-03-18 12:32:29 -04001482 if (reply == NULL)
1483 goto out;
1484
David Stevense4f67ad2012-11-20 02:50:14 +00001485 skb_reset_mac_header(reply);
1486 __skb_pull(reply, skb_network_offset(reply));
1487 reply->ip_summed = CHECKSUM_UNNECESSARY;
1488 reply->pkt_type = PACKET_HOST;
1489
1490 if (netif_rx_ni(reply) == NET_RX_DROP)
1491 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001492 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1493 union vxlan_addr ipa = {
1494 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001495 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001496 };
1497
1498 vxlan_ip_miss(dev, &ipa);
1499 }
David Stevense4f67ad2012-11-20 02:50:14 +00001500out:
1501 consume_skb(skb);
1502 return NETDEV_TX_OK;
1503}
1504
Cong Wangf564f452013-08-31 13:44:36 +08001505#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001506static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1507 struct neighbour *n, bool isrouter)
1508{
1509 struct net_device *dev = request->dev;
1510 struct sk_buff *reply;
1511 struct nd_msg *ns, *na;
1512 struct ipv6hdr *pip6;
1513 u8 *daddr;
1514 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1515 int ns_olen;
1516 int i, len;
1517
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001518 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04001519 return NULL;
1520
1521 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1522 sizeof(*na) + na_olen + dev->needed_tailroom;
1523 reply = alloc_skb(len, GFP_ATOMIC);
1524 if (reply == NULL)
1525 return NULL;
1526
1527 reply->protocol = htons(ETH_P_IPV6);
1528 reply->dev = dev;
1529 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1530 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001531 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001532
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001533 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04001534
1535 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001536 ns_olen = request->len - skb_network_offset(request) -
1537 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04001538 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1539 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1540 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1541 break;
1542 }
1543 }
1544
1545 /* Ethernet header */
1546 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1547 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1548 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1549 reply->protocol = htons(ETH_P_IPV6);
1550
1551 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001552 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001553 skb_put(reply, sizeof(struct ipv6hdr));
1554
1555 /* IPv6 header */
1556
1557 pip6 = ipv6_hdr(reply);
1558 memset(pip6, 0, sizeof(struct ipv6hdr));
1559 pip6->version = 6;
1560 pip6->priority = ipv6_hdr(request)->priority;
1561 pip6->nexthdr = IPPROTO_ICMPV6;
1562 pip6->hop_limit = 255;
1563 pip6->daddr = ipv6_hdr(request)->saddr;
1564 pip6->saddr = *(struct in6_addr *)n->primary_key;
1565
1566 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001567 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001568
1569 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1570
1571 /* Neighbor Advertisement */
1572 memset(na, 0, sizeof(*na)+na_olen);
1573 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1574 na->icmph.icmp6_router = isrouter;
1575 na->icmph.icmp6_override = 1;
1576 na->icmph.icmp6_solicited = 1;
1577 na->target = ns->target;
1578 ether_addr_copy(&na->opt[2], n->ha);
1579 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1580 na->opt[1] = na_olen >> 3;
1581
1582 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1583 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1584 csum_partial(na, sizeof(*na)+na_olen, 0));
1585
1586 pip6->payload_len = htons(sizeof(*na)+na_olen);
1587
1588 skb_push(reply, sizeof(struct ipv6hdr));
1589
1590 reply->ip_summed = CHECKSUM_UNNECESSARY;
1591
1592 return reply;
1593}
1594
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001595static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08001596{
1597 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001598 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001599 const struct ipv6hdr *iphdr;
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08001600 const struct in6_addr *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001601 struct neighbour *n;
1602 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001603
1604 in6_dev = __in6_dev_get(dev);
1605 if (!in6_dev)
1606 goto out;
1607
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001608 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
1609 goto out;
1610
Cong Wangf564f452013-08-31 13:44:36 +08001611 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001612 daddr = &iphdr->daddr;
1613
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001614 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08001615 if (msg->icmph.icmp6_code != 0 ||
1616 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1617 goto out;
1618
David Stevens4b29dba2014-03-24 10:39:58 -04001619 if (ipv6_addr_loopback(daddr) ||
1620 ipv6_addr_is_multicast(&msg->target))
1621 goto out;
1622
1623 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001624
1625 if (n) {
1626 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001627 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001628
1629 if (!(n->nud_state & NUD_CONNECTED)) {
1630 neigh_release(n);
1631 goto out;
1632 }
1633
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001634 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08001635 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1636 /* bridge-local neighbor */
1637 neigh_release(n);
1638 goto out;
1639 }
1640
David Stevens4b29dba2014-03-24 10:39:58 -04001641 reply = vxlan_na_create(skb, n,
1642 !!(f ? f->flags & NTF_ROUTER : 0));
1643
Cong Wangf564f452013-08-31 13:44:36 +08001644 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001645
1646 if (reply == NULL)
1647 goto out;
1648
1649 if (netif_rx_ni(reply) == NET_RX_DROP)
1650 dev->stats.rx_dropped++;
1651
Cong Wangf564f452013-08-31 13:44:36 +08001652 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001653 union vxlan_addr ipa = {
1654 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001655 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001656 };
1657
Cong Wangf564f452013-08-31 13:44:36 +08001658 vxlan_ip_miss(dev, &ipa);
1659 }
1660
1661out:
1662 consume_skb(skb);
1663 return NETDEV_TX_OK;
1664}
1665#endif
1666
David Stevense4f67ad2012-11-20 02:50:14 +00001667static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1668{
1669 struct vxlan_dev *vxlan = netdev_priv(dev);
1670 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001671
1672 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1673 return false;
1674
1675 n = NULL;
1676 switch (ntohs(eth_hdr(skb)->h_proto)) {
1677 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001678 {
1679 struct iphdr *pip;
1680
David Stevense4f67ad2012-11-20 02:50:14 +00001681 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1682 return false;
1683 pip = ip_hdr(skb);
1684 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001685 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1686 union vxlan_addr ipa = {
1687 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001688 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001689 };
1690
1691 vxlan_ip_miss(dev, &ipa);
1692 return false;
1693 }
1694
David Stevense4f67ad2012-11-20 02:50:14 +00001695 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001696 }
1697#if IS_ENABLED(CONFIG_IPV6)
1698 case ETH_P_IPV6:
1699 {
1700 struct ipv6hdr *pip6;
1701
1702 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1703 return false;
1704 pip6 = ipv6_hdr(skb);
1705 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1706 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1707 union vxlan_addr ipa = {
1708 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001709 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001710 };
1711
1712 vxlan_ip_miss(dev, &ipa);
1713 return false;
1714 }
1715
1716 break;
1717 }
1718#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001719 default:
1720 return false;
1721 }
1722
1723 if (n) {
1724 bool diff;
1725
Joe Perches7367d0b2013-09-01 11:51:23 -07001726 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001727 if (diff) {
1728 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1729 dev->addr_len);
1730 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1731 }
1732 neigh_release(n);
1733 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001734 }
1735
David Stevense4f67ad2012-11-20 02:50:14 +00001736 return false;
1737}
1738
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001739static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001740 struct vxlan_metadata *md)
1741{
1742 struct vxlanhdr_gbp *gbp;
1743
Thomas Grafdb79a622015-02-04 17:00:04 +01001744 if (!md->gbp)
1745 return;
1746
Thomas Graf35114942015-01-15 03:53:55 +01001747 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001748 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001749
1750 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1751 gbp->dont_learn = 1;
1752
1753 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1754 gbp->policy_applied = 1;
1755
1756 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1757}
1758
Jiri Bence1e53142016-04-05 14:47:13 +02001759static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1760 __be16 protocol)
1761{
1762 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1763
1764 gpe->np_applied = 1;
1765
1766 switch (protocol) {
1767 case htons(ETH_P_IP):
1768 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1769 return 0;
1770 case htons(ETH_P_IPV6):
1771 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1772 return 0;
1773 case htons(ETH_P_TEB):
1774 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1775 return 0;
1776 }
1777 return -EPFNOSUPPORT;
1778}
1779
Jiri Bencf491e562016-02-02 18:09:16 +01001780static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1781 int iphdr_len, __be32 vni,
1782 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001783 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001784{
Cong Wange4c7ed42013-08-31 13:44:33 +08001785 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001786 int min_headroom;
1787 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001788 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001789 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001790
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001791 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001792 skb->ip_summed == CHECKSUM_PARTIAL) {
1793 int csum_start = skb_checksum_start_offset(skb);
1794
1795 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1796 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1797 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001798 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001799 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001800 }
1801
Cong Wange4c7ed42013-08-31 13:44:33 +08001802 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08001803 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001804
1805 /* Need space for new headers (invalidates iph ptr) */
1806 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001807 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08001808 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001809
Alexander Duyckaed069d2016-04-14 15:33:37 -04001810 err = iptunnel_handle_offloads(skb, type);
1811 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08001812 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07001813
Pravin B Shelar49560532013-08-19 11:23:17 -07001814 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001815 vxh->vx_flags = VXLAN_HF_VNI;
1816 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001817
Tom Herbertdfd86452015-01-12 17:00:38 -08001818 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001819 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001820
Jiri Benc54bfd872016-02-16 21:58:58 +01001821 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1822 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1823 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001824
1825 if (!skb_is_gso(skb)) {
1826 skb->ip_summed = CHECKSUM_NONE;
1827 skb->encapsulation = 0;
1828 }
1829 }
1830
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001831 if (vxflags & VXLAN_F_GBP)
1832 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001833 if (vxflags & VXLAN_F_GPE) {
1834 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1835 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08001836 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02001837 inner_protocol = skb->protocol;
1838 }
Thomas Graf35114942015-01-15 03:53:55 +01001839
Jiri Bence1e53142016-04-05 14:47:13 +02001840 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001841 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07001842}
Pravin B Shelar49560532013-08-19 11:23:17 -07001843
pravin shelar655c3de2016-11-13 20:43:55 -08001844static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
1845 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01001846 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001847 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001848 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001849 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01001850{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001851 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01001852 struct rtable *rt = NULL;
1853 struct flowi4 fl4;
1854
pravin shelar655c3de2016-11-13 20:43:55 -08001855 if (!sock4)
1856 return ERR_PTR(-EIO);
1857
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001858 if (tos && !info)
1859 use_cache = false;
1860 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001861 rt = dst_cache_get_ip4(dst_cache, saddr);
1862 if (rt)
1863 return rt;
1864 }
1865
Jiri Benc1a8496b2016-02-02 18:09:14 +01001866 memset(&fl4, 0, sizeof(fl4));
1867 fl4.flowi4_oif = oif;
1868 fl4.flowi4_tos = RT_TOS(tos);
1869 fl4.flowi4_mark = skb->mark;
1870 fl4.flowi4_proto = IPPROTO_UDP;
1871 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001872 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001873 fl4.fl4_dport = dport;
1874 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01001875
1876 rt = ip_route_output_key(vxlan->net, &fl4);
pravin shelar655c3de2016-11-13 20:43:55 -08001877 if (likely(!IS_ERR(rt))) {
1878 if (rt->dst.dev == dev) {
1879 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
1880 ip_rt_put(rt);
1881 return ERR_PTR(-ELOOP);
1882 }
1883
Jiri Benc1a8496b2016-02-02 18:09:14 +01001884 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001885 if (use_cache)
1886 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08001887 } else {
1888 netdev_dbg(dev, "no route to %pI4\n", &daddr);
1889 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001890 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01001891 return rt;
1892}
1893
Jiri Bence5d4b292015-12-07 13:04:30 +01001894#if IS_ENABLED(CONFIG_IPV6)
1895static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08001896 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08001897 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01001898 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001899 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01001900 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001901 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001902 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001903 struct dst_cache *dst_cache,
1904 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01001905{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001906 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01001907 struct dst_entry *ndst;
1908 struct flowi6 fl6;
1909 int err;
1910
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001911 if (!sock6)
1912 return ERR_PTR(-EIO);
1913
Daniel Borkmann14006152016-03-04 15:15:08 +01001914 if (tos && !info)
1915 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001916 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001917 ndst = dst_cache_get_ip6(dst_cache, saddr);
1918 if (ndst)
1919 return ndst;
1920 }
1921
Jiri Bence5d4b292015-12-07 13:04:30 +01001922 memset(&fl6, 0, sizeof(fl6));
1923 fl6.flowi6_oif = oif;
1924 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001925 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01001926 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01001927 fl6.flowi6_mark = skb->mark;
1928 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001929 fl6.fl6_dport = dport;
1930 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01001931
1932 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001933 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01001934 &ndst, &fl6);
pravin shelar655c3de2016-11-13 20:43:55 -08001935 if (unlikely(err < 0)) {
1936 netdev_dbg(dev, "no route to %pI6\n", daddr);
1937 return ERR_PTR(-ENETUNREACH);
1938 }
1939
1940 if (unlikely(ndst->dev == dev)) {
1941 netdev_dbg(dev, "circular route to %pI6\n", daddr);
1942 dst_release(ndst);
1943 return ERR_PTR(-ELOOP);
1944 }
Jiri Bence5d4b292015-12-07 13:04:30 +01001945
1946 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001947 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001948 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01001949 return ndst;
1950}
1951#endif
1952
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001953/* Bypass encapsulation if the destination is local */
1954static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001955 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001956{
Li RongQing8f849852014-01-04 13:57:59 +08001957 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001958 union vxlan_addr loopback;
1959 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001960 struct net_device *dev = skb->dev;
1961 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001962
Li RongQing8f849852014-01-04 13:57:59 +08001963 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1964 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001965 skb->pkt_type = PACKET_HOST;
1966 skb->encapsulation = 0;
1967 skb->dev = dst_vxlan->dev;
1968 __skb_pull(skb, skb_network_offset(skb));
1969
Cong Wange4c7ed42013-08-31 13:44:33 +08001970 if (remote_ip->sa.sa_family == AF_INET) {
1971 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1972 loopback.sa.sa_family = AF_INET;
1973#if IS_ENABLED(CONFIG_IPV6)
1974 } else {
1975 loopback.sin6.sin6_addr = in6addr_loopback;
1976 loopback.sa.sa_family = AF_INET6;
1977#endif
1978 }
1979
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001980 if (dst_vxlan->flags & VXLAN_F_LEARN)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001981 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001982
1983 u64_stats_update_begin(&tx_stats->syncp);
1984 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001985 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001986 u64_stats_update_end(&tx_stats->syncp);
1987
1988 if (netif_rx(skb) == NET_RX_SUCCESS) {
1989 u64_stats_update_begin(&rx_stats->syncp);
1990 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001991 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001992 u64_stats_update_end(&rx_stats->syncp);
1993 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001994 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001995 }
1996}
1997
pravin shelarfee1fad2016-11-13 20:43:56 -08001998static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
1999 struct vxlan_dev *vxlan, union vxlan_addr *daddr,
Lance Richardson1f6cc072017-01-18 15:24:57 -05002000 __be16 dst_port, __be32 vni, struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002001 u32 rt_flags)
2002{
2003#if IS_ENABLED(CONFIG_IPV6)
2004 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2005 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2006 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2007 */
2008 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2009#endif
2010 /* Bypass encapsulation if the destination is local */
2011 if (rt_flags & RTCF_LOCAL &&
2012 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2013 struct vxlan_dev *dst_vxlan;
2014
2015 dst_release(dst);
2016 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2017 daddr->sa.sa_family, dst_port,
2018 vxlan->flags);
2019 if (!dst_vxlan) {
2020 dev->stats.tx_errors++;
2021 kfree_skb(skb);
2022
2023 return -ENOENT;
2024 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002025 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002026 return 1;
2027 }
2028
2029 return 0;
2030}
2031
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002032static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002033 __be32 default_vni, struct vxlan_rdst *rdst,
2034 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002035{
Paolo Abenid71785f2016-02-12 15:43:57 +01002036 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002037 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002038 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002039 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002040 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002041 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002042 struct vxlan_metadata _md;
2043 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002044 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002045 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002046 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002047 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07002048 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02002049 u32 flags = vxlan->flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002050 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002051 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002052
Jiri Benc61adedf2015-08-20 13:56:25 +02002053 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002054
Thomas Grafee122c72015-07-21 10:43:58 +02002055 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002056 dst = &rdst->remote_ip;
2057 if (vxlan_addr_any(dst)) {
2058 if (did_rsc) {
2059 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002060 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002061 return;
2062 }
2063 goto drop;
2064 }
2065
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002066 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002067 vni = (rdst->remote_vni) ? : default_vni;
Brian Russell11586322017-02-24 17:47:11 +00002068 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002069 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002070 md->gbp = skb->mark;
2071 ttl = vxlan->cfg.ttl;
2072 if (!ttl && vxlan_addr_multicast(dst))
2073 ttl = 1;
2074
2075 tos = vxlan->cfg.tos;
2076 if (tos == 1)
2077 tos = ip_tunnel_get_dsfield(old_iph, skb);
2078
2079 if (dst->sa.sa_family == AF_INET)
2080 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2081 else
2082 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2083 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002084 } else {
2085 if (!info) {
2086 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2087 dev->name);
2088 goto drop;
2089 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002090 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002091 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002092 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002093 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2094 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002095 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002096 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2097 }
Thomas Grafee122c72015-07-21 10:43:58 +02002098 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002099 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2100 vni = tunnel_id_to_key32(info->key.tun_id);
Paolo Abenid71785f2016-02-12 15:43:57 +01002101 dst_cache = &info->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002102 if (info->options_len)
2103 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002104 ttl = info->key.ttl;
2105 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002106 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002107 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002108 }
pravin shelar0770b532016-11-13 20:43:57 -08002109 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2110 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002111
Jakub Kicinski56de8592017-02-24 11:43:36 -08002112 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002113 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002114 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002115 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002116 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002117
pravin shelar655c3de2016-11-13 20:43:55 -08002118 rt = vxlan_get_route(vxlan, dev, sock4, skb,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002119 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002120 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002121 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002122 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002123 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002124 if (IS_ERR(rt)) {
2125 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002126 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002127 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002128
Cong Wange4c7ed42013-08-31 13:44:33 +08002129 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002130 if (!info) {
2131 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2132 dst_port, vni, &rt->dst,
2133 rt->rt_flags);
2134 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002135 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002136 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002137 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002138 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002139
pravin shelarc46b7892016-11-13 20:43:54 -08002140 ndst = &rt->dst;
Cong Wange4c7ed42013-08-31 13:44:33 +08002141 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2142 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002143 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002144 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002145 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002146 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002147
Brian Russell11586322017-02-24 17:47:11 +00002148 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002149 dst->sin.sin_addr.s_addr, tos, ttl, df,
2150 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002151#if IS_ENABLED(CONFIG_IPV6)
2152 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002153 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002154
pravin shelar655c3de2016-11-13 20:43:55 -08002155 ndst = vxlan6_get_route(vxlan, dev, sock6, skb,
Daniel Borkmann14006152016-03-04 15:15:08 +01002156 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002157 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002158 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002159 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002160 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002161 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002162 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002163 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002164 goto tx_error;
2165 }
pravin shelar655c3de2016-11-13 20:43:55 -08002166
pravin shelarfee1fad2016-11-13 20:43:56 -08002167 if (!info) {
2168 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002169
pravin shelarfee1fad2016-11-13 20:43:56 -08002170 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2171 dst_port, vni, ndst,
2172 rt6i_flags);
2173 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002174 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002175 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002176
Daniel Borkmann14006152016-03-04 15:15:08 +01002177 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002178 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002179 skb_scrub_packet(skb, xnet);
2180 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002181 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002182 if (err < 0)
2183 goto tx_error;
2184
pravin shelar0770b532016-11-13 20:43:57 -08002185 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002186 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002187 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002188 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002189#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002190 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002191out_unlock:
2192 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002193 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002194
2195drop:
2196 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002197 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002198 return;
2199
2200tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002201 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002202 if (err == -ELOOP)
2203 dev->stats.collisions++;
2204 else if (err == -ENETUNREACH)
2205 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002206 dst_release(ndst);
2207 dev->stats.tx_errors++;
2208 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002209}
2210
David Stevens66817122013-03-15 04:35:51 +00002211/* Transmit local packets over Vxlan
2212 *
2213 * Outer IP header inherits ECN and DF from inner header.
2214 * Outer UDP destination is the VXLAN assigned port.
2215 * source port is based on hash of flow
2216 */
2217static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2218{
2219 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002220 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002221 struct ethhdr *eth;
2222 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002223 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002224 struct vxlan_fdb *f;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002225 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002226
Jiri Benc61adedf2015-08-20 13:56:25 +02002227 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002228
David Stevens66817122013-03-15 04:35:51 +00002229 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002230
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002231 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002232 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2233 info->mode & IP_TUNNEL_INFO_TX) {
2234 vni = tunnel_id_to_key32(info->key.tun_id);
2235 } else {
2236 if (info && info->mode & IP_TUNNEL_INFO_TX)
2237 vxlan_xmit_one(skb, dev, vni, NULL, false);
2238 else
2239 kfree_skb(skb);
2240 return NETDEV_TX_OK;
2241 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002242 }
2243
2244 if (vxlan->flags & VXLAN_F_PROXY) {
2245 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002246 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002247 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002248#if IS_ENABLED(CONFIG_IPV6)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002249 else if (ntohs(eth->h_proto) == ETH_P_IPV6) {
2250 struct ipv6hdr *hdr, _hdr;
2251 if ((hdr = skb_header_pointer(skb,
2252 skb_network_offset(skb),
2253 sizeof(_hdr), &_hdr)) &&
2254 hdr->nexthdr == IPPROTO_ICMPV6)
2255 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002256 }
2257#endif
2258 }
David Stevens66817122013-03-15 04:35:51 +00002259
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002260 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002261 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002262 did_rsc = false;
2263
2264 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002265 (ntohs(eth->h_proto) == ETH_P_IP ||
2266 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002267 did_rsc = route_shortcircuit(dev, skb);
2268 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002269 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002270 }
2271
David Stevens66817122013-03-15 04:35:51 +00002272 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002273 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002274 if (f == NULL) {
2275 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2276 !is_multicast_ether_addr(eth->h_dest))
2277 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002278
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002279 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002280 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002281 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002282 }
David Stevens66817122013-03-15 04:35:51 +00002283 }
2284
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002285 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2286 struct sk_buff *skb1;
2287
Eric Dumazet8f646c92014-01-06 09:54:31 -08002288 if (!fdst) {
2289 fdst = rdst;
2290 continue;
2291 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002292 skb1 = skb_clone(skb, GFP_ATOMIC);
2293 if (skb1)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002294 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002295 }
2296
Eric Dumazet8f646c92014-01-06 09:54:31 -08002297 if (fdst)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002298 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002299 else
2300 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002301 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002302}
2303
stephen hemmingerd3428942012-10-01 12:32:35 +00002304/* Walk the forwarding table and purge stale entries */
2305static void vxlan_cleanup(unsigned long arg)
2306{
2307 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2308 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2309 unsigned int h;
2310
2311 if (!netif_running(vxlan->dev))
2312 return;
2313
stephen hemmingerd3428942012-10-01 12:32:35 +00002314 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2315 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002316
2317 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002318 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2319 struct vxlan_fdb *f
2320 = container_of(p, struct vxlan_fdb, hlist);
2321 unsigned long timeout;
2322
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002323 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002324 continue;
2325
Roopa Prabhudef499c2017-03-27 15:46:41 -07002326 if (f->flags & NTF_EXT_LEARNED)
2327 continue;
2328
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002329 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002330 if (time_before_eq(timeout, jiffies)) {
2331 netdev_dbg(vxlan->dev,
2332 "garbage collect %pM\n",
2333 f->eth_addr);
2334 f->state = NUD_STALE;
2335 vxlan_fdb_destroy(vxlan, f);
2336 } else if (time_before(timeout, next_timer))
2337 next_timer = timeout;
2338 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002339 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002340 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002341
2342 mod_timer(&vxlan->age_timer, next_timer);
2343}
2344
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002345static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2346{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002347 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002348 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002349
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002350 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002351 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002352 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002353}
2354
stephen hemmingerd3428942012-10-01 12:32:35 +00002355/* Setup stats when device is created */
2356static int vxlan_init(struct net_device *dev)
2357{
WANG Cong1c213bd2014-02-13 11:46:28 -08002358 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002359 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002360 return -ENOMEM;
2361
2362 return 0;
2363}
2364
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002365static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002366{
2367 struct vxlan_fdb *f;
2368
2369 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002370 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002371 if (f)
2372 vxlan_fdb_destroy(vxlan, f);
2373 spin_unlock_bh(&vxlan->hash_lock);
2374}
2375
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002376static void vxlan_uninit(struct net_device *dev)
2377{
2378 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002379
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002380 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002381
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002382 free_percpu(dev->tstats);
2383}
2384
stephen hemmingerd3428942012-10-01 12:32:35 +00002385/* Start ageing timer and join group when device is brought up */
2386static int vxlan_open(struct net_device *dev)
2387{
2388 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002389 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002390
Jiri Benc205f3562015-09-24 13:50:01 +02002391 ret = vxlan_sock_add(vxlan);
2392 if (ret < 0)
2393 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002394
Gao feng79d4a942013-12-10 16:37:32 +08002395 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002396 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002397 if (ret == -EADDRINUSE)
2398 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002399 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002400 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002401 return ret;
2402 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002403 }
2404
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002405 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002406 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2407
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002408 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002409}
2410
2411/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002412static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00002413{
Cong Wang31fec5a2013-05-27 22:35:52 +00002414 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002415
2416 spin_lock_bh(&vxlan->hash_lock);
2417 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2418 struct hlist_node *p, *n;
2419 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2420 struct vxlan_fdb *f
2421 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002422 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2423 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002424 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2425 if (!is_zero_ether_addr(f->eth_addr))
2426 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002427 }
2428 }
2429 spin_unlock_bh(&vxlan->hash_lock);
2430}
2431
2432/* Cleanup timer and forwarding table on shutdown */
2433static int vxlan_stop(struct net_device *dev)
2434{
2435 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002436 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002437 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002438
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002439 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002440 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002441 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002442
2443 del_timer_sync(&vxlan->age_timer);
2444
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002445 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02002446 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002447
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002448 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002449}
2450
stephen hemmingerd3428942012-10-01 12:32:35 +00002451/* Stub, nothing needs to be done. */
2452static void vxlan_set_multicast_list(struct net_device *dev)
2453{
2454}
2455
Daniel Borkmann345010b2013-12-18 00:21:08 +01002456static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2457{
2458 struct vxlan_dev *vxlan = netdev_priv(dev);
2459 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002460 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2461 dst->remote_ifindex);
Jarod Wilson91572082016-10-20 13:55:20 -04002462 bool use_ipv6 = false;
2463
2464 if (dst->remote_ip.sa.sa_family == AF_INET6)
2465 use_ipv6 = true;
2466
2467 /* This check is different than dev->max_mtu, because it looks at
2468 * the lowerdev->mtu, rather than the static dev->max_mtu
2469 */
2470 if (lowerdev) {
2471 int max_mtu = lowerdev->mtu -
2472 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2473 if (new_mtu > max_mtu)
2474 return -EINVAL;
2475 }
2476
2477 dev->mtu = new_mtu;
2478 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01002479}
2480
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002481static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2482{
2483 struct vxlan_dev *vxlan = netdev_priv(dev);
2484 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2485 __be16 sport, dport;
2486
2487 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2488 vxlan->cfg.port_max, true);
2489 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2490
Jiri Benc239e9442015-12-07 13:04:31 +01002491 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002492 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002493 struct rtable *rt;
2494
pravin shelar655c3de2016-11-13 20:43:55 -08002495 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002496 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002497 &info->key.u.ipv4.src, dport, sport,
2498 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002499 if (IS_ERR(rt))
2500 return PTR_ERR(rt);
2501 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002502 } else {
2503#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08002504 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01002505 struct dst_entry *ndst;
2506
pravin shelar655c3de2016-11-13 20:43:55 -08002507 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002508 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002509 &info->key.u.ipv6.src, dport, sport,
2510 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002511 if (IS_ERR(ndst))
2512 return PTR_ERR(ndst);
2513 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002514#else /* !CONFIG_IPV6 */
2515 return -EPFNOSUPPORT;
2516#endif
2517 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002518 info->key.tp_src = sport;
2519 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002520 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002521}
2522
Jiri Benc0c867c92016-04-05 14:47:10 +02002523static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002524 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002525 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002526 .ndo_open = vxlan_open,
2527 .ndo_stop = vxlan_stop,
2528 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002529 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002530 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002531 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002532 .ndo_validate_addr = eth_validate_addr,
2533 .ndo_set_mac_address = eth_mac_addr,
2534 .ndo_fdb_add = vxlan_fdb_add,
2535 .ndo_fdb_del = vxlan_fdb_delete,
2536 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002537 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002538};
2539
Jiri Bence1e53142016-04-05 14:47:13 +02002540static const struct net_device_ops vxlan_netdev_raw_ops = {
2541 .ndo_init = vxlan_init,
2542 .ndo_uninit = vxlan_uninit,
2543 .ndo_open = vxlan_open,
2544 .ndo_stop = vxlan_stop,
2545 .ndo_start_xmit = vxlan_xmit,
2546 .ndo_get_stats64 = ip_tunnel_get_stats64,
2547 .ndo_change_mtu = vxlan_change_mtu,
2548 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2549};
2550
stephen hemmingerd3428942012-10-01 12:32:35 +00002551/* Info for udev, that this is a virtual tunnel endpoint */
2552static struct device_type vxlan_type = {
2553 .name = "vxlan",
2554};
2555
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002556/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002557 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002558 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002559 */
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02002560static void vxlan_push_rx_ports(struct net_device *dev)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002561{
2562 struct vxlan_sock *vs;
2563 struct net *net = dev_net(dev);
2564 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002565 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002566
2567 spin_lock(&vn->sock_lock);
2568 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Alexander Duycke7b3db52016-06-16 12:20:52 -07002569 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2570 udp_tunnel_push_rx_port(dev, vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002571 (vs->flags & VXLAN_F_GPE) ?
2572 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002573 UDP_TUNNEL_TYPE_VXLAN);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002574 }
2575 spin_unlock(&vn->sock_lock);
2576}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002577
stephen hemmingerd3428942012-10-01 12:32:35 +00002578/* Initialize the device structure. */
2579static void vxlan_setup(struct net_device *dev)
2580{
2581 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002582 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002583
Jiri Benc65226ef2016-04-28 16:36:30 +02002584 eth_hw_addr_random(dev);
2585 ether_setup(dev);
2586
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002587 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002588 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2589
stephen hemmingerd3428942012-10-01 12:32:35 +00002590 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002591 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002592 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002593 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002594
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002595 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002596 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002597 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07002598 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002599 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002600
stephen hemminger553675f2013-05-16 11:35:20 +00002601 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002602 spin_lock_init(&vxlan->hash_lock);
2603
2604 init_timer_deferrable(&vxlan->age_timer);
2605 vxlan->age_timer.function = vxlan_cleanup;
2606 vxlan->age_timer.data = (unsigned long) vxlan;
2607
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002608 vxlan->cfg.dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002609
stephen hemmingerd3428942012-10-01 12:32:35 +00002610 vxlan->dev = dev;
2611
Tom Herbert58ce31c2015-08-19 17:07:33 -07002612 gro_cells_init(&vxlan->gro_cells, dev);
2613
stephen hemmingerd3428942012-10-01 12:32:35 +00002614 for (h = 0; h < FDB_HASH_SIZE; ++h)
2615 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2616}
2617
Jiri Benc0c867c92016-04-05 14:47:10 +02002618static void vxlan_ether_setup(struct net_device *dev)
2619{
Jiri Benc0c867c92016-04-05 14:47:10 +02002620 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2621 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2622 dev->netdev_ops = &vxlan_netdev_ether_ops;
2623}
2624
Jiri Bence1e53142016-04-05 14:47:13 +02002625static void vxlan_raw_setup(struct net_device *dev)
2626{
Jiri Benc65226ef2016-04-28 16:36:30 +02002627 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002628 dev->type = ARPHRD_NONE;
2629 dev->hard_header_len = 0;
2630 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002631 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2632 dev->netdev_ops = &vxlan_netdev_raw_ops;
2633}
2634
stephen hemmingerd3428942012-10-01 12:32:35 +00002635static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2636 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002637 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002638 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002639 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2640 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002641 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002642 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2643 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002644 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002645 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2646 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2647 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002648 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002649 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2650 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2651 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2652 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002653 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002654 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002655 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2656 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2657 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002658 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2659 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002660 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002661 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002662 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002663};
2664
2665static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2666{
2667 if (tb[IFLA_ADDRESS]) {
2668 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2669 pr_debug("invalid link address (not ethernet)\n");
2670 return -EINVAL;
2671 }
2672
2673 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2674 pr_debug("invalid all zero ethernet address\n");
2675 return -EADDRNOTAVAIL;
2676 }
2677 }
2678
2679 if (!data)
2680 return -EINVAL;
2681
2682 if (data[IFLA_VXLAN_ID]) {
2683 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
Matthias Schiffer4e37d692017-02-23 17:19:41 +01002684 if (id >= VXLAN_N_VID)
stephen hemmingerd3428942012-10-01 12:32:35 +00002685 return -ERANGE;
2686 }
2687
stephen hemminger05f47d62012-10-09 20:35:50 +00002688 if (data[IFLA_VXLAN_PORT_RANGE]) {
2689 const struct ifla_vxlan_port_range *p
2690 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2691
2692 if (ntohs(p->high) < ntohs(p->low)) {
2693 pr_debug("port range %u .. %u not valid\n",
2694 ntohs(p->low), ntohs(p->high));
2695 return -EINVAL;
2696 }
2697 }
2698
stephen hemmingerd3428942012-10-01 12:32:35 +00002699 return 0;
2700}
2701
Yan Burman1b13c972013-01-29 23:43:07 +00002702static void vxlan_get_drvinfo(struct net_device *netdev,
2703 struct ethtool_drvinfo *drvinfo)
2704{
2705 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2706 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2707}
2708
2709static const struct ethtool_ops vxlan_ethtool_ops = {
2710 .get_drvinfo = vxlan_get_drvinfo,
2711 .get_link = ethtool_op_get_link,
2712};
2713
Tom Herbert3ee64f32014-07-13 19:49:42 -07002714static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2715 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002716{
Cong Wange4c7ed42013-08-31 13:44:33 +08002717 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002718 struct udp_port_cfg udp_conf;
2719 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002720
Tom Herbert3ee64f32014-07-13 19:49:42 -07002721 memset(&udp_conf, 0, sizeof(udp_conf));
2722
2723 if (ipv6) {
2724 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002725 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002726 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002727 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002728 } else {
2729 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002730 }
2731
Tom Herbert3ee64f32014-07-13 19:49:42 -07002732 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002733
Tom Herbert3ee64f32014-07-13 19:49:42 -07002734 /* Open UDP socket */
2735 err = udp_sock_create(net, &udp_conf, &sock);
2736 if (err < 0)
2737 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002738
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002739 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002740}
2741
2742/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002743static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2744 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002745{
2746 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2747 struct vxlan_sock *vs;
2748 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002749 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002750 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002751
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002752 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002753 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002754 return ERR_PTR(-ENOMEM);
2755
2756 for (h = 0; h < VNI_HASH_SIZE; ++h)
2757 INIT_HLIST_HEAD(&vs->vni_list[h]);
2758
Tom Herbert3ee64f32014-07-13 19:49:42 -07002759 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002760 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002761 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2762 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002763 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002764 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002765 }
2766
Cong Wange4c7ed42013-08-31 13:44:33 +08002767 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002768 atomic_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002769 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002770
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002771 spin_lock(&vn->sock_lock);
2772 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07002773 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002774 (vs->flags & VXLAN_F_GPE) ?
2775 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002776 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002777 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002778
2779 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002780 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002781 tunnel_cfg.sk_user_data = vs;
2782 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002783 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002784 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002785 tunnel_cfg.gro_receive = vxlan_gro_receive;
2786 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002787
2788 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002789
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002790 return vs;
2791}
stephen hemminger553675f2013-05-16 11:35:20 +00002792
Jiri Bencb1be00a2015-09-24 13:50:02 +02002793static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002794{
Jiri Benc205f3562015-09-24 13:50:01 +02002795 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2796 struct vxlan_sock *vs = NULL;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002797
Jiri Benc205f3562015-09-24 13:50:01 +02002798 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002799 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002800 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2801 vxlan->cfg.dst_port, vxlan->flags);
2802 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002803 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002804 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002805 }
2806 spin_unlock(&vn->sock_lock);
2807 }
Jiri Benc205f3562015-09-24 13:50:01 +02002808 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002809 vs = vxlan_socket_create(vxlan->net, ipv6,
2810 vxlan->cfg.dst_port, vxlan->flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002811 if (IS_ERR(vs))
2812 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002813#if IS_ENABLED(CONFIG_IPV6)
2814 if (ipv6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002815 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002816 else
2817#endif
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002818 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc205f3562015-09-24 13:50:01 +02002819 vxlan_vs_add_dev(vs, vxlan);
2820 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002821}
2822
Jiri Bencb1be00a2015-09-24 13:50:02 +02002823static int vxlan_sock_add(struct vxlan_dev *vxlan)
2824{
2825 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2826 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
2827 int ret = 0;
2828
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002829 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002830#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002831 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002832 if (ipv6 || metadata)
2833 ret = __vxlan_sock_add(vxlan, true);
2834#endif
2835 if (!ret && (!ipv6 || metadata))
2836 ret = __vxlan_sock_add(vxlan, false);
2837 if (ret < 0)
2838 vxlan_sock_release(vxlan);
2839 return ret;
2840}
2841
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002842static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08002843 struct vxlan_config *conf,
2844 bool changelink)
stephen hemmingerd3428942012-10-01 12:32:35 +00002845{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002846 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002847 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
Atzm Watanabec7995c42013-04-16 02:50:52 +00002848 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002849 unsigned short needed_headroom = ETH_HLEN;
Cong Wange4c7ed42013-08-31 13:44:33 +08002850 bool use_ipv6 = false;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002851 __be16 default_port = vxlan->cfg.dst_port;
David Wragg7e059152016-02-10 00:05:58 +00002852 struct net_device *lowerdev = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00002853
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08002854 if (!changelink) {
2855 if (conf->flags & VXLAN_F_GPE) {
2856 /* For now, allow GPE only together with
2857 * COLLECT_METADATA. This can be relaxed later; in such
2858 * case, the other side of the PtP link will have to be
2859 * provided.
2860 */
2861 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2862 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2863 pr_info("unsupported combination of extensions\n");
2864 return -EINVAL;
2865 }
2866 vxlan_raw_setup(dev);
2867 } else {
2868 vxlan_ether_setup(dev);
Jiri Benc35556212016-09-02 13:37:12 +02002869 }
Jiri Bence1e53142016-04-05 14:47:13 +02002870
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08002871 /* MTU range: 68 - 65535 */
2872 dev->min_mtu = ETH_MIN_MTU;
2873 dev->max_mtu = ETH_MAX_MTU;
2874 vxlan->net = src_net;
Jiri Bence1e53142016-04-05 14:47:13 +02002875 }
Jiri Benc0c867c92016-04-05 14:47:10 +02002876
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002877 dst->remote_vni = conf->vni;
2878
2879 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00002880
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002881 /* Unless IPv6 is explicitly requested, assume IPv4 */
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002882 if (!dst->remote_ip.sa.sa_family)
2883 dst->remote_ip.sa.sa_family = AF_INET;
stephen hemmingerd3428942012-10-01 12:32:35 +00002884
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002885 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
Jiri Benc057ba292015-09-17 16:11:11 +02002886 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2887 if (!IS_ENABLED(CONFIG_IPV6))
2888 return -EPFNOSUPPORT;
Cong Wange4c7ed42013-08-31 13:44:33 +08002889 use_ipv6 = true;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002890 vxlan->flags |= VXLAN_F_IPV6;
Jiri Benc057ba292015-09-17 16:11:11 +02002891 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002892
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002893 if (conf->label && !use_ipv6) {
2894 pr_info("label only supported in use with IPv6\n");
2895 return -EINVAL;
2896 }
2897
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08002898 if (conf->remote_ifindex &&
2899 conf->remote_ifindex != vxlan->cfg.remote_ifindex) {
David Wragg7e059152016-02-10 00:05:58 +00002900 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002901 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00002902
stephen hemminger34e02aa2012-10-09 20:35:53 +00002903 if (!lowerdev) {
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08002904 pr_info("ifindex %d does not exist\n",
2905 dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002906 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002907 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002908
Cong Wange4c7ed42013-08-31 13:44:33 +08002909#if IS_ENABLED(CONFIG_IPV6)
2910 if (use_ipv6) {
2911 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2912 if (idev && idev->cnf.disable_ipv6) {
2913 pr_info("IPv6 is disabled via sysctl\n");
2914 return -EPERM;
2915 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002916 }
2917#endif
2918
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002919 if (!conf->mtu)
Jarod Wilson91572082016-10-20 13:55:20 -04002920 dev->mtu = lowerdev->mtu -
2921 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002922
Jiri Bencb1be00a2015-09-24 13:50:02 +02002923 needed_headroom = lowerdev->hard_header_len;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08002924 } else if (!conf->remote_ifindex &&
2925 vxlan_addr_multicast(&dst->remote_ip)) {
Jiri Benc9b4cdd52016-09-02 13:37:11 +02002926 pr_info("multicast destination requires interface to be specified\n");
2927 return -EINVAL;
Jiri Benc9dc2ad12015-09-17 16:11:10 +02002928 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002929
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07002930 if (lowerdev) {
2931 dev->gso_max_size = lowerdev->gso_max_size;
2932 dev->gso_max_segs = lowerdev->gso_max_segs;
2933 }
2934
David Wragg7e059152016-02-10 00:05:58 +00002935 if (conf->mtu) {
Jarod Wilson91572082016-10-20 13:55:20 -04002936 int max_mtu = ETH_MAX_MTU;
2937
2938 if (lowerdev)
2939 max_mtu = lowerdev->mtu;
2940
2941 max_mtu -= (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2942
2943 if (conf->mtu < dev->min_mtu || conf->mtu > dev->max_mtu)
2944 return -EINVAL;
2945
2946 dev->mtu = conf->mtu;
2947
2948 if (conf->mtu > max_mtu)
2949 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00002950 }
2951
Jiri Bencb1be00a2015-09-24 13:50:02 +02002952 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2953 needed_headroom += VXLAN6_HEADROOM;
2954 else
2955 needed_headroom += VXLAN_HEADROOM;
2956 dev->needed_headroom = needed_headroom;
2957
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002958 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Jiri Bence1e53142016-04-05 14:47:13 +02002959 if (!vxlan->cfg.dst_port) {
2960 if (conf->flags & VXLAN_F_GPE)
Lance Richardsond5ff72d2017-01-16 18:37:58 -05002961 vxlan->cfg.dst_port = htons(4790); /* IANA VXLAN-GPE port */
Jiri Bence1e53142016-04-05 14:47:13 +02002962 else
2963 vxlan->cfg.dst_port = default_port;
2964 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002965 vxlan->flags |= conf->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00002966
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002967 if (!vxlan->cfg.age_interval)
2968 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
Vincent Bernatafb97182012-10-30 10:27:16 +00002969
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08002970 if (changelink)
2971 return 0;
2972
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002973 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2974 if (tmp->cfg.vni == conf->vni &&
2975 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2976 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2977 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2978 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
Jiri Benc35556212016-09-02 13:37:12 +02002979 (vxlan->flags & VXLAN_F_RCV_FLAGS)) {
2980 pr_info("duplicate VNI %u\n", be32_to_cpu(conf->vni));
2981 return -EEXIST;
2982 }
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002983 }
stephen hemminger553675f2013-05-16 11:35:20 +00002984
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08002985 return 0;
2986}
2987
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01002988static int __vxlan_dev_create(struct net *net, struct net_device *dev,
2989 struct vxlan_config *conf)
2990{
2991 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2992 struct vxlan_dev *vxlan = netdev_priv(dev);
2993 int err;
2994
2995 err = vxlan_dev_configure(net, dev, conf, false);
2996 if (err)
2997 return err;
2998
2999 dev->ethtool_ops = &vxlan_ethtool_ops;
3000
3001 /* create an fdb entry for a valid default destination */
3002 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
3003 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3004 &vxlan->default_dst.remote_ip,
3005 NUD_REACHABLE | NUD_PERMANENT,
3006 NLM_F_EXCL | NLM_F_CREATE,
3007 vxlan->cfg.dst_port,
3008 vxlan->default_dst.remote_vni,
3009 vxlan->default_dst.remote_vni,
3010 vxlan->default_dst.remote_ifindex,
3011 NTF_SELF);
3012 if (err)
3013 return err;
3014 }
3015
3016 err = register_netdevice(dev);
3017 if (err) {
3018 vxlan_fdb_delete_default(vxlan, vxlan->default_dst.remote_vni);
3019 return err;
3020 }
3021
3022 list_add(&vxlan->next, &vn->vxlan_list);
3023 return 0;
3024}
3025
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003026static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3027 struct net_device *dev, struct vxlan_config *conf,
3028 bool changelink)
3029{
3030 struct vxlan_dev *vxlan = netdev_priv(dev);
3031
3032 memset(conf, 0, sizeof(*conf));
3033
3034 /* if changelink operation, start with old existing cfg */
3035 if (changelink)
3036 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3037
3038 if (data[IFLA_VXLAN_ID]) {
3039 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3040
3041 if (changelink && (vni != conf->vni))
3042 return -EOPNOTSUPP;
3043 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3044 }
3045
3046 if (data[IFLA_VXLAN_GROUP]) {
3047 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3048 } else if (data[IFLA_VXLAN_GROUP6]) {
3049 if (!IS_ENABLED(CONFIG_IPV6))
3050 return -EPFNOSUPPORT;
3051
3052 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3053 conf->remote_ip.sa.sa_family = AF_INET6;
3054 }
3055
3056 if (data[IFLA_VXLAN_LOCAL]) {
3057 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3058 conf->saddr.sa.sa_family = AF_INET;
3059 } else if (data[IFLA_VXLAN_LOCAL6]) {
3060 if (!IS_ENABLED(CONFIG_IPV6))
3061 return -EPFNOSUPPORT;
3062
3063 /* TODO: respect scope id */
3064 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3065 conf->saddr.sa.sa_family = AF_INET6;
3066 }
3067
3068 if (data[IFLA_VXLAN_LINK])
3069 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3070
3071 if (data[IFLA_VXLAN_TOS])
3072 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3073
3074 if (data[IFLA_VXLAN_TTL])
3075 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3076
3077 if (data[IFLA_VXLAN_LABEL])
3078 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3079 IPV6_FLOWLABEL_MASK;
3080
3081 if (data[IFLA_VXLAN_LEARNING]) {
3082 if (nla_get_u8(data[IFLA_VXLAN_LEARNING])) {
3083 conf->flags |= VXLAN_F_LEARN;
3084 } else {
3085 conf->flags &= ~VXLAN_F_LEARN;
3086 vxlan->flags &= ~VXLAN_F_LEARN;
3087 }
3088 } else if (!changelink) {
3089 /* default to learn on a new device */
3090 conf->flags |= VXLAN_F_LEARN;
3091 }
3092
3093 if (data[IFLA_VXLAN_AGEING]) {
3094 if (changelink)
3095 return -EOPNOTSUPP;
3096 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3097 }
3098
3099 if (data[IFLA_VXLAN_PROXY]) {
3100 if (changelink)
3101 return -EOPNOTSUPP;
3102 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3103 conf->flags |= VXLAN_F_PROXY;
3104 }
3105
3106 if (data[IFLA_VXLAN_RSC]) {
3107 if (changelink)
3108 return -EOPNOTSUPP;
3109 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3110 conf->flags |= VXLAN_F_RSC;
3111 }
3112
3113 if (data[IFLA_VXLAN_L2MISS]) {
3114 if (changelink)
3115 return -EOPNOTSUPP;
3116 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3117 conf->flags |= VXLAN_F_L2MISS;
3118 }
3119
3120 if (data[IFLA_VXLAN_L3MISS]) {
3121 if (changelink)
3122 return -EOPNOTSUPP;
3123 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3124 conf->flags |= VXLAN_F_L3MISS;
3125 }
3126
3127 if (data[IFLA_VXLAN_LIMIT]) {
3128 if (changelink)
3129 return -EOPNOTSUPP;
3130 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3131 }
3132
3133 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3134 if (changelink)
3135 return -EOPNOTSUPP;
3136 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3137 conf->flags |= VXLAN_F_COLLECT_METADATA;
3138 }
3139
3140 if (data[IFLA_VXLAN_PORT_RANGE]) {
3141 if (!changelink) {
3142 const struct ifla_vxlan_port_range *p
3143 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3144 conf->port_min = ntohs(p->low);
3145 conf->port_max = ntohs(p->high);
3146 } else {
3147 return -EOPNOTSUPP;
3148 }
3149 }
3150
3151 if (data[IFLA_VXLAN_PORT]) {
3152 if (changelink)
3153 return -EOPNOTSUPP;
3154 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3155 }
3156
3157 if (data[IFLA_VXLAN_UDP_CSUM]) {
3158 if (changelink)
3159 return -EOPNOTSUPP;
3160 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3161 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3162 }
3163
3164 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3165 if (changelink)
3166 return -EOPNOTSUPP;
3167 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3168 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3169 }
3170
3171 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3172 if (changelink)
3173 return -EOPNOTSUPP;
3174 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3175 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3176 }
3177
3178 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3179 if (changelink)
3180 return -EOPNOTSUPP;
3181 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3182 conf->flags |= VXLAN_F_REMCSUM_TX;
3183 }
3184
3185 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3186 if (changelink)
3187 return -EOPNOTSUPP;
3188 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3189 conf->flags |= VXLAN_F_REMCSUM_RX;
3190 }
3191
3192 if (data[IFLA_VXLAN_GBP]) {
3193 if (changelink)
3194 return -EOPNOTSUPP;
3195 conf->flags |= VXLAN_F_GBP;
3196 }
3197
3198 if (data[IFLA_VXLAN_GPE]) {
3199 if (changelink)
3200 return -EOPNOTSUPP;
3201 conf->flags |= VXLAN_F_GPE;
3202 }
3203
3204 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3205 if (changelink)
3206 return -EOPNOTSUPP;
3207 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3208 }
3209
3210 if (tb[IFLA_MTU]) {
3211 if (changelink)
3212 return -EOPNOTSUPP;
3213 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3214 }
3215
3216 return 0;
3217}
3218
3219static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3220 struct nlattr *tb[], struct nlattr *data[])
3221{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003222 struct vxlan_config conf;
3223 int err;
3224
3225 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3226 if (err)
3227 return err;
3228
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003229 return __vxlan_dev_create(src_net, dev, &conf);
stephen hemmingerd3428942012-10-01 12:32:35 +00003230}
3231
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003232static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
3233 struct nlattr *data[])
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003234{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003235 struct vxlan_dev *vxlan = netdev_priv(dev);
3236 struct vxlan_rdst *dst = &vxlan->default_dst;
3237 struct vxlan_rdst old_dst;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003238 struct vxlan_config conf;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003239 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003240
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003241 err = vxlan_nl2conf(tb, data,
3242 dev, &conf, true);
3243 if (err)
3244 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07003245
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003246 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003247
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003248 err = vxlan_dev_configure(vxlan->net, dev, &conf, true);
3249 if (err)
3250 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003251
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003252 /* handle default dst entry */
3253 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3254 spin_lock_bh(&vxlan->hash_lock);
3255 if (!vxlan_addr_any(&old_dst.remote_ip))
3256 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3257 old_dst.remote_ip,
3258 vxlan->cfg.dst_port,
3259 old_dst.remote_vni,
3260 old_dst.remote_vni,
3261 old_dst.remote_ifindex, 0);
3262
3263 if (!vxlan_addr_any(&dst->remote_ip)) {
3264 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3265 &dst->remote_ip,
3266 NUD_REACHABLE | NUD_PERMANENT,
3267 NLM_F_CREATE | NLM_F_APPEND,
3268 vxlan->cfg.dst_port,
3269 dst->remote_vni,
3270 dst->remote_vni,
3271 dst->remote_ifindex,
3272 NTF_SELF);
3273 if (err) {
3274 spin_unlock_bh(&vxlan->hash_lock);
3275 return err;
3276 }
3277 }
3278 spin_unlock_bh(&vxlan->hash_lock);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003279 }
3280
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003281 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003282}
3283
stephen hemmingerd3428942012-10-01 12:32:35 +00003284static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3285{
3286 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003287 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00003288
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003289 vxlan_flush(vxlan, true);
3290
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003291 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003292 if (!hlist_unhashed(&vxlan->hlist))
3293 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003294 spin_unlock(&vn->sock_lock);
3295
Tom Herbert58ce31c2015-08-19 17:07:33 -07003296 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003297 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003298 unregister_netdevice_queue(dev, head);
3299}
3300
3301static size_t vxlan_get_size(const struct net_device *dev)
3302{
3303
3304 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003305 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003306 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003307 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003308 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3309 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003310 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003311 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003312 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3313 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3314 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3315 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003316 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003317 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3318 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003319 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003320 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3321 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3322 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3323 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003324 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3325 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003326 0;
3327}
3328
3329static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3330{
3331 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003332 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003333 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003334 .low = htons(vxlan->cfg.port_min),
3335 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003336 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003337
Jiri Benc54bfd872016-02-16 21:58:58 +01003338 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003339 goto nla_put_failure;
3340
Cong Wange4c7ed42013-08-31 13:44:33 +08003341 if (!vxlan_addr_any(&dst->remote_ip)) {
3342 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003343 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3344 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003345 goto nla_put_failure;
3346#if IS_ENABLED(CONFIG_IPV6)
3347 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003348 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3349 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003350 goto nla_put_failure;
3351#endif
3352 }
3353 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003354
Atzm Watanabec7995c42013-04-16 02:50:52 +00003355 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003356 goto nla_put_failure;
3357
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003358 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3359 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003360 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003361 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003362 goto nla_put_failure;
3363#if IS_ENABLED(CONFIG_IPV6)
3364 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003365 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003366 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003367 goto nla_put_failure;
3368#endif
3369 }
3370 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003371
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003372 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3373 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003374 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003375 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3376 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3377 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3378 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3379 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3380 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3381 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3382 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3383 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003384 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3385 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003386 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3387 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3388 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003389 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003390 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003391 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3392 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3393 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08003394 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3395 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3396 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3397 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3398 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003399 goto nla_put_failure;
3400
stephen hemminger05f47d62012-10-09 20:35:50 +00003401 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3402 goto nla_put_failure;
3403
Thomas Graf35114942015-01-15 03:53:55 +01003404 if (vxlan->flags & VXLAN_F_GBP &&
3405 nla_put_flag(skb, IFLA_VXLAN_GBP))
3406 goto nla_put_failure;
3407
Jiri Bence1e53142016-04-05 14:47:13 +02003408 if (vxlan->flags & VXLAN_F_GPE &&
3409 nla_put_flag(skb, IFLA_VXLAN_GPE))
3410 goto nla_put_failure;
3411
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003412 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3413 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3414 goto nla_put_failure;
3415
stephen hemmingerd3428942012-10-01 12:32:35 +00003416 return 0;
3417
3418nla_put_failure:
3419 return -EMSGSIZE;
3420}
3421
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003422static struct net *vxlan_get_link_net(const struct net_device *dev)
3423{
3424 struct vxlan_dev *vxlan = netdev_priv(dev);
3425
3426 return vxlan->net;
3427}
3428
stephen hemmingerd3428942012-10-01 12:32:35 +00003429static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3430 .kind = "vxlan",
3431 .maxtype = IFLA_VXLAN_MAX,
3432 .policy = vxlan_policy,
3433 .priv_size = sizeof(struct vxlan_dev),
3434 .setup = vxlan_setup,
3435 .validate = vxlan_validate,
3436 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003437 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00003438 .dellink = vxlan_dellink,
3439 .get_size = vxlan_get_size,
3440 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003441 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003442};
3443
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003444struct net_device *vxlan_dev_create(struct net *net, const char *name,
3445 u8 name_assign_type,
3446 struct vxlan_config *conf)
3447{
3448 struct nlattr *tb[IFLA_MAX + 1];
3449 struct net_device *dev;
3450 int err;
3451
3452 memset(&tb, 0, sizeof(tb));
3453
3454 dev = rtnl_create_link(net, name, name_assign_type,
3455 &vxlan_link_ops, tb);
3456 if (IS_ERR(dev))
3457 return dev;
3458
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003459 err = __vxlan_dev_create(net, dev, conf);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003460 if (err < 0) {
3461 free_netdev(dev);
3462 return ERR_PTR(err);
3463 }
3464
3465 err = rtnl_configure_link(dev, NULL);
3466 if (err < 0) {
3467 LIST_HEAD(list_kill);
3468
3469 vxlan_dellink(dev, &list_kill);
3470 unregister_netdevice_many(&list_kill);
3471 return ERR_PTR(err);
3472 }
3473
3474 return dev;
3475}
3476EXPORT_SYMBOL_GPL(vxlan_dev_create);
3477
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003478static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3479 struct net_device *dev)
3480{
3481 struct vxlan_dev *vxlan, *next;
3482 LIST_HEAD(list_kill);
3483
3484 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3485 struct vxlan_rdst *dst = &vxlan->default_dst;
3486
3487 /* In case we created vxlan device with carrier
3488 * and we loose the carrier due to module unload
3489 * we also need to remove vxlan device. In other
3490 * cases, it's not necessary and remote_ifindex
3491 * is 0 here, so no matches.
3492 */
3493 if (dst->remote_ifindex == dev->ifindex)
3494 vxlan_dellink(vxlan->dev, &list_kill);
3495 }
3496
3497 unregister_netdevice_many(&list_kill);
3498}
3499
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003500static int vxlan_netdevice_event(struct notifier_block *unused,
3501 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003502{
3503 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003504 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003505
Daniel Borkmann783c1462014-01-22 21:07:53 +01003506 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003507 vxlan_handle_lowerdev_unregister(vn, dev);
Alexander Duyck7c46a642016-06-16 12:21:00 -07003508 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003509 vxlan_push_rx_ports(dev);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003510
3511 return NOTIFY_DONE;
3512}
3513
3514static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003515 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003516};
3517
stephen hemmingerd3428942012-10-01 12:32:35 +00003518static __net_init int vxlan_init_net(struct net *net)
3519{
3520 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003521 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003522
stephen hemminger553675f2013-05-16 11:35:20 +00003523 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003524 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003525
stephen hemminger553675f2013-05-16 11:35:20 +00003526 for (h = 0; h < PORT_HASH_SIZE; ++h)
3527 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003528
3529 return 0;
3530}
3531
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003532static void __net_exit vxlan_exit_net(struct net *net)
3533{
3534 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3535 struct vxlan_dev *vxlan, *next;
3536 struct net_device *dev, *aux;
3537 LIST_HEAD(list);
3538
3539 rtnl_lock();
3540 for_each_netdev_safe(net, dev, aux)
3541 if (dev->rtnl_link_ops == &vxlan_link_ops)
3542 unregister_netdevice_queue(dev, &list);
3543
3544 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3545 /* If vxlan->dev is in the same netns, it has already been added
3546 * to the list by the previous loop.
3547 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003548 if (!net_eq(dev_net(vxlan->dev), net)) {
3549 gro_cells_destroy(&vxlan->gro_cells);
John W. Linville13c3ed62015-05-18 13:51:24 -04003550 unregister_netdevice_queue(vxlan->dev, &list);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003551 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003552 }
3553
3554 unregister_netdevice_many(&list);
3555 rtnl_unlock();
3556}
3557
stephen hemmingerd3428942012-10-01 12:32:35 +00003558static struct pernet_operations vxlan_net_ops = {
3559 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003560 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003561 .id = &vxlan_net_id,
3562 .size = sizeof(struct vxlan_net),
3563};
3564
3565static int __init vxlan_init_module(void)
3566{
3567 int rc;
3568
3569 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3570
Daniel Borkmann783c1462014-01-22 21:07:53 +01003571 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003572 if (rc)
3573 goto out1;
3574
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003575 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003576 if (rc)
3577 goto out2;
3578
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003579 rc = rtnl_link_register(&vxlan_link_ops);
3580 if (rc)
3581 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003582
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003583 return 0;
3584out3:
3585 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003586out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003587 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003588out1:
3589 return rc;
3590}
Cong Wang7332a132013-05-27 22:35:53 +00003591late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003592
3593static void __exit vxlan_cleanup_module(void)
3594{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003595 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003596 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003597 unregister_pernet_subsys(&vxlan_net_ops);
3598 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003599}
3600module_exit(vxlan_cleanup_module);
3601
3602MODULE_LICENSE("GPL");
3603MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003604MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003605MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003606MODULE_ALIAS_RTNL_LINK("vxlan");