blob: 06c092b05a51350fcfd603c2fb7344cd3649ff66 [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
stephen hemmingerd3428942012-10-01 12:32:35 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/rculist.h>
20#include <linux/netdevice.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/udp.h>
24#include <linux/igmp.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
Pravin B Shelar1eaa8172013-08-19 11:23:29 -070027#include <linux/if_vlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000028#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000029#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000030#include <net/arp.h>
31#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000032#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000033#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/icmp.h>
35#include <net/udp.h>
Tom Herbert3ee64f32014-07-13 19:49:42 -070036#include <net/udp_tunnel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000037#include <net/rtnetlink.h>
38#include <net/route.h>
39#include <net/dsfield.h>
40#include <net/inet_ecn.h>
41#include <net/net_namespace.h>
42#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070043#include <net/vxlan.h>
Or Gerlitzdc01e7d2014-01-20 13:59:21 +020044#include <net/protocol.h>
Andy Zhouacbf74a2014-09-16 17:31:18 -070045#include <net/udp_tunnel.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080046#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ipv6.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080050#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080051#endif
Thomas Grafee122c72015-07-21 10:43:58 +020052#include <net/dst_metadata.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000053
54#define VXLAN_VERSION "0.1"
55
stephen hemminger553675f2013-05-16 11:35:20 +000056#define PORT_HASH_BITS 8
57#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000058#define VNI_HASH_BITS 10
59#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
60#define FDB_HASH_BITS 8
61#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
62#define FDB_AGE_DEFAULT 300 /* 5 min */
63#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
64
stephen hemminger23c578b2013-04-27 11:31:53 +000065/* UDP port for VXLAN traffic.
66 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070067 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000068 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070069static unsigned short vxlan_port __read_mostly = 8472;
70module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000071MODULE_PARM_DESC(udp_port, "Destination UDP port");
72
73static bool log_ecn_error = true;
74module_param(log_ecn_error, bool, 0644);
75MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
76
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070077static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000078
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030079static const u8 all_zeros_mac[ETH_ALEN];
80
stephen hemminger553675f2013-05-16 11:35:20 +000081/* per-network namespace private data for this module */
82struct vxlan_net {
83 struct list_head vxlan_list;
84 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070085 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000086};
87
Cong Wange4c7ed42013-08-31 13:44:33 +080088union vxlan_addr {
89 struct sockaddr_in sin;
90 struct sockaddr_in6 sin6;
91 struct sockaddr sa;
92};
93
David Stevens66817122013-03-15 04:35:51 +000094struct vxlan_rdst {
Cong Wange4c7ed42013-08-31 13:44:33 +080095 union vxlan_addr remote_ip;
David Stevens66817122013-03-15 04:35:51 +000096 __be16 remote_port;
97 u32 remote_vni;
98 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070099 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300100 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000101};
102
stephen hemmingerd3428942012-10-01 12:32:35 +0000103/* Forwarding table entry */
104struct vxlan_fdb {
105 struct hlist_node hlist; /* linked list of entries */
106 struct rcu_head rcu;
107 unsigned long updated; /* jiffies */
108 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700109 struct list_head remotes;
Sowmini Varadhan7177a3b2015-07-20 09:54:50 +0200110 u8 eth_addr[ETH_ALEN];
stephen hemmingerd3428942012-10-01 12:32:35 +0000111 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000112 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000113};
114
stephen hemmingerd3428942012-10-01 12:32:35 +0000115/* Pseudo network device */
116struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000117 struct hlist_node hlist; /* vni hash table */
118 struct list_head next; /* vxlan's per namespace list */
119 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000120 struct net_device *dev;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +0200121 struct net *net; /* netns for packet i/o */
Atzm Watanabec7995c42013-04-16 02:50:52 +0000122 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800123 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000124 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000125 __u16 port_min; /* source port range */
126 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000127 __u8 tos; /* TOS override */
128 __u8 ttl;
Tom Herbert359a0ea2014-06-04 17:20:29 -0700129 u32 flags; /* VXLAN_F_* in vxlan.h */
stephen hemmingerd3428942012-10-01 12:32:35 +0000130
131 unsigned long age_interval;
132 struct timer_list age_timer;
133 spinlock_t hash_lock;
134 unsigned int addrcnt;
135 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000136
137 struct hlist_head fdb_head[FDB_HASH_SIZE];
138};
139
140/* salt for hash table */
141static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700142static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000143
Thomas Grafee122c72015-07-21 10:43:58 +0200144static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
145{
146 return vs->flags & VXLAN_F_COLLECT_METADATA;
147}
148
Cong Wange4c7ed42013-08-31 13:44:33 +0800149#if IS_ENABLED(CONFIG_IPV6)
150static inline
151bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
152{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200153 if (a->sa.sa_family != b->sa.sa_family)
154 return false;
155 if (a->sa.sa_family == AF_INET6)
156 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
157 else
158 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800159}
160
161static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
162{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200163 if (ipa->sa.sa_family == AF_INET6)
164 return ipv6_addr_any(&ipa->sin6.sin6_addr);
165 else
166 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800167}
168
169static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
170{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200171 if (ipa->sa.sa_family == AF_INET6)
172 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
173 else
174 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800175}
176
177static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
178{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200179 if (nla_len(nla) >= sizeof(struct in6_addr)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200180 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200181 ip->sa.sa_family = AF_INET6;
182 return 0;
183 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200184 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200185 ip->sa.sa_family = AF_INET;
186 return 0;
187 } else {
188 return -EAFNOSUPPORT;
189 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800190}
191
192static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200193 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800194{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200195 if (ip->sa.sa_family == AF_INET6)
Jiri Benc930345e2015-03-29 16:59:25 +0200196 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200197 else
Jiri Benc930345e2015-03-29 16:59:25 +0200198 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800199}
200
201#else /* !CONFIG_IPV6 */
202
203static inline
204bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
205{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200206 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
Cong Wange4c7ed42013-08-31 13:44:33 +0800207}
208
209static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
210{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200211 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
Cong Wange4c7ed42013-08-31 13:44:33 +0800212}
213
214static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
215{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200216 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
Cong Wange4c7ed42013-08-31 13:44:33 +0800217}
218
219static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
220{
Jiri Bencf0ef3122015-03-29 16:17:37 +0200221 if (nla_len(nla) >= sizeof(struct in6_addr)) {
222 return -EAFNOSUPPORT;
223 } else if (nla_len(nla) >= sizeof(__be32)) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200224 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
Jiri Bencf0ef3122015-03-29 16:17:37 +0200225 ip->sa.sa_family = AF_INET;
226 return 0;
227 } else {
228 return -EAFNOSUPPORT;
229 }
Cong Wange4c7ed42013-08-31 13:44:33 +0800230}
231
232static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
Jiri Bencf0ef3122015-03-29 16:17:37 +0200233 const union vxlan_addr *ip)
Cong Wange4c7ed42013-08-31 13:44:33 +0800234{
Jiri Benc930345e2015-03-29 16:59:25 +0200235 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +0800236}
237#endif
238
stephen hemminger553675f2013-05-16 11:35:20 +0000239/* Virtual Network hash table head */
240static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
241{
242 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
243}
244
245/* Socket hash table head */
246static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000247{
248 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
249
stephen hemminger553675f2013-05-16 11:35:20 +0000250 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
251}
252
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700253/* First remote destination for a forwarding entry.
254 * Guaranteed to be non-NULL because remotes are never deleted.
255 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700256static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700257{
stephen hemminger5ca54612013-08-04 17:17:39 -0700258 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
259}
260
261static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
262{
263 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700264}
265
Thomas Grafac5132d2015-01-15 03:53:56 +0100266/* Find VXLAN socket based on network namespace, address family and UDP port
267 * and enabled unshareable flags.
268 */
269static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
270 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000271{
272 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800273
274 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000275
276 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200277 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Thomas Grafac5132d2015-01-15 03:53:56 +0100278 inet_sk(vs->sock->sk)->sk.sk_family == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800279 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000280 return vs;
281 }
282 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000283}
284
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700285static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000286{
287 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000288
stephen hemminger553675f2013-05-16 11:35:20 +0000289 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000290 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000291 return vxlan;
292 }
293
294 return NULL;
295}
296
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700297/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200298static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
Thomas Grafac5132d2015-01-15 03:53:56 +0100299 sa_family_t family, __be16 port,
300 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700301{
302 struct vxlan_sock *vs;
303
Thomas Grafac5132d2015-01-15 03:53:56 +0100304 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700305 if (!vs)
306 return NULL;
307
308 return vxlan_vs_find_vni(vs, id);
309}
310
stephen hemmingerd3428942012-10-01 12:32:35 +0000311/* Fill in neighbour message in skbuff. */
312static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700313 const struct vxlan_fdb *fdb,
314 u32 portid, u32 seq, int type, unsigned int flags,
315 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000316{
317 unsigned long now = jiffies;
318 struct nda_cacheinfo ci;
319 struct nlmsghdr *nlh;
320 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000321 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000322
323 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
324 if (nlh == NULL)
325 return -EMSGSIZE;
326
327 ndm = nlmsg_data(nlh);
328 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000329
330 send_eth = send_ip = true;
331
332 if (type == RTM_GETNEIGH) {
333 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800334 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000335 send_eth = !is_zero_ether_addr(fdb->eth_addr);
336 } else
337 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000338 ndm->ndm_state = fdb->state;
339 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000340 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800341 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000342
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100343 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100344 nla_put_s32(skb, NDA_LINK_NETNSID,
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +0200345 peernet2id_alloc(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100346 goto nla_put_failure;
347
David Stevense4f67ad2012-11-20 02:50:14 +0000348 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000349 goto nla_put_failure;
350
Cong Wange4c7ed42013-08-31 13:44:33 +0800351 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000352 goto nla_put_failure;
353
stephen hemminger823aa872013-04-27 11:31:57 +0000354 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000355 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
356 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000357 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700358 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000359 goto nla_put_failure;
360 if (rdst->remote_ifindex &&
361 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000362 goto nla_put_failure;
363
364 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
365 ci.ndm_confirmed = 0;
366 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
367 ci.ndm_refcnt = 0;
368
369 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
370 goto nla_put_failure;
371
Johannes Berg053c0952015-01-16 22:09:00 +0100372 nlmsg_end(skb, nlh);
373 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000374
375nla_put_failure:
376 nlmsg_cancel(skb, nlh);
377 return -EMSGSIZE;
378}
379
380static inline size_t vxlan_nlmsg_size(void)
381{
382 return NLMSG_ALIGN(sizeof(struct ndmsg))
383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100388 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000389 + nla_total_size(sizeof(struct nda_cacheinfo));
390}
391
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200392static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
393 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000394{
395 struct net *net = dev_net(vxlan->dev);
396 struct sk_buff *skb;
397 int err = -ENOBUFS;
398
399 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
400 if (skb == NULL)
401 goto errout;
402
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200403 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000404 if (err < 0) {
405 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
406 WARN_ON(err == -EMSGSIZE);
407 kfree_skb(skb);
408 goto errout;
409 }
410
411 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
412 return;
413errout:
414 if (err < 0)
415 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
416}
417
Cong Wange4c7ed42013-08-31 13:44:33 +0800418static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000419{
420 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700421 struct vxlan_fdb f = {
422 .state = NUD_STALE,
423 };
424 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800425 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700426 .remote_vni = VXLAN_N_VID,
427 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700428
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200429 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000430}
431
432static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
433{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700434 struct vxlan_fdb f = {
435 .state = NUD_STALE,
436 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200437 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000438
David Stevense4f67ad2012-11-20 02:50:14 +0000439 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
440
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200441 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000442}
443
stephen hemmingerd3428942012-10-01 12:32:35 +0000444/* Hash Ethernet address */
445static u32 eth_hash(const unsigned char *addr)
446{
447 u64 value = get_unaligned((u64 *)addr);
448
449 /* only want 6 bytes */
450#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000451 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000452#else
453 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000454#endif
455 return hash_64(value, FDB_HASH_BITS);
456}
457
458/* Hash chain to use given mac address */
459static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
460 const u8 *mac)
461{
462 return &vxlan->fdb_head[eth_hash(mac)];
463}
464
465/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000466static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000467 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000468{
469 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
470 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000471
Sasha Levinb67bfe02013-02-27 17:06:00 -0800472 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700473 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000474 return f;
475 }
476
477 return NULL;
478}
479
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000480static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
481 const u8 *mac)
482{
483 struct vxlan_fdb *f;
484
485 f = __vxlan_find_mac(vxlan, mac);
486 if (f)
487 f->used = jiffies;
488
489 return f;
490}
491
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300492/* caller should hold vxlan->hash_lock */
493static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800494 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300495 __u32 vni, __u32 ifindex)
496{
497 struct vxlan_rdst *rd;
498
499 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800500 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300501 rd->remote_port == port &&
502 rd->remote_vni == vni &&
503 rd->remote_ifindex == ifindex)
504 return rd;
505 }
506
507 return NULL;
508}
509
Thomas Richter906dc182013-07-19 17:20:07 +0200510/* Replace destination of unicast mac */
511static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800512 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200513{
514 struct vxlan_rdst *rd;
515
516 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
517 if (rd)
518 return 0;
519
520 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
521 if (!rd)
522 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800523 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200524 rd->remote_port = port;
525 rd->remote_vni = vni;
526 rd->remote_ifindex = ifindex;
527 return 1;
528}
529
David Stevens66817122013-03-15 04:35:51 +0000530/* Add/update destinations for multicast */
531static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200532 union vxlan_addr *ip, __be16 port, __u32 vni,
533 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000534{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700535 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000536
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300537 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
538 if (rd)
539 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700540
David Stevens66817122013-03-15 04:35:51 +0000541 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
542 if (rd == NULL)
543 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800544 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000545 rd->remote_port = port;
546 rd->remote_vni = vni;
547 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700548
549 list_add_tail_rcu(&rd->list, &f->remotes);
550
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200551 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000552 return 1;
553}
554
Tom Herbertdfd86452015-01-12 17:00:38 -0800555static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
556 unsigned int off,
557 struct vxlanhdr *vh, size_t hdrlen,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800558 u32 data, struct gro_remcsum *grc,
559 bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -0800560{
561 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -0800562
563 if (skb->remcsum_offload)
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800564 return NULL;
Tom Herbertdfd86452015-01-12 17:00:38 -0800565
566 if (!NAPI_GRO_CB(skb)->csum_valid)
567 return NULL;
568
569 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
570 offset = start + ((data & VXLAN_RCO_UDP) ?
571 offsetof(struct udphdr, check) :
572 offsetof(struct tcphdr, check));
573
574 plen = hdrlen + offset + sizeof(u16);
575
576 /* Pull checksum that will be written */
577 if (skb_gro_header_hard(skb, off + plen)) {
578 vh = skb_gro_header_slow(skb, off + plen, off);
579 if (!vh)
580 return NULL;
581 }
582
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800583 skb_gro_remcsum_process(skb, (void *)vh + hdrlen,
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800584 start, offset, grc, nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -0800585
586 skb->remcsum_offload = 1;
587
588 return vh;
589}
590
Tom Herberta2b12f32015-01-12 17:00:37 -0800591static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
592 struct sk_buff *skb,
593 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200594{
595 struct sk_buff *p, **pp = NULL;
596 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800597 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200598 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800599 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
600 udp_offloads);
601 u32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800602 struct gro_remcsum grc;
603
604 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200605
606 off_vx = skb_gro_offset(skb);
607 hlen = off_vx + sizeof(*vh);
608 vh = skb_gro_header_fast(skb, off_vx);
609 if (skb_gro_header_hard(skb, hlen)) {
610 vh = skb_gro_header_slow(skb, hlen, off_vx);
611 if (unlikely(!vh))
612 goto out;
613 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200614
Tom Herbertdfd86452015-01-12 17:00:38 -0800615 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
616 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
617
618 flags = ntohl(vh->vx_flags);
619
620 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
621 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800622 ntohl(vh->vx_vni), &grc,
623 !!(vs->flags &
624 VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -0800625
626 if (!vh)
627 goto out;
628 }
629
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200630 flush = 0;
631
632 for (p = *head; p; p = p->next) {
633 if (!NAPI_GRO_CB(p)->same_flow)
634 continue;
635
636 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100637 if (vh->vx_flags != vh2->vx_flags ||
638 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200639 NAPI_GRO_CB(p)->same_flow = 0;
640 continue;
641 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200642 }
643
Jesse Gross9b174d82014-12-30 19:10:15 -0800644 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200645
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200646out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800647 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200648 NAPI_GRO_CB(skb)->flush |= flush;
649
650 return pp;
651}
652
Tom Herberta2b12f32015-01-12 17:00:37 -0800653static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
654 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200655{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800656 udp_tunnel_gro_complete(skb, nhoff);
657
Jesse Gross9b174d82014-12-30 19:10:15 -0800658 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200659}
660
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700661/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200662static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700663{
664 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200665 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700666 struct net *net = sock_net(sk);
667 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700668 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200669 int err;
670
671 if (sa_family == AF_INET) {
672 err = udp_add_offload(&vs->udp_offloads);
673 if (err)
674 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
675 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700676
677 rcu_read_lock();
678 for_each_netdev_rcu(net, dev) {
679 if (dev->netdev_ops->ndo_add_vxlan_port)
680 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
681 port);
682 }
683 rcu_read_unlock();
684}
685
686/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200687static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700688{
689 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200690 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700691 struct net *net = sock_net(sk);
692 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700693 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700694
695 rcu_read_lock();
696 for_each_netdev_rcu(net, dev) {
697 if (dev->netdev_ops->ndo_del_vxlan_port)
698 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
699 port);
700 }
701 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200702
703 if (sa_family == AF_INET)
704 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700705}
706
stephen hemmingerd3428942012-10-01 12:32:35 +0000707/* Add new entry to forwarding table -- assumes lock held */
708static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800709 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000710 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000711 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000712 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000713{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200714 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000715 struct vxlan_fdb *f;
716 int notify = 0;
717
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000718 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000719 if (f) {
720 if (flags & NLM_F_EXCL) {
721 netdev_dbg(vxlan->dev,
722 "lost race to create %pM\n", mac);
723 return -EEXIST;
724 }
725 if (f->state != state) {
726 f->state = state;
727 f->updated = jiffies;
728 notify = 1;
729 }
David Stevensae884082013-04-19 00:36:26 +0000730 if (f->flags != ndm_flags) {
731 f->flags = ndm_flags;
732 f->updated = jiffies;
733 notify = 1;
734 }
Thomas Richter906dc182013-07-19 17:20:07 +0200735 if ((flags & NLM_F_REPLACE)) {
736 /* Only change unicasts */
737 if (!(is_multicast_ether_addr(f->eth_addr) ||
738 is_zero_ether_addr(f->eth_addr))) {
Li RongQing60840422015-04-22 15:49:10 +0800739 notify |= vxlan_fdb_replace(f, ip, port, vni,
Thomas Richter906dc182013-07-19 17:20:07 +0200740 ifindex);
Thomas Richter906dc182013-07-19 17:20:07 +0200741 } else
742 return -EOPNOTSUPP;
743 }
David Stevens66817122013-03-15 04:35:51 +0000744 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300745 (is_multicast_ether_addr(f->eth_addr) ||
746 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200747 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
748 &rd);
David Stevens66817122013-03-15 04:35:51 +0000749
750 if (rc < 0)
751 return rc;
752 notify |= rc;
753 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000754 } else {
755 if (!(flags & NLM_F_CREATE))
756 return -ENOENT;
757
758 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
759 return -ENOSPC;
760
Thomas Richter906dc182013-07-19 17:20:07 +0200761 /* Disallow replace to add a multicast entry */
762 if ((flags & NLM_F_REPLACE) &&
763 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
764 return -EOPNOTSUPP;
765
Cong Wange4c7ed42013-08-31 13:44:33 +0800766 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000767 f = kmalloc(sizeof(*f), GFP_ATOMIC);
768 if (!f)
769 return -ENOMEM;
770
771 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000772 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000773 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000774 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700775 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000776 memcpy(f->eth_addr, mac, ETH_ALEN);
777
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200778 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700779
stephen hemmingerd3428942012-10-01 12:32:35 +0000780 ++vxlan->addrcnt;
781 hlist_add_head_rcu(&f->hlist,
782 vxlan_fdb_head(vxlan, mac));
783 }
784
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200785 if (notify) {
786 if (rd == NULL)
787 rd = first_remote_rtnl(f);
788 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
789 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000790
791 return 0;
792}
793
Wei Yongjun6706c822013-04-11 19:00:35 +0000794static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000795{
796 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700797 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000798
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700799 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000800 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000801 kfree(f);
802}
803
stephen hemmingerd3428942012-10-01 12:32:35 +0000804static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
805{
806 netdev_dbg(vxlan->dev,
807 "delete %pM\n", f->eth_addr);
808
809 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200810 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000811
812 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000813 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000814}
815
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300816static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800817 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300818{
819 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800820 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300821
822 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800823 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
824 if (err)
825 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300826 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800827 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
828 if (remote->sa.sa_family == AF_INET) {
829 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
830 ip->sa.sa_family = AF_INET;
831#if IS_ENABLED(CONFIG_IPV6)
832 } else {
833 ip->sin6.sin6_addr = in6addr_any;
834 ip->sa.sa_family = AF_INET6;
835#endif
836 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300837 }
838
839 if (tb[NDA_PORT]) {
840 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
841 return -EINVAL;
842 *port = nla_get_be16(tb[NDA_PORT]);
843 } else {
844 *port = vxlan->dst_port;
845 }
846
847 if (tb[NDA_VNI]) {
848 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
849 return -EINVAL;
850 *vni = nla_get_u32(tb[NDA_VNI]);
851 } else {
852 *vni = vxlan->default_dst.remote_vni;
853 }
854
855 if (tb[NDA_IFINDEX]) {
856 struct net_device *tdev;
857
858 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
859 return -EINVAL;
860 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800861 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300862 if (!tdev)
863 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300864 } else {
865 *ifindex = 0;
866 }
867
868 return 0;
869}
870
stephen hemmingerd3428942012-10-01 12:32:35 +0000871/* Add static entry (via netlink) */
872static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
873 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100874 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000875{
876 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300877 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800878 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000879 __be16 port;
880 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000881 int err;
882
883 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
884 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
885 ndm->ndm_state);
886 return -EINVAL;
887 }
888
889 if (tb[NDA_DST] == NULL)
890 return -EINVAL;
891
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300892 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
893 if (err)
894 return err;
David Stevens66817122013-03-15 04:35:51 +0000895
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300896 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
897 return -EAFNOSUPPORT;
898
stephen hemmingerd3428942012-10-01 12:32:35 +0000899 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800900 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000901 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000902 spin_unlock_bh(&vxlan->hash_lock);
903
904 return err;
905}
906
907/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000908static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
909 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100910 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000911{
912 struct vxlan_dev *vxlan = netdev_priv(dev);
913 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300914 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800915 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300916 __be16 port;
917 u32 vni, ifindex;
918 int err;
919
920 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
921 if (err)
922 return err;
923
924 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000925
926 spin_lock_bh(&vxlan->hash_lock);
927 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300928 if (!f)
929 goto out;
930
Cong Wange4c7ed42013-08-31 13:44:33 +0800931 if (!vxlan_addr_any(&ip)) {
932 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300933 if (!rd)
934 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000935 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300936
937 err = 0;
938
939 /* remove a destination if it's not the only one on the list,
940 * otherwise destroy the fdb entry
941 */
942 if (rd && !list_is_singular(&f->remotes)) {
943 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200944 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800945 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300946 goto out;
947 }
948
949 vxlan_fdb_destroy(vxlan, f);
950
951out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000952 spin_unlock_bh(&vxlan->hash_lock);
953
954 return err;
955}
956
957/* Dump forwarding table */
958static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400959 struct net_device *dev,
960 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000961{
962 struct vxlan_dev *vxlan = netdev_priv(dev);
963 unsigned int h;
964
965 for (h = 0; h < FDB_HASH_SIZE; ++h) {
966 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000967 int err;
968
Sasha Levinb67bfe02013-02-27 17:06:00 -0800969 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000970 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000971
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700972 if (idx < cb->args[0])
973 goto skip;
974
975 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000976 err = vxlan_fdb_info(skb, vxlan, f,
977 NETLINK_CB(cb->skb).portid,
978 cb->nlh->nlmsg_seq,
979 RTM_NEWNEIGH,
980 NLM_F_MULTI, rd);
981 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700982 goto out;
David Stevens66817122013-03-15 04:35:51 +0000983 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700984skip:
985 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000986 }
987 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700988out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000989 return idx;
990}
991
992/* Watch incoming packets to learn mapping between Ethernet address
993 * and Tunnel endpoint.
Simon Hormanc4b49512015-04-02 11:17:58 +0900994 * Return true if packet is bogus and should be dropped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000995 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700996static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800997 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000998{
999 struct vxlan_dev *vxlan = netdev_priv(dev);
1000 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001001
1002 f = vxlan_find_mac(vxlan, src_mac);
1003 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001004 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001005
Cong Wange4c7ed42013-08-31 13:44:33 +08001006 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001007 return false;
1008
1009 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -07001010 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -07001011 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001012
1013 if (net_ratelimit())
1014 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001015 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001016 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001017
Cong Wange4c7ed42013-08-31 13:44:33 +08001018 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001019 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001020 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001021 } else {
1022 /* learned new entry */
1023 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001024
1025 /* close off race between vxlan_flush and incoming packets */
1026 if (netif_running(dev))
1027 vxlan_fdb_create(vxlan, src_mac, src_ip,
1028 NUD_REACHABLE,
1029 NLM_F_EXCL|NLM_F_CREATE,
1030 vxlan->dst_port,
1031 vxlan->default_dst.remote_vni,
1032 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001033 spin_unlock(&vxlan->hash_lock);
1034 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001035
1036 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001037}
1038
stephen hemmingerd3428942012-10-01 12:32:35 +00001039/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001040static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001041{
stephen hemminger553675f2013-05-16 11:35:20 +00001042 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001043
Gao feng95ab0992013-12-10 16:37:33 +08001044 /* The vxlan_sock is only used by dev, leaving group has
1045 * no effect on other vxlan devices.
1046 */
1047 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1048 return false;
1049
stephen hemminger553675f2013-05-16 11:35:20 +00001050 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001051 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001052 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001053
Gao feng95ab0992013-12-10 16:37:33 +08001054 if (vxlan->vn_sock != dev->vn_sock)
1055 continue;
1056
1057 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1058 &dev->default_dst.remote_ip))
1059 continue;
1060
1061 if (vxlan->default_dst.remote_ifindex !=
1062 dev->default_dst.remote_ifindex)
1063 continue;
1064
1065 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001066 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001067
1068 return false;
1069}
1070
Pravin B Shelar012a5722013-08-19 11:23:07 -07001071void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001072{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001073 struct sock *sk = vs->sock->sk;
1074 struct net *net = sock_net(sk);
1075 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001076
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001077 if (!atomic_dec_and_test(&vs->refcnt))
1078 return;
1079
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001080 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001081 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001082 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001083 spin_unlock(&vn->sock_lock);
1084
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001085 queue_work(vxlan_wq, &vs->del_work);
1086}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001087EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001088
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001089/* Update multicast group membership when first VNI on
Simon Hormanc4b49512015-04-02 11:17:58 +09001090 * multicast address is brought up
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001091 */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001092static int vxlan_igmp_join(struct vxlan_dev *vxlan)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001093{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001094 struct vxlan_sock *vs = vxlan->vn_sock;
1095 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001096 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1097 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001098 int ret = -EINVAL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001099
stephen hemmingerd3428942012-10-01 12:32:35 +00001100 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001101 if (ip->sa.sa_family == AF_INET) {
1102 struct ip_mreqn mreq = {
1103 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1104 .imr_ifindex = ifindex,
1105 };
1106
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001107 ret = ip_mc_join_group(sk, &mreq);
Cong Wange4c7ed42013-08-31 13:44:33 +08001108#if IS_ENABLED(CONFIG_IPV6)
1109 } else {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001110 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1111 &ip->sin6.sin6_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001112#endif
1113 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001114 release_sock(sk);
1115
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001116 return ret;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001117}
1118
1119/* Inverse of vxlan_igmp_join when last VNI is brought down */
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001120static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
stephen hemminger3fc2de22013-07-18 08:40:15 -07001121{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001122 struct vxlan_sock *vs = vxlan->vn_sock;
1123 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001124 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1125 int ifindex = vxlan->default_dst.remote_ifindex;
Marcelo Ricardo Leitner149d7542015-03-20 10:26:21 -03001126 int ret = -EINVAL;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001127
1128 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001129 if (ip->sa.sa_family == AF_INET) {
1130 struct ip_mreqn mreq = {
1131 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1132 .imr_ifindex = ifindex,
1133 };
1134
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001135 ret = ip_mc_leave_group(sk, &mreq);
Cong Wange4c7ed42013-08-31 13:44:33 +08001136#if IS_ENABLED(CONFIG_IPV6)
1137 } else {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001138 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1139 &ip->sin6.sin6_addr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001140#endif
1141 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001142 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001143
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03001144 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00001145}
1146
Tom Herbertdfd86452015-01-12 17:00:38 -08001147static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001148 size_t hdrlen, u32 data, bool nopartial)
Tom Herbertdfd86452015-01-12 17:00:38 -08001149{
1150 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -08001151
Tom Herbertdfd86452015-01-12 17:00:38 -08001152 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1153 offset = start + ((data & VXLAN_RCO_UDP) ?
1154 offsetof(struct udphdr, check) :
1155 offsetof(struct tcphdr, check));
1156
1157 plen = hdrlen + offset + sizeof(u16);
1158
1159 if (!pskb_may_pull(skb, plen))
1160 return NULL;
1161
1162 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1163
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001164 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset,
1165 nopartial);
Tom Herbertdfd86452015-01-12 17:00:38 -08001166
1167 return vh;
1168}
1169
stephen hemmingerd3428942012-10-01 12:32:35 +00001170/* Callback from net/ipv4/udp.c to receive packets */
1171static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1172{
Thomas Grafee122c72015-07-21 10:43:58 +02001173 struct metadata_dst *tun_dst = NULL;
1174 struct ip_tunnel_info *info;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001175 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001176 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001177 u32 flags, vni;
Thomas Grafee122c72015-07-21 10:43:58 +02001178 struct vxlan_metadata _md;
1179 struct vxlan_metadata *md = &_md;
stephen hemmingerd3428942012-10-01 12:32:35 +00001180
stephen hemmingerd3428942012-10-01 12:32:35 +00001181 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001182 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001183 goto error;
1184
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001185 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001186 flags = ntohl(vxh->vx_flags);
1187 vni = ntohl(vxh->vx_vni);
1188
1189 if (flags & VXLAN_HF_VNI) {
1190 flags &= ~VXLAN_HF_VNI;
1191 } else {
1192 /* VNI flag always required to be set */
1193 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001194 }
1195
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001196 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001197 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001198 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001199
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001200 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001201 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001202 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001203
Tom Herbertdfd86452015-01-12 17:00:38 -08001204 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
Tom Herbert0ace2ca2015-02-10 16:30:32 -08001205 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni,
1206 !!(vs->flags & VXLAN_F_REMCSUM_NOPARTIAL));
Tom Herbertdfd86452015-01-12 17:00:38 -08001207 if (!vxh)
1208 goto drop;
1209
1210 flags &= ~VXLAN_HF_RCO;
Alexey Kodanev40fb70f2015-03-13 19:13:53 +03001211 vni &= VXLAN_VNI_MASK;
Tom Herbertdfd86452015-01-12 17:00:38 -08001212 }
1213
Thomas Grafee122c72015-07-21 10:43:58 +02001214 if (vxlan_collect_metadata(vs)) {
1215 const struct iphdr *iph = ip_hdr(skb);
1216
1217 tun_dst = metadata_dst_alloc(sizeof(*md), GFP_ATOMIC);
1218 if (!tun_dst)
1219 goto drop;
1220
1221 info = &tun_dst->u.tun_info;
1222 info->key.ipv4_src = iph->saddr;
1223 info->key.ipv4_dst = iph->daddr;
1224 info->key.ipv4_tos = iph->tos;
1225 info->key.ipv4_ttl = iph->ttl;
1226 info->key.tp_src = udp_hdr(skb)->source;
1227 info->key.tp_dst = udp_hdr(skb)->dest;
1228
1229 info->mode = IP_TUNNEL_INFO_RX;
1230 info->key.tun_flags = TUNNEL_KEY;
1231 info->key.tun_id = cpu_to_be64(vni >> 8);
1232 if (udp_hdr(skb)->check != 0)
1233 info->key.tun_flags |= TUNNEL_CSUM;
1234
1235 md = ip_tunnel_info_opts(info, sizeof(*md));
1236 md->tun_dst = tun_dst;
1237 } else {
1238 memset(md, 0, sizeof(*md));
1239 }
1240
Thomas Graf35114942015-01-15 03:53:55 +01001241 /* For backwards compatibility, only allow reserved fields to be
1242 * used by VXLAN extensions if explicitly requested.
1243 */
1244 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1245 struct vxlanhdr_gbp *gbp;
1246
1247 gbp = (struct vxlanhdr_gbp *)vxh;
Thomas Grafee122c72015-07-21 10:43:58 +02001248 md->gbp = ntohs(gbp->policy_id);
1249
1250 if (tun_dst)
1251 info->key.tun_flags |= TUNNEL_VXLAN_OPT;
Thomas Graf35114942015-01-15 03:53:55 +01001252
1253 if (gbp->dont_learn)
Thomas Grafee122c72015-07-21 10:43:58 +02001254 md->gbp |= VXLAN_GBP_DONT_LEARN;
Thomas Graf35114942015-01-15 03:53:55 +01001255
1256 if (gbp->policy_applied)
Thomas Grafee122c72015-07-21 10:43:58 +02001257 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
Thomas Graf35114942015-01-15 03:53:55 +01001258
1259 flags &= ~VXLAN_GBP_USED_BITS;
1260 }
1261
Alexey Kodanev40fb70f2015-03-13 19:13:53 +03001262 if (flags || vni & ~VXLAN_VNI_MASK) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001263 /* If there are any unprocessed flags remaining treat
1264 * this as a malformed packet. This behavior diverges from
1265 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1266 * in reserved fields are to be ignored. The approach here
Simon Hormanc4b49512015-04-02 11:17:58 +09001267 * maintains compatibility with previous stack code, and also
Tom Herbert3bf39472015-01-08 12:31:18 -08001268 * is more robust and provides a little more security in
1269 * adding extensions to VXLAN.
1270 */
1271
1272 goto bad_flags;
1273 }
1274
Thomas Grafee122c72015-07-21 10:43:58 +02001275 md->vni = vxh->vx_vni;
1276 vs->rcv(vs, skb, md);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001277 return 0;
1278
1279drop:
1280 /* Consume bad packet */
1281 kfree_skb(skb);
1282 return 0;
1283
Tom Herbert3bf39472015-01-08 12:31:18 -08001284bad_flags:
1285 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1286 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1287
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001288error:
Thomas Grafee122c72015-07-21 10:43:58 +02001289 if (tun_dst)
1290 dst_release((struct dst_entry *)tun_dst);
1291
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001292 /* Return non vxlan pkt */
1293 return 1;
1294}
1295
Thomas Graf35114942015-01-15 03:53:55 +01001296static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1297 struct vxlan_metadata *md)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001298{
Cong Wange4c7ed42013-08-31 13:44:33 +08001299 struct iphdr *oip = NULL;
1300 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001301 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001302 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001303 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001304 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001305 int err = 0;
1306 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001307
Thomas Grafee122c72015-07-21 10:43:58 +02001308 /* For flow based devices, map all packets to VNI 0 */
1309 if (vs->flags & VXLAN_F_FLOW_BASED)
1310 vni = 0;
1311 else
1312 vni = ntohl(md->vni) >> 8;
1313
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001314 /* Is this VNI defined? */
1315 vxlan = vxlan_vs_find_vni(vs, vni);
1316 if (!vxlan)
1317 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001318
Cong Wange4c7ed42013-08-31 13:44:33 +08001319 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001320 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001321 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001322 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001323 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001324
1325 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001326 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001327 goto drop;
1328
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001329 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001330 if (remote_ip->sa.sa_family == AF_INET) {
1331 oip = ip_hdr(skb);
1332 saddr.sin.sin_addr.s_addr = oip->saddr;
1333 saddr.sa.sa_family = AF_INET;
1334#if IS_ENABLED(CONFIG_IPV6)
1335 } else {
1336 oip6 = ipv6_hdr(skb);
1337 saddr.sin6.sin6_addr = oip6->saddr;
1338 saddr.sa.sa_family = AF_INET6;
1339#endif
1340 }
1341
Thomas Grafee122c72015-07-21 10:43:58 +02001342 if (md->tun_dst) {
1343 skb_dst_set(skb, (struct dst_entry *)md->tun_dst);
1344 md->tun_dst = NULL;
1345 }
1346
stephen hemminger26a41ae62013-06-17 12:09:58 -07001347 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001348 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001349 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001350
stephen hemmingerd3428942012-10-01 12:32:35 +00001351 skb_reset_network_header(skb);
Thomas Grafee122c72015-07-21 10:43:58 +02001352 /* In flow-based mode, GBP is carried in dst_metadata */
1353 if (!(vs->flags & VXLAN_F_FLOW_BASED))
1354 skb->mark = md->gbp;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001355
Cong Wange4c7ed42013-08-31 13:44:33 +08001356 if (oip6)
1357 err = IP6_ECN_decapsulate(oip6, skb);
1358 if (oip)
1359 err = IP_ECN_decapsulate(oip, skb);
1360
stephen hemmingerd3428942012-10-01 12:32:35 +00001361 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001362 if (log_ecn_error) {
1363 if (oip6)
1364 net_info_ratelimited("non-ECT from %pI6\n",
1365 &oip6->saddr);
1366 if (oip)
1367 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1368 &oip->saddr, oip->tos);
1369 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001370 if (err > 1) {
1371 ++vxlan->dev->stats.rx_frame_errors;
1372 ++vxlan->dev->stats.rx_errors;
1373 goto drop;
1374 }
1375 }
1376
Pravin B Shelare8171042013-03-25 14:49:46 +00001377 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001378 u64_stats_update_begin(&stats->syncp);
1379 stats->rx_packets++;
1380 stats->rx_bytes += skb->len;
1381 u64_stats_update_end(&stats->syncp);
1382
1383 netif_rx(skb);
1384
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001385 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001386drop:
Thomas Grafee122c72015-07-21 10:43:58 +02001387 if (md->tun_dst)
1388 dst_release((struct dst_entry *)md->tun_dst);
1389
stephen hemmingerd3428942012-10-01 12:32:35 +00001390 /* Consume bad packet */
1391 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001392}
1393
David Stevense4f67ad2012-11-20 02:50:14 +00001394static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1395{
1396 struct vxlan_dev *vxlan = netdev_priv(dev);
1397 struct arphdr *parp;
1398 u8 *arpptr, *sha;
1399 __be32 sip, tip;
1400 struct neighbour *n;
1401
1402 if (dev->flags & IFF_NOARP)
1403 goto out;
1404
1405 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1406 dev->stats.tx_dropped++;
1407 goto out;
1408 }
1409 parp = arp_hdr(skb);
1410
1411 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1412 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1413 parp->ar_pro != htons(ETH_P_IP) ||
1414 parp->ar_op != htons(ARPOP_REQUEST) ||
1415 parp->ar_hln != dev->addr_len ||
1416 parp->ar_pln != 4)
1417 goto out;
1418 arpptr = (u8 *)parp + sizeof(struct arphdr);
1419 sha = arpptr;
1420 arpptr += dev->addr_len; /* sha */
1421 memcpy(&sip, arpptr, sizeof(sip));
1422 arpptr += sizeof(sip);
1423 arpptr += dev->addr_len; /* tha */
1424 memcpy(&tip, arpptr, sizeof(tip));
1425
1426 if (ipv4_is_loopback(tip) ||
1427 ipv4_is_multicast(tip))
1428 goto out;
1429
1430 n = neigh_lookup(&arp_tbl, &tip, dev);
1431
1432 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001433 struct vxlan_fdb *f;
1434 struct sk_buff *reply;
1435
1436 if (!(n->nud_state & NUD_CONNECTED)) {
1437 neigh_release(n);
1438 goto out;
1439 }
1440
1441 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001442 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001443 /* bridge-local neighbor */
1444 neigh_release(n);
1445 goto out;
1446 }
1447
1448 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1449 n->ha, sha);
1450
1451 neigh_release(n);
1452
David Stevens73461352014-03-18 12:32:29 -04001453 if (reply == NULL)
1454 goto out;
1455
David Stevense4f67ad2012-11-20 02:50:14 +00001456 skb_reset_mac_header(reply);
1457 __skb_pull(reply, skb_network_offset(reply));
1458 reply->ip_summed = CHECKSUM_UNNECESSARY;
1459 reply->pkt_type = PACKET_HOST;
1460
1461 if (netif_rx_ni(reply) == NET_RX_DROP)
1462 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001463 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1464 union vxlan_addr ipa = {
1465 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001466 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001467 };
1468
1469 vxlan_ip_miss(dev, &ipa);
1470 }
David Stevense4f67ad2012-11-20 02:50:14 +00001471out:
1472 consume_skb(skb);
1473 return NETDEV_TX_OK;
1474}
1475
Cong Wangf564f452013-08-31 13:44:36 +08001476#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001477static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1478 struct neighbour *n, bool isrouter)
1479{
1480 struct net_device *dev = request->dev;
1481 struct sk_buff *reply;
1482 struct nd_msg *ns, *na;
1483 struct ipv6hdr *pip6;
1484 u8 *daddr;
1485 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1486 int ns_olen;
1487 int i, len;
1488
1489 if (dev == NULL)
1490 return NULL;
1491
1492 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1493 sizeof(*na) + na_olen + dev->needed_tailroom;
1494 reply = alloc_skb(len, GFP_ATOMIC);
1495 if (reply == NULL)
1496 return NULL;
1497
1498 reply->protocol = htons(ETH_P_IPV6);
1499 reply->dev = dev;
1500 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1501 skb_push(reply, sizeof(struct ethhdr));
1502 skb_set_mac_header(reply, 0);
1503
1504 ns = (struct nd_msg *)skb_transport_header(request);
1505
1506 daddr = eth_hdr(request)->h_source;
1507 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1508 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1509 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1510 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1511 break;
1512 }
1513 }
1514
1515 /* Ethernet header */
1516 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1517 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1518 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1519 reply->protocol = htons(ETH_P_IPV6);
1520
1521 skb_pull(reply, sizeof(struct ethhdr));
1522 skb_set_network_header(reply, 0);
1523 skb_put(reply, sizeof(struct ipv6hdr));
1524
1525 /* IPv6 header */
1526
1527 pip6 = ipv6_hdr(reply);
1528 memset(pip6, 0, sizeof(struct ipv6hdr));
1529 pip6->version = 6;
1530 pip6->priority = ipv6_hdr(request)->priority;
1531 pip6->nexthdr = IPPROTO_ICMPV6;
1532 pip6->hop_limit = 255;
1533 pip6->daddr = ipv6_hdr(request)->saddr;
1534 pip6->saddr = *(struct in6_addr *)n->primary_key;
1535
1536 skb_pull(reply, sizeof(struct ipv6hdr));
1537 skb_set_transport_header(reply, 0);
1538
1539 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1540
1541 /* Neighbor Advertisement */
1542 memset(na, 0, sizeof(*na)+na_olen);
1543 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1544 na->icmph.icmp6_router = isrouter;
1545 na->icmph.icmp6_override = 1;
1546 na->icmph.icmp6_solicited = 1;
1547 na->target = ns->target;
1548 ether_addr_copy(&na->opt[2], n->ha);
1549 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1550 na->opt[1] = na_olen >> 3;
1551
1552 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1553 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1554 csum_partial(na, sizeof(*na)+na_olen, 0));
1555
1556 pip6->payload_len = htons(sizeof(*na)+na_olen);
1557
1558 skb_push(reply, sizeof(struct ipv6hdr));
1559
1560 reply->ip_summed = CHECKSUM_UNNECESSARY;
1561
1562 return reply;
1563}
1564
Cong Wangf564f452013-08-31 13:44:36 +08001565static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1566{
1567 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001568 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001569 const struct ipv6hdr *iphdr;
1570 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001571 struct neighbour *n;
1572 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001573
1574 in6_dev = __in6_dev_get(dev);
1575 if (!in6_dev)
1576 goto out;
1577
Cong Wangf564f452013-08-31 13:44:36 +08001578 iphdr = ipv6_hdr(skb);
1579 saddr = &iphdr->saddr;
1580 daddr = &iphdr->daddr;
1581
Cong Wangf564f452013-08-31 13:44:36 +08001582 msg = (struct nd_msg *)skb_transport_header(skb);
1583 if (msg->icmph.icmp6_code != 0 ||
1584 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1585 goto out;
1586
David Stevens4b29dba2014-03-24 10:39:58 -04001587 if (ipv6_addr_loopback(daddr) ||
1588 ipv6_addr_is_multicast(&msg->target))
1589 goto out;
1590
1591 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001592
1593 if (n) {
1594 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001595 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001596
1597 if (!(n->nud_state & NUD_CONNECTED)) {
1598 neigh_release(n);
1599 goto out;
1600 }
1601
1602 f = vxlan_find_mac(vxlan, n->ha);
1603 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1604 /* bridge-local neighbor */
1605 neigh_release(n);
1606 goto out;
1607 }
1608
David Stevens4b29dba2014-03-24 10:39:58 -04001609 reply = vxlan_na_create(skb, n,
1610 !!(f ? f->flags & NTF_ROUTER : 0));
1611
Cong Wangf564f452013-08-31 13:44:36 +08001612 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001613
1614 if (reply == NULL)
1615 goto out;
1616
1617 if (netif_rx_ni(reply) == NET_RX_DROP)
1618 dev->stats.rx_dropped++;
1619
Cong Wangf564f452013-08-31 13:44:36 +08001620 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001621 union vxlan_addr ipa = {
1622 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001623 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001624 };
1625
Cong Wangf564f452013-08-31 13:44:36 +08001626 vxlan_ip_miss(dev, &ipa);
1627 }
1628
1629out:
1630 consume_skb(skb);
1631 return NETDEV_TX_OK;
1632}
1633#endif
1634
David Stevense4f67ad2012-11-20 02:50:14 +00001635static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1636{
1637 struct vxlan_dev *vxlan = netdev_priv(dev);
1638 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001639
1640 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1641 return false;
1642
1643 n = NULL;
1644 switch (ntohs(eth_hdr(skb)->h_proto)) {
1645 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001646 {
1647 struct iphdr *pip;
1648
David Stevense4f67ad2012-11-20 02:50:14 +00001649 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1650 return false;
1651 pip = ip_hdr(skb);
1652 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001653 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1654 union vxlan_addr ipa = {
1655 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001656 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001657 };
1658
1659 vxlan_ip_miss(dev, &ipa);
1660 return false;
1661 }
1662
David Stevense4f67ad2012-11-20 02:50:14 +00001663 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001664 }
1665#if IS_ENABLED(CONFIG_IPV6)
1666 case ETH_P_IPV6:
1667 {
1668 struct ipv6hdr *pip6;
1669
1670 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1671 return false;
1672 pip6 = ipv6_hdr(skb);
1673 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1674 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1675 union vxlan_addr ipa = {
1676 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001677 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001678 };
1679
1680 vxlan_ip_miss(dev, &ipa);
1681 return false;
1682 }
1683
1684 break;
1685 }
1686#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001687 default:
1688 return false;
1689 }
1690
1691 if (n) {
1692 bool diff;
1693
Joe Perches7367d0b2013-09-01 11:51:23 -07001694 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001695 if (diff) {
1696 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1697 dev->addr_len);
1698 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1699 }
1700 neigh_release(n);
1701 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001702 }
1703
David Stevense4f67ad2012-11-20 02:50:14 +00001704 return false;
1705}
1706
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001707static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001708 struct vxlan_metadata *md)
1709{
1710 struct vxlanhdr_gbp *gbp;
1711
Thomas Grafdb79a622015-02-04 17:00:04 +01001712 if (!md->gbp)
1713 return;
1714
Thomas Graf35114942015-01-15 03:53:55 +01001715 gbp = (struct vxlanhdr_gbp *)vxh;
1716 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1717
1718 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1719 gbp->dont_learn = 1;
1720
1721 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1722 gbp->policy_applied = 1;
1723
1724 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1725}
1726
Cong Wange4c7ed42013-08-31 13:44:33 +08001727#if IS_ENABLED(CONFIG_IPV6)
David Miller79b16aa2015-04-05 22:19:09 -04001728static int vxlan6_xmit_skb(struct dst_entry *dst, struct sock *sk,
1729 struct sk_buff *skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001730 struct net_device *dev, struct in6_addr *saddr,
1731 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Thomas Graf35114942015-01-15 03:53:55 +01001732 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001733 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Cong Wange4c7ed42013-08-31 13:44:33 +08001734{
Cong Wange4c7ed42013-08-31 13:44:33 +08001735 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001736 int min_headroom;
1737 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001738 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbertdfd86452015-01-12 17:00:38 -08001739 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1740 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001741
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001742 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001743 skb->ip_summed == CHECKSUM_PARTIAL) {
1744 int csum_start = skb_checksum_start_offset(skb);
1745
1746 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1747 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1748 (skb->csum_offset == offsetof(struct udphdr, check) ||
1749 skb->csum_offset == offsetof(struct tcphdr, check))) {
1750 udp_sum = false;
1751 type |= SKB_GSO_TUNNEL_REMCSUM;
1752 }
1753 }
1754
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001755 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001756
Cong Wange4c7ed42013-08-31 13:44:33 +08001757 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1758 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001759 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001760
1761 /* Need space for new headers (invalidates iph ptr) */
1762 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001763 if (unlikely(err)) {
1764 kfree_skb(skb);
1765 goto err;
1766 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001767
Jiri Pirko59682502014-11-19 14:04:59 +01001768 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001769 if (WARN_ON(!skb)) {
1770 err = -ENOMEM;
1771 goto err;
1772 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001773
Jesse Grossb736a622015-04-09 11:19:14 -07001774 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1775 if (IS_ERR(skb)) {
1776 err = -EINVAL;
1777 goto err;
1778 }
1779
Cong Wange4c7ed42013-08-31 13:44:33 +08001780 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001781 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001782 vxh->vx_vni = md->vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001783
Tom Herbertdfd86452015-01-12 17:00:38 -08001784 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1785 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1786 VXLAN_RCO_SHIFT;
1787
1788 if (skb->csum_offset == offsetof(struct udphdr, check))
1789 data |= VXLAN_RCO_UDP;
1790
1791 vxh->vx_vni |= htonl(data);
1792 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1793
1794 if (!skb_is_gso(skb)) {
1795 skb->ip_summed = CHECKSUM_NONE;
1796 skb->encapsulation = 0;
1797 }
1798 }
1799
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001800 if (vxflags & VXLAN_F_GBP)
1801 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001802
Tom Herbert996c9fd2014-09-29 20:22:33 -07001803 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1804
David Miller79b16aa2015-04-05 22:19:09 -04001805 udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio,
Tom Herbertd998f8e2015-01-20 11:23:04 -08001806 ttl, src_port, dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001807 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
Cong Wange4c7ed42013-08-31 13:44:33 +08001808 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001809err:
1810 dst_release(dst);
1811 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001812}
1813#endif
1814
David Miller79b16aa2015-04-05 22:19:09 -04001815int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
Pravin B Shelar49560532013-08-19 11:23:17 -07001816 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Thomas Graf35114942015-01-15 03:53:55 +01001817 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001818 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Pravin B Shelar49560532013-08-19 11:23:17 -07001819{
1820 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001821 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001822 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001823 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
Tom Herbertdfd86452015-01-12 17:00:38 -08001824 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1825 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001826
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001827 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001828 skb->ip_summed == CHECKSUM_PARTIAL) {
1829 int csum_start = skb_checksum_start_offset(skb);
1830
1831 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1832 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1833 (skb->csum_offset == offsetof(struct udphdr, check) ||
1834 skb->csum_offset == offsetof(struct tcphdr, check))) {
1835 udp_sum = false;
1836 type |= SKB_GSO_TUNNEL_REMCSUM;
1837 }
1838 }
1839
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001840 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001841 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001842 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001843
1844 /* Need space for new headers (invalidates iph ptr) */
1845 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001846 if (unlikely(err)) {
1847 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001848 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001849 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001850
Jiri Pirko59682502014-11-19 14:04:59 +01001851 skb = vlan_hwaccel_push_inside(skb);
1852 if (WARN_ON(!skb))
1853 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001854
Jesse Grossb736a622015-04-09 11:19:14 -07001855 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1856 if (IS_ERR(skb))
1857 return PTR_ERR(skb);
1858
Pravin B Shelar49560532013-08-19 11:23:17 -07001859 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001860 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001861 vxh->vx_vni = md->vni;
Pravin B Shelar49560532013-08-19 11:23:17 -07001862
Tom Herbertdfd86452015-01-12 17:00:38 -08001863 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1864 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1865 VXLAN_RCO_SHIFT;
1866
1867 if (skb->csum_offset == offsetof(struct udphdr, check))
1868 data |= VXLAN_RCO_UDP;
1869
1870 vxh->vx_vni |= htonl(data);
1871 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1872
1873 if (!skb_is_gso(skb)) {
1874 skb->ip_summed = CHECKSUM_NONE;
1875 skb->encapsulation = 0;
1876 }
1877 }
1878
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001879 if (vxflags & VXLAN_F_GBP)
1880 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001881
Tom Herbert996c9fd2014-09-29 20:22:33 -07001882 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1883
David Miller79b16aa2015-04-05 22:19:09 -04001884 return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
Tom Herbertd998f8e2015-01-20 11:23:04 -08001885 ttl, df, src_port, dst_port, xnet,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001886 !(vxflags & VXLAN_F_UDP_CSUM));
Pravin B Shelar49560532013-08-19 11:23:17 -07001887}
1888EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1889
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001890/* Bypass encapsulation if the destination is local */
1891static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1892 struct vxlan_dev *dst_vxlan)
1893{
Li RongQing8f849852014-01-04 13:57:59 +08001894 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001895 union vxlan_addr loopback;
1896 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001897 struct net_device *dev = skb->dev;
1898 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001899
Li RongQing8f849852014-01-04 13:57:59 +08001900 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1901 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001902 skb->pkt_type = PACKET_HOST;
1903 skb->encapsulation = 0;
1904 skb->dev = dst_vxlan->dev;
1905 __skb_pull(skb, skb_network_offset(skb));
1906
Cong Wange4c7ed42013-08-31 13:44:33 +08001907 if (remote_ip->sa.sa_family == AF_INET) {
1908 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1909 loopback.sa.sa_family = AF_INET;
1910#if IS_ENABLED(CONFIG_IPV6)
1911 } else {
1912 loopback.sin6.sin6_addr = in6addr_loopback;
1913 loopback.sa.sa_family = AF_INET6;
1914#endif
1915 }
1916
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001917 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001918 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001919
1920 u64_stats_update_begin(&tx_stats->syncp);
1921 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001922 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001923 u64_stats_update_end(&tx_stats->syncp);
1924
1925 if (netif_rx(skb) == NET_RX_SUCCESS) {
1926 u64_stats_update_begin(&rx_stats->syncp);
1927 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001928 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001929 u64_stats_update_end(&rx_stats->syncp);
1930 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001931 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001932 }
1933}
1934
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001935static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1936 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001937{
Thomas Grafee122c72015-07-21 10:43:58 +02001938 struct ip_tunnel_info *info = skb_tunnel_info(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001939 struct vxlan_dev *vxlan = netdev_priv(dev);
David Miller79b16aa2015-04-05 22:19:09 -04001940 struct sock *sk = vxlan->vn_sock->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001941 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001942 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001943 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001944 union vxlan_addr *dst;
Thomas Grafee122c72015-07-21 10:43:58 +02001945 union vxlan_addr remote_ip;
1946 struct vxlan_metadata _md;
1947 struct vxlan_metadata *md = &_md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001948 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001949 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001950 __be16 df = 0;
1951 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001952 int err;
Thomas Grafee122c72015-07-21 10:43:58 +02001953 u32 flags = vxlan->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001954
Thomas Grafee122c72015-07-21 10:43:58 +02001955 if (rdst) {
1956 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1957 vni = rdst->remote_vni;
1958 dst = &rdst->remote_ip;
1959 } else {
1960 if (!info) {
1961 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1962 dev->name);
1963 goto drop;
1964 }
1965
1966 dst_port = info->key.tp_dst ? : vxlan->dst_port;
1967 vni = be64_to_cpu(info->key.tun_id);
1968 remote_ip.sin.sin_family = AF_INET;
1969 remote_ip.sin.sin_addr.s_addr = info->key.ipv4_dst;
1970 dst = &remote_ip;
1971 }
David Stevense4f67ad2012-11-20 02:50:14 +00001972
Cong Wange4c7ed42013-08-31 13:44:33 +08001973 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001974 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001975 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001976 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001977 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001978 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001979 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001980 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001981
stephen hemmingerd3428942012-10-01 12:32:35 +00001982 old_iph = ip_hdr(skb);
1983
stephen hemmingerd3428942012-10-01 12:32:35 +00001984 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001985 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001986 ttl = 1;
1987
1988 tos = vxlan->tos;
1989 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001990 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001991
Tom Herbert535fb8d2014-07-01 21:32:49 -07001992 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1993 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001994
Cong Wange4c7ed42013-08-31 13:44:33 +08001995 if (dst->sa.sa_family == AF_INET) {
Thomas Grafee122c72015-07-21 10:43:58 +02001996 if (info) {
1997 if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
1998 df = htons(IP_DF);
1999 if (info->key.tun_flags & TUNNEL_CSUM)
2000 flags |= VXLAN_F_UDP_CSUM;
2001 else
2002 flags &= ~VXLAN_F_UDP_CSUM;
2003
2004 ttl = info->key.ipv4_ttl;
2005 tos = info->key.ipv4_tos;
2006
2007 if (info->options_len)
2008 md = ip_tunnel_info_opts(info, sizeof(*md));
2009 } else {
2010 md->gbp = skb->mark;
2011 }
2012
Cong Wange4c7ed42013-08-31 13:44:33 +08002013 memset(&fl4, 0, sizeof(fl4));
Thomas Grafee122c72015-07-21 10:43:58 +02002014 fl4.flowi4_oif = rdst ? rdst->remote_ifindex : 0;
Cong Wange4c7ed42013-08-31 13:44:33 +08002015 fl4.flowi4_tos = RT_TOS(tos);
Thomas Graf239fb792015-05-05 15:09:21 +02002016 fl4.flowi4_mark = skb->mark;
2017 fl4.flowi4_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08002018 fl4.daddr = dst->sin.sin_addr.s_addr;
2019 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00002020
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002021 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08002022 if (IS_ERR(rt)) {
2023 netdev_dbg(dev, "no route to %pI4\n",
2024 &dst->sin.sin_addr.s_addr);
2025 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002026 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002027 }
2028
2029 if (rt->dst.dev == dev) {
2030 netdev_dbg(dev, "circular route to %pI4\n",
2031 &dst->sin.sin_addr.s_addr);
2032 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08002033 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08002034 }
2035
2036 /* Bypass encapsulation if the destination is local */
2037 if (rt->rt_flags & RTCF_LOCAL &&
2038 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2039 struct vxlan_dev *dst_vxlan;
2040
2041 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002042 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002043 dst->sa.sa_family, dst_port,
2044 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002045 if (!dst_vxlan)
2046 goto tx_error;
2047 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2048 return;
2049 }
2050
2051 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2052 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Thomas Grafee122c72015-07-21 10:43:58 +02002053 md->vni = htonl(vni << 8);
David Miller79b16aa2015-04-05 22:19:09 -04002054 err = vxlan_xmit_skb(rt, sk, skb, fl4.saddr,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002055 dst->sin.sin_addr.s_addr, tos, ttl, df,
Thomas Grafee122c72015-07-21 10:43:58 +02002056 src_port, dst_port, md,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002057 !net_eq(vxlan->net, dev_net(vxlan->dev)),
Thomas Grafee122c72015-07-21 10:43:58 +02002058 flags);
Pravin B Shelar74f47272014-12-23 16:20:36 -08002059 if (err < 0) {
2060 /* skb is already freed. */
2061 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08002062 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08002063 }
2064
Cong Wange4c7ed42013-08-31 13:44:33 +08002065 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
2066#if IS_ENABLED(CONFIG_IPV6)
2067 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +08002068 struct dst_entry *ndst;
2069 struct flowi6 fl6;
2070 u32 flags;
2071
2072 memset(&fl6, 0, sizeof(fl6));
Thomas Grafee122c72015-07-21 10:43:58 +02002073 fl6.flowi6_oif = rdst ? rdst->remote_ifindex : 0;
Cong Wange4c7ed42013-08-31 13:44:33 +08002074 fl6.daddr = dst->sin6.sin6_addr;
2075 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Thomas Graf239fb792015-05-05 15:09:21 +02002076 fl6.flowi6_mark = skb->mark;
Cong Wang8c1bb792013-09-02 10:06:51 +08002077 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08002078
2079 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2080 netdev_dbg(dev, "no route to %pI6\n",
2081 &dst->sin6.sin6_addr);
2082 dev->stats.tx_carrier_errors++;
2083 goto tx_error;
2084 }
2085
2086 if (ndst->dev == dev) {
2087 netdev_dbg(dev, "circular route to %pI6\n",
2088 &dst->sin6.sin6_addr);
2089 dst_release(ndst);
2090 dev->stats.collisions++;
2091 goto tx_error;
2092 }
2093
2094 /* Bypass encapsulation if the destination is local */
2095 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2096 if (flags & RTF_LOCAL &&
2097 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2098 struct vxlan_dev *dst_vxlan;
2099
2100 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002101 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002102 dst->sa.sa_family, dst_port,
2103 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002104 if (!dst_vxlan)
2105 goto tx_error;
2106 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2107 return;
2108 }
2109
2110 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Thomas Grafee122c72015-07-21 10:43:58 +02002111 md->vni = htonl(vni << 8);
2112 md->gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08002113
David Miller79b16aa2015-04-05 22:19:09 -04002114 err = vxlan6_xmit_skb(ndst, sk, skb, dev, &fl6.saddr, &fl6.daddr,
Thomas Grafee122c72015-07-21 10:43:58 +02002115 0, ttl, src_port, dst_port, md,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002116 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2117 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002118#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002119 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002120
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002121 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002122
2123drop:
2124 dev->stats.tx_dropped++;
2125 goto tx_free;
2126
Pravin B Shelar49560532013-08-19 11:23:17 -07002127rt_tx_error:
2128 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002129tx_error:
2130 dev->stats.tx_errors++;
2131tx_free:
2132 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002133}
2134
David Stevens66817122013-03-15 04:35:51 +00002135/* Transmit local packets over Vxlan
2136 *
2137 * Outer IP header inherits ECN and DF from inner header.
2138 * Outer UDP destination is the VXLAN assigned port.
2139 * source port is based on hash of flow
2140 */
2141static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2142{
2143 struct vxlan_dev *vxlan = netdev_priv(dev);
Thomas Grafee122c72015-07-21 10:43:58 +02002144 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
David Stevens66817122013-03-15 04:35:51 +00002145 struct ethhdr *eth;
2146 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002147 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002148 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002149
2150 skb_reset_mac_header(skb);
2151 eth = eth_hdr(skb);
2152
Cong Wangf564f452013-08-31 13:44:36 +08002153 if ((vxlan->flags & VXLAN_F_PROXY)) {
2154 if (ntohs(eth->h_proto) == ETH_P_ARP)
2155 return arp_reduce(dev, skb);
2156#if IS_ENABLED(CONFIG_IPV6)
2157 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002158 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2159 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002160 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2161 struct nd_msg *msg;
2162
2163 msg = (struct nd_msg *)skb_transport_header(skb);
2164 if (msg->icmph.icmp6_code == 0 &&
2165 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2166 return neigh_reduce(dev, skb);
2167 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002168 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002169#endif
2170 }
David Stevens66817122013-03-15 04:35:51 +00002171
Thomas Grafee122c72015-07-21 10:43:58 +02002172 if (vxlan->flags & VXLAN_F_FLOW_BASED &&
2173 info && info->mode == IP_TUNNEL_INFO_TX) {
2174 vxlan_xmit_one(skb, dev, NULL, false);
2175 return NETDEV_TX_OK;
2176 }
2177
David Stevens66817122013-03-15 04:35:51 +00002178 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002179 did_rsc = false;
2180
2181 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002182 (ntohs(eth->h_proto) == ETH_P_IP ||
2183 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002184 did_rsc = route_shortcircuit(dev, skb);
2185 if (did_rsc)
2186 f = vxlan_find_mac(vxlan, eth->h_dest);
2187 }
2188
David Stevens66817122013-03-15 04:35:51 +00002189 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002190 f = vxlan_find_mac(vxlan, all_zeros_mac);
2191 if (f == NULL) {
2192 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2193 !is_multicast_ether_addr(eth->h_dest))
2194 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002195
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002196 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002197 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002198 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002199 }
David Stevens66817122013-03-15 04:35:51 +00002200 }
2201
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002202 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2203 struct sk_buff *skb1;
2204
Eric Dumazet8f646c92014-01-06 09:54:31 -08002205 if (!fdst) {
2206 fdst = rdst;
2207 continue;
2208 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002209 skb1 = skb_clone(skb, GFP_ATOMIC);
2210 if (skb1)
2211 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2212 }
2213
Eric Dumazet8f646c92014-01-06 09:54:31 -08002214 if (fdst)
2215 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2216 else
2217 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002218 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002219}
2220
stephen hemmingerd3428942012-10-01 12:32:35 +00002221/* Walk the forwarding table and purge stale entries */
2222static void vxlan_cleanup(unsigned long arg)
2223{
2224 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2225 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2226 unsigned int h;
2227
2228 if (!netif_running(vxlan->dev))
2229 return;
2230
stephen hemmingerd3428942012-10-01 12:32:35 +00002231 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2232 struct hlist_node *p, *n;
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002233
2234 spin_lock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002235 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2236 struct vxlan_fdb *f
2237 = container_of(p, struct vxlan_fdb, hlist);
2238 unsigned long timeout;
2239
stephen hemminger3c172862012-10-26 06:24:34 +00002240 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002241 continue;
2242
2243 timeout = f->used + vxlan->age_interval * HZ;
2244 if (time_before_eq(timeout, jiffies)) {
2245 netdev_dbg(vxlan->dev,
2246 "garbage collect %pM\n",
2247 f->eth_addr);
2248 f->state = NUD_STALE;
2249 vxlan_fdb_destroy(vxlan, f);
2250 } else if (time_before(timeout, next_timer))
2251 next_timer = timeout;
2252 }
Sorin Dumitru14e1d0f2015-05-26 10:42:04 +03002253 spin_unlock_bh(&vxlan->hash_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002254 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002255
2256 mod_timer(&vxlan->age_timer, next_timer);
2257}
2258
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002259static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2260{
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002261 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002262 __u32 vni = vxlan->default_dst.remote_vni;
2263
2264 vxlan->vn_sock = vs;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002265 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002266 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002267 spin_unlock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002268}
2269
stephen hemmingerd3428942012-10-01 12:32:35 +00002270/* Setup stats when device is created */
2271static int vxlan_init(struct net_device *dev)
2272{
WANG Cong1c213bd2014-02-13 11:46:28 -08002273 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002274 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002275 return -ENOMEM;
2276
2277 return 0;
2278}
2279
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002280static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002281{
2282 struct vxlan_fdb *f;
2283
2284 spin_lock_bh(&vxlan->hash_lock);
2285 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2286 if (f)
2287 vxlan_fdb_destroy(vxlan, f);
2288 spin_unlock_bh(&vxlan->hash_lock);
2289}
2290
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002291static void vxlan_uninit(struct net_device *dev)
2292{
2293 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002294
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002295 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002296
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002297 free_percpu(dev->tstats);
2298}
2299
stephen hemmingerd3428942012-10-01 12:32:35 +00002300/* Start ageing timer and join group when device is brought up */
2301static int vxlan_open(struct net_device *dev)
2302{
2303 struct vxlan_dev *vxlan = netdev_priv(dev);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002304 struct vxlan_sock *vs;
2305 int ret = 0;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002306
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002307 vs = vxlan_sock_add(vxlan->net, vxlan->dst_port, vxlan_rcv, NULL,
2308 false, vxlan->flags);
2309 if (IS_ERR(vs))
2310 return PTR_ERR(vs);
2311
2312 vxlan_vs_add_dev(vs, vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002313
Gao feng79d4a942013-12-10 16:37:32 +08002314 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002315 ret = vxlan_igmp_join(vxlan);
2316 if (ret) {
2317 vxlan_sock_release(vs);
2318 return ret;
2319 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002320 }
2321
2322 if (vxlan->age_interval)
2323 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2324
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002325 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002326}
2327
2328/* Purge the forwarding table */
2329static void vxlan_flush(struct vxlan_dev *vxlan)
2330{
Cong Wang31fec5a2013-05-27 22:35:52 +00002331 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002332
2333 spin_lock_bh(&vxlan->hash_lock);
2334 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2335 struct hlist_node *p, *n;
2336 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2337 struct vxlan_fdb *f
2338 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002339 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2340 if (!is_zero_ether_addr(f->eth_addr))
2341 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002342 }
2343 }
2344 spin_unlock_bh(&vxlan->hash_lock);
2345}
2346
2347/* Cleanup timer and forwarding table on shutdown */
2348static int vxlan_stop(struct net_device *dev)
2349{
2350 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002351 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002352 struct vxlan_sock *vs = vxlan->vn_sock;
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002353 int ret = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002354
Marcelo Ricardo Leitner24c0e682015-03-23 16:23:12 -03002355 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
WANG Congf13b1682015-04-08 14:48:30 -07002356 !vxlan_group_used(vn, vxlan))
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002357 ret = vxlan_igmp_leave(vxlan);
stephen hemmingerd3428942012-10-01 12:32:35 +00002358
2359 del_timer_sync(&vxlan->age_timer);
2360
2361 vxlan_flush(vxlan);
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002362 vxlan_sock_release(vs);
stephen hemmingerd3428942012-10-01 12:32:35 +00002363
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002364 return ret;
stephen hemmingerd3428942012-10-01 12:32:35 +00002365}
2366
stephen hemmingerd3428942012-10-01 12:32:35 +00002367/* Stub, nothing needs to be done. */
2368static void vxlan_set_multicast_list(struct net_device *dev)
2369{
2370}
2371
Daniel Borkmann345010b2013-12-18 00:21:08 +01002372static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2373{
2374 struct vxlan_dev *vxlan = netdev_priv(dev);
2375 struct vxlan_rdst *dst = &vxlan->default_dst;
2376 struct net_device *lowerdev;
2377 int max_mtu;
2378
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002379 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002380 if (lowerdev == NULL)
2381 return eth_change_mtu(dev, new_mtu);
2382
2383 if (dst->remote_ip.sa.sa_family == AF_INET6)
2384 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2385 else
2386 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2387
2388 if (new_mtu < 68 || new_mtu > max_mtu)
2389 return -EINVAL;
2390
2391 dev->mtu = new_mtu;
2392 return 0;
2393}
2394
stephen hemmingerd3428942012-10-01 12:32:35 +00002395static const struct net_device_ops vxlan_netdev_ops = {
2396 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002397 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002398 .ndo_open = vxlan_open,
2399 .ndo_stop = vxlan_stop,
2400 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002401 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002402 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002403 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002404 .ndo_validate_addr = eth_validate_addr,
2405 .ndo_set_mac_address = eth_mac_addr,
2406 .ndo_fdb_add = vxlan_fdb_add,
2407 .ndo_fdb_del = vxlan_fdb_delete,
2408 .ndo_fdb_dump = vxlan_fdb_dump,
2409};
2410
2411/* Info for udev, that this is a virtual tunnel endpoint */
2412static struct device_type vxlan_type = {
2413 .name = "vxlan",
2414};
2415
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002416/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002417 * supply the listening VXLAN udp ports. Callers are expected
2418 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002419 */
2420void vxlan_get_rx_port(struct net_device *dev)
2421{
2422 struct vxlan_sock *vs;
2423 struct net *net = dev_net(dev);
2424 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2425 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002426 __be16 port;
2427 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002428
2429 spin_lock(&vn->sock_lock);
2430 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002431 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2432 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002433 sa_family = vs->sock->sk->sk_family;
2434 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2435 port);
2436 }
2437 }
2438 spin_unlock(&vn->sock_lock);
2439}
2440EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2441
stephen hemmingerd3428942012-10-01 12:32:35 +00002442/* Initialize the device structure. */
2443static void vxlan_setup(struct net_device *dev)
2444{
2445 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002446 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002447
2448 eth_hw_addr_random(dev);
2449 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002450 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002451 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002452 else
Cong Wang2853af62014-06-12 11:53:10 -07002453 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002454
2455 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002456 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002457 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2458
2459 dev->tx_queue_len = 0;
2460 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002461 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002462 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002463 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002464
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002465 dev->vlan_features = dev->features;
2466 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002467 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002468 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002469 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002470 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002471 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002472
stephen hemminger553675f2013-05-16 11:35:20 +00002473 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002474 spin_lock_init(&vxlan->hash_lock);
2475
2476 init_timer_deferrable(&vxlan->age_timer);
2477 vxlan->age_timer.function = vxlan_cleanup;
2478 vxlan->age_timer.data = (unsigned long) vxlan;
2479
stephen hemminger823aa872013-04-27 11:31:57 +00002480 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002481
stephen hemmingerd3428942012-10-01 12:32:35 +00002482 vxlan->dev = dev;
2483
2484 for (h = 0; h < FDB_HASH_SIZE; ++h)
2485 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2486}
2487
2488static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2489 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002490 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002491 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002492 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2493 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002494 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002495 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2496 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2497 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2498 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2499 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002500 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002501 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2502 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2503 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2504 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
Thomas Grafee122c72015-07-21 10:43:58 +02002505 [IFLA_VXLAN_FLOWBASED] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002506 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002507 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2508 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2509 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002510 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2511 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002512 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002513 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
stephen hemmingerd3428942012-10-01 12:32:35 +00002514};
2515
2516static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2517{
2518 if (tb[IFLA_ADDRESS]) {
2519 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2520 pr_debug("invalid link address (not ethernet)\n");
2521 return -EINVAL;
2522 }
2523
2524 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2525 pr_debug("invalid all zero ethernet address\n");
2526 return -EADDRNOTAVAIL;
2527 }
2528 }
2529
2530 if (!data)
2531 return -EINVAL;
2532
2533 if (data[IFLA_VXLAN_ID]) {
2534 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2535 if (id >= VXLAN_VID_MASK)
2536 return -ERANGE;
2537 }
2538
stephen hemminger05f47d62012-10-09 20:35:50 +00002539 if (data[IFLA_VXLAN_PORT_RANGE]) {
2540 const struct ifla_vxlan_port_range *p
2541 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2542
2543 if (ntohs(p->high) < ntohs(p->low)) {
2544 pr_debug("port range %u .. %u not valid\n",
2545 ntohs(p->low), ntohs(p->high));
2546 return -EINVAL;
2547 }
2548 }
2549
stephen hemmingerd3428942012-10-01 12:32:35 +00002550 return 0;
2551}
2552
Yan Burman1b13c972013-01-29 23:43:07 +00002553static void vxlan_get_drvinfo(struct net_device *netdev,
2554 struct ethtool_drvinfo *drvinfo)
2555{
2556 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2557 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2558}
2559
2560static const struct ethtool_ops vxlan_ethtool_ops = {
2561 .get_drvinfo = vxlan_get_drvinfo,
2562 .get_link = ethtool_op_get_link,
2563};
2564
stephen hemminger553675f2013-05-16 11:35:20 +00002565static void vxlan_del_work(struct work_struct *work)
2566{
2567 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002568 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002569 kfree_rcu(vs, rcu);
2570}
2571
Tom Herbert3ee64f32014-07-13 19:49:42 -07002572static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2573 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002574{
Cong Wange4c7ed42013-08-31 13:44:33 +08002575 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002576 struct udp_port_cfg udp_conf;
2577 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002578
Tom Herbert3ee64f32014-07-13 19:49:42 -07002579 memset(&udp_conf, 0, sizeof(udp_conf));
2580
2581 if (ipv6) {
2582 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002583 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002584 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002585 } else {
2586 udp_conf.family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002587 }
2588
Tom Herbert3ee64f32014-07-13 19:49:42 -07002589 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002590
Tom Herbert3ee64f32014-07-13 19:49:42 -07002591 /* Open UDP socket */
2592 err = udp_sock_create(net, &udp_conf, &sock);
2593 if (err < 0)
2594 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002595
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002596 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002597}
2598
2599/* Create new listen socket if needed */
2600static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002601 vxlan_rcv_t *rcv, void *data,
2602 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002603{
2604 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2605 struct vxlan_sock *vs;
2606 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002607 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002608 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002609 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002610
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002611 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002612 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002613 return ERR_PTR(-ENOMEM);
2614
2615 for (h = 0; h < VNI_HASH_SIZE; ++h)
2616 INIT_HLIST_HEAD(&vs->vni_list[h]);
2617
2618 INIT_WORK(&vs->del_work, vxlan_del_work);
2619
Tom Herbert3ee64f32014-07-13 19:49:42 -07002620 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002621 if (IS_ERR(sock)) {
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002622 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2623 PTR_ERR(sock));
stephen hemminger553675f2013-05-16 11:35:20 +00002624 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002625 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002626 }
2627
Cong Wange4c7ed42013-08-31 13:44:33 +08002628 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002629 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002630 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002631 vs->data = data;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002632 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002633
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002634 /* Initialize the vxlan udp offloads structure */
2635 vs->udp_offloads.port = port;
2636 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2637 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2638
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002639 spin_lock(&vn->sock_lock);
2640 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002641 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002642 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002643
2644 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002645 tunnel_cfg.sk_user_data = vs;
2646 tunnel_cfg.encap_type = 1;
2647 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2648 tunnel_cfg.encap_destroy = NULL;
2649
2650 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002651
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002652 return vs;
2653}
stephen hemminger553675f2013-05-16 11:35:20 +00002654
Pravin B Shelar012a5722013-08-19 11:23:07 -07002655struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2656 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002657 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002658{
2659 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2660 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002661 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002662
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002663 if (!no_share) {
2664 spin_lock(&vn->sock_lock);
2665 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port,
2666 flags);
2667 if (vs && vs->rcv == rcv) {
2668 if (!atomic_add_unless(&vs->refcnt, 1, 0))
2669 vs = ERR_PTR(-EBUSY);
2670 spin_unlock(&vn->sock_lock);
2671 return vs;
2672 }
2673 spin_unlock(&vn->sock_lock);
2674 }
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002675
Marcelo Ricardo Leitner56ef9c92015-03-18 14:50:44 -03002676 return vxlan_socket_create(net, port, rcv, data, flags);
stephen hemminger553675f2013-05-16 11:35:20 +00002677}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002678EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002679
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002680static int vxlan_newlink(struct net *src_net, struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +00002681 struct nlattr *tb[], struct nlattr *data[])
2682{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002683 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002684 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002685 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002686 __u32 vni;
2687 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002688 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002689
2690 if (!data[IFLA_VXLAN_ID])
2691 return -EINVAL;
2692
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002693 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002694
stephen hemmingerd3428942012-10-01 12:32:35 +00002695 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002696 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002697
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002698 /* Unless IPv6 is explicitly requested, assume IPv4 */
2699 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002700 if (data[IFLA_VXLAN_GROUP]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002701 dst->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002702 } else if (data[IFLA_VXLAN_GROUP6]) {
2703 if (!IS_ENABLED(CONFIG_IPV6))
2704 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002705
Jiri Benc67b61f62015-03-29 16:59:26 +02002706 dst->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002707 dst->remote_ip.sa.sa_family = AF_INET6;
2708 use_ipv6 = true;
2709 }
2710
2711 if (data[IFLA_VXLAN_LOCAL]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02002712 vxlan->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002713 vxlan->saddr.sa.sa_family = AF_INET;
2714 } else if (data[IFLA_VXLAN_LOCAL6]) {
2715 if (!IS_ENABLED(CONFIG_IPV6))
2716 return -EPFNOSUPPORT;
2717
2718 /* TODO: respect scope id */
Jiri Benc67b61f62015-03-29 16:59:26 +02002719 vxlan->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002720 vxlan->saddr.sa.sa_family = AF_INET6;
2721 use_ipv6 = true;
2722 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002723
stephen hemminger34e02aa2012-10-09 20:35:53 +00002724 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002725 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002726 struct net_device *lowerdev
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002727 = __dev_get_by_index(src_net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002728
stephen hemminger34e02aa2012-10-09 20:35:53 +00002729 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002730 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002731 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002732 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002733
Cong Wange4c7ed42013-08-31 13:44:33 +08002734#if IS_ENABLED(CONFIG_IPV6)
2735 if (use_ipv6) {
2736 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2737 if (idev && idev->cnf.disable_ipv6) {
2738 pr_info("IPv6 is disabled via sysctl\n");
2739 return -EPERM;
2740 }
2741 vxlan->flags |= VXLAN_F_IPV6;
2742 }
2743#endif
2744
stephen hemminger34e02aa2012-10-09 20:35:53 +00002745 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002746 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002747
Cong Wang2853af62014-06-12 11:53:10 -07002748 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002749 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002750 } else if (use_ipv6)
2751 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002752
2753 if (data[IFLA_VXLAN_TOS])
2754 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2755
Vincent Bernatafb97182012-10-30 10:27:16 +00002756 if (data[IFLA_VXLAN_TTL])
2757 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2758
stephen hemmingerd3428942012-10-01 12:32:35 +00002759 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002760 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002761
2762 if (data[IFLA_VXLAN_AGEING])
2763 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2764 else
2765 vxlan->age_interval = FDB_AGE_DEFAULT;
2766
David Stevense4f67ad2012-11-20 02:50:14 +00002767 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2768 vxlan->flags |= VXLAN_F_PROXY;
2769
2770 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2771 vxlan->flags |= VXLAN_F_RSC;
2772
2773 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2774 vxlan->flags |= VXLAN_F_L2MISS;
2775
2776 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2777 vxlan->flags |= VXLAN_F_L3MISS;
2778
stephen hemmingerd3428942012-10-01 12:32:35 +00002779 if (data[IFLA_VXLAN_LIMIT])
2780 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2781
Thomas Grafee122c72015-07-21 10:43:58 +02002782 if (data[IFLA_VXLAN_FLOWBASED] &&
2783 nla_get_u8(data[IFLA_VXLAN_FLOWBASED]))
2784 vxlan->flags |= VXLAN_F_FLOW_BASED;
2785
stephen hemminger05f47d62012-10-09 20:35:50 +00002786 if (data[IFLA_VXLAN_PORT_RANGE]) {
2787 const struct ifla_vxlan_port_range *p
2788 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2789 vxlan->port_min = ntohs(p->low);
2790 vxlan->port_max = ntohs(p->high);
2791 }
2792
stephen hemminger823aa872013-04-27 11:31:57 +00002793 if (data[IFLA_VXLAN_PORT])
2794 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2795
Tom Herbert359a0ea2014-06-04 17:20:29 -07002796 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2797 vxlan->flags |= VXLAN_F_UDP_CSUM;
2798
2799 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2800 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2801 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2802
2803 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2804 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2805 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2806
Tom Herbertdfd86452015-01-12 17:00:38 -08002807 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2808 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2809 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2810
2811 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2812 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2813 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2814
Thomas Graf35114942015-01-15 03:53:55 +01002815 if (data[IFLA_VXLAN_GBP])
2816 vxlan->flags |= VXLAN_F_GBP;
2817
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002818 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
2819 vxlan->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
2820
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002821 if (vxlan_find_vni(src_net, vni, use_ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002822 vxlan->dst_port, vxlan->flags)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002823 pr_info("duplicate VNI %u\n", vni);
2824 return -EEXIST;
2825 }
2826
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002827 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002828
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002829 /* create an fdb entry for a valid default destination */
2830 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2831 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2832 &vxlan->default_dst.remote_ip,
2833 NUD_REACHABLE|NUD_PERMANENT,
2834 NLM_F_EXCL|NLM_F_CREATE,
2835 vxlan->dst_port,
2836 vxlan->default_dst.remote_vni,
2837 vxlan->default_dst.remote_ifindex,
2838 NTF_SELF);
2839 if (err)
2840 return err;
2841 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002842
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002843 err = register_netdevice(dev);
2844 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002845 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002846 return err;
2847 }
2848
stephen hemminger553675f2013-05-16 11:35:20 +00002849 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002850
2851 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002852}
2853
2854static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2855{
2856 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002857 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002858
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002859 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002860 if (!hlist_unhashed(&vxlan->hlist))
2861 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002862 spin_unlock(&vn->sock_lock);
2863
stephen hemminger553675f2013-05-16 11:35:20 +00002864 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002865 unregister_netdevice_queue(dev, head);
2866}
2867
2868static size_t vxlan_get_size(const struct net_device *dev)
2869{
2870
2871 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002872 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002873 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002874 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002875 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2876 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2877 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002878 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2879 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2880 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2881 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
Thomas Grafee122c72015-07-21 10:43:58 +02002882 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_FLOWBASED */
stephen hemmingerd3428942012-10-01 12:32:35 +00002883 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2884 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002885 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002886 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2887 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2888 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2889 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002890 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2891 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002892 0;
2893}
2894
2895static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2896{
2897 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002898 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002899 struct ifla_vxlan_port_range ports = {
2900 .low = htons(vxlan->port_min),
2901 .high = htons(vxlan->port_max),
2902 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002903
Atzm Watanabec7995c42013-04-16 02:50:52 +00002904 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002905 goto nla_put_failure;
2906
Cong Wange4c7ed42013-08-31 13:44:33 +08002907 if (!vxlan_addr_any(&dst->remote_ip)) {
2908 if (dst->remote_ip.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02002909 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
2910 dst->remote_ip.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002911 goto nla_put_failure;
2912#if IS_ENABLED(CONFIG_IPV6)
2913 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02002914 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
2915 &dst->remote_ip.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002916 goto nla_put_failure;
2917#endif
2918 }
2919 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002920
Atzm Watanabec7995c42013-04-16 02:50:52 +00002921 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002922 goto nla_put_failure;
2923
Cong Wange4c7ed42013-08-31 13:44:33 +08002924 if (!vxlan_addr_any(&vxlan->saddr)) {
2925 if (vxlan->saddr.sa.sa_family == AF_INET) {
Jiri Benc930345e2015-03-29 16:59:25 +02002926 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
2927 vxlan->saddr.sin.sin_addr.s_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002928 goto nla_put_failure;
2929#if IS_ENABLED(CONFIG_IPV6)
2930 } else {
Jiri Benc930345e2015-03-29 16:59:25 +02002931 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
2932 &vxlan->saddr.sin6.sin6_addr))
Cong Wange4c7ed42013-08-31 13:44:33 +08002933 goto nla_put_failure;
2934#endif
2935 }
2936 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002937
2938 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2939 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002940 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2941 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2942 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2943 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2944 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2945 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2946 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2947 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2948 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
Thomas Grafee122c72015-07-21 10:43:58 +02002949 nla_put_u8(skb, IFLA_VXLAN_FLOWBASED,
2950 !!(vxlan->flags & VXLAN_F_FLOW_BASED)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002951 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002952 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002953 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2954 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2955 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2956 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2957 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2958 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002959 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2960 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2961 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2962 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2963 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002964 goto nla_put_failure;
2965
stephen hemminger05f47d62012-10-09 20:35:50 +00002966 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2967 goto nla_put_failure;
2968
Thomas Graf35114942015-01-15 03:53:55 +01002969 if (vxlan->flags & VXLAN_F_GBP &&
2970 nla_put_flag(skb, IFLA_VXLAN_GBP))
2971 goto nla_put_failure;
2972
Tom Herbert0ace2ca2015-02-10 16:30:32 -08002973 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
2974 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
2975 goto nla_put_failure;
2976
stephen hemmingerd3428942012-10-01 12:32:35 +00002977 return 0;
2978
2979nla_put_failure:
2980 return -EMSGSIZE;
2981}
2982
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002983static struct net *vxlan_get_link_net(const struct net_device *dev)
2984{
2985 struct vxlan_dev *vxlan = netdev_priv(dev);
2986
2987 return vxlan->net;
2988}
2989
stephen hemmingerd3428942012-10-01 12:32:35 +00002990static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2991 .kind = "vxlan",
2992 .maxtype = IFLA_VXLAN_MAX,
2993 .policy = vxlan_policy,
2994 .priv_size = sizeof(struct vxlan_dev),
2995 .setup = vxlan_setup,
2996 .validate = vxlan_validate,
2997 .newlink = vxlan_newlink,
2998 .dellink = vxlan_dellink,
2999 .get_size = vxlan_get_size,
3000 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01003001 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003002};
3003
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003004static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3005 struct net_device *dev)
3006{
3007 struct vxlan_dev *vxlan, *next;
3008 LIST_HEAD(list_kill);
3009
3010 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3011 struct vxlan_rdst *dst = &vxlan->default_dst;
3012
3013 /* In case we created vxlan device with carrier
3014 * and we loose the carrier due to module unload
3015 * we also need to remove vxlan device. In other
3016 * cases, it's not necessary and remote_ifindex
3017 * is 0 here, so no matches.
3018 */
3019 if (dst->remote_ifindex == dev->ifindex)
3020 vxlan_dellink(vxlan->dev, &list_kill);
3021 }
3022
3023 unregister_netdevice_many(&list_kill);
3024}
3025
3026static int vxlan_lowerdev_event(struct notifier_block *unused,
3027 unsigned long event, void *ptr)
3028{
3029 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003030 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003031
Daniel Borkmann783c1462014-01-22 21:07:53 +01003032 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003033 vxlan_handle_lowerdev_unregister(vn, dev);
3034
3035 return NOTIFY_DONE;
3036}
3037
3038static struct notifier_block vxlan_notifier_block __read_mostly = {
3039 .notifier_call = vxlan_lowerdev_event,
3040};
3041
stephen hemmingerd3428942012-10-01 12:32:35 +00003042static __net_init int vxlan_init_net(struct net *net)
3043{
3044 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00003045 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00003046
stephen hemminger553675f2013-05-16 11:35:20 +00003047 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07003048 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00003049
stephen hemminger553675f2013-05-16 11:35:20 +00003050 for (h = 0; h < PORT_HASH_SIZE; ++h)
3051 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00003052
3053 return 0;
3054}
3055
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003056static void __net_exit vxlan_exit_net(struct net *net)
3057{
3058 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3059 struct vxlan_dev *vxlan, *next;
3060 struct net_device *dev, *aux;
3061 LIST_HEAD(list);
3062
3063 rtnl_lock();
3064 for_each_netdev_safe(net, dev, aux)
3065 if (dev->rtnl_link_ops == &vxlan_link_ops)
3066 unregister_netdevice_queue(dev, &list);
3067
3068 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3069 /* If vxlan->dev is in the same netns, it has already been added
3070 * to the list by the previous loop.
3071 */
3072 if (!net_eq(dev_net(vxlan->dev), net))
John W. Linville13c3ed62015-05-18 13:51:24 -04003073 unregister_netdevice_queue(vxlan->dev, &list);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003074 }
3075
3076 unregister_netdevice_many(&list);
3077 rtnl_unlock();
3078}
3079
stephen hemmingerd3428942012-10-01 12:32:35 +00003080static struct pernet_operations vxlan_net_ops = {
3081 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003082 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003083 .id = &vxlan_net_id,
3084 .size = sizeof(struct vxlan_net),
3085};
3086
3087static int __init vxlan_init_module(void)
3088{
3089 int rc;
3090
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003091 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3092 if (!vxlan_wq)
3093 return -ENOMEM;
3094
stephen hemmingerd3428942012-10-01 12:32:35 +00003095 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3096
Daniel Borkmann783c1462014-01-22 21:07:53 +01003097 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003098 if (rc)
3099 goto out1;
3100
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003101 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003102 if (rc)
3103 goto out2;
3104
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003105 rc = rtnl_link_register(&vxlan_link_ops);
3106 if (rc)
3107 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003108
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003109 return 0;
3110out3:
3111 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003112out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003113 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003114out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003115 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00003116 return rc;
3117}
Cong Wang7332a132013-05-27 22:35:53 +00003118late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003119
3120static void __exit vxlan_cleanup_module(void)
3121{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003122 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003123 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003124 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003125 unregister_pernet_subsys(&vxlan_net_ops);
3126 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003127}
3128module_exit(vxlan_cleanup_module);
3129
3130MODULE_LICENSE("GPL");
3131MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003132MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003133MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003134MODULE_ALIAS_RTNL_LINK("vxlan");