blob: 45a8a5475f3d7b81299fee0118b586c243ac4d78 [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
stephen hemmingerd3428942012-10-01 12:32:35 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000014#include <linux/module.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000017#include <linux/udp.h>
18#include <linux/igmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000019#include <linux/if_ether.h>
Yan Burman1b13c972013-01-29 23:43:07 +000020#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000021#include <net/arp.h>
22#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000023#include <net/ip.h>
24#include <net/icmp.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000025#include <net/rtnetlink.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000026#include <net/inet_ecn.h>
27#include <net/net_namespace.h>
28#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070029#include <net/vxlan.h>
stephen hemminger40d29af2016-02-09 22:07:29 -080030
Cong Wange4c7ed42013-08-31 13:44:33 +080031#if IS_ENABLED(CONFIG_IPV6)
Cong Wange4c7ed42013-08-31 13:44:33 +080032#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080033#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080034#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000035
36#define VXLAN_VERSION "0.1"
37
stephen hemminger553675f2013-05-16 11:35:20 +000038#define PORT_HASH_BITS 8
39#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000040#define FDB_AGE_DEFAULT 300 /* 5 min */
41#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
42
stephen hemminger23c578b2013-04-27 11:31:53 +000043/* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070045 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000046 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070047static unsigned short vxlan_port __read_mostly = 8472;
48module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000049MODULE_PARM_DESC(udp_port, "Destination UDP port");
50
51static bool log_ecn_error = true;
52module_param(log_ecn_error, bool, 0644);
53MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
54
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030055static unsigned int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020056static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000057
Li RongQing7256eac2016-01-29 09:43:47 +080058static const u8 all_zeros_mac[ETH_ALEN + 2];
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030059
Jiri Benc205f3562015-09-24 13:50:01 +020060static int vxlan_sock_add(struct vxlan_dev *vxlan);
Thomas Graf614732e2015-07-21 10:44:06 +020061
Mark Blocha53cb292017-06-02 03:24:08 +030062static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
63
stephen hemminger553675f2013-05-16 11:35:20 +000064/* per-network namespace private data for this module */
65struct vxlan_net {
66 struct list_head vxlan_list;
67 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070068 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000069};
70
stephen hemmingerd3428942012-10-01 12:32:35 +000071/* Forwarding table entry */
72struct vxlan_fdb {
73 struct hlist_node hlist; /* linked list of entries */
74 struct rcu_head rcu;
75 unsigned long updated; /* jiffies */
76 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070077 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020078 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000079 u16 state; /* see ndm_state */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -080080 __be32 vni;
David Stevensae884082013-04-19 00:36:26 +000081 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +000082};
83
stephen hemmingerd3428942012-10-01 12:32:35 +000084/* salt for hash table */
85static u32 vxlan_salt __read_mostly;
86
Thomas Grafee122c72015-07-21 10:43:58 +020087static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
88{
Thomas Grafe7030872015-07-21 10:44:01 +020089 return vs->flags & VXLAN_F_COLLECT_METADATA ||
90 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +020091}
92
Cong Wange4c7ed42013-08-31 13:44:33 +080093#if IS_ENABLED(CONFIG_IPV6)
94static inline
95bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
96{
Jiri Bencf0ef3122015-03-29 16:17:37 +020097 if (a->sa.sa_family != b->sa.sa_family)
98 return false;
99 if (a->sa.sa_family == AF_INET6)
100 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
101 else
102 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800103}
104
105static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
106{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200107 if (ipa->sa.sa_family == AF_INET6)
108 return ipv6_addr_any(&ipa->sin6.sin6_addr);
109 else
110 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800111}
112
113static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
114{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200115 if (ipa->sa.sa_family == AF_INET6)
116 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
117 else
118 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800119}
120
121static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
122{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200123 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200124 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200125 ip->sa.sa_family = AF_INET6;
126 return 0;
127 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200128 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200129 ip->sa.sa_family = AF_INET;
130 return 0;
131 } else {
132 return -EAFNOSUPPORT;
133 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800134}
135
136static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200137 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800138{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200139 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200140 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200141 else
Jiri Benc930345e2015-03-29 16:59:25 +0200142 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800143}
144
145#else /* !CONFIG_IPV6 */
146
147static inline
148bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
149{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200150 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800151}
152
153static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
154{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200155 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800156}
157
158static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
159{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200160 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800161}
162
163static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
164{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200165 if (nla_len(nla) >= sizeof(struct in6_addr)) {
166 return -EAFNOSUPPORT;
167 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200168 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200169 ip->sa.sa_family = AF_INET;
170 return 0;
171 } else {
172 return -EAFNOSUPPORT;
173 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800174}
175
176static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200177 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800178{
Jiri Benc930345e2015-03-29 16:59:25 +0200179 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800180}
181#endif
182
stephen hemminger553675f2013-05-16 11:35:20 +0000183/* Virtual Network hash table head */
Jiri Benc54bfd872016-02-16 21:58:58 +0100184static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
stephen hemminger553675f2013-05-16 11:35:20 +0000185{
Jiri Benc54bfd872016-02-16 21:58:58 +0100186 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
stephen hemminger553675f2013-05-16 11:35:20 +0000187}
188
189/* Socket hash table head */
190static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000191{
192 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
193
stephen hemminger553675f2013-05-16 11:35:20 +0000194 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
195}
196
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700197/* First remote destination for a forwarding entry.
198 * Guaranteed to be non-NULL because remotes are never deleted.
199 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700200static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700201{
stephen hemminger5ca54612013-08-04 17:17:39 -0700202 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
203}
204
205static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
206{
207 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700208}
209
Thomas Grafac5132d2015-01-15 03:53:56 +0100210/* Find VXLAN socket based on network namespace, address family and UDP port
211 * and enabled unshareable flags.
212 */
213static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
214 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000215{
216 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800217
218 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000219
220 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200221 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Jiri Benc705cc622015-08-20 13:56:28 +0200222 vxlan_get_sk_family(vs) == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800223 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000224 return vs;
225 }
226 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000227}
228
Jiri Benc54bfd872016-02-16 21:58:58 +0100229static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000230{
231 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000232
Jiri Bencb9167b22016-02-16 21:59:03 +0100233 /* For flow based devices, map all packets to VNI 0 */
234 if (vs->flags & VXLAN_F_COLLECT_METADATA)
235 vni = 0;
236
Jiri Benc54bfd872016-02-16 21:58:58 +0100237 hlist_for_each_entry_rcu(vxlan, vni_head(vs, vni), hlist) {
238 if (vxlan->default_dst.remote_vni == vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000239 return vxlan;
240 }
241
242 return NULL;
243}
244
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700245/* Look up VNI in a per net namespace table */
Jiri Benc54bfd872016-02-16 21:58:58 +0100246static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni,
Thomas Grafac5132d2015-01-15 03:53:56 +0100247 sa_family_t family, __be16 port,
248 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700249{
250 struct vxlan_sock *vs;
251
Thomas Grafac5132d2015-01-15 03:53:56 +0100252 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700253 if (!vs)
254 return NULL;
255
Jiri Benc54bfd872016-02-16 21:58:58 +0100256 return vxlan_vs_find_vni(vs, vni);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700257}
258
stephen hemmingerd3428942012-10-01 12:32:35 +0000259/* Fill in neighbour message in skbuff. */
260static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700261 const struct vxlan_fdb *fdb,
262 u32 portid, u32 seq, int type, unsigned int flags,
263 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000264{
265 unsigned long now = jiffies;
266 struct nda_cacheinfo ci;
267 struct nlmsghdr *nlh;
268 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000269 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000270
271 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
272 if (nlh == NULL)
273 return -EMSGSIZE;
274
275 ndm = nlmsg_data(nlh);
276 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000277
278 send_eth = send_ip = true;
279
280 if (type == RTM_GETNEIGH) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800281 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000282 send_eth = !is_zero_ether_addr(fdb->eth_addr);
Vincent Bernat8f48ba72017-03-10 16:30:24 +0100283 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
David Stevense4f67ad2012-11-20 02:50:14 +0000284 } else
285 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000286 ndm->ndm_state = fdb->state;
287 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000288 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800289 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000290
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100291 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100292 nla_put_s32(skb, NDA_LINK_NETNSID,
WANG Cong38f507f2016-09-01 21:53:44 -0700293 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100294 goto nla_put_failure;
295
David Stevense4f67ad2012-11-20 02:50:14 +0000296 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000297 goto nla_put_failure;
298
Cong Wange4c7ed42013-08-31 13:44:33 +0800299 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000300 goto nla_put_failure;
301
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200302 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000303 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
304 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000305 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Jiri Benc54bfd872016-02-16 21:58:58 +0100306 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
David Stevens66817122013-03-15 04:35:51 +0000307 goto nla_put_failure;
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200308 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800309 nla_put_u32(skb, NDA_SRC_VNI,
310 be32_to_cpu(fdb->vni)))
311 goto nla_put_failure;
David Stevens66817122013-03-15 04:35:51 +0000312 if (rdst->remote_ifindex &&
313 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000314 goto nla_put_failure;
315
316 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
317 ci.ndm_confirmed = 0;
318 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
319 ci.ndm_refcnt = 0;
320
321 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
322 goto nla_put_failure;
323
Johannes Berg053c0952015-01-16 22:09:00 +0100324 nlmsg_end(skb, nlh);
325 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000326
327nla_put_failure:
328 nlmsg_cancel(skb, nlh);
329 return -EMSGSIZE;
330}
331
332static inline size_t vxlan_nlmsg_size(void)
333{
334 return NLMSG_ALIGN(sizeof(struct ndmsg))
335 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800336 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000337 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000338 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
339 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100340 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000341 + nla_total_size(sizeof(struct nda_cacheinfo));
342}
343
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200344static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
345 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000346{
347 struct net *net = dev_net(vxlan->dev);
348 struct sk_buff *skb;
349 int err = -ENOBUFS;
350
351 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
352 if (skb == NULL)
353 goto errout;
354
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200355 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000356 if (err < 0) {
357 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
358 WARN_ON(err == -EMSGSIZE);
359 kfree_skb(skb);
360 goto errout;
361 }
362
363 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
364 return;
365errout:
366 if (err < 0)
367 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
368}
369
Cong Wange4c7ed42013-08-31 13:44:33 +0800370static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000371{
372 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700373 struct vxlan_fdb f = {
374 .state = NUD_STALE,
375 };
376 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800377 .remote_ip = *ipa, /* goes to NDA_DST */
Jiri Benc54bfd872016-02-16 21:58:58 +0100378 .remote_vni = cpu_to_be32(VXLAN_N_VID),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700379 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700380
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200381 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000382}
383
384static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
385{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700386 struct vxlan_fdb f = {
387 .state = NUD_STALE,
388 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200389 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000390
David Stevense4f67ad2012-11-20 02:50:14 +0000391 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
392
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200393 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000394}
395
stephen hemmingerd3428942012-10-01 12:32:35 +0000396/* Hash Ethernet address */
397static u32 eth_hash(const unsigned char *addr)
398{
399 u64 value = get_unaligned((u64 *)addr);
400
401 /* only want 6 bytes */
402#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000403 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000404#else
405 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000406#endif
407 return hash_64(value, FDB_HASH_BITS);
408}
409
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800410static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
411{
412 /* use 1 byte of OUI and 3 bytes of NIC */
413 u32 key = get_unaligned((u32 *)(addr + 2));
414
415 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
416}
417
stephen hemmingerd3428942012-10-01 12:32:35 +0000418/* Hash chain to use given mac address */
419static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800420 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000421{
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200422 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800423 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
424 else
425 return &vxlan->fdb_head[eth_hash(mac)];
stephen hemmingerd3428942012-10-01 12:32:35 +0000426}
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,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800430 const u8 *mac, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000431{
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800432 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000433 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) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800436 if (ether_addr_equal(mac, f->eth_addr)) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +0200437 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800438 if (vni == f->vni)
439 return f;
440 } else {
441 return f;
442 }
443 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000444 }
445
446 return NULL;
447}
448
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000449static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800450 const u8 *mac, __be32 vni)
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000451{
452 struct vxlan_fdb *f;
453
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800454 f = __vxlan_find_mac(vxlan, mac, vni);
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000455 if (f)
456 f->used = jiffies;
457
458 return f;
459}
460
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300461/* caller should hold vxlan->hash_lock */
462static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800463 union vxlan_addr *ip, __be16 port,
Jiri Benc54bfd872016-02-16 21:58:58 +0100464 __be32 vni, __u32 ifindex)
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300465{
466 struct vxlan_rdst *rd;
467
468 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800469 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300470 rd->remote_port == port &&
471 rd->remote_vni == vni &&
472 rd->remote_ifindex == ifindex)
473 return rd;
474 }
475
476 return NULL;
477}
478
Thomas Richter906dc182013-07-19 17:20:07 +0200479/* Replace destination of unicast mac */
480static int vxlan_fdb_replace(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100481 union vxlan_addr *ip, __be16 port, __be32 vni,
482 __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200483{
484 struct vxlan_rdst *rd;
485
486 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
487 if (rd)
488 return 0;
489
490 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
491 if (!rd)
492 return 0;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100493
494 dst_cache_reset(&rd->dst_cache);
Cong Wange4c7ed42013-08-31 13:44:33 +0800495 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200496 rd->remote_port = port;
497 rd->remote_vni = vni;
498 rd->remote_ifindex = ifindex;
499 return 1;
500}
501
David Stevens66817122013-03-15 04:35:51 +0000502/* Add/update destinations for multicast */
503static int vxlan_fdb_append(struct vxlan_fdb *f,
Jiri Benc54bfd872016-02-16 21:58:58 +0100504 union vxlan_addr *ip, __be16 port, __be32 vni,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200505 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000506{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700507 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000508
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300509 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
510 if (rd)
511 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700512
David Stevens66817122013-03-15 04:35:51 +0000513 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
514 if (rd == NULL)
515 return -ENOBUFS;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100516
517 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
518 kfree(rd);
519 return -ENOBUFS;
520 }
521
Cong Wange4c7ed42013-08-31 13:44:33 +0800522 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000523 rd->remote_port = port;
524 rd->remote_vni = vni;
525 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700526
527 list_add_tail_rcu(&rd->list, &f->remotes);
528
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200529 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000530 return 1;
531}
532
Tom Herbertdfd86452015-01-12 17:00:38 -0800533static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
534 unsigned int off,
535 struct vxlanhdr *vh, size_t hdrlen,
Jiri Benc54bfd872016-02-16 21:58:58 +0100536 __be32 vni_field,
537 struct gro_remcsum *grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800538 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800539{
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700540 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -0800541
542 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700543 return vh;
Tom Herbertdfd86452015-01-12 17:00:38 -0800544
545 if (!NAPI_GRO_CB(skb)->csum_valid)
546 return NULL;
547
Jiri Benc54bfd872016-02-16 21:58:58 +0100548 start = vxlan_rco_start(vni_field);
549 offset = start + vxlan_rco_offset(vni_field);
Tom Herbertdfd86452015-01-12 17:00:38 -0800550
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700551 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
552 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800553
554 skb->remcsum_offload = 1;
555
556 return vh;
557}
558
Tom Herbert5602c482016-04-05 08:22:53 -0700559static struct sk_buff **vxlan_gro_receive(struct sock *sk,
560 struct sk_buff **head,
561 struct sk_buff *skb)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200562{
563 struct sk_buff *p, **pp = NULL;
564 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800565 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200566 int flush = 1;
Tom Herbert5602c482016-04-05 08:22:53 -0700567 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
Jiri Benc54bfd872016-02-16 21:58:58 +0100568 __be32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800569 struct gro_remcsum grc;
570
571 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200572
573 off_vx = skb_gro_offset(skb);
574 hlen = off_vx + sizeof(*vh);
575 vh = skb_gro_header_fast(skb, off_vx);
576 if (skb_gro_header_hard(skb, hlen)) {
577 vh = skb_gro_header_slow(skb, hlen, off_vx);
578 if (unlikely(!vh))
579 goto out;
580 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200581
Tom Herbertdfd86452015-01-12 17:00:38 -0800582 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
583
Jiri Benc54bfd872016-02-16 21:58:58 +0100584 flags = vh->vx_flags;
Tom Herbertdfd86452015-01-12 17:00:38 -0800585
586 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
587 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Jiri Benc54bfd872016-02-16 21:58:58 +0100588 vh->vx_vni, &grc,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800589 !!(vs->flags &
590 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800591
592 if (!vh)
593 goto out;
594 }
595
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700596 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
597
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200598 for (p = *head; p; p = p->next) {
599 if (!NAPI_GRO_CB(p)->same_flow)
600 continue;
601
602 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100603 if (vh->vx_flags != vh2->vx_flags ||
604 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200605 NAPI_GRO_CB(p)->same_flow = 0;
606 continue;
607 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200608 }
609
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200610 pp = call_gro_receive(eth_gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800611 flush = 0;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200612
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200613out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800614 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200615 NAPI_GRO_CB(skb)->flush |= flush;
616
617 return pp;
618}
619
Tom Herbert5602c482016-04-05 08:22:53 -0700620static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200621{
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700622 /* Sets 'skb->inner_mac_header' since we are always called with
623 * 'skb->encapsulation' set.
624 */
Jesse Gross9b174d82014-12-30 19:10:15 -0800625 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200626}
627
stephen hemmingerd3428942012-10-01 12:32:35 +0000628/* Add new entry to forwarding table -- assumes lock held */
629static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800630 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000631 __u16 state, __u16 flags,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800632 __be16 port, __be32 src_vni, __be32 vni,
633 __u32 ifindex, __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000634{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200635 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000636 struct vxlan_fdb *f;
637 int notify = 0;
Haishuang Yan17b46362016-11-29 09:59:36 +0800638 int rc;
stephen hemmingerd3428942012-10-01 12:32:35 +0000639
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800640 f = __vxlan_find_mac(vxlan, mac, src_vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000641 if (f) {
642 if (flags & NLM_F_EXCL) {
643 netdev_dbg(vxlan->dev,
644 "lost race to create %pM\n", mac);
645 return -EEXIST;
646 }
647 if (f->state != state) {
648 f->state = state;
649 f->updated = jiffies;
650 notify = 1;
651 }
David Stevensae884082013-04-19 00:36:26 +0000652 if (f->flags != ndm_flags) {
653 f->flags = ndm_flags;
654 f->updated = jiffies;
655 notify = 1;
656 }
Thomas Richter906dc182013-07-19 17:20:07 +0200657 if ((flags & NLM_F_REPLACE)) {
658 /* Only change unicasts */
659 if (!(is_multicast_ether_addr(f->eth_addr) ||
660 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800661 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200662 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200663 } else
664 return -EOPNOTSUPP;
665 }
David Stevens66817122013-03-15 04:35:51 +0000666 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300667 (is_multicast_ether_addr(f->eth_addr) ||
668 is_zero_ether_addr(f->eth_addr))) {
Haishuang Yan17b46362016-11-29 09:59:36 +0800669 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
David Stevens66817122013-03-15 04:35:51 +0000670
671 if (rc < 0)
672 return rc;
673 notify |= rc;
674 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000675 } else {
676 if (!(flags & NLM_F_CREATE))
677 return -ENOENT;
678
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200679 if (vxlan->cfg.addrmax &&
680 vxlan->addrcnt >= vxlan->cfg.addrmax)
stephen hemmingerd3428942012-10-01 12:32:35 +0000681 return -ENOSPC;
682
Thomas Richter906dc182013-07-19 17:20:07 +0200683 /* Disallow replace to add a multicast entry */
684 if ((flags & NLM_F_REPLACE) &&
685 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
686 return -EOPNOTSUPP;
687
Cong Wange4c7ed42013-08-31 13:44:33 +0800688 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000689 f = kmalloc(sizeof(*f), GFP_ATOMIC);
690 if (!f)
691 return -ENOMEM;
692
693 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000694 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000695 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000696 f->updated = f->used = jiffies;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800697 f->vni = src_vni;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700698 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000699 memcpy(f->eth_addr, mac, ETH_ALEN);
700
Haishuang Yan17b46362016-11-29 09:59:36 +0800701 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
702 if (rc < 0) {
703 kfree(f);
704 return rc;
705 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700706
stephen hemmingerd3428942012-10-01 12:32:35 +0000707 ++vxlan->addrcnt;
708 hlist_add_head_rcu(&f->hlist,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800709 vxlan_fdb_head(vxlan, mac, src_vni));
stephen hemmingerd3428942012-10-01 12:32:35 +0000710 }
711
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200712 if (notify) {
713 if (rd == NULL)
714 rd = first_remote_rtnl(f);
715 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
716 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000717
718 return 0;
719}
720
Wei Yongjun6706c822013-04-11 19:00:35 +0000721static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000722{
723 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700724 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000725
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100726 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
727 dst_cache_destroy(&rd->dst_cache);
David Stevens66817122013-03-15 04:35:51 +0000728 kfree(rd);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +0100729 }
David Stevens66817122013-03-15 04:35:51 +0000730 kfree(f);
731}
732
stephen hemmingerd3428942012-10-01 12:32:35 +0000733static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
734{
735 netdev_dbg(vxlan->dev,
736 "delete %pM\n", f->eth_addr);
737
738 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200739 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000740
741 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000742 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000743}
744
Lance Richardson35cf2842017-05-29 13:25:57 -0400745static void vxlan_dst_free(struct rcu_head *head)
746{
747 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
748
749 dst_cache_destroy(&rd->dst_cache);
750 kfree(rd);
751}
752
753static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
754 struct vxlan_rdst *rd)
755{
756 list_del_rcu(&rd->list);
757 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
758 call_rcu(&rd->rcu, vxlan_dst_free);
759}
760
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300761static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800762 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
763 __be32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300764{
765 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800766 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300767
768 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800769 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
770 if (err)
771 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300772 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800773 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
774 if (remote->sa.sa_family == AF_INET) {
775 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
776 ip->sa.sa_family = AF_INET;
777#if IS_ENABLED(CONFIG_IPV6)
778 } else {
779 ip->sin6.sin6_addr = in6addr_any;
780 ip->sa.sa_family = AF_INET6;
781#endif
782 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300783 }
784
785 if (tb[NDA_PORT]) {
786 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
787 return -EINVAL;
788 *port = nla_get_be16(tb[NDA_PORT]);
789 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200790 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300791 }
792
793 if (tb[NDA_VNI]) {
794 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
795 return -EINVAL;
Jiri Benc54bfd872016-02-16 21:58:58 +0100796 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300797 } else {
798 *vni = vxlan->default_dst.remote_vni;
799 }
800
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800801 if (tb[NDA_SRC_VNI]) {
802 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
803 return -EINVAL;
804 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
805 } else {
806 *src_vni = vxlan->default_dst.remote_vni;
807 }
808
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300809 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;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800834 __be32 src_vni, vni;
Jiri Benc54bfd872016-02-16 21:58:58 +0100835 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
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800847 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300848 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,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800856 port, src_vni, 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
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800862static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
863 const unsigned char *addr, union vxlan_addr ip,
864 __be16 port, __be32 src_vni, u32 vni, u32 ifindex,
865 u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000866{
stephen hemmingerd3428942012-10-01 12:32:35 +0000867 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300868 struct vxlan_rdst *rd = NULL;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800869 int err = -ENOENT;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300870
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800871 f = vxlan_find_mac(vxlan, addr, src_vni);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300872 if (!f)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800873 return err;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300874
Cong Wange4c7ed42013-08-31 13:44:33 +0800875 if (!vxlan_addr_any(&ip)) {
876 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300877 if (!rd)
878 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000879 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300880
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300881 /* remove a destination if it's not the only one on the list,
882 * otherwise destroy the fdb entry
883 */
884 if (rd && !list_is_singular(&f->remotes)) {
Lance Richardson35cf2842017-05-29 13:25:57 -0400885 vxlan_fdb_dst_destroy(vxlan, f, rd);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300886 goto out;
887 }
888
889 vxlan_fdb_destroy(vxlan, f);
890
891out:
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800892 return 0;
893}
894
895/* Delete entry (via netlink) */
896static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
897 struct net_device *dev,
898 const unsigned char *addr, u16 vid)
899{
900 struct vxlan_dev *vxlan = netdev_priv(dev);
901 union vxlan_addr ip;
902 __be32 src_vni, vni;
903 __be16 port;
904 u32 ifindex;
905 int err;
906
907 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
908 if (err)
909 return err;
910
911 spin_lock_bh(&vxlan->hash_lock);
912 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
913 vid);
stephen hemmingerd3428942012-10-01 12:32:35 +0000914 spin_unlock_bh(&vxlan->hash_lock);
915
916 return err;
917}
918
919/* Dump forwarding table */
920static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400921 struct net_device *dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700922 struct net_device *filter_dev, int *idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000923{
924 struct vxlan_dev *vxlan = netdev_priv(dev);
925 unsigned int h;
Roopa Prabhud2976532016-08-30 21:56:45 -0700926 int err = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000927
928 for (h = 0; h < FDB_HASH_SIZE; ++h) {
929 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000930
Sasha Levinb67bfe02013-02-27 17:06:00 -0800931 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000932 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000933
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700934 list_for_each_entry_rcu(rd, &f->remotes, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -0700935 if (*idx < cb->args[2])
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900936 goto skip;
937
David Stevens66817122013-03-15 04:35:51 +0000938 err = vxlan_fdb_info(skb, vxlan, f,
939 NETLINK_CB(cb->skb).portid,
940 cb->nlh->nlmsg_seq,
941 RTM_NEWNEIGH,
942 NLM_F_MULTI, rd);
Roopa Prabhud2976532016-08-30 21:56:45 -0700943 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700944 goto out;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700945skip:
Roopa Prabhud2976532016-08-30 21:56:45 -0700946 *idx += 1;
Atzm Watanabe07a51cd2015-08-10 23:39:09 +0900947 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000948 }
949 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700950out:
Roopa Prabhud2976532016-08-30 21:56:45 -0700951 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +0000952}
953
954/* Watch incoming packets to learn mapping between Ethernet address
955 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900956 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000957 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700958static bool vxlan_snoop(struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800959 union vxlan_addr *src_ip, const u8 *src_mac,
Matthias Schiffer87613de2017-06-19 10:03:59 +0200960 u32 src_ifindex, __be32 vni)
stephen hemmingerd3428942012-10-01 12:32:35 +0000961{
962 struct vxlan_dev *vxlan = netdev_priv(dev);
963 struct vxlan_fdb *f;
Matthias Schiffer87613de2017-06-19 10:03:59 +0200964 u32 ifindex = 0;
965
966#if IS_ENABLED(CONFIG_IPV6)
967 if (src_ip->sa.sa_family == AF_INET6 &&
968 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
969 ifindex = src_ifindex;
970#endif
stephen hemmingerd3428942012-10-01 12:32:35 +0000971
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -0800972 f = vxlan_find_mac(vxlan, src_mac, vni);
stephen hemmingerd3428942012-10-01 12:32:35 +0000973 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700974 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700975
Matthias Schiffer87613de2017-06-19 10:03:59 +0200976 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
977 rdst->remote_ifindex == ifindex))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700978 return false;
979
980 /* Don't migrate static entries, drop packets */
Roopa Prabhue0090a92017-06-11 16:32:50 -0700981 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700982 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000983
984 if (net_ratelimit())
985 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800986 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +0100987 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +0000988
Cong Wange4c7ed42013-08-31 13:44:33 +0800989 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000990 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200991 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000992 } else {
993 /* learned new entry */
994 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700995
996 /* close off race between vxlan_flush and incoming packets */
997 if (netif_running(dev))
998 vxlan_fdb_create(vxlan, src_mac, src_ip,
999 NUD_REACHABLE,
1000 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001001 vxlan->cfg.dst_port,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001002 vni,
stephen hemminger3bf74b12013-06-17 12:09:57 -07001003 vxlan->default_dst.remote_vni,
Matthias Schiffer87613de2017-06-19 10:03:59 +02001004 ifindex, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001005 spin_unlock(&vxlan->hash_lock);
1006 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001007
1008 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001009}
1010
stephen hemmingerd3428942012-10-01 12:32:35 +00001011/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001012static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001013{
stephen hemminger553675f2013-05-16 11:35:20 +00001014 struct vxlan_dev *vxlan;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001015 struct vxlan_sock *sock4;
Arnd Bergmann4053ab12016-11-07 22:09:07 +01001016#if IS_ENABLED(CONFIG_IPV6)
1017 struct vxlan_sock *sock6;
1018#endif
Jiri Bencb1be00a2015-09-24 13:50:02 +02001019 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
stephen hemmingerd3428942012-10-01 12:32:35 +00001020
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001021 sock4 = rtnl_dereference(dev->vn4_sock);
1022
Gao feng95ab0992013-12-10 16:37:33 +08001023 /* The vxlan_sock is only used by dev, leaving group has
1024 * no effect on other vxlan devices.
1025 */
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001026 if (family == AF_INET && sock4 && atomic_read(&sock4->refcnt) == 1)
Gao feng95ab0992013-12-10 16:37:33 +08001027 return false;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001028#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001029 sock6 = rtnl_dereference(dev->vn6_sock);
1030 if (family == AF_INET6 && sock6 && atomic_read(&sock6->refcnt) == 1)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001031 return false;
1032#endif
Gao feng95ab0992013-12-10 16:37:33 +08001033
stephen hemminger553675f2013-05-16 11:35:20 +00001034 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001035 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001036 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001037
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001038 if (family == AF_INET &&
1039 rtnl_dereference(vxlan->vn4_sock) != sock4)
Gao feng95ab0992013-12-10 16:37:33 +08001040 continue;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001041#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001042 if (family == AF_INET6 &&
1043 rtnl_dereference(vxlan->vn6_sock) != sock6)
Jiri Bencb1be00a2015-09-24 13:50:02 +02001044 continue;
1045#endif
Gao feng95ab0992013-12-10 16:37:33 +08001046
1047 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1048 &dev->default_dst.remote_ip))
1049 continue;
1050
1051 if (vxlan->default_dst.remote_ifindex !=
1052 dev->default_dst.remote_ifindex)
1053 continue;
1054
1055 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001056 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001057
1058 return false;
1059}
1060
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001061static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001062{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001063 struct vxlan_net *vn;
Pravin B Shelar012a5722013-08-19 11:23:07 -07001064
Jiri Bencb1be00a2015-09-24 13:50:02 +02001065 if (!vs)
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001066 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001067 if (!atomic_dec_and_test(&vs->refcnt))
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001068 return false;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001069
Jiri Bencb1be00a2015-09-24 13:50:02 +02001070 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001071 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001072 hlist_del_rcu(&vs->hlist);
Alexander Duycke7b3db52016-06-16 12:20:52 -07001073 udp_tunnel_notify_del_rx_port(vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07001074 (vs->flags & VXLAN_F_GPE) ?
1075 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07001076 UDP_TUNNEL_TYPE_VXLAN);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001077 spin_unlock(&vn->sock_lock);
1078
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001079 return true;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001080}
1081
Jiri Bencb1be00a2015-09-24 13:50:02 +02001082static void vxlan_sock_release(struct vxlan_dev *vxlan)
1083{
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001084 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001085#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001086 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1087
Mark Bloch57d88182017-06-07 14:36:58 +03001088 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001089#endif
1090
Mark Bloch57d88182017-06-07 14:36:58 +03001091 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001092 synchronize_net();
1093
Mark Blocha53cb292017-06-02 03:24:08 +03001094 vxlan_vs_del_dev(vxlan);
1095
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001096 if (__vxlan_sock_release_prep(sock4)) {
1097 udp_tunnel_sock_release(sock4->sock);
1098 kfree(sock4);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001099 }
1100
1101#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001102 if (__vxlan_sock_release_prep(sock6)) {
1103 udp_tunnel_sock_release(sock6->sock);
1104 kfree(sock6);
Hannes Frederic Sowa544a7732016-04-09 12:46:23 +02001105 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02001106#endif
1107}
1108
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001109/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001110 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001111 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001112static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001113{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001114 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001115 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1116 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001117 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001118
Cong Wange4c7ed42013-08-31 13:44:33 +08001119 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001120 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001121 struct ip_mreqn mreq = {
1122 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1123 .imr_ifindex = ifindex,
1124 };
1125
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001126 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001127 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001128 ret = ip_mc_join_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001129 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001130#if IS_ENABLED(CONFIG_IPV6)
1131 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001132 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1133
1134 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001135 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001136 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1137 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001138 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001139#endif
1140 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001141
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001142 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001143}
1144
1145/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001146static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001147{
Jiri Bencb1be00a2015-09-24 13:50:02 +02001148 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001149 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1150 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001151 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001152
Cong Wange4c7ed42013-08-31 13:44:33 +08001153 if (ip->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001154 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08001155 struct ip_mreqn mreq = {
1156 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1157 .imr_ifindex = ifindex,
1158 };
1159
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001160 sk = sock4->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001161 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001162 ret = ip_mc_leave_group(sk, &mreq);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001163 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001164#if IS_ENABLED(CONFIG_IPV6)
1165 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001166 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1167
1168 sk = sock6->sock->sk;
Jiri Bencb1be00a2015-09-24 13:50:02 +02001169 lock_sock(sk);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001170 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1171 &ip->sin6.sin6_addr);
Jiri Bencb1be00a2015-09-24 13:50:02 +02001172 release_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001173#endif
1174 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001175
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001176 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001177}
1178
Jiri Bencf14eceb2016-02-16 21:59:01 +01001179static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1180 struct sk_buff *skb, u32 vxflags)
Tom Herbertdfd86452015-01-12 17:00:38 -08001181{
Jiri Benc7d34fa72016-03-21 17:50:05 +01001182 size_t start, offset;
Tom Herbertdfd86452015-01-12 17:00:38 -08001183
Jiri Bencf14eceb2016-02-16 21:59:01 +01001184 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1185 goto out;
Tom Herbertb7fe10e2015-08-19 17:07:32 -07001186
Jiri Bencf14eceb2016-02-16 21:59:01 +01001187 start = vxlan_rco_start(unparsed->vx_vni);
1188 offset = start + vxlan_rco_offset(unparsed->vx_vni);
Tom Herbertdfd86452015-01-12 17:00:38 -08001189
Jiri Benc7d34fa72016-03-21 17:50:05 +01001190 if (!pskb_may_pull(skb, offset + sizeof(u16)))
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001191 return false;
Tom Herbertdfd86452015-01-12 17:00:38 -08001192
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001193 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1194 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
Jiri Bencf14eceb2016-02-16 21:59:01 +01001195out:
1196 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1197 unparsed->vx_vni &= VXLAN_VNI_MASK;
Jiri Bencbe5cfea2016-02-16 21:58:59 +01001198 return true;
Tom Herbertdfd86452015-01-12 17:00:38 -08001199}
1200
Jiri Bencf14eceb2016-02-16 21:59:01 +01001201static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
Jiri Benc64f87d32016-02-23 18:02:55 +01001202 struct sk_buff *skb, u32 vxflags,
Jiri Benc10a5af22016-02-23 18:02:59 +01001203 struct vxlan_metadata *md)
Jiri Benc3288af02016-02-16 21:59:00 +01001204{
Jiri Bencf14eceb2016-02-16 21:59:01 +01001205 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
Jiri Benc10a5af22016-02-23 18:02:59 +01001206 struct metadata_dst *tun_dst;
Jiri Benc3288af02016-02-16 21:59:00 +01001207
Jiri Bencf14eceb2016-02-16 21:59:01 +01001208 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1209 goto out;
1210
Jiri Benc3288af02016-02-16 21:59:00 +01001211 md->gbp = ntohs(gbp->policy_id);
1212
Jiri Benc10a5af22016-02-23 18:02:59 +01001213 tun_dst = (struct metadata_dst *)skb_dst(skb);
David S. Miller810813c2016-03-08 12:34:12 -05001214 if (tun_dst) {
Jiri Benc3288af02016-02-16 21:59:00 +01001215 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
David S. Miller810813c2016-03-08 12:34:12 -05001216 tun_dst->u.tun_info.options_len = sizeof(*md);
1217 }
Jiri Benc3288af02016-02-16 21:59:00 +01001218 if (gbp->dont_learn)
1219 md->gbp |= VXLAN_GBP_DONT_LEARN;
1220
1221 if (gbp->policy_applied)
1222 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001223
Jiri Benc64f87d32016-02-23 18:02:55 +01001224 /* In flow-based mode, GBP is carried in dst_metadata */
1225 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1226 skb->mark = md->gbp;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001227out:
1228 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
Jiri Benc3288af02016-02-16 21:59:00 +01001229}
1230
Jiri Bence1e53142016-04-05 14:47:13 +02001231static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
Jiri Benc61618ee2016-04-11 17:06:08 +02001232 __be16 *protocol,
Jiri Bence1e53142016-04-05 14:47:13 +02001233 struct sk_buff *skb, u32 vxflags)
1234{
1235 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1236
1237 /* Need to have Next Protocol set for interfaces in GPE mode. */
1238 if (!gpe->np_applied)
1239 return false;
1240 /* "The initial version is 0. If a receiver does not support the
1241 * version indicated it MUST drop the packet.
1242 */
1243 if (gpe->version != 0)
1244 return false;
1245 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1246 * processing MUST occur." However, we don't implement OAM
1247 * processing, thus drop the packet.
1248 */
1249 if (gpe->oam_flag)
1250 return false;
1251
1252 switch (gpe->next_protocol) {
1253 case VXLAN_GPE_NP_IPV4:
1254 *protocol = htons(ETH_P_IP);
1255 break;
1256 case VXLAN_GPE_NP_IPV6:
1257 *protocol = htons(ETH_P_IPV6);
1258 break;
1259 case VXLAN_GPE_NP_ETHERNET:
1260 *protocol = htons(ETH_P_TEB);
1261 break;
1262 default:
1263 return false;
1264 }
1265
1266 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1267 return true;
1268}
1269
Jiri Benc1ab016e2016-02-23 18:02:56 +01001270static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1271 struct vxlan_sock *vs,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001272 struct sk_buff *skb, __be32 vni)
Thomas Graf614732e2015-07-21 10:44:06 +02001273{
Thomas Graf614732e2015-07-21 10:44:06 +02001274 union vxlan_addr saddr;
Matthias Schiffer87613de2017-06-19 10:03:59 +02001275 u32 ifindex = skb->dev->ifindex;
Thomas Graf614732e2015-07-21 10:44:06 +02001276
Thomas Graf614732e2015-07-21 10:44:06 +02001277 skb_reset_mac_header(skb);
Thomas Graf614732e2015-07-21 10:44:06 +02001278 skb->protocol = eth_type_trans(skb, vxlan->dev);
1279 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1280
1281 /* Ignore packet loops (and multicast echo) */
1282 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001283 return false;
Thomas Graf614732e2015-07-21 10:44:06 +02001284
Jiri Benc760c6802016-02-23 18:02:57 +01001285 /* Get address from the outer IP header */
Jiri Bencce212d02015-12-07 16:29:08 +01001286 if (vxlan_get_sk_family(vs) == AF_INET) {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001287 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001288 saddr.sa.sa_family = AF_INET;
1289#if IS_ENABLED(CONFIG_IPV6)
1290 } else {
Jiri Benc1ab016e2016-02-23 18:02:56 +01001291 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
Thomas Graf614732e2015-07-21 10:44:06 +02001292 saddr.sa.sa_family = AF_INET6;
1293#endif
1294 }
1295
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001296 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
Matthias Schiffer87613de2017-06-19 10:03:59 +02001297 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
Jiri Benc1ab016e2016-02-23 18:02:56 +01001298 return false;
1299
1300 return true;
1301}
1302
Jiri Benc760c6802016-02-23 18:02:57 +01001303static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1304 struct sk_buff *skb)
1305{
1306 int err = 0;
1307
1308 if (vxlan_get_sk_family(vs) == AF_INET)
1309 err = IP_ECN_decapsulate(oiph, skb);
1310#if IS_ENABLED(CONFIG_IPV6)
1311 else
1312 err = IP6_ECN_decapsulate(oiph, skb);
1313#endif
1314
1315 if (unlikely(err) && log_ecn_error) {
1316 if (vxlan_get_sk_family(vs) == AF_INET)
1317 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1318 &((struct iphdr *)oiph)->saddr,
1319 ((struct iphdr *)oiph)->tos);
1320 else
1321 net_info_ratelimited("non-ECT from %pI6\n",
1322 &((struct ipv6hdr *)oiph)->saddr);
1323 }
1324 return err <= 1;
1325}
1326
stephen hemmingerd3428942012-10-01 12:32:35 +00001327/* Callback from net/ipv4/udp.c to receive packets */
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001328static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
stephen hemmingerd3428942012-10-01 12:32:35 +00001329{
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001330 struct pcpu_sw_netstats *stats;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001331 struct vxlan_dev *vxlan;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001332 struct vxlan_sock *vs;
Jiri Bencf14eceb2016-02-16 21:59:01 +01001333 struct vxlanhdr unparsed;
Thomas Grafee122c72015-07-21 10:43:58 +02001334 struct vxlan_metadata _md;
1335 struct vxlan_metadata *md = &_md;
Jiri Benc61618ee2016-04-11 17:06:08 +02001336 __be16 protocol = htons(ETH_P_TEB);
Jiri Bence1e53142016-04-05 14:47:13 +02001337 bool raw_proto = false;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001338 void *oiph;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001339 __be32 vni = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001340
Jiri Bence1e53142016-04-05 14:47:13 +02001341 /* Need UDP and VXLAN header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001342 if (!pskb_may_pull(skb, VXLAN_HLEN))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001343 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001344
Jiri Bencf14eceb2016-02-16 21:59:01 +01001345 unparsed = *vxlan_hdr(skb);
Jiri Benc288b01c2016-02-16 21:59:02 +01001346 /* VNI flag always required to be set */
1347 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1348 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1349 ntohl(vxlan_hdr(skb)->vx_flags),
1350 ntohl(vxlan_hdr(skb)->vx_vni));
1351 /* Return non vxlan pkt */
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +02001352 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001353 }
Jiri Benc288b01c2016-02-16 21:59:02 +01001354 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1355 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
stephen hemmingerd3428942012-10-01 12:32:35 +00001356
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001357 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001358 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001359 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001360
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001361 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1362
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001363 vxlan = vxlan_vs_find_vni(vs, vni);
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001364 if (!vxlan)
1365 goto drop;
1366
Jiri Bence1e53142016-04-05 14:47:13 +02001367 /* For backwards compatibility, only allow reserved fields to be
1368 * used by VXLAN extensions if explicitly requested.
1369 */
1370 if (vs->flags & VXLAN_F_GPE) {
1371 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1372 goto drop;
1373 raw_proto = true;
1374 }
1375
1376 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1377 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1378 goto drop;
Jiri Bencc9e78ef2016-02-18 11:22:51 +01001379
Thomas Grafee122c72015-07-21 10:43:58 +02001380 if (vxlan_collect_metadata(vs)) {
Jiri Benc10a5af22016-02-23 18:02:59 +01001381 struct metadata_dst *tun_dst;
Jiri Benc07dabf22016-02-18 19:19:29 +01001382
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001383 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
Amir Vadaid817f432016-09-08 16:23:45 +03001384 key32_to_tunnel_id(vni), sizeof(*md));
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07001385
Thomas Grafee122c72015-07-21 10:43:58 +02001386 if (!tun_dst)
1387 goto drop;
1388
Geert Uytterhoeven0f1b7352015-09-04 12:49:32 +02001389 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
Jiri Benc10a5af22016-02-23 18:02:59 +01001390
1391 skb_dst_set(skb, (struct dst_entry *)tun_dst);
Thomas Grafee122c72015-07-21 10:43:58 +02001392 } else {
1393 memset(md, 0, sizeof(*md));
1394 }
1395
Jiri Bencf14eceb2016-02-16 21:59:01 +01001396 if (vs->flags & VXLAN_F_REMCSUM_RX)
1397 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1398 goto drop;
1399 if (vs->flags & VXLAN_F_GBP)
Jiri Benc10a5af22016-02-23 18:02:59 +01001400 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001401 /* Note that GBP and GPE can never be active together. This is
1402 * ensured in vxlan_dev_configure.
1403 */
Thomas Graf35114942015-01-15 03:53:55 +01001404
Jiri Bencf14eceb2016-02-16 21:59:01 +01001405 if (unparsed.vx_flags || unparsed.vx_vni) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001406 /* If there are any unprocessed flags remaining treat
1407 * this as a malformed packet. This behavior diverges from
1408 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1409 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001410 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001411 * is more robust and provides a little more security in
1412 * adding extensions to VXLAN.
1413 */
Jiri Benc288b01c2016-02-16 21:59:02 +01001414 goto drop;
Tom Herbert3bf39472015-01-08 12:31:18 -08001415 }
1416
Jiri Bence1e53142016-04-05 14:47:13 +02001417 if (!raw_proto) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001418 if (!vxlan_set_mac(vxlan, vs, skb, vni))
Jiri Bence1e53142016-04-05 14:47:13 +02001419 goto drop;
1420 } else {
Jiri Benc8be0cfa2016-05-13 10:48:42 +02001421 skb_reset_mac_header(skb);
Jiri Bence1e53142016-04-05 14:47:13 +02001422 skb->dev = vxlan->dev;
1423 skb->pkt_type = PACKET_HOST;
1424 }
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001425
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01001426 oiph = skb_network_header(skb);
1427 skb_reset_network_header(skb);
1428
1429 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1430 ++vxlan->dev->stats.rx_frame_errors;
1431 ++vxlan->dev->stats.rx_errors;
1432 goto drop;
1433 }
1434
1435 stats = this_cpu_ptr(vxlan->dev->tstats);
1436 u64_stats_update_begin(&stats->syncp);
1437 stats->rx_packets++;
1438 stats->rx_bytes += skb->len;
1439 u64_stats_update_end(&stats->syncp);
1440
1441 gro_cells_receive(&vxlan->gro_cells, skb);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001442 return 0;
1443
1444drop:
Jiri Benc288b01c2016-02-16 21:59:02 +01001445 /* Consume bad packet */
1446 kfree_skb(skb);
1447 return 0;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001448}
1449
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001450static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
David Stevense4f67ad2012-11-20 02:50:14 +00001451{
1452 struct vxlan_dev *vxlan = netdev_priv(dev);
1453 struct arphdr *parp;
1454 u8 *arpptr, *sha;
1455 __be32 sip, tip;
1456 struct neighbour *n;
1457
1458 if (dev->flags & IFF_NOARP)
1459 goto out;
1460
1461 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1462 dev->stats.tx_dropped++;
1463 goto out;
1464 }
1465 parp = arp_hdr(skb);
1466
1467 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1468 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1469 parp->ar_pro != htons(ETH_P_IP) ||
1470 parp->ar_op != htons(ARPOP_REQUEST) ||
1471 parp->ar_hln != dev->addr_len ||
1472 parp->ar_pln != 4)
1473 goto out;
1474 arpptr = (u8 *)parp + sizeof(struct arphdr);
1475 sha = arpptr;
1476 arpptr += dev->addr_len; /* sha */
1477 memcpy(&sip, arpptr, sizeof(sip));
1478 arpptr += sizeof(sip);
1479 arpptr += dev->addr_len; /* tha */
1480 memcpy(&tip, arpptr, sizeof(tip));
1481
1482 if (ipv4_is_loopback(tip) ||
1483 ipv4_is_multicast(tip))
1484 goto out;
1485
1486 n = neigh_lookup(&arp_tbl, &tip, dev);
1487
1488 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001489 struct vxlan_fdb *f;
1490 struct sk_buff *reply;
1491
1492 if (!(n->nud_state & NUD_CONNECTED)) {
1493 neigh_release(n);
1494 goto out;
1495 }
1496
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001497 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wange4c7ed42013-08-31 13:44:33 +08001498 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001499 /* bridge-local neighbor */
1500 neigh_release(n);
1501 goto out;
1502 }
1503
1504 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1505 n->ha, sha);
1506
1507 neigh_release(n);
1508
David Stevens73461352014-03-18 12:32:29 -04001509 if (reply == NULL)
1510 goto out;
1511
David Stevense4f67ad2012-11-20 02:50:14 +00001512 skb_reset_mac_header(reply);
1513 __skb_pull(reply, skb_network_offset(reply));
1514 reply->ip_summed = CHECKSUM_UNNECESSARY;
1515 reply->pkt_type = PACKET_HOST;
1516
1517 if (netif_rx_ni(reply) == NET_RX_DROP)
1518 dev->stats.rx_dropped++;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001519 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001520 union vxlan_addr ipa = {
1521 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001522 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001523 };
1524
1525 vxlan_ip_miss(dev, &ipa);
1526 }
David Stevense4f67ad2012-11-20 02:50:14 +00001527out:
1528 consume_skb(skb);
1529 return NETDEV_TX_OK;
1530}
1531
Cong Wangf564f452013-08-31 13:44:36 +08001532#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001533static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1534 struct neighbour *n, bool isrouter)
1535{
1536 struct net_device *dev = request->dev;
1537 struct sk_buff *reply;
1538 struct nd_msg *ns, *na;
1539 struct ipv6hdr *pip6;
1540 u8 *daddr;
1541 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1542 int ns_olen;
1543 int i, len;
1544
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001545 if (dev == NULL || !pskb_may_pull(request, request->len))
David Stevens4b29dba2014-03-24 10:39:58 -04001546 return NULL;
1547
1548 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1549 sizeof(*na) + na_olen + dev->needed_tailroom;
1550 reply = alloc_skb(len, GFP_ATOMIC);
1551 if (reply == NULL)
1552 return NULL;
1553
1554 reply->protocol = htons(ETH_P_IPV6);
1555 reply->dev = dev;
1556 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1557 skb_push(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001558 skb_reset_mac_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001559
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001560 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
David Stevens4b29dba2014-03-24 10:39:58 -04001561
1562 daddr = eth_hdr(request)->h_source;
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001563 ns_olen = request->len - skb_network_offset(request) -
1564 sizeof(struct ipv6hdr) - sizeof(*ns);
David Stevens4b29dba2014-03-24 10:39:58 -04001565 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1566 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1567 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1568 break;
1569 }
1570 }
1571
1572 /* Ethernet header */
1573 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1574 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1575 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1576 reply->protocol = htons(ETH_P_IPV6);
1577
1578 skb_pull(reply, sizeof(struct ethhdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001579 skb_reset_network_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001580 skb_put(reply, sizeof(struct ipv6hdr));
1581
1582 /* IPv6 header */
1583
1584 pip6 = ipv6_hdr(reply);
1585 memset(pip6, 0, sizeof(struct ipv6hdr));
1586 pip6->version = 6;
1587 pip6->priority = ipv6_hdr(request)->priority;
1588 pip6->nexthdr = IPPROTO_ICMPV6;
1589 pip6->hop_limit = 255;
1590 pip6->daddr = ipv6_hdr(request)->saddr;
1591 pip6->saddr = *(struct in6_addr *)n->primary_key;
1592
1593 skb_pull(reply, sizeof(struct ipv6hdr));
Zhang Shengju6297b912016-03-03 01:16:54 +00001594 skb_reset_transport_header(reply);
David Stevens4b29dba2014-03-24 10:39:58 -04001595
David Stevens4b29dba2014-03-24 10:39:58 -04001596 /* Neighbor Advertisement */
Johannes Bergb080db52017-06-16 14:29:19 +02001597 na = skb_put_zero(reply, sizeof(*na) + na_olen);
David Stevens4b29dba2014-03-24 10:39:58 -04001598 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1599 na->icmph.icmp6_router = isrouter;
1600 na->icmph.icmp6_override = 1;
1601 na->icmph.icmp6_solicited = 1;
1602 na->target = ns->target;
1603 ether_addr_copy(&na->opt[2], n->ha);
1604 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1605 na->opt[1] = na_olen >> 3;
1606
1607 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1608 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1609 csum_partial(na, sizeof(*na)+na_olen, 0));
1610
1611 pip6->payload_len = htons(sizeof(*na)+na_olen);
1612
1613 skb_push(reply, sizeof(struct ipv6hdr));
1614
1615 reply->ip_summed = CHECKSUM_UNNECESSARY;
1616
1617 return reply;
1618}
1619
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001620static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
Cong Wangf564f452013-08-31 13:44:36 +08001621{
1622 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001623 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001624 const struct ipv6hdr *iphdr;
Roopa Prabhu8dcd81a2017-02-20 08:41:16 -08001625 const struct in6_addr *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001626 struct neighbour *n;
1627 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001628
1629 in6_dev = __in6_dev_get(dev);
1630 if (!in6_dev)
1631 goto out;
1632
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001633 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
1634 goto out;
1635
Cong Wangf564f452013-08-31 13:44:36 +08001636 iphdr = ipv6_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001637 daddr = &iphdr->daddr;
1638
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02001639 msg = (struct nd_msg *)(iphdr + 1);
Cong Wangf564f452013-08-31 13:44:36 +08001640 if (msg->icmph.icmp6_code != 0 ||
1641 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1642 goto out;
1643
David Stevens4b29dba2014-03-24 10:39:58 -04001644 if (ipv6_addr_loopback(daddr) ||
1645 ipv6_addr_is_multicast(&msg->target))
1646 goto out;
1647
1648 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001649
1650 if (n) {
1651 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001652 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001653
1654 if (!(n->nud_state & NUD_CONNECTED)) {
1655 neigh_release(n);
1656 goto out;
1657 }
1658
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001659 f = vxlan_find_mac(vxlan, n->ha, vni);
Cong Wangf564f452013-08-31 13:44:36 +08001660 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1661 /* bridge-local neighbor */
1662 neigh_release(n);
1663 goto out;
1664 }
1665
David Stevens4b29dba2014-03-24 10:39:58 -04001666 reply = vxlan_na_create(skb, n,
1667 !!(f ? f->flags & NTF_ROUTER : 0));
1668
Cong Wangf564f452013-08-31 13:44:36 +08001669 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001670
1671 if (reply == NULL)
1672 goto out;
1673
1674 if (netif_rx_ni(reply) == NET_RX_DROP)
1675 dev->stats.rx_dropped++;
1676
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001677 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001678 union vxlan_addr ipa = {
1679 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001680 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001681 };
1682
Cong Wangf564f452013-08-31 13:44:36 +08001683 vxlan_ip_miss(dev, &ipa);
1684 }
1685
1686out:
1687 consume_skb(skb);
1688 return NETDEV_TX_OK;
1689}
1690#endif
1691
David Stevense4f67ad2012-11-20 02:50:14 +00001692static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1693{
1694 struct vxlan_dev *vxlan = netdev_priv(dev);
1695 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001696
1697 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1698 return false;
1699
1700 n = NULL;
1701 switch (ntohs(eth_hdr(skb)->h_proto)) {
1702 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001703 {
1704 struct iphdr *pip;
1705
David Stevense4f67ad2012-11-20 02:50:14 +00001706 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1707 return false;
1708 pip = ip_hdr(skb);
1709 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001710 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001711 union vxlan_addr ipa = {
1712 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001713 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001714 };
1715
1716 vxlan_ip_miss(dev, &ipa);
1717 return false;
1718 }
1719
David Stevense4f67ad2012-11-20 02:50:14 +00001720 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001721 }
1722#if IS_ENABLED(CONFIG_IPV6)
1723 case ETH_P_IPV6:
1724 {
1725 struct ipv6hdr *pip6;
1726
1727 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1728 return false;
1729 pip6 = ipv6_hdr(skb);
1730 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
Matthias Schifferdc5321d2017-06-19 10:03:56 +02001731 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
Cong Wange15a00a2013-08-31 13:44:34 +08001732 union vxlan_addr ipa = {
1733 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001734 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001735 };
1736
1737 vxlan_ip_miss(dev, &ipa);
1738 return false;
1739 }
1740
1741 break;
1742 }
1743#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001744 default:
1745 return false;
1746 }
1747
1748 if (n) {
1749 bool diff;
1750
Joe Perches7367d0b2013-09-01 11:51:23 -07001751 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001752 if (diff) {
1753 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1754 dev->addr_len);
1755 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1756 }
1757 neigh_release(n);
1758 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001759 }
1760
David Stevense4f67ad2012-11-20 02:50:14 +00001761 return false;
1762}
1763
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001764static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001765 struct vxlan_metadata *md)
1766{
1767 struct vxlanhdr_gbp *gbp;
1768
Thomas Grafdb79a622015-02-04 17:00:04 +01001769 if (!md->gbp)
1770 return;
1771
Thomas Graf35114942015-01-15 03:53:55 +01001772 gbp = (struct vxlanhdr_gbp *)vxh;
Jiri Benc54bfd872016-02-16 21:58:58 +01001773 vxh->vx_flags |= VXLAN_HF_GBP;
Thomas Graf35114942015-01-15 03:53:55 +01001774
1775 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1776 gbp->dont_learn = 1;
1777
1778 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1779 gbp->policy_applied = 1;
1780
1781 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1782}
1783
Jiri Bence1e53142016-04-05 14:47:13 +02001784static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1785 __be16 protocol)
1786{
1787 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1788
1789 gpe->np_applied = 1;
1790
1791 switch (protocol) {
1792 case htons(ETH_P_IP):
1793 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1794 return 0;
1795 case htons(ETH_P_IPV6):
1796 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1797 return 0;
1798 case htons(ETH_P_TEB):
1799 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1800 return 0;
1801 }
1802 return -EPFNOSUPPORT;
1803}
1804
Jiri Bencf491e562016-02-02 18:09:16 +01001805static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1806 int iphdr_len, __be32 vni,
1807 struct vxlan_metadata *md, u32 vxflags,
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01001808 bool udp_sum)
Cong Wange4c7ed42013-08-31 13:44:33 +08001809{
Cong Wange4c7ed42013-08-31 13:44:33 +08001810 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001811 int min_headroom;
1812 int err;
Tom Herbertdfd86452015-01-12 17:00:38 -08001813 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
Jiri Bence1e53142016-04-05 14:47:13 +02001814 __be16 inner_protocol = htons(ETH_P_TEB);
Cong Wange4c7ed42013-08-31 13:44:33 +08001815
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001816 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001817 skb->ip_summed == CHECKSUM_PARTIAL) {
1818 int csum_start = skb_checksum_start_offset(skb);
1819
1820 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1821 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1822 (skb->csum_offset == offsetof(struct udphdr, check) ||
Edward Creeb5708502016-02-11 20:57:17 +00001823 skb->csum_offset == offsetof(struct tcphdr, check)))
Tom Herbertdfd86452015-01-12 17:00:38 -08001824 type |= SKB_GSO_TUNNEL_REMCSUM;
Tom Herbertdfd86452015-01-12 17:00:38 -08001825 }
1826
Cong Wange4c7ed42013-08-31 13:44:33 +08001827 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
pravin shelar4a4f86c2016-11-13 20:43:52 -08001828 + VXLAN_HLEN + iphdr_len;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001829
1830 /* Need space for new headers (invalidates iph ptr) */
1831 err = skb_cow_head(skb, min_headroom);
Jiri Bence1e53142016-04-05 14:47:13 +02001832 if (unlikely(err))
pravin shelarc46b7892016-11-13 20:43:54 -08001833 return err;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001834
Alexander Duyckaed069d2016-04-14 15:33:37 -04001835 err = iptunnel_handle_offloads(skb, type);
1836 if (err)
pravin shelarc46b7892016-11-13 20:43:54 -08001837 return err;
Jesse Grossb736a622015-04-09 11:19:14 -07001838
Johannes Bergd58ff352017-06-16 14:29:23 +02001839 vxh = __skb_push(skb, sizeof(*vxh));
Jiri Benc54bfd872016-02-16 21:58:58 +01001840 vxh->vx_flags = VXLAN_HF_VNI;
1841 vxh->vx_vni = vxlan_vni_field(vni);
Pravin B Shelar49560532013-08-19 11:23:17 -07001842
Tom Herbertdfd86452015-01-12 17:00:38 -08001843 if (type & SKB_GSO_TUNNEL_REMCSUM) {
Jiri Benc54bfd872016-02-16 21:58:58 +01001844 unsigned int start;
Tom Herbertdfd86452015-01-12 17:00:38 -08001845
Jiri Benc54bfd872016-02-16 21:58:58 +01001846 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1847 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1848 vxh->vx_flags |= VXLAN_HF_RCO;
Tom Herbertdfd86452015-01-12 17:00:38 -08001849
1850 if (!skb_is_gso(skb)) {
1851 skb->ip_summed = CHECKSUM_NONE;
1852 skb->encapsulation = 0;
1853 }
1854 }
1855
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001856 if (vxflags & VXLAN_F_GBP)
1857 vxlan_build_gbp_hdr(vxh, vxflags, md);
Jiri Bence1e53142016-04-05 14:47:13 +02001858 if (vxflags & VXLAN_F_GPE) {
1859 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1860 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08001861 return err;
Jiri Bence1e53142016-04-05 14:47:13 +02001862 inner_protocol = skb->protocol;
1863 }
Thomas Graf35114942015-01-15 03:53:55 +01001864
Jiri Bence1e53142016-04-05 14:47:13 +02001865 skb_set_inner_protocol(skb, inner_protocol);
Pravin B Shelar039f5062015-12-24 14:34:54 -08001866 return 0;
Pravin B Shelar49560532013-08-19 11:23:17 -07001867}
Pravin B Shelar49560532013-08-19 11:23:17 -07001868
pravin shelar655c3de2016-11-13 20:43:55 -08001869static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
1870 struct vxlan_sock *sock4,
Jiri Benc1a8496b2016-02-02 18:09:14 +01001871 struct sk_buff *skb, int oif, u8 tos,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001872 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001873 struct dst_cache *dst_cache,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001874 const struct ip_tunnel_info *info)
Jiri Benc1a8496b2016-02-02 18:09:14 +01001875{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001876 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01001877 struct rtable *rt = NULL;
1878 struct flowi4 fl4;
1879
pravin shelar655c3de2016-11-13 20:43:55 -08001880 if (!sock4)
1881 return ERR_PTR(-EIO);
1882
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001883 if (tos && !info)
1884 use_cache = false;
1885 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001886 rt = dst_cache_get_ip4(dst_cache, saddr);
1887 if (rt)
1888 return rt;
1889 }
1890
Jiri Benc1a8496b2016-02-02 18:09:14 +01001891 memset(&fl4, 0, sizeof(fl4));
1892 fl4.flowi4_oif = oif;
1893 fl4.flowi4_tos = RT_TOS(tos);
1894 fl4.flowi4_mark = skb->mark;
1895 fl4.flowi4_proto = IPPROTO_UDP;
1896 fl4.daddr = daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001897 fl4.saddr = *saddr;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001898 fl4.fl4_dport = dport;
1899 fl4.fl4_sport = sport;
Jiri Benc1a8496b2016-02-02 18:09:14 +01001900
1901 rt = ip_route_output_key(vxlan->net, &fl4);
pravin shelar655c3de2016-11-13 20:43:55 -08001902 if (likely(!IS_ERR(rt))) {
1903 if (rt->dst.dev == dev) {
1904 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
1905 ip_rt_put(rt);
1906 return ERR_PTR(-ELOOP);
1907 }
1908
Jiri Benc1a8496b2016-02-02 18:09:14 +01001909 *saddr = fl4.saddr;
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001910 if (use_cache)
1911 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
pravin shelar655c3de2016-11-13 20:43:55 -08001912 } else {
1913 netdev_dbg(dev, "no route to %pI4\n", &daddr);
1914 return ERR_PTR(-ENETUNREACH);
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001915 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01001916 return rt;
1917}
1918
Jiri Bence5d4b292015-12-07 13:04:30 +01001919#if IS_ENABLED(CONFIG_IPV6)
1920static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
pravin shelar655c3de2016-11-13 20:43:55 -08001921 struct net_device *dev,
pravin shelar03dc52a2016-11-13 20:43:53 -08001922 struct vxlan_sock *sock6,
Daniel Borkmann14006152016-03-04 15:15:08 +01001923 struct sk_buff *skb, int oif, u8 tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01001924 __be32 label,
Jiri Bence5d4b292015-12-07 13:04:30 +01001925 const struct in6_addr *daddr,
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001926 struct in6_addr *saddr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001927 __be16 dport, __be16 sport,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001928 struct dst_cache *dst_cache,
1929 const struct ip_tunnel_info *info)
Jiri Bence5d4b292015-12-07 13:04:30 +01001930{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001931 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01001932 struct dst_entry *ndst;
1933 struct flowi6 fl6;
1934 int err;
1935
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001936 if (!sock6)
1937 return ERR_PTR(-EIO);
1938
Daniel Borkmann14006152016-03-04 15:15:08 +01001939 if (tos && !info)
1940 use_cache = false;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001941 if (use_cache) {
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001942 ndst = dst_cache_get_ip6(dst_cache, saddr);
1943 if (ndst)
1944 return ndst;
1945 }
1946
Jiri Bence5d4b292015-12-07 13:04:30 +01001947 memset(&fl6, 0, sizeof(fl6));
1948 fl6.flowi6_oif = oif;
1949 fl6.daddr = *daddr;
pravin shelar272d96a2016-08-05 17:45:36 -07001950 fl6.saddr = *saddr;
Daniel Borkmanneaa93bf2016-03-18 18:37:57 +01001951 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
Jiri Bence5d4b292015-12-07 13:04:30 +01001952 fl6.flowi6_mark = skb->mark;
1953 fl6.flowi6_proto = IPPROTO_UDP;
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00001954 fl6.fl6_dport = dport;
1955 fl6.fl6_sport = sport;
Jiri Bence5d4b292015-12-07 13:04:30 +01001956
1957 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
pravin shelarc6fcc4f2016-10-28 09:59:15 -07001958 sock6->sock->sk,
Jiri Bence5d4b292015-12-07 13:04:30 +01001959 &ndst, &fl6);
pravin shelar655c3de2016-11-13 20:43:55 -08001960 if (unlikely(err < 0)) {
1961 netdev_dbg(dev, "no route to %pI6\n", daddr);
1962 return ERR_PTR(-ENETUNREACH);
1963 }
1964
1965 if (unlikely(ndst->dev == dev)) {
1966 netdev_dbg(dev, "circular route to %pI6\n", daddr);
1967 dst_release(ndst);
1968 return ERR_PTR(-ELOOP);
1969 }
Jiri Bence5d4b292015-12-07 13:04:30 +01001970
1971 *saddr = fl6.saddr;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001972 if (use_cache)
Paolo Abeni0c1d70a2016-02-12 15:43:56 +01001973 dst_cache_set_ip6(dst_cache, ndst, saddr);
Jiri Bence5d4b292015-12-07 13:04:30 +01001974 return ndst;
1975}
1976#endif
1977
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001978/* Bypass encapsulation if the destination is local */
1979static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08001980 struct vxlan_dev *dst_vxlan, __be32 vni)
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001981{
Li RongQing8f849852014-01-04 13:57:59 +08001982 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001983 union vxlan_addr loopback;
1984 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001985 struct net_device *dev = skb->dev;
1986 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001987
Li RongQing8f849852014-01-04 13:57:59 +08001988 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1989 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001990 skb->pkt_type = PACKET_HOST;
1991 skb->encapsulation = 0;
1992 skb->dev = dst_vxlan->dev;
1993 __skb_pull(skb, skb_network_offset(skb));
1994
Cong Wange4c7ed42013-08-31 13:44:33 +08001995 if (remote_ip->sa.sa_family == AF_INET) {
1996 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1997 loopback.sa.sa_family = AF_INET;
1998#if IS_ENABLED(CONFIG_IPV6)
1999 } else {
2000 loopback.sin6.sin6_addr = in6addr_loopback;
2001 loopback.sa.sa_family = AF_INET6;
2002#endif
2003 }
2004
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002005 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
Matthias Schiffer87613de2017-06-19 10:03:59 +02002006 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0,
2007 vni);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002008
2009 u64_stats_update_begin(&tx_stats->syncp);
2010 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002011 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002012 u64_stats_update_end(&tx_stats->syncp);
2013
2014 if (netif_rx(skb) == NET_RX_SUCCESS) {
2015 u64_stats_update_begin(&rx_stats->syncp);
2016 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08002017 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002018 u64_stats_update_end(&rx_stats->syncp);
2019 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08002020 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002021 }
2022}
2023
pravin shelarfee1fad2016-11-13 20:43:56 -08002024static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2025 struct vxlan_dev *vxlan, union vxlan_addr *daddr,
Lance Richardson1f6cc072017-01-18 15:24:57 -05002026 __be16 dst_port, __be32 vni, struct dst_entry *dst,
pravin shelarfee1fad2016-11-13 20:43:56 -08002027 u32 rt_flags)
2028{
2029#if IS_ENABLED(CONFIG_IPV6)
2030 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2031 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2032 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2033 */
2034 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2035#endif
2036 /* Bypass encapsulation if the destination is local */
2037 if (rt_flags & RTCF_LOCAL &&
2038 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2039 struct vxlan_dev *dst_vxlan;
2040
2041 dst_release(dst);
2042 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2043 daddr->sa.sa_family, dst_port,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002044 vxlan->cfg.flags);
pravin shelarfee1fad2016-11-13 20:43:56 -08002045 if (!dst_vxlan) {
2046 dev->stats.tx_errors++;
2047 kfree_skb(skb);
2048
2049 return -ENOENT;
2050 }
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002051 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
pravin shelarfee1fad2016-11-13 20:43:56 -08002052 return 1;
2053 }
2054
2055 return 0;
2056}
2057
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002058static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002059 __be32 default_vni, struct vxlan_rdst *rdst,
2060 bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00002061{
Paolo Abenid71785f2016-02-12 15:43:57 +01002062 struct dst_cache *dst_cache;
Thomas Graf3093fbe2015-07-21 10:44:00 +02002063 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00002064 struct vxlan_dev *vxlan = netdev_priv(dev);
pravin shelar0770b532016-11-13 20:43:57 -08002065 const struct iphdr *old_iph = ip_hdr(skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002066 union vxlan_addr *dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002067 union vxlan_addr remote_ip, local_ip;
Thomas Grafee122c72015-07-21 10:43:58 +02002068 struct vxlan_metadata _md;
2069 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08002070 __be16 src_port = 0, dst_port;
pravin shelar655c3de2016-11-13 20:43:55 -08002071 struct dst_entry *ndst = NULL;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002072 __be32 vni, label;
stephen hemmingerd3428942012-10-01 12:32:35 +00002073 __u8 tos, ttl;
Pravin B Shelar0e6fbc5b2013-06-17 17:49:56 -07002074 int err;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002075 u32 flags = vxlan->cfg.flags;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002076 bool udp_sum = false;
Jiri Bencf491e562016-02-02 18:09:16 +01002077 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
stephen hemmingerd3428942012-10-01 12:32:35 +00002078
Jiri Benc61adedf2015-08-20 13:56:25 +02002079 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002080
Thomas Grafee122c72015-07-21 10:43:58 +02002081 if (rdst) {
pravin shelar0770b532016-11-13 20:43:57 -08002082 dst = &rdst->remote_ip;
2083 if (vxlan_addr_any(dst)) {
2084 if (did_rsc) {
2085 /* short-circuited back to local bridge */
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002086 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
pravin shelar0770b532016-11-13 20:43:57 -08002087 return;
2088 }
2089 goto drop;
2090 }
2091
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002092 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002093 vni = (rdst->remote_vni) ? : default_vni;
Brian Russell11586322017-02-24 17:47:11 +00002094 local_ip = vxlan->cfg.saddr;
Paolo Abenid71785f2016-02-12 15:43:57 +01002095 dst_cache = &rdst->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002096 md->gbp = skb->mark;
2097 ttl = vxlan->cfg.ttl;
2098 if (!ttl && vxlan_addr_multicast(dst))
2099 ttl = 1;
2100
2101 tos = vxlan->cfg.tos;
2102 if (tos == 1)
2103 tos = ip_tunnel_get_dsfield(old_iph, skb);
2104
2105 if (dst->sa.sa_family == AF_INET)
2106 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2107 else
2108 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2109 label = vxlan->cfg.label;
Thomas Grafee122c72015-07-21 10:43:58 +02002110 } else {
2111 if (!info) {
2112 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2113 dev->name);
2114 goto drop;
2115 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002116 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
pravin shelar272d96a2016-08-05 17:45:36 -07002117 if (remote_ip.sa.sa_family == AF_INET) {
Jiri Benca725e512015-08-20 13:56:30 +02002118 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002119 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2120 } else {
Jiri Benca725e512015-08-20 13:56:30 +02002121 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
pravin shelar272d96a2016-08-05 17:45:36 -07002122 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2123 }
Thomas Grafee122c72015-07-21 10:43:58 +02002124 dst = &remote_ip;
pravin shelar0770b532016-11-13 20:43:57 -08002125 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2126 vni = tunnel_id_to_key32(info->key.tun_id);
Paolo Abenid71785f2016-02-12 15:43:57 +01002127 dst_cache = &info->dst_cache;
pravin shelar0770b532016-11-13 20:43:57 -08002128 if (info->options_len)
2129 md = ip_tunnel_info_opts(info);
Jiri Benca725e512015-08-20 13:56:30 +02002130 ttl = info->key.ttl;
2131 tos = info->key.tos;
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002132 label = info->key.label;
Jiri Bencb4ed5ca2016-02-02 18:09:15 +01002133 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Jiri Benca725e512015-08-20 13:56:30 +02002134 }
pravin shelar0770b532016-11-13 20:43:57 -08002135 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2136 vxlan->cfg.port_max, true);
Jiri Benca725e512015-08-20 13:56:30 +02002137
Jakub Kicinski56de8592017-02-24 11:43:36 -08002138 rcu_read_lock();
Cong Wange4c7ed42013-08-31 13:44:33 +08002139 if (dst->sa.sa_family == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002140 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
pravin shelarc46b7892016-11-13 20:43:54 -08002141 struct rtable *rt;
pravin shelar0770b532016-11-13 20:43:57 -08002142 __be16 df = 0;
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002143
pravin shelar655c3de2016-11-13 20:43:55 -08002144 rt = vxlan_get_route(vxlan, dev, sock4, skb,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002145 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002146 dst->sin.sin_addr.s_addr,
Brian Russell11586322017-02-24 17:47:11 +00002147 &local_ip.sin.sin_addr.s_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002148 dst_port, src_port,
Paolo Abenid71785f2016-02-12 15:43:57 +01002149 dst_cache, info);
David S. Miller8ebd1152016-11-15 16:32:11 -05002150 if (IS_ERR(rt)) {
2151 err = PTR_ERR(rt);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002152 goto tx_error;
David S. Miller8ebd1152016-11-15 16:32:11 -05002153 }
pravin shelarfee1fad2016-11-13 20:43:56 -08002154
Cong Wange4c7ed42013-08-31 13:44:33 +08002155 /* Bypass encapsulation if the destination is local */
pravin shelarfee1fad2016-11-13 20:43:56 -08002156 if (!info) {
2157 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2158 dst_port, vni, &rt->dst,
2159 rt->rt_flags);
2160 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002161 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002162 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002163 df = htons(IP_DF);
pravin shelarfee1fad2016-11-13 20:43:56 -08002164 }
Alexander Duyck6ceb31c2016-02-19 11:26:31 -08002165
pravin shelarc46b7892016-11-13 20:43:54 -08002166 ndst = &rt->dst;
Cong Wange4c7ed42013-08-31 13:44:33 +08002167 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2168 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
pravin shelarc46b7892016-11-13 20:43:54 -08002169 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002170 vni, md, flags, udp_sum);
Jiri Bencf491e562016-02-02 18:09:16 +01002171 if (err < 0)
pravin shelarc46b7892016-11-13 20:43:54 -08002172 goto tx_error;
Jiri Bencf491e562016-02-02 18:09:16 +01002173
Brian Russell11586322017-02-24 17:47:11 +00002174 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
Jiri Bencf491e562016-02-02 18:09:16 +01002175 dst->sin.sin_addr.s_addr, tos, ttl, df,
2176 src_port, dst_port, xnet, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002177#if IS_ENABLED(CONFIG_IPV6)
2178 } else {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002179 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Cong Wange4c7ed42013-08-31 13:44:33 +08002180
pravin shelar655c3de2016-11-13 20:43:55 -08002181 ndst = vxlan6_get_route(vxlan, dev, sock6, skb,
Daniel Borkmann14006152016-03-04 15:15:08 +01002182 rdst ? rdst->remote_ifindex : 0, tos,
pravin shelar272d96a2016-08-05 17:45:36 -07002183 label, &dst->sin6.sin6_addr,
Brian Russell11586322017-02-24 17:47:11 +00002184 &local_ip.sin6.sin6_addr,
Martynas Pumputis4ecb1d82017-01-11 15:18:53 +00002185 dst_port, src_port,
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002186 dst_cache, info);
Jiri Bence5d4b292015-12-07 13:04:30 +01002187 if (IS_ERR(ndst)) {
David S. Miller8ebd1152016-11-15 16:32:11 -05002188 err = PTR_ERR(ndst);
pravin shelarc46b7892016-11-13 20:43:54 -08002189 ndst = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002190 goto tx_error;
2191 }
pravin shelar655c3de2016-11-13 20:43:55 -08002192
pravin shelarfee1fad2016-11-13 20:43:56 -08002193 if (!info) {
2194 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
Cong Wange4c7ed42013-08-31 13:44:33 +08002195
pravin shelarfee1fad2016-11-13 20:43:56 -08002196 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2197 dst_port, vni, ndst,
2198 rt6i_flags);
2199 if (err)
Jakub Kicinski56de8592017-02-24 11:43:36 -08002200 goto out_unlock;
pravin shelarfee1fad2016-11-13 20:43:56 -08002201 }
Jesse Gross35e2d112016-01-20 16:22:47 -08002202
Daniel Borkmann14006152016-03-04 15:15:08 +01002203 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
Cong Wange4c7ed42013-08-31 13:44:33 +08002204 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Jiri Bencf491e562016-02-02 18:09:16 +01002205 skb_scrub_packet(skb, xnet);
2206 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
Jiri Benc54bfd872016-02-16 21:58:58 +01002207 vni, md, flags, udp_sum);
pravin shelarc46b7892016-11-13 20:43:54 -08002208 if (err < 0)
2209 goto tx_error;
2210
pravin shelar0770b532016-11-13 20:43:57 -08002211 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
Brian Russell11586322017-02-24 17:47:11 +00002212 &local_ip.sin6.sin6_addr,
pravin shelar272d96a2016-08-05 17:45:36 -07002213 &dst->sin6.sin6_addr, tos, ttl,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002214 label, src_port, dst_port, !udp_sum);
Cong Wange4c7ed42013-08-31 13:44:33 +08002215#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002216 }
Jakub Kicinski56de8592017-02-24 11:43:36 -08002217out_unlock:
2218 rcu_read_unlock();
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002219 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002220
2221drop:
2222 dev->stats.tx_dropped++;
stephen hemmingerd3428942012-10-01 12:32:35 +00002223 dev_kfree_skb(skb);
pravin shelarc46b7892016-11-13 20:43:54 -08002224 return;
2225
2226tx_error:
Jakub Kicinski56de8592017-02-24 11:43:36 -08002227 rcu_read_unlock();
pravin shelar655c3de2016-11-13 20:43:55 -08002228 if (err == -ELOOP)
2229 dev->stats.collisions++;
2230 else if (err == -ENETUNREACH)
2231 dev->stats.tx_carrier_errors++;
pravin shelarc46b7892016-11-13 20:43:54 -08002232 dst_release(ndst);
2233 dev->stats.tx_errors++;
2234 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002235}
2236
David Stevens66817122013-03-15 04:35:51 +00002237/* Transmit local packets over Vxlan
2238 *
2239 * Outer IP header inherits ECN and DF from inner header.
2240 * Outer UDP destination is the VXLAN assigned port.
2241 * source port is based on hash of flow
2242 */
2243static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2244{
2245 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002246 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002247 struct ethhdr *eth;
2248 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002249 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002250 struct vxlan_fdb *f;
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002251 __be32 vni = 0;
David Stevens66817122013-03-15 04:35:51 +00002252
Jiri Benc61adedf2015-08-20 13:56:25 +02002253 info = skb_tunnel_info(skb);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002254
David Stevens66817122013-03-15 04:35:51 +00002255 skb_reset_mac_header(skb);
David Stevens66817122013-03-15 04:35:51 +00002256
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002257 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002258 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2259 info->mode & IP_TUNNEL_INFO_TX) {
2260 vni = tunnel_id_to_key32(info->key.tun_id);
2261 } else {
2262 if (info && info->mode & IP_TUNNEL_INFO_TX)
2263 vxlan_xmit_one(skb, dev, vni, NULL, false);
2264 else
2265 kfree_skb(skb);
2266 return NETDEV_TX_OK;
2267 }
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002268 }
2269
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002270 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002271 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002272 if (ntohs(eth->h_proto) == ETH_P_ARP)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002273 return arp_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002274#if IS_ENABLED(CONFIG_IPV6)
Vincent Bernatf1fb08f2017-04-02 11:00:06 +02002275 else if (ntohs(eth->h_proto) == ETH_P_IPV6) {
2276 struct ipv6hdr *hdr, _hdr;
2277 if ((hdr = skb_header_pointer(skb,
2278 skb_network_offset(skb),
2279 sizeof(_hdr), &_hdr)) &&
2280 hdr->nexthdr == IPPROTO_ICMPV6)
2281 return neigh_reduce(dev, skb, vni);
Cong Wangf564f452013-08-31 13:44:36 +08002282 }
2283#endif
2284 }
David Stevens66817122013-03-15 04:35:51 +00002285
Jiri Benc47e5d1b2016-04-05 14:47:11 +02002286 eth = eth_hdr(skb);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002287 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002288 did_rsc = false;
2289
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002290 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002291 (ntohs(eth->h_proto) == ETH_P_IP ||
2292 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002293 did_rsc = route_shortcircuit(dev, skb);
2294 if (did_rsc)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002295 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
David Stevensae884082013-04-19 00:36:26 +00002296 }
2297
David Stevens66817122013-03-15 04:35:51 +00002298 if (f == NULL) {
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002299 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002300 if (f == NULL) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002301 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002302 !is_multicast_ether_addr(eth->h_dest))
2303 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002304
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002305 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002306 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002307 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002308 }
David Stevens66817122013-03-15 04:35:51 +00002309 }
2310
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002311 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2312 struct sk_buff *skb1;
2313
Eric Dumazet8f646c92014-01-06 09:54:31 -08002314 if (!fdst) {
2315 fdst = rdst;
2316 continue;
2317 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002318 skb1 = skb_clone(skb, GFP_ATOMIC);
2319 if (skb1)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002320 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002321 }
2322
Eric Dumazet8f646c92014-01-06 09:54:31 -08002323 if (fdst)
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002324 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
Eric Dumazet8f646c92014-01-06 09:54:31 -08002325 else
2326 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002327 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002328}
2329
stephen hemmingerd3428942012-10-01 12:32:35 +00002330/* Walk the forwarding table and purge stale entries */
2331static void vxlan_cleanup(unsigned long arg)
2332{
2333 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2334 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2335 unsigned int h;
2336
2337 if (!netif_running(vxlan->dev))
2338 return;
2339
stephen hemmingerd3428942012-10-01 12:32:35 +00002340 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2341 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002342
2343 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002344 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2345 struct vxlan_fdb *f
2346 = container_of(p, struct vxlan_fdb, hlist);
2347 unsigned long timeout;
2348
Balakrishnan Ramanefb5f682017-01-23 20:44:33 -08002349 if (f->state & (NUD_PERMANENT | NUD_NOARP))
stephen hemmingerd3428942012-10-01 12:32:35 +00002350 continue;
2351
Roopa Prabhudef499c2017-03-27 15:46:41 -07002352 if (f->flags & NTF_EXT_LEARNED)
2353 continue;
2354
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002355 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002356 if (time_before_eq(timeout, jiffies)) {
2357 netdev_dbg(vxlan->dev,
2358 "garbage collect %pM\n",
2359 f->eth_addr);
2360 f->state = NUD_STALE;
2361 vxlan_fdb_destroy(vxlan, f);
2362 } else if (time_before(timeout, next_timer))
2363 next_timer = timeout;
2364 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002365 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002366 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002367
2368 mod_timer(&vxlan->age_timer, next_timer);
2369}
2370
Mark Blocha53cb292017-06-02 03:24:08 +03002371static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2372{
2373 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2374
2375 spin_lock(&vn->sock_lock);
2376 hlist_del_init_rcu(&vxlan->hlist);
2377 spin_unlock(&vn->sock_lock);
2378}
2379
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002380static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2381{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002382 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Jiri Benc54bfd872016-02-16 21:58:58 +01002383 __be32 vni = vxlan->default_dst.remote_vni;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002384
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002385 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002386 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002387 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002388}
2389
stephen hemmingerd3428942012-10-01 12:32:35 +00002390/* Setup stats when device is created */
2391static int vxlan_init(struct net_device *dev)
2392{
WANG Cong1c213bd2014-02-13 11:46:28 -08002393 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002394 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002395 return -ENOMEM;
2396
2397 return 0;
2398}
2399
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002400static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002401{
2402 struct vxlan_fdb *f;
2403
2404 spin_lock_bh(&vxlan->hash_lock);
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002405 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002406 if (f)
2407 vxlan_fdb_destroy(vxlan, f);
2408 spin_unlock_bh(&vxlan->hash_lock);
2409}
2410
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002411static void vxlan_uninit(struct net_device *dev)
2412{
2413 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002414
Roopa Prabhu3ad7a4b2017-01-31 22:59:52 -08002415 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002416
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002417 free_percpu(dev->tstats);
2418}
2419
stephen hemmingerd3428942012-10-01 12:32:35 +00002420/* Start ageing timer and join group when device is brought up */
2421static int vxlan_open(struct net_device *dev)
2422{
2423 struct vxlan_dev *vxlan = netdev_priv(dev);
Jiri Benc205f3562015-09-24 13:50:01 +02002424 int ret;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002425
Jiri Benc205f3562015-09-24 13:50:01 +02002426 ret = vxlan_sock_add(vxlan);
2427 if (ret < 0)
2428 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002429
Gao feng79d4a942013-12-10 16:37:32 +08002430 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002431 ret = vxlan_igmp_join(vxlan);
Marcelo Ricardo Leitnerbef00572015-08-25 20:22:35 -03002432 if (ret == -EADDRINUSE)
2433 ret = 0;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002434 if (ret) {
Jiri Benc205f3562015-09-24 13:50:01 +02002435 vxlan_sock_release(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002436 return ret;
2437 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002438 }
2439
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002440 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002441 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2442
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002443 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002444}
2445
2446/* Purge the forwarding table */
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002447static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
stephen hemmingerd3428942012-10-01 12:32:35 +00002448{
Cong Wang31fec5a2013-05-27 22:35:52 +00002449 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002450
2451 spin_lock_bh(&vxlan->hash_lock);
2452 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2453 struct hlist_node *p, *n;
2454 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2455 struct vxlan_fdb *f
2456 = container_of(p, struct vxlan_fdb, hlist);
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002457 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2458 continue;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002459 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2460 if (!is_zero_ether_addr(f->eth_addr))
2461 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002462 }
2463 }
2464 spin_unlock_bh(&vxlan->hash_lock);
2465}
2466
2467/* Cleanup timer and forwarding table on shutdown */
2468static int vxlan_stop(struct net_device *dev)
2469{
2470 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002471 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002472 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002473
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002474 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002475 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002476 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002477
2478 del_timer_sync(&vxlan->age_timer);
2479
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08002480 vxlan_flush(vxlan, false);
Jiri Benc205f3562015-09-24 13:50:01 +02002481 vxlan_sock_release(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002482
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002483 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002484}
2485
stephen hemmingerd3428942012-10-01 12:32:35 +00002486/* Stub, nothing needs to be done. */
2487static void vxlan_set_multicast_list(struct net_device *dev)
2488{
2489}
2490
Daniel Borkmann345010b2013-12-18 00:21:08 +01002491static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2492{
2493 struct vxlan_dev *vxlan = netdev_priv(dev);
2494 struct vxlan_rdst *dst = &vxlan->default_dst;
David Wragg72564b52016-02-10 00:05:55 +00002495 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2496 dst->remote_ifindex);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002497 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
Jarod Wilson91572082016-10-20 13:55:20 -04002498
2499 /* This check is different than dev->max_mtu, because it looks at
2500 * the lowerdev->mtu, rather than the static dev->max_mtu
2501 */
2502 if (lowerdev) {
2503 int max_mtu = lowerdev->mtu -
2504 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2505 if (new_mtu > max_mtu)
2506 return -EINVAL;
2507 }
2508
2509 dev->mtu = new_mtu;
2510 return 0;
Daniel Borkmann345010b2013-12-18 00:21:08 +01002511}
2512
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002513static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2514{
2515 struct vxlan_dev *vxlan = netdev_priv(dev);
2516 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2517 __be16 sport, dport;
2518
2519 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2520 vxlan->cfg.port_max, true);
2521 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2522
Jiri Benc239e9442015-12-07 13:04:31 +01002523 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002524 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002525 struct rtable *rt;
2526
pravin shelar655c3de2016-11-13 20:43:55 -08002527 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
Jiri Benc1a8496b2016-02-02 18:09:14 +01002528 info->key.u.ipv4.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002529 &info->key.u.ipv4.src, dport, sport,
2530 &info->dst_cache, info);
Jiri Benc1a8496b2016-02-02 18:09:14 +01002531 if (IS_ERR(rt))
2532 return PTR_ERR(rt);
2533 ip_rt_put(rt);
Jiri Benc239e9442015-12-07 13:04:31 +01002534 } else {
2535#if IS_ENABLED(CONFIG_IPV6)
pravin shelar03dc52a2016-11-13 20:43:53 -08002536 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
Jiri Benc239e9442015-12-07 13:04:31 +01002537 struct dst_entry *ndst;
2538
pravin shelar655c3de2016-11-13 20:43:55 -08002539 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002540 info->key.label, &info->key.u.ipv6.dst,
Paolo Abeni22f07082017-02-17 19:14:27 +01002541 &info->key.u.ipv6.src, dport, sport,
2542 &info->dst_cache, info);
Jiri Benc239e9442015-12-07 13:04:31 +01002543 if (IS_ERR(ndst))
2544 return PTR_ERR(ndst);
2545 dst_release(ndst);
Jiri Benc239e9442015-12-07 13:04:31 +01002546#else /* !CONFIG_IPV6 */
2547 return -EPFNOSUPPORT;
2548#endif
2549 }
Jiri Benc1a8496b2016-02-02 18:09:14 +01002550 info->key.tp_src = sport;
2551 info->key.tp_dst = dport;
Jiri Benc239e9442015-12-07 13:04:31 +01002552 return 0;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002553}
2554
Jiri Benc0c867c92016-04-05 14:47:10 +02002555static const struct net_device_ops vxlan_netdev_ether_ops = {
stephen hemmingerd3428942012-10-01 12:32:35 +00002556 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002557 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002558 .ndo_open = vxlan_open,
2559 .ndo_stop = vxlan_stop,
2560 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002561 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002562 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002563 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002564 .ndo_validate_addr = eth_validate_addr,
2565 .ndo_set_mac_address = eth_mac_addr,
2566 .ndo_fdb_add = vxlan_fdb_add,
2567 .ndo_fdb_del = vxlan_fdb_delete,
2568 .ndo_fdb_dump = vxlan_fdb_dump,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002569 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
stephen hemmingerd3428942012-10-01 12:32:35 +00002570};
2571
Jiri Bence1e53142016-04-05 14:47:13 +02002572static const struct net_device_ops vxlan_netdev_raw_ops = {
2573 .ndo_init = vxlan_init,
2574 .ndo_uninit = vxlan_uninit,
2575 .ndo_open = vxlan_open,
2576 .ndo_stop = vxlan_stop,
2577 .ndo_start_xmit = vxlan_xmit,
2578 .ndo_get_stats64 = ip_tunnel_get_stats64,
2579 .ndo_change_mtu = vxlan_change_mtu,
2580 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2581};
2582
stephen hemmingerd3428942012-10-01 12:32:35 +00002583/* Info for udev, that this is a virtual tunnel endpoint */
2584static struct device_type vxlan_type = {
2585 .name = "vxlan",
2586};
2587
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002588/* Calls the ndo_udp_tunnel_add of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002589 * supply the listening VXLAN udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02002590 * to implement the ndo_udp_tunnel_add.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002591 */
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02002592static void vxlan_push_rx_ports(struct net_device *dev)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002593{
2594 struct vxlan_sock *vs;
2595 struct net *net = dev_net(dev);
2596 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002597 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002598
2599 spin_lock(&vn->sock_lock);
2600 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Alexander Duycke7b3db52016-06-16 12:20:52 -07002601 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2602 udp_tunnel_push_rx_port(dev, vs->sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002603 (vs->flags & VXLAN_F_GPE) ?
2604 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002605 UDP_TUNNEL_TYPE_VXLAN);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002606 }
2607 spin_unlock(&vn->sock_lock);
2608}
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002609
stephen hemmingerd3428942012-10-01 12:32:35 +00002610/* Initialize the device structure. */
2611static void vxlan_setup(struct net_device *dev)
2612{
2613 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002614 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002615
Jiri Benc65226ef2016-04-28 16:36:30 +02002616 eth_hw_addr_random(dev);
2617 ether_setup(dev);
2618
David S. Millercf124db2017-05-08 12:52:56 -04002619 dev->needs_free_netdev = true;
stephen hemmingerd3428942012-10-01 12:32:35 +00002620 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2621
stephen hemmingerd3428942012-10-01 12:32:35 +00002622 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002623 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002624 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002625 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002626
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002627 dev->vlan_features = dev->features;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002628 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002629 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Eric Dumazet02875872014-10-05 18:38:35 -07002630 netif_keep_dst(dev);
Jiri Benc0c867c92016-04-05 14:47:10 +02002631 dev->priv_flags |= IFF_NO_QUEUE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002632
Matthias Schiffera9853432017-06-19 10:03:55 +02002633 /* MTU range: 68 - 65535 */
2634 dev->min_mtu = ETH_MIN_MTU;
2635 dev->max_mtu = ETH_MAX_MTU;
2636
stephen hemminger553675f2013-05-16 11:35:20 +00002637 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002638 spin_lock_init(&vxlan->hash_lock);
2639
2640 init_timer_deferrable(&vxlan->age_timer);
2641 vxlan->age_timer.function = vxlan_cleanup;
2642 vxlan->age_timer.data = (unsigned long) vxlan;
2643
2644 vxlan->dev = dev;
Matthias Schiffera9853432017-06-19 10:03:55 +02002645 vxlan->net = dev_net(dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00002646
Tom Herbert58ce31c2015-08-19 17:07:33 -07002647 gro_cells_init(&vxlan->gro_cells, dev);
2648
stephen hemmingerd3428942012-10-01 12:32:35 +00002649 for (h = 0; h < FDB_HASH_SIZE; ++h)
2650 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2651}
2652
Jiri Benc0c867c92016-04-05 14:47:10 +02002653static void vxlan_ether_setup(struct net_device *dev)
2654{
Jiri Benc0c867c92016-04-05 14:47:10 +02002655 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2656 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2657 dev->netdev_ops = &vxlan_netdev_ether_ops;
2658}
2659
Jiri Bence1e53142016-04-05 14:47:13 +02002660static void vxlan_raw_setup(struct net_device *dev)
2661{
Jiri Benc65226ef2016-04-28 16:36:30 +02002662 dev->header_ops = NULL;
Jiri Bence1e53142016-04-05 14:47:13 +02002663 dev->type = ARPHRD_NONE;
2664 dev->hard_header_len = 0;
2665 dev->addr_len = 0;
Jiri Bence1e53142016-04-05 14:47:13 +02002666 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2667 dev->netdev_ops = &vxlan_netdev_raw_ops;
2668}
2669
stephen hemmingerd3428942012-10-01 12:32:35 +00002670static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2671 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002672 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002673 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002674 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2675 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002676 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002677 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2678 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01002679 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002680 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2681 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2682 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002683 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002684 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2685 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2686 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2687 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Alexei Starovoitovf8a9b1b2015-07-30 20:10:22 -07002688 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002689 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002690 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2691 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2692 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002693 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2694 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002695 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Jiri Bence1e53142016-04-05 14:47:13 +02002696 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002697 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002698};
2699
2700static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2701{
2702 if (tb[IFLA_ADDRESS]) {
2703 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2704 pr_debug("invalid link address (not ethernet)\n");
2705 return -EINVAL;
2706 }
2707
2708 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2709 pr_debug("invalid all zero ethernet address\n");
2710 return -EADDRNOTAVAIL;
2711 }
2712 }
2713
Matthias Schiffera9853432017-06-19 10:03:55 +02002714 if (tb[IFLA_MTU]) {
2715 u32 mtu = nla_get_u32(data[IFLA_MTU]);
2716
2717 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU)
2718 return -EINVAL;
2719 }
2720
stephen hemmingerd3428942012-10-01 12:32:35 +00002721 if (!data)
2722 return -EINVAL;
2723
2724 if (data[IFLA_VXLAN_ID]) {
Matthias Schiffera9853432017-06-19 10:03:55 +02002725 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2726
Matthias Schiffer4e37d692017-02-23 17:19:41 +01002727 if (id >= VXLAN_N_VID)
stephen hemmingerd3428942012-10-01 12:32:35 +00002728 return -ERANGE;
2729 }
2730
stephen hemminger05f47d62012-10-09 20:35:50 +00002731 if (data[IFLA_VXLAN_PORT_RANGE]) {
2732 const struct ifla_vxlan_port_range *p
2733 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2734
2735 if (ntohs(p->high) < ntohs(p->low)) {
2736 pr_debug("port range %u .. %u not valid\n",
2737 ntohs(p->low), ntohs(p->high));
2738 return -EINVAL;
2739 }
2740 }
2741
stephen hemmingerd3428942012-10-01 12:32:35 +00002742 return 0;
2743}
2744
Yan Burman1b13c972013-01-29 23:43:07 +00002745static void vxlan_get_drvinfo(struct net_device *netdev,
2746 struct ethtool_drvinfo *drvinfo)
2747{
2748 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2749 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2750}
2751
2752static const struct ethtool_ops vxlan_ethtool_ops = {
2753 .get_drvinfo = vxlan_get_drvinfo,
2754 .get_link = ethtool_op_get_link,
2755};
2756
Tom Herbert3ee64f32014-07-13 19:49:42 -07002757static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2758 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002759{
Cong Wange4c7ed42013-08-31 13:44:33 +08002760 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002761 struct udp_port_cfg udp_conf;
2762 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002763
Tom Herbert3ee64f32014-07-13 19:49:42 -07002764 memset(&udp_conf, 0, sizeof(udp_conf));
2765
2766 if (ipv6) {
2767 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002768 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002769 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Jiri Benca43a9ef2015-08-28 20:48:22 +02002770 udp_conf.ipv6_v6only = 1;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002771 } else {
2772 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002773 }
2774
Tom Herbert3ee64f32014-07-13 19:49:42 -07002775 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002776
Tom Herbert3ee64f32014-07-13 19:49:42 -07002777 /* Open UDP socket */
2778 err = udp_sock_create(net, &udp_conf, &sock);
2779 if (err < 0)
2780 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002781
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002782 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002783}
2784
2785/* Create new listen socket if needed */
Jiri Bencb1be00a2015-09-24 13:50:02 +02002786static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2787 __be16 port, u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002788{
2789 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2790 struct vxlan_sock *vs;
2791 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002792 unsigned int h;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002793 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002794
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002795 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002796 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002797 return ERR_PTR(-ENOMEM);
2798
2799 for (h = 0; h < VNI_HASH_SIZE; ++h)
2800 INIT_HLIST_HEAD(&vs->vni_list[h]);
2801
Tom Herbert3ee64f32014-07-13 19:49:42 -07002802 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002803 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002804 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002805 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002806 }
2807
Cong Wange4c7ed42013-08-31 13:44:33 +08002808 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002809 atomic_set(&vs->refcnt, 1);
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002810 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002811
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002812 spin_lock(&vn->sock_lock);
2813 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Alexander Duycke7b3db52016-06-16 12:20:52 -07002814 udp_tunnel_notify_add_rx_port(sock,
Alexander Duyckb9adcd692016-06-16 12:23:19 -07002815 (vs->flags & VXLAN_F_GPE) ?
2816 UDP_TUNNEL_TYPE_VXLAN_GPE :
Alexander Duycke7b3db52016-06-16 12:20:52 -07002817 UDP_TUNNEL_TYPE_VXLAN);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002818 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002819
2820 /* Mark socket as an encapsulation socket. */
Tom Herbert5602c482016-04-05 08:22:53 -07002821 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Andy Zhouacbf74a2014-09-16 17:31:18 -07002822 tunnel_cfg.sk_user_data = vs;
2823 tunnel_cfg.encap_type = 1;
Jiri Bencf2d1968ec2016-02-23 18:02:58 +01002824 tunnel_cfg.encap_rcv = vxlan_rcv;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002825 tunnel_cfg.encap_destroy = NULL;
Tom Herbert5602c482016-04-05 08:22:53 -07002826 tunnel_cfg.gro_receive = vxlan_gro_receive;
2827 tunnel_cfg.gro_complete = vxlan_gro_complete;
Andy Zhouacbf74a2014-09-16 17:31:18 -07002828
2829 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002830
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002831 return vs;
2832}
stephen hemminger553675f2013-05-16 11:35:20 +00002833
Jiri Bencb1be00a2015-09-24 13:50:02 +02002834static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002835{
Jiri Benc205f3562015-09-24 13:50:01 +02002836 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2837 struct vxlan_sock *vs = NULL;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002838
Jiri Benc205f3562015-09-24 13:50:01 +02002839 if (!vxlan->cfg.no_share) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002840 spin_lock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002841 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002842 vxlan->cfg.dst_port, vxlan->cfg.flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002843 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002844 spin_unlock(&vn->sock_lock);
Jiri Benc205f3562015-09-24 13:50:01 +02002845 return -EBUSY;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002846 }
2847 spin_unlock(&vn->sock_lock);
2848 }
Jiri Benc205f3562015-09-24 13:50:01 +02002849 if (!vs)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002850 vs = vxlan_socket_create(vxlan->net, ipv6,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002851 vxlan->cfg.dst_port, vxlan->cfg.flags);
Jiri Benc205f3562015-09-24 13:50:01 +02002852 if (IS_ERR(vs))
2853 return PTR_ERR(vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002854#if IS_ENABLED(CONFIG_IPV6)
2855 if (ipv6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002856 rcu_assign_pointer(vxlan->vn6_sock, vs);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002857 else
2858#endif
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002859 rcu_assign_pointer(vxlan->vn4_sock, vs);
Jiri Benc205f3562015-09-24 13:50:01 +02002860 vxlan_vs_add_dev(vs, vxlan);
2861 return 0;
stephen hemminger553675f2013-05-16 11:35:20 +00002862}
2863
Jiri Bencb1be00a2015-09-24 13:50:02 +02002864static int vxlan_sock_add(struct vxlan_dev *vxlan)
2865{
Matthias Schifferdc5321d2017-06-19 10:03:56 +02002866 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
2867 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
Jiri Bencd074bf92017-04-27 21:24:35 +02002868 bool ipv4 = !ipv6 || metadata;
Jiri Bencb1be00a2015-09-24 13:50:02 +02002869 int ret = 0;
2870
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002871 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
Jiri Bencb1be00a2015-09-24 13:50:02 +02002872#if IS_ENABLED(CONFIG_IPV6)
pravin shelarc6fcc4f2016-10-28 09:59:15 -07002873 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
Jiri Bencd074bf92017-04-27 21:24:35 +02002874 if (ipv6) {
Jiri Bencb1be00a2015-09-24 13:50:02 +02002875 ret = __vxlan_sock_add(vxlan, true);
Jiri Bencd074bf92017-04-27 21:24:35 +02002876 if (ret < 0 && ret != -EAFNOSUPPORT)
2877 ipv4 = false;
2878 }
Jiri Bencb1be00a2015-09-24 13:50:02 +02002879#endif
Jiri Bencd074bf92017-04-27 21:24:35 +02002880 if (ipv4)
Jiri Bencb1be00a2015-09-24 13:50:02 +02002881 ret = __vxlan_sock_add(vxlan, false);
2882 if (ret < 0)
2883 vxlan_sock_release(vxlan);
2884 return ret;
2885}
2886
Matthias Schiffera9853432017-06-19 10:03:55 +02002887static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
2888 struct net_device **lower,
2889 struct vxlan_dev *old)
stephen hemmingerd3428942012-10-01 12:32:35 +00002890{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002891 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
Matthias Schiffera9853432017-06-19 10:03:55 +02002892 struct vxlan_dev *tmp;
2893 bool use_ipv6 = false;
2894
2895 if (conf->flags & VXLAN_F_GPE) {
2896 /* For now, allow GPE only together with
2897 * COLLECT_METADATA. This can be relaxed later; in such
2898 * case, the other side of the PtP link will have to be
2899 * provided.
2900 */
2901 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2902 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2903 return -EINVAL;
2904 }
2905 }
2906
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002907 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
2908 /* Unless IPv6 is explicitly requested, assume IPv4 */
Matthias Schiffera9853432017-06-19 10:03:55 +02002909 conf->remote_ip.sa.sa_family = AF_INET;
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002910 conf->saddr.sa.sa_family = AF_INET;
2911 } else if (!conf->remote_ip.sa.sa_family) {
2912 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
2913 } else if (!conf->saddr.sa.sa_family) {
2914 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
2915 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002916
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002917 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family)
2918 return -EINVAL;
2919
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02002920 if (vxlan_addr_multicast(&conf->saddr))
2921 return -EINVAL;
2922
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002923 if (conf->saddr.sa.sa_family == AF_INET6) {
Matthias Schiffera9853432017-06-19 10:03:55 +02002924 if (!IS_ENABLED(CONFIG_IPV6))
2925 return -EPFNOSUPPORT;
2926 use_ipv6 = true;
2927 conf->flags |= VXLAN_F_IPV6;
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02002928
2929 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2930 int local_type =
2931 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
2932 int remote_type =
2933 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
2934
2935 if (local_type & IPV6_ADDR_LINKLOCAL) {
2936 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
2937 (remote_type != IPV6_ADDR_ANY))
2938 return -EINVAL;
2939
2940 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
2941 } else {
2942 if (remote_type ==
2943 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL))
2944 return -EINVAL;
2945
2946 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
2947 }
2948 }
Matthias Schiffera9853432017-06-19 10:03:55 +02002949 }
2950
2951 if (conf->label && !use_ipv6)
2952 return -EINVAL;
2953
2954 if (conf->remote_ifindex) {
2955 struct net_device *lowerdev;
2956
2957 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
2958 if (!lowerdev)
2959 return -ENODEV;
2960
2961#if IS_ENABLED(CONFIG_IPV6)
2962 if (use_ipv6) {
2963 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2964 if (idev && idev->cnf.disable_ipv6)
2965 return -EPERM;
2966 }
2967#endif
2968
2969 *lower = lowerdev;
2970 } else {
2971 if (vxlan_addr_multicast(&conf->remote_ip))
2972 return -EINVAL;
2973
Matthias Schiffer0f22a3c2017-06-19 10:03:58 +02002974#if IS_ENABLED(CONFIG_IPV6)
2975 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL)
2976 return -EINVAL;
2977#endif
2978
Matthias Schiffera9853432017-06-19 10:03:55 +02002979 *lower = NULL;
2980 }
2981
2982 if (!conf->dst_port) {
2983 if (conf->flags & VXLAN_F_GPE)
2984 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
2985 else
2986 conf->dst_port = htons(vxlan_port);
2987 }
2988
2989 if (!conf->age_interval)
2990 conf->age_interval = FDB_AGE_DEFAULT;
2991
2992 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2993 if (tmp == old)
2994 continue;
2995
2996 if (tmp->cfg.vni == conf->vni &&
Matthias Schiffera9853432017-06-19 10:03:55 +02002997 tmp->cfg.dst_port == conf->dst_port &&
Matthias Schifferce44a4a2017-06-19 10:03:57 +02002998 (tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) ==
2999 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
Matthias Schiffera9853432017-06-19 10:03:55 +02003000 return -EEXIST;
3001 }
3002
3003 return 0;
3004}
3005
3006static void vxlan_config_apply(struct net_device *dev,
3007 struct vxlan_config *conf,
3008 struct net_device *lowerdev, bool changelink)
3009{
3010 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003011 struct vxlan_rdst *dst = &vxlan->default_dst;
Jiri Bencb1be00a2015-09-24 13:50:02 +02003012 unsigned short needed_headroom = ETH_HLEN;
Matthias Schiffera9853432017-06-19 10:03:55 +02003013 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3014 int max_mtu = ETH_MAX_MTU;
stephen hemmingerd3428942012-10-01 12:32:35 +00003015
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003016 if (!changelink) {
Matthias Schiffera9853432017-06-19 10:03:55 +02003017 if (conf->flags & VXLAN_F_GPE)
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003018 vxlan_raw_setup(dev);
Matthias Schiffera9853432017-06-19 10:03:55 +02003019 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003020 vxlan_ether_setup(dev);
Jiri Bence1e53142016-04-05 14:47:13 +02003021
Matthias Schiffera9853432017-06-19 10:03:55 +02003022 if (conf->mtu)
3023 dev->mtu = conf->mtu;
Jiri Bence1e53142016-04-05 14:47:13 +02003024 }
Jiri Benc0c867c92016-04-05 14:47:10 +02003025
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003026 dst->remote_vni = conf->vni;
3027
3028 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00003029
Matthias Schiffera9853432017-06-19 10:03:55 +02003030 if (lowerdev) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003031 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00003032
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003033 dev->gso_max_size = lowerdev->gso_max_size;
3034 dev->gso_max_segs = lowerdev->gso_max_segs;
Matthias Schiffera9853432017-06-19 10:03:55 +02003035
3036 needed_headroom = lowerdev->hard_header_len;
3037
3038 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3039 VXLAN_HEADROOM);
Felix Manlunasd6acfeb2017-03-29 17:56:43 -07003040 }
3041
Matthias Schiffera9853432017-06-19 10:03:55 +02003042 if (dev->mtu > max_mtu)
3043 dev->mtu = max_mtu;
David Wragg7e059152016-02-10 00:05:58 +00003044
Jiri Bencb1be00a2015-09-24 13:50:02 +02003045 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3046 needed_headroom += VXLAN6_HEADROOM;
3047 else
3048 needed_headroom += VXLAN_HEADROOM;
3049 dev->needed_headroom = needed_headroom;
3050
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003051 memcpy(&vxlan->cfg, conf, sizeof(*conf));
Matthias Schiffera9853432017-06-19 10:03:55 +02003052}
stephen hemmingerd3428942012-10-01 12:32:35 +00003053
Matthias Schiffera9853432017-06-19 10:03:55 +02003054static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3055 struct vxlan_config *conf,
3056 bool changelink)
3057{
3058 struct vxlan_dev *vxlan = netdev_priv(dev);
3059 struct net_device *lowerdev;
3060 int ret;
Vincent Bernatafb97182012-10-30 10:27:16 +00003061
Matthias Schiffera9853432017-06-19 10:03:55 +02003062 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan);
3063 if (ret)
3064 return ret;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003065
Matthias Schiffera9853432017-06-19 10:03:55 +02003066 vxlan_config_apply(dev, conf, lowerdev, changelink);
stephen hemminger553675f2013-05-16 11:35:20 +00003067
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003068 return 0;
3069}
3070
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003071static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3072 struct vxlan_config *conf)
3073{
3074 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3075 struct vxlan_dev *vxlan = netdev_priv(dev);
3076 int err;
3077
3078 err = vxlan_dev_configure(net, dev, conf, false);
3079 if (err)
3080 return err;
3081
3082 dev->ethtool_ops = &vxlan_ethtool_ops;
3083
3084 /* create an fdb entry for a valid default destination */
3085 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
3086 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3087 &vxlan->default_dst.remote_ip,
3088 NUD_REACHABLE | NUD_PERMANENT,
3089 NLM_F_EXCL | NLM_F_CREATE,
3090 vxlan->cfg.dst_port,
3091 vxlan->default_dst.remote_vni,
3092 vxlan->default_dst.remote_vni,
3093 vxlan->default_dst.remote_ifindex,
3094 NTF_SELF);
3095 if (err)
3096 return err;
3097 }
3098
3099 err = register_netdevice(dev);
3100 if (err) {
3101 vxlan_fdb_delete_default(vxlan, vxlan->default_dst.remote_vni);
3102 return err;
3103 }
3104
3105 list_add(&vxlan->next, &vn->vxlan_list);
3106 return 0;
3107}
3108
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003109static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3110 struct net_device *dev, struct vxlan_config *conf,
3111 bool changelink)
3112{
3113 struct vxlan_dev *vxlan = netdev_priv(dev);
3114
3115 memset(conf, 0, sizeof(*conf));
3116
3117 /* if changelink operation, start with old existing cfg */
3118 if (changelink)
3119 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3120
3121 if (data[IFLA_VXLAN_ID]) {
3122 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3123
3124 if (changelink && (vni != conf->vni))
3125 return -EOPNOTSUPP;
3126 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3127 }
3128
3129 if (data[IFLA_VXLAN_GROUP]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003130 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET))
3131 return -EOPNOTSUPP;
3132
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003133 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003134 conf->remote_ip.sa.sa_family = AF_INET;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003135 } else if (data[IFLA_VXLAN_GROUP6]) {
3136 if (!IS_ENABLED(CONFIG_IPV6))
3137 return -EPFNOSUPPORT;
3138
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003139 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6))
3140 return -EOPNOTSUPP;
3141
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003142 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3143 conf->remote_ip.sa.sa_family = AF_INET6;
3144 }
3145
3146 if (data[IFLA_VXLAN_LOCAL]) {
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003147 if (changelink && (conf->saddr.sa.sa_family != AF_INET))
3148 return -EOPNOTSUPP;
3149
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003150 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3151 conf->saddr.sa.sa_family = AF_INET;
3152 } else if (data[IFLA_VXLAN_LOCAL6]) {
3153 if (!IS_ENABLED(CONFIG_IPV6))
3154 return -EPFNOSUPPORT;
3155
Matthias Schifferce44a4a2017-06-19 10:03:57 +02003156 if (changelink && (conf->saddr.sa.sa_family != AF_INET6))
3157 return -EOPNOTSUPP;
3158
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003159 /* TODO: respect scope id */
3160 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3161 conf->saddr.sa.sa_family = AF_INET6;
3162 }
3163
3164 if (data[IFLA_VXLAN_LINK])
3165 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3166
3167 if (data[IFLA_VXLAN_TOS])
3168 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3169
3170 if (data[IFLA_VXLAN_TTL])
3171 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3172
3173 if (data[IFLA_VXLAN_LABEL])
3174 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3175 IPV6_FLOWLABEL_MASK;
3176
3177 if (data[IFLA_VXLAN_LEARNING]) {
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003178 if (nla_get_u8(data[IFLA_VXLAN_LEARNING]))
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003179 conf->flags |= VXLAN_F_LEARN;
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003180 else
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003181 conf->flags &= ~VXLAN_F_LEARN;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003182 } else if (!changelink) {
3183 /* default to learn on a new device */
3184 conf->flags |= VXLAN_F_LEARN;
3185 }
3186
3187 if (data[IFLA_VXLAN_AGEING]) {
3188 if (changelink)
3189 return -EOPNOTSUPP;
3190 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3191 }
3192
3193 if (data[IFLA_VXLAN_PROXY]) {
3194 if (changelink)
3195 return -EOPNOTSUPP;
3196 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3197 conf->flags |= VXLAN_F_PROXY;
3198 }
3199
3200 if (data[IFLA_VXLAN_RSC]) {
3201 if (changelink)
3202 return -EOPNOTSUPP;
3203 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3204 conf->flags |= VXLAN_F_RSC;
3205 }
3206
3207 if (data[IFLA_VXLAN_L2MISS]) {
3208 if (changelink)
3209 return -EOPNOTSUPP;
3210 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3211 conf->flags |= VXLAN_F_L2MISS;
3212 }
3213
3214 if (data[IFLA_VXLAN_L3MISS]) {
3215 if (changelink)
3216 return -EOPNOTSUPP;
3217 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3218 conf->flags |= VXLAN_F_L3MISS;
3219 }
3220
3221 if (data[IFLA_VXLAN_LIMIT]) {
3222 if (changelink)
3223 return -EOPNOTSUPP;
3224 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3225 }
3226
3227 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3228 if (changelink)
3229 return -EOPNOTSUPP;
3230 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3231 conf->flags |= VXLAN_F_COLLECT_METADATA;
3232 }
3233
3234 if (data[IFLA_VXLAN_PORT_RANGE]) {
3235 if (!changelink) {
3236 const struct ifla_vxlan_port_range *p
3237 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3238 conf->port_min = ntohs(p->low);
3239 conf->port_max = ntohs(p->high);
3240 } else {
3241 return -EOPNOTSUPP;
3242 }
3243 }
3244
3245 if (data[IFLA_VXLAN_PORT]) {
3246 if (changelink)
3247 return -EOPNOTSUPP;
3248 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3249 }
3250
3251 if (data[IFLA_VXLAN_UDP_CSUM]) {
3252 if (changelink)
3253 return -EOPNOTSUPP;
3254 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3255 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3256 }
3257
3258 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3259 if (changelink)
3260 return -EOPNOTSUPP;
3261 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3262 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3263 }
3264
3265 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3266 if (changelink)
3267 return -EOPNOTSUPP;
3268 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3269 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3270 }
3271
3272 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3273 if (changelink)
3274 return -EOPNOTSUPP;
3275 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3276 conf->flags |= VXLAN_F_REMCSUM_TX;
3277 }
3278
3279 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3280 if (changelink)
3281 return -EOPNOTSUPP;
3282 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3283 conf->flags |= VXLAN_F_REMCSUM_RX;
3284 }
3285
3286 if (data[IFLA_VXLAN_GBP]) {
3287 if (changelink)
3288 return -EOPNOTSUPP;
3289 conf->flags |= VXLAN_F_GBP;
3290 }
3291
3292 if (data[IFLA_VXLAN_GPE]) {
3293 if (changelink)
3294 return -EOPNOTSUPP;
3295 conf->flags |= VXLAN_F_GPE;
3296 }
3297
3298 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3299 if (changelink)
3300 return -EOPNOTSUPP;
3301 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3302 }
3303
3304 if (tb[IFLA_MTU]) {
3305 if (changelink)
3306 return -EOPNOTSUPP;
3307 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3308 }
3309
3310 return 0;
3311}
3312
3313static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3314 struct nlattr *tb[], struct nlattr *data[])
3315{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003316 struct vxlan_config conf;
3317 int err;
3318
3319 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3320 if (err)
3321 return err;
3322
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003323 return __vxlan_dev_create(src_net, dev, &conf);
stephen hemmingerd3428942012-10-01 12:32:35 +00003324}
3325
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003326static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
3327 struct nlattr *data[])
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003328{
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003329 struct vxlan_dev *vxlan = netdev_priv(dev);
3330 struct vxlan_rdst *dst = &vxlan->default_dst;
3331 struct vxlan_rdst old_dst;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003332 struct vxlan_config conf;
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003333 int err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003334
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003335 err = vxlan_nl2conf(tb, data,
3336 dev, &conf, true);
3337 if (err)
3338 return err;
Jesse Grosse277de52015-10-16 16:36:00 -07003339
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003340 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003341
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003342 err = vxlan_dev_configure(vxlan->net, dev, &conf, true);
3343 if (err)
3344 return err;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003345
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003346 /* handle default dst entry */
3347 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3348 spin_lock_bh(&vxlan->hash_lock);
3349 if (!vxlan_addr_any(&old_dst.remote_ip))
3350 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3351 old_dst.remote_ip,
3352 vxlan->cfg.dst_port,
3353 old_dst.remote_vni,
3354 old_dst.remote_vni,
3355 old_dst.remote_ifindex, 0);
3356
3357 if (!vxlan_addr_any(&dst->remote_ip)) {
3358 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3359 &dst->remote_ip,
3360 NUD_REACHABLE | NUD_PERMANENT,
3361 NLM_F_CREATE | NLM_F_APPEND,
3362 vxlan->cfg.dst_port,
3363 dst->remote_vni,
3364 dst->remote_vni,
3365 dst->remote_ifindex,
3366 NTF_SELF);
3367 if (err) {
3368 spin_unlock_bh(&vxlan->hash_lock);
3369 return err;
3370 }
3371 }
3372 spin_unlock_bh(&vxlan->hash_lock);
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003373 }
3374
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003375 return 0;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003376}
3377
stephen hemmingerd3428942012-10-01 12:32:35 +00003378static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3379{
3380 struct vxlan_dev *vxlan = netdev_priv(dev);
3381
Roopa Prabhu8b3f9332017-01-23 20:44:32 -08003382 vxlan_flush(vxlan, true);
3383
Tom Herbert58ce31c2015-08-19 17:07:33 -07003384 gro_cells_destroy(&vxlan->gro_cells);
stephen hemminger553675f2013-05-16 11:35:20 +00003385 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00003386 unregister_netdevice_queue(dev, head);
3387}
3388
3389static size_t vxlan_get_size(const struct net_device *dev)
3390{
3391
3392 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08003393 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003394 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08003395 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00003396 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3397 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003398 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
stephen hemmingerd3428942012-10-01 12:32:35 +00003399 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00003400 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3401 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3402 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3403 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003404 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
stephen hemmingerd3428942012-10-01 12:32:35 +00003405 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3406 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00003407 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07003408 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3409 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3410 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3411 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08003412 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3413 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00003414 0;
3415}
3416
3417static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3418{
3419 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00003420 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00003421 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003422 .low = htons(vxlan->cfg.port_min),
3423 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00003424 };
stephen hemmingerd3428942012-10-01 12:32:35 +00003425
Jiri Benc54bfd872016-02-16 21:58:58 +01003426 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003427 goto nla_put_failure;
3428
Cong Wange4c7ed42013-08-31 13:44:33 +08003429 if (!vxlan_addr_any(&dst->remote_ip)) {
3430 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003431 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3432 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003433 goto nla_put_failure;
3434#if IS_ENABLED(CONFIG_IPV6)
3435 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003436 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3437 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003438 goto nla_put_failure;
3439#endif
3440 }
3441 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003442
Atzm Watanabec7995c42013-04-16 02:50:52 +00003443 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00003444 goto nla_put_failure;
3445
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003446 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3447 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02003448 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003449 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003450 goto nla_put_failure;
3451#if IS_ENABLED(CONFIG_IPV6)
3452 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02003453 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003454 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08003455 goto nla_put_failure;
3456#endif
3457 }
3458 }
stephen hemmingerd3428942012-10-01 12:32:35 +00003459
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003460 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3461 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
Daniel Borkmanne7f70af2016-03-09 03:00:03 +01003462 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003463 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003464 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003465 nla_put_u8(skb, IFLA_VXLAN_PROXY,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003466 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
3467 nla_put_u8(skb, IFLA_VXLAN_RSC,
3468 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003469 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003470 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
David Stevense4f67ad2012-11-20 02:50:14 +00003471 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003472 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -07003473 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003474 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02003475 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3476 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3477 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003478 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003479 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003480 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003481 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07003482 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003483 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003484 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003485 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
Tom Herbertdfd86452015-01-12 17:00:38 -08003486 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003487 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00003488 goto nla_put_failure;
3489
stephen hemminger05f47d62012-10-09 20:35:50 +00003490 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3491 goto nla_put_failure;
3492
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003493 if (vxlan->cfg.flags & VXLAN_F_GBP &&
Thomas Graf35114942015-01-15 03:53:55 +01003494 nla_put_flag(skb, IFLA_VXLAN_GBP))
3495 goto nla_put_failure;
3496
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003497 if (vxlan->cfg.flags & VXLAN_F_GPE &&
Jiri Bence1e53142016-04-05 14:47:13 +02003498 nla_put_flag(skb, IFLA_VXLAN_GPE))
3499 goto nla_put_failure;
3500
Matthias Schifferdc5321d2017-06-19 10:03:56 +02003501 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003502 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3503 goto nla_put_failure;
3504
stephen hemmingerd3428942012-10-01 12:32:35 +00003505 return 0;
3506
3507nla_put_failure:
3508 return -EMSGSIZE;
3509}
3510
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003511static struct net *vxlan_get_link_net(const struct net_device *dev)
3512{
3513 struct vxlan_dev *vxlan = netdev_priv(dev);
3514
3515 return vxlan->net;
3516}
3517
stephen hemmingerd3428942012-10-01 12:32:35 +00003518static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3519 .kind = "vxlan",
3520 .maxtype = IFLA_VXLAN_MAX,
3521 .policy = vxlan_policy,
3522 .priv_size = sizeof(struct vxlan_dev),
3523 .setup = vxlan_setup,
3524 .validate = vxlan_validate,
3525 .newlink = vxlan_newlink,
Roopa Prabhu8bcdc4f2017-02-20 08:29:19 -08003526 .changelink = vxlan_changelink,
stephen hemmingerd3428942012-10-01 12:32:35 +00003527 .dellink = vxlan_dellink,
3528 .get_size = vxlan_get_size,
3529 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003530 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003531};
3532
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003533struct net_device *vxlan_dev_create(struct net *net, const char *name,
3534 u8 name_assign_type,
3535 struct vxlan_config *conf)
3536{
3537 struct nlattr *tb[IFLA_MAX + 1];
3538 struct net_device *dev;
3539 int err;
3540
3541 memset(&tb, 0, sizeof(tb));
3542
3543 dev = rtnl_create_link(net, name, name_assign_type,
3544 &vxlan_link_ops, tb);
3545 if (IS_ERR(dev))
3546 return dev;
3547
Nicolas Dichtelc80498e2017-03-13 16:24:03 +01003548 err = __vxlan_dev_create(net, dev, conf);
Nicolas Dichtelcf5da332016-06-13 10:31:05 +02003549 if (err < 0) {
3550 free_netdev(dev);
3551 return ERR_PTR(err);
3552 }
3553
3554 err = rtnl_configure_link(dev, NULL);
3555 if (err < 0) {
3556 LIST_HEAD(list_kill);
3557
3558 vxlan_dellink(dev, &list_kill);
3559 unregister_netdevice_many(&list_kill);
3560 return ERR_PTR(err);
3561 }
3562
3563 return dev;
3564}
3565EXPORT_SYMBOL_GPL(vxlan_dev_create);
3566
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003567static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3568 struct net_device *dev)
3569{
3570 struct vxlan_dev *vxlan, *next;
3571 LIST_HEAD(list_kill);
3572
3573 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3574 struct vxlan_rdst *dst = &vxlan->default_dst;
3575
3576 /* In case we created vxlan device with carrier
3577 * and we loose the carrier due to module unload
3578 * we also need to remove vxlan device. In other
3579 * cases, it's not necessary and remote_ifindex
3580 * is 0 here, so no matches.
3581 */
3582 if (dst->remote_ifindex == dev->ifindex)
3583 vxlan_dellink(vxlan->dev, &list_kill);
3584 }
3585
3586 unregister_netdevice_many(&list_kill);
3587}
3588
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003589static int vxlan_netdevice_event(struct notifier_block *unused,
3590 unsigned long event, void *ptr)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003591{
3592 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003593 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003594
Daniel Borkmann783c1462014-01-22 21:07:53 +01003595 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003596 vxlan_handle_lowerdev_unregister(vn, dev);
Alexander Duyck7c46a642016-06-16 12:21:00 -07003597 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003598 vxlan_push_rx_ports(dev);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003599
3600 return NOTIFY_DONE;
3601}
3602
3603static struct notifier_block vxlan_notifier_block __read_mostly = {
Hannes Frederic Sowab7aade12016-04-18 21:19:47 +02003604 .notifier_call = vxlan_netdevice_event,
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003605};
3606
stephen hemmingerd3428942012-10-01 12:32:35 +00003607static __net_init int vxlan_init_net(struct net *net)
3608{
3609 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003610 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003611
stephen hemminger553675f2013-05-16 11:35:20 +00003612 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003613 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003614
stephen hemminger553675f2013-05-16 11:35:20 +00003615 for (h = 0; h < PORT_HASH_SIZE; ++h)
3616 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003617
3618 return 0;
3619}
3620
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003621static void __net_exit vxlan_exit_net(struct net *net)
3622{
3623 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3624 struct vxlan_dev *vxlan, *next;
3625 struct net_device *dev, *aux;
3626 LIST_HEAD(list);
3627
3628 rtnl_lock();
3629 for_each_netdev_safe(net, dev, aux)
3630 if (dev->rtnl_link_ops == &vxlan_link_ops)
3631 unregister_netdevice_queue(dev, &list);
3632
3633 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3634 /* If vxlan->dev is in the same netns, it has already been added
3635 * to the list by the previous loop.
3636 */
Tom Herbert58ce31c2015-08-19 17:07:33 -07003637 if (!net_eq(dev_net(vxlan->dev), net)) {
3638 gro_cells_destroy(&vxlan->gro_cells);
John W. Linville13c3ed62015-05-18 13:51:24 -04003639 unregister_netdevice_queue(vxlan->dev, &list);
Tom Herbert58ce31c2015-08-19 17:07:33 -07003640 }
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003641 }
3642
3643 unregister_netdevice_many(&list);
3644 rtnl_unlock();
3645}
3646
stephen hemmingerd3428942012-10-01 12:32:35 +00003647static struct pernet_operations vxlan_net_ops = {
3648 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003649 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003650 .id = &vxlan_net_id,
3651 .size = sizeof(struct vxlan_net),
3652};
3653
3654static int __init vxlan_init_module(void)
3655{
3656 int rc;
3657
3658 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3659
Daniel Borkmann783c1462014-01-22 21:07:53 +01003660 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003661 if (rc)
3662 goto out1;
3663
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003664 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003665 if (rc)
3666 goto out2;
3667
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003668 rc = rtnl_link_register(&vxlan_link_ops);
3669 if (rc)
3670 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003671
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003672 return 0;
3673out3:
3674 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003675out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003676 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003677out1:
3678 return rc;
3679}
Cong Wang7332a132013-05-27 22:35:53 +00003680late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003681
3682static void __exit vxlan_cleanup_module(void)
3683{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003684 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003685 unregister_netdevice_notifier(&vxlan_notifier_block);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003686 unregister_pernet_subsys(&vxlan_net_ops);
3687 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003688}
3689module_exit(vxlan_cleanup_module);
3690
3691MODULE_LICENSE("GPL");
3692MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003693MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003694MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003695MODULE_ALIAS_RTNL_LINK("vxlan");