blob: 19383371a27d0b689275269a92ba2c991308fc39 [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>
14#include <linux/types.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/rculist.h>
20#include <linux/netdevice.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/udp.h>
24#include <linux/igmp.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
Pravin B Shelar1eaa8172013-08-19 11:23:29 -070027#include <linux/if_vlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000028#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000029#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000030#include <net/arp.h>
31#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000032#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000033#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/icmp.h>
35#include <net/udp.h>
Tom Herbert3ee64f32014-07-13 19:49:42 -070036#include <net/udp_tunnel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000037#include <net/rtnetlink.h>
38#include <net/route.h>
39#include <net/dsfield.h>
40#include <net/inet_ecn.h>
41#include <net/net_namespace.h>
42#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070043#include <net/vxlan.h>
Or Gerlitzdc01e7d2014-01-20 13:59:21 +020044#include <net/protocol.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080045
Cong Wange4c7ed42013-08-31 13:44:33 +080046#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ipv6.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080050#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080051#endif
Thomas Grafee122c72015-07-21 10:43:58 +020052#include <net/dst_metadata.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000053
54#define VXLAN_VERSION "0.1"
55
stephen hemminger553675f2013-05-16 11:35:20 +000056#define PORT_HASH_BITS 8
57#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000058#define FDB_AGE_DEFAULT 300 /* 5 min */
59#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
60
stephen hemminger23c578b2013-04-27 11:31:53 +000061/* UDP port for VXLAN traffic.
62 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070063 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000064 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070065static unsigned short vxlan_port __read_mostly = 8472;
66module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000067MODULE_PARM_DESC(udp_port, "Destination UDP port");
68
69static bool log_ecn_error = true;
70module_param(log_ecn_error, bool, 0644);
71MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
72
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070073static int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020074static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000075
Li RongQing7256eac2016-01-29 09:43:47 +080076static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030077
Jiri Benc205f3562015-09-24 13:50:01 +020078static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020079
stephen hemminger553675f2013-05-16 11:35:20 +000080/* per-network namespace private data for this module */
81struct vxlan_net {
82 struct list_head vxlan_list;
83 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070084 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000085};
86
stephen hemmingerd3428942012-10-01 12:32:35 +000087/* Forwarding table entry */
88struct vxlan_fdb {
89 struct hlist_node hlist; /* linked list of entries */
90 struct rcu_head rcu;
91 unsigned long updated; /* jiffies */
92 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070093 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020094 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000095 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +000096 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +000097};
98
stephen hemmingerd3428942012-10-01 12:32:35 +000099/* salt for hash table */
100static u32 vxlan_salt __read_mostly;
101
Thomas Grafee122c72015-07-21 10:43:58 +0200102static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
103{
Thomas Grafe7030872015-07-21 10:44:01 +0200104 return vs->flags & VXLAN_F_COLLECT_METADATA ||
105 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +0200106}
107
Cong Wange4c7ed42013-08-31 13:44:33 +0800108#if IS_ENABLED(CONFIG_IPV6)
109static inline
110bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
111{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200112 if (a->sa.sa_family != b->sa.sa_family)
113 return false;
114 if (a->sa.sa_family == AF_INET6)
115 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
116 else
117 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800118}
119
120static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
121{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200122 if (ipa->sa.sa_family == AF_INET6)
123 return ipv6_addr_any(&ipa->sin6.sin6_addr);
124 else
125 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800126}
127
128static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
129{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200130 if (ipa->sa.sa_family == AF_INET6)
131 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
132 else
133 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800134}
135
136static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
137{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200138 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200139 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200140 ip->sa.sa_family = AF_INET6;
141 return 0;
142 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200143 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200144 ip->sa.sa_family = AF_INET;
145 return 0;
146 } else {
147 return -EAFNOSUPPORT;
148 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800149}
150
151static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200152 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800153{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200154 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200155 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200156 else
Jiri Benc930345e2015-03-29 16:59:25 +0200157 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800158}
159
160#else /* !CONFIG_IPV6 */
161
162static inline
163bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
164{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200165 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800166}
167
168static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
169{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200170 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800171}
172
173static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
174{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200175 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800176}
177
178static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
179{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200180 if (nla_len(nla) >= sizeof(struct in6_addr)) {
181 return -EAFNOSUPPORT;
182 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200183 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200184 ip->sa.sa_family = AF_INET;
185 return 0;
186 } else {
187 return -EAFNOSUPPORT;
188 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800189}
190
191static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200192 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800193{
Jiri Benc930345e2015-03-29 16:59:25 +0200194 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800195}
196#endif
197
stephen hemminger553675f2013-05-16 11:35:20 +0000198/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100199static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000200{
Jiri Benc54bfd872016-02-16 21:58:58 +0100201 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000202}
203
204/* Socket hash table head */
205static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000206{
207 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
208
stephen hemminger553675f2013-05-16 11:35:20 +0000209 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
210}
211
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700212/* First remote destination for a forwarding entry.
213 * Guaranteed to be non-NULL because remotes are never deleted.
214 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700215static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700216{
stephen hemminger5ca54612013-08-04 17:17:39 -0700217 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
218}
219
220static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
221{
222 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700223}
224
Thomas Grafac5132d2015-01-15 03:53:56 +0100225/* Find VXLAN socket based on network namespace, address family and UDP port
226 * and enabled unshareable flags.
227 */
228static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
229 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000230{
231 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800232
233 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000234
235 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200236 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200237 vxlan_get_sk_family(vs) == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800238 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000239 return vs;
240 }
241 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000242}
243
Jiri Benc54bfd872016-02-16 21:58:58 +0100244static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000245{
246 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000247
Jiri Bencb9167b22016-02-16 21:59:03 +0100248 /* For flow based devices, map all packets to VNI 0 */
249 if (vs->flags & VXLAN_F_COLLECT_METADATA)
250 vni = 0;
251
Jiri Benc54bfd872016-02-16 21:58:58 +0100252 hlist_for_each_entry_rcu(vxlan, vni_head(vs, vni), hlist) {
253 if (vxlan->default_dst.remote_vni == vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000254 return vxlan;
255 }
256
257 return NULL;
258}
259
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700260/* Look up VNI in a per net namespace table */
Jiri Benc54bfd872016-02-16 21:58:58 +0100261static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni,
Thomas Grafac5132d2015-01-15 03:53:56 +0100262 sa_family_t family, __be16 port,
263 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700264{
265 struct vxlan_sock *vs;
266
Thomas Grafac5132d2015-01-15 03:53:56 +0100267 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700268 if (!vs)
269 return NULL;
270
Jiri Benc54bfd872016-02-16 21:58:58 +0100271 return vxlan_vs_find_vni(vs, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700272}
273
stephen hemmingerd3428942012-10-01 12:32:35 +0000274/* Fill in neighbour message in skbuff. */
275static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700276 const struct vxlan_fdb *fdb,
277 u32 portid, u32 seq, int type, unsigned int flags,
278 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000279{
280 unsigned long now = jiffies;
281 struct nda_cacheinfo ci;
282 struct nlmsghdr *nlh;
283 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000284 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000285
286 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
287 if (nlh == NULL)
288 return -EMSGSIZE;
289
290 ndm = nlmsg_data(nlh);
291 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000292
293 send_eth = send_ip = true;
294
295 if (type == RTM_GETNEIGH) {
296 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800297 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000298 send_eth = !is_zero_ether_addr(fdb->eth_addr);
299 } else
300 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000301 ndm->ndm_state = fdb->state;
302 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000303 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800304 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000305
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100306 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100307 nla_put_s32(skb, NDA_LINK_NETNSID,
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +0200308 peernet2id_alloc(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100309 goto nla_put_failure;
310
David Stevense4f67ad2012-11-20 02:50:14 +0000311 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000312 goto nla_put_failure;
313
Cong Wange4c7ed42013-08-31 13:44:33 +0800314 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000315 goto nla_put_failure;
316
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200317 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000318 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
319 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000320 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100321 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000322 goto nla_put_failure;
323 if (rdst->remote_ifindex &&
324 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000325 goto nla_put_failure;
326
327 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
328 ci.ndm_confirmed = 0;
329 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
330 ci.ndm_refcnt = 0;
331
332 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
333 goto nla_put_failure;
334
Johannes Berg053c0952015-01-16 22:09:00 +0100335 nlmsg_end(skb, nlh);
336 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000337
338nla_put_failure:
339 nlmsg_cancel(skb, nlh);
340 return -EMSGSIZE;
341}
342
343static inline size_t vxlan_nlmsg_size(void)
344{
345 return NLMSG_ALIGN(sizeof(struct ndmsg))
346 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800347 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000348 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000349 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
350 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100351 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000352 + nla_total_size(sizeof(struct nda_cacheinfo));
353}
354
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200355static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
356 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000357{
358 struct net *net = dev_net(vxlan->dev);
359 struct sk_buff *skb;
360 int err = -ENOBUFS;
361
362 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
363 if (skb == NULL)
364 goto errout;
365
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200366 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000367 if (err < 0) {
368 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
369 WARN_ON(err == -EMSGSIZE);
370 kfree_skb(skb);
371 goto errout;
372 }
373
374 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
375 return;
376errout:
377 if (err < 0)
378 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
379}
380
Cong Wange4c7ed42013-08-31 13:44:33 +0800381static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000382{
383 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700384 struct vxlan_fdb f = {
385 .state = NUD_STALE,
386 };
387 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800388 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100389 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700390 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700391
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200392 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000393}
394
395static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
396{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700397 struct vxlan_fdb f = {
398 .state = NUD_STALE,
399 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200400 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000401
David Stevense4f67ad2012-11-20 02:50:14 +0000402 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
403
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200404 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000405}
406
stephen hemmingerd3428942012-10-01 12:32:35 +0000407/* Hash Ethernet address */
408static u32 eth_hash(const unsigned char *addr)
409{
410 u64 value = get_unaligned((u64 *)addr);
411
412 /* only want 6 bytes */
413#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000414 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000415#else
416 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000417#endif
418 return hash_64(value, FDB_HASH_BITS);
419}
420
421/* Hash chain to use given mac address */
422static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
423 const u8 *mac)
424{
425 return &vxlan->fdb_head[eth_hash(mac)];
426}
427
428/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000429static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000430 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000431{
432 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
433 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000434
Sasha Levinb67bfe02013-02-27 17:06:00 -0800435 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700436 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000437 return f;
438 }
439
440 return NULL;
441}
442
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000443static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
444 const u8 *mac)
445{
446 struct vxlan_fdb *f;
447
448 f = __vxlan_find_mac(vxlan, mac);
449 if (f)
450 f->used = jiffies;
451
452 return f;
453}
454
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300455/* caller should hold vxlan->hash_lock */
456static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800457 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100458 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300459{
460 struct vxlan_rdst *rd;
461
462 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800463 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300464 rd->remote_port == port &&
465 rd->remote_vni == vni &&
466 rd->remote_ifindex == ifindex)
467 return rd;
468 }
469
470 return NULL;
471}
472
Thomas Richter906dc182013-07-19 17:20:07 +0200473/* Replace destination of unicast mac */
474static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100475 union vxlan_addr *ip, __be16 port, __be32 vni,
476 __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200477{
478 struct vxlan_rdst *rd;
479
480 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
481 if (rd)
482 return 0;
483
484 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
485 if (!rd)
486 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100487
488 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800489 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200490 rd->remote_port = port;
491 rd->remote_vni = vni;
492 rd->remote_ifindex = ifindex;
493 return 1;
494}
495
David Stevens66817122013-03-15 04:35:51 +0000496/* Add/update destinations for multicast */
497static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100498 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200499 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000500{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700501 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000502
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300503 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
504 if (rd)
505 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700506
David Stevens66817122013-03-15 04:35:51 +0000507 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
508 if (rd == NULL)
509 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100510
511 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
512 kfree(rd);
513 return -ENOBUFS;
514 }
515
Cong Wange4c7ed42013-08-31 13:44:33 +0800516 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000517 rd->remote_port = port;
518 rd->remote_vni = vni;
519 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700520
521 list_add_tail_rcu(&rd->list, &f->remotes);
522
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200523 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000524 return 1;
525}
526
Tom Herbertdfd86452015-01-12 17:00:38 -0800527static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
528 unsigned int off,
529 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100530 __be32 vni_field,
531 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800532 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800533{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700534 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800535
536 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700537 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800538
539 if (!NAPI_GRO_CB(skb)->csum_valid)
540 return NULL;
541
Jiri Benc54bfd872016-02-16 21:58:58 +0100542 start = vxlan_rco_start(vni_field);
543 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800544
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700545 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
546 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800547
548 skb->remcsum_offload = 1;
549
550 return vh;
551}
552
Tom Herbert5602c482016-04-05 08:22:53 -0700553static struct sk_buff **vxlan_gro_receive(struct sock *sk,
554 struct sk_buff **head,
555 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200556{
557 struct sk_buff *p, **pp = NULL;
558 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800559 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200560 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700561 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100562 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800563 struct gro_remcsum grc;
564
565 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200566
567 off_vx = skb_gro_offset(skb);
568 hlen = off_vx + sizeof(*vh);
569 vh = skb_gro_header_fast(skb, off_vx);
570 if (skb_gro_header_hard(skb, hlen)) {
571 vh = skb_gro_header_slow(skb, hlen, off_vx);
572 if (unlikely(!vh))
573 goto out;
574 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200575
Tom Herbertdfd86452015-01-12 17:00:38 -0800576 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
577
Jiri Benc54bfd872016-02-16 21:58:58 +0100578 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800579
580 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
581 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100582 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800583 !!(vs->flags &
584 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800585
586 if (!vh)
587 goto out;
588 }
589
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700590 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
591
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200592 for (p = *head; p; p = p->next) {
593 if (!NAPI_GRO_CB(p)->same_flow)
594 continue;
595
596 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100597 if (vh->vx_flags != vh2->vx_flags ||
598 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200599 NAPI_GRO_CB(p)->same_flow = 0;
600 continue;
601 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200602 }
603
Jesse Gross9b174d82014-12-30 19:10:15 -0800604 pp = eth_gro_receive(head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800605 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200606
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200607out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800608 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200609 NAPI_GRO_CB(skb)->flush |= flush;
610
611 return pp;
612}
613
Tom Herbert5602c482016-04-05 08:22:53 -0700614static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200615{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800616 udp_tunnel_gro_complete(skb, nhoff);
617
Jesse Gross9b174d82014-12-30 19:10:15 -0800618 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200619}
620
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700621/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200622static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700623{
624 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200625 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700626 struct net *net = sock_net(sk);
Jiri Benc705cc622015-08-20 13:56:28 +0200627 sa_family_t sa_family = vxlan_get_sk_family(vs);
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700628 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700629
630 rcu_read_lock();
631 for_each_netdev_rcu(net, dev) {
632 if (dev->netdev_ops->ndo_add_vxlan_port)
633 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
634 port);
635 }
636 rcu_read_unlock();
637}
638
639/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200640static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700641{
642 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200643 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700644 struct net *net = sock_net(sk);
Jiri Benc705cc622015-08-20 13:56:28 +0200645 sa_family_t sa_family = vxlan_get_sk_family(vs);
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700646 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700647
648 rcu_read_lock();
649 for_each_netdev_rcu(net, dev) {
650 if (dev->netdev_ops->ndo_del_vxlan_port)
651 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
652 port);
653 }
654 rcu_read_unlock();
655}
656
stephen hemmingerd3428942012-10-01 12:32:35 +0000657/* Add new entry to forwarding table -- assumes lock held */
658static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800659 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000660 __u16 state, __u16 flags,
Jiri Benc54bfd872016-02-16 21:58:58 +0100661 __be16 port, __be32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000662 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000663{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200664 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000665 struct vxlan_fdb *f;
666 int notify = 0;
667
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000668 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000669 if (f) {
670 if (flags & NLM_F_EXCL) {
671 netdev_dbg(vxlan->dev,
672 "lost race to create %pM\n", mac);
673 return -EEXIST;
674 }
675 if (f->state != state) {
676 f->state = state;
677 f->updated = jiffies;
678 notify = 1;
679 }
David Stevensae884082013-04-19 00:36:26 +0000680 if (f->flags != ndm_flags) {
681 f->flags = ndm_flags;
682 f->updated = jiffies;
683 notify = 1;
684 }
Thomas Richter906dc182013-07-19 17:20:07 +0200685 if ((flags & NLM_F_REPLACE)) {
686 /* Only change unicasts */
687 if (!(is_multicast_ether_addr(f->eth_addr) ||
688 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800689 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200690 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200691 } else
692 return -EOPNOTSUPP;
693 }
David Stevens66817122013-03-15 04:35:51 +0000694 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300695 (is_multicast_ether_addr(f->eth_addr) ||
696 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200697 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
698 &rd);
David Stevens66817122013-03-15 04:35:51 +0000699
700 if (rc < 0)
701 return rc;
702 notify |= rc;
703 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000704 } else {
705 if (!(flags & NLM_F_CREATE))
706 return -ENOENT;
707
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200708 if (vxlan->cfg.addrmax &&
709 vxlan->addrcnt >= vxlan->cfg.addrmax)
stephen hemmingerd3428942012-10-01 12:32:35 +0000710 return -ENOSPC;
711
Thomas Richter906dc182013-07-19 17:20:07 +0200712 /* Disallow replace to add a multicast entry */
713 if ((flags & NLM_F_REPLACE) &&
714 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
715 return -EOPNOTSUPP;
716
Cong Wange4c7ed42013-08-31 13:44:33 +0800717 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000718 f = kmalloc(sizeof(*f), GFP_ATOMIC);
719 if (!f)
720 return -ENOMEM;
721
722 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000723 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000724 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000725 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700726 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000727 memcpy(f->eth_addr, mac, ETH_ALEN);
728
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200729 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700730
stephen hemmingerd3428942012-10-01 12:32:35 +0000731 ++vxlan->addrcnt;
732 hlist_add_head_rcu(&f->hlist,
733 vxlan_fdb_head(vxlan, mac));
734 }
735
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200736 if (notify) {
737 if (rd == NULL)
738 rd = first_remote_rtnl(f);
739 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
740 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000741
742 return 0;
743}
744
Wei Yongjun6706c822013-04-11 19:00:35 +0000745static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000746{
747 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700748 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000749
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100750 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
751 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000752 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100753 }
David Stevens66817122013-03-15 04:35:51 +0000754 kfree(f);
755}
756
stephen hemmingerd3428942012-10-01 12:32:35 +0000757static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
758{
759 netdev_dbg(vxlan->dev,
760 "delete %pM\n", f->eth_addr);
761
762 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200763 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000764
765 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000766 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000767}
768
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300769static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Jiri Benc54bfd872016-02-16 21:58:58 +0100770 union vxlan_addr *ip, __be16 *port, __be32 *vni,
771 u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300772{
773 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800774 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300775
776 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800777 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
778 if (err)
779 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300780 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800781 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
782 if (remote->sa.sa_family == AF_INET) {
783 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
784 ip->sa.sa_family = AF_INET;
785#if IS_ENABLED(CONFIG_IPV6)
786 } else {
787 ip->sin6.sin6_addr = in6addr_any;
788 ip->sa.sa_family = AF_INET6;
789#endif
790 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300791 }
792
793 if (tb[NDA_PORT]) {
794 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
795 return -EINVAL;
796 *port = nla_get_be16(tb[NDA_PORT]);
797 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200798 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300799 }
800
801 if (tb[NDA_VNI]) {
802 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
803 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100804 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300805 } else {
806 *vni = vxlan->default_dst.remote_vni;
807 }
808
809 if (tb[NDA_IFINDEX]) {
810 struct net_device *tdev;
811
812 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
813 return -EINVAL;
814 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800815 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300816 if (!tdev)
817 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300818 } else {
819 *ifindex = 0;
820 }
821
822 return 0;
823}
824
stephen hemmingerd3428942012-10-01 12:32:35 +0000825/* Add static entry (via netlink) */
826static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
827 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100828 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000829{
830 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300831 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800832 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000833 __be16 port;
Jiri Benc54bfd872016-02-16 21:58:58 +0100834 __be32 vni;
835 u32 ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000836 int err;
837
838 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
839 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
840 ndm->ndm_state);
841 return -EINVAL;
842 }
843
844 if (tb[NDA_DST] == NULL)
845 return -EINVAL;
846
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300847 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
848 if (err)
849 return err;
David Stevens66817122013-03-15 04:35:51 +0000850
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300851 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
852 return -EAFNOSUPPORT;
853
stephen hemmingerd3428942012-10-01 12:32:35 +0000854 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800855 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000856 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000857 spin_unlock_bh(&vxlan->hash_lock);
858
859 return err;
860}
861
862/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000863static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
864 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100865 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000866{
867 struct vxlan_dev *vxlan = netdev_priv(dev);
868 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300869 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800870 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300871 __be16 port;
Jiri Benc54bfd872016-02-16 21:58:58 +0100872 __be32 vni;
873 u32 ifindex;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300874 int err;
875
876 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
877 if (err)
878 return err;
879
880 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000881
882 spin_lock_bh(&vxlan->hash_lock);
883 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300884 if (!f)
885 goto out;
886
Cong Wange4c7ed42013-08-31 13:44:33 +0800887 if (!vxlan_addr_any(&ip)) {
888 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300889 if (!rd)
890 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000891 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300892
893 err = 0;
894
895 /* remove a destination if it's not the only one on the list,
896 * otherwise destroy the fdb entry
897 */
898 if (rd && !list_is_singular(&f->remotes)) {
899 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200900 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800901 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300902 goto out;
903 }
904
905 vxlan_fdb_destroy(vxlan, f);
906
907out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000908 spin_unlock_bh(&vxlan->hash_lock);
909
910 return err;
911}
912
913/* Dump forwarding table */
914static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400915 struct net_device *dev,
916 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000917{
918 struct vxlan_dev *vxlan = netdev_priv(dev);
919 unsigned int h;
920
921 for (h = 0; h < FDB_HASH_SIZE; ++h) {
922 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000923 int err;
924
Sasha Levinb67bfe02013-02-27 17:06:00 -0800925 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000926 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000927
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700928 list_for_each_entry_rcu(rd, &f->remotes, list) {
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900929 if (idx < cb->args[0])
930 goto skip;
931
David Stevens66817122013-03-15 04:35:51 +0000932 err = vxlan_fdb_info(skb, vxlan, f,
933 NETLINK_CB(cb->skb).portid,
934 cb->nlh->nlmsg_seq,
935 RTM_NEWNEIGH,
936 NLM_F_MULTI, rd);
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +0900937 if (err < 0) {
938 cb->args[1] = err;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700939 goto out;
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +0900940 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700941skip:
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900942 ++idx;
943 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000944 }
945 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700946out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000947 return idx;
948}
949
950/* Watch incoming packets to learn mapping between Ethernet address
951 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900952 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000953 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700954static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800955 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000956{
957 struct vxlan_dev *vxlan = netdev_priv(dev);
958 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000959
960 f = vxlan_find_mac(vxlan, src_mac);
961 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700962 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700963
Cong Wange4c7ed42013-08-31 13:44:33 +0800964 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700965 return false;
966
967 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700968 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700969 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000970
971 if (net_ratelimit())
972 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800973 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +0100974 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +0000975
Cong Wange4c7ed42013-08-31 13:44:33 +0800976 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000977 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200978 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000979 } else {
980 /* learned new entry */
981 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700982
983 /* close off race between vxlan_flush and incoming packets */
984 if (netif_running(dev))
985 vxlan_fdb_create(vxlan, src_mac, src_ip,
986 NUD_REACHABLE,
987 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200988 vxlan->cfg.dst_port,
stephen hemminger3bf74b12013-06-17 12:09:57 -0700989 vxlan->default_dst.remote_vni,
990 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000991 spin_unlock(&vxlan->hash_lock);
992 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700993
994 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000995}
996
stephen hemmingerd3428942012-10-01 12:32:35 +0000997/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +0800998static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +0000999{
stephen hemminger553675f2013-05-16 11:35:20 +00001000 struct vxlan_dev *vxlan;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001001 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001002
Gao feng95ab0992013-12-10 16:37:33 +08001003 /* The vxlan_sock is only used by dev, leaving group has
1004 * no effect on other vxlan devices.
1005 */
Jiri Bencb1be00a2015-09-24 13:50:02 +02001006 if (family == AF_INET && dev->vn4_sock &&
1007 atomic_read(&dev->vn4_sock->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001008 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001009#if IS_ENABLED(CONFIG_IPV6)
1010 if (family == AF_INET6 && dev->vn6_sock &&
1011 atomic_read(&dev->vn6_sock->refcnt) == 1)
1012 return false;
1013#endif
Gao feng95ab0992013-12-10 16:37:33 +08001014
stephen hemminger553675f2013-05-16 11:35:20 +00001015 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001016 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001017 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001018
Jiri Bencb1be00a2015-09-24 13:50:02 +02001019 if (family == AF_INET && vxlan->vn4_sock != dev->vn4_sock)
Gao feng95ab0992013-12-10 16:37:33 +08001020 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001021#if IS_ENABLED(CONFIG_IPV6)
1022 if (family == AF_INET6 && vxlan->vn6_sock != dev->vn6_sock)
1023 continue;
1024#endif
Gao feng95ab0992013-12-10 16:37:33 +08001025
1026 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1027 &dev->default_dst.remote_ip))
1028 continue;
1029
1030 if (vxlan->default_dst.remote_ifindex !=
1031 dev->default_dst.remote_ifindex)
1032 continue;
1033
1034 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001035 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001036
1037 return false;
1038}
1039
Jiri Bencb1be00a2015-09-24 13:50:02 +02001040static void __vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001041{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001042 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001043
Jiri Bencb1be00a2015-09-24 13:50:02 +02001044 if (!vs)
1045 return;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001046 if (!atomic_dec_and_test(&vs->refcnt))
1047 return;
1048
Jiri Bencb1be00a2015-09-24 13:50:02 +02001049 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001050 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001051 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001052 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001053 spin_unlock(&vn->sock_lock);
1054
Hannes Frederic Sowa0412bd92016-04-08 22:55:01 +02001055 synchronize_net();
1056 udp_tunnel_sock_release(vs->sock);
1057 kfree(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001058}
1059
Jiri Bencb1be00a2015-09-24 13:50:02 +02001060static void vxlan_sock_release(struct vxlan_dev *vxlan)
1061{
1062 __vxlan_sock_release(vxlan->vn4_sock);
1063#if IS_ENABLED(CONFIG_IPV6)
1064 __vxlan_sock_release(vxlan->vn6_sock);
1065#endif
1066}
1067
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001068/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001069 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001070 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001071static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001072{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001073 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001074 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1075 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001076 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001077
Cong Wange4c7ed42013-08-31 13:44:33 +08001078 if (ip->sa.sa_family == AF_INET) {
1079 struct ip_mreqn mreq = {
1080 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1081 .imr_ifindex = ifindex,
1082 };
1083
Jiri Bencb1be00a2015-09-24 13:50:02 +02001084 sk = vxlan->vn4_sock->sock->sk;
1085 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001086 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001087 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001088#if IS_ENABLED(CONFIG_IPV6)
1089 } else {
Jiri Bencb1be00a2015-09-24 13:50:02 +02001090 sk = vxlan->vn6_sock->sock->sk;
1091 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001092 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1093 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001094 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001095#endif
1096 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001097
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001098 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001099}
1100
1101/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001102static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001103{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001104 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001105 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1106 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001107 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001108
Cong Wange4c7ed42013-08-31 13:44:33 +08001109 if (ip->sa.sa_family == AF_INET) {
1110 struct ip_mreqn mreq = {
1111 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1112 .imr_ifindex = ifindex,
1113 };
1114
Jiri Bencb1be00a2015-09-24 13:50:02 +02001115 sk = vxlan->vn4_sock->sock->sk;
1116 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001117 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001118 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001119#if IS_ENABLED(CONFIG_IPV6)
1120 } else {
Jiri Bencb1be00a2015-09-24 13:50:02 +02001121 sk = vxlan->vn6_sock->sock->sk;
1122 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001123 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1124 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001125 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001126#endif
1127 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001128
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001129 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001130}
1131
Jiri Bencf14eceb2016-02-16 21:59:01 +01001132static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1133 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001134{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001135 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001136
Jiri Bencf14eceb2016-02-16 21:59:01 +01001137 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1138 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001139
Jiri Bencf14eceb2016-02-16 21:59:01 +01001140 start = vxlan_rco_start(unparsed->vx_vni);
1141 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001142
Jiri Benc7d34fa72016-03-21 17:50:05 +01001143 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001144 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001145
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001146 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1147 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001148out:
1149 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1150 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001151 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001152}
1153
Jiri Bencf14eceb2016-02-16 21:59:01 +01001154static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001155 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001156 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001157{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001158 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001159 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001160
Jiri Bencf14eceb2016-02-16 21:59:01 +01001161 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1162 goto out;
1163
Jiri Benc3288af02016-02-16 21:59:00 +01001164 md->gbp = ntohs(gbp->policy_id);
1165
Jiri Benc10a5af22016-02-23 18:02:59 +01001166 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001167 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001168 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001169 tun_dst->u.tun_info.options_len = sizeof(*md);
1170 }
Jiri Benc3288af02016-02-16 21:59:00 +01001171 if (gbp->dont_learn)
1172 md->gbp |= VXLAN_GBP_DONT_LEARN;
1173
1174 if (gbp->policy_applied)
1175 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001176
Jiri Benc64f87d32016-02-23 18:02:55 +01001177 /* In flow-based mode, GBP is carried in dst_metadata */
1178 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1179 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001180out:
1181 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001182}
1183
Jiri Bence1e53142016-04-05 14:47:13 +02001184static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001185 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001186 struct sk_buff *skb, u32 vxflags)
1187{
1188 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1189
1190 /* Need to have Next Protocol set for interfaces in GPE mode. */
1191 if (!gpe->np_applied)
1192 return false;
1193 /* "The initial version is 0. If a receiver does not support the
1194 * version indicated it MUST drop the packet.
1195 */
1196 if (gpe->version != 0)
1197 return false;
1198 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1199 * processing MUST occur." However, we don't implement OAM
1200 * processing, thus drop the packet.
1201 */
1202 if (gpe->oam_flag)
1203 return false;
1204
1205 switch (gpe->next_protocol) {
1206 case VXLAN_GPE_NP_IPV4:
1207 *protocol = htons(ETH_P_IP);
1208 break;
1209 case VXLAN_GPE_NP_IPV6:
1210 *protocol = htons(ETH_P_IPV6);
1211 break;
1212 case VXLAN_GPE_NP_ETHERNET:
1213 *protocol = htons(ETH_P_TEB);
1214 break;
1215 default:
1216 return false;
1217 }
1218
1219 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1220 return true;
1221}
1222
Jiri Benc1ab016e2016-02-23 18:02:56 +01001223static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1224 struct vxlan_sock *vs,
1225 struct sk_buff *skb)
Thomas Graf614732e2015-07-21 10:44:06 +02001226{
Thomas Graf614732e2015-07-21 10:44:06 +02001227 union vxlan_addr saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001228
Thomas Graf614732e2015-07-21 10:44:06 +02001229 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001230 skb->protocol = eth_type_trans(skb, vxlan->dev);
1231 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1232
1233 /* Ignore packet loops (and multicast echo) */
1234 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001235 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001236
Jiri Benc760c6802016-02-23 18:02:57 +01001237 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001238 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001239 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001240 saddr.sa.sa_family = AF_INET;
1241#if IS_ENABLED(CONFIG_IPV6)
1242 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001243 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001244 saddr.sa.sa_family = AF_INET6;
1245#endif
1246 }
1247
Jiri Benc1ab016e2016-02-23 18:02:56 +01001248 if ((vxlan->flags & VXLAN_F_LEARN) &&
1249 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1250 return false;
1251
1252 return true;
1253}
1254
Jiri Benc760c6802016-02-23 18:02:57 +01001255static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1256 struct sk_buff *skb)
1257{
1258 int err = 0;
1259
1260 if (vxlan_get_sk_family(vs) == AF_INET)
1261 err = IP_ECN_decapsulate(oiph, skb);
1262#if IS_ENABLED(CONFIG_IPV6)
1263 else
1264 err = IP6_ECN_decapsulate(oiph, skb);
1265#endif
1266
1267 if (unlikely(err) && log_ecn_error) {
1268 if (vxlan_get_sk_family(vs) == AF_INET)
1269 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1270 &((struct iphdr *)oiph)->saddr,
1271 ((struct iphdr *)oiph)->tos);
1272 else
1273 net_info_ratelimited("non-ECT from %pI6\n",
1274 &((struct ipv6hdr *)oiph)->saddr);
1275 }
1276 return err <= 1;
1277}
1278
stephen hemmingerd3428942012-10-01 12:32:35 +00001279/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001280static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001281{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001282 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001283 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001284 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001285 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001286 struct vxlan_metadata _md;
1287 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001288 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001289 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001290 void *oiph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001291
Jiri Bence1e53142016-04-05 14:47:13 +02001292 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001293 if (!pskb_may_pull(skb, VXLAN_HLEN))
Jiri Benc288b01c2016-02-16 21:59:02 +01001294 return 1;
stephen hemmingerd3428942012-10-01 12:32:35 +00001295
Jiri Bencf14eceb2016-02-16 21:59:01 +01001296 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001297 /* VNI flag always required to be set */
1298 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1299 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1300 ntohl(vxlan_hdr(skb)->vx_flags),
1301 ntohl(vxlan_hdr(skb)->vx_vni));
1302 /* Return non vxlan pkt */
1303 return 1;
stephen hemmingerd3428942012-10-01 12:32:35 +00001304 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001305 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1306 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001307
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001308 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001309 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001310 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001311
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001312 vxlan = vxlan_vs_find_vni(vs, vxlan_vni(vxlan_hdr(skb)->vx_vni));
1313 if (!vxlan)
1314 goto drop;
1315
Jiri Bence1e53142016-04-05 14:47:13 +02001316 /* For backwards compatibility, only allow reserved fields to be
1317 * used by VXLAN extensions if explicitly requested.
1318 */
1319 if (vs->flags & VXLAN_F_GPE) {
1320 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1321 goto drop;
1322 raw_proto = true;
1323 }
1324
1325 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1326 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1327 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001328
Thomas Grafee122c72015-07-21 10:43:58 +02001329 if (vxlan_collect_metadata(vs)) {
Jiri Benc07dabf22016-02-18 19:19:29 +01001330 __be32 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
Jiri Benc10a5af22016-02-23 18:02:59 +01001331 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001332
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001333 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Jiri Benc07dabf22016-02-18 19:19:29 +01001334 vxlan_vni_to_tun_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001335
Thomas Grafee122c72015-07-21 10:43:58 +02001336 if (!tun_dst)
1337 goto drop;
1338
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001339 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001340
1341 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001342 } else {
1343 memset(md, 0, sizeof(*md));
1344 }
1345
Jiri Bencf14eceb2016-02-16 21:59:01 +01001346 if (vs->flags & VXLAN_F_REMCSUM_RX)
1347 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1348 goto drop;
1349 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001350 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001351 /* Note that GBP and GPE can never be active together. This is
1352 * ensured in vxlan_dev_configure.
1353 */
Thomas Graf35114942015-01-15 03:53:55 +01001354
Jiri Bencf14eceb2016-02-16 21:59:01 +01001355 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001356 /* If there are any unprocessed flags remaining treat
1357 * this as a malformed packet. This behavior diverges from
1358 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1359 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001360 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001361 * is more robust and provides a little more security in
1362 * adding extensions to VXLAN.
1363 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001364 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001365 }
1366
Jiri Bence1e53142016-04-05 14:47:13 +02001367 if (!raw_proto) {
1368 if (!vxlan_set_mac(vxlan, vs, skb))
1369 goto drop;
1370 } else {
1371 skb->dev = vxlan->dev;
1372 skb->pkt_type = PACKET_HOST;
1373 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001374
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001375 oiph = skb_network_header(skb);
1376 skb_reset_network_header(skb);
1377
1378 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1379 ++vxlan->dev->stats.rx_frame_errors;
1380 ++vxlan->dev->stats.rx_errors;
1381 goto drop;
1382 }
1383
1384 stats = this_cpu_ptr(vxlan->dev->tstats);
1385 u64_stats_update_begin(&stats->syncp);
1386 stats->rx_packets++;
1387 stats->rx_bytes += skb->len;
1388 u64_stats_update_end(&stats->syncp);
1389
1390 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001391 return 0;
1392
1393drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001394 /* Consume bad packet */
1395 kfree_skb(skb);
1396 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001397}
1398
David Stevense4f67ad2012-11-20 02:50:14 +00001399static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1400{
1401 struct vxlan_dev *vxlan = netdev_priv(dev);
1402 struct arphdr *parp;
1403 u8 *arpptr, *sha;
1404 __be32 sip, tip;
1405 struct neighbour *n;
1406
1407 if (dev->flags & IFF_NOARP)
1408 goto out;
1409
1410 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1411 dev->stats.tx_dropped++;
1412 goto out;
1413 }
1414 parp = arp_hdr(skb);
1415
1416 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1417 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1418 parp->ar_pro != htons(ETH_P_IP) ||
1419 parp->ar_op != htons(ARPOP_REQUEST) ||
1420 parp->ar_hln != dev->addr_len ||
1421 parp->ar_pln != 4)
1422 goto out;
1423 arpptr = (u8 *)parp + sizeof(struct arphdr);
1424 sha = arpptr;
1425 arpptr += dev->addr_len; /* sha */
1426 memcpy(&sip, arpptr, sizeof(sip));
1427 arpptr += sizeof(sip);
1428 arpptr += dev->addr_len; /* tha */
1429 memcpy(&tip, arpptr, sizeof(tip));
1430
1431 if (ipv4_is_loopback(tip) ||
1432 ipv4_is_multicast(tip))
1433 goto out;
1434
1435 n = neigh_lookup(&arp_tbl, &tip, dev);
1436
1437 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001438 struct vxlan_fdb *f;
1439 struct sk_buff *reply;
1440
1441 if (!(n->nud_state & NUD_CONNECTED)) {
1442 neigh_release(n);
1443 goto out;
1444 }
1445
1446 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001447 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001448 /* bridge-local neighbor */
1449 neigh_release(n);
1450 goto out;
1451 }
1452
1453 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1454 n->ha, sha);
1455
1456 neigh_release(n);
1457
David Stevens73461352014-03-18 12:32:29 -04001458 if (reply == NULL)
1459 goto out;
1460
David Stevense4f67ad2012-11-20 02:50:14 +00001461 skb_reset_mac_header(reply);
1462 __skb_pull(reply, skb_network_offset(reply));
1463 reply->ip_summed = CHECKSUM_UNNECESSARY;
1464 reply->pkt_type = PACKET_HOST;
1465
1466 if (netif_rx_ni(reply) == NET_RX_DROP)
1467 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001468 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1469 union vxlan_addr ipa = {
1470 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001471 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001472 };
1473
1474 vxlan_ip_miss(dev, &ipa);
1475 }
David Stevense4f67ad2012-11-20 02:50:14 +00001476out:
1477 consume_skb(skb);
1478 return NETDEV_TX_OK;
1479}
1480
Cong Wangf564f452013-08-31 13:44:36 +08001481#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001482static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1483 struct neighbour *n, bool isrouter)
1484{
1485 struct net_device *dev = request->dev;
1486 struct sk_buff *reply;
1487 struct nd_msg *ns, *na;
1488 struct ipv6hdr *pip6;
1489 u8 *daddr;
1490 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1491 int ns_olen;
1492 int i, len;
1493
1494 if (dev == NULL)
1495 return NULL;
1496
1497 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1498 sizeof(*na) + na_olen + dev->needed_tailroom;
1499 reply = alloc_skb(len, GFP_ATOMIC);
1500 if (reply == NULL)
1501 return NULL;
1502
1503 reply->protocol = htons(ETH_P_IPV6);
1504 reply->dev = dev;
1505 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1506 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001507 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001508
1509 ns = (struct nd_msg *)skb_transport_header(request);
1510
1511 daddr = eth_hdr(request)->h_source;
1512 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1513 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1514 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1515 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1516 break;
1517 }
1518 }
1519
1520 /* Ethernet header */
1521 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1522 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1523 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1524 reply->protocol = htons(ETH_P_IPV6);
1525
1526 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001527 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001528 skb_put(reply, sizeof(struct ipv6hdr));
1529
1530 /* IPv6 header */
1531
1532 pip6 = ipv6_hdr(reply);
1533 memset(pip6, 0, sizeof(struct ipv6hdr));
1534 pip6->version = 6;
1535 pip6->priority = ipv6_hdr(request)->priority;
1536 pip6->nexthdr = IPPROTO_ICMPV6;
1537 pip6->hop_limit = 255;
1538 pip6->daddr = ipv6_hdr(request)->saddr;
1539 pip6->saddr = *(struct in6_addr *)n->primary_key;
1540
1541 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001542 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001543
1544 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1545
1546 /* Neighbor Advertisement */
1547 memset(na, 0, sizeof(*na)+na_olen);
1548 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1549 na->icmph.icmp6_router = isrouter;
1550 na->icmph.icmp6_override = 1;
1551 na->icmph.icmp6_solicited = 1;
1552 na->target = ns->target;
1553 ether_addr_copy(&na->opt[2], n->ha);
1554 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1555 na->opt[1] = na_olen >> 3;
1556
1557 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1558 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1559 csum_partial(na, sizeof(*na)+na_olen, 0));
1560
1561 pip6->payload_len = htons(sizeof(*na)+na_olen);
1562
1563 skb_push(reply, sizeof(struct ipv6hdr));
1564
1565 reply->ip_summed = CHECKSUM_UNNECESSARY;
1566
1567 return reply;
1568}
1569
Cong Wangf564f452013-08-31 13:44:36 +08001570static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1571{
1572 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001573 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001574 const struct ipv6hdr *iphdr;
1575 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001576 struct neighbour *n;
1577 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001578
1579 in6_dev = __in6_dev_get(dev);
1580 if (!in6_dev)
1581 goto out;
1582
Cong Wangf564f452013-08-31 13:44:36 +08001583 iphdr = ipv6_hdr(skb);
1584 saddr = &iphdr->saddr;
1585 daddr = &iphdr->daddr;
1586
Cong Wangf564f452013-08-31 13:44:36 +08001587 msg = (struct nd_msg *)skb_transport_header(skb);
1588 if (msg->icmph.icmp6_code != 0 ||
1589 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1590 goto out;
1591
David Stevens4b29dba2014-03-24 10:39:58 -04001592 if (ipv6_addr_loopback(daddr) ||
1593 ipv6_addr_is_multicast(&msg->target))
1594 goto out;
1595
1596 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001597
1598 if (n) {
1599 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001600 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001601
1602 if (!(n->nud_state & NUD_CONNECTED)) {
1603 neigh_release(n);
1604 goto out;
1605 }
1606
1607 f = vxlan_find_mac(vxlan, n->ha);
1608 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1609 /* bridge-local neighbor */
1610 neigh_release(n);
1611 goto out;
1612 }
1613
David Stevens4b29dba2014-03-24 10:39:58 -04001614 reply = vxlan_na_create(skb, n,
1615 !!(f ? f->flags & NTF_ROUTER : 0));
1616
Cong Wangf564f452013-08-31 13:44:36 +08001617 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001618
1619 if (reply == NULL)
1620 goto out;
1621
1622 if (netif_rx_ni(reply) == NET_RX_DROP)
1623 dev->stats.rx_dropped++;
1624
Cong Wangf564f452013-08-31 13:44:36 +08001625 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001626 union vxlan_addr ipa = {
1627 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001628 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001629 };
1630
Cong Wangf564f452013-08-31 13:44:36 +08001631 vxlan_ip_miss(dev, &ipa);
1632 }
1633
1634out:
1635 consume_skb(skb);
1636 return NETDEV_TX_OK;
1637}
1638#endif
1639
David Stevense4f67ad2012-11-20 02:50:14 +00001640static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1641{
1642 struct vxlan_dev *vxlan = netdev_priv(dev);
1643 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001644
1645 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1646 return false;
1647
1648 n = NULL;
1649 switch (ntohs(eth_hdr(skb)->h_proto)) {
1650 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001651 {
1652 struct iphdr *pip;
1653
David Stevense4f67ad2012-11-20 02:50:14 +00001654 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1655 return false;
1656 pip = ip_hdr(skb);
1657 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001658 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1659 union vxlan_addr ipa = {
1660 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001661 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001662 };
1663
1664 vxlan_ip_miss(dev, &ipa);
1665 return false;
1666 }
1667
David Stevense4f67ad2012-11-20 02:50:14 +00001668 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001669 }
1670#if IS_ENABLED(CONFIG_IPV6)
1671 case ETH_P_IPV6:
1672 {
1673 struct ipv6hdr *pip6;
1674
1675 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1676 return false;
1677 pip6 = ipv6_hdr(skb);
1678 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1679 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1680 union vxlan_addr ipa = {
1681 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001682 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001683 };
1684
1685 vxlan_ip_miss(dev, &ipa);
1686 return false;
1687 }
1688
1689 break;
1690 }
1691#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001692 default:
1693 return false;
1694 }
1695
1696 if (n) {
1697 bool diff;
1698
Joe Perches7367d0b2013-09-01 11:51:23 -07001699 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001700 if (diff) {
1701 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1702 dev->addr_len);
1703 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1704 }
1705 neigh_release(n);
1706 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001707 }
1708
David Stevense4f67ad2012-11-20 02:50:14 +00001709 return false;
1710}
1711
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001712static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001713 struct vxlan_metadata *md)
1714{
1715 struct vxlanhdr_gbp *gbp;
1716
Thomas Grafdb79a622015-02-04 17:00:04 +01001717 if (!md->gbp)
1718 return;
1719
Thomas Graf35114942015-01-15 03:53:55 +01001720 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001721 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001722
1723 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1724 gbp->dont_learn = 1;
1725
1726 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1727 gbp->policy_applied = 1;
1728
1729 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1730}
1731
Jiri Bence1e53142016-04-05 14:47:13 +02001732static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1733 __be16 protocol)
1734{
1735 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1736
1737 gpe->np_applied = 1;
1738
1739 switch (protocol) {
1740 case htons(ETH_P_IP):
1741 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1742 return 0;
1743 case htons(ETH_P_IPV6):
1744 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1745 return 0;
1746 case htons(ETH_P_TEB):
1747 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1748 return 0;
1749 }
1750 return -EPFNOSUPPORT;
1751}
1752
Jiri Bencf491e562016-02-02 18:09:16 +01001753static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1754 int iphdr_len, __be32 vni,
1755 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001756 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001757{
Cong Wange4c7ed42013-08-31 13:44:33 +08001758 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001759 int min_headroom;
1760 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001761 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001762 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001763
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001764 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001765 skb->ip_summed == CHECKSUM_PARTIAL) {
1766 int csum_start = skb_checksum_start_offset(skb);
1767
1768 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1769 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1770 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001771 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001772 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001773 }
1774
Cong Wange4c7ed42013-08-31 13:44:33 +08001775 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
Jiri Bencf491e562016-02-02 18:09:16 +01001776 + VXLAN_HLEN + iphdr_len
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001777 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001778
1779 /* Need space for new headers (invalidates iph ptr) */
1780 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001781 if (unlikely(err))
1782 goto out_free;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001783
Jiri Pirko59682502014-11-19 14:04:59 +01001784 skb = vlan_hwaccel_push_inside(skb);
1785 if (WARN_ON(!skb))
1786 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001787
Edward Cree6fa79662016-02-11 21:02:31 +00001788 skb = iptunnel_handle_offloads(skb, type);
Jesse Grossb736a622015-04-09 11:19:14 -07001789 if (IS_ERR(skb))
1790 return PTR_ERR(skb);
1791
Pravin B Shelar49560532013-08-19 11:23:17 -07001792 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001793 vxh->vx_flags = VXLAN_HF_VNI;
1794 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001795
Tom Herbertdfd86452015-01-12 17:00:38 -08001796 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001797 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001798
Jiri Benc54bfd872016-02-16 21:58:58 +01001799 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1800 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1801 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001802
1803 if (!skb_is_gso(skb)) {
1804 skb->ip_summed = CHECKSUM_NONE;
1805 skb->encapsulation = 0;
1806 }
1807 }
1808
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001809 if (vxflags & VXLAN_F_GBP)
1810 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001811 if (vxflags & VXLAN_F_GPE) {
1812 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1813 if (err < 0)
1814 goto out_free;
1815 inner_protocol = skb->protocol;
1816 }
Thomas Graf35114942015-01-15 03:53:55 +01001817
Jiri Bence1e53142016-04-05 14:47:13 +02001818 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001819 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02001820
1821out_free:
1822 kfree_skb(skb);
1823 return err;
Pravin B Shelar49560532013-08-19 11:23:17 -07001824}
Pravin B Shelar49560532013-08-19 11:23:17 -07001825
Jiri Benc1a8496b2016-02-02 18:09:14 +01001826static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan,
1827 struct sk_buff *skb, int oif, u8 tos,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001828 __be32 daddr, __be32 *saddr,
1829 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001830 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01001831{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001832 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01001833 struct rtable *rt = NULL;
1834 struct flowi4 fl4;
1835
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001836 if (tos && !info)
1837 use_cache = false;
1838 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001839 rt = dst_cache_get_ip4(dst_cache, saddr);
1840 if (rt)
1841 return rt;
1842 }
1843
Jiri Benc1a8496b2016-02-02 18:09:14 +01001844 memset(&fl4, 0, sizeof(fl4));
1845 fl4.flowi4_oif = oif;
1846 fl4.flowi4_tos = RT_TOS(tos);
1847 fl4.flowi4_mark = skb->mark;
1848 fl4.flowi4_proto = IPPROTO_UDP;
1849 fl4.daddr = daddr;
1850 fl4.saddr = vxlan->cfg.saddr.sin.sin_addr.s_addr;
1851
1852 rt = ip_route_output_key(vxlan->net, &fl4);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001853 if (!IS_ERR(rt)) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01001854 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001855 if (use_cache)
1856 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
1857 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01001858 return rt;
1859}
1860
Jiri Bence5d4b292015-12-07 13:04:30 +01001861#if IS_ENABLED(CONFIG_IPV6)
1862static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
Daniel Borkmann14006152016-03-04 15:15:08 +01001863 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001864 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01001865 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001866 struct in6_addr *saddr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001867 struct dst_cache *dst_cache,
1868 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01001869{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001870 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01001871 struct dst_entry *ndst;
1872 struct flowi6 fl6;
1873 int err;
1874
Daniel Borkmann14006152016-03-04 15:15:08 +01001875 if (tos && !info)
1876 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001877 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001878 ndst = dst_cache_get_ip6(dst_cache, saddr);
1879 if (ndst)
1880 return ndst;
1881 }
1882
Jiri Bence5d4b292015-12-07 13:04:30 +01001883 memset(&fl6, 0, sizeof(fl6));
1884 fl6.flowi6_oif = oif;
1885 fl6.daddr = *daddr;
1886 fl6.saddr = vxlan->cfg.saddr.sin6.sin6_addr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01001887 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01001888 fl6.flowi6_mark = skb->mark;
1889 fl6.flowi6_proto = IPPROTO_UDP;
1890
1891 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
1892 vxlan->vn6_sock->sock->sk,
1893 &ndst, &fl6);
1894 if (err < 0)
1895 return ERR_PTR(err);
1896
1897 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001898 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001899 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01001900 return ndst;
1901}
1902#endif
1903
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001904/* Bypass encapsulation if the destination is local */
1905static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1906 struct vxlan_dev *dst_vxlan)
1907{
Li RongQing8f849852014-01-04 13:57:59 +08001908 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001909 union vxlan_addr loopback;
1910 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001911 struct net_device *dev = skb->dev;
1912 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001913
Li RongQing8f849852014-01-04 13:57:59 +08001914 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1915 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001916 skb->pkt_type = PACKET_HOST;
1917 skb->encapsulation = 0;
1918 skb->dev = dst_vxlan->dev;
1919 __skb_pull(skb, skb_network_offset(skb));
1920
Cong Wange4c7ed42013-08-31 13:44:33 +08001921 if (remote_ip->sa.sa_family == AF_INET) {
1922 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1923 loopback.sa.sa_family = AF_INET;
1924#if IS_ENABLED(CONFIG_IPV6)
1925 } else {
1926 loopback.sin6.sin6_addr = in6addr_loopback;
1927 loopback.sa.sa_family = AF_INET6;
1928#endif
1929 }
1930
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001931 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001932 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001933
1934 u64_stats_update_begin(&tx_stats->syncp);
1935 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001936 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001937 u64_stats_update_end(&tx_stats->syncp);
1938
1939 if (netif_rx(skb) == NET_RX_SUCCESS) {
1940 u64_stats_update_begin(&rx_stats->syncp);
1941 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001942 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001943 u64_stats_update_end(&rx_stats->syncp);
1944 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001945 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001946 }
1947}
1948
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001949static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1950 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001951{
Paolo Abenid71785f2016-02-12 15:43:57 +01001952 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02001953 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00001954 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001955 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001956 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001957 const struct iphdr *old_iph;
Cong Wange4c7ed42013-08-31 13:44:33 +08001958 union vxlan_addr *dst;
Thomas Grafee122c72015-07-21 10:43:58 +02001959 union vxlan_addr remote_ip;
1960 struct vxlan_metadata _md;
1961 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001962 __be16 src_port = 0, dst_port;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001963 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00001964 __be16 df = 0;
1965 __u8 tos, ttl;
Pravin B Shelar0e6fbc5b2013-06-17 17:49:56 -07001966 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02001967 u32 flags = vxlan->flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001968 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01001969 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00001970
Jiri Benc61adedf2015-08-20 13:56:25 +02001971 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02001972
Thomas Grafee122c72015-07-21 10:43:58 +02001973 if (rdst) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001974 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Thomas Grafee122c72015-07-21 10:43:58 +02001975 vni = rdst->remote_vni;
1976 dst = &rdst->remote_ip;
Paolo Abenid71785f2016-02-12 15:43:57 +01001977 dst_cache = &rdst->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02001978 } else {
1979 if (!info) {
1980 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1981 dev->name);
1982 goto drop;
1983 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001984 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
Jiri Benc54bfd872016-02-16 21:58:58 +01001985 vni = vxlan_tun_id_to_vni(info->key.tun_id);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001986 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
1987 if (remote_ip.sa.sa_family == AF_INET)
Jiri Benca725e512015-08-20 13:56:30 +02001988 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
1989 else
1990 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
Thomas Grafee122c72015-07-21 10:43:58 +02001991 dst = &remote_ip;
Paolo Abenid71785f2016-02-12 15:43:57 +01001992 dst_cache = &info->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02001993 }
David Stevense4f67ad2012-11-20 02:50:14 +00001994
Cong Wange4c7ed42013-08-31 13:44:33 +08001995 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001996 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001997 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001998 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001999 return;
David Stevense4f67ad2012-11-20 02:50:14 +00002000 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002001 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00002002 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002003
stephen hemmingerd3428942012-10-01 12:32:35 +00002004 old_iph = ip_hdr(skb);
2005
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002006 ttl = vxlan->cfg.ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08002007 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00002008 ttl = 1;
2009
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002010 tos = vxlan->cfg.tos;
stephen hemmingerd3428942012-10-01 12:32:35 +00002011 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00002012 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002013
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002014 label = vxlan->cfg.label;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002015 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2016 vxlan->cfg.port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002017
Jiri Benca725e512015-08-20 13:56:30 +02002018 if (info) {
Jiri Benca725e512015-08-20 13:56:30 +02002019 ttl = info->key.ttl;
2020 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002021 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002022 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002023
2024 if (info->options_len)
Pravin B Shelar4c222792015-08-30 18:09:38 -07002025 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002026 } else {
2027 md->gbp = skb->mark;
2028 }
2029
Cong Wange4c7ed42013-08-31 13:44:33 +08002030 if (dst->sa.sa_family == AF_INET) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01002031 __be32 saddr;
2032
Jiri Bencb1be00a2015-09-24 13:50:02 +02002033 if (!vxlan->vn4_sock)
2034 goto drop;
2035 sk = vxlan->vn4_sock->sock->sk;
2036
Jiri Benc1a8496b2016-02-02 18:09:14 +01002037 rt = vxlan_get_route(vxlan, skb,
2038 rdst ? rdst->remote_ifindex : 0, tos,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002039 dst->sin.sin_addr.s_addr, &saddr,
Paolo Abenid71785f2016-02-12 15:43:57 +01002040 dst_cache, info);
Cong Wange4c7ed42013-08-31 13:44:33 +08002041 if (IS_ERR(rt)) {
2042 netdev_dbg(dev, "no route to %pI4\n",
2043 &dst->sin.sin_addr.s_addr);
2044 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002045 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002046 }
2047
2048 if (rt->dst.dev == dev) {
2049 netdev_dbg(dev, "circular route to %pI4\n",
2050 &dst->sin.sin_addr.s_addr);
2051 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08002052 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002053 }
2054
2055 /* Bypass encapsulation if the destination is local */
2056 if (rt->rt_flags & RTCF_LOCAL &&
2057 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2058 struct vxlan_dev *dst_vxlan;
2059
2060 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002061 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002062 dst->sa.sa_family, dst_port,
2063 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002064 if (!dst_vxlan)
2065 goto tx_error;
2066 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2067 return;
2068 }
2069
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002070 if (!info)
2071 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2072 else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
2073 df = htons(IP_DF);
2074
Cong Wange4c7ed42013-08-31 13:44:33 +08002075 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2076 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Jiri Bencf491e562016-02-02 18:09:16 +01002077 err = vxlan_build_skb(skb, &rt->dst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002078 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002079 if (err < 0)
2080 goto xmit_tx_error;
2081
2082 udp_tunnel_xmit_skb(rt, sk, skb, saddr,
2083 dst->sin.sin_addr.s_addr, tos, ttl, df,
2084 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002085#if IS_ENABLED(CONFIG_IPV6)
2086 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08002087 struct dst_entry *ndst;
Jiri Bence5d4b292015-12-07 13:04:30 +01002088 struct in6_addr saddr;
Jiri Benc6f264e42015-08-20 13:56:29 +02002089 u32 rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002090
Jiri Bencb1be00a2015-09-24 13:50:02 +02002091 if (!vxlan->vn6_sock)
2092 goto drop;
2093 sk = vxlan->vn6_sock->sock->sk;
2094
Jiri Bence5d4b292015-12-07 13:04:30 +01002095 ndst = vxlan6_get_route(vxlan, skb,
Daniel Borkmann14006152016-03-04 15:15:08 +01002096 rdst ? rdst->remote_ifindex : 0, tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002097 label, &dst->sin6.sin6_addr, &saddr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002098 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002099 if (IS_ERR(ndst)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002100 netdev_dbg(dev, "no route to %pI6\n",
2101 &dst->sin6.sin6_addr);
2102 dev->stats.tx_carrier_errors++;
2103 goto tx_error;
2104 }
2105
2106 if (ndst->dev == dev) {
2107 netdev_dbg(dev, "circular route to %pI6\n",
2108 &dst->sin6.sin6_addr);
2109 dst_release(ndst);
2110 dev->stats.collisions++;
2111 goto tx_error;
2112 }
2113
2114 /* Bypass encapsulation if the destination is local */
Jiri Benc6f264e42015-08-20 13:56:29 +02002115 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2116 if (rt6i_flags & RTF_LOCAL &&
2117 !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002118 struct vxlan_dev *dst_vxlan;
2119
2120 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002121 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002122 dst->sa.sa_family, dst_port,
2123 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002124 if (!dst_vxlan)
2125 goto tx_error;
2126 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2127 return;
2128 }
2129
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002130 if (!info)
2131 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Jesse Gross35e2d112016-01-20 16:22:47 -08002132
Daniel Borkmann14006152016-03-04 15:15:08 +01002133 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002134 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002135 skb_scrub_packet(skb, xnet);
2136 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002137 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002138 if (err < 0) {
2139 dst_release(ndst);
2140 return;
2141 }
2142 udp_tunnel6_xmit_skb(ndst, sk, skb, dev,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002143 &saddr, &dst->sin6.sin6_addr, tos, ttl,
2144 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002145#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002146 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002147
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002148 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002149
2150drop:
2151 dev->stats.tx_dropped++;
2152 goto tx_free;
2153
Jiri Bencf491e562016-02-02 18:09:16 +01002154xmit_tx_error:
2155 /* skb is already freed. */
2156 skb = NULL;
Pravin B Shelar49560532013-08-19 11:23:17 -07002157rt_tx_error:
2158 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002159tx_error:
2160 dev->stats.tx_errors++;
2161tx_free:
2162 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002163}
2164
David Stevens66817122013-03-15 04:35:51 +00002165/* Transmit local packets over Vxlan
2166 *
2167 * Outer IP header inherits ECN and DF from inner header.
2168 * Outer UDP destination is the VXLAN assigned port.
2169 * source port is based on hash of flow
2170 */
2171static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2172{
2173 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002174 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002175 struct ethhdr *eth;
2176 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002177 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002178 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002179
Jiri Benc61adedf2015-08-20 13:56:25 +02002180 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002181
David Stevens66817122013-03-15 04:35:51 +00002182 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002183
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002184 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
2185 if (info && info->mode & IP_TUNNEL_INFO_TX)
2186 vxlan_xmit_one(skb, dev, NULL, false);
2187 else
2188 kfree_skb(skb);
2189 return NETDEV_TX_OK;
2190 }
2191
2192 if (vxlan->flags & VXLAN_F_PROXY) {
2193 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002194 if (ntohs(eth->h_proto) == ETH_P_ARP)
2195 return arp_reduce(dev, skb);
2196#if IS_ENABLED(CONFIG_IPV6)
2197 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002198 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2199 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002200 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2201 struct nd_msg *msg;
2202
2203 msg = (struct nd_msg *)skb_transport_header(skb);
2204 if (msg->icmph.icmp6_code == 0 &&
2205 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2206 return neigh_reduce(dev, skb);
2207 }
2208#endif
2209 }
David Stevens66817122013-03-15 04:35:51 +00002210
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002211 eth = eth_hdr(skb);
David Stevens66817122013-03-15 04:35:51 +00002212 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002213 did_rsc = false;
2214
2215 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002216 (ntohs(eth->h_proto) == ETH_P_IP ||
2217 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002218 did_rsc = route_shortcircuit(dev, skb);
2219 if (did_rsc)
2220 f = vxlan_find_mac(vxlan, eth->h_dest);
2221 }
2222
David Stevens66817122013-03-15 04:35:51 +00002223 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002224 f = vxlan_find_mac(vxlan, all_zeros_mac);
2225 if (f == NULL) {
2226 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2227 !is_multicast_ether_addr(eth->h_dest))
2228 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002229
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002230 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002231 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002232 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002233 }
David Stevens66817122013-03-15 04:35:51 +00002234 }
2235
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002236 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2237 struct sk_buff *skb1;
2238
Eric Dumazet8f646c92014-01-06 09:54:31 -08002239 if (!fdst) {
2240 fdst = rdst;
2241 continue;
2242 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002243 skb1 = skb_clone(skb, GFP_ATOMIC);
2244 if (skb1)
2245 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2246 }
2247
Eric Dumazet8f646c92014-01-06 09:54:31 -08002248 if (fdst)
2249 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2250 else
2251 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002252 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002253}
2254
stephen hemmingerd3428942012-10-01 12:32:35 +00002255/* Walk the forwarding table and purge stale entries */
2256static void vxlan_cleanup(unsigned long arg)
2257{
2258 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2259 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2260 unsigned int h;
2261
2262 if (!netif_running(vxlan->dev))
2263 return;
2264
stephen hemmingerd3428942012-10-01 12:32:35 +00002265 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2266 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002267
2268 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002269 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2270 struct vxlan_fdb *f
2271 = container_of(p, struct vxlan_fdb, hlist);
2272 unsigned long timeout;
2273
stephen hemminger3c172862012-10-26 06:24:34 +00002274 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002275 continue;
2276
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002277 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002278 if (time_before_eq(timeout, jiffies)) {
2279 netdev_dbg(vxlan->dev,
2280 "garbage collect %pM\n",
2281 f->eth_addr);
2282 f->state = NUD_STALE;
2283 vxlan_fdb_destroy(vxlan, f);
2284 } else if (time_before(timeout, next_timer))
2285 next_timer = timeout;
2286 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002287 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002288 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002289
2290 mod_timer(&vxlan->age_timer, next_timer);
2291}
2292
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002293static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2294{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002295 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002296 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002297
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002298 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002299 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002300 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002301}
2302
stephen hemmingerd3428942012-10-01 12:32:35 +00002303/* Setup stats when device is created */
2304static int vxlan_init(struct net_device *dev)
2305{
WANG Cong1c213bd2014-02-13 11:46:28 -08002306 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002307 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002308 return -ENOMEM;
2309
2310 return 0;
2311}
2312
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002313static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002314{
2315 struct vxlan_fdb *f;
2316
2317 spin_lock_bh(&vxlan->hash_lock);
2318 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2319 if (f)
2320 vxlan_fdb_destroy(vxlan, f);
2321 spin_unlock_bh(&vxlan->hash_lock);
2322}
2323
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002324static void vxlan_uninit(struct net_device *dev)
2325{
2326 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002327
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002328 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002329
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002330 free_percpu(dev->tstats);
2331}
2332
stephen hemmingerd3428942012-10-01 12:32:35 +00002333/* Start ageing timer and join group when device is brought up */
2334static int vxlan_open(struct net_device *dev)
2335{
2336 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002337 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002338
Jiri Benc205f3562015-09-24 13:50:01 +02002339 ret = vxlan_sock_add(vxlan);
2340 if (ret < 0)
2341 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002342
Gao feng79d4a942013-12-10 16:37:32 +08002343 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002344 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002345 if (ret == -EADDRINUSE)
2346 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002347 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002348 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002349 return ret;
2350 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002351 }
2352
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002353 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002354 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2355
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002356 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002357}
2358
2359/* Purge the forwarding table */
2360static void vxlan_flush(struct vxlan_dev *vxlan)
2361{
Cong Wang31fec5a2013-05-27 22:35:52 +00002362 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002363
2364 spin_lock_bh(&vxlan->hash_lock);
2365 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2366 struct hlist_node *p, *n;
2367 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2368 struct vxlan_fdb *f
2369 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002370 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2371 if (!is_zero_ether_addr(f->eth_addr))
2372 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002373 }
2374 }
2375 spin_unlock_bh(&vxlan->hash_lock);
2376}
2377
2378/* Cleanup timer and forwarding table on shutdown */
2379static int vxlan_stop(struct net_device *dev)
2380{
2381 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002382 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002383 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002384
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002385 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002386 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002387 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002388
2389 del_timer_sync(&vxlan->age_timer);
2390
2391 vxlan_flush(vxlan);
Jiri Benc205f3562015-09-24 13:50:01 +02002392 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002393
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002394 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002395}
2396
stephen hemmingerd3428942012-10-01 12:32:35 +00002397/* Stub, nothing needs to be done. */
2398static void vxlan_set_multicast_list(struct net_device *dev)
2399{
2400}
2401
David Wragg72564b52016-02-10 00:05:55 +00002402static int __vxlan_change_mtu(struct net_device *dev,
2403 struct net_device *lowerdev,
2404 struct vxlan_rdst *dst, int new_mtu, bool strict)
2405{
2406 int max_mtu = IP_MAX_MTU;
2407
2408 if (lowerdev)
2409 max_mtu = lowerdev->mtu;
2410
2411 if (dst->remote_ip.sa.sa_family == AF_INET6)
2412 max_mtu -= VXLAN6_HEADROOM;
2413 else
2414 max_mtu -= VXLAN_HEADROOM;
2415
2416 if (new_mtu < 68)
2417 return -EINVAL;
2418
2419 if (new_mtu > max_mtu) {
2420 if (strict)
2421 return -EINVAL;
2422
2423 new_mtu = max_mtu;
2424 }
2425
2426 dev->mtu = new_mtu;
2427 return 0;
2428}
2429
Daniel Borkmann345010b2013-12-18 00:21:08 +01002430static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2431{
2432 struct vxlan_dev *vxlan = netdev_priv(dev);
2433 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002434 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2435 dst->remote_ifindex);
2436 return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002437}
2438
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002439static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2440{
2441 struct vxlan_dev *vxlan = netdev_priv(dev);
2442 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2443 __be16 sport, dport;
2444
2445 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2446 vxlan->cfg.port_max, true);
2447 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2448
Jiri Benc239e9442015-12-07 13:04:31 +01002449 if (ip_tunnel_info_af(info) == AF_INET) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01002450 struct rtable *rt;
2451
Jiri Benc239e9442015-12-07 13:04:31 +01002452 if (!vxlan->vn4_sock)
2453 return -EINVAL;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002454 rt = vxlan_get_route(vxlan, skb, 0, info->key.tos,
2455 info->key.u.ipv4.dst,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002456 &info->key.u.ipv4.src, NULL, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002457 if (IS_ERR(rt))
2458 return PTR_ERR(rt);
2459 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002460 } else {
2461#if IS_ENABLED(CONFIG_IPV6)
2462 struct dst_entry *ndst;
2463
2464 if (!vxlan->vn6_sock)
2465 return -EINVAL;
Daniel Borkmann14006152016-03-04 15:15:08 +01002466 ndst = vxlan6_get_route(vxlan, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002467 info->key.label, &info->key.u.ipv6.dst,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002468 &info->key.u.ipv6.src, NULL, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002469 if (IS_ERR(ndst))
2470 return PTR_ERR(ndst);
2471 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002472#else /* !CONFIG_IPV6 */
2473 return -EPFNOSUPPORT;
2474#endif
2475 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002476 info->key.tp_src = sport;
2477 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002478 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002479}
2480
Jiri Benc0c867c92016-04-05 14:47:10 +02002481static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002482 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002483 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002484 .ndo_open = vxlan_open,
2485 .ndo_stop = vxlan_stop,
2486 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002487 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002488 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002489 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002490 .ndo_validate_addr = eth_validate_addr,
2491 .ndo_set_mac_address = eth_mac_addr,
2492 .ndo_fdb_add = vxlan_fdb_add,
2493 .ndo_fdb_del = vxlan_fdb_delete,
2494 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002495 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002496};
2497
Jiri Bence1e53142016-04-05 14:47:13 +02002498static const struct net_device_ops vxlan_netdev_raw_ops = {
2499 .ndo_init = vxlan_init,
2500 .ndo_uninit = vxlan_uninit,
2501 .ndo_open = vxlan_open,
2502 .ndo_stop = vxlan_stop,
2503 .ndo_start_xmit = vxlan_xmit,
2504 .ndo_get_stats64 = ip_tunnel_get_stats64,
2505 .ndo_change_mtu = vxlan_change_mtu,
2506 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2507};
2508
stephen hemmingerd3428942012-10-01 12:32:35 +00002509/* Info for udev, that this is a virtual tunnel endpoint */
2510static struct device_type vxlan_type = {
2511 .name = "vxlan",
2512};
2513
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002514/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002515 * supply the listening VXLAN udp ports. Callers are expected
2516 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002517 */
2518void vxlan_get_rx_port(struct net_device *dev)
2519{
2520 struct vxlan_sock *vs;
2521 struct net *net = dev_net(dev);
2522 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2523 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002524 __be16 port;
2525 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002526
2527 spin_lock(&vn->sock_lock);
2528 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002529 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2530 port = inet_sk(vs->sock->sk)->inet_sport;
Jiri Benc705cc622015-08-20 13:56:28 +02002531 sa_family = vxlan_get_sk_family(vs);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002532 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2533 port);
2534 }
2535 }
2536 spin_unlock(&vn->sock_lock);
2537}
2538EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2539
stephen hemmingerd3428942012-10-01 12:32:35 +00002540/* Initialize the device structure. */
2541static void vxlan_setup(struct net_device *dev)
2542{
2543 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002544 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002545
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002546 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002547 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2548
stephen hemmingerd3428942012-10-01 12:32:35 +00002549 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002550 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002551 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002552 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002553
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002554 dev->vlan_features = dev->features;
2555 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002556 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002557 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002558 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002559 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002560 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002561
stephen hemminger553675f2013-05-16 11:35:20 +00002562 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002563 spin_lock_init(&vxlan->hash_lock);
2564
2565 init_timer_deferrable(&vxlan->age_timer);
2566 vxlan->age_timer.function = vxlan_cleanup;
2567 vxlan->age_timer.data = (unsigned long) vxlan;
2568
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002569 vxlan->cfg.dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002570
stephen hemmingerd3428942012-10-01 12:32:35 +00002571 vxlan->dev = dev;
2572
Tom Herbert58ce31c2015-08-19 17:07:33 -07002573 gro_cells_init(&vxlan->gro_cells, dev);
2574
stephen hemmingerd3428942012-10-01 12:32:35 +00002575 for (h = 0; h < FDB_HASH_SIZE; ++h)
2576 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2577}
2578
Jiri Benc0c867c92016-04-05 14:47:10 +02002579static void vxlan_ether_setup(struct net_device *dev)
2580{
2581 eth_hw_addr_random(dev);
2582 ether_setup(dev);
2583 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2584 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2585 dev->netdev_ops = &vxlan_netdev_ether_ops;
2586}
2587
Jiri Bence1e53142016-04-05 14:47:13 +02002588static void vxlan_raw_setup(struct net_device *dev)
2589{
2590 dev->type = ARPHRD_NONE;
2591 dev->hard_header_len = 0;
2592 dev->addr_len = 0;
2593 dev->mtu = ETH_DATA_LEN;
2594 dev->tx_queue_len = 1000;
2595 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2596 dev->netdev_ops = &vxlan_netdev_raw_ops;
2597}
2598
stephen hemmingerd3428942012-10-01 12:32:35 +00002599static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2600 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002601 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002602 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002603 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2604 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002605 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002606 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2607 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002608 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002609 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2610 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2611 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002612 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002613 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2614 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2615 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2616 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002617 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002618 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002619 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2620 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2621 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002622 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2623 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002624 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002625 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002626 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002627};
2628
2629static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2630{
2631 if (tb[IFLA_ADDRESS]) {
2632 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2633 pr_debug("invalid link address (not ethernet)\n");
2634 return -EINVAL;
2635 }
2636
2637 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2638 pr_debug("invalid all zero ethernet address\n");
2639 return -EADDRNOTAVAIL;
2640 }
2641 }
2642
2643 if (!data)
2644 return -EINVAL;
2645
2646 if (data[IFLA_VXLAN_ID]) {
2647 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2648 if (id >= VXLAN_VID_MASK)
2649 return -ERANGE;
2650 }
2651
stephen hemminger05f47d62012-10-09 20:35:50 +00002652 if (data[IFLA_VXLAN_PORT_RANGE]) {
2653 const struct ifla_vxlan_port_range *p
2654 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2655
2656 if (ntohs(p->high) < ntohs(p->low)) {
2657 pr_debug("port range %u .. %u not valid\n",
2658 ntohs(p->low), ntohs(p->high));
2659 return -EINVAL;
2660 }
2661 }
2662
stephen hemmingerd3428942012-10-01 12:32:35 +00002663 return 0;
2664}
2665
Yan Burman1b13c972013-01-29 23:43:07 +00002666static void vxlan_get_drvinfo(struct net_device *netdev,
2667 struct ethtool_drvinfo *drvinfo)
2668{
2669 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2670 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2671}
2672
2673static const struct ethtool_ops vxlan_ethtool_ops = {
2674 .get_drvinfo = vxlan_get_drvinfo,
2675 .get_link = ethtool_op_get_link,
2676};
2677
Tom Herbert3ee64f32014-07-13 19:49:42 -07002678static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2679 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002680{
Cong Wange4c7ed42013-08-31 13:44:33 +08002681 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002682 struct udp_port_cfg udp_conf;
2683 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002684
Tom Herbert3ee64f32014-07-13 19:49:42 -07002685 memset(&udp_conf, 0, sizeof(udp_conf));
2686
2687 if (ipv6) {
2688 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002689 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002690 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002691 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002692 } else {
2693 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002694 }
2695
Tom Herbert3ee64f32014-07-13 19:49:42 -07002696 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002697
Tom Herbert3ee64f32014-07-13 19:49:42 -07002698 /* Open UDP socket */
2699 err = udp_sock_create(net, &udp_conf, &sock);
2700 if (err < 0)
2701 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002702
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002703 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002704}
2705
2706/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002707static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2708 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002709{
2710 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2711 struct vxlan_sock *vs;
2712 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002713 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002714 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002715
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002716 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002717 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002718 return ERR_PTR(-ENOMEM);
2719
2720 for (h = 0; h < VNI_HASH_SIZE; ++h)
2721 INIT_HLIST_HEAD(&vs->vni_list[h]);
2722
Tom Herbert3ee64f32014-07-13 19:49:42 -07002723 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002724 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002725 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2726 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002727 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002728 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002729 }
2730
Cong Wange4c7ed42013-08-31 13:44:33 +08002731 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002732 atomic_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002733 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002734
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002735 spin_lock(&vn->sock_lock);
2736 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002737 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002738 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002739
2740 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002741 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002742 tunnel_cfg.sk_user_data = vs;
2743 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002744 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002745 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002746 tunnel_cfg.gro_receive = vxlan_gro_receive;
2747 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002748
2749 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002750
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002751 return vs;
2752}
stephen hemminger553675f2013-05-16 11:35:20 +00002753
Jiri Bencb1be00a2015-09-24 13:50:02 +02002754static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002755{
Jiri Benc205f3562015-09-24 13:50:01 +02002756 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2757 struct vxlan_sock *vs = NULL;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002758
Jiri Benc205f3562015-09-24 13:50:01 +02002759 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002760 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002761 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2762 vxlan->cfg.dst_port, vxlan->flags);
2763 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002764 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002765 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002766 }
2767 spin_unlock(&vn->sock_lock);
2768 }
Jiri Benc205f3562015-09-24 13:50:01 +02002769 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002770 vs = vxlan_socket_create(vxlan->net, ipv6,
2771 vxlan->cfg.dst_port, vxlan->flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002772 if (IS_ERR(vs))
2773 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002774#if IS_ENABLED(CONFIG_IPV6)
2775 if (ipv6)
2776 vxlan->vn6_sock = vs;
2777 else
2778#endif
2779 vxlan->vn4_sock = vs;
Jiri Benc205f3562015-09-24 13:50:01 +02002780 vxlan_vs_add_dev(vs, vxlan);
2781 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002782}
2783
Jiri Bencb1be00a2015-09-24 13:50:02 +02002784static int vxlan_sock_add(struct vxlan_dev *vxlan)
2785{
2786 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2787 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
2788 int ret = 0;
2789
2790 vxlan->vn4_sock = NULL;
2791#if IS_ENABLED(CONFIG_IPV6)
2792 vxlan->vn6_sock = NULL;
2793 if (ipv6 || metadata)
2794 ret = __vxlan_sock_add(vxlan, true);
2795#endif
2796 if (!ret && (!ipv6 || metadata))
2797 ret = __vxlan_sock_add(vxlan, false);
2798 if (ret < 0)
2799 vxlan_sock_release(vxlan);
2800 return ret;
2801}
2802
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002803static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2804 struct vxlan_config *conf)
stephen hemmingerd3428942012-10-01 12:32:35 +00002805{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002806 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002807 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
Atzm Watanabec7995c42013-04-16 02:50:52 +00002808 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002809 unsigned short needed_headroom = ETH_HLEN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002810 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002811 bool use_ipv6 = false;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002812 __be16 default_port = vxlan->cfg.dst_port;
David Wragg7e059152016-02-10 00:05:58 +00002813 struct net_device *lowerdev = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00002814
Jiri Bence1e53142016-04-05 14:47:13 +02002815 if (conf->flags & VXLAN_F_GPE) {
2816 if (conf->flags & ~VXLAN_F_ALLOWED_GPE)
2817 return -EINVAL;
2818 /* For now, allow GPE only together with COLLECT_METADATA.
2819 * This can be relaxed later; in such case, the other side
2820 * of the PtP link will have to be provided.
2821 */
2822 if (!(conf->flags & VXLAN_F_COLLECT_METADATA))
2823 return -EINVAL;
2824
2825 vxlan_raw_setup(dev);
2826 } else {
2827 vxlan_ether_setup(dev);
2828 }
Jiri Benc0c867c92016-04-05 14:47:10 +02002829
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002830 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002831
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002832 dst->remote_vni = conf->vni;
2833
2834 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00002835
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002836 /* Unless IPv6 is explicitly requested, assume IPv4 */
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002837 if (!dst->remote_ip.sa.sa_family)
2838 dst->remote_ip.sa.sa_family = AF_INET;
stephen hemmingerd3428942012-10-01 12:32:35 +00002839
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002840 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
Jiri Benc057ba292015-09-17 16:11:11 +02002841 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2842 if (!IS_ENABLED(CONFIG_IPV6))
2843 return -EPFNOSUPPORT;
Cong Wange4c7ed42013-08-31 13:44:33 +08002844 use_ipv6 = true;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002845 vxlan->flags |= VXLAN_F_IPV6;
Jiri Benc057ba292015-09-17 16:11:11 +02002846 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002847
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002848 if (conf->label && !use_ipv6) {
2849 pr_info("label only supported in use with IPv6\n");
2850 return -EINVAL;
2851 }
2852
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002853 if (conf->remote_ifindex) {
David Wragg7e059152016-02-10 00:05:58 +00002854 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002855 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00002856
stephen hemminger34e02aa2012-10-09 20:35:53 +00002857 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002858 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002859 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002860 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002861
Cong Wange4c7ed42013-08-31 13:44:33 +08002862#if IS_ENABLED(CONFIG_IPV6)
2863 if (use_ipv6) {
2864 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2865 if (idev && idev->cnf.disable_ipv6) {
2866 pr_info("IPv6 is disabled via sysctl\n");
2867 return -EPERM;
2868 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002869 }
2870#endif
2871
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002872 if (!conf->mtu)
Cong Wange4c7ed42013-08-31 13:44:33 +08002873 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002874
Jiri Bencb1be00a2015-09-24 13:50:02 +02002875 needed_headroom = lowerdev->hard_header_len;
Jiri Benc9dc2ad12015-09-17 16:11:10 +02002876 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002877
David Wragg7e059152016-02-10 00:05:58 +00002878 if (conf->mtu) {
2879 err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false);
2880 if (err)
2881 return err;
2882 }
2883
Jiri Bencb1be00a2015-09-24 13:50:02 +02002884 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2885 needed_headroom += VXLAN6_HEADROOM;
2886 else
2887 needed_headroom += VXLAN_HEADROOM;
2888 dev->needed_headroom = needed_headroom;
2889
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002890 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Jiri Bence1e53142016-04-05 14:47:13 +02002891 if (!vxlan->cfg.dst_port) {
2892 if (conf->flags & VXLAN_F_GPE)
2893 vxlan->cfg.dst_port = 4790; /* IANA assigned VXLAN-GPE port */
2894 else
2895 vxlan->cfg.dst_port = default_port;
2896 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002897 vxlan->flags |= conf->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00002898
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002899 if (!vxlan->cfg.age_interval)
2900 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
Vincent Bernatafb97182012-10-30 10:27:16 +00002901
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002902 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2903 if (tmp->cfg.vni == conf->vni &&
2904 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2905 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2906 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2907 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
2908 (vxlan->flags & VXLAN_F_RCV_FLAGS))
stephen hemminger553675f2013-05-16 11:35:20 +00002909 return -EEXIST;
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002910 }
stephen hemminger553675f2013-05-16 11:35:20 +00002911
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002912 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002913
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002914 /* create an fdb entry for a valid default destination */
2915 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2916 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2917 &vxlan->default_dst.remote_ip,
2918 NUD_REACHABLE|NUD_PERMANENT,
2919 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002920 vxlan->cfg.dst_port,
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002921 vxlan->default_dst.remote_vni,
2922 vxlan->default_dst.remote_ifindex,
2923 NTF_SELF);
2924 if (err)
2925 return err;
2926 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002927
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002928 err = register_netdevice(dev);
2929 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002930 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002931 return err;
2932 }
2933
stephen hemminger553675f2013-05-16 11:35:20 +00002934 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002935
2936 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002937}
2938
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002939struct net_device *vxlan_dev_create(struct net *net, const char *name,
2940 u8 name_assign_type, struct vxlan_config *conf)
2941{
2942 struct nlattr *tb[IFLA_MAX+1];
2943 struct net_device *dev;
2944 int err;
2945
2946 memset(&tb, 0, sizeof(tb));
2947
2948 dev = rtnl_create_link(net, name, name_assign_type,
2949 &vxlan_link_ops, tb);
2950 if (IS_ERR(dev))
2951 return dev;
2952
2953 err = vxlan_dev_configure(net, dev, conf);
2954 if (err < 0) {
2955 free_netdev(dev);
2956 return ERR_PTR(err);
2957 }
2958
2959 return dev;
2960}
2961EXPORT_SYMBOL_GPL(vxlan_dev_create);
2962
2963static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2964 struct nlattr *tb[], struct nlattr *data[])
2965{
2966 struct vxlan_config conf;
2967 int err;
2968
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002969 memset(&conf, 0, sizeof(conf));
Jesse Grosse277de52015-10-16 16:36:00 -07002970
2971 if (data[IFLA_VXLAN_ID])
Jiri Benc54bfd872016-02-16 21:58:58 +01002972 conf.vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002973
2974 if (data[IFLA_VXLAN_GROUP]) {
2975 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
2976 } else if (data[IFLA_VXLAN_GROUP6]) {
2977 if (!IS_ENABLED(CONFIG_IPV6))
2978 return -EPFNOSUPPORT;
2979
2980 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
2981 conf.remote_ip.sa.sa_family = AF_INET6;
2982 }
2983
2984 if (data[IFLA_VXLAN_LOCAL]) {
2985 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
2986 conf.saddr.sa.sa_family = AF_INET;
2987 } else if (data[IFLA_VXLAN_LOCAL6]) {
2988 if (!IS_ENABLED(CONFIG_IPV6))
2989 return -EPFNOSUPPORT;
2990
2991 /* TODO: respect scope id */
2992 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
2993 conf.saddr.sa.sa_family = AF_INET6;
2994 }
2995
2996 if (data[IFLA_VXLAN_LINK])
2997 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
2998
2999 if (data[IFLA_VXLAN_TOS])
3000 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3001
3002 if (data[IFLA_VXLAN_TTL])
3003 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3004
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003005 if (data[IFLA_VXLAN_LABEL])
3006 conf.label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3007 IPV6_FLOWLABEL_MASK;
3008
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003009 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
3010 conf.flags |= VXLAN_F_LEARN;
3011
3012 if (data[IFLA_VXLAN_AGEING])
3013 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3014
3015 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
3016 conf.flags |= VXLAN_F_PROXY;
3017
3018 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
3019 conf.flags |= VXLAN_F_RSC;
3020
3021 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3022 conf.flags |= VXLAN_F_L2MISS;
3023
3024 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3025 conf.flags |= VXLAN_F_L3MISS;
3026
3027 if (data[IFLA_VXLAN_LIMIT])
3028 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3029
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003030 if (data[IFLA_VXLAN_COLLECT_METADATA] &&
3031 nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3032 conf.flags |= VXLAN_F_COLLECT_METADATA;
3033
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003034 if (data[IFLA_VXLAN_PORT_RANGE]) {
3035 const struct ifla_vxlan_port_range *p
3036 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3037 conf.port_min = ntohs(p->low);
3038 conf.port_max = ntohs(p->high);
3039 }
3040
3041 if (data[IFLA_VXLAN_PORT])
3042 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3043
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003044 if (data[IFLA_VXLAN_UDP_CSUM] &&
3045 !nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3046 conf.flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003047
3048 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
3049 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3050 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3051
3052 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
3053 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3054 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3055
3056 if (data[IFLA_VXLAN_REMCSUM_TX] &&
3057 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3058 conf.flags |= VXLAN_F_REMCSUM_TX;
3059
3060 if (data[IFLA_VXLAN_REMCSUM_RX] &&
3061 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3062 conf.flags |= VXLAN_F_REMCSUM_RX;
3063
3064 if (data[IFLA_VXLAN_GBP])
3065 conf.flags |= VXLAN_F_GBP;
3066
Jiri Bence1e53142016-04-05 14:47:13 +02003067 if (data[IFLA_VXLAN_GPE])
3068 conf.flags |= VXLAN_F_GPE;
3069
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003070 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
3071 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3072
3073 err = vxlan_dev_configure(src_net, dev, &conf);
3074 switch (err) {
3075 case -ENODEV:
3076 pr_info("ifindex %d does not exist\n", conf.remote_ifindex);
3077 break;
3078
3079 case -EPERM:
3080 pr_info("IPv6 is disabled via sysctl\n");
3081 break;
3082
3083 case -EEXIST:
Jiri Benc54bfd872016-02-16 21:58:58 +01003084 pr_info("duplicate VNI %u\n", be32_to_cpu(conf.vni));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003085 break;
Jiri Bence1e53142016-04-05 14:47:13 +02003086
3087 case -EINVAL:
3088 pr_info("unsupported combination of extensions\n");
3089 break;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003090 }
3091
3092 return err;
3093}
3094
stephen hemmingerd3428942012-10-01 12:32:35 +00003095static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3096{
3097 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003098 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00003099
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003100 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003101 if (!hlist_unhashed(&vxlan->hlist))
3102 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003103 spin_unlock(&vn->sock_lock);
3104
Tom Herbert58ce31c2015-08-19 17:07:33 -07003105 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003106 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003107 unregister_netdevice_queue(dev, head);
3108}
3109
3110static size_t vxlan_get_size(const struct net_device *dev)
3111{
3112
3113 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003114 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003115 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003116 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003117 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3118 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003119 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003120 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003121 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3122 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3123 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3124 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003125 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003126 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3127 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003128 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003129 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3130 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3131 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3132 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003133 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3134 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003135 0;
3136}
3137
3138static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3139{
3140 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003141 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003142 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003143 .low = htons(vxlan->cfg.port_min),
3144 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003145 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003146
Jiri Benc54bfd872016-02-16 21:58:58 +01003147 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003148 goto nla_put_failure;
3149
Cong Wange4c7ed42013-08-31 13:44:33 +08003150 if (!vxlan_addr_any(&dst->remote_ip)) {
3151 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003152 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3153 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003154 goto nla_put_failure;
3155#if IS_ENABLED(CONFIG_IPV6)
3156 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003157 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3158 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003159 goto nla_put_failure;
3160#endif
3161 }
3162 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003163
Atzm Watanabec7995c42013-04-16 02:50:52 +00003164 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003165 goto nla_put_failure;
3166
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003167 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3168 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003169 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003170 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003171 goto nla_put_failure;
3172#if IS_ENABLED(CONFIG_IPV6)
3173 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003174 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003175 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003176 goto nla_put_failure;
3177#endif
3178 }
3179 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003180
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003181 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3182 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003183 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003184 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3185 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3186 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3187 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3188 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3189 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3190 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3191 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3192 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003193 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3194 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003195 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3196 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3197 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003198 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003199 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003200 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3201 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3202 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08003203 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3204 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3205 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3206 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3207 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003208 goto nla_put_failure;
3209
stephen hemminger05f47d62012-10-09 20:35:50 +00003210 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3211 goto nla_put_failure;
3212
Thomas Graf35114942015-01-15 03:53:55 +01003213 if (vxlan->flags & VXLAN_F_GBP &&
3214 nla_put_flag(skb, IFLA_VXLAN_GBP))
3215 goto nla_put_failure;
3216
Jiri Bence1e53142016-04-05 14:47:13 +02003217 if (vxlan->flags & VXLAN_F_GPE &&
3218 nla_put_flag(skb, IFLA_VXLAN_GPE))
3219 goto nla_put_failure;
3220
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003221 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3222 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3223 goto nla_put_failure;
3224
stephen hemmingerd3428942012-10-01 12:32:35 +00003225 return 0;
3226
3227nla_put_failure:
3228 return -EMSGSIZE;
3229}
3230
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003231static struct net *vxlan_get_link_net(const struct net_device *dev)
3232{
3233 struct vxlan_dev *vxlan = netdev_priv(dev);
3234
3235 return vxlan->net;
3236}
3237
stephen hemmingerd3428942012-10-01 12:32:35 +00003238static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3239 .kind = "vxlan",
3240 .maxtype = IFLA_VXLAN_MAX,
3241 .policy = vxlan_policy,
3242 .priv_size = sizeof(struct vxlan_dev),
3243 .setup = vxlan_setup,
3244 .validate = vxlan_validate,
3245 .newlink = vxlan_newlink,
3246 .dellink = vxlan_dellink,
3247 .get_size = vxlan_get_size,
3248 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003249 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003250};
3251
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003252static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3253 struct net_device *dev)
3254{
3255 struct vxlan_dev *vxlan, *next;
3256 LIST_HEAD(list_kill);
3257
3258 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3259 struct vxlan_rdst *dst = &vxlan->default_dst;
3260
3261 /* In case we created vxlan device with carrier
3262 * and we loose the carrier due to module unload
3263 * we also need to remove vxlan device. In other
3264 * cases, it's not necessary and remote_ifindex
3265 * is 0 here, so no matches.
3266 */
3267 if (dst->remote_ifindex == dev->ifindex)
3268 vxlan_dellink(vxlan->dev, &list_kill);
3269 }
3270
3271 unregister_netdevice_many(&list_kill);
3272}
3273
3274static int vxlan_lowerdev_event(struct notifier_block *unused,
3275 unsigned long event, void *ptr)
3276{
3277 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003278 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003279
Daniel Borkmann783c1462014-01-22 21:07:53 +01003280 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003281 vxlan_handle_lowerdev_unregister(vn, dev);
3282
3283 return NOTIFY_DONE;
3284}
3285
3286static struct notifier_block vxlan_notifier_block __read_mostly = {
3287 .notifier_call = vxlan_lowerdev_event,
3288};
3289
stephen hemmingerd3428942012-10-01 12:32:35 +00003290static __net_init int vxlan_init_net(struct net *net)
3291{
3292 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003293 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003294
stephen hemminger553675f2013-05-16 11:35:20 +00003295 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003296 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003297
stephen hemminger553675f2013-05-16 11:35:20 +00003298 for (h = 0; h < PORT_HASH_SIZE; ++h)
3299 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003300
3301 return 0;
3302}
3303
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003304static void __net_exit vxlan_exit_net(struct net *net)
3305{
3306 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3307 struct vxlan_dev *vxlan, *next;
3308 struct net_device *dev, *aux;
3309 LIST_HEAD(list);
3310
3311 rtnl_lock();
3312 for_each_netdev_safe(net, dev, aux)
3313 if (dev->rtnl_link_ops == &vxlan_link_ops)
3314 unregister_netdevice_queue(dev, &list);
3315
3316 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3317 /* If vxlan->dev is in the same netns, it has already been added
3318 * to the list by the previous loop.
3319 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003320 if (!net_eq(dev_net(vxlan->dev), net)) {
3321 gro_cells_destroy(&vxlan->gro_cells);
John W. Linville13c3ed62015-05-18 13:51:24 -04003322 unregister_netdevice_queue(vxlan->dev, &list);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003323 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003324 }
3325
3326 unregister_netdevice_many(&list);
3327 rtnl_unlock();
3328}
3329
stephen hemmingerd3428942012-10-01 12:32:35 +00003330static struct pernet_operations vxlan_net_ops = {
3331 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003332 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003333 .id = &vxlan_net_id,
3334 .size = sizeof(struct vxlan_net),
3335};
3336
3337static int __init vxlan_init_module(void)
3338{
3339 int rc;
3340
3341 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3342
Daniel Borkmann783c1462014-01-22 21:07:53 +01003343 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003344 if (rc)
3345 goto out1;
3346
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003347 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003348 if (rc)
3349 goto out2;
3350
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003351 rc = rtnl_link_register(&vxlan_link_ops);
3352 if (rc)
3353 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003354
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003355 return 0;
3356out3:
3357 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003358out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003359 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003360out1:
3361 return rc;
3362}
Cong Wang7332a132013-05-27 22:35:53 +00003363late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003364
3365static void __exit vxlan_cleanup_module(void)
3366{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003367 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003368 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003369 unregister_pernet_subsys(&vxlan_net_ops);
3370 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003371}
3372module_exit(vxlan_cleanup_module);
3373
3374MODULE_LICENSE("GPL");
3375MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003376MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003377MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003378MODULE_ALIAS_RTNL_LINK("vxlan");