blob: 6fb93b57a724897ccd7e0a7c1ccf0d1ebcaccb35 [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
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001040static bool __vxlan_sock_release_prep(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)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001045 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001046 if (!atomic_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001047 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001048
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 Sowa544a7732016-04-09 12:46:23 +02001055 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001056}
1057
Jiri Bencb1be00a2015-09-24 13:50:02 +02001058static void vxlan_sock_release(struct vxlan_dev *vxlan)
1059{
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001060 bool ipv4 = __vxlan_sock_release_prep(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001061#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001062 bool ipv6 = __vxlan_sock_release_prep(vxlan->vn6_sock);
1063#endif
1064
1065 synchronize_net();
1066
1067 if (ipv4) {
1068 udp_tunnel_sock_release(vxlan->vn4_sock->sock);
1069 kfree(vxlan->vn4_sock);
1070 }
1071
1072#if IS_ENABLED(CONFIG_IPV6)
1073 if (ipv6) {
1074 udp_tunnel_sock_release(vxlan->vn6_sock->sock);
1075 kfree(vxlan->vn6_sock);
1076 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001077#endif
1078}
1079
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001080/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001081 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001082 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001083static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001084{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001085 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001086 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1087 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001088 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001089
Cong Wange4c7ed42013-08-31 13:44:33 +08001090 if (ip->sa.sa_family == AF_INET) {
1091 struct ip_mreqn mreq = {
1092 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1093 .imr_ifindex = ifindex,
1094 };
1095
Jiri Bencb1be00a2015-09-24 13:50:02 +02001096 sk = vxlan->vn4_sock->sock->sk;
1097 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001098 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001099 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001100#if IS_ENABLED(CONFIG_IPV6)
1101 } else {
Jiri Bencb1be00a2015-09-24 13:50:02 +02001102 sk = vxlan->vn6_sock->sock->sk;
1103 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001104 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1105 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001106 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001107#endif
1108 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001109
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001110 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001111}
1112
1113/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001114static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001115{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001116 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001117 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1118 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001119 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001120
Cong Wange4c7ed42013-08-31 13:44:33 +08001121 if (ip->sa.sa_family == AF_INET) {
1122 struct ip_mreqn mreq = {
1123 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1124 .imr_ifindex = ifindex,
1125 };
1126
Jiri Bencb1be00a2015-09-24 13:50:02 +02001127 sk = vxlan->vn4_sock->sock->sk;
1128 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001129 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001130 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001131#if IS_ENABLED(CONFIG_IPV6)
1132 } else {
Jiri Bencb1be00a2015-09-24 13:50:02 +02001133 sk = vxlan->vn6_sock->sock->sk;
1134 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001135 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1136 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001137 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001138#endif
1139 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001140
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001141 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001142}
1143
Jiri Bencf14eceb2016-02-16 21:59:01 +01001144static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1145 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001146{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001147 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001148
Jiri Bencf14eceb2016-02-16 21:59:01 +01001149 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1150 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001151
Jiri Bencf14eceb2016-02-16 21:59:01 +01001152 start = vxlan_rco_start(unparsed->vx_vni);
1153 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001154
Jiri Benc7d34fa72016-03-21 17:50:05 +01001155 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001156 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001157
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001158 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1159 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001160out:
1161 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1162 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001163 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001164}
1165
Jiri Bencf14eceb2016-02-16 21:59:01 +01001166static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001167 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001168 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001169{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001170 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001171 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001172
Jiri Bencf14eceb2016-02-16 21:59:01 +01001173 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1174 goto out;
1175
Jiri Benc3288af02016-02-16 21:59:00 +01001176 md->gbp = ntohs(gbp->policy_id);
1177
Jiri Benc10a5af22016-02-23 18:02:59 +01001178 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001179 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001180 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001181 tun_dst->u.tun_info.options_len = sizeof(*md);
1182 }
Jiri Benc3288af02016-02-16 21:59:00 +01001183 if (gbp->dont_learn)
1184 md->gbp |= VXLAN_GBP_DONT_LEARN;
1185
1186 if (gbp->policy_applied)
1187 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001188
Jiri Benc64f87d32016-02-23 18:02:55 +01001189 /* In flow-based mode, GBP is carried in dst_metadata */
1190 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1191 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001192out:
1193 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001194}
1195
Jiri Bence1e53142016-04-05 14:47:13 +02001196static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001197 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001198 struct sk_buff *skb, u32 vxflags)
1199{
1200 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1201
1202 /* Need to have Next Protocol set for interfaces in GPE mode. */
1203 if (!gpe->np_applied)
1204 return false;
1205 /* "The initial version is 0. If a receiver does not support the
1206 * version indicated it MUST drop the packet.
1207 */
1208 if (gpe->version != 0)
1209 return false;
1210 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1211 * processing MUST occur." However, we don't implement OAM
1212 * processing, thus drop the packet.
1213 */
1214 if (gpe->oam_flag)
1215 return false;
1216
1217 switch (gpe->next_protocol) {
1218 case VXLAN_GPE_NP_IPV4:
1219 *protocol = htons(ETH_P_IP);
1220 break;
1221 case VXLAN_GPE_NP_IPV6:
1222 *protocol = htons(ETH_P_IPV6);
1223 break;
1224 case VXLAN_GPE_NP_ETHERNET:
1225 *protocol = htons(ETH_P_TEB);
1226 break;
1227 default:
1228 return false;
1229 }
1230
1231 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1232 return true;
1233}
1234
Jiri Benc1ab016e2016-02-23 18:02:56 +01001235static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1236 struct vxlan_sock *vs,
1237 struct sk_buff *skb)
Thomas Graf614732e2015-07-21 10:44:06 +02001238{
Thomas Graf614732e2015-07-21 10:44:06 +02001239 union vxlan_addr saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001240
Thomas Graf614732e2015-07-21 10:44:06 +02001241 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001242 skb->protocol = eth_type_trans(skb, vxlan->dev);
1243 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1244
1245 /* Ignore packet loops (and multicast echo) */
1246 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001247 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001248
Jiri Benc760c6802016-02-23 18:02:57 +01001249 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001250 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001251 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001252 saddr.sa.sa_family = AF_INET;
1253#if IS_ENABLED(CONFIG_IPV6)
1254 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001255 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001256 saddr.sa.sa_family = AF_INET6;
1257#endif
1258 }
1259
Jiri Benc1ab016e2016-02-23 18:02:56 +01001260 if ((vxlan->flags & VXLAN_F_LEARN) &&
1261 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1262 return false;
1263
1264 return true;
1265}
1266
Jiri Benc760c6802016-02-23 18:02:57 +01001267static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1268 struct sk_buff *skb)
1269{
1270 int err = 0;
1271
1272 if (vxlan_get_sk_family(vs) == AF_INET)
1273 err = IP_ECN_decapsulate(oiph, skb);
1274#if IS_ENABLED(CONFIG_IPV6)
1275 else
1276 err = IP6_ECN_decapsulate(oiph, skb);
1277#endif
1278
1279 if (unlikely(err) && log_ecn_error) {
1280 if (vxlan_get_sk_family(vs) == AF_INET)
1281 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1282 &((struct iphdr *)oiph)->saddr,
1283 ((struct iphdr *)oiph)->tos);
1284 else
1285 net_info_ratelimited("non-ECT from %pI6\n",
1286 &((struct ipv6hdr *)oiph)->saddr);
1287 }
1288 return err <= 1;
1289}
1290
stephen hemmingerd3428942012-10-01 12:32:35 +00001291/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001292static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001293{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001294 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001295 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001296 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001297 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001298 struct vxlan_metadata _md;
1299 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001300 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001301 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001302 void *oiph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001303
Jiri Bence1e53142016-04-05 14:47:13 +02001304 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001305 if (!pskb_may_pull(skb, VXLAN_HLEN))
Jiri Benc288b01c2016-02-16 21:59:02 +01001306 return 1;
stephen hemmingerd3428942012-10-01 12:32:35 +00001307
Jiri Bencf14eceb2016-02-16 21:59:01 +01001308 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001309 /* VNI flag always required to be set */
1310 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1311 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1312 ntohl(vxlan_hdr(skb)->vx_flags),
1313 ntohl(vxlan_hdr(skb)->vx_vni));
1314 /* Return non vxlan pkt */
1315 return 1;
stephen hemmingerd3428942012-10-01 12:32:35 +00001316 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001317 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1318 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001319
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001320 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001321 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001322 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001323
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001324 vxlan = vxlan_vs_find_vni(vs, vxlan_vni(vxlan_hdr(skb)->vx_vni));
1325 if (!vxlan)
1326 goto drop;
1327
Jiri Bence1e53142016-04-05 14:47:13 +02001328 /* For backwards compatibility, only allow reserved fields to be
1329 * used by VXLAN extensions if explicitly requested.
1330 */
1331 if (vs->flags & VXLAN_F_GPE) {
1332 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1333 goto drop;
1334 raw_proto = true;
1335 }
1336
1337 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1338 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1339 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001340
Thomas Grafee122c72015-07-21 10:43:58 +02001341 if (vxlan_collect_metadata(vs)) {
Jiri Benc07dabf22016-02-18 19:19:29 +01001342 __be32 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
Jiri Benc10a5af22016-02-23 18:02:59 +01001343 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001344
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001345 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Jiri Benc07dabf22016-02-18 19:19:29 +01001346 vxlan_vni_to_tun_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001347
Thomas Grafee122c72015-07-21 10:43:58 +02001348 if (!tun_dst)
1349 goto drop;
1350
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001351 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001352
1353 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001354 } else {
1355 memset(md, 0, sizeof(*md));
1356 }
1357
Jiri Bencf14eceb2016-02-16 21:59:01 +01001358 if (vs->flags & VXLAN_F_REMCSUM_RX)
1359 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1360 goto drop;
1361 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001362 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001363 /* Note that GBP and GPE can never be active together. This is
1364 * ensured in vxlan_dev_configure.
1365 */
Thomas Graf35114942015-01-15 03:53:55 +01001366
Jiri Bencf14eceb2016-02-16 21:59:01 +01001367 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001368 /* If there are any unprocessed flags remaining treat
1369 * this as a malformed packet. This behavior diverges from
1370 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1371 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001372 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001373 * is more robust and provides a little more security in
1374 * adding extensions to VXLAN.
1375 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001376 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001377 }
1378
Jiri Bence1e53142016-04-05 14:47:13 +02001379 if (!raw_proto) {
1380 if (!vxlan_set_mac(vxlan, vs, skb))
1381 goto drop;
1382 } else {
1383 skb->dev = vxlan->dev;
1384 skb->pkt_type = PACKET_HOST;
1385 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001386
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001387 oiph = skb_network_header(skb);
1388 skb_reset_network_header(skb);
1389
1390 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1391 ++vxlan->dev->stats.rx_frame_errors;
1392 ++vxlan->dev->stats.rx_errors;
1393 goto drop;
1394 }
1395
1396 stats = this_cpu_ptr(vxlan->dev->tstats);
1397 u64_stats_update_begin(&stats->syncp);
1398 stats->rx_packets++;
1399 stats->rx_bytes += skb->len;
1400 u64_stats_update_end(&stats->syncp);
1401
1402 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001403 return 0;
1404
1405drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001406 /* Consume bad packet */
1407 kfree_skb(skb);
1408 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001409}
1410
David Stevense4f67ad2012-11-20 02:50:14 +00001411static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1412{
1413 struct vxlan_dev *vxlan = netdev_priv(dev);
1414 struct arphdr *parp;
1415 u8 *arpptr, *sha;
1416 __be32 sip, tip;
1417 struct neighbour *n;
1418
1419 if (dev->flags & IFF_NOARP)
1420 goto out;
1421
1422 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1423 dev->stats.tx_dropped++;
1424 goto out;
1425 }
1426 parp = arp_hdr(skb);
1427
1428 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1429 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1430 parp->ar_pro != htons(ETH_P_IP) ||
1431 parp->ar_op != htons(ARPOP_REQUEST) ||
1432 parp->ar_hln != dev->addr_len ||
1433 parp->ar_pln != 4)
1434 goto out;
1435 arpptr = (u8 *)parp + sizeof(struct arphdr);
1436 sha = arpptr;
1437 arpptr += dev->addr_len; /* sha */
1438 memcpy(&sip, arpptr, sizeof(sip));
1439 arpptr += sizeof(sip);
1440 arpptr += dev->addr_len; /* tha */
1441 memcpy(&tip, arpptr, sizeof(tip));
1442
1443 if (ipv4_is_loopback(tip) ||
1444 ipv4_is_multicast(tip))
1445 goto out;
1446
1447 n = neigh_lookup(&arp_tbl, &tip, dev);
1448
1449 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001450 struct vxlan_fdb *f;
1451 struct sk_buff *reply;
1452
1453 if (!(n->nud_state & NUD_CONNECTED)) {
1454 neigh_release(n);
1455 goto out;
1456 }
1457
1458 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001459 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001460 /* bridge-local neighbor */
1461 neigh_release(n);
1462 goto out;
1463 }
1464
1465 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1466 n->ha, sha);
1467
1468 neigh_release(n);
1469
David Stevens73461352014-03-18 12:32:29 -04001470 if (reply == NULL)
1471 goto out;
1472
David Stevense4f67ad2012-11-20 02:50:14 +00001473 skb_reset_mac_header(reply);
1474 __skb_pull(reply, skb_network_offset(reply));
1475 reply->ip_summed = CHECKSUM_UNNECESSARY;
1476 reply->pkt_type = PACKET_HOST;
1477
1478 if (netif_rx_ni(reply) == NET_RX_DROP)
1479 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001480 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1481 union vxlan_addr ipa = {
1482 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001483 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001484 };
1485
1486 vxlan_ip_miss(dev, &ipa);
1487 }
David Stevense4f67ad2012-11-20 02:50:14 +00001488out:
1489 consume_skb(skb);
1490 return NETDEV_TX_OK;
1491}
1492
Cong Wangf564f452013-08-31 13:44:36 +08001493#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001494static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1495 struct neighbour *n, bool isrouter)
1496{
1497 struct net_device *dev = request->dev;
1498 struct sk_buff *reply;
1499 struct nd_msg *ns, *na;
1500 struct ipv6hdr *pip6;
1501 u8 *daddr;
1502 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1503 int ns_olen;
1504 int i, len;
1505
1506 if (dev == NULL)
1507 return NULL;
1508
1509 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1510 sizeof(*na) + na_olen + dev->needed_tailroom;
1511 reply = alloc_skb(len, GFP_ATOMIC);
1512 if (reply == NULL)
1513 return NULL;
1514
1515 reply->protocol = htons(ETH_P_IPV6);
1516 reply->dev = dev;
1517 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1518 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001519 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001520
1521 ns = (struct nd_msg *)skb_transport_header(request);
1522
1523 daddr = eth_hdr(request)->h_source;
1524 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1525 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1526 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1527 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1528 break;
1529 }
1530 }
1531
1532 /* Ethernet header */
1533 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1534 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1535 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1536 reply->protocol = htons(ETH_P_IPV6);
1537
1538 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001539 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001540 skb_put(reply, sizeof(struct ipv6hdr));
1541
1542 /* IPv6 header */
1543
1544 pip6 = ipv6_hdr(reply);
1545 memset(pip6, 0, sizeof(struct ipv6hdr));
1546 pip6->version = 6;
1547 pip6->priority = ipv6_hdr(request)->priority;
1548 pip6->nexthdr = IPPROTO_ICMPV6;
1549 pip6->hop_limit = 255;
1550 pip6->daddr = ipv6_hdr(request)->saddr;
1551 pip6->saddr = *(struct in6_addr *)n->primary_key;
1552
1553 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001554 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001555
1556 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1557
1558 /* Neighbor Advertisement */
1559 memset(na, 0, sizeof(*na)+na_olen);
1560 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1561 na->icmph.icmp6_router = isrouter;
1562 na->icmph.icmp6_override = 1;
1563 na->icmph.icmp6_solicited = 1;
1564 na->target = ns->target;
1565 ether_addr_copy(&na->opt[2], n->ha);
1566 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1567 na->opt[1] = na_olen >> 3;
1568
1569 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1570 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1571 csum_partial(na, sizeof(*na)+na_olen, 0));
1572
1573 pip6->payload_len = htons(sizeof(*na)+na_olen);
1574
1575 skb_push(reply, sizeof(struct ipv6hdr));
1576
1577 reply->ip_summed = CHECKSUM_UNNECESSARY;
1578
1579 return reply;
1580}
1581
Cong Wangf564f452013-08-31 13:44:36 +08001582static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1583{
1584 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001585 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001586 const struct ipv6hdr *iphdr;
1587 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001588 struct neighbour *n;
1589 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001590
1591 in6_dev = __in6_dev_get(dev);
1592 if (!in6_dev)
1593 goto out;
1594
Cong Wangf564f452013-08-31 13:44:36 +08001595 iphdr = ipv6_hdr(skb);
1596 saddr = &iphdr->saddr;
1597 daddr = &iphdr->daddr;
1598
Cong Wangf564f452013-08-31 13:44:36 +08001599 msg = (struct nd_msg *)skb_transport_header(skb);
1600 if (msg->icmph.icmp6_code != 0 ||
1601 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1602 goto out;
1603
David Stevens4b29dba2014-03-24 10:39:58 -04001604 if (ipv6_addr_loopback(daddr) ||
1605 ipv6_addr_is_multicast(&msg->target))
1606 goto out;
1607
1608 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001609
1610 if (n) {
1611 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001612 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001613
1614 if (!(n->nud_state & NUD_CONNECTED)) {
1615 neigh_release(n);
1616 goto out;
1617 }
1618
1619 f = vxlan_find_mac(vxlan, n->ha);
1620 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1621 /* bridge-local neighbor */
1622 neigh_release(n);
1623 goto out;
1624 }
1625
David Stevens4b29dba2014-03-24 10:39:58 -04001626 reply = vxlan_na_create(skb, n,
1627 !!(f ? f->flags & NTF_ROUTER : 0));
1628
Cong Wangf564f452013-08-31 13:44:36 +08001629 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001630
1631 if (reply == NULL)
1632 goto out;
1633
1634 if (netif_rx_ni(reply) == NET_RX_DROP)
1635 dev->stats.rx_dropped++;
1636
Cong Wangf564f452013-08-31 13:44:36 +08001637 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001638 union vxlan_addr ipa = {
1639 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001640 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001641 };
1642
Cong Wangf564f452013-08-31 13:44:36 +08001643 vxlan_ip_miss(dev, &ipa);
1644 }
1645
1646out:
1647 consume_skb(skb);
1648 return NETDEV_TX_OK;
1649}
1650#endif
1651
David Stevense4f67ad2012-11-20 02:50:14 +00001652static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1653{
1654 struct vxlan_dev *vxlan = netdev_priv(dev);
1655 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001656
1657 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1658 return false;
1659
1660 n = NULL;
1661 switch (ntohs(eth_hdr(skb)->h_proto)) {
1662 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001663 {
1664 struct iphdr *pip;
1665
David Stevense4f67ad2012-11-20 02:50:14 +00001666 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1667 return false;
1668 pip = ip_hdr(skb);
1669 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001670 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1671 union vxlan_addr ipa = {
1672 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001673 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001674 };
1675
1676 vxlan_ip_miss(dev, &ipa);
1677 return false;
1678 }
1679
David Stevense4f67ad2012-11-20 02:50:14 +00001680 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001681 }
1682#if IS_ENABLED(CONFIG_IPV6)
1683 case ETH_P_IPV6:
1684 {
1685 struct ipv6hdr *pip6;
1686
1687 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1688 return false;
1689 pip6 = ipv6_hdr(skb);
1690 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1691 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1692 union vxlan_addr ipa = {
1693 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001694 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001695 };
1696
1697 vxlan_ip_miss(dev, &ipa);
1698 return false;
1699 }
1700
1701 break;
1702 }
1703#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001704 default:
1705 return false;
1706 }
1707
1708 if (n) {
1709 bool diff;
1710
Joe Perches7367d0b2013-09-01 11:51:23 -07001711 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001712 if (diff) {
1713 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1714 dev->addr_len);
1715 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1716 }
1717 neigh_release(n);
1718 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001719 }
1720
David Stevense4f67ad2012-11-20 02:50:14 +00001721 return false;
1722}
1723
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001724static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001725 struct vxlan_metadata *md)
1726{
1727 struct vxlanhdr_gbp *gbp;
1728
Thomas Grafdb79a622015-02-04 17:00:04 +01001729 if (!md->gbp)
1730 return;
1731
Thomas Graf35114942015-01-15 03:53:55 +01001732 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001733 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001734
1735 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1736 gbp->dont_learn = 1;
1737
1738 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1739 gbp->policy_applied = 1;
1740
1741 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1742}
1743
Jiri Bence1e53142016-04-05 14:47:13 +02001744static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1745 __be16 protocol)
1746{
1747 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1748
1749 gpe->np_applied = 1;
1750
1751 switch (protocol) {
1752 case htons(ETH_P_IP):
1753 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1754 return 0;
1755 case htons(ETH_P_IPV6):
1756 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1757 return 0;
1758 case htons(ETH_P_TEB):
1759 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1760 return 0;
1761 }
1762 return -EPFNOSUPPORT;
1763}
1764
Jiri Bencf491e562016-02-02 18:09:16 +01001765static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1766 int iphdr_len, __be32 vni,
1767 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001768 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001769{
Cong Wange4c7ed42013-08-31 13:44:33 +08001770 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001771 int min_headroom;
1772 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001773 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001774 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001775
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001776 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001777 skb->ip_summed == CHECKSUM_PARTIAL) {
1778 int csum_start = skb_checksum_start_offset(skb);
1779
1780 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1781 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1782 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001783 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001784 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001785 }
1786
Cong Wange4c7ed42013-08-31 13:44:33 +08001787 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
Jiri Bencf491e562016-02-02 18:09:16 +01001788 + VXLAN_HLEN + iphdr_len
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001789 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001790
1791 /* Need space for new headers (invalidates iph ptr) */
1792 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001793 if (unlikely(err))
1794 goto out_free;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001795
Jiri Pirko59682502014-11-19 14:04:59 +01001796 skb = vlan_hwaccel_push_inside(skb);
1797 if (WARN_ON(!skb))
1798 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001799
Alexander Duyckaed069d2016-04-14 15:33:37 -04001800 err = iptunnel_handle_offloads(skb, type);
1801 if (err)
1802 goto out_free;
Jesse Grossb736a622015-04-09 11:19:14 -07001803
Pravin B Shelar49560532013-08-19 11:23:17 -07001804 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001805 vxh->vx_flags = VXLAN_HF_VNI;
1806 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001807
Tom Herbertdfd86452015-01-12 17:00:38 -08001808 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001809 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001810
Jiri Benc54bfd872016-02-16 21:58:58 +01001811 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1812 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1813 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001814
1815 if (!skb_is_gso(skb)) {
1816 skb->ip_summed = CHECKSUM_NONE;
1817 skb->encapsulation = 0;
1818 }
1819 }
1820
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001821 if (vxflags & VXLAN_F_GBP)
1822 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001823 if (vxflags & VXLAN_F_GPE) {
1824 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1825 if (err < 0)
1826 goto out_free;
1827 inner_protocol = skb->protocol;
1828 }
Thomas Graf35114942015-01-15 03:53:55 +01001829
Jiri Bence1e53142016-04-05 14:47:13 +02001830 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001831 return 0;
Jiri Bence1e53142016-04-05 14:47:13 +02001832
1833out_free:
1834 kfree_skb(skb);
1835 return err;
Pravin B Shelar49560532013-08-19 11:23:17 -07001836}
Pravin B Shelar49560532013-08-19 11:23:17 -07001837
Jiri Benc1a8496b2016-02-02 18:09:14 +01001838static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan,
1839 struct sk_buff *skb, int oif, u8 tos,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001840 __be32 daddr, __be32 *saddr,
1841 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001842 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01001843{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001844 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01001845 struct rtable *rt = NULL;
1846 struct flowi4 fl4;
1847
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001848 if (tos && !info)
1849 use_cache = false;
1850 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001851 rt = dst_cache_get_ip4(dst_cache, saddr);
1852 if (rt)
1853 return rt;
1854 }
1855
Jiri Benc1a8496b2016-02-02 18:09:14 +01001856 memset(&fl4, 0, sizeof(fl4));
1857 fl4.flowi4_oif = oif;
1858 fl4.flowi4_tos = RT_TOS(tos);
1859 fl4.flowi4_mark = skb->mark;
1860 fl4.flowi4_proto = IPPROTO_UDP;
1861 fl4.daddr = daddr;
1862 fl4.saddr = vxlan->cfg.saddr.sin.sin_addr.s_addr;
1863
1864 rt = ip_route_output_key(vxlan->net, &fl4);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001865 if (!IS_ERR(rt)) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01001866 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001867 if (use_cache)
1868 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
1869 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01001870 return rt;
1871}
1872
Jiri Bence5d4b292015-12-07 13:04:30 +01001873#if IS_ENABLED(CONFIG_IPV6)
1874static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
Daniel Borkmann14006152016-03-04 15:15:08 +01001875 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001876 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01001877 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001878 struct in6_addr *saddr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001879 struct dst_cache *dst_cache,
1880 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01001881{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001882 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01001883 struct dst_entry *ndst;
1884 struct flowi6 fl6;
1885 int err;
1886
Daniel Borkmann14006152016-03-04 15:15:08 +01001887 if (tos && !info)
1888 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001889 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001890 ndst = dst_cache_get_ip6(dst_cache, saddr);
1891 if (ndst)
1892 return ndst;
1893 }
1894
Jiri Bence5d4b292015-12-07 13:04:30 +01001895 memset(&fl6, 0, sizeof(fl6));
1896 fl6.flowi6_oif = oif;
1897 fl6.daddr = *daddr;
1898 fl6.saddr = vxlan->cfg.saddr.sin6.sin6_addr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01001899 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01001900 fl6.flowi6_mark = skb->mark;
1901 fl6.flowi6_proto = IPPROTO_UDP;
1902
1903 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
1904 vxlan->vn6_sock->sock->sk,
1905 &ndst, &fl6);
1906 if (err < 0)
1907 return ERR_PTR(err);
1908
1909 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001910 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001911 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01001912 return ndst;
1913}
1914#endif
1915
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001916/* Bypass encapsulation if the destination is local */
1917static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1918 struct vxlan_dev *dst_vxlan)
1919{
Li RongQing8f849852014-01-04 13:57:59 +08001920 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001921 union vxlan_addr loopback;
1922 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001923 struct net_device *dev = skb->dev;
1924 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001925
Li RongQing8f849852014-01-04 13:57:59 +08001926 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1927 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001928 skb->pkt_type = PACKET_HOST;
1929 skb->encapsulation = 0;
1930 skb->dev = dst_vxlan->dev;
1931 __skb_pull(skb, skb_network_offset(skb));
1932
Cong Wange4c7ed42013-08-31 13:44:33 +08001933 if (remote_ip->sa.sa_family == AF_INET) {
1934 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1935 loopback.sa.sa_family = AF_INET;
1936#if IS_ENABLED(CONFIG_IPV6)
1937 } else {
1938 loopback.sin6.sin6_addr = in6addr_loopback;
1939 loopback.sa.sa_family = AF_INET6;
1940#endif
1941 }
1942
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001943 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001944 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001945
1946 u64_stats_update_begin(&tx_stats->syncp);
1947 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001948 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001949 u64_stats_update_end(&tx_stats->syncp);
1950
1951 if (netif_rx(skb) == NET_RX_SUCCESS) {
1952 u64_stats_update_begin(&rx_stats->syncp);
1953 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001954 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001955 u64_stats_update_end(&rx_stats->syncp);
1956 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001957 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001958 }
1959}
1960
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001961static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1962 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001963{
Paolo Abenid71785f2016-02-12 15:43:57 +01001964 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02001965 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00001966 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001967 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001968 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001969 const struct iphdr *old_iph;
Cong Wange4c7ed42013-08-31 13:44:33 +08001970 union vxlan_addr *dst;
Thomas Grafee122c72015-07-21 10:43:58 +02001971 union vxlan_addr remote_ip;
1972 struct vxlan_metadata _md;
1973 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001974 __be16 src_port = 0, dst_port;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001975 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00001976 __be16 df = 0;
1977 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001978 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02001979 u32 flags = vxlan->flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001980 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01001981 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00001982
Jiri Benc61adedf2015-08-20 13:56:25 +02001983 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02001984
Thomas Grafee122c72015-07-21 10:43:58 +02001985 if (rdst) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001986 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Thomas Grafee122c72015-07-21 10:43:58 +02001987 vni = rdst->remote_vni;
1988 dst = &rdst->remote_ip;
Paolo Abenid71785f2016-02-12 15:43:57 +01001989 dst_cache = &rdst->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02001990 } else {
1991 if (!info) {
1992 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1993 dev->name);
1994 goto drop;
1995 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001996 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
Jiri Benc54bfd872016-02-16 21:58:58 +01001997 vni = vxlan_tun_id_to_vni(info->key.tun_id);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001998 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
1999 if (remote_ip.sa.sa_family == AF_INET)
Jiri Benca725e512015-08-20 13:56:30 +02002000 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2001 else
2002 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
Thomas Grafee122c72015-07-21 10:43:58 +02002003 dst = &remote_ip;
Paolo Abenid71785f2016-02-12 15:43:57 +01002004 dst_cache = &info->dst_cache;
Thomas Grafee122c72015-07-21 10:43:58 +02002005 }
David Stevense4f67ad2012-11-20 02:50:14 +00002006
Cong Wange4c7ed42013-08-31 13:44:33 +08002007 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00002008 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00002009 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002010 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002011 return;
David Stevense4f67ad2012-11-20 02:50:14 +00002012 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002013 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00002014 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00002015
stephen hemmingerd3428942012-10-01 12:32:35 +00002016 old_iph = ip_hdr(skb);
2017
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002018 ttl = vxlan->cfg.ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08002019 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00002020 ttl = 1;
2021
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002022 tos = vxlan->cfg.tos;
stephen hemmingerd3428942012-10-01 12:32:35 +00002023 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00002024 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002025
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002026 label = vxlan->cfg.label;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002027 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2028 vxlan->cfg.port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00002029
Jiri Benca725e512015-08-20 13:56:30 +02002030 if (info) {
Jiri Benca725e512015-08-20 13:56:30 +02002031 ttl = info->key.ttl;
2032 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002033 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002034 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002035
2036 if (info->options_len)
Pravin B Shelar4c222792015-08-30 18:09:38 -07002037 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002038 } else {
2039 md->gbp = skb->mark;
2040 }
2041
Cong Wange4c7ed42013-08-31 13:44:33 +08002042 if (dst->sa.sa_family == AF_INET) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01002043 __be32 saddr;
2044
Jiri Bencb1be00a2015-09-24 13:50:02 +02002045 if (!vxlan->vn4_sock)
2046 goto drop;
2047 sk = vxlan->vn4_sock->sock->sk;
2048
Jiri Benc1a8496b2016-02-02 18:09:14 +01002049 rt = vxlan_get_route(vxlan, skb,
2050 rdst ? rdst->remote_ifindex : 0, tos,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002051 dst->sin.sin_addr.s_addr, &saddr,
Paolo Abenid71785f2016-02-12 15:43:57 +01002052 dst_cache, info);
Cong Wange4c7ed42013-08-31 13:44:33 +08002053 if (IS_ERR(rt)) {
2054 netdev_dbg(dev, "no route to %pI4\n",
2055 &dst->sin.sin_addr.s_addr);
2056 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002057 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002058 }
2059
2060 if (rt->dst.dev == dev) {
2061 netdev_dbg(dev, "circular route to %pI4\n",
2062 &dst->sin.sin_addr.s_addr);
2063 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08002064 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002065 }
2066
2067 /* Bypass encapsulation if the destination is local */
2068 if (rt->rt_flags & RTCF_LOCAL &&
2069 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2070 struct vxlan_dev *dst_vxlan;
2071
2072 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002073 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002074 dst->sa.sa_family, dst_port,
2075 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002076 if (!dst_vxlan)
2077 goto tx_error;
2078 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2079 return;
2080 }
2081
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002082 if (!info)
2083 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2084 else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
2085 df = htons(IP_DF);
2086
Cong Wange4c7ed42013-08-31 13:44:33 +08002087 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2088 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Jiri Bencf491e562016-02-02 18:09:16 +01002089 err = vxlan_build_skb(skb, &rt->dst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002090 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002091 if (err < 0)
2092 goto xmit_tx_error;
2093
2094 udp_tunnel_xmit_skb(rt, sk, skb, saddr,
2095 dst->sin.sin_addr.s_addr, tos, ttl, df,
2096 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002097#if IS_ENABLED(CONFIG_IPV6)
2098 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08002099 struct dst_entry *ndst;
Jiri Bence5d4b292015-12-07 13:04:30 +01002100 struct in6_addr saddr;
Jiri Benc6f264e42015-08-20 13:56:29 +02002101 u32 rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002102
Jiri Bencb1be00a2015-09-24 13:50:02 +02002103 if (!vxlan->vn6_sock)
2104 goto drop;
2105 sk = vxlan->vn6_sock->sock->sk;
2106
Jiri Bence5d4b292015-12-07 13:04:30 +01002107 ndst = vxlan6_get_route(vxlan, skb,
Daniel Borkmann14006152016-03-04 15:15:08 +01002108 rdst ? rdst->remote_ifindex : 0, tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002109 label, &dst->sin6.sin6_addr, &saddr,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002110 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002111 if (IS_ERR(ndst)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002112 netdev_dbg(dev, "no route to %pI6\n",
2113 &dst->sin6.sin6_addr);
2114 dev->stats.tx_carrier_errors++;
2115 goto tx_error;
2116 }
2117
2118 if (ndst->dev == dev) {
2119 netdev_dbg(dev, "circular route to %pI6\n",
2120 &dst->sin6.sin6_addr);
2121 dst_release(ndst);
2122 dev->stats.collisions++;
2123 goto tx_error;
2124 }
2125
2126 /* Bypass encapsulation if the destination is local */
Jiri Benc6f264e42015-08-20 13:56:29 +02002127 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2128 if (rt6i_flags & RTF_LOCAL &&
2129 !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Cong Wange4c7ed42013-08-31 13:44:33 +08002130 struct vxlan_dev *dst_vxlan;
2131
2132 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002133 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002134 dst->sa.sa_family, dst_port,
2135 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002136 if (!dst_vxlan)
2137 goto tx_error;
2138 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2139 return;
2140 }
2141
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002142 if (!info)
2143 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Jesse Gross35e2d112016-01-20 16:22:47 -08002144
Daniel Borkmann14006152016-03-04 15:15:08 +01002145 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002146 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002147 skb_scrub_packet(skb, xnet);
2148 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002149 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002150 if (err < 0) {
2151 dst_release(ndst);
2152 return;
2153 }
2154 udp_tunnel6_xmit_skb(ndst, sk, skb, dev,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002155 &saddr, &dst->sin6.sin6_addr, tos, ttl,
2156 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002157#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002158 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002159
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002160 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002161
2162drop:
2163 dev->stats.tx_dropped++;
2164 goto tx_free;
2165
Jiri Bencf491e562016-02-02 18:09:16 +01002166xmit_tx_error:
2167 /* skb is already freed. */
2168 skb = NULL;
Pravin B Shelar49560532013-08-19 11:23:17 -07002169rt_tx_error:
2170 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002171tx_error:
2172 dev->stats.tx_errors++;
2173tx_free:
2174 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002175}
2176
David Stevens66817122013-03-15 04:35:51 +00002177/* Transmit local packets over Vxlan
2178 *
2179 * Outer IP header inherits ECN and DF from inner header.
2180 * Outer UDP destination is the VXLAN assigned port.
2181 * source port is based on hash of flow
2182 */
2183static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2184{
2185 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002186 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002187 struct ethhdr *eth;
2188 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002189 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002190 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002191
Jiri Benc61adedf2015-08-20 13:56:25 +02002192 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002193
David Stevens66817122013-03-15 04:35:51 +00002194 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002195
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002196 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
2197 if (info && info->mode & IP_TUNNEL_INFO_TX)
2198 vxlan_xmit_one(skb, dev, NULL, false);
2199 else
2200 kfree_skb(skb);
2201 return NETDEV_TX_OK;
2202 }
2203
2204 if (vxlan->flags & VXLAN_F_PROXY) {
2205 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002206 if (ntohs(eth->h_proto) == ETH_P_ARP)
2207 return arp_reduce(dev, skb);
2208#if IS_ENABLED(CONFIG_IPV6)
2209 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002210 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2211 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002212 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2213 struct nd_msg *msg;
2214
2215 msg = (struct nd_msg *)skb_transport_header(skb);
2216 if (msg->icmph.icmp6_code == 0 &&
2217 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2218 return neigh_reduce(dev, skb);
2219 }
2220#endif
2221 }
David Stevens66817122013-03-15 04:35:51 +00002222
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002223 eth = eth_hdr(skb);
David Stevens66817122013-03-15 04:35:51 +00002224 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002225 did_rsc = false;
2226
2227 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002228 (ntohs(eth->h_proto) == ETH_P_IP ||
2229 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002230 did_rsc = route_shortcircuit(dev, skb);
2231 if (did_rsc)
2232 f = vxlan_find_mac(vxlan, eth->h_dest);
2233 }
2234
David Stevens66817122013-03-15 04:35:51 +00002235 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002236 f = vxlan_find_mac(vxlan, all_zeros_mac);
2237 if (f == NULL) {
2238 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2239 !is_multicast_ether_addr(eth->h_dest))
2240 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002241
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002242 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002243 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002244 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002245 }
David Stevens66817122013-03-15 04:35:51 +00002246 }
2247
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002248 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2249 struct sk_buff *skb1;
2250
Eric Dumazet8f646c92014-01-06 09:54:31 -08002251 if (!fdst) {
2252 fdst = rdst;
2253 continue;
2254 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002255 skb1 = skb_clone(skb, GFP_ATOMIC);
2256 if (skb1)
2257 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2258 }
2259
Eric Dumazet8f646c92014-01-06 09:54:31 -08002260 if (fdst)
2261 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2262 else
2263 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002264 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002265}
2266
stephen hemmingerd3428942012-10-01 12:32:35 +00002267/* Walk the forwarding table and purge stale entries */
2268static void vxlan_cleanup(unsigned long arg)
2269{
2270 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2271 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2272 unsigned int h;
2273
2274 if (!netif_running(vxlan->dev))
2275 return;
2276
stephen hemmingerd3428942012-10-01 12:32:35 +00002277 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2278 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002279
2280 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002281 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2282 struct vxlan_fdb *f
2283 = container_of(p, struct vxlan_fdb, hlist);
2284 unsigned long timeout;
2285
stephen hemminger3c172862012-10-26 06:24:34 +00002286 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002287 continue;
2288
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002289 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002290 if (time_before_eq(timeout, jiffies)) {
2291 netdev_dbg(vxlan->dev,
2292 "garbage collect %pM\n",
2293 f->eth_addr);
2294 f->state = NUD_STALE;
2295 vxlan_fdb_destroy(vxlan, f);
2296 } else if (time_before(timeout, next_timer))
2297 next_timer = timeout;
2298 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002299 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002300 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002301
2302 mod_timer(&vxlan->age_timer, next_timer);
2303}
2304
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002305static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2306{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002307 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002308 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002309
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002310 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002311 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002312 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002313}
2314
stephen hemmingerd3428942012-10-01 12:32:35 +00002315/* Setup stats when device is created */
2316static int vxlan_init(struct net_device *dev)
2317{
WANG Cong1c213bd2014-02-13 11:46:28 -08002318 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002319 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002320 return -ENOMEM;
2321
2322 return 0;
2323}
2324
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002325static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002326{
2327 struct vxlan_fdb *f;
2328
2329 spin_lock_bh(&vxlan->hash_lock);
2330 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2331 if (f)
2332 vxlan_fdb_destroy(vxlan, f);
2333 spin_unlock_bh(&vxlan->hash_lock);
2334}
2335
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002336static void vxlan_uninit(struct net_device *dev)
2337{
2338 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002339
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002340 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002341
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002342 free_percpu(dev->tstats);
2343}
2344
stephen hemmingerd3428942012-10-01 12:32:35 +00002345/* Start ageing timer and join group when device is brought up */
2346static int vxlan_open(struct net_device *dev)
2347{
2348 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002349 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002350
Jiri Benc205f3562015-09-24 13:50:01 +02002351 ret = vxlan_sock_add(vxlan);
2352 if (ret < 0)
2353 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002354
Gao feng79d4a942013-12-10 16:37:32 +08002355 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002356 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002357 if (ret == -EADDRINUSE)
2358 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002359 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002360 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002361 return ret;
2362 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002363 }
2364
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002365 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002366 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2367
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002368 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002369}
2370
2371/* Purge the forwarding table */
2372static void vxlan_flush(struct vxlan_dev *vxlan)
2373{
Cong Wang31fec5a2013-05-27 22:35:52 +00002374 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002375
2376 spin_lock_bh(&vxlan->hash_lock);
2377 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2378 struct hlist_node *p, *n;
2379 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2380 struct vxlan_fdb *f
2381 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002382 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2383 if (!is_zero_ether_addr(f->eth_addr))
2384 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002385 }
2386 }
2387 spin_unlock_bh(&vxlan->hash_lock);
2388}
2389
2390/* Cleanup timer and forwarding table on shutdown */
2391static int vxlan_stop(struct net_device *dev)
2392{
2393 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002394 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002395 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002396
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002397 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002398 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002399 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002400
2401 del_timer_sync(&vxlan->age_timer);
2402
2403 vxlan_flush(vxlan);
Jiri Benc205f3562015-09-24 13:50:01 +02002404 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002405
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002406 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002407}
2408
stephen hemmingerd3428942012-10-01 12:32:35 +00002409/* Stub, nothing needs to be done. */
2410static void vxlan_set_multicast_list(struct net_device *dev)
2411{
2412}
2413
David Wragg72564b52016-02-10 00:05:55 +00002414static int __vxlan_change_mtu(struct net_device *dev,
2415 struct net_device *lowerdev,
2416 struct vxlan_rdst *dst, int new_mtu, bool strict)
2417{
2418 int max_mtu = IP_MAX_MTU;
2419
2420 if (lowerdev)
2421 max_mtu = lowerdev->mtu;
2422
2423 if (dst->remote_ip.sa.sa_family == AF_INET6)
2424 max_mtu -= VXLAN6_HEADROOM;
2425 else
2426 max_mtu -= VXLAN_HEADROOM;
2427
2428 if (new_mtu < 68)
2429 return -EINVAL;
2430
2431 if (new_mtu > max_mtu) {
2432 if (strict)
2433 return -EINVAL;
2434
2435 new_mtu = max_mtu;
2436 }
2437
2438 dev->mtu = new_mtu;
2439 return 0;
2440}
2441
Daniel Borkmann345010b2013-12-18 00:21:08 +01002442static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2443{
2444 struct vxlan_dev *vxlan = netdev_priv(dev);
2445 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002446 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2447 dst->remote_ifindex);
2448 return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002449}
2450
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002451static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2452{
2453 struct vxlan_dev *vxlan = netdev_priv(dev);
2454 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2455 __be16 sport, dport;
2456
2457 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2458 vxlan->cfg.port_max, true);
2459 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2460
Jiri Benc239e9442015-12-07 13:04:31 +01002461 if (ip_tunnel_info_af(info) == AF_INET) {
Jiri Benc1a8496b2016-02-02 18:09:14 +01002462 struct rtable *rt;
2463
Jiri Benc239e9442015-12-07 13:04:31 +01002464 if (!vxlan->vn4_sock)
2465 return -EINVAL;
Jiri Benc1a8496b2016-02-02 18:09:14 +01002466 rt = vxlan_get_route(vxlan, skb, 0, info->key.tos,
2467 info->key.u.ipv4.dst,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01002468 &info->key.u.ipv4.src, NULL, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002469 if (IS_ERR(rt))
2470 return PTR_ERR(rt);
2471 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002472 } else {
2473#if IS_ENABLED(CONFIG_IPV6)
2474 struct dst_entry *ndst;
2475
2476 if (!vxlan->vn6_sock)
2477 return -EINVAL;
Daniel Borkmann14006152016-03-04 15:15:08 +01002478 ndst = vxlan6_get_route(vxlan, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002479 info->key.label, &info->key.u.ipv6.dst,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002480 &info->key.u.ipv6.src, NULL, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002481 if (IS_ERR(ndst))
2482 return PTR_ERR(ndst);
2483 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002484#else /* !CONFIG_IPV6 */
2485 return -EPFNOSUPPORT;
2486#endif
2487 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002488 info->key.tp_src = sport;
2489 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002490 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002491}
2492
Jiri Benc0c867c92016-04-05 14:47:10 +02002493static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002494 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002495 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002496 .ndo_open = vxlan_open,
2497 .ndo_stop = vxlan_stop,
2498 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002499 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002500 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002501 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002502 .ndo_validate_addr = eth_validate_addr,
2503 .ndo_set_mac_address = eth_mac_addr,
2504 .ndo_fdb_add = vxlan_fdb_add,
2505 .ndo_fdb_del = vxlan_fdb_delete,
2506 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002507 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002508};
2509
Jiri Bence1e53142016-04-05 14:47:13 +02002510static const struct net_device_ops vxlan_netdev_raw_ops = {
2511 .ndo_init = vxlan_init,
2512 .ndo_uninit = vxlan_uninit,
2513 .ndo_open = vxlan_open,
2514 .ndo_stop = vxlan_stop,
2515 .ndo_start_xmit = vxlan_xmit,
2516 .ndo_get_stats64 = ip_tunnel_get_stats64,
2517 .ndo_change_mtu = vxlan_change_mtu,
2518 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2519};
2520
stephen hemmingerd3428942012-10-01 12:32:35 +00002521/* Info for udev, that this is a virtual tunnel endpoint */
2522static struct device_type vxlan_type = {
2523 .name = "vxlan",
2524};
2525
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002526/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002527 * supply the listening VXLAN udp ports. Callers are expected
2528 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002529 */
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02002530static void vxlan_push_rx_ports(struct net_device *dev)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002531{
2532 struct vxlan_sock *vs;
2533 struct net *net = dev_net(dev);
2534 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2535 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002536 __be16 port;
2537 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002538
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02002539 if (!dev->netdev_ops->ndo_add_vxlan_port)
2540 return;
2541
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002542 spin_lock(&vn->sock_lock);
2543 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002544 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2545 port = inet_sk(vs->sock->sk)->inet_sport;
Jiri Benc705cc622015-08-20 13:56:28 +02002546 sa_family = vxlan_get_sk_family(vs);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002547 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2548 port);
2549 }
2550 }
2551 spin_unlock(&vn->sock_lock);
2552}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002553
stephen hemmingerd3428942012-10-01 12:32:35 +00002554/* Initialize the device structure. */
2555static void vxlan_setup(struct net_device *dev)
2556{
2557 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002558 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002559
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002560 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002561 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2562
stephen hemmingerd3428942012-10-01 12:32:35 +00002563 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002564 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002565 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002566 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002567
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002568 dev->vlan_features = dev->features;
2569 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002570 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002571 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002572 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002573 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002574 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002575
stephen hemminger553675f2013-05-16 11:35:20 +00002576 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002577 spin_lock_init(&vxlan->hash_lock);
2578
2579 init_timer_deferrable(&vxlan->age_timer);
2580 vxlan->age_timer.function = vxlan_cleanup;
2581 vxlan->age_timer.data = (unsigned long) vxlan;
2582
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002583 vxlan->cfg.dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002584
stephen hemmingerd3428942012-10-01 12:32:35 +00002585 vxlan->dev = dev;
2586
Tom Herbert58ce31c2015-08-19 17:07:33 -07002587 gro_cells_init(&vxlan->gro_cells, dev);
2588
stephen hemmingerd3428942012-10-01 12:32:35 +00002589 for (h = 0; h < FDB_HASH_SIZE; ++h)
2590 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2591}
2592
Jiri Benc0c867c92016-04-05 14:47:10 +02002593static void vxlan_ether_setup(struct net_device *dev)
2594{
2595 eth_hw_addr_random(dev);
2596 ether_setup(dev);
2597 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2598 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2599 dev->netdev_ops = &vxlan_netdev_ether_ops;
2600}
2601
Jiri Bence1e53142016-04-05 14:47:13 +02002602static void vxlan_raw_setup(struct net_device *dev)
2603{
2604 dev->type = ARPHRD_NONE;
2605 dev->hard_header_len = 0;
2606 dev->addr_len = 0;
2607 dev->mtu = ETH_DATA_LEN;
2608 dev->tx_queue_len = 1000;
2609 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2610 dev->netdev_ops = &vxlan_netdev_raw_ops;
2611}
2612
stephen hemmingerd3428942012-10-01 12:32:35 +00002613static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2614 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002615 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002616 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002617 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2618 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002619 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002620 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2621 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002622 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002623 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2624 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2625 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002626 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002627 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2628 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2629 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2630 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002631 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002632 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002633 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2634 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2635 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002636 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2637 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002638 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002639 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002640 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002641};
2642
2643static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2644{
2645 if (tb[IFLA_ADDRESS]) {
2646 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2647 pr_debug("invalid link address (not ethernet)\n");
2648 return -EINVAL;
2649 }
2650
2651 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2652 pr_debug("invalid all zero ethernet address\n");
2653 return -EADDRNOTAVAIL;
2654 }
2655 }
2656
2657 if (!data)
2658 return -EINVAL;
2659
2660 if (data[IFLA_VXLAN_ID]) {
2661 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2662 if (id >= VXLAN_VID_MASK)
2663 return -ERANGE;
2664 }
2665
stephen hemminger05f47d62012-10-09 20:35:50 +00002666 if (data[IFLA_VXLAN_PORT_RANGE]) {
2667 const struct ifla_vxlan_port_range *p
2668 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2669
2670 if (ntohs(p->high) < ntohs(p->low)) {
2671 pr_debug("port range %u .. %u not valid\n",
2672 ntohs(p->low), ntohs(p->high));
2673 return -EINVAL;
2674 }
2675 }
2676
stephen hemmingerd3428942012-10-01 12:32:35 +00002677 return 0;
2678}
2679
Yan Burman1b13c972013-01-29 23:43:07 +00002680static void vxlan_get_drvinfo(struct net_device *netdev,
2681 struct ethtool_drvinfo *drvinfo)
2682{
2683 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2684 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2685}
2686
2687static const struct ethtool_ops vxlan_ethtool_ops = {
2688 .get_drvinfo = vxlan_get_drvinfo,
2689 .get_link = ethtool_op_get_link,
2690};
2691
Tom Herbert3ee64f32014-07-13 19:49:42 -07002692static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2693 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002694{
Cong Wange4c7ed42013-08-31 13:44:33 +08002695 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002696 struct udp_port_cfg udp_conf;
2697 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002698
Tom Herbert3ee64f32014-07-13 19:49:42 -07002699 memset(&udp_conf, 0, sizeof(udp_conf));
2700
2701 if (ipv6) {
2702 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002703 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002704 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002705 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002706 } else {
2707 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002708 }
2709
Tom Herbert3ee64f32014-07-13 19:49:42 -07002710 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002711
Tom Herbert3ee64f32014-07-13 19:49:42 -07002712 /* Open UDP socket */
2713 err = udp_sock_create(net, &udp_conf, &sock);
2714 if (err < 0)
2715 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002716
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002717 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002718}
2719
2720/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002721static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2722 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002723{
2724 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2725 struct vxlan_sock *vs;
2726 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002727 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002728 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002729
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002730 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002731 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002732 return ERR_PTR(-ENOMEM);
2733
2734 for (h = 0; h < VNI_HASH_SIZE; ++h)
2735 INIT_HLIST_HEAD(&vs->vni_list[h]);
2736
Tom Herbert3ee64f32014-07-13 19:49:42 -07002737 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002738 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002739 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2740 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002741 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002742 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002743 }
2744
Cong Wange4c7ed42013-08-31 13:44:33 +08002745 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002746 atomic_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002747 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002748
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002749 spin_lock(&vn->sock_lock);
2750 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002751 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002752 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002753
2754 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002755 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002756 tunnel_cfg.sk_user_data = vs;
2757 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002758 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002759 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002760 tunnel_cfg.gro_receive = vxlan_gro_receive;
2761 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002762
2763 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002764
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002765 return vs;
2766}
stephen hemminger553675f2013-05-16 11:35:20 +00002767
Jiri Bencb1be00a2015-09-24 13:50:02 +02002768static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002769{
Jiri Benc205f3562015-09-24 13:50:01 +02002770 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2771 struct vxlan_sock *vs = NULL;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002772
Jiri Benc205f3562015-09-24 13:50:01 +02002773 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002774 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002775 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2776 vxlan->cfg.dst_port, vxlan->flags);
2777 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002778 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002779 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002780 }
2781 spin_unlock(&vn->sock_lock);
2782 }
Jiri Benc205f3562015-09-24 13:50:01 +02002783 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002784 vs = vxlan_socket_create(vxlan->net, ipv6,
2785 vxlan->cfg.dst_port, vxlan->flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002786 if (IS_ERR(vs))
2787 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002788#if IS_ENABLED(CONFIG_IPV6)
2789 if (ipv6)
2790 vxlan->vn6_sock = vs;
2791 else
2792#endif
2793 vxlan->vn4_sock = vs;
Jiri Benc205f3562015-09-24 13:50:01 +02002794 vxlan_vs_add_dev(vs, vxlan);
2795 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002796}
2797
Jiri Bencb1be00a2015-09-24 13:50:02 +02002798static int vxlan_sock_add(struct vxlan_dev *vxlan)
2799{
2800 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2801 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
2802 int ret = 0;
2803
2804 vxlan->vn4_sock = NULL;
2805#if IS_ENABLED(CONFIG_IPV6)
2806 vxlan->vn6_sock = NULL;
2807 if (ipv6 || metadata)
2808 ret = __vxlan_sock_add(vxlan, true);
2809#endif
2810 if (!ret && (!ipv6 || metadata))
2811 ret = __vxlan_sock_add(vxlan, false);
2812 if (ret < 0)
2813 vxlan_sock_release(vxlan);
2814 return ret;
2815}
2816
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002817static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2818 struct vxlan_config *conf)
stephen hemmingerd3428942012-10-01 12:32:35 +00002819{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002820 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002821 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
Atzm Watanabec7995c42013-04-16 02:50:52 +00002822 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002823 unsigned short needed_headroom = ETH_HLEN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002824 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002825 bool use_ipv6 = false;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002826 __be16 default_port = vxlan->cfg.dst_port;
David Wragg7e059152016-02-10 00:05:58 +00002827 struct net_device *lowerdev = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00002828
Jiri Bence1e53142016-04-05 14:47:13 +02002829 if (conf->flags & VXLAN_F_GPE) {
2830 if (conf->flags & ~VXLAN_F_ALLOWED_GPE)
2831 return -EINVAL;
2832 /* For now, allow GPE only together with COLLECT_METADATA.
2833 * This can be relaxed later; in such case, the other side
2834 * of the PtP link will have to be provided.
2835 */
2836 if (!(conf->flags & VXLAN_F_COLLECT_METADATA))
2837 return -EINVAL;
2838
2839 vxlan_raw_setup(dev);
2840 } else {
2841 vxlan_ether_setup(dev);
2842 }
Jiri Benc0c867c92016-04-05 14:47:10 +02002843
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002844 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002845
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002846 dst->remote_vni = conf->vni;
2847
2848 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00002849
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002850 /* Unless IPv6 is explicitly requested, assume IPv4 */
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002851 if (!dst->remote_ip.sa.sa_family)
2852 dst->remote_ip.sa.sa_family = AF_INET;
stephen hemmingerd3428942012-10-01 12:32:35 +00002853
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002854 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
Jiri Benc057ba292015-09-17 16:11:11 +02002855 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2856 if (!IS_ENABLED(CONFIG_IPV6))
2857 return -EPFNOSUPPORT;
Cong Wange4c7ed42013-08-31 13:44:33 +08002858 use_ipv6 = true;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002859 vxlan->flags |= VXLAN_F_IPV6;
Jiri Benc057ba292015-09-17 16:11:11 +02002860 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002861
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002862 if (conf->label && !use_ipv6) {
2863 pr_info("label only supported in use with IPv6\n");
2864 return -EINVAL;
2865 }
2866
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002867 if (conf->remote_ifindex) {
David Wragg7e059152016-02-10 00:05:58 +00002868 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002869 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00002870
stephen hemminger34e02aa2012-10-09 20:35:53 +00002871 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002872 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002873 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002874 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002875
Cong Wange4c7ed42013-08-31 13:44:33 +08002876#if IS_ENABLED(CONFIG_IPV6)
2877 if (use_ipv6) {
2878 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2879 if (idev && idev->cnf.disable_ipv6) {
2880 pr_info("IPv6 is disabled via sysctl\n");
2881 return -EPERM;
2882 }
Cong Wange4c7ed42013-08-31 13:44:33 +08002883 }
2884#endif
2885
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002886 if (!conf->mtu)
Cong Wange4c7ed42013-08-31 13:44:33 +08002887 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002888
Jiri Bencb1be00a2015-09-24 13:50:02 +02002889 needed_headroom = lowerdev->hard_header_len;
Jiri Benc9dc2ad12015-09-17 16:11:10 +02002890 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002891
David Wragg7e059152016-02-10 00:05:58 +00002892 if (conf->mtu) {
2893 err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false);
2894 if (err)
2895 return err;
2896 }
2897
Jiri Bencb1be00a2015-09-24 13:50:02 +02002898 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2899 needed_headroom += VXLAN6_HEADROOM;
2900 else
2901 needed_headroom += VXLAN_HEADROOM;
2902 dev->needed_headroom = needed_headroom;
2903
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002904 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Jiri Bence1e53142016-04-05 14:47:13 +02002905 if (!vxlan->cfg.dst_port) {
2906 if (conf->flags & VXLAN_F_GPE)
2907 vxlan->cfg.dst_port = 4790; /* IANA assigned VXLAN-GPE port */
2908 else
2909 vxlan->cfg.dst_port = default_port;
2910 }
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002911 vxlan->flags |= conf->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00002912
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002913 if (!vxlan->cfg.age_interval)
2914 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
Vincent Bernatafb97182012-10-30 10:27:16 +00002915
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002916 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2917 if (tmp->cfg.vni == conf->vni &&
2918 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2919 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2920 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2921 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
2922 (vxlan->flags & VXLAN_F_RCV_FLAGS))
stephen hemminger553675f2013-05-16 11:35:20 +00002923 return -EEXIST;
Nicolas Dichtel07b9b372016-01-07 11:26:53 +01002924 }
stephen hemminger553675f2013-05-16 11:35:20 +00002925
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002926 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002927
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002928 /* create an fdb entry for a valid default destination */
2929 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2930 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2931 &vxlan->default_dst.remote_ip,
2932 NUD_REACHABLE|NUD_PERMANENT,
2933 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002934 vxlan->cfg.dst_port,
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002935 vxlan->default_dst.remote_vni,
2936 vxlan->default_dst.remote_ifindex,
2937 NTF_SELF);
2938 if (err)
2939 return err;
2940 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002941
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002942 err = register_netdevice(dev);
2943 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002944 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002945 return err;
2946 }
2947
stephen hemminger553675f2013-05-16 11:35:20 +00002948 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002949
2950 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002951}
2952
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002953struct net_device *vxlan_dev_create(struct net *net, const char *name,
2954 u8 name_assign_type, struct vxlan_config *conf)
2955{
2956 struct nlattr *tb[IFLA_MAX+1];
2957 struct net_device *dev;
2958 int err;
2959
2960 memset(&tb, 0, sizeof(tb));
2961
2962 dev = rtnl_create_link(net, name, name_assign_type,
2963 &vxlan_link_ops, tb);
2964 if (IS_ERR(dev))
2965 return dev;
2966
2967 err = vxlan_dev_configure(net, dev, conf);
2968 if (err < 0) {
2969 free_netdev(dev);
2970 return ERR_PTR(err);
2971 }
2972
2973 return dev;
2974}
2975EXPORT_SYMBOL_GPL(vxlan_dev_create);
2976
2977static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2978 struct nlattr *tb[], struct nlattr *data[])
2979{
2980 struct vxlan_config conf;
2981 int err;
2982
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002983 memset(&conf, 0, sizeof(conf));
Jesse Grosse277de52015-10-16 16:36:00 -07002984
2985 if (data[IFLA_VXLAN_ID])
Jiri Benc54bfd872016-02-16 21:58:58 +01002986 conf.vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002987
2988 if (data[IFLA_VXLAN_GROUP]) {
2989 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
2990 } else if (data[IFLA_VXLAN_GROUP6]) {
2991 if (!IS_ENABLED(CONFIG_IPV6))
2992 return -EPFNOSUPPORT;
2993
2994 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
2995 conf.remote_ip.sa.sa_family = AF_INET6;
2996 }
2997
2998 if (data[IFLA_VXLAN_LOCAL]) {
2999 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3000 conf.saddr.sa.sa_family = AF_INET;
3001 } else if (data[IFLA_VXLAN_LOCAL6]) {
3002 if (!IS_ENABLED(CONFIG_IPV6))
3003 return -EPFNOSUPPORT;
3004
3005 /* TODO: respect scope id */
3006 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3007 conf.saddr.sa.sa_family = AF_INET6;
3008 }
3009
3010 if (data[IFLA_VXLAN_LINK])
3011 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3012
3013 if (data[IFLA_VXLAN_TOS])
3014 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3015
3016 if (data[IFLA_VXLAN_TTL])
3017 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3018
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003019 if (data[IFLA_VXLAN_LABEL])
3020 conf.label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3021 IPV6_FLOWLABEL_MASK;
3022
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003023 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
3024 conf.flags |= VXLAN_F_LEARN;
3025
3026 if (data[IFLA_VXLAN_AGEING])
3027 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3028
3029 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
3030 conf.flags |= VXLAN_F_PROXY;
3031
3032 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
3033 conf.flags |= VXLAN_F_RSC;
3034
3035 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3036 conf.flags |= VXLAN_F_L2MISS;
3037
3038 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3039 conf.flags |= VXLAN_F_L3MISS;
3040
3041 if (data[IFLA_VXLAN_LIMIT])
3042 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3043
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07003044 if (data[IFLA_VXLAN_COLLECT_METADATA] &&
3045 nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3046 conf.flags |= VXLAN_F_COLLECT_METADATA;
3047
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003048 if (data[IFLA_VXLAN_PORT_RANGE]) {
3049 const struct ifla_vxlan_port_range *p
3050 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3051 conf.port_min = ntohs(p->low);
3052 conf.port_max = ntohs(p->high);
3053 }
3054
3055 if (data[IFLA_VXLAN_PORT])
3056 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3057
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003058 if (data[IFLA_VXLAN_UDP_CSUM] &&
3059 !nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3060 conf.flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003061
3062 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
3063 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3064 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3065
3066 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
3067 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3068 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3069
3070 if (data[IFLA_VXLAN_REMCSUM_TX] &&
3071 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3072 conf.flags |= VXLAN_F_REMCSUM_TX;
3073
3074 if (data[IFLA_VXLAN_REMCSUM_RX] &&
3075 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3076 conf.flags |= VXLAN_F_REMCSUM_RX;
3077
3078 if (data[IFLA_VXLAN_GBP])
3079 conf.flags |= VXLAN_F_GBP;
3080
Jiri Bence1e53142016-04-05 14:47:13 +02003081 if (data[IFLA_VXLAN_GPE])
3082 conf.flags |= VXLAN_F_GPE;
3083
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003084 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
3085 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3086
3087 err = vxlan_dev_configure(src_net, dev, &conf);
3088 switch (err) {
3089 case -ENODEV:
3090 pr_info("ifindex %d does not exist\n", conf.remote_ifindex);
3091 break;
3092
3093 case -EPERM:
3094 pr_info("IPv6 is disabled via sysctl\n");
3095 break;
3096
3097 case -EEXIST:
Jiri Benc54bfd872016-02-16 21:58:58 +01003098 pr_info("duplicate VNI %u\n", be32_to_cpu(conf.vni));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003099 break;
Jiri Bence1e53142016-04-05 14:47:13 +02003100
3101 case -EINVAL:
3102 pr_info("unsupported combination of extensions\n");
3103 break;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003104 }
3105
3106 return err;
3107}
3108
stephen hemmingerd3428942012-10-01 12:32:35 +00003109static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3110{
3111 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003112 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00003113
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003114 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07003115 if (!hlist_unhashed(&vxlan->hlist))
3116 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07003117 spin_unlock(&vn->sock_lock);
3118
Tom Herbert58ce31c2015-08-19 17:07:33 -07003119 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003120 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003121 unregister_netdevice_queue(dev, head);
3122}
3123
3124static size_t vxlan_get_size(const struct net_device *dev)
3125{
3126
3127 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003128 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003129 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003130 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003131 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3132 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003133 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003134 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003135 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3136 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3137 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3138 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003139 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003140 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3141 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003142 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003143 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3144 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3145 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3146 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003147 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3148 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003149 0;
3150}
3151
3152static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3153{
3154 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003155 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003156 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003157 .low = htons(vxlan->cfg.port_min),
3158 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003159 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003160
Jiri Benc54bfd872016-02-16 21:58:58 +01003161 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003162 goto nla_put_failure;
3163
Cong Wange4c7ed42013-08-31 13:44:33 +08003164 if (!vxlan_addr_any(&dst->remote_ip)) {
3165 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003166 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3167 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003168 goto nla_put_failure;
3169#if IS_ENABLED(CONFIG_IPV6)
3170 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003171 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3172 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003173 goto nla_put_failure;
3174#endif
3175 }
3176 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003177
Atzm Watanabec7995c42013-04-16 02:50:52 +00003178 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003179 goto nla_put_failure;
3180
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003181 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3182 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003183 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003184 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003185 goto nla_put_failure;
3186#if IS_ENABLED(CONFIG_IPV6)
3187 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003188 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003189 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003190 goto nla_put_failure;
3191#endif
3192 }
3193 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003194
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003195 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3196 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003197 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003198 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3199 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3200 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3201 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3202 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3203 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3204 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3205 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3206 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003207 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3208 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003209 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3210 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3211 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003212 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08003213 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003214 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3215 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3216 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08003217 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3218 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3219 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3220 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3221 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003222 goto nla_put_failure;
3223
stephen hemminger05f47d62012-10-09 20:35:50 +00003224 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3225 goto nla_put_failure;
3226
Thomas Graf35114942015-01-15 03:53:55 +01003227 if (vxlan->flags & VXLAN_F_GBP &&
3228 nla_put_flag(skb, IFLA_VXLAN_GBP))
3229 goto nla_put_failure;
3230
Jiri Bence1e53142016-04-05 14:47:13 +02003231 if (vxlan->flags & VXLAN_F_GPE &&
3232 nla_put_flag(skb, IFLA_VXLAN_GPE))
3233 goto nla_put_failure;
3234
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003235 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3236 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3237 goto nla_put_failure;
3238
stephen hemmingerd3428942012-10-01 12:32:35 +00003239 return 0;
3240
3241nla_put_failure:
3242 return -EMSGSIZE;
3243}
3244
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003245static struct net *vxlan_get_link_net(const struct net_device *dev)
3246{
3247 struct vxlan_dev *vxlan = netdev_priv(dev);
3248
3249 return vxlan->net;
3250}
3251
stephen hemmingerd3428942012-10-01 12:32:35 +00003252static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3253 .kind = "vxlan",
3254 .maxtype = IFLA_VXLAN_MAX,
3255 .policy = vxlan_policy,
3256 .priv_size = sizeof(struct vxlan_dev),
3257 .setup = vxlan_setup,
3258 .validate = vxlan_validate,
3259 .newlink = vxlan_newlink,
3260 .dellink = vxlan_dellink,
3261 .get_size = vxlan_get_size,
3262 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003263 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003264};
3265
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003266static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3267 struct net_device *dev)
3268{
3269 struct vxlan_dev *vxlan, *next;
3270 LIST_HEAD(list_kill);
3271
3272 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3273 struct vxlan_rdst *dst = &vxlan->default_dst;
3274
3275 /* In case we created vxlan device with carrier
3276 * and we loose the carrier due to module unload
3277 * we also need to remove vxlan device. In other
3278 * cases, it's not necessary and remote_ifindex
3279 * is 0 here, so no matches.
3280 */
3281 if (dst->remote_ifindex == dev->ifindex)
3282 vxlan_dellink(vxlan->dev, &list_kill);
3283 }
3284
3285 unregister_netdevice_many(&list_kill);
3286}
3287
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003288static int vxlan_netdevice_event(struct notifier_block *unused,
3289 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003290{
3291 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003292 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003293
Daniel Borkmann783c1462014-01-22 21:07:53 +01003294 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003295 vxlan_handle_lowerdev_unregister(vn, dev);
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003296 else if (event == NETDEV_OFFLOAD_PUSH_VXLAN)
3297 vxlan_push_rx_ports(dev);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003298
3299 return NOTIFY_DONE;
3300}
3301
3302static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003303 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003304};
3305
stephen hemmingerd3428942012-10-01 12:32:35 +00003306static __net_init int vxlan_init_net(struct net *net)
3307{
3308 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003309 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003310
stephen hemminger553675f2013-05-16 11:35:20 +00003311 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003312 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003313
stephen hemminger553675f2013-05-16 11:35:20 +00003314 for (h = 0; h < PORT_HASH_SIZE; ++h)
3315 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003316
3317 return 0;
3318}
3319
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003320static void __net_exit vxlan_exit_net(struct net *net)
3321{
3322 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3323 struct vxlan_dev *vxlan, *next;
3324 struct net_device *dev, *aux;
3325 LIST_HEAD(list);
3326
3327 rtnl_lock();
3328 for_each_netdev_safe(net, dev, aux)
3329 if (dev->rtnl_link_ops == &vxlan_link_ops)
3330 unregister_netdevice_queue(dev, &list);
3331
3332 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3333 /* If vxlan->dev is in the same netns, it has already been added
3334 * to the list by the previous loop.
3335 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003336 if (!net_eq(dev_net(vxlan->dev), net)) {
3337 gro_cells_destroy(&vxlan->gro_cells);
John W. Linville13c3ed62015-05-18 13:51:24 -04003338 unregister_netdevice_queue(vxlan->dev, &list);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003339 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003340 }
3341
3342 unregister_netdevice_many(&list);
3343 rtnl_unlock();
3344}
3345
stephen hemmingerd3428942012-10-01 12:32:35 +00003346static struct pernet_operations vxlan_net_ops = {
3347 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003348 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003349 .id = &vxlan_net_id,
3350 .size = sizeof(struct vxlan_net),
3351};
3352
3353static int __init vxlan_init_module(void)
3354{
3355 int rc;
3356
3357 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3358
Daniel Borkmann783c1462014-01-22 21:07:53 +01003359 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003360 if (rc)
3361 goto out1;
3362
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003363 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003364 if (rc)
3365 goto out2;
3366
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003367 rc = rtnl_link_register(&vxlan_link_ops);
3368 if (rc)
3369 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003370
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003371 return 0;
3372out3:
3373 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003374out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003375 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003376out1:
3377 return rc;
3378}
Cong Wang7332a132013-05-27 22:35:53 +00003379late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003380
3381static void __exit vxlan_cleanup_module(void)
3382{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003383 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003384 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003385 unregister_pernet_subsys(&vxlan_net_ops);
3386 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003387}
3388module_exit(vxlan_cleanup_module);
3389
3390MODULE_LICENSE("GPL");
3391MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003392MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003393MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003394MODULE_ALIAS_RTNL_LINK("vxlan");