blob: c833763fe5c49924c2ad88c50564e660af4e9713 [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>
36#include <net/rtnetlink.h>
37#include <net/route.h>
38#include <net/dsfield.h>
39#include <net/inet_ecn.h>
40#include <net/net_namespace.h>
41#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070042#include <net/vxlan.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080043#if IS_ENABLED(CONFIG_IPV6)
44#include <net/ipv6.h>
45#include <net/addrconf.h>
46#include <net/ip6_tunnel.h>
47#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000048
49#define VXLAN_VERSION "0.1"
50
stephen hemminger553675f2013-05-16 11:35:20 +000051#define PORT_HASH_BITS 8
52#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000053#define VNI_HASH_BITS 10
54#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
55#define FDB_HASH_BITS 8
56#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
57#define FDB_AGE_DEFAULT 300 /* 5 min */
58#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
59
60#define VXLAN_N_VID (1u << 24)
61#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000062/* IP header + UDP + VXLAN + Ethernet header */
63#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
Cong Wange4c7ed42013-08-31 13:44:33 +080064/* IPv6 header + UDP + VXLAN + Ethernet header */
65#define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
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;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000132 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800133 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000134 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000135 __u16 port_min; /* source port range */
136 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000137 __u8 tos; /* TOS override */
138 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000139 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000140
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700141 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700142 struct work_struct igmp_join;
143 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700144
stephen hemmingerd3428942012-10-01 12:32:35 +0000145 unsigned long age_interval;
146 struct timer_list age_timer;
147 spinlock_t hash_lock;
148 unsigned int addrcnt;
149 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000150
151 struct hlist_head fdb_head[FDB_HASH_SIZE];
152};
153
David Stevense4f67ad2012-11-20 02:50:14 +0000154#define VXLAN_F_LEARN 0x01
155#define VXLAN_F_PROXY 0x02
156#define VXLAN_F_RSC 0x04
157#define VXLAN_F_L2MISS 0x08
158#define VXLAN_F_L3MISS 0x10
Cong Wange4c7ed42013-08-31 13:44:33 +0800159#define VXLAN_F_IPV6 0x20 /* internal flag */
David Stevense4f67ad2012-11-20 02:50:14 +0000160
stephen hemmingerd3428942012-10-01 12:32:35 +0000161/* salt for hash table */
162static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700163static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000164
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700165static void vxlan_sock_work(struct work_struct *work);
166
Cong Wange4c7ed42013-08-31 13:44:33 +0800167#if IS_ENABLED(CONFIG_IPV6)
168static inline
169bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
170{
171 if (a->sa.sa_family != b->sa.sa_family)
172 return false;
173 if (a->sa.sa_family == AF_INET6)
174 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
175 else
176 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
177}
178
179static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
180{
181 if (ipa->sa.sa_family == AF_INET6)
182 return ipv6_addr_any(&ipa->sin6.sin6_addr);
183 else
184 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
185}
186
187static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
188{
189 if (ipa->sa.sa_family == AF_INET6)
190 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
191 else
192 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
193}
194
195static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
196{
197 if (nla_len(nla) >= sizeof(struct in6_addr)) {
198 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
199 ip->sa.sa_family = AF_INET6;
200 return 0;
201 } else if (nla_len(nla) >= sizeof(__be32)) {
202 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
203 ip->sa.sa_family = AF_INET;
204 return 0;
205 } else {
206 return -EAFNOSUPPORT;
207 }
208}
209
210static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
211 const union vxlan_addr *ip)
212{
213 if (ip->sa.sa_family == AF_INET6)
214 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
215 else
216 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
217}
218
219#else /* !CONFIG_IPV6 */
220
221static inline
222bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
223{
224 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
225}
226
227static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
228{
229 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
230}
231
232static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
233{
234 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
235}
236
237static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
238{
239 if (nla_len(nla) >= sizeof(struct in6_addr)) {
240 return -EAFNOSUPPORT;
241 } else if (nla_len(nla) >= sizeof(__be32)) {
242 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
243 ip->sa.sa_family = AF_INET;
244 return 0;
245 } else {
246 return -EAFNOSUPPORT;
247 }
248}
249
250static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
251 const union vxlan_addr *ip)
252{
253 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
254}
255#endif
256
stephen hemminger553675f2013-05-16 11:35:20 +0000257/* Virtual Network hash table head */
258static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
259{
260 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
261}
262
263/* Socket hash table head */
264static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000265{
266 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
267
stephen hemminger553675f2013-05-16 11:35:20 +0000268 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
269}
270
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700271/* First remote destination for a forwarding entry.
272 * Guaranteed to be non-NULL because remotes are never deleted.
273 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700274static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700275{
stephen hemminger5ca54612013-08-04 17:17:39 -0700276 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
277}
278
279static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
280{
281 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700282}
283
stephen hemminger553675f2013-05-16 11:35:20 +0000284/* Find VXLAN socket based on network namespace and UDP port */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -0700285static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000286{
287 struct vxlan_sock *vs;
288
289 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
290 if (inet_sk(vs->sock->sk)->inet_sport == port)
291 return vs;
292 }
293 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000294}
295
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700296static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000297{
298 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000299
stephen hemminger553675f2013-05-16 11:35:20 +0000300 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000301 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000302 return vxlan;
303 }
304
305 return NULL;
306}
307
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700308/* Look up VNI in a per net namespace table */
309static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
310{
311 struct vxlan_sock *vs;
312
313 vs = vxlan_find_sock(net, port);
314 if (!vs)
315 return NULL;
316
317 return vxlan_vs_find_vni(vs, id);
318}
319
stephen hemmingerd3428942012-10-01 12:32:35 +0000320/* Fill in neighbour message in skbuff. */
321static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700322 const struct vxlan_fdb *fdb,
323 u32 portid, u32 seq, int type, unsigned int flags,
324 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000325{
326 unsigned long now = jiffies;
327 struct nda_cacheinfo ci;
328 struct nlmsghdr *nlh;
329 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000330 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000331
332 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
333 if (nlh == NULL)
334 return -EMSGSIZE;
335
336 ndm = nlmsg_data(nlh);
337 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000338
339 send_eth = send_ip = true;
340
341 if (type == RTM_GETNEIGH) {
342 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800343 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000344 send_eth = !is_zero_ether_addr(fdb->eth_addr);
345 } else
346 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000347 ndm->ndm_state = fdb->state;
348 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000349 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000350 ndm->ndm_type = NDA_DST;
351
David Stevense4f67ad2012-11-20 02:50:14 +0000352 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000353 goto nla_put_failure;
354
Cong Wange4c7ed42013-08-31 13:44:33 +0800355 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000356 goto nla_put_failure;
357
stephen hemminger823aa872013-04-27 11:31:57 +0000358 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000359 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
360 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000361 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700362 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000363 goto nla_put_failure;
364 if (rdst->remote_ifindex &&
365 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000366 goto nla_put_failure;
367
368 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
369 ci.ndm_confirmed = 0;
370 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
371 ci.ndm_refcnt = 0;
372
373 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
374 goto nla_put_failure;
375
376 return nlmsg_end(skb, nlh);
377
378nla_put_failure:
379 nlmsg_cancel(skb, nlh);
380 return -EMSGSIZE;
381}
382
383static inline size_t vxlan_nlmsg_size(void)
384{
385 return NLMSG_ALIGN(sizeof(struct ndmsg))
386 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800387 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000388 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000389 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
390 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000391 + nla_total_size(sizeof(struct nda_cacheinfo));
392}
393
394static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700395 struct vxlan_fdb *fdb, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000396{
397 struct net *net = dev_net(vxlan->dev);
398 struct sk_buff *skb;
399 int err = -ENOBUFS;
400
401 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
402 if (skb == NULL)
403 goto errout;
404
stephen hemminger5ca54612013-08-04 17:17:39 -0700405 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
406 first_remote_rtnl(fdb));
stephen hemmingerd3428942012-10-01 12:32:35 +0000407 if (err < 0) {
408 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
409 WARN_ON(err == -EMSGSIZE);
410 kfree_skb(skb);
411 goto errout;
412 }
413
414 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
415 return;
416errout:
417 if (err < 0)
418 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
419}
420
Cong Wange4c7ed42013-08-31 13:44:33 +0800421static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000422{
423 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700424 struct vxlan_fdb f = {
425 .state = NUD_STALE,
426 };
427 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800428 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700429 .remote_vni = VXLAN_N_VID,
430 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700431
432 INIT_LIST_HEAD(&f.remotes);
433 list_add_rcu(&remote.list, &f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000434
435 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
436}
437
438static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
439{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700440 struct vxlan_fdb f = {
441 .state = NUD_STALE,
442 };
David Stevense4f67ad2012-11-20 02:50:14 +0000443
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700444 INIT_LIST_HEAD(&f.remotes);
David Stevense4f67ad2012-11-20 02:50:14 +0000445 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
446
447 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
448}
449
stephen hemmingerd3428942012-10-01 12:32:35 +0000450/* Hash Ethernet address */
451static u32 eth_hash(const unsigned char *addr)
452{
453 u64 value = get_unaligned((u64 *)addr);
454
455 /* only want 6 bytes */
456#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000457 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000458#else
459 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000460#endif
461 return hash_64(value, FDB_HASH_BITS);
462}
463
464/* Hash chain to use given mac address */
465static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
466 const u8 *mac)
467{
468 return &vxlan->fdb_head[eth_hash(mac)];
469}
470
471/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000472static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000473 const u8 *mac)
474
475{
476 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
477 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000478
Sasha Levinb67bfe02013-02-27 17:06:00 -0800479 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000480 if (compare_ether_addr(mac, f->eth_addr) == 0)
481 return f;
482 }
483
484 return NULL;
485}
486
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000487static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
488 const u8 *mac)
489{
490 struct vxlan_fdb *f;
491
492 f = __vxlan_find_mac(vxlan, mac);
493 if (f)
494 f->used = jiffies;
495
496 return f;
497}
498
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300499/* caller should hold vxlan->hash_lock */
500static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800501 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300502 __u32 vni, __u32 ifindex)
503{
504 struct vxlan_rdst *rd;
505
506 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800507 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300508 rd->remote_port == port &&
509 rd->remote_vni == vni &&
510 rd->remote_ifindex == ifindex)
511 return rd;
512 }
513
514 return NULL;
515}
516
Thomas Richter906dc182013-07-19 17:20:07 +0200517/* Replace destination of unicast mac */
518static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800519 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200520{
521 struct vxlan_rdst *rd;
522
523 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
524 if (rd)
525 return 0;
526
527 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
528 if (!rd)
529 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800530 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200531 rd->remote_port = port;
532 rd->remote_vni = vni;
533 rd->remote_ifindex = ifindex;
534 return 1;
535}
536
David Stevens66817122013-03-15 04:35:51 +0000537/* Add/update destinations for multicast */
538static int vxlan_fdb_append(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800539 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000540{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700541 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000542
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300543 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
544 if (rd)
545 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700546
David Stevens66817122013-03-15 04:35:51 +0000547 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
548 if (rd == NULL)
549 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800550 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000551 rd->remote_port = port;
552 rd->remote_vni = vni;
553 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700554
555 list_add_tail_rcu(&rd->list, &f->remotes);
556
David Stevens66817122013-03-15 04:35:51 +0000557 return 1;
558}
559
stephen hemmingerd3428942012-10-01 12:32:35 +0000560/* Add new entry to forwarding table -- assumes lock held */
561static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800562 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000563 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000564 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000565 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000566{
567 struct vxlan_fdb *f;
568 int notify = 0;
569
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000570 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000571 if (f) {
572 if (flags & NLM_F_EXCL) {
573 netdev_dbg(vxlan->dev,
574 "lost race to create %pM\n", mac);
575 return -EEXIST;
576 }
577 if (f->state != state) {
578 f->state = state;
579 f->updated = jiffies;
580 notify = 1;
581 }
David Stevensae884082013-04-19 00:36:26 +0000582 if (f->flags != ndm_flags) {
583 f->flags = ndm_flags;
584 f->updated = jiffies;
585 notify = 1;
586 }
Thomas Richter906dc182013-07-19 17:20:07 +0200587 if ((flags & NLM_F_REPLACE)) {
588 /* Only change unicasts */
589 if (!(is_multicast_ether_addr(f->eth_addr) ||
590 is_zero_ether_addr(f->eth_addr))) {
591 int rc = vxlan_fdb_replace(f, ip, port, vni,
592 ifindex);
593
594 if (rc < 0)
595 return rc;
596 notify |= rc;
597 } else
598 return -EOPNOTSUPP;
599 }
David Stevens66817122013-03-15 04:35:51 +0000600 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300601 (is_multicast_ether_addr(f->eth_addr) ||
602 is_zero_ether_addr(f->eth_addr))) {
David Stevens66817122013-03-15 04:35:51 +0000603 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
604
605 if (rc < 0)
606 return rc;
607 notify |= rc;
608 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000609 } else {
610 if (!(flags & NLM_F_CREATE))
611 return -ENOENT;
612
613 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
614 return -ENOSPC;
615
Thomas Richter906dc182013-07-19 17:20:07 +0200616 /* Disallow replace to add a multicast entry */
617 if ((flags & NLM_F_REPLACE) &&
618 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
619 return -EOPNOTSUPP;
620
Cong Wange4c7ed42013-08-31 13:44:33 +0800621 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000622 f = kmalloc(sizeof(*f), GFP_ATOMIC);
623 if (!f)
624 return -ENOMEM;
625
626 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000627 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000628 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000629 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700630 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000631 memcpy(f->eth_addr, mac, ETH_ALEN);
632
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700633 vxlan_fdb_append(f, ip, port, vni, ifindex);
634
stephen hemmingerd3428942012-10-01 12:32:35 +0000635 ++vxlan->addrcnt;
636 hlist_add_head_rcu(&f->hlist,
637 vxlan_fdb_head(vxlan, mac));
638 }
639
640 if (notify)
641 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
642
643 return 0;
644}
645
Wei Yongjun6706c822013-04-11 19:00:35 +0000646static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000647{
648 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700649 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000650
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700651 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000652 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000653 kfree(f);
654}
655
stephen hemmingerd3428942012-10-01 12:32:35 +0000656static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
657{
658 netdev_dbg(vxlan->dev,
659 "delete %pM\n", f->eth_addr);
660
661 --vxlan->addrcnt;
662 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
663
664 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000665 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000666}
667
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300668static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800669 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300670{
671 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800672 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300673
674 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800675 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
676 if (err)
677 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300678 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800679 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
680 if (remote->sa.sa_family == AF_INET) {
681 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
682 ip->sa.sa_family = AF_INET;
683#if IS_ENABLED(CONFIG_IPV6)
684 } else {
685 ip->sin6.sin6_addr = in6addr_any;
686 ip->sa.sa_family = AF_INET6;
687#endif
688 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300689 }
690
691 if (tb[NDA_PORT]) {
692 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
693 return -EINVAL;
694 *port = nla_get_be16(tb[NDA_PORT]);
695 } else {
696 *port = vxlan->dst_port;
697 }
698
699 if (tb[NDA_VNI]) {
700 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
701 return -EINVAL;
702 *vni = nla_get_u32(tb[NDA_VNI]);
703 } else {
704 *vni = vxlan->default_dst.remote_vni;
705 }
706
707 if (tb[NDA_IFINDEX]) {
708 struct net_device *tdev;
709
710 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
711 return -EINVAL;
712 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
713 tdev = dev_get_by_index(net, *ifindex);
714 if (!tdev)
715 return -EADDRNOTAVAIL;
716 dev_put(tdev);
717 } else {
718 *ifindex = 0;
719 }
720
721 return 0;
722}
723
stephen hemmingerd3428942012-10-01 12:32:35 +0000724/* Add static entry (via netlink) */
725static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
726 struct net_device *dev,
727 const unsigned char *addr, u16 flags)
728{
729 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300730 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800731 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000732 __be16 port;
733 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000734 int err;
735
736 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
737 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
738 ndm->ndm_state);
739 return -EINVAL;
740 }
741
742 if (tb[NDA_DST] == NULL)
743 return -EINVAL;
744
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300745 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
746 if (err)
747 return err;
David Stevens66817122013-03-15 04:35:51 +0000748
stephen hemmingerd3428942012-10-01 12:32:35 +0000749 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800750 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000751 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000752 spin_unlock_bh(&vxlan->hash_lock);
753
754 return err;
755}
756
757/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000758static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
759 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000760 const unsigned char *addr)
761{
762 struct vxlan_dev *vxlan = netdev_priv(dev);
763 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300764 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800765 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300766 __be16 port;
767 u32 vni, ifindex;
768 int err;
769
770 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
771 if (err)
772 return err;
773
774 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000775
776 spin_lock_bh(&vxlan->hash_lock);
777 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300778 if (!f)
779 goto out;
780
Cong Wange4c7ed42013-08-31 13:44:33 +0800781 if (!vxlan_addr_any(&ip)) {
782 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300783 if (!rd)
784 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000785 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300786
787 err = 0;
788
789 /* remove a destination if it's not the only one on the list,
790 * otherwise destroy the fdb entry
791 */
792 if (rd && !list_is_singular(&f->remotes)) {
793 list_del_rcu(&rd->list);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800794 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300795 goto out;
796 }
797
798 vxlan_fdb_destroy(vxlan, f);
799
800out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000801 spin_unlock_bh(&vxlan->hash_lock);
802
803 return err;
804}
805
806/* Dump forwarding table */
807static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
808 struct net_device *dev, int idx)
809{
810 struct vxlan_dev *vxlan = netdev_priv(dev);
811 unsigned int h;
812
813 for (h = 0; h < FDB_HASH_SIZE; ++h) {
814 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000815 int err;
816
Sasha Levinb67bfe02013-02-27 17:06:00 -0800817 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000818 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000819
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700820 if (idx < cb->args[0])
821 goto skip;
822
823 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000824 err = vxlan_fdb_info(skb, vxlan, f,
825 NETLINK_CB(cb->skb).portid,
826 cb->nlh->nlmsg_seq,
827 RTM_NEWNEIGH,
828 NLM_F_MULTI, rd);
829 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700830 goto out;
David Stevens66817122013-03-15 04:35:51 +0000831 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700832skip:
833 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000834 }
835 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700836out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000837 return idx;
838}
839
840/* Watch incoming packets to learn mapping between Ethernet address
841 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700842 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000843 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700844static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800845 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000846{
847 struct vxlan_dev *vxlan = netdev_priv(dev);
848 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000849
850 f = vxlan_find_mac(vxlan, src_mac);
851 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700852 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700853
Cong Wange4c7ed42013-08-31 13:44:33 +0800854 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700855 return false;
856
857 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700858 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700859 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000860
861 if (net_ratelimit())
862 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800863 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700864 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000865
Cong Wange4c7ed42013-08-31 13:44:33 +0800866 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000867 f->updated = jiffies;
Stephen Hemminger8385f502013-06-17 14:16:10 -0700868 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000869 } else {
870 /* learned new entry */
871 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700872
873 /* close off race between vxlan_flush and incoming packets */
874 if (netif_running(dev))
875 vxlan_fdb_create(vxlan, src_mac, src_ip,
876 NUD_REACHABLE,
877 NLM_F_EXCL|NLM_F_CREATE,
878 vxlan->dst_port,
879 vxlan->default_dst.remote_vni,
880 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000881 spin_unlock(&vxlan->hash_lock);
882 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700883
884 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000885}
886
stephen hemmingerd3428942012-10-01 12:32:35 +0000887/* See if multicast group is already in use by other ID */
Cong Wange4c7ed42013-08-31 13:44:33 +0800888static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000889{
stephen hemminger553675f2013-05-16 11:35:20 +0000890 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000891
stephen hemminger553675f2013-05-16 11:35:20 +0000892 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000893 if (!netif_running(vxlan->dev))
894 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000895
Cong Wange4c7ed42013-08-31 13:44:33 +0800896 if (vxlan_addr_equal(&vxlan->default_dst.remote_ip,
897 remote_ip))
stephen hemminger553675f2013-05-16 11:35:20 +0000898 return true;
899 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000900
901 return false;
902}
903
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700904static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000905{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700906 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000907}
908
Pravin B Shelar012a5722013-08-19 11:23:07 -0700909void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000910{
Pravin B Shelar012a5722013-08-19 11:23:07 -0700911 struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
912
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700913 if (!atomic_dec_and_test(&vs->refcnt))
914 return;
915
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700916 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700917 hlist_del_rcu(&vs->hlist);
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700918 spin_unlock(&vn->sock_lock);
919
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700920 queue_work(vxlan_wq, &vs->del_work);
921}
Pravin B Shelar012a5722013-08-19 11:23:07 -0700922EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700923
stephen hemminger3fc2de22013-07-18 08:40:15 -0700924/* Callback to update multicast group membership when first VNI on
925 * multicast asddress is brought up
926 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700927 */
stephen hemminger3fc2de22013-07-18 08:40:15 -0700928static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700929{
stephen hemminger3fc2de22013-07-18 08:40:15 -0700930 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700931 struct vxlan_sock *vs = vxlan->vn_sock;
932 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +0800933 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
934 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000935
stephen hemmingerd3428942012-10-01 12:32:35 +0000936 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +0800937 if (ip->sa.sa_family == AF_INET) {
938 struct ip_mreqn mreq = {
939 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
940 .imr_ifindex = ifindex,
941 };
942
943 ip_mc_join_group(sk, &mreq);
944#if IS_ENABLED(CONFIG_IPV6)
945 } else {
946 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
947 &ip->sin6.sin6_addr);
948#endif
949 }
stephen hemminger3fc2de22013-07-18 08:40:15 -0700950 release_sock(sk);
951
Pravin B Shelar012a5722013-08-19 11:23:07 -0700952 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700953 dev_put(vxlan->dev);
954}
955
956/* Inverse of vxlan_igmp_join when last VNI is brought down */
957static void vxlan_igmp_leave(struct work_struct *work)
958{
959 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -0700960 struct vxlan_sock *vs = vxlan->vn_sock;
961 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +0800962 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
963 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700964
965 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +0800966 if (ip->sa.sa_family == AF_INET) {
967 struct ip_mreqn mreq = {
968 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
969 .imr_ifindex = ifindex,
970 };
971
972 ip_mc_leave_group(sk, &mreq);
973#if IS_ENABLED(CONFIG_IPV6)
974 } else {
975 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
976 &ip->sin6.sin6_addr);
977#endif
978 }
979
stephen hemmingerd3428942012-10-01 12:32:35 +0000980 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000981
Pravin B Shelar012a5722013-08-19 11:23:07 -0700982 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700983 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000984}
985
986/* Callback from net/ipv4/udp.c to receive packets */
987static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
988{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700989 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000990 struct vxlanhdr *vxh;
stephen hemminger553675f2013-05-16 11:35:20 +0000991 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000992
stephen hemmingerd3428942012-10-01 12:32:35 +0000993 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700994 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +0000995 goto error;
996
Pravin B Shelar7ce04752013-08-19 11:22:54 -0700997 /* Return packets with reserved bits set */
998 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +0000999 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1000 (vxh->vx_vni & htonl(0xff))) {
1001 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1002 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1003 goto error;
1004 }
1005
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001006 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001007 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001008
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001009 port = inet_sk(sk)->inet_sport;
1010
1011 vs = vxlan_find_sock(sock_net(sk), port);
1012 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001013 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001014
1015 vs->rcv(vs, skb, vxh->vx_vni);
1016 return 0;
1017
1018drop:
1019 /* Consume bad packet */
1020 kfree_skb(skb);
1021 return 0;
1022
1023error:
1024 /* Return non vxlan pkt */
1025 return 1;
1026}
1027
1028static void vxlan_rcv(struct vxlan_sock *vs,
1029 struct sk_buff *skb, __be32 vx_vni)
1030{
Cong Wange4c7ed42013-08-31 13:44:33 +08001031 struct iphdr *oip = NULL;
1032 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001033 struct vxlan_dev *vxlan;
1034 struct pcpu_tstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001035 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001036 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001037 int err = 0;
1038 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001039
1040 vni = ntohl(vx_vni) >> 8;
1041 /* Is this VNI defined? */
1042 vxlan = vxlan_vs_find_vni(vs, vni);
1043 if (!vxlan)
1044 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001045
Cong Wange4c7ed42013-08-31 13:44:33 +08001046 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001047 skb_reset_mac_header(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001048 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001049
1050 /* Ignore packet loops (and multicast echo) */
1051 if (compare_ether_addr(eth_hdr(skb)->h_source,
1052 vxlan->dev->dev_addr) == 0)
1053 goto drop;
1054
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001055 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001056 if (remote_ip->sa.sa_family == AF_INET) {
1057 oip = ip_hdr(skb);
1058 saddr.sin.sin_addr.s_addr = oip->saddr;
1059 saddr.sa.sa_family = AF_INET;
1060#if IS_ENABLED(CONFIG_IPV6)
1061 } else {
1062 oip6 = ipv6_hdr(skb);
1063 saddr.sin6.sin6_addr = oip6->saddr;
1064 saddr.sa.sa_family = AF_INET6;
1065#endif
1066 }
1067
stephen hemminger26a41ae62013-06-17 12:09:58 -07001068 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001069 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001070 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001071
stephen hemmingerd3428942012-10-01 12:32:35 +00001072 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001073
1074 /* If the NIC driver gave us an encapsulated packet with
1075 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
1076 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
1077 * for us. Otherwise force the upper layers to verify it.
1078 */
1079 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
1080 !(vxlan->dev->features & NETIF_F_RXCSUM))
1081 skb->ip_summed = CHECKSUM_NONE;
1082
1083 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001084
Cong Wange4c7ed42013-08-31 13:44:33 +08001085 if (oip6)
1086 err = IP6_ECN_decapsulate(oip6, skb);
1087 if (oip)
1088 err = IP_ECN_decapsulate(oip, skb);
1089
stephen hemmingerd3428942012-10-01 12:32:35 +00001090 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001091 if (log_ecn_error) {
1092 if (oip6)
1093 net_info_ratelimited("non-ECT from %pI6\n",
1094 &oip6->saddr);
1095 if (oip)
1096 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1097 &oip->saddr, oip->tos);
1098 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001099 if (err > 1) {
1100 ++vxlan->dev->stats.rx_frame_errors;
1101 ++vxlan->dev->stats.rx_errors;
1102 goto drop;
1103 }
1104 }
1105
Pravin B Shelare8171042013-03-25 14:49:46 +00001106 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001107 u64_stats_update_begin(&stats->syncp);
1108 stats->rx_packets++;
1109 stats->rx_bytes += skb->len;
1110 u64_stats_update_end(&stats->syncp);
1111
1112 netif_rx(skb);
1113
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001114 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001115drop:
1116 /* Consume bad packet */
1117 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001118}
1119
David Stevense4f67ad2012-11-20 02:50:14 +00001120static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1121{
1122 struct vxlan_dev *vxlan = netdev_priv(dev);
1123 struct arphdr *parp;
1124 u8 *arpptr, *sha;
1125 __be32 sip, tip;
1126 struct neighbour *n;
1127
1128 if (dev->flags & IFF_NOARP)
1129 goto out;
1130
1131 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1132 dev->stats.tx_dropped++;
1133 goto out;
1134 }
1135 parp = arp_hdr(skb);
1136
1137 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1138 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1139 parp->ar_pro != htons(ETH_P_IP) ||
1140 parp->ar_op != htons(ARPOP_REQUEST) ||
1141 parp->ar_hln != dev->addr_len ||
1142 parp->ar_pln != 4)
1143 goto out;
1144 arpptr = (u8 *)parp + sizeof(struct arphdr);
1145 sha = arpptr;
1146 arpptr += dev->addr_len; /* sha */
1147 memcpy(&sip, arpptr, sizeof(sip));
1148 arpptr += sizeof(sip);
1149 arpptr += dev->addr_len; /* tha */
1150 memcpy(&tip, arpptr, sizeof(tip));
1151
1152 if (ipv4_is_loopback(tip) ||
1153 ipv4_is_multicast(tip))
1154 goto out;
1155
1156 n = neigh_lookup(&arp_tbl, &tip, dev);
1157
1158 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001159 struct vxlan_fdb *f;
1160 struct sk_buff *reply;
1161
1162 if (!(n->nud_state & NUD_CONNECTED)) {
1163 neigh_release(n);
1164 goto out;
1165 }
1166
1167 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001168 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001169 /* bridge-local neighbor */
1170 neigh_release(n);
1171 goto out;
1172 }
1173
1174 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1175 n->ha, sha);
1176
1177 neigh_release(n);
1178
1179 skb_reset_mac_header(reply);
1180 __skb_pull(reply, skb_network_offset(reply));
1181 reply->ip_summed = CHECKSUM_UNNECESSARY;
1182 reply->pkt_type = PACKET_HOST;
1183
1184 if (netif_rx_ni(reply) == NET_RX_DROP)
1185 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001186 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1187 union vxlan_addr ipa = {
1188 .sin.sin_addr.s_addr = tip,
1189 .sa.sa_family = AF_INET,
1190 };
1191
1192 vxlan_ip_miss(dev, &ipa);
1193 }
David Stevense4f67ad2012-11-20 02:50:14 +00001194out:
1195 consume_skb(skb);
1196 return NETDEV_TX_OK;
1197}
1198
1199static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1200{
1201 struct vxlan_dev *vxlan = netdev_priv(dev);
1202 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001203
1204 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1205 return false;
1206
1207 n = NULL;
1208 switch (ntohs(eth_hdr(skb)->h_proto)) {
1209 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001210 {
1211 struct iphdr *pip;
1212
David Stevense4f67ad2012-11-20 02:50:14 +00001213 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1214 return false;
1215 pip = ip_hdr(skb);
1216 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001217 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1218 union vxlan_addr ipa = {
1219 .sin.sin_addr.s_addr = pip->daddr,
1220 .sa.sa_family = AF_INET,
1221 };
1222
1223 vxlan_ip_miss(dev, &ipa);
1224 return false;
1225 }
1226
David Stevense4f67ad2012-11-20 02:50:14 +00001227 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001228 }
1229#if IS_ENABLED(CONFIG_IPV6)
1230 case ETH_P_IPV6:
1231 {
1232 struct ipv6hdr *pip6;
1233
1234 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1235 return false;
1236 pip6 = ipv6_hdr(skb);
1237 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1238 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1239 union vxlan_addr ipa = {
1240 .sin6.sin6_addr = pip6->daddr,
1241 .sa.sa_family = AF_INET6,
1242 };
1243
1244 vxlan_ip_miss(dev, &ipa);
1245 return false;
1246 }
1247
1248 break;
1249 }
1250#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001251 default:
1252 return false;
1253 }
1254
1255 if (n) {
1256 bool diff;
1257
1258 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1259 if (diff) {
1260 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1261 dev->addr_len);
1262 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1263 }
1264 neigh_release(n);
1265 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001266 }
1267
David Stevense4f67ad2012-11-20 02:50:14 +00001268 return false;
1269}
1270
stephen hemminger553675f2013-05-16 11:35:20 +00001271static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001272{
1273 sock_put(skb->sk);
1274}
1275
1276/* On transmit, associate with the tunnel socket */
Pravin B Shelar49560532013-08-19 11:23:17 -07001277static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +00001278{
stephen hemminger1cad8712012-10-09 20:35:49 +00001279 skb_orphan(skb);
1280 sock_hold(sk);
1281 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +00001282 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +00001283}
1284
stephen hemminger05f47d62012-10-09 20:35:50 +00001285/* Compute source port for outgoing packet
1286 * first choice to use L4 flow hash since it will spread
1287 * better and maybe available from hardware
1288 * secondary choice is to use jhash on the Ethernet header
1289 */
Pravin B Shelar49560532013-08-19 11:23:17 -07001290__be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +00001291{
Pravin B Shelar49560532013-08-19 11:23:17 -07001292 unsigned int range = (port_max - port_min) + 1;
stephen hemminger05f47d62012-10-09 20:35:50 +00001293 u32 hash;
1294
1295 hash = skb_get_rxhash(skb);
1296 if (!hash)
1297 hash = jhash(skb->data, 2 * ETH_ALEN,
1298 (__force u32) skb->protocol);
1299
Pravin B Shelar49560532013-08-19 11:23:17 -07001300 return htons((((u64) hash * range) >> 32) + port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +00001301}
Pravin B Shelar49560532013-08-19 11:23:17 -07001302EXPORT_SYMBOL_GPL(vxlan_src_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001303
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001304static int handle_offloads(struct sk_buff *skb)
1305{
1306 if (skb_is_gso(skb)) {
1307 int err = skb_unclone(skb, GFP_ATOMIC);
1308 if (unlikely(err))
1309 return err;
1310
Dmitry Kravkovf6ace502013-04-28 08:16:01 +00001311 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001312 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1313 skb->ip_summed = CHECKSUM_NONE;
1314
1315 return 0;
1316}
1317
Cong Wange4c7ed42013-08-31 13:44:33 +08001318#if IS_ENABLED(CONFIG_IPV6)
1319static int vxlan6_xmit_skb(struct net *net, struct vxlan_sock *vs,
1320 struct dst_entry *dst, struct sk_buff *skb,
1321 struct net_device *dev, struct in6_addr *saddr,
1322 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1323 __be16 src_port, __be16 dst_port, __be32 vni)
1324{
1325 struct ipv6hdr *ip6h;
1326 struct vxlanhdr *vxh;
1327 struct udphdr *uh;
1328 int min_headroom;
1329 int err;
1330
1331 if (!skb->encapsulation) {
1332 skb_reset_inner_headers(skb);
1333 skb->encapsulation = 1;
1334 }
1335
1336 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1337 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1338 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1339
1340 /* Need space for new headers (invalidates iph ptr) */
1341 err = skb_cow_head(skb, min_headroom);
1342 if (unlikely(err))
1343 return err;
1344
1345 if (vlan_tx_tag_present(skb)) {
1346 if (WARN_ON(!__vlan_put_tag(skb,
1347 skb->vlan_proto,
1348 vlan_tx_tag_get(skb))))
1349 return -ENOMEM;
1350
1351 skb->vlan_tci = 0;
1352 }
1353
1354 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1355 vxh->vx_flags = htonl(VXLAN_FLAGS);
1356 vxh->vx_vni = vni;
1357
1358 __skb_push(skb, sizeof(*uh));
1359 skb_reset_transport_header(skb);
1360 uh = udp_hdr(skb);
1361
1362 uh->dest = dst_port;
1363 uh->source = src_port;
1364
1365 uh->len = htons(skb->len);
1366 uh->check = 0;
1367
1368 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1369 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1370 IPSKB_REROUTED);
1371 skb_dst_drop(skb);
1372 skb_dst_set(skb, dst);
1373
1374 if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
1375 __wsum csum = skb_checksum(skb, 0, skb->len, 0);
1376 skb->ip_summed = CHECKSUM_UNNECESSARY;
1377 uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
1378 IPPROTO_UDP, csum);
1379 if (uh->check == 0)
1380 uh->check = CSUM_MANGLED_0;
1381 } else {
1382 skb->ip_summed = CHECKSUM_PARTIAL;
1383 skb->csum_start = skb_transport_header(skb) - skb->head;
1384 skb->csum_offset = offsetof(struct udphdr, check);
1385 uh->check = ~csum_ipv6_magic(saddr, daddr,
1386 skb->len, IPPROTO_UDP, 0);
1387 }
1388
1389 __skb_push(skb, sizeof(*ip6h));
1390 skb_reset_network_header(skb);
1391 ip6h = ipv6_hdr(skb);
1392 ip6h->version = 6;
1393 ip6h->priority = prio;
1394 ip6h->flow_lbl[0] = 0;
1395 ip6h->flow_lbl[1] = 0;
1396 ip6h->flow_lbl[2] = 0;
1397 ip6h->payload_len = htons(skb->len);
1398 ip6h->nexthdr = IPPROTO_UDP;
1399 ip6h->hop_limit = ttl;
1400 ip6h->daddr = *daddr;
1401 ip6h->saddr = *saddr;
1402
1403 vxlan_set_owner(vs->sock->sk, skb);
1404
1405 err = handle_offloads(skb);
1406 if (err)
1407 return err;
1408
1409 ip6tunnel_xmit(skb, dev);
1410 return 0;
1411}
1412#endif
1413
Pravin B Shelar49560532013-08-19 11:23:17 -07001414int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
1415 struct rtable *rt, struct sk_buff *skb,
1416 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1417 __be16 src_port, __be16 dst_port, __be32 vni)
1418{
1419 struct vxlanhdr *vxh;
1420 struct udphdr *uh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001421 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001422 int err;
1423
1424 if (!skb->encapsulation) {
1425 skb_reset_inner_headers(skb);
1426 skb->encapsulation = 1;
1427 }
1428
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001429 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001430 + VXLAN_HLEN + sizeof(struct iphdr)
1431 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001432
1433 /* Need space for new headers (invalidates iph ptr) */
1434 err = skb_cow_head(skb, min_headroom);
1435 if (unlikely(err))
1436 return err;
1437
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001438 if (vlan_tx_tag_present(skb)) {
1439 if (WARN_ON(!__vlan_put_tag(skb,
1440 skb->vlan_proto,
1441 vlan_tx_tag_get(skb))))
1442 return -ENOMEM;
1443
1444 skb->vlan_tci = 0;
1445 }
1446
Pravin B Shelar49560532013-08-19 11:23:17 -07001447 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1448 vxh->vx_flags = htonl(VXLAN_FLAGS);
1449 vxh->vx_vni = vni;
1450
1451 __skb_push(skb, sizeof(*uh));
1452 skb_reset_transport_header(skb);
1453 uh = udp_hdr(skb);
1454
1455 uh->dest = dst_port;
1456 uh->source = src_port;
1457
1458 uh->len = htons(skb->len);
1459 uh->check = 0;
1460
1461 vxlan_set_owner(vs->sock->sk, skb);
1462
1463 err = handle_offloads(skb);
1464 if (err)
1465 return err;
1466
1467 return iptunnel_xmit(net, rt, skb, src, dst,
1468 IPPROTO_UDP, tos, ttl, df);
1469}
1470EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1471
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001472/* Bypass encapsulation if the destination is local */
1473static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1474 struct vxlan_dev *dst_vxlan)
1475{
1476 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1477 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Cong Wange4c7ed42013-08-31 13:44:33 +08001478 union vxlan_addr loopback;
1479 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001480
1481 skb->pkt_type = PACKET_HOST;
1482 skb->encapsulation = 0;
1483 skb->dev = dst_vxlan->dev;
1484 __skb_pull(skb, skb_network_offset(skb));
1485
Cong Wange4c7ed42013-08-31 13:44:33 +08001486 if (remote_ip->sa.sa_family == AF_INET) {
1487 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1488 loopback.sa.sa_family = AF_INET;
1489#if IS_ENABLED(CONFIG_IPV6)
1490 } else {
1491 loopback.sin6.sin6_addr = in6addr_loopback;
1492 loopback.sa.sa_family = AF_INET6;
1493#endif
1494 }
1495
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001496 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001497 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001498
1499 u64_stats_update_begin(&tx_stats->syncp);
1500 tx_stats->tx_packets++;
1501 tx_stats->tx_bytes += skb->len;
1502 u64_stats_update_end(&tx_stats->syncp);
1503
1504 if (netif_rx(skb) == NET_RX_SUCCESS) {
1505 u64_stats_update_begin(&rx_stats->syncp);
1506 rx_stats->rx_packets++;
1507 rx_stats->rx_bytes += skb->len;
1508 u64_stats_update_end(&rx_stats->syncp);
1509 } else {
1510 skb->dev->stats.rx_dropped++;
1511 }
1512}
1513
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001514static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1515 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001516{
1517 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001518 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001519 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001520 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001521 union vxlan_addr *dst;
1522 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001523 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001524 __be16 df = 0;
1525 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001526 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001527
stephen hemminger823aa872013-04-27 11:31:57 +00001528 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001529 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001530 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001531
Cong Wange4c7ed42013-08-31 13:44:33 +08001532 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001533 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001534 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001535 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001536 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001537 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001538 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001539 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001540
stephen hemmingerd3428942012-10-01 12:32:35 +00001541 old_iph = ip_hdr(skb);
1542
stephen hemmingerd3428942012-10-01 12:32:35 +00001543 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001544 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001545 ttl = 1;
1546
1547 tos = vxlan->tos;
1548 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001549 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001550
Pravin B Shelar49560532013-08-19 11:23:17 -07001551 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001552
Cong Wange4c7ed42013-08-31 13:44:33 +08001553 if (dst->sa.sa_family == AF_INET) {
1554 memset(&fl4, 0, sizeof(fl4));
1555 fl4.flowi4_oif = rdst->remote_ifindex;
1556 fl4.flowi4_tos = RT_TOS(tos);
1557 fl4.daddr = dst->sin.sin_addr.s_addr;
1558 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001559
Cong Wange4c7ed42013-08-31 13:44:33 +08001560 rt = ip_route_output_key(dev_net(dev), &fl4);
1561 if (IS_ERR(rt)) {
1562 netdev_dbg(dev, "no route to %pI4\n",
1563 &dst->sin.sin_addr.s_addr);
1564 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001565 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001566 }
1567
1568 if (rt->dst.dev == dev) {
1569 netdev_dbg(dev, "circular route to %pI4\n",
1570 &dst->sin.sin_addr.s_addr);
1571 dev->stats.collisions++;
1572 goto tx_error;
1573 }
1574
1575 /* Bypass encapsulation if the destination is local */
1576 if (rt->rt_flags & RTCF_LOCAL &&
1577 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1578 struct vxlan_dev *dst_vxlan;
1579
1580 ip_rt_put(rt);
1581 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1582 if (!dst_vxlan)
1583 goto tx_error;
1584 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1585 return;
1586 }
1587
1588 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1589 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1590
1591 err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb,
1592 fl4.saddr, dst->sin.sin_addr.s_addr,
1593 tos, ttl, df, src_port, dst_port,
1594 htonl(vni << 8));
1595
1596 if (err < 0)
1597 goto rt_tx_error;
1598 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1599#if IS_ENABLED(CONFIG_IPV6)
1600 } else {
1601 struct sock *sk = vxlan->vn_sock->sock->sk;
1602 struct dst_entry *ndst;
1603 struct flowi6 fl6;
1604 u32 flags;
1605
1606 memset(&fl6, 0, sizeof(fl6));
1607 fl6.flowi6_oif = rdst->remote_ifindex;
1608 fl6.daddr = dst->sin6.sin6_addr;
1609 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1610 fl6.flowi6_proto = skb->protocol;
1611
1612 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1613 netdev_dbg(dev, "no route to %pI6\n",
1614 &dst->sin6.sin6_addr);
1615 dev->stats.tx_carrier_errors++;
1616 goto tx_error;
1617 }
1618
1619 if (ndst->dev == dev) {
1620 netdev_dbg(dev, "circular route to %pI6\n",
1621 &dst->sin6.sin6_addr);
1622 dst_release(ndst);
1623 dev->stats.collisions++;
1624 goto tx_error;
1625 }
1626
1627 /* Bypass encapsulation if the destination is local */
1628 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1629 if (flags & RTF_LOCAL &&
1630 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1631 struct vxlan_dev *dst_vxlan;
1632
1633 dst_release(ndst);
1634 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1635 if (!dst_vxlan)
1636 goto tx_error;
1637 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1638 return;
1639 }
1640
1641 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1642
1643 err = vxlan6_xmit_skb(dev_net(dev), vxlan->vn_sock, ndst, skb,
1644 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1645 src_port, dst_port, htonl(vni << 8));
1646#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001647 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001648
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001649 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001650
1651drop:
1652 dev->stats.tx_dropped++;
1653 goto tx_free;
1654
Pravin B Shelar49560532013-08-19 11:23:17 -07001655rt_tx_error:
1656 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001657tx_error:
1658 dev->stats.tx_errors++;
1659tx_free:
1660 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001661}
1662
David Stevens66817122013-03-15 04:35:51 +00001663/* Transmit local packets over Vxlan
1664 *
1665 * Outer IP header inherits ECN and DF from inner header.
1666 * Outer UDP destination is the VXLAN assigned port.
1667 * source port is based on hash of flow
1668 */
1669static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1670{
1671 struct vxlan_dev *vxlan = netdev_priv(dev);
1672 struct ethhdr *eth;
1673 bool did_rsc = false;
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001674 struct vxlan_rdst *rdst;
David Stevens66817122013-03-15 04:35:51 +00001675 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00001676
1677 skb_reset_mac_header(skb);
1678 eth = eth_hdr(skb);
1679
1680 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1681 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001682
1683 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001684 did_rsc = false;
1685
1686 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08001687 (ntohs(eth->h_proto) == ETH_P_IP ||
1688 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00001689 did_rsc = route_shortcircuit(dev, skb);
1690 if (did_rsc)
1691 f = vxlan_find_mac(vxlan, eth->h_dest);
1692 }
1693
David Stevens66817122013-03-15 04:35:51 +00001694 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001695 f = vxlan_find_mac(vxlan, all_zeros_mac);
1696 if (f == NULL) {
1697 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1698 !is_multicast_ether_addr(eth->h_dest))
1699 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00001700
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001701 dev->stats.tx_dropped++;
1702 dev_kfree_skb(skb);
1703 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001704 }
David Stevens66817122013-03-15 04:35:51 +00001705 }
1706
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001707 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1708 struct sk_buff *skb1;
1709
1710 skb1 = skb_clone(skb, GFP_ATOMIC);
1711 if (skb1)
1712 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1713 }
1714
1715 dev_kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001716 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00001717}
1718
stephen hemmingerd3428942012-10-01 12:32:35 +00001719/* Walk the forwarding table and purge stale entries */
1720static void vxlan_cleanup(unsigned long arg)
1721{
1722 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1723 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1724 unsigned int h;
1725
1726 if (!netif_running(vxlan->dev))
1727 return;
1728
1729 spin_lock_bh(&vxlan->hash_lock);
1730 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1731 struct hlist_node *p, *n;
1732 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1733 struct vxlan_fdb *f
1734 = container_of(p, struct vxlan_fdb, hlist);
1735 unsigned long timeout;
1736
stephen hemminger3c172862012-10-26 06:24:34 +00001737 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001738 continue;
1739
1740 timeout = f->used + vxlan->age_interval * HZ;
1741 if (time_before_eq(timeout, jiffies)) {
1742 netdev_dbg(vxlan->dev,
1743 "garbage collect %pM\n",
1744 f->eth_addr);
1745 f->state = NUD_STALE;
1746 vxlan_fdb_destroy(vxlan, f);
1747 } else if (time_before(timeout, next_timer))
1748 next_timer = timeout;
1749 }
1750 }
1751 spin_unlock_bh(&vxlan->hash_lock);
1752
1753 mod_timer(&vxlan->age_timer, next_timer);
1754}
1755
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001756static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1757{
1758 __u32 vni = vxlan->default_dst.remote_vni;
1759
1760 vxlan->vn_sock = vs;
1761 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1762}
1763
stephen hemmingerd3428942012-10-01 12:32:35 +00001764/* Setup stats when device is created */
1765static int vxlan_init(struct net_device *dev)
1766{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001767 struct vxlan_dev *vxlan = netdev_priv(dev);
1768 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1769 struct vxlan_sock *vs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001770
Pravin B Shelare8171042013-03-25 14:49:46 +00001771 dev->tstats = alloc_percpu(struct pcpu_tstats);
1772 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001773 return -ENOMEM;
1774
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001775 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001776 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001777 if (vs) {
1778 /* If we have a socket with same port already, reuse it */
1779 atomic_inc(&vs->refcnt);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07001780 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001781 } else {
1782 /* otherwise make new socket outside of RTNL */
1783 dev_hold(dev);
1784 queue_work(vxlan_wq, &vxlan->sock_work);
1785 }
1786 spin_unlock(&vn->sock_lock);
1787
stephen hemmingerd3428942012-10-01 12:32:35 +00001788 return 0;
1789}
1790
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001791static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001792{
1793 struct vxlan_fdb *f;
1794
1795 spin_lock_bh(&vxlan->hash_lock);
1796 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1797 if (f)
1798 vxlan_fdb_destroy(vxlan, f);
1799 spin_unlock_bh(&vxlan->hash_lock);
1800}
1801
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001802static void vxlan_uninit(struct net_device *dev)
1803{
1804 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001805 struct vxlan_sock *vs = vxlan->vn_sock;
1806
Stephen Hemmingerba609e92013-06-25 17:06:01 -07001807 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001808
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001809 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07001810 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001811 free_percpu(dev->tstats);
1812}
1813
stephen hemmingerd3428942012-10-01 12:32:35 +00001814/* Start ageing timer and join group when device is brought up */
1815static int vxlan_open(struct net_device *dev)
1816{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001817 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001818 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001819 struct vxlan_sock *vs = vxlan->vn_sock;
1820
1821 /* socket hasn't been created */
1822 if (!vs)
1823 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001824
Cong Wange4c7ed42013-08-31 13:44:33 +08001825 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1826 vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001827 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001828 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001829 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00001830 }
1831
1832 if (vxlan->age_interval)
1833 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1834
1835 return 0;
1836}
1837
1838/* Purge the forwarding table */
1839static void vxlan_flush(struct vxlan_dev *vxlan)
1840{
Cong Wang31fec5a2013-05-27 22:35:52 +00001841 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001842
1843 spin_lock_bh(&vxlan->hash_lock);
1844 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1845 struct hlist_node *p, *n;
1846 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1847 struct vxlan_fdb *f
1848 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03001849 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1850 if (!is_zero_ether_addr(f->eth_addr))
1851 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00001852 }
1853 }
1854 spin_unlock_bh(&vxlan->hash_lock);
1855}
1856
1857/* Cleanup timer and forwarding table on shutdown */
1858static int vxlan_stop(struct net_device *dev)
1859{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001860 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001861 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001862 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001863
Cong Wange4c7ed42013-08-31 13:44:33 +08001864 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1865 ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001866 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001867 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001868 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001869 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001870
1871 del_timer_sync(&vxlan->age_timer);
1872
1873 vxlan_flush(vxlan);
1874
1875 return 0;
1876}
1877
stephen hemmingerd3428942012-10-01 12:32:35 +00001878/* Stub, nothing needs to be done. */
1879static void vxlan_set_multicast_list(struct net_device *dev)
1880{
1881}
1882
1883static const struct net_device_ops vxlan_netdev_ops = {
1884 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001885 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00001886 .ndo_open = vxlan_open,
1887 .ndo_stop = vxlan_stop,
1888 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001889 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001890 .ndo_set_rx_mode = vxlan_set_multicast_list,
1891 .ndo_change_mtu = eth_change_mtu,
1892 .ndo_validate_addr = eth_validate_addr,
1893 .ndo_set_mac_address = eth_mac_addr,
1894 .ndo_fdb_add = vxlan_fdb_add,
1895 .ndo_fdb_del = vxlan_fdb_delete,
1896 .ndo_fdb_dump = vxlan_fdb_dump,
1897};
1898
1899/* Info for udev, that this is a virtual tunnel endpoint */
1900static struct device_type vxlan_type = {
1901 .name = "vxlan",
1902};
1903
stephen hemmingerd3428942012-10-01 12:32:35 +00001904/* Initialize the device structure. */
1905static void vxlan_setup(struct net_device *dev)
1906{
1907 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001908 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001909 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001910
1911 eth_hw_addr_random(dev);
1912 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001913 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
1914 dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM;
1915 else
1916 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001917
1918 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07001919 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00001920 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1921
1922 dev->tx_queue_len = 0;
1923 dev->features |= NETIF_F_LLTX;
1924 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001925 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001926 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001927 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001928
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001929 dev->vlan_features = dev->features;
1930 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001931 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001932 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001933 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
stephen hemmingerd3428942012-10-01 12:32:35 +00001934 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001935 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001936
stephen hemminger553675f2013-05-16 11:35:20 +00001937 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001938 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001939 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1940 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001941 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001942
1943 init_timer_deferrable(&vxlan->age_timer);
1944 vxlan->age_timer.function = vxlan_cleanup;
1945 vxlan->age_timer.data = (unsigned long) vxlan;
1946
stephen hemminger05f47d62012-10-09 20:35:50 +00001947 inet_get_local_port_range(&low, &high);
1948 vxlan->port_min = low;
1949 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001950 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001951
stephen hemmingerd3428942012-10-01 12:32:35 +00001952 vxlan->dev = dev;
1953
1954 for (h = 0; h < FDB_HASH_SIZE; ++h)
1955 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1956}
1957
1958static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1959 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001960 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08001961 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001962 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1963 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08001964 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001965 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1966 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1967 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1968 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1969 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001970 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001971 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1972 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1973 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1974 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001975 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001976};
1977
1978static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1979{
1980 if (tb[IFLA_ADDRESS]) {
1981 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1982 pr_debug("invalid link address (not ethernet)\n");
1983 return -EINVAL;
1984 }
1985
1986 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1987 pr_debug("invalid all zero ethernet address\n");
1988 return -EADDRNOTAVAIL;
1989 }
1990 }
1991
1992 if (!data)
1993 return -EINVAL;
1994
1995 if (data[IFLA_VXLAN_ID]) {
1996 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1997 if (id >= VXLAN_VID_MASK)
1998 return -ERANGE;
1999 }
2000
stephen hemminger05f47d62012-10-09 20:35:50 +00002001 if (data[IFLA_VXLAN_PORT_RANGE]) {
2002 const struct ifla_vxlan_port_range *p
2003 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2004
2005 if (ntohs(p->high) < ntohs(p->low)) {
2006 pr_debug("port range %u .. %u not valid\n",
2007 ntohs(p->low), ntohs(p->high));
2008 return -EINVAL;
2009 }
2010 }
2011
stephen hemmingerd3428942012-10-01 12:32:35 +00002012 return 0;
2013}
2014
Yan Burman1b13c972013-01-29 23:43:07 +00002015static void vxlan_get_drvinfo(struct net_device *netdev,
2016 struct ethtool_drvinfo *drvinfo)
2017{
2018 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2019 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2020}
2021
2022static const struct ethtool_ops vxlan_ethtool_ops = {
2023 .get_drvinfo = vxlan_get_drvinfo,
2024 .get_link = ethtool_op_get_link,
2025};
2026
stephen hemminger553675f2013-05-16 11:35:20 +00002027static void vxlan_del_work(struct work_struct *work)
2028{
2029 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2030
2031 sk_release_kernel(vs->sock->sk);
2032 kfree_rcu(vs, rcu);
2033}
2034
Cong Wange4c7ed42013-08-31 13:44:33 +08002035#if IS_ENABLED(CONFIG_IPV6)
2036/* Create UDP socket for encapsulation receive. AF_INET6 socket
2037 * could be used for both IPv4 and IPv6 communications, but
2038 * users may set bindv6only=1.
2039 */
2040static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
stephen hemminger553675f2013-05-16 11:35:20 +00002041{
stephen hemminger553675f2013-05-16 11:35:20 +00002042 struct sock *sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08002043 struct socket *sock;
2044 struct sockaddr_in6 vxlan_addr = {
2045 .sin6_family = AF_INET6,
2046 .sin6_port = port,
2047 };
2048 int rc, val = 1;
2049
2050 rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
2051 if (rc < 0) {
2052 pr_debug("UDPv6 socket create failed\n");
2053 return rc;
2054 }
2055
2056 /* Put in proper namespace */
2057 sk = sock->sk;
2058 sk_change_net(sk, net);
2059
2060 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
2061 (char *)&val, sizeof(val));
2062 rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
2063 sizeof(struct sockaddr_in6));
2064 if (rc < 0) {
2065 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2066 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
2067 sk_release_kernel(sk);
2068 return rc;
2069 }
2070 /* At this point, IPv6 module should have been loaded in
2071 * sock_create_kern().
2072 */
2073 BUG_ON(!ipv6_stub);
2074
2075 *psock = sock;
2076 /* Disable multicast loopback */
2077 inet_sk(sk)->mc_loop = 0;
2078 return 0;
2079}
2080
2081#else
2082
2083static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
2084{
2085 return -EPFNOSUPPORT;
2086}
2087#endif
2088
2089static int create_v4_sock(struct net *net, __be16 port, struct socket **psock)
2090{
2091 struct sock *sk;
2092 struct socket *sock;
stephen hemminger553675f2013-05-16 11:35:20 +00002093 struct sockaddr_in vxlan_addr = {
2094 .sin_family = AF_INET,
2095 .sin_addr.s_addr = htonl(INADDR_ANY),
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -07002096 .sin_port = port,
stephen hemminger553675f2013-05-16 11:35:20 +00002097 };
2098 int rc;
Cong Wange4c7ed42013-08-31 13:44:33 +08002099
2100 /* Create UDP socket for encapsulation receive. */
2101 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
2102 if (rc < 0) {
2103 pr_debug("UDP socket create failed\n");
2104 return rc;
2105 }
2106
2107 /* Put in proper namespace */
2108 sk = sock->sk;
2109 sk_change_net(sk, net);
2110
2111 rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
2112 sizeof(vxlan_addr));
2113 if (rc < 0) {
2114 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2115 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
2116 sk_release_kernel(sk);
2117 return rc;
2118 }
2119
2120 *psock = sock;
2121 /* Disable multicast loopback */
2122 inet_sk(sk)->mc_loop = 0;
2123 return 0;
2124}
2125
2126/* Create new listen socket if needed */
2127static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2128 vxlan_rcv_t *rcv, void *data, bool ipv6)
2129{
2130 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2131 struct vxlan_sock *vs;
2132 struct socket *sock;
2133 struct sock *sk;
2134 int rc = 0;
Cong Wang31fec5a2013-05-27 22:35:52 +00002135 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00002136
2137 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002138 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002139 return ERR_PTR(-ENOMEM);
2140
2141 for (h = 0; h < VNI_HASH_SIZE; ++h)
2142 INIT_HLIST_HEAD(&vs->vni_list[h]);
2143
2144 INIT_WORK(&vs->del_work, vxlan_del_work);
2145
Cong Wange4c7ed42013-08-31 13:44:33 +08002146 if (ipv6)
2147 rc = create_v6_sock(net, port, &sock);
2148 else
2149 rc = create_v4_sock(net, port, &sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002150 if (rc < 0) {
stephen hemminger553675f2013-05-16 11:35:20 +00002151 kfree(vs);
2152 return ERR_PTR(rc);
2153 }
2154
Cong Wange4c7ed42013-08-31 13:44:33 +08002155 vs->sock = sock;
2156 sk = sock->sk;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002157 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002158 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002159 vs->data = data;
stephen hemminger553675f2013-05-16 11:35:20 +00002160
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002161 spin_lock(&vn->sock_lock);
2162 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2163 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002164
2165 /* Mark socket as an encapsulation socket. */
2166 udp_sk(sk)->encap_type = 1;
2167 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
Cong Wange4c7ed42013-08-31 13:44:33 +08002168#if IS_ENABLED(CONFIG_IPV6)
2169 if (ipv6)
2170 ipv6_stub->udpv6_encap_enable();
2171 else
2172#endif
2173 udp_encap_enable();
2174
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002175 return vs;
2176}
stephen hemminger553675f2013-05-16 11:35:20 +00002177
Pravin B Shelar012a5722013-08-19 11:23:07 -07002178struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2179 vxlan_rcv_t *rcv, void *data,
Cong Wange4c7ed42013-08-31 13:44:33 +08002180 bool no_share, bool ipv6)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002181{
2182 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2183 struct vxlan_sock *vs;
2184
Cong Wange4c7ed42013-08-31 13:44:33 +08002185 vs = vxlan_socket_create(net, port, rcv, data, ipv6);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002186 if (!IS_ERR(vs))
2187 return vs;
2188
Pravin B Shelar012a5722013-08-19 11:23:07 -07002189 if (no_share) /* Return error if sharing is not allowed. */
2190 return vs;
2191
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002192 spin_lock(&vn->sock_lock);
2193 vs = vxlan_find_sock(net, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002194 if (vs) {
2195 if (vs->rcv == rcv)
2196 atomic_inc(&vs->refcnt);
2197 else
2198 vs = ERR_PTR(-EBUSY);
2199 }
2200 spin_unlock(&vn->sock_lock);
2201
2202 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002203 vs = ERR_PTR(-EINVAL);
2204
stephen hemminger553675f2013-05-16 11:35:20 +00002205 return vs;
2206}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002207EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002208
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002209/* Scheduled at device creation to bind to a socket */
2210static void vxlan_sock_work(struct work_struct *work)
2211{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002212 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2213 struct net *net = dev_net(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002214 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002215 __be16 port = vxlan->dst_port;
2216 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002217
Cong Wange4c7ed42013-08-31 13:44:33 +08002218 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002219 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002220 if (!IS_ERR(nvs))
2221 vxlan_vs_add_dev(nvs, vxlan);
2222 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002223
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002224 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002225}
2226
stephen hemmingerd3428942012-10-01 12:32:35 +00002227static int vxlan_newlink(struct net *net, struct net_device *dev,
2228 struct nlattr *tb[], struct nlattr *data[])
2229{
stephen hemminger553675f2013-05-16 11:35:20 +00002230 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002231 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002232 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002233 __u32 vni;
2234 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002235 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002236
2237 if (!data[IFLA_VXLAN_ID])
2238 return -EINVAL;
2239
2240 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002241 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002242
Cong Wange4c7ed42013-08-31 13:44:33 +08002243 if (data[IFLA_VXLAN_GROUP]) {
2244 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2245 dst->remote_ip.sa.sa_family = AF_INET;
2246 } else if (data[IFLA_VXLAN_GROUP6]) {
2247 if (!IS_ENABLED(CONFIG_IPV6))
2248 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002249
Cong Wange4c7ed42013-08-31 13:44:33 +08002250 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2251 sizeof(struct in6_addr));
2252 dst->remote_ip.sa.sa_family = AF_INET6;
2253 use_ipv6 = true;
2254 }
2255
2256 if (data[IFLA_VXLAN_LOCAL]) {
2257 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2258 vxlan->saddr.sa.sa_family = AF_INET;
2259 } else if (data[IFLA_VXLAN_LOCAL6]) {
2260 if (!IS_ENABLED(CONFIG_IPV6))
2261 return -EPFNOSUPPORT;
2262
2263 /* TODO: respect scope id */
2264 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2265 sizeof(struct in6_addr));
2266 vxlan->saddr.sa.sa_family = AF_INET6;
2267 use_ipv6 = true;
2268 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002269
stephen hemminger34e02aa2012-10-09 20:35:53 +00002270 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002271 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002272 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002273 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002274
stephen hemminger34e02aa2012-10-09 20:35:53 +00002275 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002276 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002277 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002278 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002279
Cong Wange4c7ed42013-08-31 13:44:33 +08002280#if IS_ENABLED(CONFIG_IPV6)
2281 if (use_ipv6) {
2282 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2283 if (idev && idev->cnf.disable_ipv6) {
2284 pr_info("IPv6 is disabled via sysctl\n");
2285 return -EPERM;
2286 }
2287 vxlan->flags |= VXLAN_F_IPV6;
2288 }
2289#endif
2290
stephen hemminger34e02aa2012-10-09 20:35:53 +00002291 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002292 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002293
2294 /* update header length based on lower device */
2295 dev->hard_header_len = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002296 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
stephen hemmingerd3428942012-10-01 12:32:35 +00002297 }
2298
2299 if (data[IFLA_VXLAN_TOS])
2300 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2301
Vincent Bernatafb97182012-10-30 10:27:16 +00002302 if (data[IFLA_VXLAN_TTL])
2303 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2304
stephen hemmingerd3428942012-10-01 12:32:35 +00002305 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002306 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002307
2308 if (data[IFLA_VXLAN_AGEING])
2309 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2310 else
2311 vxlan->age_interval = FDB_AGE_DEFAULT;
2312
David Stevense4f67ad2012-11-20 02:50:14 +00002313 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2314 vxlan->flags |= VXLAN_F_PROXY;
2315
2316 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2317 vxlan->flags |= VXLAN_F_RSC;
2318
2319 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2320 vxlan->flags |= VXLAN_F_L2MISS;
2321
2322 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2323 vxlan->flags |= VXLAN_F_L3MISS;
2324
stephen hemmingerd3428942012-10-01 12:32:35 +00002325 if (data[IFLA_VXLAN_LIMIT])
2326 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2327
stephen hemminger05f47d62012-10-09 20:35:50 +00002328 if (data[IFLA_VXLAN_PORT_RANGE]) {
2329 const struct ifla_vxlan_port_range *p
2330 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2331 vxlan->port_min = ntohs(p->low);
2332 vxlan->port_max = ntohs(p->high);
2333 }
2334
stephen hemminger823aa872013-04-27 11:31:57 +00002335 if (data[IFLA_VXLAN_PORT])
2336 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2337
stephen hemminger553675f2013-05-16 11:35:20 +00002338 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
2339 pr_info("duplicate VNI %u\n", vni);
2340 return -EEXIST;
2341 }
2342
Yan Burman1b13c972013-01-29 23:43:07 +00002343 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
2344
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002345 /* create an fdb entry for default destination */
2346 err = vxlan_fdb_create(vxlan, all_zeros_mac,
Cong Wange4c7ed42013-08-31 13:44:33 +08002347 &vxlan->default_dst.remote_ip,
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002348 NUD_REACHABLE|NUD_PERMANENT,
2349 NLM_F_EXCL|NLM_F_CREATE,
2350 vxlan->dst_port, vxlan->default_dst.remote_vni,
2351 vxlan->default_dst.remote_ifindex, NTF_SELF);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002352 if (err)
stephen hemminger553675f2013-05-16 11:35:20 +00002353 return err;
stephen hemmingerd3428942012-10-01 12:32:35 +00002354
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002355 err = register_netdevice(dev);
2356 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002357 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002358 return err;
2359 }
2360
stephen hemminger553675f2013-05-16 11:35:20 +00002361 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002362
2363 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002364}
2365
2366static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2367{
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002368 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002369 struct vxlan_dev *vxlan = netdev_priv(dev);
2370
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002371 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002372 if (!hlist_unhashed(&vxlan->hlist))
2373 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002374 spin_unlock(&vn->sock_lock);
2375
stephen hemminger553675f2013-05-16 11:35:20 +00002376 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002377 unregister_netdevice_queue(dev, head);
2378}
2379
2380static size_t vxlan_get_size(const struct net_device *dev)
2381{
2382
2383 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002384 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002385 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002386 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002387 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2388 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2389 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002390 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2391 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2392 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2393 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002394 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2395 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002396 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00002397 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00002398 0;
2399}
2400
2401static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2402{
2403 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002404 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002405 struct ifla_vxlan_port_range ports = {
2406 .low = htons(vxlan->port_min),
2407 .high = htons(vxlan->port_max),
2408 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002409
Atzm Watanabec7995c42013-04-16 02:50:52 +00002410 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002411 goto nla_put_failure;
2412
Cong Wange4c7ed42013-08-31 13:44:33 +08002413 if (!vxlan_addr_any(&dst->remote_ip)) {
2414 if (dst->remote_ip.sa.sa_family == AF_INET) {
2415 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2416 dst->remote_ip.sin.sin_addr.s_addr))
2417 goto nla_put_failure;
2418#if IS_ENABLED(CONFIG_IPV6)
2419 } else {
2420 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2421 &dst->remote_ip.sin6.sin6_addr))
2422 goto nla_put_failure;
2423#endif
2424 }
2425 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002426
Atzm Watanabec7995c42013-04-16 02:50:52 +00002427 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002428 goto nla_put_failure;
2429
Cong Wange4c7ed42013-08-31 13:44:33 +08002430 if (!vxlan_addr_any(&vxlan->saddr)) {
2431 if (vxlan->saddr.sa.sa_family == AF_INET) {
2432 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2433 vxlan->saddr.sin.sin_addr.s_addr))
2434 goto nla_put_failure;
2435#if IS_ENABLED(CONFIG_IPV6)
2436 } else {
2437 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2438 &vxlan->saddr.sin6.sin6_addr))
2439 goto nla_put_failure;
2440#endif
2441 }
2442 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002443
2444 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2445 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002446 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2447 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2448 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2449 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2450 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2451 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2452 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2453 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2454 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002455 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002456 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2457 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00002458 goto nla_put_failure;
2459
stephen hemminger05f47d62012-10-09 20:35:50 +00002460 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2461 goto nla_put_failure;
2462
stephen hemmingerd3428942012-10-01 12:32:35 +00002463 return 0;
2464
2465nla_put_failure:
2466 return -EMSGSIZE;
2467}
2468
2469static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2470 .kind = "vxlan",
2471 .maxtype = IFLA_VXLAN_MAX,
2472 .policy = vxlan_policy,
2473 .priv_size = sizeof(struct vxlan_dev),
2474 .setup = vxlan_setup,
2475 .validate = vxlan_validate,
2476 .newlink = vxlan_newlink,
2477 .dellink = vxlan_dellink,
2478 .get_size = vxlan_get_size,
2479 .fill_info = vxlan_fill_info,
2480};
2481
2482static __net_init int vxlan_init_net(struct net *net)
2483{
2484 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002485 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002486
stephen hemminger553675f2013-05-16 11:35:20 +00002487 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002488 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002489
stephen hemminger553675f2013-05-16 11:35:20 +00002490 for (h = 0; h < PORT_HASH_SIZE; ++h)
2491 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002492
2493 return 0;
2494}
2495
2496static __net_exit void vxlan_exit_net(struct net *net)
2497{
2498 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002499 struct vxlan_dev *vxlan;
stephen hemminger372675a2013-07-18 08:38:26 -07002500 LIST_HEAD(list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002501
2502 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00002503 list_for_each_entry(vxlan, &vn->vxlan_list, next)
stephen hemminger372675a2013-07-18 08:38:26 -07002504 unregister_netdevice_queue(vxlan->dev, &list);
2505 unregister_netdevice_many(&list);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00002506 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00002507}
2508
2509static struct pernet_operations vxlan_net_ops = {
2510 .init = vxlan_init_net,
2511 .exit = vxlan_exit_net,
2512 .id = &vxlan_net_id,
2513 .size = sizeof(struct vxlan_net),
2514};
2515
2516static int __init vxlan_init_module(void)
2517{
2518 int rc;
2519
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002520 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2521 if (!vxlan_wq)
2522 return -ENOMEM;
2523
stephen hemmingerd3428942012-10-01 12:32:35 +00002524 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2525
2526 rc = register_pernet_device(&vxlan_net_ops);
2527 if (rc)
2528 goto out1;
2529
2530 rc = rtnl_link_register(&vxlan_link_ops);
2531 if (rc)
2532 goto out2;
2533
2534 return 0;
2535
2536out2:
2537 unregister_pernet_device(&vxlan_net_ops);
2538out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002539 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002540 return rc;
2541}
Cong Wang7332a132013-05-27 22:35:53 +00002542late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002543
2544static void __exit vxlan_cleanup_module(void)
2545{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002546 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002547 destroy_workqueue(vxlan_wq);
Pravin B Shelarf89e57c2013-07-11 11:38:06 -07002548 unregister_pernet_device(&vxlan_net_ops);
David Stevens66817122013-03-15 04:35:51 +00002549 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00002550}
2551module_exit(vxlan_cleanup_module);
2552
2553MODULE_LICENSE("GPL");
2554MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00002555MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00002556MODULE_ALIAS_RTNL_LINK("vxlan");