blob: 64d45fa3d99787eb64c05cd36d3931b7e8cd545f [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
70/* VXLAN protocol header */
71struct vxlanhdr {
72 __be32 vx_flags;
73 __be32 vx_vni;
74};
75
stephen hemminger23c578b2013-04-27 11:31:53 +000076/* UDP port for VXLAN traffic.
77 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070078 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000079 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070080static unsigned short vxlan_port __read_mostly = 8472;
81module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000082MODULE_PARM_DESC(udp_port, "Destination UDP port");
83
84static bool log_ecn_error = true;
85module_param(log_ecn_error, bool, 0644);
86MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
87
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070088static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000089
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030090static const u8 all_zeros_mac[ETH_ALEN];
91
stephen hemminger553675f2013-05-16 11:35:20 +000092/* per-network namespace private data for this module */
93struct vxlan_net {
94 struct list_head vxlan_list;
95 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070096 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000097};
98
Cong Wange4c7ed42013-08-31 13:44:33 +080099union vxlan_addr {
100 struct sockaddr_in sin;
101 struct sockaddr_in6 sin6;
102 struct sockaddr sa;
103};
104
David Stevens66817122013-03-15 04:35:51 +0000105struct vxlan_rdst {
Cong Wange4c7ed42013-08-31 13:44:33 +0800106 union vxlan_addr remote_ip;
David Stevens66817122013-03-15 04:35:51 +0000107 __be16 remote_port;
108 u32 remote_vni;
109 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700110 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300111 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000112};
113
stephen hemmingerd3428942012-10-01 12:32:35 +0000114/* Forwarding table entry */
115struct vxlan_fdb {
116 struct hlist_node hlist; /* linked list of entries */
117 struct rcu_head rcu;
118 unsigned long updated; /* jiffies */
119 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700120 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000121 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000122 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000123 u8 eth_addr[ETH_ALEN];
124};
125
stephen hemmingerd3428942012-10-01 12:32:35 +0000126/* Pseudo network device */
127struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000128 struct hlist_node hlist; /* vni hash table */
129 struct list_head next; /* vxlan's per namespace list */
130 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000131 struct net_device *dev;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +0200132 struct net *net; /* netns for packet i/o */
Atzm Watanabec7995c42013-04-16 02:50:52 +0000133 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800134 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000135 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000136 __u16 port_min; /* source port range */
137 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000138 __u8 tos; /* TOS override */
139 __u8 ttl;
Tom Herbert359a0ea2014-06-04 17:20:29 -0700140 u32 flags; /* VXLAN_F_* in vxlan.h */
stephen hemmingerd3428942012-10-01 12:32:35 +0000141
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700142 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700143 struct work_struct igmp_join;
144 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700145
stephen hemmingerd3428942012-10-01 12:32:35 +0000146 unsigned long age_interval;
147 struct timer_list age_timer;
148 spinlock_t hash_lock;
149 unsigned int addrcnt;
150 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000151
152 struct hlist_head fdb_head[FDB_HASH_SIZE];
153};
154
155/* salt for hash table */
156static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700157static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000158
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700159static void vxlan_sock_work(struct work_struct *work);
160
Cong Wange4c7ed42013-08-31 13:44:33 +0800161#if IS_ENABLED(CONFIG_IPV6)
162static inline
163bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
164{
165 if (a->sa.sa_family != b->sa.sa_family)
166 return false;
167 if (a->sa.sa_family == AF_INET6)
168 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
169 else
170 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
171}
172
173static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
174{
175 if (ipa->sa.sa_family == AF_INET6)
176 return ipv6_addr_any(&ipa->sin6.sin6_addr);
177 else
178 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
179}
180
181static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
182{
183 if (ipa->sa.sa_family == AF_INET6)
184 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
185 else
186 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
187}
188
189static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
190{
191 if (nla_len(nla) >= sizeof(struct in6_addr)) {
192 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
193 ip->sa.sa_family = AF_INET6;
194 return 0;
195 } else if (nla_len(nla) >= sizeof(__be32)) {
196 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
197 ip->sa.sa_family = AF_INET;
198 return 0;
199 } else {
200 return -EAFNOSUPPORT;
201 }
202}
203
204static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
205 const union vxlan_addr *ip)
206{
207 if (ip->sa.sa_family == AF_INET6)
208 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
209 else
210 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
211}
212
213#else /* !CONFIG_IPV6 */
214
215static inline
216bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
217{
218 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
219}
220
221static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
222{
223 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
224}
225
226static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
227{
228 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
229}
230
231static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
232{
233 if (nla_len(nla) >= sizeof(struct in6_addr)) {
234 return -EAFNOSUPPORT;
235 } else if (nla_len(nla) >= sizeof(__be32)) {
236 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
237 ip->sa.sa_family = AF_INET;
238 return 0;
239 } else {
240 return -EAFNOSUPPORT;
241 }
242}
243
244static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
245 const union vxlan_addr *ip)
246{
247 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
248}
249#endif
250
stephen hemminger553675f2013-05-16 11:35:20 +0000251/* Virtual Network hash table head */
252static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
253{
254 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
255}
256
257/* Socket hash table head */
258static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000259{
260 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
261
stephen hemminger553675f2013-05-16 11:35:20 +0000262 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
263}
264
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700265/* First remote destination for a forwarding entry.
266 * Guaranteed to be non-NULL because remotes are never deleted.
267 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700268static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700269{
stephen hemminger5ca54612013-08-04 17:17:39 -0700270 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
271}
272
273static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
274{
275 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700276}
277
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200278/* Find VXLAN socket based on network namespace, address family and UDP port */
279static struct vxlan_sock *vxlan_find_sock(struct net *net,
280 sa_family_t family, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000281{
282 struct vxlan_sock *vs;
283
284 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200285 if (inet_sk(vs->sock->sk)->inet_sport == port &&
286 inet_sk(vs->sock->sk)->sk.sk_family == family)
stephen hemminger553675f2013-05-16 11:35:20 +0000287 return vs;
288 }
289 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000290}
291
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700292static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000293{
294 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000295
stephen hemminger553675f2013-05-16 11:35:20 +0000296 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000297 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000298 return vxlan;
299 }
300
301 return NULL;
302}
303
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700304/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200305static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
306 sa_family_t family, __be16 port)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700307{
308 struct vxlan_sock *vs;
309
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200310 vs = vxlan_find_sock(net, family, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700311 if (!vs)
312 return NULL;
313
314 return vxlan_vs_find_vni(vs, id);
315}
316
stephen hemmingerd3428942012-10-01 12:32:35 +0000317/* Fill in neighbour message in skbuff. */
318static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700319 const struct vxlan_fdb *fdb,
320 u32 portid, u32 seq, int type, unsigned int flags,
321 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000322{
323 unsigned long now = jiffies;
324 struct nda_cacheinfo ci;
325 struct nlmsghdr *nlh;
326 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000327 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000328
329 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
330 if (nlh == NULL)
331 return -EMSGSIZE;
332
333 ndm = nlmsg_data(nlh);
334 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000335
336 send_eth = send_ip = true;
337
338 if (type == RTM_GETNEIGH) {
339 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800340 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000341 send_eth = !is_zero_ether_addr(fdb->eth_addr);
342 } else
343 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000344 ndm->ndm_state = fdb->state;
345 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000346 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800347 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000348
David Stevense4f67ad2012-11-20 02:50:14 +0000349 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000350 goto nla_put_failure;
351
Cong Wange4c7ed42013-08-31 13:44:33 +0800352 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000353 goto nla_put_failure;
354
stephen hemminger823aa872013-04-27 11:31:57 +0000355 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000356 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
357 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000358 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700359 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000360 goto nla_put_failure;
361 if (rdst->remote_ifindex &&
362 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000363 goto nla_put_failure;
364
365 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
366 ci.ndm_confirmed = 0;
367 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
368 ci.ndm_refcnt = 0;
369
370 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
371 goto nla_put_failure;
372
373 return nlmsg_end(skb, nlh);
374
375nla_put_failure:
376 nlmsg_cancel(skb, nlh);
377 return -EMSGSIZE;
378}
379
380static inline size_t vxlan_nlmsg_size(void)
381{
382 return NLMSG_ALIGN(sizeof(struct ndmsg))
383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000388 + nla_total_size(sizeof(struct nda_cacheinfo));
389}
390
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200391static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
392 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000393{
394 struct net *net = dev_net(vxlan->dev);
395 struct sk_buff *skb;
396 int err = -ENOBUFS;
397
398 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
399 if (skb == NULL)
400 goto errout;
401
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200402 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000403 if (err < 0) {
404 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
405 WARN_ON(err == -EMSGSIZE);
406 kfree_skb(skb);
407 goto errout;
408 }
409
410 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
411 return;
412errout:
413 if (err < 0)
414 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
415}
416
Cong Wange4c7ed42013-08-31 13:44:33 +0800417static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000418{
419 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700420 struct vxlan_fdb f = {
421 .state = NUD_STALE,
422 };
423 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800424 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700425 .remote_vni = VXLAN_N_VID,
426 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700427
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200428 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000429}
430
431static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
432{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700433 struct vxlan_fdb f = {
434 .state = NUD_STALE,
435 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200436 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000437
David Stevense4f67ad2012-11-20 02:50:14 +0000438 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
439
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200440 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000441}
442
stephen hemmingerd3428942012-10-01 12:32:35 +0000443/* Hash Ethernet address */
444static u32 eth_hash(const unsigned char *addr)
445{
446 u64 value = get_unaligned((u64 *)addr);
447
448 /* only want 6 bytes */
449#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000450 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000451#else
452 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000453#endif
454 return hash_64(value, FDB_HASH_BITS);
455}
456
457/* Hash chain to use given mac address */
458static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
459 const u8 *mac)
460{
461 return &vxlan->fdb_head[eth_hash(mac)];
462}
463
464/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000465static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000466 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000467{
468 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
469 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000470
Sasha Levinb67bfe02013-02-27 17:06:00 -0800471 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700472 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000473 return f;
474 }
475
476 return NULL;
477}
478
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000479static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
480 const u8 *mac)
481{
482 struct vxlan_fdb *f;
483
484 f = __vxlan_find_mac(vxlan, mac);
485 if (f)
486 f->used = jiffies;
487
488 return f;
489}
490
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300491/* caller should hold vxlan->hash_lock */
492static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800493 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300494 __u32 vni, __u32 ifindex)
495{
496 struct vxlan_rdst *rd;
497
498 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800499 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300500 rd->remote_port == port &&
501 rd->remote_vni == vni &&
502 rd->remote_ifindex == ifindex)
503 return rd;
504 }
505
506 return NULL;
507}
508
Thomas Richter906dc182013-07-19 17:20:07 +0200509/* Replace destination of unicast mac */
510static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800511 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200512{
513 struct vxlan_rdst *rd;
514
515 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
516 if (rd)
517 return 0;
518
519 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
520 if (!rd)
521 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800522 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200523 rd->remote_port = port;
524 rd->remote_vni = vni;
525 rd->remote_ifindex = ifindex;
526 return 1;
527}
528
David Stevens66817122013-03-15 04:35:51 +0000529/* Add/update destinations for multicast */
530static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200531 union vxlan_addr *ip, __be16 port, __u32 vni,
532 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000533{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700534 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000535
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300536 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
537 if (rd)
538 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700539
David Stevens66817122013-03-15 04:35:51 +0000540 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
541 if (rd == NULL)
542 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800543 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000544 rd->remote_port = port;
545 rd->remote_vni = vni;
546 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700547
548 list_add_tail_rcu(&rd->list, &f->remotes);
549
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200550 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000551 return 1;
552}
553
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200554static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, struct sk_buff *skb)
555{
556 struct sk_buff *p, **pp = NULL;
557 struct vxlanhdr *vh, *vh2;
558 struct ethhdr *eh, *eh2;
559 unsigned int hlen, off_vx, off_eth;
560 const struct packet_offload *ptype;
561 __be16 type;
562 int flush = 1;
563
564 off_vx = skb_gro_offset(skb);
565 hlen = off_vx + sizeof(*vh);
566 vh = skb_gro_header_fast(skb, off_vx);
567 if (skb_gro_header_hard(skb, hlen)) {
568 vh = skb_gro_header_slow(skb, hlen, off_vx);
569 if (unlikely(!vh))
570 goto out;
571 }
572 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700573 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200574
575 off_eth = skb_gro_offset(skb);
576 hlen = off_eth + sizeof(*eh);
577 eh = skb_gro_header_fast(skb, off_eth);
578 if (skb_gro_header_hard(skb, hlen)) {
579 eh = skb_gro_header_slow(skb, hlen, off_eth);
580 if (unlikely(!eh))
581 goto out;
582 }
583
584 flush = 0;
585
586 for (p = *head; p; p = p->next) {
587 if (!NAPI_GRO_CB(p)->same_flow)
588 continue;
589
590 vh2 = (struct vxlanhdr *)(p->data + off_vx);
591 eh2 = (struct ethhdr *)(p->data + off_eth);
592 if (vh->vx_vni != vh2->vx_vni || compare_ether_header(eh, eh2)) {
593 NAPI_GRO_CB(p)->same_flow = 0;
594 continue;
595 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200596 }
597
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200598 type = eh->h_proto;
599
600 rcu_read_lock();
601 ptype = gro_find_receive_by_type(type);
602 if (ptype == NULL) {
603 flush = 1;
604 goto out_unlock;
605 }
606
607 skb_gro_pull(skb, sizeof(*eh)); /* pull inner eth header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700608 skb_gro_postpull_rcsum(skb, eh, sizeof(*eh));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200609 pp = ptype->callbacks.gro_receive(head, skb);
610
611out_unlock:
612 rcu_read_unlock();
613out:
614 NAPI_GRO_CB(skb)->flush |= flush;
615
616 return pp;
617}
618
619static int vxlan_gro_complete(struct sk_buff *skb, int nhoff)
620{
621 struct ethhdr *eh;
622 struct packet_offload *ptype;
623 __be16 type;
624 int vxlan_len = sizeof(struct vxlanhdr) + sizeof(struct ethhdr);
625 int err = -ENOSYS;
626
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800627 udp_tunnel_gro_complete(skb, nhoff);
628
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200629 eh = (struct ethhdr *)(skb->data + nhoff + sizeof(struct vxlanhdr));
630 type = eh->h_proto;
631
632 rcu_read_lock();
633 ptype = gro_find_complete_by_type(type);
634 if (ptype != NULL)
635 err = ptype->callbacks.gro_complete(skb, nhoff + vxlan_len);
636
637 rcu_read_unlock();
638 return err;
639}
640
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700641/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200642static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700643{
644 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200645 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700646 struct net *net = sock_net(sk);
647 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700648 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200649 int err;
650
651 if (sa_family == AF_INET) {
652 err = udp_add_offload(&vs->udp_offloads);
653 if (err)
654 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
655 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700656
657 rcu_read_lock();
658 for_each_netdev_rcu(net, dev) {
659 if (dev->netdev_ops->ndo_add_vxlan_port)
660 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
661 port);
662 }
663 rcu_read_unlock();
664}
665
666/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200667static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700668{
669 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200670 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700671 struct net *net = sock_net(sk);
672 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700673 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700674
675 rcu_read_lock();
676 for_each_netdev_rcu(net, dev) {
677 if (dev->netdev_ops->ndo_del_vxlan_port)
678 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
679 port);
680 }
681 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200682
683 if (sa_family == AF_INET)
684 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700685}
686
stephen hemmingerd3428942012-10-01 12:32:35 +0000687/* Add new entry to forwarding table -- assumes lock held */
688static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800689 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000690 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000691 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000692 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000693{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200694 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000695 struct vxlan_fdb *f;
696 int notify = 0;
697
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000698 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000699 if (f) {
700 if (flags & NLM_F_EXCL) {
701 netdev_dbg(vxlan->dev,
702 "lost race to create %pM\n", mac);
703 return -EEXIST;
704 }
705 if (f->state != state) {
706 f->state = state;
707 f->updated = jiffies;
708 notify = 1;
709 }
David Stevensae884082013-04-19 00:36:26 +0000710 if (f->flags != ndm_flags) {
711 f->flags = ndm_flags;
712 f->updated = jiffies;
713 notify = 1;
714 }
Thomas Richter906dc182013-07-19 17:20:07 +0200715 if ((flags & NLM_F_REPLACE)) {
716 /* Only change unicasts */
717 if (!(is_multicast_ether_addr(f->eth_addr) ||
718 is_zero_ether_addr(f->eth_addr))) {
719 int rc = vxlan_fdb_replace(f, ip, port, vni,
720 ifindex);
721
722 if (rc < 0)
723 return rc;
724 notify |= rc;
725 } else
726 return -EOPNOTSUPP;
727 }
David Stevens66817122013-03-15 04:35:51 +0000728 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300729 (is_multicast_ether_addr(f->eth_addr) ||
730 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200731 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
732 &rd);
David Stevens66817122013-03-15 04:35:51 +0000733
734 if (rc < 0)
735 return rc;
736 notify |= rc;
737 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000738 } else {
739 if (!(flags & NLM_F_CREATE))
740 return -ENOENT;
741
742 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
743 return -ENOSPC;
744
Thomas Richter906dc182013-07-19 17:20:07 +0200745 /* Disallow replace to add a multicast entry */
746 if ((flags & NLM_F_REPLACE) &&
747 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
748 return -EOPNOTSUPP;
749
Cong Wange4c7ed42013-08-31 13:44:33 +0800750 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000751 f = kmalloc(sizeof(*f), GFP_ATOMIC);
752 if (!f)
753 return -ENOMEM;
754
755 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000756 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000757 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000758 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700759 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000760 memcpy(f->eth_addr, mac, ETH_ALEN);
761
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200762 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700763
stephen hemmingerd3428942012-10-01 12:32:35 +0000764 ++vxlan->addrcnt;
765 hlist_add_head_rcu(&f->hlist,
766 vxlan_fdb_head(vxlan, mac));
767 }
768
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200769 if (notify) {
770 if (rd == NULL)
771 rd = first_remote_rtnl(f);
772 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
773 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000774
775 return 0;
776}
777
Wei Yongjun6706c822013-04-11 19:00:35 +0000778static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000779{
780 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700781 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000782
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700783 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000784 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000785 kfree(f);
786}
787
stephen hemmingerd3428942012-10-01 12:32:35 +0000788static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
789{
790 netdev_dbg(vxlan->dev,
791 "delete %pM\n", f->eth_addr);
792
793 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200794 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000795
796 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000797 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000798}
799
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300800static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800801 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300802{
803 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800804 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300805
806 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800807 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
808 if (err)
809 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300810 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800811 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
812 if (remote->sa.sa_family == AF_INET) {
813 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
814 ip->sa.sa_family = AF_INET;
815#if IS_ENABLED(CONFIG_IPV6)
816 } else {
817 ip->sin6.sin6_addr = in6addr_any;
818 ip->sa.sa_family = AF_INET6;
819#endif
820 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300821 }
822
823 if (tb[NDA_PORT]) {
824 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
825 return -EINVAL;
826 *port = nla_get_be16(tb[NDA_PORT]);
827 } else {
828 *port = vxlan->dst_port;
829 }
830
831 if (tb[NDA_VNI]) {
832 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
833 return -EINVAL;
834 *vni = nla_get_u32(tb[NDA_VNI]);
835 } else {
836 *vni = vxlan->default_dst.remote_vni;
837 }
838
839 if (tb[NDA_IFINDEX]) {
840 struct net_device *tdev;
841
842 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
843 return -EINVAL;
844 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800845 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300846 if (!tdev)
847 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300848 } else {
849 *ifindex = 0;
850 }
851
852 return 0;
853}
854
stephen hemmingerd3428942012-10-01 12:32:35 +0000855/* Add static entry (via netlink) */
856static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
857 struct net_device *dev,
858 const unsigned char *addr, u16 flags)
859{
860 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300861 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800862 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000863 __be16 port;
864 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000865 int err;
866
867 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
868 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
869 ndm->ndm_state);
870 return -EINVAL;
871 }
872
873 if (tb[NDA_DST] == NULL)
874 return -EINVAL;
875
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300876 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
877 if (err)
878 return err;
David Stevens66817122013-03-15 04:35:51 +0000879
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300880 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
881 return -EAFNOSUPPORT;
882
stephen hemmingerd3428942012-10-01 12:32:35 +0000883 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800884 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000885 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000886 spin_unlock_bh(&vxlan->hash_lock);
887
888 return err;
889}
890
891/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000892static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
893 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000894 const unsigned char *addr)
895{
896 struct vxlan_dev *vxlan = netdev_priv(dev);
897 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300898 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800899 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300900 __be16 port;
901 u32 vni, ifindex;
902 int err;
903
904 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
905 if (err)
906 return err;
907
908 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000909
910 spin_lock_bh(&vxlan->hash_lock);
911 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300912 if (!f)
913 goto out;
914
Cong Wange4c7ed42013-08-31 13:44:33 +0800915 if (!vxlan_addr_any(&ip)) {
916 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300917 if (!rd)
918 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000919 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300920
921 err = 0;
922
923 /* remove a destination if it's not the only one on the list,
924 * otherwise destroy the fdb entry
925 */
926 if (rd && !list_is_singular(&f->remotes)) {
927 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200928 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800929 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300930 goto out;
931 }
932
933 vxlan_fdb_destroy(vxlan, f);
934
935out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000936 spin_unlock_bh(&vxlan->hash_lock);
937
938 return err;
939}
940
941/* Dump forwarding table */
942static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400943 struct net_device *dev,
944 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000945{
946 struct vxlan_dev *vxlan = netdev_priv(dev);
947 unsigned int h;
948
949 for (h = 0; h < FDB_HASH_SIZE; ++h) {
950 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000951 int err;
952
Sasha Levinb67bfe02013-02-27 17:06:00 -0800953 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000954 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000955
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700956 if (idx < cb->args[0])
957 goto skip;
958
959 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000960 err = vxlan_fdb_info(skb, vxlan, f,
961 NETLINK_CB(cb->skb).portid,
962 cb->nlh->nlmsg_seq,
963 RTM_NEWNEIGH,
964 NLM_F_MULTI, rd);
965 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700966 goto out;
David Stevens66817122013-03-15 04:35:51 +0000967 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700968skip:
969 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000970 }
971 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700972out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000973 return idx;
974}
975
976/* Watch incoming packets to learn mapping between Ethernet address
977 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700978 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000979 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700980static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800981 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000982{
983 struct vxlan_dev *vxlan = netdev_priv(dev);
984 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000985
986 f = vxlan_find_mac(vxlan, src_mac);
987 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700988 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700989
Cong Wange4c7ed42013-08-31 13:44:33 +0800990 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700991 return false;
992
993 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700994 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700995 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000996
997 if (net_ratelimit())
998 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800999 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001000 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +00001001
Cong Wange4c7ed42013-08-31 13:44:33 +08001002 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001003 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001004 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001005 } else {
1006 /* learned new entry */
1007 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001008
1009 /* close off race between vxlan_flush and incoming packets */
1010 if (netif_running(dev))
1011 vxlan_fdb_create(vxlan, src_mac, src_ip,
1012 NUD_REACHABLE,
1013 NLM_F_EXCL|NLM_F_CREATE,
1014 vxlan->dst_port,
1015 vxlan->default_dst.remote_vni,
1016 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001017 spin_unlock(&vxlan->hash_lock);
1018 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001019
1020 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001021}
1022
stephen hemmingerd3428942012-10-01 12:32:35 +00001023/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001024static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001025{
stephen hemminger553675f2013-05-16 11:35:20 +00001026 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001027
Gao feng95ab0992013-12-10 16:37:33 +08001028 /* The vxlan_sock is only used by dev, leaving group has
1029 * no effect on other vxlan devices.
1030 */
1031 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1032 return false;
1033
stephen hemminger553675f2013-05-16 11:35:20 +00001034 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001035 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001036 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001037
Gao feng95ab0992013-12-10 16:37:33 +08001038 if (vxlan->vn_sock != dev->vn_sock)
1039 continue;
1040
1041 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1042 &dev->default_dst.remote_ip))
1043 continue;
1044
1045 if (vxlan->default_dst.remote_ifindex !=
1046 dev->default_dst.remote_ifindex)
1047 continue;
1048
1049 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001050 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001051
1052 return false;
1053}
1054
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001055static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001056{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001057 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001058}
1059
Pravin B Shelar012a5722013-08-19 11:23:07 -07001060void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001061{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001062 struct sock *sk = vs->sock->sk;
1063 struct net *net = sock_net(sk);
1064 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001065
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001066 if (!atomic_dec_and_test(&vs->refcnt))
1067 return;
1068
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001069 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001070 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001071 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001072 spin_unlock(&vn->sock_lock);
1073
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001074 queue_work(vxlan_wq, &vs->del_work);
1075}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001076EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001077
stephen hemminger3fc2de22013-07-18 08:40:15 -07001078/* Callback to update multicast group membership when first VNI on
1079 * multicast asddress is brought up
1080 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001081 */
stephen hemminger3fc2de22013-07-18 08:40:15 -07001082static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001083{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001084 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001085 struct vxlan_sock *vs = vxlan->vn_sock;
1086 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001087 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1088 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001089
stephen hemmingerd3428942012-10-01 12:32:35 +00001090 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001091 if (ip->sa.sa_family == AF_INET) {
1092 struct ip_mreqn mreq = {
1093 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1094 .imr_ifindex = ifindex,
1095 };
1096
1097 ip_mc_join_group(sk, &mreq);
1098#if IS_ENABLED(CONFIG_IPV6)
1099 } else {
1100 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1101 &ip->sin6.sin6_addr);
1102#endif
1103 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001104 release_sock(sk);
1105
Pravin B Shelar012a5722013-08-19 11:23:07 -07001106 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001107 dev_put(vxlan->dev);
1108}
1109
1110/* Inverse of vxlan_igmp_join when last VNI is brought down */
1111static void vxlan_igmp_leave(struct work_struct *work)
1112{
1113 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001114 struct vxlan_sock *vs = vxlan->vn_sock;
1115 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001116 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1117 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001118
1119 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001120 if (ip->sa.sa_family == AF_INET) {
1121 struct ip_mreqn mreq = {
1122 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1123 .imr_ifindex = ifindex,
1124 };
1125
1126 ip_mc_leave_group(sk, &mreq);
1127#if IS_ENABLED(CONFIG_IPV6)
1128 } else {
1129 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1130 &ip->sin6.sin6_addr);
1131#endif
1132 }
1133
stephen hemmingerd3428942012-10-01 12:32:35 +00001134 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001135
Pravin B Shelar012a5722013-08-19 11:23:07 -07001136 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001137 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001138}
1139
1140/* Callback from net/ipv4/udp.c to receive packets */
1141static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1142{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001143 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001144 struct vxlanhdr *vxh;
stephen hemmingerd3428942012-10-01 12:32:35 +00001145
stephen hemmingerd3428942012-10-01 12:32:35 +00001146 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001147 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001148 goto error;
1149
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001150 /* Return packets with reserved bits set */
1151 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001152 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1153 (vxh->vx_vni & htonl(0xff))) {
1154 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1155 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1156 goto error;
1157 }
1158
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001159 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001160 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001161
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001162 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001163 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001164 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001165
1166 vs->rcv(vs, skb, vxh->vx_vni);
1167 return 0;
1168
1169drop:
1170 /* Consume bad packet */
1171 kfree_skb(skb);
1172 return 0;
1173
1174error:
1175 /* Return non vxlan pkt */
1176 return 1;
1177}
1178
1179static void vxlan_rcv(struct vxlan_sock *vs,
1180 struct sk_buff *skb, __be32 vx_vni)
1181{
Cong Wange4c7ed42013-08-31 13:44:33 +08001182 struct iphdr *oip = NULL;
1183 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001184 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001185 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001186 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001187 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001188 int err = 0;
1189 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001190
1191 vni = ntohl(vx_vni) >> 8;
1192 /* Is this VNI defined? */
1193 vxlan = vxlan_vs_find_vni(vs, vni);
1194 if (!vxlan)
1195 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001196
Cong Wange4c7ed42013-08-31 13:44:33 +08001197 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001198 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001199 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001200 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001201 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001202
1203 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001204 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001205 goto drop;
1206
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001207 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001208 if (remote_ip->sa.sa_family == AF_INET) {
1209 oip = ip_hdr(skb);
1210 saddr.sin.sin_addr.s_addr = oip->saddr;
1211 saddr.sa.sa_family = AF_INET;
1212#if IS_ENABLED(CONFIG_IPV6)
1213 } else {
1214 oip6 = ipv6_hdr(skb);
1215 saddr.sin6.sin6_addr = oip6->saddr;
1216 saddr.sa.sa_family = AF_INET6;
1217#endif
1218 }
1219
stephen hemminger26a41ae62013-06-17 12:09:58 -07001220 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001221 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001222 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001223
stephen hemmingerd3428942012-10-01 12:32:35 +00001224 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001225
Cong Wange4c7ed42013-08-31 13:44:33 +08001226 if (oip6)
1227 err = IP6_ECN_decapsulate(oip6, skb);
1228 if (oip)
1229 err = IP_ECN_decapsulate(oip, skb);
1230
stephen hemmingerd3428942012-10-01 12:32:35 +00001231 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001232 if (log_ecn_error) {
1233 if (oip6)
1234 net_info_ratelimited("non-ECT from %pI6\n",
1235 &oip6->saddr);
1236 if (oip)
1237 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1238 &oip->saddr, oip->tos);
1239 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001240 if (err > 1) {
1241 ++vxlan->dev->stats.rx_frame_errors;
1242 ++vxlan->dev->stats.rx_errors;
1243 goto drop;
1244 }
1245 }
1246
Pravin B Shelare8171042013-03-25 14:49:46 +00001247 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001248 u64_stats_update_begin(&stats->syncp);
1249 stats->rx_packets++;
1250 stats->rx_bytes += skb->len;
1251 u64_stats_update_end(&stats->syncp);
1252
1253 netif_rx(skb);
1254
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001255 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001256drop:
1257 /* Consume bad packet */
1258 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001259}
1260
David Stevense4f67ad2012-11-20 02:50:14 +00001261static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1262{
1263 struct vxlan_dev *vxlan = netdev_priv(dev);
1264 struct arphdr *parp;
1265 u8 *arpptr, *sha;
1266 __be32 sip, tip;
1267 struct neighbour *n;
1268
1269 if (dev->flags & IFF_NOARP)
1270 goto out;
1271
1272 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1273 dev->stats.tx_dropped++;
1274 goto out;
1275 }
1276 parp = arp_hdr(skb);
1277
1278 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1279 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1280 parp->ar_pro != htons(ETH_P_IP) ||
1281 parp->ar_op != htons(ARPOP_REQUEST) ||
1282 parp->ar_hln != dev->addr_len ||
1283 parp->ar_pln != 4)
1284 goto out;
1285 arpptr = (u8 *)parp + sizeof(struct arphdr);
1286 sha = arpptr;
1287 arpptr += dev->addr_len; /* sha */
1288 memcpy(&sip, arpptr, sizeof(sip));
1289 arpptr += sizeof(sip);
1290 arpptr += dev->addr_len; /* tha */
1291 memcpy(&tip, arpptr, sizeof(tip));
1292
1293 if (ipv4_is_loopback(tip) ||
1294 ipv4_is_multicast(tip))
1295 goto out;
1296
1297 n = neigh_lookup(&arp_tbl, &tip, dev);
1298
1299 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001300 struct vxlan_fdb *f;
1301 struct sk_buff *reply;
1302
1303 if (!(n->nud_state & NUD_CONNECTED)) {
1304 neigh_release(n);
1305 goto out;
1306 }
1307
1308 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001309 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001310 /* bridge-local neighbor */
1311 neigh_release(n);
1312 goto out;
1313 }
1314
1315 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1316 n->ha, sha);
1317
1318 neigh_release(n);
1319
David Stevens73461352014-03-18 12:32:29 -04001320 if (reply == NULL)
1321 goto out;
1322
David Stevense4f67ad2012-11-20 02:50:14 +00001323 skb_reset_mac_header(reply);
1324 __skb_pull(reply, skb_network_offset(reply));
1325 reply->ip_summed = CHECKSUM_UNNECESSARY;
1326 reply->pkt_type = PACKET_HOST;
1327
1328 if (netif_rx_ni(reply) == NET_RX_DROP)
1329 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001330 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1331 union vxlan_addr ipa = {
1332 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001333 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001334 };
1335
1336 vxlan_ip_miss(dev, &ipa);
1337 }
David Stevense4f67ad2012-11-20 02:50:14 +00001338out:
1339 consume_skb(skb);
1340 return NETDEV_TX_OK;
1341}
1342
Cong Wangf564f452013-08-31 13:44:36 +08001343#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001344static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1345 struct neighbour *n, bool isrouter)
1346{
1347 struct net_device *dev = request->dev;
1348 struct sk_buff *reply;
1349 struct nd_msg *ns, *na;
1350 struct ipv6hdr *pip6;
1351 u8 *daddr;
1352 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1353 int ns_olen;
1354 int i, len;
1355
1356 if (dev == NULL)
1357 return NULL;
1358
1359 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1360 sizeof(*na) + na_olen + dev->needed_tailroom;
1361 reply = alloc_skb(len, GFP_ATOMIC);
1362 if (reply == NULL)
1363 return NULL;
1364
1365 reply->protocol = htons(ETH_P_IPV6);
1366 reply->dev = dev;
1367 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1368 skb_push(reply, sizeof(struct ethhdr));
1369 skb_set_mac_header(reply, 0);
1370
1371 ns = (struct nd_msg *)skb_transport_header(request);
1372
1373 daddr = eth_hdr(request)->h_source;
1374 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1375 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1376 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1377 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1378 break;
1379 }
1380 }
1381
1382 /* Ethernet header */
1383 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1384 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1385 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1386 reply->protocol = htons(ETH_P_IPV6);
1387
1388 skb_pull(reply, sizeof(struct ethhdr));
1389 skb_set_network_header(reply, 0);
1390 skb_put(reply, sizeof(struct ipv6hdr));
1391
1392 /* IPv6 header */
1393
1394 pip6 = ipv6_hdr(reply);
1395 memset(pip6, 0, sizeof(struct ipv6hdr));
1396 pip6->version = 6;
1397 pip6->priority = ipv6_hdr(request)->priority;
1398 pip6->nexthdr = IPPROTO_ICMPV6;
1399 pip6->hop_limit = 255;
1400 pip6->daddr = ipv6_hdr(request)->saddr;
1401 pip6->saddr = *(struct in6_addr *)n->primary_key;
1402
1403 skb_pull(reply, sizeof(struct ipv6hdr));
1404 skb_set_transport_header(reply, 0);
1405
1406 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1407
1408 /* Neighbor Advertisement */
1409 memset(na, 0, sizeof(*na)+na_olen);
1410 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1411 na->icmph.icmp6_router = isrouter;
1412 na->icmph.icmp6_override = 1;
1413 na->icmph.icmp6_solicited = 1;
1414 na->target = ns->target;
1415 ether_addr_copy(&na->opt[2], n->ha);
1416 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1417 na->opt[1] = na_olen >> 3;
1418
1419 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1420 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1421 csum_partial(na, sizeof(*na)+na_olen, 0));
1422
1423 pip6->payload_len = htons(sizeof(*na)+na_olen);
1424
1425 skb_push(reply, sizeof(struct ipv6hdr));
1426
1427 reply->ip_summed = CHECKSUM_UNNECESSARY;
1428
1429 return reply;
1430}
1431
Cong Wangf564f452013-08-31 13:44:36 +08001432static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1433{
1434 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001435 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001436 const struct ipv6hdr *iphdr;
1437 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001438 struct neighbour *n;
1439 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001440
1441 in6_dev = __in6_dev_get(dev);
1442 if (!in6_dev)
1443 goto out;
1444
Cong Wangf564f452013-08-31 13:44:36 +08001445 iphdr = ipv6_hdr(skb);
1446 saddr = &iphdr->saddr;
1447 daddr = &iphdr->daddr;
1448
Cong Wangf564f452013-08-31 13:44:36 +08001449 msg = (struct nd_msg *)skb_transport_header(skb);
1450 if (msg->icmph.icmp6_code != 0 ||
1451 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1452 goto out;
1453
David Stevens4b29dba2014-03-24 10:39:58 -04001454 if (ipv6_addr_loopback(daddr) ||
1455 ipv6_addr_is_multicast(&msg->target))
1456 goto out;
1457
1458 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001459
1460 if (n) {
1461 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001462 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001463
1464 if (!(n->nud_state & NUD_CONNECTED)) {
1465 neigh_release(n);
1466 goto out;
1467 }
1468
1469 f = vxlan_find_mac(vxlan, n->ha);
1470 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1471 /* bridge-local neighbor */
1472 neigh_release(n);
1473 goto out;
1474 }
1475
David Stevens4b29dba2014-03-24 10:39:58 -04001476 reply = vxlan_na_create(skb, n,
1477 !!(f ? f->flags & NTF_ROUTER : 0));
1478
Cong Wangf564f452013-08-31 13:44:36 +08001479 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001480
1481 if (reply == NULL)
1482 goto out;
1483
1484 if (netif_rx_ni(reply) == NET_RX_DROP)
1485 dev->stats.rx_dropped++;
1486
Cong Wangf564f452013-08-31 13:44:36 +08001487 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001488 union vxlan_addr ipa = {
1489 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001490 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001491 };
1492
Cong Wangf564f452013-08-31 13:44:36 +08001493 vxlan_ip_miss(dev, &ipa);
1494 }
1495
1496out:
1497 consume_skb(skb);
1498 return NETDEV_TX_OK;
1499}
1500#endif
1501
David Stevense4f67ad2012-11-20 02:50:14 +00001502static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1503{
1504 struct vxlan_dev *vxlan = netdev_priv(dev);
1505 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001506
1507 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1508 return false;
1509
1510 n = NULL;
1511 switch (ntohs(eth_hdr(skb)->h_proto)) {
1512 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001513 {
1514 struct iphdr *pip;
1515
David Stevense4f67ad2012-11-20 02:50:14 +00001516 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1517 return false;
1518 pip = ip_hdr(skb);
1519 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001520 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1521 union vxlan_addr ipa = {
1522 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001523 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001524 };
1525
1526 vxlan_ip_miss(dev, &ipa);
1527 return false;
1528 }
1529
David Stevense4f67ad2012-11-20 02:50:14 +00001530 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001531 }
1532#if IS_ENABLED(CONFIG_IPV6)
1533 case ETH_P_IPV6:
1534 {
1535 struct ipv6hdr *pip6;
1536
1537 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1538 return false;
1539 pip6 = ipv6_hdr(skb);
1540 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1541 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1542 union vxlan_addr ipa = {
1543 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001544 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001545 };
1546
1547 vxlan_ip_miss(dev, &ipa);
1548 return false;
1549 }
1550
1551 break;
1552 }
1553#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001554 default:
1555 return false;
1556 }
1557
1558 if (n) {
1559 bool diff;
1560
Joe Perches7367d0b2013-09-01 11:51:23 -07001561 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001562 if (diff) {
1563 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1564 dev->addr_len);
1565 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1566 }
1567 neigh_release(n);
1568 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001569 }
1570
David Stevense4f67ad2012-11-20 02:50:14 +00001571 return false;
1572}
1573
Cong Wange4c7ed42013-08-31 13:44:33 +08001574#if IS_ENABLED(CONFIG_IPV6)
Nicolas Dichtel11796182013-09-02 15:34:55 +02001575static int vxlan6_xmit_skb(struct vxlan_sock *vs,
Cong Wange4c7ed42013-08-31 13:44:33 +08001576 struct dst_entry *dst, struct sk_buff *skb,
1577 struct net_device *dev, struct in6_addr *saddr,
1578 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001579 __be16 src_port, __be16 dst_port, __be32 vni,
1580 bool xnet)
Cong Wange4c7ed42013-08-31 13:44:33 +08001581{
Cong Wange4c7ed42013-08-31 13:44:33 +08001582 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001583 int min_headroom;
1584 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001585 bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001586
Andy Zhouacbf74a2014-09-16 17:31:18 -07001587 skb = udp_tunnel_handle_offloads(skb, udp_sum);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001588 if (IS_ERR(skb))
1589 return -EINVAL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001590
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001591 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001592
Cong Wange4c7ed42013-08-31 13:44:33 +08001593 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1594 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1595 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1596
1597 /* Need space for new headers (invalidates iph ptr) */
1598 err = skb_cow_head(skb, min_headroom);
1599 if (unlikely(err))
1600 return err;
1601
Jiri Pirko59682502014-11-19 14:04:59 +01001602 skb = vlan_hwaccel_push_inside(skb);
1603 if (WARN_ON(!skb))
1604 return -ENOMEM;
Cong Wange4c7ed42013-08-31 13:44:33 +08001605
1606 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1607 vxh->vx_flags = htonl(VXLAN_FLAGS);
1608 vxh->vx_vni = vni;
1609
Tom Herbert996c9fd2014-09-29 20:22:33 -07001610 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1611
Andy Zhouacbf74a2014-09-16 17:31:18 -07001612 udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
1613 ttl, src_port, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001614 return 0;
1615}
1616#endif
1617
Nicolas Dichtel11796182013-09-02 15:34:55 +02001618int vxlan_xmit_skb(struct vxlan_sock *vs,
Pravin B Shelar49560532013-08-19 11:23:17 -07001619 struct rtable *rt, struct sk_buff *skb,
1620 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001621 __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)
Pravin B Shelar49560532013-08-19 11:23:17 -07001622{
1623 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001624 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001625 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001626 bool udp_sum = !vs->sock->sk->sk_no_check_tx;
Pravin B Shelar49560532013-08-19 11:23:17 -07001627
Andy Zhouacbf74a2014-09-16 17:31:18 -07001628 skb = udp_tunnel_handle_offloads(skb, udp_sum);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001629 if (IS_ERR(skb))
1630 return -EINVAL;
Pravin B Shelar49560532013-08-19 11:23:17 -07001631
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001632 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001633 + VXLAN_HLEN + sizeof(struct iphdr)
1634 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001635
1636 /* Need space for new headers (invalidates iph ptr) */
1637 err = skb_cow_head(skb, min_headroom);
1638 if (unlikely(err))
1639 return err;
1640
Jiri Pirko59682502014-11-19 14:04:59 +01001641 skb = vlan_hwaccel_push_inside(skb);
1642 if (WARN_ON(!skb))
1643 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001644
Pravin B Shelar49560532013-08-19 11:23:17 -07001645 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1646 vxh->vx_flags = htonl(VXLAN_FLAGS);
1647 vxh->vx_vni = vni;
1648
Tom Herbert996c9fd2014-09-29 20:22:33 -07001649 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1650
Andy Zhouacbf74a2014-09-16 17:31:18 -07001651 return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
1652 ttl, df, src_port, dst_port, xnet);
Pravin B Shelar49560532013-08-19 11:23:17 -07001653}
1654EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1655
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001656/* Bypass encapsulation if the destination is local */
1657static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1658 struct vxlan_dev *dst_vxlan)
1659{
Li RongQing8f849852014-01-04 13:57:59 +08001660 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001661 union vxlan_addr loopback;
1662 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001663 struct net_device *dev = skb->dev;
1664 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001665
Li RongQing8f849852014-01-04 13:57:59 +08001666 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1667 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001668 skb->pkt_type = PACKET_HOST;
1669 skb->encapsulation = 0;
1670 skb->dev = dst_vxlan->dev;
1671 __skb_pull(skb, skb_network_offset(skb));
1672
Cong Wange4c7ed42013-08-31 13:44:33 +08001673 if (remote_ip->sa.sa_family == AF_INET) {
1674 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1675 loopback.sa.sa_family = AF_INET;
1676#if IS_ENABLED(CONFIG_IPV6)
1677 } else {
1678 loopback.sin6.sin6_addr = in6addr_loopback;
1679 loopback.sa.sa_family = AF_INET6;
1680#endif
1681 }
1682
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001683 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001684 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001685
1686 u64_stats_update_begin(&tx_stats->syncp);
1687 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001688 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001689 u64_stats_update_end(&tx_stats->syncp);
1690
1691 if (netif_rx(skb) == NET_RX_SUCCESS) {
1692 u64_stats_update_begin(&rx_stats->syncp);
1693 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001694 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001695 u64_stats_update_end(&rx_stats->syncp);
1696 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001697 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001698 }
1699}
1700
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001701static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1702 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001703{
1704 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001705 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001706 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001707 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001708 union vxlan_addr *dst;
1709 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001710 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001711 __be16 df = 0;
1712 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001713 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001714
stephen hemminger823aa872013-04-27 11:31:57 +00001715 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001716 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001717 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001718
Cong Wange4c7ed42013-08-31 13:44:33 +08001719 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001720 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001721 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001722 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001723 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001724 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001725 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001726 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001727
stephen hemmingerd3428942012-10-01 12:32:35 +00001728 old_iph = ip_hdr(skb);
1729
stephen hemmingerd3428942012-10-01 12:32:35 +00001730 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001731 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001732 ttl = 1;
1733
1734 tos = vxlan->tos;
1735 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001736 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001737
Tom Herbert535fb8d2014-07-01 21:32:49 -07001738 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1739 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001740
Cong Wange4c7ed42013-08-31 13:44:33 +08001741 if (dst->sa.sa_family == AF_INET) {
1742 memset(&fl4, 0, sizeof(fl4));
1743 fl4.flowi4_oif = rdst->remote_ifindex;
1744 fl4.flowi4_tos = RT_TOS(tos);
1745 fl4.daddr = dst->sin.sin_addr.s_addr;
1746 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001747
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001748 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001749 if (IS_ERR(rt)) {
1750 netdev_dbg(dev, "no route to %pI4\n",
1751 &dst->sin.sin_addr.s_addr);
1752 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001753 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001754 }
1755
1756 if (rt->dst.dev == dev) {
1757 netdev_dbg(dev, "circular route to %pI4\n",
1758 &dst->sin.sin_addr.s_addr);
1759 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001760 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001761 }
1762
1763 /* Bypass encapsulation if the destination is local */
1764 if (rt->rt_flags & RTCF_LOCAL &&
1765 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1766 struct vxlan_dev *dst_vxlan;
1767
1768 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001769 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1770 dst->sa.sa_family, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001771 if (!dst_vxlan)
1772 goto tx_error;
1773 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1774 return;
1775 }
1776
1777 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1778 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1779
Andy Zhou3c4d1da2014-09-23 01:44:51 -07001780 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1781 fl4.saddr, dst->sin.sin_addr.s_addr,
1782 tos, ttl, df, src_port, dst_port,
1783 htonl(vni << 8),
1784 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Cong Wange4c7ed42013-08-31 13:44:33 +08001785
1786 if (err < 0)
1787 goto rt_tx_error;
1788 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1789#if IS_ENABLED(CONFIG_IPV6)
1790 } else {
1791 struct sock *sk = vxlan->vn_sock->sock->sk;
1792 struct dst_entry *ndst;
1793 struct flowi6 fl6;
1794 u32 flags;
1795
1796 memset(&fl6, 0, sizeof(fl6));
1797 fl6.flowi6_oif = rdst->remote_ifindex;
1798 fl6.daddr = dst->sin6.sin6_addr;
1799 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08001800 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001801
1802 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1803 netdev_dbg(dev, "no route to %pI6\n",
1804 &dst->sin6.sin6_addr);
1805 dev->stats.tx_carrier_errors++;
1806 goto tx_error;
1807 }
1808
1809 if (ndst->dev == dev) {
1810 netdev_dbg(dev, "circular route to %pI6\n",
1811 &dst->sin6.sin6_addr);
1812 dst_release(ndst);
1813 dev->stats.collisions++;
1814 goto tx_error;
1815 }
1816
1817 /* Bypass encapsulation if the destination is local */
1818 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1819 if (flags & RTF_LOCAL &&
1820 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1821 struct vxlan_dev *dst_vxlan;
1822
1823 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001824 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1825 dst->sa.sa_family, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001826 if (!dst_vxlan)
1827 goto tx_error;
1828 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1829 return;
1830 }
1831
1832 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1833
Nicolas Dichtel11796182013-09-02 15:34:55 +02001834 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001835 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001836 src_port, dst_port, htonl(vni << 8),
1837 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Cong Wange4c7ed42013-08-31 13:44:33 +08001838#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001839 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001840
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001841 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001842
1843drop:
1844 dev->stats.tx_dropped++;
1845 goto tx_free;
1846
Pravin B Shelar49560532013-08-19 11:23:17 -07001847rt_tx_error:
1848 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001849tx_error:
1850 dev->stats.tx_errors++;
1851tx_free:
1852 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001853}
1854
David Stevens66817122013-03-15 04:35:51 +00001855/* Transmit local packets over Vxlan
1856 *
1857 * Outer IP header inherits ECN and DF from inner header.
1858 * Outer UDP destination is the VXLAN assigned port.
1859 * source port is based on hash of flow
1860 */
1861static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1862{
1863 struct vxlan_dev *vxlan = netdev_priv(dev);
1864 struct ethhdr *eth;
1865 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08001866 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00001867 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001868
1869 skb_reset_mac_header(skb);
1870 eth = eth_hdr(skb);
1871
Cong Wangf564f452013-08-31 13:44:36 +08001872 if ((vxlan->flags & VXLAN_F_PROXY)) {
1873 if (ntohs(eth->h_proto) == ETH_P_ARP)
1874 return arp_reduce(dev, skb);
1875#if IS_ENABLED(CONFIG_IPV6)
1876 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08001877 pskb_may_pull(skb, sizeof(struct ipv6hdr)
1878 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08001879 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1880 struct nd_msg *msg;
1881
1882 msg = (struct nd_msg *)skb_transport_header(skb);
1883 if (msg->icmph.icmp6_code == 0 &&
1884 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1885 return neigh_reduce(dev, skb);
1886 }
Li RongQing7a9f5262014-10-17 14:06:16 +08001887 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08001888#endif
1889 }
David Stevens66817122013-03-15 04:35:51 +00001890
1891 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001892 did_rsc = false;
1893
1894 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08001895 (ntohs(eth->h_proto) == ETH_P_IP ||
1896 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00001897 did_rsc = route_shortcircuit(dev, skb);
1898 if (did_rsc)
1899 f = vxlan_find_mac(vxlan, eth->h_dest);
1900 }
1901
David Stevens66817122013-03-15 04:35:51 +00001902 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001903 f = vxlan_find_mac(vxlan, all_zeros_mac);
1904 if (f == NULL) {
1905 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1906 !is_multicast_ether_addr(eth->h_dest))
1907 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001908
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001909 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08001910 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001911 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001912 }
David Stevens66817122013-03-15 04:35:51 +00001913 }
1914
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001915 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1916 struct sk_buff *skb1;
1917
Eric Dumazet8f646c92014-01-06 09:54:31 -08001918 if (!fdst) {
1919 fdst = rdst;
1920 continue;
1921 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001922 skb1 = skb_clone(skb, GFP_ATOMIC);
1923 if (skb1)
1924 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1925 }
1926
Eric Dumazet8f646c92014-01-06 09:54:31 -08001927 if (fdst)
1928 vxlan_xmit_one(skb, dev, fdst, did_rsc);
1929 else
1930 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001931 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001932}
1933
stephen hemmingerd3428942012-10-01 12:32:35 +00001934/* Walk the forwarding table and purge stale entries */
1935static void vxlan_cleanup(unsigned long arg)
1936{
1937 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1938 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1939 unsigned int h;
1940
1941 if (!netif_running(vxlan->dev))
1942 return;
1943
1944 spin_lock_bh(&vxlan->hash_lock);
1945 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1946 struct hlist_node *p, *n;
1947 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1948 struct vxlan_fdb *f
1949 = container_of(p, struct vxlan_fdb, hlist);
1950 unsigned long timeout;
1951
stephen hemminger3c172862012-10-26 06:24:34 +00001952 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001953 continue;
1954
1955 timeout = f->used + vxlan->age_interval * HZ;
1956 if (time_before_eq(timeout, jiffies)) {
1957 netdev_dbg(vxlan->dev,
1958 "garbage collect %pM\n",
1959 f->eth_addr);
1960 f->state = NUD_STALE;
1961 vxlan_fdb_destroy(vxlan, f);
1962 } else if (time_before(timeout, next_timer))
1963 next_timer = timeout;
1964 }
1965 }
1966 spin_unlock_bh(&vxlan->hash_lock);
1967
1968 mod_timer(&vxlan->age_timer, next_timer);
1969}
1970
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001971static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1972{
1973 __u32 vni = vxlan->default_dst.remote_vni;
1974
1975 vxlan->vn_sock = vs;
1976 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1977}
1978
stephen hemmingerd3428942012-10-01 12:32:35 +00001979/* Setup stats when device is created */
1980static int vxlan_init(struct net_device *dev)
1981{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001982 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001983 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001984 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001985 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001986
WANG Cong1c213bd2014-02-13 11:46:28 -08001987 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00001988 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001989 return -ENOMEM;
1990
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001991 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001992 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
1993 vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001994 if (vs) {
1995 /* If we have a socket with same port already, reuse it */
1996 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001997 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001998 } else {
1999 /* otherwise make new socket outside of RTNL */
2000 dev_hold(dev);
2001 queue_work(vxlan_wq, &vxlan->sock_work);
2002 }
2003 spin_unlock(&vn->sock_lock);
2004
stephen hemmingerd3428942012-10-01 12:32:35 +00002005 return 0;
2006}
2007
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002008static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002009{
2010 struct vxlan_fdb *f;
2011
2012 spin_lock_bh(&vxlan->hash_lock);
2013 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2014 if (f)
2015 vxlan_fdb_destroy(vxlan, f);
2016 spin_unlock_bh(&vxlan->hash_lock);
2017}
2018
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002019static void vxlan_uninit(struct net_device *dev)
2020{
2021 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002022 struct vxlan_sock *vs = vxlan->vn_sock;
2023
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002024 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002025
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002026 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07002027 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002028 free_percpu(dev->tstats);
2029}
2030
stephen hemmingerd3428942012-10-01 12:32:35 +00002031/* Start ageing timer and join group when device is brought up */
2032static int vxlan_open(struct net_device *dev)
2033{
2034 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002035 struct vxlan_sock *vs = vxlan->vn_sock;
2036
2037 /* socket hasn't been created */
2038 if (!vs)
2039 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002040
Gao feng79d4a942013-12-10 16:37:32 +08002041 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002042 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002043 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002044 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00002045 }
2046
2047 if (vxlan->age_interval)
2048 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2049
2050 return 0;
2051}
2052
2053/* Purge the forwarding table */
2054static void vxlan_flush(struct vxlan_dev *vxlan)
2055{
Cong Wang31fec5a2013-05-27 22:35:52 +00002056 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002057
2058 spin_lock_bh(&vxlan->hash_lock);
2059 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2060 struct hlist_node *p, *n;
2061 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2062 struct vxlan_fdb *f
2063 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002064 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2065 if (!is_zero_ether_addr(f->eth_addr))
2066 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002067 }
2068 }
2069 spin_unlock_bh(&vxlan->hash_lock);
2070}
2071
2072/* Cleanup timer and forwarding table on shutdown */
2073static int vxlan_stop(struct net_device *dev)
2074{
2075 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002076 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002077 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00002078
Cong Wange4c7ed42013-08-31 13:44:33 +08002079 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002080 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002081 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002082 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002083 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002084 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002085
2086 del_timer_sync(&vxlan->age_timer);
2087
2088 vxlan_flush(vxlan);
2089
2090 return 0;
2091}
2092
stephen hemmingerd3428942012-10-01 12:32:35 +00002093/* Stub, nothing needs to be done. */
2094static void vxlan_set_multicast_list(struct net_device *dev)
2095{
2096}
2097
Daniel Borkmann345010b2013-12-18 00:21:08 +01002098static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2099{
2100 struct vxlan_dev *vxlan = netdev_priv(dev);
2101 struct vxlan_rdst *dst = &vxlan->default_dst;
2102 struct net_device *lowerdev;
2103 int max_mtu;
2104
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002105 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002106 if (lowerdev == NULL)
2107 return eth_change_mtu(dev, new_mtu);
2108
2109 if (dst->remote_ip.sa.sa_family == AF_INET6)
2110 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2111 else
2112 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2113
2114 if (new_mtu < 68 || new_mtu > max_mtu)
2115 return -EINVAL;
2116
2117 dev->mtu = new_mtu;
2118 return 0;
2119}
2120
stephen hemmingerd3428942012-10-01 12:32:35 +00002121static const struct net_device_ops vxlan_netdev_ops = {
2122 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002123 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002124 .ndo_open = vxlan_open,
2125 .ndo_stop = vxlan_stop,
2126 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002127 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002128 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002129 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002130 .ndo_validate_addr = eth_validate_addr,
2131 .ndo_set_mac_address = eth_mac_addr,
2132 .ndo_fdb_add = vxlan_fdb_add,
2133 .ndo_fdb_del = vxlan_fdb_delete,
2134 .ndo_fdb_dump = vxlan_fdb_dump,
2135};
2136
2137/* Info for udev, that this is a virtual tunnel endpoint */
2138static struct device_type vxlan_type = {
2139 .name = "vxlan",
2140};
2141
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002142/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002143 * supply the listening VXLAN udp ports. Callers are expected
2144 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002145 */
2146void vxlan_get_rx_port(struct net_device *dev)
2147{
2148 struct vxlan_sock *vs;
2149 struct net *net = dev_net(dev);
2150 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2151 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002152 __be16 port;
2153 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002154
2155 spin_lock(&vn->sock_lock);
2156 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002157 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2158 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002159 sa_family = vs->sock->sk->sk_family;
2160 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2161 port);
2162 }
2163 }
2164 spin_unlock(&vn->sock_lock);
2165}
2166EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2167
stephen hemmingerd3428942012-10-01 12:32:35 +00002168/* Initialize the device structure. */
2169static void vxlan_setup(struct net_device *dev)
2170{
2171 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002172 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002173
2174 eth_hw_addr_random(dev);
2175 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002176 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002177 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002178 else
Cong Wang2853af62014-06-12 11:53:10 -07002179 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002180
2181 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002182 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002183 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2184
2185 dev->tx_queue_len = 0;
2186 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002187 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002188 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002189 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002190
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002191 dev->vlan_features = dev->features;
2192 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002193 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002194 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002195 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002196 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002197 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002198
stephen hemminger553675f2013-05-16 11:35:20 +00002199 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002200 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002201 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2202 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002203 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002204
2205 init_timer_deferrable(&vxlan->age_timer);
2206 vxlan->age_timer.function = vxlan_cleanup;
2207 vxlan->age_timer.data = (unsigned long) vxlan;
2208
stephen hemminger823aa872013-04-27 11:31:57 +00002209 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002210
stephen hemmingerd3428942012-10-01 12:32:35 +00002211 vxlan->dev = dev;
2212
2213 for (h = 0; h < FDB_HASH_SIZE; ++h)
2214 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2215}
2216
2217static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2218 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002219 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002220 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002221 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2222 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002223 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002224 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2225 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2226 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2227 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2228 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002229 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002230 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2231 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2232 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2233 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002234 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002235 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2236 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2237 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002238};
2239
2240static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2241{
2242 if (tb[IFLA_ADDRESS]) {
2243 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2244 pr_debug("invalid link address (not ethernet)\n");
2245 return -EINVAL;
2246 }
2247
2248 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2249 pr_debug("invalid all zero ethernet address\n");
2250 return -EADDRNOTAVAIL;
2251 }
2252 }
2253
2254 if (!data)
2255 return -EINVAL;
2256
2257 if (data[IFLA_VXLAN_ID]) {
2258 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2259 if (id >= VXLAN_VID_MASK)
2260 return -ERANGE;
2261 }
2262
stephen hemminger05f47d62012-10-09 20:35:50 +00002263 if (data[IFLA_VXLAN_PORT_RANGE]) {
2264 const struct ifla_vxlan_port_range *p
2265 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2266
2267 if (ntohs(p->high) < ntohs(p->low)) {
2268 pr_debug("port range %u .. %u not valid\n",
2269 ntohs(p->low), ntohs(p->high));
2270 return -EINVAL;
2271 }
2272 }
2273
stephen hemmingerd3428942012-10-01 12:32:35 +00002274 return 0;
2275}
2276
Yan Burman1b13c972013-01-29 23:43:07 +00002277static void vxlan_get_drvinfo(struct net_device *netdev,
2278 struct ethtool_drvinfo *drvinfo)
2279{
2280 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2281 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2282}
2283
2284static const struct ethtool_ops vxlan_ethtool_ops = {
2285 .get_drvinfo = vxlan_get_drvinfo,
2286 .get_link = ethtool_op_get_link,
2287};
2288
stephen hemminger553675f2013-05-16 11:35:20 +00002289static void vxlan_del_work(struct work_struct *work)
2290{
2291 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002292 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002293 kfree_rcu(vs, rcu);
2294}
2295
Tom Herbert3ee64f32014-07-13 19:49:42 -07002296static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2297 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002298{
Cong Wange4c7ed42013-08-31 13:44:33 +08002299 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002300 struct udp_port_cfg udp_conf;
2301 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002302
Tom Herbert3ee64f32014-07-13 19:49:42 -07002303 memset(&udp_conf, 0, sizeof(udp_conf));
2304
2305 if (ipv6) {
2306 udp_conf.family = AF_INET6;
2307 udp_conf.use_udp6_tx_checksums =
2308 !!(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2309 udp_conf.use_udp6_rx_checksums =
2310 !!(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2311 } else {
2312 udp_conf.family = AF_INET;
2313 udp_conf.local_ip.s_addr = INADDR_ANY;
2314 udp_conf.use_udp_checksums =
2315 !!(flags & VXLAN_F_UDP_CSUM);
Cong Wange4c7ed42013-08-31 13:44:33 +08002316 }
2317
Tom Herbert3ee64f32014-07-13 19:49:42 -07002318 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002319
Tom Herbert3ee64f32014-07-13 19:49:42 -07002320 /* Open UDP socket */
2321 err = udp_sock_create(net, &udp_conf, &sock);
2322 if (err < 0)
2323 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002324
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002325 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002326}
2327
2328/* Create new listen socket if needed */
2329static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002330 vxlan_rcv_t *rcv, void *data,
2331 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002332{
2333 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2334 struct vxlan_sock *vs;
2335 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002336 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002337 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002338 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002339
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002340 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002341 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002342 return ERR_PTR(-ENOMEM);
2343
2344 for (h = 0; h < VNI_HASH_SIZE; ++h)
2345 INIT_HLIST_HEAD(&vs->vni_list[h]);
2346
2347 INIT_WORK(&vs->del_work, vxlan_del_work);
2348
Tom Herbert3ee64f32014-07-13 19:49:42 -07002349 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002350 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002351 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002352 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002353 }
2354
Cong Wange4c7ed42013-08-31 13:44:33 +08002355 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002356 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002357 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002358 vs->data = data;
stephen hemminger553675f2013-05-16 11:35:20 +00002359
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002360 /* Initialize the vxlan udp offloads structure */
2361 vs->udp_offloads.port = port;
2362 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2363 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2364
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002365 spin_lock(&vn->sock_lock);
2366 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002367 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002368 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002369
2370 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002371 tunnel_cfg.sk_user_data = vs;
2372 tunnel_cfg.encap_type = 1;
2373 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2374 tunnel_cfg.encap_destroy = NULL;
2375
2376 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002377
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002378 return vs;
2379}
stephen hemminger553675f2013-05-16 11:35:20 +00002380
Pravin B Shelar012a5722013-08-19 11:23:07 -07002381struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2382 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002383 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002384{
2385 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2386 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002387 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002388
Tom Herbert359a0ea2014-06-04 17:20:29 -07002389 vs = vxlan_socket_create(net, port, rcv, data, flags);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002390 if (!IS_ERR(vs))
2391 return vs;
2392
Pravin B Shelar012a5722013-08-19 11:23:07 -07002393 if (no_share) /* Return error if sharing is not allowed. */
2394 return vs;
2395
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002396 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002397 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002398 if (vs) {
2399 if (vs->rcv == rcv)
2400 atomic_inc(&vs->refcnt);
2401 else
2402 vs = ERR_PTR(-EBUSY);
2403 }
2404 spin_unlock(&vn->sock_lock);
2405
2406 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002407 vs = ERR_PTR(-EINVAL);
2408
stephen hemminger553675f2013-05-16 11:35:20 +00002409 return vs;
2410}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002411EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002412
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002413/* Scheduled at device creation to bind to a socket */
2414static void vxlan_sock_work(struct work_struct *work)
2415{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002416 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002417 struct net *net = vxlan->net;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002418 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002419 __be16 port = vxlan->dst_port;
2420 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002421
Tom Herbert359a0ea2014-06-04 17:20:29 -07002422 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002423 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002424 if (!IS_ERR(nvs))
2425 vxlan_vs_add_dev(nvs, vxlan);
2426 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002427
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002428 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002429}
2430
stephen hemmingerd3428942012-10-01 12:32:35 +00002431static int vxlan_newlink(struct net *net, struct net_device *dev,
2432 struct nlattr *tb[], struct nlattr *data[])
2433{
stephen hemminger553675f2013-05-16 11:35:20 +00002434 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002435 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002436 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002437 __u32 vni;
2438 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002439 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002440
2441 if (!data[IFLA_VXLAN_ID])
2442 return -EINVAL;
2443
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002444 vxlan->net = dev_net(dev);
2445
stephen hemmingerd3428942012-10-01 12:32:35 +00002446 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002447 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002448
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002449 /* Unless IPv6 is explicitly requested, assume IPv4 */
2450 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002451 if (data[IFLA_VXLAN_GROUP]) {
2452 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002453 } else if (data[IFLA_VXLAN_GROUP6]) {
2454 if (!IS_ENABLED(CONFIG_IPV6))
2455 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002456
Cong Wange4c7ed42013-08-31 13:44:33 +08002457 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2458 sizeof(struct in6_addr));
2459 dst->remote_ip.sa.sa_family = AF_INET6;
2460 use_ipv6 = true;
2461 }
2462
2463 if (data[IFLA_VXLAN_LOCAL]) {
2464 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2465 vxlan->saddr.sa.sa_family = AF_INET;
2466 } else if (data[IFLA_VXLAN_LOCAL6]) {
2467 if (!IS_ENABLED(CONFIG_IPV6))
2468 return -EPFNOSUPPORT;
2469
2470 /* TODO: respect scope id */
2471 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2472 sizeof(struct in6_addr));
2473 vxlan->saddr.sa.sa_family = AF_INET6;
2474 use_ipv6 = true;
2475 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002476
stephen hemminger34e02aa2012-10-09 20:35:53 +00002477 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002478 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002479 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002480 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002481
stephen hemminger34e02aa2012-10-09 20:35:53 +00002482 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002483 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002484 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002485 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002486
Cong Wange4c7ed42013-08-31 13:44:33 +08002487#if IS_ENABLED(CONFIG_IPV6)
2488 if (use_ipv6) {
2489 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2490 if (idev && idev->cnf.disable_ipv6) {
2491 pr_info("IPv6 is disabled via sysctl\n");
2492 return -EPERM;
2493 }
2494 vxlan->flags |= VXLAN_F_IPV6;
2495 }
2496#endif
2497
stephen hemminger34e02aa2012-10-09 20:35:53 +00002498 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002499 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002500
Cong Wang2853af62014-06-12 11:53:10 -07002501 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002502 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002503 } else if (use_ipv6)
2504 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002505
2506 if (data[IFLA_VXLAN_TOS])
2507 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2508
Vincent Bernatafb97182012-10-30 10:27:16 +00002509 if (data[IFLA_VXLAN_TTL])
2510 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2511
stephen hemmingerd3428942012-10-01 12:32:35 +00002512 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002513 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002514
2515 if (data[IFLA_VXLAN_AGEING])
2516 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2517 else
2518 vxlan->age_interval = FDB_AGE_DEFAULT;
2519
David Stevense4f67ad2012-11-20 02:50:14 +00002520 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2521 vxlan->flags |= VXLAN_F_PROXY;
2522
2523 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2524 vxlan->flags |= VXLAN_F_RSC;
2525
2526 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2527 vxlan->flags |= VXLAN_F_L2MISS;
2528
2529 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2530 vxlan->flags |= VXLAN_F_L3MISS;
2531
stephen hemmingerd3428942012-10-01 12:32:35 +00002532 if (data[IFLA_VXLAN_LIMIT])
2533 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2534
stephen hemminger05f47d62012-10-09 20:35:50 +00002535 if (data[IFLA_VXLAN_PORT_RANGE]) {
2536 const struct ifla_vxlan_port_range *p
2537 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2538 vxlan->port_min = ntohs(p->low);
2539 vxlan->port_max = ntohs(p->high);
2540 }
2541
stephen hemminger823aa872013-04-27 11:31:57 +00002542 if (data[IFLA_VXLAN_PORT])
2543 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2544
Tom Herbert359a0ea2014-06-04 17:20:29 -07002545 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2546 vxlan->flags |= VXLAN_F_UDP_CSUM;
2547
2548 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2549 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2550 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2551
2552 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2553 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2554 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2555
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002556 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
2557 vxlan->dst_port)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002558 pr_info("duplicate VNI %u\n", vni);
2559 return -EEXIST;
2560 }
2561
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002562 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002563
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002564 /* create an fdb entry for a valid default destination */
2565 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2566 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2567 &vxlan->default_dst.remote_ip,
2568 NUD_REACHABLE|NUD_PERMANENT,
2569 NLM_F_EXCL|NLM_F_CREATE,
2570 vxlan->dst_port,
2571 vxlan->default_dst.remote_vni,
2572 vxlan->default_dst.remote_ifindex,
2573 NTF_SELF);
2574 if (err)
2575 return err;
2576 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002577
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002578 err = register_netdevice(dev);
2579 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002580 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002581 return err;
2582 }
2583
stephen hemminger553675f2013-05-16 11:35:20 +00002584 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002585
2586 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002587}
2588
2589static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2590{
2591 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002592 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002593
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002594 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002595 if (!hlist_unhashed(&vxlan->hlist))
2596 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002597 spin_unlock(&vn->sock_lock);
2598
stephen hemminger553675f2013-05-16 11:35:20 +00002599 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002600 unregister_netdevice_queue(dev, head);
2601}
2602
2603static size_t vxlan_get_size(const struct net_device *dev)
2604{
2605
2606 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002607 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002608 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002609 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002610 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2611 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2612 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002613 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2614 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2615 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2616 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002617 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2618 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002619 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002620 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2621 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2622 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2623 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002624 0;
2625}
2626
2627static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2628{
2629 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002630 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002631 struct ifla_vxlan_port_range ports = {
2632 .low = htons(vxlan->port_min),
2633 .high = htons(vxlan->port_max),
2634 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002635
Atzm Watanabec7995c42013-04-16 02:50:52 +00002636 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002637 goto nla_put_failure;
2638
Cong Wange4c7ed42013-08-31 13:44:33 +08002639 if (!vxlan_addr_any(&dst->remote_ip)) {
2640 if (dst->remote_ip.sa.sa_family == AF_INET) {
2641 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2642 dst->remote_ip.sin.sin_addr.s_addr))
2643 goto nla_put_failure;
2644#if IS_ENABLED(CONFIG_IPV6)
2645 } else {
2646 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2647 &dst->remote_ip.sin6.sin6_addr))
2648 goto nla_put_failure;
2649#endif
2650 }
2651 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002652
Atzm Watanabec7995c42013-04-16 02:50:52 +00002653 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002654 goto nla_put_failure;
2655
Cong Wange4c7ed42013-08-31 13:44:33 +08002656 if (!vxlan_addr_any(&vxlan->saddr)) {
2657 if (vxlan->saddr.sa.sa_family == AF_INET) {
2658 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2659 vxlan->saddr.sin.sin_addr.s_addr))
2660 goto nla_put_failure;
2661#if IS_ENABLED(CONFIG_IPV6)
2662 } else {
2663 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2664 &vxlan->saddr.sin6.sin6_addr))
2665 goto nla_put_failure;
2666#endif
2667 }
2668 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002669
2670 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2671 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002672 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2673 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2674 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2675 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2676 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2677 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2678 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2679 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2680 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002681 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002682 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002683 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2684 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2685 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2686 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2687 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2688 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2689 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002690 goto nla_put_failure;
2691
stephen hemminger05f47d62012-10-09 20:35:50 +00002692 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2693 goto nla_put_failure;
2694
stephen hemmingerd3428942012-10-01 12:32:35 +00002695 return 0;
2696
2697nla_put_failure:
2698 return -EMSGSIZE;
2699}
2700
2701static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2702 .kind = "vxlan",
2703 .maxtype = IFLA_VXLAN_MAX,
2704 .policy = vxlan_policy,
2705 .priv_size = sizeof(struct vxlan_dev),
2706 .setup = vxlan_setup,
2707 .validate = vxlan_validate,
2708 .newlink = vxlan_newlink,
2709 .dellink = vxlan_dellink,
2710 .get_size = vxlan_get_size,
2711 .fill_info = vxlan_fill_info,
2712};
2713
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002714static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2715 struct net_device *dev)
2716{
2717 struct vxlan_dev *vxlan, *next;
2718 LIST_HEAD(list_kill);
2719
2720 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2721 struct vxlan_rdst *dst = &vxlan->default_dst;
2722
2723 /* In case we created vxlan device with carrier
2724 * and we loose the carrier due to module unload
2725 * we also need to remove vxlan device. In other
2726 * cases, it's not necessary and remote_ifindex
2727 * is 0 here, so no matches.
2728 */
2729 if (dst->remote_ifindex == dev->ifindex)
2730 vxlan_dellink(vxlan->dev, &list_kill);
2731 }
2732
2733 unregister_netdevice_many(&list_kill);
2734}
2735
2736static int vxlan_lowerdev_event(struct notifier_block *unused,
2737 unsigned long event, void *ptr)
2738{
2739 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002740 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002741
Daniel Borkmann783c1462014-01-22 21:07:53 +01002742 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002743 vxlan_handle_lowerdev_unregister(vn, dev);
2744
2745 return NOTIFY_DONE;
2746}
2747
2748static struct notifier_block vxlan_notifier_block __read_mostly = {
2749 .notifier_call = vxlan_lowerdev_event,
2750};
2751
stephen hemmingerd3428942012-10-01 12:32:35 +00002752static __net_init int vxlan_init_net(struct net *net)
2753{
2754 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002755 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002756
stephen hemminger553675f2013-05-16 11:35:20 +00002757 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002758 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002759
stephen hemminger553675f2013-05-16 11:35:20 +00002760 for (h = 0; h < PORT_HASH_SIZE; ++h)
2761 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002762
2763 return 0;
2764}
2765
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002766static void __net_exit vxlan_exit_net(struct net *net)
2767{
2768 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2769 struct vxlan_dev *vxlan, *next;
2770 struct net_device *dev, *aux;
2771 LIST_HEAD(list);
2772
2773 rtnl_lock();
2774 for_each_netdev_safe(net, dev, aux)
2775 if (dev->rtnl_link_ops == &vxlan_link_ops)
2776 unregister_netdevice_queue(dev, &list);
2777
2778 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2779 /* If vxlan->dev is in the same netns, it has already been added
2780 * to the list by the previous loop.
2781 */
2782 if (!net_eq(dev_net(vxlan->dev), net))
2783 unregister_netdevice_queue(dev, &list);
2784 }
2785
2786 unregister_netdevice_many(&list);
2787 rtnl_unlock();
2788}
2789
stephen hemmingerd3428942012-10-01 12:32:35 +00002790static struct pernet_operations vxlan_net_ops = {
2791 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002792 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002793 .id = &vxlan_net_id,
2794 .size = sizeof(struct vxlan_net),
2795};
2796
2797static int __init vxlan_init_module(void)
2798{
2799 int rc;
2800
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002801 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2802 if (!vxlan_wq)
2803 return -ENOMEM;
2804
stephen hemmingerd3428942012-10-01 12:32:35 +00002805 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2806
Daniel Borkmann783c1462014-01-22 21:07:53 +01002807 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00002808 if (rc)
2809 goto out1;
2810
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002811 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002812 if (rc)
2813 goto out2;
2814
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002815 rc = rtnl_link_register(&vxlan_link_ops);
2816 if (rc)
2817 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00002818
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002819 return 0;
2820out3:
2821 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002822out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01002823 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00002824out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002825 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002826 return rc;
2827}
Cong Wang7332a132013-05-27 22:35:53 +00002828late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002829
2830static void __exit vxlan_cleanup_module(void)
2831{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002832 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002833 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002834 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002835 unregister_pernet_subsys(&vxlan_net_ops);
2836 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00002837}
2838module_exit(vxlan_cleanup_module);
2839
2840MODULE_LICENSE("GPL");
2841MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002842MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08002843MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00002844MODULE_ALIAS_RTNL_LINK("vxlan");