blob: 51baac725a48ca8d71d708834a94c62651370b90 [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
130 unsigned long age_interval;
131 struct timer_list age_timer;
132 spinlock_t hash_lock;
133 unsigned int addrcnt;
134 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000135
136 struct hlist_head fdb_head[FDB_HASH_SIZE];
137};
138
139/* salt for hash table */
140static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700141static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000142
Cong Wange4c7ed42013-08-31 13:44:33 +0800143#if IS_ENABLED(CONFIG_IPV6)
144static inline
145bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
146{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200147 if (a->sa.sa_family != b->sa.sa_family)
148 return false;
149 if (a->sa.sa_family == AF_INET6)
150 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
151 else
152 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800153}
154
155static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
156{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200157 if (ipa->sa.sa_family == AF_INET6)
158 return ipv6_addr_any(&ipa->sin6.sin6_addr);
159 else
160 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800161}
162
163static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
164{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200165 if (ipa->sa.sa_family == AF_INET6)
166 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
167 else
168 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800169}
170
171static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
172{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200173 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200174 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200175 ip->sa.sa_family = AF_INET6;
176 return 0;
177 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200178 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200179 ip->sa.sa_family = AF_INET;
180 return 0;
181 } else {
182 return -EAFNOSUPPORT;
183 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800184}
185
186static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200187 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800188{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200189 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200190 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200191 else
Jiri Benc930345e2015-03-29 16:59:25 +0200192 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800193}
194
195#else /* !CONFIG_IPV6 */
196
197static inline
198bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
199{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200200 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800201}
202
203static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
204{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200205 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800206}
207
208static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
209{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200210 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800211}
212
213static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
214{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200215 if (nla_len(nla) >= sizeof(struct in6_addr)) {
216 return -EAFNOSUPPORT;
217 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200218 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200219 ip->sa.sa_family = AF_INET;
220 return 0;
221 } else {
222 return -EAFNOSUPPORT;
223 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800224}
225
226static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200227 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800228{
Jiri Benc930345e2015-03-29 16:59:25 +0200229 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800230}
231#endif
232
stephen hemminger553675f2013-05-16 11:35:20 +0000233/* Virtual Network hash table head */
234static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
235{
236 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
237}
238
239/* Socket hash table head */
240static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000241{
242 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
243
stephen hemminger553675f2013-05-16 11:35:20 +0000244 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
245}
246
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700247/* First remote destination for a forwarding entry.
248 * Guaranteed to be non-NULL because remotes are never deleted.
249 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700250static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700251{
stephen hemminger5ca54612013-08-04 17:17:39 -0700252 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
253}
254
255static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
256{
257 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700258}
259
Thomas Grafac5132d2015-01-15 03:53:56 +0100260/* Find VXLAN socket based on network namespace, address family and UDP port
261 * and enabled unshareable flags.
262 */
263static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
264 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000265{
266 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800267
268 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000269
270 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200271 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Thomas Grafac5132d2015-01-15 03:53:56 +0100272 inet_sk(vs->sock->sk)->sk.sk_family == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800273 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000274 return vs;
275 }
276 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000277}
278
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700279static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000280{
281 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000282
stephen hemminger553675f2013-05-16 11:35:20 +0000283 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000284 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000285 return vxlan;
286 }
287
288 return NULL;
289}
290
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700291/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200292static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
Thomas Grafac5132d2015-01-15 03:53:56 +0100293 sa_family_t family, __be16 port,
294 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700295{
296 struct vxlan_sock *vs;
297
Thomas Grafac5132d2015-01-15 03:53:56 +0100298 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700299 if (!vs)
300 return NULL;
301
302 return vxlan_vs_find_vni(vs, id);
303}
304
stephen hemmingerd3428942012-10-01 12:32:35 +0000305/* Fill in neighbour message in skbuff. */
306static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700307 const struct vxlan_fdb *fdb,
308 u32 portid, u32 seq, int type, unsigned int flags,
309 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000310{
311 unsigned long now = jiffies;
312 struct nda_cacheinfo ci;
313 struct nlmsghdr *nlh;
314 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000315 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000316
317 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
318 if (nlh == NULL)
319 return -EMSGSIZE;
320
321 ndm = nlmsg_data(nlh);
322 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000323
324 send_eth = send_ip = true;
325
326 if (type == RTM_GETNEIGH) {
327 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800328 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000329 send_eth = !is_zero_ether_addr(fdb->eth_addr);
330 } else
331 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000332 ndm->ndm_state = fdb->state;
333 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000334 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800335 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000336
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100337 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100338 nla_put_s32(skb, NDA_LINK_NETNSID,
339 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100340 goto nla_put_failure;
341
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 */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100382 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000383 + nla_total_size(sizeof(struct nda_cacheinfo));
384}
385
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200386static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
387 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000388{
389 struct net *net = dev_net(vxlan->dev);
390 struct sk_buff *skb;
391 int err = -ENOBUFS;
392
393 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
394 if (skb == NULL)
395 goto errout;
396
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200397 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000398 if (err < 0) {
399 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
400 WARN_ON(err == -EMSGSIZE);
401 kfree_skb(skb);
402 goto errout;
403 }
404
405 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
406 return;
407errout:
408 if (err < 0)
409 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
410}
411
Cong Wange4c7ed42013-08-31 13:44:33 +0800412static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000413{
414 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700415 struct vxlan_fdb f = {
416 .state = NUD_STALE,
417 };
418 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800419 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700420 .remote_vni = VXLAN_N_VID,
421 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700422
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200423 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000424}
425
426static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
427{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700428 struct vxlan_fdb f = {
429 .state = NUD_STALE,
430 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200431 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000432
David Stevense4f67ad2012-11-20 02:50:14 +0000433 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
434
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200435 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000436}
437
stephen hemmingerd3428942012-10-01 12:32:35 +0000438/* Hash Ethernet address */
439static u32 eth_hash(const unsigned char *addr)
440{
441 u64 value = get_unaligned((u64 *)addr);
442
443 /* only want 6 bytes */
444#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000445 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000446#else
447 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000448#endif
449 return hash_64(value, FDB_HASH_BITS);
450}
451
452/* Hash chain to use given mac address */
453static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
454 const u8 *mac)
455{
456 return &vxlan->fdb_head[eth_hash(mac)];
457}
458
459/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000460static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000461 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000462{
463 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
464 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000465
Sasha Levinb67bfe02013-02-27 17:06:00 -0800466 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700467 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000468 return f;
469 }
470
471 return NULL;
472}
473
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000474static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
475 const u8 *mac)
476{
477 struct vxlan_fdb *f;
478
479 f = __vxlan_find_mac(vxlan, mac);
480 if (f)
481 f->used = jiffies;
482
483 return f;
484}
485
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300486/* caller should hold vxlan->hash_lock */
487static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800488 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300489 __u32 vni, __u32 ifindex)
490{
491 struct vxlan_rdst *rd;
492
493 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800494 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300495 rd->remote_port == port &&
496 rd->remote_vni == vni &&
497 rd->remote_ifindex == ifindex)
498 return rd;
499 }
500
501 return NULL;
502}
503
Thomas Richter906dc182013-07-19 17:20:07 +0200504/* Replace destination of unicast mac */
505static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800506 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200507{
508 struct vxlan_rdst *rd;
509
510 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
511 if (rd)
512 return 0;
513
514 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
515 if (!rd)
516 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800517 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200518 rd->remote_port = port;
519 rd->remote_vni = vni;
520 rd->remote_ifindex = ifindex;
521 return 1;
522}
523
David Stevens66817122013-03-15 04:35:51 +0000524/* Add/update destinations for multicast */
525static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200526 union vxlan_addr *ip, __be16 port, __u32 vni,
527 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000528{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700529 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000530
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300531 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
532 if (rd)
533 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700534
David Stevens66817122013-03-15 04:35:51 +0000535 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
536 if (rd == NULL)
537 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800538 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000539 rd->remote_port = port;
540 rd->remote_vni = vni;
541 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700542
543 list_add_tail_rcu(&rd->list, &f->remotes);
544
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200545 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000546 return 1;
547}
548
Tom Herbertdfd86452015-01-12 17:00:38 -0800549static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
550 unsigned int off,
551 struct vxlanhdr *vh, size_t hdrlen,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800552 u32 data, struct gro_remcsum *grc,
553 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800554{
555 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -0800556
557 if (skb->remcsum_offload)
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800558 return NULL;
Tom Herbertdfd86452015-01-12 17:00:38 -0800559
560 if (!NAPI_GRO_CB(skb)->csum_valid)
561 return NULL;
562
563 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
564 offset = start + ((data & VXLAN_RCO_UDP) ?
565 offsetof(struct udphdr, check) :
566 offsetof(struct tcphdr, check));
567
568 plen = hdrlen + offset + sizeof(u16);
569
570 /* Pull checksum that will be written */
571 if (skb_gro_header_hard(skb, off + plen)) {
572 vh = skb_gro_header_slow(skb, off + plen, off);
573 if (!vh)
574 return NULL;
575 }
576
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800577 skb_gro_remcsum_process(skb, (void *)vh + hdrlen,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800578 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800579
580 skb->remcsum_offload = 1;
581
582 return vh;
583}
584
Tom Herberta2b12f32015-01-12 17:00:37 -0800585static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
586 struct sk_buff *skb,
587 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200588{
589 struct sk_buff *p, **pp = NULL;
590 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800591 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200592 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800593 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
594 udp_offloads);
595 u32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800596 struct gro_remcsum grc;
597
598 skb_gro_remcsum_init(&grc);
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),
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800616 ntohl(vh->vx_vni), &grc,
617 !!(vs->flags &
618 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800619
620 if (!vh)
621 goto out;
622 }
623
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200624 flush = 0;
625
626 for (p = *head; p; p = p->next) {
627 if (!NAPI_GRO_CB(p)->same_flow)
628 continue;
629
630 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100631 if (vh->vx_flags != vh2->vx_flags ||
632 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200633 NAPI_GRO_CB(p)->same_flow = 0;
634 continue;
635 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200636 }
637
Jesse Gross9b174d82014-12-30 19:10:15 -0800638 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200639
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200640out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800641 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200642 NAPI_GRO_CB(skb)->flush |= flush;
643
644 return pp;
645}
646
Tom Herberta2b12f32015-01-12 17:00:37 -0800647static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
648 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200649{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800650 udp_tunnel_gro_complete(skb, nhoff);
651
Jesse Gross9b174d82014-12-30 19:10:15 -0800652 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200653}
654
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700655/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200656static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700657{
658 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200659 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700660 struct net *net = sock_net(sk);
661 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700662 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200663 int err;
664
665 if (sa_family == AF_INET) {
666 err = udp_add_offload(&vs->udp_offloads);
667 if (err)
668 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
669 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700670
671 rcu_read_lock();
672 for_each_netdev_rcu(net, dev) {
673 if (dev->netdev_ops->ndo_add_vxlan_port)
674 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
675 port);
676 }
677 rcu_read_unlock();
678}
679
680/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200681static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700682{
683 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200684 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700685 struct net *net = sock_net(sk);
686 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700687 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700688
689 rcu_read_lock();
690 for_each_netdev_rcu(net, dev) {
691 if (dev->netdev_ops->ndo_del_vxlan_port)
692 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
693 port);
694 }
695 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200696
697 if (sa_family == AF_INET)
698 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700699}
700
stephen hemmingerd3428942012-10-01 12:32:35 +0000701/* Add new entry to forwarding table -- assumes lock held */
702static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800703 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000704 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000705 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000706 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000707{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200708 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000709 struct vxlan_fdb *f;
710 int notify = 0;
711
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000712 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000713 if (f) {
714 if (flags & NLM_F_EXCL) {
715 netdev_dbg(vxlan->dev,
716 "lost race to create %pM\n", mac);
717 return -EEXIST;
718 }
719 if (f->state != state) {
720 f->state = state;
721 f->updated = jiffies;
722 notify = 1;
723 }
David Stevensae884082013-04-19 00:36:26 +0000724 if (f->flags != ndm_flags) {
725 f->flags = ndm_flags;
726 f->updated = jiffies;
727 notify = 1;
728 }
Thomas Richter906dc182013-07-19 17:20:07 +0200729 if ((flags & NLM_F_REPLACE)) {
730 /* Only change unicasts */
731 if (!(is_multicast_ether_addr(f->eth_addr) ||
732 is_zero_ether_addr(f->eth_addr))) {
733 int rc = vxlan_fdb_replace(f, ip, port, vni,
734 ifindex);
735
736 if (rc < 0)
737 return rc;
738 notify |= rc;
739 } else
740 return -EOPNOTSUPP;
741 }
David Stevens66817122013-03-15 04:35:51 +0000742 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300743 (is_multicast_ether_addr(f->eth_addr) ||
744 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200745 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
746 &rd);
David Stevens66817122013-03-15 04:35:51 +0000747
748 if (rc < 0)
749 return rc;
750 notify |= rc;
751 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000752 } else {
753 if (!(flags & NLM_F_CREATE))
754 return -ENOENT;
755
756 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
757 return -ENOSPC;
758
Thomas Richter906dc182013-07-19 17:20:07 +0200759 /* Disallow replace to add a multicast entry */
760 if ((flags & NLM_F_REPLACE) &&
761 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
762 return -EOPNOTSUPP;
763
Cong Wange4c7ed42013-08-31 13:44:33 +0800764 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000765 f = kmalloc(sizeof(*f), GFP_ATOMIC);
766 if (!f)
767 return -ENOMEM;
768
769 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000770 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000771 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000772 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700773 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000774 memcpy(f->eth_addr, mac, ETH_ALEN);
775
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200776 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700777
stephen hemmingerd3428942012-10-01 12:32:35 +0000778 ++vxlan->addrcnt;
779 hlist_add_head_rcu(&f->hlist,
780 vxlan_fdb_head(vxlan, mac));
781 }
782
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200783 if (notify) {
784 if (rd == NULL)
785 rd = first_remote_rtnl(f);
786 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
787 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000788
789 return 0;
790}
791
Wei Yongjun6706c822013-04-11 19:00:35 +0000792static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000793{
794 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700795 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000796
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700797 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000798 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000799 kfree(f);
800}
801
stephen hemmingerd3428942012-10-01 12:32:35 +0000802static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
803{
804 netdev_dbg(vxlan->dev,
805 "delete %pM\n", f->eth_addr);
806
807 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200808 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000809
810 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000811 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000812}
813
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300814static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800815 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300816{
817 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800818 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300819
820 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800821 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
822 if (err)
823 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300824 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800825 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
826 if (remote->sa.sa_family == AF_INET) {
827 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
828 ip->sa.sa_family = AF_INET;
829#if IS_ENABLED(CONFIG_IPV6)
830 } else {
831 ip->sin6.sin6_addr = in6addr_any;
832 ip->sa.sa_family = AF_INET6;
833#endif
834 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300835 }
836
837 if (tb[NDA_PORT]) {
838 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
839 return -EINVAL;
840 *port = nla_get_be16(tb[NDA_PORT]);
841 } else {
842 *port = vxlan->dst_port;
843 }
844
845 if (tb[NDA_VNI]) {
846 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
847 return -EINVAL;
848 *vni = nla_get_u32(tb[NDA_VNI]);
849 } else {
850 *vni = vxlan->default_dst.remote_vni;
851 }
852
853 if (tb[NDA_IFINDEX]) {
854 struct net_device *tdev;
855
856 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
857 return -EINVAL;
858 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800859 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300860 if (!tdev)
861 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300862 } else {
863 *ifindex = 0;
864 }
865
866 return 0;
867}
868
stephen hemmingerd3428942012-10-01 12:32:35 +0000869/* Add static entry (via netlink) */
870static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
871 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100872 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000873{
874 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300875 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800876 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000877 __be16 port;
878 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000879 int err;
880
881 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
882 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
883 ndm->ndm_state);
884 return -EINVAL;
885 }
886
887 if (tb[NDA_DST] == NULL)
888 return -EINVAL;
889
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300890 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
891 if (err)
892 return err;
David Stevens66817122013-03-15 04:35:51 +0000893
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300894 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
895 return -EAFNOSUPPORT;
896
stephen hemmingerd3428942012-10-01 12:32:35 +0000897 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800898 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000899 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000900 spin_unlock_bh(&vxlan->hash_lock);
901
902 return err;
903}
904
905/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000906static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
907 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100908 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000909{
910 struct vxlan_dev *vxlan = netdev_priv(dev);
911 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300912 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800913 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300914 __be16 port;
915 u32 vni, ifindex;
916 int err;
917
918 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
919 if (err)
920 return err;
921
922 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000923
924 spin_lock_bh(&vxlan->hash_lock);
925 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300926 if (!f)
927 goto out;
928
Cong Wange4c7ed42013-08-31 13:44:33 +0800929 if (!vxlan_addr_any(&ip)) {
930 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300931 if (!rd)
932 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000933 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300934
935 err = 0;
936
937 /* remove a destination if it's not the only one on the list,
938 * otherwise destroy the fdb entry
939 */
940 if (rd && !list_is_singular(&f->remotes)) {
941 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200942 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800943 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300944 goto out;
945 }
946
947 vxlan_fdb_destroy(vxlan, f);
948
949out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000950 spin_unlock_bh(&vxlan->hash_lock);
951
952 return err;
953}
954
955/* Dump forwarding table */
956static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400957 struct net_device *dev,
958 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000959{
960 struct vxlan_dev *vxlan = netdev_priv(dev);
961 unsigned int h;
962
963 for (h = 0; h < FDB_HASH_SIZE; ++h) {
964 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000965 int err;
966
Sasha Levinb67bfe02013-02-27 17:06:00 -0800967 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000968 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000969
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700970 if (idx < cb->args[0])
971 goto skip;
972
973 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000974 err = vxlan_fdb_info(skb, vxlan, f,
975 NETLINK_CB(cb->skb).portid,
976 cb->nlh->nlmsg_seq,
977 RTM_NEWNEIGH,
978 NLM_F_MULTI, rd);
979 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700980 goto out;
David Stevens66817122013-03-15 04:35:51 +0000981 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700982skip:
983 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000984 }
985 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700986out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000987 return idx;
988}
989
990/* Watch incoming packets to learn mapping between Ethernet address
991 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900992 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000993 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700994static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800995 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000996{
997 struct vxlan_dev *vxlan = netdev_priv(dev);
998 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000999
1000 f = vxlan_find_mac(vxlan, src_mac);
1001 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001002 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001003
Cong Wange4c7ed42013-08-31 13:44:33 +08001004 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001005 return false;
1006
1007 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -07001008 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -07001009 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001010
1011 if (net_ratelimit())
1012 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001013 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001014 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001015
Cong Wange4c7ed42013-08-31 13:44:33 +08001016 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001017 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001018 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001019 } else {
1020 /* learned new entry */
1021 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001022
1023 /* close off race between vxlan_flush and incoming packets */
1024 if (netif_running(dev))
1025 vxlan_fdb_create(vxlan, src_mac, src_ip,
1026 NUD_REACHABLE,
1027 NLM_F_EXCL|NLM_F_CREATE,
1028 vxlan->dst_port,
1029 vxlan->default_dst.remote_vni,
1030 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001031 spin_unlock(&vxlan->hash_lock);
1032 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001033
1034 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001035}
1036
stephen hemmingerd3428942012-10-01 12:32:35 +00001037/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001038static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001039{
stephen hemminger553675f2013-05-16 11:35:20 +00001040 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001041
Gao feng95ab0992013-12-10 16:37:33 +08001042 /* The vxlan_sock is only used by dev, leaving group has
1043 * no effect on other vxlan devices.
1044 */
1045 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1046 return false;
1047
stephen hemminger553675f2013-05-16 11:35:20 +00001048 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001049 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001050 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001051
Gao feng95ab0992013-12-10 16:37:33 +08001052 if (vxlan->vn_sock != dev->vn_sock)
1053 continue;
1054
1055 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1056 &dev->default_dst.remote_ip))
1057 continue;
1058
1059 if (vxlan->default_dst.remote_ifindex !=
1060 dev->default_dst.remote_ifindex)
1061 continue;
1062
1063 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001064 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001065
1066 return false;
1067}
1068
Pravin B Shelar012a5722013-08-19 11:23:07 -07001069void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001070{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001071 struct sock *sk = vs->sock->sk;
1072 struct net *net = sock_net(sk);
1073 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001074
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001075 if (!atomic_dec_and_test(&vs->refcnt))
1076 return;
1077
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001078 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001079 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001080 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001081 spin_unlock(&vn->sock_lock);
1082
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001083 queue_work(vxlan_wq, &vs->del_work);
1084}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001085EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001086
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001087/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001088 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001089 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001090static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001091{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001092 struct vxlan_sock *vs = vxlan->vn_sock;
1093 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001094 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1095 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001096 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001097
stephen hemmingerd3428942012-10-01 12:32:35 +00001098 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001099 if (ip->sa.sa_family == AF_INET) {
1100 struct ip_mreqn mreq = {
1101 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1102 .imr_ifindex = ifindex,
1103 };
1104
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001105 ret = ip_mc_join_group(sk, &mreq);
Cong Wange4c7ed42013-08-31 13:44:33 +08001106#if IS_ENABLED(CONFIG_IPV6)
1107 } else {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001108 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1109 &ip->sin6.sin6_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001110#endif
1111 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001112 release_sock(sk);
1113
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001114 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001115}
1116
1117/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001118static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001119{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001120 struct vxlan_sock *vs = vxlan->vn_sock;
1121 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001122 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1123 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001124 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001125
1126 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001127 if (ip->sa.sa_family == AF_INET) {
1128 struct ip_mreqn mreq = {
1129 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1130 .imr_ifindex = ifindex,
1131 };
1132
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001133 ret = ip_mc_leave_group(sk, &mreq);
Cong Wange4c7ed42013-08-31 13:44:33 +08001134#if IS_ENABLED(CONFIG_IPV6)
1135 } else {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001136 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1137 &ip->sin6.sin6_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001138#endif
1139 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001140 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001141
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001142 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001143}
1144
Tom Herbertdfd86452015-01-12 17:00:38 -08001145static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001146 size_t hdrlen, u32 data, bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -08001147{
1148 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -08001149
Tom Herbertdfd86452015-01-12 17:00:38 -08001150 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1151 offset = start + ((data & VXLAN_RCO_UDP) ?
1152 offsetof(struct udphdr, check) :
1153 offsetof(struct tcphdr, check));
1154
1155 plen = hdrlen + offset + sizeof(u16);
1156
1157 if (!pskb_may_pull(skb, plen))
1158 return NULL;
1159
1160 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1161
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001162 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset,
1163 nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -08001164
1165 return vh;
1166}
1167
stephen hemmingerd3428942012-10-01 12:32:35 +00001168/* Callback from net/ipv4/udp.c to receive packets */
1169static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1170{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001171 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001172 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001173 u32 flags, vni;
Thomas Graf35114942015-01-15 03:53:55 +01001174 struct vxlan_metadata md = {0};
stephen hemmingerd3428942012-10-01 12:32:35 +00001175
stephen hemmingerd3428942012-10-01 12:32:35 +00001176 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001177 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001178 goto error;
1179
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001180 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001181 flags = ntohl(vxh->vx_flags);
1182 vni = ntohl(vxh->vx_vni);
1183
1184 if (flags & VXLAN_HF_VNI) {
1185 flags &= ~VXLAN_HF_VNI;
1186 } else {
1187 /* VNI flag always required to be set */
1188 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001189 }
1190
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001191 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001192 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001193 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001194
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001195 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001196 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001197 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001198
Tom Herbertdfd86452015-01-12 17:00:38 -08001199 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001200 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni,
1201 !!(vs->flags & VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -08001202 if (!vxh)
1203 goto drop;
1204
1205 flags &= ~VXLAN_HF_RCO;
Alexey Kodanev40fb70f2015-03-13 19:13:53 +03001206 vni &= VXLAN_VNI_MASK;
Tom Herbertdfd86452015-01-12 17:00:38 -08001207 }
1208
Thomas Graf35114942015-01-15 03:53:55 +01001209 /* For backwards compatibility, only allow reserved fields to be
1210 * used by VXLAN extensions if explicitly requested.
1211 */
1212 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1213 struct vxlanhdr_gbp *gbp;
1214
1215 gbp = (struct vxlanhdr_gbp *)vxh;
1216 md.gbp = ntohs(gbp->policy_id);
1217
1218 if (gbp->dont_learn)
1219 md.gbp |= VXLAN_GBP_DONT_LEARN;
1220
1221 if (gbp->policy_applied)
1222 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1223
1224 flags &= ~VXLAN_GBP_USED_BITS;
1225 }
1226
Alexey Kodanev40fb70f2015-03-13 19:13:53 +03001227 if (flags || vni & ~VXLAN_VNI_MASK) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001228 /* If there are any unprocessed flags remaining treat
1229 * this as a malformed packet. This behavior diverges from
1230 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1231 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001232 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001233 * is more robust and provides a little more security in
1234 * adding extensions to VXLAN.
1235 */
1236
1237 goto bad_flags;
1238 }
1239
Thomas Graf35114942015-01-15 03:53:55 +01001240 md.vni = vxh->vx_vni;
1241 vs->rcv(vs, skb, &md);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001242 return 0;
1243
1244drop:
1245 /* Consume bad packet */
1246 kfree_skb(skb);
1247 return 0;
1248
Tom Herbert3bf39472015-01-08 12:31:18 -08001249bad_flags:
1250 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1251 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1252
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001253error:
1254 /* Return non vxlan pkt */
1255 return 1;
1256}
1257
Thomas Graf35114942015-01-15 03:53:55 +01001258static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1259 struct vxlan_metadata *md)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001260{
Cong Wange4c7ed42013-08-31 13:44:33 +08001261 struct iphdr *oip = NULL;
1262 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001263 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001264 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001265 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001266 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001267 int err = 0;
1268 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001269
Thomas Graf35114942015-01-15 03:53:55 +01001270 vni = ntohl(md->vni) >> 8;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001271 /* Is this VNI defined? */
1272 vxlan = vxlan_vs_find_vni(vs, vni);
1273 if (!vxlan)
1274 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001275
Cong Wange4c7ed42013-08-31 13:44:33 +08001276 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001277 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001278 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001279 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001280 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001281
1282 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001283 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001284 goto drop;
1285
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001286 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001287 if (remote_ip->sa.sa_family == AF_INET) {
1288 oip = ip_hdr(skb);
1289 saddr.sin.sin_addr.s_addr = oip->saddr;
1290 saddr.sa.sa_family = AF_INET;
1291#if IS_ENABLED(CONFIG_IPV6)
1292 } else {
1293 oip6 = ipv6_hdr(skb);
1294 saddr.sin6.sin6_addr = oip6->saddr;
1295 saddr.sa.sa_family = AF_INET6;
1296#endif
1297 }
1298
stephen hemminger26a41ae62013-06-17 12:09:58 -07001299 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001300 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001301 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001302
stephen hemmingerd3428942012-10-01 12:32:35 +00001303 skb_reset_network_header(skb);
Thomas Graf35114942015-01-15 03:53:55 +01001304 skb->mark = md->gbp;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001305
Cong Wange4c7ed42013-08-31 13:44:33 +08001306 if (oip6)
1307 err = IP6_ECN_decapsulate(oip6, skb);
1308 if (oip)
1309 err = IP_ECN_decapsulate(oip, skb);
1310
stephen hemmingerd3428942012-10-01 12:32:35 +00001311 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001312 if (log_ecn_error) {
1313 if (oip6)
1314 net_info_ratelimited("non-ECT from %pI6\n",
1315 &oip6->saddr);
1316 if (oip)
1317 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1318 &oip->saddr, oip->tos);
1319 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001320 if (err > 1) {
1321 ++vxlan->dev->stats.rx_frame_errors;
1322 ++vxlan->dev->stats.rx_errors;
1323 goto drop;
1324 }
1325 }
1326
Pravin B Shelare8171042013-03-25 14:49:46 +00001327 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001328 u64_stats_update_begin(&stats->syncp);
1329 stats->rx_packets++;
1330 stats->rx_bytes += skb->len;
1331 u64_stats_update_end(&stats->syncp);
1332
1333 netif_rx(skb);
1334
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001335 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001336drop:
1337 /* Consume bad packet */
1338 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001339}
1340
David Stevense4f67ad2012-11-20 02:50:14 +00001341static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1342{
1343 struct vxlan_dev *vxlan = netdev_priv(dev);
1344 struct arphdr *parp;
1345 u8 *arpptr, *sha;
1346 __be32 sip, tip;
1347 struct neighbour *n;
1348
1349 if (dev->flags & IFF_NOARP)
1350 goto out;
1351
1352 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1353 dev->stats.tx_dropped++;
1354 goto out;
1355 }
1356 parp = arp_hdr(skb);
1357
1358 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1359 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1360 parp->ar_pro != htons(ETH_P_IP) ||
1361 parp->ar_op != htons(ARPOP_REQUEST) ||
1362 parp->ar_hln != dev->addr_len ||
1363 parp->ar_pln != 4)
1364 goto out;
1365 arpptr = (u8 *)parp + sizeof(struct arphdr);
1366 sha = arpptr;
1367 arpptr += dev->addr_len; /* sha */
1368 memcpy(&sip, arpptr, sizeof(sip));
1369 arpptr += sizeof(sip);
1370 arpptr += dev->addr_len; /* tha */
1371 memcpy(&tip, arpptr, sizeof(tip));
1372
1373 if (ipv4_is_loopback(tip) ||
1374 ipv4_is_multicast(tip))
1375 goto out;
1376
1377 n = neigh_lookup(&arp_tbl, &tip, dev);
1378
1379 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001380 struct vxlan_fdb *f;
1381 struct sk_buff *reply;
1382
1383 if (!(n->nud_state & NUD_CONNECTED)) {
1384 neigh_release(n);
1385 goto out;
1386 }
1387
1388 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001389 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001390 /* bridge-local neighbor */
1391 neigh_release(n);
1392 goto out;
1393 }
1394
1395 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1396 n->ha, sha);
1397
1398 neigh_release(n);
1399
David Stevens73461352014-03-18 12:32:29 -04001400 if (reply == NULL)
1401 goto out;
1402
David Stevense4f67ad2012-11-20 02:50:14 +00001403 skb_reset_mac_header(reply);
1404 __skb_pull(reply, skb_network_offset(reply));
1405 reply->ip_summed = CHECKSUM_UNNECESSARY;
1406 reply->pkt_type = PACKET_HOST;
1407
1408 if (netif_rx_ni(reply) == NET_RX_DROP)
1409 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001410 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1411 union vxlan_addr ipa = {
1412 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001413 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001414 };
1415
1416 vxlan_ip_miss(dev, &ipa);
1417 }
David Stevense4f67ad2012-11-20 02:50:14 +00001418out:
1419 consume_skb(skb);
1420 return NETDEV_TX_OK;
1421}
1422
Cong Wangf564f452013-08-31 13:44:36 +08001423#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001424static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1425 struct neighbour *n, bool isrouter)
1426{
1427 struct net_device *dev = request->dev;
1428 struct sk_buff *reply;
1429 struct nd_msg *ns, *na;
1430 struct ipv6hdr *pip6;
1431 u8 *daddr;
1432 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1433 int ns_olen;
1434 int i, len;
1435
1436 if (dev == NULL)
1437 return NULL;
1438
1439 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1440 sizeof(*na) + na_olen + dev->needed_tailroom;
1441 reply = alloc_skb(len, GFP_ATOMIC);
1442 if (reply == NULL)
1443 return NULL;
1444
1445 reply->protocol = htons(ETH_P_IPV6);
1446 reply->dev = dev;
1447 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1448 skb_push(reply, sizeof(struct ethhdr));
1449 skb_set_mac_header(reply, 0);
1450
1451 ns = (struct nd_msg *)skb_transport_header(request);
1452
1453 daddr = eth_hdr(request)->h_source;
1454 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1455 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1456 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1457 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1458 break;
1459 }
1460 }
1461
1462 /* Ethernet header */
1463 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1464 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1465 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1466 reply->protocol = htons(ETH_P_IPV6);
1467
1468 skb_pull(reply, sizeof(struct ethhdr));
1469 skb_set_network_header(reply, 0);
1470 skb_put(reply, sizeof(struct ipv6hdr));
1471
1472 /* IPv6 header */
1473
1474 pip6 = ipv6_hdr(reply);
1475 memset(pip6, 0, sizeof(struct ipv6hdr));
1476 pip6->version = 6;
1477 pip6->priority = ipv6_hdr(request)->priority;
1478 pip6->nexthdr = IPPROTO_ICMPV6;
1479 pip6->hop_limit = 255;
1480 pip6->daddr = ipv6_hdr(request)->saddr;
1481 pip6->saddr = *(struct in6_addr *)n->primary_key;
1482
1483 skb_pull(reply, sizeof(struct ipv6hdr));
1484 skb_set_transport_header(reply, 0);
1485
1486 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1487
1488 /* Neighbor Advertisement */
1489 memset(na, 0, sizeof(*na)+na_olen);
1490 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1491 na->icmph.icmp6_router = isrouter;
1492 na->icmph.icmp6_override = 1;
1493 na->icmph.icmp6_solicited = 1;
1494 na->target = ns->target;
1495 ether_addr_copy(&na->opt[2], n->ha);
1496 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1497 na->opt[1] = na_olen >> 3;
1498
1499 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1500 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1501 csum_partial(na, sizeof(*na)+na_olen, 0));
1502
1503 pip6->payload_len = htons(sizeof(*na)+na_olen);
1504
1505 skb_push(reply, sizeof(struct ipv6hdr));
1506
1507 reply->ip_summed = CHECKSUM_UNNECESSARY;
1508
1509 return reply;
1510}
1511
Cong Wangf564f452013-08-31 13:44:36 +08001512static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1513{
1514 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001515 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001516 const struct ipv6hdr *iphdr;
1517 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001518 struct neighbour *n;
1519 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001520
1521 in6_dev = __in6_dev_get(dev);
1522 if (!in6_dev)
1523 goto out;
1524
Cong Wangf564f452013-08-31 13:44:36 +08001525 iphdr = ipv6_hdr(skb);
1526 saddr = &iphdr->saddr;
1527 daddr = &iphdr->daddr;
1528
Cong Wangf564f452013-08-31 13:44:36 +08001529 msg = (struct nd_msg *)skb_transport_header(skb);
1530 if (msg->icmph.icmp6_code != 0 ||
1531 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1532 goto out;
1533
David Stevens4b29dba2014-03-24 10:39:58 -04001534 if (ipv6_addr_loopback(daddr) ||
1535 ipv6_addr_is_multicast(&msg->target))
1536 goto out;
1537
1538 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001539
1540 if (n) {
1541 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001542 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001543
1544 if (!(n->nud_state & NUD_CONNECTED)) {
1545 neigh_release(n);
1546 goto out;
1547 }
1548
1549 f = vxlan_find_mac(vxlan, n->ha);
1550 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1551 /* bridge-local neighbor */
1552 neigh_release(n);
1553 goto out;
1554 }
1555
David Stevens4b29dba2014-03-24 10:39:58 -04001556 reply = vxlan_na_create(skb, n,
1557 !!(f ? f->flags & NTF_ROUTER : 0));
1558
Cong Wangf564f452013-08-31 13:44:36 +08001559 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001560
1561 if (reply == NULL)
1562 goto out;
1563
1564 if (netif_rx_ni(reply) == NET_RX_DROP)
1565 dev->stats.rx_dropped++;
1566
Cong Wangf564f452013-08-31 13:44:36 +08001567 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001568 union vxlan_addr ipa = {
1569 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001570 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001571 };
1572
Cong Wangf564f452013-08-31 13:44:36 +08001573 vxlan_ip_miss(dev, &ipa);
1574 }
1575
1576out:
1577 consume_skb(skb);
1578 return NETDEV_TX_OK;
1579}
1580#endif
1581
David Stevense4f67ad2012-11-20 02:50:14 +00001582static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1583{
1584 struct vxlan_dev *vxlan = netdev_priv(dev);
1585 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001586
1587 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1588 return false;
1589
1590 n = NULL;
1591 switch (ntohs(eth_hdr(skb)->h_proto)) {
1592 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001593 {
1594 struct iphdr *pip;
1595
David Stevense4f67ad2012-11-20 02:50:14 +00001596 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1597 return false;
1598 pip = ip_hdr(skb);
1599 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001600 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1601 union vxlan_addr ipa = {
1602 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001603 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001604 };
1605
1606 vxlan_ip_miss(dev, &ipa);
1607 return false;
1608 }
1609
David Stevense4f67ad2012-11-20 02:50:14 +00001610 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001611 }
1612#if IS_ENABLED(CONFIG_IPV6)
1613 case ETH_P_IPV6:
1614 {
1615 struct ipv6hdr *pip6;
1616
1617 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1618 return false;
1619 pip6 = ipv6_hdr(skb);
1620 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1621 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1622 union vxlan_addr ipa = {
1623 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001624 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001625 };
1626
1627 vxlan_ip_miss(dev, &ipa);
1628 return false;
1629 }
1630
1631 break;
1632 }
1633#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001634 default:
1635 return false;
1636 }
1637
1638 if (n) {
1639 bool diff;
1640
Joe Perches7367d0b2013-09-01 11:51:23 -07001641 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001642 if (diff) {
1643 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1644 dev->addr_len);
1645 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1646 }
1647 neigh_release(n);
1648 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001649 }
1650
David Stevense4f67ad2012-11-20 02:50:14 +00001651 return false;
1652}
1653
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001654static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001655 struct vxlan_metadata *md)
1656{
1657 struct vxlanhdr_gbp *gbp;
1658
Thomas Grafdb79a622015-02-04 17:00:04 +01001659 if (!md->gbp)
1660 return;
1661
Thomas Graf35114942015-01-15 03:53:55 +01001662 gbp = (struct vxlanhdr_gbp *)vxh;
1663 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1664
1665 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1666 gbp->dont_learn = 1;
1667
1668 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1669 gbp->policy_applied = 1;
1670
1671 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1672}
1673
Cong Wange4c7ed42013-08-31 13:44:33 +08001674#if IS_ENABLED(CONFIG_IPV6)
David Miller79b16aa2015-04-05 22:19:09 -04001675static int vxlan6_xmit_skb(struct dst_entry *dst, struct sock *sk,
1676 struct sk_buff *skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001677 struct net_device *dev, struct in6_addr *saddr,
1678 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Thomas Graf35114942015-01-15 03:53:55 +01001679 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001680 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Cong Wange4c7ed42013-08-31 13:44:33 +08001681{
Cong Wange4c7ed42013-08-31 13:44:33 +08001682 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001683 int min_headroom;
1684 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001685 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbertdfd86452015-01-12 17:00:38 -08001686 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1687 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001688
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001689 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001690 skb->ip_summed == CHECKSUM_PARTIAL) {
1691 int csum_start = skb_checksum_start_offset(skb);
1692
1693 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1694 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1695 (skb->csum_offset == offsetof(struct udphdr, check) ||
1696 skb->csum_offset == offsetof(struct tcphdr, check))) {
1697 udp_sum = false;
1698 type |= SKB_GSO_TUNNEL_REMCSUM;
1699 }
1700 }
1701
1702 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001703 if (IS_ERR(skb)) {
1704 err = -EINVAL;
1705 goto err;
1706 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001707
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001708 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001709
Cong Wange4c7ed42013-08-31 13:44:33 +08001710 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1711 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001712 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001713
1714 /* Need space for new headers (invalidates iph ptr) */
1715 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001716 if (unlikely(err)) {
1717 kfree_skb(skb);
1718 goto err;
1719 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001720
Jiri Pirko59682502014-11-19 14:04:59 +01001721 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001722 if (WARN_ON(!skb)) {
1723 err = -ENOMEM;
1724 goto err;
1725 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001726
1727 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001728 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001729 vxh->vx_vni = md->vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001730
Tom Herbertdfd86452015-01-12 17:00:38 -08001731 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1732 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1733 VXLAN_RCO_SHIFT;
1734
1735 if (skb->csum_offset == offsetof(struct udphdr, check))
1736 data |= VXLAN_RCO_UDP;
1737
1738 vxh->vx_vni |= htonl(data);
1739 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1740
1741 if (!skb_is_gso(skb)) {
1742 skb->ip_summed = CHECKSUM_NONE;
1743 skb->encapsulation = 0;
1744 }
1745 }
1746
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001747 if (vxflags & VXLAN_F_GBP)
1748 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001749
Tom Herbert996c9fd2014-09-29 20:22:33 -07001750 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1751
David Miller79b16aa2015-04-05 22:19:09 -04001752 udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio,
Tom Herbertd998f8e2015-01-20 11:23:04 -08001753 ttl, src_port, dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001754 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
Cong Wange4c7ed42013-08-31 13:44:33 +08001755 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001756err:
1757 dst_release(dst);
1758 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001759}
1760#endif
1761
David Miller79b16aa2015-04-05 22:19:09 -04001762int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
Pravin B Shelar49560532013-08-19 11:23:17 -07001763 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Thomas Graf35114942015-01-15 03:53:55 +01001764 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001765 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Pravin B Shelar49560532013-08-19 11:23:17 -07001766{
1767 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001768 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001769 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001770 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
Tom Herbertdfd86452015-01-12 17:00:38 -08001771 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1772 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001773
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001774 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001775 skb->ip_summed == CHECKSUM_PARTIAL) {
1776 int csum_start = skb_checksum_start_offset(skb);
1777
1778 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1779 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1780 (skb->csum_offset == offsetof(struct udphdr, check) ||
1781 skb->csum_offset == offsetof(struct tcphdr, check))) {
1782 udp_sum = false;
1783 type |= SKB_GSO_TUNNEL_REMCSUM;
1784 }
1785 }
1786
1787 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001788 if (IS_ERR(skb))
Pravin B Shelar74f47272014-12-23 16:20:36 -08001789 return PTR_ERR(skb);
Pravin B Shelar49560532013-08-19 11:23:17 -07001790
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001791 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001792 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001793 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001794
1795 /* Need space for new headers (invalidates iph ptr) */
1796 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001797 if (unlikely(err)) {
1798 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001799 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001800 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001801
Jiri Pirko59682502014-11-19 14:04:59 +01001802 skb = vlan_hwaccel_push_inside(skb);
1803 if (WARN_ON(!skb))
1804 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001805
Pravin B Shelar49560532013-08-19 11:23:17 -07001806 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001807 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001808 vxh->vx_vni = md->vni;
Pravin B Shelar49560532013-08-19 11:23:17 -07001809
Tom Herbertdfd86452015-01-12 17:00:38 -08001810 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1811 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1812 VXLAN_RCO_SHIFT;
1813
1814 if (skb->csum_offset == offsetof(struct udphdr, check))
1815 data |= VXLAN_RCO_UDP;
1816
1817 vxh->vx_vni |= htonl(data);
1818 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1819
1820 if (!skb_is_gso(skb)) {
1821 skb->ip_summed = CHECKSUM_NONE;
1822 skb->encapsulation = 0;
1823 }
1824 }
1825
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001826 if (vxflags & VXLAN_F_GBP)
1827 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001828
Tom Herbert996c9fd2014-09-29 20:22:33 -07001829 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1830
David Miller79b16aa2015-04-05 22:19:09 -04001831 return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
Tom Herbertd998f8e2015-01-20 11:23:04 -08001832 ttl, df, src_port, dst_port, xnet,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001833 !(vxflags & VXLAN_F_UDP_CSUM));
Pravin B Shelar49560532013-08-19 11:23:17 -07001834}
1835EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1836
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001837/* Bypass encapsulation if the destination is local */
1838static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1839 struct vxlan_dev *dst_vxlan)
1840{
Li RongQing8f849852014-01-04 13:57:59 +08001841 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001842 union vxlan_addr loopback;
1843 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001844 struct net_device *dev = skb->dev;
1845 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001846
Li RongQing8f849852014-01-04 13:57:59 +08001847 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1848 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001849 skb->pkt_type = PACKET_HOST;
1850 skb->encapsulation = 0;
1851 skb->dev = dst_vxlan->dev;
1852 __skb_pull(skb, skb_network_offset(skb));
1853
Cong Wange4c7ed42013-08-31 13:44:33 +08001854 if (remote_ip->sa.sa_family == AF_INET) {
1855 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1856 loopback.sa.sa_family = AF_INET;
1857#if IS_ENABLED(CONFIG_IPV6)
1858 } else {
1859 loopback.sin6.sin6_addr = in6addr_loopback;
1860 loopback.sa.sa_family = AF_INET6;
1861#endif
1862 }
1863
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001864 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001865 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001866
1867 u64_stats_update_begin(&tx_stats->syncp);
1868 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001869 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001870 u64_stats_update_end(&tx_stats->syncp);
1871
1872 if (netif_rx(skb) == NET_RX_SUCCESS) {
1873 u64_stats_update_begin(&rx_stats->syncp);
1874 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001875 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001876 u64_stats_update_end(&rx_stats->syncp);
1877 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001878 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001879 }
1880}
1881
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001882static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1883 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001884{
1885 struct vxlan_dev *vxlan = netdev_priv(dev);
David Miller79b16aa2015-04-05 22:19:09 -04001886 struct sock *sk = vxlan->vn_sock->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001887 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001888 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001889 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001890 union vxlan_addr *dst;
Thomas Graf35114942015-01-15 03:53:55 +01001891 struct vxlan_metadata md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001892 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001893 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001894 __be16 df = 0;
1895 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001896 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001897
stephen hemminger823aa872013-04-27 11:31:57 +00001898 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001899 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001900 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001901
Cong Wange4c7ed42013-08-31 13:44:33 +08001902 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001903 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001904 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001905 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001906 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001907 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001908 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001909 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001910
stephen hemmingerd3428942012-10-01 12:32:35 +00001911 old_iph = ip_hdr(skb);
1912
stephen hemmingerd3428942012-10-01 12:32:35 +00001913 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001914 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001915 ttl = 1;
1916
1917 tos = vxlan->tos;
1918 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001919 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001920
Tom Herbert535fb8d2014-07-01 21:32:49 -07001921 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1922 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001923
Cong Wange4c7ed42013-08-31 13:44:33 +08001924 if (dst->sa.sa_family == AF_INET) {
1925 memset(&fl4, 0, sizeof(fl4));
1926 fl4.flowi4_oif = rdst->remote_ifindex;
1927 fl4.flowi4_tos = RT_TOS(tos);
1928 fl4.daddr = dst->sin.sin_addr.s_addr;
1929 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001930
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001931 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001932 if (IS_ERR(rt)) {
1933 netdev_dbg(dev, "no route to %pI4\n",
1934 &dst->sin.sin_addr.s_addr);
1935 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001936 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001937 }
1938
1939 if (rt->dst.dev == dev) {
1940 netdev_dbg(dev, "circular route to %pI4\n",
1941 &dst->sin.sin_addr.s_addr);
1942 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001943 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001944 }
1945
1946 /* Bypass encapsulation if the destination is local */
1947 if (rt->rt_flags & RTCF_LOCAL &&
1948 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1949 struct vxlan_dev *dst_vxlan;
1950
1951 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001952 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01001953 dst->sa.sa_family, dst_port,
1954 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08001955 if (!dst_vxlan)
1956 goto tx_error;
1957 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1958 return;
1959 }
1960
1961 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1962 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Thomas Graf35114942015-01-15 03:53:55 +01001963 md.vni = htonl(vni << 8);
1964 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08001965
David Miller79b16aa2015-04-05 22:19:09 -04001966 err = vxlan_xmit_skb(rt, sk, skb, fl4.saddr,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001967 dst->sin.sin_addr.s_addr, tos, ttl, df,
1968 src_port, dst_port, &md,
1969 !net_eq(vxlan->net, dev_net(vxlan->dev)),
1970 vxlan->flags);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001971 if (err < 0) {
1972 /* skb is already freed. */
1973 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001974 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001975 }
1976
Cong Wange4c7ed42013-08-31 13:44:33 +08001977 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1978#if IS_ENABLED(CONFIG_IPV6)
1979 } else {
1980 struct sock *sk = vxlan->vn_sock->sock->sk;
1981 struct dst_entry *ndst;
1982 struct flowi6 fl6;
1983 u32 flags;
1984
1985 memset(&fl6, 0, sizeof(fl6));
1986 fl6.flowi6_oif = rdst->remote_ifindex;
1987 fl6.daddr = dst->sin6.sin6_addr;
1988 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08001989 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001990
1991 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1992 netdev_dbg(dev, "no route to %pI6\n",
1993 &dst->sin6.sin6_addr);
1994 dev->stats.tx_carrier_errors++;
1995 goto tx_error;
1996 }
1997
1998 if (ndst->dev == dev) {
1999 netdev_dbg(dev, "circular route to %pI6\n",
2000 &dst->sin6.sin6_addr);
2001 dst_release(ndst);
2002 dev->stats.collisions++;
2003 goto tx_error;
2004 }
2005
2006 /* Bypass encapsulation if the destination is local */
2007 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2008 if (flags & RTF_LOCAL &&
2009 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2010 struct vxlan_dev *dst_vxlan;
2011
2012 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002013 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002014 dst->sa.sa_family, dst_port,
2015 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002016 if (!dst_vxlan)
2017 goto tx_error;
2018 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2019 return;
2020 }
2021
2022 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Thomas Graf35114942015-01-15 03:53:55 +01002023 md.vni = htonl(vni << 8);
2024 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08002025
David Miller79b16aa2015-04-05 22:19:09 -04002026 err = vxlan6_xmit_skb(ndst, sk, skb, dev, &fl6.saddr, &fl6.daddr,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002027 0, ttl, src_port, dst_port, &md,
2028 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2029 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002030#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002031 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002032
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002033 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002034
2035drop:
2036 dev->stats.tx_dropped++;
2037 goto tx_free;
2038
Pravin B Shelar49560532013-08-19 11:23:17 -07002039rt_tx_error:
2040 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002041tx_error:
2042 dev->stats.tx_errors++;
2043tx_free:
2044 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002045}
2046
David Stevens66817122013-03-15 04:35:51 +00002047/* Transmit local packets over Vxlan
2048 *
2049 * Outer IP header inherits ECN and DF from inner header.
2050 * Outer UDP destination is the VXLAN assigned port.
2051 * source port is based on hash of flow
2052 */
2053static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2054{
2055 struct vxlan_dev *vxlan = netdev_priv(dev);
2056 struct ethhdr *eth;
2057 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002058 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002059 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002060
2061 skb_reset_mac_header(skb);
2062 eth = eth_hdr(skb);
2063
Cong Wangf564f452013-08-31 13:44:36 +08002064 if ((vxlan->flags & VXLAN_F_PROXY)) {
2065 if (ntohs(eth->h_proto) == ETH_P_ARP)
2066 return arp_reduce(dev, skb);
2067#if IS_ENABLED(CONFIG_IPV6)
2068 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002069 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2070 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002071 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2072 struct nd_msg *msg;
2073
2074 msg = (struct nd_msg *)skb_transport_header(skb);
2075 if (msg->icmph.icmp6_code == 0 &&
2076 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2077 return neigh_reduce(dev, skb);
2078 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002079 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002080#endif
2081 }
David Stevens66817122013-03-15 04:35:51 +00002082
2083 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002084 did_rsc = false;
2085
2086 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002087 (ntohs(eth->h_proto) == ETH_P_IP ||
2088 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002089 did_rsc = route_shortcircuit(dev, skb);
2090 if (did_rsc)
2091 f = vxlan_find_mac(vxlan, eth->h_dest);
2092 }
2093
David Stevens66817122013-03-15 04:35:51 +00002094 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002095 f = vxlan_find_mac(vxlan, all_zeros_mac);
2096 if (f == NULL) {
2097 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2098 !is_multicast_ether_addr(eth->h_dest))
2099 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002100
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002101 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002102 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002103 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002104 }
David Stevens66817122013-03-15 04:35:51 +00002105 }
2106
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002107 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2108 struct sk_buff *skb1;
2109
Eric Dumazet8f646c92014-01-06 09:54:31 -08002110 if (!fdst) {
2111 fdst = rdst;
2112 continue;
2113 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002114 skb1 = skb_clone(skb, GFP_ATOMIC);
2115 if (skb1)
2116 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2117 }
2118
Eric Dumazet8f646c92014-01-06 09:54:31 -08002119 if (fdst)
2120 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2121 else
2122 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002123 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002124}
2125
stephen hemmingerd3428942012-10-01 12:32:35 +00002126/* Walk the forwarding table and purge stale entries */
2127static void vxlan_cleanup(unsigned long arg)
2128{
2129 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2130 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2131 unsigned int h;
2132
2133 if (!netif_running(vxlan->dev))
2134 return;
2135
2136 spin_lock_bh(&vxlan->hash_lock);
2137 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2138 struct hlist_node *p, *n;
2139 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2140 struct vxlan_fdb *f
2141 = container_of(p, struct vxlan_fdb, hlist);
2142 unsigned long timeout;
2143
stephen hemminger3c172862012-10-26 06:24:34 +00002144 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002145 continue;
2146
2147 timeout = f->used + vxlan->age_interval * HZ;
2148 if (time_before_eq(timeout, jiffies)) {
2149 netdev_dbg(vxlan->dev,
2150 "garbage collect %pM\n",
2151 f->eth_addr);
2152 f->state = NUD_STALE;
2153 vxlan_fdb_destroy(vxlan, f);
2154 } else if (time_before(timeout, next_timer))
2155 next_timer = timeout;
2156 }
2157 }
2158 spin_unlock_bh(&vxlan->hash_lock);
2159
2160 mod_timer(&vxlan->age_timer, next_timer);
2161}
2162
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002163static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2164{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002165 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002166 __u32 vni = vxlan->default_dst.remote_vni;
2167
2168 vxlan->vn_sock = vs;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002169 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002170 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002171 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002172}
2173
stephen hemmingerd3428942012-10-01 12:32:35 +00002174/* Setup stats when device is created */
2175static int vxlan_init(struct net_device *dev)
2176{
WANG Cong1c213bd2014-02-13 11:46:28 -08002177 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002178 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002179 return -ENOMEM;
2180
2181 return 0;
2182}
2183
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002184static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002185{
2186 struct vxlan_fdb *f;
2187
2188 spin_lock_bh(&vxlan->hash_lock);
2189 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2190 if (f)
2191 vxlan_fdb_destroy(vxlan, f);
2192 spin_unlock_bh(&vxlan->hash_lock);
2193}
2194
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002195static void vxlan_uninit(struct net_device *dev)
2196{
2197 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002198
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002199 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002200
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002201 free_percpu(dev->tstats);
2202}
2203
stephen hemmingerd3428942012-10-01 12:32:35 +00002204/* Start ageing timer and join group when device is brought up */
2205static int vxlan_open(struct net_device *dev)
2206{
2207 struct vxlan_dev *vxlan = netdev_priv(dev);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002208 struct vxlan_sock *vs;
2209 int ret = 0;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002210
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002211 vs = vxlan_sock_add(vxlan->net, vxlan->dst_port, vxlan_rcv, NULL,
2212 false, vxlan->flags);
2213 if (IS_ERR(vs))
2214 return PTR_ERR(vs);
2215
2216 vxlan_vs_add_dev(vs, vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002217
Gao feng79d4a942013-12-10 16:37:32 +08002218 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002219 ret = vxlan_igmp_join(vxlan);
2220 if (ret) {
2221 vxlan_sock_release(vs);
2222 return ret;
2223 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002224 }
2225
2226 if (vxlan->age_interval)
2227 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2228
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002229 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002230}
2231
2232/* Purge the forwarding table */
2233static void vxlan_flush(struct vxlan_dev *vxlan)
2234{
Cong Wang31fec5a2013-05-27 22:35:52 +00002235 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002236
2237 spin_lock_bh(&vxlan->hash_lock);
2238 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2239 struct hlist_node *p, *n;
2240 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2241 struct vxlan_fdb *f
2242 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002243 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2244 if (!is_zero_ether_addr(f->eth_addr))
2245 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002246 }
2247 }
2248 spin_unlock_bh(&vxlan->hash_lock);
2249}
2250
2251/* Cleanup timer and forwarding table on shutdown */
2252static int vxlan_stop(struct net_device *dev)
2253{
2254 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002255 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002256 struct vxlan_sock *vs = vxlan->vn_sock;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002257 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002258
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002259 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002260 !vxlan_group_used(vn, vxlan)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002261 ret = vxlan_igmp_leave(vxlan);
2262 if (ret)
2263 return ret;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002264 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002265
2266 del_timer_sync(&vxlan->age_timer);
2267
2268 vxlan_flush(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002269 vxlan_sock_release(vs);
stephen hemmingerd3428942012-10-01 12:32:35 +00002270
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002271 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002272}
2273
stephen hemmingerd3428942012-10-01 12:32:35 +00002274/* Stub, nothing needs to be done. */
2275static void vxlan_set_multicast_list(struct net_device *dev)
2276{
2277}
2278
Daniel Borkmann345010b2013-12-18 00:21:08 +01002279static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2280{
2281 struct vxlan_dev *vxlan = netdev_priv(dev);
2282 struct vxlan_rdst *dst = &vxlan->default_dst;
2283 struct net_device *lowerdev;
2284 int max_mtu;
2285
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002286 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002287 if (lowerdev == NULL)
2288 return eth_change_mtu(dev, new_mtu);
2289
2290 if (dst->remote_ip.sa.sa_family == AF_INET6)
2291 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2292 else
2293 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2294
2295 if (new_mtu < 68 || new_mtu > max_mtu)
2296 return -EINVAL;
2297
2298 dev->mtu = new_mtu;
2299 return 0;
2300}
2301
stephen hemmingerd3428942012-10-01 12:32:35 +00002302static const struct net_device_ops vxlan_netdev_ops = {
2303 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002304 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002305 .ndo_open = vxlan_open,
2306 .ndo_stop = vxlan_stop,
2307 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002308 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002309 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002310 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002311 .ndo_validate_addr = eth_validate_addr,
2312 .ndo_set_mac_address = eth_mac_addr,
2313 .ndo_fdb_add = vxlan_fdb_add,
2314 .ndo_fdb_del = vxlan_fdb_delete,
2315 .ndo_fdb_dump = vxlan_fdb_dump,
2316};
2317
2318/* Info for udev, that this is a virtual tunnel endpoint */
2319static struct device_type vxlan_type = {
2320 .name = "vxlan",
2321};
2322
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002323/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002324 * supply the listening VXLAN udp ports. Callers are expected
2325 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002326 */
2327void vxlan_get_rx_port(struct net_device *dev)
2328{
2329 struct vxlan_sock *vs;
2330 struct net *net = dev_net(dev);
2331 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2332 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002333 __be16 port;
2334 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002335
2336 spin_lock(&vn->sock_lock);
2337 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002338 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2339 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002340 sa_family = vs->sock->sk->sk_family;
2341 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2342 port);
2343 }
2344 }
2345 spin_unlock(&vn->sock_lock);
2346}
2347EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2348
stephen hemmingerd3428942012-10-01 12:32:35 +00002349/* Initialize the device structure. */
2350static void vxlan_setup(struct net_device *dev)
2351{
2352 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002353 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002354
2355 eth_hw_addr_random(dev);
2356 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002357 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002358 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002359 else
Cong Wang2853af62014-06-12 11:53:10 -07002360 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002361
2362 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002363 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002364 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2365
2366 dev->tx_queue_len = 0;
2367 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002368 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002369 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002370 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002371
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002372 dev->vlan_features = dev->features;
2373 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002374 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002375 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002376 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002377 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002378 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002379
stephen hemminger553675f2013-05-16 11:35:20 +00002380 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002381 spin_lock_init(&vxlan->hash_lock);
2382
2383 init_timer_deferrable(&vxlan->age_timer);
2384 vxlan->age_timer.function = vxlan_cleanup;
2385 vxlan->age_timer.data = (unsigned long) vxlan;
2386
stephen hemminger823aa872013-04-27 11:31:57 +00002387 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002388
stephen hemmingerd3428942012-10-01 12:32:35 +00002389 vxlan->dev = dev;
2390
2391 for (h = 0; h < FDB_HASH_SIZE; ++h)
2392 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2393}
2394
2395static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2396 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002397 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002398 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002399 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2400 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002401 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002402 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2403 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2404 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2405 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2406 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002407 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002408 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2409 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2410 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2411 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002412 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002413 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2414 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2415 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002416 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2417 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002418 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002419 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002420};
2421
2422static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2423{
2424 if (tb[IFLA_ADDRESS]) {
2425 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2426 pr_debug("invalid link address (not ethernet)\n");
2427 return -EINVAL;
2428 }
2429
2430 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2431 pr_debug("invalid all zero ethernet address\n");
2432 return -EADDRNOTAVAIL;
2433 }
2434 }
2435
2436 if (!data)
2437 return -EINVAL;
2438
2439 if (data[IFLA_VXLAN_ID]) {
2440 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2441 if (id >= VXLAN_VID_MASK)
2442 return -ERANGE;
2443 }
2444
stephen hemminger05f47d62012-10-09 20:35:50 +00002445 if (data[IFLA_VXLAN_PORT_RANGE]) {
2446 const struct ifla_vxlan_port_range *p
2447 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2448
2449 if (ntohs(p->high) < ntohs(p->low)) {
2450 pr_debug("port range %u .. %u not valid\n",
2451 ntohs(p->low), ntohs(p->high));
2452 return -EINVAL;
2453 }
2454 }
2455
stephen hemmingerd3428942012-10-01 12:32:35 +00002456 return 0;
2457}
2458
Yan Burman1b13c972013-01-29 23:43:07 +00002459static void vxlan_get_drvinfo(struct net_device *netdev,
2460 struct ethtool_drvinfo *drvinfo)
2461{
2462 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2463 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2464}
2465
2466static const struct ethtool_ops vxlan_ethtool_ops = {
2467 .get_drvinfo = vxlan_get_drvinfo,
2468 .get_link = ethtool_op_get_link,
2469};
2470
stephen hemminger553675f2013-05-16 11:35:20 +00002471static void vxlan_del_work(struct work_struct *work)
2472{
2473 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002474 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002475 kfree_rcu(vs, rcu);
2476}
2477
Tom Herbert3ee64f32014-07-13 19:49:42 -07002478static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2479 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002480{
Cong Wange4c7ed42013-08-31 13:44:33 +08002481 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002482 struct udp_port_cfg udp_conf;
2483 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002484
Tom Herbert3ee64f32014-07-13 19:49:42 -07002485 memset(&udp_conf, 0, sizeof(udp_conf));
2486
2487 if (ipv6) {
2488 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002489 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002490 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002491 } else {
2492 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002493 }
2494
Tom Herbert3ee64f32014-07-13 19:49:42 -07002495 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002496
Tom Herbert3ee64f32014-07-13 19:49:42 -07002497 /* Open UDP socket */
2498 err = udp_sock_create(net, &udp_conf, &sock);
2499 if (err < 0)
2500 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002501
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002502 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002503}
2504
2505/* Create new listen socket if needed */
2506static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002507 vxlan_rcv_t *rcv, void *data,
2508 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002509{
2510 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2511 struct vxlan_sock *vs;
2512 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002513 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002514 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002515 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002516
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002517 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002518 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002519 return ERR_PTR(-ENOMEM);
2520
2521 for (h = 0; h < VNI_HASH_SIZE; ++h)
2522 INIT_HLIST_HEAD(&vs->vni_list[h]);
2523
2524 INIT_WORK(&vs->del_work, vxlan_del_work);
2525
Tom Herbert3ee64f32014-07-13 19:49:42 -07002526 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002527 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002528 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2529 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002530 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002531 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002532 }
2533
Cong Wange4c7ed42013-08-31 13:44:33 +08002534 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002535 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002536 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002537 vs->data = data;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002538 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002539
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002540 /* Initialize the vxlan udp offloads structure */
2541 vs->udp_offloads.port = port;
2542 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2543 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2544
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002545 spin_lock(&vn->sock_lock);
2546 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002547 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002548 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002549
2550 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002551 tunnel_cfg.sk_user_data = vs;
2552 tunnel_cfg.encap_type = 1;
2553 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2554 tunnel_cfg.encap_destroy = NULL;
2555
2556 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002557
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002558 return vs;
2559}
stephen hemminger553675f2013-05-16 11:35:20 +00002560
Pravin B Shelar012a5722013-08-19 11:23:07 -07002561struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2562 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002563 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002564{
2565 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2566 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002567 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002568
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002569 if (!no_share) {
2570 spin_lock(&vn->sock_lock);
2571 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port,
2572 flags);
2573 if (vs && vs->rcv == rcv) {
2574 if (!atomic_add_unless(&vs->refcnt, 1, 0))
2575 vs = ERR_PTR(-EBUSY);
2576 spin_unlock(&vn->sock_lock);
2577 return vs;
2578 }
2579 spin_unlock(&vn->sock_lock);
2580 }
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002581
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002582 return vxlan_socket_create(net, port, rcv, data, flags);
stephen hemminger553675f2013-05-16 11:35:20 +00002583}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002584EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002585
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002586static int vxlan_newlink(struct net *src_net, struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +00002587 struct nlattr *tb[], struct nlattr *data[])
2588{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002589 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002590 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002591 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002592 __u32 vni;
2593 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002594 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002595
2596 if (!data[IFLA_VXLAN_ID])
2597 return -EINVAL;
2598
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002599 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002600
stephen hemmingerd3428942012-10-01 12:32:35 +00002601 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002602 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002603
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002604 /* Unless IPv6 is explicitly requested, assume IPv4 */
2605 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002606 if (data[IFLA_VXLAN_GROUP]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002607 dst->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002608 } else if (data[IFLA_VXLAN_GROUP6]) {
2609 if (!IS_ENABLED(CONFIG_IPV6))
2610 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002611
Jiri Benc67b61f62015-03-29 16:59:26 +02002612 dst->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002613 dst->remote_ip.sa.sa_family = AF_INET6;
2614 use_ipv6 = true;
2615 }
2616
2617 if (data[IFLA_VXLAN_LOCAL]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002618 vxlan->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002619 vxlan->saddr.sa.sa_family = AF_INET;
2620 } else if (data[IFLA_VXLAN_LOCAL6]) {
2621 if (!IS_ENABLED(CONFIG_IPV6))
2622 return -EPFNOSUPPORT;
2623
2624 /* TODO: respect scope id */
Jiri Benc67b61f62015-03-29 16:59:26 +02002625 vxlan->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002626 vxlan->saddr.sa.sa_family = AF_INET6;
2627 use_ipv6 = true;
2628 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002629
stephen hemminger34e02aa2012-10-09 20:35:53 +00002630 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002631 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002632 struct net_device *lowerdev
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002633 = __dev_get_by_index(src_net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002634
stephen hemminger34e02aa2012-10-09 20:35:53 +00002635 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002636 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002637 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002638 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002639
Cong Wange4c7ed42013-08-31 13:44:33 +08002640#if IS_ENABLED(CONFIG_IPV6)
2641 if (use_ipv6) {
2642 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2643 if (idev && idev->cnf.disable_ipv6) {
2644 pr_info("IPv6 is disabled via sysctl\n");
2645 return -EPERM;
2646 }
2647 vxlan->flags |= VXLAN_F_IPV6;
2648 }
2649#endif
2650
stephen hemminger34e02aa2012-10-09 20:35:53 +00002651 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002652 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002653
Cong Wang2853af62014-06-12 11:53:10 -07002654 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002655 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002656 } else if (use_ipv6)
2657 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002658
2659 if (data[IFLA_VXLAN_TOS])
2660 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2661
Vincent Bernatafb97182012-10-30 10:27:16 +00002662 if (data[IFLA_VXLAN_TTL])
2663 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2664
stephen hemmingerd3428942012-10-01 12:32:35 +00002665 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002666 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002667
2668 if (data[IFLA_VXLAN_AGEING])
2669 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2670 else
2671 vxlan->age_interval = FDB_AGE_DEFAULT;
2672
David Stevense4f67ad2012-11-20 02:50:14 +00002673 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2674 vxlan->flags |= VXLAN_F_PROXY;
2675
2676 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2677 vxlan->flags |= VXLAN_F_RSC;
2678
2679 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2680 vxlan->flags |= VXLAN_F_L2MISS;
2681
2682 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2683 vxlan->flags |= VXLAN_F_L3MISS;
2684
stephen hemmingerd3428942012-10-01 12:32:35 +00002685 if (data[IFLA_VXLAN_LIMIT])
2686 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2687
stephen hemminger05f47d62012-10-09 20:35:50 +00002688 if (data[IFLA_VXLAN_PORT_RANGE]) {
2689 const struct ifla_vxlan_port_range *p
2690 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2691 vxlan->port_min = ntohs(p->low);
2692 vxlan->port_max = ntohs(p->high);
2693 }
2694
stephen hemminger823aa872013-04-27 11:31:57 +00002695 if (data[IFLA_VXLAN_PORT])
2696 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2697
Tom Herbert359a0ea2014-06-04 17:20:29 -07002698 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2699 vxlan->flags |= VXLAN_F_UDP_CSUM;
2700
2701 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2702 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2703 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2704
2705 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2706 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2707 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2708
Tom Herbertdfd86452015-01-12 17:00:38 -08002709 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2710 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2711 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2712
2713 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2714 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2715 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2716
Thomas Graf35114942015-01-15 03:53:55 +01002717 if (data[IFLA_VXLAN_GBP])
2718 vxlan->flags |= VXLAN_F_GBP;
2719
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002720 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
2721 vxlan->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
2722
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002723 if (vxlan_find_vni(src_net, vni, use_ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002724 vxlan->dst_port, vxlan->flags)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002725 pr_info("duplicate VNI %u\n", vni);
2726 return -EEXIST;
2727 }
2728
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002729 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002730
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002731 /* create an fdb entry for a valid default destination */
2732 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2733 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2734 &vxlan->default_dst.remote_ip,
2735 NUD_REACHABLE|NUD_PERMANENT,
2736 NLM_F_EXCL|NLM_F_CREATE,
2737 vxlan->dst_port,
2738 vxlan->default_dst.remote_vni,
2739 vxlan->default_dst.remote_ifindex,
2740 NTF_SELF);
2741 if (err)
2742 return err;
2743 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002744
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002745 err = register_netdevice(dev);
2746 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002747 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002748 return err;
2749 }
2750
stephen hemminger553675f2013-05-16 11:35:20 +00002751 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002752
2753 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002754}
2755
2756static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2757{
2758 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002759 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002760
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002761 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002762 if (!hlist_unhashed(&vxlan->hlist))
2763 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002764 spin_unlock(&vn->sock_lock);
2765
stephen hemminger553675f2013-05-16 11:35:20 +00002766 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002767 unregister_netdevice_queue(dev, head);
2768}
2769
2770static size_t vxlan_get_size(const struct net_device *dev)
2771{
2772
2773 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002774 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002775 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002776 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002777 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2778 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2779 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002780 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2781 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2782 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2783 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002784 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2785 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002786 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002787 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2788 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2789 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2790 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002791 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2792 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002793 0;
2794}
2795
2796static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2797{
2798 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002799 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002800 struct ifla_vxlan_port_range ports = {
2801 .low = htons(vxlan->port_min),
2802 .high = htons(vxlan->port_max),
2803 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002804
Atzm Watanabec7995c42013-04-16 02:50:52 +00002805 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002806 goto nla_put_failure;
2807
Cong Wange4c7ed42013-08-31 13:44:33 +08002808 if (!vxlan_addr_any(&dst->remote_ip)) {
2809 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02002810 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
2811 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002812 goto nla_put_failure;
2813#if IS_ENABLED(CONFIG_IPV6)
2814 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02002815 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
2816 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002817 goto nla_put_failure;
2818#endif
2819 }
2820 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002821
Atzm Watanabec7995c42013-04-16 02:50:52 +00002822 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002823 goto nla_put_failure;
2824
Cong Wange4c7ed42013-08-31 13:44:33 +08002825 if (!vxlan_addr_any(&vxlan->saddr)) {
2826 if (vxlan->saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02002827 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
2828 vxlan->saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002829 goto nla_put_failure;
2830#if IS_ENABLED(CONFIG_IPV6)
2831 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02002832 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
2833 &vxlan->saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002834 goto nla_put_failure;
2835#endif
2836 }
2837 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002838
2839 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2840 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002841 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2842 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2843 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2844 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2845 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2846 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2847 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2848 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2849 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002850 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002851 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002852 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2853 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2854 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2855 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2856 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2857 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002858 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2859 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2860 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2861 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2862 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002863 goto nla_put_failure;
2864
stephen hemminger05f47d62012-10-09 20:35:50 +00002865 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2866 goto nla_put_failure;
2867
Thomas Graf35114942015-01-15 03:53:55 +01002868 if (vxlan->flags & VXLAN_F_GBP &&
2869 nla_put_flag(skb, IFLA_VXLAN_GBP))
2870 goto nla_put_failure;
2871
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002872 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
2873 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
2874 goto nla_put_failure;
2875
stephen hemmingerd3428942012-10-01 12:32:35 +00002876 return 0;
2877
2878nla_put_failure:
2879 return -EMSGSIZE;
2880}
2881
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002882static struct net *vxlan_get_link_net(const struct net_device *dev)
2883{
2884 struct vxlan_dev *vxlan = netdev_priv(dev);
2885
2886 return vxlan->net;
2887}
2888
stephen hemmingerd3428942012-10-01 12:32:35 +00002889static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2890 .kind = "vxlan",
2891 .maxtype = IFLA_VXLAN_MAX,
2892 .policy = vxlan_policy,
2893 .priv_size = sizeof(struct vxlan_dev),
2894 .setup = vxlan_setup,
2895 .validate = vxlan_validate,
2896 .newlink = vxlan_newlink,
2897 .dellink = vxlan_dellink,
2898 .get_size = vxlan_get_size,
2899 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002900 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002901};
2902
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002903static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2904 struct net_device *dev)
2905{
2906 struct vxlan_dev *vxlan, *next;
2907 LIST_HEAD(list_kill);
2908
2909 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2910 struct vxlan_rdst *dst = &vxlan->default_dst;
2911
2912 /* In case we created vxlan device with carrier
2913 * and we loose the carrier due to module unload
2914 * we also need to remove vxlan device. In other
2915 * cases, it's not necessary and remote_ifindex
2916 * is 0 here, so no matches.
2917 */
2918 if (dst->remote_ifindex == dev->ifindex)
2919 vxlan_dellink(vxlan->dev, &list_kill);
2920 }
2921
2922 unregister_netdevice_many(&list_kill);
2923}
2924
2925static int vxlan_lowerdev_event(struct notifier_block *unused,
2926 unsigned long event, void *ptr)
2927{
2928 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002929 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002930
Daniel Borkmann783c1462014-01-22 21:07:53 +01002931 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002932 vxlan_handle_lowerdev_unregister(vn, dev);
2933
2934 return NOTIFY_DONE;
2935}
2936
2937static struct notifier_block vxlan_notifier_block __read_mostly = {
2938 .notifier_call = vxlan_lowerdev_event,
2939};
2940
stephen hemmingerd3428942012-10-01 12:32:35 +00002941static __net_init int vxlan_init_net(struct net *net)
2942{
2943 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002944 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002945
stephen hemminger553675f2013-05-16 11:35:20 +00002946 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002947 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002948
stephen hemminger553675f2013-05-16 11:35:20 +00002949 for (h = 0; h < PORT_HASH_SIZE; ++h)
2950 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002951
2952 return 0;
2953}
2954
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002955static void __net_exit vxlan_exit_net(struct net *net)
2956{
2957 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2958 struct vxlan_dev *vxlan, *next;
2959 struct net_device *dev, *aux;
2960 LIST_HEAD(list);
2961
2962 rtnl_lock();
2963 for_each_netdev_safe(net, dev, aux)
2964 if (dev->rtnl_link_ops == &vxlan_link_ops)
2965 unregister_netdevice_queue(dev, &list);
2966
2967 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2968 /* If vxlan->dev is in the same netns, it has already been added
2969 * to the list by the previous loop.
2970 */
2971 if (!net_eq(dev_net(vxlan->dev), net))
2972 unregister_netdevice_queue(dev, &list);
2973 }
2974
2975 unregister_netdevice_many(&list);
2976 rtnl_unlock();
2977}
2978
stephen hemmingerd3428942012-10-01 12:32:35 +00002979static struct pernet_operations vxlan_net_ops = {
2980 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002981 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002982 .id = &vxlan_net_id,
2983 .size = sizeof(struct vxlan_net),
2984};
2985
2986static int __init vxlan_init_module(void)
2987{
2988 int rc;
2989
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002990 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2991 if (!vxlan_wq)
2992 return -ENOMEM;
2993
stephen hemmingerd3428942012-10-01 12:32:35 +00002994 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2995
Daniel Borkmann783c1462014-01-22 21:07:53 +01002996 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00002997 if (rc)
2998 goto out1;
2999
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003000 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003001 if (rc)
3002 goto out2;
3003
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003004 rc = rtnl_link_register(&vxlan_link_ops);
3005 if (rc)
3006 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003007
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003008 return 0;
3009out3:
3010 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003011out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003012 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003013out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003014 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00003015 return rc;
3016}
Cong Wang7332a132013-05-27 22:35:53 +00003017late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003018
3019static void __exit vxlan_cleanup_module(void)
3020{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003021 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003022 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003023 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003024 unregister_pernet_subsys(&vxlan_net_ops);
3025 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003026}
3027module_exit(vxlan_cleanup_module);
3028
3029MODULE_LICENSE("GPL");
3030MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003031MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003032MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003033MODULE_ALIAS_RTNL_LINK("vxlan");