blob: 39c86536fb9b7e5b1a2651b5a867a21a9609dd42 [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
stephen hemminger553675f2013-05-16 11:35:20 +0000278/* Find VXLAN socket based on network namespace and UDP port */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -0700279static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000280{
281 struct vxlan_sock *vs;
282
283 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
284 if (inet_sk(vs->sock->sk)->inet_sport == port)
285 return vs;
286 }
287 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000288}
289
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700290static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000291{
292 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000293
stephen hemminger553675f2013-05-16 11:35:20 +0000294 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000295 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000296 return vxlan;
297 }
298
299 return NULL;
300}
301
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700302/* Look up VNI in a per net namespace table */
303static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
304{
305 struct vxlan_sock *vs;
306
307 vs = vxlan_find_sock(net, port);
308 if (!vs)
309 return NULL;
310
311 return vxlan_vs_find_vni(vs, id);
312}
313
stephen hemmingerd3428942012-10-01 12:32:35 +0000314/* Fill in neighbour message in skbuff. */
315static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700316 const struct vxlan_fdb *fdb,
317 u32 portid, u32 seq, int type, unsigned int flags,
318 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000319{
320 unsigned long now = jiffies;
321 struct nda_cacheinfo ci;
322 struct nlmsghdr *nlh;
323 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000324 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000325
326 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
327 if (nlh == NULL)
328 return -EMSGSIZE;
329
330 ndm = nlmsg_data(nlh);
331 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000332
333 send_eth = send_ip = true;
334
335 if (type == RTM_GETNEIGH) {
336 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800337 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000338 send_eth = !is_zero_ether_addr(fdb->eth_addr);
339 } else
340 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000341 ndm->ndm_state = fdb->state;
342 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000343 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800344 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000345
David Stevense4f67ad2012-11-20 02:50:14 +0000346 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000347 goto nla_put_failure;
348
Cong Wange4c7ed42013-08-31 13:44:33 +0800349 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000350 goto nla_put_failure;
351
stephen hemminger823aa872013-04-27 11:31:57 +0000352 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000353 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
354 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000355 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700356 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000357 goto nla_put_failure;
358 if (rdst->remote_ifindex &&
359 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000360 goto nla_put_failure;
361
362 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
363 ci.ndm_confirmed = 0;
364 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
365 ci.ndm_refcnt = 0;
366
367 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
368 goto nla_put_failure;
369
370 return nlmsg_end(skb, nlh);
371
372nla_put_failure:
373 nlmsg_cancel(skb, nlh);
374 return -EMSGSIZE;
375}
376
377static inline size_t vxlan_nlmsg_size(void)
378{
379 return NLMSG_ALIGN(sizeof(struct ndmsg))
380 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800381 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000382 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000383 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
384 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000385 + nla_total_size(sizeof(struct nda_cacheinfo));
386}
387
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200388static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
389 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000390{
391 struct net *net = dev_net(vxlan->dev);
392 struct sk_buff *skb;
393 int err = -ENOBUFS;
394
395 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
396 if (skb == NULL)
397 goto errout;
398
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200399 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000400 if (err < 0) {
401 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
402 WARN_ON(err == -EMSGSIZE);
403 kfree_skb(skb);
404 goto errout;
405 }
406
407 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
408 return;
409errout:
410 if (err < 0)
411 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
412}
413
Cong Wange4c7ed42013-08-31 13:44:33 +0800414static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000415{
416 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700417 struct vxlan_fdb f = {
418 .state = NUD_STALE,
419 };
420 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800421 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700422 .remote_vni = VXLAN_N_VID,
423 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700424
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200425 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000426}
427
428static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
429{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700430 struct vxlan_fdb f = {
431 .state = NUD_STALE,
432 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200433 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000434
David Stevense4f67ad2012-11-20 02:50:14 +0000435 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
436
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200437 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000438}
439
stephen hemmingerd3428942012-10-01 12:32:35 +0000440/* Hash Ethernet address */
441static u32 eth_hash(const unsigned char *addr)
442{
443 u64 value = get_unaligned((u64 *)addr);
444
445 /* only want 6 bytes */
446#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000447 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000448#else
449 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000450#endif
451 return hash_64(value, FDB_HASH_BITS);
452}
453
454/* Hash chain to use given mac address */
455static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
456 const u8 *mac)
457{
458 return &vxlan->fdb_head[eth_hash(mac)];
459}
460
461/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000462static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000463 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000464{
465 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
466 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000467
Sasha Levinb67bfe02013-02-27 17:06:00 -0800468 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700469 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000470 return f;
471 }
472
473 return NULL;
474}
475
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000476static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
477 const u8 *mac)
478{
479 struct vxlan_fdb *f;
480
481 f = __vxlan_find_mac(vxlan, mac);
482 if (f)
483 f->used = jiffies;
484
485 return f;
486}
487
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300488/* caller should hold vxlan->hash_lock */
489static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800490 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300491 __u32 vni, __u32 ifindex)
492{
493 struct vxlan_rdst *rd;
494
495 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800496 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300497 rd->remote_port == port &&
498 rd->remote_vni == vni &&
499 rd->remote_ifindex == ifindex)
500 return rd;
501 }
502
503 return NULL;
504}
505
Thomas Richter906dc182013-07-19 17:20:07 +0200506/* Replace destination of unicast mac */
507static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800508 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200509{
510 struct vxlan_rdst *rd;
511
512 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
513 if (rd)
514 return 0;
515
516 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
517 if (!rd)
518 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800519 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200520 rd->remote_port = port;
521 rd->remote_vni = vni;
522 rd->remote_ifindex = ifindex;
523 return 1;
524}
525
David Stevens66817122013-03-15 04:35:51 +0000526/* Add/update destinations for multicast */
527static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200528 union vxlan_addr *ip, __be16 port, __u32 vni,
529 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000530{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700531 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000532
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300533 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
534 if (rd)
535 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700536
David Stevens66817122013-03-15 04:35:51 +0000537 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
538 if (rd == NULL)
539 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800540 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000541 rd->remote_port = port;
542 rd->remote_vni = vni;
543 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700544
545 list_add_tail_rcu(&rd->list, &f->remotes);
546
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200547 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000548 return 1;
549}
550
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200551static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, struct sk_buff *skb)
552{
553 struct sk_buff *p, **pp = NULL;
554 struct vxlanhdr *vh, *vh2;
555 struct ethhdr *eh, *eh2;
556 unsigned int hlen, off_vx, off_eth;
557 const struct packet_offload *ptype;
558 __be16 type;
559 int flush = 1;
560
561 off_vx = skb_gro_offset(skb);
562 hlen = off_vx + sizeof(*vh);
563 vh = skb_gro_header_fast(skb, off_vx);
564 if (skb_gro_header_hard(skb, hlen)) {
565 vh = skb_gro_header_slow(skb, hlen, off_vx);
566 if (unlikely(!vh))
567 goto out;
568 }
569 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700570 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200571
572 off_eth = skb_gro_offset(skb);
573 hlen = off_eth + sizeof(*eh);
574 eh = skb_gro_header_fast(skb, off_eth);
575 if (skb_gro_header_hard(skb, hlen)) {
576 eh = skb_gro_header_slow(skb, hlen, off_eth);
577 if (unlikely(!eh))
578 goto out;
579 }
580
581 flush = 0;
582
583 for (p = *head; p; p = p->next) {
584 if (!NAPI_GRO_CB(p)->same_flow)
585 continue;
586
587 vh2 = (struct vxlanhdr *)(p->data + off_vx);
588 eh2 = (struct ethhdr *)(p->data + off_eth);
589 if (vh->vx_vni != vh2->vx_vni || compare_ether_header(eh, eh2)) {
590 NAPI_GRO_CB(p)->same_flow = 0;
591 continue;
592 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200593 }
594
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200595 type = eh->h_proto;
596
597 rcu_read_lock();
598 ptype = gro_find_receive_by_type(type);
599 if (ptype == NULL) {
600 flush = 1;
601 goto out_unlock;
602 }
603
604 skb_gro_pull(skb, sizeof(*eh)); /* pull inner eth header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700605 skb_gro_postpull_rcsum(skb, eh, sizeof(*eh));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200606 pp = ptype->callbacks.gro_receive(head, skb);
607
608out_unlock:
609 rcu_read_unlock();
610out:
611 NAPI_GRO_CB(skb)->flush |= flush;
612
613 return pp;
614}
615
616static int vxlan_gro_complete(struct sk_buff *skb, int nhoff)
617{
618 struct ethhdr *eh;
619 struct packet_offload *ptype;
620 __be16 type;
621 int vxlan_len = sizeof(struct vxlanhdr) + sizeof(struct ethhdr);
622 int err = -ENOSYS;
623
624 eh = (struct ethhdr *)(skb->data + nhoff + sizeof(struct vxlanhdr));
625 type = eh->h_proto;
626
627 rcu_read_lock();
628 ptype = gro_find_complete_by_type(type);
629 if (ptype != NULL)
630 err = ptype->callbacks.gro_complete(skb, nhoff + vxlan_len);
631
632 rcu_read_unlock();
633 return err;
634}
635
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700636/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200637static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700638{
639 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200640 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700641 struct net *net = sock_net(sk);
642 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700643 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200644 int err;
645
646 if (sa_family == AF_INET) {
647 err = udp_add_offload(&vs->udp_offloads);
648 if (err)
649 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
650 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700651
652 rcu_read_lock();
653 for_each_netdev_rcu(net, dev) {
654 if (dev->netdev_ops->ndo_add_vxlan_port)
655 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
656 port);
657 }
658 rcu_read_unlock();
659}
660
661/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200662static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700663{
664 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200665 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700666 struct net *net = sock_net(sk);
667 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700668 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700669
670 rcu_read_lock();
671 for_each_netdev_rcu(net, dev) {
672 if (dev->netdev_ops->ndo_del_vxlan_port)
673 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
674 port);
675 }
676 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200677
678 if (sa_family == AF_INET)
679 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700680}
681
stephen hemmingerd3428942012-10-01 12:32:35 +0000682/* Add new entry to forwarding table -- assumes lock held */
683static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800684 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000685 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000686 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000687 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000688{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200689 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000690 struct vxlan_fdb *f;
691 int notify = 0;
692
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000693 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000694 if (f) {
695 if (flags & NLM_F_EXCL) {
696 netdev_dbg(vxlan->dev,
697 "lost race to create %pM\n", mac);
698 return -EEXIST;
699 }
700 if (f->state != state) {
701 f->state = state;
702 f->updated = jiffies;
703 notify = 1;
704 }
David Stevensae884082013-04-19 00:36:26 +0000705 if (f->flags != ndm_flags) {
706 f->flags = ndm_flags;
707 f->updated = jiffies;
708 notify = 1;
709 }
Thomas Richter906dc182013-07-19 17:20:07 +0200710 if ((flags & NLM_F_REPLACE)) {
711 /* Only change unicasts */
712 if (!(is_multicast_ether_addr(f->eth_addr) ||
713 is_zero_ether_addr(f->eth_addr))) {
714 int rc = vxlan_fdb_replace(f, ip, port, vni,
715 ifindex);
716
717 if (rc < 0)
718 return rc;
719 notify |= rc;
720 } else
721 return -EOPNOTSUPP;
722 }
David Stevens66817122013-03-15 04:35:51 +0000723 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300724 (is_multicast_ether_addr(f->eth_addr) ||
725 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200726 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
727 &rd);
David Stevens66817122013-03-15 04:35:51 +0000728
729 if (rc < 0)
730 return rc;
731 notify |= rc;
732 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000733 } else {
734 if (!(flags & NLM_F_CREATE))
735 return -ENOENT;
736
737 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
738 return -ENOSPC;
739
Thomas Richter906dc182013-07-19 17:20:07 +0200740 /* Disallow replace to add a multicast entry */
741 if ((flags & NLM_F_REPLACE) &&
742 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
743 return -EOPNOTSUPP;
744
Cong Wange4c7ed42013-08-31 13:44:33 +0800745 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000746 f = kmalloc(sizeof(*f), GFP_ATOMIC);
747 if (!f)
748 return -ENOMEM;
749
750 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000751 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000752 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000753 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700754 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000755 memcpy(f->eth_addr, mac, ETH_ALEN);
756
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200757 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700758
stephen hemmingerd3428942012-10-01 12:32:35 +0000759 ++vxlan->addrcnt;
760 hlist_add_head_rcu(&f->hlist,
761 vxlan_fdb_head(vxlan, mac));
762 }
763
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200764 if (notify) {
765 if (rd == NULL)
766 rd = first_remote_rtnl(f);
767 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
768 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000769
770 return 0;
771}
772
Wei Yongjun6706c822013-04-11 19:00:35 +0000773static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000774{
775 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700776 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000777
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700778 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000779 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000780 kfree(f);
781}
782
stephen hemmingerd3428942012-10-01 12:32:35 +0000783static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
784{
785 netdev_dbg(vxlan->dev,
786 "delete %pM\n", f->eth_addr);
787
788 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200789 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000790
791 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000792 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000793}
794
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300795static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800796 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300797{
798 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800799 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300800
801 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800802 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
803 if (err)
804 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300805 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800806 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
807 if (remote->sa.sa_family == AF_INET) {
808 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
809 ip->sa.sa_family = AF_INET;
810#if IS_ENABLED(CONFIG_IPV6)
811 } else {
812 ip->sin6.sin6_addr = in6addr_any;
813 ip->sa.sa_family = AF_INET6;
814#endif
815 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300816 }
817
818 if (tb[NDA_PORT]) {
819 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
820 return -EINVAL;
821 *port = nla_get_be16(tb[NDA_PORT]);
822 } else {
823 *port = vxlan->dst_port;
824 }
825
826 if (tb[NDA_VNI]) {
827 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
828 return -EINVAL;
829 *vni = nla_get_u32(tb[NDA_VNI]);
830 } else {
831 *vni = vxlan->default_dst.remote_vni;
832 }
833
834 if (tb[NDA_IFINDEX]) {
835 struct net_device *tdev;
836
837 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
838 return -EINVAL;
839 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800840 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300841 if (!tdev)
842 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300843 } else {
844 *ifindex = 0;
845 }
846
847 return 0;
848}
849
stephen hemmingerd3428942012-10-01 12:32:35 +0000850/* Add static entry (via netlink) */
851static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
852 struct net_device *dev,
853 const unsigned char *addr, u16 flags)
854{
855 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300856 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800857 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000858 __be16 port;
859 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000860 int err;
861
862 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
863 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
864 ndm->ndm_state);
865 return -EINVAL;
866 }
867
868 if (tb[NDA_DST] == NULL)
869 return -EINVAL;
870
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300871 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
872 if (err)
873 return err;
David Stevens66817122013-03-15 04:35:51 +0000874
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300875 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
876 return -EAFNOSUPPORT;
877
stephen hemmingerd3428942012-10-01 12:32:35 +0000878 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800879 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000880 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000881 spin_unlock_bh(&vxlan->hash_lock);
882
883 return err;
884}
885
886/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000887static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
888 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000889 const unsigned char *addr)
890{
891 struct vxlan_dev *vxlan = netdev_priv(dev);
892 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300893 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800894 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300895 __be16 port;
896 u32 vni, ifindex;
897 int err;
898
899 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
900 if (err)
901 return err;
902
903 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000904
905 spin_lock_bh(&vxlan->hash_lock);
906 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300907 if (!f)
908 goto out;
909
Cong Wange4c7ed42013-08-31 13:44:33 +0800910 if (!vxlan_addr_any(&ip)) {
911 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300912 if (!rd)
913 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000914 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300915
916 err = 0;
917
918 /* remove a destination if it's not the only one on the list,
919 * otherwise destroy the fdb entry
920 */
921 if (rd && !list_is_singular(&f->remotes)) {
922 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200923 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800924 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300925 goto out;
926 }
927
928 vxlan_fdb_destroy(vxlan, f);
929
930out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000931 spin_unlock_bh(&vxlan->hash_lock);
932
933 return err;
934}
935
936/* Dump forwarding table */
937static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400938 struct net_device *dev,
939 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000940{
941 struct vxlan_dev *vxlan = netdev_priv(dev);
942 unsigned int h;
943
944 for (h = 0; h < FDB_HASH_SIZE; ++h) {
945 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000946 int err;
947
Sasha Levinb67bfe02013-02-27 17:06:00 -0800948 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000949 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000950
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700951 if (idx < cb->args[0])
952 goto skip;
953
954 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000955 err = vxlan_fdb_info(skb, vxlan, f,
956 NETLINK_CB(cb->skb).portid,
957 cb->nlh->nlmsg_seq,
958 RTM_NEWNEIGH,
959 NLM_F_MULTI, rd);
960 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700961 goto out;
David Stevens66817122013-03-15 04:35:51 +0000962 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700963skip:
964 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000965 }
966 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700967out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000968 return idx;
969}
970
971/* Watch incoming packets to learn mapping between Ethernet address
972 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700973 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000974 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700975static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800976 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000977{
978 struct vxlan_dev *vxlan = netdev_priv(dev);
979 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000980
981 f = vxlan_find_mac(vxlan, src_mac);
982 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700983 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700984
Cong Wange4c7ed42013-08-31 13:44:33 +0800985 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700986 return false;
987
988 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700989 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700990 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000991
992 if (net_ratelimit())
993 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800994 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700995 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000996
Cong Wange4c7ed42013-08-31 13:44:33 +0800997 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000998 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200999 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001000 } else {
1001 /* learned new entry */
1002 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001003
1004 /* close off race between vxlan_flush and incoming packets */
1005 if (netif_running(dev))
1006 vxlan_fdb_create(vxlan, src_mac, src_ip,
1007 NUD_REACHABLE,
1008 NLM_F_EXCL|NLM_F_CREATE,
1009 vxlan->dst_port,
1010 vxlan->default_dst.remote_vni,
1011 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001012 spin_unlock(&vxlan->hash_lock);
1013 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001014
1015 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001016}
1017
stephen hemmingerd3428942012-10-01 12:32:35 +00001018/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001019static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001020{
stephen hemminger553675f2013-05-16 11:35:20 +00001021 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001022
Gao feng95ab0992013-12-10 16:37:33 +08001023 /* The vxlan_sock is only used by dev, leaving group has
1024 * no effect on other vxlan devices.
1025 */
1026 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1027 return false;
1028
stephen hemminger553675f2013-05-16 11:35:20 +00001029 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001030 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001031 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001032
Gao feng95ab0992013-12-10 16:37:33 +08001033 if (vxlan->vn_sock != dev->vn_sock)
1034 continue;
1035
1036 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1037 &dev->default_dst.remote_ip))
1038 continue;
1039
1040 if (vxlan->default_dst.remote_ifindex !=
1041 dev->default_dst.remote_ifindex)
1042 continue;
1043
1044 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001045 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001046
1047 return false;
1048}
1049
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001050static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001051{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001052 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001053}
1054
Pravin B Shelar012a5722013-08-19 11:23:07 -07001055void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001056{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001057 struct sock *sk = vs->sock->sk;
1058 struct net *net = sock_net(sk);
1059 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001060
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001061 if (!atomic_dec_and_test(&vs->refcnt))
1062 return;
1063
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001064 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001065 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001066 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001067 spin_unlock(&vn->sock_lock);
1068
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001069 queue_work(vxlan_wq, &vs->del_work);
1070}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001071EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001072
stephen hemminger3fc2de22013-07-18 08:40:15 -07001073/* Callback to update multicast group membership when first VNI on
1074 * multicast asddress is brought up
1075 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001076 */
stephen hemminger3fc2de22013-07-18 08:40:15 -07001077static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001078{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001079 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001080 struct vxlan_sock *vs = vxlan->vn_sock;
1081 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001082 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1083 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001084
stephen hemmingerd3428942012-10-01 12:32:35 +00001085 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001086 if (ip->sa.sa_family == AF_INET) {
1087 struct ip_mreqn mreq = {
1088 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1089 .imr_ifindex = ifindex,
1090 };
1091
1092 ip_mc_join_group(sk, &mreq);
1093#if IS_ENABLED(CONFIG_IPV6)
1094 } else {
1095 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1096 &ip->sin6.sin6_addr);
1097#endif
1098 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001099 release_sock(sk);
1100
Pravin B Shelar012a5722013-08-19 11:23:07 -07001101 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001102 dev_put(vxlan->dev);
1103}
1104
1105/* Inverse of vxlan_igmp_join when last VNI is brought down */
1106static void vxlan_igmp_leave(struct work_struct *work)
1107{
1108 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001109 struct vxlan_sock *vs = vxlan->vn_sock;
1110 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001111 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1112 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001113
1114 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001115 if (ip->sa.sa_family == AF_INET) {
1116 struct ip_mreqn mreq = {
1117 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1118 .imr_ifindex = ifindex,
1119 };
1120
1121 ip_mc_leave_group(sk, &mreq);
1122#if IS_ENABLED(CONFIG_IPV6)
1123 } else {
1124 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1125 &ip->sin6.sin6_addr);
1126#endif
1127 }
1128
stephen hemmingerd3428942012-10-01 12:32:35 +00001129 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001130
Pravin B Shelar012a5722013-08-19 11:23:07 -07001131 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001132 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001133}
1134
1135/* Callback from net/ipv4/udp.c to receive packets */
1136static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1137{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001138 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001139 struct vxlanhdr *vxh;
stephen hemmingerd3428942012-10-01 12:32:35 +00001140
stephen hemmingerd3428942012-10-01 12:32:35 +00001141 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001142 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001143 goto error;
1144
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001145 /* Return packets with reserved bits set */
1146 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001147 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1148 (vxh->vx_vni & htonl(0xff))) {
1149 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1150 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1151 goto error;
1152 }
1153
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001154 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001155 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001156
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001157 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001158 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001159 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001160
1161 vs->rcv(vs, skb, vxh->vx_vni);
1162 return 0;
1163
1164drop:
1165 /* Consume bad packet */
1166 kfree_skb(skb);
1167 return 0;
1168
1169error:
1170 /* Return non vxlan pkt */
1171 return 1;
1172}
1173
1174static void vxlan_rcv(struct vxlan_sock *vs,
1175 struct sk_buff *skb, __be32 vx_vni)
1176{
Cong Wange4c7ed42013-08-31 13:44:33 +08001177 struct iphdr *oip = NULL;
1178 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001179 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001180 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001181 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001182 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001183 int err = 0;
1184 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001185
1186 vni = ntohl(vx_vni) >> 8;
1187 /* Is this VNI defined? */
1188 vxlan = vxlan_vs_find_vni(vs, vni);
1189 if (!vxlan)
1190 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001191
Cong Wange4c7ed42013-08-31 13:44:33 +08001192 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001193 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001194 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001195 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001196 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001197
1198 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001199 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001200 goto drop;
1201
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001202 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001203 if (remote_ip->sa.sa_family == AF_INET) {
1204 oip = ip_hdr(skb);
1205 saddr.sin.sin_addr.s_addr = oip->saddr;
1206 saddr.sa.sa_family = AF_INET;
1207#if IS_ENABLED(CONFIG_IPV6)
1208 } else {
1209 oip6 = ipv6_hdr(skb);
1210 saddr.sin6.sin6_addr = oip6->saddr;
1211 saddr.sa.sa_family = AF_INET6;
1212#endif
1213 }
1214
stephen hemminger26a41ae62013-06-17 12:09:58 -07001215 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001216 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001217 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001218
stephen hemmingerd3428942012-10-01 12:32:35 +00001219 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001220
Cong Wange4c7ed42013-08-31 13:44:33 +08001221 if (oip6)
1222 err = IP6_ECN_decapsulate(oip6, skb);
1223 if (oip)
1224 err = IP_ECN_decapsulate(oip, skb);
1225
stephen hemmingerd3428942012-10-01 12:32:35 +00001226 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001227 if (log_ecn_error) {
1228 if (oip6)
1229 net_info_ratelimited("non-ECT from %pI6\n",
1230 &oip6->saddr);
1231 if (oip)
1232 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1233 &oip->saddr, oip->tos);
1234 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001235 if (err > 1) {
1236 ++vxlan->dev->stats.rx_frame_errors;
1237 ++vxlan->dev->stats.rx_errors;
1238 goto drop;
1239 }
1240 }
1241
Pravin B Shelare8171042013-03-25 14:49:46 +00001242 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001243 u64_stats_update_begin(&stats->syncp);
1244 stats->rx_packets++;
1245 stats->rx_bytes += skb->len;
1246 u64_stats_update_end(&stats->syncp);
1247
1248 netif_rx(skb);
1249
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001250 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001251drop:
1252 /* Consume bad packet */
1253 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001254}
1255
David Stevense4f67ad2012-11-20 02:50:14 +00001256static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1257{
1258 struct vxlan_dev *vxlan = netdev_priv(dev);
1259 struct arphdr *parp;
1260 u8 *arpptr, *sha;
1261 __be32 sip, tip;
1262 struct neighbour *n;
1263
1264 if (dev->flags & IFF_NOARP)
1265 goto out;
1266
1267 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1268 dev->stats.tx_dropped++;
1269 goto out;
1270 }
1271 parp = arp_hdr(skb);
1272
1273 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1274 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1275 parp->ar_pro != htons(ETH_P_IP) ||
1276 parp->ar_op != htons(ARPOP_REQUEST) ||
1277 parp->ar_hln != dev->addr_len ||
1278 parp->ar_pln != 4)
1279 goto out;
1280 arpptr = (u8 *)parp + sizeof(struct arphdr);
1281 sha = arpptr;
1282 arpptr += dev->addr_len; /* sha */
1283 memcpy(&sip, arpptr, sizeof(sip));
1284 arpptr += sizeof(sip);
1285 arpptr += dev->addr_len; /* tha */
1286 memcpy(&tip, arpptr, sizeof(tip));
1287
1288 if (ipv4_is_loopback(tip) ||
1289 ipv4_is_multicast(tip))
1290 goto out;
1291
1292 n = neigh_lookup(&arp_tbl, &tip, dev);
1293
1294 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001295 struct vxlan_fdb *f;
1296 struct sk_buff *reply;
1297
1298 if (!(n->nud_state & NUD_CONNECTED)) {
1299 neigh_release(n);
1300 goto out;
1301 }
1302
1303 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001304 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001305 /* bridge-local neighbor */
1306 neigh_release(n);
1307 goto out;
1308 }
1309
1310 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1311 n->ha, sha);
1312
1313 neigh_release(n);
1314
David Stevens73461352014-03-18 12:32:29 -04001315 if (reply == NULL)
1316 goto out;
1317
David Stevense4f67ad2012-11-20 02:50:14 +00001318 skb_reset_mac_header(reply);
1319 __skb_pull(reply, skb_network_offset(reply));
1320 reply->ip_summed = CHECKSUM_UNNECESSARY;
1321 reply->pkt_type = PACKET_HOST;
1322
1323 if (netif_rx_ni(reply) == NET_RX_DROP)
1324 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001325 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1326 union vxlan_addr ipa = {
1327 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001328 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001329 };
1330
1331 vxlan_ip_miss(dev, &ipa);
1332 }
David Stevense4f67ad2012-11-20 02:50:14 +00001333out:
1334 consume_skb(skb);
1335 return NETDEV_TX_OK;
1336}
1337
Cong Wangf564f452013-08-31 13:44:36 +08001338#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001339static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1340 struct neighbour *n, bool isrouter)
1341{
1342 struct net_device *dev = request->dev;
1343 struct sk_buff *reply;
1344 struct nd_msg *ns, *na;
1345 struct ipv6hdr *pip6;
1346 u8 *daddr;
1347 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1348 int ns_olen;
1349 int i, len;
1350
1351 if (dev == NULL)
1352 return NULL;
1353
1354 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1355 sizeof(*na) + na_olen + dev->needed_tailroom;
1356 reply = alloc_skb(len, GFP_ATOMIC);
1357 if (reply == NULL)
1358 return NULL;
1359
1360 reply->protocol = htons(ETH_P_IPV6);
1361 reply->dev = dev;
1362 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1363 skb_push(reply, sizeof(struct ethhdr));
1364 skb_set_mac_header(reply, 0);
1365
1366 ns = (struct nd_msg *)skb_transport_header(request);
1367
1368 daddr = eth_hdr(request)->h_source;
1369 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1370 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1371 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1372 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1373 break;
1374 }
1375 }
1376
1377 /* Ethernet header */
1378 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1379 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1380 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1381 reply->protocol = htons(ETH_P_IPV6);
1382
1383 skb_pull(reply, sizeof(struct ethhdr));
1384 skb_set_network_header(reply, 0);
1385 skb_put(reply, sizeof(struct ipv6hdr));
1386
1387 /* IPv6 header */
1388
1389 pip6 = ipv6_hdr(reply);
1390 memset(pip6, 0, sizeof(struct ipv6hdr));
1391 pip6->version = 6;
1392 pip6->priority = ipv6_hdr(request)->priority;
1393 pip6->nexthdr = IPPROTO_ICMPV6;
1394 pip6->hop_limit = 255;
1395 pip6->daddr = ipv6_hdr(request)->saddr;
1396 pip6->saddr = *(struct in6_addr *)n->primary_key;
1397
1398 skb_pull(reply, sizeof(struct ipv6hdr));
1399 skb_set_transport_header(reply, 0);
1400
1401 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1402
1403 /* Neighbor Advertisement */
1404 memset(na, 0, sizeof(*na)+na_olen);
1405 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1406 na->icmph.icmp6_router = isrouter;
1407 na->icmph.icmp6_override = 1;
1408 na->icmph.icmp6_solicited = 1;
1409 na->target = ns->target;
1410 ether_addr_copy(&na->opt[2], n->ha);
1411 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1412 na->opt[1] = na_olen >> 3;
1413
1414 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1415 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1416 csum_partial(na, sizeof(*na)+na_olen, 0));
1417
1418 pip6->payload_len = htons(sizeof(*na)+na_olen);
1419
1420 skb_push(reply, sizeof(struct ipv6hdr));
1421
1422 reply->ip_summed = CHECKSUM_UNNECESSARY;
1423
1424 return reply;
1425}
1426
Cong Wangf564f452013-08-31 13:44:36 +08001427static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1428{
1429 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001430 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001431 const struct ipv6hdr *iphdr;
1432 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001433 struct neighbour *n;
1434 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001435
1436 in6_dev = __in6_dev_get(dev);
1437 if (!in6_dev)
1438 goto out;
1439
1440 if (!pskb_may_pull(skb, skb->len))
1441 goto out;
1442
1443 iphdr = ipv6_hdr(skb);
1444 saddr = &iphdr->saddr;
1445 daddr = &iphdr->daddr;
1446
Cong Wangf564f452013-08-31 13:44:36 +08001447 msg = (struct nd_msg *)skb_transport_header(skb);
1448 if (msg->icmph.icmp6_code != 0 ||
1449 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1450 goto out;
1451
David Stevens4b29dba2014-03-24 10:39:58 -04001452 if (ipv6_addr_loopback(daddr) ||
1453 ipv6_addr_is_multicast(&msg->target))
1454 goto out;
1455
1456 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001457
1458 if (n) {
1459 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001460 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001461
1462 if (!(n->nud_state & NUD_CONNECTED)) {
1463 neigh_release(n);
1464 goto out;
1465 }
1466
1467 f = vxlan_find_mac(vxlan, n->ha);
1468 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1469 /* bridge-local neighbor */
1470 neigh_release(n);
1471 goto out;
1472 }
1473
David Stevens4b29dba2014-03-24 10:39:58 -04001474 reply = vxlan_na_create(skb, n,
1475 !!(f ? f->flags & NTF_ROUTER : 0));
1476
Cong Wangf564f452013-08-31 13:44:36 +08001477 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001478
1479 if (reply == NULL)
1480 goto out;
1481
1482 if (netif_rx_ni(reply) == NET_RX_DROP)
1483 dev->stats.rx_dropped++;
1484
Cong Wangf564f452013-08-31 13:44:36 +08001485 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001486 union vxlan_addr ipa = {
1487 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001488 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001489 };
1490
Cong Wangf564f452013-08-31 13:44:36 +08001491 vxlan_ip_miss(dev, &ipa);
1492 }
1493
1494out:
1495 consume_skb(skb);
1496 return NETDEV_TX_OK;
1497}
1498#endif
1499
David Stevense4f67ad2012-11-20 02:50:14 +00001500static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1501{
1502 struct vxlan_dev *vxlan = netdev_priv(dev);
1503 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001504
1505 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1506 return false;
1507
1508 n = NULL;
1509 switch (ntohs(eth_hdr(skb)->h_proto)) {
1510 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001511 {
1512 struct iphdr *pip;
1513
David Stevense4f67ad2012-11-20 02:50:14 +00001514 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1515 return false;
1516 pip = ip_hdr(skb);
1517 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001518 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1519 union vxlan_addr ipa = {
1520 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001521 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001522 };
1523
1524 vxlan_ip_miss(dev, &ipa);
1525 return false;
1526 }
1527
David Stevense4f67ad2012-11-20 02:50:14 +00001528 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001529 }
1530#if IS_ENABLED(CONFIG_IPV6)
1531 case ETH_P_IPV6:
1532 {
1533 struct ipv6hdr *pip6;
1534
1535 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1536 return false;
1537 pip6 = ipv6_hdr(skb);
1538 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1539 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1540 union vxlan_addr ipa = {
1541 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001542 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001543 };
1544
1545 vxlan_ip_miss(dev, &ipa);
1546 return false;
1547 }
1548
1549 break;
1550 }
1551#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001552 default:
1553 return false;
1554 }
1555
1556 if (n) {
1557 bool diff;
1558
Joe Perches7367d0b2013-09-01 11:51:23 -07001559 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001560 if (diff) {
1561 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1562 dev->addr_len);
1563 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1564 }
1565 neigh_release(n);
1566 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001567 }
1568
David Stevense4f67ad2012-11-20 02:50:14 +00001569 return false;
1570}
1571
Cong Wange4c7ed42013-08-31 13:44:33 +08001572#if IS_ENABLED(CONFIG_IPV6)
Nicolas Dichtel11796182013-09-02 15:34:55 +02001573static int vxlan6_xmit_skb(struct vxlan_sock *vs,
Cong Wange4c7ed42013-08-31 13:44:33 +08001574 struct dst_entry *dst, struct sk_buff *skb,
1575 struct net_device *dev, struct in6_addr *saddr,
1576 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001577 __be16 src_port, __be16 dst_port, __be32 vni,
1578 bool xnet)
Cong Wange4c7ed42013-08-31 13:44:33 +08001579{
Cong Wange4c7ed42013-08-31 13:44:33 +08001580 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001581 int min_headroom;
1582 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001583 bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001584
Andy Zhouacbf74a2014-09-16 17:31:18 -07001585 skb = udp_tunnel_handle_offloads(skb, udp_sum);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001586 if (IS_ERR(skb))
1587 return -EINVAL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001588
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001589 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001590
Cong Wange4c7ed42013-08-31 13:44:33 +08001591 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1592 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1593 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1594
1595 /* Need space for new headers (invalidates iph ptr) */
1596 err = skb_cow_head(skb, min_headroom);
1597 if (unlikely(err))
1598 return err;
1599
1600 if (vlan_tx_tag_present(skb)) {
1601 if (WARN_ON(!__vlan_put_tag(skb,
1602 skb->vlan_proto,
1603 vlan_tx_tag_get(skb))))
1604 return -ENOMEM;
1605
1606 skb->vlan_tci = 0;
1607 }
1608
1609 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1610 vxh->vx_flags = htonl(VXLAN_FLAGS);
1611 vxh->vx_vni = vni;
1612
Andy Zhouacbf74a2014-09-16 17:31:18 -07001613 udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
1614 ttl, src_port, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001615 return 0;
1616}
1617#endif
1618
Nicolas Dichtel11796182013-09-02 15:34:55 +02001619int vxlan_xmit_skb(struct vxlan_sock *vs,
Pravin B Shelar49560532013-08-19 11:23:17 -07001620 struct rtable *rt, struct sk_buff *skb,
1621 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001622 __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)
Pravin B Shelar49560532013-08-19 11:23:17 -07001623{
1624 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001625 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001626 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001627 bool udp_sum = !vs->sock->sk->sk_no_check_tx;
Pravin B Shelar49560532013-08-19 11:23:17 -07001628
Andy Zhouacbf74a2014-09-16 17:31:18 -07001629 skb = udp_tunnel_handle_offloads(skb, udp_sum);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001630 if (IS_ERR(skb))
1631 return -EINVAL;
Pravin B Shelar49560532013-08-19 11:23:17 -07001632
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001633 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001634 + VXLAN_HLEN + sizeof(struct iphdr)
1635 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001636
1637 /* Need space for new headers (invalidates iph ptr) */
1638 err = skb_cow_head(skb, min_headroom);
1639 if (unlikely(err))
1640 return err;
1641
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001642 if (vlan_tx_tag_present(skb)) {
1643 if (WARN_ON(!__vlan_put_tag(skb,
1644 skb->vlan_proto,
1645 vlan_tx_tag_get(skb))))
1646 return -ENOMEM;
1647
1648 skb->vlan_tci = 0;
1649 }
1650
Pravin B Shelar49560532013-08-19 11:23:17 -07001651 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1652 vxh->vx_flags = htonl(VXLAN_FLAGS);
1653 vxh->vx_vni = vni;
1654
Andy Zhouacbf74a2014-09-16 17:31:18 -07001655 return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
1656 ttl, df, src_port, dst_port, xnet);
Pravin B Shelar49560532013-08-19 11:23:17 -07001657}
1658EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1659
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001660/* Bypass encapsulation if the destination is local */
1661static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1662 struct vxlan_dev *dst_vxlan)
1663{
Li RongQing8f849852014-01-04 13:57:59 +08001664 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001665 union vxlan_addr loopback;
1666 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001667
Li RongQing8f849852014-01-04 13:57:59 +08001668 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1669 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001670 skb->pkt_type = PACKET_HOST;
1671 skb->encapsulation = 0;
1672 skb->dev = dst_vxlan->dev;
1673 __skb_pull(skb, skb_network_offset(skb));
1674
Cong Wange4c7ed42013-08-31 13:44:33 +08001675 if (remote_ip->sa.sa_family == AF_INET) {
1676 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1677 loopback.sa.sa_family = AF_INET;
1678#if IS_ENABLED(CONFIG_IPV6)
1679 } else {
1680 loopback.sin6.sin6_addr = in6addr_loopback;
1681 loopback.sa.sa_family = AF_INET6;
1682#endif
1683 }
1684
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001685 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001686 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001687
1688 u64_stats_update_begin(&tx_stats->syncp);
1689 tx_stats->tx_packets++;
1690 tx_stats->tx_bytes += skb->len;
1691 u64_stats_update_end(&tx_stats->syncp);
1692
1693 if (netif_rx(skb) == NET_RX_SUCCESS) {
1694 u64_stats_update_begin(&rx_stats->syncp);
1695 rx_stats->rx_packets++;
1696 rx_stats->rx_bytes += skb->len;
1697 u64_stats_update_end(&rx_stats->syncp);
1698 } else {
1699 skb->dev->stats.rx_dropped++;
1700 }
1701}
1702
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001703static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1704 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001705{
1706 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001707 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001708 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001709 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001710 union vxlan_addr *dst;
1711 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001712 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001713 __be16 df = 0;
1714 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001715 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001716
stephen hemminger823aa872013-04-27 11:31:57 +00001717 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001718 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001719 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001720
Cong Wange4c7ed42013-08-31 13:44:33 +08001721 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001722 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001723 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001724 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001725 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001726 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001727 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001728 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001729
stephen hemmingerd3428942012-10-01 12:32:35 +00001730 old_iph = ip_hdr(skb);
1731
stephen hemmingerd3428942012-10-01 12:32:35 +00001732 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001733 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001734 ttl = 1;
1735
1736 tos = vxlan->tos;
1737 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001738 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001739
Tom Herbert535fb8d2014-07-01 21:32:49 -07001740 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1741 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001742
Cong Wange4c7ed42013-08-31 13:44:33 +08001743 if (dst->sa.sa_family == AF_INET) {
1744 memset(&fl4, 0, sizeof(fl4));
1745 fl4.flowi4_oif = rdst->remote_ifindex;
1746 fl4.flowi4_tos = RT_TOS(tos);
1747 fl4.daddr = dst->sin.sin_addr.s_addr;
1748 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001749
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001750 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001751 if (IS_ERR(rt)) {
1752 netdev_dbg(dev, "no route to %pI4\n",
1753 &dst->sin.sin_addr.s_addr);
1754 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001755 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001756 }
1757
1758 if (rt->dst.dev == dev) {
1759 netdev_dbg(dev, "circular route to %pI4\n",
1760 &dst->sin.sin_addr.s_addr);
1761 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001762 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001763 }
1764
1765 /* Bypass encapsulation if the destination is local */
1766 if (rt->rt_flags & RTCF_LOCAL &&
1767 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1768 struct vxlan_dev *dst_vxlan;
1769
1770 ip_rt_put(rt);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001771 dst_vxlan = vxlan_find_vni(vxlan->net, vni, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001772 if (!dst_vxlan)
1773 goto tx_error;
1774 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1775 return;
1776 }
1777
1778 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1779 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1780
Andy Zhouacbf74a2014-09-16 17:31:18 -07001781 err = udp_tunnel_xmit_skb(vxlan->vn_sock->sock, rt, skb,
1782 fl4.saddr, dst->sin.sin_addr.s_addr,
1783 tos, ttl, df, src_port, dst_port,
1784 !net_eq(vxlan->net,
1785 dev_net(vxlan->dev)));
Cong Wange4c7ed42013-08-31 13:44:33 +08001786
1787 if (err < 0)
1788 goto rt_tx_error;
1789 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1790#if IS_ENABLED(CONFIG_IPV6)
1791 } else {
1792 struct sock *sk = vxlan->vn_sock->sock->sk;
1793 struct dst_entry *ndst;
1794 struct flowi6 fl6;
1795 u32 flags;
1796
1797 memset(&fl6, 0, sizeof(fl6));
1798 fl6.flowi6_oif = rdst->remote_ifindex;
1799 fl6.daddr = dst->sin6.sin6_addr;
1800 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08001801 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001802
1803 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1804 netdev_dbg(dev, "no route to %pI6\n",
1805 &dst->sin6.sin6_addr);
1806 dev->stats.tx_carrier_errors++;
1807 goto tx_error;
1808 }
1809
1810 if (ndst->dev == dev) {
1811 netdev_dbg(dev, "circular route to %pI6\n",
1812 &dst->sin6.sin6_addr);
1813 dst_release(ndst);
1814 dev->stats.collisions++;
1815 goto tx_error;
1816 }
1817
1818 /* Bypass encapsulation if the destination is local */
1819 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1820 if (flags & RTF_LOCAL &&
1821 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1822 struct vxlan_dev *dst_vxlan;
1823
1824 dst_release(ndst);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001825 dst_vxlan = vxlan_find_vni(vxlan->net, vni, 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 &&
1877 skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
1878 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1879 struct nd_msg *msg;
1880
1881 msg = (struct nd_msg *)skb_transport_header(skb);
1882 if (msg->icmph.icmp6_code == 0 &&
1883 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1884 return neigh_reduce(dev, skb);
1885 }
1886#endif
1887 }
David Stevens66817122013-03-15 04:35:51 +00001888
1889 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001890 did_rsc = false;
1891
1892 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08001893 (ntohs(eth->h_proto) == ETH_P_IP ||
1894 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00001895 did_rsc = route_shortcircuit(dev, skb);
1896 if (did_rsc)
1897 f = vxlan_find_mac(vxlan, eth->h_dest);
1898 }
1899
David Stevens66817122013-03-15 04:35:51 +00001900 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001901 f = vxlan_find_mac(vxlan, all_zeros_mac);
1902 if (f == NULL) {
1903 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1904 !is_multicast_ether_addr(eth->h_dest))
1905 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001906
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001907 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08001908 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001909 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001910 }
David Stevens66817122013-03-15 04:35:51 +00001911 }
1912
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001913 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1914 struct sk_buff *skb1;
1915
Eric Dumazet8f646c92014-01-06 09:54:31 -08001916 if (!fdst) {
1917 fdst = rdst;
1918 continue;
1919 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001920 skb1 = skb_clone(skb, GFP_ATOMIC);
1921 if (skb1)
1922 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1923 }
1924
Eric Dumazet8f646c92014-01-06 09:54:31 -08001925 if (fdst)
1926 vxlan_xmit_one(skb, dev, fdst, did_rsc);
1927 else
1928 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001929 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001930}
1931
stephen hemmingerd3428942012-10-01 12:32:35 +00001932/* Walk the forwarding table and purge stale entries */
1933static void vxlan_cleanup(unsigned long arg)
1934{
1935 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1936 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1937 unsigned int h;
1938
1939 if (!netif_running(vxlan->dev))
1940 return;
1941
1942 spin_lock_bh(&vxlan->hash_lock);
1943 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1944 struct hlist_node *p, *n;
1945 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1946 struct vxlan_fdb *f
1947 = container_of(p, struct vxlan_fdb, hlist);
1948 unsigned long timeout;
1949
stephen hemminger3c172862012-10-26 06:24:34 +00001950 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001951 continue;
1952
1953 timeout = f->used + vxlan->age_interval * HZ;
1954 if (time_before_eq(timeout, jiffies)) {
1955 netdev_dbg(vxlan->dev,
1956 "garbage collect %pM\n",
1957 f->eth_addr);
1958 f->state = NUD_STALE;
1959 vxlan_fdb_destroy(vxlan, f);
1960 } else if (time_before(timeout, next_timer))
1961 next_timer = timeout;
1962 }
1963 }
1964 spin_unlock_bh(&vxlan->hash_lock);
1965
1966 mod_timer(&vxlan->age_timer, next_timer);
1967}
1968
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001969static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1970{
1971 __u32 vni = vxlan->default_dst.remote_vni;
1972
1973 vxlan->vn_sock = vs;
1974 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1975}
1976
stephen hemmingerd3428942012-10-01 12:32:35 +00001977/* Setup stats when device is created */
1978static int vxlan_init(struct net_device *dev)
1979{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001980 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001981 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001982 struct vxlan_sock *vs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001983
WANG Cong1c213bd2014-02-13 11:46:28 -08001984 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00001985 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001986 return -ENOMEM;
1987
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001988 spin_lock(&vn->sock_lock);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001989 vs = vxlan_find_sock(vxlan->net, vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001990 if (vs) {
1991 /* If we have a socket with same port already, reuse it */
1992 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001993 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001994 } else {
1995 /* otherwise make new socket outside of RTNL */
1996 dev_hold(dev);
1997 queue_work(vxlan_wq, &vxlan->sock_work);
1998 }
1999 spin_unlock(&vn->sock_lock);
2000
stephen hemmingerd3428942012-10-01 12:32:35 +00002001 return 0;
2002}
2003
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002004static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002005{
2006 struct vxlan_fdb *f;
2007
2008 spin_lock_bh(&vxlan->hash_lock);
2009 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2010 if (f)
2011 vxlan_fdb_destroy(vxlan, f);
2012 spin_unlock_bh(&vxlan->hash_lock);
2013}
2014
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002015static void vxlan_uninit(struct net_device *dev)
2016{
2017 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002018 struct vxlan_sock *vs = vxlan->vn_sock;
2019
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002020 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002021
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002022 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07002023 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002024 free_percpu(dev->tstats);
2025}
2026
stephen hemmingerd3428942012-10-01 12:32:35 +00002027/* Start ageing timer and join group when device is brought up */
2028static int vxlan_open(struct net_device *dev)
2029{
2030 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002031 struct vxlan_sock *vs = vxlan->vn_sock;
2032
2033 /* socket hasn't been created */
2034 if (!vs)
2035 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002036
Gao feng79d4a942013-12-10 16:37:32 +08002037 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002038 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002039 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002040 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00002041 }
2042
2043 if (vxlan->age_interval)
2044 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2045
2046 return 0;
2047}
2048
2049/* Purge the forwarding table */
2050static void vxlan_flush(struct vxlan_dev *vxlan)
2051{
Cong Wang31fec5a2013-05-27 22:35:52 +00002052 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002053
2054 spin_lock_bh(&vxlan->hash_lock);
2055 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2056 struct hlist_node *p, *n;
2057 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2058 struct vxlan_fdb *f
2059 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002060 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2061 if (!is_zero_ether_addr(f->eth_addr))
2062 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002063 }
2064 }
2065 spin_unlock_bh(&vxlan->hash_lock);
2066}
2067
2068/* Cleanup timer and forwarding table on shutdown */
2069static int vxlan_stop(struct net_device *dev)
2070{
2071 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002072 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002073 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00002074
Cong Wange4c7ed42013-08-31 13:44:33 +08002075 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002076 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002077 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002078 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002079 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002080 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002081
2082 del_timer_sync(&vxlan->age_timer);
2083
2084 vxlan_flush(vxlan);
2085
2086 return 0;
2087}
2088
stephen hemmingerd3428942012-10-01 12:32:35 +00002089/* Stub, nothing needs to be done. */
2090static void vxlan_set_multicast_list(struct net_device *dev)
2091{
2092}
2093
Daniel Borkmann345010b2013-12-18 00:21:08 +01002094static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2095{
2096 struct vxlan_dev *vxlan = netdev_priv(dev);
2097 struct vxlan_rdst *dst = &vxlan->default_dst;
2098 struct net_device *lowerdev;
2099 int max_mtu;
2100
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002101 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002102 if (lowerdev == NULL)
2103 return eth_change_mtu(dev, new_mtu);
2104
2105 if (dst->remote_ip.sa.sa_family == AF_INET6)
2106 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2107 else
2108 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2109
2110 if (new_mtu < 68 || new_mtu > max_mtu)
2111 return -EINVAL;
2112
2113 dev->mtu = new_mtu;
2114 return 0;
2115}
2116
stephen hemmingerd3428942012-10-01 12:32:35 +00002117static const struct net_device_ops vxlan_netdev_ops = {
2118 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002119 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002120 .ndo_open = vxlan_open,
2121 .ndo_stop = vxlan_stop,
2122 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002123 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002124 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002125 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002126 .ndo_validate_addr = eth_validate_addr,
2127 .ndo_set_mac_address = eth_mac_addr,
2128 .ndo_fdb_add = vxlan_fdb_add,
2129 .ndo_fdb_del = vxlan_fdb_delete,
2130 .ndo_fdb_dump = vxlan_fdb_dump,
2131};
2132
2133/* Info for udev, that this is a virtual tunnel endpoint */
2134static struct device_type vxlan_type = {
2135 .name = "vxlan",
2136};
2137
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002138/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002139 * supply the listening VXLAN udp ports. Callers are expected
2140 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002141 */
2142void vxlan_get_rx_port(struct net_device *dev)
2143{
2144 struct vxlan_sock *vs;
2145 struct net *net = dev_net(dev);
2146 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2147 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002148 __be16 port;
2149 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002150
2151 spin_lock(&vn->sock_lock);
2152 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002153 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2154 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002155 sa_family = vs->sock->sk->sk_family;
2156 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2157 port);
2158 }
2159 }
2160 spin_unlock(&vn->sock_lock);
2161}
2162EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2163
stephen hemmingerd3428942012-10-01 12:32:35 +00002164/* Initialize the device structure. */
2165static void vxlan_setup(struct net_device *dev)
2166{
2167 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002168 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002169
2170 eth_hw_addr_random(dev);
2171 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002172 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002173 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002174 else
Cong Wang2853af62014-06-12 11:53:10 -07002175 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002176
2177 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002178 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002179 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2180
2181 dev->tx_queue_len = 0;
2182 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002183 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002184 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002185 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002186
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002187 dev->vlan_features = dev->features;
2188 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002189 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002190 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002191 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
stephen hemmingerd3428942012-10-01 12:32:35 +00002192 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00002193 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002194
stephen hemminger553675f2013-05-16 11:35:20 +00002195 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002196 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002197 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2198 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002199 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002200
2201 init_timer_deferrable(&vxlan->age_timer);
2202 vxlan->age_timer.function = vxlan_cleanup;
2203 vxlan->age_timer.data = (unsigned long) vxlan;
2204
stephen hemminger823aa872013-04-27 11:31:57 +00002205 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002206
stephen hemmingerd3428942012-10-01 12:32:35 +00002207 vxlan->dev = dev;
2208
2209 for (h = 0; h < FDB_HASH_SIZE; ++h)
2210 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2211}
2212
2213static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2214 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002215 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002216 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002217 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2218 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002219 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002220 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2221 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2222 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2223 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2224 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002225 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002226 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2227 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2228 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2229 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002230 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002231};
2232
2233static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2234{
2235 if (tb[IFLA_ADDRESS]) {
2236 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2237 pr_debug("invalid link address (not ethernet)\n");
2238 return -EINVAL;
2239 }
2240
2241 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2242 pr_debug("invalid all zero ethernet address\n");
2243 return -EADDRNOTAVAIL;
2244 }
2245 }
2246
2247 if (!data)
2248 return -EINVAL;
2249
2250 if (data[IFLA_VXLAN_ID]) {
2251 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2252 if (id >= VXLAN_VID_MASK)
2253 return -ERANGE;
2254 }
2255
stephen hemminger05f47d62012-10-09 20:35:50 +00002256 if (data[IFLA_VXLAN_PORT_RANGE]) {
2257 const struct ifla_vxlan_port_range *p
2258 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2259
2260 if (ntohs(p->high) < ntohs(p->low)) {
2261 pr_debug("port range %u .. %u not valid\n",
2262 ntohs(p->low), ntohs(p->high));
2263 return -EINVAL;
2264 }
2265 }
2266
stephen hemmingerd3428942012-10-01 12:32:35 +00002267 return 0;
2268}
2269
Yan Burman1b13c972013-01-29 23:43:07 +00002270static void vxlan_get_drvinfo(struct net_device *netdev,
2271 struct ethtool_drvinfo *drvinfo)
2272{
2273 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2274 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2275}
2276
2277static const struct ethtool_ops vxlan_ethtool_ops = {
2278 .get_drvinfo = vxlan_get_drvinfo,
2279 .get_link = ethtool_op_get_link,
2280};
2281
stephen hemminger553675f2013-05-16 11:35:20 +00002282static void vxlan_del_work(struct work_struct *work)
2283{
2284 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002285 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002286 kfree_rcu(vs, rcu);
2287}
2288
Tom Herbert3ee64f32014-07-13 19:49:42 -07002289static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2290 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002291{
Cong Wange4c7ed42013-08-31 13:44:33 +08002292 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002293 struct udp_port_cfg udp_conf;
2294 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002295
Tom Herbert3ee64f32014-07-13 19:49:42 -07002296 memset(&udp_conf, 0, sizeof(udp_conf));
2297
2298 if (ipv6) {
2299 udp_conf.family = AF_INET6;
2300 udp_conf.use_udp6_tx_checksums =
2301 !!(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2302 udp_conf.use_udp6_rx_checksums =
2303 !!(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2304 } else {
2305 udp_conf.family = AF_INET;
2306 udp_conf.local_ip.s_addr = INADDR_ANY;
2307 udp_conf.use_udp_checksums =
2308 !!(flags & VXLAN_F_UDP_CSUM);
Cong Wange4c7ed42013-08-31 13:44:33 +08002309 }
2310
Tom Herbert3ee64f32014-07-13 19:49:42 -07002311 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002312
Tom Herbert3ee64f32014-07-13 19:49:42 -07002313 /* Open UDP socket */
2314 err = udp_sock_create(net, &udp_conf, &sock);
2315 if (err < 0)
2316 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002317
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002318 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002319}
2320
2321/* Create new listen socket if needed */
2322static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002323 vxlan_rcv_t *rcv, void *data,
2324 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002325{
2326 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2327 struct vxlan_sock *vs;
2328 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002329 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002330 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002331 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002332
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002333 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002334 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002335 return ERR_PTR(-ENOMEM);
2336
2337 for (h = 0; h < VNI_HASH_SIZE; ++h)
2338 INIT_HLIST_HEAD(&vs->vni_list[h]);
2339
2340 INIT_WORK(&vs->del_work, vxlan_del_work);
2341
Tom Herbert3ee64f32014-07-13 19:49:42 -07002342 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002343 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002344 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002345 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002346 }
2347
Cong Wange4c7ed42013-08-31 13:44:33 +08002348 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002349 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002350 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002351 vs->data = data;
stephen hemminger553675f2013-05-16 11:35:20 +00002352
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002353 /* Initialize the vxlan udp offloads structure */
2354 vs->udp_offloads.port = port;
2355 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2356 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2357
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002358 spin_lock(&vn->sock_lock);
2359 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002360 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002361 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002362
2363 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002364 tunnel_cfg.sk_user_data = vs;
2365 tunnel_cfg.encap_type = 1;
2366 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2367 tunnel_cfg.encap_destroy = NULL;
2368
2369 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002370
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002371 return vs;
2372}
stephen hemminger553675f2013-05-16 11:35:20 +00002373
Pravin B Shelar012a5722013-08-19 11:23:07 -07002374struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2375 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002376 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002377{
2378 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2379 struct vxlan_sock *vs;
2380
Tom Herbert359a0ea2014-06-04 17:20:29 -07002381 vs = vxlan_socket_create(net, port, rcv, data, flags);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002382 if (!IS_ERR(vs))
2383 return vs;
2384
Pravin B Shelar012a5722013-08-19 11:23:07 -07002385 if (no_share) /* Return error if sharing is not allowed. */
2386 return vs;
2387
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002388 spin_lock(&vn->sock_lock);
2389 vs = vxlan_find_sock(net, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002390 if (vs) {
2391 if (vs->rcv == rcv)
2392 atomic_inc(&vs->refcnt);
2393 else
2394 vs = ERR_PTR(-EBUSY);
2395 }
2396 spin_unlock(&vn->sock_lock);
2397
2398 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002399 vs = ERR_PTR(-EINVAL);
2400
stephen hemminger553675f2013-05-16 11:35:20 +00002401 return vs;
2402}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002403EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002404
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002405/* Scheduled at device creation to bind to a socket */
2406static void vxlan_sock_work(struct work_struct *work)
2407{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002408 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002409 struct net *net = vxlan->net;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002410 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002411 __be16 port = vxlan->dst_port;
2412 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002413
Tom Herbert359a0ea2014-06-04 17:20:29 -07002414 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002415 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002416 if (!IS_ERR(nvs))
2417 vxlan_vs_add_dev(nvs, vxlan);
2418 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002419
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002420 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002421}
2422
stephen hemmingerd3428942012-10-01 12:32:35 +00002423static int vxlan_newlink(struct net *net, struct net_device *dev,
2424 struct nlattr *tb[], struct nlattr *data[])
2425{
stephen hemminger553675f2013-05-16 11:35:20 +00002426 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002427 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002428 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002429 __u32 vni;
2430 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002431 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002432
2433 if (!data[IFLA_VXLAN_ID])
2434 return -EINVAL;
2435
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002436 vxlan->net = dev_net(dev);
2437
stephen hemmingerd3428942012-10-01 12:32:35 +00002438 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002439 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002440
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002441 /* Unless IPv6 is explicitly requested, assume IPv4 */
2442 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002443 if (data[IFLA_VXLAN_GROUP]) {
2444 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002445 } else if (data[IFLA_VXLAN_GROUP6]) {
2446 if (!IS_ENABLED(CONFIG_IPV6))
2447 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002448
Cong Wange4c7ed42013-08-31 13:44:33 +08002449 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2450 sizeof(struct in6_addr));
2451 dst->remote_ip.sa.sa_family = AF_INET6;
2452 use_ipv6 = true;
2453 }
2454
2455 if (data[IFLA_VXLAN_LOCAL]) {
2456 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2457 vxlan->saddr.sa.sa_family = AF_INET;
2458 } else if (data[IFLA_VXLAN_LOCAL6]) {
2459 if (!IS_ENABLED(CONFIG_IPV6))
2460 return -EPFNOSUPPORT;
2461
2462 /* TODO: respect scope id */
2463 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2464 sizeof(struct in6_addr));
2465 vxlan->saddr.sa.sa_family = AF_INET6;
2466 use_ipv6 = true;
2467 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002468
stephen hemminger34e02aa2012-10-09 20:35:53 +00002469 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002470 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002471 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002472 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002473
stephen hemminger34e02aa2012-10-09 20:35:53 +00002474 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002475 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002476 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002477 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002478
Cong Wange4c7ed42013-08-31 13:44:33 +08002479#if IS_ENABLED(CONFIG_IPV6)
2480 if (use_ipv6) {
2481 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2482 if (idev && idev->cnf.disable_ipv6) {
2483 pr_info("IPv6 is disabled via sysctl\n");
2484 return -EPERM;
2485 }
2486 vxlan->flags |= VXLAN_F_IPV6;
2487 }
2488#endif
2489
stephen hemminger34e02aa2012-10-09 20:35:53 +00002490 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002491 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002492
Cong Wang2853af62014-06-12 11:53:10 -07002493 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002494 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002495 } else if (use_ipv6)
2496 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002497
2498 if (data[IFLA_VXLAN_TOS])
2499 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2500
Vincent Bernatafb97182012-10-30 10:27:16 +00002501 if (data[IFLA_VXLAN_TTL])
2502 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2503
stephen hemmingerd3428942012-10-01 12:32:35 +00002504 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002505 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002506
2507 if (data[IFLA_VXLAN_AGEING])
2508 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2509 else
2510 vxlan->age_interval = FDB_AGE_DEFAULT;
2511
David Stevense4f67ad2012-11-20 02:50:14 +00002512 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2513 vxlan->flags |= VXLAN_F_PROXY;
2514
2515 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2516 vxlan->flags |= VXLAN_F_RSC;
2517
2518 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2519 vxlan->flags |= VXLAN_F_L2MISS;
2520
2521 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2522 vxlan->flags |= VXLAN_F_L3MISS;
2523
stephen hemmingerd3428942012-10-01 12:32:35 +00002524 if (data[IFLA_VXLAN_LIMIT])
2525 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2526
stephen hemminger05f47d62012-10-09 20:35:50 +00002527 if (data[IFLA_VXLAN_PORT_RANGE]) {
2528 const struct ifla_vxlan_port_range *p
2529 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2530 vxlan->port_min = ntohs(p->low);
2531 vxlan->port_max = ntohs(p->high);
2532 }
2533
stephen hemminger823aa872013-04-27 11:31:57 +00002534 if (data[IFLA_VXLAN_PORT])
2535 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2536
Tom Herbert359a0ea2014-06-04 17:20:29 -07002537 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2538 vxlan->flags |= VXLAN_F_UDP_CSUM;
2539
2540 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2541 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2542 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2543
2544 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2545 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2546 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2547
stephen hemminger553675f2013-05-16 11:35:20 +00002548 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
2549 pr_info("duplicate VNI %u\n", vni);
2550 return -EEXIST;
2551 }
2552
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002553 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002554
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002555 /* create an fdb entry for a valid default destination */
2556 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2557 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2558 &vxlan->default_dst.remote_ip,
2559 NUD_REACHABLE|NUD_PERMANENT,
2560 NLM_F_EXCL|NLM_F_CREATE,
2561 vxlan->dst_port,
2562 vxlan->default_dst.remote_vni,
2563 vxlan->default_dst.remote_ifindex,
2564 NTF_SELF);
2565 if (err)
2566 return err;
2567 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002568
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002569 err = register_netdevice(dev);
2570 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002571 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002572 return err;
2573 }
2574
stephen hemminger553675f2013-05-16 11:35:20 +00002575 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002576
2577 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002578}
2579
2580static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2581{
2582 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002583 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002584
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002585 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002586 if (!hlist_unhashed(&vxlan->hlist))
2587 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002588 spin_unlock(&vn->sock_lock);
2589
stephen hemminger553675f2013-05-16 11:35:20 +00002590 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002591 unregister_netdevice_queue(dev, head);
2592}
2593
2594static size_t vxlan_get_size(const struct net_device *dev)
2595{
2596
2597 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002598 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002599 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002600 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002601 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2602 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2603 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002604 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2605 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2606 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2607 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002608 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2609 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002610 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002611 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2612 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2613 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2614 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002615 0;
2616}
2617
2618static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2619{
2620 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002621 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002622 struct ifla_vxlan_port_range ports = {
2623 .low = htons(vxlan->port_min),
2624 .high = htons(vxlan->port_max),
2625 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002626
Atzm Watanabec7995c42013-04-16 02:50:52 +00002627 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002628 goto nla_put_failure;
2629
Cong Wange4c7ed42013-08-31 13:44:33 +08002630 if (!vxlan_addr_any(&dst->remote_ip)) {
2631 if (dst->remote_ip.sa.sa_family == AF_INET) {
2632 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2633 dst->remote_ip.sin.sin_addr.s_addr))
2634 goto nla_put_failure;
2635#if IS_ENABLED(CONFIG_IPV6)
2636 } else {
2637 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2638 &dst->remote_ip.sin6.sin6_addr))
2639 goto nla_put_failure;
2640#endif
2641 }
2642 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002643
Atzm Watanabec7995c42013-04-16 02:50:52 +00002644 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002645 goto nla_put_failure;
2646
Cong Wange4c7ed42013-08-31 13:44:33 +08002647 if (!vxlan_addr_any(&vxlan->saddr)) {
2648 if (vxlan->saddr.sa.sa_family == AF_INET) {
2649 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2650 vxlan->saddr.sin.sin_addr.s_addr))
2651 goto nla_put_failure;
2652#if IS_ENABLED(CONFIG_IPV6)
2653 } else {
2654 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2655 &vxlan->saddr.sin6.sin6_addr))
2656 goto nla_put_failure;
2657#endif
2658 }
2659 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002660
2661 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2662 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002663 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2664 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2665 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2666 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2667 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2668 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2669 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2670 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2671 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002672 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002673 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002674 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2675 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2676 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2677 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2678 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2679 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2680 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002681 goto nla_put_failure;
2682
stephen hemminger05f47d62012-10-09 20:35:50 +00002683 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2684 goto nla_put_failure;
2685
stephen hemmingerd3428942012-10-01 12:32:35 +00002686 return 0;
2687
2688nla_put_failure:
2689 return -EMSGSIZE;
2690}
2691
2692static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2693 .kind = "vxlan",
2694 .maxtype = IFLA_VXLAN_MAX,
2695 .policy = vxlan_policy,
2696 .priv_size = sizeof(struct vxlan_dev),
2697 .setup = vxlan_setup,
2698 .validate = vxlan_validate,
2699 .newlink = vxlan_newlink,
2700 .dellink = vxlan_dellink,
2701 .get_size = vxlan_get_size,
2702 .fill_info = vxlan_fill_info,
2703};
2704
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002705static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2706 struct net_device *dev)
2707{
2708 struct vxlan_dev *vxlan, *next;
2709 LIST_HEAD(list_kill);
2710
2711 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2712 struct vxlan_rdst *dst = &vxlan->default_dst;
2713
2714 /* In case we created vxlan device with carrier
2715 * and we loose the carrier due to module unload
2716 * we also need to remove vxlan device. In other
2717 * cases, it's not necessary and remote_ifindex
2718 * is 0 here, so no matches.
2719 */
2720 if (dst->remote_ifindex == dev->ifindex)
2721 vxlan_dellink(vxlan->dev, &list_kill);
2722 }
2723
2724 unregister_netdevice_many(&list_kill);
2725}
2726
2727static int vxlan_lowerdev_event(struct notifier_block *unused,
2728 unsigned long event, void *ptr)
2729{
2730 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002731 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002732
Daniel Borkmann783c1462014-01-22 21:07:53 +01002733 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002734 vxlan_handle_lowerdev_unregister(vn, dev);
2735
2736 return NOTIFY_DONE;
2737}
2738
2739static struct notifier_block vxlan_notifier_block __read_mostly = {
2740 .notifier_call = vxlan_lowerdev_event,
2741};
2742
stephen hemmingerd3428942012-10-01 12:32:35 +00002743static __net_init int vxlan_init_net(struct net *net)
2744{
2745 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002746 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002747
stephen hemminger553675f2013-05-16 11:35:20 +00002748 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002749 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002750
stephen hemminger553675f2013-05-16 11:35:20 +00002751 for (h = 0; h < PORT_HASH_SIZE; ++h)
2752 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002753
2754 return 0;
2755}
2756
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002757static void __net_exit vxlan_exit_net(struct net *net)
2758{
2759 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2760 struct vxlan_dev *vxlan, *next;
2761 struct net_device *dev, *aux;
2762 LIST_HEAD(list);
2763
2764 rtnl_lock();
2765 for_each_netdev_safe(net, dev, aux)
2766 if (dev->rtnl_link_ops == &vxlan_link_ops)
2767 unregister_netdevice_queue(dev, &list);
2768
2769 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2770 /* If vxlan->dev is in the same netns, it has already been added
2771 * to the list by the previous loop.
2772 */
2773 if (!net_eq(dev_net(vxlan->dev), net))
2774 unregister_netdevice_queue(dev, &list);
2775 }
2776
2777 unregister_netdevice_many(&list);
2778 rtnl_unlock();
2779}
2780
stephen hemmingerd3428942012-10-01 12:32:35 +00002781static struct pernet_operations vxlan_net_ops = {
2782 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002783 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002784 .id = &vxlan_net_id,
2785 .size = sizeof(struct vxlan_net),
2786};
2787
2788static int __init vxlan_init_module(void)
2789{
2790 int rc;
2791
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002792 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2793 if (!vxlan_wq)
2794 return -ENOMEM;
2795
stephen hemmingerd3428942012-10-01 12:32:35 +00002796 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2797
Daniel Borkmann783c1462014-01-22 21:07:53 +01002798 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00002799 if (rc)
2800 goto out1;
2801
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002802 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002803 if (rc)
2804 goto out2;
2805
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002806 rc = rtnl_link_register(&vxlan_link_ops);
2807 if (rc)
2808 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00002809
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002810 return 0;
2811out3:
2812 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002813out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01002814 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00002815out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002816 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002817 return rc;
2818}
Cong Wang7332a132013-05-27 22:35:53 +00002819late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002820
2821static void __exit vxlan_cleanup_module(void)
2822{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002823 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002824 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002825 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002826 unregister_pernet_subsys(&vxlan_net_ops);
2827 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00002828}
2829module_exit(vxlan_cleanup_module);
2830
2831MODULE_LICENSE("GPL");
2832MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002833MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08002834MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00002835MODULE_ALIAS_RTNL_LINK("vxlan");