blob: 0e57e862c399d8c3c436aca06ab724068537a27d [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
stephen hemmingerd3428942012-10-01 12:32:35 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/rculist.h>
20#include <linux/netdevice.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/udp.h>
24#include <linux/igmp.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
Pravin B Shelar1eaa8172013-08-19 11:23:29 -070027#include <linux/if_vlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000028#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000029#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000030#include <net/arp.h>
31#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000032#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000033#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/icmp.h>
35#include <net/udp.h>
Tom Herbert3ee64f32014-07-13 19:49:42 -070036#include <net/udp_tunnel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000037#include <net/rtnetlink.h>
38#include <net/route.h>
39#include <net/dsfield.h>
40#include <net/inet_ecn.h>
41#include <net/net_namespace.h>
42#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070043#include <net/vxlan.h>
Or Gerlitzdc01e7d2014-01-20 13:59:21 +020044#include <net/protocol.h>
Andy Zhouacbf74a2014-09-16 17:31:18 -070045#include <net/udp_tunnel.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080046#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ipv6.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080050#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080051#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000052
53#define VXLAN_VERSION "0.1"
54
stephen hemminger553675f2013-05-16 11:35:20 +000055#define PORT_HASH_BITS 8
56#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000057#define VNI_HASH_BITS 10
58#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
59#define FDB_HASH_BITS 8
60#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
61#define FDB_AGE_DEFAULT 300 /* 5 min */
62#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
63
stephen hemminger23c578b2013-04-27 11:31:53 +000064/* UDP port for VXLAN traffic.
65 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070066 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000067 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070068static unsigned short vxlan_port __read_mostly = 8472;
69module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000070MODULE_PARM_DESC(udp_port, "Destination UDP port");
71
72static bool log_ecn_error = true;
73module_param(log_ecn_error, bool, 0644);
74MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
75
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070076static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000077
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030078static const u8 all_zeros_mac[ETH_ALEN];
79
stephen hemminger553675f2013-05-16 11:35:20 +000080/* per-network namespace private data for this module */
81struct vxlan_net {
82 struct list_head vxlan_list;
83 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070084 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000085};
86
Cong Wange4c7ed42013-08-31 13:44:33 +080087union vxlan_addr {
88 struct sockaddr_in sin;
89 struct sockaddr_in6 sin6;
90 struct sockaddr sa;
91};
92
David Stevens66817122013-03-15 04:35:51 +000093struct vxlan_rdst {
Cong Wange4c7ed42013-08-31 13:44:33 +080094 union vxlan_addr remote_ip;
David Stevens66817122013-03-15 04:35:51 +000095 __be16 remote_port;
96 u32 remote_vni;
97 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070098 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +030099 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000100};
101
stephen hemmingerd3428942012-10-01 12:32:35 +0000102/* Forwarding table entry */
103struct vxlan_fdb {
104 struct hlist_node hlist; /* linked list of entries */
105 struct rcu_head rcu;
106 unsigned long updated; /* jiffies */
107 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700108 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000109 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000110 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000111 u8 eth_addr[ETH_ALEN];
112};
113
stephen hemmingerd3428942012-10-01 12:32:35 +0000114/* Pseudo network device */
115struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000116 struct hlist_node hlist; /* vni hash table */
117 struct list_head next; /* vxlan's per namespace list */
118 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000119 struct net_device *dev;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +0200120 struct net *net; /* netns for packet i/o */
Atzm Watanabec7995c42013-04-16 02:50:52 +0000121 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800122 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000123 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000124 __u16 port_min; /* source port range */
125 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000126 __u8 tos; /* TOS override */
127 __u8 ttl;
Tom Herbert359a0ea2014-06-04 17:20:29 -0700128 u32 flags; /* VXLAN_F_* in vxlan.h */
stephen hemmingerd3428942012-10-01 12:32:35 +0000129
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700130 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700131 struct work_struct igmp_join;
132 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700133
stephen hemmingerd3428942012-10-01 12:32:35 +0000134 unsigned long age_interval;
135 struct timer_list age_timer;
136 spinlock_t hash_lock;
137 unsigned int addrcnt;
138 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000139
140 struct hlist_head fdb_head[FDB_HASH_SIZE];
141};
142
143/* salt for hash table */
144static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700145static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000146
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700147static void vxlan_sock_work(struct work_struct *work);
148
Cong Wange4c7ed42013-08-31 13:44:33 +0800149#if IS_ENABLED(CONFIG_IPV6)
150static inline
151bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
152{
153 if (a->sa.sa_family != b->sa.sa_family)
154 return false;
155 if (a->sa.sa_family == AF_INET6)
156 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
157 else
158 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
159}
160
161static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
162{
163 if (ipa->sa.sa_family == AF_INET6)
164 return ipv6_addr_any(&ipa->sin6.sin6_addr);
165 else
166 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
167}
168
169static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
170{
171 if (ipa->sa.sa_family == AF_INET6)
172 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
173 else
174 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
175}
176
177static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
178{
179 if (nla_len(nla) >= sizeof(struct in6_addr)) {
180 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
181 ip->sa.sa_family = AF_INET6;
182 return 0;
183 } else if (nla_len(nla) >= sizeof(__be32)) {
184 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
185 ip->sa.sa_family = AF_INET;
186 return 0;
187 } else {
188 return -EAFNOSUPPORT;
189 }
190}
191
192static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
193 const union vxlan_addr *ip)
194{
195 if (ip->sa.sa_family == AF_INET6)
196 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
197 else
198 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
199}
200
201#else /* !CONFIG_IPV6 */
202
203static inline
204bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
205{
206 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
207}
208
209static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
210{
211 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
212}
213
214static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
215{
216 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
217}
218
219static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
220{
221 if (nla_len(nla) >= sizeof(struct in6_addr)) {
222 return -EAFNOSUPPORT;
223 } else if (nla_len(nla) >= sizeof(__be32)) {
224 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
225 ip->sa.sa_family = AF_INET;
226 return 0;
227 } else {
228 return -EAFNOSUPPORT;
229 }
230}
231
232static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
233 const union vxlan_addr *ip)
234{
235 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
236}
237#endif
238
stephen hemminger553675f2013-05-16 11:35:20 +0000239/* Virtual Network hash table head */
240static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
241{
242 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
243}
244
245/* Socket hash table head */
246static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000247{
248 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
249
stephen hemminger553675f2013-05-16 11:35:20 +0000250 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
251}
252
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700253/* First remote destination for a forwarding entry.
254 * Guaranteed to be non-NULL because remotes are never deleted.
255 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700256static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700257{
stephen hemminger5ca54612013-08-04 17:17:39 -0700258 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
259}
260
261static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
262{
263 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700264}
265
Thomas Grafac5132d2015-01-15 03:53:56 +0100266/* Find VXLAN socket based on network namespace, address family and UDP port
267 * and enabled unshareable flags.
268 */
269static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
270 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000271{
272 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800273
274 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000275
276 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200277 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Thomas Grafac5132d2015-01-15 03:53:56 +0100278 inet_sk(vs->sock->sk)->sk.sk_family == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800279 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000280 return vs;
281 }
282 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000283}
284
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700285static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000286{
287 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000288
stephen hemminger553675f2013-05-16 11:35:20 +0000289 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000290 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000291 return vxlan;
292 }
293
294 return NULL;
295}
296
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700297/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200298static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
Thomas Grafac5132d2015-01-15 03:53:56 +0100299 sa_family_t family, __be16 port,
300 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700301{
302 struct vxlan_sock *vs;
303
Thomas Grafac5132d2015-01-15 03:53:56 +0100304 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700305 if (!vs)
306 return NULL;
307
308 return vxlan_vs_find_vni(vs, id);
309}
310
stephen hemmingerd3428942012-10-01 12:32:35 +0000311/* Fill in neighbour message in skbuff. */
312static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700313 const struct vxlan_fdb *fdb,
314 u32 portid, u32 seq, int type, unsigned int flags,
315 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000316{
317 unsigned long now = jiffies;
318 struct nda_cacheinfo ci;
319 struct nlmsghdr *nlh;
320 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000321 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000322
323 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
324 if (nlh == NULL)
325 return -EMSGSIZE;
326
327 ndm = nlmsg_data(nlh);
328 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000329
330 send_eth = send_ip = true;
331
332 if (type == RTM_GETNEIGH) {
333 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800334 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000335 send_eth = !is_zero_ether_addr(fdb->eth_addr);
336 } else
337 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000338 ndm->ndm_state = fdb->state;
339 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000340 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800341 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000342
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100343 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100344 nla_put_s32(skb, NDA_LINK_NETNSID,
345 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100346 goto nla_put_failure;
347
David Stevense4f67ad2012-11-20 02:50:14 +0000348 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000349 goto nla_put_failure;
350
Cong Wange4c7ed42013-08-31 13:44:33 +0800351 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000352 goto nla_put_failure;
353
stephen hemminger823aa872013-04-27 11:31:57 +0000354 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000355 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
356 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000357 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700358 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000359 goto nla_put_failure;
360 if (rdst->remote_ifindex &&
361 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000362 goto nla_put_failure;
363
364 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
365 ci.ndm_confirmed = 0;
366 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
367 ci.ndm_refcnt = 0;
368
369 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
370 goto nla_put_failure;
371
Johannes Berg053c0952015-01-16 22:09:00 +0100372 nlmsg_end(skb, nlh);
373 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000374
375nla_put_failure:
376 nlmsg_cancel(skb, nlh);
377 return -EMSGSIZE;
378}
379
380static inline size_t vxlan_nlmsg_size(void)
381{
382 return NLMSG_ALIGN(sizeof(struct ndmsg))
383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100388 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000389 + nla_total_size(sizeof(struct nda_cacheinfo));
390}
391
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200392static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
393 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000394{
395 struct net *net = dev_net(vxlan->dev);
396 struct sk_buff *skb;
397 int err = -ENOBUFS;
398
399 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
400 if (skb == NULL)
401 goto errout;
402
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200403 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000404 if (err < 0) {
405 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
406 WARN_ON(err == -EMSGSIZE);
407 kfree_skb(skb);
408 goto errout;
409 }
410
411 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
412 return;
413errout:
414 if (err < 0)
415 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
416}
417
Cong Wange4c7ed42013-08-31 13:44:33 +0800418static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000419{
420 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700421 struct vxlan_fdb f = {
422 .state = NUD_STALE,
423 };
424 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800425 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700426 .remote_vni = VXLAN_N_VID,
427 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700428
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200429 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000430}
431
432static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
433{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700434 struct vxlan_fdb f = {
435 .state = NUD_STALE,
436 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200437 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000438
David Stevense4f67ad2012-11-20 02:50:14 +0000439 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
440
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200441 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000442}
443
stephen hemmingerd3428942012-10-01 12:32:35 +0000444/* Hash Ethernet address */
445static u32 eth_hash(const unsigned char *addr)
446{
447 u64 value = get_unaligned((u64 *)addr);
448
449 /* only want 6 bytes */
450#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000451 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000452#else
453 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000454#endif
455 return hash_64(value, FDB_HASH_BITS);
456}
457
458/* Hash chain to use given mac address */
459static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
460 const u8 *mac)
461{
462 return &vxlan->fdb_head[eth_hash(mac)];
463}
464
465/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000466static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000467 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000468{
469 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
470 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000471
Sasha Levinb67bfe02013-02-27 17:06:00 -0800472 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700473 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000474 return f;
475 }
476
477 return NULL;
478}
479
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000480static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
481 const u8 *mac)
482{
483 struct vxlan_fdb *f;
484
485 f = __vxlan_find_mac(vxlan, mac);
486 if (f)
487 f->used = jiffies;
488
489 return f;
490}
491
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300492/* caller should hold vxlan->hash_lock */
493static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800494 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300495 __u32 vni, __u32 ifindex)
496{
497 struct vxlan_rdst *rd;
498
499 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800500 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300501 rd->remote_port == port &&
502 rd->remote_vni == vni &&
503 rd->remote_ifindex == ifindex)
504 return rd;
505 }
506
507 return NULL;
508}
509
Thomas Richter906dc182013-07-19 17:20:07 +0200510/* Replace destination of unicast mac */
511static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800512 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200513{
514 struct vxlan_rdst *rd;
515
516 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
517 if (rd)
518 return 0;
519
520 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
521 if (!rd)
522 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800523 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200524 rd->remote_port = port;
525 rd->remote_vni = vni;
526 rd->remote_ifindex = ifindex;
527 return 1;
528}
529
David Stevens66817122013-03-15 04:35:51 +0000530/* Add/update destinations for multicast */
531static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200532 union vxlan_addr *ip, __be16 port, __u32 vni,
533 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000534{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700535 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000536
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300537 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
538 if (rd)
539 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700540
David Stevens66817122013-03-15 04:35:51 +0000541 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
542 if (rd == NULL)
543 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800544 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000545 rd->remote_port = port;
546 rd->remote_vni = vni;
547 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700548
549 list_add_tail_rcu(&rd->list, &f->remotes);
550
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200551 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000552 return 1;
553}
554
Tom Herbertdfd86452015-01-12 17:00:38 -0800555static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
556 unsigned int off,
557 struct vxlanhdr *vh, size_t hdrlen,
558 u32 data)
559{
560 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -0800561
562 if (skb->remcsum_offload)
563 return vh;
564
565 if (!NAPI_GRO_CB(skb)->csum_valid)
566 return NULL;
567
568 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
569 offset = start + ((data & VXLAN_RCO_UDP) ?
570 offsetof(struct udphdr, check) :
571 offsetof(struct tcphdr, check));
572
573 plen = hdrlen + offset + sizeof(u16);
574
575 /* Pull checksum that will be written */
576 if (skb_gro_header_hard(skb, off + plen)) {
577 vh = skb_gro_header_slow(skb, off + plen, off);
578 if (!vh)
579 return NULL;
580 }
581
Tom Herbertdcdc8992015-02-02 16:07:34 -0800582 skb_gro_remcsum_process(skb, (void *)vh + hdrlen, start, offset);
Tom Herbertdfd86452015-01-12 17:00:38 -0800583
584 skb->remcsum_offload = 1;
585
586 return vh;
587}
588
Tom Herberta2b12f32015-01-12 17:00:37 -0800589static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
590 struct sk_buff *skb,
591 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200592{
593 struct sk_buff *p, **pp = NULL;
594 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800595 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200596 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800597 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
598 udp_offloads);
599 u32 flags;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200600
601 off_vx = skb_gro_offset(skb);
602 hlen = off_vx + sizeof(*vh);
603 vh = skb_gro_header_fast(skb, off_vx);
604 if (skb_gro_header_hard(skb, hlen)) {
605 vh = skb_gro_header_slow(skb, hlen, off_vx);
606 if (unlikely(!vh))
607 goto out;
608 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200609
Tom Herbertdfd86452015-01-12 17:00:38 -0800610 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
611 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
612
613 flags = ntohl(vh->vx_flags);
614
615 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
616 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
617 ntohl(vh->vx_vni));
618
619 if (!vh)
620 goto out;
621 }
622
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200623 flush = 0;
624
625 for (p = *head; p; p = p->next) {
626 if (!NAPI_GRO_CB(p)->same_flow)
627 continue;
628
629 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100630 if (vh->vx_flags != vh2->vx_flags ||
631 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200632 NAPI_GRO_CB(p)->same_flow = 0;
633 continue;
634 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200635 }
636
Jesse Gross9b174d82014-12-30 19:10:15 -0800637 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200638
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200639out:
640 NAPI_GRO_CB(skb)->flush |= flush;
641
642 return pp;
643}
644
Tom Herberta2b12f32015-01-12 17:00:37 -0800645static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
646 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200647{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800648 udp_tunnel_gro_complete(skb, nhoff);
649
Jesse Gross9b174d82014-12-30 19:10:15 -0800650 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200651}
652
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700653/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200654static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700655{
656 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200657 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700658 struct net *net = sock_net(sk);
659 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700660 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200661 int err;
662
663 if (sa_family == AF_INET) {
664 err = udp_add_offload(&vs->udp_offloads);
665 if (err)
666 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
667 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700668
669 rcu_read_lock();
670 for_each_netdev_rcu(net, dev) {
671 if (dev->netdev_ops->ndo_add_vxlan_port)
672 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
673 port);
674 }
675 rcu_read_unlock();
676}
677
678/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200679static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700680{
681 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200682 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700683 struct net *net = sock_net(sk);
684 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700685 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700686
687 rcu_read_lock();
688 for_each_netdev_rcu(net, dev) {
689 if (dev->netdev_ops->ndo_del_vxlan_port)
690 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
691 port);
692 }
693 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200694
695 if (sa_family == AF_INET)
696 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700697}
698
stephen hemmingerd3428942012-10-01 12:32:35 +0000699/* Add new entry to forwarding table -- assumes lock held */
700static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800701 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000702 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000703 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000704 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000705{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200706 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000707 struct vxlan_fdb *f;
708 int notify = 0;
709
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000710 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000711 if (f) {
712 if (flags & NLM_F_EXCL) {
713 netdev_dbg(vxlan->dev,
714 "lost race to create %pM\n", mac);
715 return -EEXIST;
716 }
717 if (f->state != state) {
718 f->state = state;
719 f->updated = jiffies;
720 notify = 1;
721 }
David Stevensae884082013-04-19 00:36:26 +0000722 if (f->flags != ndm_flags) {
723 f->flags = ndm_flags;
724 f->updated = jiffies;
725 notify = 1;
726 }
Thomas Richter906dc182013-07-19 17:20:07 +0200727 if ((flags & NLM_F_REPLACE)) {
728 /* Only change unicasts */
729 if (!(is_multicast_ether_addr(f->eth_addr) ||
730 is_zero_ether_addr(f->eth_addr))) {
731 int rc = vxlan_fdb_replace(f, ip, port, vni,
732 ifindex);
733
734 if (rc < 0)
735 return rc;
736 notify |= rc;
737 } else
738 return -EOPNOTSUPP;
739 }
David Stevens66817122013-03-15 04:35:51 +0000740 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300741 (is_multicast_ether_addr(f->eth_addr) ||
742 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200743 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
744 &rd);
David Stevens66817122013-03-15 04:35:51 +0000745
746 if (rc < 0)
747 return rc;
748 notify |= rc;
749 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000750 } else {
751 if (!(flags & NLM_F_CREATE))
752 return -ENOENT;
753
754 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
755 return -ENOSPC;
756
Thomas Richter906dc182013-07-19 17:20:07 +0200757 /* Disallow replace to add a multicast entry */
758 if ((flags & NLM_F_REPLACE) &&
759 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
760 return -EOPNOTSUPP;
761
Cong Wange4c7ed42013-08-31 13:44:33 +0800762 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000763 f = kmalloc(sizeof(*f), GFP_ATOMIC);
764 if (!f)
765 return -ENOMEM;
766
767 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000768 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000769 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000770 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700771 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000772 memcpy(f->eth_addr, mac, ETH_ALEN);
773
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200774 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700775
stephen hemmingerd3428942012-10-01 12:32:35 +0000776 ++vxlan->addrcnt;
777 hlist_add_head_rcu(&f->hlist,
778 vxlan_fdb_head(vxlan, mac));
779 }
780
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200781 if (notify) {
782 if (rd == NULL)
783 rd = first_remote_rtnl(f);
784 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
785 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000786
787 return 0;
788}
789
Wei Yongjun6706c822013-04-11 19:00:35 +0000790static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000791{
792 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700793 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000794
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700795 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000796 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000797 kfree(f);
798}
799
stephen hemmingerd3428942012-10-01 12:32:35 +0000800static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
801{
802 netdev_dbg(vxlan->dev,
803 "delete %pM\n", f->eth_addr);
804
805 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200806 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000807
808 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000809 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000810}
811
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300812static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800813 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300814{
815 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800816 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300817
818 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800819 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
820 if (err)
821 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300822 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800823 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
824 if (remote->sa.sa_family == AF_INET) {
825 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
826 ip->sa.sa_family = AF_INET;
827#if IS_ENABLED(CONFIG_IPV6)
828 } else {
829 ip->sin6.sin6_addr = in6addr_any;
830 ip->sa.sa_family = AF_INET6;
831#endif
832 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300833 }
834
835 if (tb[NDA_PORT]) {
836 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
837 return -EINVAL;
838 *port = nla_get_be16(tb[NDA_PORT]);
839 } else {
840 *port = vxlan->dst_port;
841 }
842
843 if (tb[NDA_VNI]) {
844 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
845 return -EINVAL;
846 *vni = nla_get_u32(tb[NDA_VNI]);
847 } else {
848 *vni = vxlan->default_dst.remote_vni;
849 }
850
851 if (tb[NDA_IFINDEX]) {
852 struct net_device *tdev;
853
854 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
855 return -EINVAL;
856 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800857 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300858 if (!tdev)
859 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300860 } else {
861 *ifindex = 0;
862 }
863
864 return 0;
865}
866
stephen hemmingerd3428942012-10-01 12:32:35 +0000867/* Add static entry (via netlink) */
868static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
869 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100870 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000871{
872 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300873 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800874 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000875 __be16 port;
876 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000877 int err;
878
879 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
880 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
881 ndm->ndm_state);
882 return -EINVAL;
883 }
884
885 if (tb[NDA_DST] == NULL)
886 return -EINVAL;
887
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300888 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
889 if (err)
890 return err;
David Stevens66817122013-03-15 04:35:51 +0000891
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300892 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
893 return -EAFNOSUPPORT;
894
stephen hemmingerd3428942012-10-01 12:32:35 +0000895 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800896 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000897 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000898 spin_unlock_bh(&vxlan->hash_lock);
899
900 return err;
901}
902
903/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000904static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
905 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100906 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000907{
908 struct vxlan_dev *vxlan = netdev_priv(dev);
909 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300910 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800911 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300912 __be16 port;
913 u32 vni, ifindex;
914 int err;
915
916 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
917 if (err)
918 return err;
919
920 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000921
922 spin_lock_bh(&vxlan->hash_lock);
923 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300924 if (!f)
925 goto out;
926
Cong Wange4c7ed42013-08-31 13:44:33 +0800927 if (!vxlan_addr_any(&ip)) {
928 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300929 if (!rd)
930 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000931 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300932
933 err = 0;
934
935 /* remove a destination if it's not the only one on the list,
936 * otherwise destroy the fdb entry
937 */
938 if (rd && !list_is_singular(&f->remotes)) {
939 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200940 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800941 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300942 goto out;
943 }
944
945 vxlan_fdb_destroy(vxlan, f);
946
947out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000948 spin_unlock_bh(&vxlan->hash_lock);
949
950 return err;
951}
952
953/* Dump forwarding table */
954static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400955 struct net_device *dev,
956 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000957{
958 struct vxlan_dev *vxlan = netdev_priv(dev);
959 unsigned int h;
960
961 for (h = 0; h < FDB_HASH_SIZE; ++h) {
962 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000963 int err;
964
Sasha Levinb67bfe02013-02-27 17:06:00 -0800965 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000966 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000967
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700968 if (idx < cb->args[0])
969 goto skip;
970
971 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000972 err = vxlan_fdb_info(skb, vxlan, f,
973 NETLINK_CB(cb->skb).portid,
974 cb->nlh->nlmsg_seq,
975 RTM_NEWNEIGH,
976 NLM_F_MULTI, rd);
977 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700978 goto out;
David Stevens66817122013-03-15 04:35:51 +0000979 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700980skip:
981 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000982 }
983 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700984out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000985 return idx;
986}
987
988/* Watch incoming packets to learn mapping between Ethernet address
989 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700990 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000991 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700992static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800993 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000994{
995 struct vxlan_dev *vxlan = netdev_priv(dev);
996 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000997
998 f = vxlan_find_mac(vxlan, src_mac);
999 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001000 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001001
Cong Wange4c7ed42013-08-31 13:44:33 +08001002 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001003 return false;
1004
1005 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -07001006 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -07001007 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001008
1009 if (net_ratelimit())
1010 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001011 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001012 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001013
Cong Wange4c7ed42013-08-31 13:44:33 +08001014 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001015 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001016 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001017 } else {
1018 /* learned new entry */
1019 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001020
1021 /* close off race between vxlan_flush and incoming packets */
1022 if (netif_running(dev))
1023 vxlan_fdb_create(vxlan, src_mac, src_ip,
1024 NUD_REACHABLE,
1025 NLM_F_EXCL|NLM_F_CREATE,
1026 vxlan->dst_port,
1027 vxlan->default_dst.remote_vni,
1028 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001029 spin_unlock(&vxlan->hash_lock);
1030 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001031
1032 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001033}
1034
stephen hemmingerd3428942012-10-01 12:32:35 +00001035/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001036static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001037{
stephen hemminger553675f2013-05-16 11:35:20 +00001038 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001039
Gao feng95ab0992013-12-10 16:37:33 +08001040 /* The vxlan_sock is only used by dev, leaving group has
1041 * no effect on other vxlan devices.
1042 */
1043 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1044 return false;
1045
stephen hemminger553675f2013-05-16 11:35:20 +00001046 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001047 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001048 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001049
Gao feng95ab0992013-12-10 16:37:33 +08001050 if (vxlan->vn_sock != dev->vn_sock)
1051 continue;
1052
1053 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1054 &dev->default_dst.remote_ip))
1055 continue;
1056
1057 if (vxlan->default_dst.remote_ifindex !=
1058 dev->default_dst.remote_ifindex)
1059 continue;
1060
1061 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001062 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001063
1064 return false;
1065}
1066
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001067static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001068{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001069 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001070}
1071
Pravin B Shelar012a5722013-08-19 11:23:07 -07001072void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001073{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001074 struct sock *sk = vs->sock->sk;
1075 struct net *net = sock_net(sk);
1076 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001077
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001078 if (!atomic_dec_and_test(&vs->refcnt))
1079 return;
1080
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001081 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001082 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001083 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001084 spin_unlock(&vn->sock_lock);
1085
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001086 queue_work(vxlan_wq, &vs->del_work);
1087}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001088EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001089
stephen hemminger3fc2de22013-07-18 08:40:15 -07001090/* Callback to update multicast group membership when first VNI on
1091 * multicast asddress is brought up
1092 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001093 */
stephen hemminger3fc2de22013-07-18 08:40:15 -07001094static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001095{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001096 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001097 struct vxlan_sock *vs = vxlan->vn_sock;
1098 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001099 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1100 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001101
stephen hemmingerd3428942012-10-01 12:32:35 +00001102 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001103 if (ip->sa.sa_family == AF_INET) {
1104 struct ip_mreqn mreq = {
1105 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1106 .imr_ifindex = ifindex,
1107 };
1108
1109 ip_mc_join_group(sk, &mreq);
1110#if IS_ENABLED(CONFIG_IPV6)
1111 } else {
1112 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1113 &ip->sin6.sin6_addr);
1114#endif
1115 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001116 release_sock(sk);
1117
Pravin B Shelar012a5722013-08-19 11:23:07 -07001118 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001119 dev_put(vxlan->dev);
1120}
1121
1122/* Inverse of vxlan_igmp_join when last VNI is brought down */
1123static void vxlan_igmp_leave(struct work_struct *work)
1124{
1125 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001126 struct vxlan_sock *vs = vxlan->vn_sock;
1127 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001128 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1129 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001130
1131 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001132 if (ip->sa.sa_family == AF_INET) {
1133 struct ip_mreqn mreq = {
1134 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1135 .imr_ifindex = ifindex,
1136 };
1137
1138 ip_mc_leave_group(sk, &mreq);
1139#if IS_ENABLED(CONFIG_IPV6)
1140 } else {
1141 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1142 &ip->sin6.sin6_addr);
1143#endif
1144 }
1145
stephen hemmingerd3428942012-10-01 12:32:35 +00001146 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001147
Pravin B Shelar012a5722013-08-19 11:23:07 -07001148 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001149 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001150}
1151
Tom Herbertdfd86452015-01-12 17:00:38 -08001152static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1153 size_t hdrlen, u32 data)
1154{
1155 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -08001156
1157 if (skb->remcsum_offload) {
1158 /* Already processed in GRO path */
1159 skb->remcsum_offload = 0;
1160 return vh;
1161 }
1162
1163 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1164 offset = start + ((data & VXLAN_RCO_UDP) ?
1165 offsetof(struct udphdr, check) :
1166 offsetof(struct tcphdr, check));
1167
1168 plen = hdrlen + offset + sizeof(u16);
1169
1170 if (!pskb_may_pull(skb, plen))
1171 return NULL;
1172
1173 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1174
Tom Herbertdcdc8992015-02-02 16:07:34 -08001175 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset);
Tom Herbertdfd86452015-01-12 17:00:38 -08001176
1177 return vh;
1178}
1179
stephen hemmingerd3428942012-10-01 12:32:35 +00001180/* Callback from net/ipv4/udp.c to receive packets */
1181static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1182{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001183 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001184 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001185 u32 flags, vni;
Thomas Graf35114942015-01-15 03:53:55 +01001186 struct vxlan_metadata md = {0};
stephen hemmingerd3428942012-10-01 12:32:35 +00001187
stephen hemmingerd3428942012-10-01 12:32:35 +00001188 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001189 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001190 goto error;
1191
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001192 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001193 flags = ntohl(vxh->vx_flags);
1194 vni = ntohl(vxh->vx_vni);
1195
1196 if (flags & VXLAN_HF_VNI) {
1197 flags &= ~VXLAN_HF_VNI;
1198 } else {
1199 /* VNI flag always required to be set */
1200 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001201 }
1202
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001203 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001204 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001205 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001206
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001207 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001208 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001209 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001210
Tom Herbertdfd86452015-01-12 17:00:38 -08001211 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1212 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1213 if (!vxh)
1214 goto drop;
1215
1216 flags &= ~VXLAN_HF_RCO;
1217 vni &= VXLAN_VID_MASK;
1218 }
1219
Thomas Graf35114942015-01-15 03:53:55 +01001220 /* For backwards compatibility, only allow reserved fields to be
1221 * used by VXLAN extensions if explicitly requested.
1222 */
1223 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1224 struct vxlanhdr_gbp *gbp;
1225
1226 gbp = (struct vxlanhdr_gbp *)vxh;
1227 md.gbp = ntohs(gbp->policy_id);
1228
1229 if (gbp->dont_learn)
1230 md.gbp |= VXLAN_GBP_DONT_LEARN;
1231
1232 if (gbp->policy_applied)
1233 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1234
1235 flags &= ~VXLAN_GBP_USED_BITS;
1236 }
1237
Tom Herbertdfd86452015-01-12 17:00:38 -08001238 if (flags || (vni & ~VXLAN_VID_MASK)) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001239 /* If there are any unprocessed flags remaining treat
1240 * this as a malformed packet. This behavior diverges from
1241 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1242 * in reserved fields are to be ignored. The approach here
1243 * maintains compatbility with previous stack code, and also
1244 * is more robust and provides a little more security in
1245 * adding extensions to VXLAN.
1246 */
1247
1248 goto bad_flags;
1249 }
1250
Thomas Graf35114942015-01-15 03:53:55 +01001251 md.vni = vxh->vx_vni;
1252 vs->rcv(vs, skb, &md);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001253 return 0;
1254
1255drop:
1256 /* Consume bad packet */
1257 kfree_skb(skb);
1258 return 0;
1259
Tom Herbert3bf39472015-01-08 12:31:18 -08001260bad_flags:
1261 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1262 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1263
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001264error:
1265 /* Return non vxlan pkt */
1266 return 1;
1267}
1268
Thomas Graf35114942015-01-15 03:53:55 +01001269static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1270 struct vxlan_metadata *md)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001271{
Cong Wange4c7ed42013-08-31 13:44:33 +08001272 struct iphdr *oip = NULL;
1273 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001274 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001275 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001276 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001277 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001278 int err = 0;
1279 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001280
Thomas Graf35114942015-01-15 03:53:55 +01001281 vni = ntohl(md->vni) >> 8;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001282 /* Is this VNI defined? */
1283 vxlan = vxlan_vs_find_vni(vs, vni);
1284 if (!vxlan)
1285 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001286
Cong Wange4c7ed42013-08-31 13:44:33 +08001287 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001288 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001289 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001290 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001291 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001292
1293 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001294 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001295 goto drop;
1296
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001297 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001298 if (remote_ip->sa.sa_family == AF_INET) {
1299 oip = ip_hdr(skb);
1300 saddr.sin.sin_addr.s_addr = oip->saddr;
1301 saddr.sa.sa_family = AF_INET;
1302#if IS_ENABLED(CONFIG_IPV6)
1303 } else {
1304 oip6 = ipv6_hdr(skb);
1305 saddr.sin6.sin6_addr = oip6->saddr;
1306 saddr.sa.sa_family = AF_INET6;
1307#endif
1308 }
1309
stephen hemminger26a41ae62013-06-17 12:09:58 -07001310 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001311 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001312 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001313
stephen hemmingerd3428942012-10-01 12:32:35 +00001314 skb_reset_network_header(skb);
Thomas Graf35114942015-01-15 03:53:55 +01001315 skb->mark = md->gbp;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001316
Cong Wange4c7ed42013-08-31 13:44:33 +08001317 if (oip6)
1318 err = IP6_ECN_decapsulate(oip6, skb);
1319 if (oip)
1320 err = IP_ECN_decapsulate(oip, skb);
1321
stephen hemmingerd3428942012-10-01 12:32:35 +00001322 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001323 if (log_ecn_error) {
1324 if (oip6)
1325 net_info_ratelimited("non-ECT from %pI6\n",
1326 &oip6->saddr);
1327 if (oip)
1328 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1329 &oip->saddr, oip->tos);
1330 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001331 if (err > 1) {
1332 ++vxlan->dev->stats.rx_frame_errors;
1333 ++vxlan->dev->stats.rx_errors;
1334 goto drop;
1335 }
1336 }
1337
Pravin B Shelare8171042013-03-25 14:49:46 +00001338 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001339 u64_stats_update_begin(&stats->syncp);
1340 stats->rx_packets++;
1341 stats->rx_bytes += skb->len;
1342 u64_stats_update_end(&stats->syncp);
1343
1344 netif_rx(skb);
1345
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001346 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001347drop:
1348 /* Consume bad packet */
1349 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001350}
1351
David Stevense4f67ad2012-11-20 02:50:14 +00001352static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1353{
1354 struct vxlan_dev *vxlan = netdev_priv(dev);
1355 struct arphdr *parp;
1356 u8 *arpptr, *sha;
1357 __be32 sip, tip;
1358 struct neighbour *n;
1359
1360 if (dev->flags & IFF_NOARP)
1361 goto out;
1362
1363 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1364 dev->stats.tx_dropped++;
1365 goto out;
1366 }
1367 parp = arp_hdr(skb);
1368
1369 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1370 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1371 parp->ar_pro != htons(ETH_P_IP) ||
1372 parp->ar_op != htons(ARPOP_REQUEST) ||
1373 parp->ar_hln != dev->addr_len ||
1374 parp->ar_pln != 4)
1375 goto out;
1376 arpptr = (u8 *)parp + sizeof(struct arphdr);
1377 sha = arpptr;
1378 arpptr += dev->addr_len; /* sha */
1379 memcpy(&sip, arpptr, sizeof(sip));
1380 arpptr += sizeof(sip);
1381 arpptr += dev->addr_len; /* tha */
1382 memcpy(&tip, arpptr, sizeof(tip));
1383
1384 if (ipv4_is_loopback(tip) ||
1385 ipv4_is_multicast(tip))
1386 goto out;
1387
1388 n = neigh_lookup(&arp_tbl, &tip, dev);
1389
1390 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001391 struct vxlan_fdb *f;
1392 struct sk_buff *reply;
1393
1394 if (!(n->nud_state & NUD_CONNECTED)) {
1395 neigh_release(n);
1396 goto out;
1397 }
1398
1399 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001400 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001401 /* bridge-local neighbor */
1402 neigh_release(n);
1403 goto out;
1404 }
1405
1406 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1407 n->ha, sha);
1408
1409 neigh_release(n);
1410
David Stevens73461352014-03-18 12:32:29 -04001411 if (reply == NULL)
1412 goto out;
1413
David Stevense4f67ad2012-11-20 02:50:14 +00001414 skb_reset_mac_header(reply);
1415 __skb_pull(reply, skb_network_offset(reply));
1416 reply->ip_summed = CHECKSUM_UNNECESSARY;
1417 reply->pkt_type = PACKET_HOST;
1418
1419 if (netif_rx_ni(reply) == NET_RX_DROP)
1420 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001421 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1422 union vxlan_addr ipa = {
1423 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001424 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001425 };
1426
1427 vxlan_ip_miss(dev, &ipa);
1428 }
David Stevense4f67ad2012-11-20 02:50:14 +00001429out:
1430 consume_skb(skb);
1431 return NETDEV_TX_OK;
1432}
1433
Cong Wangf564f452013-08-31 13:44:36 +08001434#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001435static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1436 struct neighbour *n, bool isrouter)
1437{
1438 struct net_device *dev = request->dev;
1439 struct sk_buff *reply;
1440 struct nd_msg *ns, *na;
1441 struct ipv6hdr *pip6;
1442 u8 *daddr;
1443 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1444 int ns_olen;
1445 int i, len;
1446
1447 if (dev == NULL)
1448 return NULL;
1449
1450 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1451 sizeof(*na) + na_olen + dev->needed_tailroom;
1452 reply = alloc_skb(len, GFP_ATOMIC);
1453 if (reply == NULL)
1454 return NULL;
1455
1456 reply->protocol = htons(ETH_P_IPV6);
1457 reply->dev = dev;
1458 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1459 skb_push(reply, sizeof(struct ethhdr));
1460 skb_set_mac_header(reply, 0);
1461
1462 ns = (struct nd_msg *)skb_transport_header(request);
1463
1464 daddr = eth_hdr(request)->h_source;
1465 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1466 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1467 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1468 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1469 break;
1470 }
1471 }
1472
1473 /* Ethernet header */
1474 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1475 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1476 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1477 reply->protocol = htons(ETH_P_IPV6);
1478
1479 skb_pull(reply, sizeof(struct ethhdr));
1480 skb_set_network_header(reply, 0);
1481 skb_put(reply, sizeof(struct ipv6hdr));
1482
1483 /* IPv6 header */
1484
1485 pip6 = ipv6_hdr(reply);
1486 memset(pip6, 0, sizeof(struct ipv6hdr));
1487 pip6->version = 6;
1488 pip6->priority = ipv6_hdr(request)->priority;
1489 pip6->nexthdr = IPPROTO_ICMPV6;
1490 pip6->hop_limit = 255;
1491 pip6->daddr = ipv6_hdr(request)->saddr;
1492 pip6->saddr = *(struct in6_addr *)n->primary_key;
1493
1494 skb_pull(reply, sizeof(struct ipv6hdr));
1495 skb_set_transport_header(reply, 0);
1496
1497 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1498
1499 /* Neighbor Advertisement */
1500 memset(na, 0, sizeof(*na)+na_olen);
1501 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1502 na->icmph.icmp6_router = isrouter;
1503 na->icmph.icmp6_override = 1;
1504 na->icmph.icmp6_solicited = 1;
1505 na->target = ns->target;
1506 ether_addr_copy(&na->opt[2], n->ha);
1507 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1508 na->opt[1] = na_olen >> 3;
1509
1510 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1511 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1512 csum_partial(na, sizeof(*na)+na_olen, 0));
1513
1514 pip6->payload_len = htons(sizeof(*na)+na_olen);
1515
1516 skb_push(reply, sizeof(struct ipv6hdr));
1517
1518 reply->ip_summed = CHECKSUM_UNNECESSARY;
1519
1520 return reply;
1521}
1522
Cong Wangf564f452013-08-31 13:44:36 +08001523static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1524{
1525 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001526 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001527 const struct ipv6hdr *iphdr;
1528 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001529 struct neighbour *n;
1530 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001531
1532 in6_dev = __in6_dev_get(dev);
1533 if (!in6_dev)
1534 goto out;
1535
Cong Wangf564f452013-08-31 13:44:36 +08001536 iphdr = ipv6_hdr(skb);
1537 saddr = &iphdr->saddr;
1538 daddr = &iphdr->daddr;
1539
Cong Wangf564f452013-08-31 13:44:36 +08001540 msg = (struct nd_msg *)skb_transport_header(skb);
1541 if (msg->icmph.icmp6_code != 0 ||
1542 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1543 goto out;
1544
David Stevens4b29dba2014-03-24 10:39:58 -04001545 if (ipv6_addr_loopback(daddr) ||
1546 ipv6_addr_is_multicast(&msg->target))
1547 goto out;
1548
1549 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001550
1551 if (n) {
1552 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001553 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001554
1555 if (!(n->nud_state & NUD_CONNECTED)) {
1556 neigh_release(n);
1557 goto out;
1558 }
1559
1560 f = vxlan_find_mac(vxlan, n->ha);
1561 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1562 /* bridge-local neighbor */
1563 neigh_release(n);
1564 goto out;
1565 }
1566
David Stevens4b29dba2014-03-24 10:39:58 -04001567 reply = vxlan_na_create(skb, n,
1568 !!(f ? f->flags & NTF_ROUTER : 0));
1569
Cong Wangf564f452013-08-31 13:44:36 +08001570 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001571
1572 if (reply == NULL)
1573 goto out;
1574
1575 if (netif_rx_ni(reply) == NET_RX_DROP)
1576 dev->stats.rx_dropped++;
1577
Cong Wangf564f452013-08-31 13:44:36 +08001578 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001579 union vxlan_addr ipa = {
1580 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001581 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001582 };
1583
Cong Wangf564f452013-08-31 13:44:36 +08001584 vxlan_ip_miss(dev, &ipa);
1585 }
1586
1587out:
1588 consume_skb(skb);
1589 return NETDEV_TX_OK;
1590}
1591#endif
1592
David Stevense4f67ad2012-11-20 02:50:14 +00001593static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1594{
1595 struct vxlan_dev *vxlan = netdev_priv(dev);
1596 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001597
1598 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1599 return false;
1600
1601 n = NULL;
1602 switch (ntohs(eth_hdr(skb)->h_proto)) {
1603 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001604 {
1605 struct iphdr *pip;
1606
David Stevense4f67ad2012-11-20 02:50:14 +00001607 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1608 return false;
1609 pip = ip_hdr(skb);
1610 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001611 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1612 union vxlan_addr ipa = {
1613 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001614 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001615 };
1616
1617 vxlan_ip_miss(dev, &ipa);
1618 return false;
1619 }
1620
David Stevense4f67ad2012-11-20 02:50:14 +00001621 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001622 }
1623#if IS_ENABLED(CONFIG_IPV6)
1624 case ETH_P_IPV6:
1625 {
1626 struct ipv6hdr *pip6;
1627
1628 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1629 return false;
1630 pip6 = ipv6_hdr(skb);
1631 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1632 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1633 union vxlan_addr ipa = {
1634 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001635 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001636 };
1637
1638 vxlan_ip_miss(dev, &ipa);
1639 return false;
1640 }
1641
1642 break;
1643 }
1644#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001645 default:
1646 return false;
1647 }
1648
1649 if (n) {
1650 bool diff;
1651
Joe Perches7367d0b2013-09-01 11:51:23 -07001652 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001653 if (diff) {
1654 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1655 dev->addr_len);
1656 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1657 }
1658 neigh_release(n);
1659 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001660 }
1661
David Stevense4f67ad2012-11-20 02:50:14 +00001662 return false;
1663}
1664
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001665static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001666 struct vxlan_metadata *md)
1667{
1668 struct vxlanhdr_gbp *gbp;
1669
Thomas Grafdb79a622015-02-04 17:00:04 +01001670 if (!md->gbp)
1671 return;
1672
Thomas Graf35114942015-01-15 03:53:55 +01001673 gbp = (struct vxlanhdr_gbp *)vxh;
1674 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1675
1676 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1677 gbp->dont_learn = 1;
1678
1679 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1680 gbp->policy_applied = 1;
1681
1682 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1683}
1684
Cong Wange4c7ed42013-08-31 13:44:33 +08001685#if IS_ENABLED(CONFIG_IPV6)
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001686static int vxlan6_xmit_skb(struct dst_entry *dst, struct sk_buff *skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001687 struct net_device *dev, struct in6_addr *saddr,
1688 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Thomas Graf35114942015-01-15 03:53:55 +01001689 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001690 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Cong Wange4c7ed42013-08-31 13:44:33 +08001691{
Cong Wange4c7ed42013-08-31 13:44:33 +08001692 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001693 int min_headroom;
1694 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001695 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbertdfd86452015-01-12 17:00:38 -08001696 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1697 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001698
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001699 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001700 skb->ip_summed == CHECKSUM_PARTIAL) {
1701 int csum_start = skb_checksum_start_offset(skb);
1702
1703 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1704 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1705 (skb->csum_offset == offsetof(struct udphdr, check) ||
1706 skb->csum_offset == offsetof(struct tcphdr, check))) {
1707 udp_sum = false;
1708 type |= SKB_GSO_TUNNEL_REMCSUM;
1709 }
1710 }
1711
1712 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001713 if (IS_ERR(skb)) {
1714 err = -EINVAL;
1715 goto err;
1716 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001717
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001718 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001719
Cong Wange4c7ed42013-08-31 13:44:33 +08001720 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1721 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001722 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001723
1724 /* Need space for new headers (invalidates iph ptr) */
1725 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001726 if (unlikely(err)) {
1727 kfree_skb(skb);
1728 goto err;
1729 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001730
Jiri Pirko59682502014-11-19 14:04:59 +01001731 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001732 if (WARN_ON(!skb)) {
1733 err = -ENOMEM;
1734 goto err;
1735 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001736
1737 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001738 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001739 vxh->vx_vni = md->vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001740
Tom Herbertdfd86452015-01-12 17:00:38 -08001741 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1742 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1743 VXLAN_RCO_SHIFT;
1744
1745 if (skb->csum_offset == offsetof(struct udphdr, check))
1746 data |= VXLAN_RCO_UDP;
1747
1748 vxh->vx_vni |= htonl(data);
1749 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1750
1751 if (!skb_is_gso(skb)) {
1752 skb->ip_summed = CHECKSUM_NONE;
1753 skb->encapsulation = 0;
1754 }
1755 }
1756
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001757 if (vxflags & VXLAN_F_GBP)
1758 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001759
Tom Herbert996c9fd2014-09-29 20:22:33 -07001760 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1761
Tom Herbertd998f8e2015-01-20 11:23:04 -08001762 udp_tunnel6_xmit_skb(dst, skb, dev, saddr, daddr, prio,
1763 ttl, src_port, dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001764 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
Cong Wange4c7ed42013-08-31 13:44:33 +08001765 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001766err:
1767 dst_release(dst);
1768 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001769}
1770#endif
1771
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001772int vxlan_xmit_skb(struct rtable *rt, struct sk_buff *skb,
Pravin B Shelar49560532013-08-19 11:23:17 -07001773 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Thomas Graf35114942015-01-15 03:53:55 +01001774 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001775 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Pravin B Shelar49560532013-08-19 11:23:17 -07001776{
1777 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001778 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001779 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001780 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
Tom Herbertdfd86452015-01-12 17:00:38 -08001781 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1782 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001783
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001784 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001785 skb->ip_summed == CHECKSUM_PARTIAL) {
1786 int csum_start = skb_checksum_start_offset(skb);
1787
1788 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1789 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1790 (skb->csum_offset == offsetof(struct udphdr, check) ||
1791 skb->csum_offset == offsetof(struct tcphdr, check))) {
1792 udp_sum = false;
1793 type |= SKB_GSO_TUNNEL_REMCSUM;
1794 }
1795 }
1796
1797 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001798 if (IS_ERR(skb))
Pravin B Shelar74f47272014-12-23 16:20:36 -08001799 return PTR_ERR(skb);
Pravin B Shelar49560532013-08-19 11:23:17 -07001800
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001801 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001802 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001803 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001804
1805 /* Need space for new headers (invalidates iph ptr) */
1806 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001807 if (unlikely(err)) {
1808 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001809 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001810 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001811
Jiri Pirko59682502014-11-19 14:04:59 +01001812 skb = vlan_hwaccel_push_inside(skb);
1813 if (WARN_ON(!skb))
1814 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001815
Pravin B Shelar49560532013-08-19 11:23:17 -07001816 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001817 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001818 vxh->vx_vni = md->vni;
Pravin B Shelar49560532013-08-19 11:23:17 -07001819
Tom Herbertdfd86452015-01-12 17:00:38 -08001820 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1821 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1822 VXLAN_RCO_SHIFT;
1823
1824 if (skb->csum_offset == offsetof(struct udphdr, check))
1825 data |= VXLAN_RCO_UDP;
1826
1827 vxh->vx_vni |= htonl(data);
1828 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1829
1830 if (!skb_is_gso(skb)) {
1831 skb->ip_summed = CHECKSUM_NONE;
1832 skb->encapsulation = 0;
1833 }
1834 }
1835
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001836 if (vxflags & VXLAN_F_GBP)
1837 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001838
Tom Herbert996c9fd2014-09-29 20:22:33 -07001839 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1840
Tom Herbertd998f8e2015-01-20 11:23:04 -08001841 return udp_tunnel_xmit_skb(rt, skb, src, dst, tos,
1842 ttl, df, src_port, dst_port, xnet,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001843 !(vxflags & VXLAN_F_UDP_CSUM));
Pravin B Shelar49560532013-08-19 11:23:17 -07001844}
1845EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1846
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001847/* Bypass encapsulation if the destination is local */
1848static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1849 struct vxlan_dev *dst_vxlan)
1850{
Li RongQing8f849852014-01-04 13:57:59 +08001851 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001852 union vxlan_addr loopback;
1853 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001854 struct net_device *dev = skb->dev;
1855 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001856
Li RongQing8f849852014-01-04 13:57:59 +08001857 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1858 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001859 skb->pkt_type = PACKET_HOST;
1860 skb->encapsulation = 0;
1861 skb->dev = dst_vxlan->dev;
1862 __skb_pull(skb, skb_network_offset(skb));
1863
Cong Wange4c7ed42013-08-31 13:44:33 +08001864 if (remote_ip->sa.sa_family == AF_INET) {
1865 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1866 loopback.sa.sa_family = AF_INET;
1867#if IS_ENABLED(CONFIG_IPV6)
1868 } else {
1869 loopback.sin6.sin6_addr = in6addr_loopback;
1870 loopback.sa.sa_family = AF_INET6;
1871#endif
1872 }
1873
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001874 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001875 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001876
1877 u64_stats_update_begin(&tx_stats->syncp);
1878 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001879 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001880 u64_stats_update_end(&tx_stats->syncp);
1881
1882 if (netif_rx(skb) == NET_RX_SUCCESS) {
1883 u64_stats_update_begin(&rx_stats->syncp);
1884 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001885 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001886 u64_stats_update_end(&rx_stats->syncp);
1887 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001888 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001889 }
1890}
1891
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001892static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1893 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001894{
1895 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001896 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001897 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001898 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001899 union vxlan_addr *dst;
Thomas Graf35114942015-01-15 03:53:55 +01001900 struct vxlan_metadata md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001901 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001902 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001903 __be16 df = 0;
1904 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001905 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001906
stephen hemminger823aa872013-04-27 11:31:57 +00001907 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001908 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001909 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001910
Cong Wange4c7ed42013-08-31 13:44:33 +08001911 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001912 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001913 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001914 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001915 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001916 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001917 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001918 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001919
stephen hemmingerd3428942012-10-01 12:32:35 +00001920 old_iph = ip_hdr(skb);
1921
stephen hemmingerd3428942012-10-01 12:32:35 +00001922 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001923 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001924 ttl = 1;
1925
1926 tos = vxlan->tos;
1927 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001928 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001929
Tom Herbert535fb8d2014-07-01 21:32:49 -07001930 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1931 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001932
Cong Wange4c7ed42013-08-31 13:44:33 +08001933 if (dst->sa.sa_family == AF_INET) {
1934 memset(&fl4, 0, sizeof(fl4));
1935 fl4.flowi4_oif = rdst->remote_ifindex;
1936 fl4.flowi4_tos = RT_TOS(tos);
1937 fl4.daddr = dst->sin.sin_addr.s_addr;
1938 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001939
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001940 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001941 if (IS_ERR(rt)) {
1942 netdev_dbg(dev, "no route to %pI4\n",
1943 &dst->sin.sin_addr.s_addr);
1944 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001945 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001946 }
1947
1948 if (rt->dst.dev == dev) {
1949 netdev_dbg(dev, "circular route to %pI4\n",
1950 &dst->sin.sin_addr.s_addr);
1951 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001952 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001953 }
1954
1955 /* Bypass encapsulation if the destination is local */
1956 if (rt->rt_flags & RTCF_LOCAL &&
1957 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1958 struct vxlan_dev *dst_vxlan;
1959
1960 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001961 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01001962 dst->sa.sa_family, dst_port,
1963 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08001964 if (!dst_vxlan)
1965 goto tx_error;
1966 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1967 return;
1968 }
1969
1970 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1971 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Thomas Graf35114942015-01-15 03:53:55 +01001972 md.vni = htonl(vni << 8);
1973 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08001974
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001975 err = vxlan_xmit_skb(rt, skb, fl4.saddr,
1976 dst->sin.sin_addr.s_addr, tos, ttl, df,
1977 src_port, dst_port, &md,
1978 !net_eq(vxlan->net, dev_net(vxlan->dev)),
1979 vxlan->flags);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001980 if (err < 0) {
1981 /* skb is already freed. */
1982 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001983 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001984 }
1985
Cong Wange4c7ed42013-08-31 13:44:33 +08001986 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1987#if IS_ENABLED(CONFIG_IPV6)
1988 } else {
1989 struct sock *sk = vxlan->vn_sock->sock->sk;
1990 struct dst_entry *ndst;
1991 struct flowi6 fl6;
1992 u32 flags;
1993
1994 memset(&fl6, 0, sizeof(fl6));
1995 fl6.flowi6_oif = rdst->remote_ifindex;
1996 fl6.daddr = dst->sin6.sin6_addr;
1997 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08001998 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001999
2000 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2001 netdev_dbg(dev, "no route to %pI6\n",
2002 &dst->sin6.sin6_addr);
2003 dev->stats.tx_carrier_errors++;
2004 goto tx_error;
2005 }
2006
2007 if (ndst->dev == dev) {
2008 netdev_dbg(dev, "circular route to %pI6\n",
2009 &dst->sin6.sin6_addr);
2010 dst_release(ndst);
2011 dev->stats.collisions++;
2012 goto tx_error;
2013 }
2014
2015 /* Bypass encapsulation if the destination is local */
2016 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2017 if (flags & RTF_LOCAL &&
2018 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2019 struct vxlan_dev *dst_vxlan;
2020
2021 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002022 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002023 dst->sa.sa_family, dst_port,
2024 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002025 if (!dst_vxlan)
2026 goto tx_error;
2027 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2028 return;
2029 }
2030
2031 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Thomas Graf35114942015-01-15 03:53:55 +01002032 md.vni = htonl(vni << 8);
2033 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08002034
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002035 err = vxlan6_xmit_skb(ndst, skb, dev, &fl6.saddr, &fl6.daddr,
2036 0, ttl, src_port, dst_port, &md,
2037 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2038 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002039#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002040 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002041
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002042 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002043
2044drop:
2045 dev->stats.tx_dropped++;
2046 goto tx_free;
2047
Pravin B Shelar49560532013-08-19 11:23:17 -07002048rt_tx_error:
2049 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002050tx_error:
2051 dev->stats.tx_errors++;
2052tx_free:
2053 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002054}
2055
David Stevens66817122013-03-15 04:35:51 +00002056/* Transmit local packets over Vxlan
2057 *
2058 * Outer IP header inherits ECN and DF from inner header.
2059 * Outer UDP destination is the VXLAN assigned port.
2060 * source port is based on hash of flow
2061 */
2062static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2063{
2064 struct vxlan_dev *vxlan = netdev_priv(dev);
2065 struct ethhdr *eth;
2066 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002067 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002068 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002069
2070 skb_reset_mac_header(skb);
2071 eth = eth_hdr(skb);
2072
Cong Wangf564f452013-08-31 13:44:36 +08002073 if ((vxlan->flags & VXLAN_F_PROXY)) {
2074 if (ntohs(eth->h_proto) == ETH_P_ARP)
2075 return arp_reduce(dev, skb);
2076#if IS_ENABLED(CONFIG_IPV6)
2077 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002078 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2079 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002080 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2081 struct nd_msg *msg;
2082
2083 msg = (struct nd_msg *)skb_transport_header(skb);
2084 if (msg->icmph.icmp6_code == 0 &&
2085 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2086 return neigh_reduce(dev, skb);
2087 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002088 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002089#endif
2090 }
David Stevens66817122013-03-15 04:35:51 +00002091
2092 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002093 did_rsc = false;
2094
2095 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002096 (ntohs(eth->h_proto) == ETH_P_IP ||
2097 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002098 did_rsc = route_shortcircuit(dev, skb);
2099 if (did_rsc)
2100 f = vxlan_find_mac(vxlan, eth->h_dest);
2101 }
2102
David Stevens66817122013-03-15 04:35:51 +00002103 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002104 f = vxlan_find_mac(vxlan, all_zeros_mac);
2105 if (f == NULL) {
2106 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2107 !is_multicast_ether_addr(eth->h_dest))
2108 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002109
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002110 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002111 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002112 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002113 }
David Stevens66817122013-03-15 04:35:51 +00002114 }
2115
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002116 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2117 struct sk_buff *skb1;
2118
Eric Dumazet8f646c92014-01-06 09:54:31 -08002119 if (!fdst) {
2120 fdst = rdst;
2121 continue;
2122 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002123 skb1 = skb_clone(skb, GFP_ATOMIC);
2124 if (skb1)
2125 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2126 }
2127
Eric Dumazet8f646c92014-01-06 09:54:31 -08002128 if (fdst)
2129 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2130 else
2131 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002132 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002133}
2134
stephen hemmingerd3428942012-10-01 12:32:35 +00002135/* Walk the forwarding table and purge stale entries */
2136static void vxlan_cleanup(unsigned long arg)
2137{
2138 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2139 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2140 unsigned int h;
2141
2142 if (!netif_running(vxlan->dev))
2143 return;
2144
2145 spin_lock_bh(&vxlan->hash_lock);
2146 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2147 struct hlist_node *p, *n;
2148 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2149 struct vxlan_fdb *f
2150 = container_of(p, struct vxlan_fdb, hlist);
2151 unsigned long timeout;
2152
stephen hemminger3c172862012-10-26 06:24:34 +00002153 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002154 continue;
2155
2156 timeout = f->used + vxlan->age_interval * HZ;
2157 if (time_before_eq(timeout, jiffies)) {
2158 netdev_dbg(vxlan->dev,
2159 "garbage collect %pM\n",
2160 f->eth_addr);
2161 f->state = NUD_STALE;
2162 vxlan_fdb_destroy(vxlan, f);
2163 } else if (time_before(timeout, next_timer))
2164 next_timer = timeout;
2165 }
2166 }
2167 spin_unlock_bh(&vxlan->hash_lock);
2168
2169 mod_timer(&vxlan->age_timer, next_timer);
2170}
2171
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002172static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2173{
2174 __u32 vni = vxlan->default_dst.remote_vni;
2175
2176 vxlan->vn_sock = vs;
2177 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2178}
2179
stephen hemmingerd3428942012-10-01 12:32:35 +00002180/* Setup stats when device is created */
2181static int vxlan_init(struct net_device *dev)
2182{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002183 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002184 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002185 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002186 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002187
WANG Cong1c213bd2014-02-13 11:46:28 -08002188 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002189 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002190 return -ENOMEM;
2191
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002192 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002193 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002194 vxlan->dst_port, vxlan->flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002195 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002196 /* If we have a socket with same port already, reuse it */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002197 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002198 } else {
2199 /* otherwise make new socket outside of RTNL */
2200 dev_hold(dev);
2201 queue_work(vxlan_wq, &vxlan->sock_work);
2202 }
2203 spin_unlock(&vn->sock_lock);
2204
stephen hemmingerd3428942012-10-01 12:32:35 +00002205 return 0;
2206}
2207
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002208static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002209{
2210 struct vxlan_fdb *f;
2211
2212 spin_lock_bh(&vxlan->hash_lock);
2213 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2214 if (f)
2215 vxlan_fdb_destroy(vxlan, f);
2216 spin_unlock_bh(&vxlan->hash_lock);
2217}
2218
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002219static void vxlan_uninit(struct net_device *dev)
2220{
2221 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002222 struct vxlan_sock *vs = vxlan->vn_sock;
2223
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002224 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002225
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002226 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07002227 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002228 free_percpu(dev->tstats);
2229}
2230
stephen hemmingerd3428942012-10-01 12:32:35 +00002231/* Start ageing timer and join group when device is brought up */
2232static int vxlan_open(struct net_device *dev)
2233{
2234 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002235 struct vxlan_sock *vs = vxlan->vn_sock;
2236
2237 /* socket hasn't been created */
2238 if (!vs)
2239 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002240
Gao feng79d4a942013-12-10 16:37:32 +08002241 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002242 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002243 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002244 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00002245 }
2246
2247 if (vxlan->age_interval)
2248 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2249
2250 return 0;
2251}
2252
2253/* Purge the forwarding table */
2254static void vxlan_flush(struct vxlan_dev *vxlan)
2255{
Cong Wang31fec5a2013-05-27 22:35:52 +00002256 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002257
2258 spin_lock_bh(&vxlan->hash_lock);
2259 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2260 struct hlist_node *p, *n;
2261 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2262 struct vxlan_fdb *f
2263 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002264 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2265 if (!is_zero_ether_addr(f->eth_addr))
2266 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002267 }
2268 }
2269 spin_unlock_bh(&vxlan->hash_lock);
2270}
2271
2272/* Cleanup timer and forwarding table on shutdown */
2273static int vxlan_stop(struct net_device *dev)
2274{
2275 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002276 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002277 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00002278
Cong Wange4c7ed42013-08-31 13:44:33 +08002279 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002280 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002281 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002282 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002283 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002284 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002285
2286 del_timer_sync(&vxlan->age_timer);
2287
2288 vxlan_flush(vxlan);
2289
2290 return 0;
2291}
2292
stephen hemmingerd3428942012-10-01 12:32:35 +00002293/* Stub, nothing needs to be done. */
2294static void vxlan_set_multicast_list(struct net_device *dev)
2295{
2296}
2297
Daniel Borkmann345010b2013-12-18 00:21:08 +01002298static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2299{
2300 struct vxlan_dev *vxlan = netdev_priv(dev);
2301 struct vxlan_rdst *dst = &vxlan->default_dst;
2302 struct net_device *lowerdev;
2303 int max_mtu;
2304
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002305 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002306 if (lowerdev == NULL)
2307 return eth_change_mtu(dev, new_mtu);
2308
2309 if (dst->remote_ip.sa.sa_family == AF_INET6)
2310 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2311 else
2312 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2313
2314 if (new_mtu < 68 || new_mtu > max_mtu)
2315 return -EINVAL;
2316
2317 dev->mtu = new_mtu;
2318 return 0;
2319}
2320
stephen hemmingerd3428942012-10-01 12:32:35 +00002321static const struct net_device_ops vxlan_netdev_ops = {
2322 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002323 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002324 .ndo_open = vxlan_open,
2325 .ndo_stop = vxlan_stop,
2326 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002327 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002328 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002329 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002330 .ndo_validate_addr = eth_validate_addr,
2331 .ndo_set_mac_address = eth_mac_addr,
2332 .ndo_fdb_add = vxlan_fdb_add,
2333 .ndo_fdb_del = vxlan_fdb_delete,
2334 .ndo_fdb_dump = vxlan_fdb_dump,
2335};
2336
2337/* Info for udev, that this is a virtual tunnel endpoint */
2338static struct device_type vxlan_type = {
2339 .name = "vxlan",
2340};
2341
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002342/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002343 * supply the listening VXLAN udp ports. Callers are expected
2344 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002345 */
2346void vxlan_get_rx_port(struct net_device *dev)
2347{
2348 struct vxlan_sock *vs;
2349 struct net *net = dev_net(dev);
2350 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2351 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002352 __be16 port;
2353 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002354
2355 spin_lock(&vn->sock_lock);
2356 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002357 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2358 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002359 sa_family = vs->sock->sk->sk_family;
2360 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2361 port);
2362 }
2363 }
2364 spin_unlock(&vn->sock_lock);
2365}
2366EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2367
stephen hemmingerd3428942012-10-01 12:32:35 +00002368/* Initialize the device structure. */
2369static void vxlan_setup(struct net_device *dev)
2370{
2371 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002372 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002373
2374 eth_hw_addr_random(dev);
2375 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002376 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002377 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002378 else
Cong Wang2853af62014-06-12 11:53:10 -07002379 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002380
2381 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002382 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002383 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2384
2385 dev->tx_queue_len = 0;
2386 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002387 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002388 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002389 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002390
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002391 dev->vlan_features = dev->features;
2392 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002393 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002394 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002395 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002396 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002397 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002398
stephen hemminger553675f2013-05-16 11:35:20 +00002399 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002400 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002401 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2402 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002403 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002404
2405 init_timer_deferrable(&vxlan->age_timer);
2406 vxlan->age_timer.function = vxlan_cleanup;
2407 vxlan->age_timer.data = (unsigned long) vxlan;
2408
stephen hemminger823aa872013-04-27 11:31:57 +00002409 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002410
stephen hemmingerd3428942012-10-01 12:32:35 +00002411 vxlan->dev = dev;
2412
2413 for (h = 0; h < FDB_HASH_SIZE; ++h)
2414 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2415}
2416
2417static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2418 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002419 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002420 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002421 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2422 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002423 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002424 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2425 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2426 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2427 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2428 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002429 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002430 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2431 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2432 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2433 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002434 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002435 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2436 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2437 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002438 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2439 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002440 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
stephen hemmingerd3428942012-10-01 12:32:35 +00002441};
2442
2443static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2444{
2445 if (tb[IFLA_ADDRESS]) {
2446 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2447 pr_debug("invalid link address (not ethernet)\n");
2448 return -EINVAL;
2449 }
2450
2451 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2452 pr_debug("invalid all zero ethernet address\n");
2453 return -EADDRNOTAVAIL;
2454 }
2455 }
2456
2457 if (!data)
2458 return -EINVAL;
2459
2460 if (data[IFLA_VXLAN_ID]) {
2461 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2462 if (id >= VXLAN_VID_MASK)
2463 return -ERANGE;
2464 }
2465
stephen hemminger05f47d62012-10-09 20:35:50 +00002466 if (data[IFLA_VXLAN_PORT_RANGE]) {
2467 const struct ifla_vxlan_port_range *p
2468 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2469
2470 if (ntohs(p->high) < ntohs(p->low)) {
2471 pr_debug("port range %u .. %u not valid\n",
2472 ntohs(p->low), ntohs(p->high));
2473 return -EINVAL;
2474 }
2475 }
2476
stephen hemmingerd3428942012-10-01 12:32:35 +00002477 return 0;
2478}
2479
Yan Burman1b13c972013-01-29 23:43:07 +00002480static void vxlan_get_drvinfo(struct net_device *netdev,
2481 struct ethtool_drvinfo *drvinfo)
2482{
2483 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2484 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2485}
2486
2487static const struct ethtool_ops vxlan_ethtool_ops = {
2488 .get_drvinfo = vxlan_get_drvinfo,
2489 .get_link = ethtool_op_get_link,
2490};
2491
stephen hemminger553675f2013-05-16 11:35:20 +00002492static void vxlan_del_work(struct work_struct *work)
2493{
2494 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002495 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002496 kfree_rcu(vs, rcu);
2497}
2498
Tom Herbert3ee64f32014-07-13 19:49:42 -07002499static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2500 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002501{
Cong Wange4c7ed42013-08-31 13:44:33 +08002502 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002503 struct udp_port_cfg udp_conf;
2504 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002505
Tom Herbert3ee64f32014-07-13 19:49:42 -07002506 memset(&udp_conf, 0, sizeof(udp_conf));
2507
2508 if (ipv6) {
2509 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002510 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002511 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002512 } else {
2513 udp_conf.family = AF_INET;
2514 udp_conf.local_ip.s_addr = INADDR_ANY;
Cong Wange4c7ed42013-08-31 13:44:33 +08002515 }
2516
Tom Herbert3ee64f32014-07-13 19:49:42 -07002517 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002518
Tom Herbert3ee64f32014-07-13 19:49:42 -07002519 /* Open UDP socket */
2520 err = udp_sock_create(net, &udp_conf, &sock);
2521 if (err < 0)
2522 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002523
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002524 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002525}
2526
2527/* Create new listen socket if needed */
2528static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002529 vxlan_rcv_t *rcv, void *data,
2530 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002531{
2532 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2533 struct vxlan_sock *vs;
2534 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002535 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002536 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002537 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002538
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002539 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002540 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002541 return ERR_PTR(-ENOMEM);
2542
2543 for (h = 0; h < VNI_HASH_SIZE; ++h)
2544 INIT_HLIST_HEAD(&vs->vni_list[h]);
2545
2546 INIT_WORK(&vs->del_work, vxlan_del_work);
2547
Tom Herbert3ee64f32014-07-13 19:49:42 -07002548 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002549 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002550 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002551 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002552 }
2553
Cong Wange4c7ed42013-08-31 13:44:33 +08002554 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002555 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002556 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002557 vs->data = data;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002558 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002559
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002560 /* Initialize the vxlan udp offloads structure */
2561 vs->udp_offloads.port = port;
2562 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2563 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2564
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002565 spin_lock(&vn->sock_lock);
2566 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002567 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002568 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002569
2570 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002571 tunnel_cfg.sk_user_data = vs;
2572 tunnel_cfg.encap_type = 1;
2573 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2574 tunnel_cfg.encap_destroy = NULL;
2575
2576 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002577
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002578 return vs;
2579}
stephen hemminger553675f2013-05-16 11:35:20 +00002580
Pravin B Shelar012a5722013-08-19 11:23:07 -07002581struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2582 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002583 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002584{
2585 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2586 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002587 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002588
Tom Herbert359a0ea2014-06-04 17:20:29 -07002589 vs = vxlan_socket_create(net, port, rcv, data, flags);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002590 if (!IS_ERR(vs))
2591 return vs;
2592
Pravin B Shelar012a5722013-08-19 11:23:07 -07002593 if (no_share) /* Return error if sharing is not allowed. */
2594 return vs;
2595
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002596 spin_lock(&vn->sock_lock);
Thomas Grafac5132d2015-01-15 03:53:56 +01002597 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port, flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002598 if (vs && ((vs->rcv != rcv) ||
2599 !atomic_add_unless(&vs->refcnt, 1, 0)))
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002600 vs = ERR_PTR(-EBUSY);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002601 spin_unlock(&vn->sock_lock);
2602
2603 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002604 vs = ERR_PTR(-EINVAL);
2605
stephen hemminger553675f2013-05-16 11:35:20 +00002606 return vs;
2607}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002608EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002609
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002610/* Scheduled at device creation to bind to a socket */
2611static void vxlan_sock_work(struct work_struct *work)
2612{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002613 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002614 struct net *net = vxlan->net;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002615 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002616 __be16 port = vxlan->dst_port;
2617 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002618
Tom Herbert359a0ea2014-06-04 17:20:29 -07002619 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002620 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002621 if (!IS_ERR(nvs))
2622 vxlan_vs_add_dev(nvs, vxlan);
2623 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002624
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002625 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002626}
2627
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002628static int vxlan_newlink(struct net *src_net, struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +00002629 struct nlattr *tb[], struct nlattr *data[])
2630{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002631 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002632 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002633 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002634 __u32 vni;
2635 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002636 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002637
2638 if (!data[IFLA_VXLAN_ID])
2639 return -EINVAL;
2640
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002641 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002642
stephen hemmingerd3428942012-10-01 12:32:35 +00002643 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002644 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002645
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002646 /* Unless IPv6 is explicitly requested, assume IPv4 */
2647 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002648 if (data[IFLA_VXLAN_GROUP]) {
2649 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002650 } else if (data[IFLA_VXLAN_GROUP6]) {
2651 if (!IS_ENABLED(CONFIG_IPV6))
2652 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002653
Cong Wange4c7ed42013-08-31 13:44:33 +08002654 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2655 sizeof(struct in6_addr));
2656 dst->remote_ip.sa.sa_family = AF_INET6;
2657 use_ipv6 = true;
2658 }
2659
2660 if (data[IFLA_VXLAN_LOCAL]) {
2661 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2662 vxlan->saddr.sa.sa_family = AF_INET;
2663 } else if (data[IFLA_VXLAN_LOCAL6]) {
2664 if (!IS_ENABLED(CONFIG_IPV6))
2665 return -EPFNOSUPPORT;
2666
2667 /* TODO: respect scope id */
2668 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2669 sizeof(struct in6_addr));
2670 vxlan->saddr.sa.sa_family = AF_INET6;
2671 use_ipv6 = true;
2672 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002673
stephen hemminger34e02aa2012-10-09 20:35:53 +00002674 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002675 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002676 struct net_device *lowerdev
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002677 = __dev_get_by_index(src_net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002678
stephen hemminger34e02aa2012-10-09 20:35:53 +00002679 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002680 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002681 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002682 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002683
Cong Wange4c7ed42013-08-31 13:44:33 +08002684#if IS_ENABLED(CONFIG_IPV6)
2685 if (use_ipv6) {
2686 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2687 if (idev && idev->cnf.disable_ipv6) {
2688 pr_info("IPv6 is disabled via sysctl\n");
2689 return -EPERM;
2690 }
2691 vxlan->flags |= VXLAN_F_IPV6;
2692 }
2693#endif
2694
stephen hemminger34e02aa2012-10-09 20:35:53 +00002695 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002696 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002697
Cong Wang2853af62014-06-12 11:53:10 -07002698 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002699 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002700 } else if (use_ipv6)
2701 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002702
2703 if (data[IFLA_VXLAN_TOS])
2704 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2705
Vincent Bernatafb97182012-10-30 10:27:16 +00002706 if (data[IFLA_VXLAN_TTL])
2707 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2708
stephen hemmingerd3428942012-10-01 12:32:35 +00002709 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002710 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002711
2712 if (data[IFLA_VXLAN_AGEING])
2713 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2714 else
2715 vxlan->age_interval = FDB_AGE_DEFAULT;
2716
David Stevense4f67ad2012-11-20 02:50:14 +00002717 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2718 vxlan->flags |= VXLAN_F_PROXY;
2719
2720 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2721 vxlan->flags |= VXLAN_F_RSC;
2722
2723 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2724 vxlan->flags |= VXLAN_F_L2MISS;
2725
2726 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2727 vxlan->flags |= VXLAN_F_L3MISS;
2728
stephen hemmingerd3428942012-10-01 12:32:35 +00002729 if (data[IFLA_VXLAN_LIMIT])
2730 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2731
stephen hemminger05f47d62012-10-09 20:35:50 +00002732 if (data[IFLA_VXLAN_PORT_RANGE]) {
2733 const struct ifla_vxlan_port_range *p
2734 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2735 vxlan->port_min = ntohs(p->low);
2736 vxlan->port_max = ntohs(p->high);
2737 }
2738
stephen hemminger823aa872013-04-27 11:31:57 +00002739 if (data[IFLA_VXLAN_PORT])
2740 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2741
Tom Herbert359a0ea2014-06-04 17:20:29 -07002742 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2743 vxlan->flags |= VXLAN_F_UDP_CSUM;
2744
2745 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2746 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2747 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2748
2749 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2750 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2751 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2752
Tom Herbertdfd86452015-01-12 17:00:38 -08002753 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2754 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2755 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2756
2757 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2758 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2759 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2760
Thomas Graf35114942015-01-15 03:53:55 +01002761 if (data[IFLA_VXLAN_GBP])
2762 vxlan->flags |= VXLAN_F_GBP;
2763
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002764 if (vxlan_find_vni(src_net, vni, use_ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002765 vxlan->dst_port, vxlan->flags)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002766 pr_info("duplicate VNI %u\n", vni);
2767 return -EEXIST;
2768 }
2769
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002770 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002771
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002772 /* create an fdb entry for a valid default destination */
2773 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2774 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2775 &vxlan->default_dst.remote_ip,
2776 NUD_REACHABLE|NUD_PERMANENT,
2777 NLM_F_EXCL|NLM_F_CREATE,
2778 vxlan->dst_port,
2779 vxlan->default_dst.remote_vni,
2780 vxlan->default_dst.remote_ifindex,
2781 NTF_SELF);
2782 if (err)
2783 return err;
2784 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002785
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002786 err = register_netdevice(dev);
2787 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002788 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002789 return err;
2790 }
2791
stephen hemminger553675f2013-05-16 11:35:20 +00002792 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002793
2794 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002795}
2796
2797static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2798{
2799 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002800 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002801
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002802 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002803 if (!hlist_unhashed(&vxlan->hlist))
2804 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002805 spin_unlock(&vn->sock_lock);
2806
stephen hemminger553675f2013-05-16 11:35:20 +00002807 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002808 unregister_netdevice_queue(dev, head);
2809}
2810
2811static size_t vxlan_get_size(const struct net_device *dev)
2812{
2813
2814 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002815 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002816 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002817 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002818 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2819 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2820 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002821 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2822 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2823 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2824 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002825 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2826 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002827 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002828 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2829 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2830 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2831 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002832 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2833 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002834 0;
2835}
2836
2837static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2838{
2839 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002840 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002841 struct ifla_vxlan_port_range ports = {
2842 .low = htons(vxlan->port_min),
2843 .high = htons(vxlan->port_max),
2844 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002845
Atzm Watanabec7995c42013-04-16 02:50:52 +00002846 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002847 goto nla_put_failure;
2848
Cong Wange4c7ed42013-08-31 13:44:33 +08002849 if (!vxlan_addr_any(&dst->remote_ip)) {
2850 if (dst->remote_ip.sa.sa_family == AF_INET) {
2851 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2852 dst->remote_ip.sin.sin_addr.s_addr))
2853 goto nla_put_failure;
2854#if IS_ENABLED(CONFIG_IPV6)
2855 } else {
2856 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2857 &dst->remote_ip.sin6.sin6_addr))
2858 goto nla_put_failure;
2859#endif
2860 }
2861 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002862
Atzm Watanabec7995c42013-04-16 02:50:52 +00002863 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002864 goto nla_put_failure;
2865
Cong Wange4c7ed42013-08-31 13:44:33 +08002866 if (!vxlan_addr_any(&vxlan->saddr)) {
2867 if (vxlan->saddr.sa.sa_family == AF_INET) {
2868 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2869 vxlan->saddr.sin.sin_addr.s_addr))
2870 goto nla_put_failure;
2871#if IS_ENABLED(CONFIG_IPV6)
2872 } else {
2873 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2874 &vxlan->saddr.sin6.sin6_addr))
2875 goto nla_put_failure;
2876#endif
2877 }
2878 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002879
2880 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2881 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002882 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2883 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2884 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2885 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2886 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2887 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2888 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2889 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2890 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002891 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002892 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002893 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2894 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2895 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2896 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2897 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2898 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002899 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2900 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2901 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2902 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2903 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002904 goto nla_put_failure;
2905
stephen hemminger05f47d62012-10-09 20:35:50 +00002906 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2907 goto nla_put_failure;
2908
Thomas Graf35114942015-01-15 03:53:55 +01002909 if (vxlan->flags & VXLAN_F_GBP &&
2910 nla_put_flag(skb, IFLA_VXLAN_GBP))
2911 goto nla_put_failure;
2912
stephen hemmingerd3428942012-10-01 12:32:35 +00002913 return 0;
2914
2915nla_put_failure:
2916 return -EMSGSIZE;
2917}
2918
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002919static struct net *vxlan_get_link_net(const struct net_device *dev)
2920{
2921 struct vxlan_dev *vxlan = netdev_priv(dev);
2922
2923 return vxlan->net;
2924}
2925
stephen hemmingerd3428942012-10-01 12:32:35 +00002926static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2927 .kind = "vxlan",
2928 .maxtype = IFLA_VXLAN_MAX,
2929 .policy = vxlan_policy,
2930 .priv_size = sizeof(struct vxlan_dev),
2931 .setup = vxlan_setup,
2932 .validate = vxlan_validate,
2933 .newlink = vxlan_newlink,
2934 .dellink = vxlan_dellink,
2935 .get_size = vxlan_get_size,
2936 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002937 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002938};
2939
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002940static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2941 struct net_device *dev)
2942{
2943 struct vxlan_dev *vxlan, *next;
2944 LIST_HEAD(list_kill);
2945
2946 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2947 struct vxlan_rdst *dst = &vxlan->default_dst;
2948
2949 /* In case we created vxlan device with carrier
2950 * and we loose the carrier due to module unload
2951 * we also need to remove vxlan device. In other
2952 * cases, it's not necessary and remote_ifindex
2953 * is 0 here, so no matches.
2954 */
2955 if (dst->remote_ifindex == dev->ifindex)
2956 vxlan_dellink(vxlan->dev, &list_kill);
2957 }
2958
2959 unregister_netdevice_many(&list_kill);
2960}
2961
2962static int vxlan_lowerdev_event(struct notifier_block *unused,
2963 unsigned long event, void *ptr)
2964{
2965 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002966 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002967
Daniel Borkmann783c1462014-01-22 21:07:53 +01002968 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002969 vxlan_handle_lowerdev_unregister(vn, dev);
2970
2971 return NOTIFY_DONE;
2972}
2973
2974static struct notifier_block vxlan_notifier_block __read_mostly = {
2975 .notifier_call = vxlan_lowerdev_event,
2976};
2977
stephen hemmingerd3428942012-10-01 12:32:35 +00002978static __net_init int vxlan_init_net(struct net *net)
2979{
2980 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002981 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002982
stephen hemminger553675f2013-05-16 11:35:20 +00002983 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002984 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002985
stephen hemminger553675f2013-05-16 11:35:20 +00002986 for (h = 0; h < PORT_HASH_SIZE; ++h)
2987 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002988
2989 return 0;
2990}
2991
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002992static void __net_exit vxlan_exit_net(struct net *net)
2993{
2994 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2995 struct vxlan_dev *vxlan, *next;
2996 struct net_device *dev, *aux;
2997 LIST_HEAD(list);
2998
2999 rtnl_lock();
3000 for_each_netdev_safe(net, dev, aux)
3001 if (dev->rtnl_link_ops == &vxlan_link_ops)
3002 unregister_netdevice_queue(dev, &list);
3003
3004 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3005 /* If vxlan->dev is in the same netns, it has already been added
3006 * to the list by the previous loop.
3007 */
3008 if (!net_eq(dev_net(vxlan->dev), net))
3009 unregister_netdevice_queue(dev, &list);
3010 }
3011
3012 unregister_netdevice_many(&list);
3013 rtnl_unlock();
3014}
3015
stephen hemmingerd3428942012-10-01 12:32:35 +00003016static struct pernet_operations vxlan_net_ops = {
3017 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003018 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003019 .id = &vxlan_net_id,
3020 .size = sizeof(struct vxlan_net),
3021};
3022
3023static int __init vxlan_init_module(void)
3024{
3025 int rc;
3026
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003027 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3028 if (!vxlan_wq)
3029 return -ENOMEM;
3030
stephen hemmingerd3428942012-10-01 12:32:35 +00003031 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3032
Daniel Borkmann783c1462014-01-22 21:07:53 +01003033 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003034 if (rc)
3035 goto out1;
3036
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003037 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003038 if (rc)
3039 goto out2;
3040
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003041 rc = rtnl_link_register(&vxlan_link_ops);
3042 if (rc)
3043 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003044
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003045 return 0;
3046out3:
3047 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003048out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003049 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003050out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003051 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00003052 return rc;
3053}
Cong Wang7332a132013-05-27 22:35:53 +00003054late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003055
3056static void __exit vxlan_cleanup_module(void)
3057{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003058 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003059 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003060 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003061 unregister_pernet_subsys(&vxlan_net_ops);
3062 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003063}
3064module_exit(vxlan_cleanup_module);
3065
3066MODULE_LICENSE("GPL");
3067MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003068MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003069MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003070MODULE_ALIAS_RTNL_LINK("vxlan");