blob: 19d3664ab9dd87c19077e47311e2872f016c725e [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
stephen hemmingerd3428942012-10-01 12:32:35 +000052
53#define VXLAN_VERSION "0.1"
54
stephen hemminger553675f2013-05-16 11:35:20 +000055#define PORT_HASH_BITS 8
56#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000057#define VNI_HASH_BITS 10
58#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
59#define FDB_HASH_BITS 8
60#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
61#define FDB_AGE_DEFAULT 300 /* 5 min */
62#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
63
stephen hemminger23c578b2013-04-27 11:31:53 +000064/* UDP port for VXLAN traffic.
65 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070066 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000067 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070068static unsigned short vxlan_port __read_mostly = 8472;
69module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000070MODULE_PARM_DESC(udp_port, "Destination UDP port");
71
72static bool log_ecn_error = true;
73module_param(log_ecn_error, bool, 0644);
74MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
75
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070076static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000077
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030078static const u8 all_zeros_mac[ETH_ALEN];
79
stephen hemminger553675f2013-05-16 11:35:20 +000080/* per-network namespace private data for this module */
81struct vxlan_net {
82 struct list_head vxlan_list;
83 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070084 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000085};
86
Cong Wange4c7ed42013-08-31 13:44:33 +080087union vxlan_addr {
88 struct sockaddr_in sin;
89 struct sockaddr_in6 sin6;
90 struct sockaddr sa;
91};
92
David Stevens66817122013-03-15 04:35:51 +000093struct vxlan_rdst {
Cong Wange4c7ed42013-08-31 13:44:33 +080094 union vxlan_addr remote_ip;
David Stevens66817122013-03-15 04:35:51 +000095 __be16 remote_port;
96 u32 remote_vni;
97 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070098 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +030099 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000100};
101
stephen hemmingerd3428942012-10-01 12:32:35 +0000102/* Forwarding table entry */
103struct vxlan_fdb {
104 struct hlist_node hlist; /* linked list of entries */
105 struct rcu_head rcu;
106 unsigned long updated; /* jiffies */
107 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700108 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000109 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000110 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000111 u8 eth_addr[ETH_ALEN];
112};
113
stephen hemmingerd3428942012-10-01 12:32:35 +0000114/* Pseudo network device */
115struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000116 struct hlist_node hlist; /* vni hash table */
117 struct list_head next; /* vxlan's per namespace list */
118 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000119 struct net_device *dev;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +0200120 struct net *net; /* netns for packet i/o */
Atzm Watanabec7995c42013-04-16 02:50:52 +0000121 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800122 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000123 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000124 __u16 port_min; /* source port range */
125 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000126 __u8 tos; /* TOS override */
127 __u8 ttl;
Tom Herbert359a0ea2014-06-04 17:20:29 -0700128 u32 flags; /* VXLAN_F_* in vxlan.h */
stephen hemmingerd3428942012-10-01 12:32:35 +0000129
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700130 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700131 struct work_struct igmp_join;
132 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700133
stephen hemmingerd3428942012-10-01 12:32:35 +0000134 unsigned long age_interval;
135 struct timer_list age_timer;
136 spinlock_t hash_lock;
137 unsigned int addrcnt;
138 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000139
140 struct hlist_head fdb_head[FDB_HASH_SIZE];
141};
142
143/* salt for hash table */
144static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700145static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000146
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700147static void vxlan_sock_work(struct work_struct *work);
148
Cong Wange4c7ed42013-08-31 13:44:33 +0800149#if IS_ENABLED(CONFIG_IPV6)
150static inline
151bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
152{
153 if (a->sa.sa_family != b->sa.sa_family)
154 return false;
155 if (a->sa.sa_family == AF_INET6)
156 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
157 else
158 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
159}
160
161static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
162{
163 if (ipa->sa.sa_family == AF_INET6)
164 return ipv6_addr_any(&ipa->sin6.sin6_addr);
165 else
166 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
167}
168
169static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
170{
171 if (ipa->sa.sa_family == AF_INET6)
172 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
173 else
174 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
175}
176
177static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
178{
179 if (nla_len(nla) >= sizeof(struct in6_addr)) {
180 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
181 ip->sa.sa_family = AF_INET6;
182 return 0;
183 } else if (nla_len(nla) >= sizeof(__be32)) {
184 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
185 ip->sa.sa_family = AF_INET;
186 return 0;
187 } else {
188 return -EAFNOSUPPORT;
189 }
190}
191
192static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
193 const union vxlan_addr *ip)
194{
195 if (ip->sa.sa_family == AF_INET6)
196 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
197 else
198 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
199}
200
201#else /* !CONFIG_IPV6 */
202
203static inline
204bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
205{
206 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
207}
208
209static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
210{
211 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
212}
213
214static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
215{
216 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
217}
218
219static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
220{
221 if (nla_len(nla) >= sizeof(struct in6_addr)) {
222 return -EAFNOSUPPORT;
223 } else if (nla_len(nla) >= sizeof(__be32)) {
224 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
225 ip->sa.sa_family = AF_INET;
226 return 0;
227 } else {
228 return -EAFNOSUPPORT;
229 }
230}
231
232static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
233 const union vxlan_addr *ip)
234{
235 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
236}
237#endif
238
stephen hemminger553675f2013-05-16 11:35:20 +0000239/* Virtual Network hash table head */
240static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
241{
242 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
243}
244
245/* Socket hash table head */
246static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000247{
248 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
249
stephen hemminger553675f2013-05-16 11:35:20 +0000250 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
251}
252
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700253/* First remote destination for a forwarding entry.
254 * Guaranteed to be non-NULL because remotes are never deleted.
255 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700256static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700257{
stephen hemminger5ca54612013-08-04 17:17:39 -0700258 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
259}
260
261static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
262{
263 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700264}
265
Thomas Grafac5132d2015-01-15 03:53:56 +0100266/* Find VXLAN socket based on network namespace, address family and UDP port
267 * and enabled unshareable flags.
268 */
269static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
270 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000271{
272 struct vxlan_sock *vs;
Thomas Grafac5132d2015-01-15 03:53:56 +0100273 u32 match_flags = flags & VXLAN_F_UNSHAREABLE;
stephen hemminger553675f2013-05-16 11:35:20 +0000274
275 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200276 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Thomas Grafac5132d2015-01-15 03:53:56 +0100277 inet_sk(vs->sock->sk)->sk.sk_family == family &&
278 (vs->flags & VXLAN_F_UNSHAREABLE) == match_flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000279 return vs;
280 }
281 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000282}
283
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700284static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000285{
286 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000287
stephen hemminger553675f2013-05-16 11:35:20 +0000288 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000289 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000290 return vxlan;
291 }
292
293 return NULL;
294}
295
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700296/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200297static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
Thomas Grafac5132d2015-01-15 03:53:56 +0100298 sa_family_t family, __be16 port,
299 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700300{
301 struct vxlan_sock *vs;
302
Thomas Grafac5132d2015-01-15 03:53:56 +0100303 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700304 if (!vs)
305 return NULL;
306
307 return vxlan_vs_find_vni(vs, id);
308}
309
stephen hemmingerd3428942012-10-01 12:32:35 +0000310/* Fill in neighbour message in skbuff. */
311static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700312 const struct vxlan_fdb *fdb,
313 u32 portid, u32 seq, int type, unsigned int flags,
314 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000315{
316 unsigned long now = jiffies;
317 struct nda_cacheinfo ci;
318 struct nlmsghdr *nlh;
319 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000320 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000321
322 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
323 if (nlh == NULL)
324 return -EMSGSIZE;
325
326 ndm = nlmsg_data(nlh);
327 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000328
329 send_eth = send_ip = true;
330
331 if (type == RTM_GETNEIGH) {
332 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800333 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000334 send_eth = !is_zero_ether_addr(fdb->eth_addr);
335 } else
336 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000337 ndm->ndm_state = fdb->state;
338 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000339 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800340 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000341
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100342 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
343 nla_put_s32(skb, NDA_NDM_IFINDEX_NETNSID,
344 peernet2id(vxlan->net, dev_net(vxlan->dev))))
345 goto nla_put_failure;
346
David Stevense4f67ad2012-11-20 02:50:14 +0000347 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000348 goto nla_put_failure;
349
Cong Wange4c7ed42013-08-31 13:44:33 +0800350 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000351 goto nla_put_failure;
352
stephen hemminger823aa872013-04-27 11:31:57 +0000353 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000354 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
355 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000356 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700357 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000358 goto nla_put_failure;
359 if (rdst->remote_ifindex &&
360 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000361 goto nla_put_failure;
362
363 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
364 ci.ndm_confirmed = 0;
365 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
366 ci.ndm_refcnt = 0;
367
368 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
369 goto nla_put_failure;
370
Johannes Berg053c0952015-01-16 22:09:00 +0100371 nlmsg_end(skb, nlh);
372 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000373
374nla_put_failure:
375 nlmsg_cancel(skb, nlh);
376 return -EMSGSIZE;
377}
378
379static inline size_t vxlan_nlmsg_size(void)
380{
381 return NLMSG_ALIGN(sizeof(struct ndmsg))
382 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800383 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000384 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000385 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
386 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000387 + nla_total_size(sizeof(struct nda_cacheinfo));
388}
389
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200390static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
391 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000392{
393 struct net *net = dev_net(vxlan->dev);
394 struct sk_buff *skb;
395 int err = -ENOBUFS;
396
397 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
398 if (skb == NULL)
399 goto errout;
400
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200401 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000402 if (err < 0) {
403 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
404 WARN_ON(err == -EMSGSIZE);
405 kfree_skb(skb);
406 goto errout;
407 }
408
409 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
410 return;
411errout:
412 if (err < 0)
413 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
414}
415
Cong Wange4c7ed42013-08-31 13:44:33 +0800416static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000417{
418 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700419 struct vxlan_fdb f = {
420 .state = NUD_STALE,
421 };
422 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800423 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700424 .remote_vni = VXLAN_N_VID,
425 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700426
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200427 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000428}
429
430static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
431{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700432 struct vxlan_fdb f = {
433 .state = NUD_STALE,
434 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200435 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000436
David Stevense4f67ad2012-11-20 02:50:14 +0000437 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
438
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200439 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000440}
441
stephen hemmingerd3428942012-10-01 12:32:35 +0000442/* Hash Ethernet address */
443static u32 eth_hash(const unsigned char *addr)
444{
445 u64 value = get_unaligned((u64 *)addr);
446
447 /* only want 6 bytes */
448#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000449 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000450#else
451 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000452#endif
453 return hash_64(value, FDB_HASH_BITS);
454}
455
456/* Hash chain to use given mac address */
457static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
458 const u8 *mac)
459{
460 return &vxlan->fdb_head[eth_hash(mac)];
461}
462
463/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000464static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000465 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000466{
467 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
468 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000469
Sasha Levinb67bfe02013-02-27 17:06:00 -0800470 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700471 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000472 return f;
473 }
474
475 return NULL;
476}
477
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000478static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
479 const u8 *mac)
480{
481 struct vxlan_fdb *f;
482
483 f = __vxlan_find_mac(vxlan, mac);
484 if (f)
485 f->used = jiffies;
486
487 return f;
488}
489
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300490/* caller should hold vxlan->hash_lock */
491static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800492 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300493 __u32 vni, __u32 ifindex)
494{
495 struct vxlan_rdst *rd;
496
497 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800498 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300499 rd->remote_port == port &&
500 rd->remote_vni == vni &&
501 rd->remote_ifindex == ifindex)
502 return rd;
503 }
504
505 return NULL;
506}
507
Thomas Richter906dc182013-07-19 17:20:07 +0200508/* Replace destination of unicast mac */
509static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800510 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200511{
512 struct vxlan_rdst *rd;
513
514 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
515 if (rd)
516 return 0;
517
518 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
519 if (!rd)
520 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800521 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200522 rd->remote_port = port;
523 rd->remote_vni = vni;
524 rd->remote_ifindex = ifindex;
525 return 1;
526}
527
David Stevens66817122013-03-15 04:35:51 +0000528/* Add/update destinations for multicast */
529static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200530 union vxlan_addr *ip, __be16 port, __u32 vni,
531 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000532{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700533 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000534
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300535 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
536 if (rd)
537 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700538
David Stevens66817122013-03-15 04:35:51 +0000539 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
540 if (rd == NULL)
541 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800542 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000543 rd->remote_port = port;
544 rd->remote_vni = vni;
545 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700546
547 list_add_tail_rcu(&rd->list, &f->remotes);
548
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200549 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000550 return 1;
551}
552
Tom Herbertdfd86452015-01-12 17:00:38 -0800553static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
554 unsigned int off,
555 struct vxlanhdr *vh, size_t hdrlen,
556 u32 data)
557{
558 size_t start, offset, plen;
559 __wsum delta;
560
561 if (skb->remcsum_offload)
562 return vh;
563
564 if (!NAPI_GRO_CB(skb)->csum_valid)
565 return NULL;
566
567 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
568 offset = start + ((data & VXLAN_RCO_UDP) ?
569 offsetof(struct udphdr, check) :
570 offsetof(struct tcphdr, check));
571
572 plen = hdrlen + offset + sizeof(u16);
573
574 /* Pull checksum that will be written */
575 if (skb_gro_header_hard(skb, off + plen)) {
576 vh = skb_gro_header_slow(skb, off + plen, off);
577 if (!vh)
578 return NULL;
579 }
580
581 delta = remcsum_adjust((void *)vh + hdrlen,
582 NAPI_GRO_CB(skb)->csum, start, offset);
583
584 /* Adjust skb->csum since we changed the packet */
585 skb->csum = csum_add(skb->csum, delta);
586 NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta);
587
588 skb->remcsum_offload = 1;
589
590 return vh;
591}
592
Tom Herberta2b12f32015-01-12 17:00:37 -0800593static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
594 struct sk_buff *skb,
595 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200596{
597 struct sk_buff *p, **pp = NULL;
598 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800599 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200600 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800601 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
602 udp_offloads);
603 u32 flags;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200604
605 off_vx = skb_gro_offset(skb);
606 hlen = off_vx + sizeof(*vh);
607 vh = skb_gro_header_fast(skb, off_vx);
608 if (skb_gro_header_hard(skb, hlen)) {
609 vh = skb_gro_header_slow(skb, hlen, off_vx);
610 if (unlikely(!vh))
611 goto out;
612 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200613
Tom Herbertdfd86452015-01-12 17:00:38 -0800614 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
615 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
616
617 flags = ntohl(vh->vx_flags);
618
619 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
620 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
621 ntohl(vh->vx_vni));
622
623 if (!vh)
624 goto out;
625 }
626
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200627 flush = 0;
628
629 for (p = *head; p; p = p->next) {
630 if (!NAPI_GRO_CB(p)->same_flow)
631 continue;
632
633 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100634 if (vh->vx_flags != vh2->vx_flags ||
635 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200636 NAPI_GRO_CB(p)->same_flow = 0;
637 continue;
638 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200639 }
640
Jesse Gross9b174d82014-12-30 19:10:15 -0800641 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200642
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200643out:
644 NAPI_GRO_CB(skb)->flush |= flush;
645
646 return pp;
647}
648
Tom Herberta2b12f32015-01-12 17:00:37 -0800649static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
650 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200651{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800652 udp_tunnel_gro_complete(skb, nhoff);
653
Jesse Gross9b174d82014-12-30 19:10:15 -0800654 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200655}
656
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700657/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200658static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700659{
660 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200661 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700662 struct net *net = sock_net(sk);
663 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700664 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200665 int err;
666
667 if (sa_family == AF_INET) {
668 err = udp_add_offload(&vs->udp_offloads);
669 if (err)
670 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
671 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700672
673 rcu_read_lock();
674 for_each_netdev_rcu(net, dev) {
675 if (dev->netdev_ops->ndo_add_vxlan_port)
676 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
677 port);
678 }
679 rcu_read_unlock();
680}
681
682/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200683static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700684{
685 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200686 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700687 struct net *net = sock_net(sk);
688 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700689 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700690
691 rcu_read_lock();
692 for_each_netdev_rcu(net, dev) {
693 if (dev->netdev_ops->ndo_del_vxlan_port)
694 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
695 port);
696 }
697 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200698
699 if (sa_family == AF_INET)
700 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700701}
702
stephen hemmingerd3428942012-10-01 12:32:35 +0000703/* Add new entry to forwarding table -- assumes lock held */
704static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800705 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000706 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000707 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000708 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000709{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200710 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000711 struct vxlan_fdb *f;
712 int notify = 0;
713
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000714 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000715 if (f) {
716 if (flags & NLM_F_EXCL) {
717 netdev_dbg(vxlan->dev,
718 "lost race to create %pM\n", mac);
719 return -EEXIST;
720 }
721 if (f->state != state) {
722 f->state = state;
723 f->updated = jiffies;
724 notify = 1;
725 }
David Stevensae884082013-04-19 00:36:26 +0000726 if (f->flags != ndm_flags) {
727 f->flags = ndm_flags;
728 f->updated = jiffies;
729 notify = 1;
730 }
Thomas Richter906dc182013-07-19 17:20:07 +0200731 if ((flags & NLM_F_REPLACE)) {
732 /* Only change unicasts */
733 if (!(is_multicast_ether_addr(f->eth_addr) ||
734 is_zero_ether_addr(f->eth_addr))) {
735 int rc = vxlan_fdb_replace(f, ip, port, vni,
736 ifindex);
737
738 if (rc < 0)
739 return rc;
740 notify |= rc;
741 } else
742 return -EOPNOTSUPP;
743 }
David Stevens66817122013-03-15 04:35:51 +0000744 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300745 (is_multicast_ether_addr(f->eth_addr) ||
746 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200747 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
748 &rd);
David Stevens66817122013-03-15 04:35:51 +0000749
750 if (rc < 0)
751 return rc;
752 notify |= rc;
753 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000754 } else {
755 if (!(flags & NLM_F_CREATE))
756 return -ENOENT;
757
758 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
759 return -ENOSPC;
760
Thomas Richter906dc182013-07-19 17:20:07 +0200761 /* Disallow replace to add a multicast entry */
762 if ((flags & NLM_F_REPLACE) &&
763 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
764 return -EOPNOTSUPP;
765
Cong Wange4c7ed42013-08-31 13:44:33 +0800766 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000767 f = kmalloc(sizeof(*f), GFP_ATOMIC);
768 if (!f)
769 return -ENOMEM;
770
771 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000772 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000773 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000774 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700775 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000776 memcpy(f->eth_addr, mac, ETH_ALEN);
777
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200778 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700779
stephen hemmingerd3428942012-10-01 12:32:35 +0000780 ++vxlan->addrcnt;
781 hlist_add_head_rcu(&f->hlist,
782 vxlan_fdb_head(vxlan, mac));
783 }
784
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200785 if (notify) {
786 if (rd == NULL)
787 rd = first_remote_rtnl(f);
788 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
789 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000790
791 return 0;
792}
793
Wei Yongjun6706c822013-04-11 19:00:35 +0000794static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000795{
796 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700797 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000798
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700799 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000800 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000801 kfree(f);
802}
803
stephen hemmingerd3428942012-10-01 12:32:35 +0000804static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
805{
806 netdev_dbg(vxlan->dev,
807 "delete %pM\n", f->eth_addr);
808
809 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200810 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000811
812 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000813 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000814}
815
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300816static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800817 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300818{
819 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800820 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300821
822 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800823 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
824 if (err)
825 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300826 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800827 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
828 if (remote->sa.sa_family == AF_INET) {
829 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
830 ip->sa.sa_family = AF_INET;
831#if IS_ENABLED(CONFIG_IPV6)
832 } else {
833 ip->sin6.sin6_addr = in6addr_any;
834 ip->sa.sa_family = AF_INET6;
835#endif
836 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300837 }
838
839 if (tb[NDA_PORT]) {
840 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
841 return -EINVAL;
842 *port = nla_get_be16(tb[NDA_PORT]);
843 } else {
844 *port = vxlan->dst_port;
845 }
846
847 if (tb[NDA_VNI]) {
848 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
849 return -EINVAL;
850 *vni = nla_get_u32(tb[NDA_VNI]);
851 } else {
852 *vni = vxlan->default_dst.remote_vni;
853 }
854
855 if (tb[NDA_IFINDEX]) {
856 struct net_device *tdev;
857
858 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
859 return -EINVAL;
860 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800861 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300862 if (!tdev)
863 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300864 } else {
865 *ifindex = 0;
866 }
867
868 return 0;
869}
870
stephen hemmingerd3428942012-10-01 12:32:35 +0000871/* Add static entry (via netlink) */
872static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
873 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100874 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000875{
876 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300877 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800878 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000879 __be16 port;
880 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000881 int err;
882
883 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
884 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
885 ndm->ndm_state);
886 return -EINVAL;
887 }
888
889 if (tb[NDA_DST] == NULL)
890 return -EINVAL;
891
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300892 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
893 if (err)
894 return err;
David Stevens66817122013-03-15 04:35:51 +0000895
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300896 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
897 return -EAFNOSUPPORT;
898
stephen hemmingerd3428942012-10-01 12:32:35 +0000899 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800900 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000901 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000902 spin_unlock_bh(&vxlan->hash_lock);
903
904 return err;
905}
906
907/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000908static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
909 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100910 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000911{
912 struct vxlan_dev *vxlan = netdev_priv(dev);
913 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300914 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800915 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300916 __be16 port;
917 u32 vni, ifindex;
918 int err;
919
920 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
921 if (err)
922 return err;
923
924 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000925
926 spin_lock_bh(&vxlan->hash_lock);
927 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300928 if (!f)
929 goto out;
930
Cong Wange4c7ed42013-08-31 13:44:33 +0800931 if (!vxlan_addr_any(&ip)) {
932 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300933 if (!rd)
934 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000935 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300936
937 err = 0;
938
939 /* remove a destination if it's not the only one on the list,
940 * otherwise destroy the fdb entry
941 */
942 if (rd && !list_is_singular(&f->remotes)) {
943 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200944 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800945 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300946 goto out;
947 }
948
949 vxlan_fdb_destroy(vxlan, f);
950
951out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000952 spin_unlock_bh(&vxlan->hash_lock);
953
954 return err;
955}
956
957/* Dump forwarding table */
958static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400959 struct net_device *dev,
960 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000961{
962 struct vxlan_dev *vxlan = netdev_priv(dev);
963 unsigned int h;
964
965 for (h = 0; h < FDB_HASH_SIZE; ++h) {
966 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000967 int err;
968
Sasha Levinb67bfe02013-02-27 17:06:00 -0800969 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000970 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000971
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700972 if (idx < cb->args[0])
973 goto skip;
974
975 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000976 err = vxlan_fdb_info(skb, vxlan, f,
977 NETLINK_CB(cb->skb).portid,
978 cb->nlh->nlmsg_seq,
979 RTM_NEWNEIGH,
980 NLM_F_MULTI, rd);
981 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700982 goto out;
David Stevens66817122013-03-15 04:35:51 +0000983 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700984skip:
985 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000986 }
987 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700988out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000989 return idx;
990}
991
992/* Watch incoming packets to learn mapping between Ethernet address
993 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700994 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000995 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700996static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800997 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000998{
999 struct vxlan_dev *vxlan = netdev_priv(dev);
1000 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001001
1002 f = vxlan_find_mac(vxlan, src_mac);
1003 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001004 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001005
Cong Wange4c7ed42013-08-31 13:44:33 +08001006 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001007 return false;
1008
1009 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -07001010 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -07001011 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001012
1013 if (net_ratelimit())
1014 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001015 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001016 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +00001017
Cong Wange4c7ed42013-08-31 13:44:33 +08001018 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001019 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001020 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001021 } else {
1022 /* learned new entry */
1023 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001024
1025 /* close off race between vxlan_flush and incoming packets */
1026 if (netif_running(dev))
1027 vxlan_fdb_create(vxlan, src_mac, src_ip,
1028 NUD_REACHABLE,
1029 NLM_F_EXCL|NLM_F_CREATE,
1030 vxlan->dst_port,
1031 vxlan->default_dst.remote_vni,
1032 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001033 spin_unlock(&vxlan->hash_lock);
1034 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001035
1036 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001037}
1038
stephen hemmingerd3428942012-10-01 12:32:35 +00001039/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001040static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001041{
stephen hemminger553675f2013-05-16 11:35:20 +00001042 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001043
Gao feng95ab0992013-12-10 16:37:33 +08001044 /* The vxlan_sock is only used by dev, leaving group has
1045 * no effect on other vxlan devices.
1046 */
1047 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1048 return false;
1049
stephen hemminger553675f2013-05-16 11:35:20 +00001050 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001051 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001052 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001053
Gao feng95ab0992013-12-10 16:37:33 +08001054 if (vxlan->vn_sock != dev->vn_sock)
1055 continue;
1056
1057 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1058 &dev->default_dst.remote_ip))
1059 continue;
1060
1061 if (vxlan->default_dst.remote_ifindex !=
1062 dev->default_dst.remote_ifindex)
1063 continue;
1064
1065 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001066 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001067
1068 return false;
1069}
1070
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001071static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001072{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001073 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001074}
1075
Pravin B Shelar012a5722013-08-19 11:23:07 -07001076void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001077{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001078 struct sock *sk = vs->sock->sk;
1079 struct net *net = sock_net(sk);
1080 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001081
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001082 if (!atomic_dec_and_test(&vs->refcnt))
1083 return;
1084
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001085 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001086 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001087 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001088 spin_unlock(&vn->sock_lock);
1089
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001090 queue_work(vxlan_wq, &vs->del_work);
1091}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001092EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001093
stephen hemminger3fc2de22013-07-18 08:40:15 -07001094/* Callback to update multicast group membership when first VNI on
1095 * multicast asddress is brought up
1096 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001097 */
stephen hemminger3fc2de22013-07-18 08:40:15 -07001098static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001099{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001100 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001101 struct vxlan_sock *vs = vxlan->vn_sock;
1102 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001103 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1104 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001105
stephen hemmingerd3428942012-10-01 12:32:35 +00001106 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001107 if (ip->sa.sa_family == AF_INET) {
1108 struct ip_mreqn mreq = {
1109 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1110 .imr_ifindex = ifindex,
1111 };
1112
1113 ip_mc_join_group(sk, &mreq);
1114#if IS_ENABLED(CONFIG_IPV6)
1115 } else {
1116 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1117 &ip->sin6.sin6_addr);
1118#endif
1119 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001120 release_sock(sk);
1121
Pravin B Shelar012a5722013-08-19 11:23:07 -07001122 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001123 dev_put(vxlan->dev);
1124}
1125
1126/* Inverse of vxlan_igmp_join when last VNI is brought down */
1127static void vxlan_igmp_leave(struct work_struct *work)
1128{
1129 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001130 struct vxlan_sock *vs = vxlan->vn_sock;
1131 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001132 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1133 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001134
1135 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001136 if (ip->sa.sa_family == AF_INET) {
1137 struct ip_mreqn mreq = {
1138 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1139 .imr_ifindex = ifindex,
1140 };
1141
1142 ip_mc_leave_group(sk, &mreq);
1143#if IS_ENABLED(CONFIG_IPV6)
1144 } else {
1145 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1146 &ip->sin6.sin6_addr);
1147#endif
1148 }
1149
stephen hemmingerd3428942012-10-01 12:32:35 +00001150 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001151
Pravin B Shelar012a5722013-08-19 11:23:07 -07001152 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001153 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001154}
1155
Tom Herbertdfd86452015-01-12 17:00:38 -08001156static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1157 size_t hdrlen, u32 data)
1158{
1159 size_t start, offset, plen;
1160 __wsum delta;
1161
1162 if (skb->remcsum_offload) {
1163 /* Already processed in GRO path */
1164 skb->remcsum_offload = 0;
1165 return vh;
1166 }
1167
1168 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1169 offset = start + ((data & VXLAN_RCO_UDP) ?
1170 offsetof(struct udphdr, check) :
1171 offsetof(struct tcphdr, check));
1172
1173 plen = hdrlen + offset + sizeof(u16);
1174
1175 if (!pskb_may_pull(skb, plen))
1176 return NULL;
1177
1178 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1179
1180 if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE))
1181 __skb_checksum_complete(skb);
1182
1183 delta = remcsum_adjust((void *)vh + hdrlen,
1184 skb->csum, start, offset);
1185
1186 /* Adjust skb->csum since we changed the packet */
1187 skb->csum = csum_add(skb->csum, delta);
1188
1189 return vh;
1190}
1191
stephen hemmingerd3428942012-10-01 12:32:35 +00001192/* Callback from net/ipv4/udp.c to receive packets */
1193static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1194{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001195 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001196 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001197 u32 flags, vni;
Thomas Graf35114942015-01-15 03:53:55 +01001198 struct vxlan_metadata md = {0};
stephen hemmingerd3428942012-10-01 12:32:35 +00001199
stephen hemmingerd3428942012-10-01 12:32:35 +00001200 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001201 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001202 goto error;
1203
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001204 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001205 flags = ntohl(vxh->vx_flags);
1206 vni = ntohl(vxh->vx_vni);
1207
1208 if (flags & VXLAN_HF_VNI) {
1209 flags &= ~VXLAN_HF_VNI;
1210 } else {
1211 /* VNI flag always required to be set */
1212 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001213 }
1214
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001215 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001216 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001217 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001218
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001219 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001220 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001221 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001222
Tom Herbertdfd86452015-01-12 17:00:38 -08001223 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1224 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1225 if (!vxh)
1226 goto drop;
1227
1228 flags &= ~VXLAN_HF_RCO;
1229 vni &= VXLAN_VID_MASK;
1230 }
1231
Thomas Graf35114942015-01-15 03:53:55 +01001232 /* For backwards compatibility, only allow reserved fields to be
1233 * used by VXLAN extensions if explicitly requested.
1234 */
1235 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1236 struct vxlanhdr_gbp *gbp;
1237
1238 gbp = (struct vxlanhdr_gbp *)vxh;
1239 md.gbp = ntohs(gbp->policy_id);
1240
1241 if (gbp->dont_learn)
1242 md.gbp |= VXLAN_GBP_DONT_LEARN;
1243
1244 if (gbp->policy_applied)
1245 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1246
1247 flags &= ~VXLAN_GBP_USED_BITS;
1248 }
1249
Tom Herbertdfd86452015-01-12 17:00:38 -08001250 if (flags || (vni & ~VXLAN_VID_MASK)) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001251 /* If there are any unprocessed flags remaining treat
1252 * this as a malformed packet. This behavior diverges from
1253 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1254 * in reserved fields are to be ignored. The approach here
1255 * maintains compatbility with previous stack code, and also
1256 * is more robust and provides a little more security in
1257 * adding extensions to VXLAN.
1258 */
1259
1260 goto bad_flags;
1261 }
1262
Thomas Graf35114942015-01-15 03:53:55 +01001263 md.vni = vxh->vx_vni;
1264 vs->rcv(vs, skb, &md);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001265 return 0;
1266
1267drop:
1268 /* Consume bad packet */
1269 kfree_skb(skb);
1270 return 0;
1271
Tom Herbert3bf39472015-01-08 12:31:18 -08001272bad_flags:
1273 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1274 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1275
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001276error:
1277 /* Return non vxlan pkt */
1278 return 1;
1279}
1280
Thomas Graf35114942015-01-15 03:53:55 +01001281static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1282 struct vxlan_metadata *md)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001283{
Cong Wange4c7ed42013-08-31 13:44:33 +08001284 struct iphdr *oip = NULL;
1285 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001286 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001287 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001288 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001289 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001290 int err = 0;
1291 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001292
Thomas Graf35114942015-01-15 03:53:55 +01001293 vni = ntohl(md->vni) >> 8;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001294 /* Is this VNI defined? */
1295 vxlan = vxlan_vs_find_vni(vs, vni);
1296 if (!vxlan)
1297 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001298
Cong Wange4c7ed42013-08-31 13:44:33 +08001299 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001300 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001301 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001302 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001303 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001304
1305 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001306 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001307 goto drop;
1308
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001309 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001310 if (remote_ip->sa.sa_family == AF_INET) {
1311 oip = ip_hdr(skb);
1312 saddr.sin.sin_addr.s_addr = oip->saddr;
1313 saddr.sa.sa_family = AF_INET;
1314#if IS_ENABLED(CONFIG_IPV6)
1315 } else {
1316 oip6 = ipv6_hdr(skb);
1317 saddr.sin6.sin6_addr = oip6->saddr;
1318 saddr.sa.sa_family = AF_INET6;
1319#endif
1320 }
1321
stephen hemminger26a41ae62013-06-17 12:09:58 -07001322 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001323 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001324 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001325
stephen hemmingerd3428942012-10-01 12:32:35 +00001326 skb_reset_network_header(skb);
Thomas Graf35114942015-01-15 03:53:55 +01001327 skb->mark = md->gbp;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001328
Cong Wange4c7ed42013-08-31 13:44:33 +08001329 if (oip6)
1330 err = IP6_ECN_decapsulate(oip6, skb);
1331 if (oip)
1332 err = IP_ECN_decapsulate(oip, skb);
1333
stephen hemmingerd3428942012-10-01 12:32:35 +00001334 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001335 if (log_ecn_error) {
1336 if (oip6)
1337 net_info_ratelimited("non-ECT from %pI6\n",
1338 &oip6->saddr);
1339 if (oip)
1340 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1341 &oip->saddr, oip->tos);
1342 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001343 if (err > 1) {
1344 ++vxlan->dev->stats.rx_frame_errors;
1345 ++vxlan->dev->stats.rx_errors;
1346 goto drop;
1347 }
1348 }
1349
Pravin B Shelare8171042013-03-25 14:49:46 +00001350 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001351 u64_stats_update_begin(&stats->syncp);
1352 stats->rx_packets++;
1353 stats->rx_bytes += skb->len;
1354 u64_stats_update_end(&stats->syncp);
1355
1356 netif_rx(skb);
1357
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001358 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001359drop:
1360 /* Consume bad packet */
1361 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001362}
1363
David Stevense4f67ad2012-11-20 02:50:14 +00001364static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1365{
1366 struct vxlan_dev *vxlan = netdev_priv(dev);
1367 struct arphdr *parp;
1368 u8 *arpptr, *sha;
1369 __be32 sip, tip;
1370 struct neighbour *n;
1371
1372 if (dev->flags & IFF_NOARP)
1373 goto out;
1374
1375 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1376 dev->stats.tx_dropped++;
1377 goto out;
1378 }
1379 parp = arp_hdr(skb);
1380
1381 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1382 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1383 parp->ar_pro != htons(ETH_P_IP) ||
1384 parp->ar_op != htons(ARPOP_REQUEST) ||
1385 parp->ar_hln != dev->addr_len ||
1386 parp->ar_pln != 4)
1387 goto out;
1388 arpptr = (u8 *)parp + sizeof(struct arphdr);
1389 sha = arpptr;
1390 arpptr += dev->addr_len; /* sha */
1391 memcpy(&sip, arpptr, sizeof(sip));
1392 arpptr += sizeof(sip);
1393 arpptr += dev->addr_len; /* tha */
1394 memcpy(&tip, arpptr, sizeof(tip));
1395
1396 if (ipv4_is_loopback(tip) ||
1397 ipv4_is_multicast(tip))
1398 goto out;
1399
1400 n = neigh_lookup(&arp_tbl, &tip, dev);
1401
1402 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001403 struct vxlan_fdb *f;
1404 struct sk_buff *reply;
1405
1406 if (!(n->nud_state & NUD_CONNECTED)) {
1407 neigh_release(n);
1408 goto out;
1409 }
1410
1411 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001412 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001413 /* bridge-local neighbor */
1414 neigh_release(n);
1415 goto out;
1416 }
1417
1418 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1419 n->ha, sha);
1420
1421 neigh_release(n);
1422
David Stevens73461352014-03-18 12:32:29 -04001423 if (reply == NULL)
1424 goto out;
1425
David Stevense4f67ad2012-11-20 02:50:14 +00001426 skb_reset_mac_header(reply);
1427 __skb_pull(reply, skb_network_offset(reply));
1428 reply->ip_summed = CHECKSUM_UNNECESSARY;
1429 reply->pkt_type = PACKET_HOST;
1430
1431 if (netif_rx_ni(reply) == NET_RX_DROP)
1432 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001433 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1434 union vxlan_addr ipa = {
1435 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001436 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001437 };
1438
1439 vxlan_ip_miss(dev, &ipa);
1440 }
David Stevense4f67ad2012-11-20 02:50:14 +00001441out:
1442 consume_skb(skb);
1443 return NETDEV_TX_OK;
1444}
1445
Cong Wangf564f452013-08-31 13:44:36 +08001446#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001447static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1448 struct neighbour *n, bool isrouter)
1449{
1450 struct net_device *dev = request->dev;
1451 struct sk_buff *reply;
1452 struct nd_msg *ns, *na;
1453 struct ipv6hdr *pip6;
1454 u8 *daddr;
1455 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1456 int ns_olen;
1457 int i, len;
1458
1459 if (dev == NULL)
1460 return NULL;
1461
1462 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1463 sizeof(*na) + na_olen + dev->needed_tailroom;
1464 reply = alloc_skb(len, GFP_ATOMIC);
1465 if (reply == NULL)
1466 return NULL;
1467
1468 reply->protocol = htons(ETH_P_IPV6);
1469 reply->dev = dev;
1470 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1471 skb_push(reply, sizeof(struct ethhdr));
1472 skb_set_mac_header(reply, 0);
1473
1474 ns = (struct nd_msg *)skb_transport_header(request);
1475
1476 daddr = eth_hdr(request)->h_source;
1477 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1478 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1479 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1480 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1481 break;
1482 }
1483 }
1484
1485 /* Ethernet header */
1486 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1487 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1488 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1489 reply->protocol = htons(ETH_P_IPV6);
1490
1491 skb_pull(reply, sizeof(struct ethhdr));
1492 skb_set_network_header(reply, 0);
1493 skb_put(reply, sizeof(struct ipv6hdr));
1494
1495 /* IPv6 header */
1496
1497 pip6 = ipv6_hdr(reply);
1498 memset(pip6, 0, sizeof(struct ipv6hdr));
1499 pip6->version = 6;
1500 pip6->priority = ipv6_hdr(request)->priority;
1501 pip6->nexthdr = IPPROTO_ICMPV6;
1502 pip6->hop_limit = 255;
1503 pip6->daddr = ipv6_hdr(request)->saddr;
1504 pip6->saddr = *(struct in6_addr *)n->primary_key;
1505
1506 skb_pull(reply, sizeof(struct ipv6hdr));
1507 skb_set_transport_header(reply, 0);
1508
1509 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1510
1511 /* Neighbor Advertisement */
1512 memset(na, 0, sizeof(*na)+na_olen);
1513 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1514 na->icmph.icmp6_router = isrouter;
1515 na->icmph.icmp6_override = 1;
1516 na->icmph.icmp6_solicited = 1;
1517 na->target = ns->target;
1518 ether_addr_copy(&na->opt[2], n->ha);
1519 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1520 na->opt[1] = na_olen >> 3;
1521
1522 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1523 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1524 csum_partial(na, sizeof(*na)+na_olen, 0));
1525
1526 pip6->payload_len = htons(sizeof(*na)+na_olen);
1527
1528 skb_push(reply, sizeof(struct ipv6hdr));
1529
1530 reply->ip_summed = CHECKSUM_UNNECESSARY;
1531
1532 return reply;
1533}
1534
Cong Wangf564f452013-08-31 13:44:36 +08001535static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1536{
1537 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001538 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001539 const struct ipv6hdr *iphdr;
1540 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001541 struct neighbour *n;
1542 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001543
1544 in6_dev = __in6_dev_get(dev);
1545 if (!in6_dev)
1546 goto out;
1547
Cong Wangf564f452013-08-31 13:44:36 +08001548 iphdr = ipv6_hdr(skb);
1549 saddr = &iphdr->saddr;
1550 daddr = &iphdr->daddr;
1551
Cong Wangf564f452013-08-31 13:44:36 +08001552 msg = (struct nd_msg *)skb_transport_header(skb);
1553 if (msg->icmph.icmp6_code != 0 ||
1554 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1555 goto out;
1556
David Stevens4b29dba2014-03-24 10:39:58 -04001557 if (ipv6_addr_loopback(daddr) ||
1558 ipv6_addr_is_multicast(&msg->target))
1559 goto out;
1560
1561 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001562
1563 if (n) {
1564 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001565 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001566
1567 if (!(n->nud_state & NUD_CONNECTED)) {
1568 neigh_release(n);
1569 goto out;
1570 }
1571
1572 f = vxlan_find_mac(vxlan, n->ha);
1573 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1574 /* bridge-local neighbor */
1575 neigh_release(n);
1576 goto out;
1577 }
1578
David Stevens4b29dba2014-03-24 10:39:58 -04001579 reply = vxlan_na_create(skb, n,
1580 !!(f ? f->flags & NTF_ROUTER : 0));
1581
Cong Wangf564f452013-08-31 13:44:36 +08001582 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001583
1584 if (reply == NULL)
1585 goto out;
1586
1587 if (netif_rx_ni(reply) == NET_RX_DROP)
1588 dev->stats.rx_dropped++;
1589
Cong Wangf564f452013-08-31 13:44:36 +08001590 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001591 union vxlan_addr ipa = {
1592 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001593 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001594 };
1595
Cong Wangf564f452013-08-31 13:44:36 +08001596 vxlan_ip_miss(dev, &ipa);
1597 }
1598
1599out:
1600 consume_skb(skb);
1601 return NETDEV_TX_OK;
1602}
1603#endif
1604
David Stevense4f67ad2012-11-20 02:50:14 +00001605static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1606{
1607 struct vxlan_dev *vxlan = netdev_priv(dev);
1608 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001609
1610 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1611 return false;
1612
1613 n = NULL;
1614 switch (ntohs(eth_hdr(skb)->h_proto)) {
1615 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001616 {
1617 struct iphdr *pip;
1618
David Stevense4f67ad2012-11-20 02:50:14 +00001619 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1620 return false;
1621 pip = ip_hdr(skb);
1622 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001623 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1624 union vxlan_addr ipa = {
1625 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001626 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001627 };
1628
1629 vxlan_ip_miss(dev, &ipa);
1630 return false;
1631 }
1632
David Stevense4f67ad2012-11-20 02:50:14 +00001633 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001634 }
1635#if IS_ENABLED(CONFIG_IPV6)
1636 case ETH_P_IPV6:
1637 {
1638 struct ipv6hdr *pip6;
1639
1640 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1641 return false;
1642 pip6 = ipv6_hdr(skb);
1643 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1644 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1645 union vxlan_addr ipa = {
1646 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001647 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001648 };
1649
1650 vxlan_ip_miss(dev, &ipa);
1651 return false;
1652 }
1653
1654 break;
1655 }
1656#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001657 default:
1658 return false;
1659 }
1660
1661 if (n) {
1662 bool diff;
1663
Joe Perches7367d0b2013-09-01 11:51:23 -07001664 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001665 if (diff) {
1666 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1667 dev->addr_len);
1668 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1669 }
1670 neigh_release(n);
1671 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001672 }
1673
David Stevense4f67ad2012-11-20 02:50:14 +00001674 return false;
1675}
1676
Thomas Graf35114942015-01-15 03:53:55 +01001677static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, struct vxlan_sock *vs,
1678 struct vxlan_metadata *md)
1679{
1680 struct vxlanhdr_gbp *gbp;
1681
1682 gbp = (struct vxlanhdr_gbp *)vxh;
1683 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1684
1685 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1686 gbp->dont_learn = 1;
1687
1688 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1689 gbp->policy_applied = 1;
1690
1691 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1692}
1693
Cong Wange4c7ed42013-08-31 13:44:33 +08001694#if IS_ENABLED(CONFIG_IPV6)
Nicolas Dichtel11796182013-09-02 15:34:55 +02001695static int vxlan6_xmit_skb(struct vxlan_sock *vs,
Cong Wange4c7ed42013-08-31 13:44:33 +08001696 struct dst_entry *dst, struct sk_buff *skb,
1697 struct net_device *dev, struct in6_addr *saddr,
1698 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Thomas Graf35114942015-01-15 03:53:55 +01001699 __be16 src_port, __be16 dst_port,
1700 struct vxlan_metadata *md, bool xnet)
Cong Wange4c7ed42013-08-31 13:44:33 +08001701{
Cong Wange4c7ed42013-08-31 13:44:33 +08001702 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001703 int min_headroom;
1704 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001705 bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
Tom Herbertdfd86452015-01-12 17:00:38 -08001706 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1707 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001708
Tom Herbertdfd86452015-01-12 17:00:38 -08001709 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1710 skb->ip_summed == CHECKSUM_PARTIAL) {
1711 int csum_start = skb_checksum_start_offset(skb);
1712
1713 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1714 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1715 (skb->csum_offset == offsetof(struct udphdr, check) ||
1716 skb->csum_offset == offsetof(struct tcphdr, check))) {
1717 udp_sum = false;
1718 type |= SKB_GSO_TUNNEL_REMCSUM;
1719 }
1720 }
1721
1722 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001723 if (IS_ERR(skb)) {
1724 err = -EINVAL;
1725 goto err;
1726 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001727
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001728 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001729
Cong Wange4c7ed42013-08-31 13:44:33 +08001730 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1731 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001732 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001733
1734 /* Need space for new headers (invalidates iph ptr) */
1735 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001736 if (unlikely(err)) {
1737 kfree_skb(skb);
1738 goto err;
1739 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001740
Jiri Pirko59682502014-11-19 14:04:59 +01001741 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001742 if (WARN_ON(!skb)) {
1743 err = -ENOMEM;
1744 goto err;
1745 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001746
1747 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001748 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001749 vxh->vx_vni = md->vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001750
Tom Herbertdfd86452015-01-12 17:00:38 -08001751 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1752 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1753 VXLAN_RCO_SHIFT;
1754
1755 if (skb->csum_offset == offsetof(struct udphdr, check))
1756 data |= VXLAN_RCO_UDP;
1757
1758 vxh->vx_vni |= htonl(data);
1759 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1760
1761 if (!skb_is_gso(skb)) {
1762 skb->ip_summed = CHECKSUM_NONE;
1763 skb->encapsulation = 0;
1764 }
1765 }
1766
Thomas Graf35114942015-01-15 03:53:55 +01001767 if (vs->flags & VXLAN_F_GBP)
1768 vxlan_build_gbp_hdr(vxh, vs, md);
1769
Tom Herbert996c9fd2014-09-29 20:22:33 -07001770 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1771
Andy Zhouacbf74a2014-09-16 17:31:18 -07001772 udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
1773 ttl, src_port, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001774 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001775err:
1776 dst_release(dst);
1777 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001778}
1779#endif
1780
Nicolas Dichtel11796182013-09-02 15:34:55 +02001781int vxlan_xmit_skb(struct vxlan_sock *vs,
Pravin B Shelar49560532013-08-19 11:23:17 -07001782 struct rtable *rt, struct sk_buff *skb,
1783 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Thomas Graf35114942015-01-15 03:53:55 +01001784 __be16 src_port, __be16 dst_port,
1785 struct vxlan_metadata *md, bool xnet)
Pravin B Shelar49560532013-08-19 11:23:17 -07001786{
1787 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001788 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001789 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001790 bool udp_sum = !vs->sock->sk->sk_no_check_tx;
Tom Herbertdfd86452015-01-12 17:00:38 -08001791 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1792 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001793
Tom Herbertdfd86452015-01-12 17:00:38 -08001794 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1795 skb->ip_summed == CHECKSUM_PARTIAL) {
1796 int csum_start = skb_checksum_start_offset(skb);
1797
1798 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1799 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1800 (skb->csum_offset == offsetof(struct udphdr, check) ||
1801 skb->csum_offset == offsetof(struct tcphdr, check))) {
1802 udp_sum = false;
1803 type |= SKB_GSO_TUNNEL_REMCSUM;
1804 }
1805 }
1806
1807 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001808 if (IS_ERR(skb))
Pravin B Shelar74f47272014-12-23 16:20:36 -08001809 return PTR_ERR(skb);
Pravin B Shelar49560532013-08-19 11:23:17 -07001810
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001811 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001812 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001813 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001814
1815 /* Need space for new headers (invalidates iph ptr) */
1816 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001817 if (unlikely(err)) {
1818 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001819 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001820 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001821
Jiri Pirko59682502014-11-19 14:04:59 +01001822 skb = vlan_hwaccel_push_inside(skb);
1823 if (WARN_ON(!skb))
1824 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001825
Pravin B Shelar49560532013-08-19 11:23:17 -07001826 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001827 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001828 vxh->vx_vni = md->vni;
Pravin B Shelar49560532013-08-19 11:23:17 -07001829
Tom Herbertdfd86452015-01-12 17:00:38 -08001830 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1831 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1832 VXLAN_RCO_SHIFT;
1833
1834 if (skb->csum_offset == offsetof(struct udphdr, check))
1835 data |= VXLAN_RCO_UDP;
1836
1837 vxh->vx_vni |= htonl(data);
1838 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1839
1840 if (!skb_is_gso(skb)) {
1841 skb->ip_summed = CHECKSUM_NONE;
1842 skb->encapsulation = 0;
1843 }
1844 }
1845
Thomas Graf35114942015-01-15 03:53:55 +01001846 if (vs->flags & VXLAN_F_GBP)
1847 vxlan_build_gbp_hdr(vxh, vs, md);
1848
Tom Herbert996c9fd2014-09-29 20:22:33 -07001849 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1850
Andy Zhouacbf74a2014-09-16 17:31:18 -07001851 return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
1852 ttl, df, src_port, dst_port, xnet);
Pravin B Shelar49560532013-08-19 11:23:17 -07001853}
1854EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1855
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001856/* Bypass encapsulation if the destination is local */
1857static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1858 struct vxlan_dev *dst_vxlan)
1859{
Li RongQing8f849852014-01-04 13:57:59 +08001860 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001861 union vxlan_addr loopback;
1862 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001863 struct net_device *dev = skb->dev;
1864 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001865
Li RongQing8f849852014-01-04 13:57:59 +08001866 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1867 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001868 skb->pkt_type = PACKET_HOST;
1869 skb->encapsulation = 0;
1870 skb->dev = dst_vxlan->dev;
1871 __skb_pull(skb, skb_network_offset(skb));
1872
Cong Wange4c7ed42013-08-31 13:44:33 +08001873 if (remote_ip->sa.sa_family == AF_INET) {
1874 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1875 loopback.sa.sa_family = AF_INET;
1876#if IS_ENABLED(CONFIG_IPV6)
1877 } else {
1878 loopback.sin6.sin6_addr = in6addr_loopback;
1879 loopback.sa.sa_family = AF_INET6;
1880#endif
1881 }
1882
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001883 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001884 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001885
1886 u64_stats_update_begin(&tx_stats->syncp);
1887 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001888 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001889 u64_stats_update_end(&tx_stats->syncp);
1890
1891 if (netif_rx(skb) == NET_RX_SUCCESS) {
1892 u64_stats_update_begin(&rx_stats->syncp);
1893 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001894 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001895 u64_stats_update_end(&rx_stats->syncp);
1896 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001897 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001898 }
1899}
1900
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001901static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1902 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001903{
1904 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001905 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001906 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001907 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001908 union vxlan_addr *dst;
Thomas Graf35114942015-01-15 03:53:55 +01001909 struct vxlan_metadata md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001910 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001911 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001912 __be16 df = 0;
1913 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001914 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001915
stephen hemminger823aa872013-04-27 11:31:57 +00001916 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001917 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001918 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001919
Cong Wange4c7ed42013-08-31 13:44:33 +08001920 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001921 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001922 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001923 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001924 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001925 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001926 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001927 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001928
stephen hemmingerd3428942012-10-01 12:32:35 +00001929 old_iph = ip_hdr(skb);
1930
stephen hemmingerd3428942012-10-01 12:32:35 +00001931 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001932 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001933 ttl = 1;
1934
1935 tos = vxlan->tos;
1936 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001937 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001938
Tom Herbert535fb8d2014-07-01 21:32:49 -07001939 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1940 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001941
Cong Wange4c7ed42013-08-31 13:44:33 +08001942 if (dst->sa.sa_family == AF_INET) {
1943 memset(&fl4, 0, sizeof(fl4));
1944 fl4.flowi4_oif = rdst->remote_ifindex;
1945 fl4.flowi4_tos = RT_TOS(tos);
1946 fl4.daddr = dst->sin.sin_addr.s_addr;
1947 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001948
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001949 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001950 if (IS_ERR(rt)) {
1951 netdev_dbg(dev, "no route to %pI4\n",
1952 &dst->sin.sin_addr.s_addr);
1953 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001954 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001955 }
1956
1957 if (rt->dst.dev == dev) {
1958 netdev_dbg(dev, "circular route to %pI4\n",
1959 &dst->sin.sin_addr.s_addr);
1960 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001961 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001962 }
1963
1964 /* Bypass encapsulation if the destination is local */
1965 if (rt->rt_flags & RTCF_LOCAL &&
1966 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1967 struct vxlan_dev *dst_vxlan;
1968
1969 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001970 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01001971 dst->sa.sa_family, dst_port,
1972 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08001973 if (!dst_vxlan)
1974 goto tx_error;
1975 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1976 return;
1977 }
1978
1979 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1980 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Thomas Graf35114942015-01-15 03:53:55 +01001981 md.vni = htonl(vni << 8);
1982 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08001983
Andy Zhou3c4d1da2014-09-23 01:44:51 -07001984 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1985 fl4.saddr, dst->sin.sin_addr.s_addr,
Thomas Graf35114942015-01-15 03:53:55 +01001986 tos, ttl, df, src_port, dst_port, &md,
Andy Zhou3c4d1da2014-09-23 01:44:51 -07001987 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Pravin B Shelar74f47272014-12-23 16:20:36 -08001988 if (err < 0) {
1989 /* skb is already freed. */
1990 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001991 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001992 }
1993
Cong Wange4c7ed42013-08-31 13:44:33 +08001994 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1995#if IS_ENABLED(CONFIG_IPV6)
1996 } else {
1997 struct sock *sk = vxlan->vn_sock->sock->sk;
1998 struct dst_entry *ndst;
1999 struct flowi6 fl6;
2000 u32 flags;
2001
2002 memset(&fl6, 0, sizeof(fl6));
2003 fl6.flowi6_oif = rdst->remote_ifindex;
2004 fl6.daddr = dst->sin6.sin6_addr;
2005 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08002006 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08002007
2008 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2009 netdev_dbg(dev, "no route to %pI6\n",
2010 &dst->sin6.sin6_addr);
2011 dev->stats.tx_carrier_errors++;
2012 goto tx_error;
2013 }
2014
2015 if (ndst->dev == dev) {
2016 netdev_dbg(dev, "circular route to %pI6\n",
2017 &dst->sin6.sin6_addr);
2018 dst_release(ndst);
2019 dev->stats.collisions++;
2020 goto tx_error;
2021 }
2022
2023 /* Bypass encapsulation if the destination is local */
2024 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2025 if (flags & RTF_LOCAL &&
2026 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2027 struct vxlan_dev *dst_vxlan;
2028
2029 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002030 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002031 dst->sa.sa_family, dst_port,
2032 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002033 if (!dst_vxlan)
2034 goto tx_error;
2035 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2036 return;
2037 }
2038
2039 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Thomas Graf35114942015-01-15 03:53:55 +01002040 md.vni = htonl(vni << 8);
2041 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08002042
Nicolas Dichtel11796182013-09-02 15:34:55 +02002043 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08002044 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
Thomas Graf35114942015-01-15 03:53:55 +01002045 src_port, dst_port, &md,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002046 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Cong Wange4c7ed42013-08-31 13:44:33 +08002047#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002048 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002049
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002050 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002051
2052drop:
2053 dev->stats.tx_dropped++;
2054 goto tx_free;
2055
Pravin B Shelar49560532013-08-19 11:23:17 -07002056rt_tx_error:
2057 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002058tx_error:
2059 dev->stats.tx_errors++;
2060tx_free:
2061 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002062}
2063
David Stevens66817122013-03-15 04:35:51 +00002064/* Transmit local packets over Vxlan
2065 *
2066 * Outer IP header inherits ECN and DF from inner header.
2067 * Outer UDP destination is the VXLAN assigned port.
2068 * source port is based on hash of flow
2069 */
2070static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2071{
2072 struct vxlan_dev *vxlan = netdev_priv(dev);
2073 struct ethhdr *eth;
2074 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002075 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002076 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002077
2078 skb_reset_mac_header(skb);
2079 eth = eth_hdr(skb);
2080
Cong Wangf564f452013-08-31 13:44:36 +08002081 if ((vxlan->flags & VXLAN_F_PROXY)) {
2082 if (ntohs(eth->h_proto) == ETH_P_ARP)
2083 return arp_reduce(dev, skb);
2084#if IS_ENABLED(CONFIG_IPV6)
2085 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002086 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2087 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002088 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2089 struct nd_msg *msg;
2090
2091 msg = (struct nd_msg *)skb_transport_header(skb);
2092 if (msg->icmph.icmp6_code == 0 &&
2093 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2094 return neigh_reduce(dev, skb);
2095 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002096 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002097#endif
2098 }
David Stevens66817122013-03-15 04:35:51 +00002099
2100 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002101 did_rsc = false;
2102
2103 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002104 (ntohs(eth->h_proto) == ETH_P_IP ||
2105 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002106 did_rsc = route_shortcircuit(dev, skb);
2107 if (did_rsc)
2108 f = vxlan_find_mac(vxlan, eth->h_dest);
2109 }
2110
David Stevens66817122013-03-15 04:35:51 +00002111 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002112 f = vxlan_find_mac(vxlan, all_zeros_mac);
2113 if (f == NULL) {
2114 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2115 !is_multicast_ether_addr(eth->h_dest))
2116 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002117
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002118 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002119 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002120 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002121 }
David Stevens66817122013-03-15 04:35:51 +00002122 }
2123
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002124 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2125 struct sk_buff *skb1;
2126
Eric Dumazet8f646c92014-01-06 09:54:31 -08002127 if (!fdst) {
2128 fdst = rdst;
2129 continue;
2130 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002131 skb1 = skb_clone(skb, GFP_ATOMIC);
2132 if (skb1)
2133 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2134 }
2135
Eric Dumazet8f646c92014-01-06 09:54:31 -08002136 if (fdst)
2137 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2138 else
2139 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002140 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002141}
2142
stephen hemmingerd3428942012-10-01 12:32:35 +00002143/* Walk the forwarding table and purge stale entries */
2144static void vxlan_cleanup(unsigned long arg)
2145{
2146 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2147 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2148 unsigned int h;
2149
2150 if (!netif_running(vxlan->dev))
2151 return;
2152
2153 spin_lock_bh(&vxlan->hash_lock);
2154 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2155 struct hlist_node *p, *n;
2156 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2157 struct vxlan_fdb *f
2158 = container_of(p, struct vxlan_fdb, hlist);
2159 unsigned long timeout;
2160
stephen hemminger3c172862012-10-26 06:24:34 +00002161 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002162 continue;
2163
2164 timeout = f->used + vxlan->age_interval * HZ;
2165 if (time_before_eq(timeout, jiffies)) {
2166 netdev_dbg(vxlan->dev,
2167 "garbage collect %pM\n",
2168 f->eth_addr);
2169 f->state = NUD_STALE;
2170 vxlan_fdb_destroy(vxlan, f);
2171 } else if (time_before(timeout, next_timer))
2172 next_timer = timeout;
2173 }
2174 }
2175 spin_unlock_bh(&vxlan->hash_lock);
2176
2177 mod_timer(&vxlan->age_timer, next_timer);
2178}
2179
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002180static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2181{
2182 __u32 vni = vxlan->default_dst.remote_vni;
2183
2184 vxlan->vn_sock = vs;
2185 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2186}
2187
stephen hemmingerd3428942012-10-01 12:32:35 +00002188/* Setup stats when device is created */
2189static int vxlan_init(struct net_device *dev)
2190{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002191 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002192 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002193 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002194 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002195
WANG Cong1c213bd2014-02-13 11:46:28 -08002196 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002197 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002198 return -ENOMEM;
2199
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002200 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002201 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002202 vxlan->dst_port, vxlan->flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002203 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002204 /* If we have a socket with same port already, reuse it */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002205 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002206 } else {
2207 /* otherwise make new socket outside of RTNL */
2208 dev_hold(dev);
2209 queue_work(vxlan_wq, &vxlan->sock_work);
2210 }
2211 spin_unlock(&vn->sock_lock);
2212
stephen hemmingerd3428942012-10-01 12:32:35 +00002213 return 0;
2214}
2215
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002216static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002217{
2218 struct vxlan_fdb *f;
2219
2220 spin_lock_bh(&vxlan->hash_lock);
2221 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2222 if (f)
2223 vxlan_fdb_destroy(vxlan, f);
2224 spin_unlock_bh(&vxlan->hash_lock);
2225}
2226
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002227static void vxlan_uninit(struct net_device *dev)
2228{
2229 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002230 struct vxlan_sock *vs = vxlan->vn_sock;
2231
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002232 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002233
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002234 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07002235 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002236 free_percpu(dev->tstats);
2237}
2238
stephen hemmingerd3428942012-10-01 12:32:35 +00002239/* Start ageing timer and join group when device is brought up */
2240static int vxlan_open(struct net_device *dev)
2241{
2242 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002243 struct vxlan_sock *vs = vxlan->vn_sock;
2244
2245 /* socket hasn't been created */
2246 if (!vs)
2247 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002248
Gao feng79d4a942013-12-10 16:37:32 +08002249 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002250 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002251 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002252 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00002253 }
2254
2255 if (vxlan->age_interval)
2256 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2257
2258 return 0;
2259}
2260
2261/* Purge the forwarding table */
2262static void vxlan_flush(struct vxlan_dev *vxlan)
2263{
Cong Wang31fec5a2013-05-27 22:35:52 +00002264 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002265
2266 spin_lock_bh(&vxlan->hash_lock);
2267 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2268 struct hlist_node *p, *n;
2269 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2270 struct vxlan_fdb *f
2271 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002272 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2273 if (!is_zero_ether_addr(f->eth_addr))
2274 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002275 }
2276 }
2277 spin_unlock_bh(&vxlan->hash_lock);
2278}
2279
2280/* Cleanup timer and forwarding table on shutdown */
2281static int vxlan_stop(struct net_device *dev)
2282{
2283 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002284 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002285 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00002286
Cong Wange4c7ed42013-08-31 13:44:33 +08002287 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002288 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002289 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002290 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002291 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002292 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002293
2294 del_timer_sync(&vxlan->age_timer);
2295
2296 vxlan_flush(vxlan);
2297
2298 return 0;
2299}
2300
stephen hemmingerd3428942012-10-01 12:32:35 +00002301/* Stub, nothing needs to be done. */
2302static void vxlan_set_multicast_list(struct net_device *dev)
2303{
2304}
2305
Daniel Borkmann345010b2013-12-18 00:21:08 +01002306static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2307{
2308 struct vxlan_dev *vxlan = netdev_priv(dev);
2309 struct vxlan_rdst *dst = &vxlan->default_dst;
2310 struct net_device *lowerdev;
2311 int max_mtu;
2312
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002313 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002314 if (lowerdev == NULL)
2315 return eth_change_mtu(dev, new_mtu);
2316
2317 if (dst->remote_ip.sa.sa_family == AF_INET6)
2318 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2319 else
2320 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2321
2322 if (new_mtu < 68 || new_mtu > max_mtu)
2323 return -EINVAL;
2324
2325 dev->mtu = new_mtu;
2326 return 0;
2327}
2328
stephen hemmingerd3428942012-10-01 12:32:35 +00002329static const struct net_device_ops vxlan_netdev_ops = {
2330 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002331 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002332 .ndo_open = vxlan_open,
2333 .ndo_stop = vxlan_stop,
2334 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002335 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002336 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002337 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002338 .ndo_validate_addr = eth_validate_addr,
2339 .ndo_set_mac_address = eth_mac_addr,
2340 .ndo_fdb_add = vxlan_fdb_add,
2341 .ndo_fdb_del = vxlan_fdb_delete,
2342 .ndo_fdb_dump = vxlan_fdb_dump,
2343};
2344
2345/* Info for udev, that this is a virtual tunnel endpoint */
2346static struct device_type vxlan_type = {
2347 .name = "vxlan",
2348};
2349
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002350/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002351 * supply the listening VXLAN udp ports. Callers are expected
2352 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002353 */
2354void vxlan_get_rx_port(struct net_device *dev)
2355{
2356 struct vxlan_sock *vs;
2357 struct net *net = dev_net(dev);
2358 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2359 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002360 __be16 port;
2361 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002362
2363 spin_lock(&vn->sock_lock);
2364 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002365 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2366 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002367 sa_family = vs->sock->sk->sk_family;
2368 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2369 port);
2370 }
2371 }
2372 spin_unlock(&vn->sock_lock);
2373}
2374EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2375
stephen hemmingerd3428942012-10-01 12:32:35 +00002376/* Initialize the device structure. */
2377static void vxlan_setup(struct net_device *dev)
2378{
2379 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002380 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002381
2382 eth_hw_addr_random(dev);
2383 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002384 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002385 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002386 else
Cong Wang2853af62014-06-12 11:53:10 -07002387 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002388
2389 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002390 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002391 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2392
2393 dev->tx_queue_len = 0;
2394 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002395 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002396 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002397 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002398
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002399 dev->vlan_features = dev->features;
2400 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002401 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002402 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002403 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002404 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002405 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002406
stephen hemminger553675f2013-05-16 11:35:20 +00002407 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002408 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002409 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2410 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002411 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002412
2413 init_timer_deferrable(&vxlan->age_timer);
2414 vxlan->age_timer.function = vxlan_cleanup;
2415 vxlan->age_timer.data = (unsigned long) vxlan;
2416
stephen hemminger823aa872013-04-27 11:31:57 +00002417 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002418
stephen hemmingerd3428942012-10-01 12:32:35 +00002419 vxlan->dev = dev;
2420
2421 for (h = 0; h < FDB_HASH_SIZE; ++h)
2422 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2423}
2424
2425static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2426 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002427 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002428 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002429 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2430 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002431 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002432 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2433 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2434 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2435 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2436 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002437 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002438 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2439 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2440 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2441 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002442 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002443 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2444 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2445 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002446 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2447 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002448 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
stephen hemmingerd3428942012-10-01 12:32:35 +00002449};
2450
2451static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2452{
2453 if (tb[IFLA_ADDRESS]) {
2454 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2455 pr_debug("invalid link address (not ethernet)\n");
2456 return -EINVAL;
2457 }
2458
2459 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2460 pr_debug("invalid all zero ethernet address\n");
2461 return -EADDRNOTAVAIL;
2462 }
2463 }
2464
2465 if (!data)
2466 return -EINVAL;
2467
2468 if (data[IFLA_VXLAN_ID]) {
2469 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2470 if (id >= VXLAN_VID_MASK)
2471 return -ERANGE;
2472 }
2473
stephen hemminger05f47d62012-10-09 20:35:50 +00002474 if (data[IFLA_VXLAN_PORT_RANGE]) {
2475 const struct ifla_vxlan_port_range *p
2476 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2477
2478 if (ntohs(p->high) < ntohs(p->low)) {
2479 pr_debug("port range %u .. %u not valid\n",
2480 ntohs(p->low), ntohs(p->high));
2481 return -EINVAL;
2482 }
2483 }
2484
stephen hemmingerd3428942012-10-01 12:32:35 +00002485 return 0;
2486}
2487
Yan Burman1b13c972013-01-29 23:43:07 +00002488static void vxlan_get_drvinfo(struct net_device *netdev,
2489 struct ethtool_drvinfo *drvinfo)
2490{
2491 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2492 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2493}
2494
2495static const struct ethtool_ops vxlan_ethtool_ops = {
2496 .get_drvinfo = vxlan_get_drvinfo,
2497 .get_link = ethtool_op_get_link,
2498};
2499
stephen hemminger553675f2013-05-16 11:35:20 +00002500static void vxlan_del_work(struct work_struct *work)
2501{
2502 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002503 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002504 kfree_rcu(vs, rcu);
2505}
2506
Tom Herbert3ee64f32014-07-13 19:49:42 -07002507static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2508 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002509{
Cong Wange4c7ed42013-08-31 13:44:33 +08002510 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002511 struct udp_port_cfg udp_conf;
2512 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002513
Tom Herbert3ee64f32014-07-13 19:49:42 -07002514 memset(&udp_conf, 0, sizeof(udp_conf));
2515
2516 if (ipv6) {
2517 udp_conf.family = AF_INET6;
2518 udp_conf.use_udp6_tx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002519 !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002520 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002521 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002522 } else {
2523 udp_conf.family = AF_INET;
2524 udp_conf.local_ip.s_addr = INADDR_ANY;
2525 udp_conf.use_udp_checksums =
2526 !!(flags & VXLAN_F_UDP_CSUM);
Cong Wange4c7ed42013-08-31 13:44:33 +08002527 }
2528
Tom Herbert3ee64f32014-07-13 19:49:42 -07002529 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002530
Tom Herbert3ee64f32014-07-13 19:49:42 -07002531 /* Open UDP socket */
2532 err = udp_sock_create(net, &udp_conf, &sock);
2533 if (err < 0)
2534 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002535
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002536 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002537}
2538
2539/* Create new listen socket if needed */
2540static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002541 vxlan_rcv_t *rcv, void *data,
2542 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002543{
2544 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2545 struct vxlan_sock *vs;
2546 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002547 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002548 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002549 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002550
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002551 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002552 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002553 return ERR_PTR(-ENOMEM);
2554
2555 for (h = 0; h < VNI_HASH_SIZE; ++h)
2556 INIT_HLIST_HEAD(&vs->vni_list[h]);
2557
2558 INIT_WORK(&vs->del_work, vxlan_del_work);
2559
Tom Herbert3ee64f32014-07-13 19:49:42 -07002560 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002561 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002562 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002563 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002564 }
2565
Cong Wange4c7ed42013-08-31 13:44:33 +08002566 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002567 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002568 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002569 vs->data = data;
Tom Herbertdfd86452015-01-12 17:00:38 -08002570 vs->flags = flags;
stephen hemminger553675f2013-05-16 11:35:20 +00002571
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002572 /* Initialize the vxlan udp offloads structure */
2573 vs->udp_offloads.port = port;
2574 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2575 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2576
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002577 spin_lock(&vn->sock_lock);
2578 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002579 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002580 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002581
2582 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002583 tunnel_cfg.sk_user_data = vs;
2584 tunnel_cfg.encap_type = 1;
2585 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2586 tunnel_cfg.encap_destroy = NULL;
2587
2588 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002589
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002590 return vs;
2591}
stephen hemminger553675f2013-05-16 11:35:20 +00002592
Pravin B Shelar012a5722013-08-19 11:23:07 -07002593struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2594 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002595 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002596{
2597 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2598 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002599 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002600
Tom Herbert359a0ea2014-06-04 17:20:29 -07002601 vs = vxlan_socket_create(net, port, rcv, data, flags);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002602 if (!IS_ERR(vs))
2603 return vs;
2604
Pravin B Shelar012a5722013-08-19 11:23:07 -07002605 if (no_share) /* Return error if sharing is not allowed. */
2606 return vs;
2607
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002608 spin_lock(&vn->sock_lock);
Thomas Grafac5132d2015-01-15 03:53:56 +01002609 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port, flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002610 if (vs && ((vs->rcv != rcv) ||
2611 !atomic_add_unless(&vs->refcnt, 1, 0)))
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002612 vs = ERR_PTR(-EBUSY);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002613 spin_unlock(&vn->sock_lock);
2614
2615 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002616 vs = ERR_PTR(-EINVAL);
2617
stephen hemminger553675f2013-05-16 11:35:20 +00002618 return vs;
2619}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002620EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002621
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002622/* Scheduled at device creation to bind to a socket */
2623static void vxlan_sock_work(struct work_struct *work)
2624{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002625 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002626 struct net *net = vxlan->net;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002627 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002628 __be16 port = vxlan->dst_port;
2629 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002630
Tom Herbert359a0ea2014-06-04 17:20:29 -07002631 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002632 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002633 if (!IS_ERR(nvs))
2634 vxlan_vs_add_dev(nvs, vxlan);
2635 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002636
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002637 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002638}
2639
stephen hemmingerd3428942012-10-01 12:32:35 +00002640static int vxlan_newlink(struct net *net, struct net_device *dev,
2641 struct nlattr *tb[], struct nlattr *data[])
2642{
stephen hemminger553675f2013-05-16 11:35:20 +00002643 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002644 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002645 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002646 __u32 vni;
2647 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002648 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002649
2650 if (!data[IFLA_VXLAN_ID])
2651 return -EINVAL;
2652
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002653 vxlan->net = dev_net(dev);
2654
stephen hemmingerd3428942012-10-01 12:32:35 +00002655 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002656 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002657
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002658 /* Unless IPv6 is explicitly requested, assume IPv4 */
2659 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002660 if (data[IFLA_VXLAN_GROUP]) {
2661 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002662 } else if (data[IFLA_VXLAN_GROUP6]) {
2663 if (!IS_ENABLED(CONFIG_IPV6))
2664 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002665
Cong Wange4c7ed42013-08-31 13:44:33 +08002666 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2667 sizeof(struct in6_addr));
2668 dst->remote_ip.sa.sa_family = AF_INET6;
2669 use_ipv6 = true;
2670 }
2671
2672 if (data[IFLA_VXLAN_LOCAL]) {
2673 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2674 vxlan->saddr.sa.sa_family = AF_INET;
2675 } else if (data[IFLA_VXLAN_LOCAL6]) {
2676 if (!IS_ENABLED(CONFIG_IPV6))
2677 return -EPFNOSUPPORT;
2678
2679 /* TODO: respect scope id */
2680 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2681 sizeof(struct in6_addr));
2682 vxlan->saddr.sa.sa_family = AF_INET6;
2683 use_ipv6 = true;
2684 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002685
stephen hemminger34e02aa2012-10-09 20:35:53 +00002686 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002687 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002688 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002689 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002690
stephen hemminger34e02aa2012-10-09 20:35:53 +00002691 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002692 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002693 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002694 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002695
Cong Wange4c7ed42013-08-31 13:44:33 +08002696#if IS_ENABLED(CONFIG_IPV6)
2697 if (use_ipv6) {
2698 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2699 if (idev && idev->cnf.disable_ipv6) {
2700 pr_info("IPv6 is disabled via sysctl\n");
2701 return -EPERM;
2702 }
2703 vxlan->flags |= VXLAN_F_IPV6;
2704 }
2705#endif
2706
stephen hemminger34e02aa2012-10-09 20:35:53 +00002707 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002708 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002709
Cong Wang2853af62014-06-12 11:53:10 -07002710 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002711 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002712 } else if (use_ipv6)
2713 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002714
2715 if (data[IFLA_VXLAN_TOS])
2716 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2717
Vincent Bernatafb97182012-10-30 10:27:16 +00002718 if (data[IFLA_VXLAN_TTL])
2719 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2720
stephen hemmingerd3428942012-10-01 12:32:35 +00002721 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002722 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002723
2724 if (data[IFLA_VXLAN_AGEING])
2725 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2726 else
2727 vxlan->age_interval = FDB_AGE_DEFAULT;
2728
David Stevense4f67ad2012-11-20 02:50:14 +00002729 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2730 vxlan->flags |= VXLAN_F_PROXY;
2731
2732 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2733 vxlan->flags |= VXLAN_F_RSC;
2734
2735 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2736 vxlan->flags |= VXLAN_F_L2MISS;
2737
2738 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2739 vxlan->flags |= VXLAN_F_L3MISS;
2740
stephen hemmingerd3428942012-10-01 12:32:35 +00002741 if (data[IFLA_VXLAN_LIMIT])
2742 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2743
stephen hemminger05f47d62012-10-09 20:35:50 +00002744 if (data[IFLA_VXLAN_PORT_RANGE]) {
2745 const struct ifla_vxlan_port_range *p
2746 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2747 vxlan->port_min = ntohs(p->low);
2748 vxlan->port_max = ntohs(p->high);
2749 }
2750
stephen hemminger823aa872013-04-27 11:31:57 +00002751 if (data[IFLA_VXLAN_PORT])
2752 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2753
Tom Herbert359a0ea2014-06-04 17:20:29 -07002754 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2755 vxlan->flags |= VXLAN_F_UDP_CSUM;
2756
2757 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2758 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2759 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2760
2761 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2762 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2763 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2764
Tom Herbertdfd86452015-01-12 17:00:38 -08002765 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2766 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2767 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2768
2769 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2770 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2771 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2772
Thomas Graf35114942015-01-15 03:53:55 +01002773 if (data[IFLA_VXLAN_GBP])
2774 vxlan->flags |= VXLAN_F_GBP;
2775
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002776 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002777 vxlan->dst_port, vxlan->flags)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002778 pr_info("duplicate VNI %u\n", vni);
2779 return -EEXIST;
2780 }
2781
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002782 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002783
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002784 /* create an fdb entry for a valid default destination */
2785 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2786 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2787 &vxlan->default_dst.remote_ip,
2788 NUD_REACHABLE|NUD_PERMANENT,
2789 NLM_F_EXCL|NLM_F_CREATE,
2790 vxlan->dst_port,
2791 vxlan->default_dst.remote_vni,
2792 vxlan->default_dst.remote_ifindex,
2793 NTF_SELF);
2794 if (err)
2795 return err;
2796 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002797
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002798 err = register_netdevice(dev);
2799 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002800 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002801 return err;
2802 }
2803
stephen hemminger553675f2013-05-16 11:35:20 +00002804 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002805
2806 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002807}
2808
2809static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2810{
2811 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002812 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002813
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002814 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002815 if (!hlist_unhashed(&vxlan->hlist))
2816 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002817 spin_unlock(&vn->sock_lock);
2818
stephen hemminger553675f2013-05-16 11:35:20 +00002819 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002820 unregister_netdevice_queue(dev, head);
2821}
2822
2823static size_t vxlan_get_size(const struct net_device *dev)
2824{
2825
2826 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002827 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002828 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002829 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002830 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2831 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2832 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002833 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2834 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2835 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2836 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002837 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2838 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002839 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002840 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2841 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2842 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2843 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002844 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2845 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002846 0;
2847}
2848
2849static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2850{
2851 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002852 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002853 struct ifla_vxlan_port_range ports = {
2854 .low = htons(vxlan->port_min),
2855 .high = htons(vxlan->port_max),
2856 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002857
Atzm Watanabec7995c42013-04-16 02:50:52 +00002858 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002859 goto nla_put_failure;
2860
Cong Wange4c7ed42013-08-31 13:44:33 +08002861 if (!vxlan_addr_any(&dst->remote_ip)) {
2862 if (dst->remote_ip.sa.sa_family == AF_INET) {
2863 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2864 dst->remote_ip.sin.sin_addr.s_addr))
2865 goto nla_put_failure;
2866#if IS_ENABLED(CONFIG_IPV6)
2867 } else {
2868 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2869 &dst->remote_ip.sin6.sin6_addr))
2870 goto nla_put_failure;
2871#endif
2872 }
2873 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002874
Atzm Watanabec7995c42013-04-16 02:50:52 +00002875 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002876 goto nla_put_failure;
2877
Cong Wange4c7ed42013-08-31 13:44:33 +08002878 if (!vxlan_addr_any(&vxlan->saddr)) {
2879 if (vxlan->saddr.sa.sa_family == AF_INET) {
2880 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2881 vxlan->saddr.sin.sin_addr.s_addr))
2882 goto nla_put_failure;
2883#if IS_ENABLED(CONFIG_IPV6)
2884 } else {
2885 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2886 &vxlan->saddr.sin6.sin6_addr))
2887 goto nla_put_failure;
2888#endif
2889 }
2890 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002891
2892 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2893 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002894 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2895 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2896 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2897 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2898 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2899 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2900 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2901 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2902 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002903 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002904 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002905 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2906 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2907 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2908 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2909 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2910 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002911 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2912 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2913 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2914 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2915 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002916 goto nla_put_failure;
2917
stephen hemminger05f47d62012-10-09 20:35:50 +00002918 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2919 goto nla_put_failure;
2920
Thomas Graf35114942015-01-15 03:53:55 +01002921 if (vxlan->flags & VXLAN_F_GBP &&
2922 nla_put_flag(skb, IFLA_VXLAN_GBP))
2923 goto nla_put_failure;
2924
stephen hemmingerd3428942012-10-01 12:32:35 +00002925 return 0;
2926
2927nla_put_failure:
2928 return -EMSGSIZE;
2929}
2930
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002931static struct net *vxlan_get_link_net(const struct net_device *dev)
2932{
2933 struct vxlan_dev *vxlan = netdev_priv(dev);
2934
2935 return vxlan->net;
2936}
2937
stephen hemmingerd3428942012-10-01 12:32:35 +00002938static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2939 .kind = "vxlan",
2940 .maxtype = IFLA_VXLAN_MAX,
2941 .policy = vxlan_policy,
2942 .priv_size = sizeof(struct vxlan_dev),
2943 .setup = vxlan_setup,
2944 .validate = vxlan_validate,
2945 .newlink = vxlan_newlink,
2946 .dellink = vxlan_dellink,
2947 .get_size = vxlan_get_size,
2948 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002949 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002950};
2951
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002952static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2953 struct net_device *dev)
2954{
2955 struct vxlan_dev *vxlan, *next;
2956 LIST_HEAD(list_kill);
2957
2958 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2959 struct vxlan_rdst *dst = &vxlan->default_dst;
2960
2961 /* In case we created vxlan device with carrier
2962 * and we loose the carrier due to module unload
2963 * we also need to remove vxlan device. In other
2964 * cases, it's not necessary and remote_ifindex
2965 * is 0 here, so no matches.
2966 */
2967 if (dst->remote_ifindex == dev->ifindex)
2968 vxlan_dellink(vxlan->dev, &list_kill);
2969 }
2970
2971 unregister_netdevice_many(&list_kill);
2972}
2973
2974static int vxlan_lowerdev_event(struct notifier_block *unused,
2975 unsigned long event, void *ptr)
2976{
2977 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002978 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002979
Daniel Borkmann783c1462014-01-22 21:07:53 +01002980 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002981 vxlan_handle_lowerdev_unregister(vn, dev);
2982
2983 return NOTIFY_DONE;
2984}
2985
2986static struct notifier_block vxlan_notifier_block __read_mostly = {
2987 .notifier_call = vxlan_lowerdev_event,
2988};
2989
stephen hemmingerd3428942012-10-01 12:32:35 +00002990static __net_init int vxlan_init_net(struct net *net)
2991{
2992 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002993 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002994
stephen hemminger553675f2013-05-16 11:35:20 +00002995 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002996 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002997
stephen hemminger553675f2013-05-16 11:35:20 +00002998 for (h = 0; h < PORT_HASH_SIZE; ++h)
2999 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003000
3001 return 0;
3002}
3003
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003004static void __net_exit vxlan_exit_net(struct net *net)
3005{
3006 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3007 struct vxlan_dev *vxlan, *next;
3008 struct net_device *dev, *aux;
3009 LIST_HEAD(list);
3010
3011 rtnl_lock();
3012 for_each_netdev_safe(net, dev, aux)
3013 if (dev->rtnl_link_ops == &vxlan_link_ops)
3014 unregister_netdevice_queue(dev, &list);
3015
3016 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3017 /* If vxlan->dev is in the same netns, it has already been added
3018 * to the list by the previous loop.
3019 */
3020 if (!net_eq(dev_net(vxlan->dev), net))
3021 unregister_netdevice_queue(dev, &list);
3022 }
3023
3024 unregister_netdevice_many(&list);
3025 rtnl_unlock();
3026}
3027
stephen hemmingerd3428942012-10-01 12:32:35 +00003028static struct pernet_operations vxlan_net_ops = {
3029 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003030 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003031 .id = &vxlan_net_id,
3032 .size = sizeof(struct vxlan_net),
3033};
3034
3035static int __init vxlan_init_module(void)
3036{
3037 int rc;
3038
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003039 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3040 if (!vxlan_wq)
3041 return -ENOMEM;
3042
stephen hemmingerd3428942012-10-01 12:32:35 +00003043 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3044
Daniel Borkmann783c1462014-01-22 21:07:53 +01003045 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003046 if (rc)
3047 goto out1;
3048
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003049 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003050 if (rc)
3051 goto out2;
3052
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003053 rc = rtnl_link_register(&vxlan_link_ops);
3054 if (rc)
3055 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003056
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003057 return 0;
3058out3:
3059 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003060out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003061 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003062out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003063 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00003064 return rc;
3065}
Cong Wang7332a132013-05-27 22:35:53 +00003066late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003067
3068static void __exit vxlan_cleanup_module(void)
3069{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003070 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003071 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003072 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003073 unregister_pernet_subsys(&vxlan_net_ops);
3074 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003075}
3076module_exit(vxlan_cleanup_module);
3077
3078MODULE_LICENSE("GPL");
3079MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003080MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003081MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003082MODULE_ALIAS_RTNL_LINK("vxlan");