blob: 31ecb03368c6dc3d581fdbd30b409b88190f3c71 [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
stephen hemmingerd3428942012-10-01 12:32:35 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/rculist.h>
20#include <linux/netdevice.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/udp.h>
24#include <linux/igmp.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
Pravin B Shelar1eaa8172013-08-19 11:23:29 -070027#include <linux/if_vlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000028#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000029#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000030#include <net/arp.h>
31#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000032#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000033#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/icmp.h>
35#include <net/udp.h>
Tom Herbert3ee64f32014-07-13 19:49:42 -070036#include <net/udp_tunnel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000037#include <net/rtnetlink.h>
38#include <net/route.h>
39#include <net/dsfield.h>
40#include <net/inet_ecn.h>
41#include <net/net_namespace.h>
42#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070043#include <net/vxlan.h>
Or Gerlitzdc01e7d2014-01-20 13:59:21 +020044#include <net/protocol.h>
Andy Zhouacbf74a2014-09-16 17:31:18 -070045#include <net/udp_tunnel.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080046#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ipv6.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080050#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080051#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000052
53#define VXLAN_VERSION "0.1"
54
stephen hemminger553675f2013-05-16 11:35:20 +000055#define PORT_HASH_BITS 8
56#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000057#define VNI_HASH_BITS 10
58#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
59#define FDB_HASH_BITS 8
60#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
61#define FDB_AGE_DEFAULT 300 /* 5 min */
62#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
63
64#define VXLAN_N_VID (1u << 24)
65#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Pravin B Shelar7ce04752013-08-19 11:22:54 -070066#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
stephen hemmingerd3428942012-10-01 12:32:35 +000067
68#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
69
stephen hemminger23c578b2013-04-27 11:31:53 +000070/* UDP port for VXLAN traffic.
71 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070072 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000073 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070074static unsigned short vxlan_port __read_mostly = 8472;
75module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000076MODULE_PARM_DESC(udp_port, "Destination UDP port");
77
78static bool log_ecn_error = true;
79module_param(log_ecn_error, bool, 0644);
80MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
81
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070082static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000083
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030084static const u8 all_zeros_mac[ETH_ALEN];
85
stephen hemminger553675f2013-05-16 11:35:20 +000086/* per-network namespace private data for this module */
87struct vxlan_net {
88 struct list_head vxlan_list;
89 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070090 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000091};
92
Cong Wange4c7ed42013-08-31 13:44:33 +080093union vxlan_addr {
94 struct sockaddr_in sin;
95 struct sockaddr_in6 sin6;
96 struct sockaddr sa;
97};
98
David Stevens66817122013-03-15 04:35:51 +000099struct vxlan_rdst {
Cong Wange4c7ed42013-08-31 13:44:33 +0800100 union vxlan_addr remote_ip;
David Stevens66817122013-03-15 04:35:51 +0000101 __be16 remote_port;
102 u32 remote_vni;
103 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700104 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300105 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000106};
107
stephen hemmingerd3428942012-10-01 12:32:35 +0000108/* Forwarding table entry */
109struct vxlan_fdb {
110 struct hlist_node hlist; /* linked list of entries */
111 struct rcu_head rcu;
112 unsigned long updated; /* jiffies */
113 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700114 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000115 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000116 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000117 u8 eth_addr[ETH_ALEN];
118};
119
stephen hemmingerd3428942012-10-01 12:32:35 +0000120/* Pseudo network device */
121struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000122 struct hlist_node hlist; /* vni hash table */
123 struct list_head next; /* vxlan's per namespace list */
124 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000125 struct net_device *dev;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +0200126 struct net *net; /* netns for packet i/o */
Atzm Watanabec7995c42013-04-16 02:50:52 +0000127 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800128 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000129 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000130 __u16 port_min; /* source port range */
131 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000132 __u8 tos; /* TOS override */
133 __u8 ttl;
Tom Herbert359a0ea2014-06-04 17:20:29 -0700134 u32 flags; /* VXLAN_F_* in vxlan.h */
stephen hemmingerd3428942012-10-01 12:32:35 +0000135
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700136 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700137 struct work_struct igmp_join;
138 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700139
stephen hemmingerd3428942012-10-01 12:32:35 +0000140 unsigned long age_interval;
141 struct timer_list age_timer;
142 spinlock_t hash_lock;
143 unsigned int addrcnt;
144 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000145
146 struct hlist_head fdb_head[FDB_HASH_SIZE];
147};
148
149/* salt for hash table */
150static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700151static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000152
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700153static void vxlan_sock_work(struct work_struct *work);
154
Cong Wange4c7ed42013-08-31 13:44:33 +0800155#if IS_ENABLED(CONFIG_IPV6)
156static inline
157bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
158{
159 if (a->sa.sa_family != b->sa.sa_family)
160 return false;
161 if (a->sa.sa_family == AF_INET6)
162 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
163 else
164 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
165}
166
167static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
168{
169 if (ipa->sa.sa_family == AF_INET6)
170 return ipv6_addr_any(&ipa->sin6.sin6_addr);
171 else
172 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
173}
174
175static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
176{
177 if (ipa->sa.sa_family == AF_INET6)
178 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
179 else
180 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
181}
182
183static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
184{
185 if (nla_len(nla) >= sizeof(struct in6_addr)) {
186 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
187 ip->sa.sa_family = AF_INET6;
188 return 0;
189 } else if (nla_len(nla) >= sizeof(__be32)) {
190 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
191 ip->sa.sa_family = AF_INET;
192 return 0;
193 } else {
194 return -EAFNOSUPPORT;
195 }
196}
197
198static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
199 const union vxlan_addr *ip)
200{
201 if (ip->sa.sa_family == AF_INET6)
202 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
203 else
204 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
205}
206
207#else /* !CONFIG_IPV6 */
208
209static inline
210bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
211{
212 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
213}
214
215static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
216{
217 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
218}
219
220static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
221{
222 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
223}
224
225static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
226{
227 if (nla_len(nla) >= sizeof(struct in6_addr)) {
228 return -EAFNOSUPPORT;
229 } else if (nla_len(nla) >= sizeof(__be32)) {
230 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
231 ip->sa.sa_family = AF_INET;
232 return 0;
233 } else {
234 return -EAFNOSUPPORT;
235 }
236}
237
238static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
239 const union vxlan_addr *ip)
240{
241 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
242}
243#endif
244
stephen hemminger553675f2013-05-16 11:35:20 +0000245/* Virtual Network hash table head */
246static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
247{
248 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
249}
250
251/* Socket hash table head */
252static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000253{
254 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
255
stephen hemminger553675f2013-05-16 11:35:20 +0000256 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
257}
258
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700259/* First remote destination for a forwarding entry.
260 * Guaranteed to be non-NULL because remotes are never deleted.
261 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700262static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700263{
stephen hemminger5ca54612013-08-04 17:17:39 -0700264 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
265}
266
267static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
268{
269 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700270}
271
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200272/* Find VXLAN socket based on network namespace, address family and UDP port */
273static struct vxlan_sock *vxlan_find_sock(struct net *net,
274 sa_family_t family, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000275{
276 struct vxlan_sock *vs;
277
278 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200279 if (inet_sk(vs->sock->sk)->inet_sport == port &&
280 inet_sk(vs->sock->sk)->sk.sk_family == family)
stephen hemminger553675f2013-05-16 11:35:20 +0000281 return vs;
282 }
283 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000284}
285
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700286static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000287{
288 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000289
stephen hemminger553675f2013-05-16 11:35:20 +0000290 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000291 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000292 return vxlan;
293 }
294
295 return NULL;
296}
297
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700298/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200299static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
300 sa_family_t family, __be16 port)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700301{
302 struct vxlan_sock *vs;
303
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200304 vs = vxlan_find_sock(net, family, port);
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
David Stevense4f67ad2012-11-20 02:50:14 +0000343 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000344 goto nla_put_failure;
345
Cong Wange4c7ed42013-08-31 13:44:33 +0800346 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000347 goto nla_put_failure;
348
stephen hemminger823aa872013-04-27 11:31:57 +0000349 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000350 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
351 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000352 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700353 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000354 goto nla_put_failure;
355 if (rdst->remote_ifindex &&
356 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000357 goto nla_put_failure;
358
359 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
360 ci.ndm_confirmed = 0;
361 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
362 ci.ndm_refcnt = 0;
363
364 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
365 goto nla_put_failure;
366
367 return nlmsg_end(skb, nlh);
368
369nla_put_failure:
370 nlmsg_cancel(skb, nlh);
371 return -EMSGSIZE;
372}
373
374static inline size_t vxlan_nlmsg_size(void)
375{
376 return NLMSG_ALIGN(sizeof(struct ndmsg))
377 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800378 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000379 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000380 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
381 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000382 + nla_total_size(sizeof(struct nda_cacheinfo));
383}
384
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200385static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
386 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000387{
388 struct net *net = dev_net(vxlan->dev);
389 struct sk_buff *skb;
390 int err = -ENOBUFS;
391
392 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
393 if (skb == NULL)
394 goto errout;
395
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200396 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000397 if (err < 0) {
398 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
399 WARN_ON(err == -EMSGSIZE);
400 kfree_skb(skb);
401 goto errout;
402 }
403
404 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
405 return;
406errout:
407 if (err < 0)
408 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
409}
410
Cong Wange4c7ed42013-08-31 13:44:33 +0800411static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000412{
413 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700414 struct vxlan_fdb f = {
415 .state = NUD_STALE,
416 };
417 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800418 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700419 .remote_vni = VXLAN_N_VID,
420 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700421
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200422 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000423}
424
425static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
426{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700427 struct vxlan_fdb f = {
428 .state = NUD_STALE,
429 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200430 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000431
David Stevense4f67ad2012-11-20 02:50:14 +0000432 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
433
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200434 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000435}
436
stephen hemmingerd3428942012-10-01 12:32:35 +0000437/* Hash Ethernet address */
438static u32 eth_hash(const unsigned char *addr)
439{
440 u64 value = get_unaligned((u64 *)addr);
441
442 /* only want 6 bytes */
443#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000444 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000445#else
446 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000447#endif
448 return hash_64(value, FDB_HASH_BITS);
449}
450
451/* Hash chain to use given mac address */
452static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
453 const u8 *mac)
454{
455 return &vxlan->fdb_head[eth_hash(mac)];
456}
457
458/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000459static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000460 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000461{
462 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
463 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000464
Sasha Levinb67bfe02013-02-27 17:06:00 -0800465 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700466 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000467 return f;
468 }
469
470 return NULL;
471}
472
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000473static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
474 const u8 *mac)
475{
476 struct vxlan_fdb *f;
477
478 f = __vxlan_find_mac(vxlan, mac);
479 if (f)
480 f->used = jiffies;
481
482 return f;
483}
484
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300485/* caller should hold vxlan->hash_lock */
486static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800487 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300488 __u32 vni, __u32 ifindex)
489{
490 struct vxlan_rdst *rd;
491
492 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800493 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300494 rd->remote_port == port &&
495 rd->remote_vni == vni &&
496 rd->remote_ifindex == ifindex)
497 return rd;
498 }
499
500 return NULL;
501}
502
Thomas Richter906dc182013-07-19 17:20:07 +0200503/* Replace destination of unicast mac */
504static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800505 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200506{
507 struct vxlan_rdst *rd;
508
509 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
510 if (rd)
511 return 0;
512
513 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
514 if (!rd)
515 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800516 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200517 rd->remote_port = port;
518 rd->remote_vni = vni;
519 rd->remote_ifindex = ifindex;
520 return 1;
521}
522
David Stevens66817122013-03-15 04:35:51 +0000523/* Add/update destinations for multicast */
524static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200525 union vxlan_addr *ip, __be16 port, __u32 vni,
526 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000527{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700528 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000529
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300530 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
531 if (rd)
532 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700533
David Stevens66817122013-03-15 04:35:51 +0000534 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
535 if (rd == NULL)
536 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800537 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000538 rd->remote_port = port;
539 rd->remote_vni = vni;
540 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700541
542 list_add_tail_rcu(&rd->list, &f->remotes);
543
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200544 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000545 return 1;
546}
547
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200548static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, struct sk_buff *skb)
549{
550 struct sk_buff *p, **pp = NULL;
551 struct vxlanhdr *vh, *vh2;
552 struct ethhdr *eh, *eh2;
553 unsigned int hlen, off_vx, off_eth;
554 const struct packet_offload *ptype;
555 __be16 type;
556 int flush = 1;
557
558 off_vx = skb_gro_offset(skb);
559 hlen = off_vx + sizeof(*vh);
560 vh = skb_gro_header_fast(skb, off_vx);
561 if (skb_gro_header_hard(skb, hlen)) {
562 vh = skb_gro_header_slow(skb, hlen, off_vx);
563 if (unlikely(!vh))
564 goto out;
565 }
566 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700567 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200568
569 off_eth = skb_gro_offset(skb);
570 hlen = off_eth + sizeof(*eh);
571 eh = skb_gro_header_fast(skb, off_eth);
572 if (skb_gro_header_hard(skb, hlen)) {
573 eh = skb_gro_header_slow(skb, hlen, off_eth);
574 if (unlikely(!eh))
575 goto out;
576 }
577
578 flush = 0;
579
580 for (p = *head; p; p = p->next) {
581 if (!NAPI_GRO_CB(p)->same_flow)
582 continue;
583
584 vh2 = (struct vxlanhdr *)(p->data + off_vx);
585 eh2 = (struct ethhdr *)(p->data + off_eth);
586 if (vh->vx_vni != vh2->vx_vni || compare_ether_header(eh, eh2)) {
587 NAPI_GRO_CB(p)->same_flow = 0;
588 continue;
589 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200590 }
591
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200592 type = eh->h_proto;
593
594 rcu_read_lock();
595 ptype = gro_find_receive_by_type(type);
596 if (ptype == NULL) {
597 flush = 1;
598 goto out_unlock;
599 }
600
601 skb_gro_pull(skb, sizeof(*eh)); /* pull inner eth header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700602 skb_gro_postpull_rcsum(skb, eh, sizeof(*eh));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200603 pp = ptype->callbacks.gro_receive(head, skb);
604
605out_unlock:
606 rcu_read_unlock();
607out:
608 NAPI_GRO_CB(skb)->flush |= flush;
609
610 return pp;
611}
612
613static int vxlan_gro_complete(struct sk_buff *skb, int nhoff)
614{
615 struct ethhdr *eh;
616 struct packet_offload *ptype;
617 __be16 type;
618 int vxlan_len = sizeof(struct vxlanhdr) + sizeof(struct ethhdr);
619 int err = -ENOSYS;
620
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800621 udp_tunnel_gro_complete(skb, nhoff);
622
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200623 eh = (struct ethhdr *)(skb->data + nhoff + sizeof(struct vxlanhdr));
624 type = eh->h_proto;
625
626 rcu_read_lock();
627 ptype = gro_find_complete_by_type(type);
628 if (ptype != NULL)
629 err = ptype->callbacks.gro_complete(skb, nhoff + vxlan_len);
630
631 rcu_read_unlock();
632 return err;
633}
634
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700635/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200636static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700637{
638 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200639 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700640 struct net *net = sock_net(sk);
641 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700642 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200643 int err;
644
645 if (sa_family == AF_INET) {
646 err = udp_add_offload(&vs->udp_offloads);
647 if (err)
648 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
649 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700650
651 rcu_read_lock();
652 for_each_netdev_rcu(net, dev) {
653 if (dev->netdev_ops->ndo_add_vxlan_port)
654 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
655 port);
656 }
657 rcu_read_unlock();
658}
659
660/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200661static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700662{
663 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200664 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700665 struct net *net = sock_net(sk);
666 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700667 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700668
669 rcu_read_lock();
670 for_each_netdev_rcu(net, dev) {
671 if (dev->netdev_ops->ndo_del_vxlan_port)
672 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
673 port);
674 }
675 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200676
677 if (sa_family == AF_INET)
678 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700679}
680
stephen hemmingerd3428942012-10-01 12:32:35 +0000681/* Add new entry to forwarding table -- assumes lock held */
682static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800683 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000684 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000685 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000686 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000687{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200688 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000689 struct vxlan_fdb *f;
690 int notify = 0;
691
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000692 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000693 if (f) {
694 if (flags & NLM_F_EXCL) {
695 netdev_dbg(vxlan->dev,
696 "lost race to create %pM\n", mac);
697 return -EEXIST;
698 }
699 if (f->state != state) {
700 f->state = state;
701 f->updated = jiffies;
702 notify = 1;
703 }
David Stevensae884082013-04-19 00:36:26 +0000704 if (f->flags != ndm_flags) {
705 f->flags = ndm_flags;
706 f->updated = jiffies;
707 notify = 1;
708 }
Thomas Richter906dc182013-07-19 17:20:07 +0200709 if ((flags & NLM_F_REPLACE)) {
710 /* Only change unicasts */
711 if (!(is_multicast_ether_addr(f->eth_addr) ||
712 is_zero_ether_addr(f->eth_addr))) {
713 int rc = vxlan_fdb_replace(f, ip, port, vni,
714 ifindex);
715
716 if (rc < 0)
717 return rc;
718 notify |= rc;
719 } else
720 return -EOPNOTSUPP;
721 }
David Stevens66817122013-03-15 04:35:51 +0000722 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300723 (is_multicast_ether_addr(f->eth_addr) ||
724 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200725 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
726 &rd);
David Stevens66817122013-03-15 04:35:51 +0000727
728 if (rc < 0)
729 return rc;
730 notify |= rc;
731 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000732 } else {
733 if (!(flags & NLM_F_CREATE))
734 return -ENOENT;
735
736 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
737 return -ENOSPC;
738
Thomas Richter906dc182013-07-19 17:20:07 +0200739 /* Disallow replace to add a multicast entry */
740 if ((flags & NLM_F_REPLACE) &&
741 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
742 return -EOPNOTSUPP;
743
Cong Wange4c7ed42013-08-31 13:44:33 +0800744 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000745 f = kmalloc(sizeof(*f), GFP_ATOMIC);
746 if (!f)
747 return -ENOMEM;
748
749 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000750 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000751 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000752 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700753 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000754 memcpy(f->eth_addr, mac, ETH_ALEN);
755
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200756 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700757
stephen hemmingerd3428942012-10-01 12:32:35 +0000758 ++vxlan->addrcnt;
759 hlist_add_head_rcu(&f->hlist,
760 vxlan_fdb_head(vxlan, mac));
761 }
762
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200763 if (notify) {
764 if (rd == NULL)
765 rd = first_remote_rtnl(f);
766 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
767 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000768
769 return 0;
770}
771
Wei Yongjun6706c822013-04-11 19:00:35 +0000772static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000773{
774 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700775 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000776
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700777 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000778 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000779 kfree(f);
780}
781
stephen hemmingerd3428942012-10-01 12:32:35 +0000782static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
783{
784 netdev_dbg(vxlan->dev,
785 "delete %pM\n", f->eth_addr);
786
787 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200788 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000789
790 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000791 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000792}
793
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300794static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800795 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300796{
797 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800798 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300799
800 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800801 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
802 if (err)
803 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300804 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800805 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
806 if (remote->sa.sa_family == AF_INET) {
807 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
808 ip->sa.sa_family = AF_INET;
809#if IS_ENABLED(CONFIG_IPV6)
810 } else {
811 ip->sin6.sin6_addr = in6addr_any;
812 ip->sa.sa_family = AF_INET6;
813#endif
814 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300815 }
816
817 if (tb[NDA_PORT]) {
818 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
819 return -EINVAL;
820 *port = nla_get_be16(tb[NDA_PORT]);
821 } else {
822 *port = vxlan->dst_port;
823 }
824
825 if (tb[NDA_VNI]) {
826 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
827 return -EINVAL;
828 *vni = nla_get_u32(tb[NDA_VNI]);
829 } else {
830 *vni = vxlan->default_dst.remote_vni;
831 }
832
833 if (tb[NDA_IFINDEX]) {
834 struct net_device *tdev;
835
836 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
837 return -EINVAL;
838 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800839 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300840 if (!tdev)
841 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300842 } else {
843 *ifindex = 0;
844 }
845
846 return 0;
847}
848
stephen hemmingerd3428942012-10-01 12:32:35 +0000849/* Add static entry (via netlink) */
850static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
851 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100852 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000853{
854 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300855 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800856 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000857 __be16 port;
858 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000859 int err;
860
861 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
862 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
863 ndm->ndm_state);
864 return -EINVAL;
865 }
866
867 if (tb[NDA_DST] == NULL)
868 return -EINVAL;
869
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300870 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
871 if (err)
872 return err;
David Stevens66817122013-03-15 04:35:51 +0000873
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300874 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
875 return -EAFNOSUPPORT;
876
stephen hemmingerd3428942012-10-01 12:32:35 +0000877 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800878 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000879 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000880 spin_unlock_bh(&vxlan->hash_lock);
881
882 return err;
883}
884
885/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000886static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
887 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100888 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000889{
890 struct vxlan_dev *vxlan = netdev_priv(dev);
891 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300892 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800893 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300894 __be16 port;
895 u32 vni, ifindex;
896 int err;
897
898 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
899 if (err)
900 return err;
901
902 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000903
904 spin_lock_bh(&vxlan->hash_lock);
905 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300906 if (!f)
907 goto out;
908
Cong Wange4c7ed42013-08-31 13:44:33 +0800909 if (!vxlan_addr_any(&ip)) {
910 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300911 if (!rd)
912 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000913 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300914
915 err = 0;
916
917 /* remove a destination if it's not the only one on the list,
918 * otherwise destroy the fdb entry
919 */
920 if (rd && !list_is_singular(&f->remotes)) {
921 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200922 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800923 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300924 goto out;
925 }
926
927 vxlan_fdb_destroy(vxlan, f);
928
929out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000930 spin_unlock_bh(&vxlan->hash_lock);
931
932 return err;
933}
934
935/* Dump forwarding table */
936static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400937 struct net_device *dev,
938 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000939{
940 struct vxlan_dev *vxlan = netdev_priv(dev);
941 unsigned int h;
942
943 for (h = 0; h < FDB_HASH_SIZE; ++h) {
944 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000945 int err;
946
Sasha Levinb67bfe02013-02-27 17:06:00 -0800947 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000948 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000949
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700950 if (idx < cb->args[0])
951 goto skip;
952
953 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000954 err = vxlan_fdb_info(skb, vxlan, f,
955 NETLINK_CB(cb->skb).portid,
956 cb->nlh->nlmsg_seq,
957 RTM_NEWNEIGH,
958 NLM_F_MULTI, rd);
959 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700960 goto out;
David Stevens66817122013-03-15 04:35:51 +0000961 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700962skip:
963 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000964 }
965 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700966out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000967 return idx;
968}
969
970/* Watch incoming packets to learn mapping between Ethernet address
971 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700972 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000973 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700974static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800975 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000976{
977 struct vxlan_dev *vxlan = netdev_priv(dev);
978 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000979
980 f = vxlan_find_mac(vxlan, src_mac);
981 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700982 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700983
Cong Wange4c7ed42013-08-31 13:44:33 +0800984 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700985 return false;
986
987 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700988 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700989 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000990
991 if (net_ratelimit())
992 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800993 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700994 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000995
Cong Wange4c7ed42013-08-31 13:44:33 +0800996 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000997 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200998 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000999 } else {
1000 /* learned new entry */
1001 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001002
1003 /* close off race between vxlan_flush and incoming packets */
1004 if (netif_running(dev))
1005 vxlan_fdb_create(vxlan, src_mac, src_ip,
1006 NUD_REACHABLE,
1007 NLM_F_EXCL|NLM_F_CREATE,
1008 vxlan->dst_port,
1009 vxlan->default_dst.remote_vni,
1010 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001011 spin_unlock(&vxlan->hash_lock);
1012 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001013
1014 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001015}
1016
stephen hemmingerd3428942012-10-01 12:32:35 +00001017/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001018static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001019{
stephen hemminger553675f2013-05-16 11:35:20 +00001020 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001021
Gao feng95ab0992013-12-10 16:37:33 +08001022 /* The vxlan_sock is only used by dev, leaving group has
1023 * no effect on other vxlan devices.
1024 */
1025 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1026 return false;
1027
stephen hemminger553675f2013-05-16 11:35:20 +00001028 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001029 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001030 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001031
Gao feng95ab0992013-12-10 16:37:33 +08001032 if (vxlan->vn_sock != dev->vn_sock)
1033 continue;
1034
1035 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1036 &dev->default_dst.remote_ip))
1037 continue;
1038
1039 if (vxlan->default_dst.remote_ifindex !=
1040 dev->default_dst.remote_ifindex)
1041 continue;
1042
1043 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001044 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001045
1046 return false;
1047}
1048
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001049static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001050{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001051 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001052}
1053
Pravin B Shelar012a5722013-08-19 11:23:07 -07001054void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001055{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001056 struct sock *sk = vs->sock->sk;
1057 struct net *net = sock_net(sk);
1058 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001059
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001060 if (!atomic_dec_and_test(&vs->refcnt))
1061 return;
1062
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001063 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001064 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001065 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001066 spin_unlock(&vn->sock_lock);
1067
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001068 queue_work(vxlan_wq, &vs->del_work);
1069}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001070EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001071
stephen hemminger3fc2de22013-07-18 08:40:15 -07001072/* Callback to update multicast group membership when first VNI on
1073 * multicast asddress is brought up
1074 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001075 */
stephen hemminger3fc2de22013-07-18 08:40:15 -07001076static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001077{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001078 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001079 struct vxlan_sock *vs = vxlan->vn_sock;
1080 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001081 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1082 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001083
stephen hemmingerd3428942012-10-01 12:32:35 +00001084 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001085 if (ip->sa.sa_family == AF_INET) {
1086 struct ip_mreqn mreq = {
1087 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1088 .imr_ifindex = ifindex,
1089 };
1090
1091 ip_mc_join_group(sk, &mreq);
1092#if IS_ENABLED(CONFIG_IPV6)
1093 } else {
1094 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1095 &ip->sin6.sin6_addr);
1096#endif
1097 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001098 release_sock(sk);
1099
Pravin B Shelar012a5722013-08-19 11:23:07 -07001100 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001101 dev_put(vxlan->dev);
1102}
1103
1104/* Inverse of vxlan_igmp_join when last VNI is brought down */
1105static void vxlan_igmp_leave(struct work_struct *work)
1106{
1107 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001108 struct vxlan_sock *vs = vxlan->vn_sock;
1109 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001110 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1111 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001112
1113 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001114 if (ip->sa.sa_family == AF_INET) {
1115 struct ip_mreqn mreq = {
1116 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1117 .imr_ifindex = ifindex,
1118 };
1119
1120 ip_mc_leave_group(sk, &mreq);
1121#if IS_ENABLED(CONFIG_IPV6)
1122 } else {
1123 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1124 &ip->sin6.sin6_addr);
1125#endif
1126 }
1127
stephen hemmingerd3428942012-10-01 12:32:35 +00001128 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001129
Pravin B Shelar012a5722013-08-19 11:23:07 -07001130 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001131 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001132}
1133
1134/* Callback from net/ipv4/udp.c to receive packets */
1135static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1136{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001137 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001138 struct vxlanhdr *vxh;
stephen hemmingerd3428942012-10-01 12:32:35 +00001139
stephen hemmingerd3428942012-10-01 12:32:35 +00001140 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001141 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001142 goto error;
1143
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001144 /* Return packets with reserved bits set */
1145 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001146 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1147 (vxh->vx_vni & htonl(0xff))) {
1148 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1149 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1150 goto error;
1151 }
1152
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001153 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001154 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001155
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001156 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001157 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001158 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001159
1160 vs->rcv(vs, skb, vxh->vx_vni);
1161 return 0;
1162
1163drop:
1164 /* Consume bad packet */
1165 kfree_skb(skb);
1166 return 0;
1167
1168error:
1169 /* Return non vxlan pkt */
1170 return 1;
1171}
1172
1173static void vxlan_rcv(struct vxlan_sock *vs,
1174 struct sk_buff *skb, __be32 vx_vni)
1175{
Cong Wange4c7ed42013-08-31 13:44:33 +08001176 struct iphdr *oip = NULL;
1177 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001178 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001179 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001180 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001181 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001182 int err = 0;
1183 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001184
1185 vni = ntohl(vx_vni) >> 8;
1186 /* Is this VNI defined? */
1187 vxlan = vxlan_vs_find_vni(vs, vni);
1188 if (!vxlan)
1189 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001190
Cong Wange4c7ed42013-08-31 13:44:33 +08001191 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001192 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001193 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001194 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001195 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001196
1197 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001198 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001199 goto drop;
1200
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001201 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001202 if (remote_ip->sa.sa_family == AF_INET) {
1203 oip = ip_hdr(skb);
1204 saddr.sin.sin_addr.s_addr = oip->saddr;
1205 saddr.sa.sa_family = AF_INET;
1206#if IS_ENABLED(CONFIG_IPV6)
1207 } else {
1208 oip6 = ipv6_hdr(skb);
1209 saddr.sin6.sin6_addr = oip6->saddr;
1210 saddr.sa.sa_family = AF_INET6;
1211#endif
1212 }
1213
stephen hemminger26a41ae62013-06-17 12:09:58 -07001214 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001215 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001216 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001217
stephen hemmingerd3428942012-10-01 12:32:35 +00001218 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001219
Cong Wange4c7ed42013-08-31 13:44:33 +08001220 if (oip6)
1221 err = IP6_ECN_decapsulate(oip6, skb);
1222 if (oip)
1223 err = IP_ECN_decapsulate(oip, skb);
1224
stephen hemmingerd3428942012-10-01 12:32:35 +00001225 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001226 if (log_ecn_error) {
1227 if (oip6)
1228 net_info_ratelimited("non-ECT from %pI6\n",
1229 &oip6->saddr);
1230 if (oip)
1231 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1232 &oip->saddr, oip->tos);
1233 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001234 if (err > 1) {
1235 ++vxlan->dev->stats.rx_frame_errors;
1236 ++vxlan->dev->stats.rx_errors;
1237 goto drop;
1238 }
1239 }
1240
Pravin B Shelare8171042013-03-25 14:49:46 +00001241 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001242 u64_stats_update_begin(&stats->syncp);
1243 stats->rx_packets++;
1244 stats->rx_bytes += skb->len;
1245 u64_stats_update_end(&stats->syncp);
1246
1247 netif_rx(skb);
1248
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001249 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001250drop:
1251 /* Consume bad packet */
1252 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001253}
1254
David Stevense4f67ad2012-11-20 02:50:14 +00001255static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1256{
1257 struct vxlan_dev *vxlan = netdev_priv(dev);
1258 struct arphdr *parp;
1259 u8 *arpptr, *sha;
1260 __be32 sip, tip;
1261 struct neighbour *n;
1262
1263 if (dev->flags & IFF_NOARP)
1264 goto out;
1265
1266 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1267 dev->stats.tx_dropped++;
1268 goto out;
1269 }
1270 parp = arp_hdr(skb);
1271
1272 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1273 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1274 parp->ar_pro != htons(ETH_P_IP) ||
1275 parp->ar_op != htons(ARPOP_REQUEST) ||
1276 parp->ar_hln != dev->addr_len ||
1277 parp->ar_pln != 4)
1278 goto out;
1279 arpptr = (u8 *)parp + sizeof(struct arphdr);
1280 sha = arpptr;
1281 arpptr += dev->addr_len; /* sha */
1282 memcpy(&sip, arpptr, sizeof(sip));
1283 arpptr += sizeof(sip);
1284 arpptr += dev->addr_len; /* tha */
1285 memcpy(&tip, arpptr, sizeof(tip));
1286
1287 if (ipv4_is_loopback(tip) ||
1288 ipv4_is_multicast(tip))
1289 goto out;
1290
1291 n = neigh_lookup(&arp_tbl, &tip, dev);
1292
1293 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001294 struct vxlan_fdb *f;
1295 struct sk_buff *reply;
1296
1297 if (!(n->nud_state & NUD_CONNECTED)) {
1298 neigh_release(n);
1299 goto out;
1300 }
1301
1302 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001303 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001304 /* bridge-local neighbor */
1305 neigh_release(n);
1306 goto out;
1307 }
1308
1309 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1310 n->ha, sha);
1311
1312 neigh_release(n);
1313
David Stevens73461352014-03-18 12:32:29 -04001314 if (reply == NULL)
1315 goto out;
1316
David Stevense4f67ad2012-11-20 02:50:14 +00001317 skb_reset_mac_header(reply);
1318 __skb_pull(reply, skb_network_offset(reply));
1319 reply->ip_summed = CHECKSUM_UNNECESSARY;
1320 reply->pkt_type = PACKET_HOST;
1321
1322 if (netif_rx_ni(reply) == NET_RX_DROP)
1323 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001324 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1325 union vxlan_addr ipa = {
1326 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001327 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001328 };
1329
1330 vxlan_ip_miss(dev, &ipa);
1331 }
David Stevense4f67ad2012-11-20 02:50:14 +00001332out:
1333 consume_skb(skb);
1334 return NETDEV_TX_OK;
1335}
1336
Cong Wangf564f452013-08-31 13:44:36 +08001337#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001338static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1339 struct neighbour *n, bool isrouter)
1340{
1341 struct net_device *dev = request->dev;
1342 struct sk_buff *reply;
1343 struct nd_msg *ns, *na;
1344 struct ipv6hdr *pip6;
1345 u8 *daddr;
1346 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1347 int ns_olen;
1348 int i, len;
1349
1350 if (dev == NULL)
1351 return NULL;
1352
1353 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1354 sizeof(*na) + na_olen + dev->needed_tailroom;
1355 reply = alloc_skb(len, GFP_ATOMIC);
1356 if (reply == NULL)
1357 return NULL;
1358
1359 reply->protocol = htons(ETH_P_IPV6);
1360 reply->dev = dev;
1361 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1362 skb_push(reply, sizeof(struct ethhdr));
1363 skb_set_mac_header(reply, 0);
1364
1365 ns = (struct nd_msg *)skb_transport_header(request);
1366
1367 daddr = eth_hdr(request)->h_source;
1368 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1369 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1370 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1371 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1372 break;
1373 }
1374 }
1375
1376 /* Ethernet header */
1377 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1378 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1379 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1380 reply->protocol = htons(ETH_P_IPV6);
1381
1382 skb_pull(reply, sizeof(struct ethhdr));
1383 skb_set_network_header(reply, 0);
1384 skb_put(reply, sizeof(struct ipv6hdr));
1385
1386 /* IPv6 header */
1387
1388 pip6 = ipv6_hdr(reply);
1389 memset(pip6, 0, sizeof(struct ipv6hdr));
1390 pip6->version = 6;
1391 pip6->priority = ipv6_hdr(request)->priority;
1392 pip6->nexthdr = IPPROTO_ICMPV6;
1393 pip6->hop_limit = 255;
1394 pip6->daddr = ipv6_hdr(request)->saddr;
1395 pip6->saddr = *(struct in6_addr *)n->primary_key;
1396
1397 skb_pull(reply, sizeof(struct ipv6hdr));
1398 skb_set_transport_header(reply, 0);
1399
1400 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1401
1402 /* Neighbor Advertisement */
1403 memset(na, 0, sizeof(*na)+na_olen);
1404 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1405 na->icmph.icmp6_router = isrouter;
1406 na->icmph.icmp6_override = 1;
1407 na->icmph.icmp6_solicited = 1;
1408 na->target = ns->target;
1409 ether_addr_copy(&na->opt[2], n->ha);
1410 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1411 na->opt[1] = na_olen >> 3;
1412
1413 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1414 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1415 csum_partial(na, sizeof(*na)+na_olen, 0));
1416
1417 pip6->payload_len = htons(sizeof(*na)+na_olen);
1418
1419 skb_push(reply, sizeof(struct ipv6hdr));
1420
1421 reply->ip_summed = CHECKSUM_UNNECESSARY;
1422
1423 return reply;
1424}
1425
Cong Wangf564f452013-08-31 13:44:36 +08001426static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1427{
1428 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001429 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001430 const struct ipv6hdr *iphdr;
1431 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001432 struct neighbour *n;
1433 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001434
1435 in6_dev = __in6_dev_get(dev);
1436 if (!in6_dev)
1437 goto out;
1438
Cong Wangf564f452013-08-31 13:44:36 +08001439 iphdr = ipv6_hdr(skb);
1440 saddr = &iphdr->saddr;
1441 daddr = &iphdr->daddr;
1442
Cong Wangf564f452013-08-31 13:44:36 +08001443 msg = (struct nd_msg *)skb_transport_header(skb);
1444 if (msg->icmph.icmp6_code != 0 ||
1445 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1446 goto out;
1447
David Stevens4b29dba2014-03-24 10:39:58 -04001448 if (ipv6_addr_loopback(daddr) ||
1449 ipv6_addr_is_multicast(&msg->target))
1450 goto out;
1451
1452 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001453
1454 if (n) {
1455 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001456 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001457
1458 if (!(n->nud_state & NUD_CONNECTED)) {
1459 neigh_release(n);
1460 goto out;
1461 }
1462
1463 f = vxlan_find_mac(vxlan, n->ha);
1464 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1465 /* bridge-local neighbor */
1466 neigh_release(n);
1467 goto out;
1468 }
1469
David Stevens4b29dba2014-03-24 10:39:58 -04001470 reply = vxlan_na_create(skb, n,
1471 !!(f ? f->flags & NTF_ROUTER : 0));
1472
Cong Wangf564f452013-08-31 13:44:36 +08001473 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001474
1475 if (reply == NULL)
1476 goto out;
1477
1478 if (netif_rx_ni(reply) == NET_RX_DROP)
1479 dev->stats.rx_dropped++;
1480
Cong Wangf564f452013-08-31 13:44:36 +08001481 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001482 union vxlan_addr ipa = {
1483 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001484 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001485 };
1486
Cong Wangf564f452013-08-31 13:44:36 +08001487 vxlan_ip_miss(dev, &ipa);
1488 }
1489
1490out:
1491 consume_skb(skb);
1492 return NETDEV_TX_OK;
1493}
1494#endif
1495
David Stevense4f67ad2012-11-20 02:50:14 +00001496static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1497{
1498 struct vxlan_dev *vxlan = netdev_priv(dev);
1499 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001500
1501 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1502 return false;
1503
1504 n = NULL;
1505 switch (ntohs(eth_hdr(skb)->h_proto)) {
1506 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001507 {
1508 struct iphdr *pip;
1509
David Stevense4f67ad2012-11-20 02:50:14 +00001510 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1511 return false;
1512 pip = ip_hdr(skb);
1513 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001514 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1515 union vxlan_addr ipa = {
1516 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001517 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001518 };
1519
1520 vxlan_ip_miss(dev, &ipa);
1521 return false;
1522 }
1523
David Stevense4f67ad2012-11-20 02:50:14 +00001524 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001525 }
1526#if IS_ENABLED(CONFIG_IPV6)
1527 case ETH_P_IPV6:
1528 {
1529 struct ipv6hdr *pip6;
1530
1531 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1532 return false;
1533 pip6 = ipv6_hdr(skb);
1534 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1535 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1536 union vxlan_addr ipa = {
1537 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001538 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001539 };
1540
1541 vxlan_ip_miss(dev, &ipa);
1542 return false;
1543 }
1544
1545 break;
1546 }
1547#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001548 default:
1549 return false;
1550 }
1551
1552 if (n) {
1553 bool diff;
1554
Joe Perches7367d0b2013-09-01 11:51:23 -07001555 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001556 if (diff) {
1557 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1558 dev->addr_len);
1559 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1560 }
1561 neigh_release(n);
1562 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001563 }
1564
David Stevense4f67ad2012-11-20 02:50:14 +00001565 return false;
1566}
1567
Cong Wange4c7ed42013-08-31 13:44:33 +08001568#if IS_ENABLED(CONFIG_IPV6)
Nicolas Dichtel11796182013-09-02 15:34:55 +02001569static int vxlan6_xmit_skb(struct vxlan_sock *vs,
Cong Wange4c7ed42013-08-31 13:44:33 +08001570 struct dst_entry *dst, struct sk_buff *skb,
1571 struct net_device *dev, struct in6_addr *saddr,
1572 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001573 __be16 src_port, __be16 dst_port, __be32 vni,
1574 bool xnet)
Cong Wange4c7ed42013-08-31 13:44:33 +08001575{
Cong Wange4c7ed42013-08-31 13:44:33 +08001576 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001577 int min_headroom;
1578 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001579 bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001580
Andy Zhouacbf74a2014-09-16 17:31:18 -07001581 skb = udp_tunnel_handle_offloads(skb, udp_sum);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001582 if (IS_ERR(skb))
1583 return -EINVAL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001584
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001585 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001586
Cong Wange4c7ed42013-08-31 13:44:33 +08001587 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1588 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1589 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1590
1591 /* Need space for new headers (invalidates iph ptr) */
1592 err = skb_cow_head(skb, min_headroom);
1593 if (unlikely(err))
1594 return err;
1595
Jiri Pirko59682502014-11-19 14:04:59 +01001596 skb = vlan_hwaccel_push_inside(skb);
1597 if (WARN_ON(!skb))
1598 return -ENOMEM;
Cong Wange4c7ed42013-08-31 13:44:33 +08001599
1600 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1601 vxh->vx_flags = htonl(VXLAN_FLAGS);
1602 vxh->vx_vni = vni;
1603
Tom Herbert996c9fd2014-09-29 20:22:33 -07001604 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1605
Andy Zhouacbf74a2014-09-16 17:31:18 -07001606 udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
1607 ttl, src_port, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001608 return 0;
1609}
1610#endif
1611
Nicolas Dichtel11796182013-09-02 15:34:55 +02001612int vxlan_xmit_skb(struct vxlan_sock *vs,
Pravin B Shelar49560532013-08-19 11:23:17 -07001613 struct rtable *rt, struct sk_buff *skb,
1614 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001615 __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)
Pravin B Shelar49560532013-08-19 11:23:17 -07001616{
1617 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001618 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001619 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001620 bool udp_sum = !vs->sock->sk->sk_no_check_tx;
Pravin B Shelar49560532013-08-19 11:23:17 -07001621
Andy Zhouacbf74a2014-09-16 17:31:18 -07001622 skb = udp_tunnel_handle_offloads(skb, udp_sum);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001623 if (IS_ERR(skb))
1624 return -EINVAL;
Pravin B Shelar49560532013-08-19 11:23:17 -07001625
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001626 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001627 + VXLAN_HLEN + sizeof(struct iphdr)
1628 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001629
1630 /* Need space for new headers (invalidates iph ptr) */
1631 err = skb_cow_head(skb, min_headroom);
1632 if (unlikely(err))
1633 return err;
1634
Jiri Pirko59682502014-11-19 14:04:59 +01001635 skb = vlan_hwaccel_push_inside(skb);
1636 if (WARN_ON(!skb))
1637 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001638
Pravin B Shelar49560532013-08-19 11:23:17 -07001639 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1640 vxh->vx_flags = htonl(VXLAN_FLAGS);
1641 vxh->vx_vni = vni;
1642
Tom Herbert996c9fd2014-09-29 20:22:33 -07001643 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1644
Andy Zhouacbf74a2014-09-16 17:31:18 -07001645 return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
1646 ttl, df, src_port, dst_port, xnet);
Pravin B Shelar49560532013-08-19 11:23:17 -07001647}
1648EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1649
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001650/* Bypass encapsulation if the destination is local */
1651static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1652 struct vxlan_dev *dst_vxlan)
1653{
Li RongQing8f849852014-01-04 13:57:59 +08001654 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001655 union vxlan_addr loopback;
1656 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001657 struct net_device *dev = skb->dev;
1658 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001659
Li RongQing8f849852014-01-04 13:57:59 +08001660 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1661 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001662 skb->pkt_type = PACKET_HOST;
1663 skb->encapsulation = 0;
1664 skb->dev = dst_vxlan->dev;
1665 __skb_pull(skb, skb_network_offset(skb));
1666
Cong Wange4c7ed42013-08-31 13:44:33 +08001667 if (remote_ip->sa.sa_family == AF_INET) {
1668 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1669 loopback.sa.sa_family = AF_INET;
1670#if IS_ENABLED(CONFIG_IPV6)
1671 } else {
1672 loopback.sin6.sin6_addr = in6addr_loopback;
1673 loopback.sa.sa_family = AF_INET6;
1674#endif
1675 }
1676
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001677 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001678 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001679
1680 u64_stats_update_begin(&tx_stats->syncp);
1681 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001682 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001683 u64_stats_update_end(&tx_stats->syncp);
1684
1685 if (netif_rx(skb) == NET_RX_SUCCESS) {
1686 u64_stats_update_begin(&rx_stats->syncp);
1687 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001688 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001689 u64_stats_update_end(&rx_stats->syncp);
1690 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001691 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001692 }
1693}
1694
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001695static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1696 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001697{
1698 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001699 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001700 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001701 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001702 union vxlan_addr *dst;
1703 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001704 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001705 __be16 df = 0;
1706 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001707 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001708
stephen hemminger823aa872013-04-27 11:31:57 +00001709 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001710 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001711 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001712
Cong Wange4c7ed42013-08-31 13:44:33 +08001713 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001714 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001715 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001716 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001717 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001718 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001719 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001720 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001721
stephen hemmingerd3428942012-10-01 12:32:35 +00001722 old_iph = ip_hdr(skb);
1723
stephen hemmingerd3428942012-10-01 12:32:35 +00001724 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001725 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001726 ttl = 1;
1727
1728 tos = vxlan->tos;
1729 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001730 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001731
Tom Herbert535fb8d2014-07-01 21:32:49 -07001732 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1733 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001734
Cong Wange4c7ed42013-08-31 13:44:33 +08001735 if (dst->sa.sa_family == AF_INET) {
1736 memset(&fl4, 0, sizeof(fl4));
1737 fl4.flowi4_oif = rdst->remote_ifindex;
1738 fl4.flowi4_tos = RT_TOS(tos);
1739 fl4.daddr = dst->sin.sin_addr.s_addr;
1740 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001741
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001742 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001743 if (IS_ERR(rt)) {
1744 netdev_dbg(dev, "no route to %pI4\n",
1745 &dst->sin.sin_addr.s_addr);
1746 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001747 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001748 }
1749
1750 if (rt->dst.dev == dev) {
1751 netdev_dbg(dev, "circular route to %pI4\n",
1752 &dst->sin.sin_addr.s_addr);
1753 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001754 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001755 }
1756
1757 /* Bypass encapsulation if the destination is local */
1758 if (rt->rt_flags & RTCF_LOCAL &&
1759 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1760 struct vxlan_dev *dst_vxlan;
1761
1762 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001763 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1764 dst->sa.sa_family, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001765 if (!dst_vxlan)
1766 goto tx_error;
1767 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1768 return;
1769 }
1770
1771 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1772 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1773
Andy Zhou3c4d1da2014-09-23 01:44:51 -07001774 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1775 fl4.saddr, dst->sin.sin_addr.s_addr,
1776 tos, ttl, df, src_port, dst_port,
1777 htonl(vni << 8),
1778 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Cong Wange4c7ed42013-08-31 13:44:33 +08001779
1780 if (err < 0)
1781 goto rt_tx_error;
1782 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1783#if IS_ENABLED(CONFIG_IPV6)
1784 } else {
1785 struct sock *sk = vxlan->vn_sock->sock->sk;
1786 struct dst_entry *ndst;
1787 struct flowi6 fl6;
1788 u32 flags;
1789
1790 memset(&fl6, 0, sizeof(fl6));
1791 fl6.flowi6_oif = rdst->remote_ifindex;
1792 fl6.daddr = dst->sin6.sin6_addr;
1793 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08001794 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001795
1796 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1797 netdev_dbg(dev, "no route to %pI6\n",
1798 &dst->sin6.sin6_addr);
1799 dev->stats.tx_carrier_errors++;
1800 goto tx_error;
1801 }
1802
1803 if (ndst->dev == dev) {
1804 netdev_dbg(dev, "circular route to %pI6\n",
1805 &dst->sin6.sin6_addr);
1806 dst_release(ndst);
1807 dev->stats.collisions++;
1808 goto tx_error;
1809 }
1810
1811 /* Bypass encapsulation if the destination is local */
1812 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1813 if (flags & RTF_LOCAL &&
1814 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1815 struct vxlan_dev *dst_vxlan;
1816
1817 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001818 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1819 dst->sa.sa_family, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001820 if (!dst_vxlan)
1821 goto tx_error;
1822 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1823 return;
1824 }
1825
1826 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1827
Nicolas Dichtel11796182013-09-02 15:34:55 +02001828 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001829 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001830 src_port, dst_port, htonl(vni << 8),
1831 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Cong Wange4c7ed42013-08-31 13:44:33 +08001832#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001833 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001834
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001835 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001836
1837drop:
1838 dev->stats.tx_dropped++;
1839 goto tx_free;
1840
Pravin B Shelar49560532013-08-19 11:23:17 -07001841rt_tx_error:
1842 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001843tx_error:
1844 dev->stats.tx_errors++;
1845tx_free:
1846 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001847}
1848
David Stevens66817122013-03-15 04:35:51 +00001849/* Transmit local packets over Vxlan
1850 *
1851 * Outer IP header inherits ECN and DF from inner header.
1852 * Outer UDP destination is the VXLAN assigned port.
1853 * source port is based on hash of flow
1854 */
1855static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1856{
1857 struct vxlan_dev *vxlan = netdev_priv(dev);
1858 struct ethhdr *eth;
1859 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08001860 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00001861 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001862
1863 skb_reset_mac_header(skb);
1864 eth = eth_hdr(skb);
1865
Cong Wangf564f452013-08-31 13:44:36 +08001866 if ((vxlan->flags & VXLAN_F_PROXY)) {
1867 if (ntohs(eth->h_proto) == ETH_P_ARP)
1868 return arp_reduce(dev, skb);
1869#if IS_ENABLED(CONFIG_IPV6)
1870 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08001871 pskb_may_pull(skb, sizeof(struct ipv6hdr)
1872 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08001873 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1874 struct nd_msg *msg;
1875
1876 msg = (struct nd_msg *)skb_transport_header(skb);
1877 if (msg->icmph.icmp6_code == 0 &&
1878 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1879 return neigh_reduce(dev, skb);
1880 }
Li RongQing7a9f5262014-10-17 14:06:16 +08001881 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001882#endif
1883 }
David Stevens66817122013-03-15 04:35:51 +00001884
1885 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001886 did_rsc = false;
1887
1888 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08001889 (ntohs(eth->h_proto) == ETH_P_IP ||
1890 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00001891 did_rsc = route_shortcircuit(dev, skb);
1892 if (did_rsc)
1893 f = vxlan_find_mac(vxlan, eth->h_dest);
1894 }
1895
David Stevens66817122013-03-15 04:35:51 +00001896 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001897 f = vxlan_find_mac(vxlan, all_zeros_mac);
1898 if (f == NULL) {
1899 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1900 !is_multicast_ether_addr(eth->h_dest))
1901 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001902
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001903 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08001904 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001905 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001906 }
David Stevens66817122013-03-15 04:35:51 +00001907 }
1908
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001909 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1910 struct sk_buff *skb1;
1911
Eric Dumazet8f646c92014-01-06 09:54:31 -08001912 if (!fdst) {
1913 fdst = rdst;
1914 continue;
1915 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001916 skb1 = skb_clone(skb, GFP_ATOMIC);
1917 if (skb1)
1918 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1919 }
1920
Eric Dumazet8f646c92014-01-06 09:54:31 -08001921 if (fdst)
1922 vxlan_xmit_one(skb, dev, fdst, did_rsc);
1923 else
1924 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001925 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001926}
1927
stephen hemmingerd3428942012-10-01 12:32:35 +00001928/* Walk the forwarding table and purge stale entries */
1929static void vxlan_cleanup(unsigned long arg)
1930{
1931 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1932 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1933 unsigned int h;
1934
1935 if (!netif_running(vxlan->dev))
1936 return;
1937
1938 spin_lock_bh(&vxlan->hash_lock);
1939 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1940 struct hlist_node *p, *n;
1941 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1942 struct vxlan_fdb *f
1943 = container_of(p, struct vxlan_fdb, hlist);
1944 unsigned long timeout;
1945
stephen hemminger3c172862012-10-26 06:24:34 +00001946 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001947 continue;
1948
1949 timeout = f->used + vxlan->age_interval * HZ;
1950 if (time_before_eq(timeout, jiffies)) {
1951 netdev_dbg(vxlan->dev,
1952 "garbage collect %pM\n",
1953 f->eth_addr);
1954 f->state = NUD_STALE;
1955 vxlan_fdb_destroy(vxlan, f);
1956 } else if (time_before(timeout, next_timer))
1957 next_timer = timeout;
1958 }
1959 }
1960 spin_unlock_bh(&vxlan->hash_lock);
1961
1962 mod_timer(&vxlan->age_timer, next_timer);
1963}
1964
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001965static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1966{
1967 __u32 vni = vxlan->default_dst.remote_vni;
1968
1969 vxlan->vn_sock = vs;
1970 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1971}
1972
stephen hemmingerd3428942012-10-01 12:32:35 +00001973/* Setup stats when device is created */
1974static int vxlan_init(struct net_device *dev)
1975{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001976 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001977 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001978 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001979 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001980
WANG Cong1c213bd2014-02-13 11:46:28 -08001981 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00001982 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001983 return -ENOMEM;
1984
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001985 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001986 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
1987 vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001988 if (vs) {
1989 /* If we have a socket with same port already, reuse it */
1990 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001991 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001992 } else {
1993 /* otherwise make new socket outside of RTNL */
1994 dev_hold(dev);
1995 queue_work(vxlan_wq, &vxlan->sock_work);
1996 }
1997 spin_unlock(&vn->sock_lock);
1998
stephen hemmingerd3428942012-10-01 12:32:35 +00001999 return 0;
2000}
2001
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002002static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002003{
2004 struct vxlan_fdb *f;
2005
2006 spin_lock_bh(&vxlan->hash_lock);
2007 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2008 if (f)
2009 vxlan_fdb_destroy(vxlan, f);
2010 spin_unlock_bh(&vxlan->hash_lock);
2011}
2012
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002013static void vxlan_uninit(struct net_device *dev)
2014{
2015 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002016 struct vxlan_sock *vs = vxlan->vn_sock;
2017
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002018 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002019
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002020 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07002021 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002022 free_percpu(dev->tstats);
2023}
2024
stephen hemmingerd3428942012-10-01 12:32:35 +00002025/* Start ageing timer and join group when device is brought up */
2026static int vxlan_open(struct net_device *dev)
2027{
2028 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002029 struct vxlan_sock *vs = vxlan->vn_sock;
2030
2031 /* socket hasn't been created */
2032 if (!vs)
2033 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002034
Gao feng79d4a942013-12-10 16:37:32 +08002035 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002036 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002037 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002038 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00002039 }
2040
2041 if (vxlan->age_interval)
2042 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2043
2044 return 0;
2045}
2046
2047/* Purge the forwarding table */
2048static void vxlan_flush(struct vxlan_dev *vxlan)
2049{
Cong Wang31fec5a2013-05-27 22:35:52 +00002050 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002051
2052 spin_lock_bh(&vxlan->hash_lock);
2053 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2054 struct hlist_node *p, *n;
2055 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2056 struct vxlan_fdb *f
2057 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002058 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2059 if (!is_zero_ether_addr(f->eth_addr))
2060 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002061 }
2062 }
2063 spin_unlock_bh(&vxlan->hash_lock);
2064}
2065
2066/* Cleanup timer and forwarding table on shutdown */
2067static int vxlan_stop(struct net_device *dev)
2068{
2069 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002070 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002071 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00002072
Cong Wange4c7ed42013-08-31 13:44:33 +08002073 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002074 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002075 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002076 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002077 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002078 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002079
2080 del_timer_sync(&vxlan->age_timer);
2081
2082 vxlan_flush(vxlan);
2083
2084 return 0;
2085}
2086
stephen hemmingerd3428942012-10-01 12:32:35 +00002087/* Stub, nothing needs to be done. */
2088static void vxlan_set_multicast_list(struct net_device *dev)
2089{
2090}
2091
Daniel Borkmann345010b2013-12-18 00:21:08 +01002092static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2093{
2094 struct vxlan_dev *vxlan = netdev_priv(dev);
2095 struct vxlan_rdst *dst = &vxlan->default_dst;
2096 struct net_device *lowerdev;
2097 int max_mtu;
2098
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002099 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002100 if (lowerdev == NULL)
2101 return eth_change_mtu(dev, new_mtu);
2102
2103 if (dst->remote_ip.sa.sa_family == AF_INET6)
2104 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2105 else
2106 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2107
2108 if (new_mtu < 68 || new_mtu > max_mtu)
2109 return -EINVAL;
2110
2111 dev->mtu = new_mtu;
2112 return 0;
2113}
2114
stephen hemmingerd3428942012-10-01 12:32:35 +00002115static const struct net_device_ops vxlan_netdev_ops = {
2116 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002117 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002118 .ndo_open = vxlan_open,
2119 .ndo_stop = vxlan_stop,
2120 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002121 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002122 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002123 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002124 .ndo_validate_addr = eth_validate_addr,
2125 .ndo_set_mac_address = eth_mac_addr,
2126 .ndo_fdb_add = vxlan_fdb_add,
2127 .ndo_fdb_del = vxlan_fdb_delete,
2128 .ndo_fdb_dump = vxlan_fdb_dump,
2129};
2130
2131/* Info for udev, that this is a virtual tunnel endpoint */
2132static struct device_type vxlan_type = {
2133 .name = "vxlan",
2134};
2135
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002136/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002137 * supply the listening VXLAN udp ports. Callers are expected
2138 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002139 */
2140void vxlan_get_rx_port(struct net_device *dev)
2141{
2142 struct vxlan_sock *vs;
2143 struct net *net = dev_net(dev);
2144 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2145 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002146 __be16 port;
2147 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002148
2149 spin_lock(&vn->sock_lock);
2150 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002151 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2152 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002153 sa_family = vs->sock->sk->sk_family;
2154 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2155 port);
2156 }
2157 }
2158 spin_unlock(&vn->sock_lock);
2159}
2160EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2161
stephen hemmingerd3428942012-10-01 12:32:35 +00002162/* Initialize the device structure. */
2163static void vxlan_setup(struct net_device *dev)
2164{
2165 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002166 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002167
2168 eth_hw_addr_random(dev);
2169 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002170 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002171 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002172 else
Cong Wang2853af62014-06-12 11:53:10 -07002173 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002174
2175 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002176 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002177 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2178
2179 dev->tx_queue_len = 0;
2180 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002181 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002182 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002183 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002184
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002185 dev->vlan_features = dev->features;
2186 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002187 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002188 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002189 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002190 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002191 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002192
stephen hemminger553675f2013-05-16 11:35:20 +00002193 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002194 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002195 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2196 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002197 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002198
2199 init_timer_deferrable(&vxlan->age_timer);
2200 vxlan->age_timer.function = vxlan_cleanup;
2201 vxlan->age_timer.data = (unsigned long) vxlan;
2202
stephen hemminger823aa872013-04-27 11:31:57 +00002203 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002204
stephen hemmingerd3428942012-10-01 12:32:35 +00002205 vxlan->dev = dev;
2206
2207 for (h = 0; h < FDB_HASH_SIZE; ++h)
2208 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2209}
2210
2211static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2212 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002213 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002214 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002215 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2216 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002217 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002218 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2219 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2220 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2221 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2222 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002223 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002224 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2225 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2226 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2227 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002228 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002229 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2230 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2231 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002232};
2233
2234static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2235{
2236 if (tb[IFLA_ADDRESS]) {
2237 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2238 pr_debug("invalid link address (not ethernet)\n");
2239 return -EINVAL;
2240 }
2241
2242 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2243 pr_debug("invalid all zero ethernet address\n");
2244 return -EADDRNOTAVAIL;
2245 }
2246 }
2247
2248 if (!data)
2249 return -EINVAL;
2250
2251 if (data[IFLA_VXLAN_ID]) {
2252 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2253 if (id >= VXLAN_VID_MASK)
2254 return -ERANGE;
2255 }
2256
stephen hemminger05f47d62012-10-09 20:35:50 +00002257 if (data[IFLA_VXLAN_PORT_RANGE]) {
2258 const struct ifla_vxlan_port_range *p
2259 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2260
2261 if (ntohs(p->high) < ntohs(p->low)) {
2262 pr_debug("port range %u .. %u not valid\n",
2263 ntohs(p->low), ntohs(p->high));
2264 return -EINVAL;
2265 }
2266 }
2267
stephen hemmingerd3428942012-10-01 12:32:35 +00002268 return 0;
2269}
2270
Yan Burman1b13c972013-01-29 23:43:07 +00002271static void vxlan_get_drvinfo(struct net_device *netdev,
2272 struct ethtool_drvinfo *drvinfo)
2273{
2274 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2275 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2276}
2277
2278static const struct ethtool_ops vxlan_ethtool_ops = {
2279 .get_drvinfo = vxlan_get_drvinfo,
2280 .get_link = ethtool_op_get_link,
2281};
2282
stephen hemminger553675f2013-05-16 11:35:20 +00002283static void vxlan_del_work(struct work_struct *work)
2284{
2285 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002286 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002287 kfree_rcu(vs, rcu);
2288}
2289
Tom Herbert3ee64f32014-07-13 19:49:42 -07002290static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2291 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002292{
Cong Wange4c7ed42013-08-31 13:44:33 +08002293 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002294 struct udp_port_cfg udp_conf;
2295 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002296
Tom Herbert3ee64f32014-07-13 19:49:42 -07002297 memset(&udp_conf, 0, sizeof(udp_conf));
2298
2299 if (ipv6) {
2300 udp_conf.family = AF_INET6;
2301 udp_conf.use_udp6_tx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002302 !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002303 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002304 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002305 } else {
2306 udp_conf.family = AF_INET;
2307 udp_conf.local_ip.s_addr = INADDR_ANY;
2308 udp_conf.use_udp_checksums =
2309 !!(flags & VXLAN_F_UDP_CSUM);
Cong Wange4c7ed42013-08-31 13:44:33 +08002310 }
2311
Tom Herbert3ee64f32014-07-13 19:49:42 -07002312 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002313
Tom Herbert3ee64f32014-07-13 19:49:42 -07002314 /* Open UDP socket */
2315 err = udp_sock_create(net, &udp_conf, &sock);
2316 if (err < 0)
2317 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002318
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002319 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002320}
2321
2322/* Create new listen socket if needed */
2323static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002324 vxlan_rcv_t *rcv, void *data,
2325 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002326{
2327 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2328 struct vxlan_sock *vs;
2329 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002330 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002331 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002332 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002333
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002334 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002335 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002336 return ERR_PTR(-ENOMEM);
2337
2338 for (h = 0; h < VNI_HASH_SIZE; ++h)
2339 INIT_HLIST_HEAD(&vs->vni_list[h]);
2340
2341 INIT_WORK(&vs->del_work, vxlan_del_work);
2342
Tom Herbert3ee64f32014-07-13 19:49:42 -07002343 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002344 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002345 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002346 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002347 }
2348
Cong Wange4c7ed42013-08-31 13:44:33 +08002349 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002350 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002351 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002352 vs->data = data;
stephen hemminger553675f2013-05-16 11:35:20 +00002353
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002354 /* Initialize the vxlan udp offloads structure */
2355 vs->udp_offloads.port = port;
2356 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2357 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2358
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002359 spin_lock(&vn->sock_lock);
2360 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002361 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002362 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002363
2364 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002365 tunnel_cfg.sk_user_data = vs;
2366 tunnel_cfg.encap_type = 1;
2367 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2368 tunnel_cfg.encap_destroy = NULL;
2369
2370 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002371
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002372 return vs;
2373}
stephen hemminger553675f2013-05-16 11:35:20 +00002374
Pravin B Shelar012a5722013-08-19 11:23:07 -07002375struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2376 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002377 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002378{
2379 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2380 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002381 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002382
Tom Herbert359a0ea2014-06-04 17:20:29 -07002383 vs = vxlan_socket_create(net, port, rcv, data, flags);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002384 if (!IS_ERR(vs))
2385 return vs;
2386
Pravin B Shelar012a5722013-08-19 11:23:07 -07002387 if (no_share) /* Return error if sharing is not allowed. */
2388 return vs;
2389
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002390 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002391 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002392 if (vs) {
2393 if (vs->rcv == rcv)
2394 atomic_inc(&vs->refcnt);
2395 else
2396 vs = ERR_PTR(-EBUSY);
2397 }
2398 spin_unlock(&vn->sock_lock);
2399
2400 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002401 vs = ERR_PTR(-EINVAL);
2402
stephen hemminger553675f2013-05-16 11:35:20 +00002403 return vs;
2404}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002405EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002406
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002407/* Scheduled at device creation to bind to a socket */
2408static void vxlan_sock_work(struct work_struct *work)
2409{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002410 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002411 struct net *net = vxlan->net;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002412 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002413 __be16 port = vxlan->dst_port;
2414 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002415
Tom Herbert359a0ea2014-06-04 17:20:29 -07002416 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002417 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002418 if (!IS_ERR(nvs))
2419 vxlan_vs_add_dev(nvs, vxlan);
2420 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002421
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002422 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002423}
2424
stephen hemmingerd3428942012-10-01 12:32:35 +00002425static int vxlan_newlink(struct net *net, struct net_device *dev,
2426 struct nlattr *tb[], struct nlattr *data[])
2427{
stephen hemminger553675f2013-05-16 11:35:20 +00002428 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002429 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002430 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002431 __u32 vni;
2432 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002433 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002434
2435 if (!data[IFLA_VXLAN_ID])
2436 return -EINVAL;
2437
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002438 vxlan->net = dev_net(dev);
2439
stephen hemmingerd3428942012-10-01 12:32:35 +00002440 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002441 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002442
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002443 /* Unless IPv6 is explicitly requested, assume IPv4 */
2444 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002445 if (data[IFLA_VXLAN_GROUP]) {
2446 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002447 } else if (data[IFLA_VXLAN_GROUP6]) {
2448 if (!IS_ENABLED(CONFIG_IPV6))
2449 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002450
Cong Wange4c7ed42013-08-31 13:44:33 +08002451 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2452 sizeof(struct in6_addr));
2453 dst->remote_ip.sa.sa_family = AF_INET6;
2454 use_ipv6 = true;
2455 }
2456
2457 if (data[IFLA_VXLAN_LOCAL]) {
2458 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2459 vxlan->saddr.sa.sa_family = AF_INET;
2460 } else if (data[IFLA_VXLAN_LOCAL6]) {
2461 if (!IS_ENABLED(CONFIG_IPV6))
2462 return -EPFNOSUPPORT;
2463
2464 /* TODO: respect scope id */
2465 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2466 sizeof(struct in6_addr));
2467 vxlan->saddr.sa.sa_family = AF_INET6;
2468 use_ipv6 = true;
2469 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002470
stephen hemminger34e02aa2012-10-09 20:35:53 +00002471 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002472 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002473 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002474 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002475
stephen hemminger34e02aa2012-10-09 20:35:53 +00002476 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002477 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002478 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002479 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002480
Cong Wange4c7ed42013-08-31 13:44:33 +08002481#if IS_ENABLED(CONFIG_IPV6)
2482 if (use_ipv6) {
2483 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2484 if (idev && idev->cnf.disable_ipv6) {
2485 pr_info("IPv6 is disabled via sysctl\n");
2486 return -EPERM;
2487 }
2488 vxlan->flags |= VXLAN_F_IPV6;
2489 }
2490#endif
2491
stephen hemminger34e02aa2012-10-09 20:35:53 +00002492 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002493 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002494
Cong Wang2853af62014-06-12 11:53:10 -07002495 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002496 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002497 } else if (use_ipv6)
2498 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002499
2500 if (data[IFLA_VXLAN_TOS])
2501 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2502
Vincent Bernatafb97182012-10-30 10:27:16 +00002503 if (data[IFLA_VXLAN_TTL])
2504 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2505
stephen hemmingerd3428942012-10-01 12:32:35 +00002506 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002507 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002508
2509 if (data[IFLA_VXLAN_AGEING])
2510 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2511 else
2512 vxlan->age_interval = FDB_AGE_DEFAULT;
2513
David Stevense4f67ad2012-11-20 02:50:14 +00002514 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2515 vxlan->flags |= VXLAN_F_PROXY;
2516
2517 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2518 vxlan->flags |= VXLAN_F_RSC;
2519
2520 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2521 vxlan->flags |= VXLAN_F_L2MISS;
2522
2523 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2524 vxlan->flags |= VXLAN_F_L3MISS;
2525
stephen hemmingerd3428942012-10-01 12:32:35 +00002526 if (data[IFLA_VXLAN_LIMIT])
2527 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2528
stephen hemminger05f47d62012-10-09 20:35:50 +00002529 if (data[IFLA_VXLAN_PORT_RANGE]) {
2530 const struct ifla_vxlan_port_range *p
2531 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2532 vxlan->port_min = ntohs(p->low);
2533 vxlan->port_max = ntohs(p->high);
2534 }
2535
stephen hemminger823aa872013-04-27 11:31:57 +00002536 if (data[IFLA_VXLAN_PORT])
2537 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2538
Tom Herbert359a0ea2014-06-04 17:20:29 -07002539 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2540 vxlan->flags |= VXLAN_F_UDP_CSUM;
2541
2542 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2543 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2544 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2545
2546 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2547 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2548 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2549
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002550 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
2551 vxlan->dst_port)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002552 pr_info("duplicate VNI %u\n", vni);
2553 return -EEXIST;
2554 }
2555
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002556 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002557
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002558 /* create an fdb entry for a valid default destination */
2559 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2560 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2561 &vxlan->default_dst.remote_ip,
2562 NUD_REACHABLE|NUD_PERMANENT,
2563 NLM_F_EXCL|NLM_F_CREATE,
2564 vxlan->dst_port,
2565 vxlan->default_dst.remote_vni,
2566 vxlan->default_dst.remote_ifindex,
2567 NTF_SELF);
2568 if (err)
2569 return err;
2570 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002571
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002572 err = register_netdevice(dev);
2573 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002574 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002575 return err;
2576 }
2577
stephen hemminger553675f2013-05-16 11:35:20 +00002578 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002579
2580 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002581}
2582
2583static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2584{
2585 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002586 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002587
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002588 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002589 if (!hlist_unhashed(&vxlan->hlist))
2590 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002591 spin_unlock(&vn->sock_lock);
2592
stephen hemminger553675f2013-05-16 11:35:20 +00002593 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002594 unregister_netdevice_queue(dev, head);
2595}
2596
2597static size_t vxlan_get_size(const struct net_device *dev)
2598{
2599
2600 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002601 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002602 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002603 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002604 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2605 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2606 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002607 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2608 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2609 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2610 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002611 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2612 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002613 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002614 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2615 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2616 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2617 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002618 0;
2619}
2620
2621static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2622{
2623 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002624 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002625 struct ifla_vxlan_port_range ports = {
2626 .low = htons(vxlan->port_min),
2627 .high = htons(vxlan->port_max),
2628 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002629
Atzm Watanabec7995c42013-04-16 02:50:52 +00002630 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002631 goto nla_put_failure;
2632
Cong Wange4c7ed42013-08-31 13:44:33 +08002633 if (!vxlan_addr_any(&dst->remote_ip)) {
2634 if (dst->remote_ip.sa.sa_family == AF_INET) {
2635 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2636 dst->remote_ip.sin.sin_addr.s_addr))
2637 goto nla_put_failure;
2638#if IS_ENABLED(CONFIG_IPV6)
2639 } else {
2640 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2641 &dst->remote_ip.sin6.sin6_addr))
2642 goto nla_put_failure;
2643#endif
2644 }
2645 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002646
Atzm Watanabec7995c42013-04-16 02:50:52 +00002647 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002648 goto nla_put_failure;
2649
Cong Wange4c7ed42013-08-31 13:44:33 +08002650 if (!vxlan_addr_any(&vxlan->saddr)) {
2651 if (vxlan->saddr.sa.sa_family == AF_INET) {
2652 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2653 vxlan->saddr.sin.sin_addr.s_addr))
2654 goto nla_put_failure;
2655#if IS_ENABLED(CONFIG_IPV6)
2656 } else {
2657 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2658 &vxlan->saddr.sin6.sin6_addr))
2659 goto nla_put_failure;
2660#endif
2661 }
2662 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002663
2664 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2665 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002666 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2667 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2668 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2669 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2670 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2671 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2672 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2673 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2674 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002675 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002676 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002677 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2678 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2679 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2680 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2681 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2682 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2683 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002684 goto nla_put_failure;
2685
stephen hemminger05f47d62012-10-09 20:35:50 +00002686 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2687 goto nla_put_failure;
2688
stephen hemmingerd3428942012-10-01 12:32:35 +00002689 return 0;
2690
2691nla_put_failure:
2692 return -EMSGSIZE;
2693}
2694
2695static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2696 .kind = "vxlan",
2697 .maxtype = IFLA_VXLAN_MAX,
2698 .policy = vxlan_policy,
2699 .priv_size = sizeof(struct vxlan_dev),
2700 .setup = vxlan_setup,
2701 .validate = vxlan_validate,
2702 .newlink = vxlan_newlink,
2703 .dellink = vxlan_dellink,
2704 .get_size = vxlan_get_size,
2705 .fill_info = vxlan_fill_info,
2706};
2707
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002708static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2709 struct net_device *dev)
2710{
2711 struct vxlan_dev *vxlan, *next;
2712 LIST_HEAD(list_kill);
2713
2714 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2715 struct vxlan_rdst *dst = &vxlan->default_dst;
2716
2717 /* In case we created vxlan device with carrier
2718 * and we loose the carrier due to module unload
2719 * we also need to remove vxlan device. In other
2720 * cases, it's not necessary and remote_ifindex
2721 * is 0 here, so no matches.
2722 */
2723 if (dst->remote_ifindex == dev->ifindex)
2724 vxlan_dellink(vxlan->dev, &list_kill);
2725 }
2726
2727 unregister_netdevice_many(&list_kill);
2728}
2729
2730static int vxlan_lowerdev_event(struct notifier_block *unused,
2731 unsigned long event, void *ptr)
2732{
2733 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002734 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002735
Daniel Borkmann783c1462014-01-22 21:07:53 +01002736 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002737 vxlan_handle_lowerdev_unregister(vn, dev);
2738
2739 return NOTIFY_DONE;
2740}
2741
2742static struct notifier_block vxlan_notifier_block __read_mostly = {
2743 .notifier_call = vxlan_lowerdev_event,
2744};
2745
stephen hemmingerd3428942012-10-01 12:32:35 +00002746static __net_init int vxlan_init_net(struct net *net)
2747{
2748 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002749 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002750
stephen hemminger553675f2013-05-16 11:35:20 +00002751 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002752 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002753
stephen hemminger553675f2013-05-16 11:35:20 +00002754 for (h = 0; h < PORT_HASH_SIZE; ++h)
2755 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002756
2757 return 0;
2758}
2759
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002760static void __net_exit vxlan_exit_net(struct net *net)
2761{
2762 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2763 struct vxlan_dev *vxlan, *next;
2764 struct net_device *dev, *aux;
2765 LIST_HEAD(list);
2766
2767 rtnl_lock();
2768 for_each_netdev_safe(net, dev, aux)
2769 if (dev->rtnl_link_ops == &vxlan_link_ops)
2770 unregister_netdevice_queue(dev, &list);
2771
2772 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2773 /* If vxlan->dev is in the same netns, it has already been added
2774 * to the list by the previous loop.
2775 */
2776 if (!net_eq(dev_net(vxlan->dev), net))
2777 unregister_netdevice_queue(dev, &list);
2778 }
2779
2780 unregister_netdevice_many(&list);
2781 rtnl_unlock();
2782}
2783
stephen hemmingerd3428942012-10-01 12:32:35 +00002784static struct pernet_operations vxlan_net_ops = {
2785 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002786 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002787 .id = &vxlan_net_id,
2788 .size = sizeof(struct vxlan_net),
2789};
2790
2791static int __init vxlan_init_module(void)
2792{
2793 int rc;
2794
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002795 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2796 if (!vxlan_wq)
2797 return -ENOMEM;
2798
stephen hemmingerd3428942012-10-01 12:32:35 +00002799 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2800
Daniel Borkmann783c1462014-01-22 21:07:53 +01002801 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00002802 if (rc)
2803 goto out1;
2804
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002805 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002806 if (rc)
2807 goto out2;
2808
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002809 rc = rtnl_link_register(&vxlan_link_ops);
2810 if (rc)
2811 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00002812
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002813 return 0;
2814out3:
2815 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002816out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01002817 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00002818out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002819 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002820 return rc;
2821}
Cong Wang7332a132013-05-27 22:35:53 +00002822late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002823
2824static void __exit vxlan_cleanup_module(void)
2825{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002826 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002827 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002828 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002829 unregister_pernet_subsys(&vxlan_net_ops);
2830 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00002831}
2832module_exit(vxlan_cleanup_module);
2833
2834MODULE_LICENSE("GPL");
2835MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002836MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08002837MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00002838MODULE_ALIAS_RTNL_LINK("vxlan");