blob: 0346eaa6d236429ac736eb19c763dfb780bc4fdb [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
David Stevense4f67ad2012-11-20 02:50:14 +0000342 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000343 goto nla_put_failure;
344
Cong Wange4c7ed42013-08-31 13:44:33 +0800345 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000346 goto nla_put_failure;
347
stephen hemminger823aa872013-04-27 11:31:57 +0000348 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000349 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
350 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000351 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700352 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000353 goto nla_put_failure;
354 if (rdst->remote_ifindex &&
355 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000356 goto nla_put_failure;
357
358 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
359 ci.ndm_confirmed = 0;
360 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
361 ci.ndm_refcnt = 0;
362
363 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
364 goto nla_put_failure;
365
Johannes Berg053c0952015-01-16 22:09:00 +0100366 nlmsg_end(skb, nlh);
367 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000368
369nla_put_failure:
370 nlmsg_cancel(skb, nlh);
371 return -EMSGSIZE;
372}
373
374static inline size_t vxlan_nlmsg_size(void)
375{
376 return NLMSG_ALIGN(sizeof(struct ndmsg))
377 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800378 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000379 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000380 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
381 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000382 + nla_total_size(sizeof(struct nda_cacheinfo));
383}
384
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200385static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
386 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000387{
388 struct net *net = dev_net(vxlan->dev);
389 struct sk_buff *skb;
390 int err = -ENOBUFS;
391
392 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
393 if (skb == NULL)
394 goto errout;
395
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200396 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000397 if (err < 0) {
398 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
399 WARN_ON(err == -EMSGSIZE);
400 kfree_skb(skb);
401 goto errout;
402 }
403
404 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
405 return;
406errout:
407 if (err < 0)
408 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
409}
410
Cong Wange4c7ed42013-08-31 13:44:33 +0800411static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000412{
413 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700414 struct vxlan_fdb f = {
415 .state = NUD_STALE,
416 };
417 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800418 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700419 .remote_vni = VXLAN_N_VID,
420 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700421
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200422 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000423}
424
425static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
426{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700427 struct vxlan_fdb f = {
428 .state = NUD_STALE,
429 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200430 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000431
David Stevense4f67ad2012-11-20 02:50:14 +0000432 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
433
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200434 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000435}
436
stephen hemmingerd3428942012-10-01 12:32:35 +0000437/* Hash Ethernet address */
438static u32 eth_hash(const unsigned char *addr)
439{
440 u64 value = get_unaligned((u64 *)addr);
441
442 /* only want 6 bytes */
443#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000444 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000445#else
446 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000447#endif
448 return hash_64(value, FDB_HASH_BITS);
449}
450
451/* Hash chain to use given mac address */
452static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
453 const u8 *mac)
454{
455 return &vxlan->fdb_head[eth_hash(mac)];
456}
457
458/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000459static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000460 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000461{
462 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
463 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000464
Sasha Levinb67bfe02013-02-27 17:06:00 -0800465 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700466 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000467 return f;
468 }
469
470 return NULL;
471}
472
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000473static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
474 const u8 *mac)
475{
476 struct vxlan_fdb *f;
477
478 f = __vxlan_find_mac(vxlan, mac);
479 if (f)
480 f->used = jiffies;
481
482 return f;
483}
484
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300485/* caller should hold vxlan->hash_lock */
486static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800487 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300488 __u32 vni, __u32 ifindex)
489{
490 struct vxlan_rdst *rd;
491
492 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800493 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300494 rd->remote_port == port &&
495 rd->remote_vni == vni &&
496 rd->remote_ifindex == ifindex)
497 return rd;
498 }
499
500 return NULL;
501}
502
Thomas Richter906dc182013-07-19 17:20:07 +0200503/* Replace destination of unicast mac */
504static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800505 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200506{
507 struct vxlan_rdst *rd;
508
509 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
510 if (rd)
511 return 0;
512
513 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
514 if (!rd)
515 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800516 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200517 rd->remote_port = port;
518 rd->remote_vni = vni;
519 rd->remote_ifindex = ifindex;
520 return 1;
521}
522
David Stevens66817122013-03-15 04:35:51 +0000523/* Add/update destinations for multicast */
524static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200525 union vxlan_addr *ip, __be16 port, __u32 vni,
526 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000527{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700528 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000529
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300530 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
531 if (rd)
532 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700533
David Stevens66817122013-03-15 04:35:51 +0000534 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
535 if (rd == NULL)
536 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800537 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000538 rd->remote_port = port;
539 rd->remote_vni = vni;
540 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700541
542 list_add_tail_rcu(&rd->list, &f->remotes);
543
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200544 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000545 return 1;
546}
547
Tom Herbertdfd86452015-01-12 17:00:38 -0800548static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
549 unsigned int off,
550 struct vxlanhdr *vh, size_t hdrlen,
551 u32 data)
552{
553 size_t start, offset, plen;
554 __wsum delta;
555
556 if (skb->remcsum_offload)
557 return vh;
558
559 if (!NAPI_GRO_CB(skb)->csum_valid)
560 return NULL;
561
562 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
563 offset = start + ((data & VXLAN_RCO_UDP) ?
564 offsetof(struct udphdr, check) :
565 offsetof(struct tcphdr, check));
566
567 plen = hdrlen + offset + sizeof(u16);
568
569 /* Pull checksum that will be written */
570 if (skb_gro_header_hard(skb, off + plen)) {
571 vh = skb_gro_header_slow(skb, off + plen, off);
572 if (!vh)
573 return NULL;
574 }
575
576 delta = remcsum_adjust((void *)vh + hdrlen,
577 NAPI_GRO_CB(skb)->csum, start, offset);
578
579 /* Adjust skb->csum since we changed the packet */
580 skb->csum = csum_add(skb->csum, delta);
581 NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta);
582
583 skb->remcsum_offload = 1;
584
585 return vh;
586}
587
Tom Herberta2b12f32015-01-12 17:00:37 -0800588static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
589 struct sk_buff *skb,
590 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200591{
592 struct sk_buff *p, **pp = NULL;
593 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800594 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200595 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800596 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
597 udp_offloads);
598 u32 flags;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200599
600 off_vx = skb_gro_offset(skb);
601 hlen = off_vx + sizeof(*vh);
602 vh = skb_gro_header_fast(skb, off_vx);
603 if (skb_gro_header_hard(skb, hlen)) {
604 vh = skb_gro_header_slow(skb, hlen, off_vx);
605 if (unlikely(!vh))
606 goto out;
607 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200608
Tom Herbertdfd86452015-01-12 17:00:38 -0800609 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
610 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
611
612 flags = ntohl(vh->vx_flags);
613
614 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
615 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
616 ntohl(vh->vx_vni));
617
618 if (!vh)
619 goto out;
620 }
621
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200622 flush = 0;
623
624 for (p = *head; p; p = p->next) {
625 if (!NAPI_GRO_CB(p)->same_flow)
626 continue;
627
628 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100629 if (vh->vx_flags != vh2->vx_flags ||
630 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200631 NAPI_GRO_CB(p)->same_flow = 0;
632 continue;
633 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200634 }
635
Jesse Gross9b174d82014-12-30 19:10:15 -0800636 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200637
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200638out:
639 NAPI_GRO_CB(skb)->flush |= flush;
640
641 return pp;
642}
643
Tom Herberta2b12f32015-01-12 17:00:37 -0800644static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
645 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200646{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800647 udp_tunnel_gro_complete(skb, nhoff);
648
Jesse Gross9b174d82014-12-30 19:10:15 -0800649 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200650}
651
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700652/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200653static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700654{
655 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200656 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700657 struct net *net = sock_net(sk);
658 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700659 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200660 int err;
661
662 if (sa_family == AF_INET) {
663 err = udp_add_offload(&vs->udp_offloads);
664 if (err)
665 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
666 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700667
668 rcu_read_lock();
669 for_each_netdev_rcu(net, dev) {
670 if (dev->netdev_ops->ndo_add_vxlan_port)
671 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
672 port);
673 }
674 rcu_read_unlock();
675}
676
677/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200678static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700679{
680 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200681 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700682 struct net *net = sock_net(sk);
683 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700684 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700685
686 rcu_read_lock();
687 for_each_netdev_rcu(net, dev) {
688 if (dev->netdev_ops->ndo_del_vxlan_port)
689 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
690 port);
691 }
692 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200693
694 if (sa_family == AF_INET)
695 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700696}
697
stephen hemmingerd3428942012-10-01 12:32:35 +0000698/* Add new entry to forwarding table -- assumes lock held */
699static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800700 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000701 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000702 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000703 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000704{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200705 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000706 struct vxlan_fdb *f;
707 int notify = 0;
708
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000709 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000710 if (f) {
711 if (flags & NLM_F_EXCL) {
712 netdev_dbg(vxlan->dev,
713 "lost race to create %pM\n", mac);
714 return -EEXIST;
715 }
716 if (f->state != state) {
717 f->state = state;
718 f->updated = jiffies;
719 notify = 1;
720 }
David Stevensae884082013-04-19 00:36:26 +0000721 if (f->flags != ndm_flags) {
722 f->flags = ndm_flags;
723 f->updated = jiffies;
724 notify = 1;
725 }
Thomas Richter906dc182013-07-19 17:20:07 +0200726 if ((flags & NLM_F_REPLACE)) {
727 /* Only change unicasts */
728 if (!(is_multicast_ether_addr(f->eth_addr) ||
729 is_zero_ether_addr(f->eth_addr))) {
730 int rc = vxlan_fdb_replace(f, ip, port, vni,
731 ifindex);
732
733 if (rc < 0)
734 return rc;
735 notify |= rc;
736 } else
737 return -EOPNOTSUPP;
738 }
David Stevens66817122013-03-15 04:35:51 +0000739 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300740 (is_multicast_ether_addr(f->eth_addr) ||
741 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200742 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
743 &rd);
David Stevens66817122013-03-15 04:35:51 +0000744
745 if (rc < 0)
746 return rc;
747 notify |= rc;
748 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000749 } else {
750 if (!(flags & NLM_F_CREATE))
751 return -ENOENT;
752
753 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
754 return -ENOSPC;
755
Thomas Richter906dc182013-07-19 17:20:07 +0200756 /* Disallow replace to add a multicast entry */
757 if ((flags & NLM_F_REPLACE) &&
758 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
759 return -EOPNOTSUPP;
760
Cong Wange4c7ed42013-08-31 13:44:33 +0800761 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000762 f = kmalloc(sizeof(*f), GFP_ATOMIC);
763 if (!f)
764 return -ENOMEM;
765
766 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000767 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000768 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000769 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700770 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000771 memcpy(f->eth_addr, mac, ETH_ALEN);
772
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200773 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700774
stephen hemmingerd3428942012-10-01 12:32:35 +0000775 ++vxlan->addrcnt;
776 hlist_add_head_rcu(&f->hlist,
777 vxlan_fdb_head(vxlan, mac));
778 }
779
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200780 if (notify) {
781 if (rd == NULL)
782 rd = first_remote_rtnl(f);
783 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
784 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000785
786 return 0;
787}
788
Wei Yongjun6706c822013-04-11 19:00:35 +0000789static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000790{
791 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700792 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000793
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700794 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000795 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000796 kfree(f);
797}
798
stephen hemmingerd3428942012-10-01 12:32:35 +0000799static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
800{
801 netdev_dbg(vxlan->dev,
802 "delete %pM\n", f->eth_addr);
803
804 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200805 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000806
807 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000808 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000809}
810
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300811static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800812 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300813{
814 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800815 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300816
817 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800818 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
819 if (err)
820 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300821 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800822 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
823 if (remote->sa.sa_family == AF_INET) {
824 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
825 ip->sa.sa_family = AF_INET;
826#if IS_ENABLED(CONFIG_IPV6)
827 } else {
828 ip->sin6.sin6_addr = in6addr_any;
829 ip->sa.sa_family = AF_INET6;
830#endif
831 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300832 }
833
834 if (tb[NDA_PORT]) {
835 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
836 return -EINVAL;
837 *port = nla_get_be16(tb[NDA_PORT]);
838 } else {
839 *port = vxlan->dst_port;
840 }
841
842 if (tb[NDA_VNI]) {
843 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
844 return -EINVAL;
845 *vni = nla_get_u32(tb[NDA_VNI]);
846 } else {
847 *vni = vxlan->default_dst.remote_vni;
848 }
849
850 if (tb[NDA_IFINDEX]) {
851 struct net_device *tdev;
852
853 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
854 return -EINVAL;
855 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800856 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300857 if (!tdev)
858 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300859 } else {
860 *ifindex = 0;
861 }
862
863 return 0;
864}
865
stephen hemmingerd3428942012-10-01 12:32:35 +0000866/* Add static entry (via netlink) */
867static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
868 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100869 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000870{
871 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300872 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800873 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000874 __be16 port;
875 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000876 int err;
877
878 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
879 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
880 ndm->ndm_state);
881 return -EINVAL;
882 }
883
884 if (tb[NDA_DST] == NULL)
885 return -EINVAL;
886
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300887 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
888 if (err)
889 return err;
David Stevens66817122013-03-15 04:35:51 +0000890
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300891 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
892 return -EAFNOSUPPORT;
893
stephen hemmingerd3428942012-10-01 12:32:35 +0000894 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800895 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000896 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000897 spin_unlock_bh(&vxlan->hash_lock);
898
899 return err;
900}
901
902/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000903static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
904 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100905 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000906{
907 struct vxlan_dev *vxlan = netdev_priv(dev);
908 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300909 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800910 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300911 __be16 port;
912 u32 vni, ifindex;
913 int err;
914
915 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
916 if (err)
917 return err;
918
919 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000920
921 spin_lock_bh(&vxlan->hash_lock);
922 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300923 if (!f)
924 goto out;
925
Cong Wange4c7ed42013-08-31 13:44:33 +0800926 if (!vxlan_addr_any(&ip)) {
927 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300928 if (!rd)
929 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000930 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300931
932 err = 0;
933
934 /* remove a destination if it's not the only one on the list,
935 * otherwise destroy the fdb entry
936 */
937 if (rd && !list_is_singular(&f->remotes)) {
938 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200939 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800940 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300941 goto out;
942 }
943
944 vxlan_fdb_destroy(vxlan, f);
945
946out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000947 spin_unlock_bh(&vxlan->hash_lock);
948
949 return err;
950}
951
952/* Dump forwarding table */
953static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400954 struct net_device *dev,
955 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000956{
957 struct vxlan_dev *vxlan = netdev_priv(dev);
958 unsigned int h;
959
960 for (h = 0; h < FDB_HASH_SIZE; ++h) {
961 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000962 int err;
963
Sasha Levinb67bfe02013-02-27 17:06:00 -0800964 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000965 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000966
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700967 if (idx < cb->args[0])
968 goto skip;
969
970 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000971 err = vxlan_fdb_info(skb, vxlan, f,
972 NETLINK_CB(cb->skb).portid,
973 cb->nlh->nlmsg_seq,
974 RTM_NEWNEIGH,
975 NLM_F_MULTI, rd);
976 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700977 goto out;
David Stevens66817122013-03-15 04:35:51 +0000978 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700979skip:
980 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000981 }
982 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700983out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000984 return idx;
985}
986
987/* Watch incoming packets to learn mapping between Ethernet address
988 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700989 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000990 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700991static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800992 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000993{
994 struct vxlan_dev *vxlan = netdev_priv(dev);
995 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000996
997 f = vxlan_find_mac(vxlan, src_mac);
998 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700999 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001000
Cong Wange4c7ed42013-08-31 13:44:33 +08001001 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001002 return false;
1003
1004 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -07001005 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -07001006 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001007
1008 if (net_ratelimit())
1009 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001010 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001011 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +00001012
Cong Wange4c7ed42013-08-31 13:44:33 +08001013 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001014 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001015 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001016 } else {
1017 /* learned new entry */
1018 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001019
1020 /* close off race between vxlan_flush and incoming packets */
1021 if (netif_running(dev))
1022 vxlan_fdb_create(vxlan, src_mac, src_ip,
1023 NUD_REACHABLE,
1024 NLM_F_EXCL|NLM_F_CREATE,
1025 vxlan->dst_port,
1026 vxlan->default_dst.remote_vni,
1027 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001028 spin_unlock(&vxlan->hash_lock);
1029 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001030
1031 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001032}
1033
stephen hemmingerd3428942012-10-01 12:32:35 +00001034/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001035static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001036{
stephen hemminger553675f2013-05-16 11:35:20 +00001037 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001038
Gao feng95ab0992013-12-10 16:37:33 +08001039 /* The vxlan_sock is only used by dev, leaving group has
1040 * no effect on other vxlan devices.
1041 */
1042 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1043 return false;
1044
stephen hemminger553675f2013-05-16 11:35:20 +00001045 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001046 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001047 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001048
Gao feng95ab0992013-12-10 16:37:33 +08001049 if (vxlan->vn_sock != dev->vn_sock)
1050 continue;
1051
1052 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1053 &dev->default_dst.remote_ip))
1054 continue;
1055
1056 if (vxlan->default_dst.remote_ifindex !=
1057 dev->default_dst.remote_ifindex)
1058 continue;
1059
1060 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001061 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001062
1063 return false;
1064}
1065
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001066static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001067{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001068 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001069}
1070
Pravin B Shelar012a5722013-08-19 11:23:07 -07001071void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001072{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001073 struct sock *sk = vs->sock->sk;
1074 struct net *net = sock_net(sk);
1075 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001076
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001077 if (!atomic_dec_and_test(&vs->refcnt))
1078 return;
1079
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001080 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001081 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001082 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001083 spin_unlock(&vn->sock_lock);
1084
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001085 queue_work(vxlan_wq, &vs->del_work);
1086}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001087EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001088
stephen hemminger3fc2de22013-07-18 08:40:15 -07001089/* Callback to update multicast group membership when first VNI on
1090 * multicast asddress is brought up
1091 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001092 */
stephen hemminger3fc2de22013-07-18 08:40:15 -07001093static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001094{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001095 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001096 struct vxlan_sock *vs = vxlan->vn_sock;
1097 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001098 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1099 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001100
stephen hemmingerd3428942012-10-01 12:32:35 +00001101 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001102 if (ip->sa.sa_family == AF_INET) {
1103 struct ip_mreqn mreq = {
1104 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1105 .imr_ifindex = ifindex,
1106 };
1107
1108 ip_mc_join_group(sk, &mreq);
1109#if IS_ENABLED(CONFIG_IPV6)
1110 } else {
1111 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1112 &ip->sin6.sin6_addr);
1113#endif
1114 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001115 release_sock(sk);
1116
Pravin B Shelar012a5722013-08-19 11:23:07 -07001117 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001118 dev_put(vxlan->dev);
1119}
1120
1121/* Inverse of vxlan_igmp_join when last VNI is brought down */
1122static void vxlan_igmp_leave(struct work_struct *work)
1123{
1124 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001125 struct vxlan_sock *vs = vxlan->vn_sock;
1126 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001127 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1128 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001129
1130 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001131 if (ip->sa.sa_family == AF_INET) {
1132 struct ip_mreqn mreq = {
1133 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1134 .imr_ifindex = ifindex,
1135 };
1136
1137 ip_mc_leave_group(sk, &mreq);
1138#if IS_ENABLED(CONFIG_IPV6)
1139 } else {
1140 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1141 &ip->sin6.sin6_addr);
1142#endif
1143 }
1144
stephen hemmingerd3428942012-10-01 12:32:35 +00001145 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001146
Pravin B Shelar012a5722013-08-19 11:23:07 -07001147 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001148 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001149}
1150
Tom Herbertdfd86452015-01-12 17:00:38 -08001151static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1152 size_t hdrlen, u32 data)
1153{
1154 size_t start, offset, plen;
1155 __wsum delta;
1156
1157 if (skb->remcsum_offload) {
1158 /* Already processed in GRO path */
1159 skb->remcsum_offload = 0;
1160 return vh;
1161 }
1162
1163 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1164 offset = start + ((data & VXLAN_RCO_UDP) ?
1165 offsetof(struct udphdr, check) :
1166 offsetof(struct tcphdr, check));
1167
1168 plen = hdrlen + offset + sizeof(u16);
1169
1170 if (!pskb_may_pull(skb, plen))
1171 return NULL;
1172
1173 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1174
1175 if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE))
1176 __skb_checksum_complete(skb);
1177
1178 delta = remcsum_adjust((void *)vh + hdrlen,
1179 skb->csum, start, offset);
1180
1181 /* Adjust skb->csum since we changed the packet */
1182 skb->csum = csum_add(skb->csum, delta);
1183
1184 return vh;
1185}
1186
stephen hemmingerd3428942012-10-01 12:32:35 +00001187/* Callback from net/ipv4/udp.c to receive packets */
1188static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1189{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001190 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001191 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001192 u32 flags, vni;
Thomas Graf35114942015-01-15 03:53:55 +01001193 struct vxlan_metadata md = {0};
stephen hemmingerd3428942012-10-01 12:32:35 +00001194
stephen hemmingerd3428942012-10-01 12:32:35 +00001195 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001196 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001197 goto error;
1198
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001199 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001200 flags = ntohl(vxh->vx_flags);
1201 vni = ntohl(vxh->vx_vni);
1202
1203 if (flags & VXLAN_HF_VNI) {
1204 flags &= ~VXLAN_HF_VNI;
1205 } else {
1206 /* VNI flag always required to be set */
1207 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001208 }
1209
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001210 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001211 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001212 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001213
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001214 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001215 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001216 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001217
Tom Herbertdfd86452015-01-12 17:00:38 -08001218 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1219 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1220 if (!vxh)
1221 goto drop;
1222
1223 flags &= ~VXLAN_HF_RCO;
1224 vni &= VXLAN_VID_MASK;
1225 }
1226
Thomas Graf35114942015-01-15 03:53:55 +01001227 /* For backwards compatibility, only allow reserved fields to be
1228 * used by VXLAN extensions if explicitly requested.
1229 */
1230 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1231 struct vxlanhdr_gbp *gbp;
1232
1233 gbp = (struct vxlanhdr_gbp *)vxh;
1234 md.gbp = ntohs(gbp->policy_id);
1235
1236 if (gbp->dont_learn)
1237 md.gbp |= VXLAN_GBP_DONT_LEARN;
1238
1239 if (gbp->policy_applied)
1240 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1241
1242 flags &= ~VXLAN_GBP_USED_BITS;
1243 }
1244
Tom Herbertdfd86452015-01-12 17:00:38 -08001245 if (flags || (vni & ~VXLAN_VID_MASK)) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001246 /* If there are any unprocessed flags remaining treat
1247 * this as a malformed packet. This behavior diverges from
1248 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1249 * in reserved fields are to be ignored. The approach here
1250 * maintains compatbility with previous stack code, and also
1251 * is more robust and provides a little more security in
1252 * adding extensions to VXLAN.
1253 */
1254
1255 goto bad_flags;
1256 }
1257
Thomas Graf35114942015-01-15 03:53:55 +01001258 md.vni = vxh->vx_vni;
1259 vs->rcv(vs, skb, &md);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001260 return 0;
1261
1262drop:
1263 /* Consume bad packet */
1264 kfree_skb(skb);
1265 return 0;
1266
Tom Herbert3bf39472015-01-08 12:31:18 -08001267bad_flags:
1268 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1269 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1270
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001271error:
1272 /* Return non vxlan pkt */
1273 return 1;
1274}
1275
Thomas Graf35114942015-01-15 03:53:55 +01001276static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1277 struct vxlan_metadata *md)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001278{
Cong Wange4c7ed42013-08-31 13:44:33 +08001279 struct iphdr *oip = NULL;
1280 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001281 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001282 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001283 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001284 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001285 int err = 0;
1286 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001287
Thomas Graf35114942015-01-15 03:53:55 +01001288 vni = ntohl(md->vni) >> 8;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001289 /* Is this VNI defined? */
1290 vxlan = vxlan_vs_find_vni(vs, vni);
1291 if (!vxlan)
1292 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001293
Cong Wange4c7ed42013-08-31 13:44:33 +08001294 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001295 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001296 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001297 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001298 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001299
1300 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001301 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001302 goto drop;
1303
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001304 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001305 if (remote_ip->sa.sa_family == AF_INET) {
1306 oip = ip_hdr(skb);
1307 saddr.sin.sin_addr.s_addr = oip->saddr;
1308 saddr.sa.sa_family = AF_INET;
1309#if IS_ENABLED(CONFIG_IPV6)
1310 } else {
1311 oip6 = ipv6_hdr(skb);
1312 saddr.sin6.sin6_addr = oip6->saddr;
1313 saddr.sa.sa_family = AF_INET6;
1314#endif
1315 }
1316
stephen hemminger26a41ae62013-06-17 12:09:58 -07001317 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001318 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001319 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001320
stephen hemmingerd3428942012-10-01 12:32:35 +00001321 skb_reset_network_header(skb);
Thomas Graf35114942015-01-15 03:53:55 +01001322 skb->mark = md->gbp;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001323
Cong Wange4c7ed42013-08-31 13:44:33 +08001324 if (oip6)
1325 err = IP6_ECN_decapsulate(oip6, skb);
1326 if (oip)
1327 err = IP_ECN_decapsulate(oip, skb);
1328
stephen hemmingerd3428942012-10-01 12:32:35 +00001329 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001330 if (log_ecn_error) {
1331 if (oip6)
1332 net_info_ratelimited("non-ECT from %pI6\n",
1333 &oip6->saddr);
1334 if (oip)
1335 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1336 &oip->saddr, oip->tos);
1337 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001338 if (err > 1) {
1339 ++vxlan->dev->stats.rx_frame_errors;
1340 ++vxlan->dev->stats.rx_errors;
1341 goto drop;
1342 }
1343 }
1344
Pravin B Shelare8171042013-03-25 14:49:46 +00001345 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001346 u64_stats_update_begin(&stats->syncp);
1347 stats->rx_packets++;
1348 stats->rx_bytes += skb->len;
1349 u64_stats_update_end(&stats->syncp);
1350
1351 netif_rx(skb);
1352
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001353 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001354drop:
1355 /* Consume bad packet */
1356 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001357}
1358
David Stevense4f67ad2012-11-20 02:50:14 +00001359static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1360{
1361 struct vxlan_dev *vxlan = netdev_priv(dev);
1362 struct arphdr *parp;
1363 u8 *arpptr, *sha;
1364 __be32 sip, tip;
1365 struct neighbour *n;
1366
1367 if (dev->flags & IFF_NOARP)
1368 goto out;
1369
1370 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1371 dev->stats.tx_dropped++;
1372 goto out;
1373 }
1374 parp = arp_hdr(skb);
1375
1376 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1377 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1378 parp->ar_pro != htons(ETH_P_IP) ||
1379 parp->ar_op != htons(ARPOP_REQUEST) ||
1380 parp->ar_hln != dev->addr_len ||
1381 parp->ar_pln != 4)
1382 goto out;
1383 arpptr = (u8 *)parp + sizeof(struct arphdr);
1384 sha = arpptr;
1385 arpptr += dev->addr_len; /* sha */
1386 memcpy(&sip, arpptr, sizeof(sip));
1387 arpptr += sizeof(sip);
1388 arpptr += dev->addr_len; /* tha */
1389 memcpy(&tip, arpptr, sizeof(tip));
1390
1391 if (ipv4_is_loopback(tip) ||
1392 ipv4_is_multicast(tip))
1393 goto out;
1394
1395 n = neigh_lookup(&arp_tbl, &tip, dev);
1396
1397 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001398 struct vxlan_fdb *f;
1399 struct sk_buff *reply;
1400
1401 if (!(n->nud_state & NUD_CONNECTED)) {
1402 neigh_release(n);
1403 goto out;
1404 }
1405
1406 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001407 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001408 /* bridge-local neighbor */
1409 neigh_release(n);
1410 goto out;
1411 }
1412
1413 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1414 n->ha, sha);
1415
1416 neigh_release(n);
1417
David Stevens73461352014-03-18 12:32:29 -04001418 if (reply == NULL)
1419 goto out;
1420
David Stevense4f67ad2012-11-20 02:50:14 +00001421 skb_reset_mac_header(reply);
1422 __skb_pull(reply, skb_network_offset(reply));
1423 reply->ip_summed = CHECKSUM_UNNECESSARY;
1424 reply->pkt_type = PACKET_HOST;
1425
1426 if (netif_rx_ni(reply) == NET_RX_DROP)
1427 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001428 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1429 union vxlan_addr ipa = {
1430 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001431 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001432 };
1433
1434 vxlan_ip_miss(dev, &ipa);
1435 }
David Stevense4f67ad2012-11-20 02:50:14 +00001436out:
1437 consume_skb(skb);
1438 return NETDEV_TX_OK;
1439}
1440
Cong Wangf564f452013-08-31 13:44:36 +08001441#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001442static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1443 struct neighbour *n, bool isrouter)
1444{
1445 struct net_device *dev = request->dev;
1446 struct sk_buff *reply;
1447 struct nd_msg *ns, *na;
1448 struct ipv6hdr *pip6;
1449 u8 *daddr;
1450 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1451 int ns_olen;
1452 int i, len;
1453
1454 if (dev == NULL)
1455 return NULL;
1456
1457 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1458 sizeof(*na) + na_olen + dev->needed_tailroom;
1459 reply = alloc_skb(len, GFP_ATOMIC);
1460 if (reply == NULL)
1461 return NULL;
1462
1463 reply->protocol = htons(ETH_P_IPV6);
1464 reply->dev = dev;
1465 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1466 skb_push(reply, sizeof(struct ethhdr));
1467 skb_set_mac_header(reply, 0);
1468
1469 ns = (struct nd_msg *)skb_transport_header(request);
1470
1471 daddr = eth_hdr(request)->h_source;
1472 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1473 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1474 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1475 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1476 break;
1477 }
1478 }
1479
1480 /* Ethernet header */
1481 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1482 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1483 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1484 reply->protocol = htons(ETH_P_IPV6);
1485
1486 skb_pull(reply, sizeof(struct ethhdr));
1487 skb_set_network_header(reply, 0);
1488 skb_put(reply, sizeof(struct ipv6hdr));
1489
1490 /* IPv6 header */
1491
1492 pip6 = ipv6_hdr(reply);
1493 memset(pip6, 0, sizeof(struct ipv6hdr));
1494 pip6->version = 6;
1495 pip6->priority = ipv6_hdr(request)->priority;
1496 pip6->nexthdr = IPPROTO_ICMPV6;
1497 pip6->hop_limit = 255;
1498 pip6->daddr = ipv6_hdr(request)->saddr;
1499 pip6->saddr = *(struct in6_addr *)n->primary_key;
1500
1501 skb_pull(reply, sizeof(struct ipv6hdr));
1502 skb_set_transport_header(reply, 0);
1503
1504 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1505
1506 /* Neighbor Advertisement */
1507 memset(na, 0, sizeof(*na)+na_olen);
1508 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1509 na->icmph.icmp6_router = isrouter;
1510 na->icmph.icmp6_override = 1;
1511 na->icmph.icmp6_solicited = 1;
1512 na->target = ns->target;
1513 ether_addr_copy(&na->opt[2], n->ha);
1514 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1515 na->opt[1] = na_olen >> 3;
1516
1517 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1518 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1519 csum_partial(na, sizeof(*na)+na_olen, 0));
1520
1521 pip6->payload_len = htons(sizeof(*na)+na_olen);
1522
1523 skb_push(reply, sizeof(struct ipv6hdr));
1524
1525 reply->ip_summed = CHECKSUM_UNNECESSARY;
1526
1527 return reply;
1528}
1529
Cong Wangf564f452013-08-31 13:44:36 +08001530static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1531{
1532 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001533 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001534 const struct ipv6hdr *iphdr;
1535 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001536 struct neighbour *n;
1537 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001538
1539 in6_dev = __in6_dev_get(dev);
1540 if (!in6_dev)
1541 goto out;
1542
Cong Wangf564f452013-08-31 13:44:36 +08001543 iphdr = ipv6_hdr(skb);
1544 saddr = &iphdr->saddr;
1545 daddr = &iphdr->daddr;
1546
Cong Wangf564f452013-08-31 13:44:36 +08001547 msg = (struct nd_msg *)skb_transport_header(skb);
1548 if (msg->icmph.icmp6_code != 0 ||
1549 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1550 goto out;
1551
David Stevens4b29dba2014-03-24 10:39:58 -04001552 if (ipv6_addr_loopback(daddr) ||
1553 ipv6_addr_is_multicast(&msg->target))
1554 goto out;
1555
1556 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001557
1558 if (n) {
1559 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001560 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001561
1562 if (!(n->nud_state & NUD_CONNECTED)) {
1563 neigh_release(n);
1564 goto out;
1565 }
1566
1567 f = vxlan_find_mac(vxlan, n->ha);
1568 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1569 /* bridge-local neighbor */
1570 neigh_release(n);
1571 goto out;
1572 }
1573
David Stevens4b29dba2014-03-24 10:39:58 -04001574 reply = vxlan_na_create(skb, n,
1575 !!(f ? f->flags & NTF_ROUTER : 0));
1576
Cong Wangf564f452013-08-31 13:44:36 +08001577 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001578
1579 if (reply == NULL)
1580 goto out;
1581
1582 if (netif_rx_ni(reply) == NET_RX_DROP)
1583 dev->stats.rx_dropped++;
1584
Cong Wangf564f452013-08-31 13:44:36 +08001585 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001586 union vxlan_addr ipa = {
1587 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001588 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001589 };
1590
Cong Wangf564f452013-08-31 13:44:36 +08001591 vxlan_ip_miss(dev, &ipa);
1592 }
1593
1594out:
1595 consume_skb(skb);
1596 return NETDEV_TX_OK;
1597}
1598#endif
1599
David Stevense4f67ad2012-11-20 02:50:14 +00001600static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1601{
1602 struct vxlan_dev *vxlan = netdev_priv(dev);
1603 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001604
1605 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1606 return false;
1607
1608 n = NULL;
1609 switch (ntohs(eth_hdr(skb)->h_proto)) {
1610 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001611 {
1612 struct iphdr *pip;
1613
David Stevense4f67ad2012-11-20 02:50:14 +00001614 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1615 return false;
1616 pip = ip_hdr(skb);
1617 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001618 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1619 union vxlan_addr ipa = {
1620 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001621 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001622 };
1623
1624 vxlan_ip_miss(dev, &ipa);
1625 return false;
1626 }
1627
David Stevense4f67ad2012-11-20 02:50:14 +00001628 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001629 }
1630#if IS_ENABLED(CONFIG_IPV6)
1631 case ETH_P_IPV6:
1632 {
1633 struct ipv6hdr *pip6;
1634
1635 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1636 return false;
1637 pip6 = ipv6_hdr(skb);
1638 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1639 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1640 union vxlan_addr ipa = {
1641 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001642 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001643 };
1644
1645 vxlan_ip_miss(dev, &ipa);
1646 return false;
1647 }
1648
1649 break;
1650 }
1651#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001652 default:
1653 return false;
1654 }
1655
1656 if (n) {
1657 bool diff;
1658
Joe Perches7367d0b2013-09-01 11:51:23 -07001659 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001660 if (diff) {
1661 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1662 dev->addr_len);
1663 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1664 }
1665 neigh_release(n);
1666 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001667 }
1668
David Stevense4f67ad2012-11-20 02:50:14 +00001669 return false;
1670}
1671
Thomas Graf35114942015-01-15 03:53:55 +01001672static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, struct vxlan_sock *vs,
1673 struct vxlan_metadata *md)
1674{
1675 struct vxlanhdr_gbp *gbp;
1676
1677 gbp = (struct vxlanhdr_gbp *)vxh;
1678 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1679
1680 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1681 gbp->dont_learn = 1;
1682
1683 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1684 gbp->policy_applied = 1;
1685
1686 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1687}
1688
Cong Wange4c7ed42013-08-31 13:44:33 +08001689#if IS_ENABLED(CONFIG_IPV6)
Nicolas Dichtel11796182013-09-02 15:34:55 +02001690static int vxlan6_xmit_skb(struct vxlan_sock *vs,
Cong Wange4c7ed42013-08-31 13:44:33 +08001691 struct dst_entry *dst, struct sk_buff *skb,
1692 struct net_device *dev, struct in6_addr *saddr,
1693 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Thomas Graf35114942015-01-15 03:53:55 +01001694 __be16 src_port, __be16 dst_port,
1695 struct vxlan_metadata *md, bool xnet)
Cong Wange4c7ed42013-08-31 13:44:33 +08001696{
Cong Wange4c7ed42013-08-31 13:44:33 +08001697 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001698 int min_headroom;
1699 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001700 bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
Tom Herbertdfd86452015-01-12 17:00:38 -08001701 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1702 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001703
Tom Herbertdfd86452015-01-12 17:00:38 -08001704 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1705 skb->ip_summed == CHECKSUM_PARTIAL) {
1706 int csum_start = skb_checksum_start_offset(skb);
1707
1708 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1709 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1710 (skb->csum_offset == offsetof(struct udphdr, check) ||
1711 skb->csum_offset == offsetof(struct tcphdr, check))) {
1712 udp_sum = false;
1713 type |= SKB_GSO_TUNNEL_REMCSUM;
1714 }
1715 }
1716
1717 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001718 if (IS_ERR(skb)) {
1719 err = -EINVAL;
1720 goto err;
1721 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001722
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001723 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001724
Cong Wange4c7ed42013-08-31 13:44:33 +08001725 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1726 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001727 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001728
1729 /* Need space for new headers (invalidates iph ptr) */
1730 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001731 if (unlikely(err)) {
1732 kfree_skb(skb);
1733 goto err;
1734 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001735
Jiri Pirko59682502014-11-19 14:04:59 +01001736 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001737 if (WARN_ON(!skb)) {
1738 err = -ENOMEM;
1739 goto err;
1740 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001741
1742 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001743 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001744 vxh->vx_vni = md->vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001745
Tom Herbertdfd86452015-01-12 17:00:38 -08001746 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1747 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1748 VXLAN_RCO_SHIFT;
1749
1750 if (skb->csum_offset == offsetof(struct udphdr, check))
1751 data |= VXLAN_RCO_UDP;
1752
1753 vxh->vx_vni |= htonl(data);
1754 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1755
1756 if (!skb_is_gso(skb)) {
1757 skb->ip_summed = CHECKSUM_NONE;
1758 skb->encapsulation = 0;
1759 }
1760 }
1761
Thomas Graf35114942015-01-15 03:53:55 +01001762 if (vs->flags & VXLAN_F_GBP)
1763 vxlan_build_gbp_hdr(vxh, vs, md);
1764
Tom Herbert996c9fd2014-09-29 20:22:33 -07001765 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1766
Andy Zhouacbf74a2014-09-16 17:31:18 -07001767 udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
1768 ttl, src_port, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001769 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001770err:
1771 dst_release(dst);
1772 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001773}
1774#endif
1775
Nicolas Dichtel11796182013-09-02 15:34:55 +02001776int vxlan_xmit_skb(struct vxlan_sock *vs,
Pravin B Shelar49560532013-08-19 11:23:17 -07001777 struct rtable *rt, struct sk_buff *skb,
1778 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Thomas Graf35114942015-01-15 03:53:55 +01001779 __be16 src_port, __be16 dst_port,
1780 struct vxlan_metadata *md, bool xnet)
Pravin B Shelar49560532013-08-19 11:23:17 -07001781{
1782 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001783 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001784 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001785 bool udp_sum = !vs->sock->sk->sk_no_check_tx;
Tom Herbertdfd86452015-01-12 17:00:38 -08001786 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1787 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001788
Tom Herbertdfd86452015-01-12 17:00:38 -08001789 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1790 skb->ip_summed == CHECKSUM_PARTIAL) {
1791 int csum_start = skb_checksum_start_offset(skb);
1792
1793 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1794 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1795 (skb->csum_offset == offsetof(struct udphdr, check) ||
1796 skb->csum_offset == offsetof(struct tcphdr, check))) {
1797 udp_sum = false;
1798 type |= SKB_GSO_TUNNEL_REMCSUM;
1799 }
1800 }
1801
1802 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001803 if (IS_ERR(skb))
Pravin B Shelar74f47272014-12-23 16:20:36 -08001804 return PTR_ERR(skb);
Pravin B Shelar49560532013-08-19 11:23:17 -07001805
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001806 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001807 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001808 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001809
1810 /* Need space for new headers (invalidates iph ptr) */
1811 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001812 if (unlikely(err)) {
1813 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001814 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001815 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001816
Jiri Pirko59682502014-11-19 14:04:59 +01001817 skb = vlan_hwaccel_push_inside(skb);
1818 if (WARN_ON(!skb))
1819 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001820
Pravin B Shelar49560532013-08-19 11:23:17 -07001821 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001822 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001823 vxh->vx_vni = md->vni;
Pravin B Shelar49560532013-08-19 11:23:17 -07001824
Tom Herbertdfd86452015-01-12 17:00:38 -08001825 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1826 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1827 VXLAN_RCO_SHIFT;
1828
1829 if (skb->csum_offset == offsetof(struct udphdr, check))
1830 data |= VXLAN_RCO_UDP;
1831
1832 vxh->vx_vni |= htonl(data);
1833 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1834
1835 if (!skb_is_gso(skb)) {
1836 skb->ip_summed = CHECKSUM_NONE;
1837 skb->encapsulation = 0;
1838 }
1839 }
1840
Thomas Graf35114942015-01-15 03:53:55 +01001841 if (vs->flags & VXLAN_F_GBP)
1842 vxlan_build_gbp_hdr(vxh, vs, md);
1843
Tom Herbert996c9fd2014-09-29 20:22:33 -07001844 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1845
Andy Zhouacbf74a2014-09-16 17:31:18 -07001846 return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
1847 ttl, df, src_port, dst_port, xnet);
Pravin B Shelar49560532013-08-19 11:23:17 -07001848}
1849EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1850
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001851/* Bypass encapsulation if the destination is local */
1852static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1853 struct vxlan_dev *dst_vxlan)
1854{
Li RongQing8f849852014-01-04 13:57:59 +08001855 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001856 union vxlan_addr loopback;
1857 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001858 struct net_device *dev = skb->dev;
1859 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001860
Li RongQing8f849852014-01-04 13:57:59 +08001861 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1862 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001863 skb->pkt_type = PACKET_HOST;
1864 skb->encapsulation = 0;
1865 skb->dev = dst_vxlan->dev;
1866 __skb_pull(skb, skb_network_offset(skb));
1867
Cong Wange4c7ed42013-08-31 13:44:33 +08001868 if (remote_ip->sa.sa_family == AF_INET) {
1869 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1870 loopback.sa.sa_family = AF_INET;
1871#if IS_ENABLED(CONFIG_IPV6)
1872 } else {
1873 loopback.sin6.sin6_addr = in6addr_loopback;
1874 loopback.sa.sa_family = AF_INET6;
1875#endif
1876 }
1877
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001878 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001879 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001880
1881 u64_stats_update_begin(&tx_stats->syncp);
1882 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001883 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001884 u64_stats_update_end(&tx_stats->syncp);
1885
1886 if (netif_rx(skb) == NET_RX_SUCCESS) {
1887 u64_stats_update_begin(&rx_stats->syncp);
1888 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001889 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001890 u64_stats_update_end(&rx_stats->syncp);
1891 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001892 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001893 }
1894}
1895
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001896static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1897 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001898{
1899 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001900 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001901 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001902 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001903 union vxlan_addr *dst;
Thomas Graf35114942015-01-15 03:53:55 +01001904 struct vxlan_metadata md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001905 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001906 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001907 __be16 df = 0;
1908 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001909 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001910
stephen hemminger823aa872013-04-27 11:31:57 +00001911 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001912 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001913 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001914
Cong Wange4c7ed42013-08-31 13:44:33 +08001915 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001916 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001917 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001918 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001919 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001920 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001921 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001922 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001923
stephen hemmingerd3428942012-10-01 12:32:35 +00001924 old_iph = ip_hdr(skb);
1925
stephen hemmingerd3428942012-10-01 12:32:35 +00001926 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001927 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001928 ttl = 1;
1929
1930 tos = vxlan->tos;
1931 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001932 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001933
Tom Herbert535fb8d2014-07-01 21:32:49 -07001934 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1935 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001936
Cong Wange4c7ed42013-08-31 13:44:33 +08001937 if (dst->sa.sa_family == AF_INET) {
1938 memset(&fl4, 0, sizeof(fl4));
1939 fl4.flowi4_oif = rdst->remote_ifindex;
1940 fl4.flowi4_tos = RT_TOS(tos);
1941 fl4.daddr = dst->sin.sin_addr.s_addr;
1942 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001943
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001944 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001945 if (IS_ERR(rt)) {
1946 netdev_dbg(dev, "no route to %pI4\n",
1947 &dst->sin.sin_addr.s_addr);
1948 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001949 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001950 }
1951
1952 if (rt->dst.dev == dev) {
1953 netdev_dbg(dev, "circular route to %pI4\n",
1954 &dst->sin.sin_addr.s_addr);
1955 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001956 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001957 }
1958
1959 /* Bypass encapsulation if the destination is local */
1960 if (rt->rt_flags & RTCF_LOCAL &&
1961 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1962 struct vxlan_dev *dst_vxlan;
1963
1964 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001965 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01001966 dst->sa.sa_family, dst_port,
1967 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08001968 if (!dst_vxlan)
1969 goto tx_error;
1970 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1971 return;
1972 }
1973
1974 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1975 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Thomas Graf35114942015-01-15 03:53:55 +01001976 md.vni = htonl(vni << 8);
1977 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08001978
Andy Zhou3c4d1da2014-09-23 01:44:51 -07001979 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1980 fl4.saddr, dst->sin.sin_addr.s_addr,
Thomas Graf35114942015-01-15 03:53:55 +01001981 tos, ttl, df, src_port, dst_port, &md,
Andy Zhou3c4d1da2014-09-23 01:44:51 -07001982 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Pravin B Shelar74f47272014-12-23 16:20:36 -08001983 if (err < 0) {
1984 /* skb is already freed. */
1985 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001986 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001987 }
1988
Cong Wange4c7ed42013-08-31 13:44:33 +08001989 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1990#if IS_ENABLED(CONFIG_IPV6)
1991 } else {
1992 struct sock *sk = vxlan->vn_sock->sock->sk;
1993 struct dst_entry *ndst;
1994 struct flowi6 fl6;
1995 u32 flags;
1996
1997 memset(&fl6, 0, sizeof(fl6));
1998 fl6.flowi6_oif = rdst->remote_ifindex;
1999 fl6.daddr = dst->sin6.sin6_addr;
2000 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08002001 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08002002
2003 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2004 netdev_dbg(dev, "no route to %pI6\n",
2005 &dst->sin6.sin6_addr);
2006 dev->stats.tx_carrier_errors++;
2007 goto tx_error;
2008 }
2009
2010 if (ndst->dev == dev) {
2011 netdev_dbg(dev, "circular route to %pI6\n",
2012 &dst->sin6.sin6_addr);
2013 dst_release(ndst);
2014 dev->stats.collisions++;
2015 goto tx_error;
2016 }
2017
2018 /* Bypass encapsulation if the destination is local */
2019 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2020 if (flags & RTF_LOCAL &&
2021 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2022 struct vxlan_dev *dst_vxlan;
2023
2024 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002025 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002026 dst->sa.sa_family, dst_port,
2027 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002028 if (!dst_vxlan)
2029 goto tx_error;
2030 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2031 return;
2032 }
2033
2034 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Thomas Graf35114942015-01-15 03:53:55 +01002035 md.vni = htonl(vni << 8);
2036 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08002037
Nicolas Dichtel11796182013-09-02 15:34:55 +02002038 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08002039 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
Thomas Graf35114942015-01-15 03:53:55 +01002040 src_port, dst_port, &md,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002041 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Cong Wange4c7ed42013-08-31 13:44:33 +08002042#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002043 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002044
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002045 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002046
2047drop:
2048 dev->stats.tx_dropped++;
2049 goto tx_free;
2050
Pravin B Shelar49560532013-08-19 11:23:17 -07002051rt_tx_error:
2052 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002053tx_error:
2054 dev->stats.tx_errors++;
2055tx_free:
2056 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002057}
2058
David Stevens66817122013-03-15 04:35:51 +00002059/* Transmit local packets over Vxlan
2060 *
2061 * Outer IP header inherits ECN and DF from inner header.
2062 * Outer UDP destination is the VXLAN assigned port.
2063 * source port is based on hash of flow
2064 */
2065static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2066{
2067 struct vxlan_dev *vxlan = netdev_priv(dev);
2068 struct ethhdr *eth;
2069 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002070 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002071 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002072
2073 skb_reset_mac_header(skb);
2074 eth = eth_hdr(skb);
2075
Cong Wangf564f452013-08-31 13:44:36 +08002076 if ((vxlan->flags & VXLAN_F_PROXY)) {
2077 if (ntohs(eth->h_proto) == ETH_P_ARP)
2078 return arp_reduce(dev, skb);
2079#if IS_ENABLED(CONFIG_IPV6)
2080 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002081 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2082 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002083 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2084 struct nd_msg *msg;
2085
2086 msg = (struct nd_msg *)skb_transport_header(skb);
2087 if (msg->icmph.icmp6_code == 0 &&
2088 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2089 return neigh_reduce(dev, skb);
2090 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002091 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002092#endif
2093 }
David Stevens66817122013-03-15 04:35:51 +00002094
2095 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002096 did_rsc = false;
2097
2098 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002099 (ntohs(eth->h_proto) == ETH_P_IP ||
2100 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002101 did_rsc = route_shortcircuit(dev, skb);
2102 if (did_rsc)
2103 f = vxlan_find_mac(vxlan, eth->h_dest);
2104 }
2105
David Stevens66817122013-03-15 04:35:51 +00002106 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002107 f = vxlan_find_mac(vxlan, all_zeros_mac);
2108 if (f == NULL) {
2109 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2110 !is_multicast_ether_addr(eth->h_dest))
2111 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002112
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002113 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002114 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002115 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002116 }
David Stevens66817122013-03-15 04:35:51 +00002117 }
2118
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002119 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2120 struct sk_buff *skb1;
2121
Eric Dumazet8f646c92014-01-06 09:54:31 -08002122 if (!fdst) {
2123 fdst = rdst;
2124 continue;
2125 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002126 skb1 = skb_clone(skb, GFP_ATOMIC);
2127 if (skb1)
2128 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2129 }
2130
Eric Dumazet8f646c92014-01-06 09:54:31 -08002131 if (fdst)
2132 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2133 else
2134 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002135 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002136}
2137
stephen hemmingerd3428942012-10-01 12:32:35 +00002138/* Walk the forwarding table and purge stale entries */
2139static void vxlan_cleanup(unsigned long arg)
2140{
2141 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2142 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2143 unsigned int h;
2144
2145 if (!netif_running(vxlan->dev))
2146 return;
2147
2148 spin_lock_bh(&vxlan->hash_lock);
2149 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2150 struct hlist_node *p, *n;
2151 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2152 struct vxlan_fdb *f
2153 = container_of(p, struct vxlan_fdb, hlist);
2154 unsigned long timeout;
2155
stephen hemminger3c172862012-10-26 06:24:34 +00002156 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002157 continue;
2158
2159 timeout = f->used + vxlan->age_interval * HZ;
2160 if (time_before_eq(timeout, jiffies)) {
2161 netdev_dbg(vxlan->dev,
2162 "garbage collect %pM\n",
2163 f->eth_addr);
2164 f->state = NUD_STALE;
2165 vxlan_fdb_destroy(vxlan, f);
2166 } else if (time_before(timeout, next_timer))
2167 next_timer = timeout;
2168 }
2169 }
2170 spin_unlock_bh(&vxlan->hash_lock);
2171
2172 mod_timer(&vxlan->age_timer, next_timer);
2173}
2174
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002175static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2176{
2177 __u32 vni = vxlan->default_dst.remote_vni;
2178
2179 vxlan->vn_sock = vs;
2180 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2181}
2182
stephen hemmingerd3428942012-10-01 12:32:35 +00002183/* Setup stats when device is created */
2184static int vxlan_init(struct net_device *dev)
2185{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002186 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002187 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002188 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002189 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002190
WANG Cong1c213bd2014-02-13 11:46:28 -08002191 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002192 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002193 return -ENOMEM;
2194
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002195 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002196 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002197 vxlan->dst_port, vxlan->flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002198 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002199 /* If we have a socket with same port already, reuse it */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002200 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002201 } else {
2202 /* otherwise make new socket outside of RTNL */
2203 dev_hold(dev);
2204 queue_work(vxlan_wq, &vxlan->sock_work);
2205 }
2206 spin_unlock(&vn->sock_lock);
2207
stephen hemmingerd3428942012-10-01 12:32:35 +00002208 return 0;
2209}
2210
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002211static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002212{
2213 struct vxlan_fdb *f;
2214
2215 spin_lock_bh(&vxlan->hash_lock);
2216 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2217 if (f)
2218 vxlan_fdb_destroy(vxlan, f);
2219 spin_unlock_bh(&vxlan->hash_lock);
2220}
2221
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002222static void vxlan_uninit(struct net_device *dev)
2223{
2224 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002225 struct vxlan_sock *vs = vxlan->vn_sock;
2226
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002227 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002228
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002229 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07002230 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002231 free_percpu(dev->tstats);
2232}
2233
stephen hemmingerd3428942012-10-01 12:32:35 +00002234/* Start ageing timer and join group when device is brought up */
2235static int vxlan_open(struct net_device *dev)
2236{
2237 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002238 struct vxlan_sock *vs = vxlan->vn_sock;
2239
2240 /* socket hasn't been created */
2241 if (!vs)
2242 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002243
Gao feng79d4a942013-12-10 16:37:32 +08002244 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002245 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002246 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002247 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00002248 }
2249
2250 if (vxlan->age_interval)
2251 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2252
2253 return 0;
2254}
2255
2256/* Purge the forwarding table */
2257static void vxlan_flush(struct vxlan_dev *vxlan)
2258{
Cong Wang31fec5a2013-05-27 22:35:52 +00002259 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002260
2261 spin_lock_bh(&vxlan->hash_lock);
2262 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2263 struct hlist_node *p, *n;
2264 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2265 struct vxlan_fdb *f
2266 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002267 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2268 if (!is_zero_ether_addr(f->eth_addr))
2269 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002270 }
2271 }
2272 spin_unlock_bh(&vxlan->hash_lock);
2273}
2274
2275/* Cleanup timer and forwarding table on shutdown */
2276static int vxlan_stop(struct net_device *dev)
2277{
2278 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002279 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002280 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00002281
Cong Wange4c7ed42013-08-31 13:44:33 +08002282 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002283 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002284 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002285 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002286 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002287 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002288
2289 del_timer_sync(&vxlan->age_timer);
2290
2291 vxlan_flush(vxlan);
2292
2293 return 0;
2294}
2295
stephen hemmingerd3428942012-10-01 12:32:35 +00002296/* Stub, nothing needs to be done. */
2297static void vxlan_set_multicast_list(struct net_device *dev)
2298{
2299}
2300
Daniel Borkmann345010b2013-12-18 00:21:08 +01002301static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2302{
2303 struct vxlan_dev *vxlan = netdev_priv(dev);
2304 struct vxlan_rdst *dst = &vxlan->default_dst;
2305 struct net_device *lowerdev;
2306 int max_mtu;
2307
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002308 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002309 if (lowerdev == NULL)
2310 return eth_change_mtu(dev, new_mtu);
2311
2312 if (dst->remote_ip.sa.sa_family == AF_INET6)
2313 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2314 else
2315 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2316
2317 if (new_mtu < 68 || new_mtu > max_mtu)
2318 return -EINVAL;
2319
2320 dev->mtu = new_mtu;
2321 return 0;
2322}
2323
stephen hemmingerd3428942012-10-01 12:32:35 +00002324static const struct net_device_ops vxlan_netdev_ops = {
2325 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002326 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002327 .ndo_open = vxlan_open,
2328 .ndo_stop = vxlan_stop,
2329 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002330 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002331 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002332 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002333 .ndo_validate_addr = eth_validate_addr,
2334 .ndo_set_mac_address = eth_mac_addr,
2335 .ndo_fdb_add = vxlan_fdb_add,
2336 .ndo_fdb_del = vxlan_fdb_delete,
2337 .ndo_fdb_dump = vxlan_fdb_dump,
2338};
2339
2340/* Info for udev, that this is a virtual tunnel endpoint */
2341static struct device_type vxlan_type = {
2342 .name = "vxlan",
2343};
2344
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002345/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002346 * supply the listening VXLAN udp ports. Callers are expected
2347 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002348 */
2349void vxlan_get_rx_port(struct net_device *dev)
2350{
2351 struct vxlan_sock *vs;
2352 struct net *net = dev_net(dev);
2353 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2354 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002355 __be16 port;
2356 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002357
2358 spin_lock(&vn->sock_lock);
2359 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002360 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2361 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002362 sa_family = vs->sock->sk->sk_family;
2363 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2364 port);
2365 }
2366 }
2367 spin_unlock(&vn->sock_lock);
2368}
2369EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2370
stephen hemmingerd3428942012-10-01 12:32:35 +00002371/* Initialize the device structure. */
2372static void vxlan_setup(struct net_device *dev)
2373{
2374 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002375 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002376
2377 eth_hw_addr_random(dev);
2378 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002379 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002380 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002381 else
Cong Wang2853af62014-06-12 11:53:10 -07002382 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002383
2384 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002385 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002386 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2387
2388 dev->tx_queue_len = 0;
2389 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002390 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002391 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002392 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002393
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002394 dev->vlan_features = dev->features;
2395 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002396 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002397 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002398 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002399 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002400 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002401
stephen hemminger553675f2013-05-16 11:35:20 +00002402 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002403 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002404 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2405 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002406 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002407
2408 init_timer_deferrable(&vxlan->age_timer);
2409 vxlan->age_timer.function = vxlan_cleanup;
2410 vxlan->age_timer.data = (unsigned long) vxlan;
2411
stephen hemminger823aa872013-04-27 11:31:57 +00002412 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002413
stephen hemmingerd3428942012-10-01 12:32:35 +00002414 vxlan->dev = dev;
2415
2416 for (h = 0; h < FDB_HASH_SIZE; ++h)
2417 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2418}
2419
2420static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2421 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002422 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002423 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002424 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2425 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002426 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002427 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2428 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2429 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2430 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2431 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002432 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002433 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2434 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2435 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2436 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002437 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002438 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2439 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2440 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002441 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2442 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002443 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
stephen hemmingerd3428942012-10-01 12:32:35 +00002444};
2445
2446static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2447{
2448 if (tb[IFLA_ADDRESS]) {
2449 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2450 pr_debug("invalid link address (not ethernet)\n");
2451 return -EINVAL;
2452 }
2453
2454 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2455 pr_debug("invalid all zero ethernet address\n");
2456 return -EADDRNOTAVAIL;
2457 }
2458 }
2459
2460 if (!data)
2461 return -EINVAL;
2462
2463 if (data[IFLA_VXLAN_ID]) {
2464 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2465 if (id >= VXLAN_VID_MASK)
2466 return -ERANGE;
2467 }
2468
stephen hemminger05f47d62012-10-09 20:35:50 +00002469 if (data[IFLA_VXLAN_PORT_RANGE]) {
2470 const struct ifla_vxlan_port_range *p
2471 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2472
2473 if (ntohs(p->high) < ntohs(p->low)) {
2474 pr_debug("port range %u .. %u not valid\n",
2475 ntohs(p->low), ntohs(p->high));
2476 return -EINVAL;
2477 }
2478 }
2479
stephen hemmingerd3428942012-10-01 12:32:35 +00002480 return 0;
2481}
2482
Yan Burman1b13c972013-01-29 23:43:07 +00002483static void vxlan_get_drvinfo(struct net_device *netdev,
2484 struct ethtool_drvinfo *drvinfo)
2485{
2486 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2487 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2488}
2489
2490static const struct ethtool_ops vxlan_ethtool_ops = {
2491 .get_drvinfo = vxlan_get_drvinfo,
2492 .get_link = ethtool_op_get_link,
2493};
2494
stephen hemminger553675f2013-05-16 11:35:20 +00002495static void vxlan_del_work(struct work_struct *work)
2496{
2497 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002498 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002499 kfree_rcu(vs, rcu);
2500}
2501
Tom Herbert3ee64f32014-07-13 19:49:42 -07002502static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2503 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002504{
Cong Wange4c7ed42013-08-31 13:44:33 +08002505 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002506 struct udp_port_cfg udp_conf;
2507 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002508
Tom Herbert3ee64f32014-07-13 19:49:42 -07002509 memset(&udp_conf, 0, sizeof(udp_conf));
2510
2511 if (ipv6) {
2512 udp_conf.family = AF_INET6;
2513 udp_conf.use_udp6_tx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002514 !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002515 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002516 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002517 } else {
2518 udp_conf.family = AF_INET;
2519 udp_conf.local_ip.s_addr = INADDR_ANY;
2520 udp_conf.use_udp_checksums =
2521 !!(flags & VXLAN_F_UDP_CSUM);
Cong Wange4c7ed42013-08-31 13:44:33 +08002522 }
2523
Tom Herbert3ee64f32014-07-13 19:49:42 -07002524 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002525
Tom Herbert3ee64f32014-07-13 19:49:42 -07002526 /* Open UDP socket */
2527 err = udp_sock_create(net, &udp_conf, &sock);
2528 if (err < 0)
2529 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002530
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002531 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002532}
2533
2534/* Create new listen socket if needed */
2535static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002536 vxlan_rcv_t *rcv, void *data,
2537 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002538{
2539 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2540 struct vxlan_sock *vs;
2541 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002542 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002543 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002544 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002545
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002546 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002547 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002548 return ERR_PTR(-ENOMEM);
2549
2550 for (h = 0; h < VNI_HASH_SIZE; ++h)
2551 INIT_HLIST_HEAD(&vs->vni_list[h]);
2552
2553 INIT_WORK(&vs->del_work, vxlan_del_work);
2554
Tom Herbert3ee64f32014-07-13 19:49:42 -07002555 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002556 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002557 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002558 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002559 }
2560
Cong Wange4c7ed42013-08-31 13:44:33 +08002561 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002562 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002563 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002564 vs->data = data;
Tom Herbertdfd86452015-01-12 17:00:38 -08002565 vs->flags = flags;
stephen hemminger553675f2013-05-16 11:35:20 +00002566
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002567 /* Initialize the vxlan udp offloads structure */
2568 vs->udp_offloads.port = port;
2569 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2570 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2571
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002572 spin_lock(&vn->sock_lock);
2573 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002574 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002575 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002576
2577 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002578 tunnel_cfg.sk_user_data = vs;
2579 tunnel_cfg.encap_type = 1;
2580 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2581 tunnel_cfg.encap_destroy = NULL;
2582
2583 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002584
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002585 return vs;
2586}
stephen hemminger553675f2013-05-16 11:35:20 +00002587
Pravin B Shelar012a5722013-08-19 11:23:07 -07002588struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2589 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002590 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002591{
2592 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2593 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002594 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002595
Tom Herbert359a0ea2014-06-04 17:20:29 -07002596 vs = vxlan_socket_create(net, port, rcv, data, flags);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002597 if (!IS_ERR(vs))
2598 return vs;
2599
Pravin B Shelar012a5722013-08-19 11:23:07 -07002600 if (no_share) /* Return error if sharing is not allowed. */
2601 return vs;
2602
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002603 spin_lock(&vn->sock_lock);
Thomas Grafac5132d2015-01-15 03:53:56 +01002604 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port, flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002605 if (vs && ((vs->rcv != rcv) ||
2606 !atomic_add_unless(&vs->refcnt, 1, 0)))
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002607 vs = ERR_PTR(-EBUSY);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002608 spin_unlock(&vn->sock_lock);
2609
2610 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002611 vs = ERR_PTR(-EINVAL);
2612
stephen hemminger553675f2013-05-16 11:35:20 +00002613 return vs;
2614}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002615EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002616
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002617/* Scheduled at device creation to bind to a socket */
2618static void vxlan_sock_work(struct work_struct *work)
2619{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002620 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002621 struct net *net = vxlan->net;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002622 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002623 __be16 port = vxlan->dst_port;
2624 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002625
Tom Herbert359a0ea2014-06-04 17:20:29 -07002626 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002627 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002628 if (!IS_ERR(nvs))
2629 vxlan_vs_add_dev(nvs, vxlan);
2630 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002631
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002632 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002633}
2634
stephen hemmingerd3428942012-10-01 12:32:35 +00002635static int vxlan_newlink(struct net *net, struct net_device *dev,
2636 struct nlattr *tb[], struct nlattr *data[])
2637{
stephen hemminger553675f2013-05-16 11:35:20 +00002638 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002639 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002640 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002641 __u32 vni;
2642 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002643 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002644
2645 if (!data[IFLA_VXLAN_ID])
2646 return -EINVAL;
2647
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002648 vxlan->net = dev_net(dev);
2649
stephen hemmingerd3428942012-10-01 12:32:35 +00002650 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002651 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002652
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002653 /* Unless IPv6 is explicitly requested, assume IPv4 */
2654 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002655 if (data[IFLA_VXLAN_GROUP]) {
2656 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002657 } else if (data[IFLA_VXLAN_GROUP6]) {
2658 if (!IS_ENABLED(CONFIG_IPV6))
2659 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002660
Cong Wange4c7ed42013-08-31 13:44:33 +08002661 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2662 sizeof(struct in6_addr));
2663 dst->remote_ip.sa.sa_family = AF_INET6;
2664 use_ipv6 = true;
2665 }
2666
2667 if (data[IFLA_VXLAN_LOCAL]) {
2668 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2669 vxlan->saddr.sa.sa_family = AF_INET;
2670 } else if (data[IFLA_VXLAN_LOCAL6]) {
2671 if (!IS_ENABLED(CONFIG_IPV6))
2672 return -EPFNOSUPPORT;
2673
2674 /* TODO: respect scope id */
2675 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2676 sizeof(struct in6_addr));
2677 vxlan->saddr.sa.sa_family = AF_INET6;
2678 use_ipv6 = true;
2679 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002680
stephen hemminger34e02aa2012-10-09 20:35:53 +00002681 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002682 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002683 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002684 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002685
stephen hemminger34e02aa2012-10-09 20:35:53 +00002686 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002687 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002688 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002689 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002690
Cong Wange4c7ed42013-08-31 13:44:33 +08002691#if IS_ENABLED(CONFIG_IPV6)
2692 if (use_ipv6) {
2693 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2694 if (idev && idev->cnf.disable_ipv6) {
2695 pr_info("IPv6 is disabled via sysctl\n");
2696 return -EPERM;
2697 }
2698 vxlan->flags |= VXLAN_F_IPV6;
2699 }
2700#endif
2701
stephen hemminger34e02aa2012-10-09 20:35:53 +00002702 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002703 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002704
Cong Wang2853af62014-06-12 11:53:10 -07002705 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002706 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002707 } else if (use_ipv6)
2708 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002709
2710 if (data[IFLA_VXLAN_TOS])
2711 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2712
Vincent Bernatafb97182012-10-30 10:27:16 +00002713 if (data[IFLA_VXLAN_TTL])
2714 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2715
stephen hemmingerd3428942012-10-01 12:32:35 +00002716 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002717 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002718
2719 if (data[IFLA_VXLAN_AGEING])
2720 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2721 else
2722 vxlan->age_interval = FDB_AGE_DEFAULT;
2723
David Stevense4f67ad2012-11-20 02:50:14 +00002724 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2725 vxlan->flags |= VXLAN_F_PROXY;
2726
2727 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2728 vxlan->flags |= VXLAN_F_RSC;
2729
2730 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2731 vxlan->flags |= VXLAN_F_L2MISS;
2732
2733 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2734 vxlan->flags |= VXLAN_F_L3MISS;
2735
stephen hemmingerd3428942012-10-01 12:32:35 +00002736 if (data[IFLA_VXLAN_LIMIT])
2737 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2738
stephen hemminger05f47d62012-10-09 20:35:50 +00002739 if (data[IFLA_VXLAN_PORT_RANGE]) {
2740 const struct ifla_vxlan_port_range *p
2741 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2742 vxlan->port_min = ntohs(p->low);
2743 vxlan->port_max = ntohs(p->high);
2744 }
2745
stephen hemminger823aa872013-04-27 11:31:57 +00002746 if (data[IFLA_VXLAN_PORT])
2747 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2748
Tom Herbert359a0ea2014-06-04 17:20:29 -07002749 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2750 vxlan->flags |= VXLAN_F_UDP_CSUM;
2751
2752 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2753 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2754 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2755
2756 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2757 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2758 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2759
Tom Herbertdfd86452015-01-12 17:00:38 -08002760 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2761 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2762 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2763
2764 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2765 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2766 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2767
Thomas Graf35114942015-01-15 03:53:55 +01002768 if (data[IFLA_VXLAN_GBP])
2769 vxlan->flags |= VXLAN_F_GBP;
2770
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002771 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002772 vxlan->dst_port, vxlan->flags)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002773 pr_info("duplicate VNI %u\n", vni);
2774 return -EEXIST;
2775 }
2776
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002777 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002778
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002779 /* create an fdb entry for a valid default destination */
2780 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2781 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2782 &vxlan->default_dst.remote_ip,
2783 NUD_REACHABLE|NUD_PERMANENT,
2784 NLM_F_EXCL|NLM_F_CREATE,
2785 vxlan->dst_port,
2786 vxlan->default_dst.remote_vni,
2787 vxlan->default_dst.remote_ifindex,
2788 NTF_SELF);
2789 if (err)
2790 return err;
2791 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002792
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002793 err = register_netdevice(dev);
2794 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002795 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002796 return err;
2797 }
2798
stephen hemminger553675f2013-05-16 11:35:20 +00002799 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002800
2801 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002802}
2803
2804static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2805{
2806 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002807 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002808
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002809 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002810 if (!hlist_unhashed(&vxlan->hlist))
2811 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002812 spin_unlock(&vn->sock_lock);
2813
stephen hemminger553675f2013-05-16 11:35:20 +00002814 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002815 unregister_netdevice_queue(dev, head);
2816}
2817
2818static size_t vxlan_get_size(const struct net_device *dev)
2819{
2820
2821 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002822 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002823 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002824 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002825 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2826 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2827 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002828 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2829 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2830 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2831 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002832 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2833 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002834 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002835 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2836 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2837 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2838 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002839 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2840 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002841 0;
2842}
2843
2844static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2845{
2846 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002847 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002848 struct ifla_vxlan_port_range ports = {
2849 .low = htons(vxlan->port_min),
2850 .high = htons(vxlan->port_max),
2851 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002852
Atzm Watanabec7995c42013-04-16 02:50:52 +00002853 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002854 goto nla_put_failure;
2855
Cong Wange4c7ed42013-08-31 13:44:33 +08002856 if (!vxlan_addr_any(&dst->remote_ip)) {
2857 if (dst->remote_ip.sa.sa_family == AF_INET) {
2858 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2859 dst->remote_ip.sin.sin_addr.s_addr))
2860 goto nla_put_failure;
2861#if IS_ENABLED(CONFIG_IPV6)
2862 } else {
2863 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2864 &dst->remote_ip.sin6.sin6_addr))
2865 goto nla_put_failure;
2866#endif
2867 }
2868 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002869
Atzm Watanabec7995c42013-04-16 02:50:52 +00002870 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002871 goto nla_put_failure;
2872
Cong Wange4c7ed42013-08-31 13:44:33 +08002873 if (!vxlan_addr_any(&vxlan->saddr)) {
2874 if (vxlan->saddr.sa.sa_family == AF_INET) {
2875 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2876 vxlan->saddr.sin.sin_addr.s_addr))
2877 goto nla_put_failure;
2878#if IS_ENABLED(CONFIG_IPV6)
2879 } else {
2880 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2881 &vxlan->saddr.sin6.sin6_addr))
2882 goto nla_put_failure;
2883#endif
2884 }
2885 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002886
2887 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2888 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002889 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2890 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2891 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2892 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2893 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2894 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2895 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2896 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2897 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002898 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002899 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002900 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2901 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2902 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2903 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2904 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2905 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002906 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2907 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2908 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2909 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2910 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002911 goto nla_put_failure;
2912
stephen hemminger05f47d62012-10-09 20:35:50 +00002913 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2914 goto nla_put_failure;
2915
Thomas Graf35114942015-01-15 03:53:55 +01002916 if (vxlan->flags & VXLAN_F_GBP &&
2917 nla_put_flag(skb, IFLA_VXLAN_GBP))
2918 goto nla_put_failure;
2919
stephen hemmingerd3428942012-10-01 12:32:35 +00002920 return 0;
2921
2922nla_put_failure:
2923 return -EMSGSIZE;
2924}
2925
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002926static struct net *vxlan_get_link_net(const struct net_device *dev)
2927{
2928 struct vxlan_dev *vxlan = netdev_priv(dev);
2929
2930 return vxlan->net;
2931}
2932
stephen hemmingerd3428942012-10-01 12:32:35 +00002933static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2934 .kind = "vxlan",
2935 .maxtype = IFLA_VXLAN_MAX,
2936 .policy = vxlan_policy,
2937 .priv_size = sizeof(struct vxlan_dev),
2938 .setup = vxlan_setup,
2939 .validate = vxlan_validate,
2940 .newlink = vxlan_newlink,
2941 .dellink = vxlan_dellink,
2942 .get_size = vxlan_get_size,
2943 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002944 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002945};
2946
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002947static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2948 struct net_device *dev)
2949{
2950 struct vxlan_dev *vxlan, *next;
2951 LIST_HEAD(list_kill);
2952
2953 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2954 struct vxlan_rdst *dst = &vxlan->default_dst;
2955
2956 /* In case we created vxlan device with carrier
2957 * and we loose the carrier due to module unload
2958 * we also need to remove vxlan device. In other
2959 * cases, it's not necessary and remote_ifindex
2960 * is 0 here, so no matches.
2961 */
2962 if (dst->remote_ifindex == dev->ifindex)
2963 vxlan_dellink(vxlan->dev, &list_kill);
2964 }
2965
2966 unregister_netdevice_many(&list_kill);
2967}
2968
2969static int vxlan_lowerdev_event(struct notifier_block *unused,
2970 unsigned long event, void *ptr)
2971{
2972 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002973 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002974
Daniel Borkmann783c1462014-01-22 21:07:53 +01002975 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002976 vxlan_handle_lowerdev_unregister(vn, dev);
2977
2978 return NOTIFY_DONE;
2979}
2980
2981static struct notifier_block vxlan_notifier_block __read_mostly = {
2982 .notifier_call = vxlan_lowerdev_event,
2983};
2984
stephen hemmingerd3428942012-10-01 12:32:35 +00002985static __net_init int vxlan_init_net(struct net *net)
2986{
2987 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002988 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002989
stephen hemminger553675f2013-05-16 11:35:20 +00002990 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002991 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002992
stephen hemminger553675f2013-05-16 11:35:20 +00002993 for (h = 0; h < PORT_HASH_SIZE; ++h)
2994 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002995
2996 return 0;
2997}
2998
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002999static void __net_exit vxlan_exit_net(struct net *net)
3000{
3001 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3002 struct vxlan_dev *vxlan, *next;
3003 struct net_device *dev, *aux;
3004 LIST_HEAD(list);
3005
3006 rtnl_lock();
3007 for_each_netdev_safe(net, dev, aux)
3008 if (dev->rtnl_link_ops == &vxlan_link_ops)
3009 unregister_netdevice_queue(dev, &list);
3010
3011 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3012 /* If vxlan->dev is in the same netns, it has already been added
3013 * to the list by the previous loop.
3014 */
3015 if (!net_eq(dev_net(vxlan->dev), net))
3016 unregister_netdevice_queue(dev, &list);
3017 }
3018
3019 unregister_netdevice_many(&list);
3020 rtnl_unlock();
3021}
3022
stephen hemmingerd3428942012-10-01 12:32:35 +00003023static struct pernet_operations vxlan_net_ops = {
3024 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003025 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003026 .id = &vxlan_net_id,
3027 .size = sizeof(struct vxlan_net),
3028};
3029
3030static int __init vxlan_init_module(void)
3031{
3032 int rc;
3033
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003034 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3035 if (!vxlan_wq)
3036 return -ENOMEM;
3037
stephen hemmingerd3428942012-10-01 12:32:35 +00003038 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3039
Daniel Borkmann783c1462014-01-22 21:07:53 +01003040 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003041 if (rc)
3042 goto out1;
3043
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003044 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003045 if (rc)
3046 goto out2;
3047
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003048 rc = rtnl_link_register(&vxlan_link_ops);
3049 if (rc)
3050 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003051
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003052 return 0;
3053out3:
3054 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003055out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003056 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003057out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003058 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00003059 return rc;
3060}
Cong Wang7332a132013-05-27 22:35:53 +00003061late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003062
3063static void __exit vxlan_cleanup_module(void)
3064{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003065 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003066 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003067 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003068 unregister_pernet_subsys(&vxlan_net_ops);
3069 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003070}
3071module_exit(vxlan_cleanup_module);
3072
3073MODULE_LICENSE("GPL");
3074MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003075MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003076MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003077MODULE_ALIAS_RTNL_LINK("vxlan");