blob: 30e1f215af736aac9a24f2ec36dc6ce8721c0965 [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
stephen hemmingerd3428942012-10-01 12:32:35 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/rculist.h>
20#include <linux/netdevice.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/udp.h>
24#include <linux/igmp.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
Pravin B Shelar1eaa8172013-08-19 11:23:29 -070027#include <linux/if_vlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000028#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000029#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000030#include <net/arp.h>
31#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000032#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000033#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/icmp.h>
35#include <net/udp.h>
Tom Herbert3ee64f32014-07-13 19:49:42 -070036#include <net/udp_tunnel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000037#include <net/rtnetlink.h>
38#include <net/route.h>
39#include <net/dsfield.h>
40#include <net/inet_ecn.h>
41#include <net/net_namespace.h>
42#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070043#include <net/vxlan.h>
Or Gerlitzdc01e7d2014-01-20 13:59:21 +020044#include <net/protocol.h>
Andy Zhouacbf74a2014-09-16 17:31:18 -070045#include <net/udp_tunnel.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080046#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ipv6.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080050#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080051#endif
Thomas Grafee122c72015-07-21 10:43:58 +020052#include <net/dst_metadata.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000053
54#define VXLAN_VERSION "0.1"
55
stephen hemminger553675f2013-05-16 11:35:20 +000056#define PORT_HASH_BITS 8
57#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000058#define FDB_AGE_DEFAULT 300 /* 5 min */
59#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
60
stephen hemminger23c578b2013-04-27 11:31:53 +000061/* UDP port for VXLAN traffic.
62 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070063 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000064 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070065static unsigned short vxlan_port __read_mostly = 8472;
66module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000067MODULE_PARM_DESC(udp_port, "Destination UDP port");
68
69static bool log_ecn_error = true;
70module_param(log_ecn_error, bool, 0644);
71MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
72
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070073static int vxlan_net_id;
Thomas Graf0dfbdf42015-07-21 10:44:02 +020074static struct rtnl_link_ops vxlan_link_ops;
stephen hemminger553675f2013-05-16 11:35:20 +000075
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030076static const u8 all_zeros_mac[ETH_ALEN];
77
stephen hemminger553675f2013-05-16 11:35:20 +000078/* per-network namespace private data for this module */
79struct vxlan_net {
80 struct list_head vxlan_list;
81 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070082 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000083};
84
stephen hemmingerd3428942012-10-01 12:32:35 +000085/* Forwarding table entry */
86struct vxlan_fdb {
87 struct hlist_node hlist; /* linked list of entries */
88 struct rcu_head rcu;
89 unsigned long updated; /* jiffies */
90 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070091 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +020092 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +000093 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +000094 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +000095};
96
stephen hemmingerd3428942012-10-01 12:32:35 +000097/* salt for hash table */
98static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -070099static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000100
Thomas Grafee122c72015-07-21 10:43:58 +0200101static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
102{
Thomas Grafe7030872015-07-21 10:44:01 +0200103 return vs->flags & VXLAN_F_COLLECT_METADATA ||
104 ip_tunnel_collect_metadata();
Thomas Grafee122c72015-07-21 10:43:58 +0200105}
106
Cong Wange4c7ed42013-08-31 13:44:33 +0800107#if IS_ENABLED(CONFIG_IPV6)
108static inline
109bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
110{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200111 if (a->sa.sa_family != b->sa.sa_family)
112 return false;
113 if (a->sa.sa_family == AF_INET6)
114 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
115 else
116 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800117}
118
119static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
120{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200121 if (ipa->sa.sa_family == AF_INET6)
122 return ipv6_addr_any(&ipa->sin6.sin6_addr);
123 else
124 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800125}
126
127static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
128{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200129 if (ipa->sa.sa_family == AF_INET6)
130 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
131 else
132 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800133}
134
135static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
136{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200137 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200138 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200139 ip->sa.sa_family = AF_INET6;
140 return 0;
141 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200142 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200143 ip->sa.sa_family = AF_INET;
144 return 0;
145 } else {
146 return -EAFNOSUPPORT;
147 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800148}
149
150static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200151 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800152{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200153 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200154 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200155 else
Jiri Benc930345e2015-03-29 16:59:25 +0200156 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800157}
158
159#else /* !CONFIG_IPV6 */
160
161static inline
162bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
163{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200164 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800165}
166
167static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
168{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200169 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800170}
171
172static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
173{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200174 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800175}
176
177static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
178{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200179 if (nla_len(nla) >= sizeof(struct in6_addr)) {
180 return -EAFNOSUPPORT;
181 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200182 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200183 ip->sa.sa_family = AF_INET;
184 return 0;
185 } else {
186 return -EAFNOSUPPORT;
187 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800188}
189
190static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200191 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800192{
Jiri Benc930345e2015-03-29 16:59:25 +0200193 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800194}
195#endif
196
stephen hemminger553675f2013-05-16 11:35:20 +0000197/* Virtual Network hash table head */
198static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
199{
200 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
201}
202
203/* Socket hash table head */
204static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000205{
206 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
207
stephen hemminger553675f2013-05-16 11:35:20 +0000208 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
209}
210
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700211/* First remote destination for a forwarding entry.
212 * Guaranteed to be non-NULL because remotes are never deleted.
213 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700214static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700215{
stephen hemminger5ca54612013-08-04 17:17:39 -0700216 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
217}
218
219static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
220{
221 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700222}
223
Thomas Grafac5132d2015-01-15 03:53:56 +0100224/* Find VXLAN socket based on network namespace, address family and UDP port
225 * and enabled unshareable flags.
226 */
227static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
228 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000229{
230 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800231
232 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000233
234 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200235 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Thomas Grafac5132d2015-01-15 03:53:56 +0100236 inet_sk(vs->sock->sk)->sk.sk_family == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800237 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000238 return vs;
239 }
240 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000241}
242
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700243static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000244{
245 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000246
stephen hemminger553675f2013-05-16 11:35:20 +0000247 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000248 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000249 return vxlan;
250 }
251
252 return NULL;
253}
254
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700255/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200256static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
Thomas Grafac5132d2015-01-15 03:53:56 +0100257 sa_family_t family, __be16 port,
258 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700259{
260 struct vxlan_sock *vs;
261
Thomas Grafac5132d2015-01-15 03:53:56 +0100262 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700263 if (!vs)
264 return NULL;
265
266 return vxlan_vs_find_vni(vs, id);
267}
268
stephen hemmingerd3428942012-10-01 12:32:35 +0000269/* Fill in neighbour message in skbuff. */
270static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700271 const struct vxlan_fdb *fdb,
272 u32 portid, u32 seq, int type, unsigned int flags,
273 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000274{
275 unsigned long now = jiffies;
276 struct nda_cacheinfo ci;
277 struct nlmsghdr *nlh;
278 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000279 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000280
281 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
282 if (nlh == NULL)
283 return -EMSGSIZE;
284
285 ndm = nlmsg_data(nlh);
286 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000287
288 send_eth = send_ip = true;
289
290 if (type == RTM_GETNEIGH) {
291 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800292 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000293 send_eth = !is_zero_ether_addr(fdb->eth_addr);
294 } else
295 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000296 ndm->ndm_state = fdb->state;
297 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000298 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800299 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000300
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100301 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100302 nla_put_s32(skb, NDA_LINK_NETNSID,
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +0200303 peernet2id_alloc(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100304 goto nla_put_failure;
305
David Stevense4f67ad2012-11-20 02:50:14 +0000306 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000307 goto nla_put_failure;
308
Cong Wange4c7ed42013-08-31 13:44:33 +0800309 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000310 goto nla_put_failure;
311
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200312 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000313 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
314 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000315 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700316 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000317 goto nla_put_failure;
318 if (rdst->remote_ifindex &&
319 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000320 goto nla_put_failure;
321
322 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
323 ci.ndm_confirmed = 0;
324 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
325 ci.ndm_refcnt = 0;
326
327 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
328 goto nla_put_failure;
329
Johannes Berg053c0952015-01-16 22:09:00 +0100330 nlmsg_end(skb, nlh);
331 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000332
333nla_put_failure:
334 nlmsg_cancel(skb, nlh);
335 return -EMSGSIZE;
336}
337
338static inline size_t vxlan_nlmsg_size(void)
339{
340 return NLMSG_ALIGN(sizeof(struct ndmsg))
341 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800342 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000343 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000344 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
345 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100346 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000347 + nla_total_size(sizeof(struct nda_cacheinfo));
348}
349
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200350static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
351 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000352{
353 struct net *net = dev_net(vxlan->dev);
354 struct sk_buff *skb;
355 int err = -ENOBUFS;
356
357 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
358 if (skb == NULL)
359 goto errout;
360
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200361 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000362 if (err < 0) {
363 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
364 WARN_ON(err == -EMSGSIZE);
365 kfree_skb(skb);
366 goto errout;
367 }
368
369 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
370 return;
371errout:
372 if (err < 0)
373 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
374}
375
Cong Wange4c7ed42013-08-31 13:44:33 +0800376static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000377{
378 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700379 struct vxlan_fdb f = {
380 .state = NUD_STALE,
381 };
382 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800383 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700384 .remote_vni = VXLAN_N_VID,
385 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700386
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200387 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000388}
389
390static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
391{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700392 struct vxlan_fdb f = {
393 .state = NUD_STALE,
394 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200395 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000396
David Stevense4f67ad2012-11-20 02:50:14 +0000397 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
398
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200399 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000400}
401
stephen hemmingerd3428942012-10-01 12:32:35 +0000402/* Hash Ethernet address */
403static u32 eth_hash(const unsigned char *addr)
404{
405 u64 value = get_unaligned((u64 *)addr);
406
407 /* only want 6 bytes */
408#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000409 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000410#else
411 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000412#endif
413 return hash_64(value, FDB_HASH_BITS);
414}
415
416/* Hash chain to use given mac address */
417static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
418 const u8 *mac)
419{
420 return &vxlan->fdb_head[eth_hash(mac)];
421}
422
423/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000424static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000425 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000426{
427 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
428 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000429
Sasha Levinb67bfe02013-02-27 17:06:00 -0800430 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700431 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000432 return f;
433 }
434
435 return NULL;
436}
437
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000438static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
439 const u8 *mac)
440{
441 struct vxlan_fdb *f;
442
443 f = __vxlan_find_mac(vxlan, mac);
444 if (f)
445 f->used = jiffies;
446
447 return f;
448}
449
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300450/* caller should hold vxlan->hash_lock */
451static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800452 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300453 __u32 vni, __u32 ifindex)
454{
455 struct vxlan_rdst *rd;
456
457 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800458 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300459 rd->remote_port == port &&
460 rd->remote_vni == vni &&
461 rd->remote_ifindex == ifindex)
462 return rd;
463 }
464
465 return NULL;
466}
467
Thomas Richter906dc182013-07-19 17:20:07 +0200468/* Replace destination of unicast mac */
469static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800470 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200471{
472 struct vxlan_rdst *rd;
473
474 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
475 if (rd)
476 return 0;
477
478 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
479 if (!rd)
480 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800481 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200482 rd->remote_port = port;
483 rd->remote_vni = vni;
484 rd->remote_ifindex = ifindex;
485 return 1;
486}
487
David Stevens66817122013-03-15 04:35:51 +0000488/* Add/update destinations for multicast */
489static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200490 union vxlan_addr *ip, __be16 port, __u32 vni,
491 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000492{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700493 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000494
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300495 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
496 if (rd)
497 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700498
David Stevens66817122013-03-15 04:35:51 +0000499 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
500 if (rd == NULL)
501 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800502 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000503 rd->remote_port = port;
504 rd->remote_vni = vni;
505 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700506
507 list_add_tail_rcu(&rd->list, &f->remotes);
508
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200509 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000510 return 1;
511}
512
Tom Herbertdfd86452015-01-12 17:00:38 -0800513static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
514 unsigned int off,
515 struct vxlanhdr *vh, size_t hdrlen,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800516 u32 data, struct gro_remcsum *grc,
517 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800518{
519 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -0800520
521 if (skb->remcsum_offload)
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800522 return NULL;
Tom Herbertdfd86452015-01-12 17:00:38 -0800523
524 if (!NAPI_GRO_CB(skb)->csum_valid)
525 return NULL;
526
527 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
528 offset = start + ((data & VXLAN_RCO_UDP) ?
529 offsetof(struct udphdr, check) :
530 offsetof(struct tcphdr, check));
531
532 plen = hdrlen + offset + sizeof(u16);
533
534 /* Pull checksum that will be written */
535 if (skb_gro_header_hard(skb, off + plen)) {
536 vh = skb_gro_header_slow(skb, off + plen, off);
537 if (!vh)
538 return NULL;
539 }
540
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800541 skb_gro_remcsum_process(skb, (void *)vh + hdrlen,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800542 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800543
544 skb->remcsum_offload = 1;
545
546 return vh;
547}
548
Tom Herberta2b12f32015-01-12 17:00:37 -0800549static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
550 struct sk_buff *skb,
551 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200552{
553 struct sk_buff *p, **pp = NULL;
554 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800555 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200556 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800557 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
558 udp_offloads);
559 u32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800560 struct gro_remcsum grc;
561
562 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200563
564 off_vx = skb_gro_offset(skb);
565 hlen = off_vx + sizeof(*vh);
566 vh = skb_gro_header_fast(skb, off_vx);
567 if (skb_gro_header_hard(skb, hlen)) {
568 vh = skb_gro_header_slow(skb, hlen, off_vx);
569 if (unlikely(!vh))
570 goto out;
571 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200572
Tom Herbertdfd86452015-01-12 17:00:38 -0800573 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
574 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
575
576 flags = ntohl(vh->vx_flags);
577
578 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
579 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800580 ntohl(vh->vx_vni), &grc,
581 !!(vs->flags &
582 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800583
584 if (!vh)
585 goto out;
586 }
587
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200588 flush = 0;
589
590 for (p = *head; p; p = p->next) {
591 if (!NAPI_GRO_CB(p)->same_flow)
592 continue;
593
594 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100595 if (vh->vx_flags != vh2->vx_flags ||
596 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200597 NAPI_GRO_CB(p)->same_flow = 0;
598 continue;
599 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200600 }
601
Jesse Gross9b174d82014-12-30 19:10:15 -0800602 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200603
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200604out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800605 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200606 NAPI_GRO_CB(skb)->flush |= flush;
607
608 return pp;
609}
610
Tom Herberta2b12f32015-01-12 17:00:37 -0800611static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
612 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200613{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800614 udp_tunnel_gro_complete(skb, nhoff);
615
Jesse Gross9b174d82014-12-30 19:10:15 -0800616 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200617}
618
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700619/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200620static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700621{
622 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200623 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700624 struct net *net = sock_net(sk);
625 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700626 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200627 int err;
628
629 if (sa_family == AF_INET) {
630 err = udp_add_offload(&vs->udp_offloads);
631 if (err)
632 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
633 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700634
635 rcu_read_lock();
636 for_each_netdev_rcu(net, dev) {
637 if (dev->netdev_ops->ndo_add_vxlan_port)
638 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
639 port);
640 }
641 rcu_read_unlock();
642}
643
644/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200645static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700646{
647 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200648 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700649 struct net *net = sock_net(sk);
650 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700651 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700652
653 rcu_read_lock();
654 for_each_netdev_rcu(net, dev) {
655 if (dev->netdev_ops->ndo_del_vxlan_port)
656 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
657 port);
658 }
659 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200660
661 if (sa_family == AF_INET)
662 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700663}
664
stephen hemmingerd3428942012-10-01 12:32:35 +0000665/* Add new entry to forwarding table -- assumes lock held */
666static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800667 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000668 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000669 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000670 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000671{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200672 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000673 struct vxlan_fdb *f;
674 int notify = 0;
675
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000676 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000677 if (f) {
678 if (flags & NLM_F_EXCL) {
679 netdev_dbg(vxlan->dev,
680 "lost race to create %pM\n", mac);
681 return -EEXIST;
682 }
683 if (f->state != state) {
684 f->state = state;
685 f->updated = jiffies;
686 notify = 1;
687 }
David Stevensae884082013-04-19 00:36:26 +0000688 if (f->flags != ndm_flags) {
689 f->flags = ndm_flags;
690 f->updated = jiffies;
691 notify = 1;
692 }
Thomas Richter906dc182013-07-19 17:20:07 +0200693 if ((flags & NLM_F_REPLACE)) {
694 /* Only change unicasts */
695 if (!(is_multicast_ether_addr(f->eth_addr) ||
696 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800697 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200698 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200699 } else
700 return -EOPNOTSUPP;
701 }
David Stevens66817122013-03-15 04:35:51 +0000702 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300703 (is_multicast_ether_addr(f->eth_addr) ||
704 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200705 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
706 &rd);
David Stevens66817122013-03-15 04:35:51 +0000707
708 if (rc < 0)
709 return rc;
710 notify |= rc;
711 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000712 } else {
713 if (!(flags & NLM_F_CREATE))
714 return -ENOENT;
715
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200716 if (vxlan->cfg.addrmax &&
717 vxlan->addrcnt >= vxlan->cfg.addrmax)
stephen hemmingerd3428942012-10-01 12:32:35 +0000718 return -ENOSPC;
719
Thomas Richter906dc182013-07-19 17:20:07 +0200720 /* Disallow replace to add a multicast entry */
721 if ((flags & NLM_F_REPLACE) &&
722 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
723 return -EOPNOTSUPP;
724
Cong Wange4c7ed42013-08-31 13:44:33 +0800725 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000726 f = kmalloc(sizeof(*f), GFP_ATOMIC);
727 if (!f)
728 return -ENOMEM;
729
730 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000731 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000732 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000733 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700734 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000735 memcpy(f->eth_addr, mac, ETH_ALEN);
736
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200737 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700738
stephen hemmingerd3428942012-10-01 12:32:35 +0000739 ++vxlan->addrcnt;
740 hlist_add_head_rcu(&f->hlist,
741 vxlan_fdb_head(vxlan, mac));
742 }
743
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200744 if (notify) {
745 if (rd == NULL)
746 rd = first_remote_rtnl(f);
747 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
748 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000749
750 return 0;
751}
752
Wei Yongjun6706c822013-04-11 19:00:35 +0000753static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000754{
755 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700756 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000757
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700758 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000759 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000760 kfree(f);
761}
762
stephen hemmingerd3428942012-10-01 12:32:35 +0000763static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
764{
765 netdev_dbg(vxlan->dev,
766 "delete %pM\n", f->eth_addr);
767
768 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200769 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000770
771 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000772 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000773}
774
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300775static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800776 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300777{
778 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800779 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300780
781 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800782 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
783 if (err)
784 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300785 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800786 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
787 if (remote->sa.sa_family == AF_INET) {
788 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
789 ip->sa.sa_family = AF_INET;
790#if IS_ENABLED(CONFIG_IPV6)
791 } else {
792 ip->sin6.sin6_addr = in6addr_any;
793 ip->sa.sa_family = AF_INET6;
794#endif
795 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300796 }
797
798 if (tb[NDA_PORT]) {
799 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
800 return -EINVAL;
801 *port = nla_get_be16(tb[NDA_PORT]);
802 } else {
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200803 *port = vxlan->cfg.dst_port;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300804 }
805
806 if (tb[NDA_VNI]) {
807 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
808 return -EINVAL;
809 *vni = nla_get_u32(tb[NDA_VNI]);
810 } else {
811 *vni = vxlan->default_dst.remote_vni;
812 }
813
814 if (tb[NDA_IFINDEX]) {
815 struct net_device *tdev;
816
817 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
818 return -EINVAL;
819 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800820 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300821 if (!tdev)
822 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300823 } else {
824 *ifindex = 0;
825 }
826
827 return 0;
828}
829
stephen hemmingerd3428942012-10-01 12:32:35 +0000830/* Add static entry (via netlink) */
831static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
832 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100833 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000834{
835 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300836 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800837 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000838 __be16 port;
839 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000840 int err;
841
842 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
843 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
844 ndm->ndm_state);
845 return -EINVAL;
846 }
847
848 if (tb[NDA_DST] == NULL)
849 return -EINVAL;
850
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300851 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
852 if (err)
853 return err;
David Stevens66817122013-03-15 04:35:51 +0000854
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300855 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
856 return -EAFNOSUPPORT;
857
stephen hemmingerd3428942012-10-01 12:32:35 +0000858 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800859 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000860 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000861 spin_unlock_bh(&vxlan->hash_lock);
862
863 return err;
864}
865
866/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000867static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
868 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100869 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000870{
871 struct vxlan_dev *vxlan = netdev_priv(dev);
872 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300873 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800874 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300875 __be16 port;
876 u32 vni, ifindex;
877 int err;
878
879 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
880 if (err)
881 return err;
882
883 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000884
885 spin_lock_bh(&vxlan->hash_lock);
886 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300887 if (!f)
888 goto out;
889
Cong Wange4c7ed42013-08-31 13:44:33 +0800890 if (!vxlan_addr_any(&ip)) {
891 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300892 if (!rd)
893 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000894 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300895
896 err = 0;
897
898 /* remove a destination if it's not the only one on the list,
899 * otherwise destroy the fdb entry
900 */
901 if (rd && !list_is_singular(&f->remotes)) {
902 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200903 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800904 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300905 goto out;
906 }
907
908 vxlan_fdb_destroy(vxlan, f);
909
910out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000911 spin_unlock_bh(&vxlan->hash_lock);
912
913 return err;
914}
915
916/* Dump forwarding table */
917static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400918 struct net_device *dev,
919 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000920{
921 struct vxlan_dev *vxlan = netdev_priv(dev);
922 unsigned int h;
923
924 for (h = 0; h < FDB_HASH_SIZE; ++h) {
925 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000926 int err;
927
Sasha Levinb67bfe02013-02-27 17:06:00 -0800928 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000929 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000930
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700931 if (idx < cb->args[0])
932 goto skip;
933
934 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000935 err = vxlan_fdb_info(skb, vxlan, f,
936 NETLINK_CB(cb->skb).portid,
937 cb->nlh->nlmsg_seq,
938 RTM_NEWNEIGH,
939 NLM_F_MULTI, rd);
940 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700941 goto out;
David Stevens66817122013-03-15 04:35:51 +0000942 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700943skip:
944 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000945 }
946 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700947out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000948 return idx;
949}
950
951/* Watch incoming packets to learn mapping between Ethernet address
952 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900953 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000954 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700955static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800956 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000957{
958 struct vxlan_dev *vxlan = netdev_priv(dev);
959 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000960
961 f = vxlan_find_mac(vxlan, src_mac);
962 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700963 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700964
Cong Wange4c7ed42013-08-31 13:44:33 +0800965 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700966 return false;
967
968 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700969 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700970 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000971
972 if (net_ratelimit())
973 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800974 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +0100975 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +0000976
Cong Wange4c7ed42013-08-31 13:44:33 +0800977 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000978 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200979 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000980 } else {
981 /* learned new entry */
982 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700983
984 /* close off race between vxlan_flush and incoming packets */
985 if (netif_running(dev))
986 vxlan_fdb_create(vxlan, src_mac, src_ip,
987 NUD_REACHABLE,
988 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200989 vxlan->cfg.dst_port,
stephen hemminger3bf74b12013-06-17 12:09:57 -0700990 vxlan->default_dst.remote_vni,
991 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000992 spin_unlock(&vxlan->hash_lock);
993 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700994
995 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000996}
997
stephen hemmingerd3428942012-10-01 12:32:35 +0000998/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +0800999static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001000{
stephen hemminger553675f2013-05-16 11:35:20 +00001001 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001002
Gao feng95ab0992013-12-10 16:37:33 +08001003 /* The vxlan_sock is only used by dev, leaving group has
1004 * no effect on other vxlan devices.
1005 */
1006 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1007 return false;
1008
stephen hemminger553675f2013-05-16 11:35:20 +00001009 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001010 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001011 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001012
Gao feng95ab0992013-12-10 16:37:33 +08001013 if (vxlan->vn_sock != dev->vn_sock)
1014 continue;
1015
1016 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1017 &dev->default_dst.remote_ip))
1018 continue;
1019
1020 if (vxlan->default_dst.remote_ifindex !=
1021 dev->default_dst.remote_ifindex)
1022 continue;
1023
1024 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001025 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001026
1027 return false;
1028}
1029
Pravin B Shelar012a5722013-08-19 11:23:07 -07001030void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001031{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001032 struct sock *sk = vs->sock->sk;
1033 struct net *net = sock_net(sk);
1034 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001035
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001036 if (!atomic_dec_and_test(&vs->refcnt))
1037 return;
1038
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001039 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001040 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001041 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001042 spin_unlock(&vn->sock_lock);
1043
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001044 queue_work(vxlan_wq, &vs->del_work);
1045}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001046EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001047
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001048/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001049 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001050 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001051static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001052{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001053 struct vxlan_sock *vs = vxlan->vn_sock;
1054 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001055 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1056 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001057 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001058
stephen hemmingerd3428942012-10-01 12:32:35 +00001059 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001060 if (ip->sa.sa_family == AF_INET) {
1061 struct ip_mreqn mreq = {
1062 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1063 .imr_ifindex = ifindex,
1064 };
1065
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001066 ret = ip_mc_join_group(sk, &mreq);
Cong Wange4c7ed42013-08-31 13:44:33 +08001067#if IS_ENABLED(CONFIG_IPV6)
1068 } else {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001069 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1070 &ip->sin6.sin6_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001071#endif
1072 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001073 release_sock(sk);
1074
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001075 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001076}
1077
1078/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001079static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001080{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001081 struct vxlan_sock *vs = vxlan->vn_sock;
1082 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001083 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1084 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001085 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001086
1087 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001088 if (ip->sa.sa_family == AF_INET) {
1089 struct ip_mreqn mreq = {
1090 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1091 .imr_ifindex = ifindex,
1092 };
1093
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001094 ret = ip_mc_leave_group(sk, &mreq);
Cong Wange4c7ed42013-08-31 13:44:33 +08001095#if IS_ENABLED(CONFIG_IPV6)
1096 } else {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001097 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1098 &ip->sin6.sin6_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001099#endif
1100 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001101 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001102
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001103 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001104}
1105
Tom Herbertdfd86452015-01-12 17:00:38 -08001106static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001107 size_t hdrlen, u32 data, bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -08001108{
1109 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -08001110
Tom Herbertdfd86452015-01-12 17:00:38 -08001111 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1112 offset = start + ((data & VXLAN_RCO_UDP) ?
1113 offsetof(struct udphdr, check) :
1114 offsetof(struct tcphdr, check));
1115
1116 plen = hdrlen + offset + sizeof(u16);
1117
1118 if (!pskb_may_pull(skb, plen))
1119 return NULL;
1120
1121 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1122
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001123 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset,
1124 nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -08001125
1126 return vh;
1127}
1128
stephen hemmingerd3428942012-10-01 12:32:35 +00001129/* Callback from net/ipv4/udp.c to receive packets */
1130static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1131{
Thomas Grafee122c72015-07-21 10:43:58 +02001132 struct metadata_dst *tun_dst = NULL;
1133 struct ip_tunnel_info *info;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001134 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001135 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001136 u32 flags, vni;
Thomas Grafee122c72015-07-21 10:43:58 +02001137 struct vxlan_metadata _md;
1138 struct vxlan_metadata *md = &_md;
stephen hemmingerd3428942012-10-01 12:32:35 +00001139
stephen hemmingerd3428942012-10-01 12:32:35 +00001140 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001141 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001142 goto error;
1143
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001144 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001145 flags = ntohl(vxh->vx_flags);
1146 vni = ntohl(vxh->vx_vni);
1147
1148 if (flags & VXLAN_HF_VNI) {
1149 flags &= ~VXLAN_HF_VNI;
1150 } else {
1151 /* VNI flag always required to be set */
1152 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001153 }
1154
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001155 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001156 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001157 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001158
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001159 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001160 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001161 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001162
Tom Herbertdfd86452015-01-12 17:00:38 -08001163 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001164 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni,
1165 !!(vs->flags & VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -08001166 if (!vxh)
1167 goto drop;
1168
1169 flags &= ~VXLAN_HF_RCO;
Alexey Kodanev40fb70f2015-03-13 19:13:53 +03001170 vni &= VXLAN_VNI_MASK;
Tom Herbertdfd86452015-01-12 17:00:38 -08001171 }
1172
Thomas Grafee122c72015-07-21 10:43:58 +02001173 if (vxlan_collect_metadata(vs)) {
1174 const struct iphdr *iph = ip_hdr(skb);
1175
1176 tun_dst = metadata_dst_alloc(sizeof(*md), GFP_ATOMIC);
1177 if (!tun_dst)
1178 goto drop;
1179
1180 info = &tun_dst->u.tun_info;
1181 info->key.ipv4_src = iph->saddr;
1182 info->key.ipv4_dst = iph->daddr;
1183 info->key.ipv4_tos = iph->tos;
1184 info->key.ipv4_ttl = iph->ttl;
1185 info->key.tp_src = udp_hdr(skb)->source;
1186 info->key.tp_dst = udp_hdr(skb)->dest;
1187
1188 info->mode = IP_TUNNEL_INFO_RX;
1189 info->key.tun_flags = TUNNEL_KEY;
1190 info->key.tun_id = cpu_to_be64(vni >> 8);
1191 if (udp_hdr(skb)->check != 0)
1192 info->key.tun_flags |= TUNNEL_CSUM;
1193
1194 md = ip_tunnel_info_opts(info, sizeof(*md));
1195 md->tun_dst = tun_dst;
1196 } else {
1197 memset(md, 0, sizeof(*md));
1198 }
1199
Thomas Graf35114942015-01-15 03:53:55 +01001200 /* For backwards compatibility, only allow reserved fields to be
1201 * used by VXLAN extensions if explicitly requested.
1202 */
1203 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1204 struct vxlanhdr_gbp *gbp;
1205
1206 gbp = (struct vxlanhdr_gbp *)vxh;
Thomas Grafee122c72015-07-21 10:43:58 +02001207 md->gbp = ntohs(gbp->policy_id);
1208
1209 if (tun_dst)
1210 info->key.tun_flags |= TUNNEL_VXLAN_OPT;
Thomas Graf35114942015-01-15 03:53:55 +01001211
1212 if (gbp->dont_learn)
Thomas Grafee122c72015-07-21 10:43:58 +02001213 md->gbp |= VXLAN_GBP_DONT_LEARN;
Thomas Graf35114942015-01-15 03:53:55 +01001214
1215 if (gbp->policy_applied)
Thomas Grafee122c72015-07-21 10:43:58 +02001216 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Thomas Graf35114942015-01-15 03:53:55 +01001217
1218 flags &= ~VXLAN_GBP_USED_BITS;
1219 }
1220
Alexey Kodanev40fb70f2015-03-13 19:13:53 +03001221 if (flags || vni & ~VXLAN_VNI_MASK) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001222 /* If there are any unprocessed flags remaining treat
1223 * this as a malformed packet. This behavior diverges from
1224 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1225 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001226 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001227 * is more robust and provides a little more security in
1228 * adding extensions to VXLAN.
1229 */
1230
1231 goto bad_flags;
1232 }
1233
Thomas Grafee122c72015-07-21 10:43:58 +02001234 md->vni = vxh->vx_vni;
1235 vs->rcv(vs, skb, md);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001236 return 0;
1237
1238drop:
1239 /* Consume bad packet */
1240 kfree_skb(skb);
1241 return 0;
1242
Tom Herbert3bf39472015-01-08 12:31:18 -08001243bad_flags:
1244 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1245 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1246
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001247error:
Thomas Grafee122c72015-07-21 10:43:58 +02001248 if (tun_dst)
1249 dst_release((struct dst_entry *)tun_dst);
1250
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001251 /* Return non vxlan pkt */
1252 return 1;
1253}
1254
Thomas Graf35114942015-01-15 03:53:55 +01001255static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1256 struct vxlan_metadata *md)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001257{
Cong Wange4c7ed42013-08-31 13:44:33 +08001258 struct iphdr *oip = NULL;
1259 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001260 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001261 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001262 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001263 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001264 int err = 0;
1265 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001266
Thomas Grafee122c72015-07-21 10:43:58 +02001267 /* For flow based devices, map all packets to VNI 0 */
1268 if (vs->flags & VXLAN_F_FLOW_BASED)
1269 vni = 0;
1270 else
1271 vni = ntohl(md->vni) >> 8;
1272
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001273 /* Is this VNI defined? */
1274 vxlan = vxlan_vs_find_vni(vs, vni);
1275 if (!vxlan)
1276 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001277
Cong Wange4c7ed42013-08-31 13:44:33 +08001278 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001279 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001280 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001281 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001282 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001283
1284 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001285 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001286 goto drop;
1287
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001288 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001289 if (remote_ip->sa.sa_family == AF_INET) {
1290 oip = ip_hdr(skb);
1291 saddr.sin.sin_addr.s_addr = oip->saddr;
1292 saddr.sa.sa_family = AF_INET;
1293#if IS_ENABLED(CONFIG_IPV6)
1294 } else {
1295 oip6 = ipv6_hdr(skb);
1296 saddr.sin6.sin6_addr = oip6->saddr;
1297 saddr.sa.sa_family = AF_INET6;
1298#endif
1299 }
1300
Thomas Grafee122c72015-07-21 10:43:58 +02001301 if (md->tun_dst) {
1302 skb_dst_set(skb, (struct dst_entry *)md->tun_dst);
1303 md->tun_dst = NULL;
1304 }
1305
stephen hemminger26a41ae62013-06-17 12:09:58 -07001306 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001307 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001308 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001309
stephen hemmingerd3428942012-10-01 12:32:35 +00001310 skb_reset_network_header(skb);
Thomas Grafee122c72015-07-21 10:43:58 +02001311 /* In flow-based mode, GBP is carried in dst_metadata */
1312 if (!(vs->flags & VXLAN_F_FLOW_BASED))
1313 skb->mark = md->gbp;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001314
Cong Wange4c7ed42013-08-31 13:44:33 +08001315 if (oip6)
1316 err = IP6_ECN_decapsulate(oip6, skb);
1317 if (oip)
1318 err = IP_ECN_decapsulate(oip, skb);
1319
stephen hemmingerd3428942012-10-01 12:32:35 +00001320 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001321 if (log_ecn_error) {
1322 if (oip6)
1323 net_info_ratelimited("non-ECT from %pI6\n",
1324 &oip6->saddr);
1325 if (oip)
1326 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1327 &oip->saddr, oip->tos);
1328 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001329 if (err > 1) {
1330 ++vxlan->dev->stats.rx_frame_errors;
1331 ++vxlan->dev->stats.rx_errors;
1332 goto drop;
1333 }
1334 }
1335
Pravin B Shelare8171042013-03-25 14:49:46 +00001336 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001337 u64_stats_update_begin(&stats->syncp);
1338 stats->rx_packets++;
1339 stats->rx_bytes += skb->len;
1340 u64_stats_update_end(&stats->syncp);
1341
1342 netif_rx(skb);
1343
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001344 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001345drop:
Thomas Grafee122c72015-07-21 10:43:58 +02001346 if (md->tun_dst)
1347 dst_release((struct dst_entry *)md->tun_dst);
1348
stephen hemmingerd3428942012-10-01 12:32:35 +00001349 /* Consume bad packet */
1350 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001351}
1352
David Stevense4f67ad2012-11-20 02:50:14 +00001353static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1354{
1355 struct vxlan_dev *vxlan = netdev_priv(dev);
1356 struct arphdr *parp;
1357 u8 *arpptr, *sha;
1358 __be32 sip, tip;
1359 struct neighbour *n;
1360
1361 if (dev->flags & IFF_NOARP)
1362 goto out;
1363
1364 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1365 dev->stats.tx_dropped++;
1366 goto out;
1367 }
1368 parp = arp_hdr(skb);
1369
1370 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1371 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1372 parp->ar_pro != htons(ETH_P_IP) ||
1373 parp->ar_op != htons(ARPOP_REQUEST) ||
1374 parp->ar_hln != dev->addr_len ||
1375 parp->ar_pln != 4)
1376 goto out;
1377 arpptr = (u8 *)parp + sizeof(struct arphdr);
1378 sha = arpptr;
1379 arpptr += dev->addr_len; /* sha */
1380 memcpy(&sip, arpptr, sizeof(sip));
1381 arpptr += sizeof(sip);
1382 arpptr += dev->addr_len; /* tha */
1383 memcpy(&tip, arpptr, sizeof(tip));
1384
1385 if (ipv4_is_loopback(tip) ||
1386 ipv4_is_multicast(tip))
1387 goto out;
1388
1389 n = neigh_lookup(&arp_tbl, &tip, dev);
1390
1391 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001392 struct vxlan_fdb *f;
1393 struct sk_buff *reply;
1394
1395 if (!(n->nud_state & NUD_CONNECTED)) {
1396 neigh_release(n);
1397 goto out;
1398 }
1399
1400 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001401 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001402 /* bridge-local neighbor */
1403 neigh_release(n);
1404 goto out;
1405 }
1406
1407 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1408 n->ha, sha);
1409
1410 neigh_release(n);
1411
David Stevens73461352014-03-18 12:32:29 -04001412 if (reply == NULL)
1413 goto out;
1414
David Stevense4f67ad2012-11-20 02:50:14 +00001415 skb_reset_mac_header(reply);
1416 __skb_pull(reply, skb_network_offset(reply));
1417 reply->ip_summed = CHECKSUM_UNNECESSARY;
1418 reply->pkt_type = PACKET_HOST;
1419
1420 if (netif_rx_ni(reply) == NET_RX_DROP)
1421 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001422 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1423 union vxlan_addr ipa = {
1424 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001425 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001426 };
1427
1428 vxlan_ip_miss(dev, &ipa);
1429 }
David Stevense4f67ad2012-11-20 02:50:14 +00001430out:
1431 consume_skb(skb);
1432 return NETDEV_TX_OK;
1433}
1434
Cong Wangf564f452013-08-31 13:44:36 +08001435#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001436static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1437 struct neighbour *n, bool isrouter)
1438{
1439 struct net_device *dev = request->dev;
1440 struct sk_buff *reply;
1441 struct nd_msg *ns, *na;
1442 struct ipv6hdr *pip6;
1443 u8 *daddr;
1444 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1445 int ns_olen;
1446 int i, len;
1447
1448 if (dev == NULL)
1449 return NULL;
1450
1451 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1452 sizeof(*na) + na_olen + dev->needed_tailroom;
1453 reply = alloc_skb(len, GFP_ATOMIC);
1454 if (reply == NULL)
1455 return NULL;
1456
1457 reply->protocol = htons(ETH_P_IPV6);
1458 reply->dev = dev;
1459 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1460 skb_push(reply, sizeof(struct ethhdr));
1461 skb_set_mac_header(reply, 0);
1462
1463 ns = (struct nd_msg *)skb_transport_header(request);
1464
1465 daddr = eth_hdr(request)->h_source;
1466 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1467 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1468 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1469 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1470 break;
1471 }
1472 }
1473
1474 /* Ethernet header */
1475 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1476 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1477 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1478 reply->protocol = htons(ETH_P_IPV6);
1479
1480 skb_pull(reply, sizeof(struct ethhdr));
1481 skb_set_network_header(reply, 0);
1482 skb_put(reply, sizeof(struct ipv6hdr));
1483
1484 /* IPv6 header */
1485
1486 pip6 = ipv6_hdr(reply);
1487 memset(pip6, 0, sizeof(struct ipv6hdr));
1488 pip6->version = 6;
1489 pip6->priority = ipv6_hdr(request)->priority;
1490 pip6->nexthdr = IPPROTO_ICMPV6;
1491 pip6->hop_limit = 255;
1492 pip6->daddr = ipv6_hdr(request)->saddr;
1493 pip6->saddr = *(struct in6_addr *)n->primary_key;
1494
1495 skb_pull(reply, sizeof(struct ipv6hdr));
1496 skb_set_transport_header(reply, 0);
1497
1498 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1499
1500 /* Neighbor Advertisement */
1501 memset(na, 0, sizeof(*na)+na_olen);
1502 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1503 na->icmph.icmp6_router = isrouter;
1504 na->icmph.icmp6_override = 1;
1505 na->icmph.icmp6_solicited = 1;
1506 na->target = ns->target;
1507 ether_addr_copy(&na->opt[2], n->ha);
1508 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1509 na->opt[1] = na_olen >> 3;
1510
1511 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1512 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1513 csum_partial(na, sizeof(*na)+na_olen, 0));
1514
1515 pip6->payload_len = htons(sizeof(*na)+na_olen);
1516
1517 skb_push(reply, sizeof(struct ipv6hdr));
1518
1519 reply->ip_summed = CHECKSUM_UNNECESSARY;
1520
1521 return reply;
1522}
1523
Cong Wangf564f452013-08-31 13:44:36 +08001524static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1525{
1526 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001527 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001528 const struct ipv6hdr *iphdr;
1529 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001530 struct neighbour *n;
1531 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001532
1533 in6_dev = __in6_dev_get(dev);
1534 if (!in6_dev)
1535 goto out;
1536
Cong Wangf564f452013-08-31 13:44:36 +08001537 iphdr = ipv6_hdr(skb);
1538 saddr = &iphdr->saddr;
1539 daddr = &iphdr->daddr;
1540
Cong Wangf564f452013-08-31 13:44:36 +08001541 msg = (struct nd_msg *)skb_transport_header(skb);
1542 if (msg->icmph.icmp6_code != 0 ||
1543 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1544 goto out;
1545
David Stevens4b29dba2014-03-24 10:39:58 -04001546 if (ipv6_addr_loopback(daddr) ||
1547 ipv6_addr_is_multicast(&msg->target))
1548 goto out;
1549
1550 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001551
1552 if (n) {
1553 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001554 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001555
1556 if (!(n->nud_state & NUD_CONNECTED)) {
1557 neigh_release(n);
1558 goto out;
1559 }
1560
1561 f = vxlan_find_mac(vxlan, n->ha);
1562 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1563 /* bridge-local neighbor */
1564 neigh_release(n);
1565 goto out;
1566 }
1567
David Stevens4b29dba2014-03-24 10:39:58 -04001568 reply = vxlan_na_create(skb, n,
1569 !!(f ? f->flags & NTF_ROUTER : 0));
1570
Cong Wangf564f452013-08-31 13:44:36 +08001571 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001572
1573 if (reply == NULL)
1574 goto out;
1575
1576 if (netif_rx_ni(reply) == NET_RX_DROP)
1577 dev->stats.rx_dropped++;
1578
Cong Wangf564f452013-08-31 13:44:36 +08001579 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001580 union vxlan_addr ipa = {
1581 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001582 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001583 };
1584
Cong Wangf564f452013-08-31 13:44:36 +08001585 vxlan_ip_miss(dev, &ipa);
1586 }
1587
1588out:
1589 consume_skb(skb);
1590 return NETDEV_TX_OK;
1591}
1592#endif
1593
David Stevense4f67ad2012-11-20 02:50:14 +00001594static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1595{
1596 struct vxlan_dev *vxlan = netdev_priv(dev);
1597 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001598
1599 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1600 return false;
1601
1602 n = NULL;
1603 switch (ntohs(eth_hdr(skb)->h_proto)) {
1604 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001605 {
1606 struct iphdr *pip;
1607
David Stevense4f67ad2012-11-20 02:50:14 +00001608 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1609 return false;
1610 pip = ip_hdr(skb);
1611 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001612 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1613 union vxlan_addr ipa = {
1614 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001615 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001616 };
1617
1618 vxlan_ip_miss(dev, &ipa);
1619 return false;
1620 }
1621
David Stevense4f67ad2012-11-20 02:50:14 +00001622 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001623 }
1624#if IS_ENABLED(CONFIG_IPV6)
1625 case ETH_P_IPV6:
1626 {
1627 struct ipv6hdr *pip6;
1628
1629 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1630 return false;
1631 pip6 = ipv6_hdr(skb);
1632 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1633 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1634 union vxlan_addr ipa = {
1635 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001636 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001637 };
1638
1639 vxlan_ip_miss(dev, &ipa);
1640 return false;
1641 }
1642
1643 break;
1644 }
1645#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001646 default:
1647 return false;
1648 }
1649
1650 if (n) {
1651 bool diff;
1652
Joe Perches7367d0b2013-09-01 11:51:23 -07001653 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001654 if (diff) {
1655 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1656 dev->addr_len);
1657 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1658 }
1659 neigh_release(n);
1660 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001661 }
1662
David Stevense4f67ad2012-11-20 02:50:14 +00001663 return false;
1664}
1665
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001666static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001667 struct vxlan_metadata *md)
1668{
1669 struct vxlanhdr_gbp *gbp;
1670
Thomas Grafdb79a622015-02-04 17:00:04 +01001671 if (!md->gbp)
1672 return;
1673
Thomas Graf35114942015-01-15 03:53:55 +01001674 gbp = (struct vxlanhdr_gbp *)vxh;
1675 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1676
1677 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1678 gbp->dont_learn = 1;
1679
1680 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1681 gbp->policy_applied = 1;
1682
1683 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1684}
1685
Cong Wange4c7ed42013-08-31 13:44:33 +08001686#if IS_ENABLED(CONFIG_IPV6)
David Miller79b16aa2015-04-05 22:19:09 -04001687static int vxlan6_xmit_skb(struct dst_entry *dst, struct sock *sk,
1688 struct sk_buff *skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001689 struct net_device *dev, struct in6_addr *saddr,
1690 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Thomas Graf35114942015-01-15 03:53:55 +01001691 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001692 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Cong Wange4c7ed42013-08-31 13:44:33 +08001693{
Cong Wange4c7ed42013-08-31 13:44:33 +08001694 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001695 int min_headroom;
1696 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001697 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbertdfd86452015-01-12 17:00:38 -08001698 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1699 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001700
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001701 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001702 skb->ip_summed == CHECKSUM_PARTIAL) {
1703 int csum_start = skb_checksum_start_offset(skb);
1704
1705 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1706 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1707 (skb->csum_offset == offsetof(struct udphdr, check) ||
1708 skb->csum_offset == offsetof(struct tcphdr, check))) {
1709 udp_sum = false;
1710 type |= SKB_GSO_TUNNEL_REMCSUM;
1711 }
1712 }
1713
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001714 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001715
Cong Wange4c7ed42013-08-31 13:44:33 +08001716 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1717 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001718 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001719
1720 /* Need space for new headers (invalidates iph ptr) */
1721 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001722 if (unlikely(err)) {
1723 kfree_skb(skb);
1724 goto err;
1725 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001726
Jiri Pirko59682502014-11-19 14:04:59 +01001727 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001728 if (WARN_ON(!skb)) {
1729 err = -ENOMEM;
1730 goto err;
1731 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001732
Jesse Grossb736a622015-04-09 11:19:14 -07001733 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1734 if (IS_ERR(skb)) {
1735 err = -EINVAL;
1736 goto err;
1737 }
1738
Cong Wange4c7ed42013-08-31 13:44:33 +08001739 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001740 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001741 vxh->vx_vni = md->vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001742
Tom Herbertdfd86452015-01-12 17:00:38 -08001743 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1744 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1745 VXLAN_RCO_SHIFT;
1746
1747 if (skb->csum_offset == offsetof(struct udphdr, check))
1748 data |= VXLAN_RCO_UDP;
1749
1750 vxh->vx_vni |= htonl(data);
1751 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1752
1753 if (!skb_is_gso(skb)) {
1754 skb->ip_summed = CHECKSUM_NONE;
1755 skb->encapsulation = 0;
1756 }
1757 }
1758
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001759 if (vxflags & VXLAN_F_GBP)
1760 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001761
Tom Herbert996c9fd2014-09-29 20:22:33 -07001762 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1763
David Miller79b16aa2015-04-05 22:19:09 -04001764 udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio,
Tom Herbertd998f8e2015-01-20 11:23:04 -08001765 ttl, src_port, dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001766 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
Cong Wange4c7ed42013-08-31 13:44:33 +08001767 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001768err:
1769 dst_release(dst);
1770 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001771}
1772#endif
1773
David Miller79b16aa2015-04-05 22:19:09 -04001774int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
Pravin B Shelar49560532013-08-19 11:23:17 -07001775 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Thomas Graf35114942015-01-15 03:53:55 +01001776 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001777 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Pravin B Shelar49560532013-08-19 11:23:17 -07001778{
1779 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001780 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001781 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001782 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
Tom Herbertdfd86452015-01-12 17:00:38 -08001783 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1784 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001785
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001786 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001787 skb->ip_summed == CHECKSUM_PARTIAL) {
1788 int csum_start = skb_checksum_start_offset(skb);
1789
1790 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1791 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1792 (skb->csum_offset == offsetof(struct udphdr, check) ||
1793 skb->csum_offset == offsetof(struct tcphdr, check))) {
1794 udp_sum = false;
1795 type |= SKB_GSO_TUNNEL_REMCSUM;
1796 }
1797 }
1798
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001799 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001800 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001801 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001802
1803 /* Need space for new headers (invalidates iph ptr) */
1804 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001805 if (unlikely(err)) {
1806 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001807 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001808 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001809
Jiri Pirko59682502014-11-19 14:04:59 +01001810 skb = vlan_hwaccel_push_inside(skb);
1811 if (WARN_ON(!skb))
1812 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001813
Jesse Grossb736a622015-04-09 11:19:14 -07001814 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1815 if (IS_ERR(skb))
1816 return PTR_ERR(skb);
1817
Pravin B Shelar49560532013-08-19 11:23:17 -07001818 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001819 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001820 vxh->vx_vni = md->vni;
Pravin B Shelar49560532013-08-19 11:23:17 -07001821
Tom Herbertdfd86452015-01-12 17:00:38 -08001822 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1823 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1824 VXLAN_RCO_SHIFT;
1825
1826 if (skb->csum_offset == offsetof(struct udphdr, check))
1827 data |= VXLAN_RCO_UDP;
1828
1829 vxh->vx_vni |= htonl(data);
1830 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1831
1832 if (!skb_is_gso(skb)) {
1833 skb->ip_summed = CHECKSUM_NONE;
1834 skb->encapsulation = 0;
1835 }
1836 }
1837
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001838 if (vxflags & VXLAN_F_GBP)
1839 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001840
Tom Herbert996c9fd2014-09-29 20:22:33 -07001841 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1842
David Miller79b16aa2015-04-05 22:19:09 -04001843 return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
Tom Herbertd998f8e2015-01-20 11:23:04 -08001844 ttl, df, src_port, dst_port, xnet,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001845 !(vxflags & VXLAN_F_UDP_CSUM));
Pravin B Shelar49560532013-08-19 11:23:17 -07001846}
1847EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1848
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001849/* Bypass encapsulation if the destination is local */
1850static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1851 struct vxlan_dev *dst_vxlan)
1852{
Li RongQing8f849852014-01-04 13:57:59 +08001853 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001854 union vxlan_addr loopback;
1855 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001856 struct net_device *dev = skb->dev;
1857 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001858
Li RongQing8f849852014-01-04 13:57:59 +08001859 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1860 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001861 skb->pkt_type = PACKET_HOST;
1862 skb->encapsulation = 0;
1863 skb->dev = dst_vxlan->dev;
1864 __skb_pull(skb, skb_network_offset(skb));
1865
Cong Wange4c7ed42013-08-31 13:44:33 +08001866 if (remote_ip->sa.sa_family == AF_INET) {
1867 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1868 loopback.sa.sa_family = AF_INET;
1869#if IS_ENABLED(CONFIG_IPV6)
1870 } else {
1871 loopback.sin6.sin6_addr = in6addr_loopback;
1872 loopback.sa.sa_family = AF_INET6;
1873#endif
1874 }
1875
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001876 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001877 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001878
1879 u64_stats_update_begin(&tx_stats->syncp);
1880 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001881 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001882 u64_stats_update_end(&tx_stats->syncp);
1883
1884 if (netif_rx(skb) == NET_RX_SUCCESS) {
1885 u64_stats_update_begin(&rx_stats->syncp);
1886 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001887 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001888 u64_stats_update_end(&rx_stats->syncp);
1889 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001890 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001891 }
1892}
1893
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001894static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1895 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001896{
Thomas Graf3093fbe2015-07-21 10:44:00 +02001897 struct ip_tunnel_info *info;
stephen hemmingerd3428942012-10-01 12:32:35 +00001898 struct vxlan_dev *vxlan = netdev_priv(dev);
David Miller79b16aa2015-04-05 22:19:09 -04001899 struct sock *sk = vxlan->vn_sock->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001900 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001901 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001902 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001903 union vxlan_addr *dst;
Thomas Grafee122c72015-07-21 10:43:58 +02001904 union vxlan_addr remote_ip;
1905 struct vxlan_metadata _md;
1906 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001907 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001908 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001909 __be16 df = 0;
1910 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001911 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02001912 u32 flags = vxlan->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001913
Thomas Graf3093fbe2015-07-21 10:44:00 +02001914 /* FIXME: Support IPv6 */
1915 info = skb_tunnel_info(skb, AF_INET);
1916
Thomas Grafee122c72015-07-21 10:43:58 +02001917 if (rdst) {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001918 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
Thomas Grafee122c72015-07-21 10:43:58 +02001919 vni = rdst->remote_vni;
1920 dst = &rdst->remote_ip;
1921 } else {
1922 if (!info) {
1923 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1924 dev->name);
1925 goto drop;
1926 }
1927
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001928 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
Thomas Grafee122c72015-07-21 10:43:58 +02001929 vni = be64_to_cpu(info->key.tun_id);
1930 remote_ip.sin.sin_family = AF_INET;
1931 remote_ip.sin.sin_addr.s_addr = info->key.ipv4_dst;
1932 dst = &remote_ip;
1933 }
David Stevense4f67ad2012-11-20 02:50:14 +00001934
Cong Wange4c7ed42013-08-31 13:44:33 +08001935 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001936 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001937 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001938 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001939 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001940 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001941 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001942 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001943
stephen hemmingerd3428942012-10-01 12:32:35 +00001944 old_iph = ip_hdr(skb);
1945
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001946 ttl = vxlan->cfg.ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001947 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001948 ttl = 1;
1949
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001950 tos = vxlan->cfg.tos;
stephen hemmingerd3428942012-10-01 12:32:35 +00001951 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001952 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001953
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001954 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
1955 vxlan->cfg.port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001956
Cong Wange4c7ed42013-08-31 13:44:33 +08001957 if (dst->sa.sa_family == AF_INET) {
Thomas Grafee122c72015-07-21 10:43:58 +02001958 if (info) {
1959 if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
1960 df = htons(IP_DF);
1961 if (info->key.tun_flags & TUNNEL_CSUM)
1962 flags |= VXLAN_F_UDP_CSUM;
1963 else
1964 flags &= ~VXLAN_F_UDP_CSUM;
1965
1966 ttl = info->key.ipv4_ttl;
1967 tos = info->key.ipv4_tos;
1968
1969 if (info->options_len)
1970 md = ip_tunnel_info_opts(info, sizeof(*md));
1971 } else {
1972 md->gbp = skb->mark;
1973 }
1974
Cong Wange4c7ed42013-08-31 13:44:33 +08001975 memset(&fl4, 0, sizeof(fl4));
Thomas Grafee122c72015-07-21 10:43:58 +02001976 fl4.flowi4_oif = rdst ? rdst->remote_ifindex : 0;
Cong Wange4c7ed42013-08-31 13:44:33 +08001977 fl4.flowi4_tos = RT_TOS(tos);
Thomas Graf239fb792015-05-05 15:09:21 +02001978 fl4.flowi4_mark = skb->mark;
1979 fl4.flowi4_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001980 fl4.daddr = dst->sin.sin_addr.s_addr;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02001981 fl4.saddr = vxlan->cfg.saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001982
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001983 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001984 if (IS_ERR(rt)) {
1985 netdev_dbg(dev, "no route to %pI4\n",
1986 &dst->sin.sin_addr.s_addr);
1987 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001988 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001989 }
1990
1991 if (rt->dst.dev == dev) {
1992 netdev_dbg(dev, "circular route to %pI4\n",
1993 &dst->sin.sin_addr.s_addr);
1994 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001995 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001996 }
1997
1998 /* Bypass encapsulation if the destination is local */
1999 if (rt->rt_flags & RTCF_LOCAL &&
2000 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2001 struct vxlan_dev *dst_vxlan;
2002
2003 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002004 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002005 dst->sa.sa_family, dst_port,
2006 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002007 if (!dst_vxlan)
2008 goto tx_error;
2009 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2010 return;
2011 }
2012
2013 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2014 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Thomas Grafee122c72015-07-21 10:43:58 +02002015 md->vni = htonl(vni << 8);
David Miller79b16aa2015-04-05 22:19:09 -04002016 err = vxlan_xmit_skb(rt, sk, skb, fl4.saddr,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002017 dst->sin.sin_addr.s_addr, tos, ttl, df,
Thomas Grafee122c72015-07-21 10:43:58 +02002018 src_port, dst_port, md,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002019 !net_eq(vxlan->net, dev_net(vxlan->dev)),
Thomas Grafee122c72015-07-21 10:43:58 +02002020 flags);
Pravin B Shelar74f47272014-12-23 16:20:36 -08002021 if (err < 0) {
2022 /* skb is already freed. */
2023 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002024 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08002025 }
2026
Cong Wange4c7ed42013-08-31 13:44:33 +08002027 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
2028#if IS_ENABLED(CONFIG_IPV6)
2029 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08002030 struct dst_entry *ndst;
2031 struct flowi6 fl6;
2032 u32 flags;
2033
2034 memset(&fl6, 0, sizeof(fl6));
Thomas Grafee122c72015-07-21 10:43:58 +02002035 fl6.flowi6_oif = rdst ? rdst->remote_ifindex : 0;
Cong Wange4c7ed42013-08-31 13:44:33 +08002036 fl6.daddr = dst->sin6.sin6_addr;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002037 fl6.saddr = vxlan->cfg.saddr.sin6.sin6_addr;
Thomas Graf239fb792015-05-05 15:09:21 +02002038 fl6.flowi6_mark = skb->mark;
Cong Wang8c1bb792013-09-02 10:06:51 +08002039 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08002040
2041 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2042 netdev_dbg(dev, "no route to %pI6\n",
2043 &dst->sin6.sin6_addr);
2044 dev->stats.tx_carrier_errors++;
2045 goto tx_error;
2046 }
2047
2048 if (ndst->dev == dev) {
2049 netdev_dbg(dev, "circular route to %pI6\n",
2050 &dst->sin6.sin6_addr);
2051 dst_release(ndst);
2052 dev->stats.collisions++;
2053 goto tx_error;
2054 }
2055
2056 /* Bypass encapsulation if the destination is local */
2057 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2058 if (flags & RTF_LOCAL &&
2059 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2060 struct vxlan_dev *dst_vxlan;
2061
2062 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002063 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002064 dst->sa.sa_family, dst_port,
2065 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002066 if (!dst_vxlan)
2067 goto tx_error;
2068 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2069 return;
2070 }
2071
2072 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Thomas Grafee122c72015-07-21 10:43:58 +02002073 md->vni = htonl(vni << 8);
2074 md->gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08002075
David Miller79b16aa2015-04-05 22:19:09 -04002076 err = vxlan6_xmit_skb(ndst, sk, skb, dev, &fl6.saddr, &fl6.daddr,
Thomas Grafee122c72015-07-21 10:43:58 +02002077 0, ttl, src_port, dst_port, md,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002078 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2079 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002080#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002081 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002082
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002083 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002084
2085drop:
2086 dev->stats.tx_dropped++;
2087 goto tx_free;
2088
Pravin B Shelar49560532013-08-19 11:23:17 -07002089rt_tx_error:
2090 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002091tx_error:
2092 dev->stats.tx_errors++;
2093tx_free:
2094 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002095}
2096
David Stevens66817122013-03-15 04:35:51 +00002097/* Transmit local packets over Vxlan
2098 *
2099 * Outer IP header inherits ECN and DF from inner header.
2100 * Outer UDP destination is the VXLAN assigned port.
2101 * source port is based on hash of flow
2102 */
2103static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2104{
2105 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Graf3093fbe2015-07-21 10:44:00 +02002106 const struct ip_tunnel_info *info;
David Stevens66817122013-03-15 04:35:51 +00002107 struct ethhdr *eth;
2108 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002109 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002110 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002111
Thomas Graf3093fbe2015-07-21 10:44:00 +02002112 /* FIXME: Support IPv6 */
2113 info = skb_tunnel_info(skb, AF_INET);
2114
David Stevens66817122013-03-15 04:35:51 +00002115 skb_reset_mac_header(skb);
2116 eth = eth_hdr(skb);
2117
Cong Wangf564f452013-08-31 13:44:36 +08002118 if ((vxlan->flags & VXLAN_F_PROXY)) {
2119 if (ntohs(eth->h_proto) == ETH_P_ARP)
2120 return arp_reduce(dev, skb);
2121#if IS_ENABLED(CONFIG_IPV6)
2122 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002123 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2124 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002125 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2126 struct nd_msg *msg;
2127
2128 msg = (struct nd_msg *)skb_transport_header(skb);
2129 if (msg->icmph.icmp6_code == 0 &&
2130 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2131 return neigh_reduce(dev, skb);
2132 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002133 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002134#endif
2135 }
David Stevens66817122013-03-15 04:35:51 +00002136
Thomas Grafee122c72015-07-21 10:43:58 +02002137 if (vxlan->flags & VXLAN_F_FLOW_BASED &&
2138 info && info->mode == IP_TUNNEL_INFO_TX) {
2139 vxlan_xmit_one(skb, dev, NULL, false);
2140 return NETDEV_TX_OK;
2141 }
2142
David Stevens66817122013-03-15 04:35:51 +00002143 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002144 did_rsc = false;
2145
2146 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002147 (ntohs(eth->h_proto) == ETH_P_IP ||
2148 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002149 did_rsc = route_shortcircuit(dev, skb);
2150 if (did_rsc)
2151 f = vxlan_find_mac(vxlan, eth->h_dest);
2152 }
2153
David Stevens66817122013-03-15 04:35:51 +00002154 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002155 f = vxlan_find_mac(vxlan, all_zeros_mac);
2156 if (f == NULL) {
2157 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2158 !is_multicast_ether_addr(eth->h_dest))
2159 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002160
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002161 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002162 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002163 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002164 }
David Stevens66817122013-03-15 04:35:51 +00002165 }
2166
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002167 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2168 struct sk_buff *skb1;
2169
Eric Dumazet8f646c92014-01-06 09:54:31 -08002170 if (!fdst) {
2171 fdst = rdst;
2172 continue;
2173 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002174 skb1 = skb_clone(skb, GFP_ATOMIC);
2175 if (skb1)
2176 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2177 }
2178
Eric Dumazet8f646c92014-01-06 09:54:31 -08002179 if (fdst)
2180 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2181 else
2182 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002183 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002184}
2185
stephen hemmingerd3428942012-10-01 12:32:35 +00002186/* Walk the forwarding table and purge stale entries */
2187static void vxlan_cleanup(unsigned long arg)
2188{
2189 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2190 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2191 unsigned int h;
2192
2193 if (!netif_running(vxlan->dev))
2194 return;
2195
stephen hemmingerd3428942012-10-01 12:32:35 +00002196 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2197 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002198
2199 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002200 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2201 struct vxlan_fdb *f
2202 = container_of(p, struct vxlan_fdb, hlist);
2203 unsigned long timeout;
2204
stephen hemminger3c172862012-10-26 06:24:34 +00002205 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002206 continue;
2207
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002208 timeout = f->used + vxlan->cfg.age_interval * HZ;
stephen hemmingerd3428942012-10-01 12:32:35 +00002209 if (time_before_eq(timeout, jiffies)) {
2210 netdev_dbg(vxlan->dev,
2211 "garbage collect %pM\n",
2212 f->eth_addr);
2213 f->state = NUD_STALE;
2214 vxlan_fdb_destroy(vxlan, f);
2215 } else if (time_before(timeout, next_timer))
2216 next_timer = timeout;
2217 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002218 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002219 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002220
2221 mod_timer(&vxlan->age_timer, next_timer);
2222}
2223
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002224static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2225{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002226 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002227 __u32 vni = vxlan->default_dst.remote_vni;
2228
2229 vxlan->vn_sock = vs;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002230 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002231 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002232 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002233}
2234
stephen hemmingerd3428942012-10-01 12:32:35 +00002235/* Setup stats when device is created */
2236static int vxlan_init(struct net_device *dev)
2237{
WANG Cong1c213bd2014-02-13 11:46:28 -08002238 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002239 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002240 return -ENOMEM;
2241
2242 return 0;
2243}
2244
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002245static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002246{
2247 struct vxlan_fdb *f;
2248
2249 spin_lock_bh(&vxlan->hash_lock);
2250 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2251 if (f)
2252 vxlan_fdb_destroy(vxlan, f);
2253 spin_unlock_bh(&vxlan->hash_lock);
2254}
2255
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002256static void vxlan_uninit(struct net_device *dev)
2257{
2258 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002259
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002260 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002261
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002262 free_percpu(dev->tstats);
2263}
2264
stephen hemmingerd3428942012-10-01 12:32:35 +00002265/* Start ageing timer and join group when device is brought up */
2266static int vxlan_open(struct net_device *dev)
2267{
2268 struct vxlan_dev *vxlan = netdev_priv(dev);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002269 struct vxlan_sock *vs;
2270 int ret = 0;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002271
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002272 vs = vxlan_sock_add(vxlan->net, vxlan->cfg.dst_port, vxlan_rcv,
2273 NULL, vxlan->cfg.no_share, vxlan->flags);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002274 if (IS_ERR(vs))
2275 return PTR_ERR(vs);
2276
2277 vxlan_vs_add_dev(vs, vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002278
Gao feng79d4a942013-12-10 16:37:32 +08002279 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002280 ret = vxlan_igmp_join(vxlan);
2281 if (ret) {
2282 vxlan_sock_release(vs);
2283 return ret;
2284 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002285 }
2286
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002287 if (vxlan->cfg.age_interval)
stephen hemmingerd3428942012-10-01 12:32:35 +00002288 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2289
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002290 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002291}
2292
2293/* Purge the forwarding table */
2294static void vxlan_flush(struct vxlan_dev *vxlan)
2295{
Cong Wang31fec5a2013-05-27 22:35:52 +00002296 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002297
2298 spin_lock_bh(&vxlan->hash_lock);
2299 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2300 struct hlist_node *p, *n;
2301 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2302 struct vxlan_fdb *f
2303 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002304 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2305 if (!is_zero_ether_addr(f->eth_addr))
2306 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002307 }
2308 }
2309 spin_unlock_bh(&vxlan->hash_lock);
2310}
2311
2312/* Cleanup timer and forwarding table on shutdown */
2313static int vxlan_stop(struct net_device *dev)
2314{
2315 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002316 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002317 struct vxlan_sock *vs = vxlan->vn_sock;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002318 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002319
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002320 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002321 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002322 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002323
2324 del_timer_sync(&vxlan->age_timer);
2325
2326 vxlan_flush(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002327 vxlan_sock_release(vs);
stephen hemmingerd3428942012-10-01 12:32:35 +00002328
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002329 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002330}
2331
stephen hemmingerd3428942012-10-01 12:32:35 +00002332/* Stub, nothing needs to be done. */
2333static void vxlan_set_multicast_list(struct net_device *dev)
2334{
2335}
2336
Daniel Borkmann345010b2013-12-18 00:21:08 +01002337static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2338{
2339 struct vxlan_dev *vxlan = netdev_priv(dev);
2340 struct vxlan_rdst *dst = &vxlan->default_dst;
2341 struct net_device *lowerdev;
2342 int max_mtu;
2343
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002344 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002345 if (lowerdev == NULL)
2346 return eth_change_mtu(dev, new_mtu);
2347
2348 if (dst->remote_ip.sa.sa_family == AF_INET6)
2349 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2350 else
2351 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2352
2353 if (new_mtu < 68 || new_mtu > max_mtu)
2354 return -EINVAL;
2355
2356 dev->mtu = new_mtu;
2357 return 0;
2358}
2359
stephen hemmingerd3428942012-10-01 12:32:35 +00002360static const struct net_device_ops vxlan_netdev_ops = {
2361 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002362 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002363 .ndo_open = vxlan_open,
2364 .ndo_stop = vxlan_stop,
2365 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002366 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002367 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002368 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002369 .ndo_validate_addr = eth_validate_addr,
2370 .ndo_set_mac_address = eth_mac_addr,
2371 .ndo_fdb_add = vxlan_fdb_add,
2372 .ndo_fdb_del = vxlan_fdb_delete,
2373 .ndo_fdb_dump = vxlan_fdb_dump,
2374};
2375
2376/* Info for udev, that this is a virtual tunnel endpoint */
2377static struct device_type vxlan_type = {
2378 .name = "vxlan",
2379};
2380
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002381/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002382 * supply the listening VXLAN udp ports. Callers are expected
2383 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002384 */
2385void vxlan_get_rx_port(struct net_device *dev)
2386{
2387 struct vxlan_sock *vs;
2388 struct net *net = dev_net(dev);
2389 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2390 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002391 __be16 port;
2392 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002393
2394 spin_lock(&vn->sock_lock);
2395 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002396 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2397 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002398 sa_family = vs->sock->sk->sk_family;
2399 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2400 port);
2401 }
2402 }
2403 spin_unlock(&vn->sock_lock);
2404}
2405EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2406
stephen hemmingerd3428942012-10-01 12:32:35 +00002407/* Initialize the device structure. */
2408static void vxlan_setup(struct net_device *dev)
2409{
2410 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002411 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002412
2413 eth_hw_addr_random(dev);
2414 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002415 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002416 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002417 else
Cong Wang2853af62014-06-12 11:53:10 -07002418 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002419
2420 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002421 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002422 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2423
2424 dev->tx_queue_len = 0;
2425 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002426 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002427 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002428 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002429
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002430 dev->vlan_features = dev->features;
2431 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002432 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002433 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002434 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002435 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002436 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002437
stephen hemminger553675f2013-05-16 11:35:20 +00002438 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002439 spin_lock_init(&vxlan->hash_lock);
2440
2441 init_timer_deferrable(&vxlan->age_timer);
2442 vxlan->age_timer.function = vxlan_cleanup;
2443 vxlan->age_timer.data = (unsigned long) vxlan;
2444
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002445 vxlan->cfg.dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002446
stephen hemmingerd3428942012-10-01 12:32:35 +00002447 vxlan->dev = dev;
2448
2449 for (h = 0; h < FDB_HASH_SIZE; ++h)
2450 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2451}
2452
2453static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2454 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002455 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002456 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002457 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2458 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002459 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002460 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2461 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2462 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2463 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2464 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002465 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002466 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2467 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2468 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2469 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Thomas Grafee122c72015-07-21 10:43:58 +02002470 [IFLA_VXLAN_FLOWBASED] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002471 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002472 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2473 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2474 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002475 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2476 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002477 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002478 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002479};
2480
2481static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2482{
2483 if (tb[IFLA_ADDRESS]) {
2484 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2485 pr_debug("invalid link address (not ethernet)\n");
2486 return -EINVAL;
2487 }
2488
2489 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2490 pr_debug("invalid all zero ethernet address\n");
2491 return -EADDRNOTAVAIL;
2492 }
2493 }
2494
2495 if (!data)
2496 return -EINVAL;
2497
2498 if (data[IFLA_VXLAN_ID]) {
2499 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2500 if (id >= VXLAN_VID_MASK)
2501 return -ERANGE;
2502 }
2503
stephen hemminger05f47d62012-10-09 20:35:50 +00002504 if (data[IFLA_VXLAN_PORT_RANGE]) {
2505 const struct ifla_vxlan_port_range *p
2506 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2507
2508 if (ntohs(p->high) < ntohs(p->low)) {
2509 pr_debug("port range %u .. %u not valid\n",
2510 ntohs(p->low), ntohs(p->high));
2511 return -EINVAL;
2512 }
2513 }
2514
stephen hemmingerd3428942012-10-01 12:32:35 +00002515 return 0;
2516}
2517
Yan Burman1b13c972013-01-29 23:43:07 +00002518static void vxlan_get_drvinfo(struct net_device *netdev,
2519 struct ethtool_drvinfo *drvinfo)
2520{
2521 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2522 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2523}
2524
2525static const struct ethtool_ops vxlan_ethtool_ops = {
2526 .get_drvinfo = vxlan_get_drvinfo,
2527 .get_link = ethtool_op_get_link,
2528};
2529
stephen hemminger553675f2013-05-16 11:35:20 +00002530static void vxlan_del_work(struct work_struct *work)
2531{
2532 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002533 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002534 kfree_rcu(vs, rcu);
2535}
2536
Tom Herbert3ee64f32014-07-13 19:49:42 -07002537static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2538 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002539{
Cong Wange4c7ed42013-08-31 13:44:33 +08002540 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002541 struct udp_port_cfg udp_conf;
2542 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002543
Tom Herbert3ee64f32014-07-13 19:49:42 -07002544 memset(&udp_conf, 0, sizeof(udp_conf));
2545
2546 if (ipv6) {
2547 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002548 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002549 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002550 } else {
2551 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002552 }
2553
Tom Herbert3ee64f32014-07-13 19:49:42 -07002554 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002555
Tom Herbert3ee64f32014-07-13 19:49:42 -07002556 /* Open UDP socket */
2557 err = udp_sock_create(net, &udp_conf, &sock);
2558 if (err < 0)
2559 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002560
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002561 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002562}
2563
2564/* Create new listen socket if needed */
2565static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002566 vxlan_rcv_t *rcv, void *data,
2567 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002568{
2569 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2570 struct vxlan_sock *vs;
2571 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002572 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002573 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002574 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002575
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002576 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002577 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002578 return ERR_PTR(-ENOMEM);
2579
2580 for (h = 0; h < VNI_HASH_SIZE; ++h)
2581 INIT_HLIST_HEAD(&vs->vni_list[h]);
2582
2583 INIT_WORK(&vs->del_work, vxlan_del_work);
2584
Tom Herbert3ee64f32014-07-13 19:49:42 -07002585 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002586 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002587 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2588 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002589 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002590 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002591 }
2592
Cong Wange4c7ed42013-08-31 13:44:33 +08002593 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002594 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002595 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002596 vs->data = data;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002597 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002598
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002599 /* Initialize the vxlan udp offloads structure */
2600 vs->udp_offloads.port = port;
2601 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2602 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2603
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002604 spin_lock(&vn->sock_lock);
2605 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002606 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002607 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002608
2609 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002610 tunnel_cfg.sk_user_data = vs;
2611 tunnel_cfg.encap_type = 1;
2612 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2613 tunnel_cfg.encap_destroy = NULL;
2614
2615 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002616
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002617 return vs;
2618}
stephen hemminger553675f2013-05-16 11:35:20 +00002619
Pravin B Shelar012a5722013-08-19 11:23:07 -07002620struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2621 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002622 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002623{
2624 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2625 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002626 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002627
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002628 if (!no_share) {
2629 spin_lock(&vn->sock_lock);
2630 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port,
2631 flags);
2632 if (vs && vs->rcv == rcv) {
2633 if (!atomic_add_unless(&vs->refcnt, 1, 0))
2634 vs = ERR_PTR(-EBUSY);
2635 spin_unlock(&vn->sock_lock);
2636 return vs;
2637 }
2638 spin_unlock(&vn->sock_lock);
2639 }
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002640
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002641 return vxlan_socket_create(net, port, rcv, data, flags);
stephen hemminger553675f2013-05-16 11:35:20 +00002642}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002643EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002644
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002645static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2646 struct vxlan_config *conf)
stephen hemmingerd3428942012-10-01 12:32:35 +00002647{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002648 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002649 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002650 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002651 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002652 bool use_ipv6 = false;
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002653 __be16 default_port = vxlan->cfg.dst_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00002654
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002655 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002656
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002657 dst->remote_vni = conf->vni;
2658
2659 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
stephen hemmingerd3428942012-10-01 12:32:35 +00002660
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002661 /* Unless IPv6 is explicitly requested, assume IPv4 */
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002662 if (!dst->remote_ip.sa.sa_family)
2663 dst->remote_ip.sa.sa_family = AF_INET;
stephen hemmingerd3428942012-10-01 12:32:35 +00002664
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002665 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
2666 vxlan->cfg.saddr.sa.sa_family == AF_INET6)
Cong Wange4c7ed42013-08-31 13:44:33 +08002667 use_ipv6 = true;
Cong Wange4c7ed42013-08-31 13:44:33 +08002668
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002669 if (conf->remote_ifindex) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002670 struct net_device *lowerdev
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002671 = __dev_get_by_index(src_net, conf->remote_ifindex);
2672
2673 dst->remote_ifindex = conf->remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00002674
stephen hemminger34e02aa2012-10-09 20:35:53 +00002675 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002676 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002677 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002678 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002679
Cong Wange4c7ed42013-08-31 13:44:33 +08002680#if IS_ENABLED(CONFIG_IPV6)
2681 if (use_ipv6) {
2682 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2683 if (idev && idev->cnf.disable_ipv6) {
2684 pr_info("IPv6 is disabled via sysctl\n");
2685 return -EPERM;
2686 }
2687 vxlan->flags |= VXLAN_F_IPV6;
2688 }
2689#endif
2690
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002691 if (!conf->mtu)
Cong Wange4c7ed42013-08-31 13:44:33 +08002692 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002693
Cong Wang2853af62014-06-12 11:53:10 -07002694 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002695 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002696 } else if (use_ipv6)
2697 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002698
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002699 memcpy(&vxlan->cfg, conf, sizeof(*conf));
2700 if (!vxlan->cfg.dst_port)
2701 vxlan->cfg.dst_port = default_port;
2702 vxlan->flags |= conf->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00002703
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002704 if (!vxlan->cfg.age_interval)
2705 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
Vincent Bernatafb97182012-10-30 10:27:16 +00002706
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002707 if (vxlan_find_vni(src_net, conf->vni, use_ipv6 ? AF_INET6 : AF_INET,
2708 vxlan->cfg.dst_port, vxlan->flags))
stephen hemminger553675f2013-05-16 11:35:20 +00002709 return -EEXIST;
stephen hemminger553675f2013-05-16 11:35:20 +00002710
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002711 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002712
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002713 /* create an fdb entry for a valid default destination */
2714 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2715 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2716 &vxlan->default_dst.remote_ip,
2717 NUD_REACHABLE|NUD_PERMANENT,
2718 NLM_F_EXCL|NLM_F_CREATE,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002719 vxlan->cfg.dst_port,
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002720 vxlan->default_dst.remote_vni,
2721 vxlan->default_dst.remote_ifindex,
2722 NTF_SELF);
2723 if (err)
2724 return err;
2725 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002726
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002727 err = register_netdevice(dev);
2728 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002729 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002730 return err;
2731 }
2732
stephen hemminger553675f2013-05-16 11:35:20 +00002733 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002734
2735 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002736}
2737
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002738struct net_device *vxlan_dev_create(struct net *net, const char *name,
2739 u8 name_assign_type, struct vxlan_config *conf)
2740{
2741 struct nlattr *tb[IFLA_MAX+1];
2742 struct net_device *dev;
2743 int err;
2744
2745 memset(&tb, 0, sizeof(tb));
2746
2747 dev = rtnl_create_link(net, name, name_assign_type,
2748 &vxlan_link_ops, tb);
2749 if (IS_ERR(dev))
2750 return dev;
2751
2752 err = vxlan_dev_configure(net, dev, conf);
2753 if (err < 0) {
2754 free_netdev(dev);
2755 return ERR_PTR(err);
2756 }
2757
2758 return dev;
2759}
2760EXPORT_SYMBOL_GPL(vxlan_dev_create);
2761
2762static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2763 struct nlattr *tb[], struct nlattr *data[])
2764{
2765 struct vxlan_config conf;
2766 int err;
2767
2768 if (!data[IFLA_VXLAN_ID])
2769 return -EINVAL;
2770
2771 memset(&conf, 0, sizeof(conf));
2772 conf.vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2773
2774 if (data[IFLA_VXLAN_GROUP]) {
2775 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
2776 } else if (data[IFLA_VXLAN_GROUP6]) {
2777 if (!IS_ENABLED(CONFIG_IPV6))
2778 return -EPFNOSUPPORT;
2779
2780 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
2781 conf.remote_ip.sa.sa_family = AF_INET6;
2782 }
2783
2784 if (data[IFLA_VXLAN_LOCAL]) {
2785 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
2786 conf.saddr.sa.sa_family = AF_INET;
2787 } else if (data[IFLA_VXLAN_LOCAL6]) {
2788 if (!IS_ENABLED(CONFIG_IPV6))
2789 return -EPFNOSUPPORT;
2790
2791 /* TODO: respect scope id */
2792 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
2793 conf.saddr.sa.sa_family = AF_INET6;
2794 }
2795
2796 if (data[IFLA_VXLAN_LINK])
2797 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
2798
2799 if (data[IFLA_VXLAN_TOS])
2800 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2801
2802 if (data[IFLA_VXLAN_TTL])
2803 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2804
2805 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2806 conf.flags |= VXLAN_F_LEARN;
2807
2808 if (data[IFLA_VXLAN_AGEING])
2809 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2810
2811 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2812 conf.flags |= VXLAN_F_PROXY;
2813
2814 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2815 conf.flags |= VXLAN_F_RSC;
2816
2817 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2818 conf.flags |= VXLAN_F_L2MISS;
2819
2820 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2821 conf.flags |= VXLAN_F_L3MISS;
2822
2823 if (data[IFLA_VXLAN_LIMIT])
2824 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2825
2826 if (data[IFLA_VXLAN_FLOWBASED] &&
2827 nla_get_u8(data[IFLA_VXLAN_FLOWBASED]))
2828 conf.flags |= VXLAN_F_FLOW_BASED;
2829
2830 if (data[IFLA_VXLAN_PORT_RANGE]) {
2831 const struct ifla_vxlan_port_range *p
2832 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2833 conf.port_min = ntohs(p->low);
2834 conf.port_max = ntohs(p->high);
2835 }
2836
2837 if (data[IFLA_VXLAN_PORT])
2838 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2839
2840 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2841 conf.flags |= VXLAN_F_UDP_CSUM;
2842
2843 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2844 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2845 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2846
2847 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2848 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2849 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2850
2851 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2852 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2853 conf.flags |= VXLAN_F_REMCSUM_TX;
2854
2855 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2856 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2857 conf.flags |= VXLAN_F_REMCSUM_RX;
2858
2859 if (data[IFLA_VXLAN_GBP])
2860 conf.flags |= VXLAN_F_GBP;
2861
2862 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
2863 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
2864
2865 err = vxlan_dev_configure(src_net, dev, &conf);
2866 switch (err) {
2867 case -ENODEV:
2868 pr_info("ifindex %d does not exist\n", conf.remote_ifindex);
2869 break;
2870
2871 case -EPERM:
2872 pr_info("IPv6 is disabled via sysctl\n");
2873 break;
2874
2875 case -EEXIST:
2876 pr_info("duplicate VNI %u\n", conf.vni);
2877 break;
2878 }
2879
2880 return err;
2881}
2882
stephen hemmingerd3428942012-10-01 12:32:35 +00002883static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2884{
2885 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002886 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002887
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002888 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002889 if (!hlist_unhashed(&vxlan->hlist))
2890 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002891 spin_unlock(&vn->sock_lock);
2892
stephen hemminger553675f2013-05-16 11:35:20 +00002893 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002894 unregister_netdevice_queue(dev, head);
2895}
2896
2897static size_t vxlan_get_size(const struct net_device *dev)
2898{
2899
2900 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002901 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002902 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002903 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002904 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2905 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2906 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002907 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2908 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2909 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2910 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Thomas Grafee122c72015-07-21 10:43:58 +02002911 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_FLOWBASED */
stephen hemmingerd3428942012-10-01 12:32:35 +00002912 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2913 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002914 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002915 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2916 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2917 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2918 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002919 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2920 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002921 0;
2922}
2923
2924static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2925{
2926 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002927 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002928 struct ifla_vxlan_port_range ports = {
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002929 .low = htons(vxlan->cfg.port_min),
2930 .high = htons(vxlan->cfg.port_max),
stephen hemminger05f47d62012-10-09 20:35:50 +00002931 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002932
Atzm Watanabec7995c42013-04-16 02:50:52 +00002933 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002934 goto nla_put_failure;
2935
Cong Wange4c7ed42013-08-31 13:44:33 +08002936 if (!vxlan_addr_any(&dst->remote_ip)) {
2937 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02002938 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
2939 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002940 goto nla_put_failure;
2941#if IS_ENABLED(CONFIG_IPV6)
2942 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02002943 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
2944 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002945 goto nla_put_failure;
2946#endif
2947 }
2948 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002949
Atzm Watanabec7995c42013-04-16 02:50:52 +00002950 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002951 goto nla_put_failure;
2952
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002953 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
2954 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02002955 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002956 vxlan->cfg.saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002957 goto nla_put_failure;
2958#if IS_ENABLED(CONFIG_IPV6)
2959 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02002960 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002961 &vxlan->cfg.saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002962 goto nla_put_failure;
2963#endif
2964 }
2965 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002966
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002967 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
2968 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002969 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2970 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2971 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2972 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2973 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2974 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2975 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2976 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2977 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Thomas Grafee122c72015-07-21 10:43:58 +02002978 nla_put_u8(skb, IFLA_VXLAN_FLOWBASED,
2979 !!(vxlan->flags & VXLAN_F_FLOW_BASED)) ||
Thomas Graf0dfbdf42015-07-21 10:44:02 +02002980 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
2981 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
2982 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002983 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2984 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2985 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2986 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2987 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002988 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2989 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2990 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2991 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2992 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002993 goto nla_put_failure;
2994
stephen hemminger05f47d62012-10-09 20:35:50 +00002995 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2996 goto nla_put_failure;
2997
Thomas Graf35114942015-01-15 03:53:55 +01002998 if (vxlan->flags & VXLAN_F_GBP &&
2999 nla_put_flag(skb, IFLA_VXLAN_GBP))
3000 goto nla_put_failure;
3001
Tom Herbert0ace2ca2015-02-10 16:30:32 -08003002 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3003 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3004 goto nla_put_failure;
3005
stephen hemmingerd3428942012-10-01 12:32:35 +00003006 return 0;
3007
3008nla_put_failure:
3009 return -EMSGSIZE;
3010}
3011
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003012static struct net *vxlan_get_link_net(const struct net_device *dev)
3013{
3014 struct vxlan_dev *vxlan = netdev_priv(dev);
3015
3016 return vxlan->net;
3017}
3018
stephen hemmingerd3428942012-10-01 12:32:35 +00003019static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3020 .kind = "vxlan",
3021 .maxtype = IFLA_VXLAN_MAX,
3022 .policy = vxlan_policy,
3023 .priv_size = sizeof(struct vxlan_dev),
3024 .setup = vxlan_setup,
3025 .validate = vxlan_validate,
3026 .newlink = vxlan_newlink,
3027 .dellink = vxlan_dellink,
3028 .get_size = vxlan_get_size,
3029 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003030 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003031};
3032
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003033static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3034 struct net_device *dev)
3035{
3036 struct vxlan_dev *vxlan, *next;
3037 LIST_HEAD(list_kill);
3038
3039 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3040 struct vxlan_rdst *dst = &vxlan->default_dst;
3041
3042 /* In case we created vxlan device with carrier
3043 * and we loose the carrier due to module unload
3044 * we also need to remove vxlan device. In other
3045 * cases, it's not necessary and remote_ifindex
3046 * is 0 here, so no matches.
3047 */
3048 if (dst->remote_ifindex == dev->ifindex)
3049 vxlan_dellink(vxlan->dev, &list_kill);
3050 }
3051
3052 unregister_netdevice_many(&list_kill);
3053}
3054
3055static int vxlan_lowerdev_event(struct notifier_block *unused,
3056 unsigned long event, void *ptr)
3057{
3058 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003059 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003060
Daniel Borkmann783c1462014-01-22 21:07:53 +01003061 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003062 vxlan_handle_lowerdev_unregister(vn, dev);
3063
3064 return NOTIFY_DONE;
3065}
3066
3067static struct notifier_block vxlan_notifier_block __read_mostly = {
3068 .notifier_call = vxlan_lowerdev_event,
3069};
3070
stephen hemmingerd3428942012-10-01 12:32:35 +00003071static __net_init int vxlan_init_net(struct net *net)
3072{
3073 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003074 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003075
stephen hemminger553675f2013-05-16 11:35:20 +00003076 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003077 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003078
stephen hemminger553675f2013-05-16 11:35:20 +00003079 for (h = 0; h < PORT_HASH_SIZE; ++h)
3080 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003081
3082 return 0;
3083}
3084
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003085static void __net_exit vxlan_exit_net(struct net *net)
3086{
3087 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3088 struct vxlan_dev *vxlan, *next;
3089 struct net_device *dev, *aux;
3090 LIST_HEAD(list);
3091
3092 rtnl_lock();
3093 for_each_netdev_safe(net, dev, aux)
3094 if (dev->rtnl_link_ops == &vxlan_link_ops)
3095 unregister_netdevice_queue(dev, &list);
3096
3097 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3098 /* If vxlan->dev is in the same netns, it has already been added
3099 * to the list by the previous loop.
3100 */
3101 if (!net_eq(dev_net(vxlan->dev), net))
John W. Linville13c3ed62015-05-18 13:51:24 -04003102 unregister_netdevice_queue(vxlan->dev, &list);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003103 }
3104
3105 unregister_netdevice_many(&list);
3106 rtnl_unlock();
3107}
3108
stephen hemmingerd3428942012-10-01 12:32:35 +00003109static struct pernet_operations vxlan_net_ops = {
3110 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003111 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003112 .id = &vxlan_net_id,
3113 .size = sizeof(struct vxlan_net),
3114};
3115
3116static int __init vxlan_init_module(void)
3117{
3118 int rc;
3119
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003120 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3121 if (!vxlan_wq)
3122 return -ENOMEM;
3123
stephen hemmingerd3428942012-10-01 12:32:35 +00003124 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3125
Daniel Borkmann783c1462014-01-22 21:07:53 +01003126 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003127 if (rc)
3128 goto out1;
3129
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003130 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003131 if (rc)
3132 goto out2;
3133
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003134 rc = rtnl_link_register(&vxlan_link_ops);
3135 if (rc)
3136 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003137
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003138 return 0;
3139out3:
3140 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003141out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003142 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003143out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003144 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00003145 return rc;
3146}
Cong Wang7332a132013-05-27 22:35:53 +00003147late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003148
3149static void __exit vxlan_cleanup_module(void)
3150{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003151 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003152 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003153 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003154 unregister_pernet_subsys(&vxlan_net_ops);
3155 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003156}
3157module_exit(vxlan_cleanup_module);
3158
3159MODULE_LICENSE("GPL");
3160MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003161MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003162MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003163MODULE_ALIAS_RTNL_LINK("vxlan");