blob: 31aeec9671758ef91bc771315417437010f7e19c [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>
Or Gerlitzdc01e7d2014-01-20 13:59:21 +020030#include <net/protocol.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080031
Cong Wange4c7ed42013-08-31 13:44:33 +080032#if IS_ENABLED(CONFIG_IPV6)
Cong Wange4c7ed42013-08-31 13:44:33 +080033#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080034#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080035#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000036
37#define VXLAN_VERSION "0.1"
38
stephen hemminger553675f2013-05-16 11:35:20 +000039#define PORT_HASH_BITS 8
40#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000041#define FDB_AGE_DEFAULT 300 /* 5 min */
42#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
43
stephen hemminger23c578b2013-04-27 11:31:53 +000044/* UDP port for VXLAN traffic.
45 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070046 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000047 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070048static unsigned short vxlan_port __read_mostly = 8472;
49module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000050MODULE_PARM_DESC(udp_port, "Destination UDP port");
51
52static bool log_ecn_error = true;
53module_param(log_ecn_error, bool, 0644);
54MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
55
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070056static int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020057static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000058
Li RongQing7256eac2016-01-29 09:43:47 +080059static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030060
Jiri Benc205f3562015-09-24 13:50:01 +020061static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020062
stephen hemminger553675f2013-05-16 11:35:20 +000063/* per-network namespace private data for this module */
64struct vxlan_net {
65 struct list_head vxlan_list;
66 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070067 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000068};
69
stephen hemmingerd3428942012-10-01 12:32:35 +000070/* Forwarding table entry */
71struct vxlan_fdb {
72 struct hlist_node hlist; /* linked list of entries */
73 struct rcu_head rcu;
74 unsigned long updated; /* jiffies */
75 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070076 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020077 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000078 u16 state; /* see ndm_state */
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) {
279 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800280 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000281 send_eth = !is_zero_ether_addr(fdb->eth_addr);
282 } 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,
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +0200291 peernet2id_alloc(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;
306 if (rdst->remote_ifindex &&
307 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000308 goto nla_put_failure;
309
310 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
311 ci.ndm_confirmed = 0;
312 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
313 ci.ndm_refcnt = 0;
314
315 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
316 goto nla_put_failure;
317
Johannes Berg053c0952015-01-16 22:09:00 +0100318 nlmsg_end(skb, nlh);
319 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000320
321nla_put_failure:
322 nlmsg_cancel(skb, nlh);
323 return -EMSGSIZE;
324}
325
326static inline size_t vxlan_nlmsg_size(void)
327{
328 return NLMSG_ALIGN(sizeof(struct ndmsg))
329 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800330 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000331 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000332 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
333 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100334 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000335 + nla_total_size(sizeof(struct nda_cacheinfo));
336}
337
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200338static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
339 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000340{
341 struct net *net = dev_net(vxlan->dev);
342 struct sk_buff *skb;
343 int err = -ENOBUFS;
344
345 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
346 if (skb == NULL)
347 goto errout;
348
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200349 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000350 if (err < 0) {
351 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
352 WARN_ON(err == -EMSGSIZE);
353 kfree_skb(skb);
354 goto errout;
355 }
356
357 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
358 return;
359errout:
360 if (err < 0)
361 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
362}
363
Cong Wange4c7ed42013-08-31 13:44:33 +0800364static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000365{
366 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700367 struct vxlan_fdb f = {
368 .state = NUD_STALE,
369 };
370 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800371 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100372 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700373 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700374
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200375 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000376}
377
378static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
379{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700380 struct vxlan_fdb f = {
381 .state = NUD_STALE,
382 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200383 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000384
David Stevense4f67ad2012-11-20 02:50:14 +0000385 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
386
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200387 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000388}
389
stephen hemmingerd3428942012-10-01 12:32:35 +0000390/* Hash Ethernet address */
391static u32 eth_hash(const unsigned char *addr)
392{
393 u64 value = get_unaligned((u64 *)addr);
394
395 /* only want 6 bytes */
396#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000397 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000398#else
399 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000400#endif
401 return hash_64(value, FDB_HASH_BITS);
402}
403
404/* Hash chain to use given mac address */
405static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
406 const u8 *mac)
407{
408 return &vxlan->fdb_head[eth_hash(mac)];
409}
410
411/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000412static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000413 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000414{
415 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
416 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000417
Sasha Levinb67bfe02013-02-27 17:06:00 -0800418 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700419 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000420 return f;
421 }
422
423 return NULL;
424}
425
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000426static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
427 const u8 *mac)
428{
429 struct vxlan_fdb *f;
430
431 f = __vxlan_find_mac(vxlan, mac);
432 if (f)
433 f->used = jiffies;
434
435 return f;
436}
437
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300438/* caller should hold vxlan->hash_lock */
439static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800440 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100441 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300442{
443 struct vxlan_rdst *rd;
444
445 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800446 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300447 rd->remote_port == port &&
448 rd->remote_vni == vni &&
449 rd->remote_ifindex == ifindex)
450 return rd;
451 }
452
453 return NULL;
454}
455
Thomas Richter906dc182013-07-19 17:20:07 +0200456/* Replace destination of unicast mac */
457static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100458 union vxlan_addr *ip, __be16 port, __be32 vni,
459 __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200460{
461 struct vxlan_rdst *rd;
462
463 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
464 if (rd)
465 return 0;
466
467 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
468 if (!rd)
469 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100470
471 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800472 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200473 rd->remote_port = port;
474 rd->remote_vni = vni;
475 rd->remote_ifindex = ifindex;
476 return 1;
477}
478
David Stevens66817122013-03-15 04:35:51 +0000479/* Add/update destinations for multicast */
480static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100481 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200482 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000483{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700484 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000485
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300486 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
487 if (rd)
488 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700489
David Stevens66817122013-03-15 04:35:51 +0000490 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
491 if (rd == NULL)
492 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100493
494 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
495 kfree(rd);
496 return -ENOBUFS;
497 }
498
Cong Wange4c7ed42013-08-31 13:44:33 +0800499 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000500 rd->remote_port = port;
501 rd->remote_vni = vni;
502 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700503
504 list_add_tail_rcu(&rd->list, &f->remotes);
505
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200506 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000507 return 1;
508}
509
Tom Herbertdfd86452015-01-12 17:00:38 -0800510static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
511 unsigned int off,
512 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100513 __be32 vni_field,
514 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800515 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800516{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700517 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800518
519 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700520 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800521
522 if (!NAPI_GRO_CB(skb)->csum_valid)
523 return NULL;
524
Jiri Benc54bfd872016-02-16 21:58:58 +0100525 start = vxlan_rco_start(vni_field);
526 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800527
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700528 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
529 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800530
531 skb->remcsum_offload = 1;
532
533 return vh;
534}
535
Tom Herbert5602c482016-04-05 08:22:53 -0700536static struct sk_buff **vxlan_gro_receive(struct sock *sk,
537 struct sk_buff **head,
538 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200539{
540 struct sk_buff *p, **pp = NULL;
541 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800542 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200543 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700544 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100545 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800546 struct gro_remcsum grc;
547
548 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200549
550 off_vx = skb_gro_offset(skb);
551 hlen = off_vx + sizeof(*vh);
552 vh = skb_gro_header_fast(skb, off_vx);
553 if (skb_gro_header_hard(skb, hlen)) {
554 vh = skb_gro_header_slow(skb, hlen, off_vx);
555 if (unlikely(!vh))
556 goto out;
557 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200558
Tom Herbertdfd86452015-01-12 17:00:38 -0800559 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
560
Jiri Benc54bfd872016-02-16 21:58:58 +0100561 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800562
563 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
564 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100565 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800566 !!(vs->flags &
567 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800568
569 if (!vh)
570 goto out;
571 }
572
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700573 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
574
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200575 for (p = *head; p; p = p->next) {
576 if (!NAPI_GRO_CB(p)->same_flow)
577 continue;
578
579 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100580 if (vh->vx_flags != vh2->vx_flags ||
581 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200582 NAPI_GRO_CB(p)->same_flow = 0;
583 continue;
584 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200585 }
586
Jesse Gross9b174d82014-12-30 19:10:15 -0800587 pp = eth_gro_receive(head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800588 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200589
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200590out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800591 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200592 NAPI_GRO_CB(skb)->flush |= flush;
593
594 return pp;
595}
596
Tom Herbert5602c482016-04-05 08:22:53 -0700597static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200598{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700599 /* Sets 'skb->inner_mac_header' since we are always called with
600 * 'skb->encapsulation' set.
601 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800602 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200603}
604
stephen hemmingerd3428942012-10-01 12:32:35 +0000605/* Add new entry to forwarding table -- assumes lock held */
606static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800607 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000608 __u16 state, __u16 flags,
Jiri Benc54bfd872016-02-16 21:58:58 +0100609 __be16 port, __be32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000610 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000611{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200612 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000613 struct vxlan_fdb *f;
614 int notify = 0;
615
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000616 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000617 if (f) {
618 if (flags & NLM_F_EXCL) {
619 netdev_dbg(vxlan->dev,
620 "lost race to create %pM\n", mac);
621 return -EEXIST;
622 }
623 if (f->state != state) {
624 f->state = state;
625 f->updated = jiffies;
626 notify = 1;
627 }
David Stevensae884082013-04-19 00:36:26 +0000628 if (f->flags != ndm_flags) {
629 f->flags = ndm_flags;
630 f->updated = jiffies;
631 notify = 1;
632 }
Thomas Richter906dc182013-07-19 17:20:07 +0200633 if ((flags & NLM_F_REPLACE)) {
634 /* Only change unicasts */
635 if (!(is_multicast_ether_addr(f->eth_addr) ||
636 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800637 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200638 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200639 } else
640 return -EOPNOTSUPP;
641 }
David Stevens66817122013-03-15 04:35:51 +0000642 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300643 (is_multicast_ether_addr(f->eth_addr) ||
644 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200645 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
646 &rd);
David Stevens66817122013-03-15 04:35:51 +0000647
648 if (rc < 0)
649 return rc;
650 notify |= rc;
651 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000652 } else {
653 if (!(flags & NLM_F_CREATE))
654 return -ENOENT;
655
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200656 if (vxlan->cfg.addrmax &&
657 vxlan->addrcnt >= vxlan->cfg.addrmax)
stephen hemmingerd3428942012-10-01 12:32:35 +0000658 return -ENOSPC;
659
Thomas Richter906dc182013-07-19 17:20:07 +0200660 /* Disallow replace to add a multicast entry */
661 if ((flags & NLM_F_REPLACE) &&
662 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
663 return -EOPNOTSUPP;
664
Cong Wange4c7ed42013-08-31 13:44:33 +0800665 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000666 f = kmalloc(sizeof(*f), GFP_ATOMIC);
667 if (!f)
668 return -ENOMEM;
669
670 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000671 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000672 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000673 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700674 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000675 memcpy(f->eth_addr, mac, ETH_ALEN);
676
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200677 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700678
stephen hemmingerd3428942012-10-01 12:32:35 +0000679 ++vxlan->addrcnt;
680 hlist_add_head_rcu(&f->hlist,
681 vxlan_fdb_head(vxlan, mac));
682 }
683
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200684 if (notify) {
685 if (rd == NULL)
686 rd = first_remote_rtnl(f);
687 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
688 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000689
690 return 0;
691}
692
Wei Yongjun6706c822013-04-11 19:00:35 +0000693static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000694{
695 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700696 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000697
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100698 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
699 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000700 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100701 }
David Stevens66817122013-03-15 04:35:51 +0000702 kfree(f);
703}
704
stephen hemmingerd3428942012-10-01 12:32:35 +0000705static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
706{
707 netdev_dbg(vxlan->dev,
708 "delete %pM\n", f->eth_addr);
709
710 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200711 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000712
713 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000714 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000715}
716
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300717static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Jiri Benc54bfd872016-02-16 21:58:58 +0100718 union vxlan_addr *ip, __be16 *port, __be32 *vni,
719 u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300720{
721 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800722 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300723
724 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800725 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
726 if (err)
727 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300728 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800729 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
730 if (remote->sa.sa_family == AF_INET) {
731 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
732 ip->sa.sa_family = AF_INET;
733#if IS_ENABLED(CONFIG_IPV6)
734 } else {
735 ip->sin6.sin6_addr = in6addr_any;
736 ip->sa.sa_family = AF_INET6;
737#endif
738 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300739 }
740
741 if (tb[NDA_PORT]) {
742 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
743 return -EINVAL;
744 *port = nla_get_be16(tb[NDA_PORT]);
745 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200746 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300747 }
748
749 if (tb[NDA_VNI]) {
750 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
751 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100752 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300753 } else {
754 *vni = vxlan->default_dst.remote_vni;
755 }
756
757 if (tb[NDA_IFINDEX]) {
758 struct net_device *tdev;
759
760 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
761 return -EINVAL;
762 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800763 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300764 if (!tdev)
765 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300766 } else {
767 *ifindex = 0;
768 }
769
770 return 0;
771}
772
stephen hemmingerd3428942012-10-01 12:32:35 +0000773/* Add static entry (via netlink) */
774static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
775 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100776 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000777{
778 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300779 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800780 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000781 __be16 port;
Jiri Benc54bfd872016-02-16 21:58:58 +0100782 __be32 vni;
783 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000784 int err;
785
786 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
787 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
788 ndm->ndm_state);
789 return -EINVAL;
790 }
791
792 if (tb[NDA_DST] == NULL)
793 return -EINVAL;
794
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300795 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
796 if (err)
797 return err;
David Stevens66817122013-03-15 04:35:51 +0000798
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300799 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
800 return -EAFNOSUPPORT;
801
stephen hemmingerd3428942012-10-01 12:32:35 +0000802 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800803 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000804 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000805 spin_unlock_bh(&vxlan->hash_lock);
806
807 return err;
808}
809
810/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000811static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
812 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100813 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000814{
815 struct vxlan_dev *vxlan = netdev_priv(dev);
816 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300817 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800818 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300819 __be16 port;
Jiri Benc54bfd872016-02-16 21:58:58 +0100820 __be32 vni;
821 u32 ifindex;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300822 int err;
823
824 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
825 if (err)
826 return err;
827
828 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000829
830 spin_lock_bh(&vxlan->hash_lock);
831 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300832 if (!f)
833 goto out;
834
Cong Wange4c7ed42013-08-31 13:44:33 +0800835 if (!vxlan_addr_any(&ip)) {
836 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300837 if (!rd)
838 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000839 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300840
841 err = 0;
842
843 /* remove a destination if it's not the only one on the list,
844 * otherwise destroy the fdb entry
845 */
846 if (rd && !list_is_singular(&f->remotes)) {
847 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200848 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800849 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300850 goto out;
851 }
852
853 vxlan_fdb_destroy(vxlan, f);
854
855out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000856 spin_unlock_bh(&vxlan->hash_lock);
857
858 return err;
859}
860
861/* Dump forwarding table */
862static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400863 struct net_device *dev,
864 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000865{
866 struct vxlan_dev *vxlan = netdev_priv(dev);
867 unsigned int h;
868
869 for (h = 0; h < FDB_HASH_SIZE; ++h) {
870 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000871 int err;
872
Sasha Levinb67bfe02013-02-27 17:06:00 -0800873 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000874 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000875
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700876 list_for_each_entry_rcu(rd, &f->remotes, list) {
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900877 if (idx < cb->args[0])
878 goto skip;
879
David Stevens66817122013-03-15 04:35:51 +0000880 err = vxlan_fdb_info(skb, vxlan, f,
881 NETLINK_CB(cb->skb).portid,
882 cb->nlh->nlmsg_seq,
883 RTM_NEWNEIGH,
884 NLM_F_MULTI, rd);
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +0900885 if (err < 0) {
886 cb->args[1] = err;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700887 goto out;
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +0900888 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700889skip:
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900890 ++idx;
891 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000892 }
893 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700894out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000895 return idx;
896}
897
898/* Watch incoming packets to learn mapping between Ethernet address
899 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900900 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000901 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700902static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800903 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000904{
905 struct vxlan_dev *vxlan = netdev_priv(dev);
906 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000907
908 f = vxlan_find_mac(vxlan, src_mac);
909 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700910 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700911
Cong Wange4c7ed42013-08-31 13:44:33 +0800912 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700913 return false;
914
915 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700916 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700917 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000918
919 if (net_ratelimit())
920 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800921 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +0100922 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +0000923
Cong Wange4c7ed42013-08-31 13:44:33 +0800924 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000925 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200926 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000927 } else {
928 /* learned new entry */
929 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700930
931 /* close off race between vxlan_flush and incoming packets */
932 if (netif_running(dev))
933 vxlan_fdb_create(vxlan, src_mac, src_ip,
934 NUD_REACHABLE,
935 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200936 vxlan->cfg.dst_port,
stephen hemminger3bf74b12013-06-17 12:09:57 -0700937 vxlan->default_dst.remote_vni,
938 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000939 spin_unlock(&vxlan->hash_lock);
940 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700941
942 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000943}
944
stephen hemmingerd3428942012-10-01 12:32:35 +0000945/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +0800946static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +0000947{
stephen hemminger553675f2013-05-16 11:35:20 +0000948 struct vxlan_dev *vxlan;
Jiri Bencb1be00a2015-09-24 13:50:02 +0200949 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +0000950
Gao feng95ab0992013-12-10 16:37:33 +0800951 /* The vxlan_sock is only used by dev, leaving group has
952 * no effect on other vxlan devices.
953 */
Jiri Bencb1be00a2015-09-24 13:50:02 +0200954 if (family == AF_INET && dev->vn4_sock &&
955 atomic_read(&dev->vn4_sock->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +0800956 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +0200957#if IS_ENABLED(CONFIG_IPV6)
958 if (family == AF_INET6 && dev->vn6_sock &&
959 atomic_read(&dev->vn6_sock->refcnt) == 1)
960 return false;
961#endif
Gao feng95ab0992013-12-10 16:37:33 +0800962
stephen hemminger553675f2013-05-16 11:35:20 +0000963 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +0800964 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +0000965 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000966
Jiri Bencb1be00a2015-09-24 13:50:02 +0200967 if (family == AF_INET && vxlan->vn4_sock != dev->vn4_sock)
Gao feng95ab0992013-12-10 16:37:33 +0800968 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +0200969#if IS_ENABLED(CONFIG_IPV6)
970 if (family == AF_INET6 && vxlan->vn6_sock != dev->vn6_sock)
971 continue;
972#endif
Gao feng95ab0992013-12-10 16:37:33 +0800973
974 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
975 &dev->default_dst.remote_ip))
976 continue;
977
978 if (vxlan->default_dst.remote_ifindex !=
979 dev->default_dst.remote_ifindex)
980 continue;
981
982 return true;
stephen hemminger553675f2013-05-16 11:35:20 +0000983 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000984
985 return false;
986}
987
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +0200988static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000989{
Jiri Bencb1be00a2015-09-24 13:50:02 +0200990 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -0700991
Jiri Bencb1be00a2015-09-24 13:50:02 +0200992 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +0200993 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700994 if (!atomic_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +0200995 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700996
Jiri Bencb1be00a2015-09-24 13:50:02 +0200997 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700998 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700999 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001000 udp_tunnel_notify_del_rx_port(vs->sock,
1001 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001002 spin_unlock(&vn->sock_lock);
1003
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001004 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001005}
1006
Jiri Bencb1be00a2015-09-24 13:50:02 +02001007static void vxlan_sock_release(struct vxlan_dev *vxlan)
1008{
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001009 bool ipv4 = __vxlan_sock_release_prep(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001010#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001011 bool ipv6 = __vxlan_sock_release_prep(vxlan->vn6_sock);
1012#endif
1013
1014 synchronize_net();
1015
1016 if (ipv4) {
1017 udp_tunnel_sock_release(vxlan->vn4_sock->sock);
1018 kfree(vxlan->vn4_sock);
1019 }
1020
1021#if IS_ENABLED(CONFIG_IPV6)
1022 if (ipv6) {
1023 udp_tunnel_sock_release(vxlan->vn6_sock->sock);
1024 kfree(vxlan->vn6_sock);
1025 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001026#endif
1027}
1028
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001029/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001030 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001031 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001032static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001033{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001034 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001035 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1036 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001037 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001038
Cong Wange4c7ed42013-08-31 13:44:33 +08001039 if (ip->sa.sa_family == AF_INET) {
1040 struct ip_mreqn mreq = {
1041 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1042 .imr_ifindex = ifindex,
1043 };
1044
Jiri Bencb1be00a2015-09-24 13:50:02 +02001045 sk = vxlan->vn4_sock->sock->sk;
1046 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001047 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001048 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001049#if IS_ENABLED(CONFIG_IPV6)
1050 } else {
Jiri Bencb1be00a2015-09-24 13:50:02 +02001051 sk = vxlan->vn6_sock->sock->sk;
1052 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001053 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1054 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001055 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001056#endif
1057 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001058
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001059 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001060}
1061
1062/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001063static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001064{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001065 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001066 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1067 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001068 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001069
Cong Wange4c7ed42013-08-31 13:44:33 +08001070 if (ip->sa.sa_family == AF_INET) {
1071 struct ip_mreqn mreq = {
1072 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1073 .imr_ifindex = ifindex,
1074 };
1075
Jiri Bencb1be00a2015-09-24 13:50:02 +02001076 sk = vxlan->vn4_sock->sock->sk;
1077 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001078 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001079 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001080#if IS_ENABLED(CONFIG_IPV6)
1081 } else {
Jiri Bencb1be00a2015-09-24 13:50:02 +02001082 sk = vxlan->vn6_sock->sock->sk;
1083 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001084 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1085 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001086 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001087#endif
1088 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001089
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001090 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001091}
1092
Jiri Bencf14eceb2016-02-16 21:59:01 +01001093static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1094 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001095{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001096 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001097
Jiri Bencf14eceb2016-02-16 21:59:01 +01001098 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1099 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001100
Jiri Bencf14eceb2016-02-16 21:59:01 +01001101 start = vxlan_rco_start(unparsed->vx_vni);
1102 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001103
Jiri Benc7d34fa72016-03-21 17:50:05 +01001104 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001105 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001106
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001107 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1108 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001109out:
1110 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1111 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001112 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001113}
1114
Jiri Bencf14eceb2016-02-16 21:59:01 +01001115static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001116 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001117 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001118{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001119 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001120 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001121
Jiri Bencf14eceb2016-02-16 21:59:01 +01001122 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1123 goto out;
1124
Jiri Benc3288af02016-02-16 21:59:00 +01001125 md->gbp = ntohs(gbp->policy_id);
1126
Jiri Benc10a5af22016-02-23 18:02:59 +01001127 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001128 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001129 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001130 tun_dst->u.tun_info.options_len = sizeof(*md);
1131 }
Jiri Benc3288af02016-02-16 21:59:00 +01001132 if (gbp->dont_learn)
1133 md->gbp |= VXLAN_GBP_DONT_LEARN;
1134
1135 if (gbp->policy_applied)
1136 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001137
Jiri Benc64f87d32016-02-23 18:02:55 +01001138 /* In flow-based mode, GBP is carried in dst_metadata */
1139 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1140 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001141out:
1142 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001143}
1144
Jiri Bence1e53142016-04-05 14:47:13 +02001145static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001146 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001147 struct sk_buff *skb, u32 vxflags)
1148{
1149 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1150
1151 /* Need to have Next Protocol set for interfaces in GPE mode. */
1152 if (!gpe->np_applied)
1153 return false;
1154 /* "The initial version is 0. If a receiver does not support the
1155 * version indicated it MUST drop the packet.
1156 */
1157 if (gpe->version != 0)
1158 return false;
1159 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1160 * processing MUST occur." However, we don't implement OAM
1161 * processing, thus drop the packet.
1162 */
1163 if (gpe->oam_flag)
1164 return false;
1165
1166 switch (gpe->next_protocol) {
1167 case VXLAN_GPE_NP_IPV4:
1168 *protocol = htons(ETH_P_IP);
1169 break;
1170 case VXLAN_GPE_NP_IPV6:
1171 *protocol = htons(ETH_P_IPV6);
1172 break;
1173 case VXLAN_GPE_NP_ETHERNET:
1174 *protocol = htons(ETH_P_TEB);
1175 break;
1176 default:
1177 return false;
1178 }
1179
1180 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1181 return true;
1182}
1183
Jiri Benc1ab016e2016-02-23 18:02:56 +01001184static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1185 struct vxlan_sock *vs,
1186 struct sk_buff *skb)
Thomas Graf614732e2015-07-21 10:44:06 +02001187{
Thomas Graf614732e2015-07-21 10:44:06 +02001188 union vxlan_addr saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001189
Thomas Graf614732e2015-07-21 10:44:06 +02001190 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001191 skb->protocol = eth_type_trans(skb, vxlan->dev);
1192 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1193
1194 /* Ignore packet loops (and multicast echo) */
1195 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001196 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001197
Jiri Benc760c6802016-02-23 18:02:57 +01001198 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001199 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001200 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001201 saddr.sa.sa_family = AF_INET;
1202#if IS_ENABLED(CONFIG_IPV6)
1203 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001204 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001205 saddr.sa.sa_family = AF_INET6;
1206#endif
1207 }
1208
Jiri Benc1ab016e2016-02-23 18:02:56 +01001209 if ((vxlan->flags & VXLAN_F_LEARN) &&
1210 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1211 return false;
1212
1213 return true;
1214}
1215
Jiri Benc760c6802016-02-23 18:02:57 +01001216static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1217 struct sk_buff *skb)
1218{
1219 int err = 0;
1220
1221 if (vxlan_get_sk_family(vs) == AF_INET)
1222 err = IP_ECN_decapsulate(oiph, skb);
1223#if IS_ENABLED(CONFIG_IPV6)
1224 else
1225 err = IP6_ECN_decapsulate(oiph, skb);
1226#endif
1227
1228 if (unlikely(err) && log_ecn_error) {
1229 if (vxlan_get_sk_family(vs) == AF_INET)
1230 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1231 &((struct iphdr *)oiph)->saddr,
1232 ((struct iphdr *)oiph)->tos);
1233 else
1234 net_info_ratelimited("non-ECT from %pI6\n",
1235 &((struct ipv6hdr *)oiph)->saddr);
1236 }
1237 return err <= 1;
1238}
1239
stephen hemmingerd3428942012-10-01 12:32:35 +00001240/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001241static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001242{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001243 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001244 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001245 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001246 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001247 struct vxlan_metadata _md;
1248 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001249 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001250 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001251 void *oiph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001252
Jiri Bence1e53142016-04-05 14:47:13 +02001253 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001254 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001255 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001256
Jiri Bencf14eceb2016-02-16 21:59:01 +01001257 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001258 /* VNI flag always required to be set */
1259 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1260 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1261 ntohl(vxlan_hdr(skb)->vx_flags),
1262 ntohl(vxlan_hdr(skb)->vx_vni));
1263 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001264 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001265 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001266 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1267 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001268
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001269 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001270 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001271 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001272
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001273 vxlan = vxlan_vs_find_vni(vs, vxlan_vni(vxlan_hdr(skb)->vx_vni));
1274 if (!vxlan)
1275 goto drop;
1276
Jiri Bence1e53142016-04-05 14:47:13 +02001277 /* For backwards compatibility, only allow reserved fields to be
1278 * used by VXLAN extensions if explicitly requested.
1279 */
1280 if (vs->flags & VXLAN_F_GPE) {
1281 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1282 goto drop;
1283 raw_proto = true;
1284 }
1285
1286 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1287 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1288 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001289
Thomas Grafee122c72015-07-21 10:43:58 +02001290 if (vxlan_collect_metadata(vs)) {
Jiri Benc07dabf22016-02-18 19:19:29 +01001291 __be32 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
Jiri Benc10a5af22016-02-23 18:02:59 +01001292 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001293
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001294 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Jiri Benc07dabf22016-02-18 19:19:29 +01001295 vxlan_vni_to_tun_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001296
Thomas Grafee122c72015-07-21 10:43:58 +02001297 if (!tun_dst)
1298 goto drop;
1299
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001300 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001301
1302 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001303 } else {
1304 memset(md, 0, sizeof(*md));
1305 }
1306
Jiri Bencf14eceb2016-02-16 21:59:01 +01001307 if (vs->flags & VXLAN_F_REMCSUM_RX)
1308 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1309 goto drop;
1310 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001311 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001312 /* Note that GBP and GPE can never be active together. This is
1313 * ensured in vxlan_dev_configure.
1314 */
Thomas Graf35114942015-01-15 03:53:55 +01001315
Jiri Bencf14eceb2016-02-16 21:59:01 +01001316 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001317 /* If there are any unprocessed flags remaining treat
1318 * this as a malformed packet. This behavior diverges from
1319 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1320 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001321 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001322 * is more robust and provides a little more security in
1323 * adding extensions to VXLAN.
1324 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001325 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001326 }
1327
Jiri Bence1e53142016-04-05 14:47:13 +02001328 if (!raw_proto) {
1329 if (!vxlan_set_mac(vxlan, vs, skb))
1330 goto drop;
1331 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001332 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001333 skb->dev = vxlan->dev;
1334 skb->pkt_type = PACKET_HOST;
1335 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001336
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001337 oiph = skb_network_header(skb);
1338 skb_reset_network_header(skb);
1339
1340 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1341 ++vxlan->dev->stats.rx_frame_errors;
1342 ++vxlan->dev->stats.rx_errors;
1343 goto drop;
1344 }
1345
1346 stats = this_cpu_ptr(vxlan->dev->tstats);
1347 u64_stats_update_begin(&stats->syncp);
1348 stats->rx_packets++;
1349 stats->rx_bytes += skb->len;
1350 u64_stats_update_end(&stats->syncp);
1351
1352 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001353 return 0;
1354
1355drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001356 /* Consume bad packet */
1357 kfree_skb(skb);
1358 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001359}
1360
David Stevense4f67ad2012-11-20 02:50:14 +00001361static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1362{
1363 struct vxlan_dev *vxlan = netdev_priv(dev);
1364 struct arphdr *parp;
1365 u8 *arpptr, *sha;
1366 __be32 sip, tip;
1367 struct neighbour *n;
1368
1369 if (dev->flags & IFF_NOARP)
1370 goto out;
1371
1372 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1373 dev->stats.tx_dropped++;
1374 goto out;
1375 }
1376 parp = arp_hdr(skb);
1377
1378 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1379 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1380 parp->ar_pro != htons(ETH_P_IP) ||
1381 parp->ar_op != htons(ARPOP_REQUEST) ||
1382 parp->ar_hln != dev->addr_len ||
1383 parp->ar_pln != 4)
1384 goto out;
1385 arpptr = (u8 *)parp + sizeof(struct arphdr);
1386 sha = arpptr;
1387 arpptr += dev->addr_len; /* sha */
1388 memcpy(&sip, arpptr, sizeof(sip));
1389 arpptr += sizeof(sip);
1390 arpptr += dev->addr_len; /* tha */
1391 memcpy(&tip, arpptr, sizeof(tip));
1392
1393 if (ipv4_is_loopback(tip) ||
1394 ipv4_is_multicast(tip))
1395 goto out;
1396
1397 n = neigh_lookup(&arp_tbl, &tip, dev);
1398
1399 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001400 struct vxlan_fdb *f;
1401 struct sk_buff *reply;
1402
1403 if (!(n->nud_state & NUD_CONNECTED)) {
1404 neigh_release(n);
1405 goto out;
1406 }
1407
1408 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001409 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001410 /* bridge-local neighbor */
1411 neigh_release(n);
1412 goto out;
1413 }
1414
1415 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1416 n->ha, sha);
1417
1418 neigh_release(n);
1419
David Stevens73461352014-03-18 12:32:29 -04001420 if (reply == NULL)
1421 goto out;
1422
David Stevense4f67ad2012-11-20 02:50:14 +00001423 skb_reset_mac_header(reply);
1424 __skb_pull(reply, skb_network_offset(reply));
1425 reply->ip_summed = CHECKSUM_UNNECESSARY;
1426 reply->pkt_type = PACKET_HOST;
1427
1428 if (netif_rx_ni(reply) == NET_RX_DROP)
1429 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001430 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1431 union vxlan_addr ipa = {
1432 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001433 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001434 };
1435
1436 vxlan_ip_miss(dev, &ipa);
1437 }
David Stevense4f67ad2012-11-20 02:50:14 +00001438out:
1439 consume_skb(skb);
1440 return NETDEV_TX_OK;
1441}
1442
Cong Wangf564f452013-08-31 13:44:36 +08001443#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001444static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1445 struct neighbour *n, bool isrouter)
1446{
1447 struct net_device *dev = request->dev;
1448 struct sk_buff *reply;
1449 struct nd_msg *ns, *na;
1450 struct ipv6hdr *pip6;
1451 u8 *daddr;
1452 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1453 int ns_olen;
1454 int i, len;
1455
1456 if (dev == NULL)
1457 return NULL;
1458
1459 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1460 sizeof(*na) + na_olen + dev->needed_tailroom;
1461 reply = alloc_skb(len, GFP_ATOMIC);
1462 if (reply == NULL)
1463 return NULL;
1464
1465 reply->protocol = htons(ETH_P_IPV6);
1466 reply->dev = dev;
1467 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1468 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001469 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001470
1471 ns = (struct nd_msg *)skb_transport_header(request);
1472
1473 daddr = eth_hdr(request)->h_source;
1474 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1475 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1476 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1477 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1478 break;
1479 }
1480 }
1481
1482 /* Ethernet header */
1483 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1484 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1485 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1486 reply->protocol = htons(ETH_P_IPV6);
1487
1488 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001489 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001490 skb_put(reply, sizeof(struct ipv6hdr));
1491
1492 /* IPv6 header */
1493
1494 pip6 = ipv6_hdr(reply);
1495 memset(pip6, 0, sizeof(struct ipv6hdr));
1496 pip6->version = 6;
1497 pip6->priority = ipv6_hdr(request)->priority;
1498 pip6->nexthdr = IPPROTO_ICMPV6;
1499 pip6->hop_limit = 255;
1500 pip6->daddr = ipv6_hdr(request)->saddr;
1501 pip6->saddr = *(struct in6_addr *)n->primary_key;
1502
1503 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001504 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001505
1506 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1507
1508 /* Neighbor Advertisement */
1509 memset(na, 0, sizeof(*na)+na_olen);
1510 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1511 na->icmph.icmp6_router = isrouter;
1512 na->icmph.icmp6_override = 1;
1513 na->icmph.icmp6_solicited = 1;
1514 na->target = ns->target;
1515 ether_addr_copy(&na->opt[2], n->ha);
1516 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1517 na->opt[1] = na_olen >> 3;
1518
1519 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1520 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1521 csum_partial(na, sizeof(*na)+na_olen, 0));
1522
1523 pip6->payload_len = htons(sizeof(*na)+na_olen);
1524
1525 skb_push(reply, sizeof(struct ipv6hdr));
1526
1527 reply->ip_summed = CHECKSUM_UNNECESSARY;
1528
1529 return reply;
1530}
1531
Cong Wangf564f452013-08-31 13:44:36 +08001532static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1533{
1534 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001535 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001536 const struct ipv6hdr *iphdr;
1537 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001538 struct neighbour *n;
1539 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001540
1541 in6_dev = __in6_dev_get(dev);
1542 if (!in6_dev)
1543 goto out;
1544
Cong Wangf564f452013-08-31 13:44:36 +08001545 iphdr = ipv6_hdr(skb);
1546 saddr = &iphdr->saddr;
1547 daddr = &iphdr->daddr;
1548
Cong Wangf564f452013-08-31 13:44:36 +08001549 msg = (struct nd_msg *)skb_transport_header(skb);
1550 if (msg->icmph.icmp6_code != 0 ||
1551 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1552 goto out;
1553
David Stevens4b29dba2014-03-24 10:39:58 -04001554 if (ipv6_addr_loopback(daddr) ||
1555 ipv6_addr_is_multicast(&msg->target))
1556 goto out;
1557
1558 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001559
1560 if (n) {
1561 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001562 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001563
1564 if (!(n->nud_state & NUD_CONNECTED)) {
1565 neigh_release(n);
1566 goto out;
1567 }
1568
1569 f = vxlan_find_mac(vxlan, n->ha);
1570 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1571 /* bridge-local neighbor */
1572 neigh_release(n);
1573 goto out;
1574 }
1575
David Stevens4b29dba2014-03-24 10:39:58 -04001576 reply = vxlan_na_create(skb, n,
1577 !!(f ? f->flags & NTF_ROUTER : 0));
1578
Cong Wangf564f452013-08-31 13:44:36 +08001579 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001580
1581 if (reply == NULL)
1582 goto out;
1583
1584 if (netif_rx_ni(reply) == NET_RX_DROP)
1585 dev->stats.rx_dropped++;
1586
Cong Wangf564f452013-08-31 13:44:36 +08001587 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001588 union vxlan_addr ipa = {
1589 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001590 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001591 };
1592
Cong Wangf564f452013-08-31 13:44:36 +08001593 vxlan_ip_miss(dev, &ipa);
1594 }
1595
1596out:
1597 consume_skb(skb);
1598 return NETDEV_TX_OK;
1599}
1600#endif
1601
David Stevense4f67ad2012-11-20 02:50:14 +00001602static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1603{
1604 struct vxlan_dev *vxlan = netdev_priv(dev);
1605 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001606
1607 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1608 return false;
1609
1610 n = NULL;
1611 switch (ntohs(eth_hdr(skb)->h_proto)) {
1612 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001613 {
1614 struct iphdr *pip;
1615
David Stevense4f67ad2012-11-20 02:50:14 +00001616 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1617 return false;
1618 pip = ip_hdr(skb);
1619 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001620 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1621 union vxlan_addr ipa = {
1622 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001623 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001624 };
1625
1626 vxlan_ip_miss(dev, &ipa);
1627 return false;
1628 }
1629
David Stevense4f67ad2012-11-20 02:50:14 +00001630 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001631 }
1632#if IS_ENABLED(CONFIG_IPV6)
1633 case ETH_P_IPV6:
1634 {
1635 struct ipv6hdr *pip6;
1636
1637 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1638 return false;
1639 pip6 = ipv6_hdr(skb);
1640 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1641 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1642 union vxlan_addr ipa = {
1643 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001644 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001645 };
1646
1647 vxlan_ip_miss(dev, &ipa);
1648 return false;
1649 }
1650
1651 break;
1652 }
1653#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001654 default:
1655 return false;
1656 }
1657
1658 if (n) {
1659 bool diff;
1660
Joe Perches7367d0b2013-09-01 11:51:23 -07001661 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001662 if (diff) {
1663 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1664 dev->addr_len);
1665 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1666 }
1667 neigh_release(n);
1668 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001669 }
1670
David Stevense4f67ad2012-11-20 02:50:14 +00001671 return false;
1672}
1673
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001674static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001675 struct vxlan_metadata *md)
1676{
1677 struct vxlanhdr_gbp *gbp;
1678
Thomas Grafdb79a622015-02-04 17:00:04 +01001679 if (!md->gbp)
1680 return;
1681
Thomas Graf35114942015-01-15 03:53:55 +01001682 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001683 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001684
1685 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1686 gbp->dont_learn = 1;
1687
1688 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1689 gbp->policy_applied = 1;
1690
1691 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1692}
1693
Jiri Bence1e53142016-04-05 14:47:13 +02001694static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1695 __be16 protocol)
1696{
1697 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1698
1699 gpe->np_applied = 1;
1700
1701 switch (protocol) {
1702 case htons(ETH_P_IP):
1703 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1704 return 0;
1705 case htons(ETH_P_IPV6):
1706 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1707 return 0;
1708 case htons(ETH_P_TEB):
1709 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1710 return 0;
1711 }
1712 return -EPFNOSUPPORT;
1713}
1714
Jiri Bencf491e562016-02-02 18:09:16 +01001715static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1716 int iphdr_len, __be32 vni,
1717 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001718 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001719{
Cong Wange4c7ed42013-08-31 13:44:33 +08001720 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001721 int min_headroom;
1722 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001723 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001724 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001725
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001726 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001727 skb->ip_summed == CHECKSUM_PARTIAL) {
1728 int csum_start = skb_checksum_start_offset(skb);
1729
1730 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1731 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1732 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001733 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001734 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001735 }
1736
Cong Wange4c7ed42013-08-31 13:44:33 +08001737 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
Jiri Bencf491e562016-02-02 18:09:16 +01001738 + VXLAN_HLEN + iphdr_len
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001739 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001740
1741 /* Need space for new headers (invalidates iph ptr) */
1742 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001743 if (unlikely(err))
1744 goto out_free;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001745
Jiri Pirko59682502014-11-19 14:04:59 +01001746 skb = vlan_hwaccel_push_inside(skb);
1747 if (WARN_ON(!skb))
1748 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001749
Alexander Duyckaed069d2016-04-14 15:33:37 -04001750 err = iptunnel_handle_offloads(skb, type);
1751 if (err)
1752 goto out_free;
Jesse Grossb736a622015-04-09 11:19:14 -07001753
Pravin B Shelar49560532013-08-19 11:23:17 -07001754 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001755 vxh->vx_flags = VXLAN_HF_VNI;
1756 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001757
Tom Herbertdfd86452015-01-12 17:00:38 -08001758 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001759 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001760
Jiri Benc54bfd872016-02-16 21:58:58 +01001761 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1762 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1763 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001764
1765 if (!skb_is_gso(skb)) {
1766 skb->ip_summed = CHECKSUM_NONE;
1767 skb->encapsulation = 0;
1768 }
1769 }
1770
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001771 if (vxflags & VXLAN_F_GBP)
1772 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001773 if (vxflags & VXLAN_F_GPE) {
1774 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1775 if (err < 0)
1776 goto out_free;
1777 inner_protocol = skb->protocol;
1778 }
Thomas Graf35114942015-01-15 03:53:55 +01001779
Jiri Bence1e53142016-04-05 14:47:13 +02001780 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001781 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02001782
1783out_free:
1784 kfree_skb(skb);
1785 return err;
Pravin B Shelar49560532013-08-19 11:23:17 -07001786}
Pravin B Shelar49560532013-08-19 11:23:17 -07001787
Jiri Benc1a8496b2016-02-02 18:09:14 +01001788static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan,
1789 struct sk_buff *skb, int oif, u8 tos,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001790 __be32 daddr, __be32 *saddr,
1791 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001792 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01001793{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001794 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01001795 struct rtable *rt = NULL;
1796 struct flowi4 fl4;
1797
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001798 if (tos && !info)
1799 use_cache = false;
1800 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001801 rt = dst_cache_get_ip4(dst_cache, saddr);
1802 if (rt)
1803 return rt;
1804 }
1805
Jiri Benc1a8496b2016-02-02 18:09:14 +01001806 memset(&fl4, 0, sizeof(fl4));
1807 fl4.flowi4_oif = oif;
1808 fl4.flowi4_tos = RT_TOS(tos);
1809 fl4.flowi4_mark = skb->mark;
1810 fl4.flowi4_proto = IPPROTO_UDP;
1811 fl4.daddr = daddr;
1812 fl4.saddr = vxlan->cfg.saddr.sin.sin_addr.s_addr;
1813
1814 rt = ip_route_output_key(vxlan->net, &fl4);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001815 if (!IS_ERR(rt)) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01001816 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001817 if (use_cache)
1818 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
1819 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01001820 return rt;
1821}
1822
Jiri Bence5d4b292015-12-07 13:04:30 +01001823#if IS_ENABLED(CONFIG_IPV6)
1824static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
Daniel Borkmann14006152016-03-04 15:15:08 +01001825 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001826 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01001827 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001828 struct in6_addr *saddr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001829 struct dst_cache *dst_cache,
1830 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01001831{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001832 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01001833 struct dst_entry *ndst;
1834 struct flowi6 fl6;
1835 int err;
1836
Daniel Borkmann14006152016-03-04 15:15:08 +01001837 if (tos && !info)
1838 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001839 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001840 ndst = dst_cache_get_ip6(dst_cache, saddr);
1841 if (ndst)
1842 return ndst;
1843 }
1844
Jiri Bence5d4b292015-12-07 13:04:30 +01001845 memset(&fl6, 0, sizeof(fl6));
1846 fl6.flowi6_oif = oif;
1847 fl6.daddr = *daddr;
1848 fl6.saddr = vxlan->cfg.saddr.sin6.sin6_addr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01001849 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01001850 fl6.flowi6_mark = skb->mark;
1851 fl6.flowi6_proto = IPPROTO_UDP;
1852
1853 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
1854 vxlan->vn6_sock->sock->sk,
1855 &ndst, &fl6);
1856 if (err < 0)
1857 return ERR_PTR(err);
1858
1859 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001860 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001861 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01001862 return ndst;
1863}
1864#endif
1865
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001866/* Bypass encapsulation if the destination is local */
1867static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1868 struct vxlan_dev *dst_vxlan)
1869{
Li RongQing8f849852014-01-04 13:57:59 +08001870 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001871 union vxlan_addr loopback;
1872 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001873 struct net_device *dev = skb->dev;
1874 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001875
Li RongQing8f849852014-01-04 13:57:59 +08001876 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1877 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001878 skb->pkt_type = PACKET_HOST;
1879 skb->encapsulation = 0;
1880 skb->dev = dst_vxlan->dev;
1881 __skb_pull(skb, skb_network_offset(skb));
1882
Cong Wange4c7ed42013-08-31 13:44:33 +08001883 if (remote_ip->sa.sa_family == AF_INET) {
1884 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1885 loopback.sa.sa_family = AF_INET;
1886#if IS_ENABLED(CONFIG_IPV6)
1887 } else {
1888 loopback.sin6.sin6_addr = in6addr_loopback;
1889 loopback.sa.sa_family = AF_INET6;
1890#endif
1891 }
1892
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001893 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001894 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001895
1896 u64_stats_update_begin(&tx_stats->syncp);
1897 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001898 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001899 u64_stats_update_end(&tx_stats->syncp);
1900
1901 if (netif_rx(skb) == NET_RX_SUCCESS) {
1902 u64_stats_update_begin(&rx_stats->syncp);
1903 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001904 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001905 u64_stats_update_end(&rx_stats->syncp);
1906 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001907 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001908 }
1909}
1910
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001911static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1912 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001913{
Paolo Abenid71785f2016-02-12 15:43:57 +01001914 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02001915 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00001916 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001917 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001918 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001919 const struct iphdr *old_iph;
Cong Wange4c7ed42013-08-31 13:44:33 +08001920 union vxlan_addr *dst;
Thomas Grafee122c72015-07-21 10:43:58 +02001921 union vxlan_addr remote_ip;
1922 struct vxlan_metadata _md;
1923 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001924 __be16 src_port = 0, dst_port;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001925 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00001926 __be16 df = 0;
1927 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001928 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02001929 u32 flags = vxlan->flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001930 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01001931 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00001932
Jiri Benc61adedf2015-08-20 13:56:25 +02001933 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02001934
Thomas Grafee122c72015-07-21 10:43:58 +02001935 if (rdst) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001936 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Thomas Grafee122c72015-07-21 10:43:58 +02001937 vni = rdst->remote_vni;
1938 dst = &rdst->remote_ip;
Paolo Abenid71785f2016-02-12 15:43:57 +01001939 dst_cache = &rdst->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02001940 } else {
1941 if (!info) {
1942 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1943 dev->name);
1944 goto drop;
1945 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001946 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
Jiri Benc54bfd872016-02-16 21:58:58 +01001947 vni = vxlan_tun_id_to_vni(info->key.tun_id);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001948 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
1949 if (remote_ip.sa.sa_family == AF_INET)
Jiri Benca725e512015-08-20 13:56:30 +02001950 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
1951 else
1952 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
Thomas Grafee122c72015-07-21 10:43:58 +02001953 dst = &remote_ip;
Paolo Abenid71785f2016-02-12 15:43:57 +01001954 dst_cache = &info->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02001955 }
David Stevense4f67ad2012-11-20 02:50:14 +00001956
Cong Wange4c7ed42013-08-31 13:44:33 +08001957 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001958 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001959 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001960 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001961 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001962 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001963 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001964 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001965
stephen hemmingerd3428942012-10-01 12:32:35 +00001966 old_iph = ip_hdr(skb);
1967
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001968 ttl = vxlan->cfg.ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001969 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001970 ttl = 1;
1971
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001972 tos = vxlan->cfg.tos;
stephen hemmingerd3428942012-10-01 12:32:35 +00001973 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001974 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001975
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001976 label = vxlan->cfg.label;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001977 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
1978 vxlan->cfg.port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001979
Jiri Benca725e512015-08-20 13:56:30 +02001980 if (info) {
Jiri Benca725e512015-08-20 13:56:30 +02001981 ttl = info->key.ttl;
1982 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001983 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001984 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02001985
1986 if (info->options_len)
Pravin B Shelar4c222792015-08-30 18:09:38 -07001987 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02001988 } else {
1989 md->gbp = skb->mark;
1990 }
1991
Cong Wange4c7ed42013-08-31 13:44:33 +08001992 if (dst->sa.sa_family == AF_INET) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01001993 __be32 saddr;
1994
Jiri Bencb1be00a2015-09-24 13:50:02 +02001995 if (!vxlan->vn4_sock)
1996 goto drop;
1997 sk = vxlan->vn4_sock->sock->sk;
1998
Jiri Benc1a8496b2016-02-02 18:09:14 +01001999 rt = vxlan_get_route(vxlan, skb,
2000 rdst ? rdst->remote_ifindex : 0, tos,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002001 dst->sin.sin_addr.s_addr, &saddr,
Paolo Abenid71785f2016-02-12 15:43:57 +01002002 dst_cache, info);
Cong Wange4c7ed42013-08-31 13:44:33 +08002003 if (IS_ERR(rt)) {
2004 netdev_dbg(dev, "no route to %pI4\n",
2005 &dst->sin.sin_addr.s_addr);
2006 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002007 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002008 }
2009
2010 if (rt->dst.dev == dev) {
2011 netdev_dbg(dev, "circular route to %pI4\n",
2012 &dst->sin.sin_addr.s_addr);
2013 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08002014 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002015 }
2016
2017 /* Bypass encapsulation if the destination is local */
2018 if (rt->rt_flags & RTCF_LOCAL &&
2019 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2020 struct vxlan_dev *dst_vxlan;
2021
2022 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002023 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002024 dst->sa.sa_family, dst_port,
2025 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002026 if (!dst_vxlan)
2027 goto tx_error;
2028 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2029 return;
2030 }
2031
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002032 if (!info)
2033 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2034 else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
2035 df = htons(IP_DF);
2036
Cong Wange4c7ed42013-08-31 13:44:33 +08002037 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2038 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Jiri Bencf491e562016-02-02 18:09:16 +01002039 err = vxlan_build_skb(skb, &rt->dst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002040 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002041 if (err < 0)
2042 goto xmit_tx_error;
2043
2044 udp_tunnel_xmit_skb(rt, sk, skb, saddr,
2045 dst->sin.sin_addr.s_addr, tos, ttl, df,
2046 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002047#if IS_ENABLED(CONFIG_IPV6)
2048 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08002049 struct dst_entry *ndst;
Jiri Bence5d4b292015-12-07 13:04:30 +01002050 struct in6_addr saddr;
Jiri Benc6f264e42015-08-20 13:56:29 +02002051 u32 rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002052
Jiri Bencb1be00a2015-09-24 13:50:02 +02002053 if (!vxlan->vn6_sock)
2054 goto drop;
2055 sk = vxlan->vn6_sock->sock->sk;
2056
Jiri Bence5d4b292015-12-07 13:04:30 +01002057 ndst = vxlan6_get_route(vxlan, skb,
Daniel Borkmann14006152016-03-04 15:15:08 +01002058 rdst ? rdst->remote_ifindex : 0, tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002059 label, &dst->sin6.sin6_addr, &saddr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002060 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002061 if (IS_ERR(ndst)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002062 netdev_dbg(dev, "no route to %pI6\n",
2063 &dst->sin6.sin6_addr);
2064 dev->stats.tx_carrier_errors++;
2065 goto tx_error;
2066 }
2067
2068 if (ndst->dev == dev) {
2069 netdev_dbg(dev, "circular route to %pI6\n",
2070 &dst->sin6.sin6_addr);
2071 dst_release(ndst);
2072 dev->stats.collisions++;
2073 goto tx_error;
2074 }
2075
2076 /* Bypass encapsulation if the destination is local */
Jiri Benc6f264e42015-08-20 13:56:29 +02002077 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2078 if (rt6i_flags & RTF_LOCAL &&
2079 !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002080 struct vxlan_dev *dst_vxlan;
2081
2082 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002083 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002084 dst->sa.sa_family, dst_port,
2085 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002086 if (!dst_vxlan)
2087 goto tx_error;
2088 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2089 return;
2090 }
2091
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002092 if (!info)
2093 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Jesse Gross35e2d112016-01-20 16:22:47 -08002094
Daniel Borkmann14006152016-03-04 15:15:08 +01002095 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002096 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002097 skb_scrub_packet(skb, xnet);
2098 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002099 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002100 if (err < 0) {
2101 dst_release(ndst);
2102 return;
2103 }
2104 udp_tunnel6_xmit_skb(ndst, sk, skb, dev,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002105 &saddr, &dst->sin6.sin6_addr, tos, ttl,
2106 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002107#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002108 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002109
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002110 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002111
2112drop:
2113 dev->stats.tx_dropped++;
2114 goto tx_free;
2115
Jiri Bencf491e562016-02-02 18:09:16 +01002116xmit_tx_error:
2117 /* skb is already freed. */
2118 skb = NULL;
Pravin B Shelar49560532013-08-19 11:23:17 -07002119rt_tx_error:
2120 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002121tx_error:
2122 dev->stats.tx_errors++;
2123tx_free:
2124 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002125}
2126
David Stevens66817122013-03-15 04:35:51 +00002127/* Transmit local packets over Vxlan
2128 *
2129 * Outer IP header inherits ECN and DF from inner header.
2130 * Outer UDP destination is the VXLAN assigned port.
2131 * source port is based on hash of flow
2132 */
2133static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2134{
2135 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002136 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002137 struct ethhdr *eth;
2138 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002139 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002140 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002141
Jiri Benc61adedf2015-08-20 13:56:25 +02002142 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002143
David Stevens66817122013-03-15 04:35:51 +00002144 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002145
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002146 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
2147 if (info && info->mode & IP_TUNNEL_INFO_TX)
2148 vxlan_xmit_one(skb, dev, NULL, false);
2149 else
2150 kfree_skb(skb);
2151 return NETDEV_TX_OK;
2152 }
2153
2154 if (vxlan->flags & VXLAN_F_PROXY) {
2155 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002156 if (ntohs(eth->h_proto) == ETH_P_ARP)
2157 return arp_reduce(dev, skb);
2158#if IS_ENABLED(CONFIG_IPV6)
2159 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002160 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2161 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002162 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2163 struct nd_msg *msg;
2164
2165 msg = (struct nd_msg *)skb_transport_header(skb);
2166 if (msg->icmph.icmp6_code == 0 &&
2167 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2168 return neigh_reduce(dev, skb);
2169 }
2170#endif
2171 }
David Stevens66817122013-03-15 04:35:51 +00002172
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002173 eth = eth_hdr(skb);
David Stevens66817122013-03-15 04:35:51 +00002174 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002175 did_rsc = false;
2176
2177 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002178 (ntohs(eth->h_proto) == ETH_P_IP ||
2179 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002180 did_rsc = route_shortcircuit(dev, skb);
2181 if (did_rsc)
2182 f = vxlan_find_mac(vxlan, eth->h_dest);
2183 }
2184
David Stevens66817122013-03-15 04:35:51 +00002185 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002186 f = vxlan_find_mac(vxlan, all_zeros_mac);
2187 if (f == NULL) {
2188 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2189 !is_multicast_ether_addr(eth->h_dest))
2190 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002191
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002192 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002193 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002194 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002195 }
David Stevens66817122013-03-15 04:35:51 +00002196 }
2197
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002198 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2199 struct sk_buff *skb1;
2200
Eric Dumazet8f646c92014-01-06 09:54:31 -08002201 if (!fdst) {
2202 fdst = rdst;
2203 continue;
2204 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002205 skb1 = skb_clone(skb, GFP_ATOMIC);
2206 if (skb1)
2207 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2208 }
2209
Eric Dumazet8f646c92014-01-06 09:54:31 -08002210 if (fdst)
2211 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2212 else
2213 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002214 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002215}
2216
stephen hemmingerd3428942012-10-01 12:32:35 +00002217/* Walk the forwarding table and purge stale entries */
2218static void vxlan_cleanup(unsigned long arg)
2219{
2220 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2221 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2222 unsigned int h;
2223
2224 if (!netif_running(vxlan->dev))
2225 return;
2226
stephen hemmingerd3428942012-10-01 12:32:35 +00002227 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2228 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002229
2230 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002231 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2232 struct vxlan_fdb *f
2233 = container_of(p, struct vxlan_fdb, hlist);
2234 unsigned long timeout;
2235
stephen hemminger3c172862012-10-26 06:24:34 +00002236 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002237 continue;
2238
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002239 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002240 if (time_before_eq(timeout, jiffies)) {
2241 netdev_dbg(vxlan->dev,
2242 "garbage collect %pM\n",
2243 f->eth_addr);
2244 f->state = NUD_STALE;
2245 vxlan_fdb_destroy(vxlan, f);
2246 } else if (time_before(timeout, next_timer))
2247 next_timer = timeout;
2248 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002249 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002250 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002251
2252 mod_timer(&vxlan->age_timer, next_timer);
2253}
2254
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002255static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2256{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002257 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002258 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002259
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002260 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002261 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002262 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002263}
2264
stephen hemmingerd3428942012-10-01 12:32:35 +00002265/* Setup stats when device is created */
2266static int vxlan_init(struct net_device *dev)
2267{
WANG Cong1c213bd2014-02-13 11:46:28 -08002268 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002269 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002270 return -ENOMEM;
2271
2272 return 0;
2273}
2274
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002275static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002276{
2277 struct vxlan_fdb *f;
2278
2279 spin_lock_bh(&vxlan->hash_lock);
2280 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2281 if (f)
2282 vxlan_fdb_destroy(vxlan, f);
2283 spin_unlock_bh(&vxlan->hash_lock);
2284}
2285
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002286static void vxlan_uninit(struct net_device *dev)
2287{
2288 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002289
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002290 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002291
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002292 free_percpu(dev->tstats);
2293}
2294
stephen hemmingerd3428942012-10-01 12:32:35 +00002295/* Start ageing timer and join group when device is brought up */
2296static int vxlan_open(struct net_device *dev)
2297{
2298 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002299 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002300
Jiri Benc205f3562015-09-24 13:50:01 +02002301 ret = vxlan_sock_add(vxlan);
2302 if (ret < 0)
2303 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002304
Gao feng79d4a942013-12-10 16:37:32 +08002305 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002306 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002307 if (ret == -EADDRINUSE)
2308 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002309 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002310 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002311 return ret;
2312 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002313 }
2314
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002315 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002316 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2317
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002318 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002319}
2320
2321/* Purge the forwarding table */
2322static void vxlan_flush(struct vxlan_dev *vxlan)
2323{
Cong Wang31fec5a2013-05-27 22:35:52 +00002324 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002325
2326 spin_lock_bh(&vxlan->hash_lock);
2327 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2328 struct hlist_node *p, *n;
2329 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2330 struct vxlan_fdb *f
2331 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002332 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2333 if (!is_zero_ether_addr(f->eth_addr))
2334 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002335 }
2336 }
2337 spin_unlock_bh(&vxlan->hash_lock);
2338}
2339
2340/* Cleanup timer and forwarding table on shutdown */
2341static int vxlan_stop(struct net_device *dev)
2342{
2343 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002344 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002345 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002346
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002347 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002348 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002349 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002350
2351 del_timer_sync(&vxlan->age_timer);
2352
2353 vxlan_flush(vxlan);
Jiri Benc205f3562015-09-24 13:50:01 +02002354 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002355
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002356 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002357}
2358
stephen hemmingerd3428942012-10-01 12:32:35 +00002359/* Stub, nothing needs to be done. */
2360static void vxlan_set_multicast_list(struct net_device *dev)
2361{
2362}
2363
David Wragg72564b52016-02-10 00:05:55 +00002364static int __vxlan_change_mtu(struct net_device *dev,
2365 struct net_device *lowerdev,
2366 struct vxlan_rdst *dst, int new_mtu, bool strict)
2367{
2368 int max_mtu = IP_MAX_MTU;
2369
2370 if (lowerdev)
2371 max_mtu = lowerdev->mtu;
2372
2373 if (dst->remote_ip.sa.sa_family == AF_INET6)
2374 max_mtu -= VXLAN6_HEADROOM;
2375 else
2376 max_mtu -= VXLAN_HEADROOM;
2377
2378 if (new_mtu < 68)
2379 return -EINVAL;
2380
2381 if (new_mtu > max_mtu) {
2382 if (strict)
2383 return -EINVAL;
2384
2385 new_mtu = max_mtu;
2386 }
2387
2388 dev->mtu = new_mtu;
2389 return 0;
2390}
2391
Daniel Borkmann345010b2013-12-18 00:21:08 +01002392static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2393{
2394 struct vxlan_dev *vxlan = netdev_priv(dev);
2395 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002396 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2397 dst->remote_ifindex);
2398 return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002399}
2400
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002401static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2402{
2403 struct vxlan_dev *vxlan = netdev_priv(dev);
2404 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2405 __be16 sport, dport;
2406
2407 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2408 vxlan->cfg.port_max, true);
2409 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2410
Jiri Benc239e9442015-12-07 13:04:31 +01002411 if (ip_tunnel_info_af(info) == AF_INET) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01002412 struct rtable *rt;
2413
Jiri Benc239e9442015-12-07 13:04:31 +01002414 if (!vxlan->vn4_sock)
2415 return -EINVAL;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002416 rt = vxlan_get_route(vxlan, skb, 0, info->key.tos,
2417 info->key.u.ipv4.dst,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002418 &info->key.u.ipv4.src, NULL, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002419 if (IS_ERR(rt))
2420 return PTR_ERR(rt);
2421 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002422 } else {
2423#if IS_ENABLED(CONFIG_IPV6)
2424 struct dst_entry *ndst;
2425
2426 if (!vxlan->vn6_sock)
2427 return -EINVAL;
Daniel Borkmann14006152016-03-04 15:15:08 +01002428 ndst = vxlan6_get_route(vxlan, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002429 info->key.label, &info->key.u.ipv6.dst,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002430 &info->key.u.ipv6.src, NULL, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002431 if (IS_ERR(ndst))
2432 return PTR_ERR(ndst);
2433 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002434#else /* !CONFIG_IPV6 */
2435 return -EPFNOSUPPORT;
2436#endif
2437 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002438 info->key.tp_src = sport;
2439 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002440 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002441}
2442
Jiri Benc0c867c92016-04-05 14:47:10 +02002443static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002444 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002445 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002446 .ndo_open = vxlan_open,
2447 .ndo_stop = vxlan_stop,
2448 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002449 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002450 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002451 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002452 .ndo_validate_addr = eth_validate_addr,
2453 .ndo_set_mac_address = eth_mac_addr,
2454 .ndo_fdb_add = vxlan_fdb_add,
2455 .ndo_fdb_del = vxlan_fdb_delete,
2456 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002457 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002458};
2459
Jiri Bence1e53142016-04-05 14:47:13 +02002460static const struct net_device_ops vxlan_netdev_raw_ops = {
2461 .ndo_init = vxlan_init,
2462 .ndo_uninit = vxlan_uninit,
2463 .ndo_open = vxlan_open,
2464 .ndo_stop = vxlan_stop,
2465 .ndo_start_xmit = vxlan_xmit,
2466 .ndo_get_stats64 = ip_tunnel_get_stats64,
2467 .ndo_change_mtu = vxlan_change_mtu,
2468 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2469};
2470
stephen hemmingerd3428942012-10-01 12:32:35 +00002471/* Info for udev, that this is a virtual tunnel endpoint */
2472static struct device_type vxlan_type = {
2473 .name = "vxlan",
2474};
2475
Alexander Duycke7b3db52016-06-16 12:20:52 -07002476/* Calls the ndo_add_udp_enc_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002477 * supply the listening VXLAN udp ports. Callers are expected
Alexander Duycke7b3db52016-06-16 12:20:52 -07002478 * to implement the ndo_add_udp_enc_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002479 */
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02002480static void vxlan_push_rx_ports(struct net_device *dev)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002481{
2482 struct vxlan_sock *vs;
2483 struct net *net = dev_net(dev);
2484 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002485 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002486
2487 spin_lock(&vn->sock_lock);
2488 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Alexander Duycke7b3db52016-06-16 12:20:52 -07002489 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2490 udp_tunnel_push_rx_port(dev, vs->sock,
2491 UDP_TUNNEL_TYPE_VXLAN);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002492 }
2493 spin_unlock(&vn->sock_lock);
2494}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002495
stephen hemmingerd3428942012-10-01 12:32:35 +00002496/* Initialize the device structure. */
2497static void vxlan_setup(struct net_device *dev)
2498{
2499 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002500 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002501
Jiri Benc65226ef2016-04-28 16:36:30 +02002502 eth_hw_addr_random(dev);
2503 ether_setup(dev);
2504
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002505 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002506 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2507
stephen hemmingerd3428942012-10-01 12:32:35 +00002508 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002509 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002510 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002511 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002512
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002513 dev->vlan_features = dev->features;
2514 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002515 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002516 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002517 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002518 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002519 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002520
stephen hemminger553675f2013-05-16 11:35:20 +00002521 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002522 spin_lock_init(&vxlan->hash_lock);
2523
2524 init_timer_deferrable(&vxlan->age_timer);
2525 vxlan->age_timer.function = vxlan_cleanup;
2526 vxlan->age_timer.data = (unsigned long) vxlan;
2527
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002528 vxlan->cfg.dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002529
stephen hemmingerd3428942012-10-01 12:32:35 +00002530 vxlan->dev = dev;
2531
Tom Herbert58ce31c2015-08-19 17:07:33 -07002532 gro_cells_init(&vxlan->gro_cells, dev);
2533
stephen hemmingerd3428942012-10-01 12:32:35 +00002534 for (h = 0; h < FDB_HASH_SIZE; ++h)
2535 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2536}
2537
Jiri Benc0c867c92016-04-05 14:47:10 +02002538static void vxlan_ether_setup(struct net_device *dev)
2539{
Jiri Benc0c867c92016-04-05 14:47:10 +02002540 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2541 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2542 dev->netdev_ops = &vxlan_netdev_ether_ops;
2543}
2544
Jiri Bence1e53142016-04-05 14:47:13 +02002545static void vxlan_raw_setup(struct net_device *dev)
2546{
Jiri Benc65226ef2016-04-28 16:36:30 +02002547 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002548 dev->type = ARPHRD_NONE;
2549 dev->hard_header_len = 0;
2550 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002551 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2552 dev->netdev_ops = &vxlan_netdev_raw_ops;
2553}
2554
stephen hemmingerd3428942012-10-01 12:32:35 +00002555static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2556 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002557 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002558 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002559 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2560 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002561 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002562 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2563 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002564 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002565 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2566 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2567 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002568 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002569 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2570 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2571 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2572 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002573 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002574 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002575 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2576 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2577 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002578 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2579 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002580 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002581 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002582 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002583};
2584
2585static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2586{
2587 if (tb[IFLA_ADDRESS]) {
2588 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2589 pr_debug("invalid link address (not ethernet)\n");
2590 return -EINVAL;
2591 }
2592
2593 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2594 pr_debug("invalid all zero ethernet address\n");
2595 return -EADDRNOTAVAIL;
2596 }
2597 }
2598
2599 if (!data)
2600 return -EINVAL;
2601
2602 if (data[IFLA_VXLAN_ID]) {
2603 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2604 if (id >= VXLAN_VID_MASK)
2605 return -ERANGE;
2606 }
2607
stephen hemminger05f47d62012-10-09 20:35:50 +00002608 if (data[IFLA_VXLAN_PORT_RANGE]) {
2609 const struct ifla_vxlan_port_range *p
2610 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2611
2612 if (ntohs(p->high) < ntohs(p->low)) {
2613 pr_debug("port range %u .. %u not valid\n",
2614 ntohs(p->low), ntohs(p->high));
2615 return -EINVAL;
2616 }
2617 }
2618
stephen hemmingerd3428942012-10-01 12:32:35 +00002619 return 0;
2620}
2621
Yan Burman1b13c972013-01-29 23:43:07 +00002622static void vxlan_get_drvinfo(struct net_device *netdev,
2623 struct ethtool_drvinfo *drvinfo)
2624{
2625 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2626 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2627}
2628
2629static const struct ethtool_ops vxlan_ethtool_ops = {
2630 .get_drvinfo = vxlan_get_drvinfo,
2631 .get_link = ethtool_op_get_link,
2632};
2633
Tom Herbert3ee64f32014-07-13 19:49:42 -07002634static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2635 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002636{
Cong Wange4c7ed42013-08-31 13:44:33 +08002637 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002638 struct udp_port_cfg udp_conf;
2639 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002640
Tom Herbert3ee64f32014-07-13 19:49:42 -07002641 memset(&udp_conf, 0, sizeof(udp_conf));
2642
2643 if (ipv6) {
2644 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002645 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002646 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002647 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002648 } else {
2649 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002650 }
2651
Tom Herbert3ee64f32014-07-13 19:49:42 -07002652 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002653
Tom Herbert3ee64f32014-07-13 19:49:42 -07002654 /* Open UDP socket */
2655 err = udp_sock_create(net, &udp_conf, &sock);
2656 if (err < 0)
2657 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002658
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002659 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002660}
2661
2662/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002663static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2664 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002665{
2666 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2667 struct vxlan_sock *vs;
2668 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002669 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002670 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002671
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002672 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002673 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002674 return ERR_PTR(-ENOMEM);
2675
2676 for (h = 0; h < VNI_HASH_SIZE; ++h)
2677 INIT_HLIST_HEAD(&vs->vni_list[h]);
2678
Tom Herbert3ee64f32014-07-13 19:49:42 -07002679 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002680 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002681 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2682 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002683 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002684 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002685 }
2686
Cong Wange4c7ed42013-08-31 13:44:33 +08002687 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002688 atomic_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002689 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002690
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002691 spin_lock(&vn->sock_lock);
2692 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07002693 udp_tunnel_notify_add_rx_port(sock,
2694 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002695 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002696
2697 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002698 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002699 tunnel_cfg.sk_user_data = vs;
2700 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002701 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002702 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002703 tunnel_cfg.gro_receive = vxlan_gro_receive;
2704 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002705
2706 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002707
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002708 return vs;
2709}
stephen hemminger553675f2013-05-16 11:35:20 +00002710
Jiri Bencb1be00a2015-09-24 13:50:02 +02002711static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002712{
Jiri Benc205f3562015-09-24 13:50:01 +02002713 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2714 struct vxlan_sock *vs = NULL;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002715
Jiri Benc205f3562015-09-24 13:50:01 +02002716 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002717 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002718 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2719 vxlan->cfg.dst_port, vxlan->flags);
2720 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002721 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002722 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002723 }
2724 spin_unlock(&vn->sock_lock);
2725 }
Jiri Benc205f3562015-09-24 13:50:01 +02002726 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002727 vs = vxlan_socket_create(vxlan->net, ipv6,
2728 vxlan->cfg.dst_port, vxlan->flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002729 if (IS_ERR(vs))
2730 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002731#if IS_ENABLED(CONFIG_IPV6)
2732 if (ipv6)
2733 vxlan->vn6_sock = vs;
2734 else
2735#endif
2736 vxlan->vn4_sock = vs;
Jiri Benc205f3562015-09-24 13:50:01 +02002737 vxlan_vs_add_dev(vs, vxlan);
2738 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002739}
2740
Jiri Bencb1be00a2015-09-24 13:50:02 +02002741static int vxlan_sock_add(struct vxlan_dev *vxlan)
2742{
2743 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2744 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
2745 int ret = 0;
2746
2747 vxlan->vn4_sock = NULL;
2748#if IS_ENABLED(CONFIG_IPV6)
2749 vxlan->vn6_sock = NULL;
2750 if (ipv6 || metadata)
2751 ret = __vxlan_sock_add(vxlan, true);
2752#endif
2753 if (!ret && (!ipv6 || metadata))
2754 ret = __vxlan_sock_add(vxlan, false);
2755 if (ret < 0)
2756 vxlan_sock_release(vxlan);
2757 return ret;
2758}
2759
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002760static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2761 struct vxlan_config *conf)
stephen hemmingerd3428942012-10-01 12:32:35 +00002762{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002763 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002764 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
Atzm Watanabec7995c42013-04-16 02:50:52 +00002765 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002766 unsigned short needed_headroom = ETH_HLEN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002767 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002768 bool use_ipv6 = false;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002769 __be16 default_port = vxlan->cfg.dst_port;
David Wragg7e059152016-02-10 00:05:58 +00002770 struct net_device *lowerdev = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00002771
Jiri Bence1e53142016-04-05 14:47:13 +02002772 if (conf->flags & VXLAN_F_GPE) {
2773 if (conf->flags & ~VXLAN_F_ALLOWED_GPE)
2774 return -EINVAL;
2775 /* For now, allow GPE only together with COLLECT_METADATA.
2776 * This can be relaxed later; in such case, the other side
2777 * of the PtP link will have to be provided.
2778 */
2779 if (!(conf->flags & VXLAN_F_COLLECT_METADATA))
2780 return -EINVAL;
2781
2782 vxlan_raw_setup(dev);
2783 } else {
2784 vxlan_ether_setup(dev);
2785 }
Jiri Benc0c867c92016-04-05 14:47:10 +02002786
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002787 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002788
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002789 dst->remote_vni = conf->vni;
2790
2791 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00002792
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002793 /* Unless IPv6 is explicitly requested, assume IPv4 */
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002794 if (!dst->remote_ip.sa.sa_family)
2795 dst->remote_ip.sa.sa_family = AF_INET;
stephen hemmingerd3428942012-10-01 12:32:35 +00002796
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002797 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
Jiri Benc057ba292015-09-17 16:11:11 +02002798 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2799 if (!IS_ENABLED(CONFIG_IPV6))
2800 return -EPFNOSUPPORT;
Cong Wange4c7ed42013-08-31 13:44:33 +08002801 use_ipv6 = true;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002802 vxlan->flags |= VXLAN_F_IPV6;
Jiri Benc057ba292015-09-17 16:11:11 +02002803 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002804
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002805 if (conf->label && !use_ipv6) {
2806 pr_info("label only supported in use with IPv6\n");
2807 return -EINVAL;
2808 }
2809
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002810 if (conf->remote_ifindex) {
David Wragg7e059152016-02-10 00:05:58 +00002811 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002812 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00002813
stephen hemminger34e02aa2012-10-09 20:35:53 +00002814 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002815 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002816 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002817 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002818
Cong Wange4c7ed42013-08-31 13:44:33 +08002819#if IS_ENABLED(CONFIG_IPV6)
2820 if (use_ipv6) {
2821 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2822 if (idev && idev->cnf.disable_ipv6) {
2823 pr_info("IPv6 is disabled via sysctl\n");
2824 return -EPERM;
2825 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002826 }
2827#endif
2828
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002829 if (!conf->mtu)
Cong Wange4c7ed42013-08-31 13:44:33 +08002830 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002831
Jiri Bencb1be00a2015-09-24 13:50:02 +02002832 needed_headroom = lowerdev->hard_header_len;
Jiri Benc9dc2ad12015-09-17 16:11:10 +02002833 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002834
David Wragg7e059152016-02-10 00:05:58 +00002835 if (conf->mtu) {
2836 err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false);
2837 if (err)
2838 return err;
2839 }
2840
Jiri Bencb1be00a2015-09-24 13:50:02 +02002841 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2842 needed_headroom += VXLAN6_HEADROOM;
2843 else
2844 needed_headroom += VXLAN_HEADROOM;
2845 dev->needed_headroom = needed_headroom;
2846
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002847 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Jiri Bence1e53142016-04-05 14:47:13 +02002848 if (!vxlan->cfg.dst_port) {
2849 if (conf->flags & VXLAN_F_GPE)
2850 vxlan->cfg.dst_port = 4790; /* IANA assigned VXLAN-GPE port */
2851 else
2852 vxlan->cfg.dst_port = default_port;
2853 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002854 vxlan->flags |= conf->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00002855
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002856 if (!vxlan->cfg.age_interval)
2857 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
Vincent Bernatafb97182012-10-30 10:27:16 +00002858
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002859 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2860 if (tmp->cfg.vni == conf->vni &&
2861 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2862 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2863 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2864 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
2865 (vxlan->flags & VXLAN_F_RCV_FLAGS))
stephen hemminger553675f2013-05-16 11:35:20 +00002866 return -EEXIST;
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002867 }
stephen hemminger553675f2013-05-16 11:35:20 +00002868
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002869 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002870
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002871 /* create an fdb entry for a valid default destination */
2872 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2873 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2874 &vxlan->default_dst.remote_ip,
2875 NUD_REACHABLE|NUD_PERMANENT,
2876 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002877 vxlan->cfg.dst_port,
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002878 vxlan->default_dst.remote_vni,
2879 vxlan->default_dst.remote_ifindex,
2880 NTF_SELF);
2881 if (err)
2882 return err;
2883 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002884
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002885 err = register_netdevice(dev);
2886 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002887 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002888 return err;
2889 }
2890
stephen hemminger553675f2013-05-16 11:35:20 +00002891 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002892
2893 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002894}
2895
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002896struct net_device *vxlan_dev_create(struct net *net, const char *name,
2897 u8 name_assign_type, struct vxlan_config *conf)
2898{
2899 struct nlattr *tb[IFLA_MAX+1];
2900 struct net_device *dev;
2901 int err;
2902
2903 memset(&tb, 0, sizeof(tb));
2904
2905 dev = rtnl_create_link(net, name, name_assign_type,
2906 &vxlan_link_ops, tb);
2907 if (IS_ERR(dev))
2908 return dev;
2909
2910 err = vxlan_dev_configure(net, dev, conf);
2911 if (err < 0) {
2912 free_netdev(dev);
2913 return ERR_PTR(err);
2914 }
2915
2916 return dev;
2917}
2918EXPORT_SYMBOL_GPL(vxlan_dev_create);
2919
2920static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2921 struct nlattr *tb[], struct nlattr *data[])
2922{
2923 struct vxlan_config conf;
2924 int err;
2925
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002926 memset(&conf, 0, sizeof(conf));
Jesse Grosse277de52015-10-16 16:36:00 -07002927
2928 if (data[IFLA_VXLAN_ID])
Jiri Benc54bfd872016-02-16 21:58:58 +01002929 conf.vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002930
2931 if (data[IFLA_VXLAN_GROUP]) {
2932 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
2933 } else if (data[IFLA_VXLAN_GROUP6]) {
2934 if (!IS_ENABLED(CONFIG_IPV6))
2935 return -EPFNOSUPPORT;
2936
2937 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
2938 conf.remote_ip.sa.sa_family = AF_INET6;
2939 }
2940
2941 if (data[IFLA_VXLAN_LOCAL]) {
2942 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
2943 conf.saddr.sa.sa_family = AF_INET;
2944 } else if (data[IFLA_VXLAN_LOCAL6]) {
2945 if (!IS_ENABLED(CONFIG_IPV6))
2946 return -EPFNOSUPPORT;
2947
2948 /* TODO: respect scope id */
2949 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
2950 conf.saddr.sa.sa_family = AF_INET6;
2951 }
2952
2953 if (data[IFLA_VXLAN_LINK])
2954 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
2955
2956 if (data[IFLA_VXLAN_TOS])
2957 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2958
2959 if (data[IFLA_VXLAN_TTL])
2960 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2961
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002962 if (data[IFLA_VXLAN_LABEL])
2963 conf.label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
2964 IPV6_FLOWLABEL_MASK;
2965
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002966 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2967 conf.flags |= VXLAN_F_LEARN;
2968
2969 if (data[IFLA_VXLAN_AGEING])
2970 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2971
2972 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2973 conf.flags |= VXLAN_F_PROXY;
2974
2975 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2976 conf.flags |= VXLAN_F_RSC;
2977
2978 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2979 conf.flags |= VXLAN_F_L2MISS;
2980
2981 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2982 conf.flags |= VXLAN_F_L3MISS;
2983
2984 if (data[IFLA_VXLAN_LIMIT])
2985 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2986
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002987 if (data[IFLA_VXLAN_COLLECT_METADATA] &&
2988 nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
2989 conf.flags |= VXLAN_F_COLLECT_METADATA;
2990
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002991 if (data[IFLA_VXLAN_PORT_RANGE]) {
2992 const struct ifla_vxlan_port_range *p
2993 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2994 conf.port_min = ntohs(p->low);
2995 conf.port_max = ntohs(p->high);
2996 }
2997
2998 if (data[IFLA_VXLAN_PORT])
2999 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3000
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003001 if (data[IFLA_VXLAN_UDP_CSUM] &&
3002 !nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3003 conf.flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003004
3005 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
3006 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3007 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3008
3009 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
3010 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3011 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3012
3013 if (data[IFLA_VXLAN_REMCSUM_TX] &&
3014 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3015 conf.flags |= VXLAN_F_REMCSUM_TX;
3016
3017 if (data[IFLA_VXLAN_REMCSUM_RX] &&
3018 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3019 conf.flags |= VXLAN_F_REMCSUM_RX;
3020
3021 if (data[IFLA_VXLAN_GBP])
3022 conf.flags |= VXLAN_F_GBP;
3023
Jiri Bence1e53142016-04-05 14:47:13 +02003024 if (data[IFLA_VXLAN_GPE])
3025 conf.flags |= VXLAN_F_GPE;
3026
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003027 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
3028 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3029
Chen Haiquance577662016-05-27 10:49:11 +08003030 if (tb[IFLA_MTU])
3031 conf.mtu = nla_get_u32(tb[IFLA_MTU]);
3032
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003033 err = vxlan_dev_configure(src_net, dev, &conf);
3034 switch (err) {
3035 case -ENODEV:
3036 pr_info("ifindex %d does not exist\n", conf.remote_ifindex);
3037 break;
3038
3039 case -EPERM:
3040 pr_info("IPv6 is disabled via sysctl\n");
3041 break;
3042
3043 case -EEXIST:
Jiri Benc54bfd872016-02-16 21:58:58 +01003044 pr_info("duplicate VNI %u\n", be32_to_cpu(conf.vni));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003045 break;
Jiri Bence1e53142016-04-05 14:47:13 +02003046
3047 case -EINVAL:
3048 pr_info("unsupported combination of extensions\n");
3049 break;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003050 }
3051
3052 return err;
3053}
3054
stephen hemmingerd3428942012-10-01 12:32:35 +00003055static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3056{
3057 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003058 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00003059
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003060 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003061 if (!hlist_unhashed(&vxlan->hlist))
3062 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003063 spin_unlock(&vn->sock_lock);
3064
Tom Herbert58ce31c2015-08-19 17:07:33 -07003065 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003066 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003067 unregister_netdevice_queue(dev, head);
3068}
3069
3070static size_t vxlan_get_size(const struct net_device *dev)
3071{
3072
3073 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003074 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003075 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003076 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003077 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3078 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003079 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003080 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003081 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3082 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3083 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3084 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003085 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003086 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3087 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003088 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003089 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3090 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3091 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3092 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003093 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3094 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003095 0;
3096}
3097
3098static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3099{
3100 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003101 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003102 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003103 .low = htons(vxlan->cfg.port_min),
3104 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003105 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003106
Jiri Benc54bfd872016-02-16 21:58:58 +01003107 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003108 goto nla_put_failure;
3109
Cong Wange4c7ed42013-08-31 13:44:33 +08003110 if (!vxlan_addr_any(&dst->remote_ip)) {
3111 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003112 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3113 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003114 goto nla_put_failure;
3115#if IS_ENABLED(CONFIG_IPV6)
3116 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003117 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3118 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003119 goto nla_put_failure;
3120#endif
3121 }
3122 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003123
Atzm Watanabec7995c42013-04-16 02:50:52 +00003124 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003125 goto nla_put_failure;
3126
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003127 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3128 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003129 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003130 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003131 goto nla_put_failure;
3132#if IS_ENABLED(CONFIG_IPV6)
3133 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003134 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003135 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003136 goto nla_put_failure;
3137#endif
3138 }
3139 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003140
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003141 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3142 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003143 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003144 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3145 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3146 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3147 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3148 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3149 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3150 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3151 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3152 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003153 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3154 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003155 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3156 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3157 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003158 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003159 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003160 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3161 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3162 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08003163 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3164 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3165 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3166 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3167 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003168 goto nla_put_failure;
3169
stephen hemminger05f47d62012-10-09 20:35:50 +00003170 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3171 goto nla_put_failure;
3172
Thomas Graf35114942015-01-15 03:53:55 +01003173 if (vxlan->flags & VXLAN_F_GBP &&
3174 nla_put_flag(skb, IFLA_VXLAN_GBP))
3175 goto nla_put_failure;
3176
Jiri Bence1e53142016-04-05 14:47:13 +02003177 if (vxlan->flags & VXLAN_F_GPE &&
3178 nla_put_flag(skb, IFLA_VXLAN_GPE))
3179 goto nla_put_failure;
3180
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003181 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3182 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3183 goto nla_put_failure;
3184
stephen hemmingerd3428942012-10-01 12:32:35 +00003185 return 0;
3186
3187nla_put_failure:
3188 return -EMSGSIZE;
3189}
3190
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003191static struct net *vxlan_get_link_net(const struct net_device *dev)
3192{
3193 struct vxlan_dev *vxlan = netdev_priv(dev);
3194
3195 return vxlan->net;
3196}
3197
stephen hemmingerd3428942012-10-01 12:32:35 +00003198static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3199 .kind = "vxlan",
3200 .maxtype = IFLA_VXLAN_MAX,
3201 .policy = vxlan_policy,
3202 .priv_size = sizeof(struct vxlan_dev),
3203 .setup = vxlan_setup,
3204 .validate = vxlan_validate,
3205 .newlink = vxlan_newlink,
3206 .dellink = vxlan_dellink,
3207 .get_size = vxlan_get_size,
3208 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003209 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003210};
3211
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003212static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3213 struct net_device *dev)
3214{
3215 struct vxlan_dev *vxlan, *next;
3216 LIST_HEAD(list_kill);
3217
3218 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3219 struct vxlan_rdst *dst = &vxlan->default_dst;
3220
3221 /* In case we created vxlan device with carrier
3222 * and we loose the carrier due to module unload
3223 * we also need to remove vxlan device. In other
3224 * cases, it's not necessary and remote_ifindex
3225 * is 0 here, so no matches.
3226 */
3227 if (dst->remote_ifindex == dev->ifindex)
3228 vxlan_dellink(vxlan->dev, &list_kill);
3229 }
3230
3231 unregister_netdevice_many(&list_kill);
3232}
3233
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003234static int vxlan_netdevice_event(struct notifier_block *unused,
3235 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003236{
3237 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003238 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003239
Daniel Borkmann783c1462014-01-22 21:07:53 +01003240 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003241 vxlan_handle_lowerdev_unregister(vn, dev);
Alexander Duyck7c46a642016-06-16 12:21:00 -07003242 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003243 vxlan_push_rx_ports(dev);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003244
3245 return NOTIFY_DONE;
3246}
3247
3248static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003249 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003250};
3251
stephen hemmingerd3428942012-10-01 12:32:35 +00003252static __net_init int vxlan_init_net(struct net *net)
3253{
3254 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003255 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003256
stephen hemminger553675f2013-05-16 11:35:20 +00003257 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003258 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003259
stephen hemminger553675f2013-05-16 11:35:20 +00003260 for (h = 0; h < PORT_HASH_SIZE; ++h)
3261 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003262
3263 return 0;
3264}
3265
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003266static void __net_exit vxlan_exit_net(struct net *net)
3267{
3268 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3269 struct vxlan_dev *vxlan, *next;
3270 struct net_device *dev, *aux;
3271 LIST_HEAD(list);
3272
3273 rtnl_lock();
3274 for_each_netdev_safe(net, dev, aux)
3275 if (dev->rtnl_link_ops == &vxlan_link_ops)
3276 unregister_netdevice_queue(dev, &list);
3277
3278 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3279 /* If vxlan->dev is in the same netns, it has already been added
3280 * to the list by the previous loop.
3281 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003282 if (!net_eq(dev_net(vxlan->dev), net)) {
3283 gro_cells_destroy(&vxlan->gro_cells);
John W. Linville13c3ed62015-05-18 13:51:24 -04003284 unregister_netdevice_queue(vxlan->dev, &list);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003285 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003286 }
3287
3288 unregister_netdevice_many(&list);
3289 rtnl_unlock();
3290}
3291
stephen hemmingerd3428942012-10-01 12:32:35 +00003292static struct pernet_operations vxlan_net_ops = {
3293 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003294 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003295 .id = &vxlan_net_id,
3296 .size = sizeof(struct vxlan_net),
3297};
3298
3299static int __init vxlan_init_module(void)
3300{
3301 int rc;
3302
3303 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3304
Daniel Borkmann783c1462014-01-22 21:07:53 +01003305 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003306 if (rc)
3307 goto out1;
3308
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003309 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003310 if (rc)
3311 goto out2;
3312
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003313 rc = rtnl_link_register(&vxlan_link_ops);
3314 if (rc)
3315 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003316
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003317 return 0;
3318out3:
3319 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003320out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003321 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003322out1:
3323 return rc;
3324}
Cong Wang7332a132013-05-27 22:35:53 +00003325late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003326
3327static void __exit vxlan_cleanup_module(void)
3328{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003329 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003330 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003331 unregister_pernet_subsys(&vxlan_net_ops);
3332 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003333}
3334module_exit(vxlan_cleanup_module);
3335
3336MODULE_LICENSE("GPL");
3337MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003338MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003339MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003340MODULE_ALIAS_RTNL_LINK("vxlan");